-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
executable file
·305 lines (259 loc) · 6.56 KB
/
server.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
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
/*
* server.c
*
* This file contains the server application. It simply waits for the
* client to send it something, which it interprets as the name of some
* file. It then sends an OK to the client (if it can access the requested
* file) and finally sends the file.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <assert.h>
#include "mysock.h"
static char usage[] = "usage: %s \n";
static void do_connection(mysocket_t bindsd);
static int get_nvt_line(int sd, char *);
static int process_line(int sd, char *);
static int local_name(mysocket_t sd, char *name);
/**********************************************************************/
int
main(int argc, char *argv[])
{
struct sockaddr_in sin;
mysocket_t bindsd;
int len, opt, errflg = 0;
char localname[256];
/* Parse the command line */
while ((opt = getopt(argc, argv, "")) != EOF)
{
switch (opt)
{
case '?':
++errflg;
break;
}
}
if (errflg || optind != argc)
{
fprintf(stderr, usage, argv[0]);
exit(EXIT_FAILURE);
}
/* open connection on any available port */
if ((bindsd = mysocket()) < 0)
{
perror("mysocket");
exit(EXIT_FAILURE);
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(0);
len = sizeof(struct sockaddr_in);
if (mybind(bindsd, (struct sockaddr *) &sin, len) < 0)
{
perror("mybind");
exit(EXIT_FAILURE);
}
if (mylisten(bindsd, 5) < 0)
{
perror("mylisten");
exit(EXIT_FAILURE);
}
if (local_name(bindsd, localname) < 0)
{
perror("local_name");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Server's address is %s\n", localname);
fflush(stderr);
for (;;)
{
mysocket_t sd;
/* just keep accepting connections forever */
if ((sd = myaccept(bindsd, (struct sockaddr *) &sin, &len)) < 0)
{
perror("myaccept");
exit(EXIT_FAILURE);
}
assert(sin.sin_family == AF_INET);
fprintf(stderr, "connected to %s at port %u\n",
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
do_connection(sd);
} /* end for(;;) */
if (myclose(bindsd) < 0)
perror("myclose (bindsd)");
return 0;
}
/* process a single client connection */
static void do_connection(mysocket_t sd)
{
char line[256];
int rc;
/* loop over:
- get a request from the client
- process the request
*/
for (;;)
{
rc = get_nvt_line(sd, line);
if (rc < 0 || !*line)
goto done;
fprintf(stderr, "client: %s\n", line);
if (process_line(sd, line) < 0)
{
perror("process_line");
goto done;
}
} /* for (;;) */
done:
if (myclose(sd) < 0)
{
perror("myclose (sd)");
}
}
/**********************************************************************/
/* get_nvt_line
*
* Retrieves the next line of NVT ASCII from mysocket layer.
*
* Returns
* 0 on success
* -1 on failure
*/
static int
get_nvt_line(int sd, char *line)
{
char last_char;
char this_char;
int len;
last_char = '\0';
for (;;)
{
len = myread(sd, &this_char, sizeof(this_char));
if (len < 0)
return -1;
if (len == 0)
{
/* Connection ended before line terminator (or empty string) */
*line = '\0';
return 0;
}
/** fprintf(stderr, "read character %c\n", this_char); **/
if (last_char == '\r' && this_char == '\n')
{
/* Reached the end of line. Already wrote \r into string, overwrite
* it with a NUL */
line[-1] = '\0';
return 0;
}
*line++ = this_char;
last_char = this_char;
}
return -1;
}
/**********************************************************************/
/* process_line
*
* Process the request (a filename) from the client. Send back the
* response and then the content of the requested file through
* mysocket layer.
*
* Returns
* 0 on success
* -1 on failure
*/
static int
process_line(int sd, char *line)
{
char resp[5000];
int fd = -1, length;
if (!*line || access(line, R_OK) < 0)
{
sprintf(resp, "%s,-1,File does not exist or access denied\r\n", line);
}
else
{
if ((fd = open(line, O_RDONLY)) < 0)
{
sprintf(resp, "%s,-1,File could not be opened\r\n", line);
}
else
{
sprintf(resp, "%s,%lu,Ok\r\n", line, lseek(fd, 0, SEEK_END));
lseek(fd, 0, SEEK_SET);
}
}
/** fprintf(stderr, "sending to client: %s of length %d bytes\n", resp, strlen(resp)); **/
/* Return the response to the client */
if (mywrite(sd, resp, strlen(resp)) < 0)
{
if (fd != -1)
close(fd);
return -1;
}
if (fd == -1)
return 0;
for (;;)
{
length = read(fd, resp, sizeof(resp));
if (length == 0)
break;
if (length == -1)
{
perror("read");
close(fd);
return -1;
}
/* fwrite(resp, length, 1, stdout); */
if (mywrite(sd, resp, length) < 0)
{
close(fd);
return -1;
}
}
close(fd);
return 0;
}
/* local_name()
*
* Takes in a mysocket descriptor and finds the (local_addr, local_port)
* associated with sd. It places into name a string of the form
* "hostname:portname"; name must have memory allocated already.
*
* XXX: This is broken on multi-homed hosts (e.g. VNS).
*
* Returns 0 on success and -1 on failure.
*/
static int local_name(mysocket_t sd, char *name)
{
#define MAX_HOSTNAMELEN 100
struct sockaddr_in sin;
socklen_t sin_len = sizeof(sin);
char myhostname[MAX_HOSTNAMELEN];
assert(name);
/* get local port associated with the mysocket */
if (mygetsockname(sd, (struct sockaddr *) &sin, &sin_len) < 0)
{
assert(0);
return -1;
}
/* get name of current host */
if (gethostname(myhostname, sizeof(myhostname)) < 0)
{
assert(0);
return -1;
}
sprintf(name, "%s:%u", myhostname, ntohs(sin.sin_port));
return 0;
}