Commit | Line | Data |
---|---|---|
6364a07a DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * more details. | |
12 | * | |
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. | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <assert.h> | |
20 | #include <limits.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <sys/stat.h> | |
25 | #include <sys/types.h> | |
26 | #include <unistd.h> | |
27 | #include <errno.h> | |
28 | ||
90e535ef | 29 | #include <common/common.h> |
6364a07a DG |
30 | |
31 | #include "inet6.h" | |
32 | ||
33 | /* | |
34 | * INET protocol operations. | |
35 | */ | |
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, | |
44 | }; | |
45 | ||
46 | /* | |
47 | * Creates an PF_INET socket. | |
48 | */ | |
90e535ef | 49 | LTTNG_HIDDEN |
6364a07a DG |
50 | int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto) |
51 | { | |
de5e9086 | 52 | int val = 1, ret; |
6364a07a DG |
53 | |
54 | /* Create server socket */ | |
7b43086d | 55 | if ((sock->fd = socket(PF_INET6, type, proto)) < 0) { |
6364a07a DG |
56 | PERROR("socket inet6"); |
57 | goto error; | |
58 | } | |
59 | ||
60 | sock->ops = &inet6_ops; | |
61 | ||
62 | /* | |
63 | * Set socket option to reuse the address. | |
64 | */ | |
65 | ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int)); | |
66 | if (ret < 0) { | |
67 | PERROR("setsockopt inet6"); | |
68 | goto error; | |
69 | } | |
70 | ||
71 | return 0; | |
72 | ||
73 | error: | |
74 | return -1; | |
75 | } | |
76 | ||
77 | /* | |
78 | * Bind socket and return. | |
79 | */ | |
90e535ef | 80 | LTTNG_HIDDEN |
6364a07a DG |
81 | int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock) |
82 | { | |
83 | int ret; | |
84 | ||
85 | ret = bind(sock->fd, &sock->sockaddr.addr.sin6, | |
86 | sizeof(sock->sockaddr.addr.sin6)); | |
87 | if (ret < 0) { | |
88 | PERROR("bind inet6"); | |
89 | } | |
90 | ||
91 | return ret; | |
92 | } | |
93 | ||
94 | /* | |
95 | * Connect PF_INET socket. | |
96 | */ | |
90e535ef | 97 | LTTNG_HIDDEN |
6364a07a DG |
98 | int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock) |
99 | { | |
100 | int ret, closeret; | |
101 | ||
102 | ret = connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin6, | |
103 | sizeof(sock->sockaddr.addr.sin6)); | |
104 | if (ret < 0) { | |
105 | /* | |
106 | * Don't print message on connect error, because connect is used in | |
107 | * normal execution to detect if sessiond is alive. | |
108 | */ | |
109 | goto error_connect; | |
110 | } | |
111 | ||
112 | return ret; | |
113 | ||
114 | error_connect: | |
115 | closeret = close(sock->fd); | |
116 | if (closeret) { | |
117 | PERROR("close inet6"); | |
118 | } | |
119 | ||
120 | return ret; | |
121 | } | |
122 | ||
123 | /* | |
124 | * Do an accept(2) on the sock and return the new lttcomm socket. The socket | |
125 | * MUST be bind(2) before. | |
126 | */ | |
90e535ef | 127 | LTTNG_HIDDEN |
6364a07a DG |
128 | struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock) |
129 | { | |
130 | int new_fd; | |
88a5db70 | 131 | socklen_t len; |
6364a07a DG |
132 | struct lttcomm_sock *new_sock; |
133 | ||
134 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
135 | /* | |
136 | * accept(2) does not exist for UDP so simply return the passed socket. | |
137 | */ | |
138 | new_sock = sock; | |
139 | goto end; | |
140 | } | |
141 | ||
de5e9086 | 142 | new_sock = lttcomm_alloc_sock(sock->proto); |
6364a07a DG |
143 | if (new_sock == NULL) { |
144 | goto error; | |
145 | } | |
146 | ||
88a5db70 DG |
147 | len = sizeof(new_sock->sockaddr.addr.sin6); |
148 | ||
6364a07a DG |
149 | /* Blocking call */ |
150 | new_fd = accept(sock->fd, | |
151 | (struct sockaddr *) &new_sock->sockaddr.addr.sin6, &len); | |
152 | if (new_fd < 0) { | |
153 | PERROR("accept inet6"); | |
154 | goto error; | |
155 | } | |
156 | ||
157 | new_sock->fd = new_fd; | |
de5e9086 | 158 | new_sock->ops = &inet6_ops; |
6364a07a DG |
159 | |
160 | end: | |
161 | return new_sock; | |
162 | ||
163 | error: | |
164 | free(new_sock); | |
165 | return NULL; | |
166 | } | |
167 | ||
168 | /* | |
169 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. | |
170 | */ | |
90e535ef | 171 | LTTNG_HIDDEN |
6364a07a DG |
172 | int lttcomm_listen_inet6_sock(struct lttcomm_sock *sock, int backlog) |
173 | { | |
174 | int ret; | |
175 | ||
176 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
177 | /* listen(2) does not exist for UDP so simply return success. */ | |
178 | ret = 0; | |
179 | goto end; | |
180 | } | |
181 | ||
182 | /* Default listen backlog */ | |
183 | if (backlog <= 0) { | |
184 | backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN; | |
185 | } | |
186 | ||
187 | ret = listen(sock->fd, backlog); | |
188 | if (ret < 0) { | |
189 | PERROR("listen inet6"); | |
190 | } | |
191 | ||
192 | end: | |
193 | return ret; | |
194 | } | |
195 | ||
196 | /* | |
197 | * Receive data of size len in put that data into the buf param. Using recvmsg | |
198 | * API. | |
199 | * | |
200 | * Return the size of received data. | |
201 | */ | |
90e535ef | 202 | LTTNG_HIDDEN |
6364a07a DG |
203 | ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf, |
204 | size_t len, int flags) | |
205 | { | |
206 | struct msghdr msg; | |
207 | struct iovec iov[1]; | |
208 | ssize_t ret = -1; | |
7c5aef62 | 209 | size_t len_last; |
6364a07a DG |
210 | |
211 | memset(&msg, 0, sizeof(msg)); | |
212 | ||
213 | iov[0].iov_base = buf; | |
214 | iov[0].iov_len = len; | |
215 | msg.msg_iov = iov; | |
216 | msg.msg_iovlen = 1; | |
217 | ||
218 | msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6; | |
219 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6); | |
220 | ||
6364a07a | 221 | do { |
7c5aef62 | 222 | len_last = iov[0].iov_len; |
6364a07a | 223 | ret = recvmsg(sock->fd, &msg, flags); |
7c5aef62 DG |
224 | if (ret > 0) { |
225 | iov[0].iov_base += ret; | |
226 | iov[0].iov_len -= ret; | |
227 | assert(ret <= len_last); | |
228 | } | |
229 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
6364a07a | 230 | if (ret < 0) { |
7c5aef62 DG |
231 | PERROR("recvmsg inet"); |
232 | } else if (ret > 0) { | |
233 | ret = len; | |
6364a07a | 234 | } |
7c5aef62 | 235 | /* Else ret = 0 meaning an orderly shutdown. */ |
6364a07a DG |
236 | |
237 | return ret; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Send buf data of size len. Using sendmsg API. | |
242 | * | |
243 | * Return the size of sent data. | |
244 | */ | |
90e535ef | 245 | LTTNG_HIDDEN |
6364a07a DG |
246 | ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, void *buf, |
247 | size_t len, int flags) | |
248 | { | |
249 | struct msghdr msg; | |
250 | struct iovec iov[1]; | |
251 | ssize_t ret = -1; | |
252 | ||
253 | memset(&msg, 0, sizeof(msg)); | |
254 | ||
255 | iov[0].iov_base = buf; | |
256 | iov[0].iov_len = len; | |
257 | msg.msg_iov = iov; | |
258 | msg.msg_iovlen = 1; | |
259 | ||
260 | switch (sock->proto) { | |
261 | case LTTCOMM_SOCK_UDP: | |
262 | msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6; | |
263 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6); | |
264 | break; | |
265 | default: | |
266 | break; | |
267 | } | |
268 | ||
269 | do { | |
270 | ret = sendmsg(sock->fd, &msg, flags); | |
271 | } while (ret < 0 && errno == EINTR); | |
272 | if (ret < 0) { | |
273 | /* | |
274 | * Only warn about EPIPE when quiet mode is deactivated. | |
275 | * We consider EPIPE as expected. | |
276 | */ | |
277 | if (errno != EPIPE || !lttng_opt_quiet) { | |
278 | PERROR("sendmsg inet6"); | |
279 | } | |
280 | } | |
281 | ||
282 | return ret; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Shutdown cleanly and close. | |
287 | */ | |
90e535ef | 288 | LTTNG_HIDDEN |
6364a07a DG |
289 | int lttcomm_close_inet6_sock(struct lttcomm_sock *sock) |
290 | { | |
6e742359 | 291 | int ret; |
6364a07a | 292 | |
de5e9086 | 293 | /* Don't try to close an invalid marked socket */ |
6364a07a DG |
294 | if (sock->fd == -1) { |
295 | return 0; | |
296 | } | |
297 | ||
6e742359 DG |
298 | ret = close(sock->fd); |
299 | if (ret) { | |
6364a07a DG |
300 | PERROR("close inet6"); |
301 | } | |
302 | ||
303 | /* Mark socket */ | |
304 | sock->fd = -1; | |
305 | ||
306 | return ret; | |
307 | } |