netinet.h 5.1 KB

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