inet: fix: possible unaligned access in packed structure (inet/inet6)
[lttng-tools.git] / src / common / sessiond-comm / inet6.c
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
18 #define _LGPL_SOURCE
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>
28 #include <fcntl.h>
29 #include <common/compat/time.h>
30 #include <poll.h>
31
32 #include <common/common.h>
33 #include <common/time.h>
34
35 #include "inet6.h"
36
37 #define RECONNECT_DELAY 200 /* ms */
38
39 /*
40 * INET protocol operations.
41 */
42 static 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 */
55 LTTNG_HIDDEN
56 int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto)
57 {
58 int val = 1, ret;
59 unsigned long timeout;
60
61 /* Create server socket */
62 if ((sock->fd = socket(PF_INET6, type, proto)) < 0) {
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 }
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 }
88
89 return 0;
90
91 error:
92 return -1;
93 }
94
95 /*
96 * Bind socket and return.
97 */
98 LTTNG_HIDDEN
99 int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock)
100 {
101 return bind(sock->fd,
102 (const struct sockaddr *) ALIGNED_CONST_PTR(
103 sock->sockaddr.addr.sin6),
104 sizeof(sock->sockaddr.addr.sin6));
105 }
106
107 static
108 int connect_no_timeout(struct lttcomm_sock *sock)
109 {
110 return connect(sock->fd,
111 (const struct sockaddr *) ALIGNED_CONST_PTR(
112 sock->sockaddr.addr.sin6),
113 sizeof(sock->sockaddr.addr.sin6));
114 }
115
116 static
117 int 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;
122 unsigned long diff_ms;
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
138 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time);
139 if (ret == -1) {
140 PERROR("clock_gettime");
141 return -1;
142 }
143
144 connect_ret = connect(sock->fd,
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) {
150 goto error;
151 } else if (!connect_ret) {
152 /* Connect succeeded */
153 goto success;
154 }
155
156 DBG("Asynchronous connect for sock %d, performing polling with"
157 " timeout: %lums", sock->fd, timeout);
158
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) {
185 PERROR("getsockopt");
186 goto error;
187 }
188 if (!optval) {
189 connect_ret = 0;
190 goto success;
191 } else {
192 /* Get actual connect() errno from opt_val */
193 errno = optval;
194 goto error;
195 }
196 }
197 /* ret == 0: timeout */
198 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time);
199 if (ret == -1) {
200 PERROR("clock_gettime");
201 connect_ret = ret;
202 goto error;
203 }
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);
210
211 /* Timeout */
212 errno = ETIMEDOUT;
213 connect_ret = -1;
214
215 success:
216 /* Restore initial flags */
217 ret = fcntl(sock->fd, F_SETFL, flags);
218 if (ret == -1) {
219 PERROR("fcntl");
220 /* Continue anyway */
221 }
222 error:
223 return connect_ret;
224 }
225
226 /*
227 * Connect PF_INET socket.
228 */
229 LTTNG_HIDDEN
230 int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock)
231 {
232 int ret, closeret;
233
234 if (lttcomm_get_network_timeout()) {
235 ret = connect_with_timeout(sock);
236 } else {
237 ret = connect_no_timeout(sock);
238 }
239 if (ret < 0) {
240 PERROR("connect inet6");
241 goto error_connect;
242 }
243
244 return ret;
245
246 error_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 */
259 LTTNG_HIDDEN
260 struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock)
261 {
262 int new_fd;
263 socklen_t len;
264 struct lttcomm_sock *new_sock;
265 struct sockaddr_in6 new_addr = {};
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
275 new_sock = lttcomm_alloc_sock(sock->proto);
276 if (new_sock == NULL) {
277 goto error;
278 }
279
280 len = sizeof(new_addr);
281
282 /* Blocking call */
283 new_fd = accept(sock->fd, (struct sockaddr *) &new_addr, &len);
284 if (new_fd < 0) {
285 PERROR("accept inet6");
286 goto error;
287 }
288 new_sock->sockaddr.addr.sin6 = new_addr;
289 new_sock->fd = new_fd;
290 new_sock->ops = &inet6_ops;
291
292 end:
293 return new_sock;
294
295 error:
296 free(new_sock);
297 return NULL;
298 }
299
300 /*
301 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
302 */
303 LTTNG_HIDDEN
304 int 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
324 end:
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 */
334 LTTNG_HIDDEN
335 ssize_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;
341 size_t len_last;
342 struct sockaddr_in6 addr = sock->sockaddr.addr.sin6;
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
351 msg.msg_name = (struct sockaddr *) &addr;
352 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
353
354 do {
355 len_last = iov[0].iov_len;
356 ret = recvmsg(sock->fd, &msg, flags);
357 if (ret > 0) {
358 if (flags & MSG_DONTWAIT) {
359 goto end;
360 }
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));
366 if (ret < 0) {
367 PERROR("recvmsg inet");
368 } else if (ret > 0) {
369 ret = len;
370 }
371 /* Else ret = 0 meaning an orderly shutdown. */
372 end:
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 */
381 LTTNG_HIDDEN
382 ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, const void *buf,
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
391 iov[0].iov_base = (void *) buf;
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:
398 {
399 struct sockaddr_in6 addr = sock->sockaddr.addr.sin6;
400
401 msg.msg_name = (struct sockaddr *) &addr;
402 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
403 break;
404 }
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 */
428 LTTNG_HIDDEN
429 int lttcomm_close_inet6_sock(struct lttcomm_sock *sock)
430 {
431 int ret;
432
433 /* Don't try to close an invalid marked socket */
434 if (sock->fd == -1) {
435 return 0;
436 }
437
438 ret = close(sock->fd);
439 if (ret) {
440 PERROR("close inet6");
441 }
442
443 /* Mark socket */
444 sock->fd = -1;
445
446 return ret;
447 }
This page took 0.037782 seconds and 4 git commands to generate.