2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
29 #include <common/compat/time.h>
32 #include <common/common.h>
33 #include <common/time.h>
37 #define RECONNECT_DELAY 200 /* ms */
40 * INET protocol operations.
42 static const struct lttcomm_proto_ops inet6_ops
= {
43 .bind
= lttcomm_bind_inet6_sock
,
44 .close
= lttcomm_close_inet6_sock
,
45 .connect
= lttcomm_connect_inet6_sock
,
46 .accept
= lttcomm_accept_inet6_sock
,
47 .listen
= lttcomm_listen_inet6_sock
,
48 .recvmsg
= lttcomm_recvmsg_inet6_sock
,
49 .sendmsg
= lttcomm_sendmsg_inet6_sock
,
53 * Creates an PF_INET socket.
56 int lttcomm_create_inet6_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
59 unsigned long timeout
;
61 /* Create server socket */
62 if ((sock
->fd
= socket(PF_INET6
, type
, proto
)) < 0) {
63 PERROR("socket inet6");
67 sock
->ops
= &inet6_ops
;
70 * Set socket option to reuse the address.
72 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
74 PERROR("setsockopt inet6");
77 timeout
= lttcomm_get_network_timeout();
79 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
83 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
96 * Bind socket and return.
99 int lttcomm_bind_inet6_sock(struct lttcomm_sock
*sock
)
101 return bind(sock
->fd
,
102 (const struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
103 sizeof(sock
->sockaddr
.addr
.sin6
));
107 int connect_no_timeout(struct lttcomm_sock
*sock
)
109 return connect(sock
->fd
, (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
110 sizeof(sock
->sockaddr
.addr
.sin6
));
114 * Return time_a - time_b in milliseconds.
117 unsigned long time_diff_ms(struct timespec
*time_a
,
118 struct timespec
*time_b
)
122 unsigned long result_ms
;
124 sec_diff
= time_a
->tv_sec
- time_b
->tv_sec
;
125 nsec_diff
= time_a
->tv_nsec
- time_b
->tv_nsec
;
127 result_ms
= sec_diff
* MSEC_PER_SEC
;
128 result_ms
+= nsec_diff
/ NSEC_PER_MSEC
;
133 int connect_with_timeout(struct lttcomm_sock
*sock
)
135 unsigned long timeout
= lttcomm_get_network_timeout();
136 int ret
, flags
, connect_ret
;
137 struct timespec orig_time
, cur_time
;
139 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
146 /* Set socket to nonblock */
147 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
153 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
155 PERROR("clock_gettime");
159 connect_ret
= connect(sock
->fd
,
160 (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
161 sizeof(sock
->sockaddr
.addr
.sin6
));
162 if (connect_ret
== -1 && errno
!= EAGAIN
163 && errno
!= EWOULDBLOCK
164 && errno
!= EINPROGRESS
) {
166 } else if (!connect_ret
) {
167 /* Connect succeeded */
171 DBG("Asynchronous connect for sock %d, performing polling with"
172 " timeout: %lums", sock
->fd
, timeout
);
175 * Perform poll loop following EINPROGRESS recommendation from
176 * connect(2) man page.
182 fds
.events
= POLLOUT
;
184 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
187 } else if (ret
> 0) {
189 socklen_t optval_len
= sizeof(optval
);
191 if (!(fds
.revents
& POLLOUT
)) {
192 /* Either hup or error */
197 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
198 SO_ERROR
, &optval
, &optval_len
);
200 PERROR("getsockopt");
207 /* Get actual connect() errno from opt_val */
212 /* ret == 0: timeout */
213 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
215 PERROR("clock_gettime");
219 } while (time_diff_ms(&cur_time
, &orig_time
) < timeout
);
226 /* Restore initial flags */
227 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
230 /* Continue anyway */
237 * Connect PF_INET socket.
240 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
244 if (lttcomm_get_network_timeout()) {
245 ret
= connect_with_timeout(sock
);
247 ret
= connect_no_timeout(sock
);
250 PERROR("connect inet6");
257 closeret
= close(sock
->fd
);
259 PERROR("close inet6");
266 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
267 * MUST be bind(2) before.
270 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
274 struct lttcomm_sock
*new_sock
;
276 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
278 * accept(2) does not exist for UDP so simply return the passed socket.
284 new_sock
= lttcomm_alloc_sock(sock
->proto
);
285 if (new_sock
== NULL
) {
289 len
= sizeof(new_sock
->sockaddr
.addr
.sin6
);
292 new_fd
= accept(sock
->fd
,
293 (struct sockaddr
*) &new_sock
->sockaddr
.addr
.sin6
, &len
);
295 PERROR("accept inet6");
299 new_sock
->fd
= new_fd
;
300 new_sock
->ops
= &inet6_ops
;
311 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
314 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
318 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
319 /* listen(2) does not exist for UDP so simply return success. */
324 /* Default listen backlog */
326 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
329 ret
= listen(sock
->fd
, backlog
);
331 PERROR("listen inet6");
339 * Receive data of size len in put that data into the buf param. Using recvmsg
342 * Return the size of received data.
345 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
346 size_t len
, int flags
)
353 memset(&msg
, 0, sizeof(msg
));
355 iov
[0].iov_base
= buf
;
356 iov
[0].iov_len
= len
;
360 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
361 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
364 len_last
= iov
[0].iov_len
;
365 ret
= recvmsg(sock
->fd
, &msg
, flags
);
367 if (flags
& MSG_DONTWAIT
) {
370 iov
[0].iov_base
+= ret
;
371 iov
[0].iov_len
-= ret
;
372 assert(ret
<= len_last
);
374 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
376 PERROR("recvmsg inet");
377 } else if (ret
> 0) {
380 /* Else ret = 0 meaning an orderly shutdown. */
386 * Send buf data of size len. Using sendmsg API.
388 * Return the size of sent data.
391 ssize_t
lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, const void *buf
,
392 size_t len
, int flags
)
398 memset(&msg
, 0, sizeof(msg
));
400 iov
[0].iov_base
= (void *) buf
;
401 iov
[0].iov_len
= len
;
405 switch (sock
->proto
) {
406 case LTTCOMM_SOCK_UDP
:
407 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
408 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
415 ret
= sendmsg(sock
->fd
, &msg
, flags
);
416 } while (ret
< 0 && errno
== EINTR
);
419 * Only warn about EPIPE when quiet mode is deactivated.
420 * We consider EPIPE as expected.
422 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
423 PERROR("sendmsg inet6");
431 * Shutdown cleanly and close.
434 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
438 /* Don't try to close an invalid marked socket */
439 if (sock
->fd
== -1) {
443 ret
= close(sock
->fd
);
445 PERROR("close inet6");