[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.3

1.3     ! ratchov     1: /*     $OpenBSD: listen.c,v 1.2 2013/03/13 08:28:33 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:
                     73:        if (f->path != NULL) {
                     74:                unlink(f->path);
                     75:                xfree(f->path);
                     76:        }
                     77:        file_del(f->file);
                     78:        close(f->fd);
                     79:        xfree(f);
                     80: }
                     81:
                     82: void
                     83: listen_new_un(char *path)
                     84: {
                     85:        int sock, oldumask;
                     86:        struct sockaddr_un sockname;
                     87:        struct listen *f;
                     88:
                     89:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                     90:        if (sock < 0) {
                     91:                perror("socket");
                     92:                exit(1);
                     93:        }
                     94:        if (unlink(path) < 0 && errno != ENOENT) {
                     95:                perror("unlink");
                     96:                goto bad_close;
                     97:        }
                     98:        sockname.sun_family = AF_UNIX;
                     99:        strlcpy(sockname.sun_path, path, sizeof(sockname.sun_path));
                    100:        oldumask = umask(0111);
                    101:        if (bind(sock, (struct sockaddr *)&sockname,
                    102:                sizeof(struct sockaddr_un)) < 0) {
                    103:                perror("bind");
                    104:                goto bad_close;
                    105:        }
                    106:        if (listen(sock, 1) < 0) {
                    107:                perror("listen");
                    108:                goto bad_close;
                    109:        }
                    110:        umask(oldumask);
                    111:        f = xmalloc(sizeof(struct listen));
                    112:        f->file = file_new(&listen_fileops, f, path, 1);
                    113:        if (f->file == NULL)
                    114:                goto bad_close;
                    115:        f->path = xstrdup(path);
                    116:        if (f->path == NULL) {
                    117:                perror("strdup");
                    118:                exit(1);
                    119:        }
                    120:        f->fd = sock;
                    121:        f->next = listen_list;
                    122:        listen_list = f;
                    123:        return;
                    124:  bad_close:
                    125:        close(sock);
                    126:        exit(1);
                    127: }
                    128:
1.3     ! ratchov   129: #ifdef USE_TCP
1.1       ratchov   130: void
                    131: listen_new_tcp(char *addr, unsigned int port)
                    132: {
                    133:        char *host, serv[sizeof(unsigned int) * 3 + 1];
                    134:        struct addrinfo *ailist, *ai, aihints;
                    135:        struct listen *f;
                    136:        int s, error, opt = 1, n = 0;
                    137:
                    138:        /*
                    139:         * obtain a list of possible addresses for the host/port
                    140:         */
                    141:        memset(&aihints, 0, sizeof(struct addrinfo));
                    142:        snprintf(serv, sizeof(serv), "%u", port);
                    143:        host = strcmp(addr, "-") == 0 ? NULL : addr;
                    144:        aihints.ai_flags |= AI_PASSIVE;
                    145:        aihints.ai_socktype = SOCK_STREAM;
                    146:        aihints.ai_protocol = IPPROTO_TCP;
                    147:        error = getaddrinfo(host, serv, &aihints, &ailist);
                    148:        if (error) {
                    149:                fprintf(stderr, "%s: %s\n", addr, gai_strerror(error));
                    150:                exit(1);
                    151:        }
                    152:
                    153:        /*
                    154:         * for each address, try create a listening socket bound on
                    155:         * that address
                    156:         */
                    157:        for (ai = ailist; ai != NULL; ai = ai->ai_next) {
                    158:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
                    159:                if (s < 0) {
                    160:                        perror("socket");
                    161:                        continue;
                    162:                }
                    163:                opt = 1;
                    164:                if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
                    165:                        &opt, sizeof(int)) < 0) {
                    166:                        perror("setsockopt");
                    167:                        goto bad_close;
                    168:                }
                    169:                if (ai->ai_family == AF_INET6) {
                    170:                        /*
                    171:                         * make sure IPv6 sockets are restricted to IPv6
                    172:                         * addresses because we already use a IP socket
                    173:                         * for IP addresses
                    174:                         */
                    175:                        opt = 1;
                    176:                        if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
                    177:                                &opt, sizeof(int)) < 0) {
                    178:                                perror("setsockopt");
                    179:                                goto bad_close;
                    180:                        }
                    181:                }
                    182:
                    183:                if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
                    184:                        perror("bind");
                    185:                        goto bad_close;
                    186:                }
                    187:                if (listen(s, 1) < 0) {
                    188:                        perror("listen");
                    189:                        goto bad_close;
                    190:                }
                    191:                f = xmalloc(sizeof(struct listen));
                    192:                f->file = file_new(&listen_fileops, f, addr, 1);
                    193:                if (f == NULL) {
                    194:                bad_close:
                    195:                        close(s);
                    196:                        continue;
                    197:                }
                    198:                f->path = NULL;
                    199:                f->fd = s;
                    200:                f->next = listen_list;
                    201:                listen_list = f;
                    202:                n++;
                    203:        }
                    204:        freeaddrinfo(ailist);
                    205:        if (n == 0)
                    206:                exit(1);
                    207: }
1.3     ! ratchov   208: #endif
1.1       ratchov   209:
                    210: int
                    211: listen_init(struct listen *f)
                    212: {
                    213:        return 1;
                    214: }
                    215:
                    216: int
                    217: listen_pollfd(void *arg, struct pollfd *pfd)
                    218: {
                    219:        struct listen *f = arg;
                    220:
                    221:        if (file_slowaccept)
                    222:                return 0;
                    223:        pfd->fd = f->fd;
                    224:        pfd->events = POLLIN;
                    225:        return 1;
                    226: }
                    227:
                    228: int
                    229: listen_revents(void *arg, struct pollfd *pfd)
                    230: {
                    231:        return pfd->revents;
                    232: }
                    233:
                    234: void
                    235: listen_in(void *arg)
                    236: {
                    237:        struct listen *f = arg;
                    238:        struct sockaddr caddr;
                    239:        socklen_t caddrlen;
                    240:        int sock, opt;
                    241:
                    242:        caddrlen = sizeof(caddrlen);
                    243:        while ((sock = accept(f->fd, &caddr, &caddrlen)) < 0) {
                    244:                if (errno == EINTR)
                    245:                        continue;
                    246:                if (errno == ENFILE || errno == EMFILE)
                    247:                        file_slowaccept = 1;
1.2       ratchov   248:                else if (errno != ECONNABORTED && errno != EWOULDBLOCK)
1.1       ratchov   249:                        perror("accept");
                    250:                return;
                    251:        }
                    252:        if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
                    253:                perror("fcntl(sock, O_NONBLOCK)");
                    254:                close(sock);
                    255:                return;
                    256:        }
                    257:        if (f->path == NULL) {
                    258:                opt = 1;
                    259:                if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
                    260:                        &opt, sizeof(int)) < 0) {
                    261:                        perror("setsockopt");
                    262:                        close(sock);
                    263:                        return;
                    264:                }
                    265:        }
                    266:        if (sock_new(sock) == NULL) {
                    267:                close(sock);
                    268:                return;
                    269:        }
                    270: }
                    271:
                    272: void
                    273: listen_out(void *arg)
                    274: {
                    275: }
                    276:
                    277: void
                    278: listen_hup(void *arg)
                    279: {
                    280:        struct listen *f = arg;
                    281:
                    282:        listen_close(f);
                    283: }