netinet.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef HW_NETINET_H_
  2. #define HW_NETINET_H_
  3. #include "hplatform.h"
  4. /*
  5. #ifdef OS_UNIX
  6. #include <netinet/in.h>
  7. #include <netinet/ip.h>
  8. #include <netinet/udp.h>
  9. #include <netinet/tcp.h>
  10. #include <netinet/ip_icmp.h>
  11. typedef struct iphdr iphdr_t;
  12. typedef struct udphdr udphdr_t;
  13. typedef struct tcphdr tcphdr_t;
  14. typedef struct icmphdr icmphdr_t;
  15. typedef struct icmp icmp_t;
  16. #else
  17. */
  18. // sizeof(iphdr_t) = 20
  19. typedef struct iphdr_s {
  20. #if BYTE_ORDER == LITTLE_ENDIAN
  21. uint8_t ihl:4; // ip header length
  22. uint8_t version:4;
  23. #elif BYTE_ORDER == BIG_ENDIAN
  24. uint8_t version:4;
  25. uint8_t ihl:4;
  26. #else
  27. #error "BYTE_ORDER undefined!"
  28. #endif
  29. uint8_t tos; // type of service
  30. uint16_t tot_len; // total length
  31. uint16_t id;
  32. uint16_t frag_off; // fragment offset
  33. uint8_t ttl; // Time To Live
  34. uint8_t protocol;
  35. uint16_t check; // checksum
  36. uint32_t saddr; // srcaddr
  37. uint32_t daddr; // dstaddr
  38. /*The options start here.*/
  39. } iphdr_t;
  40. // sizeof(udphdr_t) = 8
  41. typedef struct udphdr_s {
  42. uint16_t source; // source port
  43. uint16_t dest; // dest port
  44. uint16_t len; // udp length
  45. uint16_t check; // checksum
  46. } udphdr_t;
  47. // sizeof(tcphdr_t) = 20
  48. typedef struct tcphdr_s {
  49. uint16_t source; // source port
  50. uint16_t dest; // dest port
  51. uint32_t seq; // sequence
  52. uint32_t ack_seq;
  53. #if BYTE_ORDER == LITTLE_ENDIAN
  54. uint16_t res1:4;
  55. uint16_t doff:4;
  56. uint16_t fin:1;
  57. uint16_t syn:1;
  58. uint16_t rst:1;
  59. uint16_t psh:1;
  60. uint16_t ack:1;
  61. uint16_t urg:1;
  62. uint16_t res2:2;
  63. #elif BYTE_ORDER == BIG_ENDIAN
  64. uint16_t doff:4;
  65. uint16_t res1:4;
  66. uint16_t res2:2;
  67. uint16_t urg:1;
  68. uint16_t ack:1;
  69. uint16_t psh:1;
  70. uint16_t rst:1;
  71. uint16_t syn:1;
  72. uint16_t fin:1;
  73. #else
  74. #error "BYTE_ORDER undefined!"
  75. #endif
  76. uint16_t window;
  77. uint16_t check; // checksum
  78. uint16_t urg_ptr; // urgent pointer
  79. } tcphdr_t;
  80. //----------------------icmp----------------------------------
  81. #define ICMP_ECHOREPLY 0 /* Echo Reply */
  82. #define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
  83. #define ICMP_SOURCE_QUENCH 4 /* Source Quench */
  84. #define ICMP_REDIRECT 5 /* Redirect (change route) */
  85. #define ICMP_ECHO 8 /* Echo Request */
  86. #define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
  87. #define ICMP_PARAMETERPROB 12 /* Parameter Problem */
  88. #define ICMP_TIMESTAMP 13 /* Timestamp Request */
  89. #define ICMP_TIMESTAMPREPLY 14 /* Timestamp Reply */
  90. #define ICMP_INFO_REQUEST 15 /* Information Request */
  91. #define ICMP_INFO_REPLY 16 /* Information Reply */
  92. #define ICMP_ADDRESS 17 /* Address Mask Request */
  93. #define ICMP_ADDRESSREPLY 18 /* Address Mask Reply */
  94. // sizeof(icmphdr_t) = 8
  95. typedef struct icmphdr_s {
  96. uint8_t type; // message type
  97. uint8_t code; // type sub-code
  98. uint16_t checksum;
  99. union {
  100. struct {
  101. uint16_t id;
  102. uint16_t sequence;
  103. } echo;
  104. uint32_t gateway;
  105. struct {
  106. uint16_t reserved;
  107. uint16_t mtu;
  108. } frag;
  109. } un;
  110. } icmphdr_t;
  111. typedef struct icmp_s {
  112. uint8_t icmp_type;
  113. uint8_t icmp_code;
  114. uint16_t icmp_cksum;
  115. union {
  116. uint8_t ih_pptr;
  117. struct in_addr ih_gwaddr;
  118. struct ih_idseq {
  119. uint16_t icd_id;
  120. uint16_t icd_seq;
  121. } ih_idseq;
  122. uint32_t ih_void;
  123. struct ih_pmtu {
  124. uint16_t ipm_void;
  125. uint16_t ipm_nextmtu;
  126. } ih_pmtu;
  127. struct ih_rtradv {
  128. uint8_t irt_num_addrs;
  129. uint8_t irt_wpa;
  130. uint16_t irt_lifetime;
  131. } ih_rtradv;
  132. } icmp_hun;
  133. #define icmp_pptr icmp_hun.ih_pptr
  134. #define icmp_gwaddr icmp_hun.ih_gwaddr
  135. #define icmp_id icmp_hun.ih_idseq.icd_id
  136. #define icmp_seq icmp_hun.ih_idseq.icd_seq
  137. #define icmp_void icmp_hun.ih_void
  138. #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
  139. #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
  140. #define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
  141. #define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
  142. #define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
  143. union {
  144. struct {
  145. uint32_t its_otime;
  146. uint32_t its_rtime;
  147. uint32_t its_ttime;
  148. } id_ts;
  149. /*
  150. struct {
  151. struct ip idi_ip;
  152. } id_ip;
  153. struct icmp_ra_addr id_radv;
  154. */
  155. uint32_t id_mask;
  156. uint8_t id_data[1];
  157. } icmp_dun;
  158. #define icmp_otime icmp_dun.id_ts.its_otime
  159. #define icmp_rtime icmp_dun.id_ts.its_rtime
  160. #define icmp_ttime icmp_dun.id_ts.its_ttime
  161. #define icmp_ip icmp_dun.id_ip.idi_ip
  162. #define icmp_radv icmp_dun.id_radv
  163. #define icmp_mask icmp_dun.id_mask
  164. #define icmp_data icmp_dun.id_data
  165. } icmp_t;
  166. //#endif
  167. static inline uint16_t checksum(uint8_t* buf, int len) {
  168. unsigned int sum = 0;
  169. uint16_t* ptr = (uint16_t*)buf;
  170. while(len > 1) {
  171. sum += *ptr++;
  172. len -= 2;
  173. }
  174. if(len) {
  175. sum += *(uint8_t*)ptr;
  176. }
  177. sum = (sum >> 16) + (sum & 0xffff);
  178. sum += (sum >> 16);
  179. return (uint16_t)(~sum);
  180. };
  181. #endif // HW_NETINET_H_