# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / snmpUnixDomain.c
1 #ifdef BRCM_SNMP_NOT_USED
2 #include <net-snmp/net-snmp-config.h>
3
4 #include <stdio.h>
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <errno.h>
8
9 #if HAVE_STRING_H
10 #include <string.h>
11 #else
12 #include <strings.h>
13 #endif
14 #if HAVE_STDLIB_H
15 #include <stdlib.h>
16 #endif
17 #if HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #if HAVE_SYS_SOCKET_H
21 #include <sys/socket.h>
22 #endif
23 #if HAVE_SYS_UN_H
24 #include <sys/un.h>
25 #endif
26
27 #if HAVE_DMALLOC_H
28 #include <dmalloc.h>
29 #endif
30
31 #include <net-snmp/types.h>
32 #include <net-snmp/output_api.h>
33 #include <net-snmp/config_api.h>
34
35 #include <net-snmp/library/snmp_transport.h>
36 #include <net-snmp/library/snmpUnixDomain.h>
37
38
39 #ifndef NETSNMP_STREAM_QUEUE_LEN
40 #define NETSNMP_STREAM_QUEUE_LEN  5
41 #endif
42
43 #ifndef SUN_LEN
44 /*
45  * Evaluate to actual length of the `sockaddr_un' structure.  
46  */
47 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path)         \
48                       + strlen ((ptr)->sun_path))
49 #endif
50
51 oid netsnmp_UnixDomain[10] = { ENTERPRISE_MIB, 3, 3, 2 };
52 static netsnmp_tdomain unixDomain;
53
54
55 /*
56  * This is the structure we use to hold transport-specific data.  
57  */
58
59 typedef struct _sockaddr_un_pair {
60     int             local;
61     struct sockaddr_un server;
62     struct sockaddr_un client;
63 } sockaddr_un_pair;
64
65
66 /*
67  * Return a string representing the address in data, or else the "far end"
68  * address if data is NULL.  
69  */
70
71 static char *
72 netsnmp_unix_fmtaddr(netsnmp_transport *t, void *data, int len)
73 {
74     struct sockaddr_un *to = NULL;
75
76     if (data != NULL) {
77         to = (struct sockaddr_un *) data;
78     } else if (t != NULL && t->data != NULL) {
79         to = &(((sockaddr_un_pair *) t->data)->server);
80         len = SUN_LEN(to);
81     }
82     if (to == NULL) {
83         /*
84          * "Local IPC" is the Posix.1g term for Unix domain protocols,
85          * according to W. R. Stevens, ``Unix Network Programming Volume I
86          * Second Edition'', p. 374.
87          */
88         return strdup("Local IPC: unknown");
89     } else if (to->sun_path[0] == 0) {
90         /*
91          * This is an abstract name.  We could render it as hex or something
92          * but let's not worry about that for now.
93          */
94         return strdup("Local IPC: abstract");
95     } else {
96         char           *tmp = (char *) malloc(16 + len);
97         if (tmp != NULL) {
98             sprintf(tmp, "Local IPC: %s", to->sun_path);
99         }
100         return tmp;
101     }
102 }
103
104
105
106 /*
107  * You can write something into opaque that will subsequently get passed back 
108  * to your send function if you like.  For instance, you might want to
109  * remember where a PDU came from, so that you can send a reply there...  
110  */
111
112 static int
113 netsnmp_unix_recv(netsnmp_transport *t, void *buf, int size,
114                   void **opaque, int *olength)
115 {
116     int rc = -1;
117
118     *opaque = NULL;
119     *olength = 0;
120     if (t != NULL && t->sock >= 0) {
121         while (rc < 0) {
122             rc = recv(t->sock, buf, size, 0);
123             if (rc < 0 && errno != EINTR) {
124                 DEBUGMSGTL(("netsnmp_unix", "recv fd %d err %d (\"%s\")\n",
125                             t->sock, errno, strerror(errno)));
126                 return rc;
127             }
128         }
129         DEBUGMSGTL(("netsnmp_unix", "recv fd %d got %d bytes\n", t->sock, rc));
130     }
131     return rc;
132 }
133
134
135
136 static int
137 netsnmp_unix_send(netsnmp_transport *t, void *buf, int size,
138                   void **opaque, int *olength)
139 {
140     int rc = -1;
141
142     if (t != NULL && t->sock >= 0) {
143         DEBUGMSGTL(("netsnmp_unix", "send %d bytes from %p on fd %d\n",
144                     size, buf, t->sock));
145         while (rc < 0) {
146             rc = send(t->sock, buf, size, 0);
147             if (rc < 0 && errno != EINTR) {
148                 break;
149             }
150         }
151     }
152     return rc;
153 }
154
155
156
157 static int
158 netsnmp_unix_close(netsnmp_transport *t)
159 {
160     int rc = 0;
161     sockaddr_un_pair *sup = (sockaddr_un_pair *) t->data;
162
163     if (t->sock >= 0) {
164 #ifndef HAVE_CLOSESOCKET
165         rc = close(t->sock);
166 #else
167         rc = closesocket(t->sock);
168 #endif
169         t->sock = -1;
170         if (sup != NULL) {
171             if (sup->local) {
172                 DEBUGMSGTL(("netsnmp_unix", "close: server unlink(\"%s\")\n",
173                             sup->server.sun_path));
174                 unlink(sup->server.sun_path);
175             } else {
176                 DEBUGMSGTL(("netsnmp_unix", "close: client unlink(\"%s\")\n",
177                             sup->client.sun_path));
178                 unlink(sup->client.sun_path);
179             }
180         }
181         return rc;
182     } else {
183         return -1;
184     }
185 }
186
187
188
189 static int
190 netsnmp_unix_accept(netsnmp_transport *t)
191 {
192     struct sockaddr *farend = NULL;
193     int             newsock = -1;
194     socklen_t       farendlen = sizeof(struct sockaddr_un);
195
196     farend = (struct sockaddr *) malloc(farendlen);
197
198     if (farend == NULL) {
199         /*
200          * Indicate that the acceptance of this socket failed.  
201          */
202         DEBUGMSGTL(("netsnmp_unix", "accept: malloc failed\n"));
203         return -1;
204     }
205     memset(farend, 0, farendlen);
206
207     if (t != NULL && t->sock >= 0) {
208         newsock = accept(t->sock, farend, &farendlen);
209
210         if (newsock < 0) {
211             DEBUGMSGTL(("netsnmp_unix","accept failed rc %d errno %d \"%s\"\n",
212                         newsock, errno, strerror(errno)));
213             free(farend);
214             return newsock;
215         }
216
217         if (t->data != NULL) {
218             free(t->data);
219         }
220
221         DEBUGMSGTL(("netsnmp_unix", "accept succeeded (farend %p len %d)\n",
222                     farend, farendlen));
223         t->data = farend;
224         t->data_length = sizeof(struct sockaddr_un);
225         return newsock;
226     } else {
227         free(farend);
228         return -1;
229     }
230 }
231
232
233
234 /*
235  * Open a Unix-domain transport for SNMP.  Local is TRUE if addr is the local 
236  * address to bind to (i.e. this is a server-type session); otherwise addr is 
237  * the remote address to send things to (and we make up a temporary name for
238  * the local end of the connection).  
239  */
240
241 netsnmp_transport *
242 netsnmp_unix_transport(struct sockaddr_un *addr, int local)
243 {
244     netsnmp_transport *t = NULL;
245     sockaddr_un_pair *sup = NULL;
246     int             rc = 0;
247     char           *string = NULL;
248
249     if (addr == NULL || addr->sun_family != AF_UNIX) {
250         return NULL;
251     }
252
253     t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
254     if (t == NULL) {
255         return NULL;
256     }
257
258     string = netsnmp_unix_fmtaddr(NULL, (void *)addr, 
259                                   sizeof(struct sockaddr_un));
260     DEBUGMSGTL(("netsnmp_unix", "open %s %s\n", local ? "local" : "remote",
261                 string));
262     free(string);
263
264     memset(t, 0, sizeof(netsnmp_transport));
265
266     t->domain = netsnmp_UnixDomain;
267     t->domain_length =
268         sizeof(netsnmp_UnixDomain) / sizeof(netsnmp_UnixDomain[0]);
269
270     t->data = malloc(sizeof(sockaddr_un_pair));
271     if (t->data == NULL) {
272         netsnmp_transport_free(t);
273         return NULL;
274     }
275     memset(t->data, 0, sizeof(sockaddr_un_pair));
276     t->data_length = sizeof(sockaddr_un_pair);
277     sup = (sockaddr_un_pair *) t->data;
278
279     t->sock = socket(PF_UNIX, SOCK_STREAM, 0);
280     if (t->sock < 0) {
281         netsnmp_transport_free(t);
282         return NULL;
283     }
284
285     t->flags = NETSNMP_TRANSPORT_FLAG_STREAM;
286
287     if (local) {
288         t->local = malloc(strlen(addr->sun_path));
289         if (t->local == NULL) {
290             netsnmp_transport_free(t);
291             return NULL;
292         }
293         memcpy(t->local, addr->sun_path, strlen(addr->sun_path));
294         t->local_length = strlen(addr->sun_path);
295
296         /*
297          * This session is inteneded as a server, so we must bind to the given
298          * path (unlinking it first, to avoid errors).  
299          */
300
301         t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
302
303         unlink(addr->sun_path);
304         rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
305         if (rc != 0) {
306             DEBUGMSGTL(("netsnmp_unix_transport",
307                         "couldn't bind \"%s\", errno %d (%s)\n",
308                         addr->sun_path, errno, strerror(errno)));
309             netsnmp_unix_close(t);
310             netsnmp_transport_free(t);
311             return NULL;
312         }
313
314         /*
315          * Save the address in the transport-specific data pointer for later
316          * use by netsnmp_unix_close.
317          */
318
319         sup->server.sun_family = AF_UNIX;
320         strcpy(sup->server.sun_path, addr->sun_path);
321         sup->local = 1;
322
323         /*
324          * Now sit here and listen for connections to arrive.  
325          */
326
327         rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
328         if (rc != 0) {
329             DEBUGMSGTL(("netsnmp_unix_transport",
330                         "couldn't listen to \"%s\", errno %d (%s)\n",
331                         addr->sun_path, errno, strerror(errno)));
332             netsnmp_unix_close(t);
333             netsnmp_transport_free(t);
334         }
335
336     } else {
337         t->remote = malloc(strlen(addr->sun_path));
338         if (t->remote == NULL) {
339             netsnmp_transport_free(t);
340             return NULL;
341         }
342         memcpy(t->remote, addr->sun_path, strlen(addr->sun_path));
343         t->remote_length = strlen(addr->sun_path);
344
345         rc = connect(t->sock, (struct sockaddr *) addr,
346                      sizeof(struct sockaddr_un));
347         if (rc != 0) {
348             DEBUGMSGTL(("netsnmp_unix_transport",
349                         "couldn't connect to \"%s\", errno %d (%s)\n",
350                         addr->sun_path, errno, strerror(errno)));
351             netsnmp_unix_close(t);
352             netsnmp_transport_free(t);
353             return NULL;
354         }
355
356         /*
357          * Save the remote address in the transport-specific data pointer for
358          * later use by netsnmp_unix_send.  
359          */
360
361         sup->server.sun_family = AF_UNIX;
362         strcpy(sup->server.sun_path, addr->sun_path);
363         sup->local = 0;
364     }
365
366     /*
367      * Message size is not limited by this transport (hence msgMaxSize
368      * is equal to the maximum legal size of an SNMP message).  
369      */
370
371     t->msgMaxSize = 0x7fffffff;
372     t->f_recv     = netsnmp_unix_recv;
373     t->f_send     = netsnmp_unix_send;
374     t->f_close    = netsnmp_unix_close;
375     t->f_accept   = netsnmp_unix_accept;
376     t->f_fmtaddr  = netsnmp_unix_fmtaddr;
377
378     return t;
379 }
380
381 netsnmp_transport *
382 netsnmp_unix_create_tstring(const char *string, int local)
383 {
384     struct sockaddr_un addr;
385
386     if ((string != NULL) && (strlen(string) < sizeof(addr.sun_path))) {
387         addr.sun_family = AF_UNIX;
388         memset(addr.sun_path, 0, sizeof(addr.sun_path));
389         strncpy(addr.sun_path, string, sizeof(addr.sun_path) - 1);
390         return netsnmp_unix_transport(&addr, local);
391     } else {
392         if (string != NULL) {
393             snmp_log(LOG_ERR, "Path too long for Unix domain transport\n");
394         }
395         return NULL;
396     }
397 }
398
399
400
401 netsnmp_transport *
402 netsnmp_unix_create_ostring(const u_char * o, size_t o_len, int local)
403 {
404     struct sockaddr_un addr;
405
406     if (o_len > 0 && o_len < (sizeof(addr.sun_path) - 1)) {
407         addr.sun_family = AF_UNIX;
408         memset(addr.sun_path, 0, sizeof(addr.sun_path));
409         strncpy(addr.sun_path, o, o_len);
410         return netsnmp_unix_transport(&addr, local);
411     } else {
412         if (o_len > 0) {
413             snmp_log(LOG_ERR, "Path too long for Unix domain transport\n");
414         }
415     }
416     return NULL;
417 }
418
419
420
421 void
422 netsnmp_unix_ctor(void)
423 {
424     unixDomain.name = netsnmp_UnixDomain;
425     unixDomain.name_length = sizeof(netsnmp_UnixDomain) / sizeof(oid);
426     unixDomain.prefix = calloc(2, sizeof(char *));
427     unixDomain.prefix[0] = "unix";
428
429     unixDomain.f_create_from_tstring = netsnmp_unix_create_tstring;
430     unixDomain.f_create_from_ostring = netsnmp_unix_create_ostring;
431
432     netsnmp_tdomain_register(&unixDomain);
433 }
434 #endif /* #if BRCM_SNMP_NOT_USED */