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>
40 struct sockaddr_storage name;
43 static int listensock4(int port)
45 struct sockaddr_in name;
49 memset(&name, 0, sizeof(name));
50 name.sin_family = AF_INET;
51 name.sin_port = htons(port);
52 if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
55 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
56 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
60 if(listen(fd, 16) < 0) {
67 static int listensock6(int port)
69 struct sockaddr_in6 name;
73 memset(&name, 0, sizeof(name));
74 name.sin6_family = AF_INET6;
75 name.sin6_port = htons(port);
76 if((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
79 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
80 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
84 if(listen(fd, 16) < 0) {
91 static int initreq(struct conn *conn, struct hthead *req)
93 struct tcpconn *tcp = conn->pdata;
96 if(tcp->name.ss_family == AF_INET) {
97 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&tcp->name)->sin_addr, nmbuf, sizeof(nmbuf)));
98 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&tcp->name)->sin_port)));
99 } else if(tcp->name.ss_family == AF_INET6) {
100 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&tcp->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
101 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&tcp->name)->sin6_port)));
106 void servetcp(struct muth *muth, va_list args)
109 vavar(struct sockaddr_storage, name);
114 in = mtstdopen(fd, 1, 60, "r+");
116 conn->pdata = omalloc(tcp);
117 conn->initreq = initreq;
126 static void listenloop(struct muth *muth, va_list args)
130 struct sockaddr_storage name;
134 namelen = sizeof(name);
135 block(ss, EV_READ, 0);
136 ns = accept(ss, (struct sockaddr *)&name, &namelen);
138 flog(LOG_ERR, "accept: %s", strerror(errno));
141 mustart(servetcp, ns, name);
148 void handleplain(int argc, char **argp, char **argv)
154 for(i = 0; i < argc; i++) {
155 if(!strcmp(argp[i], "help")) {
156 printf("plain handler parameters:\n");
157 printf("\tport=TCP-PORT (default is 80)\n");
159 } else if(!strcmp(argp[i], "port")) {
160 port = atoi(argv[i]);
162 flog(LOG_ERR, "unknown parameter `%s' to plain handler", argp[i]);
166 if((fd = listensock6(port)) < 0) {
167 flog(LOG_ERR, "could not listen on IPv6 (port %i): %s", port, strerror(errno));
170 mustart(listenloop, fd);
171 if((fd = listensock4(port)) < 0) {
172 if(errno != EADDRINUSE) {
173 flog(LOG_ERR, "could not listen on IPv4 (port %i): %s", port, strerror(errno));
177 mustart(listenloop, fd);