| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
|
| 3 |
#include <net-snmp/library/snmpIPXDomain.h> |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <sys/types.h> |
| 7 |
#include <ctype.h> |
| 8 |
#include <errno.h> |
| 9 |
|
| 10 |
#ifdef HAVE_STRING_H |
| 11 |
#include <string.h> |
| 12 |
#else |
| 13 |
#include <strings.h> |
| 14 |
#endif |
| 15 |
#ifdef HAVE_STDLIB_H |
| 16 |
#include <stdlib.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_UNISTD_H |
| 19 |
#include <unistd.h> |
| 20 |
#endif |
| 21 |
#ifdef HAVE_SYS_SOCKET_H |
| 22 |
#include <sys/socket.h> |
| 23 |
#endif |
| 24 |
#ifdef HAVE_NETINET_IN_H |
| 25 |
#include <netinet/in.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <net-snmp/types.h> |
| 29 |
#include <net-snmp/output_api.h> |
| 30 |
#include <net-snmp/config_api.h> |
| 31 |
|
| 32 |
#include <net-snmp/library/snmp_assert.h> |
| 33 |
#include <net-snmp/library/snmp_transport.h> |
| 34 |
#include <net-snmp/library/tools.h> |
| 35 |
|
| 36 |
#define SNMP_IPX_DEFAULT_PORT 36879 /* Specified in RFC 1420. */ |
| 37 |
static netsnmp_tdomain ipxDomain; |
| 38 |
|
| 39 |
/* |
| 40 |
* Return a string representing the address in data, or else the "far end" |
| 41 |
* address if data is NULL. |
| 42 |
*/ |
| 43 |
|
| 44 |
static char * |
| 45 |
netsnmp_ipx_fmtaddr(netsnmp_transport *t, const void *data, int len) |
| 46 |
{ |
| 47 |
const struct sockaddr_ipx *to = NULL; |
| 48 |
|
| 49 |
if (data != NULL && len == sizeof(struct sockaddr_ipx)) { |
| 50 |
to = (const struct sockaddr_ipx *) data; |
| 51 |
} else if (t != NULL && t->data != NULL) { |
| 52 |
to = (const struct sockaddr_ipx *) t->data; |
| 53 |
} |
| 54 |
if (to == NULL) { |
| 55 |
return strdup("IPX: unknown"); |
| 56 |
} else { |
| 57 |
char *tmp; |
| 58 |
|
| 59 |
if (asprintf(&tmp, "IPX: %08X:%02X%02X%02X%02X%02X%02X/%hu", |
| 60 |
ntohl(to->sipx_network), to->sipx_node[0], |
| 61 |
to->sipx_node[1], to->sipx_node[2], to->sipx_node[3], |
| 62 |
to->sipx_node[4], to->sipx_node[5], ntohs(to->sipx_port)) |
| 63 |
< 0) |
| 64 |
tmp = NULL; |
| 65 |
return tmp; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static void netsnmp_ipx_get_taddr(struct netsnmp_transport_s *t, |
| 70 |
void **addr, size_t *addr_len) |
| 71 |
{ |
| 72 |
struct sockaddr_ipx *sa = t->remote; |
| 73 |
|
| 74 |
netsnmp_assert(t->remote_length == sizeof(*sa)); |
| 75 |
*addr_len = 12; |
| 76 |
if ((*addr = malloc(*addr_len))) { |
| 77 |
unsigned char *p = *addr; |
| 78 |
|
| 79 |
memcpy(p + 0, &sa->sipx_network, 4); |
| 80 |
memcpy(p + 4, &sa->sipx_node, 6); |
| 81 |
memcpy(p + 10, &sa->sipx_port, 2); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* You can write something into opaque that will subsequently get passed back |
| 87 |
* to your send function if you like. For instance, you might want to |
| 88 |
* remember where a PDU came from, so that you can send a reply there... |
| 89 |
*/ |
| 90 |
|
| 91 |
static int |
| 92 |
netsnmp_ipx_recv(netsnmp_transport *t, void *buf, int size, |
| 93 |
void **opaque, int *olength) |
| 94 |
{ |
| 95 |
int rc = -1; |
| 96 |
socklen_t fromlen = sizeof(struct sockaddr); |
| 97 |
struct sockaddr *from; |
| 98 |
|
| 99 |
if (t != NULL && t->sock >= 0) { |
| 100 |
from = (struct sockaddr *)malloc(sizeof(struct sockaddr_ipx)); |
| 101 |
if (from == NULL) { |
| 102 |
*opaque = NULL; |
| 103 |
*olength = 0; |
| 104 |
return -1; |
| 105 |
} else { |
| 106 |
memset(from, 0, fromlen); |
| 107 |
} |
| 108 |
|
| 109 |
while (rc < 0) { |
| 110 |
rc = recvfrom(t->sock, buf, size, 0, from, &fromlen); |
| 111 |
if (rc < 0 && errno != EINTR) { |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if (rc >= 0) { |
| 117 |
DEBUGIF("netsnmp_ipx") { |
| 118 |
char *str = netsnmp_ipx_fmtaddr(NULL, from, fromlen); |
| 119 |
DEBUGMSGTL(("netsnmp_ipx", |
| 120 |
"recvfrom fd %d got %d bytes(from %s)\n", |
| 121 |
t->sock, rc, str)); |
| 122 |
free(str); |
| 123 |
} |
| 124 |
} else { |
| 125 |
DEBUGMSGTL(("netsnmp_ipx", "recvfrom fd %d err %d (\"%s\")\n", |
| 126 |
t->sock, errno, strerror(errno))); |
| 127 |
} |
| 128 |
*opaque = (void *) from; |
| 129 |
*olength = sizeof(struct sockaddr_ipx); |
| 130 |
} |
| 131 |
return rc; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static int |
| 137 |
netsnmp_ipx_send(netsnmp_transport *t, const void *buf, int size, |
| 138 |
void **opaque, int *olength) |
| 139 |
{ |
| 140 |
int rc = -1; |
| 141 |
const struct sockaddr *to = NULL; |
| 142 |
|
| 143 |
if (opaque != NULL && *opaque != NULL && |
| 144 |
*olength == sizeof(struct sockaddr_ipx)) { |
| 145 |
to = (const struct sockaddr *) (*opaque); |
| 146 |
} else if (t != NULL && t->data != NULL && |
| 147 |
t->data_length == sizeof(struct sockaddr_ipx)) { |
| 148 |
to = (const struct sockaddr *) (t->data); |
| 149 |
} |
| 150 |
|
| 151 |
if (to != NULL && t != NULL && t->sock >= 0) { |
| 152 |
DEBUGIF("netsnmp_ipx") { |
| 153 |
char *str = netsnmp_ipx_fmtaddr(NULL, to, |
| 154 |
sizeof(struct sockaddr_ipx)); |
| 155 |
DEBUGMSGTL(("netsnmp_ipx", "send %d bytes from %p to %s on fd %d\n", |
| 156 |
size, buf, str, t->sock)); |
| 157 |
free(str); |
| 158 |
} |
| 159 |
while (rc < 0) { |
| 160 |
rc = sendto(t->sock, buf, size, 0, to, sizeof(struct sockaddr)); |
| 161 |
if (rc < 0 && errno != EINTR) { |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
return rc; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static int |
| 172 |
netsnmp_ipx_close(netsnmp_transport *t) |
| 173 |
{ |
| 174 |
int rc = -1; |
| 175 |
if (t->sock >= 0) { |
| 176 |
#ifndef HAVE_CLOSESOCKET |
| 177 |
rc = close(t->sock); |
| 178 |
#else |
| 179 |
rc = closesocket(t->sock); |
| 180 |
#endif |
| 181 |
t->sock = -1; |
| 182 |
} |
| 183 |
return rc; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
/* |
| 189 |
* Open a IPX-based transport for SNMP. Local is TRUE if addr is the local |
| 190 |
* address to bind to (i.e. this is a server-type session); otherwise addr is |
| 191 |
* the remote address to send things to. |
| 192 |
*/ |
| 193 |
|
| 194 |
netsnmp_transport * |
| 195 |
netsnmp_ipx_transport(const struct sockaddr_ipx *addr, int local) |
| 196 |
{ |
| 197 |
netsnmp_transport *t = NULL; |
| 198 |
int rc = 0; |
| 199 |
|
| 200 |
#ifdef NETSNMP_NO_LISTEN_SUPPORT |
| 201 |
if (local) |
| 202 |
return NULL; |
| 203 |
#endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
| 204 |
|
| 205 |
if (addr == NULL || addr->sipx_family != AF_IPX) { |
| 206 |
return NULL; |
| 207 |
} |
| 208 |
|
| 209 |
t = SNMP_MALLOC_TYPEDEF(netsnmp_transport); |
| 210 |
if (t == NULL) { |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
DEBUGIF("netsnmp_ipx") { |
| 215 |
char *str = netsnmp_ipx_fmtaddr(NULL, addr, |
| 216 |
sizeof(struct sockaddr_ipx)); |
| 217 |
DEBUGMSGTL(("netsnmp_ipx", "open %s %s\n", local ? "local" : "remote", |
| 218 |
str)); |
| 219 |
free(str); |
| 220 |
} |
| 221 |
|
| 222 |
t->domain = netsnmpIPXDomain; |
| 223 |
t->domain_length = netsnmpIPXDomain_len; |
| 224 |
|
| 225 |
t->sock = socket(AF_IPX, SOCK_DGRAM, AF_IPX); |
| 226 |
if (t->sock < 0) { |
| 227 |
netsnmp_transport_free(t); |
| 228 |
return NULL; |
| 229 |
} |
| 230 |
|
| 231 |
if (local) { |
| 232 |
#ifndef NETSNMP_NO_LISTEN_SUPPORT |
| 233 |
t->local_length = sizeof(*addr); |
| 234 |
t->local = netsnmp_memdup(addr, sizeof(*addr)); |
| 235 |
if (t->local == NULL) { |
| 236 |
netsnmp_transport_free(t); |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
* This session is inteneded as a server, so we must bind on to the |
| 242 |
* given address (which may include a particular network and/or node |
| 243 |
* address, but definitely includes a port number). |
| 244 |
*/ |
| 245 |
|
| 246 |
rc = bind(t->sock, addr, sizeof(struct sockaddr)); |
| 247 |
if (rc != 0) { |
| 248 |
netsnmp_ipx_close(t); |
| 249 |
netsnmp_transport_free(t); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
t->data = NULL; |
| 253 |
t->data_length = 0; |
| 254 |
#else /* NETSNMP_NO_LISTEN_SUPPORT */ |
| 255 |
return NULL; |
| 256 |
#endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
| 257 |
} else { |
| 258 |
t->remote_length = sizeof(*addr); |
| 259 |
t->remote = netsnmp_memdup(addr, sizeof(*addr)); |
| 260 |
if (t->remote == NULL) { |
| 261 |
netsnmp_transport_free(t); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* This is a client session. Save the address in the |
| 267 |
* transport-specific data pointer for later use by snmp_ipx_send. |
| 268 |
*/ |
| 269 |
|
| 270 |
t->data = malloc(sizeof(struct sockaddr_ipx)); |
| 271 |
if (t->data == NULL) { |
| 272 |
netsnmp_transport_free(t); |
| 273 |
return NULL; |
| 274 |
} |
| 275 |
memcpy(t->data, addr, sizeof(struct sockaddr_ipx)); |
| 276 |
t->data_length = sizeof(struct sockaddr_ipx); |
| 277 |
} |
| 278 |
|
| 279 |
/* |
| 280 |
* Maximum size of an IPX PDU is 576 bytes including a 30-byte header. |
| 281 |
* Ridiculous! |
| 282 |
*/ |
| 283 |
|
| 284 |
t->msgMaxSize = 576 - 30; |
| 285 |
t->f_recv = netsnmp_ipx_recv; |
| 286 |
t->f_send = netsnmp_ipx_send; |
| 287 |
t->f_close = netsnmp_ipx_close; |
| 288 |
t->f_accept = NULL; |
| 289 |
t->f_fmtaddr = netsnmp_ipx_fmtaddr; |
| 290 |
t->f_get_taddr = netsnmp_ipx_get_taddr; |
| 291 |
|
| 292 |
return t; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
/* |
| 298 |
* Attempt to parse a string of the form [%08x]:%12x[/%d] where the parts |
| 299 |
* are the network number, the node address and the port in that order. |
| 300 |
*/ |
| 301 |
|
| 302 |
int |
| 303 |
netsnmp_sockaddr_ipx2(struct sockaddr_ipx *addr, const char *peername, |
| 304 |
const char *default_target) |
| 305 |
{ |
| 306 |
char *input = NULL, *def = NULL; |
| 307 |
const char *network, *node, *port; |
| 308 |
char *tmp; |
| 309 |
unsigned long i; |
| 310 |
|
| 311 |
if (addr == NULL) { |
| 312 |
return 0; |
| 313 |
} |
| 314 |
memset(addr, 0, sizeof(struct sockaddr_ipx)); |
| 315 |
|
| 316 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", |
| 317 |
"addr %p, peername \"%s\" default_target \"%s\"\n", |
| 318 |
addr, peername ? peername : "[NIL]", |
| 319 |
default_target ? default_target : "[NIL]")); |
| 320 |
|
| 321 |
addr->sipx_family = AF_IPX; |
| 322 |
addr->sipx_type = 4; /* Specified in RFC 1420. */ |
| 323 |
|
| 324 |
network = input = strdup(peername ? peername : ""); |
| 325 |
tmp = strchr(input, ':'); |
| 326 |
if (tmp != NULL) { |
| 327 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "Node identified\n")); |
| 328 |
*tmp++ = '\0'; |
| 329 |
node = tmp; |
| 330 |
tmp = strchr(tmp, '/'); |
| 331 |
} else { |
| 332 |
node = NULL; |
| 333 |
tmp = strchr(input, '/'); |
| 334 |
} |
| 335 |
if (tmp != NULL) { |
| 336 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "Port identified\n")); |
| 337 |
*tmp++ = '\0'; |
| 338 |
port = tmp; |
| 339 |
} else |
| 340 |
port = NULL; |
| 341 |
|
| 342 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "Address: %s:%s/%s\n", |
| 343 |
network ? network : "[NIL]", node ? node : "[NIL]", |
| 344 |
port ? port : "[NIL]")); |
| 345 |
|
| 346 |
def = strdup(default_target ? default_target : ""); |
| 347 |
if (network == NULL || *network == '\0') |
| 348 |
network = def; |
| 349 |
tmp = strchr(def, ':'); |
| 350 |
if (tmp != NULL) { |
| 351 |
*tmp++ = '\0'; |
| 352 |
if (node == NULL || *node == '\0') |
| 353 |
node = tmp; |
| 354 |
tmp = strchr(tmp, '/'); |
| 355 |
} else |
| 356 |
tmp = strchr(def, '/'); |
| 357 |
if (tmp != NULL) { |
| 358 |
*tmp++ = '\0'; |
| 359 |
if (port == NULL || *port == '\0') |
| 360 |
port = tmp; |
| 361 |
} |
| 362 |
|
| 363 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "Address: %s:%s/%s\n", |
| 364 |
network ? network : "[NIL]", node ? node : "[NIL]", |
| 365 |
port ? port : "[NIL]")); |
| 366 |
|
| 367 |
if (network == NULL || *network == '\0') |
| 368 |
network = "0"; |
| 369 |
|
| 370 |
if (node == NULL || *node == '\0') |
| 371 |
node = "000000000000"; |
| 372 |
|
| 373 |
if (port == NULL || *port == '\0') |
| 374 |
#define val(x) __STRING(x) |
| 375 |
port = val(SNMP_IPX_DEFAULT_PORT); |
| 376 |
#undef val |
| 377 |
|
| 378 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "Address: %s:%s/%s\n", |
| 379 |
network ? network : "[NIL]", node ? node : "[NIL]", |
| 380 |
port ? port : "[NIL]")); |
| 381 |
|
| 382 |
if(sscanf(network, "%8lx%*c", &i) == 1) { |
| 383 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "network parsed okay\n")); |
| 384 |
addr->sipx_network = htonl(i); |
| 385 |
} else { |
| 386 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", |
| 387 |
"failed to parse network part of address\n")); |
| 388 |
free(def); |
| 389 |
free(input); |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
if(sscanf(node, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%*c", |
| 394 |
&addr->sipx_node[0], &addr->sipx_node[1], |
| 395 |
&addr->sipx_node[2], &addr->sipx_node[3], |
| 396 |
&addr->sipx_node[4], &addr->sipx_node[5]) == 6) { |
| 397 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "node parsed okay\n")); |
| 398 |
} else { |
| 399 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", |
| 400 |
"failed to parse node part of address\n")); |
| 401 |
free(def); |
| 402 |
free(input); |
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
if(sscanf(port, "%lu%*c", &i) == 1) { |
| 407 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", "port parsed okay\n")); |
| 408 |
addr->sipx_port = htons(i); |
| 409 |
} else { |
| 410 |
DEBUGMSGTL(("netsnmp_sockaddr_ipx", |
| 411 |
"failed to parse port part of address\n")); |
| 412 |
free(def); |
| 413 |
free(input); |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
free(def); |
| 418 |
free(input); |
| 419 |
return 1; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
int |
| 425 |
netsnmp_sockaddr_ipx(struct sockaddr_ipx *addr, const char *peername) |
| 426 |
{ |
| 427 |
return netsnmp_sockaddr_ipx2(addr, peername, NULL); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
netsnmp_transport * |
| 433 |
netsnmp_ipx_create_tstring(const char *str, int local, |
| 434 |
const char *default_target) |
| 435 |
{ |
| 436 |
struct sockaddr_ipx addr; |
| 437 |
|
| 438 |
if (netsnmp_sockaddr_ipx2(&addr, str, default_target)) { |
| 439 |
return netsnmp_ipx_transport(&addr, local); |
| 440 |
} else { |
| 441 |
return NULL; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
static int netsnmp_ipx_ostring_to_sockaddr(struct sockaddr_ipx *sa, |
| 446 |
const void *o, size_t o_len) |
| 447 |
{ |
| 448 |
const char *p = o; |
| 449 |
|
| 450 |
if (o_len != 12) |
| 451 |
return 0; |
| 452 |
|
| 453 |
memset(sa, 0, sizeof(*sa)); |
| 454 |
sa->sipx_family = AF_IPX; |
| 455 |
memcpy(&sa->sipx_network, p + 0, 4); |
| 456 |
memcpy(&sa->sipx_node, p + 4, 6); |
| 457 |
memcpy(&sa->sipx_port, p + 10, 2); |
| 458 |
return 1; |
| 459 |
} |
| 460 |
|
| 461 |
netsnmp_transport * |
| 462 |
netsnmp_ipx_create_ostring(const void *o, size_t o_len, int local) |
| 463 |
{ |
| 464 |
struct sockaddr_ipx sa; |
| 465 |
|
| 466 |
if (netsnmp_ipx_ostring_to_sockaddr(&sa, o, o_len)) |
| 467 |
return netsnmp_ipx_transport(&sa, local); |
| 468 |
|
| 469 |
return NULL; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
void |
| 475 |
netsnmp_ipx_ctor(void) |
| 476 |
{ |
| 477 |
ipxDomain.name = netsnmpIPXDomain; |
| 478 |
ipxDomain.name_length = netsnmpIPXDomain_len; |
| 479 |
ipxDomain.prefix = (const char**)calloc(2, sizeof(char *)); |
| 480 |
ipxDomain.prefix[0] = "ipx"; |
| 481 |
|
| 482 |
ipxDomain.f_create_from_tstring_new = netsnmp_ipx_create_tstring; |
| 483 |
ipxDomain.f_create_from_ostring = netsnmp_ipx_create_ostring; |
| 484 |
|
| 485 |
netsnmp_tdomain_register(&ipxDomain); |
| 486 |
} |