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>
32 #include <common/common.h>
36 #define MSEC_PER_SEC 1000
37 #define NSEC_PER_MSEC 1000000
38 #define RECONNECT_DELAY 200 /* ms */
41 * INET protocol operations.
43 static const struct lttcomm_proto_ops inet6_ops
= {
44 .bind
= lttcomm_bind_inet6_sock
,
45 .close
= lttcomm_close_inet6_sock
,
46 .connect
= lttcomm_connect_inet6_sock
,
47 .accept
= lttcomm_accept_inet6_sock
,
48 .listen
= lttcomm_listen_inet6_sock
,
49 .recvmsg
= lttcomm_recvmsg_inet6_sock
,
50 .sendmsg
= lttcomm_sendmsg_inet6_sock
,
54 * Creates an PF_INET socket.
57 int lttcomm_create_inet6_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
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");
85 * Bind socket and return.
88 int lttcomm_bind_inet6_sock(struct lttcomm_sock
*sock
)
92 ret
= bind(sock
->fd
, &sock
->sockaddr
.addr
.sin6
,
93 sizeof(sock
->sockaddr
.addr
.sin6
));
102 int connect_no_timeout(struct lttcomm_sock
*sock
)
104 return connect(sock
->fd
, (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
105 sizeof(sock
->sockaddr
.addr
.sin6
));
109 * Return time_a - time_b in milliseconds.
112 unsigned long time_diff_ms(struct timespec
*time_a
,
113 struct timespec
*time_b
)
117 unsigned long result_ms
;
119 sec_diff
= time_a
->tv_sec
- time_b
->tv_sec
;
120 nsec_diff
= time_a
->tv_nsec
- time_b
->tv_nsec
;
122 result_ms
= sec_diff
* MSEC_PER_SEC
;
123 result_ms
+= nsec_diff
/ NSEC_PER_MSEC
;
128 int connect_with_timeout(struct lttcomm_sock
*sock
)
130 unsigned long timeout
= lttcomm_get_network_timeout();
131 int ret
, flags
, connect_ret
;
132 struct timespec orig_time
, cur_time
;
134 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
141 /* Set socket to nonblock */
142 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
148 ret
= clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
150 PERROR("clock_gettime");
154 connect_ret
= connect(sock
->fd
,
155 (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
156 sizeof(sock
->sockaddr
.addr
.sin6
));
157 if (connect_ret
== -1 && errno
!= EAGAIN
158 && errno
!= EWOULDBLOCK
159 && errno
!= EINPROGRESS
) {
161 } else if (!connect_ret
) {
162 /* Connect succeeded */
167 * Perform poll loop following EINPROGRESS recommendation from
168 * connect(2) man page.
174 fds
.events
= POLLOUT
;
176 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
179 } else if (ret
> 0) {
181 socklen_t optval_len
= sizeof(optval
);
183 if (!(fds
.revents
& POLLOUT
)) {
184 /* Either hup or error */
189 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
190 SO_ERROR
, &optval
, &optval_len
);
201 /* ret == 0: timeout */
202 ret
= clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
204 PERROR("clock_gettime");
208 } while (time_diff_ms(&cur_time
, &orig_time
) < timeout
);
215 /* Restore initial flags */
216 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
219 /* Continue anyway */
226 * Connect PF_INET socket.
229 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
233 if (lttcomm_get_network_timeout()) {
234 ret
= connect_with_timeout(sock
);
236 ret
= connect_no_timeout(sock
);
239 PERROR("connect inet6");
246 closeret
= close(sock
->fd
);
248 PERROR("close inet6");
255 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
256 * MUST be bind(2) before.
259 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
263 struct lttcomm_sock
*new_sock
;
265 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
267 * accept(2) does not exist for UDP so simply return the passed socket.
273 new_sock
= lttcomm_alloc_sock(sock
->proto
);
274 if (new_sock
== NULL
) {
278 len
= sizeof(new_sock
->sockaddr
.addr
.sin6
);
281 new_fd
= accept(sock
->fd
,
282 (struct sockaddr
*) &new_sock
->sockaddr
.addr
.sin6
, &len
);
284 PERROR("accept inet6");
288 new_sock
->fd
= new_fd
;
289 new_sock
->ops
= &inet6_ops
;
300 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
303 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
307 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
308 /* listen(2) does not exist for UDP so simply return success. */
313 /* Default listen backlog */
315 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
318 ret
= listen(sock
->fd
, backlog
);
320 PERROR("listen inet6");
328 * Receive data of size len in put that data into the buf param. Using recvmsg
331 * Return the size of received data.
334 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
335 size_t len
, int flags
)
342 memset(&msg
, 0, sizeof(msg
));
344 iov
[0].iov_base
= buf
;
345 iov
[0].iov_len
= len
;
349 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
350 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
353 len_last
= iov
[0].iov_len
;
354 ret
= recvmsg(sock
->fd
, &msg
, flags
);
356 iov
[0].iov_base
+= ret
;
357 iov
[0].iov_len
-= ret
;
358 assert(ret
<= len_last
);
360 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
362 PERROR("recvmsg inet");
363 } else if (ret
> 0) {
366 /* Else ret = 0 meaning an orderly shutdown. */
372 * Send buf data of size len. Using sendmsg API.
374 * Return the size of sent data.
377 ssize_t
lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
378 size_t len
, int flags
)
384 memset(&msg
, 0, sizeof(msg
));
386 iov
[0].iov_base
= buf
;
387 iov
[0].iov_len
= len
;
391 switch (sock
->proto
) {
392 case LTTCOMM_SOCK_UDP
:
393 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
394 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
401 ret
= sendmsg(sock
->fd
, &msg
, flags
);
402 } while (ret
< 0 && errno
== EINTR
);
405 * Only warn about EPIPE when quiet mode is deactivated.
406 * We consider EPIPE as expected.
408 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
409 PERROR("sendmsg inet6");
417 * Shutdown cleanly and close.
420 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
424 /* Don't try to close an invalid marked socket */
425 if (sock
->fd
== -1) {
429 ret
= close(sock
->fd
);
431 PERROR("close inet6");