store net_connection addresses in binary rather than string

This commit is contained in:
Doug MacEachern 2006-07-04 16:35:27 +00:00
parent 196c91703c
commit e4303ee612
7 changed files with 84 additions and 44 deletions

View File

@ -783,7 +783,7 @@ my %classes = (
plat => 'LFSW' plat => 'LFSW'
}, },
{ {
name => 'local_address', type => 'String', name => 'local_address', type => 'NetAddress',
desc => '', desc => '',
plat => 'LFSW' plat => 'LFSW'
}, },
@ -793,7 +793,7 @@ my %classes = (
plat => 'LFSW' plat => 'LFSW'
}, },
{ {
name => 'remote_address', type => 'String', name => 'remote_address', type => 'NetAddress',
desc => '', desc => '',
plat => 'LFSW' plat => 'LFSW'
}, },
@ -1070,7 +1070,7 @@ my %pfields = (
NetAddr => "Sigar::NetAddr", NetAddr => "Sigar::NetAddr",
); );
$jfields{'NetAddr'} = $jfields{'String'}; $jfields{'NetAddress'} = $jfields{'NetAddr'} = $jfields{'String'};
my %jinit = ( my %jinit = (
String => 'null', String => 'null',
@ -1082,7 +1082,7 @@ my %jtype = (
#alias #alias
for my $j (\%jfields, \%jinit, \%jtype) { for my $j (\%jfields, \%jinit, \%jtype) {
$j->{'NetAddr'} = $j->{'String'}; $j->{'NetAddress'} = $j->{'NetAddr'} = $j->{'String'};
} }
my %func_alias = ( my %func_alias = (

View File

@ -309,6 +309,15 @@ static jstring jinet_ntoa(JNIEnv *env, sigar_t *sigar, sigar_uint64_t val) {
#define SetNetAddrField(env, obj, fieldID, val) \ #define SetNetAddrField(env, obj, fieldID, val) \
SetObjectField(env, obj, fieldID, jinet_ntoa(env, sigar, val)) SetObjectField(env, obj, fieldID, jinet_ntoa(env, sigar, val))
static jstring jnet_address_to_string(JNIEnv *env, sigar_t *sigar, sigar_net_address_t *val) {
char addr_str[INET6_ADDRSTRLEN];
sigar_net_address_to_string(sigar, val, addr_str);
return JENV->NewStringUTF(env, addr_str);
}
#define SetNetAddressField(env, obj, fieldID, val) \
SetObjectField(env, obj, fieldID, jnet_address_to_string(env, sigar, &val))
#include "javasigar_generated.c" #include "javasigar_generated.c"
#define SIGAR_ALLOC_OBJECT(name) \ #define SIGAR_ALLOC_OBJECT(name) \

View File

@ -391,6 +391,22 @@ sigar_file_system_usage_get(sigar_t *sigar,
const char *dirname, const char *dirname,
sigar_file_system_usage_t *fsusage); sigar_file_system_usage_t *fsusage);
typedef struct {
enum {
SIGAR_AF_UNSPEC,
SIGAR_AF_INET,
SIGAR_AF_INET6
} family;
union {
sigar_uint32_t in;
sigar_uint32_t in6[4];
} addr;
} sigar_net_address_t;
SIGAR_DECLARE(int) sigar_net_address_to_string(sigar_t *sigar,
sigar_net_address_t *address,
char *addr_str);
#ifndef INET6_ADDRSTRLEN #ifndef INET6_ADDRSTRLEN
# define INET6_ADDRSTRLEN 46 # define INET6_ADDRSTRLEN 46
#endif #endif
@ -546,9 +562,9 @@ enum {
typedef struct { typedef struct {
unsigned long local_port; unsigned long local_port;
char local_address[INET6_ADDRSTRLEN]; sigar_net_address_t local_address;
unsigned long remote_port; unsigned long remote_port;
char remote_address[INET6_ADDRSTRLEN]; sigar_net_address_t remote_address;
sigar_uid_t uid; sigar_uid_t uid;
unsigned long inode; unsigned long inode;
int type; int type;

View File

@ -192,6 +192,14 @@ int sigar_net_connection_list_grow(sigar_net_connection_list_t *connlist);
sigar_net_connection_list_grow(connlist); \ sigar_net_connection_list_grow(connlist); \
} }
#define sigar_net_address_set(a, val) \
(a).addr.in = val; \
(a).family = SIGAR_AF_INET
#define sigar_net_address6_set(addr, val) \
memcpy(&((a).addr.in6), val, sizeof((a).addr.in6)); \
(a).family = SIGAR_AF_INET6
int sigar_who_list_create(sigar_who_list_t *wholist); int sigar_who_list_create(sigar_who_list_t *wholist);
int sigar_who_list_grow(sigar_who_list_t *wholist); int sigar_who_list_grow(sigar_who_list_t *wholist);

View File

@ -1685,27 +1685,23 @@ int sigar_net_interface_stat_get(sigar_t *sigar, const char *name,
return found ? SIGAR_OK : ENXIO; 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) { if (len > HEX_ENT_LEN) {
struct in6_addr addr;
int i; int i;
for (i=0; i<=3; i++, ptr+=HEX_ENT_LEN) { for (i=0; i<=3; i++, ptr+=HEX_ENT_LEN) {
addr.s6_addr32[i] = hex2int(ptr, HEX_ENT_LEN); address->addr.in6[i] = hex2int(ptr, HEX_ENT_LEN);
}
if (!inet_ntop(AF_INET6, &addr, buffer, buflen)) {
return errno;
} }
address->family = SIGAR_AF_INET6;
} }
else { else {
struct in_addr addr; address->addr.in =
addr.s_addr = (len == HEX_ENT_LEN) ? hex2int(ptr, HEX_ENT_LEN) : 0; (len == HEX_ENT_LEN) ? hex2int(ptr, HEX_ENT_LEN) : 0;
if (!inet_ntop(AF_INET, &addr, buffer, buflen)) {
return errno;
}
}
return SIGAR_OK; address->family = SIGAR_AF_INET;
}
} }
typedef struct { typedef struct {
@ -1761,7 +1757,7 @@ static int proc_net_read(sigar_net_connection_walker_t *walker,
sigar_net_connection_t conn; sigar_net_connection_t conn;
char *laddr, *raddr; char *laddr, *raddr;
int laddr_len=0, raddr_len=0; int laddr_len=0, raddr_len=0;
int status, more; int more;
/* skip leading space */ /* skip leading space */
SKIP_WHILE(ptr, ' '); SKIP_WHILE(ptr, ' ');
@ -1799,21 +1795,11 @@ static int proc_net_read(sigar_net_connection_walker_t *walker,
conn.type = type; conn.type = type;
status = ip_format(conn.local_address, convert_hex_address(&conn.local_address,
sizeof(conn.local_address), laddr, laddr_len);
laddr, laddr_len);
if (status != SIGAR_OK) { convert_hex_address(&conn.remote_address,
return status; raddr, raddr_len);
}
status = ip_format(conn.remote_address,
sizeof(conn.remote_address),
raddr, raddr_len);
if (status != SIGAR_OK) {
return status;
}
/* SIGAR_TCP_* currently matches TCP_* in linux/tcp.h */ /* SIGAR_TCP_* currently matches TCP_* in linux/tcp.h */
conn.state = hex2int(ptr, 2); conn.state = hex2int(ptr, 2);

View File

@ -2032,7 +2032,6 @@ static int tcp_connection_get(sigar_net_connection_walker_t *walker,
struct mib2_tcpConnEntry *entry, struct mib2_tcpConnEntry *entry,
int len) int len)
{ {
sigar_t *sigar = walker->sigar;
int flags = walker->flags; int flags = walker->flags;
int status; int status;
char *end = (char *)entry + len; char *end = (char *)entry + len;
@ -2047,11 +2046,8 @@ static int tcp_connection_get(sigar_net_connection_walker_t *walker,
SIGAR_ZERO(&conn); SIGAR_ZERO(&conn);
sigar_inet_ntoa(sigar, entry->tcpConnLocalAddress, sigar_net_address_set(conn.local_address, entry->tcpConnLocalAddress);
conn.local_address); sigar_net_address_set(conn.remote_address, entry->tcpConnRemAddress);
sigar_inet_ntoa(sigar, entry->tcpConnRemAddress,
conn.remote_address);
conn.local_port = entry->tcpConnLocalPort; conn.local_port = entry->tcpConnLocalPort;
conn.remote_port = entry->tcpConnRemPort; conn.remote_port = entry->tcpConnRemPort;
@ -2124,7 +2120,6 @@ static int udp_connection_get(sigar_net_connection_walker_t *walker,
struct mib2_udpEntry *entry, struct mib2_udpEntry *entry,
int len) int len)
{ {
sigar_t *sigar = walker->sigar;
int flags = walker->flags; int flags = walker->flags;
int status; int status;
char *end = (char *)entry + len; char *end = (char *)entry + len;
@ -2140,9 +2135,9 @@ static int udp_connection_get(sigar_net_connection_walker_t *walker,
SIGAR_ZERO(&conn); SIGAR_ZERO(&conn);
sigar_inet_ntoa(sigar, entry->udpLocalAddress, sigar_net_address_set(conn.local_address, entry->udpLocalAddress);
conn.local_address); sigar_net_address_set(conn.remote_address, 0);
SIGAR_SSTRCPY(conn.remote_address, "0.0.0.0");
conn.local_port = entry->udpLocalPort; conn.local_port = entry->udpLocalPort;
conn.remote_port = 0; conn.remote_port = 0;
conn.type = SIGAR_NETCONN_UDP; conn.type = SIGAR_NETCONN_UDP;

View File

@ -1634,6 +1634,32 @@ SIGAR_DECLARE(int) sigar_inet_ntoa(sigar_t *sigar,
return SIGAR_OK; return SIGAR_OK;
} }
SIGAR_DECLARE(int) sigar_net_address_to_string(sigar_t *sigar,
sigar_net_address_t *address,
char *addr_str)
{
switch (address->family) {
case SIGAR_AF_INET6:
#if defined(__linux__) /*XXX*/
if (inet_ntop(AF_INET6, (const void *)&address->addr.in6,
addr_str, INET6_ADDRSTRLEN))
{
return SIGAR_OK;
}
else {
return errno;
}
#endif
case SIGAR_AF_INET:
return sigar_inet_ntoa(sigar, address->addr.in, addr_str);
case SIGAR_AF_UNSPEC:
default:
return EINVAL;
}
return SIGAR_OK;
}
static int fqdn_ip_get(sigar_t *sigar, char *name) static int fqdn_ip_get(sigar_t *sigar, char *name)
{ {
int i, status; int i, status;