Commit | Line | Data |
---|---|---|
8512671a LM |
1 | /* depend: */ |
2 | /* cflags: */ | |
06ec8057 | 3 | /* linker: color.o debug.o server.o signal.o */ |
8512671a | 4 | |
184be781 | 5 | #include <assert.h> |
8512671a LM |
6 | #include <stdio.h> |
7 | #include <stdlib.h> | |
8 | ||
9 | #include "debug.h" | |
00afbb65 | 10 | #include "server.h" |
06ec8057 | 11 | #include "signal.h" |
8512671a | 12 | |
184be781 LM |
13 | /* types */ |
14 | ||
8512671a LM |
15 | /* constants */ |
16 | ||
17 | #define BUFFER_SIZE 4096 | |
18 | ||
19 | /* macros */ | |
20 | ||
21 | /* gobal variables */ | |
22 | ||
23 | char *progname = NULL; | |
21ac031b | 24 | int port = 8080; |
06ec8057 | 25 | socket_t sock = INVALID_SOCKET; |
8512671a LM |
26 | |
27 | /* help function */ | |
28 | ||
29 | int usage (int ret) | |
30 | { | |
31 | FILE *fid = ret ? stderr : stdout; | |
32 | fprintf (fid, "usage: %s\n", progname); | |
33 | fprintf (fid, " -h : help message\n"); | |
21ac031b | 34 | fprintf (fid, " -p : port number (%d)\n", port); |
8512671a LM |
35 | fprintf (fid, " -v : verbose level (%d)\n", verbose); |
36 | ||
37 | return ret; | |
38 | } | |
39 | ||
06ec8057 LM |
40 | void stop_server (__attribute__((unused)) int sig) |
41 | { | |
42 | if (sock != INVALID_SOCKET) { | |
43 | close_listening_socket (sock); | |
44 | exit (0); | |
45 | } | |
46 | } | |
47 | ||
8512671a LM |
48 | /* main function */ |
49 | ||
50 | int main (int argc, char *argv[]) | |
51 | { | |
52 | int i = 0; | |
8512671a LM |
53 | |
54 | /* program name */ | |
55 | ||
56 | progname = argv[0]; | |
57 | while (progname[i] != '\0') { | |
58 | if ((progname[i] == '/') || (progname[i] == '\\')) { | |
59 | progname += i + 1; | |
60 | i = 0; | |
61 | } else { | |
62 | i++; | |
63 | } | |
64 | } | |
65 | ||
66 | /* argument processing */ | |
67 | ||
68 | while (argc-- > 1) { | |
69 | char *arg = *(++argv); | |
70 | if (arg[0] != '-') { | |
71 | VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1)); | |
72 | return 1; | |
73 | } | |
74 | char c = arg[1]; | |
75 | switch (c) { | |
21ac031b | 76 | case 'p': |
8512671a LM |
77 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
78 | if (arg == NULL) { | |
21ac031b LM |
79 | VERBOSE (ERROR, fprintf (stderr, "%s: missing port number\n", progname); usage (1)); |
80 | return 1; | |
81 | } | |
82 | port = atoi (arg); | |
83 | if (port <= 0) { | |
84 | VERBOSE (ERROR, fprintf (stderr, "%s: incorrect port number (%s)\n", progname, arg); usage (1)); | |
8512671a LM |
85 | return 1; |
86 | } | |
8512671a | 87 | break; |
8512671a LM |
88 | case 'v': |
89 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; | |
90 | if (arg == NULL) { | |
91 | VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1)); | |
92 | return 1; | |
93 | } | |
94 | verbose = atoi (arg); | |
95 | break; | |
96 | case 'h': | |
97 | default: | |
98 | return usage (c != 'h'); | |
99 | } | |
100 | } | |
101 | ||
184be781 | 102 | VERBOSE (DEBUG, fprintf (stdout, "Initializing socket\n")); |
06ec8057 LM |
103 | sock = open_listening_socket (port); |
104 | if (sock == INVALID_SOCKET) { | |
184be781 LM |
105 | VERBOSE (ERROR, fprintf (stderr, "Can't open listening socket\n")); |
106 | return 1; | |
107 | } | |
184be781 | 108 | VERBOSE (INFO, fprintf (stdout, "Listening socket on port %d\n", port)); |
04a2223d LM |
109 | |
110 | /* main loop */ | |
111 | while (1) { | |
112 | int connection = accept_incoming_connection (sock); | |
113 | if ((connection < 0) && (errno == EAGAIN)) { | |
114 | accept_nonblock_ok = 1; | |
115 | } | |
116 | if (connection < 0) { | |
117 | sleep (1); | |
118 | continue; | |
119 | } | |
120 | ||
121 | VERBOSE (DEBUG, fprintf (stdout, "Server connected, waiting for data\n")); | |
122 | ||
123 | while (1) { | |
124 | unsigned char data[BUFFER_SIZE] = {0}; | |
125 | int len = 0; | |
126 | ||
127 | len = receive_data (connection, data, size (data)); | |
128 | if (len == 0) { | |
129 | // nonblocking connection | |
130 | sleep (1); | |
131 | continue; | |
132 | } | |
133 | if (len < 0) { | |
134 | VERBOSE (WARNING, fprintf (stdout, "Connection closed by peer\n")); | |
135 | close_connection (connection); | |
136 | break; | |
137 | } | |
138 | ||
139 | // processing | |
140 | ||
141 | len = send_data (connection, data, size (data)); | |
142 | if (len < 0) { | |
143 | VERBOSE (WARNING, fprintf (stdout, "Connection closed by peer\n")); | |
144 | close_connection (connection); | |
145 | break; | |
146 | } | |
147 | ||
148 | /* sleep so that we go trhough remaining data in receive buffer */ | |
149 | usleep (1e5); | |
150 | } | |
151 | } | |
184be781 LM |
152 | |
153 | VERBOSE (DEBUG, fprintf (stdout, "Closing socket\n")); | |
154 | close_listening_socket (sock); | |
155 | ||
06ec8057 | 156 | return 2; |
8512671a LM |
157 | } |
158 | ||
159 | // test: webserver.exe -h | |
160 | // test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' | |
21ac031b | 161 | // test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 = |
8512671a LM |
162 | // test: webserver.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
163 | // test: webserver.exe error 2>&1 | grep -q 'invalid option' | |
21ac031b LM |
164 | // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level' |
165 | // test: webserver.exe -p 2>&1 | grep -q 'missing port number' | |
166 | // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number' | |
06ec8057 LM |
167 | // test: ( webserver.exe & pid=$!; ( sleep 1; kill -TERM $pid ) ) | grep -q 'Listening socket on port 8080' |
168 | // test: ( webserver.exe -p 8008 & pid=$!; ( sleep 1; kill -TERM $pid ) ) | grep -q 'Listening socket on port 8008' | |
169 | // test: webserver.exe & pid=$!; sleep 1; kill -INT $pid; ps aux | grep -q [w]ebserver.exe && kill -TERM $pid || rc=1; test x$rc = x1 | |
8512671a LM |
170 | |
171 | /* vim: set ts=4 sw=4 et: */ |