initial server for Windows
[webserver.git] / webserver.c
1 /* depend: */
2 /* cflags: */
3 /* linker: color.o debug.o */
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #ifdef _WIN32 /* Windows */
9 #include <winsock2.h>
10 #include <ws2tcpip.h>
11 #else /* Posix */
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #endif
17
18 #include "debug.h"
19
20 /* types */
21
22 #ifdef _WIN32 /* Windows */
23 typedef SOCKET socket_t;
24 #else /* Posix */
25 typedef int socket_t;
26 #endif
27
28 /* constants */
29
30 #define BUFFER_SIZE 4096
31
32 /* macros */
33
34 /* gobal variables */
35
36 char *progname = NULL;
37 int port = 8080;
38
39 /* help function */
40
41 int usage (int ret)
42 {
43 FILE *fid = ret ? stderr : stdout;
44 fprintf (fid, "usage: %s\n", progname);
45 fprintf (fid, " -h : help message\n");
46 fprintf (fid, " -p : port number (%d)\n", port);
47 fprintf (fid, " -v : verbose level (%d)\n", verbose);
48
49 return ret;
50 }
51
52 /* open listening socket */
53
54 socket_t open_listening_socket (int port)
55 {
56 #ifdef _WIN32 /* Windows */
57 WSADATA WSAData;
58 WSAStartup (MAKEWORD(2,0), &WSAData);
59 assert (INVALID_SOCKET == (socket_t)-1);
60 #endif
61
62 VERBOSE (DEBUG, fprintf (stdout, "Opening socket\n"));
63 #ifdef _WIN32 /* Windows */
64 socket_t sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
65 if (sock == INVALID_SOCKET)
66 return -1;
67 #else /* Posix */
68 socket_t sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
69 if (socket < 0)
70 return -1;
71 #endif
72
73 struct sockaddr_in addr = {0};
74 #ifdef _WIN32 /* Windows */
75 addr.sin_family = AF_INET;
76 #else /* Posix */
77 addr.sin_family = PF_INET;
78 #endif
79 addr.sin_port = htons (port);
80 addr.sin_addr.s_addr = htonl (INADDR_ANY);
81
82 VERBOSE (DEBUG, fprintf (stdout, "Binding socket\n"));
83 int rc = bind (sock, (struct sockaddr *)&addr, sizeof (addr));
84 #ifdef _WIN32 /* Windows */
85 if (rc == SOCKET_ERROR) {
86 VERBOSE (ERROR, fprintf (stderr, "error: %d\n", WSAGetLastError ()));
87 if (closesocket (sock) == SOCKET_ERROR) {
88 VERBOSE (ERROR, fprintf (stderr, "error: %d\n", WSAGetLastError ()));
89 }
90 #else /* Posix */
91 if (socket < 0) {
92 VERBOSE (ERROR, fprintf (stderr, "error: %d\n", errno));
93 close (sock);
94 #endif
95 return -1;
96 }
97
98 VERBOSE (DEBUG, fprintf (stdout, "Configuring socket\n"));
99 #ifdef _WIN32 /* Windows */
100 #else /* Posix */
101 fcntl (sock, F_SETFL, O_NONBLOCK);
102 #endif
103 int val = 1;
104 rc = setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
105 if (rc < 0) {
106 VERBOSE (ERROR, fprintf (stderr, "%s\n", "setsockopt/TCP_NODELAY"));
107 #ifdef _WIN32 /* Windows */
108 closesocket (sock);
109 #else /* Posix */
110 close (sock);
111 #endif
112 return -1;
113 }
114
115 return sock;
116 }
117
118 /* close listening socket */
119 void close_listening_socket (socket_t sock)
120 {
121 #ifdef _WIN32 /* Windows */
122 closesocket (sock);
123 WSACleanup ();
124 #else /* Posix */
125 close (sock);
126 #endif
127 }
128
129 /* main function */
130
131 int main (int argc, char *argv[])
132 {
133 int i = 0;
134 int ret = 0;
135
136 /* program name */
137
138 progname = argv[0];
139 while (progname[i] != '\0') {
140 if ((progname[i] == '/') || (progname[i] == '\\')) {
141 progname += i + 1;
142 i = 0;
143 } else {
144 i++;
145 }
146 }
147
148 /* argument processing */
149
150 while (argc-- > 1) {
151 char *arg = *(++argv);
152 if (arg[0] != '-') {
153 VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
154 return 1;
155 }
156 char c = arg[1];
157 switch (c) {
158 case 'p':
159 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
160 if (arg == NULL) {
161 VERBOSE (ERROR, fprintf (stderr, "%s: missing port number\n", progname); usage (1));
162 return 1;
163 }
164 port = atoi (arg);
165 if (port <= 0) {
166 VERBOSE (ERROR, fprintf (stderr, "%s: incorrect port number (%s)\n", progname, arg); usage (1));
167 return 1;
168 }
169 break;
170 case 'v':
171 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
172 if (arg == NULL) {
173 VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1));
174 return 1;
175 }
176 verbose = atoi (arg);
177 break;
178 case 'h':
179 default:
180 return usage (c != 'h');
181 }
182 }
183
184 VERBOSE (DEBUG, fprintf (stdout, "Initializing socket\n"));
185 socket_t sock = open_listening_socket (port);
186 if (sock == (socket_t)-1) {
187 VERBOSE (ERROR, fprintf (stderr, "Can't open listening socket\n"));
188 return 1;
189 }
190
191 VERBOSE (INFO, fprintf (stdout, "Listening socket on port %d\n", port));
192 sleep (2);
193
194 VERBOSE (DEBUG, fprintf (stdout, "Closing socket\n"));
195 close_listening_socket (sock);
196
197 return ret;
198 }
199
200 // test: webserver.exe -h
201 // test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
202 // test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 =
203 // test: webserver.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
204 // test: webserver.exe error 2>&1 | grep -q 'invalid option'
205 // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level'
206 // test: webserver.exe -p 2>&1 | grep -q 'missing port number'
207 // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number'
208 // test: webserver.exe | grep -q 'Listening socket on port 8080'
209 // test: webserver.exe -p 8000 | grep -q 'Listening socket on port 8000'
210
211 /* vim: set ts=4 sw=4 et: */