doc: Fixed typo in dirplex manpage.
[ashd.git] / src / plaintcp.c
... / ...
CommitLineData
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
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.
9
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.
14
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/>.
17*/
18
19#include <stdlib.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25#include <errno.h>
26#include <string.h>
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <utils.h>
32#include <req.h>
33#include <mt.h>
34#include <mtio.h>
35#include <log.h>
36
37#include "htparser.h"
38
39struct tcpport {
40 int fd;
41 int sport;
42};
43
44struct tcpconn {
45 struct sockaddr_storage name;
46 struct tcpport *port;
47 int fd;
48};
49
50int listensock4(int port)
51{
52 struct sockaddr_in name;
53 int fd;
54 int valbuf;
55
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)
60 return(-1);
61 valbuf = 1;
62 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
63 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
64 close(fd);
65 return(-1);
66 }
67 if(listen(fd, 16) < 0) {
68 close(fd);
69 return(-1);
70 }
71 return(fd);
72}
73
74int listensock6(int port)
75{
76 struct sockaddr_in6 name;
77 int fd;
78 int valbuf;
79
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)
84 return(-1);
85 valbuf = 1;
86 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
87 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
88 close(fd);
89 return(-1);
90 }
91 if(listen(fd, 16) < 0) {
92 close(fd);
93 return(-1);
94 }
95 return(fd);
96}
97
98static int initreq(struct conn *conn, struct hthead *req)
99{
100 struct tcpconn *tcp = conn->pdata;
101 struct sockaddr_storage sa;
102 socklen_t salen;
103 char nmbuf[256];
104
105 if(tcp->name.ss_family == AF_INET) {
106 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&tcp->name)->sin_addr, nmbuf, sizeof(nmbuf)));
107 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&tcp->name)->sin_port)));
108 } else if(tcp->name.ss_family == AF_INET6) {
109 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&tcp->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
110 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&tcp->name)->sin6_port)));
111 }
112 salen = sizeof(sa);
113 if(!getsockname(tcp->fd, (struct sockaddr *)&sa, &salen)) {
114 if(sa.ss_family == AF_INET)
115 headappheader(req, "X-Ash-Server-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr, nmbuf, sizeof(nmbuf)));
116 else if(sa.ss_family == AF_INET6)
117 headappheader(req, "X-Ash-Server-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr, nmbuf, sizeof(nmbuf)));
118 }
119 headappheader(req, "X-Ash-Server-Port", sprintf3("%i", tcp->port->sport));
120 headappheader(req, "X-Ash-Protocol", "http");
121 return(0);
122}
123
124void servetcp(struct muth *muth, va_list args)
125{
126 vavar(int, fd);
127 vavar(struct sockaddr_storage, name);
128 vavar(struct tcpport *, stcp);
129 FILE *in;
130 struct conn conn;
131 struct tcpconn tcp;
132
133 memset(&conn, 0, sizeof(conn));
134 memset(&tcp, 0, sizeof(tcp));
135 in = mtstdopen(fd, 1, 60, "r+");
136 conn.pdata = &tcp;
137 conn.initreq = initreq;
138 tcp.fd = fd;
139 tcp.name = name;
140 tcp.port = stcp;
141 serve(in, &conn);
142}
143
144static void listenloop(struct muth *muth, va_list args)
145{
146 vavar(struct tcpport *, tcp);
147 int ns;
148 struct sockaddr_storage name;
149 socklen_t namelen;
150
151 while(1) {
152 namelen = sizeof(name);
153 block(tcp->fd, EV_READ, 0);
154 ns = accept(tcp->fd, (struct sockaddr *)&name, &namelen);
155 if(ns < 0) {
156 flog(LOG_ERR, "accept: %s", strerror(errno));
157 goto out;
158 }
159 mustart(servetcp, ns, name, tcp);
160 }
161
162out:
163 close(tcp->fd);
164 free(tcp);
165}
166
167void handleplain(int argc, char **argp, char **argv)
168{
169 int port, fd;
170 int i;
171 struct tcpport *tcp;
172
173 port = 80;
174 for(i = 0; i < argc; i++) {
175 if(!strcmp(argp[i], "help")) {
176 printf("plain handler parameters:\n");
177 printf("\tport=TCP-PORT [80]\n");
178 printf("\t\tThe TCP port to listen on.\n");
179 exit(0);
180 } else if(!strcmp(argp[i], "port")) {
181 port = atoi(argv[i]);
182 } else {
183 flog(LOG_ERR, "unknown parameter `%s' to plain handler", argp[i]);
184 exit(1);
185 }
186 }
187 if((fd = listensock6(port)) < 0) {
188 flog(LOG_ERR, "could not listen on IPv6 (port %i): %s", port, strerror(errno));
189 exit(1);
190 }
191 omalloc(tcp);
192 tcp->fd = fd;
193 tcp->sport = port;
194 mustart(listenloop, tcp);
195 if((fd = listensock4(port)) < 0) {
196 if(errno != EADDRINUSE) {
197 flog(LOG_ERR, "could not listen on IPv4 (port %i): %s", port, strerror(errno));
198 exit(1);
199 }
200 } else {
201 omalloc(tcp);
202 tcp->fd = fd;
203 tcp->sport = port;
204 mustart(listenloop, tcp);
205 }
206}