[BACK]Return to listen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sndiod

Annotation of src/usr.bin/sndiod/listen.c, Revision 1.6

1.5       ratchov     1: /*     $OpenBSD: listen.c,v 1.4 2015/12/14 17:44:29 ratchov Exp $      */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <sys/types.h>
                     18: #include <sys/socket.h>
                     19: #include <sys/signal.h>
                     20: #include <sys/stat.h>
                     21: #include <sys/un.h>
                     22:
                     23: #include <netinet/in.h>
                     24: #include <netinet/tcp.h>
                     25: #include <netdb.h>
                     26:
                     27: #include <err.h>
                     28: #include <errno.h>
                     29: #include <fcntl.h>
                     30: #include <poll.h>
                     31: #include <stdio.h>
                     32: #include <stdlib.h>
                     33: #include <string.h>
                     34: #include <unistd.h>
                     35:
                     36: #include "listen.h"
                     37: #include "file.h"
                     38: #include "sock.h"
                     39: #include "utils.h"
                     40:
                     41: int listen_pollfd(void *, struct pollfd *);
                     42: int listen_revents(void *, struct pollfd *);
                     43: void listen_in(void *);
                     44: void listen_out(void *);
                     45: void listen_hup(void *);
                     46:
                     47: struct fileops listen_fileops = {
                     48:        "listen",
                     49:        listen_pollfd,
                     50:        listen_revents,
                     51:        listen_in,
                     52:        listen_out,
                     53:        listen_hup
                     54: };
                     55:
                     56: struct listen *listen_list = NULL;
                     57:
                     58: void
                     59: listen_close(struct listen *f)
                     60: {
                     61:        struct listen **pf;
                     62:
                     63:        for (pf = &listen_list; *pf != f; pf = &(*pf)->next) {
                     64: #ifdef DEBUG
                     65:                if (*pf == NULL) {
                     66:                        log_puts("listen_close: not on list\n");
                     67:                        panic();
                     68:                }
                     69: #endif
                     70:        }
                     71:        *pf = f->next;
                     72:
1.6     ! ratchov    73:        if (f->path != NULL)
1.1       ratchov    74:                xfree(f->path);
                     75:        file_del(f->file);
                     76:        close(f->fd);
                     77:        xfree(f);
                     78: }
                     79:
                     80: void
                     81: listen_new_un(char *path)
                     82: {
                     83:        int sock, oldumask;
                     84:        struct sockaddr_un sockname;
                     85:        struct listen *f;
                     86:
                     87:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                     88:        if (sock < 0) {
                     89:                perror("socket");
                     90:                exit(1);
                     91:        }
                     92:        if (unlink(path) < 0 && errno != ENOENT) {
                     93:                perror("unlink");
                     94:                goto bad_close;
                     95:        }
                     96:        sockname.sun_family = AF_UNIX;
                     97:        strlcpy(sockname.sun_path, path, sizeof(sockname.sun_path));
                     98:        oldumask = umask(0111);
                     99:        if (bind(sock, (struct sockaddr *)&sockname,
                    100:                sizeof(struct sockaddr_un)) < 0) {
                    101:                perror("bind");
                    102:                goto bad_close;
                    103:        }
                    104:        if (listen(sock, 1) < 0) {
                    105:                perror("listen");
                    106:                goto bad_close;
                    107:        }
                    108:        umask(oldumask);
                    109:        f = xmalloc(sizeof(struct listen));
                    110:        f->file = file_new(&listen_fileops, f, path, 1);
                    111:        if (f->file == NULL)
                    112:                goto bad_close;
                    113:        f->path = xstrdup(path);
                    114:        if (f->path == NULL) {
                    115:                perror("strdup");
                    116:                exit(1);
                    117:        }
                    118:        f->fd = sock;
                    119:        f->next = listen_list;
                    120:        listen_list = f;
                    121:        return;
                    122:  bad_close:
                    123:        close(sock);
                    124:        exit(1);
                    125: }
                    126:
                    127: void
                    128: listen_new_tcp(char *addr, unsigned int port)
                    129: {
                    130:        char *host, serv[sizeof(unsigned int) * 3 + 1];
                    131:        struct addrinfo *ailist, *ai, aihints;
                    132:        struct listen *f;
                    133:        int s, error, opt = 1, n = 0;
                    134:
                    135:        /*
                    136:         * obtain a list of possible addresses for the host/port
                    137:         */
                    138:        memset(&aihints, 0, sizeof(struct addrinfo));
                    139:        snprintf(serv, sizeof(serv), "%u", port);
                    140:        host = strcmp(addr, "-") == 0 ? NULL : addr;
                    141:        aihints.ai_flags |= AI_PASSIVE;
                    142:        aihints.ai_socktype = SOCK_STREAM;
                    143:        aihints.ai_protocol = IPPROTO_TCP;
                    144:        error = getaddrinfo(host, serv, &aihints, &ailist);
                    145:        if (error) {
                    146:                fprintf(stderr, "%s: %s\n", addr, gai_strerror(error));
                    147:                exit(1);
                    148:        }
                    149:
                    150:        /*
                    151:         * for each address, try create a listening socket bound on
                    152:         * that address
                    153:         */
                    154:        for (ai = ailist; ai != NULL; ai = ai->ai_next) {
                    155:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
                    156:                if (s < 0) {
                    157:                        perror("socket");
                    158:                        continue;
                    159:                }
                    160:                opt = 1;
                    161:                if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
                    162:                        &opt, sizeof(int)) < 0) {
                    163:                        perror("setsockopt");
                    164:                        goto bad_close;
                    165:                }
                    166:                if (ai->ai_family == AF_INET6) {
                    167:                        /*
                    168:                         * make sure IPv6 sockets are restricted to IPv6
                    169:                         * addresses because we already use a IP socket
                    170:                         * for IP addresses
                    171:                         */
                    172:                        opt = 1;
                    173:                        if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
                    174:                                &opt, sizeof(int)) < 0) {
                    175:                                perror("setsockopt");
                    176:                                goto bad_close;
                    177:                        }
                    178:                }
                    179:
                    180:                if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
                    181:                        perror("bind");
                    182:                        goto bad_close;
                    183:                }
                    184:                if (listen(s, 1) < 0) {
                    185:                        perror("listen");
                    186:                        goto bad_close;
                    187:                }
                    188:                f = xmalloc(sizeof(struct listen));
                    189:                f->file = file_new(&listen_fileops, f, addr, 1);
                    190:                if (f == NULL) {
                    191:                bad_close:
                    192:                        close(s);
                    193:                        continue;
                    194:                }
                    195:                f->path = NULL;
                    196:                f->fd = s;
                    197:                f->next = listen_list;
                    198:                listen_list = f;
                    199:                n++;
                    200:        }
                    201:        freeaddrinfo(ailist);
                    202:        if (n == 0)
                    203:                exit(1);
                    204: }
                    205:
                    206: int
                    207: listen_init(struct listen *f)
                    208: {
                    209:        return 1;
                    210: }
                    211:
                    212: int
                    213: listen_pollfd(void *arg, struct pollfd *pfd)
                    214: {
                    215:        struct listen *f = arg;
                    216:
1.4       ratchov   217:        f->slowaccept = file_slowaccept;
                    218:        if (f->slowaccept)
1.1       ratchov   219:                return 0;
                    220:        pfd->fd = f->fd;
                    221:        pfd->events = POLLIN;
                    222:        return 1;
                    223: }
                    224:
                    225: int
                    226: listen_revents(void *arg, struct pollfd *pfd)
                    227: {
1.4       ratchov   228:        struct listen *f = arg;
                    229:
                    230:        if (f->slowaccept)
                    231:                return 0;
1.1       ratchov   232:        return pfd->revents;
                    233: }
                    234:
                    235: void
                    236: listen_in(void *arg)
                    237: {
                    238:        struct listen *f = arg;
                    239:        struct sockaddr caddr;
                    240:        socklen_t caddrlen;
                    241:        int sock, opt;
                    242:
                    243:        caddrlen = sizeof(caddrlen);
                    244:        while ((sock = accept(f->fd, &caddr, &caddrlen)) < 0) {
                    245:                if (errno == EINTR)
                    246:                        continue;
                    247:                if (errno == ENFILE || errno == EMFILE)
                    248:                        file_slowaccept = 1;
1.2       ratchov   249:                else if (errno != ECONNABORTED && errno != EWOULDBLOCK)
1.1       ratchov   250:                        perror("accept");
                    251:                return;
                    252:        }
                    253:        if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
                    254:                perror("fcntl(sock, O_NONBLOCK)");
                    255:                close(sock);
                    256:                return;
                    257:        }
                    258:        if (f->path == NULL) {
                    259:                opt = 1;
                    260:                if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
                    261:                        &opt, sizeof(int)) < 0) {
                    262:                        perror("setsockopt");
                    263:                        close(sock);
                    264:                        return;
                    265:                }
                    266:        }
                    267:        if (sock_new(sock) == NULL) {
                    268:                close(sock);
                    269:                return;
                    270:        }
                    271: }
                    272:
                    273: void
                    274: listen_out(void *arg)
                    275: {
                    276: }
                    277:
                    278: void
                    279: listen_hup(void *arg)
                    280: {
                    281:        struct listen *f = arg;
                    282:
                    283:        listen_close(f);
                    284: }