Fix: define _LGPL_SOURCE in C files
[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
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
6364a07a
DG
20#include <assert.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
28#include <errno.h>
a655f4cf
MD
29#include <fcntl.h>
30#include <time.h>
31#include <poll.h>
6364a07a 32
90e535ef 33#include <common/common.h>
6364a07a
DG
34
35#include "inet6.h"
36
a655f4cf
MD
37#define MSEC_PER_SEC 1000
38#define NSEC_PER_MSEC 1000000
39#define RECONNECT_DELAY 200 /* ms */
40
6364a07a
DG
41/*
42 * INET protocol operations.
43 */
44static const struct lttcomm_proto_ops inet6_ops = {
45 .bind = lttcomm_bind_inet6_sock,
46 .close = lttcomm_close_inet6_sock,
47 .connect = lttcomm_connect_inet6_sock,
48 .accept = lttcomm_accept_inet6_sock,
49 .listen = lttcomm_listen_inet6_sock,
50 .recvmsg = lttcomm_recvmsg_inet6_sock,
51 .sendmsg = lttcomm_sendmsg_inet6_sock,
52};
53
54/*
55 * Creates an PF_INET socket.
56 */
90e535ef 57LTTNG_HIDDEN
6364a07a
DG
58int lttcomm_create_inet6_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 */
7b43086d 64 if ((sock->fd = socket(PF_INET6, type, proto)) < 0) {
6364a07a
DG
65 PERROR("socket inet6");
66 goto error;
67 }
68
69 sock->ops = &inet6_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 inet6");
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_inet6_sock(struct lttcomm_sock *sock)
102{
103 int ret;
104
105 ret = bind(sock->fd, &sock->sockaddr.addr.sin6,
106 sizeof(sock->sockaddr.addr.sin6));
107 if (ret < 0) {
108 PERROR("bind inet6");
109 }
110
111 return ret;
112}
113
a655f4cf
MD
114static
115int connect_no_timeout(struct lttcomm_sock *sock)
116{
117 return connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin6,
118 sizeof(sock->sockaddr.addr.sin6));
119}
120
121/*
122 * Return time_a - time_b in milliseconds.
123 */
124static
125unsigned long time_diff_ms(struct timespec *time_a,
126 struct timespec *time_b)
127{
128 time_t sec_diff;
129 long nsec_diff;
130 unsigned long result_ms;
131
132 sec_diff = time_a->tv_sec - time_b->tv_sec;
133 nsec_diff = time_a->tv_nsec - time_b->tv_nsec;
134
135 result_ms = sec_diff * MSEC_PER_SEC;
136 result_ms += nsec_diff / NSEC_PER_MSEC;
137 return result_ms;
138}
139
140static
141int connect_with_timeout(struct lttcomm_sock *sock)
142{
143 unsigned long timeout = lttcomm_get_network_timeout();
144 int ret, flags, connect_ret;
145 struct timespec orig_time, cur_time;
146
147 ret = fcntl(sock->fd, F_GETFL, 0);
148 if (ret == -1) {
149 PERROR("fcntl");
150 return -1;
151 }
152 flags = ret;
153
154 /* Set socket to nonblock */
155 ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK);
156 if (ret == -1) {
157 PERROR("fcntl");
158 return -1;
159 }
160
161 ret = clock_gettime(CLOCK_MONOTONIC, &orig_time);
162 if (ret == -1) {
163 PERROR("clock_gettime");
164 return -1;
165 }
166
167 connect_ret = connect(sock->fd,
168 (struct sockaddr *) &sock->sockaddr.addr.sin6,
169 sizeof(sock->sockaddr.addr.sin6));
170 if (connect_ret == -1 && errno != EAGAIN
171 && errno != EWOULDBLOCK
172 && errno != EINPROGRESS) {
173 goto error;
174 } else if (!connect_ret) {
175 /* Connect succeeded */
176 goto success;
177 }
178
179 /*
180 * Perform poll loop following EINPROGRESS recommendation from
181 * connect(2) man page.
182 */
183 do {
184 struct pollfd fds;
185
186 fds.fd = sock->fd;
187 fds.events = POLLOUT;
188 fds.revents = 0;
189 ret = poll(&fds, 1, RECONNECT_DELAY);
190 if (ret < 0) {
191 goto error;
192 } else if (ret > 0) {
193 int optval;
194 socklen_t optval_len = sizeof(optval);
195
196 if (!(fds.revents & POLLOUT)) {
197 /* Either hup or error */
198 errno = EPIPE;
199 goto error;
200 }
201 /* got something */
202 ret = getsockopt(sock->fd, SOL_SOCKET,
203 SO_ERROR, &optval, &optval_len);
204 if (ret) {
205 goto error;
206 }
207 if (!optval) {
208 connect_ret = 0;
209 goto success;
210 } else {
211 goto error;
212 }
213 }
214 /* ret == 0: timeout */
215 ret = clock_gettime(CLOCK_MONOTONIC, &cur_time);
216 if (ret == -1) {
217 PERROR("clock_gettime");
218 connect_ret = ret;
219 goto error;
220 }
221 } while (time_diff_ms(&cur_time, &orig_time) < timeout);
222
223 /* Timeout */
224 errno = ETIMEDOUT;
225 connect_ret = -1;
226
227success:
228 /* Restore initial flags */
229 ret = fcntl(sock->fd, F_SETFL, flags);
230 if (ret == -1) {
231 PERROR("fcntl");
232 /* Continue anyway */
233 }
234error:
235 return connect_ret;
236}
237
6364a07a
DG
238/*
239 * Connect PF_INET socket.
240 */
90e535ef 241LTTNG_HIDDEN
6364a07a
DG
242int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock)
243{
244 int ret, closeret;
245
a655f4cf
MD
246 if (lttcomm_get_network_timeout()) {
247 ret = connect_with_timeout(sock);
248 } else {
249 ret = connect_no_timeout(sock);
250 }
6364a07a 251 if (ret < 0) {
82c05d47 252 PERROR("connect inet6");
6364a07a
DG
253 goto error_connect;
254 }
255
256 return ret;
257
258error_connect:
259 closeret = close(sock->fd);
260 if (closeret) {
261 PERROR("close inet6");
262 }
263
264 return ret;
265}
266
267/*
268 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
269 * MUST be bind(2) before.
270 */
90e535ef 271LTTNG_HIDDEN
6364a07a
DG
272struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock)
273{
274 int new_fd;
88a5db70 275 socklen_t len;
6364a07a
DG
276 struct lttcomm_sock *new_sock;
277
278 if (sock->proto == LTTCOMM_SOCK_UDP) {
279 /*
280 * accept(2) does not exist for UDP so simply return the passed socket.
281 */
282 new_sock = sock;
283 goto end;
284 }
285
de5e9086 286 new_sock = lttcomm_alloc_sock(sock->proto);
6364a07a
DG
287 if (new_sock == NULL) {
288 goto error;
289 }
290
88a5db70
DG
291 len = sizeof(new_sock->sockaddr.addr.sin6);
292
6364a07a
DG
293 /* Blocking call */
294 new_fd = accept(sock->fd,
295 (struct sockaddr *) &new_sock->sockaddr.addr.sin6, &len);
296 if (new_fd < 0) {
297 PERROR("accept inet6");
298 goto error;
299 }
300
301 new_sock->fd = new_fd;
de5e9086 302 new_sock->ops = &inet6_ops;
6364a07a
DG
303
304end:
305 return new_sock;
306
307error:
308 free(new_sock);
309 return NULL;
310}
311
312/*
313 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
314 */
90e535ef 315LTTNG_HIDDEN
6364a07a
DG
316int lttcomm_listen_inet6_sock(struct lttcomm_sock *sock, int backlog)
317{
318 int ret;
319
320 if (sock->proto == LTTCOMM_SOCK_UDP) {
321 /* listen(2) does not exist for UDP so simply return success. */
322 ret = 0;
323 goto end;
324 }
325
326 /* Default listen backlog */
327 if (backlog <= 0) {
328 backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN;
329 }
330
331 ret = listen(sock->fd, backlog);
332 if (ret < 0) {
333 PERROR("listen inet6");
334 }
335
336end:
337 return ret;
338}
339
340/*
341 * Receive data of size len in put that data into the buf param. Using recvmsg
342 * API.
343 *
344 * Return the size of received data.
345 */
90e535ef 346LTTNG_HIDDEN
6364a07a
DG
347ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf,
348 size_t len, int flags)
349{
350 struct msghdr msg;
351 struct iovec iov[1];
352 ssize_t ret = -1;
7c5aef62 353 size_t len_last;
6364a07a
DG
354
355 memset(&msg, 0, sizeof(msg));
356
357 iov[0].iov_base = buf;
358 iov[0].iov_len = len;
359 msg.msg_iov = iov;
360 msg.msg_iovlen = 1;
361
362 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6;
363 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
364
6364a07a 365 do {
7c5aef62 366 len_last = iov[0].iov_len;
6364a07a 367 ret = recvmsg(sock->fd, &msg, flags);
7c5aef62
DG
368 if (ret > 0) {
369 iov[0].iov_base += ret;
370 iov[0].iov_len -= ret;
371 assert(ret <= len_last);
372 }
373 } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR));
6364a07a 374 if (ret < 0) {
7c5aef62
DG
375 PERROR("recvmsg inet");
376 } else if (ret > 0) {
377 ret = len;
6364a07a 378 }
7c5aef62 379 /* Else ret = 0 meaning an orderly shutdown. */
6364a07a
DG
380
381 return ret;
382}
383
384/*
385 * Send buf data of size len. Using sendmsg API.
386 *
387 * Return the size of sent data.
388 */
90e535ef 389LTTNG_HIDDEN
6364a07a
DG
390ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, void *buf,
391 size_t len, int flags)
392{
393 struct msghdr msg;
394 struct iovec iov[1];
395 ssize_t ret = -1;
396
397 memset(&msg, 0, sizeof(msg));
398
399 iov[0].iov_base = buf;
400 iov[0].iov_len = len;
401 msg.msg_iov = iov;
402 msg.msg_iovlen = 1;
403
404 switch (sock->proto) {
405 case LTTCOMM_SOCK_UDP:
406 msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6;
407 msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6);
408 break;
409 default:
410 break;
411 }
412
413 do {
414 ret = sendmsg(sock->fd, &msg, flags);
415 } while (ret < 0 && errno == EINTR);
416 if (ret < 0) {
417 /*
418 * Only warn about EPIPE when quiet mode is deactivated.
419 * We consider EPIPE as expected.
420 */
421 if (errno != EPIPE || !lttng_opt_quiet) {
422 PERROR("sendmsg inet6");
423 }
424 }
425
426 return ret;
427}
428
429/*
430 * Shutdown cleanly and close.
431 */
90e535ef 432LTTNG_HIDDEN
6364a07a
DG
433int lttcomm_close_inet6_sock(struct lttcomm_sock *sock)
434{
6e742359 435 int ret;
6364a07a 436
de5e9086 437 /* Don't try to close an invalid marked socket */
6364a07a
DG
438 if (sock->fd == -1) {
439 return 0;
440 }
441
6e742359
DG
442 ret = close(sock->fd);
443 if (ret) {
6364a07a
DG
444 PERROR("close inet6");
445 }
446
447 /* Mark socket */
448 sock->fd = -1;
449
450 return ret;
451}
This page took 0.058623 seconds and 5 git commands to generate.