Coverage Report

Created: 2026-09-04 09:22

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