Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmpIPXDomain.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <ctype.h>
6 #include <errno.h>
7
8 #if HAVE_STRING_H
9 #include <string.h>
10 #else
11 #include <strings.h>
12 #endif
13 #if HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #if HAVE_SYS_SOCKET_H
20 #include <sys/socket.h>
21 #endif
22 #if HAVE_NETINET_IN_H
23 #include <netinet/in.h>
24 #endif
25
26 #if HAVE_DMALLOC_H
27 #include <dmalloc.h>
28 #endif
29
30 #include <net-snmp/types.h>
31 #include <net-snmp/output_api.h>
32 #include <net-snmp/config_api.h>
33
34 #include <net-snmp/library/snmp_transport.h>
35 #include <net-snmp/library/snmpIPXDomain.h>
36
37 #define SNMP_IPX_DEFAULT_PORT   36879   /*  Specified in RFC 1420.  */
38 static netsnmp_tdomain ipxDomain;
39
40 /*
41  * Return a string representing the address in data, or else the "far end"
42  * address if data is NULL.  
43  */
44
45 static char *
46 netsnmp_ipx_fmtaddr(netsnmp_transport *t, void *data, int len)
47 {
48     struct sockaddr_ipx *to = NULL;
49
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;
54     }
55     if (to == NULL) {
56         return strdup("IPX: unknown");
57     } else {
58         char tmp[64];
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));
63         return strdup(tmp);
64     }
65 }
66
67
68
69 /*
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...  
73  */
74
75 static int
76 netsnmp_ipx_recv(netsnmp_transport *t, void *buf, int size,
77                  void **opaque, int *olength)
78 {
79     int rc = -1, fromlen = sizeof(struct sockaddr);
80     struct sockaddr *from;
81
82     if (t != NULL && t->sock >= 0) {
83         from = (struct sockaddr *)malloc(sizeof(struct sockaddr_ipx));
84         if (from == NULL) {
85             *opaque = NULL;
86             *olength = 0;
87             return -1;
88         } else {
89             memset(from, 0, fromlen);
90         }
91
92         while (rc < 0) {
93           rc = recvfrom(t->sock, buf, size, 0, from, &fromlen);
94           if (rc < 0 && errno != EINTR) {
95             break;
96           }
97         }
98
99         if (rc >= 0) {
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));
103             free(string);
104         } else {
105             DEBUGMSGTL(("netsnmp_ipx", "recvfrom fd %d err %d (\"%s\")\n",
106                         t->sock, errno, strerror(errno)));
107         }
108         *opaque = (void *) from;
109         *olength = sizeof(struct sockaddr_ipx);
110     }
111     return rc;
112 }
113
114
115
116 static int
117 netsnmp_ipx_send(netsnmp_transport *t, void *buf, int size,
118                  void **opaque, int *olength)
119 {
120     int rc = -1;
121     struct sockaddr *to = NULL;
122
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);
129     }
130
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));
136         free(string);
137         while (rc < 0) {
138             rc = sendto(t->sock, buf, size, 0, to, sizeof(struct sockaddr));
139             if (rc < 0 && errno != EINTR) {
140                 break;
141             }
142         }
143     }
144     return rc;
145 }
146
147
148
149 static int
150 netsnmp_ipx_close(netsnmp_transport *t)
151 {
152     int rc = -1;
153     if (t->sock >= 0) {
154 #ifndef HAVE_CLOSESOCKET
155         rc = close(t->sock);
156 #else
157         rc = closesocket(t->sock);
158 #endif
159         t->sock = -1;
160     }
161     return rc;
162 }
163
164
165
166 /*
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.  
170  */
171
172 netsnmp_transport *
173 netsnmp_ipx_transport(struct sockaddr_ipx *addr, int local)
174 {
175     netsnmp_transport *t = NULL;
176     int             rc = 0;
177     char           *string = NULL;
178
179     if (addr == NULL || addr->sipx_family != AF_IPX) {
180         return NULL;
181     }
182
183     t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
184     if (t == NULL) {
185         return NULL;
186     }
187
188     string = netsnmp_ipx_fmtaddr(NULL, (void *)addr, 
189                                  sizeof(struct sockaddr_ipx));
190     DEBUGMSGTL(("netsnmp_ipx", "open %s %s\n", local ? "local" : "remote",
191                 string));
192     free(string);
193
194     memset(t, 0, sizeof(netsnmp_transport));
195
196     t->domain = netsnmpIPXDomain;
197     t->domain_length = netsnmpIPXDomain_len;
198
199     t->sock = socket(AF_IPX, SOCK_DGRAM, AF_IPX);
200     if (t->sock < 0) {
201         netsnmp_transport_free(t);
202         return NULL;
203     }
204
205     if (local) {
206         t->local = malloc(12);
207         if (t->local == NULL) {
208             netsnmp_transport_free(t);
209             return NULL;
210         }
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;
215
216         /*
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).
220          */
221
222         rc = bind(t->sock, (struct sockaddr *) addr,
223                   sizeof(struct sockaddr));
224         if (rc != 0) {
225             netsnmp_ipx_close(t);
226             netsnmp_transport_free(t);
227             return NULL;
228         }
229         t->data = NULL;
230         t->data_length = 0;
231     } else {
232         t->remote = malloc(12);
233         if (t->remote == NULL) {
234             netsnmp_transport_free(t);
235             return NULL;
236         }
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;
241
242         /*
243          * This is a client session.  Save the address in the
244          * transport-specific data pointer for later use by snmp_ipx_send.
245          */
246
247         t->data = malloc(sizeof(struct sockaddr_ipx));
248         if (t->data == NULL) {
249             netsnmp_transport_free(t);
250             return NULL;
251         }
252         memcpy(t->data, addr, sizeof(struct sockaddr_ipx));
253         t->data_length = sizeof(struct sockaddr_ipx);
254     }
255
256     /*
257      * Maximum size of an IPX PDU is 576 bytes including a 30-byte header.
258      * Ridiculous!  
259      */
260
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;
265     t->f_accept   = NULL;
266     t->f_fmtaddr  = netsnmp_ipx_fmtaddr;
267
268     return t;
269 }
270
271
272
273 /*
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.  
276  */
277
278 int
279 netsnmp_sockaddr_ipx(struct sockaddr_ipx *addr, const char *peername)
280 {
281     char           *cp = NULL;
282     unsigned int    network = 0, i = 0;
283
284     if (addr == NULL) {
285         return 0;
286     }
287     memset(addr, 0, sizeof(struct sockaddr_ipx));
288
289     DEBUGMSGTL(("netsnmp_sockaddr_ipx", "addr %p, peername \"%s\"\n",
290                 addr, peername ? peername : "[NIL]"));
291
292     addr->sipx_family = AF_IPX;
293     addr->sipx_type = 4;        /*  Specified in RFC 1420.  */
294
295     if (peername == NULL) {
296         return 0;
297     }
298
299     /*
300      * Skip leading white space.  
301      */
302
303     while (*peername && isspace((int) *peername)) {
304         peername++;
305     }
306
307     if (!*peername) {
308         /*
309          * Completely blank address.  Let this mean "any network, any address,
310          * default SNMP port".  
311          */
312         addr->sipx_network = htonl(0);
313         for (i = 0; i < 6; i++) {
314             addr->sipx_node[i] = 0;
315         }
316         addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
317         return 1;
318     }
319
320     /*
321      * Try to get a leading network address.  
322      */
323
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);
328         peername = cp;
329     } else {
330         DEBUGMSGTL(("netsnmp_sockaddr_ipx",
331                     "no network part of address\n"));
332         addr->sipx_network = htonl(0);
333     }
334
335     if (*peername == ':') {
336         /*
337          * Okay we are looking for a node number plus optionally a port here.  
338          */
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],
343                     &node[5], &port);
344         if (rc < 6) {
345             DEBUGMSGTL(("netsnmp_sockaddr_ipx",
346                         "no node -- fail (rc %d)\n", rc));
347             return 0;
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];
352             }
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];
358             }
359             addr->sipx_port = htons(port);
360         }
361     } else if (*peername == '/') {
362         /*
363          * Okay we are just looking for a port number here.  
364          */
365         unsigned short  port = 0;
366         for (i = 0; i < 6; i++) {
367             addr->sipx_node[i] = 0;
368         }
369         if (sscanf(peername, "/%hu", &port) != 1) {
370             DEBUGMSGTL(("netsnmp_sockaddr_ipx", "no port\n"));
371             addr->sipx_port = htons(SNMP_IPX_DEFAULT_PORT);
372         } else {
373             addr->sipx_port = htons(port);
374         }
375     } else {
376         return 0;
377     }
378
379     return 1;
380 }
381
382
383
384 netsnmp_transport *
385 netsnmp_ipx_create_tstring(const char *string, int local)
386 {
387     struct sockaddr_ipx addr;
388
389     if (netsnmp_sockaddr_ipx(&addr, string)) {
390         return netsnmp_ipx_transport(&addr, local);
391     } else {
392         return NULL;
393     }
394 }
395
396
397
398 netsnmp_transport *
399 netsnmp_ipx_create_ostring(const u_char * o, size_t o_len, int local)
400 {
401     struct sockaddr_ipx addr;
402
403     if (o_len == 12) {
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);
409     }
410     return NULL;
411 }
412
413
414
415 void
416 netsnmp_ipx_ctor(void)
417 {
418     ipxDomain.name = netsnmpIPXDomain;
419     ipxDomain.name_length = netsnmpIPXDomain_len;
420     ipxDomain.prefix = calloc(2, sizeof(char *));
421     ipxDomain.prefix[0] = "ipx";
422
423     ipxDomain.f_create_from_tstring = netsnmp_ipx_create_tstring;
424     ipxDomain.f_create_from_ostring = netsnmp_ipx_create_ostring;
425
426     netsnmp_tdomain_register(&ipxDomain);
427 }