1 #ifdef BRCM_SNMP_NOT_USED
2 #include <net-snmp/net-snmp-config.h>
21 #include <sys/socket.h>
31 #include <net-snmp/types.h>
32 #include <net-snmp/output_api.h>
33 #include <net-snmp/config_api.h>
35 #include <net-snmp/library/snmp_transport.h>
36 #include <net-snmp/library/snmpUnixDomain.h>
39 #ifndef NETSNMP_STREAM_QUEUE_LEN
40 #define NETSNMP_STREAM_QUEUE_LEN 5
45 * Evaluate to actual length of the `sockaddr_un' structure.
47 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
48 + strlen ((ptr)->sun_path))
51 oid netsnmp_UnixDomain[10] = { ENTERPRISE_MIB, 3, 3, 2 };
52 static netsnmp_tdomain unixDomain;
56 * This is the structure we use to hold transport-specific data.
59 typedef struct _sockaddr_un_pair {
61 struct sockaddr_un server;
62 struct sockaddr_un client;
67 * Return a string representing the address in data, or else the "far end"
68 * address if data is NULL.
72 netsnmp_unix_fmtaddr(netsnmp_transport *t, void *data, int len)
74 struct sockaddr_un *to = NULL;
77 to = (struct sockaddr_un *) data;
78 } else if (t != NULL && t->data != NULL) {
79 to = &(((sockaddr_un_pair *) t->data)->server);
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.
88 return strdup("Local IPC: unknown");
89 } else if (to->sun_path[0] == 0) {
91 * This is an abstract name. We could render it as hex or something
92 * but let's not worry about that for now.
94 return strdup("Local IPC: abstract");
96 char *tmp = (char *) malloc(16 + len);
98 sprintf(tmp, "Local IPC: %s", to->sun_path);
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...
113 netsnmp_unix_recv(netsnmp_transport *t, void *buf, int size,
114 void **opaque, int *olength)
120 if (t != NULL && t->sock >= 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)));
129 DEBUGMSGTL(("netsnmp_unix", "recv fd %d got %d bytes\n", t->sock, rc));
137 netsnmp_unix_send(netsnmp_transport *t, void *buf, int size,
138 void **opaque, int *olength)
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));
146 rc = send(t->sock, buf, size, 0);
147 if (rc < 0 && errno != EINTR) {
158 netsnmp_unix_close(netsnmp_transport *t)
161 sockaddr_un_pair *sup = (sockaddr_un_pair *) t->data;
164 #ifndef HAVE_CLOSESOCKET
167 rc = closesocket(t->sock);
172 DEBUGMSGTL(("netsnmp_unix", "close: server unlink(\"%s\")\n",
173 sup->server.sun_path));
174 unlink(sup->server.sun_path);
176 DEBUGMSGTL(("netsnmp_unix", "close: client unlink(\"%s\")\n",
177 sup->client.sun_path));
178 unlink(sup->client.sun_path);
190 netsnmp_unix_accept(netsnmp_transport *t)
192 struct sockaddr *farend = NULL;
194 socklen_t farendlen = sizeof(struct sockaddr_un);
196 farend = (struct sockaddr *) malloc(farendlen);
198 if (farend == NULL) {
200 * Indicate that the acceptance of this socket failed.
202 DEBUGMSGTL(("netsnmp_unix", "accept: malloc failed\n"));
205 memset(farend, 0, farendlen);
207 if (t != NULL && t->sock >= 0) {
208 newsock = accept(t->sock, farend, &farendlen);
211 DEBUGMSGTL(("netsnmp_unix","accept failed rc %d errno %d \"%s\"\n",
212 newsock, errno, strerror(errno)));
217 if (t->data != NULL) {
221 DEBUGMSGTL(("netsnmp_unix", "accept succeeded (farend %p len %d)\n",
224 t->data_length = sizeof(struct sockaddr_un);
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).
242 netsnmp_unix_transport(struct sockaddr_un *addr, int local)
244 netsnmp_transport *t = NULL;
245 sockaddr_un_pair *sup = NULL;
249 if (addr == NULL || addr->sun_family != AF_UNIX) {
253 t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
258 string = netsnmp_unix_fmtaddr(NULL, (void *)addr,
259 sizeof(struct sockaddr_un));
260 DEBUGMSGTL(("netsnmp_unix", "open %s %s\n", local ? "local" : "remote",
264 memset(t, 0, sizeof(netsnmp_transport));
266 t->domain = netsnmp_UnixDomain;
268 sizeof(netsnmp_UnixDomain) / sizeof(netsnmp_UnixDomain[0]);
270 t->data = malloc(sizeof(sockaddr_un_pair));
271 if (t->data == NULL) {
272 netsnmp_transport_free(t);
275 memset(t->data, 0, sizeof(sockaddr_un_pair));
276 t->data_length = sizeof(sockaddr_un_pair);
277 sup = (sockaddr_un_pair *) t->data;
279 t->sock = socket(PF_UNIX, SOCK_STREAM, 0);
281 netsnmp_transport_free(t);
285 t->flags = NETSNMP_TRANSPORT_FLAG_STREAM;
288 t->local = malloc(strlen(addr->sun_path));
289 if (t->local == NULL) {
290 netsnmp_transport_free(t);
293 memcpy(t->local, addr->sun_path, strlen(addr->sun_path));
294 t->local_length = strlen(addr->sun_path);
297 * This session is inteneded as a server, so we must bind to the given
298 * path (unlinking it first, to avoid errors).
301 t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
303 unlink(addr->sun_path);
304 rc = bind(t->sock, (struct sockaddr *) addr, SUN_LEN(addr));
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);
315 * Save the address in the transport-specific data pointer for later
316 * use by netsnmp_unix_close.
319 sup->server.sun_family = AF_UNIX;
320 strcpy(sup->server.sun_path, addr->sun_path);
324 * Now sit here and listen for connections to arrive.
327 rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
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);
337 t->remote = malloc(strlen(addr->sun_path));
338 if (t->remote == NULL) {
339 netsnmp_transport_free(t);
342 memcpy(t->remote, addr->sun_path, strlen(addr->sun_path));
343 t->remote_length = strlen(addr->sun_path);
345 rc = connect(t->sock, (struct sockaddr *) addr,
346 sizeof(struct sockaddr_un));
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);
357 * Save the remote address in the transport-specific data pointer for
358 * later use by netsnmp_unix_send.
361 sup->server.sun_family = AF_UNIX;
362 strcpy(sup->server.sun_path, addr->sun_path);
367 * Message size is not limited by this transport (hence msgMaxSize
368 * is equal to the maximum legal size of an SNMP message).
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;
382 netsnmp_unix_create_tstring(const char *string, int local)
384 struct sockaddr_un addr;
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);
392 if (string != NULL) {
393 snmp_log(LOG_ERR, "Path too long for Unix domain transport\n");
402 netsnmp_unix_create_ostring(const u_char * o, size_t o_len, int local)
404 struct sockaddr_un addr;
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);
413 snmp_log(LOG_ERR, "Path too long for Unix domain transport\n");
422 netsnmp_unix_ctor(void)
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";
429 unixDomain.f_create_from_tstring = netsnmp_unix_create_tstring;
430 unixDomain.f_create_from_ostring = netsnmp_unix_create_ostring;
432 netsnmp_tdomain_register(&unixDomain);
434 #endif /* #if BRCM_SNMP_NOT_USED */