This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_note_server.c
349 lines (316 loc) · 11.1 KB
/
simple_note_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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
// Saves the state of a connected client
struct client_state {
void (*state_fn)(struct client_state *state);
int fd;
char *note_ptr;
char recv_buffer[200];
unsigned int recv_buffer_size;
};
// Helper function to send a message to the client
void send_message(struct client_state *state, char *message) {
write(state->fd, message, strlen(message));
}
// Helper function to receive client input.
// Guarantees that recv_buffer is null-terminated if recv_buffer_size > 0.
// Sets recv_buffer_size = 0 on error.
void receive_message(struct client_state *state) {
// Remove any old message (till the terminating null-character) from the recv_buffer
if(state->recv_buffer_size > 0) {
size_t msg_size = strlen(state->recv_buffer) + 1;
if(state->recv_buffer_size > msg_size) {
memmove(state->recv_buffer, &state->recv_buffer[msg_size], state->recv_buffer_size - msg_size);
state->recv_buffer_size -= msg_size;
} else {
state->recv_buffer_size = 0;
}
}
// Try to receive something while skipping single newlines
char *newline = NULL;
while(1) {
// Send the prompt
send_message(state, "> ");
// Read until newline
while(1) {
// Parse any data that's in the buffer
newline = memchr(state->recv_buffer, '\n', state->recv_buffer_size);
if(newline == NULL) {
// Nothing in the buffer, read more data from socket
size_t buffer_space = sizeof(state->recv_buffer) - state->recv_buffer_size;
if(buffer_space == 0) {
// Read 200 bytes without newline. Sth's wrong. Aborting.
state->recv_buffer_size = 0;
return;
}
ssize_t recv_size = read(state->fd, &state->recv_buffer[state->recv_buffer_size], buffer_space);
if(recv_size > 0) {
state->recv_buffer_size += recv_size;
} else {
// Broken pipe
state->recv_buffer_size = 0;
return;
}
continue;
}
break;
}
// Remove single newlines
if(newline == state->recv_buffer) {
if(state->recv_buffer_size > 1) {
memmove(state->recv_buffer, &state->recv_buffer[1], state->recv_buffer_size - 1);
}
state->recv_buffer_size -= 1;
continue;
}
break;
}
// Got something, replace newline with terminating null-character and return
*newline = '\0';
}
// Checks if an entered password matches.
// Password is stored with high security Caesar encryption
// over the range of printable non whitespace ascii chars.
// int check_password(char *input) {
// const char *password = "vX|{9]m-<[|"; //N0TSo5Ecr3T
// const int pwlen = strlen(password);
// if(strlen(input) != pwlen) {
// return 0;
// }
// for(int i = 0; i < pwlen; ++i) {
// if((((input[i]-33+40)%94)+33) != password[i]) {
// return 0;
// }
// }
// return 1;
// }
// Checks if the password is "FUZZ"
// int check_password(char *input) {
// if(strlen(input) != 4) {
// return 0;
// }
// if(input[0] == 'F' && input[1] == 'U' && input[2] == 'Z' && input[3] == 'Z') {
// return 1;
// }
// }
// Accepts all passwords
int check_password(char *input) {
return 1;
}
//=========================
// State machine functions
//=========================
void state_ask_name(struct client_state *state);
void state_main_menu(struct client_state *state);
void state_set_note(struct client_state *state);
void state_get_note(struct client_state *state);
void state_secret_pass(struct client_state *state);
void state_secret_menu(struct client_state *state);
void state_delete_note(struct client_state *state);
void state_ask_name(struct client_state *state) {
// Ask for a name
send_message(state, "Please enter your name:\n");
receive_message(state);
if(state->recv_buffer_size == 0) {
state->state_fn = NULL;
return;
}
// Send a welcome message
char msg[sizeof("Welcome !\n") + strlen(state->recv_buffer) + 1];
strcpy(msg, "Welcome ");
strcat(msg, state->recv_buffer);
strcat(msg, "!\n");
send_message(state, msg);
// Go to next state
state->state_fn = state_main_menu;
}
void state_main_menu(struct client_state *state) {
// Ask what to do
send_message(state, "\n[MAIN MENU]\n");
send_message(state, "setnote: Save a note in the system\n");
send_message(state, "getnote: Print the saved note\n");
send_message(state, "secret: Access the super secret special menu\n");
send_message(state, "quit: Exit the system\n");
receive_message(state);
if(state->recv_buffer_size == 0 || strcmp(state->recv_buffer, "quit") == 0) {
state->state_fn = NULL;
} else if(strcmp(state->recv_buffer, "setnote") == 0) {
state->state_fn = state_set_note;
} else if(strcmp(state->recv_buffer, "getnote") == 0) {
state->state_fn = state_get_note;
} else if(strcmp(state->recv_buffer, "secret") == 0) {
state->state_fn = state_secret_pass;
} else {
send_message(state, "Uhmm, what?\n");
}
}
void state_set_note(struct client_state *state) {
// Ask for a note to save
send_message(state, "\nPlease enter your note:\n");
receive_message(state);
if(state->recv_buffer_size == 0) {
state->state_fn = NULL;
return;
}
// Save it in our client state
if(state->note_ptr != NULL) {
free(state->note_ptr);
}
state->note_ptr = (char *) malloc(strlen(state->recv_buffer) + 1);
strcpy(state->note_ptr, state->recv_buffer);
send_message(state, "Note saved!\n");
state->state_fn = state_main_menu;
}
void state_get_note(struct client_state *state) {
// Print the saved note if it exists
if(state->note_ptr != NULL) {
send_message(state, "\nHere is your saved note:\n");
send_message(state, state->note_ptr);
send_message(state, "\n");
} else {
send_message(state, "\nNo note saved yet...\n");
}
state->state_fn = state_main_menu;
}
void state_secret_pass(struct client_state *state) {
// Ask for the secret menu password
send_message(state, "\nPlease enter the password for the secret menu:\n");
receive_message(state);
if(state->recv_buffer_size == 0) {
state->state_fn = NULL;
return;
}
if(check_password(state->recv_buffer)) {
send_message(state, "Correct!\n");
state->state_fn = state_secret_menu;
} else {
send_message(state, "Nope!\n");
state->state_fn = state_main_menu;
}
}
void state_secret_menu(struct client_state *state) {
// Ask what to do
send_message(state, "\n[SECRET MENU]\n");
send_message(state, "delnote: Delete a saved note\n");
send_message(state, "back: Go back to main menu\n");
receive_message(state);
if(state->recv_buffer_size == 0) {
state->state_fn = NULL;
} else if(strcmp(state->recv_buffer, "delnote") == 0) {
state->state_fn = state_delete_note;
} else if(strcmp(state->recv_buffer, "back") == 0) {
state->state_fn = state_main_menu;
} else {
send_message(state, "Nonononono!\n");
}
}
void state_delete_note(struct client_state *state) {
if(state->note_ptr != NULL) {
// BUG: we casually forget here to set note_ptr = NULL,
// provoking a use-after-free or double-free later
free(state->note_ptr);
send_message(state, "It's gone!\n");
state->state_fn = state_secret_menu;
// // Ask for confirmation
// send_message(state, "\nReally delete? (y/n)\n");
// receive_message(state);
// if(state->recv_buffer_size == 0) {
// state->state_fn = NULL;
// return;
// } else if(strcmp(state->recv_buffer, "y") == 0) {
// // BUG: we casually forget here to set note_ptr = NULL,
// // provoking a use-after-free or double-free later
// free(state->note_ptr);
// send_message(state, "It's gone!\n");
// state->state_fn = state_secret_menu;
// } else {
// state->state_fn = state_secret_menu;
// }
} else {
send_message(state, "\nThere is no note saved currently...\n");
state->state_fn = state_secret_menu;
}
}
//=========================
// State machine end
//=========================
// Setup client state and loop over our simple state machine
void handle_client(int client_fd) {
struct client_state cstate = {
.state_fn = state_ask_name,
.fd = client_fd,
.note_ptr = NULL,
.recv_buffer_size = 0
};
while(cstate.state_fn != NULL) {
cstate.state_fn(&cstate);
}
}
// Open a TCP socket on port 5000 and listen for connections.
// Client connections are then handled by handle_client().
int main(int argc, char *argv[]) {
// Prevent SIGPIPE crashing the server when a client looses connection
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
sigaction(SIGPIPE, &sa, NULL);
//signal(SIGPIPE, SIG_IGN);
// Open socket
int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == -1) {
perror("Failed to open socket");
return 1;
}
// Set server address
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(5000);
// Set SO_REUSEADDR, useful for locally testing a crashing application
int flag = 1;
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0) {
perror("Failed to set option SO_REUSEADDR");
}
// Bind to address
if(bind(socket_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
perror("Failed to bind to address");
return 1;
}
// Start listening on socket
if(listen(socket_fd, 1) < 0) {
perror("Failed to start listening on socket");
return 1;
}
// Accept clients in a loop
while(1) {
// Accept an incoming connection
#ifndef LOG_SILENT
printf("Waiting for connections...\n");
#endif
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
if(client_fd < 0) {
perror("Failed to accept connection");
continue;
}
// Do something with it
#ifndef LOG_SILENT
printf("Client connected!\n");
#endif
handle_client(client_fd);
// Close connection
#ifndef LOG_SILENT
printf("Client disconnected!\n");
#endif
if(close(client_fd) < 0) {
perror("Failed to close client connection");
}
}
return 0;
}