4 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static void server_connection(int fd);
40 static void service_packet_loop(int fd);
41 static void server_chld_exit(int signo);
50 * Listen on one or more UDP and TCP addresses, fork for each one.
52 signal(SIGCHLD, SIG_IGN);
53 for (tag = AddrBase; tag; tag = tag->next) {
54 struct sockaddr_in sain;
60 bzero(&sain, sizeof(sain));
61 if (tag->name == NULL) {
62 sain.sin_addr.s_addr = INADDR_ANY;
65 if (inet_aton(tag->name, &sain.sin_addr) == 0) {
67 if ((hp = gethostbyname2(tag->name, AF_INET)) == NULL) {
68 fprintf(stderr, "Unable to resolve %s\n", tag->name);
71 bcopy(hp->h_addr_list[0], &sain.sin_addr, hp->h_length);
72 host = strdup(hp->h_name);
75 host = strdup(tag->name);
78 sain.sin_port = htons(257);
79 sain.sin_len = sizeof(sain);
80 sain.sin_family = AF_INET;
83 if ((lfd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
84 fprintf(stderr, "%s: socket: %s\n", host, strerror(errno));
87 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
88 if (bind(lfd, (void *)&sain, sizeof(sain)) < 0) {
89 fprintf(stderr, "%s: bind: %s\n", host, strerror(errno));
92 if (listen(lfd, 20) < 0) {
93 fprintf(stderr, "%s: listen: %s\n", host, strerror(errno));
96 signal(SIGCHLD, server_chld_exit);
98 socklen_t slen = sizeof(sain);
99 fd = accept(lfd, (void *)&sain, &slen);
105 ++nconnects; /* XXX sigblock/sigsetmask */
108 server_connection(fd);
116 if ((lfd = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC)) < 0) {
117 fprintf(stderr, "%s: socket: %s\n", host, strerror(errno));
120 if (bind(lfd, (void *)&sain, sizeof(sain)) < 0) {
121 fprintf(stderr, "%s: bind: %s\n", host, strerror(errno));
124 service_packet_loop(lfd);
128 while (wait(NULL) > 0 || errno != EINTR)
134 server_chld_exit(int signo __unused)
136 while (wait3(NULL, WNOHANG, NULL) > 0)
142 server_connection(int fd)
151 fi = fdopen(fd, "r");
152 fo = fdopen(dup(fd), "w");
154 if (gethostname(buf, sizeof(buf)) == 0) {
155 fprintf(fo, "108 HELLO SERVER=%s\r\n", buf);
157 fprintf(fo, "108 HELLO\r\n");
161 while (fgets(buf, sizeof(buf), fi) != NULL) {
163 cmd = parse_str(&scan, PAS_ALPHA);
165 fprintf(fo, "502 Illegal Command String\r\n");
166 } else if (strcasecmp(cmd, "VAR") == 0) {
167 fprintf(fo, "100 OK\r\n");
168 } else if (strcasecmp(cmd, "TAG") == 0) {
169 if ((name = parse_str(&scan, PAS_ALPHA|PAS_NUMERIC)) == NULL) {
170 fprintf(fo, "401 Illegal Tag\r\n");
174 asprintf(&path, "%s/%s.sh", TagDir, name);
175 if ((fp = fopen(path, "r")) == NULL) {
176 fprintf(fo, "402 '%s' Not Found\r\n", name);
183 bytes = (size_t)ftell(fp);
185 fprintf(fo, "201 SIZE=%d\r\n", (int)bytes);
187 n = (bytes > sizeof(buf)) ? sizeof(buf) : bytes;
188 n = fread(buf, 1, n, fp);
193 if (fwrite(buf, 1, n, fo) != n) {
200 if (bytes > 0 && ferror(fo) == 0) {
201 bzero(buf, sizeof(buf));
203 n = (bytes > sizeof(buf)) ? sizeof(buf) : bytes;
204 if (fwrite(buf, 1, n, fo) != n)
209 fprintf(fo, "202 ERROR=%d\r\n", error);
213 } else if (strcasecmp(cmd, "IDLE") == 0) {
214 if ((name = parse_str(&scan, PAS_ANY)) == NULL) {
215 fprintf(fo, "401 Illegal String\r\n");
217 fprintf(fo, "109 %s\r\n", name);
219 } else if (strcasecmp(cmd, "QUIT") == 0) {
220 fprintf(fo, "409 Bye!\r\n");
223 fprintf(fo, "501 Unknown Command\r\n");
232 * UDP packet loop. For now just handle one request per packet. Note that
233 * since the protocol is designed to be used in a broadcast environment,
234 * we only respond when we have something to contribute.
238 service_packet_loop(int fd)
240 struct sockaddr_in sain;
250 sain_len = sizeof(sain);
251 n = recvfrom(fd, ibuf, sizeof(ibuf) - 1, 0, (void *)&sain, &sain_len);
260 cmd = parse_str(&scan, PAS_ALPHA);
263 } else if (strcasecmp(cmd, "TAG") == 0) {
264 if ((name = parse_str(&scan, PAS_ALPHA|PAS_NUMERIC)) != NULL) {
267 asprintf(&path, "%s/%s.sh", TagDir, name);
268 if (stat(path, &st) == 0) {
269 snprintf(obuf, sizeof(obuf), "101 TAG=%s\r\n", name);
276 sendto(fd, obuf, n, 0, (void *)&sain, sain_len);