From 8774c31b4795b6cd61aeddefe8bbd1d2551d84ca Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sat, 21 Aug 2010 15:46:51 +0200 Subject: [PATCH] Made htparser listening much more flexible. --- lib/mtio.h | 2 + src/Makefile.am | 2 +- src/htparser.c | 182 +++++++++++++++++++++++++------------------------------- src/htparser.h | 13 ++++ src/plaintcp.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+), 102 deletions(-) create mode 100644 src/htparser.h create mode 100644 src/plaintcp.c diff --git a/lib/mtio.h b/lib/mtio.h index 0ae9780..874eec6 100644 --- a/lib/mtio.h +++ b/lib/mtio.h @@ -1,6 +1,8 @@ #ifndef _LIB_MTIO_H #define _LIB_MTIO_H +#include + #define EV_READ 1 #define EV_WRITE 2 diff --git a/src/Makefile.am b/src/Makefile.am index 3b67b3c..cc85e2e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,7 @@ bin_PROGRAMS = htparser dirplex sendfile callcgi patplex noinst_PROGRAMS = debugsink -htparser_SOURCES = htparser.c +htparser_SOURCES = htparser.c htparser.h plaintcp.c dirplex_SOURCES = dirplex.c debugsink_SOURCES = debugsink.c diff --git a/src/htparser.c b/src/htparser.c index 9f02ade..0c9d6ba 100644 --- a/src/htparser.c +++ b/src/htparser.c @@ -35,55 +35,9 @@ #include #include -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) { @@ -253,18 +207,14 @@ static int hasheader(struct hthead *head, char *name, char *val) 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) { @@ -276,14 +226,9 @@ static void serve(struct muth *muth, va_list args) 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)) @@ -365,28 +310,6 @@ static void serve(struct muth *muth, va_list args) 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); @@ -409,30 +332,87 @@ static void plexwatch(struct muth *muth, va_list args) } } +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(); diff --git a/src/htparser.h b/src/htparser.h new file mode 100644 index 0000000..a84086c --- /dev/null +++ b/src/htparser.h @@ -0,0 +1,13 @@ +#ifndef _ASH_HTPARSER_H +#define _ASH_HTPARSER_H + +struct conn { + int (*initreq)(struct conn *, struct hthead *); + void *pdata; +}; + +void serve(FILE *in, struct conn *conn); + +void handleplain(int argc, char **argp, char **argv); + +#endif diff --git a/src/plaintcp.c b/src/plaintcp.c new file mode 100644 index 0000000..72a3959 --- /dev/null +++ b/src/plaintcp.c @@ -0,0 +1,179 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + 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 . +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif +#include +#include +#include +#include +#include + +#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); + } +} -- 2.11.0