diff options
| author | lijia <[email protected]> | 2021-07-04 18:35:15 +0800 |
|---|---|---|
| committer | lijia <[email protected]> | 2021-07-04 18:35:15 +0800 |
| commit | 6fb379595a760bc32969e29487af8b017a24e6f1 (patch) | |
| tree | f826aae99c1d9c2de0f4ef2a70352336d908f820 /src/common | |
| parent | 5f6ad8c1e65d6b5bd8d3141b47ffd47c1976912e (diff) | |
replace usleep, rand, random, srand (PRNG), realloc.
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/net_common.c | 89 | ||||
| -rw-r--r-- | src/common/stream_addr_inet.c | 48 |
2 files changed, 115 insertions, 22 deletions
diff --git a/src/common/net_common.c b/src/common/net_common.c index 4898ece..bd123ef 100644 --- a/src/common/net_common.c +++ b/src/common/net_common.c @@ -70,6 +70,10 @@ char MESA_ascii_to_hex(char ascii) case 'F': c = 10 + ascii - 0x41; break; + + default: + c = 0; //todo , what error number return? + break; } return c; @@ -896,6 +900,9 @@ const void *MESA_net_jump_to_layer(const void *raw_data, int raw_layer_type, in break; case ADDR_TYPE_PPPOE_SES: + return NULL; + break; + default: sapp_runtime_log(20, "MESA_net_jump_to_layer(): unsupport raw_layer_type:%d in MESA_net_jump_to_layer()!\n", raw_layer_type); return NULL; @@ -1583,6 +1590,88 @@ const char *sapp_raw_ipv6_ntop(const struct mesa_ip6_hdr *ip6_hdr, char *out_buf return out_buf; } +const char *sapp_rawpkt_ntop(const raw_pkt_t *raw_pkt, int greedy_inner_layer, char *out_buf, int buf_len) +{ + const struct mesa_ethernet_hdr *ehdr; + const struct mesa_ip4_hdr *ip4hdr; + const struct mesa_ip6_hdr *ip6hdr; + const struct mesa_tcp_hdr *thdr; + const struct mesa_udp_hdr *uhdr; + char ipsrc_str[64], ipdst_str[64]; + unsigned short sport, dport, ipid, transport_checksum; + unsigned char ip_pro; + + if(greedy_inner_layer){ + ip4hdr = (struct mesa_ip4_hdr *)MESA_net_jump_to_layer_greedy(raw_pkt->raw_pkt_data, (int)raw_pkt->low_layer_type, (int)ADDR_TYPE_IPV4); + }else{ + ip4hdr = (struct mesa_ip4_hdr *)MESA_net_jump_to_layer(raw_pkt->raw_pkt_data, (int)raw_pkt->low_layer_type, (int)ADDR_TYPE_IPV4); + } + if(ip4hdr){ + inet_ntop(AF_INET, &ip4hdr->ip_src.s_addr, ipsrc_str, 64); + inet_ntop(AF_INET, &ip4hdr->ip_dst.s_addr, ipdst_str, 64); + ipid = ntohs(ip4hdr->ip_id); + ip_pro = ip4hdr->ip_p; + + if(IPPROTO_TCP == ip_pro){ + thdr = (const struct mesa_tcp_hdr *)((char *)ip4hdr + ip4hdr->ip_hl * 4); + sport = ntohs(thdr->th_sport); + dport = ntohs(thdr->th_dport); + transport_checksum = ntohs(thdr->th_sum); + + snprintf(out_buf, buf_len, "IPv4: %s.%u > %s.%u, IPID:%u, PROTO:TCP, TCP-Checksum:0x%x", + ipsrc_str, sport, ipdst_str, dport, ipid, transport_checksum); + }else if(IPPROTO_UDP == ip_pro){ + uhdr = (const struct mesa_udp_hdr *)((char *)ip4hdr + ip4hdr->ip_hl * 4); + sport = ntohs(uhdr->uh_sport); + dport = ntohs(uhdr->uh_dport); + transport_checksum = ntohs(uhdr->uh_sum); + snprintf(out_buf, buf_len, "IPv4: %s.%u > %s.%u, IPID:%u, PROTO:UDP, UDP-Checksum:0x%x", + ipsrc_str, sport, ipdst_str, dport, ipid, transport_checksum); + + }else{ + snprintf(out_buf, buf_len, "IPv4: %s > %s, IPID:%u, IPPROTO:%u",ipsrc_str, ipdst_str, ipid, ip_pro); + } + + return out_buf; + } + + if(greedy_inner_layer){ + ip6hdr = (struct mesa_ip6_hdr *)MESA_net_jump_to_layer_greedy(raw_pkt->raw_pkt_data, (int)raw_pkt->low_layer_type, (int)ADDR_TYPE_IPV6); + }else{ + ip6hdr = (struct mesa_ip6_hdr *)MESA_net_jump_to_layer(raw_pkt->raw_pkt_data, (int)raw_pkt->low_layer_type, (int)ADDR_TYPE_IPV6); + } + if(NULL == ip6hdr){ + return ""; /* �˴η���һ�����ַ���, ������NULL, ��ֹ�����߲��жϷ���ֵ������, ���ؿմ�ֻ��ʲô������ӡ */ + } + + { + inet_ntop(AF_INET6, &ip6hdr->ip6_src, ipsrc_str, 64); + inet_ntop(AF_INET6, &ip6hdr->ip6_dst, ipdst_str, 64); + ip_pro = ip6hdr->ip6_nxt_hdr; + + if(IPPROTO_TCP == ip_pro){ + thdr = (const struct mesa_tcp_hdr *)((char *)ip6hdr + sizeof(struct mesa_ip6_hdr)); + sport = ntohs(thdr->th_sport); + dport = ntohs(thdr->th_dport); + transport_checksum = ntohs(thdr->th_sum); + + snprintf(out_buf, buf_len, "IPv6: %s.%u > %s.%u, IPID:%u, PROTO:TCP, TCP-Checksum:0x%x", + ipsrc_str, sport, ipdst_str, dport, ipid, transport_checksum); + }else if(IPPROTO_UDP == ip_pro){ + uhdr = (const struct mesa_udp_hdr *)((char *)ip6hdr + sizeof(struct mesa_ip6_hdr)); + sport = ntohs(uhdr->uh_sport); + dport = ntohs(uhdr->uh_dport); + transport_checksum = ntohs(uhdr->uh_sum); + snprintf(out_buf, buf_len, "IPv6: %s.%u > %s.%u, IPID:%u, PROTO:UDP, UDP-Checksum:0x%x", + ipsrc_str, sport, ipdst_str, dport, ipid, transport_checksum); + + }else{ + snprintf(out_buf, buf_len, "IPv6: %s > %s, IPPROTO:%u",ipsrc_str, ipdst_str, ip_pro); + } + } + return out_buf; +} + /* return value: diff --git a/src/common/stream_addr_inet.c b/src/common/stream_addr_inet.c index e6978c9..d22df42 100644 --- a/src/common/stream_addr_inet.c +++ b/src/common/stream_addr_inet.c @@ -220,7 +220,7 @@ static int __addr_pure_tcp_pton(char *addr_str, addr_continuous_bin_t *addr_bin_ if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -230,7 +230,7 @@ static int __addr_pure_tcp_pton(char *addr_str, addr_continuous_bin_t *addr_bin_ if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -260,7 +260,7 @@ static int __addr_tcpv4_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -270,7 +270,7 @@ static int __addr_tcpv4_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -318,7 +318,7 @@ static int __addr_tcpv6_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -328,7 +328,7 @@ static int __addr_tcpv6_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -376,7 +376,7 @@ static int __addr_pure_udp_pton(char *addr_str, addr_continuous_bin_t *addr_bin_ if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -386,7 +386,7 @@ static int __addr_pure_udp_pton(char *addr_str, addr_continuous_bin_t *addr_bin_ if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -416,7 +416,7 @@ static int __addr_udpv4_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -426,7 +426,7 @@ static int __addr_udpv4_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -474,7 +474,7 @@ static int __addr_udpv6_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -484,7 +484,7 @@ static int __addr_udpv6_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -749,7 +749,7 @@ static int __addr_pptp_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val) if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -759,7 +759,7 @@ static int __addr_pptp_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val) if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -798,14 +798,14 @@ static int __addr_gtp_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val) if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); gtpaddr->teid_c2s = htonl(temp); item = strtok_r(NULL, "-", &save_ptr); if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); gtpaddr->teid_s2c = htonl(temp); addr_bin_val->stream.addr.addrtype = ADDR_TYPE_GPRS_TUNNEL; @@ -835,26 +835,26 @@ static int __addr_vxlan_pton(char *addr_str, addr_continuous_bin_t *addr_bin_val if(NULL == vxlan_struct_item){ return -1; } - vxlan_bin_addr->vlan_id = atoi(vxlan_struct_item); + vxlan_bin_addr->vlan_id = strtol(vxlan_struct_item, NULL, 10); vxlan_struct_item = strtok_r(NULL, delim, &save_ptr); if(NULL == vxlan_struct_item){ return -1; } - vxlan_bin_addr->dir = atoi(vxlan_struct_item); + vxlan_bin_addr->dir = strtol(vxlan_struct_item, NULL, 10); vxlan_struct_item = strtok_r(NULL, delim, &save_ptr); if(NULL == vxlan_struct_item){ return -1; } - vxlan_bin_addr->link_id = atoi(vxlan_struct_item); + vxlan_bin_addr->link_id = strtol(vxlan_struct_item, NULL, 10); vxlan_struct_item = strtok_r(NULL, delim, &save_ptr); if(NULL == vxlan_struct_item){ return -1; } - vxlan_bin_addr->link_type = atoi(vxlan_struct_item); + vxlan_bin_addr->link_type = strtol(vxlan_struct_item, NULL, 10); return 0; @@ -873,7 +873,7 @@ static int __addr_sockv4_pton(char *addr_str, addr_continuous_bin_t *addr_bin_va if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -883,7 +883,7 @@ static int __addr_sockv4_pton(char *addr_str, addr_continuous_bin_t *addr_bin_va if(NULL == item){ goto err; } - temp = atoi(item); + temp = strtol(item, NULL, 10); if(temp < 0 || temp > 65535){ goto err; } @@ -1966,6 +1966,10 @@ const char *__layer_addr_prefix_ntop_pad(const struct streaminfo_private *pstrea case ADDR_TYPE_VLAN: addr_prefix = "_PAD_VLAN"; break; + + default: + addr_prefix = NULL; + break; } return addr_prefix; |
