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