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