2017-07-29 20:32:26 +08:00
/*
* network . h
*
* Created on : Jul 29 , 2017
* Author : wangyu
*/
2017-09-01 17:40:55 -05:00
# ifndef UDP2RAW_NETWORK_H_
# define UDP2RAW_NETWORK_H_
2017-07-29 20:32:26 +08:00
extern int raw_recv_fd ;
extern int raw_send_fd ;
extern int seq_mode ;
2017-09-06 05:31:29 -05:00
extern int max_seq_mode ;
2017-07-29 20:32:26 +08:00
extern int filter_port ;
2018-07-17 11:33:02 -05:00
//extern u32_t bind_address_uint32;
2017-07-29 20:32:26 +08:00
extern int disable_bpf_filter ;
2017-08-07 13:58:42 +08:00
extern int lower_level ;
2017-08-21 20:26:55 +08:00
extern int lower_level_manual ;
2017-08-07 13:58:42 +08:00
extern char if_name [ 100 ] ;
2018-07-18 02:02:30 -05:00
extern char dev [ 100 ] ;
2017-08-18 00:23:18 +08:00
extern unsigned char dest_hw_addr [ ] ;
2017-08-07 13:58:42 +08:00
2017-10-11 09:10:38 -05:00
extern int random_drop ;
2017-08-21 20:26:55 +08:00
extern int ifindex ;
2018-07-21 14:23:13 -05:00
extern char g_packet_buf [ buf_len ] ;
extern int g_packet_buf_len ;
extern int g_packet_buf_cnt ;
2018-07-26 11:25:36 -05:00
struct my_iphdr
{
# ifdef UDP2RAW_LITTLE_ENDIAN
unsigned char ihl : 4 ;
unsigned char version : 4 ;
# else
unsigned char version : 4 ;
unsigned char ihl : 4 ;
# endif
u_int8_t tos ;
u_int16_t tot_len ;
u_int16_t id ;
u_int16_t frag_off ;
u_int8_t ttl ;
u_int8_t protocol ;
u_int16_t check ;
u_int32_t saddr ;
u_int32_t daddr ;
/*The options start here. */
} ;
struct my_udphdr
{
/*__extension__*/ union
{
struct
{
u_int16_t uh_sport ; /* source port */
u_int16_t uh_dport ; /* destination port */
u_int16_t uh_ulen ; /* udp length */
u_int16_t uh_sum ; /* udp checksum */
} ;
struct
{
u_int16_t source ;
u_int16_t dest ;
u_int16_t len ;
u_int16_t check ;
} ;
} ;
} ;
struct my_tcphdr
{
/*__extension__*/ union
{
struct
{
u_int16_t th_sport ; /* source port */
u_int16_t th_dport ; /* destination port */
u_int32_t th_seq ; /* sequence number */
u_int32_t th_ack ; /* acknowledgement number */
# ifdef UDP2RAW_LITTLE_ENDIAN
u_int8_t th_x2 : 4 ; /* (unused) */
u_int8_t tc_off : 4 ; /* data offset */
# else
u_int8_t th_off : 4 ; /* data offset */
u_int8_t th_x2 : 4 ; /* (unused) */
# endif
u_int8_t th_flags ;
# define TH_FIN 0x01
# define TH_SYN 0x02
# define TH_RST 0x04
# define TH_PUSH 0x08
# define TH_ACK 0x10
# define TH_URG 0x20
u_int16_t th_win ; /* window */
u_int16_t th_sum ; /* checksum */
u_int16_t th_urp ; /* urgent pointer */
} ;
struct
{
u_int16_t source ;
u_int16_t dest ;
u_int32_t seq ;
u_int32_t ack_seq ;
# ifdef UDP2RAW_LITTLE_ENDIAN
u_int16_t res1 : 4 ;
u_int16_t doff : 4 ;
u_int16_t fin : 1 ;
u_int16_t syn : 1 ;
u_int16_t rst : 1 ;
u_int16_t psh : 1 ;
u_int16_t ack : 1 ;
u_int16_t urg : 1 ;
u_int16_t res2 : 2 ;
# else
u_int16_t doff : 4 ;
u_int16_t res1 : 4 ;
u_int16_t res2 : 2 ;
u_int16_t urg : 1 ;
u_int16_t ack : 1 ;
u_int16_t psh : 1 ;
u_int16_t rst : 1 ;
u_int16_t syn : 1 ;
u_int16_t fin : 1 ;
# endif
u_int16_t window ;
u_int16_t check ;
u_int16_t urg_ptr ;
} ;
} ;
} ;
2018-07-24 05:36:56 -05:00
struct my_ip6hdr
{
2018-07-26 11:25:36 -05:00
# ifdef UDP2RAW_LITTLE_ENDIAN
2018-07-25 02:23:04 -05:00
uint8_t traffic_class_high : 4 ;
uint8_t version : 4 ;
uint8_t flow_label_high : 4 ;
uint8_t traffic_class_low : 4 ;
2018-07-26 11:25:36 -05:00
# else
2018-07-25 02:23:04 -05:00
uint8_t version : 4 ;
uint8_t traffic_class_high : 4 ;
uint8_t traffic_class_low : 4 ;
uint8_t flow_label_high : 4 ;
2018-07-24 05:36:56 -05:00
# endif
2018-07-25 02:23:04 -05:00
u_int16_t flow_label_low ;
2018-07-24 05:36:56 -05:00
u_int16_t payload_len ;
2018-07-25 02:23:04 -05:00
uint8_t next_header ;
uint8_t hop_limit ;
2018-07-24 05:36:56 -05:00
struct in6_addr src ;
struct in6_addr dst ;
} ;
2018-07-23 09:50:21 -05:00
2018-07-26 11:25:36 -05:00
struct my_icmphdr
2017-07-29 20:32:26 +08:00
{
uint8_t type ;
uint8_t code ;
uint16_t check_sum ;
uint16_t id ;
uint16_t seq ;
} ;
struct pseudo_header {
u_int32_t source_address ;
u_int32_t dest_address ;
u_int8_t placeholder ;
u_int8_t protocol ;
u_int16_t tcp_length ;
} ;
2018-07-24 05:36:56 -05:00
struct pseudo_header6 {
struct in6_addr src ;
struct in6_addr dst ;
u_int32_t tcp_length ;
u_int16_t placeholder1 ;
u_int8_t placeholder2 ;
u_int8_t next_header ;
} ;
2017-07-31 19:01:50 +08:00
struct packet_info_t //todo change this to union
2017-07-29 20:32:26 +08:00
{
uint8_t protocol ;
2018-07-22 08:59:09 -05:00
2018-07-24 08:33:42 -05:00
//u32_t src_ip;
//u32_t dst_ip;
2018-07-23 09:50:21 -05:00
my_ip_t new_src_ip ;
my_ip_t new_dst_ip ;
2018-07-22 08:59:09 -05:00
uint16_t src_port ;
2017-07-29 20:32:26 +08:00
uint16_t dst_port ;
//tcp_part:
bool syn , ack , psh , rst ;
2017-08-04 17:12:23 +08:00
u32_t seq , ack_seq ;
2017-08-01 20:14:13 +08:00
2017-09-06 05:31:29 -05:00
u32_t ack_seq_counter ;
2017-07-29 20:32:26 +08:00
2017-08-04 17:12:23 +08:00
u32_t ts , ts_ack ;
2017-07-29 20:32:26 +08:00
2018-07-26 12:41:55 -05:00
uint16_t my_icmp_seq ;
2017-07-29 20:32:26 +08:00
bool has_ts ;
2017-08-21 20:26:55 +08:00
sockaddr_ll addr_ll ;
2017-09-06 01:31:29 -05:00
i32_t data_len ;
2017-07-30 03:46:28 +08:00
packet_info_t ( ) ;
2017-07-29 20:32:26 +08:00
} ;
struct raw_info_t
{
packet_info_t send_info ;
packet_info_t recv_info ;
2017-09-06 01:31:29 -05:00
//int last_send_len;
//int last_recv_len;
2018-07-23 13:52:36 -05:00
bool peek = 0 ;
//bool csum=1;
2017-09-06 01:37:14 -05:00
u32_t reserved_send_seq ;
2017-08-03 23:32:19 +08:00
//uint32_t first_seq,first_ack_seq;
2018-02-24 17:26:29 -06:00
int rst_received = 0 ;
bool disabled = 0 ;
2017-08-01 20:14:13 +08:00
2017-07-29 20:32:26 +08:00
} ; //g_raw_info;
int init_raw_socket ( ) ;
void init_filter ( int port ) ;
void remove_filter ( ) ;
2017-09-23 03:05:23 -05:00
2018-07-18 02:02:30 -05:00
int init_ifindex ( const char * if_name , int fd , int & index ) ;
2017-07-29 20:32:26 +08:00
2017-08-26 05:38:33 -05:00
int find_lower_level_info ( u32_t ip , u32_t & dest_ip , string & if_name , string & hw ) ;
2017-07-29 20:32:26 +08:00
2017-09-23 03:05:23 -05:00
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
2018-07-17 11:33:02 -05:00
int get_src_adress2 ( address_t & output_addr , address_t remote_addr ) ;
2017-11-01 08:25:50 -05:00
int try_to_list_and_bind ( int & bind_fd , u32_t local_ip_uint32 , int port ) ; //try to bind to a port,may fail.
2017-09-23 03:05:23 -05:00
2017-11-01 08:25:50 -05:00
int client_bind_to_a_new_port ( int & bind_fd , u32_t local_ip_uint32 ) ; //find a free port and bind to it.
2018-07-17 11:33:02 -05:00
int client_bind_to_a_new_port2 ( int & fd , const address_t & address ) ;
2017-09-23 03:05:23 -05:00
2018-07-21 14:23:13 -05:00
int discard_raw_packet ( ) ;
int pre_recv_raw_packet ( ) ;
2017-07-29 20:32:26 +08:00
int send_raw_ip ( raw_info_t & raw_info , const char * payload , int payloadlen ) ;
2018-07-23 13:52:36 -05:00
int peek_raw ( raw_info_t & peek_info ) ;
2017-07-29 20:32:26 +08:00
int recv_raw_ip ( raw_info_t & raw_info , char * & payload , int & payloadlen ) ;
int send_raw_icmp ( raw_info_t & raw_info , const char * payload , int payloadlen ) ;
int send_raw_udp ( raw_info_t & raw_info , const char * payload , int payloadlen ) ;
int send_raw_tcp ( raw_info_t & raw_info , const char * payload , int payloadlen ) ;
int recv_raw_icmp ( raw_info_t & raw_info , char * & payload , int & payloadlen ) ;
int recv_raw_udp ( raw_info_t & raw_info , char * & payload , int & payloadlen ) ;
int recv_raw_tcp ( raw_info_t & raw_info , char * & payload , int & payloadlen ) ;
2017-08-04 03:29:53 +08:00
//int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen);
2017-07-29 20:32:26 +08:00
2017-08-04 03:29:53 +08:00
//int recv_raw(raw_info_t &raw_info,char * &payload,int &payloadlen);
2017-07-29 20:32:26 +08:00
2017-08-03 23:32:19 +08:00
int send_raw0 ( raw_info_t & raw_info , const char * payload , int payloadlen ) ;
int recv_raw0 ( raw_info_t & raw_info , char * & payload , int & payloadlen ) ;
int after_send_raw0 ( raw_info_t & raw_info ) ;
2017-07-29 20:32:26 +08:00
2017-08-03 23:32:19 +08:00
int after_recv_raw0 ( raw_info_t & raw_info ) ;
2017-07-29 20:32:26 +08:00
# endif /* NETWORK_H_ */