UIP协议栈笔记·一

转载地址:  http://blog.chinaunix.net/uid-23247944-id-2973884.html

UIP协议多用于嵌入式产品。
     结合如CP2200芯片的网卡芯片,组成嵌入式网卡,硬件提供能力,UIP提供的是策略。
     由上往下逐步封装用户的数据,如:
     应用层----------传输层--------网络层------数据链路层-----物理层
     应用数据---TCP封装头部---IP封装头部-----mac封装+尾部-----发送 
    任何的事物需要经过一定的初始阶段,在UIP协议里面通过uip_init()来初始化。
    在uip_init()函数里面主要工作是:
     1. 将uip_state结构体全部清零。
     2. 初始化用于TCP链接的uip_conn结构体,将连接状态置为close。
     3. 设置用于TCP链接的端口lastport = 4096;应该是最大的端口号,待查证。
     4. 如果定义了UDP,同样进行初始化。

  1. void uip_init(void) {
  2.     // clean statistics
  3.     char* ptr= (char*) &uip_stat;
  4.     for (int i = 0; i<sizeof (uip_stat); i++) {
  5.         ptr[i] = 0;   
  6.     }
  7.  
  8.     for (c = 0; c < UIP_LISTENPORTS; ++c) {
  9.         uip_listenports[c] = 0;
  10.     }
  11.     for (c = 0; c < UIP_CONNS; ++c) {
  12.         uip_conns[c].tcpstateflags = UIP_CLOSED;
  13.     }
  14.     lastport = 4096;
  15.  
  16. #if UIP_UDP
  17.     for (c = 0; c < UIP_UDP_CONNS; ++c) {
  18.         uip_udp_conns[c].lport = 0;
  19.     }
  20. #endif /* UIP_UDP */
  21.  
  22.  
  23.     /* IPv4 initialization. */
  24. #if UIP_FIXEDADDR == 0
  25.     /* uip_hostaddr[0] = uip_hostaddr[1] = 0;*/
  26. #endif /* UIP_FIXEDADDR */
  27.  
  28. }

   同样,我在ourdev.cn上下载了,一份总结,一点一点上传,感谢ourdev.cn。
   uip_arp_init(); arp协议的初始化,其中进行的是构造arp协议的缓存。
   在配置UIP协议的时候要主要配置超时。
   // 摘自uip协议包main.c

  1. struct timer periodic_timer, arp_timer;
  2. timer_set(&periodic_timer, CLOCK_SECOND / 2);
  3. timer_set(&arp_timer, CLOCK_SECOND * 10);

   还要进行的配置是,比如配置主机地址,ip地址,还有掩码,以太网mac地址等信息,或者配置dhcp。
   这些配置完成之后,进入协议的主循环,接受,和发送等等的过程了。
   要应用到实际的使用中,还需要结合硬件,比如CP2200芯片,使用过程中,需要有接收,和发送函
   数,这个需要自己实现,循环的流程如下:
  

  1. while(1)
  2.  {
  3.     uip_len = tapdev_read();  // 接收的函数
  4.     if(uip_len > 0)
  5.     {
  6.       if(BUF->type == htons(UIP_ETHTYPE_IP))
  7.       {
  8.           uip_arp_ipin();
  9.           uip_input();  // 这个是实际的从上往下封装包的函数
  10.          /* If the above function invocation resulted in data that
  11.           should be sent out on the network, the global variable
  12.            uip_len is set to a value > 0. */
  13.           if(uip_len > 0)
  14.           {
  15.               uip_arp_out();
  16.               tapdev_send(); // 发送的实际函数 
  17.           }
  18.       } 
  19.       else if(BUF->type == htons(UIP_ETHTYPE_ARP))
  20.       {
  21.           uip_arp_arpin();
  22.           /* If the above function invocation resulted in data that
  23.           should be sent out on the network, the global variable
  24.           uip_len is set to a value > 0. */
  25.           if(uip_len > 0) 
  26.           {
  27.            tapdev_send();
  28.           }
  29.       }
  30.  
  31.     } 
  32.     else if(timer_expired(&periodic_timer))
  33.     {
  34.          timer_reset(&periodic_timer);
  35.          for(i = 0; i < UIP_CONNS; i++)
  36.          {
  37.              uip_periodic(i);
  38.              /* If the above function invocation resulted in data that
  39.              should be sent out on the network, the global variable
  40.              uip_len is set to a value > 0. */
  41.              if(uip_len > 0)
  42.              {
  43.                  uip_arp_out();
  44.                  tapdev_send();
  45.              }
  46.         }
  47.  
  48. #if UIP_UDP
  49.       for(i = 0; i < UIP_UDP_CONNS; i++)
  50.       {
  51.            uip_udp_periodic(i);
  52.            /* If the above function invocation resulted in data that
  53.             should be sent out on the network, the global variable
  54.              uip_len is set to a value > 0. */
  55.            if(uip_len > 0)
  56.            {
  57.                 uip_arp_out();
  58.                 tapdev_send();
  59.            }
  60.       }
  61. #endif /* UIP_UDP */
  62.  
  63.       /* Call the ARP timer function every 10 seconds. */
  64.       if(timer_expired(&arp_timer))
  65.       {
  66.            timer_reset(&arp_timer);
  67.            uip_arp_timer();
  68.       }
  69.     }
  70.   }

    在主循环里面,还有好多需要分析的,特别是uip_input(),各种跳转...
 UIP.zip   (从ourdev.cn下载的一点资料,感谢ourdev.cn)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值