1/* $Id: upnphttp.h,v 1.40 2014/12/09 16:41:21 nanard Exp $ */
2/* MiniUPnP project
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2014 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
7
8#ifndef UPNPHTTP_H_INCLUDED
9#define UPNPHTTP_H_INCLUDED
10
11#include <netinet/in.h>
12#include <sys/queue.h>
13
14#include "config.h"
15
16#ifdef ENABLE_HTTPS
17#include <openssl/ssl.h>
18#endif /* ENABLE_HTTPS */
19
20#if 0
21/* according to "UPnP Device Architecture 1.0" */
22#define UPNP_VERSION_STRING "UPnP/1.0"
23#else
24/* according to "UPnP Device Architecture 1.1" */
25#define UPNP_VERSION_STRING "UPnP/1.1"
26#endif
27
28/* server: HTTP header returned in all HTTP responses : */
29#define MINIUPNPD_SERVER_STRING	OS_VERSION " " UPNP_VERSION_STRING " MiniUPnPd/" MINIUPNPD_VERSION
30
31/*
32 states :
33  0 - waiting for data to read
34  1 - waiting for HTTP Post Content.
35  ...
36  >= 100 - to be deleted
37*/
38enum httpStates {
39	EWaitingForHttpRequest = 0,
40	EWaitingForHttpContent,
41	ESendingContinue,
42	ESendingAndClosing,
43	EToDelete = 100
44};
45
46enum httpCommands {
47	EUnknown = 0,
48	EGet,
49	EPost,
50	ESubscribe,
51	EUnSubscribe
52};
53
54struct upnphttp {
55	int socket;
56	struct in_addr clientaddr;	/* client address */
57#ifdef ENABLE_IPV6
58	int ipv6;
59	struct in6_addr clientaddr_v6;
60#endif /* ENABLE_IPV6 */
61#ifdef ENABLE_HTTPS
62	SSL * ssl;
63#endif /* ENABLE_HTTPS */
64	enum httpStates state;
65	char HttpVer[16];
66	/* request */
67	char * req_buf;
68	char accept_language[8];
69	int req_buflen;
70	int req_contentlen;
71	int req_contentoff;     /* header length */
72	enum httpCommands req_command;
73	int req_soapActionOff;
74	int req_soapActionLen;
75	int req_HostOff;	/* Host: header */
76	int req_HostLen;
77#ifdef ENABLE_EVENTS
78	int req_CallbackOff;	/* For SUBSCRIBE */
79	int req_CallbackLen;
80	int req_Timeout;
81	int req_SIDOff;		/* For UNSUBSCRIBE */
82	int req_SIDLen;
83	const char * res_SID;
84#ifdef UPNP_STRICT
85	int req_NTOff;
86	int req_NTLen;
87#endif
88#endif
89	int respflags;				/* see FLAG_* constants below */
90	/* response */
91	char * res_buf;
92	int res_buflen;
93	int res_sent;
94	int res_buf_alloclen;
95	LIST_ENTRY(upnphttp) entries;
96};
97
98/* Include the "Timeout:" header in response */
99#define FLAG_TIMEOUT	0x01
100/* Include the "SID:" header in response */
101#define FLAG_SID		0x02
102
103/* If set, the POST request included a "Expect: 100-continue" header */
104#define FLAG_CONTINUE	0x40
105
106/* If set, the Content-Type is set to text/xml, otherwise it is text/xml */
107#define FLAG_HTML		0x80
108
109/* If set, the corresponding Allow: header is set */
110#define FLAG_ALLOW_POST			0x100
111#define FLAG_ALLOW_SUB_UNSUB	0x200
112
113#ifdef ENABLE_HTTPS
114int init_ssl(void);
115void free_ssl(void);
116#endif /* ENABLE_HTTPS */
117
118/* New_upnphttp() */
119struct upnphttp *
120New_upnphttp(int);
121
122#ifdef ENABLE_HTTPS
123void
124InitSSL_upnphttp(struct upnphttp *);
125#endif /* ENABLE_HTTPS */
126
127/* CloseSocket_upnphttp() */
128void
129CloseSocket_upnphttp(struct upnphttp *);
130
131/* Delete_upnphttp() */
132void
133Delete_upnphttp(struct upnphttp *);
134
135/* Process_upnphttp() */
136void
137Process_upnphttp(struct upnphttp *);
138
139/* BuildHeader_upnphttp()
140 * build the header for the HTTP Response
141 * also allocate the buffer for body data
142 * return -1 on error */
143int
144BuildHeader_upnphttp(struct upnphttp * h, int respcode,
145                     const char * respmsg,
146                     int bodylen);
147
148/* BuildResp_upnphttp()
149 * fill the res_buf buffer with the complete
150 * HTTP 200 OK response from the body passed as argument */
151void
152BuildResp_upnphttp(struct upnphttp *, const char *, int);
153
154/* BuildResp2_upnphttp()
155 * same but with given response code/message */
156void
157BuildResp2_upnphttp(struct upnphttp * h, int respcode,
158                    const char * respmsg,
159                    const char * body, int bodylen);
160
161int
162SendResp_upnphttp(struct upnphttp *);
163
164/* SendRespAndClose_upnphttp() */
165void
166SendRespAndClose_upnphttp(struct upnphttp *);
167
168#endif
169
170