| 1 |
/* |
| 2 |
* UDP MIB architecture support for FreeBSD/DragonFlyBsd |
| 3 |
*/ |
| 4 |
#include <net-snmp/net-snmp-config.h> |
| 5 |
#include <net-snmp/net-snmp-includes.h> |
| 6 |
|
| 7 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 8 |
#include <net-snmp/data_access/ipaddress.h> |
| 9 |
#include <net-snmp/data_access/udp_endpoint.h> |
| 10 |
|
| 11 |
#include "udp-mib/udpEndpointTable/udpEndpointTable_constants.h" |
| 12 |
#include "udp-mib/data_access/udp_endpoint_private.h" |
| 13 |
|
| 14 |
#include "mibII/mibII_common.h" |
| 15 |
|
| 16 |
#ifdef HAVE_NETINET_UDP_H |
| 17 |
#include <netinet/udp.h> |
| 18 |
#endif |
| 19 |
#ifdef HAVE_NETINET_UDP_VAR_H |
| 20 |
#include <netinet/udp_var.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
static int _load(netsnmp_container *container, u_int flags); |
| 24 |
|
| 25 |
/* |
| 26 |
* initialize arch specific storage |
| 27 |
* |
| 28 |
* @retval 0: success |
| 29 |
* @retval <0: error |
| 30 |
*/ |
| 31 |
int |
| 32 |
netsnmp_arch_udp_endpoint_entry_init(netsnmp_udp_endpoint_entry *entry) |
| 33 |
{ |
| 34 |
/* |
| 35 |
* init |
| 36 |
*/ |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
/* |
| 41 |
* cleanup arch specific storage |
| 42 |
*/ |
| 43 |
void |
| 44 |
netsnmp_arch_udp_endpoint_entry_cleanup(netsnmp_udp_endpoint_entry *entry) |
| 45 |
{ |
| 46 |
/* |
| 47 |
* cleanup |
| 48 |
*/ |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* copy arch specific storage |
| 53 |
*/ |
| 54 |
int |
| 55 |
netsnmp_arch_udp_endpoint_entry_copy(netsnmp_udp_endpoint_entry *lhs, |
| 56 |
netsnmp_udp_endpoint_entry *rhs) |
| 57 |
{ |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* delete an entry |
| 63 |
*/ |
| 64 |
int |
| 65 |
netsnmp_arch_udp_endpoint_entry_delete(netsnmp_udp_endpoint_entry *entry) |
| 66 |
{ |
| 67 |
if (NULL == entry) |
| 68 |
return -1; |
| 69 |
/** xxx-rks:9 tcpConn delete not implemented */ |
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* |
| 76 |
* @retval 0 no errors |
| 77 |
* @retval !0 errors |
| 78 |
*/ |
| 79 |
int |
| 80 |
netsnmp_arch_udp_endpoint_container_load(netsnmp_container *container, |
| 81 |
u_int load_flags ) |
| 82 |
{ |
| 83 |
int rc = 0; |
| 84 |
|
| 85 |
DEBUGMSGTL(("access:udp_endpoint:container", |
| 86 |
"udp_endpoint_container_arch_load (flags %x)\n", load_flags)); |
| 87 |
|
| 88 |
if (NULL == container) { |
| 89 |
snmp_log(LOG_ERR, "no container specified/found for access_udp_endpoint\n"); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
rc = _load(container, load_flags); |
| 94 |
|
| 95 |
return rc; |
| 96 |
} |
| 97 |
|
| 98 |
#define NS_ELEM struct xinpcb |
| 99 |
|
| 100 |
/** |
| 101 |
* |
| 102 |
* @retval 0 no errors |
| 103 |
* @retval !0 errors |
| 104 |
*/ |
| 105 |
static int |
| 106 |
_load(netsnmp_container *container, u_int load_flags) |
| 107 |
{ |
| 108 |
size_t len; |
| 109 |
int sname[] = { CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_PCBLIST }; |
| 110 |
char *udpcb_buf = NULL; |
| 111 |
#if defined(dragonfly) |
| 112 |
struct xinpcb *xig = NULL; |
| 113 |
int i, count; |
| 114 |
#else |
| 115 |
struct xinpgen *xig = NULL; |
| 116 |
#endif |
| 117 |
netsnmp_udp_endpoint_entry *entry; |
| 118 |
int rc = 0; |
| 119 |
|
| 120 |
/* |
| 121 |
* Read in the buffer containing the TCP table data |
| 122 |
*/ |
| 123 |
len = 0; |
| 124 |
if (sysctl(sname, 4, 0, &len, 0, 0) < 0 || |
| 125 |
(udpcb_buf = malloc(len)) == NULL) |
| 126 |
return -1; |
| 127 |
if (sysctl(sname, 4, udpcb_buf, &len, 0, 0) < 0) { |
| 128 |
free(udpcb_buf); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
/* |
| 133 |
* Unpick this into the constituent 'xinpgen' structures, and extract |
| 134 |
* the 'inpcb' elements into a linked list (built in reverse) |
| 135 |
*/ |
| 136 |
#if defined(dragonfly) |
| 137 |
xig = (struct xinpcb *) udpcb_buf; |
| 138 |
count = len / sizeof(NS_ELEM); |
| 139 |
#else |
| 140 |
xig = (struct xinpgen *) udpcb_buf; |
| 141 |
xig = (struct xinpgen *) ((char *) xig + xig->xig_len); |
| 142 |
#endif |
| 143 |
|
| 144 |
#if defined(dragonfly) |
| 145 |
for (i = 0; i < count; i++) |
| 146 |
#else |
| 147 |
while (xig && (xig->xig_len > sizeof(struct xinpgen))) |
| 148 |
#endif |
| 149 |
{ |
| 150 |
NS_ELEM pcb = *((NS_ELEM *) xig); |
| 151 |
#if defined(dragonfly) |
| 152 |
xig = (struct xinpcb *) ((char *) xig + xig->xi_len); |
| 153 |
#else |
| 154 |
xig = (struct xinpgen *) ((char *) xig + xig->xig_len); |
| 155 |
#endif |
| 156 |
|
| 157 |
#if !defined(NETSNMP_ENABLE_IPV6) |
| 158 |
#ifdef INP_ISIPV6 |
| 159 |
if (INP_ISIPV6(&pcb.xi_inp)) |
| 160 |
#else |
| 161 |
if (pcb.xi_inp.inp_vflag & INP_IPV6) |
| 162 |
#endif |
| 163 |
continue; |
| 164 |
#endif |
| 165 |
|
| 166 |
entry = netsnmp_access_udp_endpoint_entry_create(); |
| 167 |
if(NULL == entry) { |
| 168 |
rc = -3; |
| 169 |
break; |
| 170 |
} |
| 171 |
|
| 172 |
/** oddly enough, these appear to already be in network order */ |
| 173 |
#if __FreeBSD_version >= 1200026 |
| 174 |
entry->loc_port = htons(pcb.inp_lport); |
| 175 |
entry->rmt_port = htons(pcb.inp_fport); |
| 176 |
|
| 177 |
/** the addr string may need work */ |
| 178 |
if (pcb.inp_vflag & INP_IPV6) { |
| 179 |
entry->loc_addr_len = entry->rmt_addr_len = 16; |
| 180 |
memcpy(entry->loc_addr, &pcb.in6p_laddr, 16); |
| 181 |
memcpy(entry->rmt_addr, &pcb.in6p_faddr, 16); |
| 182 |
} |
| 183 |
else { |
| 184 |
entry->loc_addr_len = entry->rmt_addr_len = 4; |
| 185 |
memcpy(entry->loc_addr, &pcb.inp_laddr, 4); |
| 186 |
memcpy(entry->rmt_addr, &pcb.inp_faddr, 4); |
| 187 |
} |
| 188 |
#else |
| 189 |
entry->loc_port = htons(pcb.xi_inp.inp_lport); |
| 190 |
entry->rmt_port = htons(pcb.xi_inp.inp_fport); |
| 191 |
|
| 192 |
/** the addr string may need work */ |
| 193 |
#ifdef INP_ISIPV6 |
| 194 |
if (INP_ISIPV6(&pcb.xi_inp)) { |
| 195 |
#else |
| 196 |
if (pcb.xi_inp.inp_vflag & INP_IPV6) { |
| 197 |
#endif |
| 198 |
entry->loc_addr_len = entry->rmt_addr_len = 16; |
| 199 |
memcpy(entry->loc_addr, &pcb.xi_inp.in6p_laddr, 16); |
| 200 |
memcpy(entry->rmt_addr, &pcb.xi_inp.in6p_faddr, 16); |
| 201 |
} |
| 202 |
else { |
| 203 |
entry->loc_addr_len = entry->rmt_addr_len = 4; |
| 204 |
memcpy(entry->loc_addr, &pcb.xi_inp.inp_laddr, 4); |
| 205 |
memcpy(entry->rmt_addr, &pcb.xi_inp.inp_faddr, 4); |
| 206 |
} |
| 207 |
#endif |
| 208 |
entry->pid = 0; |
| 209 |
|
| 210 |
/* |
| 211 |
* add entry to container |
| 212 |
*/ |
| 213 |
entry->index = CONTAINER_SIZE(container) + 1; |
| 214 |
CONTAINER_INSERT(container, entry); |
| 215 |
} |
| 216 |
|
| 217 |
free(udpcb_buf); |
| 218 |
|
| 219 |
if(rc<0) |
| 220 |
return rc; |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |