2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
18 #include <common/compat/time.h>
21 #include <common/common.h>
22 #include <common/time.h>
23 #include <common/compat/errno.h>
27 #define RECONNECT_DELAY 200 /* ms */
30 * INET protocol operations.
32 static const struct lttcomm_proto_ops inet6_ops
= {
33 .bind
= lttcomm_bind_inet6_sock
,
34 .close
= lttcomm_close_inet6_sock
,
35 .connect
= lttcomm_connect_inet6_sock
,
36 .accept
= lttcomm_accept_inet6_sock
,
37 .listen
= lttcomm_listen_inet6_sock
,
38 .recvmsg
= lttcomm_recvmsg_inet6_sock
,
39 .sendmsg
= lttcomm_sendmsg_inet6_sock
,
43 * Creates an PF_INET socket.
46 int lttcomm_create_inet6_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
49 unsigned long timeout
;
51 /* Create server socket */
52 if ((sock
->fd
= socket(PF_INET6
, type
, proto
)) < 0) {
53 PERROR("socket inet6");
57 sock
->ops
= &inet6_ops
;
60 * Set socket option to reuse the address.
62 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
64 PERROR("setsockopt inet6");
67 timeout
= lttcomm_get_network_timeout();
69 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
73 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
86 * Bind socket and return.
89 int lttcomm_bind_inet6_sock(struct lttcomm_sock
*sock
)
92 (const struct sockaddr
*) ALIGNED_CONST_PTR(
93 sock
->sockaddr
.addr
.sin6
),
94 sizeof(sock
->sockaddr
.addr
.sin6
));
98 int connect_no_timeout(struct lttcomm_sock
*sock
)
100 return connect(sock
->fd
,
101 (const struct sockaddr
*) ALIGNED_CONST_PTR(
102 sock
->sockaddr
.addr
.sin6
),
103 sizeof(sock
->sockaddr
.addr
.sin6
));
107 int connect_with_timeout(struct lttcomm_sock
*sock
)
109 unsigned long timeout
= lttcomm_get_network_timeout();
110 int ret
, flags
, connect_ret
;
111 struct timespec orig_time
, cur_time
;
112 unsigned long diff_ms
;
114 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
121 /* Set socket to nonblock */
122 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
128 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
130 PERROR("clock_gettime");
134 connect_ret
= connect(sock
->fd
,
135 (const struct sockaddr
*) ALIGNED_CONST_PTR(
136 sock
->sockaddr
.addr
.sin6
),
137 sizeof(sock
->sockaddr
.addr
.sin6
));
138 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
139 errno
!= EINPROGRESS
) {
141 } else if (!connect_ret
) {
142 /* Connect succeeded */
146 DBG("Asynchronous connect for sock %d, performing polling with"
147 " timeout: %lums", sock
->fd
, timeout
);
150 * Perform poll loop following EINPROGRESS recommendation from
151 * connect(2) man page.
157 fds
.events
= POLLOUT
;
159 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
162 } else if (ret
> 0) {
164 socklen_t optval_len
= sizeof(optval
);
166 if (!(fds
.revents
& POLLOUT
)) {
167 /* Either hup or error */
172 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
173 SO_ERROR
, &optval
, &optval_len
);
175 PERROR("getsockopt");
182 /* Get actual connect() errno from opt_val */
187 /* ret == 0: timeout */
188 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
190 PERROR("clock_gettime");
194 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
195 ERR("timespec_to_ms input overflows milliseconds output");
199 } while (diff_ms
< timeout
);
206 /* Restore initial flags */
207 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
210 /* Continue anyway */
217 * Connect PF_INET socket.
220 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
224 if (lttcomm_get_network_timeout()) {
225 ret
= connect_with_timeout(sock
);
227 ret
= connect_no_timeout(sock
);
230 PERROR("connect inet6");
237 closeret
= close(sock
->fd
);
239 PERROR("close inet6");
246 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
247 * MUST be bind(2) before.
250 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
254 struct lttcomm_sock
*new_sock
;
255 struct sockaddr_in6 new_addr
= {};
257 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
259 * accept(2) does not exist for UDP so simply return the passed socket.
265 new_sock
= lttcomm_alloc_sock(sock
->proto
);
266 if (new_sock
== NULL
) {
270 len
= sizeof(new_addr
);
273 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
275 PERROR("accept inet6");
278 new_sock
->sockaddr
.addr
.sin6
= new_addr
;
279 new_sock
->fd
= new_fd
;
280 new_sock
->ops
= &inet6_ops
;
291 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
294 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
298 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
299 /* listen(2) does not exist for UDP so simply return success. */
304 /* Default listen backlog */
306 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
309 ret
= listen(sock
->fd
, backlog
);
311 PERROR("listen inet6");
319 * Receive data of size len in put that data into the buf param. Using recvmsg
322 * Return the size of received data.
325 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
326 size_t len
, int flags
)
332 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
334 memset(&msg
, 0, sizeof(msg
));
336 iov
[0].iov_base
= buf
;
337 iov
[0].iov_len
= len
;
341 msg
.msg_name
= (struct sockaddr
*) &addr
;
342 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
345 len_last
= iov
[0].iov_len
;
346 ret
= recvmsg(sock
->fd
, &msg
, flags
);
348 if (flags
& MSG_DONTWAIT
) {
351 iov
[0].iov_base
+= ret
;
352 iov
[0].iov_len
-= ret
;
353 assert(ret
<= len_last
);
355 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
357 PERROR("recvmsg inet");
358 } else if (ret
> 0) {
361 /* Else ret = 0 meaning an orderly shutdown. */
367 * Send buf data of size len. Using sendmsg API.
369 * Return the size of sent data.
372 ssize_t
lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, const void *buf
,
373 size_t len
, int flags
)
379 memset(&msg
, 0, sizeof(msg
));
381 iov
[0].iov_base
= (void *) buf
;
382 iov
[0].iov_len
= len
;
386 switch (sock
->proto
) {
387 case LTTCOMM_SOCK_UDP
:
389 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
391 msg
.msg_name
= (struct sockaddr
*) &addr
;
392 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
400 ret
= sendmsg(sock
->fd
, &msg
, flags
);
401 } while (ret
< 0 && errno
== EINTR
);
404 * Only warn about EPIPE when quiet mode is deactivated.
405 * We consider EPIPE as expected.
407 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
408 PERROR("sendmsg inet6");
416 * Shutdown cleanly and close.
419 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
423 /* Don't try to close an invalid marked socket */
424 if (sock
->fd
== -1) {
428 ret
= close(sock
->fd
);
430 PERROR("close inet6");