📄 tinytcp.c
字号:
/*-----------------------------------------------------------------------------*//* *//* Copyright (C) 2001-2003 by Texas Instruments, Inc. All rights reserved. *//* Copyright (C) 2001-2003 Telogy Networks, Inc. *//* *//* NOTE: THIS VERSION OF CODE IS MAINTAINED BY TELOGY NETWORKS AND NOT TI! *//* *//* IMPORTANT - READ CAREFULLY BEFORE PROCEEDING TO USE SOFTWARE. *//* *//* This document is displayed for you to read prior to using the software *//* and documentation. By using the software and documentation, or opening *//* the sealed packet containing the software, or proceeding to download the *//* software from a Bulletin Board System(BBS) or a WEB Server, you agree to *//* abide by the following Texas Instruments License Agreement. If you choose *//* not to agree with these provisions, promptly discontinue use of the *//* software and documentation and return the material to the place you *//* obtained it. *//* *//* *** NOTE *** *//* *//* The licensed materials contain MIPS Technologies, Inc. confidential *//* information which is protected by the appropriate MIPS Technologies, Inc. *//* license agreement. It is your responsibility to comply with these *//* licenses. *//* *//* Texas Instruments License Agreement *//* *//* 1. License - Texas Instruments (hereinafter "TI"), grants you a license *//* to use the software program and documentation in this package ("Licensed *//* Materials") for Texas Instruments broadband products. *//* *//* 2. Restrictions - You may not reverse-assemble or reverse-compile the *//* Licensed Materials provided in object code or executable format. You may *//* not sublicense, transfer, assign, rent, or lease the Licensed Materials *//* or this Agreement without written permission from TI. *//* *//* 3. Copyright - The Licensed Materials are copyrighted. Accordingly, you *//* may either make one copy of the Licensed Materials for backup and/or *//* archival purposes or copy the Licensed Materials to another medium and *//* keep the original Licensed Materials for backup and/or archival purposes. *//* *//* 4. Runtime and Applications Software - You may create modified or *//* derivative programs of software identified as Runtime Libraries or *//* Applications Software, which, in source code form, remain subject to this *//* Agreement, but object code versions of such derivative programs are not *//* subject to this Agreement. *//* *//* 5. Warranty - TI warrants the media to be free from defects in material *//* and workmanship and that the software will substantially conform to the *//* related documentation for a period of ninety (90) days after the date of *//* your purchase. TI does not warrant that the Licensed Materials will be *//* free from error or will meet your specific requirements. *//* *//* 6. Remedies - If you find defects in the media or that the software does *//* not conform to the enclosed documentation, you may return the Licensed *//* Materials along with the purchase receipt, postage prepaid, to the *//* following address within the warranty period and receive a refund. *//* *//* TEXAS INSTRUMENTS *//* Application Specific Products, MS 8650 *//* c/o ADAM2 Application Manager *//* 12500 TI Boulevard *//* Dallas, TX 75243 - U.S.A. *//* *//* 7. Limitations - TI makes no warranty or condition, either expressed or *//* implied, including, but not limited to, any implied warranties of *//* merchantability and fitness for a particular purpose, regarding the *//* licensed materials. *//* *//* Neither TI nor any applicable licensor will be liable for any indirect, *//* incidental or consequential damages, including but not limited to loss of *//* profits. *//* *//* 8. Term - The license is effective until terminated. You may terminate *//* it at any other time by destroying the program together with all copies, *//* modifications and merged portions in any form. It also will terminate if *//* you fail to comply with any term or condition of this Agreement. *//* *//* 9. Export Control - The re-export of United States origin software and *//* documentation is subject to the U.S. Export Administration Regulations or *//* your equivalent local regulations. Compliance with such regulations is *//* your responsibility. *//* *//* *** IMPORTANT NOTICE *** *//* *//* Texas Instruments (TI) reserves the right to make changes to or to *//* discontinue any semiconductor product or service identified in this *//* publication without notice. TI advises its customers to obtain the latest *//* version of the relevant information to verify, before placing orders, *//* that the information being relied upon is current. *//* *//* TI warrants performance of its semiconductor products and related *//* software to current specifications in accordance with TI's standard *//* warranty. Testing and other quality control techniques are utilized to *//* the extent TI deems necessary to support this warranty. Unless mandated *//* by government requirements, specific testing of all parameters of each *//* device is not necessarily performed. *//* *//* Please be aware that Texas Instruments products are not intended for use *//* in life-support appliances, devices, or systems. Use of a TI product in *//* such applications without the written approval of the appropriate TI *//* officer is prohibited. Certain applications using semiconductor devices *//* may involve potential risks of injury, property damage, or loss of life. *//* In order to minimize these risks, adequate design and operating *//* safeguards should be provided by the customer to minimize inherent or *//* procedural hazards. Inclusion of TI products in such applications is *//* understood to be fully at the risk of the customer using TI devices or *//* systems. *//* *//* TI assumes no liability for TI applications assistance, customer product *//* design, software performance, or infringement of patents or services *//* described herein. Nor does TI warrant or represent that license, either *//* expressed or implied, is granted under any patent right, copyright, mask *//* work right, or other intellectual property right of TI covering or *//* relating to any combination, machine, or process in which such *//* semiconductor products or services might be or are used. *//* *//* All company and/or product names are trademarks and/or registered *//* trademarks of their respective manaufacturers. *//* *//*-----------------------------------------------------------------------------*//* *//* tinytcp.c - Tiny Implementation of the Transmission Control Protocol *//* *//* Written March 28, 1986 by Geoffrey Cooper, IMAGEN Corporation. *//* *//* This code is a small implementation of the TCP and IP protocols, suitable *//* for burning into ROM. The implementation is bare-bones and represents *//* two days' coding efforts. A timer and an ethernet board are assumed. The *//* implementation is based on busy-waiting, but the tcp_handler procedure *//* could easily be integrated into an interrupt driven scheme. *//* *//* IP routing is accomplished on active opens by broadcasting the tcp SYN *//* packet when ARP mapping fails. If anyone answers, the ethernet address *//* used is saved for future use. This also allows IP routing on incoming *//* connections. *//* */ /* The TCP does not implement urgent pointers (easy to add), and discards *//* segments that are received out of order. It ignores the received window *//* and always offers a fixed window size on input (i.e., it is not flow *//* controlled). *//* *//* Special care is taken to access the ethernet buffers only in word *//* mode. This is to support boards that only allow word accesses. *//* *//* Copyright (C) 1986, IMAGEN Corporation *//* "This code may be duplicated in whole or in part provided that [1] there *//* is no commercial gain involved in the duplication, and [2] that this *//* copyright notice is preserved on all copies. Any other duplication *//* requires written notice of the author." *//* *//*-----------------------------------------------------------------------------*/#include "_stdio.h"#include "tinyip.h"tcp_Socket *tcp_allsocs;/* IP identification numbers */static word tcp_id;/* Timer definitions */#define tcp_RETRANSMITTIME 1000 /* interval at which retransmitter is called */#define tcp_LONGTIMEOUT 31000 /* timeout for opens */#define tcp_TIMEOUT 10000 /* timeout during a connection *//* * Initialize the tcp implementation */int tcp_Init() { int ret = 0;#ifndef DHCP_SUPPORT /* initialize ethernet interface */ ret = sed_Init(0);#endif tcp_allsocs = NULL; tcp_id = 0; return ret; }/* * Actively open a TCP connection to a particular destination. */void tcp_Open(tcp_Socket *s, word lport, in_HwAddress ina, word port, procref datahandler) { extern eth_HwAddress sed_ethBcastAddr;#ifdef FTP_SERVER_SUPPORT if ((s->state != 0) && (s->state != tcp_StateCLOSED)) { s->flags = tcp_FlagACK; tcp_Send(s); s->state = tcp_StateCLOSED; s->dataHandler(s, 0, 0,SABORT); tcp_Unthread(s); }#endif s->ip_type = TCP_PROTO; s->state = tcp_StateSYNSENT; s->timeout = tcp_LONGTIMEOUT; if ( lport == 0 ) lport = clock_ValueRough(); s->myport = lport; if ( ! sar_MapIn2Eth(ina, (eth_HwAddress *)&s->hisethaddr[0]) ) { sys_printf("tcp_Open of 0x%x: defaulting ethernet address to broadcast\n", ina); Move(&sed_ethBcastAddr[0], &s->hisethaddr[0], sizeof(eth_HwAddress)); } s->hisaddr = ina; s->hisport = port; s->seqnum = 0; s->dataSize = 0; s->flags = tcp_FlagSYN; s->unhappy = TRUE; s->dataHandler = datahandler; s->next = tcp_allsocs; tcp_allsocs = s; tcp_Send(s); }/* * Passive open: listen for a connection on a particular port */void tcp_Listen(tcp_Socket *s, word port, procref datahandler, int timeout) {#ifdef FTP_SERVER_SUPPORT if ((s->state != 0) && (s->state != tcp_StateCLOSED)) { s->flags = tcp_FlagACK; tcp_Send(s); s->state = tcp_StateCLOSED; s->dataHandler(s, 0, 0,SABORT); tcp_Unthread(s); }#endif s->ip_type = TCP_PROTO; s->state = tcp_StateLISTEN; if ( timeout == 0 ) s->timeout = 0x7ffffff; /* forever... */ else s->timeout = timeout; s->myport = port; s->hisport = 0; s->seqnum = 0; s->dataSize = 0; s->flags = 0; s->unhappy = 0; s->dataHandler = datahandler; s->next = tcp_allsocs; tcp_allsocs = s; }/* * Send a FIN on a particular port -- only works if it is open */void tcp_Close(tcp_Socket *s) { if ( s->state == tcp_StateESTAB || s->state == tcp_StateSYNREC ) { s->flags = tcp_FlagACK | tcp_FlagFIN; s->state = tcp_StateFINWT1; s->unhappy = TRUE; tcp_Send(s); /*TODO*/ } }/* * Abort a tcp connection */void tcp_Abort(tcp_Socket *s) { if ( s->state != tcp_StateLISTEN && s->state != tcp_StateCLOSED ) { s->flags = tcp_FlagRST | tcp_FlagACK; tcp_Send(s); } s->unhappy = 0; s->dataSize = 0; s->state = tcp_StateCLOSED; s->dataHandler(s, 0, -1,SABORT); tcp_Unthread(s); }/* * Unthread a socket from the socket list, if it's there */void tcp_Unthread(tcp_Socket *ds){ tcp_Socket *s, **sp; sp = &tcp_allsocs; for (;;) { s = *sp; if ( s == ds ) { *sp = s->next; break; } if ( s == NULL ) break; sp = &s->next; }}#ifdef FTP_CLIENT_SUPPORT/* * Retransmitter - called periodically to perform tcp retransmissions */void tcp_Retransmitter() { tcp_Socket *s; BOOL x; for ( s = tcp_allsocs; s; s = s->next ) { x = FALSE; if ( s->dataSize > 0 || s->unhappy ) { tcp_Send(s); x = TRUE; } if ( x || s->state != tcp_StateESTAB ) s->timeout -= tcp_RETRANSMITTIME; if ( s->timeout <= 0 ) { if ( s->state == tcp_StateTIMEWT ) { s->state = tcp_StateCLOSED; s->dataHandler(s, 0, 0,SABORT); tcp_Unthread(s); } else { tcp_Abort(s); } } } }#ifndef DHCP_SUPPORT/* * busy-wait loop for tcp. Also calls an "application proc" */int tcp(procref application){ in_Header *ip; longword timeout, start; int x; timeout = clock_ValueRough() + tcp_RETRANSMITTIME; while ( tcp_allsocs ) { start = clock_ValueRough(); ip = (in_Header *)sed_IsPacket(); if ( ip == NULL ) { if ( clock_ValueRough() > timeout ) { tcp_Retransmitter(); timeout = clock_ValueRough() + tcp_RETRANSMITTIME; } application(); continue; } if ( sed_CheckPacket(ip, 0x806) == 1 ) { /* do arp */ sar_CheckPacket((arp_Header *)ip); } else if ( sed_CheckPacket(ip, 0x800) == 1 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -