1 #include <net-snmp/net-snmp-config.h>
20 #include <sys/socket.h>
23 #include <netinet/in.h>
30 #include <net-snmp/types.h>
31 #include <net-snmp/output_api.h>
32 #include <net-snmp/config_api.h>
34 #include <net-snmp/library/snmp_transport.h>
35 #include <net-snmp/library/snmpIPXDomain.h>
37 #define SNMP_IPX_DEFAULT_PORT 36879 /* Specified in RFC 1420. */
38 static netsnmp_tdomain ipxDomain;
41 * Return a string representing the address in data, or else the "far end"
42 * address if data is NULL.
46 netsnmp_ipx_fmtaddr(netsnmp_transport *t, void *data, int len)
48 struct sockaddr_ipx *to = NULL;
50 if (data != NULL && len == sizeof(struct sockaddr_ipx)) {
51 to = (struct sockaddr_ipx *) data;
52 } else if (t != NULL && t->data != NULL) {
53 to = (struct sockaddr_ipx *) t->data;
56 return strdup("IPX: unknown");
59 sprintf(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));
70 * You can write something into opaque that will subsequently get passed back
71 * to your send function if you like. For instance, you might want to
72 * remember where a PDU came from, so that you can send a reply there...
76 netsnmp_ipx_recv(netsnmp_transport *t, void *buf, int size,
77 void **opaque, int *olength)
79 int rc = -1, fromlen = sizeof(struct sockaddr);
80 struct sockaddr *from;
82 if (t != NULL && t->sock >= 0) {
83 from = (struct sockaddr *)malloc(sizeof(struct sockaddr_ipx));
89 memset(from, 0, fromlen);
93 rc = recvfrom(t->sock, buf, size, 0, from, &fromlen);
94 if (rc < 0 && errno != EINTR) {
100 char *string = netsnmp_ipx_fmtaddr(NULL, from, fromlen);
101 DEBUGMSGTL(("netsnmp_ipx","recvfrom fd %d got %d bytes(from %s)\n",
102 t->sock, rc, string));
105 DEBUGMSGTL(("netsnmp_ipx", "recvfrom fd %d err %d (\"%s\")\n",
106 t->sock, errno, strerror(errno)));
108 *opaque = (void *) from;
109 *olength = sizeof(struct sockaddr_ipx);
117 netsnmp_ipx_send(netsnmp_transport *t, void *buf, int size,
118 void **opaque, int *olength)
121 struct sockaddr *to = NULL;
123 if (opaque != NULL && *opaque != NULL &&
124 *olength == sizeof(struct sockaddr_ipx)) {
125 to = (struct sockaddr *) (*opaque);
126 } else if (t != NULL && t->data != NULL &&
127 t->data_length == sizeof(struct sockaddr_ipx)) {
128 to = (struct sockaddr *) (t->data);
131 if (to != NULL && t != NULL && t->sock >= 0) {
132 char *string = netsnmp_ipx_fmtaddr(NULL, (void *)to,
133 sizeof(struct sockaddr_ipx));
134 DEBUGMSGTL(("netsnmp_ipx", "send %d bytes from %p to %s on fd %d\n",
135 size, buf, string, t->sock));
138 rc = sendto(t->sock, buf, size, 0, to, sizeof(struct sockaddr));
139 if (rc < 0 && errno != EINTR) {
150 netsnmp_ipx_close(netsnmp_transport *t)
154 #ifndef HAVE_CLOSESOCKET
157 rc = closesocket(t->sock);
167 * Open a IPX-based transport for SNMP. Local is TRUE if addr is the local
168 * address to bind to (i.e. this is a server-type session); otherwise addr is
169 * the remote address to send things to.
173 netsnmp_ipx_transport(struct sockaddr_ipx *addr, int local)
175 netsnmp_transport *t = NULL;
179 if (addr == NULL || addr->sipx_family != AF_IPX) {
183 t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
188 string = netsnmp_ipx_fmtaddr(NULL, (void *)addr,
189 sizeof(struct sockaddr_ipx));
190 DEBUGMSGTL(("netsnmp_ipx", "open %s %s\n", local ? "local" : "remote",
194 memset(t, 0, sizeof(netsnmp_transport));
196 t->domain = netsnmpIPXDomain;
197 t->domain_length = netsnmpIPXDomain_len;
199 t->sock = socket(AF_IPX, SOCK_DGRAM, AF_IPX);
201 netsnmp_transport_free(t);
206 t->local = malloc(12);
207 if (t->local == NULL) {
208 netsnmp_transport_free(t);
211 memcpy(&(t->local[00]), (u_char *) & (addr->sipx_network), 4);
212 memcpy(&(t->local[04]), (u_char *) & (addr->sipx_node), 6);
213 memcpy(&(t->local[10]), (u_char *) & (addr->sipx_port), 2);
214 t->local_length = 12;
217 * This session is inteneded as a server, so we must bind on to the
218 * given address (which may include a particular network and/or node
219 * address, but definitely includes a port number).
222 rc = bind(t->sock, (struct sockaddr *) addr,
223 sizeof(struct sockaddr));
225 netsnmp_ipx_close(t);
226 netsnmp_transport_free(t);
232 t->remote = malloc(12);
233 if (t->remote == NULL) {
234 netsnmp_transport_free(t);
237 memcpy(&(t->remote[00]), (u_char *) & (addr->sipx_network), 4);
238 memcpy(&(t->remote[04]), (u_char *) & (addr->sipx_node), 6);
239 memcpy(&(t->remote[10]), (u_char *) & (addr->sipx_port), 2);
240 t->remote_length = 12;
243 * This is a client session. Save the address in the
244 * transport-specific data pointer for later use by snmp_ipx_send.
247 t->data = malloc(sizeof(struct sockaddr_ipx));
248 if (t->data == NULL) {
249 netsnmp_transport_free(t);
252 memcpy(t->data, addr, sizeof(struct sockaddr_ipx));
253 t->data_length = sizeof(struct sockaddr_ipx);
257 * Maximum size of an IPX PDU is 576 bytes including a 30-byte header.
261 t->msgMaxSize = 576 - 30;
262 t->f_recv = netsnmp_ipx_recv;
263 t->f_send = netsnmp_ipx_send;
264 t->f_close = netsnmp_ipx_close;
266 t->f_fmtaddr = netsnmp_ipx_fmtaddr;
274 * Attempt to parse a string of the form [%08x]:%12x[/%d] where the parts
275 * are the network number, the node address and the port in that order.
279 netsnmp_sockaddr_ipx(struct sockaddr_ipx *addr, const char *peername)
282 unsigned int network = 0, i = 0;
287 memset(addr, 0, sizeof(struct sockaddr_ipx));
289 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "addr %p, peername \"%s\"\n",
290 addr, peername ? peername : "[NIL]"));
292 addr->sipx_family = AF_IPX;
293 addr->sipx_type = 4; /* Specified in RFC 1420. */
295 if (peername == NULL) {
300 * Skip leading white space.
303 while (*peername && isspace((int) *peername)) {
309 * Completely blank address. Let this mean "any network, any address,
310 * default SNMP port".
312 addr->sipx_network = htonl(0);
313 for (i = 0; i < 6; i++) {
314 addr->sipx_node[i] = 0;
316 addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
321 * Try to get a leading network address.
324 network = strtoul(peername, &cp, 16);
325 if (cp != peername) {
326 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "network parsed okay\n"));
327 addr->sipx_network = htonl(network);
330 DEBUGMSGTL(("netsnmp_sockaddr_ipx",
331 "no network part of address\n"));
332 addr->sipx_network = htonl(0);
335 if (*peername == ':') {
337 * Okay we are looking for a node number plus optionally a port here.
339 int node[6] = { 0, 0, 0, 0, 0, 0 }, rc = 0;
340 unsigned short port = 0;
341 rc = sscanf(peername, ":%02X%02X%02X%02X%02X%02X/%hu",
342 &node[0], &node[1], &node[2], &node[3], &node[4],
345 DEBUGMSGTL(("netsnmp_sockaddr_ipx",
346 "no node -- fail (rc %d)\n", rc));
348 } else if (rc == 6) {
349 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "node, no port\n"));
350 for (i = 0; i < 6; i++) {
351 addr->sipx_node[i] = node[i];
353 addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
354 } else if (rc == 7) {
355 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "node and port\n"));
356 for (i = 0; i < 6; i++) {
357 addr->sipx_node[i] = node[i];
359 addr->sipx_port = htons(port);
361 } else if (*peername == '/') {
363 * Okay we are just looking for a port number here.
365 unsigned short port = 0;
366 for (i = 0; i < 6; i++) {
367 addr->sipx_node[i] = 0;
369 if (sscanf(peername, "/%hu", &port) != 1) {
370 DEBUGMSGTL(("netsnmp_sockaddr_ipx", "no port\n"));
371 addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
373 addr->sipx_port = htons(port);
385 netsnmp_ipx_create_tstring(const char *string, int local)
387 struct sockaddr_ipx addr;
389 if (netsnmp_sockaddr_ipx(&addr, string)) {
390 return netsnmp_ipx_transport(&addr, local);
399 netsnmp_ipx_create_ostring(const u_char * o, size_t o_len, int local)
401 struct sockaddr_ipx addr;
404 addr.sipx_family = AF_IPX;
405 memcpy((u_char *) & (addr.sipx_network), &(o[00]), 4);
406 memcpy((u_char *) & (addr.sipx_node), &(o[04]), 6);
407 memcpy((u_char *) & (addr.sipx_port), &(o[10]), 2);
408 return netsnmp_ipx_transport(&addr, local);
416 netsnmp_ipx_ctor(void)
418 ipxDomain.name = netsnmpIPXDomain;
419 ipxDomain.name_length = netsnmpIPXDomain_len;
420 ipxDomain.prefix = calloc(2, sizeof(char *));
421 ipxDomain.prefix[0] = "ipx";
423 ipxDomain.f_create_from_tstring = netsnmp_ipx_create_tstring;
424 ipxDomain.f_create_from_ostring = netsnmp_ipx_create_ostring;
426 netsnmp_tdomain_register(&ipxDomain);