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