123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941 |
- #define DEBUG_PRINTF(...)
- #include "uip.h"
- #include "uipopt.h"
- #include "uip_arp.h"
- #if !UIP_CONF_IPV6
- #if UIP_CONF_IPV6
- #include "net/uip-neighbor.h"
- #endif
- #include <string.h>
- #if UIP_FIXEDADDR > 0
- const uip_ipaddr_t uip_hostaddr =
- { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 };
- const uip_ipaddr_t uip_draddr =
- { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 };
- const uip_ipaddr_t uip_netmask =
- { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 };
- #else
- uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask;
- #endif
- const uip_ipaddr_t uip_broadcast_addr =
- #if UIP_CONF_IPV6
- { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
- #else
- { { 0xff, 0xff, 0xff, 0xff } };
- #endif
- const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, } };
- #if UIP_FIXEDETHADDR
- const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
- UIP_ETHADDR1,
- UIP_ETHADDR2,
- UIP_ETHADDR3,
- UIP_ETHADDR4,
- UIP_ETHADDR5}};
- #else
- struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}};
- #endif
- #ifndef UIP_CONF_EXTERNAL_BUFFER
- u8_t uip_buf[UIP_BUFSIZE + 2];
- #endif
- void *uip_appdata;
- void *uip_sappdata;
- #if UIP_URGDATA > 0
- void *uip_urgdata;
- u16_t uip_urglen, uip_surglen;
- #endif
- u16_t uip_len, uip_slen;
-
- u8_t uip_flags;
- struct uip_conn *uip_conn;
- struct uip_conn uip_conns[UIP_CONNS];
-
- u16_t uip_listenports[UIP_LISTENPORTS];
-
- #if UIP_UDP
- struct uip_udp_conn *uip_udp_conn;
- struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
- #endif
- static u16_t ipid;
- void uip_setipid(u16_t id) { ipid = id; }
- static u8_t iss[4];
- #if UIP_ACTIVE_OPEN
- static u16_t lastport;
- #endif
- u8_t uip_acc32[4];
- static u8_t c, opt;
- static u16_t tmp16;
- #define TCP_FIN 0x01
- #define TCP_SYN 0x02
- #define TCP_RST 0x04
- #define TCP_PSH 0x08
- #define TCP_ACK 0x10
- #define TCP_URG 0x20
- #define TCP_CTL 0x3f
- #define TCP_OPT_END 0
- #define TCP_OPT_NOOP 1
- #define TCP_OPT_MSS 2
- #define TCP_OPT_MSS_LEN 4
- #define ICMP_ECHO_REPLY 0
- #define ICMP_ECHO 8
- #define ICMP_DEST_UNREACHABLE 3
- #define ICMP_PORT_UNREACHABLE 3
- #define ICMP6_ECHO_REPLY 129
- #define ICMP6_ECHO 128
- #define ICMP6_NEIGHBOR_SOLICITATION 135
- #define ICMP6_NEIGHBOR_ADVERTISEMENT 136
- #define ICMP6_FLAG_S (1 << 6)
- #define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1
- #define ICMP6_OPTION_TARGET_LINK_ADDRESS 2
- #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
- #define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])
- #define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
- #define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
- #if UIP_STATISTICS == 1
- struct uip_stats uip_stat;
- #define UIP_STAT(s) s
- #else
- #define UIP_STAT(s)
- #endif
- #if UIP_LOGGING == 1
- #include <stdio.h>
- void uip_log(char *msg);
- #define UIP_LOG(m) uip_log(m)
- #else
- #define UIP_LOG(m)
- #endif
- #if ! UIP_ARCH_ADD32
- void
- uip_add32(u8_t *op32, u16_t op16)
- {
- uip_acc32[3] = op32[3] + (op16 & 0xff);
- uip_acc32[2] = op32[2] + (op16 >> 8);
- uip_acc32[1] = op32[1];
- uip_acc32[0] = op32[0];
- if(uip_acc32[2] < (op16 >> 8)) {
- ++uip_acc32[1];
- if(uip_acc32[1] == 0) {
- ++uip_acc32[0];
- }
- }
- if(uip_acc32[3] < (op16 & 0xff)) {
- ++uip_acc32[2];
- if(uip_acc32[2] == 0) {
- ++uip_acc32[1];
- if(uip_acc32[1] == 0) {
- ++uip_acc32[0];
- }
- }
- }
- }
- #endif
- #if ! UIP_ARCH_CHKSUM
- static u16_t
- chksum(u16_t sum, const u8_t *data, u16_t len)
- {
- u16_t t;
- const u8_t *dataptr;
- const u8_t *last_byte;
- dataptr = data;
- last_byte = data + len - 1;
- while(dataptr < last_byte) {
- t = (dataptr[0] << 8) + dataptr[1];
- sum += t;
- if(sum < t) {
- sum++;
- }
- dataptr += 2;
- }
- if(dataptr == last_byte) {
- t = (dataptr[0] << 8) + 0;
- sum += t;
- if(sum < t) {
- sum++;
- }
- }
-
- return sum;
- }
- u16_t
- uip_chksum(u16_t *data, u16_t len)
- {
- return htons(chksum(0, (u8_t *)data, len));
- }
- #ifndef UIP_ARCH_IPCHKSUM
- u16_t
- uip_ipchksum(void)
- {
- u16_t sum;
- sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
- DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum);
- return (sum == 0) ? 0xffff : htons(sum);
- }
- #endif
- static u16_t
- upper_layer_chksum(u8_t proto)
- {
- u16_t upper_layer_len;
- u16_t sum;
- #if UIP_CONF_IPV6
- upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]);
- #else
- upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN;
- #endif
-
-
- sum = upper_layer_len + proto;
-
- sum = chksum(sum, (u8_t *)&BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t));
-
- sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN],
- upper_layer_len);
- return (sum == 0) ? 0xffff : htons(sum);
- }
- #if UIP_CONF_IPV6
- u16_t
- uip_icmp6chksum(void)
- {
- return upper_layer_chksum(UIP_PROTO_ICMP6);
- }
- #endif
- u16_t
- uip_tcpchksum(void)
- {
- return upper_layer_chksum(UIP_PROTO_TCP);
- }
- #if UIP_UDP_CHECKSUMS
- u16_t
- uip_udpchksum(void)
- {
- return upper_layer_chksum(UIP_PROTO_UDP);
- }
- #endif
- #endif
- void
- uip_init(void)
- {
- for(c = 0; c < UIP_LISTENPORTS; ++c) {
- uip_listenports[c] = 0;
- }
- for(c = 0; c < UIP_CONNS; ++c) {
- uip_conns[c].tcpstateflags = UIP_CLOSED;
- }
- #if UIP_ACTIVE_OPEN
- lastport = 1024;
- #endif
- #if UIP_UDP
- for(c = 0; c < UIP_UDP_CONNS; ++c) {
- uip_udp_conns[c].lport = 0;
- }
- #endif
-
- #if UIP_FIXEDADDR == 0
-
- #endif
- }
- #if UIP_ACTIVE_OPEN
- struct uip_conn *
- uip_connect(uip_ipaddr_t *ripaddr, u16_t rport)
- {
- register struct uip_conn *conn, *cconn;
-
- again:
- ++lastport;
- if(lastport >= 32000) {
- lastport = 4096;
- }
-
- for(c = 0; c < UIP_CONNS; ++c) {
- conn = &uip_conns[c];
- if(conn->tcpstateflags != UIP_CLOSED &&
- conn->lport == htons(lastport)) {
- goto again;
- }
- }
- conn = 0;
- for(c = 0; c < UIP_CONNS; ++c) {
- cconn = &uip_conns[c];
- if(cconn->tcpstateflags == UIP_CLOSED) {
- conn = cconn;
- break;
- }
- if(cconn->tcpstateflags == UIP_TIME_WAIT) {
- if(conn == 0 ||
- cconn->timer > conn->timer) {
- conn = cconn;
- }
- }
- }
- if(conn == 0) {
- return 0;
- }
- conn->tcpstateflags = UIP_SYN_SENT;
- conn->snd_nxt[0] = iss[0];
- conn->snd_nxt[1] = iss[1];
- conn->snd_nxt[2] = iss[2];
- conn->snd_nxt[3] = iss[3];
- conn->initialmss = conn->mss = UIP_TCP_MSS;
- conn->len = 1;
- conn->nrtx = 0;
- conn->timer = 1;
- conn->rto = UIP_RTO;
- conn->sa = 0;
- conn->sv = 16;
- conn->lport = htons(lastport);
- conn->rport = rport;
- uip_ipaddr_copy(&conn->ripaddr, ripaddr);
- return conn;
- }
- #endif
- #if UIP_UDP
- struct uip_udp_conn *
- uip_udp_new(const uip_ipaddr_t *ripaddr, u16_t rport)
- {
- register struct uip_udp_conn *conn;
-
- again:
- ++lastport;
- if(lastport >= 32000) {
- lastport = 4096;
- }
- for(c = 0; c < UIP_UDP_CONNS; ++c) {
- if(uip_udp_conns[c].lport == htons(lastport)) {
- goto again;
- }
- }
- conn = 0;
- for(c = 0; c < UIP_UDP_CONNS; ++c) {
- if(uip_udp_conns[c].lport == 0) {
- conn = &uip_udp_conns[c];
- break;
- }
- }
- if(conn == 0) {
- return 0;
- }
- conn->lport = HTONS(lastport);
- conn->rport = rport;
- if(ripaddr == NULL) {
- memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t));
- } else {
- uip_ipaddr_copy(&conn->ripaddr, ripaddr);
- }
- conn->ttl = UIP_TTL;
- return conn;
- }
- #endif
- void
- uip_unlisten(u16_t port)
- {
- for(c = 0; c < UIP_LISTENPORTS; ++c) {
- if(uip_listenports[c] == port) {
- uip_listenports[c] = 0;
- return;
- }
- }
- }
- void
- uip_listen(u16_t port)
- {
- for(c = 0; c < UIP_LISTENPORTS; ++c) {
- if(uip_listenports[c] == 0) {
- uip_listenports[c] = port;
- return;
- }
- }
- }
- #if UIP_REASSEMBLY && !UIP_CONF_IPV6
- #define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
- static u8_t uip_reassbuf[UIP_REASS_BUFSIZE];
- static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
- static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f,
- 0x0f, 0x07, 0x03, 0x01};
- static u16_t uip_reasslen;
- static u8_t uip_reassflags;
- #define UIP_REASS_FLAG_LASTFRAG 0x01
- static u8_t uip_reasstmr;
- #define IP_MF 0x20
- static u8_t
- uip_reass(void)
- {
- u16_t offset, len;
- u16_t i;
-
- if(uip_reasstmr == 0) {
- memcpy(uip_reassbuf, &BUF->vhl, UIP_IPH_LEN);
- uip_reasstmr = UIP_REASS_MAXAGE;
- uip_reassflags = 0;
-
- memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap));
- }
-
- if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] &&
- BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
- BUF->destipaddr[0] == FBUF->destipaddr[0] &&
- BUF->destipaddr[1] == FBUF->destipaddr[1] &&
- BUF->ipid[0] == FBUF->ipid[0] &&
- BUF->ipid[1] == FBUF->ipid[1]) {
- len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
- offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
-
- if(offset > UIP_REASS_BUFSIZE ||
- offset + len > UIP_REASS_BUFSIZE) {
- uip_reasstmr = 0;
- goto nullreturn;
- }
-
- memcpy(&uip_reassbuf[UIP_IPH_LEN + offset],
- (char *)BUF + (int)((BUF->vhl & 0x0f) * 4),
- len);
-
- if(offset / (8 * 8) == (offset + len) / (8 * 8)) {
-
- uip_reassbitmap[offset / (8 * 8)] |=
- bitmap_bits[(offset / 8 ) & 7] &
- ~bitmap_bits[((offset + len) / 8 ) & 7];
- } else {
-
- uip_reassbitmap[offset / (8 * 8)] |=
- bitmap_bits[(offset / 8 ) & 7];
- for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
- uip_reassbitmap[i] = 0xff;
- }
- uip_reassbitmap[(offset + len) / (8 * 8)] |=
- ~bitmap_bits[((offset + len) / 8 ) & 7];
- }
-
- if((BUF->ipoffset[0] & IP_MF) == 0) {
- uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
- uip_reasslen = offset + len;
- }
-
- if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) {
-
- for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) {
- if(uip_reassbitmap[i] != 0xff) {
- goto nullreturn;
- }
- }
-
- if(uip_reassbitmap[uip_reasslen / (8 * 8)] !=
- (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) {
- goto nullreturn;
- }
-
- uip_reasstmr = 0;
- memcpy(BUF, FBUF, uip_reasslen);
-
- BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
- BUF->len[0] = uip_reasslen >> 8;
- BUF->len[1] = uip_reasslen & 0xff;
- BUF->ipchksum = 0;
- BUF->ipchksum = ~(uip_ipchksum());
- return uip_reasslen;
- }
- }
- nullreturn:
- return 0;
- }
- #endif
- static void
- uip_add_rcv_nxt(u16_t n)
- {
- uip_add32(uip_conn->rcv_nxt, n);
- uip_conn->rcv_nxt[0] = uip_acc32[0];
- uip_conn->rcv_nxt[1] = uip_acc32[1];
- uip_conn->rcv_nxt[2] = uip_acc32[2];
- uip_conn->rcv_nxt[3] = uip_acc32[3];
- }
- void
- uip_process(u8_t flag)
- {
- register struct uip_conn *uip_connr = uip_conn;
- #if UIP_UDP
- if(flag == UIP_UDP_SEND_CONN) {
- goto udp_send;
- }
- #endif
- uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
-
- if(flag == UIP_POLL_REQUEST) {
- if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&
- !uip_outstanding(uip_connr)) {
- uip_len = uip_slen = 0;
- uip_flags = UIP_POLL;
- UIP_APPCALL();
- goto appsend;
- }
- goto drop;
-
- } else if(flag == UIP_TIMER) {
- #if UIP_REASSEMBLY
- if(uip_reasstmr != 0) {
- --uip_reasstmr;
- }
- #endif
-
- if(++iss[3] == 0) {
- if(++iss[2] == 0) {
- if(++iss[1] == 0) {
- ++iss[0];
- }
- }
- }
-
- uip_len = 0;
- uip_slen = 0;
-
- if(uip_connr->tcpstateflags == UIP_TIME_WAIT ||
- uip_connr->tcpstateflags == UIP_FIN_WAIT_2) {
- ++(uip_connr->timer);
- if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) {
- uip_connr->tcpstateflags = UIP_CLOSED;
- }
- } else if(uip_connr->tcpstateflags != UIP_CLOSED) {
-
- if(uip_outstanding(uip_connr)) {
- if(uip_connr->timer-- == 0) {
- if(uip_connr->nrtx == UIP_MAXRTX ||
- ((uip_connr->tcpstateflags == UIP_SYN_SENT ||
- uip_connr->tcpstateflags == UIP_SYN_RCVD) &&
- uip_connr->nrtx == UIP_MAXSYNRTX)) {
- uip_connr->tcpstateflags = UIP_CLOSED;
-
- uip_flags = UIP_TIMEDOUT;
- UIP_APPCALL();
-
- BUF->flags = TCP_RST | TCP_ACK;
- goto tcp_send_nodata;
- }
-
- uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4?
- 4:
- uip_connr->nrtx);
- ++(uip_connr->nrtx);
-
- UIP_STAT(++uip_stat.tcp.rexmit);
- switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
- case UIP_SYN_RCVD:
-
- goto tcp_send_synack;
- #if UIP_ACTIVE_OPEN
- case UIP_SYN_SENT:
-
- BUF->flags = 0;
- goto tcp_send_syn;
- #endif
- case UIP_ESTABLISHED:
-
- uip_flags = UIP_REXMIT;
- UIP_APPCALL();
- goto apprexmit;
- case UIP_FIN_WAIT_1:
- case UIP_CLOSING:
- case UIP_LAST_ACK:
-
- goto tcp_send_finack;
- }
- }
- } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) {
-
- uip_len = uip_slen = 0;
- uip_flags = UIP_POLL;
- UIP_APPCALL();
- goto appsend;
- }
- }
- goto drop;
- }
- #if UIP_UDP
- if(flag == UIP_UDP_TIMER) {
- if(uip_udp_conn->lport != 0) {
- uip_conn = NULL;
- uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
- uip_len = uip_slen = 0;
- uip_flags = UIP_POLL;
- UIP_UDP_APPCALL();
- goto udp_send;
- } else {
- goto drop;
- }
- }
- #endif
-
- UIP_STAT(++uip_stat.ip.recv);
-
- #if UIP_CONF_IPV6
-
- if((BUF->vtc & 0xf0) != 0x60) {
- UIP_STAT(++uip_stat.ip.drop);
- UIP_STAT(++uip_stat.ip.vhlerr);
- UIP_LOG("ipv6: invalid version.");
- goto drop;
- }
- #else
-
- if(BUF->vhl != 0x45) {
- UIP_STAT(++uip_stat.ip.drop);
- UIP_STAT(++uip_stat.ip.vhlerr);
- UIP_LOG("ip: invalid version or header length.");
- goto drop;
- }
- #endif
-
- if((BUF->len[0] << 8) + BUF->len[1] <= uip_len) {
- uip_len = (BUF->len[0] << 8) + BUF->len[1];
- #if UIP_CONF_IPV6
- uip_len += 40;
- #endif
- } else {
- UIP_LOG("ip: packet shorter than reported in IP header.");
- goto drop;
- }
- #if !UIP_CONF_IPV6
-
- if((BUF->ipoffset[0] & 0x3f) != 0 ||
- BUF->ipoffset[1] != 0) {
- #if UIP_REASSEMBLY
- uip_len = uip_reass();
- if(uip_len == 0) {
- goto drop;
- }
- #else
- UIP_STAT(++uip_stat.ip.drop);
- UIP_STAT(++uip_stat.ip.fragerr);
- UIP_LOG("ip: fragment dropped.");
- goto drop;
- #endif
- }
- #endif
- if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
-
- #if UIP_PINGADDRCONF && !UIP_CONF_IPV6
- if(BUF->proto == UIP_PROTO_ICMP) {
- UIP_LOG("ip: possible ping config packet received.");
- goto icmp_input;
- } else {
- UIP_LOG("ip: packet dropped since no address assigned.");
- goto drop;
- }
- #endif
- } else {
-
- #if UIP_BROADCAST
- DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum());
- if(BUF->proto == UIP_PROTO_UDP &&
- uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr))
- {
- if (uip_ipaddr_cmp(&BUF->srcipaddr, &uip_all_zeroes_addr))
- uip_ipaddr_copy(&BUF->srcipaddr, &uip_broadcast_addr);
- goto udp_input;
- }
- #endif
-
- #if !UIP_CONF_IPV6
- if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
- UIP_STAT(++uip_stat.ip.drop);
- goto drop;
- }
- #else
-
- if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr) &&
- BUF->destipaddr.u16[0] != HTONS(0xff02)) {
- UIP_STAT(++uip_stat.ip.drop);
- goto drop;
- }
- #endif
- }
- #if !UIP_CONF_IPV6
- if(uip_ipchksum() != 0xffff) {
- UIP_STAT(++uip_stat.ip.drop);
- UIP_STAT(++uip_stat.ip.chkerr);
- UIP_LOG("ip: bad checksum.");
- goto drop;
- }
- #endif
- if(BUF->proto == UIP_PROTO_TCP) {
- goto tcp_input;
- }
- #if UIP_UDP
- if(BUF->proto == UIP_PROTO_UDP) {
- goto udp_input;
- }
- #endif
- #if !UIP_CONF_IPV6
-
- if(BUF->proto != UIP_PROTO_ICMP) {
- UIP_STAT(++uip_stat.ip.drop);
- UIP_STAT(++uip_stat.ip.protoerr);
- UIP_LOG("ip: neither tcp nor icmp.");
- goto drop;
- }
- #if UIP_PINGADDRCONF
- icmp_input:
- #endif
- UIP_STAT(++uip_stat.icmp.recv);
-
- if(ICMPBUF->type != ICMP_ECHO) {
- UIP_STAT(++uip_stat.icmp.drop);
- UIP_STAT(++uip_stat.icmp.typeerr);
- UIP_LOG("icmp: not icmp echo.");
- goto drop;
- }
-
- #if UIP_PINGADDRCONF
- if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
- uip_hostaddr = BUF->destipaddr;
- }
- #endif
- ICMPBUF->type = ICMP_ECHO_REPLY;
- if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
- ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
- } else {
- ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
- }
-
- uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
- uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
- UIP_STAT(++uip_stat.icmp.sent);
- BUF->ttl = UIP_TTL;
- goto ip_send_nolen;
-
- #else
-
- DEBUG_PRINTF("icmp6_input: length %d\n", uip_len);
- if(BUF->proto != UIP_PROTO_ICMP6) {
- UIP_STAT(++uip_stat.ip.drop);
- UIP_STAT(++uip_stat.ip.protoerr);
- UIP_LOG("ip: neither tcp nor icmp6.");
- goto drop;
- }
- UIP_STAT(++uip_stat.icmp.recv);
-
- if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {
- if(uip_ipaddr_cmp(&ICMPBUF->icmp6data, &uip_hostaddr)) {
- if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {
-
- uip_neighbor_add(&ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));
- }
-
- ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT;
- ICMPBUF->flags = ICMP6_FLAG_S;
- ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;
- uip_ipaddr_copy(&ICMPBUF->destipaddr, &ICMPBUF->srcipaddr);
- uip_ipaddr_copy(&ICMPBUF->srcipaddr, &uip_hostaddr);
- ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;
- ICMPBUF->options[1] = 1;
- memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr));
- ICMPBUF->icmpchksum = 0;
- ICMPBUF->icmpchksum = ~uip_icmp6chksum();
- goto send;
- }
- goto drop;
- } else if(ICMPBUF->type == ICMP6_ECHO) {
-
- ICMPBUF->type = ICMP6_ECHO_REPLY;
- uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
- uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
- ICMPBUF->icmpchksum = 0;
- ICMPBUF->icmpchksum = ~uip_icmp6chksum();
- UIP_STAT(++uip_stat.icmp.sent);
- goto send;
- } else {
- DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type);
- UIP_STAT(++uip_stat.icmp.drop);
- UIP_STAT(++uip_stat.icmp.typeerr);
- UIP_LOG("icmp: unknown ICMP message.");
- goto drop;
- }
-
- #endif
- #if UIP_UDP
-
- udp_input:
-
- #if UIP_UDP_CHECKSUMS
- uip_len = uip_len - UIP_IPUDPH_LEN;
- uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
- if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {
- UIP_STAT(++uip_stat.udp.drop);
- UIP_STAT(++uip_stat.udp.chkerr);
- UIP_LOG("udp: bad checksum.");
- goto drop;
- }
- #else
- uip_len = uip_len - UIP_IPUDPH_LEN;
- #endif
-
- for(uip_udp_conn = &uip_udp_conns[0];
- uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
- ++uip_udp_conn) {
-
- if(uip_udp_conn->lport != 0 &&
- UDPBUF->destport == uip_udp_conn->lport &&
- (uip_udp_conn->rport == 0 ||
- UDPBUF->srcport == uip_udp_conn->rport) &&
- (uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) ||
- uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) ||
- uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr))) {
- goto udp_found;
- }
- }
- UIP_LOG("udp: no matching connection found");
- #if UIP_CONF_ICMP_DEST_UNREACH && !UIP_CONF_IPV6
-
- memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);
-
- ICMPBUF->type = ICMP_DEST_UNREACHABLE;
- ICMPBUF->icode = ICMP_PORT_UNREACHABLE;
-
- ICMPBUF->icmpchksum = 0;
- ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36);
-
- uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
-
- uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
-
- uip_len = 36 + UIP_IPH_LEN;
- ICMPBUF->len[0] = 0;
- ICMPBUF->len[1] = (u8_t)uip_len;
- ICMPBUF->ttl = UIP_TTL;
- ICMPBUF->proto = UIP_PROTO_ICMP;
- goto ip_send_nolen;
- #else
- goto drop;
- #endif
- udp_found:
- uip_conn = NULL;
- uip_flags = UIP_NEWDATA;
- uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
- uip_slen = 0;
- UIP_UDP_APPCALL();
- udp_send:
- if(uip_slen == 0) {
- goto drop;
- }
- uip_len = uip_slen + UIP_IPUDPH_LEN;
- #if UIP_CONF_IPV6
-
- BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
- BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
- #else
- BUF->len[0] = (uip_len >> 8);
- BUF->len[1] = (uip_len & 0xff);
- #endif
- BUF->ttl = uip_udp_conn->ttl;
- BUF->proto = UIP_PROTO_UDP;
- UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);
- UDPBUF->udpchksum = 0;
- BUF->srcport = uip_udp_conn->lport;
- BUF->destport = uip_udp_conn->rport;
- uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
- uip_ipaddr_copy(&BUF->destipaddr, &uip_udp_conn->ripaddr);
- uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
- #if UIP_UDP_CHECKSUMS
-
- UDPBUF->udpchksum = ~(uip_udpchksum());
- if(UDPBUF->udpchksum == 0) {
- UDPBUF->udpchksum = 0xffff;
- }
- #endif
- goto ip_send_nolen;
- #endif
-
- tcp_input:
- UIP_STAT(++uip_stat.tcp.recv);
-
- if(uip_tcpchksum() != 0xffff) {
- UIP_STAT(++uip_stat.tcp.drop);
- UIP_STAT(++uip_stat.tcp.chkerr);
- UIP_LOG("tcp: bad checksum.");
- goto drop;
- }
-
-
- for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];
- ++uip_connr) {
- if(uip_connr->tcpstateflags != UIP_CLOSED &&
- BUF->destport == uip_connr->lport &&
- BUF->srcport == uip_connr->rport &&
- uip_ipaddr_cmp(&BUF->srcipaddr, &uip_connr->ripaddr)) {
- goto found;
- }
- }
-
- if((BUF->flags & TCP_CTL) != TCP_SYN) {
- goto reset;
- }
- tmp16 = BUF->destport;
-
- for(c = 0; c < UIP_LISTENPORTS; ++c) {
- if(tmp16 == uip_listenports[c]) {
- goto found_listen;
- }
- }
-
- UIP_STAT(++uip_stat.tcp.synrst);
- reset:
-
- if(BUF->flags & TCP_RST) {
- goto drop;
- }
- UIP_STAT(++uip_stat.tcp.rst);
- BUF->flags = TCP_RST | TCP_ACK;
- uip_len = UIP_IPTCPH_LEN;
- BUF->tcpoffset = 5 << 4;
-
- c = BUF->seqno[3];
- BUF->seqno[3] = BUF->ackno[3];
- BUF->ackno[3] = c;
- c = BUF->seqno[2];
- BUF->seqno[2] = BUF->ackno[2];
- BUF->ackno[2] = c;
- c = BUF->seqno[1];
- BUF->seqno[1] = BUF->ackno[1];
- BUF->ackno[1] = c;
- c = BUF->seqno[0];
- BUF->seqno[0] = BUF->ackno[0];
- BUF->ackno[0] = c;
-
- if(++BUF->ackno[3] == 0) {
- if(++BUF->ackno[2] == 0) {
- if(++BUF->ackno[1] == 0) {
- ++BUF->ackno[0];
- }
- }
- }
-
- tmp16 = BUF->srcport;
- BUF->srcport = BUF->destport;
- BUF->destport = tmp16;
-
- uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
- uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
-
- goto tcp_send_noconn;
-
- found_listen:
-
- uip_connr = 0;
- for(c = 0; c < UIP_CONNS; ++c) {
- if(uip_conns[c].tcpstateflags == UIP_CLOSED) {
- uip_connr = &uip_conns[c];
- break;
- }
- if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {
- if(uip_connr == 0 ||
- uip_conns[c].timer > uip_connr->timer) {
- uip_connr = &uip_conns[c];
- }
- }
- }
- if(uip_connr == 0) {
-
- UIP_STAT(++uip_stat.tcp.syndrop);
- UIP_LOG("tcp: found no unused connections.");
- goto drop;
- }
- uip_conn = uip_connr;
-
- uip_connr->rto = uip_connr->timer = UIP_RTO;
- uip_connr->sa = 0;
- uip_connr->sv = 4;
- uip_connr->nrtx = 0;
- uip_connr->lport = BUF->destport;
- uip_connr->rport = BUF->srcport;
- uip_ipaddr_copy(&uip_connr->ripaddr, &BUF->srcipaddr);
- uip_connr->tcpstateflags = UIP_SYN_RCVD;
- uip_connr->snd_nxt[0] = iss[0];
- uip_connr->snd_nxt[1] = iss[1];
- uip_connr->snd_nxt[2] = iss[2];
- uip_connr->snd_nxt[3] = iss[3];
- uip_connr->len = 1;
-
- uip_connr->rcv_nxt[3] = BUF->seqno[3];
- uip_connr->rcv_nxt[2] = BUF->seqno[2];
- uip_connr->rcv_nxt[1] = BUF->seqno[1];
- uip_connr->rcv_nxt[0] = BUF->seqno[0];
- uip_add_rcv_nxt(1);
-
- if((BUF->tcpoffset & 0xf0) > 0x50) {
- for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
- opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
- if(opt == TCP_OPT_END) {
-
- break;
- } else if(opt == TCP_OPT_NOOP) {
- ++c;
-
- } else if(opt == TCP_OPT_MSS &&
- uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
-
- tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
- (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];
- uip_connr->initialmss = uip_connr->mss =
- tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
-
- break;
- } else {
-
- if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
-
- break;
- }
- c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
- }
- }
- }
-
- #if UIP_ACTIVE_OPEN
- tcp_send_synack:
- BUF->flags = TCP_ACK;
- tcp_send_syn:
- BUF->flags |= TCP_SYN;
- #else
- tcp_send_synack:
- BUF->flags = TCP_SYN | TCP_ACK;
- #endif
-
- BUF->optdata[0] = TCP_OPT_MSS;
- BUF->optdata[1] = TCP_OPT_MSS_LEN;
- BUF->optdata[2] = (UIP_TCP_MSS) / 256;
- BUF->optdata[3] = (UIP_TCP_MSS) & 255;
- uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;
- BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;
- goto tcp_send;
-
- found:
- uip_conn = uip_connr;
- uip_flags = 0;
-
- if(BUF->flags & TCP_RST) {
- uip_connr->tcpstateflags = UIP_CLOSED;
- UIP_LOG("tcp: got reset, aborting connection.");
- uip_flags = UIP_ABORT;
- UIP_APPCALL();
- goto drop;
- }
-
- c = (BUF->tcpoffset >> 4) << 2;
-
- uip_len = uip_len - c - UIP_IPH_LEN;
-
- if(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
- ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) {
- if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&
- (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
- BUF->seqno[1] != uip_connr->rcv_nxt[1] ||
- BUF->seqno[2] != uip_connr->rcv_nxt[2] ||
- BUF->seqno[3] != uip_connr->rcv_nxt[3])) {
- goto tcp_send_ack;
- }
- }
-
- if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {
- uip_add32(uip_connr->snd_nxt, uip_connr->len);
- if(BUF->ackno[0] == uip_acc32[0] &&
- BUF->ackno[1] == uip_acc32[1] &&
- BUF->ackno[2] == uip_acc32[2] &&
- BUF->ackno[3] == uip_acc32[3]) {
-
- uip_connr->snd_nxt[0] = uip_acc32[0];
- uip_connr->snd_nxt[1] = uip_acc32[1];
- uip_connr->snd_nxt[2] = uip_acc32[2];
- uip_connr->snd_nxt[3] = uip_acc32[3];
-
- if(uip_connr->nrtx == 0) {
- signed char m;
- m = uip_connr->rto - uip_connr->timer;
-
- m = m - (uip_connr->sa >> 3);
- uip_connr->sa += m;
- if(m < 0) {
- m = -m;
- }
- m = m - (uip_connr->sv >> 2);
- uip_connr->sv += m;
- uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv;
- }
-
- uip_flags = UIP_ACKDATA;
-
- uip_connr->timer = uip_connr->rto;
-
- uip_connr->len = 0;
- }
- }
-
- switch(uip_connr->tcpstateflags & UIP_TS_MASK) {
-
- case UIP_SYN_RCVD:
-
- if(uip_flags & UIP_ACKDATA) {
- uip_connr->tcpstateflags = UIP_ESTABLISHED;
- uip_flags = UIP_CONNECTED;
- uip_connr->len = 0;
- if(uip_len > 0) {
- uip_flags |= UIP_NEWDATA;
- uip_add_rcv_nxt(uip_len);
- }
- uip_slen = 0;
- UIP_APPCALL();
- goto appsend;
- }
- goto drop;
- #if UIP_ACTIVE_OPEN
- case UIP_SYN_SENT:
-
- if((uip_flags & UIP_ACKDATA) &&
- (BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) {
-
- if((BUF->tcpoffset & 0xf0) > 0x50) {
- for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
- opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c];
- if(opt == TCP_OPT_END) {
-
- break;
- } else if(opt == TCP_OPT_NOOP) {
- ++c;
-
- } else if(opt == TCP_OPT_MSS &&
- uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
-
- tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |
- uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c];
- uip_connr->initialmss =
- uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;
-
- break;
- } else {
-
- if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {
-
- break;
- }
- c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];
- }
- }
- }
- uip_connr->tcpstateflags = UIP_ESTABLISHED;
- uip_connr->rcv_nxt[0] = BUF->seqno[0];
- uip_connr->rcv_nxt[1] = BUF->seqno[1];
- uip_connr->rcv_nxt[2] = BUF->seqno[2];
- uip_connr->rcv_nxt[3] = BUF->seqno[3];
- uip_add_rcv_nxt(1);
- uip_flags = UIP_CONNECTED | UIP_NEWDATA;
- uip_connr->len = 0;
- uip_len = 0;
- uip_slen = 0;
- UIP_APPCALL();
- goto appsend;
- }
-
- uip_flags = UIP_ABORT;
- UIP_APPCALL();
-
- uip_conn->tcpstateflags = UIP_CLOSED;
- goto reset;
- #endif
- case UIP_ESTABLISHED:
-
- if(BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
- if(uip_outstanding(uip_connr)) {
- goto drop;
- }
- uip_add_rcv_nxt(1 + uip_len);
- uip_flags |= UIP_CLOSE;
- if(uip_len > 0) {
- uip_flags |= UIP_NEWDATA;
- }
- UIP_APPCALL();
- uip_connr->len = 1;
- uip_connr->tcpstateflags = UIP_LAST_ACK;
- uip_connr->nrtx = 0;
- tcp_send_finack:
- BUF->flags = TCP_FIN | TCP_ACK;
- goto tcp_send_nodata;
- }
-
- if((BUF->flags & TCP_URG) != 0) {
- #if UIP_URGDATA > 0
- uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1];
- if(uip_urglen > uip_len) {
-
- uip_urglen = uip_len;
- }
- uip_add_rcv_nxt(uip_urglen);
- uip_len -= uip_urglen;
- uip_urgdata = uip_appdata;
- uip_appdata += uip_urglen;
- } else {
- uip_urglen = 0;
- #else
- uip_appdata = ((char *)uip_appdata) + ((BUF->urgp[0] << 8) | BUF->urgp[1]);
- uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1];
- #endif
- }
-
- if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) {
- uip_flags |= UIP_NEWDATA;
- uip_add_rcv_nxt(uip_len);
- }
-
- tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1];
- if(tmp16 > uip_connr->initialmss ||
- tmp16 == 0) {
- tmp16 = uip_connr->initialmss;
- }
- uip_connr->mss = tmp16;
-
- if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {
- uip_slen = 0;
- UIP_APPCALL();
- appsend:
- if(uip_flags & UIP_ABORT) {
- uip_slen = 0;
- uip_connr->tcpstateflags = UIP_CLOSED;
- BUF->flags = TCP_RST | TCP_ACK;
- goto tcp_send_nodata;
- }
- if(uip_flags & UIP_CLOSE) {
- uip_slen = 0;
- uip_connr->len = 1;
- uip_connr->tcpstateflags = UIP_FIN_WAIT_1;
- uip_connr->nrtx = 0;
- BUF->flags = TCP_FIN | TCP_ACK;
- goto tcp_send_nodata;
- }
-
- if(uip_slen > 0) {
-
- if((uip_flags & UIP_ACKDATA) != 0) {
- uip_connr->len = 0;
- }
-
- if(uip_connr->len == 0) {
-
- if(uip_slen > uip_connr->mss) {
- uip_slen = uip_connr->mss;
- }
-
- uip_connr->len = uip_slen;
- } else {
-
- uip_slen = uip_connr->len;
- }
- }
- uip_connr->nrtx = 0;
- apprexmit:
- uip_appdata = uip_sappdata;
-
- if(uip_slen > 0 && uip_connr->len > 0) {
-
- uip_len = uip_connr->len + UIP_TCPIP_HLEN;
-
- BUF->flags = TCP_ACK | TCP_PSH;
-
- goto tcp_send_noopts;
- }
-
- if(uip_flags & UIP_NEWDATA) {
- uip_len = UIP_TCPIP_HLEN;
- BUF->flags = TCP_ACK;
- goto tcp_send_noopts;
- }
- }
- goto drop;
- case UIP_LAST_ACK:
-
- if(uip_flags & UIP_ACKDATA) {
- uip_connr->tcpstateflags = UIP_CLOSED;
- uip_flags = UIP_CLOSE;
- UIP_APPCALL();
- }
- break;
- case UIP_FIN_WAIT_1:
-
- if(uip_len > 0) {
- uip_add_rcv_nxt(uip_len);
- }
- if(BUF->flags & TCP_FIN) {
- if(uip_flags & UIP_ACKDATA) {
- uip_connr->tcpstateflags = UIP_TIME_WAIT;
- uip_connr->timer = 0;
- uip_connr->len = 0;
- } else {
- uip_connr->tcpstateflags = UIP_CLOSING;
- }
- uip_add_rcv_nxt(1);
- uip_flags = UIP_CLOSE;
- UIP_APPCALL();
- goto tcp_send_ack;
- } else if(uip_flags & UIP_ACKDATA) {
- uip_connr->tcpstateflags = UIP_FIN_WAIT_2;
- uip_connr->len = 0;
- goto drop;
- }
- if(uip_len > 0) {
- goto tcp_send_ack;
- }
- goto drop;
- case UIP_FIN_WAIT_2:
- if(uip_len > 0) {
- uip_add_rcv_nxt(uip_len);
- }
- if(BUF->flags & TCP_FIN) {
- uip_connr->tcpstateflags = UIP_TIME_WAIT;
- uip_connr->timer = 0;
- uip_add_rcv_nxt(1);
- uip_flags = UIP_CLOSE;
- UIP_APPCALL();
- goto tcp_send_ack;
- }
- if(uip_len > 0) {
- goto tcp_send_ack;
- }
- goto drop;
- case UIP_TIME_WAIT:
- goto tcp_send_ack;
- case UIP_CLOSING:
- if(uip_flags & UIP_ACKDATA) {
- uip_connr->tcpstateflags = UIP_TIME_WAIT;
- uip_connr->timer = 0;
- }
- }
- goto drop;
-
- tcp_send_ack:
- BUF->flags = TCP_ACK;
- tcp_send_nodata:
- uip_len = UIP_IPTCPH_LEN;
- tcp_send_noopts:
- BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;
-
- tcp_send:
- BUF->ackno[0] = uip_connr->rcv_nxt[0];
- BUF->ackno[1] = uip_connr->rcv_nxt[1];
- BUF->ackno[2] = uip_connr->rcv_nxt[2];
- BUF->ackno[3] = uip_connr->rcv_nxt[3];
- BUF->seqno[0] = uip_connr->snd_nxt[0];
- BUF->seqno[1] = uip_connr->snd_nxt[1];
- BUF->seqno[2] = uip_connr->snd_nxt[2];
- BUF->seqno[3] = uip_connr->snd_nxt[3];
- BUF->proto = UIP_PROTO_TCP;
- BUF->srcport = uip_connr->lport;
- BUF->destport = uip_connr->rport;
- uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
- uip_ipaddr_copy(&BUF->destipaddr, &uip_connr->ripaddr);
- if(uip_connr->tcpstateflags & UIP_STOPPED) {
-
- BUF->wnd[0] = BUF->wnd[1] = 0;
- } else {
- BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8);
- BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff);
- }
- tcp_send_noconn:
- BUF->ttl = UIP_TTL;
- #if UIP_CONF_IPV6
-
- BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
- BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
- #else
- BUF->len[0] = (uip_len >> 8);
- BUF->len[1] = (uip_len & 0xff);
- #endif
- BUF->urgp[0] = BUF->urgp[1] = 0;
-
- BUF->tcpchksum = 0;
- BUF->tcpchksum = ~(uip_tcpchksum());
- ip_send_nolen:
- #if UIP_CONF_IPV6
- BUF->vtc = 0x60;
- BUF->tcflow = 0x00;
- BUF->flow = 0x00;
- #else
- BUF->vhl = 0x45;
- BUF->tos = 0;
- BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
- ++ipid;
- BUF->ipid[0] = ipid >> 8;
- BUF->ipid[1] = ipid & 0xff;
-
- BUF->ipchksum = 0;
- BUF->ipchksum = ~(uip_ipchksum());
- DEBUG_PRINTF("uip ip_send_nolen: checksum 0x%04x\n", uip_ipchksum());
- #endif
- UIP_STAT(++uip_stat.tcp.sent);
- #if UIP_CONF_IPV6
- send:
- #endif
- DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len,
- (BUF->len[0] << 8) | BUF->len[1]);
- UIP_STAT(++uip_stat.ip.sent);
-
- uip_flags = 0;
- return;
- drop:
- uip_len = 0;
- uip_flags = 0;
- return;
- }
- u16_t
- htons(u16_t val)
- {
- return HTONS(val);
- }
- u32_t
- htonl(u32_t val)
- {
- return HTONL(val);
- }
- void
- uip_send(const void *data, int len)
- {
- int copylen;
- #define MIN(a,b) ((a) < (b)? (a): (b))
- copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN -
- (int)((char *)uip_sappdata - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN]));
- if(copylen > 0) {
- uip_slen = copylen;
- if(data != uip_sappdata) {
- memcpy(uip_sappdata, (data), uip_slen);
- }
- }
- }
- #endif
|