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