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