network.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * network.h
  3. *
  4. * Created on: Jul 29, 2017
  5. * Author: wangyu
  6. */
  7. #ifndef UDP2RAW_NETWORK_H_
  8. #define UDP2RAW_NETWORK_H_
  9. extern int raw_recv_fd;
  10. extern int raw_send_fd;
  11. extern int use_tcp_dummy_socket;
  12. extern int seq_mode;
  13. extern int max_seq_mode;
  14. extern int filter_port;
  15. //extern u32_t bind_address_uint32;
  16. extern int disable_bpf_filter;
  17. extern int lower_level;
  18. extern int lower_level_manual;
  19. extern char if_name[100];
  20. extern char dev[100];
  21. extern unsigned char dest_hw_addr[];
  22. extern int random_drop;
  23. extern int ifindex;
  24. extern char g_packet_buf[huge_buf_len];
  25. extern int g_packet_buf_len;
  26. extern int g_packet_buf_cnt;
  27. struct my_iphdr
  28. {
  29. #ifdef UDP2RAW_LITTLE_ENDIAN
  30. unsigned char ihl:4;
  31. unsigned char version:4;
  32. #else
  33. unsigned char version:4;
  34. unsigned char ihl:4;
  35. #endif
  36. u_int8_t tos;
  37. u_int16_t tot_len;
  38. u_int16_t id;
  39. u_int16_t frag_off;
  40. u_int8_t ttl;
  41. u_int8_t protocol;
  42. u_int16_t check;
  43. u_int32_t saddr;
  44. u_int32_t daddr;
  45. /*The options start here. */
  46. };
  47. struct my_udphdr
  48. {
  49. /*__extension__*/ union
  50. {
  51. struct
  52. {
  53. u_int16_t uh_sport; /* source port */
  54. u_int16_t uh_dport; /* destination port */
  55. u_int16_t uh_ulen; /* udp length */
  56. u_int16_t uh_sum; /* udp checksum */
  57. };
  58. struct
  59. {
  60. u_int16_t source;
  61. u_int16_t dest;
  62. u_int16_t len;
  63. u_int16_t check;
  64. };
  65. };
  66. };
  67. struct my_tcphdr
  68. {
  69. /*__extension__*/ union
  70. {
  71. struct
  72. {
  73. u_int16_t th_sport; /* source port */
  74. u_int16_t th_dport; /* destination port */
  75. u_int32_t th_seq; /* sequence number */
  76. u_int32_t th_ack; /* acknowledgement number */
  77. # ifdef UDP2RAW_LITTLE_ENDIAN
  78. u_int8_t th_x2:4; /* (unused) */
  79. u_int8_t tc_off:4; /* data offset */
  80. # else
  81. u_int8_t th_off:4; /* data offset */
  82. u_int8_t th_x2:4; /* (unused) */
  83. # endif
  84. u_int8_t th_flags;
  85. # define TH_FIN 0x01
  86. # define TH_SYN 0x02
  87. # define TH_RST 0x04
  88. # define TH_PUSH 0x08
  89. # define TH_ACK 0x10
  90. # define TH_URG 0x20
  91. u_int16_t th_win; /* window */
  92. u_int16_t th_sum; /* checksum */
  93. u_int16_t th_urp; /* urgent pointer */
  94. };
  95. struct
  96. {
  97. u_int16_t source;
  98. u_int16_t dest;
  99. u_int32_t seq;
  100. u_int32_t ack_seq;
  101. # ifdef UDP2RAW_LITTLE_ENDIAN
  102. u_int16_t res1:4;
  103. u_int16_t doff:4;
  104. u_int16_t fin:1;
  105. u_int16_t syn:1;
  106. u_int16_t rst:1;
  107. u_int16_t psh:1;
  108. u_int16_t ack:1;
  109. u_int16_t urg:1;
  110. u_int16_t res2:2;
  111. # else
  112. u_int16_t doff:4;
  113. u_int16_t res1:4;
  114. u_int16_t res2:2;
  115. u_int16_t urg:1;
  116. u_int16_t ack:1;
  117. u_int16_t psh:1;
  118. u_int16_t rst:1;
  119. u_int16_t syn:1;
  120. u_int16_t fin:1;
  121. # endif
  122. u_int16_t window;
  123. u_int16_t check;
  124. u_int16_t urg_ptr;
  125. };
  126. };
  127. };
  128. struct my_ip6hdr
  129. {
  130. # ifdef UDP2RAW_LITTLE_ENDIAN
  131. uint8_t traffic_class_high:4;
  132. uint8_t version:4;
  133. uint8_t flow_label_high:4;
  134. uint8_t traffic_class_low:4;
  135. #else
  136. uint8_t version:4;
  137. uint8_t traffic_class_high:4;
  138. uint8_t traffic_class_low:4;
  139. uint8_t flow_label_high:4;
  140. #endif
  141. u_int16_t flow_label_low;
  142. u_int16_t payload_len;
  143. uint8_t next_header;
  144. uint8_t hop_limit;
  145. struct in6_addr src;
  146. struct in6_addr dst;
  147. };
  148. struct my_icmphdr
  149. {
  150. uint8_t type;
  151. uint8_t code;
  152. uint16_t check_sum;
  153. uint16_t id;
  154. uint16_t seq;
  155. };
  156. struct pseudo_header {
  157. u_int32_t source_address;
  158. u_int32_t dest_address;
  159. u_int8_t placeholder;
  160. u_int8_t protocol;
  161. u_int16_t tcp_length;
  162. };
  163. struct pseudo_header6 {
  164. struct in6_addr src;
  165. struct in6_addr dst;
  166. u_int32_t tcp_length;
  167. u_int16_t placeholder1;
  168. u_int8_t placeholder2;
  169. u_int8_t next_header;
  170. };
  171. struct packet_info_t //todo change this to union
  172. {
  173. uint8_t protocol;
  174. //u32_t src_ip;
  175. //u32_t dst_ip;
  176. my_ip_t new_src_ip;
  177. my_ip_t new_dst_ip;
  178. uint16_t src_port;
  179. uint16_t dst_port;
  180. //tcp_part:
  181. bool syn,ack,psh,rst;
  182. u32_t seq,ack_seq;
  183. u32_t ack_seq_counter;
  184. u32_t ts,ts_ack;
  185. uint16_t my_icmp_seq;
  186. bool has_ts;
  187. sockaddr_ll addr_ll;
  188. i32_t data_len;
  189. packet_info_t();
  190. };
  191. struct raw_info_t
  192. {
  193. packet_info_t send_info;
  194. packet_info_t recv_info;
  195. //int last_send_len;
  196. //int last_recv_len;
  197. bool peek=0;
  198. //bool csum=1;
  199. u32_t reserved_send_seq;
  200. //uint32_t first_seq,first_ack_seq;
  201. int rst_received=0;
  202. bool disabled=0;
  203. };//g_raw_info;
  204. int init_raw_socket();
  205. void init_filter(int port);
  206. void remove_filter();
  207. int init_ifindex(const char * if_name,int fd,int &index);
  208. int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw);
  209. int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port); //a trick to get src adress for a dest adress,so that we can use the src address in raw socket as source ip
  210. int get_src_adress2(address_t &output_addr,address_t remote_addr);
  211. int try_to_list_and_bind(int & bind_fd,u32_t local_ip_uint32,int port); //try to bind to a port,may fail.
  212. int try_to_list_and_bind2(int &fd,address_t address);
  213. int client_bind_to_a_new_port(int & bind_fd,u32_t local_ip_uint32);//find a free port and bind to it.
  214. int client_bind_to_a_new_port2(int &fd,const address_t& address);
  215. int discard_raw_packet();
  216. int pre_recv_raw_packet();
  217. int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen);
  218. int peek_raw(raw_info_t &peek_info);
  219. int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen);
  220. int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen);
  221. int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen);
  222. int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen);
  223. int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen);
  224. int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen);
  225. int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen);
  226. //int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen);
  227. //int recv_raw(raw_info_t &raw_info,char * &payload,int &payloadlen);
  228. int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen);
  229. int recv_raw0(raw_info_t &raw_info,char * &payload,int &payloadlen);
  230. int after_send_raw0(raw_info_t &raw_info);
  231. int after_recv_raw0(raw_info_t &raw_info);
  232. #endif /* NETWORK_H_ */