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