Annotation of src/usr.bin/sndiod/listen.c, Revision 1.4
1.4 ! ratchov 1: /* $OpenBSD: listen.c,v 1.3 2015/11/18 08:36:20 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:
1.4 ! ratchov 221: f->slowaccept = file_slowaccept;
! 222: if (f->slowaccept)
1.1 ratchov 223: return 0;
224: pfd->fd = f->fd;
225: pfd->events = POLLIN;
226: return 1;
227: }
228:
229: int
230: listen_revents(void *arg, struct pollfd *pfd)
231: {
1.4 ! ratchov 232: struct listen *f = arg;
! 233:
! 234: if (f->slowaccept)
! 235: return 0;
1.1 ratchov 236: return pfd->revents;
237: }
238:
239: void
240: listen_in(void *arg)
241: {
242: struct listen *f = arg;
243: struct sockaddr caddr;
244: socklen_t caddrlen;
245: int sock, opt;
246:
247: caddrlen = sizeof(caddrlen);
248: while ((sock = accept(f->fd, &caddr, &caddrlen)) < 0) {
249: if (errno == EINTR)
250: continue;
251: if (errno == ENFILE || errno == EMFILE)
252: file_slowaccept = 1;
1.2 ratchov 253: else if (errno != ECONNABORTED && errno != EWOULDBLOCK)
1.1 ratchov 254: perror("accept");
255: return;
256: }
257: if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
258: perror("fcntl(sock, O_NONBLOCK)");
259: close(sock);
260: return;
261: }
262: if (f->path == NULL) {
263: opt = 1;
264: if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
265: &opt, sizeof(int)) < 0) {
266: perror("setsockopt");
267: close(sock);
268: return;
269: }
270: }
271: if (sock_new(sock) == NULL) {
272: close(sock);
273: return;
274: }
275: }
276:
277: void
278: listen_out(void *arg)
279: {
280: }
281:
282: void
283: listen_hup(void *arg)
284: {
285: struct listen *f = arg;
286:
287: listen_close(f);
288: }