找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1351|回复: 0
打印 上一主题 下一主题
收起左侧

uIP网络开发学习资料和源码

[复制链接]
跳转到指定楼层
楼主




单片机源程序如下:
  1. #define DEBUG_PRINTF(...) /*printf(__VA_ARGS__)*/


  2. #include "uip.h"
  3. #include "uipopt.h"
  4. #include "uip_arch.h"

  5. #if UIP_CONF_IPV6
  6. #include "uip-neighbor.h"
  7. #endif /* UIP_CONF_IPV6 */

  8. #include <string.h>

  9. /*---------------------------------------------------------------------------*/
  10. /* Variable definitions. */


  11. /* The IP address of this host. If it is defined to be fixed (by
  12.    setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set
  13.    here. Otherwise, the address */
  14. #if UIP_FIXEDADDR > 0
  15. const uip_ipaddr_t uip_hostaddr =
  16.   {HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
  17.    HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
  18. const uip_ipaddr_t uip_draddr =
  19.   {HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
  20.    HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
  21. const uip_ipaddr_t uip_netmask =
  22.   {HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
  23.    HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
  24. #else
  25. uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask;
  26. #endif /* UIP_FIXEDADDR */

  27. static const uip_ipaddr_t all_ones_addr =
  28. #if UIP_CONF_IPV6
  29.   {0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff};
  30. #else /* UIP_CONF_IPV6 */
  31.   {0xffff,0xffff};
  32. #endif /* UIP_CONF_IPV6 */
  33. static const uip_ipaddr_t all_zeroes_addr =
  34. #if UIP_CONF_IPV6
  35.   {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
  36. #else /* UIP_CONF_IPV6 */
  37.   {0x0000,0x0000};
  38. #endif /* UIP_CONF_IPV6 */


  39. #if UIP_FIXEDETHADDR
  40. const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
  41.                                           UIP_ETHADDR1,
  42.                                           UIP_ETHADDR2,
  43.                                           UIP_ETHADDR3,
  44.                                           UIP_ETHADDR4,
  45.                                           UIP_ETHADDR5}};
  46. #else
  47. struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}};
  48. #endif

  49. #ifndef UIP_CONF_EXTERNAL_BUFFER
  50. u8_t uip_buf[UIP_BUFSIZE + 2];   /* The packet buffer that contains
  51.                                     incoming packets. */
  52. #endif /* UIP_CONF_EXTERNAL_BUFFER */

  53. void *uip_appdata;               /* The uip_appdata pointer points to
  54.                                     application data. */
  55. void *uip_sappdata;              /* The uip_appdata pointer points to
  56.                                     the application data which is to
  57.                                     be sent. */
  58. #if UIP_URGDATA > 0
  59. void *uip_urgdata;               /* The uip_urgdata pointer points to
  60.                                        urgent data (out-of-band data), if
  61.                                        present. */
  62. u16_t uip_urglen, uip_surglen;
  63. #endif /* UIP_URGDATA > 0 */

  64. u16_t uip_len, uip_slen;
  65.                              /* The uip_len is either 8 or 16 bits,
  66.                                 depending on the maximum packet
  67.                                 size. */

  68. u8_t uip_flags;     /* The uip_flags variable is used for
  69.                                 communication between the TCP/IP stack
  70.                                 and the application program. */
  71. struct uip_conn *uip_conn;   /* uip_conn always points to the current
  72.                                 connection. */

  73. struct uip_conn uip_conns[UIP_CONNS];
  74.                              /* The uip_conns array holds all TCP
  75.                                 connections. */
  76. u16_t uip_listenports[UIP_LISTENPORTS];
  77.                              /* The uip_listenports list all currently
  78.                                 listning ports. */
  79. #if UIP_UDP
  80. struct uip_udp_conn *uip_udp_conn;
  81. struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
  82. #endif /* UIP_UDP */

  83. static u16_t ipid;           /* Ths ipid variable is an increasing
  84.                                 number that is used for the IP ID
  85.                                 field. */

  86. void uip_setipid(u16_t id) { ipid = id; }

  87. static u8_t iss[4];          /* The iss variable is used for the TCP
  88.                                 initial sequence number. */

  89. #if UIP_ACTIVE_OPEN
  90. static u16_t lastport;       /* Keeps track of the last port used for
  91.                                 a new connection. */
  92. #endif /* UIP_ACTIVE_OPEN */

  93. /* Temporary variables. */
  94. u8_t uip_acc32[4];
  95. static u8_t c, opt;
  96. static u16_t tmp16;

  97. /* Structures and definitions. */
  98. #define TCP_FIN 0x01
  99. #define TCP_SYN 0x02
  100. #define TCP_RST 0x04
  101. #define TCP_PSH 0x08
  102. #define TCP_ACK 0x10
  103. #define TCP_URG 0x20
  104. #define TCP_CTL 0x3f

  105. #define TCP_OPT_END     0   /* End of TCP options list */
  106. #define TCP_OPT_NOOP    1   /* "No-operation" TCP option */
  107. #define TCP_OPT_MSS     2   /* Maximum segment size TCP option */

  108. #define TCP_OPT_MSS_LEN 4   /* Length of TCP MSS option. */

  109. #define ICMP_ECHO_REPLY 0
  110. #define ICMP_ECHO       8

  111. #define ICMP6_ECHO_REPLY             129
  112. #define ICMP6_ECHO                   128
  113. #define ICMP6_NEIGHBOR_SOLICITATION  135
  114. #define ICMP6_NEIGHBOR_ADVERTISEMENT 136

  115. #define ICMP6_FLAG_S (1 << 6)

  116. #define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1
  117. #define ICMP6_OPTION_TARGET_LINK_ADDRESS 2


  118. /* Macros. */
  119. #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
  120. #define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])
  121. #define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
  122. #define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])


  123. #if UIP_STATISTICS == 1
  124. struct uip_stats uip_stat;
  125. #define UIP_STAT(s) s
  126. #else
  127. #define UIP_STAT(s)
  128. #endif /* UIP_STATISTICS == 1 */

  129. #if UIP_LOGGING == 1
  130. #include <stdio.h>
  131. void uip_log(char *msg);
  132. #define UIP_LOG(m) uip_log(m)
  133. #else
  134. #define UIP_LOG(m)
  135. #endif /* UIP_LOGGING == 1 */

  136. #if ! UIP_ARCH_ADD32
  137. void
  138. uip_add32(u8_t *op32, u16_t op16)
  139. {
  140.   uip_acc32[3] = op32[3] + (op16 & 0xff);
  141.   uip_acc32[2] = op32[2] + (op16 >> 8);
  142.   uip_acc32[1] = op32[1];
  143.   uip_acc32[0] = op32[0];
  144.   
  145.   if(uip_acc32[2] < (op16 >> 8)) {
  146.     ++uip_acc32[1];
  147.     if(uip_acc32[1] == 0) {
  148.       ++uip_acc32[0];
  149.     }
  150.   }
  151.   
  152.   
  153.   if(uip_acc32[3] < (op16 & 0xff)) {
  154.     ++uip_acc32[2];
  155.     if(uip_acc32[2] == 0) {
  156.       ++uip_acc32[1];
  157.       if(uip_acc32[1] == 0) {
  158.         ++uip_acc32[0];
  159.       }
  160.     }
  161.   }
  162. }

  163. #endif /* UIP_ARCH_ADD32 */

  164. #if ! UIP_ARCH_CHKSUM
  165. /*---------------------------------------------------------------------------*/
  166. static u16_t
  167. chksum(u16_t sum, const u8_t *data, u16_t len)
  168. {
  169.   u16_t t;
  170.   const u8_t *dataptr;
  171.   const u8_t *last_byte;

  172.   dataptr = data;
  173.   last_byte = data + len - 1;
  174.   
  175.   while(dataptr < last_byte) {        /* At least two more bytes */
  176.     t = (dataptr[0] << 8) + dataptr[1];
  177.     sum += t;
  178.     if(sum < t) {
  179.       sum++;                /* carry */
  180.     }
  181.     dataptr += 2;
  182.   }
  183.   
  184.   if(dataptr == last_byte) {
  185.     t = (dataptr[0] << 8) + 0;
  186.     sum += t;
  187.     if(sum < t) {
  188.       sum++;                /* carry */
  189.     }
  190.   }

  191.   /* Return sum in host byte order. */
  192.   return sum;
  193. }
  194. /*---------------------------------------------------------------------------*/
  195. u16_t
  196. uip_chksum(u16_t *data, u16_t len)
  197. {
  198.   return htons(chksum(0, (u8_t *)data, len));
  199. }
  200. /*---------------------------------------------------------------------------*/

  201.       
复制代码

所有资料51hei提供下载:
uIP学习资料.7z (3.73 MB, 下载次数: 23)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表