aboutsummaryrefslogtreecommitdiff
path: root/src/examples/mhd2spdy_structures.h
blob: 88c28c645df5ae7a064579346f9926e89aa1a976 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
    Copyright (C) 2013 Andrey Uzunov

    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/>.
*/

/**
 * @file mhd2spdy_structures.h
 * @brief  Common structures, functions, macros and global variables.
 * @author Andrey Uzunov
 */
#ifndef STRUCTURES_H
#define STRUCTURES_H

#define _GNU_SOURCE
 
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <microhttpd.h>
#include <signal.h>
#include <poll.h>
#include <fcntl.h>
#include <regex.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <spdylay/spdylay.h>
#include <getopt.h>


/* WANT_READ if SSL connection needs more input; or WANT_WRITE if it
   needs more output; or IO_NONE. This is necessary because SSL/TLS
   re-negotiation is possible at any time. Spdylay API offers
   similar functions like spdylay_session_want_read() and
   spdylay_session_want_write() but they do not take into account
   SSL connection. */
enum
{
  IO_NONE,
  WANT_READ,
  WANT_WRITE
};


struct Proxy;


struct SPDY_Connection {
  SSL *ssl;
  spdylay_session *session;
  struct SPDY_Connection *prev;
  struct SPDY_Connection *next;
  struct Proxy *proxies_head;
  struct Proxy *proxies_tail;
  char *host;
  int fd;
  int want_io;
  uint counter;
  uint streams_opened;
  bool is_tls;
};


struct URI
{
  char * full_uri;
  char * scheme;
  char * host_and_port;
  char * host;
  char * path;
  char * path_and_more;
  char * query;
  char * fragment;
  uint16_t port;
};


struct HTTP_URI;


struct Proxy
{
	struct MHD_Connection *http_connection;
	struct MHD_Response *http_response;
	struct URI *uri;
  struct HTTP_URI *http_uri;
  struct SPDY_Connection *spdy_connection;
  struct Proxy *next;
  struct Proxy *prev;
	char *url;
	char *version;
	void *http_body;
	size_t http_body_size;
	ssize_t length;
	int status;
	int id;
	bool done;
	bool error;
  bool http_active;
  bool spdy_active;
};


struct HTTP_URI
{
  char * uri;
  struct Proxy * proxy;
};


struct SPDY_Headers
{
  const char **nv;
  int num;
  int cnt;
};


struct global_options
{
  char *spdy2http_str;
  struct SPDY_Connection *spdy_connection;
  struct SPDY_Connection *spdy_connections_head;
  struct SPDY_Connection *spdy_connections_tail;
  int streams_opened;
  int responses_pending;
  regex_t uri_preg;
  size_t global_memory;
  SSL_CTX *ssl_ctx;
  uint32_t total_spdy_connections;
  uint16_t spdy_proto_version;
  uint16_t listen_port;
  bool verbose;
  bool only_proxy;
  bool spdy_data_received;
}
glob_opt;


//forbidden headers
#define SPDY_HTTP_HEADER_TRANSFER_ENCODING "transfer-encoding"
#define SPDY_HTTP_HEADER_PROXY_CONNECTION "proxy-connection"
#define SPDY_HTTP_HEADER_KEEP_ALIVE "keep-alive"
#define SPDY_HTTP_HEADER_CONNECTION "connection"

#define MAX_SPDY_CONNECTIONS 100

#define SPDY_MAX_OUTLEN 4096

/**
 * Insert an element at the head of a DLL. Assumes that head, tail and
 * element are structs with prev and next fields.
 *
 * @param head pointer to the head of the DLL (struct ? *)
 * @param tail pointer to the tail of the DLL (struct ? *)
 * @param element element to insert (struct ? *)
 */
#define DLL_insert(head,tail,element) do { \
	(element)->next = (head); \
	(element)->prev = NULL; \
	if ((tail) == NULL) \
		(tail) = element; \
	else \
		(head)->prev = element; \
	(head) = (element); } while (0)


/**
 * Remove an element from a DLL. Assumes
 * that head, tail and element are structs
 * with prev and next fields.
 *
 * @param head pointer to the head of the DLL (struct ? *)
 * @param tail pointer to the tail of the DLL (struct ? *)
 * @param element element to remove (struct ? *)
 */
#define DLL_remove(head,tail,element) do { \
	if ((element)->prev == NULL) \
		(head) = (element)->next;  \
	else \
		(element)->prev->next = (element)->next; \
	if ((element)->next == NULL) \
		(tail) = (element)->prev;  \
	else \
		(element)->next->prev = (element)->prev; \
	(element)->next = NULL; \
	(element)->prev = NULL; } while (0)


#define PRINT_INFO(msg) do{\
  if(glob_opt.verbose){\
	printf("%i:%s\n", __LINE__, msg);\
	fflush(stdout);\
	}\
  }\
	while(0)


#define PRINT_INFO2(fmt, ...) do{\
  if(glob_opt.verbose){\
	printf("%i\n", __LINE__);\
	printf(fmt,##__VA_ARGS__);\
	printf("\n");\
	fflush(stdout);\
	}\
	}\
	while(0)
  

#define DIE(msg) do{\
	printf("FATAL ERROR (line %i): %s\n", __LINE__, msg);\
	fflush(stdout);\
  exit(EXIT_FAILURE);\
	}\
	while(0)
  
  


void
free_uri(struct URI * uri);


int
init_parse_uri(regex_t * preg);


void
deinit_parse_uri(regex_t * preg);


int
parse_uri(regex_t * preg,
          char * full_uri,
          struct URI ** uri);


void
free_proxy(struct Proxy *proxy);


void *
au_malloc(size_t size);


#endif