Coverage Report

Created: 2026-12-01 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/snmplib/transports/snmpTCPIPv6Domain.c
Line
Count
Source
1
#include <net-snmp/net-snmp-config.h>
2
3
#ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN
4
5
#include <net-snmp/types.h>
6
#include "snmpIPBaseDomain.h"
7
#include <net-snmp/library/snmpTCPIPv6Domain.h>
8
9
#include <stdio.h>
10
#include <sys/types.h>
11
#include <errno.h>
12
13
#ifdef HAVE_STRING_H
14
#include <string.h>
15
#else
16
#include <strings.h>
17
#endif
18
#ifdef HAVE_STDLIB_H
19
#include <stdlib.h>
20
#endif
21
#ifdef HAVE_UNISTD_H
22
#include <unistd.h>
23
#endif
24
#ifdef HAVE_SYS_SOCKET_H
25
#include <sys/socket.h>
26
#endif
27
#ifdef HAVE_NETINET_IN_H
28
#include <netinet/in.h>
29
#endif
30
#ifdef HAVE_ARPA_INET_H
31
#include <arpa/inet.h>
32
#endif
33
#ifdef HAVE_NETDB_H
34
#include <netdb.h>
35
#endif
36
#ifdef HAVE_FCNTL_H
37
#include <fcntl.h>
38
#endif
39
40
#include <net-snmp/types.h>
41
#include <net-snmp/output_api.h>
42
#include <net-snmp/config_api.h>
43
44
#include <net-snmp/library/snmp.h>
45
#include <net-snmp/library/snmp_transport.h>
46
#include <net-snmp/library/snmpSocketBaseDomain.h>
47
#include <net-snmp/library/snmpTCPBaseDomain.h>
48
#include <net-snmp/library/tools.h>
49
50
#ifndef NETSNMP_NO_SYSTEMD
51
#include <net-snmp/library/sd-daemon.h>
52
#endif
53
54
#include "inet_ntop.h"
55
56
/*
57
 * needs to be in sync with the definitions in snmplib/snmpTCPDomain.c
58
 * and perl/agent/agent.xs
59
 */
60
typedef netsnmp_indexed_addr_pair netsnmp_udp_addr_pair;
61
62
const oid netsnmp_TCPIPv6Domain[] = { TRANSPORT_DOMAIN_TCP_IPV6 };
63
static netsnmp_tdomain tcp6Domain;
64
65
/*
66
 * Return a string representing the address in data, or else the "far end"
67
 * address if data is NULL.  
68
 */
69
70
static char *
71
netsnmp_tcp6_fmtaddr(netsnmp_transport *t, const void *data, int len)
72
0
{
73
0
    return netsnmp_ipv6_fmtaddr("TCP/IPv6", t, data, len);
74
0
}
75
76
static NETSNMP_SOCKET
77
netsnmp_tcp6_accept(netsnmp_transport *t)
78
0
{
79
0
    netsnmp_udp_addr_pair *addr_pair;
80
0
    struct sockaddr       *farend;
81
0
    NETSNMP_SOCKET         newsock = NETSNMP_INVALID_SOCKET;
82
0
    socklen_t              farendlen;
83
84
0
    addr_pair = calloc(1, sizeof(*addr_pair));
85
0
    if (addr_pair == NULL) {
86
        /*
87
         * Indicate that the acceptance of this socket failed.  
88
         */
89
0
        DEBUGMSGTL(("netsnmp_tcp6", "accept: malloc failed\n"));
90
0
        return NETSNMP_INVALID_SOCKET;
91
0
    }
92
93
0
    farend = &addr_pair->remote_addr.sa;
94
0
    farendlen = sizeof(addr_pair->remote_addr.sin6);
95
96
0
    if (t && NETSNMP_IS_VALID_SOCKET(t->sock)) {
97
0
        newsock = accept(t->sock, farend, &farendlen);
98
99
0
        if (!NETSNMP_IS_VALID_SOCKET(newsock)) {
100
0
            DEBUGMSGTL(("netsnmp_tcp6", "accept failed errno %d \"%s\"\n",
101
0
                        errno, strerror(errno)));
102
0
            free(addr_pair);
103
0
            return newsock;
104
0
        }
105
106
0
        free(t->data);
107
0
        t->data = addr_pair;
108
0
        t->data_length = sizeof(netsnmp_udp_addr_pair);
109
0
        DEBUGIF("netsnmp_tcp6") {
110
0
            char *str = netsnmp_tcp6_fmtaddr(NULL, addr_pair,
111
0
                                             sizeof(netsnmp_udp_addr_pair));
112
113
0
            DEBUGMSGTL(("netsnmp_tcp6", "accept succeeded (from %s)\n", str));
114
0
            free(str);
115
0
        }
116
117
        /*
118
         * Try to make the new socket blocking.  
119
         */
120
121
0
        if (netsnmp_set_non_blocking_mode(newsock, FALSE) < 0)
122
0
            DEBUGMSGTL(("netsnmp_tcp6",
123
0
                        "accept: couldn't f_getfl of fd %" NETSNMP_FMT_SKT "\n",
124
0
                        newsock));
125
126
0
        netsnmp_set_tcp_nodelay(newsock, 1);
127
128
        /*
129
         * Allow user to override the send and receive buffers. Default is
130
         * to use os default.  Don't worry too much about errors --
131
         * just plough on regardless.  
132
         */
133
0
        netsnmp_sock_buffer_set(newsock, SO_SNDBUF, 1, 0);
134
0
        netsnmp_sock_buffer_set(newsock, SO_RCVBUF, 1, 0);
135
136
0
        return newsock;
137
0
    } else {
138
0
        free(addr_pair);
139
0
        return NETSNMP_INVALID_SOCKET;
140
0
    }
141
0
}
142
143
144
145
/*
146
 * Open a TCP/IPv6-based transport for SNMP.  Local is TRUE if addr is the
147
 * local address to bind to (i.e. this is a server-type session); otherwise
148
 * addr is the remote address to send things to.  
149
 */
150
151
netsnmp_transport *
152
netsnmp_tcp6_transport(const struct netsnmp_ep *ep, int local)
153
0
{
154
0
    const struct sockaddr_in6 *addr = &ep->a.sin6;
155
0
    netsnmp_transport *t = NULL;
156
0
    netsnmp_udp_addr_pair     *addr_pair = NULL;
157
0
    int             rc = 0;
158
0
    int             socket_initialized = 0;
159
160
#ifdef NETSNMP_NO_LISTEN_SUPPORT
161
    if (local)
162
        return NULL;
163
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
164
165
0
    if (addr == NULL || addr->sin6_family != AF_INET6)
166
0
        return NULL;
167
168
0
    t = netsnmp_transport_alloc();
169
0
    if (t == NULL)
170
0
        return NULL;
171
172
0
    DEBUGIF("netsnmp_tcp6") {
173
0
        char *str = netsnmp_tcp6_fmtaddr(NULL, addr,
174
0
                                         sizeof(struct sockaddr_in6));
175
176
0
        DEBUGMSGTL(("netsnmp_tcp6", "open %s %s\n", local ? "local" : "remote",
177
0
                    str));
178
0
        free(str);
179
0
    }
180
181
0
    addr_pair = calloc(1, sizeof(netsnmp_udp_addr_pair));
182
0
    if (addr_pair == NULL)
183
0
        goto err;
184
0
    t->data = addr_pair;
185
0
    t->data_length = sizeof(netsnmp_udp_addr_pair);
186
0
    memcpy(&addr_pair->remote_addr.sin6, addr, sizeof(struct sockaddr_in6));
187
188
0
    t->domain = netsnmp_TCPIPv6Domain;
189
0
    t->domain_length = OID_LENGTH(netsnmp_TCPIPv6Domain);
190
191
0
#ifndef NETSNMP_NO_SYSTEMD
192
    /*
193
     * Maybe the socket was already provided by systemd...
194
     */
195
0
    if (local) {
196
0
        t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_STREAM, 1,
197
0
                ntohs(addr->sin6_port));
198
0
        if (t->sock >= 0)
199
0
            socket_initialized = 1;
200
0
    }
201
0
#endif
202
0
    if (!socket_initialized)
203
0
        t->sock = socket(PF_INET6, SOCK_STREAM, 0);
204
0
    if (!NETSNMP_IS_VALID_SOCKET(t->sock))
205
0
        goto err;
206
207
0
    t->flags = NETSNMP_TRANSPORT_FLAG_STREAM;
208
209
0
    if (!local)
210
0
        netsnmp_bind_ep_iface(t->sock, ep, "netsnmp_tcp");
211
212
0
    if (local) {
213
0
#ifndef NETSNMP_NO_LISTEN_SUPPORT
214
0
        int opt = 1;
215
216
        /*
217
         * This session is intended as a server, so we must bind on to the
218
         * given IP address, which may include an interface address, or could
219
         * be INADDR_ANY, but certainly includes a port number.
220
         */
221
222
0
#ifdef IPV6_V6ONLY
223
        /* Try to restrict PF_INET6 socket to IPv6 communications only. */
224
0
        {
225
0
    int one=1;
226
0
    if (setsockopt(t->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&one,
227
0
                         sizeof(one)) != 0) {
228
0
      DEBUGMSGTL(("netsnmp_tcp6",
229
0
                        "couldn't set IPV6_V6ONLY to %d bytes: %s\n", one,
230
0
                        strerror(errno)));
231
0
    } 
232
0
  }
233
0
#endif
234
235
0
        t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
236
0
        t->local_length = sizeof(*addr);
237
0
        t->local = netsnmp_memdup(addr, sizeof(*addr));
238
0
        if (!t->local)
239
0
            goto err;
240
241
        /*
242
         * We should set SO_REUSEADDR too.  
243
         */
244
245
0
        setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt,
246
0
                   sizeof(opt));
247
248
0
        if (!socket_initialized) {
249
0
            rc = netsnmp_bindtodevice(t->sock, ep->iface);
250
0
            if (rc != 0) {
251
0
                DEBUGMSGTL(("netsnmp_tcp6", "failed to bind to iface %s: %s\n",
252
0
                            ep->iface, strerror(errno)));
253
0
                goto err;
254
0
            }
255
0
            rc = bind(t->sock, (const struct sockaddr *)addr, sizeof(*addr));
256
0
            if (rc != 0)
257
0
                goto err;
258
0
        }
259
260
        /*
261
         * Since we are going to be letting select() tell us when connections
262
         * are ready to be accept()ed, we need to make the socket n0n-blocking
263
         * to avoid the race condition described in W. R. Stevens, ``Unix
264
         * Network Programming Volume I Second Edition'', pp. 422--4, which
265
         * could otherwise wedge the agent.
266
         */
267
268
0
        if (netsnmp_set_non_blocking_mode(t->sock, TRUE) < 0)
269
0
            DEBUGMSGTL(("netsnmp_tcp6",
270
0
                        "couldn't set non-blocking mode on fd %" NETSNMP_FMT_SKT "\n",
271
0
                        t->sock));
272
273
        /*
274
         * Now sit here and wait for connections to arrive.  
275
         */
276
277
0
        if (!socket_initialized) {
278
0
            rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
279
0
            if (rc != 0)
280
0
                goto err;
281
0
        }
282
283
        /*
284
         * no buffer size on listen socket - doesn't make sense
285
         */
286
#else /* NETSNMP_NO_LISTEN_SUPPORT */
287
        return NULL;
288
#endif /* NETSNMP_NO_LISTEN_SUPPORT */
289
0
    } else {
290
0
        t->remote_length = sizeof(*addr);
291
0
        t->remote = netsnmp_memdup(addr, sizeof(*addr));
292
0
        if (!t->remote)
293
0
            goto err;
294
295
        /*
296
         * This is a client-type session, so attempt to connect to the far
297
         * end.  We don't go non-blocking here because it's not obvious what
298
         * you'd then do if you tried to do snmp_sends before the connection
299
         * had completed.  So this can block.
300
         */
301
302
0
        rc = connect(t->sock, (const struct sockaddr *)addr, sizeof(*addr));
303
0
        DEBUGMSGTL(("netsnmp_tcp6", "connect returns %d\n", rc));
304
0
        if (rc < 0)
305
0
            goto err;
306
307
0
        netsnmp_set_tcp_nodelay(t->sock, 1);
308
309
        /*
310
         * Allow user to override the send and receive buffers. Default is
311
         * to use os default.  Don't worry too much about errors --
312
         * just plough on regardless.  
313
         */
314
0
        netsnmp_sock_buffer_set(t->sock, SO_SNDBUF, local, 0);
315
0
        netsnmp_sock_buffer_set(t->sock, SO_RCVBUF, local, 0);
316
0
    }
317
318
    /*
319
     * Message size is not limited by this transport (hence msgMaxSize
320
     * is equal to the maximum legal size of an SNMP message).  
321
     */
322
323
0
    t->msgMaxSize = SNMP_MAX_PACKET_LEN;
324
0
    t->f_recv     = netsnmp_tcpbase_recv;
325
0
    t->f_send     = netsnmp_tcpbase_send;
326
0
    t->f_close    = netsnmp_socketbase_close;
327
0
    t->f_accept   = netsnmp_tcp6_accept;
328
0
    t->f_setup_session = netsnmp_ipbase_session_init;
329
0
    t->f_fmtaddr  = netsnmp_tcp6_fmtaddr;
330
0
    t->f_get_taddr = netsnmp_ipv6_get_taddr;
331
332
0
    return t;
333
334
0
err:
335
0
    netsnmp_socketbase_close(t);
336
0
    netsnmp_transport_free(t);
337
0
    return NULL;
338
0
}
339
340
netsnmp_transport *
341
netsnmp_tcp6_create_tstring(const char *str, int local,
342
          const char *default_target)
343
0
{
344
0
    struct netsnmp_ep ep;
345
346
0
    if (netsnmp_sockaddr_in6_3(&ep, str, default_target)) {
347
0
        return netsnmp_tcp6_transport(&ep, local);
348
0
    } else {
349
0
        return NULL;
350
0
    }
351
0
}
352
353
354
/*
355
 * See:
356
 * 
357
 * http://www.ietf.org/internet-drafts/draft-ietf-ops-taddress-mib-01.txt
358
 * 
359
 * (or newer equivalent) for details of the TC which we are using for
360
 * the mapping here.  
361
 */
362
363
netsnmp_transport *
364
netsnmp_tcp6_create_ostring(const void *o, size_t o_len, int local)
365
0
{
366
0
    struct netsnmp_ep ep;
367
368
0
    memset(&ep, 0, sizeof(ep));
369
0
    if (netsnmp_ipv6_ostring_to_sockaddr(&ep.a.sin6, o, o_len))
370
0
        return netsnmp_tcp6_transport(&ep, local);
371
0
    return NULL;
372
0
}
373
374
375
void
376
netsnmp_tcpipv6_ctor(void)
377
4.64k
{
378
4.64k
    tcp6Domain.name = netsnmp_TCPIPv6Domain;
379
4.64k
    tcp6Domain.name_length = OID_LENGTH(netsnmp_TCPIPv6Domain);
380
4.64k
    tcp6Domain.f_create_from_tstring_new = netsnmp_tcp6_create_tstring;
381
4.64k
    tcp6Domain.f_create_from_ostring     = netsnmp_tcp6_create_ostring;
382
4.64k
    tcp6Domain.prefix = calloc(4, sizeof(char *));
383
4.64k
    if (!tcp6Domain.prefix) {
384
0
        snmp_log(LOG_ERR, "calloc() failed - out of memory\n");
385
0
        return;
386
0
    }
387
4.64k
    tcp6Domain.prefix[0] = "tcp6";
388
4.64k
    tcp6Domain.prefix[1] = "tcpv6";
389
4.64k
    tcp6Domain.prefix[2] = "tcpipv6";
390
391
4.64k
    netsnmp_tdomain_register(&tcp6Domain);
392
4.64k
}
393
394
#endif /* NETSNMP_TRANSPORT_TCPIPV6_DOMAIN */
395