Annotation of src/usr.bin/sndiod/listen.c, Revision 1.9
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 <errno.h>
28: #include <fcntl.h>
29: #include <poll.h>
30: #include <stdio.h>
31: #include <string.h>
32: #include <unistd.h>
33:
34: #include "listen.h"
35: #include "file.h"
36: #include "sock.h"
37: #include "utils.h"
38:
39: int listen_pollfd(void *, struct pollfd *);
40: int listen_revents(void *, struct pollfd *);
41: void listen_in(void *);
42: void listen_out(void *);
43: void listen_hup(void *);
44:
45: struct fileops listen_fileops = {
46: "listen",
47: listen_pollfd,
48: listen_revents,
49: listen_in,
50: listen_out,
51: listen_hup
52: };
53:
54: struct listen *listen_list = NULL;
55:
56: void
57: listen_close(struct listen *f)
58: {
59: struct listen **pf;
60:
61: for (pf = &listen_list; *pf != f; pf = &(*pf)->next) {
62: #ifdef DEBUG
63: if (*pf == NULL) {
64: log_puts("listen_close: not on list\n");
65: panic();
66: }
67: #endif
68: }
69: *pf = f->next;
70:
1.8 ratchov 71: if (f->path != NULL) {
1.1 ratchov 72: xfree(f->path);
1.8 ratchov 73: }
1.1 ratchov 74: file_del(f->file);
75: close(f->fd);
76: xfree(f);
77: }
78:
1.8 ratchov 79: int
1.1 ratchov 80: listen_new_un(char *path)
81: {
82: int sock, oldumask;
83: struct sockaddr_un sockname;
84: struct listen *f;
85:
86: sock = socket(AF_UNIX, SOCK_STREAM, 0);
87: if (sock < 0) {
1.8 ratchov 88: log_puts(path);
89: log_puts(": failed to create socket\n");
90: return 0;
1.1 ratchov 91: }
92: if (unlink(path) < 0 && errno != ENOENT) {
1.8 ratchov 93: log_puts(path);
94: log_puts(": failed to unlink socket\n");
1.1 ratchov 95: goto bad_close;
96: }
97: sockname.sun_family = AF_UNIX;
98: strlcpy(sockname.sun_path, path, sizeof(sockname.sun_path));
99: oldumask = umask(0111);
100: if (bind(sock, (struct sockaddr *)&sockname,
101: sizeof(struct sockaddr_un)) < 0) {
1.8 ratchov 102: log_puts(path);
103: log_puts(": failed to bind socket\n");
1.1 ratchov 104: goto bad_close;
105: }
106: if (listen(sock, 1) < 0) {
1.8 ratchov 107: log_puts(path);
108: log_puts(": failed to listen\n");
1.1 ratchov 109: goto bad_close;
110: }
111: umask(oldumask);
112: f = xmalloc(sizeof(struct listen));
113: f->file = file_new(&listen_fileops, f, path, 1);
114: if (f->file == NULL)
115: goto bad_close;
116: f->path = xstrdup(path);
117: f->fd = sock;
118: f->next = listen_list;
119: listen_list = f;
1.8 ratchov 120: return 1;
1.1 ratchov 121: bad_close:
122: close(sock);
1.8 ratchov 123: return 0;
1.1 ratchov 124: }
125:
1.8 ratchov 126: int
1.1 ratchov 127: listen_new_tcp(char *addr, unsigned int port)
128: {
129: char *host, serv[sizeof(unsigned int) * 3 + 1];
130: struct addrinfo *ailist, *ai, aihints;
131: struct listen *f;
132: int s, error, opt = 1, n = 0;
133:
134: /*
135: * obtain a list of possible addresses for the host/port
136: */
137: memset(&aihints, 0, sizeof(struct addrinfo));
138: snprintf(serv, sizeof(serv), "%u", port);
139: host = strcmp(addr, "-") == 0 ? NULL : addr;
140: aihints.ai_flags |= AI_PASSIVE;
141: aihints.ai_socktype = SOCK_STREAM;
142: aihints.ai_protocol = IPPROTO_TCP;
143: error = getaddrinfo(host, serv, &aihints, &ailist);
144: if (error) {
1.8 ratchov 145: log_puts(addr);
146: log_puts(": failed to resolve address\n");
147: return 0;
1.1 ratchov 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) {
1.8 ratchov 157: log_puts(addr);
158: log_puts(": failed to create socket\n");
1.1 ratchov 159: continue;
160: }
161: opt = 1;
162: if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
163: &opt, sizeof(int)) < 0) {
1.8 ratchov 164: log_puts(addr);
165: log_puts(": failed to set SO_REUSEADDR\n");
1.1 ratchov 166: goto bad_close;
167: }
1.8 ratchov 168: if (ai->ai_family == AF_INET6) {
169: /*
170: * make sure IPv6 sockets are restricted to IPv6
171: * addresses because we already use a IP socket
172: * for IP addresses
173: */
174: opt = 1;
175: if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
176: &opt, sizeof(int)) < 0) {
177: log_puts(addr);
178: log_puts(": failed to set IPV6_V6ONLY\n");
179: goto bad_close;
180: }
181: }
182:
1.1 ratchov 183: if (bind(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1.8 ratchov 184: log_puts(addr);
185: log_puts(": failed to bind socket\n");
1.1 ratchov 186: goto bad_close;
187: }
188: if (listen(s, 1) < 0) {
1.8 ratchov 189: log_puts(addr);
190: log_puts(": failed to listen\n");
1.1 ratchov 191: goto bad_close;
192: }
193: f = xmalloc(sizeof(struct listen));
194: f->file = file_new(&listen_fileops, f, addr, 1);
195: if (f == NULL) {
196: bad_close:
197: close(s);
198: continue;
199: }
200: f->path = NULL;
201: f->fd = s;
202: f->next = listen_list;
203: listen_list = f;
204: n++;
205: }
206: freeaddrinfo(ailist);
1.8 ratchov 207: return n;
1.1 ratchov 208: }
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;
253: return;
254: }
255: if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
1.8 ratchov 256: file_log(f->file);
257: log_puts(": failed to set non-blocking mode\n");
1.1 ratchov 258: close(sock);
259: return;
260: }
261: if (f->path == NULL) {
262: opt = 1;
263: if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
264: &opt, sizeof(int)) < 0) {
1.8 ratchov 265: file_log(f->file);
266: log_puts(": failed to set TCP_NODELAY flag\n");
1.1 ratchov 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: }