PERFORCE change 166184 for review
Gabor Pali
pgj at FreeBSD.org
Thu Jul 16 23:27:26 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=166184
Change 166184 by pgj at petymeg-current on 2009/07/16 23:26:51
- Teach the bsnmpd(1) module snmp_mibII to use libnetstat for listing
TCP and UDP connections.
- Add libnetstat as dependency.
Affected files ...
.. //depot/projects/soc2009/pgj_libstat/src/contrib/bsnmp/snmp_mibII/mibII_tcp.c#2 edit
.. //depot/projects/soc2009/pgj_libstat/src/contrib/bsnmp/snmp_mibII/mibII_udp.c#2 edit
.. //depot/projects/soc2009/pgj_libstat/src/usr.sbin/bsnmpd/modules/snmp_mibII/Makefile#2 edit
Differences ...
==== //depot/projects/soc2009/pgj_libstat/src/contrib/bsnmp/snmp_mibII/mibII_tcp.c#2 (text+ko) ====
@@ -38,16 +38,15 @@
#include <netinet/tcp_var.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_fsm.h>
+#include <netstat.h>
struct tcp_index {
struct asn_oid index;
- struct xtcpcb *tp;
+ enum tcp_state tcp_state;
};
static uint64_t tcp_tick;
static struct tcpstat tcpstat;
-static struct xinpgen *xinpgen;
-static size_t xinpgen_len;
static u_int tcp_count;
static u_int tcp_total;
@@ -67,9 +66,15 @@
fetch_tcp(void)
{
size_t len;
- struct xinpgen *ptr;
- struct xtcpcb *tp;
struct tcp_index *oid;
+
+ struct socket_type_list *stlp;
+ struct socket_type_iterator *stip;
+ struct socket_type *stp;
+ struct addr_type *atp;
+ int error;
+
+ struct in_addr in;
in_addr_t inaddr;
len = sizeof(tcpstat);
@@ -82,39 +87,32 @@
return (-1);
}
- len = 0;
- if (sysctlbyname("net.inet.tcp.pcblist", NULL, &len, NULL, 0) == -1) {
- syslog(LOG_ERR, "net.inet.tcp.pcblist: %m");
+ stlp = netstat_stl_alloc();
+ if (stlp == NULL) {
+ syslog(LOG_ERR, "netstat_stl_alloc() in fetch_tcp()");
return (-1);
}
- if (len > xinpgen_len) {
- if ((ptr = realloc(xinpgen, len)) == NULL) {
- syslog(LOG_ERR, "%zu: %m", len);
- return (-1);
- }
- xinpgen = ptr;
- xinpgen_len = len;
+ if (netstat_socket(PF_INET, 0, IPPROTO_TCP, stlp, NETSTAT_SOCKET_ALL,
+ NULL) < 0) {
+ error = netstat_stl_geterror(stlp);
+ syslog(LOG_ERR, "netstat_socket: %s", netstat_strerror(error));
+ return (-1);
}
- if (sysctlbyname("net.inet.tcp.pcblist", xinpgen, &len, NULL, 0) == -1) {
- syslog(LOG_ERR, "net.inet.tcp.pcblist: %m");
+ if (netstat_sti_alloc(stlp, &stip) < 0) {
+ netstat_stl_free(stlp);
+ syslog(LOG_ERR, "netstat_sti_alloc() in fetch_tcp()");
return (-1);
}
tcp_tick = get_ticks();
-
tcp_count = 0;
tcp_total = 0;
- for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
- ptr->xig_len > sizeof(struct xinpgen);
- ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
- tp = (struct xtcpcb *)ptr;
- if (tp->xt_inp.inp_gencnt > xinpgen->xig_gen ||
- (tp->xt_inp.inp_vflag & INP_IPV4) == 0)
- continue;
+ for (stp = netstat_sti_first(stip); stp != NULL;
+ stp = netstat_sti_next(stip)) {
tcp_total++;
- if (tp->xt_tp.t_state == TCPS_ESTABLISHED ||
- tp->xt_tp.t_state == TCPS_CLOSE_WAIT)
+ if (netstat_st_get_tcps(stp) == tcps_Established ||
+ netstat_st_get_tcps(stp) == tcps_CloseWait)
tcp_count++;
}
@@ -130,32 +128,33 @@
}
oid = tcpoids;
- for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
- ptr->xig_len > sizeof(struct xinpgen);
- ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
- tp = (struct xtcpcb *)ptr;
- if (tp->xt_inp.inp_gencnt > xinpgen->xig_gen ||
- (tp->xt_inp.inp_vflag & INP_IPV4) == 0)
- continue;
- oid->tp = tp;
+
+ for (stp = netstat_sti_first(stip); stp != NULL;
+ stp = netstat_sti_next(stip)) {
+ oid->tcp_state = netstat_st_get_tcps(stp);
oid->index.len = 10;
- inaddr = ntohl(tp->xt_inp.inp_laddr.s_addr);
+ atp = netstat_st_get_address(stp, 0);
+ netstat_at_get_address(atp, (char *) &in, sizeof(in));
+ inaddr = ntohl(in.s_addr);
oid->index.subs[0] = (inaddr >> 24) & 0xff;
oid->index.subs[1] = (inaddr >> 16) & 0xff;
oid->index.subs[2] = (inaddr >> 8) & 0xff;
- oid->index.subs[3] = (inaddr >> 0) & 0xff;
- oid->index.subs[4] = ntohs(tp->xt_inp.inp_lport);
- inaddr = ntohl(tp->xt_inp.inp_faddr.s_addr);
+ oid->index.subs[3] = (inaddr ) & 0xff;
+ oid->index.subs[4] = netstat_at_get_port(atp);
+ atp = netstat_st_get_address(stp, 1);
+ netstat_at_get_address(atp, (char *) &in, sizeof(in));
+ inaddr = ntohl(in.s_addr);
oid->index.subs[5] = (inaddr >> 24) & 0xff;
oid->index.subs[6] = (inaddr >> 16) & 0xff;
oid->index.subs[7] = (inaddr >> 8) & 0xff;
- oid->index.subs[8] = (inaddr >> 0) & 0xff;
- oid->index.subs[9] = ntohs(tp->xt_inp.inp_fport);
+ oid->index.subs[8] = (inaddr ) & 0xff;
+ oid->index.subs[9] = netstat_at_get_port(atp);
oid++;
}
qsort(tcpoids, tcp_total, sizeof(tcpoids[0]), tcp_compare);
-
+ netstat_sti_free(stip);
+ netstat_stl_free(stlp);
return (0);
}
@@ -290,39 +289,39 @@
switch (value->var.subs[sub - 1]) {
case LEAF_tcpConnState:
- switch (tcpoids[i].tp->xt_tp.t_state) {
+ switch (tcpoids[i].tcp_state) {
- case TCPS_CLOSED:
+ case tcps_Closed:
value->v.integer = 1;
break;
- case TCPS_LISTEN:
+ case tcps_Listen:
value->v.integer = 2;
break;
- case TCPS_SYN_SENT:
+ case tcps_SynSent:
value->v.integer = 3;
break;
- case TCPS_SYN_RECEIVED:
+ case tcps_SynReceived:
value->v.integer = 4;
break;
- case TCPS_ESTABLISHED:
+ case tcps_Established:
value->v.integer = 5;
break;
- case TCPS_CLOSE_WAIT:
+ case tcps_CloseWait:
value->v.integer = 8;
break;
- case TCPS_FIN_WAIT_1:
+ case tcps_FinWait1:
value->v.integer = 6;
break;
- case TCPS_CLOSING:
+ case tcps_Closing:
value->v.integer = 10;
break;
- case TCPS_LAST_ACK:
+ case tcps_LastAck:
value->v.integer = 9;
break;
- case TCPS_FIN_WAIT_2:
+ case tcps_FinWait2:
value->v.integer = 7;
break;
- case TCPS_TIME_WAIT:
+ case tcps_TimeWait:
value->v.integer = 11;
break;
default:
==== //depot/projects/soc2009/pgj_libstat/src/contrib/bsnmp/snmp_mibII/mibII_udp.c#2 (text+ko) ====
@@ -37,37 +37,36 @@
#include <netinet/udp.h>
#include <netinet/ip_var.h>
#include <netinet/udp_var.h>
+#include <netstat.h>
-struct udp_index {
- struct asn_oid index;
- struct xinpcb *inp;
-};
-
static uint64_t udp_tick;
static struct udpstat udpstat;
-static struct xinpgen *xinpgen;
-static size_t xinpgen_len;
static u_int udp_total;
static u_int oidnum;
-static struct udp_index *udpoids;
+static struct asn_oid *udpoids;
static int
udp_compare(const void *p1, const void *p2)
{
- const struct udp_index *t1 = p1;
- const struct udp_index *t2 = p2;
+ const struct asn_oid *t1 = p1;
+ const struct asn_oid *t2 = p2;
- return (asn_compare_oid(&t1->index, &t2->index));
+ return (asn_compare_oid(t1, t2));
}
static int
fetch_udp(void)
{
size_t len;
- struct xinpgen *ptr;
- struct xinpcb *inp;
- struct udp_index *oid;
+ struct socket_type_list *stlp;
+ struct socket_type_iterator *stip;
+ struct socket_type *stp;
+ struct addr_type *atp;
+ int error;
+
+ struct asn_oid *oid;
+ struct in_addr in;
in_addr_t inaddr;
len = sizeof(udpstat);
@@ -80,37 +79,25 @@
return (-1);
}
- udp_tick = get_ticks();
-
- len = 0;
- if (sysctlbyname("net.inet.udp.pcblist", NULL, &len, NULL, 0) == -1) {
- syslog(LOG_ERR, "net.inet.udp.pcblist: %m");
+ stlp = netstat_stl_alloc();
+ if (stlp == NULL) {
+ syslog(LOG_ERR, "netstat_stl_alloc() in fetch_udp()");
return (-1);
}
- if (len > xinpgen_len) {
- if ((ptr = realloc(xinpgen, len)) == NULL) {
- syslog(LOG_ERR, "%zu: %m", len);
- return (-1);
- }
- xinpgen = ptr;
- xinpgen_len = len;
+ if (netstat_socket(PF_INET, 0, IPPROTO_UDP, stlp, NETSTAT_SOCKET_ALL,
+ NULL) < 0) {
+ error = netstat_stl_geterror(stlp);
+ syslog(LOG_ERR, "netstat_socket: %s", netstat_strerror(error));
+ return (-1);
}
- if (sysctlbyname("net.inet.udp.pcblist", xinpgen, &len, NULL, 0) == -1) {
- syslog(LOG_ERR, "net.inet.udp.pcblist: %m");
+ if (netstat_sti_alloc(stlp, &stip) < 0) {
+ netstat_stl_free(stlp);
+ syslog(LOG_ERR, "netstat_sti_alloc() in fetch_udp()");
return (-1);
}
- udp_total = 0;
- for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
- ptr->xig_len > sizeof(struct xinpgen);
- ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
- inp = (struct xinpcb *)ptr;
- if (inp->xi_inp.inp_gencnt > xinpgen->xig_gen ||
- (inp->xi_inp.inp_vflag & INP_IPV4) == 0)
- continue;
-
- udp_total++;
- }
+ udp_tick = get_ticks();
+ udp_total = netstat_stl_length(stlp);
if (oidnum < udp_total) {
oid = realloc(udpoids, udp_total * sizeof(udpoids[0]));
@@ -124,26 +111,24 @@
}
oid = udpoids;
- for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len);
- ptr->xig_len > sizeof(struct xinpgen);
- ptr = (struct xinpgen *)(void *)((char *)ptr + ptr->xig_len)) {
- inp = (struct xinpcb *)ptr;
- if (inp->xi_inp.inp_gencnt > xinpgen->xig_gen ||
- (inp->xi_inp.inp_vflag & INP_IPV4) == 0)
- continue;
- oid->inp = inp;
- oid->index.len = 5;
- inaddr = ntohl(inp->xi_inp.inp_laddr.s_addr);
- oid->index.subs[0] = (inaddr >> 24) & 0xff;
- oid->index.subs[1] = (inaddr >> 16) & 0xff;
- oid->index.subs[2] = (inaddr >> 8) & 0xff;
- oid->index.subs[3] = (inaddr >> 0) & 0xff;
- oid->index.subs[4] = ntohs(inp->xi_inp.inp_lport);
+
+ for (stp = netstat_sti_first(stip); stp != NULL;
+ stp = netstat_sti_next(stip)) {
+ oid->len = 5;
+ atp = netstat_st_get_address(stp, 0);
+ netstat_at_get_address(atp, (char *) &in, sizeof(in));
+ inaddr = ntohl(in.s_addr);
+ oid->subs[0] = (inaddr << 24) & 0xff;
+ oid->subs[1] = (inaddr << 16) & 0xff;
+ oid->subs[2] = (inaddr << 8) & 0xff;
+ oid->subs[3] = (inaddr ) & 0xff;
+ oid->subs[4] = netstat_at_get_port(atp);
oid++;
}
qsort(udpoids, udp_total, sizeof(udpoids[0]), udp_compare);
-
+ netstat_sti_free(stip);
+ netstat_stl_free(stlp);
return (0);
}
@@ -211,16 +196,16 @@
case SNMP_OP_GETNEXT:
for (i = 0; i < udp_total; i++)
- if (index_compare(&value->var, sub, &udpoids[i].index) < 0)
+ if (index_compare(&value->var, sub, &udpoids[i]) < 0)
break;
if (i == udp_total)
return (SNMP_ERR_NOSUCHNAME);
- index_append(&value->var, sub, &udpoids[i].index);
+ index_append(&value->var, sub, &udpoids[i]);
break;
case SNMP_OP_GET:
for (i = 0; i < udp_total; i++)
- if (index_compare(&value->var, sub, &udpoids[i].index) == 0)
+ if (index_compare(&value->var, sub, &udpoids[i]) == 0)
break;
if (i == udp_total)
return (SNMP_ERR_NOSUCHNAME);
@@ -238,14 +223,14 @@
switch (value->var.subs[sub - 1]) {
case LEAF_udpLocalAddress:
- value->v.ipaddress[0] = udpoids[i].index.subs[0];
- value->v.ipaddress[1] = udpoids[i].index.subs[1];
- value->v.ipaddress[2] = udpoids[i].index.subs[2];
- value->v.ipaddress[3] = udpoids[i].index.subs[3];
+ value->v.ipaddress[0] = udpoids[i].subs[0];
+ value->v.ipaddress[1] = udpoids[i].subs[1];
+ value->v.ipaddress[2] = udpoids[i].subs[2];
+ value->v.ipaddress[3] = udpoids[i].subs[3];
break;
case LEAF_udpLocalPort:
- value->v.integer = udpoids[i].index.subs[4];
+ value->v.integer = udpoids[i].subs[4];
break;
}
==== //depot/projects/soc2009/pgj_libstat/src/usr.sbin/bsnmpd/modules/snmp_mibII/Makefile#2 (text+ko) ====
@@ -13,9 +13,13 @@
ipForward ifIndex linkDown linkUp
MAN= snmp_mibII.3
-CFLAGS+= -I${CONTRIB}/lib -I${CONTRIB}/snmpd
+CFLAGS+= -I${CONTRIB}/lib -I${CONTRIB}/snmpd -I../../../../lib/libnetstat
CFLAGS+= -DHAVE_ERR_H -DHAVE_GETADDRINFO -DHAVE_STRLCPY -DHAVE_SYS_TREE_H
+DPADD= ${LIBNETSTAT}
+LDADD= -lnetstat
+LDFLAGS+= -L../../../../lib/libnetstat
+
DEFS= ${MOD}_tree.def
INCS= snmp_${MOD}.h
BMIBS= BEGEMOT-IP-MIB.txt BEGEMOT-MIB2-MIB.txt
More information about the p4-projects
mailing list