This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
87 lines (74 loc) · 2.91 KB
/
main.c
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
//
// Created by Ugo Cottin on 21/11/2020.
//
#include <errno.h>
#include "main.h"
int main(int argc, char** argv) {
websnarf snarf;
snarf.logfile = NULL;
snarf.port = 80;
snarf.timeout = 0;
snarf.maxsize = 4096;
snarf.debug = 0;
snarf.apache = 0;
snarf.resolve = 0;
snarf.v6 = 0;
for(int i = 1; i < argc; i++) {
char* arg = argv[i];
if (strcmp(arg, "--help") == 0) {
printf("usage: %s [options]\n", argv[0]);
printf("--timeout <n> wait at most <n> seconds on a read (default 0 = no timeout)\n"
"--log FILE append output to FILE (default stdout only)\n"
"--port <n> listen on TCP port <n> (default 80)\n"
"--max <n> save at most <n> chars of request (default 4096 bytes)\n"
"--save DIR save all incoming data into DIR/ipaddress\n"
"--debug turn on a bit of debugging\n"
"--apache logs are in Apache style (non implemented)\n"
"--resolve resolve ip hostname of incoming request\n"
"--6 use IPv6 instead of IPv4\n"
"--version show version info\n"
"\n"
"--help show this listing");
} else if (strcmp(arg, "--log") == 0) {
snarf.logfile = argv[i + 1];
snarf.file = fopen(snarf.logfile, "w+");
if (snarf.file == NULL) {
printf("Error: cannot create logfile %s: %s\n", snarf.logfile, strerror(errno));
exit(EXIT_FAILURE);
}
i++;
} else if (strcmp(arg, "--port") == 0) {
snarf.port = (int) strtol(argv[i + 1], NULL, 10);
i++;
} else if (strcmp(arg, "--timeout") == 0) {
snarf.timeout = (int) strtol(argv[i + 1], NULL, 10);
i++;
} else if (strcmp(arg, "--max") == 0) {
snarf.maxsize = (int) strtol(argv[i + 1], NULL, 10);
i++;
} else if (strcmp(arg, "--save") == 0) {
snarf.save_dir = argv[i + 1];
i++;
} else if (strcmp(arg, "--debug") == 0) {
snarf.debug = 1;
} else if (strcmp(arg, "--apache") == 0) {
snarf.apache = 1;
} else if (strcmp(arg, "--resolve") == 0) {
snarf.resolve = 1;
} else if (strcmp(arg, "--6") == 0) {
snarf.v6 = 1;
} else if (strcmp(arg, "--version") == 0) {
printf("Websnarf version %s\n", VERSION);
exit(EXIT_SUCCESS);
} else {
printf("%s in an invalid option\n", arg);
exit(EXIT_FAILURE);
}
}
server _server;
int af = snarf.v6 ? AF_INET6 : AF_INET;
_server.socket = createSocket(snarf, SOCK_STREAM, af);
listen(_server.socket.socket, 5);
run(snarf, _server);
return EXIT_SUCCESS;
}