📄 uip.lst
字号:
447 2 goto drop;
448 2 }
449 1
450 1
451 1
452 1 if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
453 2 checksum. */
454 2 UIP_STAT(++uip_stat.ip.drop);
455 2 UIP_STAT(++uip_stat.ip.chkerr);
456 2 UIP_LOG("ip: bad checksum.");
457 2 goto drop;
458 2 }
459 1
460 1 if(BUF->proto == UIP_PROTO_TCP) /* Check for TCP packet. If so, jump
461 1 to the tcp_input label. */
462 1 goto tcp_input;
463 1
464 1
465 1 if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from
466 2 here. */
467 2 UIP_STAT(++uip_stat.ip.drop);
468 2 UIP_STAT(++uip_stat.ip.protoerr);
469 2 UIP_LOG("ip: neither tcp nor icmp.");
470 2 goto drop;
471 2 }
472 1
473 1 #if UIP_PINGADDRCONF
icmp_input:
#endif /* UIP_PINGADDRCONF */
476 1 UIP_STAT(++uip_stat.icmp.recv);
477 1
478 1 /* ICMP echo (i.e., ping) processing. This is simple, we only change
479 1 the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
480 1 checksum before we return the packet. */
481 1 if(ICMPBUF->type != ICMP_ECHO) { //ICMP_ECHO=8,ICMP_ECHO_replay=0
482 2 UIP_STAT(++uip_stat.icmp.drop);
483 2 UIP_STAT(++uip_stat.icmp.typeerr);
484 2 UIP_LOG("icmp: not icmp echo.");
485 2 goto drop;
486 2 }
487 1
488 1 /* If we are configured to use ping IP address assignment, we use
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 9
489 1 the destination IP address of this ping packet and assign it to
490 1 ourself.
491 1 #if UIP_PINGADDRCONF
492 1 if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) { //改动
493 1 uip_hostaddr[0] = BUF->destipaddr[0];
494 1 uip_hostaddr[1] = BUF->destipaddr[1];
495 1 }
496 1 #endif/* UIP_PINGADDRCONF */
497 1
498 1 ICMPBUF->type = ICMP_ECHO_REPLY;
499 1
500 1 if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) { //校验和核实
501 2 ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
502 2 } else {
503 2 ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
504 2 }
505 1 /* Swap IP addresses. */
506 1 tmp16 = BUF->destipaddr[0]; //BUF中的源末地址对调,准备发送
507 1 BUF->destipaddr[0] = BUF->srcipaddr[0];
508 1 BUF->srcipaddr[0] = tmp16;
509 1 tmp16 = BUF->destipaddr[1];
510 1 BUF->destipaddr[1] = BUF->srcipaddr[1];
511 1 BUF->srcipaddr[1] = tmp16;
512 1
513 1 UIP_STAT(++uip_stat.icmp.sent);
514 1 goto send;
515 1
516 1 /* End of IPv4 input header processing code. */
517 1
518 1
519 1 /************************************** TCP input processing. *******************************/
520 1 tcp_input:
521 1 UIP_STAT(++uip_stat.tcp.recv);
522 1
523 1 /* Start of TCP input header processing code. */
524 1
525 1 if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
526 2 checksum. */
527 2 UIP_STAT(++uip_stat.tcp.drop);
528 2 UIP_STAT(++uip_stat.tcp.chkerr);
529 2 UIP_LOG("tcp: bad checksum.");
530 2 goto drop;
531 2 }
532 1
533 1
534 1 /* Demultiplex this segment. 多元化段*/
535 1 /* First check any active connections.首先查看是否有任何已有的连接 */ // 怎么产生的连接?
536 1 for(uip_connr = &uip_conns[0]; uip_connr < &uip_conns[UIP_CONNS]; ++uip_connr) {
537 2 if(uip_connr->tcpstateflags != CLOSED &&
538 2 BUF->destport == uip_connr->lport &&
539 2 BUF->srcport == uip_connr->rport &&
540 2 BUF->srcipaddr[0] == uip_connr->ripaddr[0] &&
541 2 BUF->srcipaddr[1] == uip_connr->ripaddr[1]) {
542 3 goto found;
543 3 }
544 2 }
545 1
546 1 /* If we didn't find and active connection that expected the packet,
547 1 either this packet is an old duplicate(复制品), or this is a SYN packet
548 1 destined for a connection in LISTEN. If the SYN flag isn't set,
549 1 it is an old packet and we send a RST. */
550 1 if((BUF->flags & TCP_CTL) != TCP_SYN)
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 10
551 1 goto reset;
552 1
553 1 tmp16 = BUF->destport;
554 1 /* Next, check listening connections. */
555 1 for(c = 0; c < UIP_LISTENPORTS; ++c) {
556 2 if(tmp16 == uip_listenports[c])
557 2 goto found_listen;
558 2 }
559 1
560 1 /* No matching connection found, so we send a RST packet. */
561 1 UIP_STAT(++uip_stat.tcp.synrst);
562 1 reset: //发送重启包
563 1
564 1 /* We do not send resets in response to resets. */ //未发过重起包发送一个重起的包
565 1 if(BUF->flags & TCP_RST) //TCP_RST=0x04
566 1 goto drop;
567 1
568 1 UIP_STAT(++uip_stat.tcp.rst);
569 1
570 1 BUF->flags = TCP_RST | TCP_ACK; // TCP_ACK 0x10,为什么用或,BUF->flags=0x14
571 1 uip_len = 40; //tcp长
572 1 BUF->tcpoffset = 5 << 4; //BUF->tcpoffset=0x50
573 1
574 1 /* Flip赋值 the seqno and ackno fields in the TCP header. */
575 1 c = BUF->seqno[3];
576 1 BUF->seqno[3] = BUF->ackno[3];
577 1 BUF->ackno[3] = c;
578 1
579 1 c = BUF->seqno[2];
580 1 BUF->seqno[2] = BUF->ackno[2];
581 1 BUF->ackno[2] = c;
582 1
583 1 c = BUF->seqno[1];
584 1 BUF->seqno[1] = BUF->ackno[1];
585 1 BUF->ackno[1] = c;
586 1
587 1 c = BUF->seqno[0];
588 1 BUF->seqno[0] = BUF->ackno[0];
589 1 BUF->ackno[0] = c;
590 1
591 1 /* We also have to increase the sequence number we are
592 1 acknowledging. If the least significant byte overflowed, we need
593 1 to propagate the carry to the other bytes as well. */
594 1 if(++BUF->ackno[3] == 0) {
595 2 if(++BUF->ackno[2] == 0) {
596 3 if(++BUF->ackno[1] == 0) {
597 4 ++BUF->ackno[0];
598 4 }
599 3 }
600 2 }
601 1
602 1 /* Swap交换 port numbers. */
603 1 tmp16 = BUF->srcport;
604 1 BUF->srcport = BUF->destport;
605 1 BUF->destport = tmp16;
606 1
607 1 /* Swap交换 IP addresses. */
608 1 tmp16 = BUF->destipaddr[0];
609 1 BUF->destipaddr[0] = BUF->srcipaddr[0];
610 1 BUF->srcipaddr[0] = tmp16;
611 1 tmp16 = BUF->destipaddr[1];
612 1 BUF->destipaddr[1] = BUF->srcipaddr[1];
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 11
613 1 BUF->srcipaddr[1] = tmp16;
614 1
615 1
616 1 /* And send out the RST packet! */
617 1 goto tcp_send_noconn;
618 1
619 1 /* This label will be jumped to if we matched the incoming packet
620 1 with a connection in LISTEN. In that case, we should create a new
621 1 connection and send a SYNACK in return. */
622 1 found_listen: //通过端口建立连接
623 1 /* First we check if there are any connections avaliable. Unused
624 1 connections are kept in the same table as used connections, but
625 1 unused ones have the tcpstate set to CLOSED. Also, connections in
626 1 TIME_WAIT are kept track of and we'll use the oldest one if no
627 1 CLOSED connections are found. Thanks to Eddie C. Dost for a very
628 1 nice algorithm for the TIME_WAIT search. */
629 1
630 1 uip_connr = -1; // 8051 xdata starts at 0x0000
631 1 for(c = 0; c < UIP_CONNS; ++c) {
632 2 if(uip_conns[c].tcpstateflags == CLOSED) {
633 3 uip_connr = &uip_conns[c];
634 3 break;
635 3 }
636 2 if(uip_conns[c].tcpstateflags == TIME_WAIT) {
637 3 if(uip_connr == 0 ||
638 3 uip_conns[c].timer > uip_connr->timer) {
639 4 uip_connr = &uip_conns[c]; break; //改加了个break
640 4 }
641 3 }
642 2 }
643 1
644 1 if(uip_connr == -1) { // 8051 xdata starts at 0x0000
645 2 /* All connections are used already, we drop packet and hope that
646 2 the remote end will retransmit the packet at a time when we
647 2 have more spare connections. */
648 2 UIP_STAT(++uip_stat.tcp.syndrop);
649 2 UIP_LOG("tcp: found no unused connections.");
650 2 goto drop;
651 2 }
652 1 uip_conn = uip_connr;
653 1
654 1 /* Fill in the necessary fields for the new connection. */
655 1 //在此建立了新的连接,主要是有先建立的端口
656 1 uip_connr->rto = uip_connr->timer = UIP_RTO;
657 1 uip_connr->sa = 0;
658 1 uip_connr->sv = 4;
659 1 uip_connr->nrtx = 0;
660 1 uip_connr->lport = BUF->destport;
661 1 uip_connr->rport = BUF->srcport;
662 1 uip_connr->ripaddr[0] = BUF->srcipaddr[0];
663 1 uip_connr->ripaddr[1] = BUF->srcipaddr[1];
664 1
665 1 uip_connr->tcpstateflags = SYN_RCVD;
666 1
667 1 uip_connr->snd_nxt[0] = iss[0];
668 1 uip_connr->snd_nxt[1] = iss[1];
669 1 uip_connr->snd_nxt[2] = iss[2];
670 1 uip_connr->snd_nxt[3] = iss[3];
671 1 uip_connr->len = 1;
672 1
673 1 /* rcv_nxt should be the seqno from the incoming packet + 1. */
674 1 uip_connr->rcv_nxt[3] = BUF->seqno[3];
C51 COMPILER V8.08 UIP 08/22/2008 14:32:51 PAGE 12
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -