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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
6364a07a DG |
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> | |
a655f4cf | 28 | #include <fcntl.h> |
389fbf04 | 29 | #include <common/compat/time.h> |
a655f4cf | 30 | #include <poll.h> |
6364a07a | 31 | |
90e535ef | 32 | #include <common/common.h> |
395d6b02 | 33 | #include <common/time.h> |
6364a07a DG |
34 | |
35 | #include "inet6.h" | |
36 | ||
a655f4cf MD |
37 | #define RECONNECT_DELAY 200 /* ms */ |
38 | ||
6364a07a DG |
39 | /* |
40 | * INET protocol operations. | |
41 | */ | |
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, | |
50 | }; | |
51 | ||
52 | /* | |
53 | * Creates an PF_INET socket. | |
54 | */ | |
90e535ef | 55 | LTTNG_HIDDEN |
6364a07a DG |
56 | int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto) |
57 | { | |
de5e9086 | 58 | int val = 1, ret; |
783a3b9a | 59 | unsigned long timeout; |
6364a07a DG |
60 | |
61 | /* Create server socket */ | |
7b43086d | 62 | if ((sock->fd = socket(PF_INET6, type, proto)) < 0) { |
6364a07a DG |
63 | PERROR("socket inet6"); |
64 | goto error; | |
65 | } | |
66 | ||
67 | sock->ops = &inet6_ops; | |
68 | ||
69 | /* | |
70 | * Set socket option to reuse the address. | |
71 | */ | |
72 | ret = setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int)); | |
73 | if (ret < 0) { | |
74 | PERROR("setsockopt inet6"); | |
75 | goto error; | |
76 | } | |
783a3b9a MD |
77 | timeout = lttcomm_get_network_timeout(); |
78 | if (timeout) { | |
79 | ret = lttcomm_setsockopt_rcv_timeout(sock->fd, timeout); | |
80 | if (ret) { | |
81 | goto error; | |
82 | } | |
83 | ret = lttcomm_setsockopt_snd_timeout(sock->fd, timeout); | |
84 | if (ret) { | |
85 | goto error; | |
86 | } | |
87 | } | |
6364a07a DG |
88 | |
89 | return 0; | |
90 | ||
91 | error: | |
92 | return -1; | |
93 | } | |
94 | ||
95 | /* | |
96 | * Bind socket and return. | |
97 | */ | |
90e535ef | 98 | LTTNG_HIDDEN |
6364a07a DG |
99 | int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock) |
100 | { | |
2288467f JG |
101 | return bind(sock->fd, |
102 | (const struct sockaddr *) &sock->sockaddr.addr.sin6, | |
6364a07a | 103 | sizeof(sock->sockaddr.addr.sin6)); |
6364a07a DG |
104 | } |
105 | ||
a655f4cf MD |
106 | static |
107 | int connect_no_timeout(struct lttcomm_sock *sock) | |
108 | { | |
109 | return connect(sock->fd, (struct sockaddr *) &sock->sockaddr.addr.sin6, | |
110 | sizeof(sock->sockaddr.addr.sin6)); | |
111 | } | |
112 | ||
a655f4cf MD |
113 | static |
114 | int connect_with_timeout(struct lttcomm_sock *sock) | |
115 | { | |
116 | unsigned long timeout = lttcomm_get_network_timeout(); | |
117 | int ret, flags, connect_ret; | |
118 | struct timespec orig_time, cur_time; | |
2daf6502 | 119 | unsigned long diff_ms; |
a655f4cf MD |
120 | |
121 | ret = fcntl(sock->fd, F_GETFL, 0); | |
122 | if (ret == -1) { | |
123 | PERROR("fcntl"); | |
124 | return -1; | |
125 | } | |
126 | flags = ret; | |
127 | ||
128 | /* Set socket to nonblock */ | |
129 | ret = fcntl(sock->fd, F_SETFL, flags | O_NONBLOCK); | |
130 | if (ret == -1) { | |
131 | PERROR("fcntl"); | |
132 | return -1; | |
133 | } | |
134 | ||
389fbf04 | 135 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time); |
a655f4cf MD |
136 | if (ret == -1) { |
137 | PERROR("clock_gettime"); | |
138 | return -1; | |
139 | } | |
140 | ||
141 | connect_ret = connect(sock->fd, | |
142 | (struct sockaddr *) &sock->sockaddr.addr.sin6, | |
143 | sizeof(sock->sockaddr.addr.sin6)); | |
144 | if (connect_ret == -1 && errno != EAGAIN | |
145 | && errno != EWOULDBLOCK | |
146 | && errno != EINPROGRESS) { | |
147 | goto error; | |
148 | } else if (!connect_ret) { | |
149 | /* Connect succeeded */ | |
150 | goto success; | |
151 | } | |
152 | ||
48ac9596 JR |
153 | DBG("Asynchronous connect for sock %d, performing polling with" |
154 | " timeout: %lums", sock->fd, timeout); | |
155 | ||
a655f4cf MD |
156 | /* |
157 | * Perform poll loop following EINPROGRESS recommendation from | |
158 | * connect(2) man page. | |
159 | */ | |
160 | do { | |
161 | struct pollfd fds; | |
162 | ||
163 | fds.fd = sock->fd; | |
164 | fds.events = POLLOUT; | |
165 | fds.revents = 0; | |
166 | ret = poll(&fds, 1, RECONNECT_DELAY); | |
167 | if (ret < 0) { | |
168 | goto error; | |
169 | } else if (ret > 0) { | |
170 | int optval; | |
171 | socklen_t optval_len = sizeof(optval); | |
172 | ||
173 | if (!(fds.revents & POLLOUT)) { | |
174 | /* Either hup or error */ | |
175 | errno = EPIPE; | |
176 | goto error; | |
177 | } | |
178 | /* got something */ | |
179 | ret = getsockopt(sock->fd, SOL_SOCKET, | |
180 | SO_ERROR, &optval, &optval_len); | |
181 | if (ret) { | |
48ac9596 | 182 | PERROR("getsockopt"); |
a655f4cf MD |
183 | goto error; |
184 | } | |
185 | if (!optval) { | |
186 | connect_ret = 0; | |
187 | goto success; | |
188 | } else { | |
48ac9596 JR |
189 | /* Get actual connect() errno from opt_val */ |
190 | errno = optval; | |
a655f4cf MD |
191 | goto error; |
192 | } | |
193 | } | |
194 | /* ret == 0: timeout */ | |
389fbf04 | 195 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time); |
a655f4cf MD |
196 | if (ret == -1) { |
197 | PERROR("clock_gettime"); | |
198 | connect_ret = ret; | |
199 | goto error; | |
200 | } | |
2daf6502 MD |
201 | if (timespec_to_ms(timespec_abs_diff(cur_time, orig_time), &diff_ms) < 0) { |
202 | ERR("timespec_to_ms input overflows milliseconds output"); | |
203 | connect_ret = -1; | |
204 | goto error; | |
205 | } | |
206 | } while (diff_ms < timeout); | |
a655f4cf MD |
207 | |
208 | /* Timeout */ | |
209 | errno = ETIMEDOUT; | |
210 | connect_ret = -1; | |
211 | ||
212 | success: | |
213 | /* Restore initial flags */ | |
214 | ret = fcntl(sock->fd, F_SETFL, flags); | |
215 | if (ret == -1) { | |
216 | PERROR("fcntl"); | |
217 | /* Continue anyway */ | |
218 | } | |
219 | error: | |
220 | return connect_ret; | |
221 | } | |
222 | ||
6364a07a DG |
223 | /* |
224 | * Connect PF_INET socket. | |
225 | */ | |
90e535ef | 226 | LTTNG_HIDDEN |
6364a07a DG |
227 | int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock) |
228 | { | |
229 | int ret, closeret; | |
230 | ||
a655f4cf MD |
231 | if (lttcomm_get_network_timeout()) { |
232 | ret = connect_with_timeout(sock); | |
233 | } else { | |
234 | ret = connect_no_timeout(sock); | |
235 | } | |
6364a07a | 236 | if (ret < 0) { |
82c05d47 | 237 | PERROR("connect inet6"); |
6364a07a DG |
238 | goto error_connect; |
239 | } | |
240 | ||
241 | return ret; | |
242 | ||
243 | error_connect: | |
244 | closeret = close(sock->fd); | |
245 | if (closeret) { | |
246 | PERROR("close inet6"); | |
247 | } | |
248 | ||
249 | return ret; | |
250 | } | |
251 | ||
252 | /* | |
253 | * Do an accept(2) on the sock and return the new lttcomm socket. The socket | |
254 | * MUST be bind(2) before. | |
255 | */ | |
90e535ef | 256 | LTTNG_HIDDEN |
6364a07a DG |
257 | struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock) |
258 | { | |
259 | int new_fd; | |
88a5db70 | 260 | socklen_t len; |
6364a07a DG |
261 | struct lttcomm_sock *new_sock; |
262 | ||
263 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
264 | /* | |
265 | * accept(2) does not exist for UDP so simply return the passed socket. | |
266 | */ | |
267 | new_sock = sock; | |
268 | goto end; | |
269 | } | |
270 | ||
de5e9086 | 271 | new_sock = lttcomm_alloc_sock(sock->proto); |
6364a07a DG |
272 | if (new_sock == NULL) { |
273 | goto error; | |
274 | } | |
275 | ||
88a5db70 DG |
276 | len = sizeof(new_sock->sockaddr.addr.sin6); |
277 | ||
6364a07a DG |
278 | /* Blocking call */ |
279 | new_fd = accept(sock->fd, | |
280 | (struct sockaddr *) &new_sock->sockaddr.addr.sin6, &len); | |
281 | if (new_fd < 0) { | |
282 | PERROR("accept inet6"); | |
283 | goto error; | |
284 | } | |
285 | ||
286 | new_sock->fd = new_fd; | |
de5e9086 | 287 | new_sock->ops = &inet6_ops; |
6364a07a DG |
288 | |
289 | end: | |
290 | return new_sock; | |
291 | ||
292 | error: | |
293 | free(new_sock); | |
294 | return NULL; | |
295 | } | |
296 | ||
297 | /* | |
298 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. | |
299 | */ | |
90e535ef | 300 | LTTNG_HIDDEN |
6364a07a DG |
301 | int lttcomm_listen_inet6_sock(struct lttcomm_sock *sock, int backlog) |
302 | { | |
303 | int ret; | |
304 | ||
305 | if (sock->proto == LTTCOMM_SOCK_UDP) { | |
306 | /* listen(2) does not exist for UDP so simply return success. */ | |
307 | ret = 0; | |
308 | goto end; | |
309 | } | |
310 | ||
311 | /* Default listen backlog */ | |
312 | if (backlog <= 0) { | |
313 | backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN; | |
314 | } | |
315 | ||
316 | ret = listen(sock->fd, backlog); | |
317 | if (ret < 0) { | |
318 | PERROR("listen inet6"); | |
319 | } | |
320 | ||
321 | end: | |
322 | return ret; | |
323 | } | |
324 | ||
325 | /* | |
326 | * Receive data of size len in put that data into the buf param. Using recvmsg | |
327 | * API. | |
328 | * | |
329 | * Return the size of received data. | |
330 | */ | |
90e535ef | 331 | LTTNG_HIDDEN |
6364a07a DG |
332 | ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf, |
333 | size_t len, int flags) | |
334 | { | |
335 | struct msghdr msg; | |
336 | struct iovec iov[1]; | |
337 | ssize_t ret = -1; | |
7c5aef62 | 338 | size_t len_last; |
6364a07a DG |
339 | |
340 | memset(&msg, 0, sizeof(msg)); | |
341 | ||
342 | iov[0].iov_base = buf; | |
343 | iov[0].iov_len = len; | |
344 | msg.msg_iov = iov; | |
345 | msg.msg_iovlen = 1; | |
346 | ||
347 | msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6; | |
348 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6); | |
349 | ||
6364a07a | 350 | do { |
7c5aef62 | 351 | len_last = iov[0].iov_len; |
6364a07a | 352 | ret = recvmsg(sock->fd, &msg, flags); |
7c5aef62 | 353 | if (ret > 0) { |
5312a3ed JG |
354 | if (flags & MSG_DONTWAIT) { |
355 | goto end; | |
356 | } | |
7c5aef62 DG |
357 | iov[0].iov_base += ret; |
358 | iov[0].iov_len -= ret; | |
359 | assert(ret <= len_last); | |
360 | } | |
361 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); | |
6364a07a | 362 | if (ret < 0) { |
7c5aef62 DG |
363 | PERROR("recvmsg inet"); |
364 | } else if (ret > 0) { | |
365 | ret = len; | |
6364a07a | 366 | } |
7c5aef62 | 367 | /* Else ret = 0 meaning an orderly shutdown. */ |
5312a3ed | 368 | end: |
6364a07a DG |
369 | return ret; |
370 | } | |
371 | ||
372 | /* | |
373 | * Send buf data of size len. Using sendmsg API. | |
374 | * | |
375 | * Return the size of sent data. | |
376 | */ | |
90e535ef | 377 | LTTNG_HIDDEN |
c2d69327 | 378 | ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, const void *buf, |
6364a07a DG |
379 | size_t len, int flags) |
380 | { | |
381 | struct msghdr msg; | |
382 | struct iovec iov[1]; | |
383 | ssize_t ret = -1; | |
384 | ||
385 | memset(&msg, 0, sizeof(msg)); | |
386 | ||
c2d69327 | 387 | iov[0].iov_base = (void *) buf; |
6364a07a DG |
388 | iov[0].iov_len = len; |
389 | msg.msg_iov = iov; | |
390 | msg.msg_iovlen = 1; | |
391 | ||
392 | switch (sock->proto) { | |
393 | case LTTCOMM_SOCK_UDP: | |
394 | msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6; | |
395 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6); | |
396 | break; | |
397 | default: | |
398 | break; | |
399 | } | |
400 | ||
401 | do { | |
402 | ret = sendmsg(sock->fd, &msg, flags); | |
403 | } while (ret < 0 && errno == EINTR); | |
404 | if (ret < 0) { | |
405 | /* | |
406 | * Only warn about EPIPE when quiet mode is deactivated. | |
407 | * We consider EPIPE as expected. | |
408 | */ | |
409 | if (errno != EPIPE || !lttng_opt_quiet) { | |
410 | PERROR("sendmsg inet6"); | |
411 | } | |
412 | } | |
413 | ||
414 | return ret; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Shutdown cleanly and close. | |
419 | */ | |
90e535ef | 420 | LTTNG_HIDDEN |
6364a07a DG |
421 | int lttcomm_close_inet6_sock(struct lttcomm_sock *sock) |
422 | { | |
6e742359 | 423 | int ret; |
6364a07a | 424 | |
de5e9086 | 425 | /* Don't try to close an invalid marked socket */ |
6364a07a DG |
426 | if (sock->fd == -1) { |
427 | return 0; | |
428 | } | |
429 | ||
6e742359 DG |
430 | ret = close(sock->fd); |
431 | if (ret) { | |
6364a07a DG |
432 | PERROR("close inet6"); |
433 | } | |
434 | ||
435 | /* Mark socket */ | |
436 | sock->fd = -1; | |
437 | ||
438 | return ret; | |
439 | } |