00001 #include <net-snmp/net-snmp-config.h>
00002
00003 #include <stdio.h>
00004 #include <sys/types.h>
00005 #include <ctype.h>
00006 #include <errno.h>
00007
00008 #if HAVE_STRING_H
00009 #include <string.h>
00010 #else
00011 #include <strings.h>
00012 #endif
00013 #if HAVE_STDLIB_H
00014 #include <stdlib.h>
00015 #endif
00016 #if HAVE_UNISTD_H
00017 #include <unistd.h>
00018 #endif
00019 #if HAVE_SYS_SOCKET_H
00020 #include <sys/socket.h>
00021 #endif
00022 #if HAVE_NETINET_IN_H
00023 #include <netinet/in.h>
00024 #endif
00025
00026 #if HAVE_DMALLOC_H
00027 #include <dmalloc.h>
00028 #endif
00029
00030 #include <net-snmp/types.h>
00031 #include <net-snmp/output_api.h>
00032 #include <net-snmp/config_api.h>
00033
00034 #include <net-snmp/library/snmp_transport.h>
00035 #include <net-snmp/library/snmpIPXDomain.h>
00036
00037 #define SNMP_IPX_DEFAULT_PORT 36879
00038 static netsnmp_tdomain ipxDomain;
00039
00040
00041
00042
00043
00044
00045 static char *
00046 netsnmp_ipx_fmtaddr(netsnmp_transport *t, void *data, int len)
00047 {
00048 struct sockaddr_ipx *to = NULL;
00049
00050 if (data != NULL && len == sizeof(struct sockaddr_ipx)) {
00051 to = (struct sockaddr_ipx *) data;
00052 } else if (t != NULL && t->data != NULL) {
00053 to = (struct sockaddr_ipx *) t->data;
00054 }
00055 if (to == NULL) {
00056 return strdup("IPX: unknown");
00057 } else {
00058 char tmp[64];
00059 sprintf(tmp, "IPX: %08X:%02X%02X%02X%02X%02X%02X/%hu",
00060 ntohl(to->sipx_network), to->sipx_node[0],
00061 to->sipx_node[1], to->sipx_node[2], to->sipx_node[3],
00062 to->sipx_node[4], to->sipx_node[5], ntohs(to->sipx_port));
00063 return strdup(tmp);
00064 }
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 static int
00076 netsnmp_ipx_recv(netsnmp_transport *t, void *buf, int size,
00077 void **opaque, int *olength)
00078 {
00079 int rc = -1, fromlen = sizeof(struct sockaddr);
00080 struct sockaddr *from;
00081
00082 if (t != NULL && t->sock >= 0) {
00083 from = (struct sockaddr *)malloc(sizeof(struct sockaddr_ipx));
00084 if (from == NULL) {
00085 *opaque = NULL;
00086 *olength = 0;
00087 return -1;
00088 } else {
00089 memset(from, 0, fromlen);
00090 }
00091
00092 while (rc < 0) {
00093 rc = recvfrom(t->sock, buf, size, 0, from, &fromlen);
00094 if (rc < 0 && errno != EINTR) {
00095 break;
00096 }
00097 }
00098
00099 if (rc >= 0) {
00100 char *str = netsnmp_ipx_fmtaddr(NULL, from, fromlen);
00101 DEBUGMSGTL(("netsnmp_ipx","recvfrom fd %d got %d bytes(from %s)\n",
00102 t->sock, rc, str));
00103 free(str);
00104 } else {
00105 DEBUGMSGTL(("netsnmp_ipx", "recvfrom fd %d err %d (\"%s\")\n",
00106 t->sock, errno, strerror(errno)));
00107 }
00108 *opaque = (void *) from;
00109 *olength = sizeof(struct sockaddr_ipx);
00110 }
00111 return rc;
00112 }
00113
00114
00115
00116 static int
00117 netsnmp_ipx_send(netsnmp_transport *t, void *buf, int size,
00118 void **opaque, int *olength)
00119 {
00120 int rc = -1;
00121 struct sockaddr *to = NULL;
00122
00123 if (opaque != NULL && *opaque != NULL &&
00124 *olength == sizeof(struct sockaddr_ipx)) {
00125 to = (struct sockaddr *) (*opaque);
00126 } else if (t != NULL && t->data != NULL &&
00127 t->data_length == sizeof(struct sockaddr_ipx)) {
00128 to = (struct sockaddr *) (t->data);
00129 }
00130
00131 if (to != NULL && t != NULL && t->sock >= 0) {
00132 char *str = netsnmp_ipx_fmtaddr(NULL, (void *)to,
00133 sizeof(struct sockaddr_ipx));
00134 DEBUGMSGTL(("netsnmp_ipx", "send %d bytes from %p to %s on fd %d\n",
00135 size, buf, str, t->sock));
00136 free(str);
00137 while (rc < 0) {
00138 rc = sendto(t->sock, buf, size, 0, to, sizeof(struct sockaddr));
00139 if (rc < 0 && errno != EINTR) {
00140 break;
00141 }
00142 }
00143 }
00144 return rc;
00145 }
00146
00147
00148
00149 static int
00150 netsnmp_ipx_close(netsnmp_transport *t)
00151 {
00152 int rc = -1;
00153 if (t->sock >= 0) {
00154 #ifndef HAVE_CLOSESOCKET
00155 rc = close(t->sock);
00156 #else
00157 rc = closesocket(t->sock);
00158 #endif
00159 t->sock = -1;
00160 }
00161 return rc;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 netsnmp_transport *
00173 netsnmp_ipx_transport(struct sockaddr_ipx *addr, int local)
00174 {
00175 netsnmp_transport *t = NULL;
00176 int rc = 0;
00177 char *str = NULL;
00178
00179 if (addr == NULL || addr->sipx_family != AF_IPX) {
00180 return NULL;
00181 }
00182
00183 t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
00184 if (t == NULL) {
00185 return NULL;
00186 }
00187
00188 str = netsnmp_ipx_fmtaddr(NULL, (void *)addr,
00189 sizeof(struct sockaddr_ipx));
00190 DEBUGMSGTL(("netsnmp_ipx", "open %s %s\n", local ? "local" : "remote",
00191 str));
00192 free(str);
00193
00194 memset(t, 0, sizeof(netsnmp_transport));
00195
00196 t->domain = netsnmpIPXDomain;
00197 t->domain_length = netsnmpIPXDomain_len;
00198
00199 t->sock = socket(AF_IPX, SOCK_DGRAM, AF_IPX);
00200 if (t->sock < 0) {
00201 netsnmp_transport_free(t);
00202 return NULL;
00203 }
00204
00205 if (local) {
00206 t->local = malloc(12);
00207 if (t->local == NULL) {
00208 netsnmp_transport_free(t);
00209 return NULL;
00210 }
00211 memcpy(&(t->local[00]), (u_char *) & (addr->sipx_network), 4);
00212 memcpy(&(t->local[04]), (u_char *) & (addr->sipx_node), 6);
00213 memcpy(&(t->local[10]), (u_char *) & (addr->sipx_port), 2);
00214 t->local_length = 12;
00215
00216
00217
00218
00219
00220
00221
00222 rc = bind(t->sock, (struct sockaddr *) addr,
00223 sizeof(struct sockaddr));
00224 if (rc != 0) {
00225 netsnmp_ipx_close(t);
00226 netsnmp_transport_free(t);
00227 return NULL;
00228 }
00229 t->data = NULL;
00230 t->data_length = 0;
00231 } else {
00232 t->remote = malloc(12);
00233 if (t->remote == NULL) {
00234 netsnmp_transport_free(t);
00235 return NULL;
00236 }
00237 memcpy(&(t->remote[00]), (u_char *) & (addr->sipx_network), 4);
00238 memcpy(&(t->remote[04]), (u_char *) & (addr->sipx_node), 6);
00239 memcpy(&(t->remote[10]), (u_char *) & (addr->sipx_port), 2);
00240 t->remote_length = 12;
00241
00242
00243
00244
00245
00246
00247 t->data = malloc(sizeof(struct sockaddr_ipx));
00248 if (t->data == NULL) {
00249 netsnmp_transport_free(t);
00250 return NULL;
00251 }
00252 memcpy(t->data, addr, sizeof(struct sockaddr_ipx));
00253 t->data_length = sizeof(struct sockaddr_ipx);
00254 }
00255
00256
00257
00258
00259
00260
00261 t->msgMaxSize = 576 - 30;
00262 t->f_recv = netsnmp_ipx_recv;
00263 t->f_send = netsnmp_ipx_send;
00264 t->f_close = netsnmp_ipx_close;
00265 t->f_accept = NULL;
00266 t->f_fmtaddr = netsnmp_ipx_fmtaddr;
00267
00268 return t;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278 int
00279 netsnmp_sockaddr_ipx(struct sockaddr_ipx *addr, const char *peername)
00280 {
00281 char *cp = NULL;
00282 unsigned int network = 0, i = 0;
00283
00284 if (addr == NULL) {
00285 return 0;
00286 }
00287 memset(addr, 0, sizeof(struct sockaddr_ipx));
00288
00289 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "addr %p, peername \"%s\"\n",
00290 addr, peername ? peername : "[NIL]"));
00291
00292 addr->sipx_family = AF_IPX;
00293 addr->sipx_type = 4;
00294
00295 if (peername == NULL) {
00296 return 0;
00297 }
00298
00299
00300
00301
00302
00303 while (*peername && isspace((int) *peername)) {
00304 peername++;
00305 }
00306
00307 if (!*peername) {
00308
00309
00310
00311
00312 addr->sipx_network = htonl(0);
00313 for (i = 0; i < 6; i++) {
00314 addr->sipx_node[i] = 0;
00315 }
00316 addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
00317 return 1;
00318 }
00319
00320
00321
00322
00323
00324 network = strtoul(peername, &cp, 16);
00325 if (cp != peername) {
00326 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "network parsed okay\n"));
00327 addr->sipx_network = htonl(network);
00328 peername = cp;
00329 } else {
00330 DEBUGMSGTL(("netsnmp_sockaddr_ipx",
00331 "no network part of address\n"));
00332 addr->sipx_network = htonl(0);
00333 }
00334
00335 if (*peername == ':') {
00336
00337
00338
00339 int node[6] = { 0, 0, 0, 0, 0, 0 }, rc = 0;
00340 unsigned short port = 0;
00341 rc = sscanf(peername, ":%02X%02X%02X%02X%02X%02X/%hu",
00342 &node[0], &node[1], &node[2], &node[3], &node[4],
00343 &node[5], &port);
00344 if (rc < 6) {
00345 DEBUGMSGTL(("netsnmp_sockaddr_ipx",
00346 "no node -- fail (rc %d)\n", rc));
00347 return 0;
00348 } else if (rc == 6) {
00349 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "node, no port\n"));
00350 for (i = 0; i < 6; i++) {
00351 addr->sipx_node[i] = node[i];
00352 }
00353 addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
00354 } else if (rc == 7) {
00355 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "node and port\n"));
00356 for (i = 0; i < 6; i++) {
00357 addr->sipx_node[i] = node[i];
00358 }
00359 addr->sipx_port = htons(port);
00360 }
00361 } else if (*peername == '/') {
00362
00363
00364
00365 unsigned short port = 0;
00366 for (i = 0; i < 6; i++) {
00367 addr->sipx_node[i] = 0;
00368 }
00369 if (sscanf(peername, "/%hu", &port) != 1) {
00370 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "no port\n"));
00371 addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
00372 } else {
00373 addr->sipx_port = htons(port);
00374 }
00375 } else {
00376 return 0;
00377 }
00378
00379 return 1;
00380 }
00381
00382
00383
00384 netsnmp_transport *
00385 netsnmp_ipx_create_tstring(const char *str, int local)
00386 {
00387 struct sockaddr_ipx addr;
00388
00389 if (netsnmp_sockaddr_ipx(&addr, str)) {
00390 return netsnmp_ipx_transport(&addr, local);
00391 } else {
00392 return NULL;
00393 }
00394 }
00395
00396
00397
00398 netsnmp_transport *
00399 netsnmp_ipx_create_ostring(const u_char * o, size_t o_len, int local)
00400 {
00401 struct sockaddr_ipx addr;
00402
00403 if (o_len == 12) {
00404 addr.sipx_family = AF_IPX;
00405 memcpy((u_char *) & (addr.sipx_network), &(o[00]), 4);
00406 memcpy((u_char *) & (addr.sipx_node), &(o[04]), 6);
00407 memcpy((u_char *) & (addr.sipx_port), &(o[10]), 2);
00408 return netsnmp_ipx_transport(&addr, local);
00409 }
00410 return NULL;
00411 }
00412
00413
00414
00415 void
00416 netsnmp_ipx_ctor(void)
00417 {
00418 ipxDomain.name = netsnmpIPXDomain;
00419 ipxDomain.name_length = netsnmpIPXDomain_len;
00420 ipxDomain.prefix = calloc(2, sizeof(char *));
00421 ipxDomain.prefix[0] = "ipx";
00422
00423 ipxDomain.f_create_from_tstring = netsnmp_ipx_create_tstring;
00424 ipxDomain.f_create_from_ostring = netsnmp_ipx_create_ostring;
00425
00426 netsnmp_tdomain_register(&ipxDomain);
00427 }