2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
45 struct sockaddr_storage name;
50 int listensock4(int port)
52 struct sockaddr_in name;
56 memset(&name, 0, sizeof(name));
57 name.sin_family = AF_INET;
58 name.sin_port = htons(port);
59 if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
62 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
63 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
67 if(listen(fd, 16) < 0) {
74 int listensock6(int port)
76 struct sockaddr_in6 name;
80 memset(&name, 0, sizeof(name));
81 name.sin6_family = AF_INET6;
82 name.sin6_port = htons(port);
83 if((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
86 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
87 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
91 if(listen(fd, 16) < 0) {
98 char *formathaddress(struct sockaddr *name, socklen_t namelen)
100 static char buf[128];
101 struct sockaddr_in *v4;
102 struct sockaddr_in6 *v6;
104 switch(name->sa_family) {
106 v4 = (struct sockaddr_in *)name;
107 if(!inet_ntop(AF_INET, &v4->sin_addr, buf, sizeof(buf)))
111 v6 = (struct sockaddr_in6 *)name;
112 if(IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) {
113 if(!inet_ntop(AF_INET, ((char *)&v6->sin6_addr) + 12, buf, sizeof(buf)))
116 if(!inet_ntop(AF_INET6, &v6->sin6_addr, buf, sizeof(buf)))
121 errno = EPFNOSUPPORT;
126 static int initreq(struct conn *conn, struct hthead *req)
128 struct tcpconn *tcp = conn->pdata;
129 struct sockaddr_storage sa;
132 headappheader(req, "X-Ash-Address", formathaddress((struct sockaddr *)&tcp->name, sizeof(sa)));
133 if(tcp->name.ss_family == AF_INET)
134 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&tcp->name)->sin_port)));
135 else if(tcp->name.ss_family == AF_INET6)
136 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&tcp->name)->sin6_port)));
138 if(!getsockname(tcp->fd, (struct sockaddr *)&sa, &salen))
139 headappheader(req, "X-Ash-Server-Address", formathaddress((struct sockaddr *)&sa, sizeof(sa)));
140 headappheader(req, "X-Ash-Server-Port", sprintf3("%i", tcp->port->sport));
141 headappheader(req, "X-Ash-Protocol", "http");
145 void servetcp(struct muth *muth, va_list args)
148 vavar(struct sockaddr_storage, name);
149 vavar(struct tcpport *, stcp);
154 memset(&conn, 0, sizeof(conn));
155 memset(&tcp, 0, sizeof(tcp));
156 in = mtstdopen(fd, 1, 60, "r+");
158 conn.initreq = initreq;
165 static void listenloop(struct muth *muth, va_list args)
167 vavar(struct tcpport *, tcp);
169 struct sockaddr_storage name;
173 namelen = sizeof(name);
174 block(tcp->fd, EV_READ, 0);
175 ns = accept(tcp->fd, (struct sockaddr *)&name, &namelen);
177 flog(LOG_ERR, "accept: %s", strerror(errno));
180 mustart(servetcp, ns, name, tcp);
188 void handleplain(int argc, char **argp, char **argv)
195 for(i = 0; i < argc; i++) {
196 if(!strcmp(argp[i], "help")) {
197 printf("plain handler parameters:\n");
198 printf("\tport=TCP-PORT [80]\n");
199 printf("\t\tThe TCP port to listen on.\n");
201 } else if(!strcmp(argp[i], "port")) {
202 port = atoi(argv[i]);
204 flog(LOG_ERR, "unknown parameter `%s' to plain handler", argp[i]);
208 if((fd = listensock6(port)) < 0) {
209 flog(LOG_ERR, "could not listen on IPv6 (port %i): %s", port, strerror(errno));
215 mustart(listenloop, tcp);
216 if((fd = listensock4(port)) < 0) {
217 if(errno != EADDRINUSE) {
218 flog(LOG_ERR, "could not listen on IPv4 (port %i): %s", port, strerror(errno));
225 mustart(listenloop, tcp);