| 1 |
/* |
| 2 |
* udp_endpointTable MIB architecture support for NetBSD |
| 3 |
* |
| 4 |
* $Id: udp_endpoint_linux.c 18994 2010-06-16 13:13:25Z dts12 $ |
| 5 |
*/ |
| 6 |
#include <net-snmp/net-snmp-config.h> |
| 7 |
#include <net-snmp/net-snmp-includes.h> |
| 8 |
|
| 9 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 10 |
#include <net-snmp/data_access/ipaddress.h> |
| 11 |
#include <net-snmp/data_access/udp_endpoint.h> |
| 12 |
|
| 13 |
#include "udp-mib/udpEndpointTable/udpEndpointTable_constants.h" |
| 14 |
#include "udp-mib/data_access/udp_endpoint_private.h" |
| 15 |
|
| 16 |
#include "mibII/mibII_common.h" |
| 17 |
|
| 18 |
#ifdef HAVE_NETINET_UDP_H |
| 19 |
#include <netinet/udp.h> |
| 20 |
#endif |
| 21 |
#ifdef HAVE_NETINET_UDP_VAR_H |
| 22 |
#include <netinet/udp_var.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
static int _load(netsnmp_container *container, u_int flags, int var); |
| 26 |
|
| 27 |
/* |
| 28 |
* initialize arch specific storage |
| 29 |
* |
| 30 |
* @retval 0: success |
| 31 |
* @retval <0: error |
| 32 |
*/ |
| 33 |
int |
| 34 |
netsnmp_arch_udp_endpoint_entry_init(netsnmp_udp_endpoint_entry *entry) |
| 35 |
{ |
| 36 |
/* |
| 37 |
* init |
| 38 |
*/ |
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
/* |
| 43 |
* cleanup arch specific storage |
| 44 |
*/ |
| 45 |
void |
| 46 |
netsnmp_arch_udp_endpoint_entry_cleanup(netsnmp_udp_endpoint_entry *entry) |
| 47 |
{ |
| 48 |
/* |
| 49 |
* cleanup |
| 50 |
*/ |
| 51 |
} |
| 52 |
|
| 53 |
/* |
| 54 |
* copy arch specific storage |
| 55 |
*/ |
| 56 |
int |
| 57 |
netsnmp_arch_udp_endpoint_entry_copy(netsnmp_udp_endpoint_entry *lhs, |
| 58 |
netsnmp_udp_endpoint_entry *rhs) |
| 59 |
{ |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* delete an entry |
| 65 |
*/ |
| 66 |
int |
| 67 |
netsnmp_arch_udp_endpoint_entry_delete(netsnmp_udp_endpoint_entry *entry) |
| 68 |
{ |
| 69 |
if (NULL == entry) |
| 70 |
return -1; |
| 71 |
/** xxx-rks:9 udp_endpoint delete not implemented */ |
| 72 |
return -1; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* |
| 78 |
* @retval 0 no errors |
| 79 |
* @retval !0 errors |
| 80 |
*/ |
| 81 |
int |
| 82 |
netsnmp_arch_udp_endpoint_container_load(netsnmp_container *container, |
| 83 |
u_int load_flags ) |
| 84 |
{ |
| 85 |
int rc = 0; |
| 86 |
|
| 87 |
DEBUGMSGTL(("access:udp_endpoint:container", |
| 88 |
"udp_endpoint_container_arch_load (flags %x)\n", load_flags)); |
| 89 |
|
| 90 |
if (NULL == container) { |
| 91 |
snmp_log(LOG_ERR, "no container specified/found for access_udp_endpoint\n"); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
rc = _load(container, load_flags, 4); |
| 96 |
#if defined(NETSNMP_ENABLE_IPV6) |
| 97 |
rc = _load(container, load_flags, 6); |
| 98 |
#endif |
| 99 |
|
| 100 |
return rc; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* |
| 106 |
* @retval 0 no errors |
| 107 |
* @retval !0 errors |
| 108 |
*/ |
| 109 |
static int |
| 110 |
_load(netsnmp_container *container, u_int load_flags, int ver) |
| 111 |
{ |
| 112 |
const char *mibname; |
| 113 |
int mib[8]; |
| 114 |
size_t mib_len; |
| 115 |
struct kinfo_pcb *pcblist; |
| 116 |
size_t pcb_len; |
| 117 |
netsnmp_udp_endpoint_entry *entry; |
| 118 |
int i, rc = 0; |
| 119 |
|
| 120 |
/* |
| 121 |
* Read in the buffer containing the TCP table data |
| 122 |
*/ |
| 123 |
switch (ver) { |
| 124 |
case 4: |
| 125 |
mibname = "net.inet.udp.pcblist"; |
| 126 |
break; |
| 127 |
case 6: |
| 128 |
mibname = "net.inet6.udp6.pcblist"; |
| 129 |
break; |
| 130 |
default: |
| 131 |
snmp_log(LOG_ERR, "udp-mib:data_access:_load: bad version %d\n", ver); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
if (sysctlnametomib(mibname, mib, &mib_len) == -1) { |
| 136 |
snmp_log(LOG_ERR, "udp-mib:data_access:_load: cant resolve mib %s\n", mibname); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
if (sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &pcb_len, NULL, 0) == -1) { |
| 141 |
snmp_log(LOG_ERR, "udp-mib:data_access:_load: cant size mib %s\n", mibname); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
|
| 145 |
if ((pcblist = malloc(pcb_len)) == NULL) { |
| 146 |
snmp_log(LOG_ERR, "udp-mib:data_access:_load: cant allocate mib %s\n", mibname); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
memset(pcblist, 0, pcb_len); |
| 150 |
|
| 151 |
mib[6] = sizeof(*pcblist); |
| 152 |
mib[7] = pcb_len / sizeof(*pcblist); |
| 153 |
|
| 154 |
if (sysctl(mib, sizeof(mib) / sizeof(*mib), |
| 155 |
pcblist, &pcb_len, NULL, 0) == -1) { |
| 156 |
snmp_log(LOG_ERR, "udp-mib:data_access:_load: cant size mib %s\n", mibname); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* Unpick this into the constituent structures, and extract |
| 162 |
* the 'inpcb' elements into a linked list (built in reverse) |
| 163 |
*/ |
| 164 |
for (i = 0; i < pcb_len / sizeof(*pcblist); i++) { |
| 165 |
struct kinfo_pcb *pcb = pcblist+i; |
| 166 |
|
| 167 |
entry = netsnmp_access_udp_endpoint_entry_create(); |
| 168 |
if(NULL == entry) { |
| 169 |
rc = -3; |
| 170 |
break; |
| 171 |
} |
| 172 |
|
| 173 |
entry->pid = 0; |
| 174 |
|
| 175 |
if (ver == 6) { |
| 176 |
struct sockaddr_in6 src, dst; |
| 177 |
memcpy(&src, &pcb->ki_s, sizeof(src)); |
| 178 |
memcpy(&dst, &pcb->ki_d, sizeof(dst)); |
| 179 |
entry->loc_addr_len = entry->rmt_addr_len = 16; |
| 180 |
memcpy(entry->loc_addr, &src.sin6_addr, 16); |
| 181 |
memcpy(entry->rmt_addr, &dst.sin6_addr, 16); |
| 182 |
entry->loc_port = ntohs(src.sin6_port); |
| 183 |
entry->rmt_port = ntohs(dst.sin6_port); |
| 184 |
} |
| 185 |
else { |
| 186 |
struct sockaddr_in src, dst; |
| 187 |
memcpy(&src, &pcb->ki_s, sizeof(src)); |
| 188 |
memcpy(&dst, &pcb->ki_d, sizeof(dst)); |
| 189 |
entry->loc_addr_len = entry->rmt_addr_len = 4; |
| 190 |
memcpy(entry->loc_addr, &src.sin_addr, 4); |
| 191 |
memcpy(entry->rmt_addr, &dst.sin_addr, 4); |
| 192 |
entry->loc_port = ntohs(src.sin_port); |
| 193 |
entry->rmt_port = ntohs(dst.sin_port); |
| 194 |
} |
| 195 |
|
| 196 |
/* |
| 197 |
* add entry to container |
| 198 |
*/ |
| 199 |
entry->index = CONTAINER_SIZE(container) + 1; |
| 200 |
CONTAINER_INSERT(container, entry); |
| 201 |
} |
| 202 |
|
| 203 |
if(rc<0) |
| 204 |
return rc; |
| 205 |
|
| 206 |
return 0; |
| 207 |
} |