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