inet: fix: possible unaligned access in packed structure (inet/inet6)
[lttng-tools.git] / src / common / sessiond-comm / inet.c
CommitLineData
6364a07a
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
6364a07a
DG
19#include <assert.h>
20#include <limits.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <errno.h>
a655f4cf 28#include <fcntl.h>
389fbf04 29#include <common/compat/time.h>
a655f4cf 30#include <poll.h>
6364a07a 31
90e535ef 32#include <common/common.h>
395d6b02 33#include <common/time.h>
6364a07a
DG
34
35#include "inet.h"
36
a655f4cf
MD
37#define RECONNECT_DELAY 200 /* ms */
38
6364a07a
DG
39/*
40 * INET protocol operations.
41 */
42static const struct lttcomm_proto_ops inet_ops = {
43 .bind = lttcomm_bind_inet_sock,
44 .close = lttcomm_close_inet_sock,
45 .connect = lttcomm_connect_inet_sock,
46 .accept = lttcomm_accept_inet_sock,
47 .listen = lttcomm_listen_inet_sock,
48 .recvmsg = lttcomm_recvmsg_inet_sock,
49 .sendmsg = lttcomm_sendmsg_inet_sock,
50};
51
d831c249
DG
52unsigned long lttcomm_inet_tcp_timeout;
53
6364a07a
DG
54/*
55 * Creates an PF_INET socket.
56 */
90e535ef 57LTTNG_HIDDEN
6364a07a
DG
58int lttcomm_create_inet_sock(struct lttcomm_sock *sock, int type, int proto)
59{
de5e9086 60 int val = 1, ret;
783a3b9a 61 unsigned long timeout;
6364a07a
DG
62
63 /* Create server socket */
64 if ((sock->fd = socket(PF_INET, type, proto)) < 0) {
65 PERROR("socket inet");
66 goto error;
67 }
68
69 sock->ops = &inet_ops;
70
71 /*
72 * Set socket option to reuse the address.
73 */
74 ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
75 if (ret < 0) {
76 PERROR("setsockopt inet");
77 goto error;
78 }
783a3b9a
MD
79 timeout = lttcomm_get_network_timeout();
80 if (timeout) {
81 ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout);
82 if (ret) {
83 goto error;
84 }
85 ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout);
86 if (ret) {
87 goto error;
88 }
89 }
6364a07a
DG
90
91 return 0;
92
93error:
94 return -1;
95}
96
97/*
98 * Bind socket and return.
99 */
90e535ef 100LTTNG_HIDDEN
6364a07a
DG
101int lttcomm_bind_inet_sock(struct lttcomm_sock *sock)
102{
e6c533d6 103 return bind(sock->fd,
825395e2
JG
104 (const struct sockaddr *) ALIGNED_CONST_PTR(
105 sock->sockaddr.addr.sin),
6364a07a 106 sizeof(sock->sockaddr.addr.sin));
6364a07a
DG
107}
108
a655f4cf
MD
109static
110int connect_no_timeout(struct lttcomm_sock *sock)
111{
825395e2
JG
112 return connect(sock->fd,
113 (const struct sockaddr *) ALIGNED_CONST_PTR(
114 sock->sockaddr.addr.sin),
a655f4cf
MD
115 sizeof(sock->sockaddr.addr.sin));
116}
117
a655f4cf
MD
118static
119int connect_with_timeout(struct lttcomm_sock *sock)
120{
121 unsigned long timeout = lttcomm_get_network_timeout();
122 int ret, flags, connect_ret;
123 struct timespec orig_time, cur_time;
25683b5a 124 unsigned long diff_ms;
a655f4cf
MD
125
126 ret = fcntl(sock->fd, F_GETFL, 0);
127 if (ret == -1) {
128 PERROR("fcntl");
129 return -1;
130 }
131 flags = ret;
132
133 /* Set socket to nonblock */
134 ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
135 if (ret == -1) {
136 PERROR("fcntl");
137 return -1;
138 }
139
389fbf04 140 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
a655f4cf
MD
141 if (ret == -1) {
142 PERROR("clock_gettime");
143 return -1;
144 }
145
146 connect_ret = connect(sock->fd,
825395e2
JG
147 (const struct sockaddr *) ALIGNED_CONST_PTR(
148 sock->sockaddr.addr.sin),
149 sizeof(sock->sockaddr.addr.sin));
150 if (connect_ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK &&
151 errno != EINPROGRESS) {
a655f4cf
MD
152 goto error;
153 } else if (!connect_ret) {
154 /* Connect succeeded */
155 goto success;
156 }
157
dfe758f3
JR
158 DBG("Asynchronous connect for sock %d, performing polling with"
159 " timeout: %lums", sock->fd, timeout);
a655f4cf
MD
160 /*
161 * Perform poll loop following EINPROGRESS recommendation from
162 * connect(2) man page.
163 */
164 do {
165 struct pollfd fds;
166
167 fds.fd = sock->fd;
168 fds.events = POLLOUT;
169 fds.revents = 0;
170 ret = poll(&fds, 1, RECONNECT_DELAY);
171 if (ret < 0) {
172 goto error;
173 } else if (ret > 0) {
174 int optval;
175 socklen_t optval_len = sizeof(optval);
176
177 if (!(fds.revents & POLLOUT)) {
178 /* Either hup or error */
179 errno = EPIPE;
180 goto error;
181 }
182 /* got something */
183 ret = getsockopt(sock->fd, SOL_SOCKET,
184 SO_ERROR, &optval, &optval_len);
185 if (ret) {
dfe758f3 186 PERROR("getsockopt");
a655f4cf
MD
187 goto error;
188 }
189 if (!optval) {
190 connect_ret = 0;
191 goto success;
192 } else {
dfe758f3
JR
193 /* Get actual connect() errno from opt_val */
194 errno = optval;
a655f4cf
MD
195 goto error;
196 }
197 }
198 /* ret == 0: timeout */
389fbf04 199 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
a655f4cf
MD
200 if (ret == -1) {
201 PERROR("clock_gettime");
202 connect_ret = ret;
203 goto error;
204 }
25683b5a
MD
205 if (timespec_to_ms(timespec_abs_diff(cur_time, orig_time), &diff_ms) < 0) {
206 ERR("timespec_to_ms input overflows milliseconds output");
207 connect_ret = -1;
208 goto error;
209 }
210 } while (diff_ms < timeout);
a655f4cf
MD
211
212 /* Timeout */
213 errno = ETIMEDOUT;
214 connect_ret = -1;
215
216success:
217 /* Restore initial flags */
218 ret = fcntl(sock->fd, F_SETFL, flags);
219 if (ret == -1) {
220 PERROR("fcntl");
221 /* Continue anyway */
222 }
223error:
224 return connect_ret;
225}
226
6364a07a
DG
227/*
228 * Connect PF_INET socket.
229 */
90e535ef 230LTTNG_HIDDEN
6364a07a
DG
231int lttcomm_connect_inet_sock(struct lttcomm_sock *sock)
232{
233 int ret, closeret;
234
a655f4cf
MD
235 if (lttcomm_get_network_timeout()) {
236 ret = connect_with_timeout(sock);
237 } else {
238 ret = connect_no_timeout(sock);
239 }
6364a07a 240 if (ret < 0) {
82c05d47 241 PERROR("connect");
6364a07a
DG
242 goto error_connect;
243 }
244
245 return ret;
246
247error_connect:
248 closeret = close(sock->fd);
249 if (closeret) {
250 PERROR("close inet");
251 }
252
253 return ret;
254}
255
256/*
257 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
258 * MUST be bind(2) before.
259 */
90e535ef 260LTTNG_HIDDEN
6364a07a
DG
261struct lttcomm_sock *lttcomm_accept_inet_sock(struct lttcomm_sock *sock)
262{
263 int new_fd;
88a5db70 264 socklen_t len;
6364a07a 265 struct lttcomm_sock *new_sock;
71c648d8 266 unsigned long timeout;
825395e2 267 struct sockaddr_in new_addr = {};
6364a07a
DG
268
269 if (sock->proto == LTTCOMM_SOCK_UDP) {
270 /*
271 * accept(2) does not exist for UDP so simply return the passed socket.
272 */
273 new_sock = sock;
274 goto end;
275 }
276
de5e9086 277 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
278 if (new_sock == NULL) {
279 goto error;
280 }
281
825395e2 282 len = sizeof(new_addr);
88a5db70 283
6364a07a 284 /* Blocking call */
825395e2 285 new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len);
6364a07a
DG
286 if (new_fd < 0) {
287 PERROR("accept inet");
288 goto error;
289 }
825395e2 290 new_sock->sockaddr.addr.sin = new_addr;
71c648d8
JG
291 timeout = lttcomm_get_network_timeout();
292 if (timeout) {
293 int ret;
294
295 ret = lttcomm_setsockopt_rcv_timeout(new_fd, timeout);
296 if (ret) {
475cd9fa 297 goto error_close;
71c648d8
JG
298 }
299 ret = lttcomm_setsockopt_snd_timeout(new_fd, timeout);
300 if (ret) {
475cd9fa 301 goto error_close;
71c648d8
JG
302 }
303 }
6364a07a
DG
304
305 new_sock->fd = new_fd;
de5e9086 306 new_sock->ops = &inet_ops;
6364a07a
DG
307
308end:
309 return new_sock;
310
475cd9fa
DG
311error_close:
312 if (close(new_fd) < 0) {
313 PERROR("accept inet close fd");
314 }
315
6364a07a
DG
316error:
317 free(new_sock);
318 return NULL;
319}
320
321/*
322 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
323 */
90e535ef 324LTTNG_HIDDEN
6364a07a
DG
325int lttcomm_listen_inet_sock(struct lttcomm_sock *sock, int backlog)
326{
327 int ret;
328
329 if (sock->proto == LTTCOMM_SOCK_UDP) {
330 /* listen(2) does not exist for UDP so simply return success. */
331 ret = 0;
332 goto end;
333 }
334
335 /* Default listen backlog */
336 if (backlog <= 0) {
337 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
338 }
339
340 ret = listen(sock->fd, backlog);
341 if (ret < 0) {
342 PERROR("listen inet");
343 }
344
345end:
346 return ret;
347}
348
349/*
350 * Receive data of size len in put that data into the buf param. Using recvmsg
351 * API.
352 *
353 * Return the size of received data.
354 */
90e535ef 355LTTNG_HIDDEN
6364a07a
DG
356ssize_t lttcomm_recvmsg_inet_sock(struct lttcomm_sock *sock, void *buf,
357 size_t len, int flags)
358{
359 struct msghdr msg;
360 struct iovec iov[1];
361 ssize_t ret = -1;
7c5aef62 362 size_t len_last;
825395e2 363 struct sockaddr_in addr = sock->sockaddr.addr.sin;
6364a07a
DG
364
365 memset(&msg, 0, sizeof(msg));
366
367 iov[0].iov_base = buf;
368 iov[0].iov_len = len;
369 msg.msg_iov = iov;
370 msg.msg_iovlen = 1;
371
825395e2 372 msg.msg_name = (struct sockaddr *) &addr;
6364a07a
DG
373 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
374
6364a07a 375 do {
7c5aef62 376 len_last = iov[0].iov_len;
6364a07a 377 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62
DG
378 if (ret > 0) {
379 iov[0].iov_base += ret;
380 iov[0].iov_len -= ret;
381 assert(ret <= len_last);
382 }
383 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a
DG
384 if (ret < 0) {
385 PERROR("recvmsg inet");
7c5aef62
DG
386 } else if (ret > 0) {
387 ret = len;
6364a07a 388 }
7c5aef62 389 /* Else ret = 0 meaning an orderly shutdown. */
6364a07a
DG
390
391 return ret;
392}
393
394/*
395 * Send buf data of size len. Using sendmsg API.
396 *
397 * Return the size of sent data.
398 */
90e535ef 399LTTNG_HIDDEN
c2d69327 400ssize_t lttcomm_sendmsg_inet_sock(struct lttcomm_sock *sock, const void *buf,
6364a07a
DG
401 size_t len, int flags)
402{
403 struct msghdr msg;
404 struct iovec iov[1];
405 ssize_t ret = -1;
406
407 memset(&msg, 0, sizeof(msg));
408
c2d69327 409 iov[0].iov_base = (void *) buf;
6364a07a
DG
410 iov[0].iov_len = len;
411 msg.msg_iov = iov;
412 msg.msg_iovlen = 1;
413
414 switch (sock->proto) {
415 case LTTCOMM_SOCK_UDP:
825395e2
JG
416 {
417 struct sockaddr_in addr = sock->sockaddr.addr.sin;
418
419 msg.msg_name = (struct sockaddr *) &addr;
6364a07a
DG
420 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin);
421 break;
825395e2 422 }
6364a07a
DG
423 default:
424 break;
425 }
426
427 do {
428 ret = sendmsg(sock->fd, &msg, flags);
429 } while (ret < 0 && errno == EINTR);
430 if (ret < 0) {
431 /*
432 * Only warn about EPIPE when quiet mode is deactivated.
433 * We consider EPIPE as expected.
434 */
435 if (errno != EPIPE || !lttng_opt_quiet) {
436 PERROR("sendmsg inet");
437 }
438 }
439
440 return ret;
441}
442
443/*
444 * Shutdown cleanly and close.
445 */
90e535ef 446LTTNG_HIDDEN
6364a07a
DG
447int lttcomm_close_inet_sock(struct lttcomm_sock *sock)
448{
6e742359 449 int ret;
6364a07a 450
de5e9086 451 /* Don't try to close an invalid marked socket */
6364a07a
DG
452 if (sock->fd == -1) {
453 return 0;
454 }
455
6e742359
DG
456 ret = close(sock->fd);
457 if (ret) {
6364a07a
DG
458 PERROR("close inet");
459 }
460
461 /* Mark socket */
462 sock->fd = -1;
463
464 return ret;
465}
d831c249
DG
466
467/*
468 * Return value read from /proc or else 0 if value is not found.
469 */
470static unsigned long read_proc_value(const char *path)
471{
472 int ret, fd;
6cd525e8 473 ssize_t size_ret;
d831c249
DG
474 long r_val;
475 unsigned long val = 0;
476 char buf[64];
477
478 fd = open(path, O_RDONLY);
479 if (fd < 0) {
480 goto error;
481 }
482
6cd525e8
MD
483 size_ret = lttng_read(fd, buf, sizeof(buf));
484 /*
485 * Allow reading a file smaller than buf, but keep space for
486 * final \0.
487 */
488 if (size_ret < 0 || size_ret >= sizeof(buf)) {
d831c249
DG
489 PERROR("read proc failed");
490 goto error_close;
491 }
6cd525e8 492 buf[size_ret] = '\0';
d831c249
DG
493
494 errno = 0;
495 r_val = strtol(buf, NULL, 10);
496 if (errno != 0 || r_val < -1L) {
497 val = 0;
498 goto error_close;
499 } else {
500 if (r_val > 0) {
501 val = r_val;
502 }
503 }
504
505error_close:
506 ret = close(fd);
507 if (ret) {
508 PERROR("close /proc value");
509 }
510error:
511 return val;
512}
513
514LTTNG_HIDDEN
515void lttcomm_inet_init(void)
516{
18eed43f
DG
517 unsigned long syn_retries, fin_timeout, syn_timeout, env;
518
519 env = lttcomm_get_network_timeout();
520 if (env) {
521 lttcomm_inet_tcp_timeout = env;
522 goto end;
523 }
d831c249
DG
524
525 /* Assign default value and see if we can change it. */
526 lttcomm_inet_tcp_timeout = DEFAULT_INET_TCP_TIMEOUT;
527
528 syn_retries = read_proc_value(LTTCOMM_INET_PROC_SYN_RETRIES_PATH);
529 fin_timeout = read_proc_value(LTTCOMM_INET_PROC_FIN_TIMEOUT_PATH);
530
531 syn_timeout = syn_retries * LTTCOMM_INET_SYN_TIMEOUT_FACTOR;
532
533 /*
534 * Get the maximum between the two possible timeout value and use that to
535 * get the maximum with the default timeout.
536 */
537 lttcomm_inet_tcp_timeout = max_t(unsigned long,
538 max_t(unsigned long, syn_timeout, fin_timeout),
539 lttcomm_inet_tcp_timeout);
540
18eed43f 541end:
d831c249
DG
542 DBG("TCP inet operation timeout set to %lu sec", lttcomm_inet_tcp_timeout);
543}
This page took 0.064652 seconds and 4 git commands to generate.