1 #include <net-snmp/net-snmp-config.h>
19 #include <sys/socket.h>
22 #include <netinet/in.h>
25 #include <arpa/inet.h>
35 #include <net-snmp/types.h>
36 #include <net-snmp/output_api.h>
38 #include <net-snmp/library/snmp_transport.h>
39 #include <net-snmp/library/snmpUDPDomain.h>
40 #include <net-snmp/library/snmpTCPDomain.h>
43 oid netsnmp_snmpTCPDomain[8] = { 1, 3, 6, 1, 3, 91, 1, 1 };
44 static netsnmp_tdomain tcpDomain;
48 * Return a string representing the address in data, or else the "far end"
49 * address if data is NULL.
53 netsnmp_tcp_fmtaddr(netsnmp_transport *t, void *data, int len)
55 struct sockaddr_in *to = NULL;
57 if (data != NULL && len == sizeof(struct sockaddr_in)) {
58 to = (struct sockaddr_in *) data;
59 } else if (t != NULL && t->data != NULL &&
60 t->data_length == sizeof(struct sockaddr_in)) {
61 to = (struct sockaddr_in *) t->data;
64 return strdup("TCP: unknown");
69 * Here we just print the IP address of the peer for compatibility
70 * purposes. It would be nice if we could include the port number and
71 * some indication of the domain (c.f. AAL5PVC).
74 sprintf(tmp, "%s", inet_ntoa(to->sin_addr));
82 * You can write something into opaque that will subsequently get passed back
83 * to your send function if you like. For instance, you might want to
84 * remember where a PDU came from, so that you can send a reply there...
88 netsnmp_tcp_recv(netsnmp_transport *t, void *buf, int size,
89 void **opaque, int *olength)
93 if (t != NULL && t->sock >= 0) {
95 rc = recv(t->sock, buf, size, 0);
96 if (rc < 0 && errno != EINTR) {
97 DEBUGMSGTL(("netsnmp_tcp", "recv fd %d err %d (\"%s\")\n",
98 t->sock, errno, strerror(errno)));
101 DEBUGMSGTL(("netsnmp_tcp", "recv fd %d got %d bytes\n",
108 if (opaque != NULL && olength != NULL) {
109 if (t->data_length > 0) {
110 if ((*opaque = malloc(t->data_length)) != NULL) {
111 memcpy(*opaque, t->data, t->data_length);
112 *olength = t->data_length;
128 netsnmp_tcp_send(netsnmp_transport *t, void *buf, int size,
129 void **opaque, int *olength)
133 if (t != NULL && t->sock >= 0) {
135 rc = send(t->sock, buf, size, 0);
136 if (rc < 0 && errno != EINTR) {
147 netsnmp_tcp_close(netsnmp_transport *t)
150 if (t != NULL && t->sock >= 0) {
151 DEBUGMSGTL(("netsnmp_tcp", "close fd %d\n", t->sock));
152 #ifndef HAVE_CLOSESOCKET
155 rc = closesocket(t->sock);
165 netsnmp_tcp_accept(netsnmp_transport *t)
167 struct sockaddr *farend = NULL;
168 int newsock = -1, sockflags = 0;
169 socklen_t farendlen = sizeof(struct sockaddr_in);
172 farend = (struct sockaddr *) malloc(sizeof(struct sockaddr_in));
174 if (farend == NULL) {
176 * Indicate that the acceptance of this socket failed.
178 DEBUGMSGTL(("netsnmp_tcp", "accept: malloc failed\n"));
182 if (t != NULL && t->sock >= 0) {
183 newsock = accept(t->sock, farend, &farendlen);
186 DEBUGMSGTL(("netsnmp_tcp", "accept failed rc %d errno %d \"%s\"\n",
187 newsock, errno, strerror(errno)));
192 if (t->data != NULL) {
197 t->data_length = farendlen;
198 string = netsnmp_tcp_fmtaddr(NULL, farend, farendlen);
199 DEBUGMSGTL(("netsnmp_tcp", "accept succeeded (from %s)\n", string));
203 * Try to make the new socket blocking.
207 ioctlsocket(newsock, FIONBIO, &sockflags);
209 if ((sockflags = fcntl(newsock, F_GETFL, 0)) >= 0) {
210 fcntl(newsock, F_SETFL, (sockflags & ~O_NONBLOCK));
212 DEBUGMSGTL(("netsnmp_tcp", "couldn't f_getfl of fd %d\n",newsock));
225 * Open a TCP-based transport for SNMP. Local is TRUE if addr is the local
226 * address to bind to (i.e. this is a server-type session); otherwise addr is
227 * the remote address to send things to.
231 netsnmp_tcp_transport(struct sockaddr_in *addr, int local)
233 netsnmp_transport *t = NULL;
237 if (addr == NULL || addr->sin_family != AF_INET) {
241 t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
245 memset(t, 0, sizeof(netsnmp_transport));
247 t->data = malloc(sizeof(struct sockaddr_in));
248 if (t->data == NULL) {
249 netsnmp_transport_free(t);
252 t->data_length = sizeof(struct sockaddr_in);
253 memcpy(t->data, addr, sizeof(struct sockaddr_in));
255 t->domain = netsnmp_snmpTCPDomain;
257 sizeof(netsnmp_snmpTCPDomain) / sizeof(netsnmp_snmpTCPDomain[0]);
259 t->sock = socket(PF_INET, SOCK_STREAM, 0);
261 netsnmp_transport_free(t);
265 t->flags = NETSNMP_TRANSPORT_FLAG_STREAM;
268 int sockflags = 0, opt = 1;
271 * This session is inteneded as a server, so we must bind to the given
272 * IP address (which may include an interface address, or could be
273 * INADDR_ANY, but will always include a port number.
276 t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN;
277 t->local = malloc(6);
278 if (t->local == NULL) {
279 netsnmp_tcp_close(t);
280 netsnmp_transport_free(t);
283 memcpy(t->local, (u_char *) & (addr->sin_addr.s_addr), 4);
284 t->local[4] = (htons(addr->sin_port) & 0xff00) >> 8;
285 t->local[5] = (htons(addr->sin_port) & 0x00ff) >> 0;
289 * We should set SO_REUSEADDR too.
292 setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt,
295 rc = bind(t->sock, (struct sockaddr *)addr, sizeof(struct sockaddr));
297 netsnmp_tcp_close(t);
298 netsnmp_transport_free(t);
303 * Since we are going to be letting select() tell us when connections
304 * are ready to be accept()ed, we need to make the socket n0n-blocking
305 * to avoid the race condition described in W. R. Stevens, ``Unix
306 * Network Programming Volume I Second Edition'', pp. 422--4, which
307 * could otherwise wedge the agent.
312 ioctlsocket(t->sock, FIONBIO, &opt);
314 sockflags = fcntl(t->sock, F_GETFL, 0);
315 fcntl(t->sock, F_SETFL, sockflags | O_NONBLOCK);
319 * Now sit here and wait for connections to arrive.
322 rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN);
324 netsnmp_tcp_close(t);
325 netsnmp_transport_free(t);
329 t->remote = malloc(6);
330 if (t->remote == NULL) {
331 netsnmp_tcp_close(t);
332 netsnmp_transport_free(t);
335 memcpy(t->remote, (u_char *) & (addr->sin_addr.s_addr), 4);
336 t->remote[4] = (htons(addr->sin_port) & 0xff00) >> 8;
337 t->remote[5] = (htons(addr->sin_port) & 0x00ff) >> 0;
338 t->remote_length = 6;
341 * This is a client-type session, so attempt to connect to the far
342 * end. We don't go non-blocking here because it's not obvious what
343 * you'd then do if you tried to do snmp_sends before the connection
344 * had completed. So this can block.
347 rc = connect(t->sock, (struct sockaddr *)addr,
348 sizeof(struct sockaddr));
351 netsnmp_tcp_close(t);
352 netsnmp_transport_free(t);
358 * Message size is not limited by this transport (hence msgMaxSize
359 * is equal to the maximum legal size of an SNMP message).
362 t->msgMaxSize = 0x7fffffff;
363 t->f_recv = netsnmp_tcp_recv;
364 t->f_send = netsnmp_tcp_send;
365 t->f_close = netsnmp_tcp_close;
366 t->f_accept = netsnmp_tcp_accept;
367 t->f_fmtaddr = netsnmp_tcp_fmtaddr;
375 netsnmp_tcp_create_tstring(const char *string, int local)
377 struct sockaddr_in addr;
379 if (netsnmp_sockaddr_in(&addr, string, 0)) {
380 return netsnmp_tcp_transport(&addr, local);
389 netsnmp_tcp_create_ostring(const u_char * o, size_t o_len, int local)
391 struct sockaddr_in addr;
394 addr.sin_family = AF_INET;
395 memcpy((u_char *) & (addr.sin_addr.s_addr), o, 4);
396 addr.sin_port = ntohs((o[4] << 8) + o[5]);
397 return netsnmp_tcp_transport(&addr, local);
405 netsnmp_tcp_ctor(void)
407 tcpDomain.name = netsnmp_snmpTCPDomain;
408 tcpDomain.name_length = sizeof(netsnmp_snmpTCPDomain) / sizeof(oid);
409 tcpDomain.prefix = calloc(2, sizeof(char *));
410 tcpDomain.prefix[0] = "tcp";
412 tcpDomain.f_create_from_tstring = netsnmp_tcp_create_tstring;
413 tcpDomain.f_create_from_ostring = netsnmp_tcp_create_ostring;
415 netsnmp_tdomain_register(&tcpDomain);