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