变量 u16_t uip_len
uip_buf缓冲区中的数据包长度(译者注:uip_buf是一数列,其长度固定,此处指的是动态存的数据长度,不是指数列本身长度)。
这个全局函数盛放存储于uip_buf中的数据包的长度。
当网络设备驱动调用uip输入函数时,uip_len要被设为传入数据包的大小。
当向处发送数据包时,设备驱动通过这个变量确定要发送的数据包在uip_buf中的长度。 应用例程:
example-mainloop-with-arp.c, andexample-mainloop-without-arp.c. 此变量定义于uip.c中的155行。
此变量被uip_arp_arpin(),uip_process(), anduip_split_output()引用。
UIP中文文档第二 uIP初始化函数
uIP初始化函数用于启动uIP.它包括以下两个函数:
1. 2.
1.
void uip_init(void) void uip_setipid(u16_t id)
void uip_init(void)
此函数用于在启动时初始化uIP的TCP/IP栈。 应用示例:
example-mainloop-with-arp.c, and example-mainloop-without-arp.c. 定义于uip.c的379行。
2. void uip_setipid(u16_t id) 此函数用于启动时设置初始的ip_id. 此函数定义于uip.c的181行。
追加:下面为uip源代码中上述两函数作注。
文中所涉代码属于uip 1.0:http://www.sics.se/~adam/uip/uip-1.0-refman/a00202.html#l00379 1. void uip_init(void)代码分析
2. 3. 4. 5. 6. 7. 8. 9.
void uip_init(void) {
for(c = 0; c < UIP_LISTENPORTS; ++c) { uip_listenports[c] = 0;
} //将uip_listenports数组全部清零 for(c = 0; c < UIP_CONNS; ++c) { uip_conns[c].tcpstateflags = UIP_CLOSED;
} //将所有连接的tcpstateflags状态标志设为关闭,表示此连接为关闭状态。
10. #if UIP_ACTIVE_OPEN 11. lastport = 1024;
12. #endif /* UIP_ACTIVE_OPEN */ 13. //上面的段不知什么意思。 14. #if UIP_UDP
15. for(c = 0; c < UIP_UDP_CONNS; ++c) {
16. uip_udp_conns[c].lport = 0; 17. }
18. #endif /* UIP_UDP *///如果定义了UIP_UDP则将uip_udp_conns的lport清零。 19. /* IPv4 initialization. */
20. #if UIP_FIXEDADDR == 0/* uip_hostaddr[0] = uip_hostaddr[1] = 0;*/ 21. #endif /* UIP_FIXEDADDR *///如果主机地址要为固定的,在上面这里赋值。}
UIP_LISTENPORTS:可同时监听的TCP端口数最大值,每监听一个TCP端口都要占用两字节内存。 此宏定义于uipopt.h的259行。其默认值为20。
UIP_CONNS: 可同时打开的TCP端口数的最大值,由于TCP连接是静态分配的,所以降低这个值的大小可以降低占用的内存空间,每个TCP连接大约需要30字节内存。 此宏定义于uipopt.h的245行。其默认值为10。
UIP_UDP_CONNS:最大的并行UDP连接数。 此宏定义于uipopt.h的206行。其默认值是10。
uip_conns: struct uip_conn型结构体数列,数列大小为UIP_CONNS.每个uip_conn代表一个TCP连接。
uip_udp_conns:struct uip_udp_conn型号结构体数列,数列大小为UIP_UDP_CONNS.每个uip_udp_conn代表一个UDP连接。
UIP_ACTIVE_OPEN:是否编译可以让上层应用程序动态打开外向的TCP连接的代码。关闭此功能可以降低uIP的代码大小。
此宏定义于uipopt.h的233行。 2. uip_conn结构体 代表一个uip的TCP连接
此结构体用于鉴定一个TCP连接,对应用来说,除一个域之外,所有的域都是只读的。例外的是appstate域,它的目的是让应用存取应用相关的连接状态,此域的类型定义于uipopt.h中。 此结构体定义于uip.h的1153行。
1. 2. 3.
struct uip_conn {
uip_ipaddr_t ripaddr; /**< The IP address of the remote host. 运程主机的ip地址*/
u16_t lport; /**< The local TCP port, in network byte order. 本地主机的TCP端口,以网络传输中的字节顺序*/
4. 5. 6. 7. 8. 9.
order. 远程主机的TCP端口,以网络传输中的字节顺序*/ u8_t rcv_nxt[4]; /**< The sequence number that we expect to receive next.我们期待接收的下一个序号*/
u8_t snd_nxt[4]; /**< The sequence number that was last sent by us. 我们上次发送的序号*/
u16_t len; /**< Length of the data that was previously sent.上次发送的数据长度 */
10. u16_t mss; /**< Current maximum segment size for the 11. connection. 现行连接的段大小最大值*/
12. u16_t initialmss; /**< Initial maximum segment size for the 13. connection. 连接的初始段大小最大值*/
14. u8_t sa; /**< Retransmission time-out calculation state
15. variable. 重传超时计算状态变量*/
16. u8_t sv; /**< Retransmission time-out calculation state 17. variable. 重传超时计算状态变量*/
18. u8_t rto; /**< Retransmission time-out.重传超时 */ 19. u8_t tcpstateflags; /**< TCP state and flags. TCP状态标志*/ 20. u8_t timer; /**< The retransmission timer. 重传定时器*/ 21. u8_t nrtx; /**< The number of retransmissions for the last 22. segment sent. 上次发送的段的重传次数*/ 23.
24. /** The application state.应用状态 */ 25. uip_tcp_appstate_t appstate; 26. };
27. order. 远程主机的TCP端口,以网络传输中的字节顺序*/ 28. u8_t rcv_nxt[4]; /**< The sequence number that we expect to 29. receive next.我们期待接收的下一个序号*/
30. u8_t snd_nxt[4]; /**< The sequence number that was last sent by 31. us. 我们上次发送的序号*/
32. u16_t len; /**< Length of the data that was previously sent.上次发送的数据长度 */ 33. u16_t mss; /**< Current maximum segment size for the 34. connection. 现行连接的段大小最大值*/
35. u16_t initialmss; /**< Initial maximum segment size for the 36. connection. 连接的初始段大小最大值*/
37. u8_t sa; /**< Retransmission time-out calculation state 38. variable. 重传超时计算状态变量*/
39. u8_t sv; /**< Retransmission time-out calculation state 40. variable. 重传超时计算状态变量*/
41. u8_t rto; /**< Retransmission time-out.重传超时 */ 42. u8_t tcpstateflags; /**< TCP state and flags. TCP状态标志*/ 43. u8_t timer; /**< The retransmission timer. 重传定时器*/ 44. u8_t nrtx; /**< The number of retransmissions for the last 45. segment sent. 上次发送的段的重传次数*/ 46. /** The application state.应用状态 */ 47. uip_tcp_appstate_t appstate;};
uip_udp_conn结构体 代表一个uIP UDP连接 定义于uip.h的1210行。
1. 2. 3. 4. 5. 6. 7.
struct uip_udp_conn {
uip_ipaddr_t ripaddr; /**< The IP address of the remote peer. 远程对等主机的ip地址*/ u16_t lport; /**< The local port number in network byte order. 本地端口号,以网络字节顺序*/ u16_t rport; /**< The remote port number in network byte order. 远程端口号,以网络字节顺序*/ u8_t ttl; /**< Default time-to-live. 默认存活时间*/ /** The application state. 应用状态*/ uip_udp_appstate_t appstate;};
3. uip_udp_conn结构体 代表一个uIP UDP连接 定义于uip.h的1210行。
8. 9.
struct uip_udp_conn {
uip_ipaddr_t ripaddr; /**< The IP address of the remote peer. 远程对等主机的ip地址*/
10. u16_t lport; /**< The local port number in network byte order. 本地端口号,以网络字节顺序*/ 11. u16_t rport; /**< The remote port number in network byte order. 远程端口号,以网络字节顺序*/ 12. u8_t ttl; /**< Default time-to-live. 默认存活时间*/ 13.
14. /** The application state. 应用状态*/ 15. uip_udp_appstate_t appstate;
};
4. void uip_setipid(u16_t id)
void uip_setipid(u16_t id) { ipid = id; }//此函数只是将传来的参数赋值给ipid.
UIP中文文档第三 uIP配置函数
uIP配置函数用于设置一些如ip地址等的uIP运行时参数.
它包括以下一些函数:
#define uip_sethostaddr(addr) 设定主机IP地址 #define uip_gethostaddr(addr) 获取主机IP地址 #define uip_setdraddr(addr) 设定默认路由器地址 #define uip_getdraddr(addr) 获取默认路由器地址 #define uip_setnetmask(addr) 设定网络掩码 #define uip_getnetmask(addr) 获取网络掩码
#define uip_setethaddr(eaddr) 设定以态网MAC地址
前六个函数皆定义于uip.h,最后一个定义于uip_arp.h 1. uip_sethostaddr(addr)
#define uip_sethostaddr(addr) uip_ipaddr_copy(uip_hostaddr, (addr))
获取主机IP地址,主机IP地址由4字节的数列表示,第一个八位数就是数组的第一个成员.(八位数,octet,是网络术语,即是一个字节,但网络术语不叫字节),常用使用方法如下:
1. 2. 3.
uip_ipaddr_t addr;
uip_ipaddr(&addr, 192,168,1,2); uip_sethostaddr(&addr);
参数:
addr 指向IP地址类型uip_ipaddr_t的指针. 参考: uip_ipaddr() 应用例程:
dhcpc.c, example-mainloop-with-arp.c, and example-mainloop-without-arp.c. 定义于uip.h的106行. 2. uip_gethostaddr(addr)
#define uip_gethostaddr(addr) uip_ipaddr_copy((addr), uip_hostaddr)使用方法例:
1. 2.
uip_ipaddr_t hostaddr; uip_gethostaddr(&hostaddr) 3. uip_setdraddr(addr) 设定的是默认路由器地址.
#define uip_setdraddr(addr) uip_ipaddr_copy(uip_draddr, (addr))
4. uip_getdraddr(addr) 获取默认路由器地址
#define uip_getdraddr(addr) uip_ipaddr_copy((addr), uip_draddr) 5. uip_setnetmask(addr) 设定子网掩码
#define uip_setnetmask(addr) uip_ipaddr_copy(uip_netmask, (addr)) 6. uip_getnetmask(addr) 获取子网掩码
#define uip_getnetmask(addr) uip_ipaddr_copy((addr), uip_netmask) 7. uip_setethaddr(eaddr)
1. 2. 3. 4. 5. 6.
#define uip_setethaddr(eaddr) do {uip_ethaddr.addr[0] = eaddr.addr[0]; \\ uip_ethaddr.addr[1] = eaddr.addr[1];\\ uip_ethaddr.addr[2] = eaddr.addr[2];\\ uip_ethaddr.addr[3] = eaddr.addr[3];\\ uip_ethaddr.addr[4] = eaddr.addr[4];\\ uip_ethaddr.addr[5] = eaddr.addr[5];} while(0)
获取以太网的MAC地址,ARP代码需要知道以太网卡的MAC地址,才能回应ARP查询,并产生以太网头. 注意:
此宏只能用来确定以太网卡的MAC地址,并不能用来改变它. 参数:
eaddr 指向uip_eth_addr结构体的指针,里面包含了以太网卡的MAC地址. 定义于:
line 134 of file uip_arp.h.
1. 2. 3.
struct uip_eth_addr { u8_t addr[6]; };
定义于:line1542of file uip.h.