| 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 _LGPL_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 <common/compat/time.h> |
| 30 | #include <poll.h> |
| 31 | |
| 32 | #include <common/common.h> |
| 33 | #include <common/time.h> |
| 34 | |
| 35 | #include "inet6.h" |
| 36 | |
| 37 | #define RECONNECT_DELAY 200 /* ms */ |
| 38 | |
| 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 | */ |
| 55 | LTTNG_HIDDEN |
| 56 | int lttcomm_create_inet6_sock(struct lttcomm_sock *sock, int type, int proto) |
| 57 | { |
| 58 | int val = 1, ret; |
| 59 | unsigned long timeout; |
| 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 | 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 | } |
| 88 | |
| 89 | return 0; |
| 90 | |
| 91 | error: |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Bind socket and return. |
| 97 | */ |
| 98 | LTTNG_HIDDEN |
| 99 | int lttcomm_bind_inet6_sock(struct lttcomm_sock *sock) |
| 100 | { |
| 101 | int ret; |
| 102 | |
| 103 | ret = bind(sock->fd, (const struct sockaddr *) &sock->sockaddr.addr.sin6, |
| 104 | sizeof(sock->sockaddr.addr.sin6)); |
| 105 | if (ret < 0) { |
| 106 | PERROR("bind inet6"); |
| 107 | } |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 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 | |
| 159 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &orig_time); |
| 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 | |
| 177 | /* |
| 178 | * Perform poll loop following EINPROGRESS recommendation from |
| 179 | * connect(2) man page. |
| 180 | */ |
| 181 | do { |
| 182 | struct pollfd fds; |
| 183 | |
| 184 | fds.fd = sock->fd; |
| 185 | fds.events = POLLOUT; |
| 186 | fds.revents = 0; |
| 187 | ret = poll(&fds, 1, RECONNECT_DELAY); |
| 188 | if (ret < 0) { |
| 189 | goto error; |
| 190 | } else if (ret > 0) { |
| 191 | int optval; |
| 192 | socklen_t optval_len = sizeof(optval); |
| 193 | |
| 194 | if (!(fds.revents & POLLOUT)) { |
| 195 | /* Either hup or error */ |
| 196 | errno = EPIPE; |
| 197 | goto error; |
| 198 | } |
| 199 | /* got something */ |
| 200 | ret = getsockopt(sock->fd, SOL_SOCKET, |
| 201 | SO_ERROR, &optval, &optval_len); |
| 202 | if (ret) { |
| 203 | goto error; |
| 204 | } |
| 205 | if (!optval) { |
| 206 | connect_ret = 0; |
| 207 | goto success; |
| 208 | } else { |
| 209 | goto error; |
| 210 | } |
| 211 | } |
| 212 | /* ret == 0: timeout */ |
| 213 | ret = lttng_clock_gettime(CLOCK_MONOTONIC, &cur_time); |
| 214 | if (ret == -1) { |
| 215 | PERROR("clock_gettime"); |
| 216 | connect_ret = ret; |
| 217 | goto error; |
| 218 | } |
| 219 | } while (time_diff_ms(&cur_time, &orig_time) < timeout); |
| 220 | |
| 221 | /* Timeout */ |
| 222 | errno = ETIMEDOUT; |
| 223 | connect_ret = -1; |
| 224 | |
| 225 | success: |
| 226 | /* Restore initial flags */ |
| 227 | ret = fcntl(sock->fd, F_SETFL, flags); |
| 228 | if (ret == -1) { |
| 229 | PERROR("fcntl"); |
| 230 | /* Continue anyway */ |
| 231 | } |
| 232 | error: |
| 233 | return connect_ret; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Connect PF_INET socket. |
| 238 | */ |
| 239 | LTTNG_HIDDEN |
| 240 | int lttcomm_connect_inet6_sock(struct lttcomm_sock *sock) |
| 241 | { |
| 242 | int ret, closeret; |
| 243 | |
| 244 | if (lttcomm_get_network_timeout()) { |
| 245 | ret = connect_with_timeout(sock); |
| 246 | } else { |
| 247 | ret = connect_no_timeout(sock); |
| 248 | } |
| 249 | if (ret < 0) { |
| 250 | PERROR("connect inet6"); |
| 251 | goto error_connect; |
| 252 | } |
| 253 | |
| 254 | return ret; |
| 255 | |
| 256 | error_connect: |
| 257 | closeret = close(sock->fd); |
| 258 | if (closeret) { |
| 259 | PERROR("close inet6"); |
| 260 | } |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Do an accept(2) on the sock and return the new lttcomm socket. The socket |
| 267 | * MUST be bind(2) before. |
| 268 | */ |
| 269 | LTTNG_HIDDEN |
| 270 | struct lttcomm_sock *lttcomm_accept_inet6_sock(struct lttcomm_sock *sock) |
| 271 | { |
| 272 | int new_fd; |
| 273 | socklen_t len; |
| 274 | struct lttcomm_sock *new_sock; |
| 275 | |
| 276 | if (sock->proto == LTTCOMM_SOCK_UDP) { |
| 277 | /* |
| 278 | * accept(2) does not exist for UDP so simply return the passed socket. |
| 279 | */ |
| 280 | new_sock = sock; |
| 281 | goto end; |
| 282 | } |
| 283 | |
| 284 | new_sock = lttcomm_alloc_sock(sock->proto); |
| 285 | if (new_sock == NULL) { |
| 286 | goto error; |
| 287 | } |
| 288 | |
| 289 | len = sizeof(new_sock->sockaddr.addr.sin6); |
| 290 | |
| 291 | /* Blocking call */ |
| 292 | new_fd = accept(sock->fd, |
| 293 | (struct sockaddr *) &new_sock->sockaddr.addr.sin6, &len); |
| 294 | if (new_fd < 0) { |
| 295 | PERROR("accept inet6"); |
| 296 | goto error; |
| 297 | } |
| 298 | |
| 299 | new_sock->fd = new_fd; |
| 300 | new_sock->ops = &inet6_ops; |
| 301 | |
| 302 | end: |
| 303 | return new_sock; |
| 304 | |
| 305 | error: |
| 306 | free(new_sock); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
| 312 | */ |
| 313 | LTTNG_HIDDEN |
| 314 | int lttcomm_listen_inet6_sock(struct lttcomm_sock *sock, int backlog) |
| 315 | { |
| 316 | int ret; |
| 317 | |
| 318 | if (sock->proto == LTTCOMM_SOCK_UDP) { |
| 319 | /* listen(2) does not exist for UDP so simply return success. */ |
| 320 | ret = 0; |
| 321 | goto end; |
| 322 | } |
| 323 | |
| 324 | /* Default listen backlog */ |
| 325 | if (backlog <= 0) { |
| 326 | backlog = LTTNG_SESSIOND_COMM_MAX_LISTEN; |
| 327 | } |
| 328 | |
| 329 | ret = listen(sock->fd, backlog); |
| 330 | if (ret < 0) { |
| 331 | PERROR("listen inet6"); |
| 332 | } |
| 333 | |
| 334 | end: |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Receive data of size len in put that data into the buf param. Using recvmsg |
| 340 | * API. |
| 341 | * |
| 342 | * Return the size of received data. |
| 343 | */ |
| 344 | LTTNG_HIDDEN |
| 345 | ssize_t lttcomm_recvmsg_inet6_sock(struct lttcomm_sock *sock, void *buf, |
| 346 | size_t len, int flags) |
| 347 | { |
| 348 | struct msghdr msg; |
| 349 | struct iovec iov[1]; |
| 350 | ssize_t ret = -1; |
| 351 | size_t len_last; |
| 352 | |
| 353 | memset(&msg, 0, sizeof(msg)); |
| 354 | |
| 355 | iov[0].iov_base = buf; |
| 356 | iov[0].iov_len = len; |
| 357 | msg.msg_iov = iov; |
| 358 | msg.msg_iovlen = 1; |
| 359 | |
| 360 | msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6; |
| 361 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6); |
| 362 | |
| 363 | do { |
| 364 | len_last = iov[0].iov_len; |
| 365 | ret = recvmsg(sock->fd, &msg, flags); |
| 366 | if (ret > 0) { |
| 367 | iov[0].iov_base += ret; |
| 368 | iov[0].iov_len -= ret; |
| 369 | assert(ret <= len_last); |
| 370 | } |
| 371 | } while ((ret > 0 && ret < len_last) || (ret < 0 && errno == EINTR)); |
| 372 | if (ret < 0) { |
| 373 | PERROR("recvmsg inet"); |
| 374 | } else if (ret > 0) { |
| 375 | ret = len; |
| 376 | } |
| 377 | /* Else ret = 0 meaning an orderly shutdown. */ |
| 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Send buf data of size len. Using sendmsg API. |
| 384 | * |
| 385 | * Return the size of sent data. |
| 386 | */ |
| 387 | LTTNG_HIDDEN |
| 388 | ssize_t lttcomm_sendmsg_inet6_sock(struct lttcomm_sock *sock, const void *buf, |
| 389 | size_t len, int flags) |
| 390 | { |
| 391 | struct msghdr msg; |
| 392 | struct iovec iov[1]; |
| 393 | ssize_t ret = -1; |
| 394 | |
| 395 | memset(&msg, 0, sizeof(msg)); |
| 396 | |
| 397 | iov[0].iov_base = (void *) buf; |
| 398 | iov[0].iov_len = len; |
| 399 | msg.msg_iov = iov; |
| 400 | msg.msg_iovlen = 1; |
| 401 | |
| 402 | switch (sock->proto) { |
| 403 | case LTTCOMM_SOCK_UDP: |
| 404 | msg.msg_name = (struct sockaddr *) &sock->sockaddr.addr.sin6; |
| 405 | msg.msg_namelen = sizeof(sock->sockaddr.addr.sin6); |
| 406 | break; |
| 407 | default: |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | do { |
| 412 | ret = sendmsg(sock->fd, &msg, flags); |
| 413 | } while (ret < 0 && errno == EINTR); |
| 414 | if (ret < 0) { |
| 415 | /* |
| 416 | * Only warn about EPIPE when quiet mode is deactivated. |
| 417 | * We consider EPIPE as expected. |
| 418 | */ |
| 419 | if (errno != EPIPE || !lttng_opt_quiet) { |
| 420 | PERROR("sendmsg inet6"); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Shutdown cleanly and close. |
| 429 | */ |
| 430 | LTTNG_HIDDEN |
| 431 | int lttcomm_close_inet6_sock(struct lttcomm_sock *sock) |
| 432 | { |
| 433 | int ret; |
| 434 | |
| 435 | /* Don't try to close an invalid marked socket */ |
| 436 | if (sock->fd == -1) { |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | ret = close(sock->fd); |
| 441 | if (ret) { |
| 442 | PERROR("close inet6"); |
| 443 | } |
| 444 | |
| 445 | /* Mark socket */ |
| 446 | sock->fd = -1; |
| 447 | |
| 448 | return ret; |
| 449 | } |