-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
277 lines (232 loc) · 8.06 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
#include <ctype.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
// Flag for use by child processes.
int RUNNING = 1;
// Structure containing information about the server.
struct server_instance {
int f_descriptor;
struct sockaddr_in address;
};
// Structure containing information about some arbitrary client.
struct client_instance {
int c_descriptor;
struct sockaddr_in address;
};
// General logging functions for the server.
void err(char* err_msg, int code);
void warn(char* warn_msg);
void success(char* s_msg);
// Instantiate the server running on some port and it.
struct server_instance* start_server(const int port);
// Wait for clients and once they're accepted, start a process for them.
void accept_client(const struct server_instance* s_instance);
// Maintain a connection to a client in the child proccess once a connection has been made.
void maintain_client(const struct server_instance* s_instance, struct client_instance* c_instance);
// Server get command. Sends contents of a file.
void server_get(struct client_instance* c_instance, const char* argument);
// Server put comamnd. Writes to file.
void server_put(struct client_instance* c_instance, const char* argument);
int main(int argc, char* argv[])
{
// Verify Port
if (argc < 2 || atoi(argv[1]) < 1024 || atol(argv[1]) > 65535) {
exit(-1);
}
// Server Setup
const int port = atoi(argv[1]);
const struct server_instance* s_instance = start_server(port);
// Run Server
while (RUNNING)
accept_client(s_instance);
return 0;
}
struct server_instance* start_server(const int port)
{
// Create socket.
int f_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if (f_descriptor < 0)
err("Failed to create socket.", 2);
success("Socket created.");
// Create Server Address.
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
server_address.sin_addr.s_addr = INADDR_ANY;
success("Address created.");
// Bind Address
int b_descriptor = bind(f_descriptor, (struct sockaddr*)&server_address,
sizeof(server_address));
if (b_descriptor < 0)
err("Failed to bind to port.", 3);
success("Port bound.");
// Listen on socket
if (listen(f_descriptor, SOMAXCONN) < 0)
err("Listening failed.", 4);
// Message Length + Maximum Port Digit Count
char s_port[18 + 5];
sprintf(s_port, "Listening on port %d", port);
success(s_port);
// Instantiate Server instance.
struct server_instance* instance = malloc(sizeof(struct server_instance));
instance->f_descriptor = f_descriptor;
instance->address = server_address;
return instance;
}
void accept_client(const struct server_instance* s_instance)
{
// Accept a client connection.
struct sockaddr_in client_address;
int c_addr_len = sizeof(client_address);
int client_f_descriptor = accept(s_instance->f_descriptor, (struct sockaddr*)&client_address,
(socklen_t*)&c_addr_len);
if (client_f_descriptor < 0)
warn("Failed to accept client");
success("Client accepted.");
// Fork here after client accepted.
pid_t pid = fork();
// Parent instance should return to accept new clients.
if (pid > 0) {
// Hand off maintenance of this client to the child proccess.
close(client_f_descriptor);
return;
}
// Child instance.
// Don't account for pid_t < 0 because of Task 3 brief.
struct client_instance* c_instance = malloc(sizeof(struct client_instance));
c_instance->c_descriptor = client_f_descriptor;
c_instance->address = client_address;
maintain_client(s_instance, c_instance);
free(c_instance);
}
void maintain_client(const struct server_instance* s_instance, struct client_instance* c_instance)
{
// Intital Communication
char* msg = "HELLO\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
char buffer[100];
// Continuously read commands from the client.
while (1) {
// Retreive command.
memset(buffer, 0, 100);
if ((read(c_instance->c_descriptor, buffer, 100)) < 0) {
warn("Failed to read from client.");
close(c_instance->c_descriptor);
break;
}
// This is 10 to account for client typos and bad commands.
char request[10];
memset(request, 0, 10);
char argument[100];
memset(argument, 0, 100);
sscanf(buffer, "%s %s", request, argument);
// Action command after splitting.
if (strlen(request) == 3 && !strncasecmp(request, "GET", 3)) {
server_get(c_instance, argument);
} else if (strlen(request) == 3 && !strncasecmp(request, "PUT", 3)) {
server_put(c_instance, argument);
} else if (strlen(request) == 3 && !strncasecmp(request, "BYE", 3)) {
close(c_instance->c_descriptor);
return;
} else {
char* msg = "SERVER 502 Command Error\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
}
}
}
void server_get(struct client_instance* c_instance, const char* argument)
{
// Check argument.
if (strlen(argument) == 0) {
char* msg = "SERVER 500 Get Error\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
return;
}
// Open file for reading.
FILE* file_descriptor;
if ((file_descriptor = fopen(argument, "r")) == NULL) {
char* msg = "SERVER 404 Not Found\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
return;
}
// On file found.
char* msg = "SEVER 200 OK\n\n";
write(c_instance->c_descriptor, msg, strlen(msg));
// Write contents of file to client.
char c[2];
memset(c, 0, 2);
while (!feof(file_descriptor)) {
c[0] = fgetc(file_descriptor);
if ((write(c_instance->c_descriptor, c, 2)) < 0) {
warn("Failed to write to socket.");
}
}
fclose(file_descriptor);
msg = "\n\n\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
}
void server_put(struct client_instance* c_instance, const char* argument)
{
// Check argument.
if (strlen(argument) == 0) {
char* msg = "SERVER 502 Command Error\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
return;
}
// Open file for writing.
FILE* file_descriptor;
if ((file_descriptor = fopen(argument, "w")) == NULL) {
char* msg = "SERVER 501 Put Error\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
return;
}
// Continuously read in chars from the client using a flag to stop when double new lines are seen.
int STOP = 0;
char buffer[1000];
memset(buffer, 0, 1000);
while (STOP != 2) {
memset(buffer, 0, 1000);
if ((read(c_instance->c_descriptor, buffer, 1000)) < 0) {
warn("Failed to read from client.");
}
for (int i = 0; i < strlen(buffer); ++i) {
fputc(buffer[i], file_descriptor);
if (buffer[i] == '\n')
STOP++;
else
STOP = 0;
}
}
// On success.
char* msg = "SEVER 201 Created\n";
if ((write(c_instance->c_descriptor, msg, strlen(msg))) < 0) {
warn("Failed to write to socket.");
}
fclose(file_descriptor);
}
void err(char* err_msg, int code)
{
printf("ERROR: %s\n", err_msg);
exit(code);
}
void warn(char* warn_msg) { printf("WARNING: %s\n", warn_msg); }
void success(char* s_msg) { printf("SUCCESS: %s\n", s_msg); }