-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
408 lines (359 loc) · 9.96 KB
/
server.c
File metadata and controls
408 lines (359 loc) · 9.96 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
char dest_host[20];
int dest_port;
#define SERVER_STRING "Server: jdbhttpd/0.1.0\r\n"
#define ISspace(x) isspace((int)(x))
#define MAXBUF 1024
int GetLine(SSL*, char*, int);
void error_die(const char*);
int InitSocket(u_short*);
void Return501(SSL*);
void Return400(SSL*);
void Return404(SSL*);
void cat(SSL*, FILE*);
void accept_request(SSL *);
int Strcmp(const char *, const char *);
int char2num(const char *);
int Transmit(SSL*, int);
int SendHeaders(int , int );
int char2num(const char *buf)
{
int i = 0, sum = 0;
while(buf[i] != '\0' && buf[i] != '\n'){
sum *= 10;
sum += buf[i]-'0';
i++;
}
return sum;
}
int Strcmp(const char * buf, const char * arr)
{
int i = 0;
for(i = 0; i < 15; i++){
if(buf[i] != arr[i]){
return 0;
}
}
return 1;
}
void error_die(const char *sc)
{
perror(sc);
exit(1);
}
int GetLine(SSL* ssl, char *buf, int size)
{
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n'))
{
n = SSL_read(ssl, &c, 1);
/* DEBUG printf("%02X\n", c); */
if (n > 0)
{
if (c == '\r')
{
n = SSL_read(ssl, &c, 1);
/* DEBUG printf("%02X\n", c); */
if(c != '\n')
printf("error: GetLine end is't \n");
}
buf[i] = c;
i++;
}
else
c = '\n';
}
buf[i] = '\0';
printf("%s\n",buf);
return(i);
}
void accept_request(SSL *client)
{
printf("accept_request start=========================\n");
int clientSocket = 0;
char buf[1024];
size_t numchars;
char method[255];
char path[512];
char url[255];
size_t i, j;
struct stat st;
int cgi = 0;
int contentLength = 0;
char *query_string = NULL;
numchars = GetLine(client, buf, sizeof(buf));
i = 0; j = 0;
while(!ISspace(buf[i]) && (i < sizeof(method) - 1))
method[i] = buf[i],i++;
j = i;
method[i] = '\0';
printf("%s\n",buf);
printf("method =====> %s\n", method);
if(strcasecmp(method, "GET") && strcasecmp(method, "POST"))
{
Return501(client);
return ;
}
i = 0;
while (ISspace(buf[j]) && (j < numchars))
j++;
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars))
{
url[i] = buf[j];
i++; j++;
}
url[i] = '\0';
if(strcasecmp(method, "GET") == 0){
query_string = url;
while(( *query_string != '?') && (*query_string != '\0'))
query_string++;
if(*query_string == '?'){
cgi = 1;
*query_string++ = '\0';
}
}
sprintf(path, "htdocs%s", url);
if(path[strlen(path) - 1] == '/')
strcat(path, "index.html");
/* DEBUG printf("path: %s\n",path); */
if(stat(path, &st) == -1){
while((numchars > 0) && strcmp("\n", buf))
numchars = GetLine(client, buf, sizeof(buf));
Return404(client);
}
else
{
/*
if((st.st_mode & S_IFMT) == S_IFDIR)
strcat(path, "/index.html");
if((st.st_mode & S_IXUSR) ||
(st.st_mode & S_IXGRP) ||
(st.st_mode & S_IXOTH) )
cgi = 0;
*/
while(1){
int iii = GetLine(client, buf, 1024);
if(Strcmp(buf,"Content-Length:")){
contentLength = char2num(buf+16);
//printf("Contentlen:%d/n",contentLength);
}
// printf("iii:%d -- %s",iii,buf);
if(iii == 1) break;
}
Transmit(client, contentLength);
}
close(clientSocket);
}
int Transmit(SSL *client ,int contentLength)
{
/* DEBUG printf("Transmit start =========\n"); */
int fd = 0;
struct sockaddr_in dest;
char buf[1024];
SSL_read(client, buf, contentLength);
buf[contentLength] = '\0';
fd = socket( AF_INET, SOCK_STREAM, 0 );
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(dest_port);
if (inet_aton(dest_host, (struct in_addr *) &dest.sin_addr.s_addr) == 0) {
perror("error 222");
exit(errno);
}
if (connect(fd, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
perror("Connect ");
exit(errno);
}
SendHeaders(fd, contentLength);
write(fd, buf, contentLength);
close(fd);
return 0;
}
int SendHeaders(int client, int contentLength)
{
char buf[256];
strcpy(buf, "POST /zxsm_gzh_recall HTTP/1.1\r\n");
write(client, buf, strlen(buf));
strcpy(buf, "Content-Type: text/xml;charset=ISO-8859-1\r\n");
write(client, buf, strlen(buf));
sprintf(buf, "Content-Length: %d\r\n", contentLength);
write(client, buf, strlen(buf));
strcpy(buf, "HOST: 172.20.11.36:16500\r\n");
write(client, buf, strlen(buf));
strcpy(buf, "Connection: Keep-Alive\r\n");
write(client, buf, strlen(buf));
strcpy(buf, "User-Agent: Apache-HttpClient/4.3.5 (java 1.5)\r\n");
write(client, buf, strlen(buf));
strcpy(buf, "Accept-Encoding: gzip,deflate\r\n");
write(client, buf, strlen(buf));
strcpy(buf, "\r\n");
write(client, buf, strlen(buf));
return 1;
}
void cat(SSL *ssl, FILE *resource)
{
char buf[1024];
fgets(buf, sizeof(buf), resource);
while (!feof(resource))
{
SSL_write(ssl, buf, strlen(buf));
fgets(buf, sizeof(buf), resource);
}
}
void Return404(SSL *ssl)
{
char buf[256];
sprintf(buf, "HTTP/1.0 404 NOT FOUNT\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "Content-type: text/html\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "<h1>404 Not Fount </h1>\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "The requested URL was not found on this server.\r\n");
SSL_write(ssl, buf, strlen(buf));
}
void Return400(SSL *ssl)
{
char buf[256];
sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "Content-type: text/html\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "<P>400 error. Your browser sent a bad request, ");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "such as a POST without a Content-Length.\r\n");
SSL_write(ssl, buf, strlen(buf));
}
void Return501(SSL * ssl)
{
char buf[256];
int len;
sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, SERVER_STRING);
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "Content-Type: text/html\r\n");
SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "\r\n");
len = SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
len = SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "</TITLE></HEAD>\r\n");
len = SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "<BODY><P>HTTP request method not supported.\r\n");
len = SSL_write(ssl, buf, strlen(buf));
sprintf(buf, "</BODY></HTML>\r\n");
SSL_write(ssl, buf, strlen(buf));
}
int InitSocket(u_short *port)
{
int httpd = 0;
int on = 1;
struct sockaddr_in sk_in;
httpd = socket(PF_INET, SOCK_STREAM, 0);
if(httpd == -1)
error_die("socket");
memset(&sk_in, 0, sizeof(sk_in));
sk_in.sin_family = AF_INET;
sk_in.sin_port = htons(*port);
sk_in.sin_addr.s_addr = htonl(INADDR_ANY);
//设置改sokcet立即释放端口,让端口可以重复使用
if((setsockopt(httpd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))<0) {
error_die("setsockopt failed");
}
if(bind(httpd, (struct sockaddr*)&sk_in, sizeof(sk_in)) < 0){
error_die("bind");
}
if(*port == 0){
socklen_t len = sizeof(sk_in);
if(getsockname(httpd, (struct sockaddr *)&sk_in, &len) == -1)
error_die("getsockanme");
*port = ntohs(sk_in.sin_port);
}
if(listen(httpd, 5) < 0)
error_die("listen");
printf("Init Socket Success\n");
return(httpd);
}
int main(int argc, char **argv)
{
int sockfd = -1;
int new_fd = -1;
socklen_t len;
struct sockaddr_in their_addr;
u_short myport = 9999;
SSL_CTX *ctx;
if(argc < 4){
strcpy(dest_host, "172.20.11.36");
dest_port = 16500;
}
else {
printf("%s\n", argv[3]);
strcpy(dest_host, argv[3]);
dest_port = char2num(argv[4]);
}
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ctx = SSL_CTX_new(TLSv1_2_server_method());
if (ctx == NULL) {
ERR_print_errors_fp(stdout);
exit(1);
}
if (SSL_CTX_use_certificate_file(ctx, argv[1], SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stdout);
exit(1);
}
if (SSL_CTX_use_PrivateKey_file(ctx, argv[2], SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stdout);
exit(1);
}
if (!SSL_CTX_check_private_key(ctx)) {
ERR_print_errors_fp(stdout);
exit(1);
}
sockfd = InitSocket(&myport);
while (1) {
SSL *ssl;
len = sizeof(struct sockaddr);
if ((new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &len)) == -1) {
perror("accept");
exit(errno);
} else
printf("server: got connection from %s, port %d, socket %d \n",
inet_ntoa(their_addr.sin_addr),ntohs(their_addr.sin_port), new_fd);
ssl = SSL_new(ctx);
SSL_set_fd(ssl, new_fd);
if (SSL_accept(ssl) == -1) {
printf("error:accept");
goto finish;
}
accept_request(ssl);
finish:
SSL_shutdown(ssl);
SSL_free(ssl);
close(new_fd);
}
close(sockfd);
SSL_CTX_free(ctx);
return 0;
}