| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef _GNU_SOURCE |
| 4 | #define _GNU_SOURCE // for POLLRDHUP |
| 5 | #endif |
| 6 | |
| 7 | #ifndef __BSD_VISIBLE |
| 8 | #define __BSD_VISIBLE // for POLLRDHUP |
| 9 | #endif |
| 10 | |
| 11 | #include "../libnetdata.h" |
| 12 | |
| 13 | bool ip_to_hostname(const char *ip, char *dst, size_t dst_len) { |
| 14 | if(!dst || !dst_len) |
| 15 | return false; |
| 16 | |
| 17 | struct sockaddr_in sa; |
| 18 | struct sockaddr_in6 sa6; |
| 19 | struct sockaddr *sa_ptr; |
| 20 | int sa_len; |
| 21 | |
| 22 | // Try to convert the IP address to sockaddr_in (IPv4) |
| 23 | if (inet_pton(AF_INET, ip, &(sa.sin_addr)) == 1) { |
| 24 | sa.sin_family = AF_INET; |
| 25 | sa_ptr = (struct sockaddr *)&sa; |
| 26 | sa_len = sizeof(sa); |
| 27 | } |
| 28 | // Try to convert the IP address to sockaddr_in6 (IPv6) |
| 29 | else if (inet_pton(AF_INET6, ip, &(sa6.sin6_addr)) == 1) { |
| 30 | sa6.sin6_family = AF_INET6; |
| 31 | sa_ptr = (struct sockaddr *)&sa6; |
| 32 | sa_len = sizeof(sa6); |
| 33 | } |
| 34 | |
| 35 | else { |
| 36 | dst[0] = '\0'; |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | // Perform the reverse lookup |
| 41 | int res = getnameinfo(sa_ptr, sa_len, dst, dst_len, NULL, 0, NI_NAMEREQD); |
| 42 | if(res != 0) |
| 43 | return false; |
| 44 | |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | // -------------------------------------------------------------------------------------------------------------------- |
| 49 | // various library calls |
| 50 | |
| 51 | bool fd_is_socket(int fd) { |
| 52 | int type; |
| 53 | socklen_t len = sizeof(type); |
| 54 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) == -1) |
| 55 | return false; |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | #if defined(POLLRDHUP) && 0 // ktsaou: disabled because the recv() method is faster (1 syscall vs multiple by poll()) |
| 61 | bool is_socket_closed(int fd) { |
| 62 | if(fd < 0) |
| 63 | return true; |
| 64 | |
| 65 | // if(!fd_is_socket(fd)) { |
| 66 | // //internal_error(true, "fd %d is not a socket", fd); |
| 67 | // return false; |
| 68 | // } |
| 69 | |
| 70 | short int errors = POLLERR | POLLHUP | POLLNVAL | POLLRDHUP; |
| 71 | |
| 72 | struct pollfd pfd = { |
| 73 | .fd = fd, |
| 74 | .events = POLLOUT | errors, |
| 75 | .revents = 0, |
| 76 | }; |
| 77 | |
| 78 | if(poll(&pfd, 1, 0) == -1) { |
| 79 | //internal_error(true, "poll() failed"); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return ((pfd.revents & errors) || !(pfd.revents & POLLOUT)); |
| 84 | } |
| 85 | #else |
| 86 | bool is_socket_closed(int fd) { |
| 87 | if(fd < 0) |
| 88 | return true; |
| 89 | |
| 90 | char buffer; |
| 91 | ssize_t result = recv(fd, &buffer, 1, MSG_PEEK | MSG_DONTWAIT); |
| 92 | if (result == 0) { |
| 93 | // Connection closed |
| 94 | return true; |
| 95 | } |
| 96 | else if (result < 0) { |
| 97 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 98 | // No data available, but socket is still open |
| 99 | return false; |
| 100 | } else { |
| 101 | // An error occurred |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Data is available, socket is open |
| 107 | return false; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | #if defined(OS_LINUX) |
| 112 | // Valid from: 4 KB to 64 MB (typical range) |
| 113 | // Default is usually: 128 KB to 256 KB |
| 114 | // Maximum is controlled by: /proc/sys/net/core/rmem_max and /proc/sys/net/core/wmem_max |
| 115 | // Interactive applications should use: 256 KB |
| 116 | // High-performance applications should use: 8 MB to 64 MB |
| 117 | #define LARGE_SOCK_SIZE (32 * 1024 * 1024) |
| 118 | |
| 119 | #elif defined(OS_FREEBSD) |
| 120 | // Valid from: 4 KB to 16 MB (typical range) |
| 121 | // Default is usually: 64 KB to 256 KB |
| 122 | // Maximum is controlled by: kern.ipc.maxsockbuf |
| 123 | // Interactive applications should use: 128 KB to 256 KB |
| 124 | // High-performance applications should use: 2 MB to 16 MB |
| 125 | #define LARGE_SOCK_SIZE (8 * 1024 * 1024) |
| 126 | |
| 127 | #elif defined(OS_MACOS) |
| 128 | // Valid from: 4 KB to 8 MB (typical range) |
| 129 | // Default is usually: 128 KB |
| 130 | // Maximum is controlled by: net.inet.tcp.sendspace and net.inet.tcp.recvspace |
| 131 | // Interactive applications should use: 128 KB |
| 132 | // High-performance applications should use: 1 MB to 8 MB |
| 133 | #define LARGE_SOCK_SIZE (4 * 1024 * 1024) |
| 134 | |
| 135 | #elif defined(OS_WINDOWS) |
| 136 | // Valid from: 8 KB to 16 MB (typical range) |
| 137 | // Default is usually: 8 KB to 64 KB |
| 138 | // Maximum is controlled by: registry keys such as TcpWindowSize |
| 139 | // Interactive applications should use: 64 KB to 128 KB |
| 140 | // High-performance applications should use: 1 MB to 16 MB |
| 141 | #define LARGE_SOCK_SIZE (8 * 1024 * 1024) |
| 142 | |
| 143 | #else |
| 144 | // Valid from: 4 KB to platform-dependent maximum |
| 145 | // Default is usually: 64 KB to 256 KB |
| 146 | // Interactive applications should use: 128 KB to 256 KB |
| 147 | // High-performance applications should use: 1 MB to platform-dependent maximum |
| 148 | #define LARGE_SOCK_SIZE (1 * 1024 * 1024) |
| 149 | #endif |
| 150 | |
| 151 | // Returns -1 for errors, current buffer size if successful |
| 152 | int sock_enlarge_rcv_buf(int fd) { |
| 153 | int ret = -1; |
| 154 | int bs = LARGE_SOCK_SIZE; |
| 155 | int current_bs = 0; |
| 156 | socklen_t optlen = sizeof(current_bs); |
| 157 | |
| 158 | // Get the current receive buffer size |
| 159 | if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, ¤t_bs, &optlen) == 0) { |
| 160 | // Set the buffer size only if it's smaller than the desired size |
| 161 | if (current_bs < bs) { |
| 162 | if(setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bs, sizeof(bs)) != 0) |
| 163 | return -1; |
| 164 | |
| 165 | // Re-check the buffer size after attempting to set it |
| 166 | if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, ¤t_bs, &optlen) == 0) |
| 167 | ret = current_bs; |
| 168 | } |
| 169 | else { |
| 170 | // Current buffer size is already large enough |
| 171 | ret = current_bs; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | // Returns -1 for errors, current buffer size if successful |
| 179 | int sock_enlarge_snd_buf(int fd) { |
| 180 | int ret = -1; |
| 181 | int bs = LARGE_SOCK_SIZE; |
| 182 | int current_bs = 0; |
| 183 | socklen_t optlen = sizeof(current_bs); |
| 184 | |
| 185 | // Get the current send buffer size |
| 186 | if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, ¤t_bs, &optlen) == 0) { |
| 187 | // Set the buffer size only if it's smaller than the desired size |
| 188 | if (current_bs < bs) { |
| 189 | if(setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bs, sizeof(bs)) != 0) |
| 190 | return -1; |
| 191 | |
| 192 | // Re-check the buffer size after attempting to set it |
| 193 | if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, ¤t_bs, &optlen) == 0) |
| 194 | ret = current_bs; |
| 195 | } |
| 196 | else { |
| 197 | // Current buffer size is already large enough |
| 198 | ret = current_bs; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | // returns -1 for errors, 0 if cork is unset, 1 if cork is set |
| 206 | int sock_setcork(int fd __maybe_unused, bool cork __maybe_unused) { |
| 207 | int rc = -1; |
| 208 | |
| 209 | #ifdef TCP_CORK |
| 210 | int tcp_cork = (cork) ? 1 : 0; |
| 211 | socklen_t optlen = sizeof(tcp_cork); |
| 212 | |
| 213 | if(setsockopt(fd, IPPROTO_TCP, TCP_CORK, &tcp_cork, optlen) == 0) { |
| 214 | // setting was successful, return the intended state |
| 215 | rc = cork ? 1 : 0; |
| 216 | } |
| 217 | else if(getsockopt(fd, IPPROTO_TCP, TCP_CORK, &tcp_cork, &optlen) == 0) { |
| 218 | // return the current state since retrieval is successful |
| 219 | rc = tcp_cork ? 1 : 0; |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | // Returns -1 for errors, 0 if O_NONBLOCK is unset, 1 if O_NONBLOCK is set |
| 227 | int sock_setnonblock(int fd, bool nonblock) { |
| 228 | int rc = -1; |
| 229 | int flags = fcntl(fd, F_GETFL); |
| 230 | |
| 231 | if (flags < 0) { |
| 232 | // Failed to get current flags |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | int new_flags = nonblock ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK); |
| 237 | |
| 238 | if (fcntl(fd, F_SETFL, new_flags) == 0) { |
| 239 | // Setting was successful, return the intended state |
| 240 | rc = nonblock ? 1 : 0; |
| 241 | } else { |
| 242 | // If setting failed, return the current state |
| 243 | flags = fcntl(fd, F_GETFL); |
| 244 | if (flags >= 0) |
| 245 | rc = (flags & O_NONBLOCK) ? 1 : 0; |
| 246 | } |
| 247 | |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | // Returns -1 for errors, 0 if SO_REUSEADDR is unset, 1 if SO_REUSEADDR is set |
| 252 | int sock_setreuse_addr(int fd, bool reuse) { |
| 253 | int rc = -1; |
| 254 | int reuse_val = reuse ? 1 : 0; |
| 255 | socklen_t optlen = sizeof(reuse_val); |
| 256 | |
| 257 | // Attempt to set SO_REUSEADDR |
| 258 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_val, optlen) == 0) { |
| 259 | // Setting was successful, return the intended state |
| 260 | rc = reuse ? 1 : 0; |
| 261 | } else { |
| 262 | // If setting failed, attempt to retrieve the current state |
| 263 | if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_val, &optlen) == 0) { |
| 264 | // Return the current state |
| 265 | rc = reuse_val ? 1 : 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return rc; |
| 270 | } |
| 271 | |
| 272 | // Returns -1 for errors, 0 if SO_REUSEPORT is unset, 1 if SO_REUSEPORT is set |
| 273 | int sock_setreuse_port(int fd __maybe_unused, bool reuse __maybe_unused) { |
| 274 | int rc = -1; |
| 275 | |
| 276 | #ifdef SO_REUSEPORT |
| 277 | int reuse_val = reuse ? 1 : 0; |
| 278 | socklen_t optlen = sizeof(reuse_val); |
| 279 | |
| 280 | // Attempt to set SO_REUSEPORT |
| 281 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse_val, optlen) == 0) { |
| 282 | // Setting was successful, return the intended state |
| 283 | rc = reuse ? 1 : 0; |
| 284 | } else if (errno != ENOPROTOOPT) { |
| 285 | // If setting failed for a reason other than unsupported option, check the current state |
| 286 | if (getsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse_val, &optlen) == 0) { |
| 287 | // Return the current state |
| 288 | rc = reuse_val ? 1 : 0; |
| 289 | } |
| 290 | } |
| 291 | #else |
| 292 | // SO_REUSEPORT is not supported |
| 293 | errno = ENOPROTOOPT; |
| 294 | #endif |
| 295 | |
| 296 | return rc; |
| 297 | } |
| 298 | |
| 299 | // Returns -1 for errors, 0 if FD_CLOEXEC is unset, 1 if FD_CLOEXEC is set |
| 300 | int sock_setcloexec(int fd, bool cloexec) { |
| 301 | int rc = -1; |
| 302 | |
| 303 | // Get current file descriptor flags |
| 304 | int flags = fcntl(fd, F_GETFD); |
| 305 | if (flags == -1) |
| 306 | return -1; // Error retrieving flags |
| 307 | |
| 308 | int new_flags = cloexec ? (flags | FD_CLOEXEC) : (flags & ~FD_CLOEXEC); |
| 309 | |
| 310 | // Set the FD_CLOEXEC flag as requested |
| 311 | if (fcntl(fd, F_SETFD, new_flags) == 0) { |
| 312 | // Setting was successful, return the intended state |
| 313 | rc = cloexec ? 1 : 0; |
| 314 | } else { |
| 315 | // If setting failed, return the current state |
| 316 | flags = fcntl(fd, F_GETFD); |
| 317 | if (flags != -1) { |
| 318 | rc = (flags & FD_CLOEXEC) ? 1 : 0; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return rc; |
| 323 | } |
| 324 | |
| 325 | // Returns -1 for errors, 0 if TCP_DEFER_ACCEPT is unset, 1 if TCP_DEFER_ACCEPT is set |
| 326 | int sock_set_tcp_defer_accept(int fd __maybe_unused, bool defer __maybe_unused) { |
| 327 | #ifdef TCP_DEFER_ACCEPT |
| 328 | // Check if the file descriptor is a socket |
| 329 | if (!fd_is_socket(fd)) |
| 330 | return 0; // Not a socket |
| 331 | |
| 332 | int rc = -1; |
| 333 | int timeout = defer ? 5 : 0; // Set timeout to 5 seconds for enabling, 0 to disable |
| 334 | socklen_t optlen = sizeof(timeout); |
| 335 | |
| 336 | // Attempt to set TCP_DEFER_ACCEPT |
| 337 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, optlen) == 0) { |
| 338 | // Setting was successful, return the intended state |
| 339 | rc = defer ? 1 : 0; |
| 340 | } else if (errno != EINVAL && errno != ENOPROTOOPT) { |
| 341 | // If setting failed and it's not because of invalid option or unsupported protocol |
| 342 | // Check the current state |
| 343 | if (getsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, &optlen) == 0) { |
| 344 | rc = timeout > 0 ? 1 : 0; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return rc; |
| 349 | #else |
| 350 | // TCP_DEFER_ACCEPT not supported |
| 351 | errno = ENOPROTOOPT; |
| 352 | return -1; |
| 353 | #endif |
| 354 | } |
| 355 | |
| 356 | // -------------------------------------------------------------------------------------------------------------------- |
| 357 | // helpers to send/receive data in one call, in blocking mode, with a timeout |
| 358 | |
| 359 | // returns: -1 = thread cancelled, 0 = proceed to read/write, 1 = time exceeded, 2 = error on fd |
| 360 | // timeout parameter can be zero to wait forever |
| 361 | inline int wait_on_socket_or_cancel_with_timeout( |
| 362 | NETDATA_SSL *ssl, |
| 363 | int fd, int timeout_ms, short int poll_events, short int *revents) { |
| 364 | |
| 365 | #if defined(OS_WINDOWS) |
| 366 | // WSAPoll() (used internally by MinGW poll()) only works for sockets. |
| 367 | // For pipe file descriptors (e.g. stdin when launched as a subprocess), |
| 368 | // poll() fails and kills the reader thread. Use PeekNamedPipe instead. |
| 369 | if(poll_events & POLLIN) { |
| 370 | HANDLE h = (HANDLE)_get_osfhandle(fd); |
| 371 | if(h != INVALID_HANDLE_VALUE && GetFileType(h) == FILE_TYPE_PIPE) { |
| 372 | bool forever = (timeout_ms <= 0); |
| 373 | if(revents) |
| 374 | *revents = 0; |
| 375 | while(timeout_ms > 0 || forever) { |
| 376 | if(nd_thread_signaled_to_cancel()) { |
| 377 | errno = ECANCELED; |
| 378 | if(revents) |
| 379 | *revents = 0; |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | DWORD available = 0; |
| 384 | if(!PeekNamedPipe(h, NULL, 0, NULL, &available, NULL)) { |
| 385 | DWORD winerr = GetLastError(); |
| 386 | short pipe_revents = POLLERR; |
| 387 | switch(winerr) { |
| 388 | case ERROR_BROKEN_PIPE: |
| 389 | case ERROR_PIPE_NOT_CONNECTED: |
| 390 | case ERROR_NO_DATA: |
| 391 | errno = EPIPE; |
| 392 | pipe_revents = POLLHUP; |
| 393 | break; |
| 394 | case ERROR_INVALID_HANDLE: |
| 395 | errno = EBADF; |
| 396 | pipe_revents = POLLNVAL; |
| 397 | break; |
| 398 | case ERROR_OPERATION_ABORTED: |
| 399 | errno = ECANCELED; |
| 400 | if(revents) |
| 401 | *revents = 0; |
| 402 | return -1; |
| 403 | default: |
| 404 | errno = EIO; |
| 405 | pipe_revents = POLLERR; |
| 406 | break; |
| 407 | } |
| 408 | if(revents) *revents = pipe_revents; |
| 409 | return 2; |
| 410 | } |
| 411 | |
| 412 | if(available > 0) { |
| 413 | if(revents) *revents = POLLIN; |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | // Waiting on the pipe HANDLE itself is not a reliable readiness |
| 418 | // indicator for named pipes. Use a time-based loop around |
| 419 | // PeekNamedPipe() and sleep only for the cancellability window. |
| 420 | const DWORD wait_ms = (DWORD)((timeout_ms >= ND_CHECK_CANCELLABILITY_WHILE_WAITING_EVERY_MS || forever) ? |
| 421 | ND_CHECK_CANCELLABILITY_WHILE_WAITING_EVERY_MS : timeout_ms); |
| 422 | Sleep(wait_ms); |
| 423 | if(!forever) |
| 424 | timeout_ms -= (int)wait_ms; |
| 425 | } |
| 426 | errno = ETIMEDOUT; |
| 427 | if(revents) |
| 428 | *revents = 0; |
| 429 | return 1; |
| 430 | } |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | struct pollfd pfd = { |
| 435 | .fd = fd, |
| 436 | .events = poll_events, |
| 437 | .revents = 0, |
| 438 | }; |
| 439 | |
| 440 | bool forever = (timeout_ms <= 0); |
| 441 | |
| 442 | while (timeout_ms > 0 || forever) { |
| 443 | if(nd_thread_signaled_to_cancel()) { |
| 444 | errno = ECANCELED; |
| 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | if(poll_events == POLLIN && ssl && SSL_connection(ssl) && netdata_ssl_has_pending(ssl)) |
| 449 | return 0; |
| 450 | |
| 451 | const int wait_ms = (timeout_ms >= ND_CHECK_CANCELLABILITY_WHILE_WAITING_EVERY_MS || forever) ? |
| 452 | ND_CHECK_CANCELLABILITY_WHILE_WAITING_EVERY_MS : timeout_ms; |
| 453 | |
| 454 | errno_clear(); |
| 455 | |
| 456 | // check every wait_ms |
| 457 | const int ret = poll(&pfd, 1, wait_ms); |
| 458 | |
| 459 | if(revents) |
| 460 | *revents = pfd.revents; |
| 461 | |
| 462 | if(ret == -1) { |
| 463 | // poll failed |
| 464 | |
| 465 | if(errno == EINTR || errno == EAGAIN) |
| 466 | continue; |
| 467 | |
| 468 | return 2; |
| 469 | } |
| 470 | |
| 471 | if(ret == 0) { |
| 472 | // timeout |
| 473 | if(!forever) |
| 474 | timeout_ms -= wait_ms; |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | if(pfd.revents & poll_events) |
| 479 | return 0; |
| 480 | |
| 481 | // all other errors |
| 482 | return 2; |
| 483 | } |
| 484 | |
| 485 | errno = ETIMEDOUT; |
| 486 | return 1; |
| 487 | } |
| 488 | |
| 489 | ssize_t send_timeout(NETDATA_SSL *ssl, int sockfd, void *buf, size_t len, int flags, time_t timeout) { |
| 490 | |
| 491 | switch(wait_on_socket_or_cancel_with_timeout(ssl, sockfd, timeout * 1000, POLLOUT, NULL)) { |
| 492 | case 0: // data are waiting |
| 493 | break; |
| 494 | |
| 495 | case 1: // timeout |
| 496 | return 0; |
| 497 | |
| 498 | default: |
| 499 | case -1: // thread cancelled |
| 500 | case 2: // error on socket |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | if(ssl->conn) { |
| 505 | if (SSL_connection(ssl)) |
| 506 | return netdata_ssl_write(ssl, buf, len); |
| 507 | |
| 508 | else { |
| 509 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 510 | "cannot write to SSL connection - connection is not ready."); |
| 511 | |
| 512 | return -1; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return send(sockfd, buf, len, flags); |
| 517 | } |
| 518 | |
| 519 | // -------------------------------------------------------------------------------------------------------------------- |
| 520 | // accept4() replacement for systems that do not have one |
| 521 | |
| 522 | #ifndef HAVE_ACCEPT4 |
| 523 | int accept4(int sock, struct sockaddr *addr, socklen_t *addrlen, int flags) { |
| 524 | int fd = accept(sock, addr, addrlen); |
| 525 | int newflags = 0; |
| 526 | |
| 527 | if (fd < 0) return fd; |
| 528 | |
| 529 | #ifdef SOCK_CLOEXEC |
| 530 | #ifdef O_CLOEXEC |
| 531 | if (flags & SOCK_CLOEXEC) { |
| 532 | newflags |= O_CLOEXEC; |
| 533 | flags &= ~SOCK_CLOEXEC; |
| 534 | } |
| 535 | #endif |
| 536 | #endif |
| 537 | |
| 538 | if (flags) { |
| 539 | close(fd); |
| 540 | errno = EINVAL; |
| 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | if (fcntl(fd, F_SETFL, newflags) < 0) { |
| 545 | int saved_errno = errno; |
| 546 | close(fd); |
| 547 | errno = saved_errno; |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | return fd; |
| 552 | } |
| 553 | #endif |
| 554 | |
| 555 | /* |
| 556 | * --------------------------------------------------------------------------------------------------------------------- |
| 557 | * connection_allowed() - if there is an access list then check the connection matches a pattern. |
| 558 | * Numeric patterns are checked against the IP address first, only if they |
| 559 | * do not match is the hostname resolved (reverse-DNS) and checked. If the |
| 560 | * hostname matches then we perform forward DNS resolution to check the IP |
| 561 | * is really associated with the DNS record. This call is repeatable: the |
| 562 | * web server may check more refined matches against the connection. Will |
| 563 | * update the client_host if uninitialized - ensure the hostsize is the number |
| 564 | * of *writable* bytes (i.e. be aware of the strdup used to compact the pollinfo). |
| 565 | */ |
| 566 | int connection_allowed(int fd, char *client_ip, char *client_host, size_t hostsize, SIMPLE_PATTERN *access_list, |
| 567 | const char *patname, int allow_dns) |
| 568 | { |
| 569 | if (!access_list) |
| 570 | return 1; |
| 571 | if (simple_pattern_matches(access_list, client_ip)) |
| 572 | return 1; |
| 573 | // If the hostname is unresolved (and needed) then attempt the DNS lookups. |
| 574 | //if (client_host[0]==0 && simple_pattern_is_potential_name(access_list)) |
| 575 | if (client_host[0]==0 && allow_dns) |
| 576 | { |
| 577 | struct sockaddr_storage sadr; |
| 578 | socklen_t addrlen = sizeof(sadr); |
| 579 | int err = getpeername(fd, (struct sockaddr*)&sadr, &addrlen); |
| 580 | if (err != 0 || |
| 581 | (err = getnameinfo((struct sockaddr *)&sadr, addrlen, client_host, (socklen_t)hostsize, |
| 582 | NULL, 0, NI_NAMEREQD)) != 0) { |
| 583 | |
| 584 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 585 | "Incoming %s on '%s' does not match a numeric pattern, and host could not be resolved (err=%s)", |
| 586 | patname, client_ip, gai_strerror(err)); |
| 587 | |
| 588 | if (hostsize >= 8) |
| 589 | strcpy(client_host,"UNKNOWN"); |
| 590 | return 0; |
| 591 | } |
| 592 | struct addrinfo *addr_infos = NULL; |
| 593 | if (getaddrinfo(client_host, NULL, NULL, &addr_infos) !=0 ) { |
| 594 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 595 | "LISTENER: cannot validate hostname '%s' from '%s' by resolving it", |
| 596 | client_host, client_ip); |
| 597 | |
| 598 | if (hostsize >= 8) |
| 599 | strcpy(client_host,"UNKNOWN"); |
| 600 | return 0; |
| 601 | } |
| 602 | struct addrinfo *scan = addr_infos; |
| 603 | int validated = 0; |
| 604 | while (scan) { |
| 605 | char address[INET6_ADDRSTRLEN]; |
| 606 | address[0] = 0; |
| 607 | switch (scan->ai_addr->sa_family) { |
| 608 | case AF_INET: |
| 609 | inet_ntop(AF_INET, &((struct sockaddr_in*)(scan->ai_addr))->sin_addr, address, INET6_ADDRSTRLEN); |
| 610 | break; |
| 611 | case AF_INET6: |
| 612 | inet_ntop(AF_INET6, &((struct sockaddr_in6*)(scan->ai_addr))->sin6_addr, address, INET6_ADDRSTRLEN); |
| 613 | break; |
| 614 | } |
| 615 | if (!strcmp(client_ip, address)) { |
| 616 | validated = 1; |
| 617 | break; |
| 618 | } |
| 619 | scan = scan->ai_next; |
| 620 | } |
| 621 | if (!validated) { |
| 622 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 623 | "LISTENER: Cannot validate '%s' as ip of '%s', not listed in DNS", |
| 624 | client_ip, client_host); |
| 625 | |
| 626 | if (hostsize >= 8) |
| 627 | strcpy(client_host,"UNKNOWN"); |
| 628 | } |
| 629 | if (addr_infos!=NULL) |
| 630 | freeaddrinfo(addr_infos); |
| 631 | } |
| 632 | if (!simple_pattern_matches(access_list, client_host)) |
| 633 | return 0; |
| 634 | |
| 635 | return 1; |
| 636 | } |
| 637 | |
| 638 | // -------------------------------------------------------------------------------------------------------------------- |
| 639 | // accept_socket() - accept a socket and store client IP and port |
| 640 | int accept_socket(int fd, int flags, char *client_ip, size_t ipsize, char *client_port, size_t portsize, |
| 641 | char *client_host, size_t hostsize, SIMPLE_PATTERN *access_list, int allow_dns) { |
| 642 | struct sockaddr_storage sadr; |
| 643 | socklen_t addrlen = sizeof(sadr); |
| 644 | |
| 645 | int nfd = accept4(fd, (struct sockaddr *)&sadr, &addrlen, flags | DEFAULT_SOCKET_FLAGS); |
| 646 | if (likely(nfd >= 0)) { |
| 647 | if (getnameinfo((struct sockaddr *)&sadr, addrlen, client_ip, (socklen_t)ipsize, |
| 648 | client_port, (socklen_t)portsize, NI_NUMERICHOST | NI_NUMERICSERV) != 0) { |
| 649 | |
| 650 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 651 | "LISTENER: cannot getnameinfo() on received client connection."); |
| 652 | |
| 653 | strncpyz(client_ip, "UNKNOWN", ipsize); |
| 654 | strncpyz(client_port, "UNKNOWN", portsize); |
| 655 | } |
| 656 | if (!strcmp(client_ip, "127.0.0.1") || !strcmp(client_ip, "::1")) { |
| 657 | strncpyz(client_ip, "localhost", ipsize); |
| 658 | } |
| 659 | sock_setcloexec(nfd, true); |
| 660 | |
| 661 | #ifdef __FreeBSD__ |
| 662 | if(((struct sockaddr *)&sadr)->sa_family == AF_LOCAL) |
| 663 | strncpyz(client_ip, "localhost", ipsize); |
| 664 | #endif |
| 665 | |
| 666 | client_ip[ipsize - 1] = '\0'; |
| 667 | client_port[portsize - 1] = '\0'; |
| 668 | |
| 669 | switch (((struct sockaddr *)&sadr)->sa_family) { |
| 670 | case AF_UNIX: |
| 671 | // netdata_log_debug(D_LISTENER, "New UNIX domain web client from %s on socket %d.", client_ip, fd); |
| 672 | // set the port - certain versions of libc return garbage on unix sockets |
| 673 | strncpyz(client_port, "UNIX", portsize); |
| 674 | break; |
| 675 | |
| 676 | case AF_INET: |
| 677 | // netdata_log_debug(D_LISTENER, "New IPv4 web client from %s port %s on socket %d.", client_ip, client_port, fd); |
| 678 | break; |
| 679 | |
| 680 | case AF_INET6: |
| 681 | if (strncmp(client_ip, "::ffff:", 7) == 0) { |
| 682 | memmove(client_ip, &client_ip[7], strlen(&client_ip[7]) + 1); |
| 683 | // netdata_log_debug(D_LISTENER, "New IPv4 web client from %s port %s on socket %d.", client_ip, client_port, fd); |
| 684 | } |
| 685 | // else |
| 686 | // netdata_log_debug(D_LISTENER, "New IPv6 web client from %s port %s on socket %d.", client_ip, client_port, fd); |
| 687 | break; |
| 688 | |
| 689 | default: |
| 690 | // netdata_log_debug(D_LISTENER, "New UNKNOWN web client from %s port %s on socket %d.", client_ip, client_port, fd); |
| 691 | break; |
| 692 | } |
| 693 | if (!connection_allowed(nfd, client_ip, client_host, hostsize, access_list, "connection", allow_dns)) { |
| 694 | errno_clear(); |
| 695 | nd_log(NDLS_DAEMON, NDLP_WARNING, |
| 696 | "Permission denied for client '%s', port '%s'", |
| 697 | client_ip, client_port); |
| 698 | |
| 699 | close(nfd); |
| 700 | nfd = -1; |
| 701 | errno = EPERM; |
| 702 | } |
| 703 | } |
| 704 | #ifdef HAVE_ACCEPT4 |
| 705 | else if (errno == ENOSYS) |
| 706 | nd_log(NDLS_DAEMON, NDLP_ERR, |
| 707 | "Netdata has been compiled with the assumption that the system has the accept4() call, but it is not here. " |
| 708 | "Recompile netdata like this: ./configure --disable-accept4 ..."); |
| 709 | #endif |
| 710 | |
| 711 | return nfd; |
| 712 | } |