inet: fix: possible unaligned access in packed structure (inet/inet6)
[lttng-tools.git] / src / common / sessiond-comm / inet6.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 "inet6.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 inet6_ops = {
43 .bind = lttcomm_bind_inet6_sock,
44 .close = lttcomm_close_inet6_sock,
45 .connect = lttcomm_connect_inet6_sock,
46 .accept = lttcomm_accept_inet6_sock,
47 .listen = lttcomm_listen_inet6_sock,
48 .recvmsg = lttcomm_recvmsg_inet6_sock,
49 .sendmsg = lttcomm_sendmsg_inet6_sock,
50};
51
52/*
53 * Creates an PF_INET socket.
54 */
90e535ef 55LTTNG_HIDDEN
6364a07a
DG
56int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto)
57{
de5e9086 58 int val = 1, ret;
783a3b9a 59 unsigned long timeout;
6364a07a
DG
60
61 /* Create server socket */
7b43086d 62 if ((sock->fd = socket(PF_INET6, type, proto)) < 0) {
6364a07a
DG
63 PERROR("socket inet6");
64 goto error;
65 }
66
67 sock->ops = &inet6_ops;
68
69 /*
70 * Set socket option to reuse the address.
71 */
72 ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
73 if (ret < 0) {
74 PERROR("setsockopt inet6");
75 goto error;
76 }
783a3b9a
MD
77 timeout = lttcomm_get_network_timeout();
78 if (timeout) {
79 ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout);
80 if (ret) {
81 goto error;
82 }
83 ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout);
84 if (ret) {
85 goto error;
86 }
87 }
6364a07a
DG
88
89 return 0;
90
91error:
92 return -1;
93}
94
95/*
96 * Bind socket and return.
97 */
90e535ef 98LTTNG_HIDDEN
6364a07a
DG
99int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock)
100{
2288467f 101 return bind(sock->fd,
1a65bbb8
JG
102 (const struct sockaddr *) ALIGNED_CONST_PTR(
103 sock->sockaddr.addr.sin6),
6364a07a 104 sizeof(sock->sockaddr.addr.sin6));
6364a07a
DG
105}
106
a655f4cf
MD
107static
108int connect_no_timeout(struct lttcomm_sock *sock)
109{
1a65bbb8
JG
110 return connect(sock->fd,
111 (const struct sockaddr *) ALIGNED_CONST_PTR(
112 sock->sockaddr.addr.sin6),
a655f4cf
MD
113 sizeof(sock->sockaddr.addr.sin6));
114}
115
a655f4cf
MD
116static
117int connect_with_timeout(struct lttcomm_sock *sock)
118{
119 unsigned long timeout = lttcomm_get_network_timeout();
120 int ret, flags, connect_ret;
121 struct timespec orig_time, cur_time;
2daf6502 122 unsigned long diff_ms;
a655f4cf
MD
123
124 ret = fcntl(sock->fd, F_GETFL, 0);
125 if (ret == -1) {
126 PERROR("fcntl");
127 return -1;
128 }
129 flags = ret;
130
131 /* Set socket to nonblock */
132 ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
133 if (ret == -1) {
134 PERROR("fcntl");
135 return -1;
136 }
137
389fbf04 138 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
a655f4cf
MD
139 if (ret == -1) {
140 PERROR("clock_gettime");
141 return -1;
142 }
143
144 connect_ret = connect(sock->fd,
1a65bbb8
JG
145 (const struct sockaddr *) ALIGNED_CONST_PTR(
146 sock->sockaddr.addr.sin6),
147 sizeof(sock->sockaddr.addr.sin6));
148 if (connect_ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK &&
149 errno != EINPROGRESS) {
a655f4cf
MD
150 goto error;
151 } else if (!connect_ret) {
152 /* Connect succeeded */
153 goto success;
154 }
155
48ac9596
JR
156 DBG("Asynchronous connect for sock %d, performing polling with"
157 " timeout: %lums", sock->fd, timeout);
158
a655f4cf
MD
159 /*
160 * Perform poll loop following EINPROGRESS recommendation from
161 * connect(2) man page.
162 */
163 do {
164 struct pollfd fds;
165
166 fds.fd = sock->fd;
167 fds.events = POLLOUT;
168 fds.revents = 0;
169 ret = poll(&fds, 1, RECONNECT_DELAY);
170 if (ret < 0) {
171 goto error;
172 } else if (ret > 0) {
173 int optval;
174 socklen_t optval_len = sizeof(optval);
175
176 if (!(fds.revents & POLLOUT)) {
177 /* Either hup or error */
178 errno = EPIPE;
179 goto error;
180 }
181 /* got something */
182 ret = getsockopt(sock->fd, SOL_SOCKET,
183 SO_ERROR, &optval, &optval_len);
184 if (ret) {
48ac9596 185 PERROR("getsockopt");
a655f4cf
MD
186 goto error;
187 }
188 if (!optval) {
189 connect_ret = 0;
190 goto success;
191 } else {
48ac9596
JR
192 /* Get actual connect() errno from opt_val */
193 errno = optval;
a655f4cf
MD
194 goto error;
195 }
196 }
197 /* ret == 0: timeout */
389fbf04 198 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
a655f4cf
MD
199 if (ret == -1) {
200 PERROR("clock_gettime");
201 connect_ret = ret;
202 goto error;
203 }
2daf6502
MD
204 if (timespec_to_ms(timespec_abs_diff(cur_time, orig_time), &diff_ms) < 0) {
205 ERR("timespec_to_ms input overflows milliseconds output");
206 connect_ret = -1;
207 goto error;
208 }
209 } while (diff_ms < timeout);
a655f4cf
MD
210
211 /* Timeout */
212 errno = ETIMEDOUT;
213 connect_ret = -1;
214
215success:
216 /* Restore initial flags */
217 ret = fcntl(sock->fd, F_SETFL, flags);
218 if (ret == -1) {
219 PERROR("fcntl");
220 /* Continue anyway */
221 }
222error:
223 return connect_ret;
224}
225
6364a07a
DG
226/*
227 * Connect PF_INET socket.
228 */
90e535ef 229LTTNG_HIDDEN
6364a07a
DG
230int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock)
231{
232 int ret, closeret;
233
a655f4cf
MD
234 if (lttcomm_get_network_timeout()) {
235 ret = connect_with_timeout(sock);
236 } else {
237 ret = connect_no_timeout(sock);
238 }
6364a07a 239 if (ret < 0) {
82c05d47 240 PERROR("connect inet6");
6364a07a
DG
241 goto error_connect;
242 }
243
244 return ret;
245
246error_connect:
247 closeret = close(sock->fd);
248 if (closeret) {
249 PERROR("close inet6");
250 }
251
252 return ret;
253}
254
255/*
256 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
257 * MUST be bind(2) before.
258 */
90e535ef 259LTTNG_HIDDEN
6364a07a
DG
260struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock)
261{
262 int new_fd;
88a5db70 263 socklen_t len;
6364a07a 264 struct lttcomm_sock *new_sock;
1a65bbb8 265 struct sockaddr_in6 new_addr = {};
6364a07a
DG
266
267 if (sock->proto == LTTCOMM_SOCK_UDP) {
268 /*
269 * accept(2) does not exist for UDP so simply return the passed socket.
270 */
271 new_sock = sock;
272 goto end;
273 }
274
de5e9086 275 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
276 if (new_sock == NULL) {
277 goto error;
278 }
279
1a65bbb8 280 len = sizeof(new_addr);
88a5db70 281
6364a07a 282 /* Blocking call */
1a65bbb8 283 new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len);
6364a07a
DG
284 if (new_fd < 0) {
285 PERROR("accept inet6");
286 goto error;
287 }
1a65bbb8 288 new_sock->sockaddr.addr.sin6 = new_addr;
6364a07a 289 new_sock->fd = new_fd;
de5e9086 290 new_sock->ops = &inet6_ops;
6364a07a
DG
291
292end:
293 return new_sock;
294
295error:
296 free(new_sock);
297 return NULL;
298}
299
300/*
301 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
302 */
90e535ef 303LTTNG_HIDDEN
6364a07a
DG
304int lttcomm_listen_inet6_sock(struct lttcomm_sock *sock, int backlog)
305{
306 int ret;
307
308 if (sock->proto == LTTCOMM_SOCK_UDP) {
309 /* listen(2) does not exist for UDP so simply return success. */
310 ret = 0;
311 goto end;
312 }
313
314 /* Default listen backlog */
315 if (backlog <= 0) {
316 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
317 }
318
319 ret = listen(sock->fd, backlog);
320 if (ret < 0) {
321 PERROR("listen inet6");
322 }
323
324end:
325 return ret;
326}
327
328/*
329 * Receive data of size len in put that data into the buf param. Using recvmsg
330 * API.
331 *
332 * Return the size of received data.
333 */
90e535ef 334LTTNG_HIDDEN
6364a07a
DG
335ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf,
336 size_t len, int flags)
337{
338 struct msghdr msg;
339 struct iovec iov[1];
340 ssize_t ret = -1;
7c5aef62 341 size_t len_last;
1a65bbb8 342 struct sockaddr_in6 addr = sock->sockaddr.addr.sin6;
6364a07a
DG
343
344 memset(&msg, 0, sizeof(msg));
345
346 iov[0].iov_base = buf;
347 iov[0].iov_len = len;
348 msg.msg_iov = iov;
349 msg.msg_iovlen = 1;
350
1a65bbb8 351 msg.msg_name = (struct sockaddr *) &addr;
6364a07a
DG
352 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
353
6364a07a 354 do {
7c5aef62 355 len_last = iov[0].iov_len;
6364a07a 356 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62 357 if (ret > 0) {
5312a3ed
JG
358 if (flags & MSG_DONTWAIT) {
359 goto end;
360 }
7c5aef62
DG
361 iov[0].iov_base += ret;
362 iov[0].iov_len -= ret;
363 assert(ret <= len_last);
364 }
365 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a 366 if (ret < 0) {
7c5aef62
DG
367 PERROR("recvmsg inet");
368 } else if (ret > 0) {
369 ret = len;
6364a07a 370 }
7c5aef62 371 /* Else ret = 0 meaning an orderly shutdown. */
5312a3ed 372end:
6364a07a
DG
373 return ret;
374}
375
376/*
377 * Send buf data of size len. Using sendmsg API.
378 *
379 * Return the size of sent data.
380 */
90e535ef 381LTTNG_HIDDEN
c2d69327 382ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, const void *buf,
6364a07a
DG
383 size_t len, int flags)
384{
385 struct msghdr msg;
386 struct iovec iov[1];
387 ssize_t ret = -1;
388
389 memset(&msg, 0, sizeof(msg));
390
c2d69327 391 iov[0].iov_base = (void *) buf;
6364a07a
DG
392 iov[0].iov_len = len;
393 msg.msg_iov = iov;
394 msg.msg_iovlen = 1;
395
396 switch (sock->proto) {
397 case LTTCOMM_SOCK_UDP:
1a65bbb8
JG
398 {
399 struct sockaddr_in6 addr = sock->sockaddr.addr.sin6;
400
401 msg.msg_name = (struct sockaddr *) &addr;
6364a07a
DG
402 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
403 break;
1a65bbb8 404 }
6364a07a
DG
405 default:
406 break;
407 }
408
409 do {
410 ret = sendmsg(sock->fd, &msg, flags);
411 } while (ret < 0 && errno == EINTR);
412 if (ret < 0) {
413 /*
414 * Only warn about EPIPE when quiet mode is deactivated.
415 * We consider EPIPE as expected.
416 */
417 if (errno != EPIPE || !lttng_opt_quiet) {
418 PERROR("sendmsg inet6");
419 }
420 }
421
422 return ret;
423}
424
425/*
426 * Shutdown cleanly and close.
427 */
90e535ef 428LTTNG_HIDDEN
6364a07a
DG
429int lttcomm_close_inet6_sock(struct lttcomm_sock *sock)
430{
6e742359 431 int ret;
6364a07a 432
de5e9086 433 /* Don't try to close an invalid marked socket */
6364a07a
DG
434 if (sock->fd == -1) {
435 return 0;
436 }
437
6e742359
DG
438 ret = close(sock->fd);
439 if (ret) {
6364a07a
DG
440 PERROR("close inet6");
441 }
442
443 /* Mark socket */
444 sock->fd = -1;
445
446 return ret;
447}
This page took 0.063823 seconds and 4 git commands to generate.