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