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.
45 int lttcomm_create_inet6_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
48 unsigned long timeout
;
50 /* Create server socket */
51 if ((sock
->fd
= socket(PF_INET6
, type
, proto
)) < 0) {
52 PERROR("socket inet6");
56 sock
->ops
= &inet6_ops
;
59 * Set socket option to reuse the address.
61 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
63 PERROR("setsockopt inet6");
66 timeout
= lttcomm_get_network_timeout();
68 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
72 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
85 * Bind socket and return.
88 int lttcomm_bind_inet6_sock(struct lttcomm_sock
*sock
)
91 (const struct sockaddr
*) ALIGNED_CONST_PTR(
92 sock
->sockaddr
.addr
.sin6
),
93 sizeof(sock
->sockaddr
.addr
.sin6
));
97 int connect_no_timeout(struct lttcomm_sock
*sock
)
99 return connect(sock
->fd
,
100 (const struct sockaddr
*) ALIGNED_CONST_PTR(
101 sock
->sockaddr
.addr
.sin6
),
102 sizeof(sock
->sockaddr
.addr
.sin6
));
106 int connect_with_timeout(struct lttcomm_sock
*sock
)
108 unsigned long timeout
= lttcomm_get_network_timeout();
109 int ret
, flags
, connect_ret
;
110 struct timespec orig_time
, cur_time
;
111 unsigned long diff_ms
;
113 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
120 /* Set socket to nonblock */
121 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
127 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
129 PERROR("clock_gettime");
133 connect_ret
= connect(sock
->fd
,
134 (const struct sockaddr
*) ALIGNED_CONST_PTR(
135 sock
->sockaddr
.addr
.sin6
),
136 sizeof(sock
->sockaddr
.addr
.sin6
));
137 if (connect_ret
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&&
138 errno
!= EINPROGRESS
) {
140 } else if (!connect_ret
) {
141 /* Connect succeeded */
145 DBG("Asynchronous connect for sock %d, performing polling with"
146 " timeout: %lums", sock
->fd
, timeout
);
149 * Perform poll loop following EINPROGRESS recommendation from
150 * connect(2) man page.
156 fds
.events
= POLLOUT
;
158 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
161 } else if (ret
> 0) {
163 socklen_t optval_len
= sizeof(optval
);
165 if (!(fds
.revents
& POLLOUT
)) {
166 /* Either hup or error */
171 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
172 SO_ERROR
, &optval
, &optval_len
);
174 PERROR("getsockopt");
181 /* Get actual connect() errno from opt_val */
186 /* ret == 0: timeout */
187 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
189 PERROR("clock_gettime");
193 if (timespec_to_ms(timespec_abs_diff(cur_time
, orig_time
), &diff_ms
) < 0) {
194 ERR("timespec_to_ms input overflows milliseconds output");
198 } while (diff_ms
< timeout
);
205 /* Restore initial flags */
206 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
209 /* Continue anyway */
216 * Connect PF_INET socket.
219 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
223 if (lttcomm_get_network_timeout()) {
224 ret
= connect_with_timeout(sock
);
226 ret
= connect_no_timeout(sock
);
229 PERROR("connect inet6");
236 closeret
= close(sock
->fd
);
238 PERROR("close inet6");
245 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
246 * MUST be bind(2) before.
249 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
253 struct lttcomm_sock
*new_sock
;
254 struct sockaddr_in6 new_addr
= {};
256 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
258 * accept(2) does not exist for UDP so simply return the passed socket.
264 new_sock
= lttcomm_alloc_sock(sock
->proto
);
265 if (new_sock
== NULL
) {
269 len
= sizeof(new_addr
);
272 new_fd
= accept(sock
->fd
, (struct sockaddr
*) &new_addr
, &len
);
274 PERROR("accept inet6");
277 new_sock
->sockaddr
.addr
.sin6
= new_addr
;
278 new_sock
->fd
= new_fd
;
279 new_sock
->ops
= &inet6_ops
;
290 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
293 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
297 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
298 /* listen(2) does not exist for UDP so simply return success. */
303 /* Default listen backlog */
305 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
308 ret
= listen(sock
->fd
, backlog
);
310 PERROR("listen inet6");
318 * Receive data of size len in put that data into the buf param. Using recvmsg
321 * Return the size of received data.
324 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
325 size_t len
, int flags
)
331 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
333 memset(&msg
, 0, sizeof(msg
));
335 iov
[0].iov_base
= buf
;
336 iov
[0].iov_len
= len
;
340 msg
.msg_name
= (struct sockaddr
*) &addr
;
341 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
344 len_last
= iov
[0].iov_len
;
345 ret
= recvmsg(sock
->fd
, &msg
, flags
);
347 if (flags
& MSG_DONTWAIT
) {
350 iov
[0].iov_base
+= ret
;
351 iov
[0].iov_len
-= ret
;
352 LTTNG_ASSERT(ret
<= len_last
);
354 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
356 PERROR("recvmsg inet");
357 } else if (ret
> 0) {
360 /* Else ret = 0 meaning an orderly shutdown. */
366 * Send buf data of size len. Using sendmsg API.
368 * Return the size of sent data.
371 ssize_t
lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, const void *buf
,
372 size_t len
, int flags
)
378 memset(&msg
, 0, sizeof(msg
));
380 iov
[0].iov_base
= (void *) buf
;
381 iov
[0].iov_len
= len
;
385 switch (sock
->proto
) {
386 case LTTCOMM_SOCK_UDP
:
388 struct sockaddr_in6 addr
= sock
->sockaddr
.addr
.sin6
;
390 msg
.msg_name
= (struct sockaddr
*) &addr
;
391 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
399 ret
= sendmsg(sock
->fd
, &msg
, flags
);
400 } while (ret
< 0 && errno
== EINTR
);
403 * Only warn about EPIPE when quiet mode is deactivated.
404 * We consider EPIPE as expected.
406 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
407 PERROR("sendmsg inet6");
415 * Shutdown cleanly and close.
418 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
422 /* Don't try to close an invalid marked socket */
423 if (sock
->fd
== -1) {
427 ret
= close(sock
->fd
);
429 PERROR("close inet6");