2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/compat/time.h>
20 #include <common/common.h>
21 #include <common/time.h>
22 #include <common/compat/errno.h>
26 #define RECONNECT_DELAY 200 /* ms */
29 * INET protocol operations.
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
,
42 * Creates an PF_INET socket.
44 int lttcomm_create_inet6_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
47 unsigned long timeout
;
49 /* Create server socket */
50 if ((sock
->fd
= socket(PF_INET6
, type
, proto
)) < 0) {
51 PERROR("socket inet6");
55 sock
->ops
= &inet6_ops
;
58 * Set socket option to reuse the address.
60 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
62 PERROR("setsockopt inet6");
65 timeout
= lttcomm_get_network_timeout();
67 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
71 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
84 * Bind socket and return.
86 int lttcomm_bind_inet6_sock(struct lttcomm_sock
*sock
)
89 (const struct sockaddr
*) ALIGNED_CONST_PTR(
90 sock
->sockaddr
.addr
.sin6
),
91 sizeof(sock
->sockaddr
.addr
.sin6
));
95 int connect_no_timeout(struct lttcomm_sock
*sock
)
97 return connect(sock
->fd
,
98 (const struct sockaddr
*) ALIGNED_CONST_PTR(
99 sock
->sockaddr
.addr
.sin6
),
100 sizeof(sock
->sockaddr
.addr
.sin6
));
104 int connect_with_timeout(struct lttcomm_sock
*sock
)
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
;
111 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
118 /* Set socket to nonblock */
119 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
125 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
127 PERROR("clock_gettime");
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
) {
138 } else if (!connect_ret
) {
139 /* Connect succeeded */
143 DBG("Asynchronous connect for sock %d, performing polling with"
144 " timeout: %lums", sock
->fd
, timeout
);
147 * Perform poll loop following EINPROGRESS recommendation from
148 * connect(2) man page.
154 fds
.events
= POLLOUT
;
156 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
159 } else if (ret
> 0) {
161 socklen_t optval_len
= sizeof(optval
);
163 if (!(fds
.revents
& POLLOUT
)) {
164 /* Either hup or error */
169 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
170 SO_ERROR
, &optval
, &optval_len
);
172 PERROR("getsockopt");
179 /* Get actual connect() errno from opt_val */
184 /* ret == 0: timeout */
185 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
187 PERROR("clock_gettime");
191 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
192 ERR("timespec_to_ms input overflows milliseconds output");
196 } while (diff_ms
< timeout
);
203 /* Restore initial flags */
204 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
207 /* Continue anyway */
214 * Connect PF_INET socket.
216 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
220 if (lttcomm_get_network_timeout()) {
221 ret
= connect_with_timeout(sock
);
223 ret
= connect_no_timeout(sock
);
226 PERROR("connect inet6");
233 closeret
= close(sock
->fd
);
235 PERROR("close inet6");
242 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
243 * MUST be bind(2) before.
245 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
249 struct lttcomm_sock
*new_sock
;
250 struct sockaddr_in6 new_addr
= {};
252 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
254 * accept(2) does not exist for UDP so simply return the passed socket.
260 new_sock
= lttcomm_alloc_sock(sock
->proto
);
261 if (new_sock
== NULL
) {
265 len
= sizeof(new_addr
);
268 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
270 PERROR("accept inet6");
273 new_sock
->sockaddr
.addr
.sin6
= new_addr
;
274 new_sock
->fd
= new_fd
;
275 new_sock
->ops
= &inet6_ops
;
286 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
288 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
292 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
293 /* listen(2) does not exist for UDP so simply return success. */
298 /* Default listen backlog */
300 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
303 ret
= listen(sock
->fd
, backlog
);
305 PERROR("listen inet6");
313 * Receive data of size len in put that data into the buf param. Using recvmsg
316 * Return the size of received data.
318 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
319 size_t len
, int flags
)
325 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
327 memset(&msg
, 0, sizeof(msg
));
329 iov
[0].iov_base
= buf
;
330 iov
[0].iov_len
= len
;
334 msg
.msg_name
= (struct sockaddr
*) &addr
;
335 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
338 len_last
= iov
[0].iov_len
;
339 ret
= recvmsg(sock
->fd
, &msg
, flags
);
341 if (flags
& MSG_DONTWAIT
) {
344 iov
[0].iov_base
+= ret
;
345 iov
[0].iov_len
-= ret
;
346 LTTNG_ASSERT(ret
<= len_last
);
348 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
350 PERROR("recvmsg inet");
351 } else if (ret
> 0) {
354 /* Else ret = 0 meaning an orderly shutdown. */
360 * Send buf data of size len. Using sendmsg API.
362 * Return the size of sent data.
364 ssize_t
lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, const void *buf
,
365 size_t len
, int flags
)
371 memset(&msg
, 0, sizeof(msg
));
373 iov
[0].iov_base
= (void *) buf
;
374 iov
[0].iov_len
= len
;
378 switch (sock
->proto
) {
379 case LTTCOMM_SOCK_UDP
:
381 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
383 msg
.msg_name
= (struct sockaddr
*) &addr
;
384 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
392 ret
= sendmsg(sock
->fd
, &msg
, flags
);
393 } while (ret
< 0 && errno
== EINTR
);
396 * Only warn about EPIPE when quiet mode is deactivated.
397 * We consider EPIPE as expected.
399 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
400 PERROR("sendmsg inet6");
408 * Shutdown cleanly and close.
410 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
414 /* Don't try to close an invalid marked socket */
415 if (sock
->fd
== -1) {
419 ret
= close(sock
->fd
);
421 PERROR("close inet6");