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.
26 #include <sys/types.h>
33 #include <common/common.h>
37 #define MSEC_PER_SEC 1000
38 #define NSEC_PER_MSEC 1000000
39 #define RECONNECT_DELAY 200 /* ms */
42 * INET protocol operations.
44 static const struct lttcomm_proto_ops inet6_ops
= {
45 .bind
= lttcomm_bind_inet6_sock
,
46 .close
= lttcomm_close_inet6_sock
,
47 .connect
= lttcomm_connect_inet6_sock
,
48 .accept
= lttcomm_accept_inet6_sock
,
49 .listen
= lttcomm_listen_inet6_sock
,
50 .recvmsg
= lttcomm_recvmsg_inet6_sock
,
51 .sendmsg
= lttcomm_sendmsg_inet6_sock
,
55 * Creates an PF_INET socket.
58 int lttcomm_create_inet6_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
61 unsigned long timeout
;
63 /* Create server socket */
64 if ((sock
->fd
= socket(PF_INET6
, type
, proto
)) < 0) {
65 PERROR("socket inet6");
69 sock
->ops
= &inet6_ops
;
72 * Set socket option to reuse the address.
74 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
76 PERROR("setsockopt inet6");
79 timeout
= lttcomm_get_network_timeout();
81 ret
= lttcomm_setsockopt_rcv_timeout(sock
->fd
, timeout
);
85 ret
= lttcomm_setsockopt_snd_timeout(sock
->fd
, timeout
);
98 * Bind socket and return.
101 int lttcomm_bind_inet6_sock(struct lttcomm_sock
*sock
)
105 ret
= bind(sock
->fd
, &sock
->sockaddr
.addr
.sin6
,
106 sizeof(sock
->sockaddr
.addr
.sin6
));
108 PERROR("bind inet6");
115 int connect_no_timeout(struct lttcomm_sock
*sock
)
117 return connect(sock
->fd
, (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
118 sizeof(sock
->sockaddr
.addr
.sin6
));
122 * Return time_a - time_b in milliseconds.
125 unsigned long time_diff_ms(struct timespec
*time_a
,
126 struct timespec
*time_b
)
130 unsigned long result_ms
;
132 sec_diff
= time_a
->tv_sec
- time_b
->tv_sec
;
133 nsec_diff
= time_a
->tv_nsec
- time_b
->tv_nsec
;
135 result_ms
= sec_diff
* MSEC_PER_SEC
;
136 result_ms
+= nsec_diff
/ NSEC_PER_MSEC
;
141 int connect_with_timeout(struct lttcomm_sock
*sock
)
143 unsigned long timeout
= lttcomm_get_network_timeout();
144 int ret
, flags
, connect_ret
;
145 struct timespec orig_time
, cur_time
;
147 ret
= fcntl(sock
->fd
, F_GETFL
, 0);
154 /* Set socket to nonblock */
155 ret
= fcntl(sock
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
161 ret
= clock_gettime(CLOCK_MONOTONIC
, &orig_time
);
163 PERROR("clock_gettime");
167 connect_ret
= connect(sock
->fd
,
168 (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
169 sizeof(sock
->sockaddr
.addr
.sin6
));
170 if (connect_ret
== -1 && errno
!= EAGAIN
171 && errno
!= EWOULDBLOCK
172 && errno
!= EINPROGRESS
) {
174 } else if (!connect_ret
) {
175 /* Connect succeeded */
180 * Perform poll loop following EINPROGRESS recommendation from
181 * connect(2) man page.
187 fds
.events
= POLLOUT
;
189 ret
= poll(&fds
, 1, RECONNECT_DELAY
);
192 } else if (ret
> 0) {
194 socklen_t optval_len
= sizeof(optval
);
196 if (!(fds
.revents
& POLLOUT
)) {
197 /* Either hup or error */
202 ret
= getsockopt(sock
->fd
, SOL_SOCKET
,
203 SO_ERROR
, &optval
, &optval_len
);
214 /* ret == 0: timeout */
215 ret
= clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
217 PERROR("clock_gettime");
221 } while (time_diff_ms(&cur_time
, &orig_time
) < timeout
);
228 /* Restore initial flags */
229 ret
= fcntl(sock
->fd
, F_SETFL
, flags
);
232 /* Continue anyway */
239 * Connect PF_INET socket.
242 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
246 if (lttcomm_get_network_timeout()) {
247 ret
= connect_with_timeout(sock
);
249 ret
= connect_no_timeout(sock
);
252 PERROR("connect inet6");
259 closeret
= close(sock
->fd
);
261 PERROR("close inet6");
268 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
269 * MUST be bind(2) before.
272 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
276 struct lttcomm_sock
*new_sock
;
278 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
280 * accept(2) does not exist for UDP so simply return the passed socket.
286 new_sock
= lttcomm_alloc_sock(sock
->proto
);
287 if (new_sock
== NULL
) {
291 len
= sizeof(new_sock
->sockaddr
.addr
.sin6
);
294 new_fd
= accept(sock
->fd
,
295 (struct sockaddr
*) &new_sock
->sockaddr
.addr
.sin6
, &len
);
297 PERROR("accept inet6");
301 new_sock
->fd
= new_fd
;
302 new_sock
->ops
= &inet6_ops
;
313 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
316 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
320 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
321 /* listen(2) does not exist for UDP so simply return success. */
326 /* Default listen backlog */
328 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
331 ret
= listen(sock
->fd
, backlog
);
333 PERROR("listen inet6");
341 * Receive data of size len in put that data into the buf param. Using recvmsg
344 * Return the size of received data.
347 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
348 size_t len
, int flags
)
355 memset(&msg
, 0, sizeof(msg
));
357 iov
[0].iov_base
= buf
;
358 iov
[0].iov_len
= len
;
362 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
363 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
366 len_last
= iov
[0].iov_len
;
367 ret
= recvmsg(sock
->fd
, &msg
, flags
);
369 iov
[0].iov_base
+= ret
;
370 iov
[0].iov_len
-= ret
;
371 assert(ret
<= len_last
);
373 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
375 PERROR("recvmsg inet");
376 } else if (ret
> 0) {
379 /* Else ret = 0 meaning an orderly shutdown. */
385 * Send buf data of size len. Using sendmsg API.
387 * Return the size of sent data.
390 ssize_t
lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
391 size_t len
, int flags
)
397 memset(&msg
, 0, sizeof(msg
));
399 iov
[0].iov_base
= buf
;
400 iov
[0].iov_len
= len
;
404 switch (sock
->proto
) {
405 case LTTCOMM_SOCK_UDP
:
406 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
407 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
414 ret
= sendmsg(sock
->fd
, &msg
, flags
);
415 } while (ret
< 0 && errno
== EINTR
);
418 * Only warn about EPIPE when quiet mode is deactivated.
419 * We consider EPIPE as expected.
421 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
422 PERROR("sendmsg inet6");
430 * Shutdown cleanly and close.
433 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
437 /* Don't try to close an invalid marked socket */
438 if (sock
->fd
== -1) {
442 ret
= close(sock
->fd
);
444 PERROR("close inet6");