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