Debian Codegraph @

snmplib/transports/snmpUDPsharedDomain.c

raw
1 /* UDPshared transport support functions
2 *
3 * Portions of this file are copyrighted by:
4 * Copyright (c) 2016 VMware, Inc. All rights reserved.
5 * Use is subject to license terms specified in the COPYING file
6 * distributed with the Net-SNMP package.
7 */
8
9 #include <net-snmp/net-snmp-config.h>
10
11 #include <net-snmp/types.h>
12 #include "snmpIPBaseDomain.h"
13 #include <net-snmp/library/snmpUDPsharedDomain.h>
14
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <ctype.h>
19 #ifdef HAVE_STDLIB_H
20 #include <stdlib.h>
21 #endif
22 #ifdef HAVE_STRING_H
23 #include <string.h>
24 #else
25 #include <strings.h>
26 #endif
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36 #ifdef HAVE_NETDB_H
37 #include <netdb.h>
38 #endif
39 #include <errno.h>
40
41 #include <net-snmp/types.h>
42 #include <net-snmp/library/snmp_debug.h>
43 #include <net-snmp/library/tools.h>
44 #include <net-snmp/library/snmp_assert.h>
45
46 #include <net-snmp/library/snmpSocketBaseDomain.h>
47 #include <net-snmp/library/snmpUDPDomain.h>
48 #include <net-snmp/library/snmpUDPIPv4BaseDomain.h>
49
50 netsnmp_feature_require(transport_cache);
51
52 oid netsnmpUDPsharedDomain[] = { 1,3,6,1,2,1,100,1,999 }; /** made up */
53 size_t netsnmpUDPsharedDomain_len = OID_LENGTH(netsnmpUDPsharedDomain);
54
55 /* ***************************************************************************
56 */
57 static int
58 _udpshared_close(netsnmp_transport *t)
59 {
60 netsnmp_transport *b = t? t->base_transport : NULL;
61
62 DEBUGMSGTL(("udpshared:close", "%p/%p\n", t,b ));
63
64 /** we don't really have a socket - base transport does */
65 if (NULL == t || NULL == b)
66 return -1;
67
68 t->base_transport = NULL;
69 t->sock = -1; /* we just had a copy of base's fd */
70
71 /** remove base transport. if it is still in use, return. */
72 if (netsnmp_transport_cache_remove(b) == 1)
73 DEBUGMSGTL(("udpshared:close", "keeping socket open for %d user(s)\n",
74 b->local_length));
75 else
76 b->f_close(b); /** close base transport */
77
78 return 0;
79 }
80
81 static int
82 _udpshared_recv(netsnmp_transport *t, void *buf, int size,
83 void **opaque, int *olength)
84 {
85 if (NULL == t || NULL == t->base_transport)
86 return -1;
87
88 return t->base_transport->f_recv(t->base_transport, buf, size, opaque,
89 olength);
90 }
91
92 static int
93 _udpshared_send(netsnmp_transport *t, const void *buf, int size,
94 void **opaque, int *olength)
95 {
96 void *_opaque, **_opaque_p = &_opaque;
97 int _olength, *_olength_p = &_olength;
98
99 if (NULL == t || NULL == t->base_transport)
100 return -1;
101
102 /*
103 * opaque points to an address pair to use to send, overriding the
104 * address pair in the transport. So if no address was specified by
105 * the caller, use the udpshared transport address, overriding the
106 * empty base_transport address.
107 */
108 if (NULL == opaque || NULL == *opaque) {
109 _opaque = t->data;
110 _olength = t->data_length;
111 } else {
112 _opaque_p = opaque;
113 _olength_p = olength;
114 }
115 return t->base_transport->f_send(t->base_transport, buf, size, _opaque_p,
116 _olength_p);
117 }
118
119 static char *
120 _udpshared_fmtaddr(netsnmp_transport *t, const void *data, int len)
121 {
122 if (NULL == t || NULL == t->base_transport ||
123 NULL == t->base_transport->f_fmtaddr)
124 return strdup("<UNKNOWN>");
125
126 return t->base_transport->f_fmtaddr(t->base_transport, data, len);
127 }
128
129 static int
130 _setup_session(netsnmp_transport *t, netsnmp_session *session)
131 {
132 if (NULL == t || NULL == session)
133 return -1;
134
135 session->flags |= SNMP_FLAGS_SHARED_SOCKET;
136
137 return 0;
138 }
139
140 static netsnmp_transport *
141 _transport_common(netsnmp_transport *t)
142 {
143 void *save_data;
144 int save_data_len;
145
146 DEBUGTRACETOK("9:udpshared");
147
148 if (NULL == t)
149 return NULL;
150
151 /*
152 * t->data contains remote addr, which can/will vary, so don't
153 * copy it to base transport
154 */
155 save_data = t->data;
156 save_data_len = t->data_length;
157 t->data = NULL;
158 t->data_length = 0;
159
160 /** save base transport for clients; need in send/recv functions later */
161 t->base_transport = netsnmp_transport_copy(t);
162 if (NULL == t->base_transport) {
163 free(save_data);
164 netsnmp_transport_free(t);
165 return NULL;
166 }
167
168 /** restore remote addr */
169 t->data = save_data;
170 t->data_length = save_data_len;
171
172 /** Set UDPsharedDomain specifics */
173 t->domain = netsnmpUDPsharedDomain;
174 t->domain_length = netsnmpUDPsharedDomain_len;
175
176 t->f_recv = _udpshared_recv;
177 t->f_send = _udpshared_send;
178 t->f_close = _udpshared_close;
179 t->f_fmtaddr = _udpshared_fmtaddr;
180 t->f_setup_session = _setup_session;
181 t->flags = NETSNMP_TRANSPORT_FLAG_SHARED;
182 if (t->base_transport->domain == netsnmpUDPDomain)
183 t->f_get_taddr = netsnmp_ipv4_get_taddr;
184 #ifdef NETSNMP_ENABLE_IPV6
185 else if (t->base_transport->domain == netsnmp_UDPIPv6Domain)
186 t->f_get_taddr = netsnmp_ipv6_get_taddr;
187 #endif
188 else
189 netsnmp_assert(0);
190
191 return t;
192 }
193
194 netsnmp_transport *
195 netsnmp_udpshared_transport(const struct netsnmp_ep *ep, int local)
196 {
197 netsnmp_transport *t = NULL;
198
199 t = netsnmp_udp_transport(ep, local);
200 if (NULL == t)
201 return NULL;
202
203 t = _transport_common(t);
204
205 return t;
206 }
207
208 netsnmp_transport *
209 netsnmp_udpshared_transport_with_source(const struct netsnmp_ep *ep,
210 int flags,
211 const struct netsnmp_ep *src_addr)
212 {
213 netsnmp_transport *t = NULL, *b = NULL;
214 int local = flags & NETSNMP_TSPEC_LOCAL;
215
216 DEBUGMSGTL(("udpshared:create", "from addr with source\n"));
217
218 /** init common parts of parent transport */
219 t = netsnmp_udpipv4base_transport_init(ep, local);
220 if (NULL == t)
221 return NULL;
222
223 if (!_transport_common(t))
224 return NULL;
225
226 if (!local && src_addr) {
227 /** check for existing base transport */
228 b = netsnmp_transport_cache_get(PF_INET, SOCK_DGRAM, local,
229 (const void *)src_addr);
230 if (NULL != b && NULL != b->local) {
231 /*
232 * uh-oh. we've assumed sharedudp is just for clients, and we're
233 * using local_length as a reference count.
234 */
235 snmp_log(LOG_ERR,
236 "sharedudp transport is only for client/remote\n");
237 netsnmp_transport_free(t);
238 return NULL;
239 }
240 }
241
242 /** if no base transport found, create one */
243 if (NULL == b) {
244 b = netsnmp_udp_transport_with_source(ep, local, src_addr);
245 if (NULL == b) {
246 netsnmp_transport_free(t);
247 return NULL;
248 }
249 }
250 ++b->local_length; /* reference count */
251 t->base_transport = b;
252 t->msgMaxSize = b->msgMaxSize;
253 t->flags |= NETSNMP_TRANSPORT_FLAG_SHARED;
254
255 /** get local socket address */
256 if (!local) {
257 t->sock = b->sock;
258 netsnmp_udpipv4base_transport_get_bound_addr(t);
259 }
260
261 /** cache base transport for future use */
262 if (!local && src_addr && 1 == b->local_length) {
263 netsnmp_transport_cache_save(PF_INET, SOCK_DGRAM, local,
264 (const void *)src_addr, b);
265 }
266
267 return t;
268 }
269
270 #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
271
272 /*
273 * Open a shared UDP transport for SNMP. Local is TRUE if addr is the local
274 * address to bind to (i.e. this is a server-type session); otherwise addr is
275 * the remote address to send things to.
276 */
277 netsnmp_transport *
278 netsnmp_udpshared6_transport(const struct netsnmp_ep *ep, int local)
279 {
280 netsnmp_transport *t = NULL;
281
282 t = netsnmp_udp6_transport(ep, local);
283 if (NULL != t)
284 t = _transport_common(t);
285
286 return t;
287 }
288
289 netsnmp_transport *
290 netsnmp_udpshared6_transport_with_source(const struct netsnmp_ep *ep,
291 int flags,
292 const struct netsnmp_ep *src_addr6)
293 {
294 netsnmp_transport *t = NULL, *b = NULL;
295 int local = flags & NETSNMP_TSPEC_LOCAL;
296
297 DEBUGMSGTL(("udpshared:create", "from addr6 with source\n"));
298
299 /** init common parts of parent transport */
300 t = netsnmp_udp6_transport_init(ep, local);
301 if (NULL == t)
302 return NULL;
303
304 if (!_transport_common(t))
305 return NULL;
306
307 if (!local && src_addr6) {
308 /** check for existing base transport */
309 b = netsnmp_transport_cache_get(PF_INET6, SOCK_DGRAM, local,
310 (const void *)src_addr6);
311 if (NULL != b && NULL != b->local) {
312 /*
313 * uh-oh. we've assumed sharedudp is just for clients, and we're
314 * using local_length as a reference count.
315 */
316 snmp_log(LOG_ERR,
317 "sharedudp transport is only for client/remote\n");
318 netsnmp_transport_free(t);
319 return NULL;
320 }
321 }
322
323 /** if no base transport found, create one */
324 if (NULL == b) {
325 b = netsnmp_udp6_transport_with_source(ep, local, src_addr6);
326 if (NULL == b) {
327 netsnmp_transport_free(t);
328 return NULL;
329 }
330 }
331 ++b->local_length; /* reference count */
332 t->base_transport = b;
333 t->flags |= NETSNMP_TRANSPORT_FLAG_SHARED;
334
335 /** get local socket address */
336 if (!local) {
337 t->sock = b->sock;
338 netsnmp_udp6_transport_get_bound_addr(t);
339 }
340
341 /** cache base transport for future use */
342 if (!local && src_addr6 && 1 == b->local_length) {
343 netsnmp_transport_cache_save(PF_INET6, SOCK_DGRAM, local,
344 (const void *)src_addr6, b);
345 }
346
347 return t;
348 }
349 #endif /* NETSNMP_TRANSPORT_UDPIPV6_DOMAIN */
350
351 netsnmp_transport *
352 netsnmp_udpshared_create_ostring(const void *o, size_t o_len, int local)
353 {
354 struct netsnmp_ep ep;
355
356 DEBUGMSGTL(("udpshared:create", "from ostring\n"));
357
358 memset(&ep, 0, sizeof(ep));
359 if (netsnmp_ipv4_ostring_to_sockaddr(&ep.a.sin, o, o_len))
360 return netsnmp_udpshared_transport(&ep, local);
361 #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
362 else if (netsnmp_ipv6_ostring_to_sockaddr(&ep.a.sin6, o, o_len))
363 return netsnmp_udpshared6_transport(&ep, local);
364 #endif
365 return NULL;
366 }
367
368 netsnmp_transport *
369 netsnmp_udpshared_create_tstring(const char *str, int isserver,
370 const char *default_target)
371 {
372 struct netsnmp_ep ep;
373 netsnmp_transport *t;
374
375 DEBUGMSGTL(("udpshared:create", "from tstring %s\n", str));
376
377 if (netsnmp_sockaddr_in3(&ep, str, default_target))
378 t = netsnmp_udpshared_transport(&ep, isserver);
379 #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
380 else if (netsnmp_sockaddr_in6_3(&ep, str, default_target))
381 t = netsnmp_udpshared6_transport(&ep, isserver);
382 #endif
383 else
384 return NULL;
385
386 return t;
387 }
388
389 static netsnmp_transport *
390 _tspec_v4(const struct netsnmp_ep *ep, netsnmp_tdomain_spec *tspec)
391 {
392 int local = tspec->flags & NETSNMP_TSPEC_LOCAL;
393
394 if (NULL != tspec->source) {
395 struct netsnmp_ep src_addr;
396
397 /** get sockaddr from source */
398 if (!netsnmp_sockaddr_in3(&src_addr, tspec->source, NULL))
399 return NULL;
400 return netsnmp_udpshared_transport_with_source(ep, local, &src_addr);
401 }
402
403 /** no source and default client address ok */
404 return netsnmp_udpshared_transport(ep, local);
405 }
406
407 #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
408 static netsnmp_transport *
409 _tspec_v6(const struct netsnmp_ep *ep, netsnmp_tdomain_spec *tspec)
410 {
411 int local = tspec->flags & NETSNMP_TSPEC_LOCAL;
412
413 if (NULL != tspec->source) {
414 struct netsnmp_ep src_addr;
415
416 /** get sockaddr from source */
417 if (!netsnmp_sockaddr_in6_3(&src_addr, tspec->source, NULL))
418 return NULL;
419 return netsnmp_udpshared6_transport_with_source(ep, local, &src_addr);
420 }
421
422 /** no source and default client address ok */
423 return netsnmp_udpshared6_transport(ep, local);
424 }
425 #endif /* NETSNMP_TRANSPORT_UDPIPV6_DOMAIN */
426
427 netsnmp_transport *
428 netsnmp_udpshared_create_tspec(netsnmp_tdomain_spec *tspec)
429 {
430 struct netsnmp_ep ep;
431
432 DEBUGMSGTL(("udpshared:create", "from tspec\n"));
433
434 if (NULL == tspec)
435 return NULL;
436
437 if (netsnmp_sockaddr_in3(&ep, tspec->target, tspec->default_target))
438 return _tspec_v4(&ep, tspec);
439 #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
440 else if (netsnmp_sockaddr_in6_3(&ep, tspec->target, tspec->default_target))
441 return _tspec_v6(&ep, tspec);
442 #endif
443
444 return NULL;
445 }
446
447 void
448 netsnmp_udpshared_ctor(void)
449 {
450 static netsnmp_tdomain domain;
451 static int done = 0;
452
453 if (done)
454 return;
455 done = 1;
456
457 domain.name = netsnmpUDPsharedDomain;
458 domain.name_length = netsnmpUDPsharedDomain_len;
459
460 domain.prefix = (const char**)calloc(2, sizeof(char *));
461 domain.prefix[0] = "udpshared";
462
463 domain.f_create_from_tstring_new = netsnmp_udpshared_create_tstring;
464 domain.f_create_from_tspec = netsnmp_udpshared_create_tspec;
465 domain.f_create_from_ostring = netsnmp_udpshared_create_ostring;
466
467 netsnmp_tdomain_register(&domain);
468 }