Skip to content

Commit 68ee5b7

Browse files
committed
nettrace: use inet_pton() to parse ip address
Signed-off-by: Menglong Dong <imagedong@tencent.com>
1 parent 41c2c46 commit 68ee5b7

3 files changed

Lines changed: 9 additions & 65 deletions

File tree

component/net_utils.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -148,39 +148,3 @@ int proto2i(char *proto, int *dest)
148148
return 4;
149149
return 0;
150150
}
151-
152-
void i2ipv6(char *dest, __u8 ip[])
153-
{
154-
int i = 0, offset = 0;
155-
156-
for (; i < 16; i += 2) {
157-
if (ip[i] || ip[i + 1])
158-
offset += sprintf(dest + offset, "%02x%02x:",
159-
ip[i], ip[i + 1]);
160-
else
161-
offset += sprintf(dest + offset, ":");
162-
}
163-
*(dest + offset - 1) = '\0';
164-
}
165-
166-
int ipv6toi(char *ip, __u8 *dest)
167-
{
168-
u16 *c = (u16 *)dest;
169-
u32 t[8] = {}, i = 0;
170-
171-
if (sscanf(ip, "%x:%x:%x:%x:%x:%x:%x:%x", t, t + 1, t + 2,
172-
t + 3, t + 4, t + 5, t + 6, t + 7) == 8)
173-
goto is_valid;
174-
175-
memset(t, 0, sizeof(t));
176-
if (sscanf(ip, "%x::%x:%x:%x:%x", t, t + 4, t + 5, t + 6,
177-
t + 7) == 5)
178-
goto is_valid;
179-
180-
return -1;
181-
182-
is_valid:
183-
for (i = 0; i < 8; i++)
184-
c[i] = htons(t[i]);
185-
return 0;
186-
}

component/net_utils.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,11 @@ typedef __u64 u64;
2727

2828
extern char *l4_proto_names[];
2929

30-
static inline void i2ip(char *dest, __u32 ip)
31-
{
32-
u8 *t = (u8 *)&ip;
33-
sprintf(dest, "%d.%d.%d.%d", t[0], t[1], t[2], t[3]);
34-
}
35-
36-
static inline int ip2i(char *ip, __u32 *dest)
37-
{
38-
u8 *c = (u8 *)dest;
39-
u32 t[4] = {};
40-
41-
if (sscanf(ip, "%u.%u.%u.%u", t, t + 1, t + 2, t + 3) != 4)
42-
return -EINVAL;
43-
44-
#define C(index) c[index] = t[index]
45-
C(0);
46-
C(1);
47-
C(2);
48-
C(3);
49-
#undef C
50-
return 0;
51-
}
52-
5330
static inline char *i2l4(u8 num)
5431
{
5532
return l4_proto_names[num];
5633
}
5734

5835
int proto2i(char *proto, int *dest);
59-
void i2ipv6(char *dest, u8 ip[]);
60-
int ipv6toi(char *ip, u8 *dest);
6136

6237
#endif

shared/pkt_utils.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/icmpv6.h>
77
#define _LINUX_IN_H
88
#include <netinet/in.h>
9+
#include <arpa/inet.h>
910

1011
#include "pkt_utils.h"
1112

@@ -56,12 +57,16 @@ int ts_print_packet(char *buf, packet_t *pkt, char *minfo,
5657

5758
switch (pkt->proto_l3) {
5859
case ETH_P_IP:
59-
i2ip(saddr, pkt->l3.ipv4.saddr);
60-
i2ip(daddr, pkt->l3.ipv4.daddr);
60+
inet_ntop(AF_INET, (void *)&pkt->l3.ipv4.saddr, saddr,
61+
sizeof(saddr));
62+
inet_ntop(AF_INET, (void *)&pkt->l3.ipv4.daddr, daddr,
63+
sizeof(daddr));
6164
goto print_ip;
6265
case ETH_P_IPV6:
63-
i2ipv6(saddr, pkt->l3.ipv6.saddr);
64-
i2ipv6(daddr, pkt->l3.ipv6.daddr);
66+
inet_ntop(AF_INET6, (void *)pkt->l3.ipv6.saddr, saddr,
67+
sizeof(saddr));
68+
inet_ntop(AF_INET6, (void *)pkt->l3.ipv6.daddr, daddr,
69+
sizeof(daddr));
6570
goto print_ip;
6671
case ETH_P_ARP:
6772
goto print_arp;

0 commit comments

Comments
 (0)