2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.hpp>
12 #include <common/compat/errno.hpp>
13 #include <common/compat/time.hpp>
14 #include <common/time.hpp>
23 #include <sys/types.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
)
88 struct sockaddr_in6 sockaddr
= sock
->sockaddr
.addr
.sin6
;
89 return bind(sock
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
));
92 static int connect_no_timeout(struct lttcomm_sock
*sock
)
94 struct sockaddr_in6 sockaddr
= sock
->sockaddr
.addr
.sin6
;
95 return connect(sock
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
));
98 static int connect_with_timeout(struct lttcomm_sock
*sock
)
100 unsigned long timeout
= lttcomm_get_network_timeout();
101 int ret
, flags
, connect_ret
;
102 struct timespec orig_time
, cur_time
;
103 unsigned long diff_ms
;
104 struct sockaddr_in6 sockaddr
;
106 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
113 /* Set socket to nonblock */
114 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
120 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
122 PERROR("clock_gettime");
126 sockaddr
= sock
->sockaddr
.addr
.sin6
;
127 connect_ret
= connect(sock
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
));
128 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&& errno
!= EINPROGRESS
) {
130 } else if (!connect_ret
) {
131 /* Connect succeeded */
135 DBG("Asynchronous connect for sock %d, performing polling with"
141 * Perform poll loop following EINPROGRESS recommendation from
142 * connect(2) man page.
148 fds
.events
= POLLOUT
;
150 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
153 } else if (ret
> 0) {
155 socklen_t optval_len
= sizeof(optval
);
157 if (!(fds
.revents
& POLLOUT
)) {
158 /* Either hup or error */
163 ret
= getsockopt(sock
->fd
, SOL_SOCKET
, SO_ERROR
, &optval
, &optval_len
);
165 PERROR("getsockopt");
172 /* Get actual connect() errno from opt_val */
177 /* ret == 0: timeout */
178 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
180 PERROR("clock_gettime");
184 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
185 ERR("timespec_to_ms input overflows milliseconds output");
189 } while (diff_ms
< timeout
);
196 /* Restore initial flags */
197 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
200 /* Continue anyway */
207 * Connect PF_INET socket.
209 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
213 if (lttcomm_get_network_timeout()) {
214 ret
= connect_with_timeout(sock
);
216 ret
= connect_no_timeout(sock
);
219 PERROR("connect inet6");
226 closeret
= close(sock
->fd
);
228 PERROR("close inet6");
235 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
236 * MUST be bind(2) before.
238 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
242 struct lttcomm_sock
*new_sock
;
243 struct sockaddr_in6 new_addr
= {};
245 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
247 * accept(2) does not exist for UDP so simply return the passed socket.
253 new_sock
= lttcomm_alloc_sock(sock
->proto
);
254 if (new_sock
== nullptr) {
258 len
= sizeof(new_addr
);
261 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
263 PERROR("accept inet6");
266 new_sock
->sockaddr
.addr
.sin6
= new_addr
;
267 new_sock
->fd
= new_fd
;
268 new_sock
->ops
= &inet6_ops
;
279 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
281 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
285 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
286 /* listen(2) does not exist for UDP so simply return success. */
291 /* Default listen backlog */
293 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
296 ret
= listen(sock
->fd
, backlog
);
298 PERROR("listen inet6");
306 * Receive data of size len in put that data into the buf param. Using recvmsg
309 * Return the size of received data.
311 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
, size_t len
, int flags
)
317 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
319 memset(&msg
, 0, sizeof(msg
));
321 iov
[0].iov_base
= buf
;
322 iov
[0].iov_len
= len
;
326 msg
.msg_name
= (struct sockaddr
*) &addr
;
327 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
330 len_last
= iov
[0].iov_len
;
331 ret
= recvmsg(sock
->fd
, &msg
, flags
);
333 if (flags
& MSG_DONTWAIT
) {
336 iov
[0].iov_base
= ((char *) iov
[0].iov_base
) + ret
;
337 iov
[0].iov_len
-= ret
;
338 LTTNG_ASSERT(ret
<= len_last
);
340 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
342 PERROR("recvmsg inet");
343 } else if (ret
> 0) {
346 /* Else ret = 0 meaning an orderly shutdown. */
352 * Send buf data of size len. Using sendmsg API.
354 * Return the size of sent data.
357 lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, const void *buf
, size_t len
, int flags
)
363 memset(&msg
, 0, sizeof(msg
));
365 iov
[0].iov_base
= (void *) buf
;
366 iov
[0].iov_len
= len
;
370 switch (sock
->proto
) {
371 case LTTCOMM_SOCK_UDP
:
373 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
375 msg
.msg_name
= (struct sockaddr
*) &addr
;
376 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
384 ret
= sendmsg(sock
->fd
, &msg
, flags
);
385 } while (ret
< 0 && errno
== EINTR
);
388 * Only warn about EPIPE when quiet mode is deactivated.
389 * We consider EPIPE as expected.
391 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
392 PERROR("sendmsg inet6");
400 * Shutdown cleanly and close.
402 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
406 /* Don't try to close an invalid marked socket */
407 if (sock
->fd
== -1) {
411 ret
= close(sock
->fd
);
413 PERROR("close inet6");