Added an initial implementation of HTTPS.
[ashd.git] / src / plaintcp.c
CommitLineData
8774c31b
FT
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
c063c3b8
FT
39struct tcpport {
40 int fd;
41 int sport;
42};
43
8774c31b
FT
44struct tcpconn {
45 struct sockaddr_storage name;
c063c3b8 46 struct tcpport *port;
8774c31b
FT
47};
48
6ca53b2e 49int listensock4(int port)
8774c31b
FT
50{
51 struct sockaddr_in name;
52 int fd;
53 int valbuf;
54
55 memset(&name, 0, sizeof(name));
56 name.sin_family = AF_INET;
57 name.sin_port = htons(port);
58 if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
59 return(-1);
60 valbuf = 1;
61 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
62 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
63 close(fd);
64 return(-1);
65 }
66 if(listen(fd, 16) < 0) {
67 close(fd);
68 return(-1);
69 }
70 return(fd);
71}
72
6ca53b2e 73int listensock6(int port)
8774c31b
FT
74{
75 struct sockaddr_in6 name;
76 int fd;
77 int valbuf;
78
79 memset(&name, 0, sizeof(name));
80 name.sin6_family = AF_INET6;
81 name.sin6_port = htons(port);
82 if((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
83 return(-1);
84 valbuf = 1;
85 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &valbuf, sizeof(valbuf));
86 if(bind(fd, (struct sockaddr *)&name, sizeof(name))) {
87 close(fd);
88 return(-1);
89 }
90 if(listen(fd, 16) < 0) {
91 close(fd);
92 return(-1);
93 }
94 return(fd);
95}
96
97static int initreq(struct conn *conn, struct hthead *req)
98{
99 struct tcpconn *tcp = conn->pdata;
100 char nmbuf[256];
101
102 if(tcp->name.ss_family == AF_INET) {
103 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&tcp->name)->sin_addr, nmbuf, sizeof(nmbuf)));
104 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&tcp->name)->sin_port)));
105 } else if(tcp->name.ss_family == AF_INET6) {
106 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&tcp->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
107 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&tcp->name)->sin6_port)));
108 }
c063c3b8 109 headappheader(req, "X-Ash-Server-Port", sprintf3("%i", tcp->port->sport));
46e66302 110 headappheader(req, "X-Ash-Protocol", "http");
8774c31b
FT
111 return(0);
112}
113
114void servetcp(struct muth *muth, va_list args)
115{
116 vavar(int, fd);
117 vavar(struct sockaddr_storage, name);
c063c3b8 118 vavar(struct tcpport *, stcp);
8774c31b 119 FILE *in;
c063c3b8
FT
120 struct conn conn;
121 struct tcpconn tcp;
8774c31b 122
c063c3b8
FT
123 memset(&conn, 0, sizeof(conn));
124 memset(&tcp, 0, sizeof(tcp));
8774c31b 125 in = mtstdopen(fd, 1, 60, "r+");
c063c3b8
FT
126 conn.pdata = &tcp;
127 conn.initreq = initreq;
128 tcp.name = name;
129 tcp.port = stcp;
130 serve(in, &conn);
8774c31b
FT
131}
132
133static void listenloop(struct muth *muth, va_list args)
134{
c063c3b8 135 vavar(struct tcpport *, tcp);
8774c31b
FT
136 int ns;
137 struct sockaddr_storage name;
138 socklen_t namelen;
139
140 while(1) {
141 namelen = sizeof(name);
c063c3b8
FT
142 block(tcp->fd, EV_READ, 0);
143 ns = accept(tcp->fd, (struct sockaddr *)&name, &namelen);
8774c31b
FT
144 if(ns < 0) {
145 flog(LOG_ERR, "accept: %s", strerror(errno));
146 goto out;
147 }
c063c3b8 148 mustart(servetcp, ns, name, tcp);
8774c31b
FT
149 }
150
151out:
c063c3b8
FT
152 close(tcp->fd);
153 free(tcp);
8774c31b
FT
154}
155
156void handleplain(int argc, char **argp, char **argv)
157{
158 int port, fd;
159 int i;
c063c3b8 160 struct tcpport *tcp;
8774c31b
FT
161
162 port = 80;
163 for(i = 0; i < argc; i++) {
164 if(!strcmp(argp[i], "help")) {
165 printf("plain handler parameters:\n");
6ca53b2e
FT
166 printf("\tport=TCP-PORT [80]\n");
167 printf("\t\tThe TCP port to listen on.\n");
8774c31b
FT
168 exit(0);
169 } else if(!strcmp(argp[i], "port")) {
170 port = atoi(argv[i]);
171 } else {
172 flog(LOG_ERR, "unknown parameter `%s' to plain handler", argp[i]);
173 exit(1);
174 }
175 }
176 if((fd = listensock6(port)) < 0) {
177 flog(LOG_ERR, "could not listen on IPv6 (port %i): %s", port, strerror(errno));
178 exit(1);
179 }
c063c3b8
FT
180 omalloc(tcp);
181 tcp->fd = fd;
182 tcp->sport = port;
183 mustart(listenloop, tcp);
8774c31b
FT
184 if((fd = listensock4(port)) < 0) {
185 if(errno != EADDRINUSE) {
186 flog(LOG_ERR, "could not listen on IPv4 (port %i): %s", port, strerror(errno));
187 exit(1);
188 }
189 } else {
41fe6c39
FT
190 omalloc(tcp);
191 tcp->fd = fd;
192 tcp->sport = port;
193 mustart(listenloop, tcp);
8774c31b
FT
194 }
195}