Annotation of src/usr.bin/sndiod/listen.c, Revision 1.7
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 (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
167: perror("bind");
168: goto bad_close;
169: }
170: if (listen(s, 1) < 0) {
171: perror("listen");
172: goto bad_close;
173: }
174: f = xmalloc(sizeof(struct listen));
175: f->file = file_new(&listen_fileops, f, addr, 1);
176: if (f == NULL) {
177: bad_close:
178: close(s);
179: continue;
180: }
181: f->path = NULL;
182: f->fd = s;
183: f->next = listen_list;
184: listen_list = f;
185: n++;
186: }
187: freeaddrinfo(ailist);
188: if (n == 0)
189: exit(1);
190: }
191:
192: int
193: listen_init(struct listen *f)
194: {
195: return 1;
196: }
197:
198: int
199: listen_pollfd(void *arg, struct pollfd *pfd)
200: {
201: struct listen *f = arg;
202:
1.4 ratchov 203: f->slowaccept = file_slowaccept;
204: if (f->slowaccept)
1.1 ratchov 205: return 0;
206: pfd->fd = f->fd;
207: pfd->events = POLLIN;
208: return 1;
209: }
210:
211: int
212: listen_revents(void *arg, struct pollfd *pfd)
213: {
1.4 ratchov 214: struct listen *f = arg;
215:
216: if (f->slowaccept)
217: return 0;
1.1 ratchov 218: return pfd->revents;
219: }
220:
221: void
222: listen_in(void *arg)
223: {
224: struct listen *f = arg;
225: struct sockaddr caddr;
226: socklen_t caddrlen;
227: int sock, opt;
228:
229: caddrlen = sizeof(caddrlen);
230: while ((sock = accept(f->fd, &caddr, &caddrlen)) < 0) {
231: if (errno == EINTR)
232: continue;
233: if (errno == ENFILE || errno == EMFILE)
234: file_slowaccept = 1;
1.2 ratchov 235: else if (errno != ECONNABORTED && errno != EWOULDBLOCK)
1.1 ratchov 236: perror("accept");
237: return;
238: }
239: if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
240: perror("fcntl(sock, O_NONBLOCK)");
241: close(sock);
242: return;
243: }
244: if (f->path == NULL) {
245: opt = 1;
246: if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
247: &opt, sizeof(int)) < 0) {
248: perror("setsockopt");
249: close(sock);
250: return;
251: }
252: }
253: if (sock_new(sock) == NULL) {
254: close(sock);
255: return;
256: }
257: }
258:
259: void
260: listen_out(void *arg)
261: {
262: }
263:
264: void
265: listen_hup(void *arg)
266: {
267: struct listen *f = arg;
268:
269: listen_close(f);
270: }