store net_connection addresses in binary rather than string
This commit is contained in:
1 parent
196c91703c
commit
e4303ee612
7 files changed
+84
-44
No files matched your search
+14
-28
@@ -1685,27 +1685,23 @@ int sigar_net_interface_stat_get(sigar_t *sigar, const char *name,
|
||||
return found ? SIGAR_OK : ENXIO;
|
||||
}
|
||||
|
||||
static SIGAR_INLINE int ip_format(char *buffer, int buflen, char *ptr, int len)
|
||||
static SIGAR_INLINE void convert_hex_address(sigar_net_address_t *address,
|
||||
char *ptr, int len)
|
||||
{
|
||||
if (len > HEX_ENT_LEN) {
|
||||
struct in6_addr addr;
|
||||
int i;
|
||||
for (i=0; i<=3; i++, ptr+=HEX_ENT_LEN) {
|
||||
addr.s6_addr32[i] = hex2int(ptr, HEX_ENT_LEN);
|
||||
}
|
||||
if (!inet_ntop(AF_INET6, &addr, buffer, buflen)) {
|
||||
return errno;
|
||||
address->addr.in6[i] = hex2int(ptr, HEX_ENT_LEN);
|
||||
}
|
||||
|
||||
address->family = SIGAR_AF_INET6;
|
||||
}
|
||||
else {
|
||||
struct in_addr addr;
|
||||
addr.s_addr = (len == HEX_ENT_LEN) ? hex2int(ptr, HEX_ENT_LEN) : 0;
|
||||
if (!inet_ntop(AF_INET, &addr, buffer, buflen)) {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
address->addr.in =
|
||||
(len == HEX_ENT_LEN) ? hex2int(ptr, HEX_ENT_LEN) : 0;
|
||||
|
||||
return SIGAR_OK;
|
||||
address->family = SIGAR_AF_INET;
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
@@ -1761,7 +1757,7 @@ static int proc_net_read(sigar_net_connection_walker_t *walker,
|
||||
sigar_net_connection_t conn;
|
||||
char *laddr, *raddr;
|
||||
int laddr_len=0, raddr_len=0;
|
||||
int status, more;
|
||||
int more;
|
||||
|
||||
/* skip leading space */
|
||||
SKIP_WHILE(ptr, ' ');
|
||||
@@ -1799,21 +1795,11 @@ static int proc_net_read(sigar_net_connection_walker_t *walker,
|
||||
|
||||
conn.type = type;
|
||||
|
||||
status = ip_format(conn.local_address,
|
||||
sizeof(conn.local_address),
|
||||
laddr, laddr_len);
|
||||
convert_hex_address(&conn.local_address,
|
||||
laddr, laddr_len);
|
||||
|
||||
if (status != SIGAR_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = ip_format(conn.remote_address,
|
||||
sizeof(conn.remote_address),
|
||||
raddr, raddr_len);
|
||||
|
||||
if (status != SIGAR_OK) {
|
||||
return status;
|
||||
}
|
||||
convert_hex_address(&conn.remote_address,
|
||||
raddr, raddr_len);
|
||||
|
||||
/* SIGAR_TCP_* currently matches TCP_* in linux/tcp.h */
|
||||
conn.state = hex2int(ptr, 2);
|
||||
|
||||
@@ -2032,7 +2032,6 @@ static int tcp_connection_get(sigar_net_connection_walker_t *walker,
|
||||
struct mib2_tcpConnEntry *entry,
|
||||
int len)
|
||||
{
|
||||
sigar_t *sigar = walker->sigar;
|
||||
int flags = walker->flags;
|
||||
int status;
|
||||
char *end = (char *)entry + len;
|
||||
@@ -2047,11 +2046,8 @@ static int tcp_connection_get(sigar_net_connection_walker_t *walker,
|
||||
|
||||
SIGAR_ZERO(&conn);
|
||||
|
||||
sigar_inet_ntoa(sigar, entry->tcpConnLocalAddress,
|
||||
conn.local_address);
|
||||
|
||||
sigar_inet_ntoa(sigar, entry->tcpConnRemAddress,
|
||||
conn.remote_address);
|
||||
sigar_net_address_set(conn.local_address, entry->tcpConnLocalAddress);
|
||||
sigar_net_address_set(conn.remote_address, entry->tcpConnRemAddress);
|
||||
|
||||
conn.local_port = entry->tcpConnLocalPort;
|
||||
conn.remote_port = entry->tcpConnRemPort;
|
||||
@@ -2124,7 +2120,6 @@ static int udp_connection_get(sigar_net_connection_walker_t *walker,
|
||||
struct mib2_udpEntry *entry,
|
||||
int len)
|
||||
{
|
||||
sigar_t *sigar = walker->sigar;
|
||||
int flags = walker->flags;
|
||||
int status;
|
||||
char *end = (char *)entry + len;
|
||||
@@ -2140,9 +2135,9 @@ static int udp_connection_get(sigar_net_connection_walker_t *walker,
|
||||
|
||||
SIGAR_ZERO(&conn);
|
||||
|
||||
sigar_inet_ntoa(sigar, entry->udpLocalAddress,
|
||||
conn.local_address);
|
||||
SIGAR_SSTRCPY(conn.remote_address, "0.0.0.0");
|
||||
sigar_net_address_set(conn.local_address, entry->udpLocalAddress);
|
||||
sigar_net_address_set(conn.remote_address, 0);
|
||||
|
||||
conn.local_port = entry->udpLocalPort;
|
||||
conn.remote_port = 0;
|
||||
conn.type = SIGAR_NETCONN_UDP;
|
||||
|
||||
Reference in new issue
Block a user