📄 uip.lst
字号:
246 6 /* We call UIP_APPCALL() with uip_flags set to
247 6 UIP_TIMEDOUT to inform the application that the
248 6 connection has timed out. */
249 6 uip_flags = UIP_TIMEDOUT;
250 6 UIP_APPCALL();
251 6
252 6 /* We also send a reset packet to the remote host. */
253 6 BUF->flags = TCP_RST | TCP_ACK;
254 6 goto tcp_send_nodata;
255 6 }
256 5
257 5 /* Exponential backoff. */
258 5 uip_conn->timer = UIP_RTO << (uip_conn->nrtx > 4? 4: uip_conn->nrtx);
259 5
260 5 ++(uip_conn->nrtx);
261 5
262 5 /* Ok, so we need to retransmit. We do this differently
263 5 depending on which state we are in. In ESTABLISHED, we
264 5 call upon the application so that it may prepare the
265 5 data for the retransmit. In SYN_RCVD, we resend the
266 5 SYNACK that we sent earlier and in LAST_ACK we have to
267 5 retransmit our FINACK. */
268 5 UIP_STAT(++uip_stat.tcp.rexmit);
269 5 switch(uip_conn->tcpstateflags & TS_MASK) {
270 6 case SYN_RCVD:
271 6 /* In the SYN_RCVD state, we should retransmit our
272 6 SYNACK. */
273 6 goto tcp_send_synack;
274 6
275 6 #if UIP_ACTIVE_OPEN
case SYN_SENT:
/* In the SYN_SENT state, we retransmit out SYN. */
BUF->flags = 0;
goto tcp_send_syn;
#endif /* UIP_ACTIVE_OPEN */
281 6
282 6 case ESTABLISHED:
283 6 /* In the ESTABLISHED state, we call upon the application
284 6 to do the actual retransmit after which we jump into
285 6 the code for sending out the packet (the apprexmit
286 6 label). */
287 6 uip_len = 0;
288 6 uip_flags = UIP_REXMIT;
289 6 UIP_APPCALL();
290 6 goto apprexmit;
291 6
292 6 case FIN_WAIT_1:
293 6 case CLOSING:
294 6 case LAST_ACK:
295 6 /* In all these states we should retransmit a FINACK. */
296 6 goto tcp_send_finack;
297 6
298 6 }
299 5 }
300 4 } else if((uip_conn->tcpstateflags & TS_MASK) == ESTABLISHED) {
301 4 /* If there was no need for a retransmission, we poll the
302 4 application for new data. */
C51 COMPILER V7.06 UIP 04/05/2006 12:13:01 PAGE 6
303 4 uip_len = 0;
304 4 uip_flags = UIP_POLL;
305 4 UIP_APPCALL();
306 4 goto appsend;
307 4 }
308 3 }
309 2 goto drop;
310 2 }
311 1
312 1 /* This is where the input processing starts. */
313 1 UIP_STAT(++uip_stat.ip.recv);
314 1
315 1 /* Check validity of the IP header. */
316 1 if(BUF->vhl != 0x45) { /* IP version and header length. */
317 2 UIP_STAT(++uip_stat.ip.drop);
318 2 UIP_STAT(++uip_stat.ip.vhlerr);
319 2 UIP_LOG("ip: invalid version or header length.");
320 2 goto drop;
321 2 }
322 1
323 1 /* Check the size of the packet. If the size reported to us in
324 1 uip_len doesn't match the size reported in the IP header, there
325 1 has been a transmission error and we drop the packet. */
326 1
327 1 #if UIP_BUFSIZE > 255
if(BUF->len[0] != (uip_len >> 8)) {
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.hblenerr);
UIP_LOG("ip: invalid length, high byte.");
/* IP length, high byte. */
goto drop;
}
if(BUF->len[1] != (uip_len & 0xff)) {
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.lblenerr);
UIP_LOG("ip: invalid length, low byte.");
/* IP length, low byte. */
goto drop;
}
#else
343 1 if(BUF->len[0] != 0) { /* IP length, high byte. */
344 2 UIP_STAT(++uip_stat.ip.drop);
345 2 UIP_STAT(++uip_stat.ip.hblenerr);
346 2 UIP_LOG("ip: invalid length, high byte.");
347 2 goto drop;
348 2 }
349 1 if(BUF->len[1] != uip_len) { /* IP length, low byte. */
350 2 UIP_STAT(++uip_stat.ip.drop);
351 2 UIP_STAT(++uip_stat.ip.lblenerr);
352 2 UIP_LOG("ip: invalid length, low byte.");
353 2 goto drop;
354 2 }
355 1 #endif /* UIP_BUFSIZE > 255 */
356 1
357 1 if(BUF->ipoffset[0] & 0x3f) { /* We don't allow IP fragments. */
358 2 UIP_STAT(++uip_stat.ip.drop);
359 2 UIP_STAT(++uip_stat.ip.fragerr);
360 2 UIP_LOG("ip: fragment dropped.");
361 2 goto drop;
362 2 }
363 1
364 1 /* Check if the packet is destined for our IP address. */
C51 COMPILER V7.06 UIP 04/05/2006 12:13:01 PAGE 7
365 1 if(BUF->destipaddr[0] != htons(((u16_t)UIP_IPADDR0 << 8) | UIP_IPADDR1)) {
366 2 UIP_STAT(++uip_stat.ip.drop);
367 2 UIP_LOG("ip: packet not for us.");
368 2 goto drop;
369 2 }
370 1 if(BUF->destipaddr[1] != htons(((u16_t)UIP_IPADDR2 << 8) | UIP_IPADDR3)) {
371 2 UIP_STAT(++uip_stat.ip.drop);
372 2 UIP_LOG("ip: packet not for us.");
373 2 goto drop;
374 2 }
375 1
376 1 if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
377 2 checksum. */
378 2 UIP_STAT(++uip_stat.ip.drop);
379 2 UIP_STAT(++uip_stat.ip.chkerr);
380 2 UIP_LOG("ip: bad checksum.");
381 2 goto drop;
382 2 }
383 1
384 1 if(BUF->proto == IP_PROTO_TCP) /* Check for TCP packet. If so, jump
385 1 to the tcp_input label. */
386 1 goto tcp_input;
387 1
388 1 if(BUF->proto != IP_PROTO_ICMP) { /* We only allow ICMP packets from
389 2 here. */
390 2 UIP_STAT(++uip_stat.ip.drop);
391 2 UIP_STAT(++uip_stat.ip.protoerr);
392 2 UIP_LOG("ip: neither tcp nor icmp.");
393 2 goto drop;
394 2 }
395 1
396 1 UIP_STAT(++uip_stat.icmp.recv);
397 1
398 1 /* ICMP echo (i.e., ping) processing. This is simple, we only change
399 1 the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
400 1 checksum before we return the packet. */
401 1 if(ICMPBUF->type != ICMP_ECHO) {
402 2 UIP_STAT(++uip_stat.icmp.drop);
403 2 UIP_STAT(++uip_stat.icmp.typeerr);
404 2 UIP_LOG("icmp: not icmp echo.");
405 2 goto drop;
406 2 }
407 1
408 1 ICMPBUF->type = ICMP_ECHO_REPLY;
409 1
410 1 if(ICMPBUF->icmpchksum >= htons(0xffff - (ICMP_ECHO << 8))) {
411 2 ICMPBUF->icmpchksum += htons(ICMP_ECHO << 8) + 1;
412 2 } else {
413 2 ICMPBUF->icmpchksum += htons(ICMP_ECHO << 8);
414 2 }
415 1
416 1 /* Swap IP addresses. */
417 1 tmpport = BUF->destipaddr[0];
418 1 BUF->destipaddr[0] = BUF->srcipaddr[0];
419 1 BUF->srcipaddr[0] = tmpport;
420 1 tmpport = BUF->destipaddr[1];
421 1 BUF->destipaddr[1] = BUF->srcipaddr[1];
422 1 BUF->srcipaddr[1] = tmpport;
423 1
424 1 UIP_STAT(++uip_stat.icmp.sent);
425 1 goto send;
426 1
C51 COMPILER V7.06 UIP 04/05/2006 12:13:01 PAGE 8
427 1 /* TCP input processing. */
428 1 tcp_input:
429 1 UIP_STAT(++uip_stat.tcp.recv);
430 1
431 1 if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
432 2 checksum. */
433 2 UIP_STAT(++uip_stat.tcp.drop);
434 2 UIP_STAT(++uip_stat.tcp.chkerr);
435 2 UIP_LOG("tcp: bad checksum.");
436 2 goto drop;
437 2 }
438 1
439 1 /* Demultiplex this segment. */
440 1 /* First check any active connections. */
441 1 for(uip_conn = &uip_conns[0]; uip_conn < &uip_conns[UIP_CONNS]; ++uip_conn) {
442 2 if(uip_conn->tcpstateflags != CLOSED &&
443 2 BUF->srcipaddr[0] == uip_conn->ripaddr[0] &&
444 2 BUF->srcipaddr[1] == uip_conn->ripaddr[1] &&
445 2 BUF->destport == uip_conn->lport &&
446 2 BUF->srcport == uip_conn->rport)
447 2 goto found;
448 2 }
449 1
450 1 /* If we didn't find and active connection that expected the packet,
451 1 either this packet is an old duplicate, or this is a SYN packet
452 1 destined for a connection in LISTEN. If the SYN flag isn't set,
453 1 it is an old packet and we send a RST. */
454 1 if(BUF->flags != TCP_SYN)
455 1 goto reset;
456 1
457 1 tmpport = BUF->destport;
458 1 /* Next, check listening connections. */
459 1 for(c = 0; c < UIP_LISTENPORTS && uip_listenports[c] != 0; ++c) {
460 2 if(tmpport == uip_listenports[c])
461 2 goto found_listen;
462 2 }
463 1
464 1 /* No matching connection found, so we send a RST packet. */
465 1 UIP_STAT(++uip_stat.tcp.synrst);
466 1 reset:
467 1
468 1 /* We do not send resets in response to resets. */
469 1 if(BUF->flags & TCP_RST)
470 1 goto drop;
471 1
472 1 UIP_STAT(++uip_stat.tcp.rst);
473 1
474 1 BUF->flags = TCP_RST | TCP_ACK;
475 1 uip_len = 40;
476 1 BUF->tcpoffset = 5 << 4;
477 1
478 1 /* Flip the seqno and ackno fields in the TCP header. */
479 1 c = BUF->seqno[3];
480 1 BUF->seqno[3] = BUF->ackno[3];
481 1 BUF->ackno[3] = c;
482 1
483 1 c = BUF->seqno[2];
484 1 BUF->seqno[2] = BUF->ackno[2];
485 1 BUF->ackno[2] = c;
486 1
487 1 c = BUF->seqno[1];
488 1 BUF->seqno[1] = BUF->ackno[1];
C51 COMPILER V7.06 UIP 04/05/2006 12:13:01 PAGE 9
489 1 BUF->ackno[1] = c;
490 1
491 1 c = BUF->seqno[0];
492 1 BUF->seqno[0] = BUF->ackno[0];
493 1 BUF->ackno[0] = c;
494 1
495 1 /* We also have to increase the sequence number we are
496 1 acknowledging. If the least significant byte overflowed, we need
497 1 to propagate the carry to the other bytes as well. */
498 1 if(++BUF->ackno[3] == 0) {
499 2 if(++BUF->ackno[2] == 0) {
500 3 if(++BUF->ackno[1] == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -