Debian Codegraph @

snmplib/transports/snmpTCPIPv6Domain.c

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