listensocket.c 22 KB
Newer Older
1 2 3 4 5
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
6
 * Copyright 2018-2020, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
7 8 9 10 11 12 13 14 15 16 17 18
 */

/**
 * Listen socket operations.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_POLL
#include <poll.h>
19
#elif HAVE_SYS_SELECT_H
20 21 22 23 24 25
#include <sys/select.h>
#endif

#include <string.h>

#include "common/net/sock.h"
26
#include "common/thread/thread.h"
27 28 29 30 31 32 33 34 35 36 37 38

#include "listensocket.h"
#include "global.h"
#include "connection.h"
#include "refobject.h"

#include "logging.h"
#define CATMODULE "listensocket"


struct listensocket_container_tag {
    refobject_base_t __base;
39
    mutex_t lock;
40 41 42
    listensocket_t **sock;
    int *sockref;
    size_t sock_len;
43 44
    void (*sockcount_cb)(size_t count, void *userdata);
    void *sockcount_userdata;
45 46 47 48
};
struct listensocket_tag {
    refobject_base_t __base;
    size_t sockrefc;
49 50
    mutex_t lock;
    rwlock_t listener_rwlock;
51
    listener_t *listener;
52
    listener_t *listener_update;
53 54 55
    sock_t sock;
};

56 57 58
static int listensocket_container_configure__unlocked(listensocket_container_t *self, const ice_config_t *config);
static int listensocket_container_setup__unlocked(listensocket_container_t *self);
static ssize_t listensocket_container_sockcount__unlocked(listensocket_container_t *self);
59
static listensocket_t * listensocket_new(const listener_t *listener);
60
static int              listensocket_apply_config(listensocket_t *self);
61
static int              listensocket_apply_config__unlocked(listensocket_t *self);
62
static int              listensocket_set_update(listensocket_t *self, const listener_t *listener);
63
#ifdef HAVE_POLL
64
static inline int listensocket__poll_fill(listensocket_t *self, struct pollfd *p);
65
#else
66 67
static inline int listensocket__select_set(listensocket_t *self, fd_set *set, int *max);
static inline int listensocket__select_isset(listensocket_t *self, fd_set *set);
68 69
#endif

70 71 72 73 74
static inline const char * __string_default(const char *str, const char *def)
{
    return str != NULL ? str : def;
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88
static inline int __socket_listen(sock_t serversock, const listener_t *listener)
{
    int listen_backlog = listener->listen_backlog;

    if (listen_backlog < 1)
        listen_backlog = ICECAST_LISTEN_QUEUE;
    if (listen_backlog > 128) {
        listen_backlog = 128;
        ICECAST_LOG_WARN("Listen backlog for listen socket on %s port %i is set insanely high. Limiting to sane range.", __string_default(listener->bind_address, "<ANY>"), listener->port);
    }

    return sock_listen(serversock, listen_backlog);
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static inline int __listener_cmp(const listener_t *a, const listener_t *b)
{
    if (a == b)
        return 1;

    if (a->port != b->port)
        return 0;

    if ((a->bind_address == NULL && b->bind_address != NULL) ||
        (a->bind_address != NULL && b->bind_address == NULL))
        return 0;


    if (a->bind_address != NULL && b->bind_address != NULL && strcmp(a->bind_address, b->bind_address) != 0)
        return 0;

    return 1;
}

108 109 110 111 112
static inline void __call_sockcount_cb(listensocket_container_t *self)
{
    if (self->sockcount_cb == NULL)
        return;

113
    self->sockcount_cb(listensocket_container_sockcount__unlocked(self), self->sockcount_userdata);
114 115
}

116
static void __listensocket_container_clear_sockets(listensocket_container_t *self)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
{
    size_t i;

    if (self->sock == NULL)
        return;

    for (i = 0; i < self->sock_len; i++) {
        if (self->sock[i] != NULL) {
            if (self->sockref[i]) {
                listensocket_unrefsock(self->sock[i]);
            }
            refobject_unref(self->sock[i]);
            self->sock[i] = NULL;
        }
    }

    self->sock_len = 0;
    free(self->sock);
    free(self->sockref);
    self->sock = NULL;
    self->sockref = NULL;
138 139

    __call_sockcount_cb(self);
140 141 142 143 144 145
}


static void __listensocket_container_free(refobject_t self, void **userdata)
{
    listensocket_container_t *container = REFOBJECT_TO_TYPE(self, listensocket_container_t *);
146 147 148 149
    thread_mutex_lock(&container->lock);
    __listensocket_container_clear_sockets(container);
    thread_mutex_unlock(&container->lock);
    thread_mutex_destroy(&container->lock);
150 151
}

152
int __listensocket_container_new(refobject_t self, const refobject_type_t *type, va_list ap)
153
{
154
    listensocket_container_t *ret = REFOBJECT_TO_TYPE(self, listensocket_container_t*);
155

156 157 158 159
    ret->sock = NULL;
    ret->sock_len = 0;
    ret->sockcount_cb = NULL;
    ret->sockcount_userdata = NULL;
160

161
    thread_mutex_create(&ret->lock);
162

163
    return 0;
164 165
}

166 167 168 169 170
REFOBJECT_DEFINE_TYPE(listensocket_container_t,
        REFOBJECT_DEFINE_TYPE_FREE(__listensocket_container_free),
        REFOBJECT_DEFINE_TYPE_NEW(__listensocket_container_new)
        );

171 172 173
static inline void __find_matching_entry(listensocket_container_t *self, const listener_t *listener, listensocket_t ***found, int **ref)
{
    const listener_t *b;
174
    int test;
175 176 177 178 179 180
    size_t i;

    for (i = 0; i < self->sock_len; i++) {
        if (self->sock[i] != NULL) {
            if (self->sockref[i]) {
                b = listensocket_get_listener(self->sock[i]);
181 182 183
                test = __listener_cmp(listener, b);
                listensocket_release_listener(self->sock[i]);
                if (test == 1) {
184 185 186 187 188 189 190 191 192 193 194 195
                    *found = &(self->sock[i]);
                    *ref = &(self->sockref[i]);
                    return;
                }
            }
        }
    }

    *found = NULL;
    *ref = NULL;
}

196
int                         listensocket_container_configure(listensocket_container_t *self, const ice_config_t *config)
197 198 199 200 201 202 203 204 205 206 207 208 209 210
{
    int ret;

    if (!self)
        return -1;

    thread_mutex_lock(&self->lock);
    ret = listensocket_container_configure__unlocked(self, config);
    thread_mutex_unlock(&self->lock);

    return ret;
}

static int listensocket_container_configure__unlocked(listensocket_container_t *self, const ice_config_t *config)
211 212
{
    listensocket_t **n;
213
    listensocket_t **match;
214 215
    listener_t *cur;
    int *r;
216
    int *m;
217 218 219 220 221 222
    size_t i;

    if (!self || !config)
        return -1;

    if (!config->listen_sock_count) {
223
        __listensocket_container_clear_sockets(self);
224 225 226 227 228 229 230 231 232 233 234 235 236 237
        return 0;
    }

    n = calloc(config->listen_sock_count, sizeof(listensocket_t *));
    r = calloc(config->listen_sock_count, sizeof(int));
    if (!n || !r) {
        free(n);
        free(r);
        return -1;
    }

    cur = config->listen_sock;
    for (i = 0; i < config->listen_sock_count; i++) {
        if (cur) {
238 239 240 241 242 243 244 245 246 247
            __find_matching_entry(self, cur, &match, &m);
            if (match) {
                n[i] = *match;
                r[i] = 1;
                *match = NULL;
                *m = 0;
                listensocket_set_update(n[i], cur);
            } else {
                n[i] = listensocket_new(cur);
            }
248 249 250 251 252 253 254 255 256 257 258 259 260
        } else {
            n[i] = NULL;
        }
        if (n[i] == NULL) {
            for (; i; i--) {
                refobject_unref(n[i - 1]);
            }
            return -1;
        }

        cur = cur->next;
    }

261
    __listensocket_container_clear_sockets(self);
262 263 264 265 266 267 268 269

    self->sock = n;
    self->sockref = r;
    self->sock_len = config->listen_sock_count;

    return 0;
}

270 271 272 273 274 275 276 277
int                         listensocket_container_configure_and_setup(listensocket_container_t *self, const ice_config_t *config)
{
    void (*cb)(size_t count, void *userdata);
    int ret;

    if (!self)
        return -1;

278
    thread_mutex_lock(&self->lock);
279 280 281
    cb = self->sockcount_cb;
    self->sockcount_cb = NULL;

282 283
    if (listensocket_container_configure__unlocked(self, config) == 0) {
        ret = listensocket_container_setup__unlocked(self);
284 285 286 287 288 289
    } else {
        ret = -1;
    }

    self->sockcount_cb = cb;
    __call_sockcount_cb(self);
290
    thread_mutex_unlock(&self->lock);
291 292 293 294

    return ret;
}

295 296 297
int                         listensocket_container_setup(listensocket_container_t *self)
{
    int ret;
298 299 300 301

    if (!self)
        return -1;

302 303 304 305 306 307 308 309
    thread_mutex_lock(&self->lock);
    ret = listensocket_container_setup__unlocked(self);
    thread_mutex_unlock(&self->lock);

    return ret;
}
static int listensocket_container_setup__unlocked(listensocket_container_t *self)
{
310
    listener_type_t type;
311 312 313
    size_t i;
    int ret = 0;

314
    for (i = 0; i < self->sock_len; i++) {
315 316 317 318 319 320 321 322
        listensocket_apply_config(self->sock[i]);

        type = listensocket_get_type(self->sock[i]);
        if (self->sockref[i] && type == LISTENER_TYPE_VIRTUAL) {
            if (listensocket_unrefsock(self->sock[i]) == 0) {
                self->sockref[i] = 0;
            }
        } else if (!self->sockref[i] && type != LISTENER_TYPE_VIRTUAL) {
323 324 325 326 327 328
            if (listensocket_refsock(self->sock[i]) == 0) {
                self->sockref[i] = 1;
            } else {
                ICECAST_LOG_DEBUG("Can not ref socket.");
                ret = 1;
            }
329 330 331
        }
    }

332 333
    __call_sockcount_cb(self);

334 335 336
    return ret;
}

337
static listensocket_t *       listensocket_container_accept__inner(listensocket_container_t *self, int timeout)
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
{
#ifdef HAVE_POLL
    struct pollfd ufds[self->sock_len];
    listensocket_t *socks[self->sock_len];
    size_t i, found, p;
    int ok;
    int ret;

    for (i = 0, found = 0; i < self->sock_len; i++) {
        ok = self->sockref[i];

        if (ok && listensocket__poll_fill(self->sock[i], &(ufds[found])) == -1) {
            ICECAST_LOG_WARN("Can not poll on closed socket.");
            ok = 0;
        }

        if (ok) {
            socks[found] = self->sock[i];
            found++;
        }
    }

    if (!found) {
        ICECAST_LOG_ERROR("No sockets found to poll on.");
        return NULL;
    }

    ret = poll(ufds, found, timeout);
    if (ret <= 0)
        return NULL;

    for (i = 0; i < found; i++) {
        if (ufds[i].revents & POLLIN) {
371
            return socks[i];
372 373 374 375 376 377 378 379 380 381 382
        }

        if (!(ufds[i].revents & (POLLHUP|POLLERR|POLLNVAL)))
            continue;

        for (p = 0; p < self->sock_len; p++) {
            if (self->sock[p] == socks[i]) {
                if (self->sockref[p]) {
                    ICECAST_LOG_ERROR("Closing listen socket in error state.");
                    listensocket_unrefsock(socks[i]);
                    self->sockref[p] = 0;
383
                    __call_sockcount_cb(self);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
                }
            }
        }
    }

    return NULL;
#else
    fd_set rfds;
    size_t i;
    struct timeval tv, *p=NULL;
    int ret;
    int max = -1;

    FD_ZERO(&rfds);

    if (timeout >= 0) {
        tv.tv_sec = timeout/1000;
        tv.tv_usec = (timeout % 1000) * 1000;
        p = &tv;
    }

    for (i = 0; i < self->sock_len; i++) {
        if (self->sockref[i]) {
            listensocket__select_set(self->sock[i], &rfds, &max);
        }
    }

    ret = select(max+1, &rfds, NULL, NULL, p);
    if (ret <= 0)
        return NULL;

    for (i = 0; i < self->sock_len; i++) {
        if (self->sockref[i]) {
            if (listensocket__select_isset(self->sock[i], &rfds)) {
418
                return self->sock[i];
419 420 421 422 423 424 425 426 427
            }
        }
    }

    return NULL;
#endif
}
connection_t *              listensocket_container_accept(listensocket_container_t *self, int timeout)
{
428
    listensocket_t *ls;
429 430
    connection_t *ret;

431 432 433
    if (!self)
        return NULL;

434
    thread_mutex_lock(&self->lock);
435 436
    ls = listensocket_container_accept__inner(self, timeout);
    refobject_ref(ls);
437
    thread_mutex_unlock(&self->lock);
438 439 440 441

    ret = listensocket_accept(ls, self);
    refobject_unref(ls);

442
    return ret;
443 444
}

445 446 447 448 449
int                         listensocket_container_set_sockcount_cb(listensocket_container_t *self, void (*cb)(size_t count, void *userdata), void *userdata)
{
    if (!self)
        return -1;

450
    thread_mutex_lock(&self->lock);
451 452
    self->sockcount_cb = cb;
    self->sockcount_userdata = userdata;
453
    thread_mutex_unlock(&self->lock);
454 455 456 457 458 459

    return 0;
}

ssize_t                     listensocket_container_sockcount(listensocket_container_t *self)
{
460
    ssize_t ret;
461 462 463 464

    if (!self)
        return -1;

465 466 467 468 469 470 471 472 473 474 475 476
    thread_mutex_lock(&self->lock);
    ret = listensocket_container_sockcount__unlocked(self);
    thread_mutex_unlock(&self->lock);

    return ret;
}

static ssize_t listensocket_container_sockcount__unlocked(listensocket_container_t *self)
{
    ssize_t count = 0;
    size_t i;

477 478 479 480 481 482 483 484 485
    for (i = 0; i < self->sock_len; i++) {
        if (self->sockref[i]) {
            count++;
        }
    }

    return count;
}

486
listensocket_t * listensocket_container_get_by_id(listensocket_container_t *self, const char *id)
487 488 489 490 491 492 493 494
{
    size_t i;
    const listener_t *listener;

    for (i = 0; i < self->sock_len; i++) {
        if (self->sock[i] != NULL) {
            listener = listensocket_get_listener(self->sock[i]);
            if (listener) {
495
                if (listener->id != NULL && strcmp(listener->id, id) == 0) {
496
                    if (refobject_ref(self->sock[i]) == 0) {
497
                        listensocket_release_listener(self->sock[i]);
498 499 500 501 502 503 504 505 506 507 508
                        return self->sock[i];
                    }
                }
                listensocket_release_listener(self->sock[i]);
            }
        }
    }

    return NULL;
}

509 510
/* ---------------------------------------------------------------------------- */

511 512 513 514
static void __listensocket_free(refobject_t self, void **userdata)
{
    listensocket_t *listensocket = REFOBJECT_TO_TYPE(self, listensocket_t *);

515 516
    thread_mutex_lock(&listensocket->lock);

517 518 519 520 521 522
    if (listensocket->sockrefc) {
        ICECAST_LOG_ERROR("BUG: listensocket->sockrefc == 0 && listensocket->sockrefc == %zu", listensocket->sockrefc);
        listensocket->sockrefc = 1;
        listensocket_unrefsock(listensocket);
    }

523
    while ((listensocket->listener_update = config_clear_listener(listensocket->listener_update)));
524 525 526 527 528 529
    thread_rwlock_wlock(&listensocket->listener_rwlock);
    while ((listensocket->listener = config_clear_listener(listensocket->listener)));
    thread_rwlock_unlock(&listensocket->listener_rwlock);
    thread_rwlock_destroy(&listensocket->listener_rwlock);
    thread_mutex_unlock(&listensocket->lock);
    thread_mutex_destroy(&listensocket->lock);
530 531
}

532 533 534 535
REFOBJECT_DEFINE_TYPE(listensocket_t,
        REFOBJECT_DEFINE_TYPE_FREE(__listensocket_free)
        );

536 537 538 539 540 541
static listensocket_t * listensocket_new(const listener_t *listener) {
    listensocket_t *self;

    if (listener == NULL)
        return NULL;

542
    self = refobject_new__new(listensocket_t, NULL, NULL, NULL);
543 544 545 546 547
    if (!self)
        return NULL;

    self->sock = SOCK_ERROR;

548 549 550
    thread_mutex_create(&self->lock);
    thread_rwlock_create(&self->listener_rwlock);

551 552 553 554 555 556 557 558 559
    self->listener = config_copy_listener_one(listener);
    if (self->listener == NULL) {
        refobject_unref(self);
        return NULL;
    }

    return self;
}

560
static int              listensocket_apply_config(listensocket_t *self)
561 562 563 564 565 566 567 568 569 570 571 572 573 574
{
    int ret;

    if (!self)
        return -1;

    thread_mutex_lock(&self->lock);
    ret = listensocket_apply_config__unlocked(self);
    thread_mutex_unlock(&self->lock);

    return ret;
}

static int              listensocket_apply_config__unlocked(listensocket_t *self)
575
{
576
    const listener_t *listener;
577

578
    if (!self)
579 580
        return -1;

581
    thread_rwlock_wlock(&self->listener_rwlock);
582 583
    if (self->listener_update) {
        if (__listener_cmp(self->listener, self->listener_update) != 1) {
584
            ICECAST_LOG_ERROR("Tried to apply incomplete configuration to listensocket: bind address missmatch: have %s:%i, got %s:%i",
585
                                __string_default(self->listener->bind_address, "<ANY>"),
586
                                self->listener->port,
587 588
                                __string_default(self->listener_update->bind_address, "<ANY>"),
                                self->listener_update->port
589
                             );
590
            thread_rwlock_unlock(&self->listener_rwlock);
591 592 593
            return -1;
        }

594 595 596
        listener = self->listener_update;
    } else {
        listener = self->listener;
597 598
    }

599 600 601
    if (self->sock != SOCK_ERROR) {
        if (listener->so_sndbuf)
            sock_set_send_buffer(self->sock, listener->so_sndbuf);
602

603
        sock_set_blocking(self->sock, 0);
604 605

        __socket_listen(self->sock, listener);
606
    }
607

608
    if (self->listener_update) {
609
        while ((self->listener = config_clear_listener(self->listener)));
610 611
        self->listener = self->listener_update;
        self->listener_update = NULL;
612 613
    }

614 615
    thread_rwlock_unlock(&self->listener_rwlock);

616 617 618
    return 0;
}

619 620 621 622 623 624 625 626 627 628 629
static int              listensocket_set_update(listensocket_t *self, const listener_t *listener)
{
    listener_t *n;

    if (!self || !listener)
        return -1;

    n = config_copy_listener_one(listener);
    if (n == NULL)
        return -1;

630
    thread_mutex_lock(&self->lock);
631 632
    while ((self->listener_update = config_clear_listener(self->listener_update)));
    self->listener_update = n;
633
    thread_mutex_unlock(&self->lock);
634 635 636
    return 0;
}

637 638 639 640 641
int                         listensocket_refsock(listensocket_t *self)
{
    if (!self)
        return -1;

642
    thread_mutex_lock(&self->lock);
643 644
    if (self->sockrefc) {
        self->sockrefc++;
645
        thread_mutex_unlock(&self->lock);
646 647 648
        return 0;
    }

649
    thread_rwlock_rlock(&self->listener_rwlock);
650
    self->sock = sock_get_server_socket(self->listener->port, self->listener->bind_address);
651 652 653
    thread_rwlock_unlock(&self->listener_rwlock);
    if (self->sock == SOCK_ERROR) {
        thread_mutex_unlock(&self->lock);
654
        return -1;
655
    }
656

657
    if (__socket_listen(self->sock, self->listener) == 0) {
658 659
        sock_close(self->sock);
        self->sock = SOCK_ERROR;
660
        thread_rwlock_rlock(&self->listener_rwlock);
661
        ICECAST_LOG_ERROR("Can not listen on socket: %s port %i", __string_default(self->listener->bind_address, "<ANY>"), self->listener->port);
662 663
        thread_rwlock_unlock(&self->listener_rwlock);
        thread_mutex_unlock(&self->lock);
664 665 666
        return -1;
    }

667 668
    if (listensocket_apply_config__unlocked(self) == -1) {
        thread_mutex_unlock(&self->lock);
669
        return -1;
670
    }
671 672

    self->sockrefc++;
673
    thread_mutex_unlock(&self->lock);
674 675 676 677 678 679 680 681 682

    return 0;
}

int                         listensocket_unrefsock(listensocket_t *self)
{
    if (!self)
        return -1;

683
    thread_mutex_lock(&self->lock);
684
    self->sockrefc--;
685 686
    if (self->sockrefc) {
        thread_mutex_unlock(&self->lock);
687
        return 0;
688
    }
689

690 691
    if (self->sock == SOCK_ERROR) {
        thread_mutex_unlock(&self->lock);
692
        return 0;
693
    }
694 695 696

    sock_close(self->sock);
    self->sock = SOCK_ERROR;
697
    thread_mutex_unlock(&self->lock);
698 699 700 701

    return 0;
}

702
connection_t *              listensocket_accept(listensocket_t *self, listensocket_container_t *container)
703 704
{
    connection_t *con;
705
    listensocket_t *effective = NULL;
706 707 708 709 710 711 712 713 714 715
    sock_t sock;
    char *ip;

    if (!self)
        return NULL;

    ip = calloc(MAX_ADDR_LEN, 1);
    if (!ip)
        return NULL;

716
    thread_mutex_lock(&self->lock);
717
    sock = sock_accept(self->sock, ip, MAX_ADDR_LEN);
718
    thread_mutex_unlock(&self->lock);
719 720 721 722 723 724 725 726 727
    if (sock == SOCK_ERROR) {
        free(ip);
        return NULL;
    }

    if (strncmp(ip, "::ffff:", 7) == 0) {
        memmove(ip, ip+7, strlen(ip+7)+1);
    }

728
    ICECAST_LOG_DEBUG("Client on socket %p \"%H\".", self, self->listener->id);
729 730 731 732 733 734 735 736 737

    if (self->listener->on_behalf_of) {
        ICECAST_LOG_DEBUG("This socket is acting on behalf of \"%H\"", self->listener->on_behalf_of);
        effective = listensocket_container_get_by_id(container, self->listener->on_behalf_of);
        if (!effective) {
            ICECAST_LOG_ERROR("Can not find listen socket with ID \"%H\". Will continue on behalf of myself.", self->listener->on_behalf_of);
        }
    }

738 739 740 741 742 743 744 745 746
    if (!effective) {
        effective = self;
        refobject_ref(effective);
    }

    con = connection_create(sock, self, effective, ip);

    refobject_unref(effective);

747 748 749 750 751 752 753 754 755 756 757
    if (con == NULL) {
        sock_close(sock);
        free(ip);
        return NULL;
    }

    return con;
}

const listener_t *          listensocket_get_listener(listensocket_t *self)
{
758 759
    const listener_t *ret;

760 761 762
    if (!self)
        return NULL;

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
    thread_mutex_lock(&self->lock);
    thread_rwlock_rlock(&self->listener_rwlock);
    ret = self->listener;
    thread_mutex_unlock(&self->lock);

    return ret;
}

int                         listensocket_release_listener(listensocket_t *self)
{
    if (!self)
        return -1;

    /* This is safe with no self->lock holding as unref requires a wlock.
     * A wlock can not be acquired when someone still holds the rlock.
     * In fact this must be done in unlocked state as otherwise we could end up in a
     * dead lock with some 3rd party holding the self->lock for an unrelated operation
     * waiting for a wlock to be come available.
     * -- ph3-der-loewe, 2018-05-11
     */
    thread_rwlock_unlock(&self->listener_rwlock);

    return 0;
786 787
}

788 789 790 791 792 793 794 795 796 797 798 799 800 801
listener_type_t             listensocket_get_type(listensocket_t *self)
{
    listener_type_t ret;

    if (!self)
        return LISTENER_TYPE_ERROR;

    thread_mutex_lock(&self->lock);
    ret = self->listener->type;
    thread_mutex_unlock(&self->lock);

    return ret;
}

802
#ifdef HAVE_POLL
803
static inline int listensocket__poll_fill(listensocket_t *self, struct pollfd *p)
804
{
805
    if (!self)
806 807
        return -1;

808 809 810 811 812 813
    thread_mutex_lock(&self->lock);
    if (self->sock == SOCK_ERROR) {
        thread_mutex_unlock(&self->lock);
        return -1;
    }

814 815 816 817 818
    memset(p, 0, sizeof(*p));
    p->fd = self->sock;
    p->events = POLLIN;
    p->revents = 0;

819 820
    thread_mutex_unlock(&self->lock);

821 822 823
    return 0;
}
#else
824
static inline int listensocket__select_set(listensocket_t *self, fd_set *set, int *max)
825
{
826
    if (!self)
827 828
        return -1;

829 830 831 832 833 834
    thread_mutex_lock(&self->lock);
    if (self->sock == SOCK_ERROR) {
        thread_mutex_unlock(&self->lock);
        return -1;
    }

835 836 837 838
    if (*max < self->sock)
        *max = self->sock;

    FD_SET(self->sock, set);
839 840
    thread_mutex_unlock(&self->lock);

841 842
    return 0;
}
843
static inline int listensocket__select_isset(listensocket_t *self, fd_set *set)
844
{
845 846 847
    int ret;

    if (!self)
848
        return -1;
849 850 851 852 853 854 855 856 857 858

    thread_mutex_lock(&self->lock);
    if (self->sock == SOCK_ERROR) {
        thread_mutex_unlock(&self->lock);
        return -1;
    }
    ret = FD_ISSET(self->sock, set);
    thread_mutex_unlock(&self->lock);
    return ret;

859 860
}
#endif