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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
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 structures.h
* @author Andrey Uzunov
*/
/*
* TODOs
* non blocking SSL connect
* check certificate
*
*
*
*
*/
#include "mhd2spdy_structures.h"
#include "mhd2spdy_spdy.h"
#include "mhd2spdy_http.h"
static int run = 1;
//static int spdy_close = 0;
static void catch_signal(int signal)
{
//spdy_close = 1;
run = 0;
}
int
run_everything ()
{
unsigned long long timeoutlong=0;
struct timeval timeout;
int ret;
fd_set rs;
fd_set ws;
fd_set es;
int maxfd = -1;
int maxfd_s = -1;
struct MHD_Daemon *daemon;
nfds_t spdy_npollfds = 1;
//struct pollfd spdy_pollfds[MAX_SPDY_CONNECTIONS];
struct URI * spdy2http_uri = NULL;
//int spdy_nfds;
//int spdylay_timeout = 0;
struct SPDY_Connection *connection;
struct SPDY_Connection *connections[MAX_SPDY_CONNECTIONS];
struct SPDY_Connection *connection_for_delete;
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
PRINT_INFO("signal failed");
if (signal(SIGINT, catch_signal) == SIG_ERR)
PRINT_INFO("signal failed");
//spdy2http_url = argv[2];
glob_opt.streams_opened = 0;
glob_opt.responses_pending = 0;
glob_opt.global_memory = 0;
//spdy_proto_version = 0;
srand(time(NULL));
if(init_parse_uri(&glob_opt.uri_preg))
DIE("Regexp compilation failed");
if(NULL != glob_opt.spdy2http_str)
{
ret = parse_uri(&glob_opt.uri_preg, glob_opt.spdy2http_str, &spdy2http_uri);
if(ret != 0)
DIE("spdy_parse_uri failed");
}
SSL_load_error_strings();
SSL_library_init();
glob_opt.ssl_ctx = SSL_CTX_new(SSLv23_client_method());
if(glob_opt.ssl_ctx == NULL) {
PRINT_INFO2("SSL_CTX_new %s", ERR_error_string(ERR_get_error(), NULL));
abort();
}
spdy_ssl_init_ssl_ctx(glob_opt.ssl_ctx, &glob_opt.spdy_proto_version);
daemon = MHD_start_daemon (
MHD_SUPPRESS_DATE_NO_CLOCK,
glob_opt.listen_port,
NULL, NULL, &http_cb_request, NULL,
MHD_OPTION_URI_LOG_CALLBACK, &http_log_cb, NULL,
MHD_OPTION_END);
if(NULL==daemon)
DIE("MHD_start_daemon failed");
do
{
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if(NULL == glob_opt.spdy_connection && NULL != glob_opt.spdy2http_str)
{
glob_opt.spdy_connection = spdy_connect(spdy2http_uri, spdy2http_uri->port, strcmp("https", spdy2http_uri->scheme)==0);
if(NULL == glob_opt.spdy_connection && glob_opt.only_proxy)
PRINT_INFO("cannot connect to the proxy");
}
//PRINT_INFO("while1");
FD_ZERO(&rs);
FD_ZERO(&ws);
FD_ZERO(&es);
/*if(glob_opt.spdy_data_received)
{
timeout.tv_sec = 0;
timeout.tv_usec = 0;
glob_opt.spdy_data_received = false;
}
else{*/
/*if(glob_opt.responses_pending || glob_opt.streams_opened)// TODO only streams_opened true?
timeout.tv_usec = 0; //return immediately
else
{*/
ret = MHD_get_timeout(daemon, &timeoutlong);
if(MHD_NO == ret || timeoutlong > 5000)
timeout.tv_sec = 5;
else
{
timeout.tv_sec = timeoutlong / 1000;
timeout.tv_usec = (timeoutlong % 1000) * 1000;
}
//}
//}
if(MHD_NO == MHD_get_fdset (daemon,
&rs,
&ws,
&es,
&maxfd))
{
PRINT_INFO("MHD_get_fdset error");
}
assert(-1 != maxfd);
maxfd_s = spdy_get_selectfdset(
&rs,
&ws,
&es,
connections, MAX_SPDY_CONNECTIONS, &spdy_npollfds);
if(maxfd_s > maxfd) maxfd = maxfd_s;
PRINT_INFO2("MHD timeout %i %i", timeout.tv_sec, timeout.tv_usec);
//TODO
//timeout.tv_sec = 0;
//timeout.tv_usec = 0;
glob_opt.spdy_data_received = false;
ret = select(maxfd+1, &rs, &ws, &es, &timeout);
PRINT_INFO2("timeout now %i %i", timeout.tv_sec, timeout.tv_usec);
switch(ret) {
case -1:
PRINT_INFO2("select error: %i", errno);
break;
case 0:
break;
default:
PRINT_INFO("run");
MHD_run_from_select(daemon,&rs, &ws, &es);
spdy_run_select(&rs, &ws, &es, connections, spdy_npollfds);
if(glob_opt.spdy_data_received)
{
PRINT_INFO("MHD run again");
MHD_run_from_select(daemon,&rs, &ws, &es);
}
break;
}
/*
//if(glob_opt.streams_opened) spdylay_timeout = 500;
//if(glob_opt.responses_pending || glob_opt.streams_opened) spdylay_timeout = 0;
//else spdylay_timeout = 0;
//else spdylay_timeout = 0;
spdy_get_pollfdset(spdy_pollfds, connections, MAX_SPDY_CONNECTIONS, &spdy_npollfds);
//TODO
//spdylay_timeout = 0;
PRINT_INFO2("spdylay timeout %i", spdylay_timeout);
ret = poll(spdy_pollfds, spdy_npollfds, spdylay_timeout);
if(ret == -1)
DIE("poll");
if(ret > 0){
PRINT_INFO("spdy_run");
spdy_run(spdy_pollfds, connections, spdy_npollfds);
}*/
}
while(run);
//TODO exit from loop and clean
MHD_stop_daemon (daemon);
//TODO SSL_free brakes
spdy_free_connection(glob_opt.spdy_connection);
connection = glob_opt.spdy_connections_head;
while(NULL != connection)
{
connection_for_delete = connection;
connection = connection_for_delete->next;
glob_opt.streams_opened -= connection_for_delete->streams_opened;
DLL_remove(glob_opt.spdy_connections_head, glob_opt.spdy_connections_tail, connection_for_delete);
spdy_free_connection(connection_for_delete);
}
free_uri(spdy2http_uri);
deinit_parse_uri(&glob_opt.uri_preg);
SSL_CTX_free(glob_opt.ssl_ctx);
ERR_free_strings();
EVP_cleanup();
PRINT_INFO2("spdy streams: %i; http requests: %i", glob_opt.streams_opened, glob_opt.responses_pending);
PRINT_INFO2("memory allocated %zu bytes", glob_opt.global_memory);
return 0;
}
void
display_usage()
{
printf(
"Usage: http2spdy [-vo] [-b <SPDY2HTTP-PROXY>] -p <PORT>\n"
"TODO\n"
);
}
int
main (int argc,
char *const *argv)
{
int getopt_ret;
int option_index;
struct option long_options[] = {
{"port", required_argument, 0, 'p'},
{"backend-proxy", required_argument, 0, 'b'},
{"verbose", no_argument, 0, 'v'},
{"only-proxy", no_argument, 0, 'o'},
{0, 0, 0, 0}
};
while (1)
{
getopt_ret = getopt_long( argc, argv, "p:b:vo", long_options, &option_index);
if (getopt_ret == -1)
break;
switch(getopt_ret)
{
case 'p':
glob_opt.listen_port = atoi(optarg);
break;
case 'b':
glob_opt.spdy2http_str = strdup(optarg);
if(NULL == glob_opt.spdy2http_str)
return 1;
break;
case 'v':
glob_opt.verbose = true;
break;
case 'o':
glob_opt.only_proxy = true;
break;
case 0:
PRINT_INFO("0 from getopt");
break;
case '?':
display_usage();
return 1;
default:
DIE("default from getopt");
}
}
if(
0 == glob_opt.listen_port
|| (glob_opt.only_proxy && NULL == glob_opt.spdy2http_str)
)
{
display_usage();
return 1;
}
return run_everything();
}
|