#include <req.h>
#include <proc.h>
-static int plex;
-
-static int listensock4(int port)
-{
- struct sockaddr_in name;
- int fd;
- int valbuf;
-
- memset(&name, 0, sizeof(name));
- name.sin_family = AF_INET;
- name.sin_port = htons(port);
- if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
- return(-1);
- valbuf = 1;
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
- if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
- close(fd);
- return(-1);
- }
- if(listen(fd, 16) < 0) {
- close(fd);
- return(-1);
- }
- return(fd);
-}
+#include "htparser.h"
-static int listensock6(int port)
-{
- struct sockaddr_in6 name;
- int fd;
- int valbuf;
-
- memset(&name, 0, sizeof(name));
- name.sin6_family = AF_INET6;
- name.sin6_port = htons(port);
- if((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
- return(-1);
- valbuf = 1;
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
- if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
- close(fd);
- return(-1);
- }
- if(listen(fd, 16) < 0) {
- close(fd);
- return(-1);
- }
- return(fd);
-}
+static int plex;
static struct hthead *parsereq(FILE *in)
{
return(!strcasecmp(hd, val));
}
-static void serve(struct muth *muth, va_list args)
+void serve(FILE *in, struct conn *conn)
{
- vavar(int, fd);
- vavar(struct sockaddr_storage, name);
int pfds[2];
- FILE *in, *out;
+ FILE *out;
struct hthead *req, *resp;
- char nmbuf[256];
char *hd, *p;
off_t dlen;
- in = mtstdopen(fd, 1, 60, "r+");
out = NULL;
req = resp = NULL;
while(1) {
if((p = strchr(req->rest, '?')) != NULL)
*p = 0;
- if(name.ss_family == AF_INET) {
- headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&name)->sin_addr, nmbuf, sizeof(nmbuf)));
- headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&name)->sin_port)));
- } else if(name.ss_family == AF_INET6) {
- headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&name)->sin6_addr, nmbuf, sizeof(nmbuf)));
- headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&name)->sin6_port)));
- }
-
+ if((conn->initreq != NULL) && conn->initreq(conn, req))
+ break;
+
if(block(plex, EV_WRITE, 60) <= 0)
break;
if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
fclose(in);
}
-static void listenloop(struct muth *muth, va_list args)
-{
- vavar(int, ss);
- int ns;
- struct sockaddr_storage name;
- socklen_t namelen;
-
- while(1) {
- namelen = sizeof(name);
- block(ss, EV_READ, 0);
- ns = accept(ss, (struct sockaddr *)&name, &namelen);
- if(ns < 0) {
- flog(LOG_ERR, "accept: %s", strerror(errno));
- goto out;
- }
- mustart(serve, ns, name);
- }
-
-out:
- close(ss);
-}
-
static void plexwatch(struct muth *muth, va_list args)
{
vavar(int, fd);
}
}
+static void usage(FILE *out)
+{
+ fprintf(out, "usage: htparser [-h] PORTSPEC... -- ROOT [ARGS...]\n");
+ fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
+ fprintf(out, "\tavailable handlers are `plain'.\n");
+}
+
+static void addport(char *spec)
+{
+ char *nm, *p, *p2, *n;
+ struct charvbuf pars, vals;
+
+ bufinit(pars);
+ bufinit(vals);
+ if((p = strchr(spec, ':')) == NULL) {
+ nm = spec;
+ } else {
+ nm = spec;
+ *(p++) = 0;
+ do {
+ if((n = strchr(p, ',')) != NULL)
+ *(n++) = 0;
+ if((p2 = strchr(p, '=')) != NULL)
+ *(p2++) = 0;
+ if(!*p) {
+ usage(stderr);
+ exit(1);
+ }
+ bufadd(pars, p);
+ if(p2)
+ bufadd(vals, p2);
+ else
+ bufadd(vals, "");
+ } while((p = n) != NULL);
+ }
+
+ /* XXX: It would be nice to decentralize this, but, meh... */
+ if(!strcmp(nm, "plain")) {
+ handleplain(pars.d, pars.b, vals.b);
+ } else {
+ flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
+ exit(1);
+ }
+
+ buffree(pars);
+ buffree(vals);
+}
+
int main(int argc, char **argv)
{
- int fd;
+ int c;
+ int i, s1;
- if(argc < 2) {
- fprintf(stderr, "usage: htparser ROOT [ARGS...]\n");
+ while((c = getopt(argc, argv, "+h")) >= 0) {
+ switch(c) {
+ case 'h':
+ usage(stdout);
+ exit(0);
+ default:
+ usage(stderr);
+ exit(1);
+ }
+ }
+ if((argc - optind) < 3) {
+ usage(stderr);
exit(1);
}
- if((plex = stdmkchild(argv + 1)) < 0) {
- flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
- return(1);
+ s1 = 0;
+ for(i = optind; i < argc; i++) {
+ if(!strcmp(argv[i], "--"))
+ break;
+ s1 = 1;
+ addport(argv[i]);
}
- if((fd = listensock6(8080)) < 0) {
- flog(LOG_ERR, "could not listen on IPv6: %s", strerror(errno));
- return(1);
+ if(!s1 || (i == argc)) {
+ usage(stderr);
+ exit(1);
}
- mustart(listenloop, fd);
- if((fd = listensock4(8080)) < 0) {
- if(errno != EADDRINUSE) {
- flog(LOG_ERR, "could not listen on IPv4: %s", strerror(errno));
- return(1);
- }
- } else {
- mustart(listenloop, fd);
+ if((plex = stdmkchild(argv + ++i)) < 0) {
+ flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
+ return(1);
}
mustart(plexwatch, plex);
ioloop();
--- /dev/null
+/*
+ ashd - A Sane HTTP Daemon
+ Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <string.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <utils.h>
+#include <req.h>
+#include <mt.h>
+#include <mtio.h>
+#include <log.h>
+
+#include "htparser.h"
+
+struct tcpconn {
+ struct sockaddr_storage name;
+};
+
+static int listensock4(int port)
+{
+ struct sockaddr_in name;
+ int fd;
+ int valbuf;
+
+ memset(&name, 0, sizeof(name));
+ name.sin_family = AF_INET;
+ name.sin_port = htons(port);
+ if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
+ return(-1);
+ valbuf = 1;
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
+ if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
+ close(fd);
+ return(-1);
+ }
+ if(listen(fd, 16) < 0) {
+ close(fd);
+ return(-1);
+ }
+ return(fd);
+}
+
+static int listensock6(int port)
+{
+ struct sockaddr_in6 name;
+ int fd;
+ int valbuf;
+
+ memset(&name, 0, sizeof(name));
+ name.sin6_family = AF_INET6;
+ name.sin6_port = htons(port);
+ if((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
+ return(-1);
+ valbuf = 1;
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
+ if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
+ close(fd);
+ return(-1);
+ }
+ if(listen(fd, 16) < 0) {
+ close(fd);
+ return(-1);
+ }
+ return(fd);
+}
+
+static int initreq(struct conn *conn, struct hthead *req)
+{
+ struct tcpconn *tcp = conn->pdata;
+ char nmbuf[256];
+
+ if(tcp->name.ss_family == AF_INET) {
+ headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&tcp->name)->sin_addr, nmbuf, sizeof(nmbuf)));
+ headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&tcp->name)->sin_port)));
+ } else if(tcp->name.ss_family == AF_INET6) {
+ headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&tcp->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
+ headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&tcp->name)->sin6_port)));
+ }
+ return(0);
+}
+
+void servetcp(struct muth *muth, va_list args)
+{
+ vavar(int, fd);
+ vavar(struct sockaddr_storage, name);
+ FILE *in;
+ struct conn *conn;
+ struct tcpconn *tcp;
+
+ in = mtstdopen(fd, 1, 60, "r+");
+ omalloc(conn);
+ conn->pdata = omalloc(tcp);
+ conn->initreq = initreq;
+ tcp->name = name;
+
+ serve(in, conn);
+
+ free(tcp);
+ free(conn);
+}
+
+static void listenloop(struct muth *muth, va_list args)
+{
+ vavar(int, ss);
+ int ns;
+ struct sockaddr_storage name;
+ socklen_t namelen;
+
+ while(1) {
+ namelen = sizeof(name);
+ block(ss, EV_READ, 0);
+ ns = accept(ss, (struct sockaddr *)&name, &namelen);
+ if(ns < 0) {
+ flog(LOG_ERR, "accept: %s", strerror(errno));
+ goto out;
+ }
+ mustart(servetcp, ns, name);
+ }
+
+out:
+ close(ss);
+}
+
+void handleplain(int argc, char **argp, char **argv)
+{
+ int port, fd;
+ int i;
+
+ port = 80;
+ for(i = 0; i < argc; i++) {
+ if(!strcmp(argp[i], "help")) {
+ printf("plain handler parameters:\n");
+ printf("\tport=TCP-PORT (default is 80)\n");
+ exit(0);
+ } else if(!strcmp(argp[i], "port")) {
+ port = atoi(argv[i]);
+ } else {
+ flog(LOG_ERR, "unknown parameter `%s' to plain handler", argp[i]);
+ exit(1);
+ }
+ }
+ if((fd = listensock6(port)) < 0) {
+ flog(LOG_ERR, "could not listen on IPv6 (port %i): %s", port, strerror(errno));
+ exit(1);
+ }
+ mustart(listenloop, fd);
+ if((fd = listensock4(port)) < 0) {
+ if(errno != EADDRINUSE) {
+ flog(LOG_ERR, "could not listen on IPv4 (port %i): %s", port, strerror(errno));
+ exit(1);
+ }
+ } else {
+ mustart(listenloop, fd);
+ }
+}