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