blob: 58fa0cae98cb6e131bdc565f023ed805683804ae [file] [log] [blame]
Wes Hardakereb6d4fc2002-01-04 21:00:48 +00001#include <net-snmp/net-snmp-config.h>
John Naylonc1ec3d82001-12-05 15:27:14 +00002
3#include <stdio.h>
4#include <sys/types.h>
John Naylonc1ec3d82001-12-05 15:27:14 +00005#include <errno.h>
6
7#if HAVE_STRING_H
8#include <string.h>
9#else
10#include <strings.h>
11#endif
12#if HAVE_STDLIB_H
13#include <stdlib.h>
14#endif
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#if HAVE_SYS_SOCKET_H
19#include <sys/socket.h>
20#endif
21#if HAVE_NETINET_IN_H
22#include <netinet/in.h>
23#endif
24#if HAVE_ARPA_INET_H
25#include <arpa/inet.h>
26#endif
27#if HAVE_NETDB_H
28#include <netdb.h>
29#endif
John Naylon608e8322001-12-05 15:48:35 +000030#if HAVE_FCNTL_H
31#include <fcntl.h>
32#endif
John Naylonc1ec3d82001-12-05 15:27:14 +000033
Niels Baggesen6d875f32002-12-16 07:40:39 +000034#if HAVE_DMALLOC_H
35#include <dmalloc.h>
36#endif
37
Wes Hardaker6d1f6022002-04-20 07:08:20 +000038#include <net-snmp/types.h>
Dave Shield67e824b2002-02-14 12:41:29 +000039#include <net-snmp/output_api.h>
40#include <net-snmp/config_api.h>
41
Dave Shieldff06f8d2002-02-14 14:23:04 +000042#include <net-snmp/library/snmp_transport.h>
43#include <net-snmp/library/snmpUDPIPv6Domain.h>
44#include <net-snmp/library/snmpTCPIPv6Domain.h>
John Naylonc1ec3d82001-12-05 15:27:14 +000045
Dave Shield187ccf62004-01-29 14:13:59 +000046oid netsnmp_TCPIPv6Domain[] = { TRANSPORT_DOMAIN_TCP_IPV6 };
Wes Hardakerd7de7e92002-03-08 15:43:16 +000047static netsnmp_tdomain tcp6Domain;
John Naylonc1ec3d82001-12-05 15:27:14 +000048
Wes Hardaker6d1f6022002-04-20 07:08:20 +000049/*
50 * Return a string representing the address in data, or else the "far end"
51 * address if data is NULL.
52 */
John Naylonc1ec3d82001-12-05 15:27:14 +000053
John Naylon47e64df2002-08-09 13:57:43 +000054static char *
55netsnmp_tcp6_fmtaddr(netsnmp_transport *t, void *data, int len)
John Naylonc1ec3d82001-12-05 15:27:14 +000056{
Wes Hardaker6d1f6022002-04-20 07:08:20 +000057 struct sockaddr_in6 *to = NULL;
John Naylonc1ec3d82001-12-05 15:27:14 +000058
John Naylon47e64df2002-08-09 13:57:43 +000059 DEBUGMSGTL(("netsnmp_tcp6", "fmtaddr: t = %p, data = %p, len = %d\n", t,
Wes Hardaker6d1f6022002-04-20 07:08:20 +000060 data, len));
61 if (data != NULL && len == sizeof(struct sockaddr_in6)) {
62 to = (struct sockaddr_in6 *) data;
63 } else if (t != NULL && t->data != NULL) {
64 to = (struct sockaddr_in6 *) t->data;
65 }
66 if (to == NULL) {
67 return strdup("TCP/IPv6: unknown");
John Naylonc1ec3d82001-12-05 15:27:14 +000068 } else {
John Naylon47e64df2002-08-09 13:57:43 +000069 char addr[INET6_ADDRSTRLEN];
70 char tmp[INET6_ADDRSTRLEN + 8];
Wes Hardaker6d1f6022002-04-20 07:08:20 +000071
72 sprintf(tmp, "[%s]:%hd",
73 inet_ntop(AF_INET6, (void *) &(to->sin6_addr), addr,
74 INET6_ADDRSTRLEN), ntohs(to->sin6_port));
75 return strdup(tmp);
John Naylonc1ec3d82001-12-05 15:27:14 +000076 }
John Naylonc1ec3d82001-12-05 15:27:14 +000077}
78
Wes Hardaker6d1f6022002-04-20 07:08:20 +000079/*
80 * You can write something into opaque that will subsequently get passed back
81 * to your send function if you like. For instance, you might want to
82 * remember where a PDU came from, so that you can send a reply there...
83 */
84
John Naylon47e64df2002-08-09 13:57:43 +000085static int
86netsnmp_tcp6_recv(netsnmp_transport *t, void *buf, int size,
87 void **opaque, int *olength)
John Naylonc1ec3d82001-12-05 15:27:14 +000088{
John Naylon47e64df2002-08-09 13:57:43 +000089 int rc = -1;
John Naylonc1ec3d82001-12-05 15:27:14 +000090
Wes Hardaker6d1f6022002-04-20 07:08:20 +000091 if (t != NULL && t->sock >= 0) {
John Naylon47e64df2002-08-09 13:57:43 +000092 while (rc < 0) {
93 rc = recv(t->sock, buf, size, 0);
94 if (rc < 0 && errno != EINTR) {
95 DEBUGMSGTL(("netsnmp_tcp6", "recv fd %d err %d (\"%s\")\n",
96 t->sock, errno, strerror(errno)));
97 return -1;
98 }
99 }
100 DEBUGMSGTL(("netsnmp_tcp6", "recv fd %d got %d bytes\n", t->sock, rc));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000101 } else {
102 return -1;
103 }
John Naylonc1ec3d82001-12-05 15:27:14 +0000104
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000105 if (opaque != NULL && olength != NULL) {
106 if (t->data_length > 0) {
107 if ((*opaque = malloc(t->data_length)) != NULL) {
108 memcpy(*opaque, t->data, t->data_length);
109 *olength = t->data_length;
110 } else {
111 *olength = 0;
112 }
113 } else {
114 *opaque = NULL;
115 *olength = 0;
116 }
117 }
John Naylonc1ec3d82001-12-05 15:27:14 +0000118
John Naylonc1ec3d82001-12-05 15:27:14 +0000119 return rc;
John Naylonc1ec3d82001-12-05 15:27:14 +0000120}
121
John Naylon47e64df2002-08-09 13:57:43 +0000122static int
123netsnmp_tcp6_send(netsnmp_transport *t, void *buf, int size,
124 void **opaque, int *olength)
John Naylonc1ec3d82001-12-05 15:27:14 +0000125{
John Naylon47e64df2002-08-09 13:57:43 +0000126 int rc = -1;
John Naylonc1ec3d82001-12-05 15:27:14 +0000127
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000128 if (t != NULL && t->sock >= 0) {
John Naylon47e64df2002-08-09 13:57:43 +0000129 while (rc < 0) {
130 rc = send(t->sock, buf, size, 0);
131 if (rc < 0 && errno != EINTR) {
132 break;
133 }
134 }
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000135 }
136 return rc;
137}
John Naylonc1ec3d82001-12-05 15:27:14 +0000138
John Naylon47e64df2002-08-09 13:57:43 +0000139static int
140netsnmp_tcp6_close(netsnmp_transport *t)
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000141{
John Naylon47e64df2002-08-09 13:57:43 +0000142 int rc = -1;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000143 if (t != NULL && t->sock >= 0) {
John Naylon47e64df2002-08-09 13:57:43 +0000144 DEBUGMSGTL(("netsnmp_tcp6", "close fd %d\n", t->sock));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000145#ifndef HAVE_CLOSESOCKET
146 rc = close(t->sock);
147#else
148 rc = closesocket(t->sock);
149#endif
150 t->sock = -1;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000151 }
John Naylon47e64df2002-08-09 13:57:43 +0000152 return rc;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000153}
154
John Naylon47e64df2002-08-09 13:57:43 +0000155static int
156netsnmp_tcp6_accept(netsnmp_transport *t)
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000157{
158 struct sockaddr_in6 *farend = NULL;
Wes Hardakerd3f6bad2002-05-06 22:59:34 +0000159 int newsock = -1, sockflags = 0;
Wes Hardakerd3f6bad2002-05-06 22:59:34 +0000160 socklen_t farendlen = sizeof(struct sockaddr_in6);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000161 char *string = NULL;
162
163 farend = (struct sockaddr_in6 *) malloc(sizeof(struct sockaddr_in6));
164
165 if (farend == NULL) {
166 /*
167 * Indicate that the acceptance of this socket failed.
168 */
John Naylon47e64df2002-08-09 13:57:43 +0000169 DEBUGMSGTL(("netsnmp_tcp6", "accept: malloc failed\n"));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000170 return -1;
John Naylonc1ec3d82001-12-05 15:27:14 +0000171 }
172
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000173 if (t != NULL && t->sock >= 0) {
174 newsock = accept(t->sock, (struct sockaddr *) farend, &farendlen);
175
176 if (newsock < 0) {
John Naylon47e64df2002-08-09 13:57:43 +0000177 DEBUGMSGTL(("netsnmp_tcp6","accept failed rc %d errno %d \"%s\"\n",
178 newsock, errno, strerror(errno)));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000179 free(farend);
180 return newsock;
181 }
182
183 if (t->data != NULL) {
184 free(t->data);
185 }
186
187 t->data = farend;
188 t->data_length = farendlen;
John Naylon47e64df2002-08-09 13:57:43 +0000189 string = netsnmp_tcp6_fmtaddr(NULL, farend, farendlen);
190 DEBUGMSGTL(("netsnmp_tcp6", "accept succeeded (from %s)\n", string));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000191 free(string);
192
193 /*
194 * Try to make the new socket blocking.
195 */
196
197#ifdef WIN32
198 ioctlsocket(newsock, FIONBIO, &sockflags);
199#else
200 if ((sockflags = fcntl(newsock, F_GETFL, 0)) >= 0) {
201 fcntl(newsock, F_SETFL, (sockflags & ~O_NONBLOCK));
202 } else {
John Naylon47e64df2002-08-09 13:57:43 +0000203 DEBUGMSGTL(("netsnmp_tcp6", "accept: couldn't f_getfl of fd %d\n",
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000204 newsock));
205 }
206#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000207 return newsock;
208 } else {
209 free(farend);
210 return -1;
211 }
212}
213
214
215
216/*
217 * Open a TCP/IPv6-based transport for SNMP. Local is TRUE if addr is the
218 * local address to bind to (i.e. this is a server-type session); otherwise
219 * addr is the remote address to send things to.
220 */
221
222netsnmp_transport *
223netsnmp_tcp6_transport(struct sockaddr_in6 *addr, int local)
224{
225 netsnmp_transport *t = NULL;
226 int rc = 0;
227 char *string = NULL;
228
229 if (addr == NULL || addr->sin6_family != AF_INET6) {
230 return NULL;
John Naylonc1ec3d82001-12-05 15:27:14 +0000231 }
232
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000233 t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
234 if (t == NULL) {
235 return NULL;
236 }
237 memset(t, 0, sizeof(netsnmp_transport));
238
John Naylon47e64df2002-08-09 13:57:43 +0000239 string = netsnmp_tcp6_fmtaddr(NULL, (void *)addr,
240 sizeof(struct sockaddr_in6));
241 DEBUGMSGTL(("netsnmp_tcp6", "open %s %s\n", local ? "local" : "remote",
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000242 string));
John Naylonc1ec3d82001-12-05 15:27:14 +0000243 free(string);
244
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000245 memset(t, 0, sizeof(netsnmp_transport));
246
247 t->data = malloc(sizeof(struct sockaddr_in6));
248 if (t->data == NULL) {
249 netsnmp_transport_free(t);
250 return NULL;
251 }
252 t->data_length = sizeof(struct sockaddr_in6);
253 memcpy(t->data, addr, sizeof(struct sockaddr_in6));
254
Wes Hardakere3f8d252002-05-01 16:31:38 +0000255 t->domain = netsnmp_TCPIPv6Domain;
256 t->domain_length = sizeof(netsnmp_TCPIPv6Domain) / sizeof(oid);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000257
258 t->sock = socket(PF_INET6, SOCK_STREAM, 0);
259 if (t->sock < 0) {
260 netsnmp_transport_free(t);
261 return NULL;
262 }
263
264 t->flags = NETSNMP_TRANSPORT_FLAG_STREAM;
265
266 if (local) {
John Naylon47e64df2002-08-09 13:57:43 +0000267 int sockflags = 0, opt = 1;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000268
269 /*
John Naylon47e64df2002-08-09 13:57:43 +0000270 * This session is inteneded as a server, so we must bind on to the
271 * given IP address, which may include an interface address, or could
272 * be INADDR_ANY, but certainly includes a port number.
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000273 */
274
275 t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
276 t->local = malloc(18);
277 if (t->local == NULL) {
John Naylon47e64df2002-08-09 13:57:43 +0000278 netsnmp_tcp6_close(t);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000279 netsnmp_transport_free(t);
280 return NULL;
281 }
282 memcpy(t->local, addr->sin6_addr.s6_addr, 16);
283 t->local[16] = (addr->sin6_port & 0xff00) >> 8;
284 t->local[17] = (addr->sin6_port & 0x00ff) >> 0;
285 t->local_length = 18;
286
287 /*
288 * We should set SO_REUSEADDR too.
289 */
290
291 setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
292
293 rc = bind(t->sock, (struct sockaddr *) addr,
John Naylon47e64df2002-08-09 13:57:43 +0000294 sizeof(struct sockaddr_in6));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000295 if (rc != 0) {
John Naylon47e64df2002-08-09 13:57:43 +0000296 netsnmp_tcp6_close(t);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000297 netsnmp_transport_free(t);
298 return NULL;
299 }
300
301 /*
John Naylon47e64df2002-08-09 13:57:43 +0000302 * Since we are going to be letting select() tell us when connections
303 * are ready to be accept()ed, we need to make the socket n0n-blocking
304 * to avoid the race condition described in W. R. Stevens, ``Unix
305 * Network Programming Volume I Second Edition'', pp. 422--4, which
306 * could otherwise wedge the agent.
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000307 */
John Naylonc1ec3d82001-12-05 15:27:14 +0000308
309#ifdef WIN32
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000310 opt = 1;
311 ioctlsocket(t->sock, FIONBIO, &opt);
John Naylonc1ec3d82001-12-05 15:27:14 +0000312#else
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000313 sockflags = fcntl(t->sock, F_GETFL, 0);
314 fcntl(t->sock, F_SETFL, sockflags | O_NONBLOCK);
315#endif
316
317 /*
318 * Now sit here and wait for connections to arrive.
319 */
320
321 rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
322 if (rc != 0) {
John Naylon47e64df2002-08-09 13:57:43 +0000323 netsnmp_tcp6_close(t);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000324 netsnmp_transport_free(t);
325 return NULL;
326 }
John Naylonc1ec3d82001-12-05 15:27:14 +0000327 } else {
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000328 t->remote = malloc(18);
329 if (t->remote == NULL) {
John Naylon47e64df2002-08-09 13:57:43 +0000330 netsnmp_tcp6_close(t);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000331 netsnmp_transport_free(t);
332 return NULL;
333 }
334 memcpy(t->remote, addr->sin6_addr.s6_addr, 16);
335 t->remote[16] = (addr->sin6_port & 0xff00) >> 8;
336 t->remote[17] = (addr->sin6_port & 0x00ff) >> 0;
337 t->remote_length = 18;
John Naylonc1ec3d82001-12-05 15:27:14 +0000338
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000339 /*
John Naylon47e64df2002-08-09 13:57:43 +0000340 * This is a client-type session, so attempt to connect to the far
341 * end. We don't go non-blocking here because it's not obvious what
342 * you'd then do if you tried to do snmp_sends before the connection
343 * had completed. So this can block.
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000344 */
345
346 rc = connect(t->sock, (struct sockaddr *) addr,
347 sizeof(struct sockaddr_in6));
348
John Naylon47e64df2002-08-09 13:57:43 +0000349 DEBUGMSGTL(("netsnmp_tcp6", "connect returns %d\n", rc));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000350
351 if (rc < 0) {
John Naylon47e64df2002-08-09 13:57:43 +0000352 netsnmp_tcp6_close(t);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000353 netsnmp_transport_free(t);
354 return NULL;
355 }
356 }
357
358 /*
359 * Message size is not limited by this transport (hence msgMaxSize
360 * is equal to the maximum legal size of an SNMP message).
361 */
362
363 t->msgMaxSize = 0x7fffffff;
John Naylon47e64df2002-08-09 13:57:43 +0000364 t->f_recv = netsnmp_tcp6_recv;
365 t->f_send = netsnmp_tcp6_send;
366 t->f_close = netsnmp_tcp6_close;
367 t->f_accept = netsnmp_tcp6_accept;
368 t->f_fmtaddr = netsnmp_tcp6_fmtaddr;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000369
370 return t;
John Naylonc1ec3d82001-12-05 15:27:14 +0000371}
372
373
374
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000375netsnmp_transport *
John Naylon47e64df2002-08-09 13:57:43 +0000376netsnmp_tcp6_create_tstring(const char *string, int local)
John Naylonc1ec3d82001-12-05 15:27:14 +0000377{
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000378 struct sockaddr_in6 addr;
John Naylonc1ec3d82001-12-05 15:27:14 +0000379
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000380 if (netsnmp_sockaddr_in6(&addr, string, 0)) {
381 return netsnmp_tcp6_transport(&addr, local);
382 } else {
383 return NULL;
John Naylonc1ec3d82001-12-05 15:27:14 +0000384 }
John Naylonc1ec3d82001-12-05 15:27:14 +0000385}
386
387
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000388/*
389 * See:
390 *
391 * http://www.ietf.org/internet-drafts/draft-ietf-ops-taddress-mib-01.txt
392 *
393 * (or newer equivalent) for details of the TC which we are using for
394 * the mapping here.
395 */
John Naylonc1ec3d82001-12-05 15:27:14 +0000396
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000397netsnmp_transport *
John Naylon47e64df2002-08-09 13:57:43 +0000398netsnmp_tcp6_create_ostring(const u_char * o, size_t o_len, int local)
John Naylonc1ec3d82001-12-05 15:27:14 +0000399{
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000400 struct sockaddr_in6 addr;
John Naylonc1ec3d82001-12-05 15:27:14 +0000401
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000402 if (o_len == 18) {
403 memset((u_char *) & addr, 0, sizeof(struct sockaddr_in6));
404 addr.sin6_family = AF_INET6;
405 memcpy((u_char *) & (addr.sin6_addr.s6_addr), o, 16);
406 addr.sin6_port = (o[16] << 8) + o[17];
407 return netsnmp_tcp6_transport(&addr, local);
408 }
John Naylonc1ec3d82001-12-05 15:27:14 +0000409 return NULL;
John Naylonc1ec3d82001-12-05 15:27:14 +0000410}
411
412
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000413void
414netsnmp_tcp6_ctor(void)
John Naylonc1ec3d82001-12-05 15:27:14 +0000415{
Wes Hardakere3f8d252002-05-01 16:31:38 +0000416 tcp6Domain.name = netsnmp_TCPIPv6Domain;
Wes Hardakerd3f6bad2002-05-06 22:59:34 +0000417 tcp6Domain.name_length = sizeof(netsnmp_TCPIPv6Domain) / sizeof(oid);
John Naylon47e64df2002-08-09 13:57:43 +0000418 tcp6Domain.f_create_from_tstring = netsnmp_tcp6_create_tstring;
419 tcp6Domain.f_create_from_ostring = netsnmp_tcp6_create_ostring;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000420 tcp6Domain.prefix = calloc(4, sizeof(char *));
421 tcp6Domain.prefix[0] = "tcp6";
422 tcp6Domain.prefix[1] = "tcpv6";
423 tcp6Domain.prefix[2] = "tcpipv6";
John Naylonc1ec3d82001-12-05 15:27:14 +0000424
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000425 netsnmp_tdomain_register(&tcp6Domain);
John Naylonc1ec3d82001-12-05 15:27:14 +0000426}