Debian Codegraph @

snmplib/transports/snmpTCPDomain.c

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