diff --git a/common.cpp b/common.cpp index 6b8b825..2f98b1b 100644 --- a/common.cpp +++ b/common.cpp @@ -90,6 +90,46 @@ int address_t::from_str(char *str) return 0; } +int address_t::from_str_ip_only(char * str) +{ + clear(); + + u32_t type; + + if(strchr(str,':')==NULL) + type=AF_INET; + else + type=AF_INET6; + + ((sockaddr*)&inner)->sa_family=type; + + int ret; + if(type==AF_INET) + { + ret=inet_pton(type, str,&inner.ipv4.sin_addr); + } + else + { + ret=inet_pton(type, str,&inner.ipv6.sin6_addr); + } + + if(ret==0) // 0 if address type doesnt match + { + mylog(log_error,"confusion in parsing %s, %d\n",str,ret); + myexit(-1); + } + else if(ret==1) // inet_pton returns 1 on success + { + //okay + } + else + { + mylog(log_error,"ip_addr %s is invalid, %d\n",str,ret); + myexit(-1); + } + return 0; +} + char * address_t::get_str() { static char res[max_addr_len]; @@ -220,7 +260,74 @@ int address_t::new_connected_udp_fd() return new_udp_fd; } +bool my_ip_t::equal (const my_ip_t &b) const +{ + //extern int raw_ip_version; + if(raw_ip_version==AF_INET) + { + return v4==b.v4; + }else if(raw_ip_version==AF_INET) + { + return memcmp(&v6,&b.v6,sizeof(v6)); + } + assert(0==1); + return 0; +} +char * my_ip_t::get_str1() const +{ + static char res[max_addr_len]; + if(raw_ip_version==AF_INET6) + { + assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0); + } + else if(raw_ip_version==AF_INET) + { + assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0); + } + return res; +} +char * my_ip_t::get_str2() const +{ + static char res[max_addr_len]; + if(raw_ip_version==AF_INET6) + { + assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0); + } + else if(raw_ip_version==AF_INET) + { + assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0); + } + return res; +} +/* +int my_ip_t::from_str(char * str) +{ + u32_t type; + if(strchr(str,':')==NULL) + type=AF_INET; + else + type=AF_INET6; + + int ret; + ret=inet_pton(type, str,this); + + if(ret==0) // 0 if address type doesnt match + { + mylog(log_error,"confusion in parsing %s, %d\n",str,ret); + myexit(-1); + } + else if(ret==1) // inet_pton returns 1 on success + { + //okay + } + else + { + mylog(log_error,"ip_addr %s is invalid, %d\n",str,ret); + myexit(-1); + } + return 0; +}*/ u64_t get_current_time() { timespec tmp_time; diff --git a/common.h b/common.h index b22833c..f5b6960 100644 --- a/common.h +++ b/common.h @@ -28,6 +28,7 @@ #include //Provides declarations for tcp header #include #include //Provides declarations for ip header +#include #include #include #include @@ -150,6 +151,8 @@ struct address_t //TODO scope id int from_str(char * str); + int from_str_ip_only(char * str); + int from_sockaddr(sockaddr *,socklen_t); char* get_str(); @@ -233,6 +236,19 @@ template <> }; } +union my_ip_t //just a simple version of address_t,stores ip only +{ + u32_t v4; + in6_addr v6; + + bool equal (const my_ip_t &b) const; + + //int from_str(char * str); + char * get_str1() const; + char * get_str2() const; + +}; + struct not_copy_able_t { not_copy_able_t() diff --git a/main.cpp b/main.cpp index ee2e640..abb3d04 100755 --- a/main.cpp +++ b/main.cpp @@ -100,7 +100,7 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is } else { - send_info.src_port = source_addr.get_port(); + send_info.src_port = source_port; } if (raw_mode == mode_icmp) diff --git a/misc.cpp b/misc.cpp index 8778c25..8e313cc 100644 --- a/misc.cpp +++ b/misc.cpp @@ -34,6 +34,8 @@ fd_manager_t fd_manager; //int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled address_t local_addr,remote_addr,source_addr,bind_addr; +int source_port=-1; + int bind_addr_used=0; int force_source_ip=0; //if --source-ip is enabled int force_source_port=0; @@ -132,7 +134,7 @@ void print_help() printf("common options,these options must be same on both side:\n"); printf(" --raw-mode avaliable values:faketcp(default),udp,icmp\n"); printf(" -k,--key password to gen symetric key,default:\"secret key\"\n"); - printf(" --cipher-mode avaliable values:aes128cbc(default),xor,none\n"); + printf(" --cipher-mode avaliable values:aes128cfb,aes128cbc(default),xor,none\n"); printf(" --auth-mode avaliable values:hmac_sha1,md5(default),crc32,simple,none\n"); printf(" -a,--auto-rule auto add (and delete) iptables rule\n"); printf(" -g,--gen-rule generate iptables rule then exit,so that you can copy and\n"); @@ -470,13 +472,13 @@ void process_arg(int argc, char *argv[]) //process all options { clear_iptables=1; } - /////////////////////fix this later - /* + else if(strcmp(long_options[option_index].name,"source-ip")==0) { mylog(log_debug,"parsing long option :source-ip\n"); - sscanf(optarg, "%s", source_ip); - mylog(log_debug,"source: %s\n",source_ip); + //sscanf(optarg, "%s", source_ip); + source_addr.from_str_ip_only(optarg); + mylog(log_debug,"source: %s\n",source_addr.get_ip()); force_source_ip=1; } else if(strcmp(long_options[option_index].name,"source-port")==0) @@ -484,7 +486,8 @@ void process_arg(int argc, char *argv[]) //process all options mylog(log_debug,"parsing long option :source-port\n"); sscanf(optarg, "%d", &source_port); mylog(log_info,"source: %d\n",source_port); - }*/ + force_source_port=1; + } else if(strcmp(long_options[option_index].name,"raw-mode")==0) { for(i=0;icheck=0; - int ret; - if(lower_level==0) - { - struct sockaddr_in sin={0}; - sin.sin_family = AF_INET; - //sin.sin_port = htons(info.dst_port); //dont need this - sin.sin_addr.s_addr = send_info.new_dst_ip.v4; - ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin)); - } - else - { - - struct sockaddr_ll addr={0}; //={0} not necessary - memcpy(&addr,&send_info.addr_ll,sizeof(addr)); - - ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &addr, sizeof (addr)); - } - if(ret==-1) - { - - mylog(log_trace,"sendto failed\n"); - //perror("why?"); - return -1; - } - else - { - //mylog(log_info,"sendto succ\n"); - } - return 0; + return send_raw_packet(raw_info,send_raw_ip_buf,ip_tot_len); } int peek_raw(packet_info_t &peek_info) { @@ -814,15 +782,33 @@ int discard_raw_packet() g_packet_buf_cnt--; return 0; } -int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen) +int recv_raw_packet(char * &packet,int &len) { assert(g_packet_buf_cnt==1); g_packet_buf_cnt--; - char *recv_raw_ip_buf=g_packet_buf; - //static char recv_raw_ip_buf[buf_len]; + if(g_packet_buf_lentot_len); - if(recv_len-int(link_level_header_len)