File: snmpUDPsharedDomain.c

package info (click to toggle)
net-snmp 5.9.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,540 kB
  • sloc: ansic: 282,201; perl: 17,710; sh: 12,006; makefile: 2,712; python: 735; xml: 663; pascal: 62; sql: 47
file content (466 lines) | stat: -rw-r--r-- 13,289 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* UDPshared transport support functions
 *
 * Portions of this file are copyrighted by:
 * Copyright (c) 2016 VMware, Inc. All rights reserved.
 * Use is subject to license terms specified in the COPYING file
 * distributed with the Net-SNMP package.
 */

#include <net-snmp/net-snmp-config.h>

#include <net-snmp/types.h>
#include <net-snmp/library/snmpIPBaseDomain.h>
#include <net-snmp/library/snmpUDPsharedDomain.h>

#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#include <errno.h>

#include <net-snmp/types.h>
#include <net-snmp/library/snmp_debug.h>
#include <net-snmp/library/tools.h>
#include <net-snmp/library/snmp_assert.h>

#include <net-snmp/library/snmpSocketBaseDomain.h>
#include <net-snmp/library/snmpUDPDomain.h>
#include <net-snmp/library/snmpUDPIPv4BaseDomain.h>

netsnmp_feature_require(transport_cache);

oid    netsnmpUDPsharedDomain[] = { 1,3,6,1,2,1,100,1,999 }; /** made up */
size_t netsnmpUDPsharedDomain_len = OID_LENGTH(netsnmpUDPsharedDomain);

/* ***************************************************************************
 */
static int
_udpshared_close(netsnmp_transport *t)
{
    netsnmp_transport *b = t? t->base_transport : NULL;

    DEBUGMSGTL(("udpshared:close", "%p/%p\n", t,b ));

    /** we don't really have a socket - base transport does */
    if (NULL == t || NULL == b)
        return -1;

    t->base_transport = NULL;
    t->sock = -1; /* we just had a copy of base's fd */

    /** remove base transport. if it is still in use, return. */
    if (netsnmp_transport_cache_remove(b) == 1)
        DEBUGMSGTL(("udpshared:close", "keeping socket open for %d user(s)\n",
                    b->local_length));
    else
        b->f_close(b); /** close base transport */

    return 0;
}

static int
_udpshared_recv(netsnmp_transport *t, void *buf, int size,
                void **opaque, int *olength)
{
    if (NULL == t || NULL == t->base_transport)
        return -1;

    return t->base_transport->f_recv(t->base_transport, buf, size, opaque,
                                     olength);
}

static int
_udpshared_send(netsnmp_transport *t, const void *buf, int size,
                void **opaque, int *olength)
{
    void *_opaque, **_opaque_p = &_opaque;
    int  _olength, *_olength_p = &_olength;

    if (NULL == t || NULL == t->base_transport)
        return -1;

    /*
     * opaque points to an address pair to use to send, overriding the
     * address pair in the transport. So if no address was specified by
     * the caller, use the udpshared transport address, overriding the
     * empty base_transport address.
     */
    if (NULL == opaque || NULL == *opaque) {
        _opaque = t->data;
        _olength = t->data_length;
    } else {
        _opaque_p = opaque;
        _olength_p = olength;
    }
    return t->base_transport->f_send(t->base_transport, buf, size, _opaque_p,
                                     _olength_p);
}

static char *
_udpshared_fmtaddr(netsnmp_transport *t, const void *data, int len)
{
    if (NULL == t || NULL == t->base_transport ||
        NULL == t->base_transport->f_fmtaddr)
        return strdup("<UNKNOWN>");

    return t->base_transport->f_fmtaddr(t->base_transport, data, len);
}

static int
_setup_session(netsnmp_transport *t, netsnmp_session *session)
{
    if (NULL == t || NULL == session)
        return -1;

    session->flags |= SNMP_FLAGS_SHARED_SOCKET;

    return 0;
}

static netsnmp_transport *
_transport_common(netsnmp_transport *t)
{
    void *save_data;
    int   save_data_len;

    DEBUGTRACETOK("9:udpshared");

    if (NULL == t)
        return NULL;

    /*
     * t->data contains remote addr, which can/will vary, so don't
     * copy it to base transport
     */
    save_data = t->data;
    save_data_len = t->data_length;
    t->data = NULL;
    t->data_length = 0;

    /** save base transport for clients; need in send/recv functions later */
    t->base_transport = netsnmp_transport_copy(t);
    if (NULL == t->base_transport) {
        free(save_data);
        netsnmp_transport_free(t);
        return NULL;
    }

    /** restore remote addr */
    t->data = save_data;
    t->data_length = save_data_len;

    /** Set UDPsharedDomain specifics */
    t->domain = netsnmpUDPsharedDomain;
    t->domain_length = netsnmpUDPsharedDomain_len;

    t->f_recv          = _udpshared_recv;
    t->f_send          = _udpshared_send;
    t->f_close         = _udpshared_close;
    t->f_fmtaddr       = _udpshared_fmtaddr;
    t->f_setup_session = _setup_session;
    t->flags = NETSNMP_TRANSPORT_FLAG_SHARED;
    if (t->base_transport->domain == netsnmpUDPDomain)
        t->f_get_taddr = netsnmp_ipv4_get_taddr;
    else if (t->base_transport->domain == netsnmp_UDPIPv6Domain)
        t->f_get_taddr = netsnmp_ipv6_get_taddr;
    else
        netsnmp_assert(0);

    return t;
}

netsnmp_transport *
netsnmp_udpshared_transport(const struct netsnmp_ep *ep, int local)
{
    netsnmp_transport *t = NULL;

    t = netsnmp_udp_transport(ep, local);
    if (NULL == t)
        return NULL;

    t = _transport_common(t);

    return t;
}

netsnmp_transport *
netsnmp_udpshared_transport_with_source(const struct netsnmp_ep *ep,
                                        int flags,
                                        const struct netsnmp_ep *src_addr)
{
    netsnmp_transport *t = NULL, *b = NULL;
    int                local = flags & NETSNMP_TSPEC_LOCAL;

    DEBUGMSGTL(("udpshared:create", "from addr with source\n"));

    /** init common parts of parent transport */
    t = netsnmp_udpipv4base_transport_init(ep, local);
    if (NULL == t)
        return NULL;

    if (!_transport_common(t))
        return NULL;

    if (!local && src_addr) {
        /** check for existing base transport */
        b = netsnmp_transport_cache_get(PF_INET, SOCK_DGRAM, local,
                                        (const void *)src_addr);
        if (NULL != b && NULL != b->local) {
            /*
             * uh-oh. we've assumed sharedudp is just for clients, and we're
             * using local_length as a reference count.
             */
            snmp_log(LOG_ERR,
                     "sharedudp transport is only for client/remote\n");
            netsnmp_transport_free(t);
            return NULL;
        }
    }

    /** if no base transport found, create one */
    if (NULL == b) {
        b = netsnmp_udp_transport_with_source(ep, local, src_addr);
        if (NULL == b) {
            netsnmp_transport_free(t);
            return NULL;
        }
    }
    ++b->local_length; /* reference count */
    t->base_transport = b;
    t->msgMaxSize = b->msgMaxSize;
    t->flags |= NETSNMP_TRANSPORT_FLAG_SHARED;

    /** get local socket address */
    if (!local) {
        t->sock = b->sock;
        netsnmp_udpipv4base_transport_get_bound_addr(t);
    }

    /** cache base transport for future use */
    if (!local && src_addr && 1 == b->local_length) {
        netsnmp_transport_cache_save(PF_INET, SOCK_DGRAM, local,
                                     (const void *)src_addr, b);
    }

    return t;
}

#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN

/*
 * Open a shared UDP transport for SNMP.  Local is TRUE if addr is the local
 * address to bind to (i.e. this is a server-type session); otherwise addr is
 * the remote address to send things to.
 */
netsnmp_transport *
netsnmp_udpshared6_transport(const struct netsnmp_ep *ep, int local)
{
    netsnmp_transport *t = NULL;

    t = netsnmp_udp6_transport(ep, local);
    if (NULL != t)
        t = _transport_common(t);

    return t;
}

netsnmp_transport *
netsnmp_udpshared6_transport_with_source(const struct netsnmp_ep *ep,
                                         int flags,
                                         const struct netsnmp_ep *src_addr6)
{
    netsnmp_transport *t = NULL, *b = NULL;
    int                local = flags & NETSNMP_TSPEC_LOCAL;

    DEBUGMSGTL(("udpshared:create", "from addr6 with source\n"));

    /** init common parts of parent transport */
    t = netsnmp_udp6_transport_init(ep, local);
    if (NULL == t)
        return NULL;

    if (!_transport_common(t))
        return NULL;

    if (!local && src_addr6) {
        /** check for existing base transport */
        b = netsnmp_transport_cache_get(PF_INET6, SOCK_DGRAM, local,
                                        (const void *)src_addr6);
        if (NULL != b && NULL != b->local) {
            /*
             * uh-oh. we've assumed sharedudp is just for clients, and we're
             * using local_length as a reference count.
             */
            snmp_log(LOG_ERR,
                     "sharedudp transport is only for client/remote\n");
            netsnmp_transport_free(t);
            return NULL;
        }
    }

    /** if no base transport found, create one */
    if (NULL == b) {
        b = netsnmp_udp6_transport_with_source(ep, local, src_addr6);
        if (NULL == b) {
            netsnmp_transport_free(t);
            return NULL;
        }
    }
    ++b->local_length; /* reference count */
    t->base_transport = b;
    t->flags |= NETSNMP_TRANSPORT_FLAG_SHARED;

    /** get local socket address */
    if (!local) {
        t->sock = b->sock;
        netsnmp_udp6_transport_get_bound_addr(t);
    }

    /** cache base transport for future use */
    if (!local && src_addr6 && 1 == b->local_length) {
        netsnmp_transport_cache_save(PF_INET6, SOCK_DGRAM, local,
                                     (const void *)src_addr6, b);
    }

    return t;
}
#endif /* NETSNMP_TRANSPORT_UDPIPV6_DOMAIN */

netsnmp_transport *
netsnmp_udpshared_create_ostring(const void *o, size_t o_len, int local)
{
    struct netsnmp_ep ep;

    DEBUGMSGTL(("udpshared:create", "from ostring\n"));

    memset(&ep, 0, sizeof(ep));
    if (netsnmp_ipv4_ostring_to_sockaddr(&ep.a.sin, o, o_len))
        return netsnmp_udpshared_transport(&ep, local);
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
    else if (netsnmp_ipv6_ostring_to_sockaddr(&ep.a.sin6, o, o_len))
        return netsnmp_udpshared6_transport(&ep, local);
#endif
    return NULL;
}

netsnmp_transport *
netsnmp_udpshared_create_tstring(const char *str, int isserver,
                                 const char *default_target)
{
    struct netsnmp_ep ep;
    netsnmp_transport *t;

    DEBUGMSGTL(("udpshared:create", "from tstring %s\n", str));

    if (netsnmp_sockaddr_in3(&ep, str, default_target))
        t = netsnmp_udpshared_transport(&ep, isserver);
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
    else if (netsnmp_sockaddr_in6_3(&ep, str, default_target))
        t = netsnmp_udpshared6_transport(&ep, isserver);
#endif
    else
        return NULL;

    return t;
}

static netsnmp_transport *
_tspec_v4(const struct netsnmp_ep *ep, netsnmp_tdomain_spec *tspec)
{
    int local = tspec->flags & NETSNMP_TSPEC_LOCAL;

    if (NULL != tspec->source) {
        struct netsnmp_ep src_addr;

        /** get sockaddr from source */
        if (!netsnmp_sockaddr_in3(&src_addr, tspec->source, NULL))
            return NULL;
        return netsnmp_udpshared_transport_with_source(ep, local, &src_addr);
    }

    /** no source and default client address ok */
    return netsnmp_udpshared_transport(ep, local);
}

#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
static netsnmp_transport *
_tspec_v6(const struct netsnmp_ep *ep, netsnmp_tdomain_spec *tspec)
{
    int local = tspec->flags & NETSNMP_TSPEC_LOCAL;

    if (NULL != tspec->source) {
        struct netsnmp_ep src_addr;

        /** get sockaddr from source */
        if (!netsnmp_sockaddr_in6_3(&src_addr, tspec->source, NULL))
            return NULL;
        return netsnmp_udpshared6_transport_with_source(ep, local, &src_addr);
    }

    /** no source and default client address ok */
    return netsnmp_udpshared6_transport(ep, local);
}
#endif /* NETSNMP_TRANSPORT_UDPIPV6_DOMAIN */

netsnmp_transport *
netsnmp_udpshared_create_tspec(netsnmp_tdomain_spec *tspec)
{
    struct netsnmp_ep ep;

    DEBUGMSGTL(("udpshared:create", "from tspec\n"));

    if (NULL == tspec)
        return NULL;

    if (netsnmp_sockaddr_in3(&ep, tspec->target, tspec->default_target))
        return _tspec_v4(&ep, tspec);
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
    else if (netsnmp_sockaddr_in6_3(&ep, tspec->target, tspec->default_target))
        return _tspec_v6(&ep, tspec);
#endif

    return NULL;
}

void
netsnmp_udpshared_ctor(void)
{
    static netsnmp_tdomain domain;
    static int done = 0;

    if (done)
        return;
    done = 1;

    domain.name = netsnmpUDPsharedDomain;
    domain.name_length = netsnmpUDPsharedDomain_len;

    domain.prefix = (const char**)calloc(2, sizeof(char *));
    domain.prefix[0] = "udpshared";

    domain.f_create_from_tstring_new = netsnmp_udpshared_create_tstring;
    domain.f_create_from_tspec       = netsnmp_udpshared_create_tspec;
    domain.f_create_from_ostring     = netsnmp_udpshared_create_ostring;

    netsnmp_tdomain_register(&domain);
}