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/common.h>
34 * INET protocol operations.
36 static const struct lttcomm_proto_ops inet6_ops
= {
37 .bind
= lttcomm_bind_inet6_sock
,
38 .close
= lttcomm_close_inet6_sock
,
39 .connect
= lttcomm_connect_inet6_sock
,
40 .accept
= lttcomm_accept_inet6_sock
,
41 .listen
= lttcomm_listen_inet6_sock
,
42 .recvmsg
= lttcomm_recvmsg_inet6_sock
,
43 .sendmsg
= lttcomm_sendmsg_inet6_sock
,
47 * Creates an PF_INET socket.
50 int lttcomm_create_inet6_sock(struct lttcomm_sock
*sock
, int type
, int proto
)
54 /* Create server socket */
55 if ((sock
->fd
= socket(PF_INET6
, type
, proto
)) < 0) {
56 PERROR("socket inet6");
60 sock
->ops
= &inet6_ops
;
63 * Set socket option to reuse the address.
65 ret
= setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(int));
67 PERROR("setsockopt inet6");
78 * Bind socket and return.
81 int lttcomm_bind_inet6_sock(struct lttcomm_sock
*sock
)
85 ret
= bind(sock
->fd
, &sock
->sockaddr
.addr
.sin6
,
86 sizeof(sock
->sockaddr
.addr
.sin6
));
95 * Connect PF_INET socket.
98 int lttcomm_connect_inet6_sock(struct lttcomm_sock
*sock
)
102 ret
= connect(sock
->fd
, (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
,
103 sizeof(sock
->sockaddr
.addr
.sin6
));
105 PERROR("connect inet6");
112 closeret
= close(sock
->fd
);
114 PERROR("close inet6");
121 * Do an accept(2) on the sock and return the new lttcomm socket. The socket
122 * MUST be bind(2) before.
125 struct lttcomm_sock
*lttcomm_accept_inet6_sock(struct lttcomm_sock
*sock
)
129 struct lttcomm_sock
*new_sock
;
131 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
133 * accept(2) does not exist for UDP so simply return the passed socket.
139 new_sock
= lttcomm_alloc_sock(sock
->proto
);
140 if (new_sock
== NULL
) {
144 len
= sizeof(new_sock
->sockaddr
.addr
.sin6
);
147 new_fd
= accept(sock
->fd
,
148 (struct sockaddr
*) &new_sock
->sockaddr
.addr
.sin6
, &len
);
150 PERROR("accept inet6");
154 new_sock
->fd
= new_fd
;
155 new_sock
->ops
= &inet6_ops
;
166 * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN.
169 int lttcomm_listen_inet6_sock(struct lttcomm_sock
*sock
, int backlog
)
173 if (sock
->proto
== LTTCOMM_SOCK_UDP
) {
174 /* listen(2) does not exist for UDP so simply return success. */
179 /* Default listen backlog */
181 backlog
= LTTNG_SESSIOND_COMM_MAX_LISTEN
;
184 ret
= listen(sock
->fd
, backlog
);
186 PERROR("listen inet6");
194 * Receive data of size len in put that data into the buf param. Using recvmsg
197 * Return the size of received data.
200 ssize_t
lttcomm_recvmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
201 size_t len
, int flags
)
208 memset(&msg
, 0, sizeof(msg
));
210 iov
[0].iov_base
= buf
;
211 iov
[0].iov_len
= len
;
215 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
216 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
219 len_last
= iov
[0].iov_len
;
220 ret
= recvmsg(sock
->fd
, &msg
, flags
);
222 iov
[0].iov_base
+= ret
;
223 iov
[0].iov_len
-= ret
;
224 assert(ret
<= len_last
);
226 } while ((ret
> 0 && ret
< len_last
) || (ret
< 0 && errno
== EINTR
));
228 PERROR("recvmsg inet");
229 } else if (ret
> 0) {
232 /* Else ret = 0 meaning an orderly shutdown. */
238 * Send buf data of size len. Using sendmsg API.
240 * Return the size of sent data.
243 ssize_t
lttcomm_sendmsg_inet6_sock(struct lttcomm_sock
*sock
, void *buf
,
244 size_t len
, int flags
)
250 memset(&msg
, 0, sizeof(msg
));
252 iov
[0].iov_base
= buf
;
253 iov
[0].iov_len
= len
;
257 switch (sock
->proto
) {
258 case LTTCOMM_SOCK_UDP
:
259 msg
.msg_name
= (struct sockaddr
*) &sock
->sockaddr
.addr
.sin6
;
260 msg
.msg_namelen
= sizeof(sock
->sockaddr
.addr
.sin6
);
267 ret
= sendmsg(sock
->fd
, &msg
, flags
);
268 } while (ret
< 0 && errno
== EINTR
);
271 * Only warn about EPIPE when quiet mode is deactivated.
272 * We consider EPIPE as expected.
274 if (errno
!= EPIPE
|| !lttng_opt_quiet
) {
275 PERROR("sendmsg inet6");
283 * Shutdown cleanly and close.
286 int lttcomm_close_inet6_sock(struct lttcomm_sock
*sock
)
290 /* Don't try to close an invalid marked socket */
291 if (sock
->fd
== -1) {
295 ret
= close(sock
->fd
);
297 PERROR("close inet6");