Skip to content

Commit 4d74162

Browse files
committed
L: added DOS_PROTECTION
missing argv params W: not tested
1 parent cac9c5d commit 4d74162

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

general/include/definitions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#define CONNECTION_QUEUE 5000
2020
#define MAX_CONNECTIONS_ALLOWED 1
2121
#define BUFFER_SIZE 2048
22-
22+
// DOS_PROTECTION is a switcher used in socket.c, when it is true we close the connection immediately without read the whole message, else we read the entire message and we response with error.
23+
int DOS_PROTECTION;
2324
#define SEND_BUFFER_SIZE 512
2425
char cwd[BUFFER_SIZE];
2526
#if defined(__unix__) || defined(__APPLE__)

general/include/socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ FILE *sendFileToClient(int fd);
1717

1818
int fsize(FILE *fp);
1919

20-
void socket_read_request(struct ThreadArgs *args, char **buf);
20+
int socket_read_request(struct ThreadArgs *args, char **buf);
2121

2222
void socket_resolve_selector(struct ThreadArgs *args, char *buf, char **path);
2323

general/lib/socket/socket.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <windows_pipe.h>
1414
#include <windows_utils.h>
1515

16+
1617
#endif
1718

1819
#if defined(__unix__) || defined(__APPLE__)
@@ -34,6 +35,7 @@
3435
#include "socket.h"
3536
#include "utils.h"
3637
#include "files_interaction.h"
38+
#include "definitions.h"
3739

3840

3941
int write_to_log(struct PipeArgs *data);
@@ -180,7 +182,7 @@ void socket_resolve_selector(struct ThreadArgs *args, char *buf, char **path) {
180182
* */
181183

182184

183-
void socket_read_request(struct ThreadArgs *args, char **buf) {
185+
int socket_read_request(struct ThreadArgs *args, char **buf) {
184186

185187
int ptr = 0;
186188
ssize_t got_bytes = 0;
@@ -200,7 +202,7 @@ void socket_read_request(struct ThreadArgs *args, char **buf) {
200202
clean_request(NULL, *buf, args);
201203
}
202204

203-
if (0 == ptr && 0 == got_bytes) {
205+
if (0 == ptr && 0 == got_bytes) { // empty body case
204206
clean_request(NULL, *buf, args);
205207
}
206208

@@ -210,10 +212,13 @@ void socket_read_request(struct ThreadArgs *args, char **buf) {
210212
}
211213
ptr += got_bytes;
212214
if (ptr >= BUFFER_SIZE) {
213-
// clean_request(NULL, *buf, args);
214215
printf("%s %d\n", "effettuo il drain", ptr);
216+
217+
// DOS_PROTECTION is a switcher used in socket.c, when it is true we close the connection immediately without read the whole message, else we read the entire message and we response with error.
218+
if (DOS_PROTECTION) clean_request(NULL, *buf, args);
215219
socket_drain_tcp(args->fd);
216-
break;
220+
return -2;
221+
//socket_send_error_to_client();
217222
}
218223
if (ut_get_line(*buf, ptr)) {
219224
break;

linux/lib/socket/linux_socket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ void *handle_request(void *params) {
301301
printf("%s\n", "------- new handle request ---------------");
302302
// printf("args: %d\n", args->fd);
303303

304-
socket_read_request(args, &buf); // fill the buffer with the request
304+
int ret = socket_read_request(args, &buf); // fill the buffer with the request
305305

306306
socket_resolve_selector(args, buf, &path); // parse the request
307-
307+
if (ret == -2) socket_send_error_to_client(path, buf, args);
308308
printf("going to socket_manage_files - full required path: %s \n", path);
309309
socket_manage_files(path, buf, args); // send response
310310
printf("going to clean_request exiting handle_request\n");

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void run_in_daemon() {
8484

8585

8686
int main(int argc, char *argv[]) {
87-
87+
DOS_PROTECTION = 0;
8888
printf("%s\n", "Gopher start ...");
8989
printf("PID: %d\n", getpid());
9090

0 commit comments

Comments
 (0)