-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
204 lines (177 loc) · 5.62 KB
/
client.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include "config.h"
#include "client.h"
#include "util.h"
struct client_t {
int sock;
char ip[INET6_ADDRSTRLEN];
time_t start_time;
size_t bytes_transferred;
char *file_path;
FILE *file;
char buffer[BUFFER_SIZE];
size_t buffer_pos;
size_t buffer_size;
};
static client_t *clients[MAX_CONNECTIONS] = {NULL};
static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
static void apply_random_delay(void) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
srand(ts.tv_nsec);
usleep(MIN_DELAY + rand() % (MAX_DELAY - MIN_DELAY + 1));
}
static void close_client(io_handler_t *io_handler, client_t *client) {
if (!client) return;
remove_from_io_handler(io_handler, client->sock);
close(client->sock);
if (client->file) {
fclose(client->file);
}
time_t end_time = time(NULL);
log_connection(client->ip, client->start_time, end_time,
client->bytes_transferred, 1, client->file_path);
free(client->file_path);
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (clients[i] == client) {
clients[i] = NULL;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
free(client);
}
int handle_new_connection(io_handler_t *io_handler, int server_sock) {
struct sockaddr_storage client_addr;
socklen_t addr_size = sizeof(client_addr);
int client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &addr_size);
if (client_sock == -1) {
perror("accept failed");
return -1;
}
if (fcntl(client_sock, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl failed");
close(client_sock);
return -1;
}
client_t *client = calloc(1, sizeof(client_t));
if (!client) {
perror("calloc failed");
close(client_sock);
return -1;
}
client->sock = client_sock;
client->start_time = time(NULL);
get_ip_str((struct sockaddr *)&client_addr, client->ip, sizeof(client->ip));
client->file_path = get_random_file();
if (client->file_path) {
client->file = fopen(client->file_path, "r");
if (!client->file) {
perror("fopen failed");
free(client->file_path);
free(client);
close(client_sock);
return -1;
}
} else {
const char *error_message = "No files available. Please add some text files.\n";
strncpy(client->buffer, error_message, sizeof(client->buffer));
client->buffer_size = strlen(error_message);
}
pthread_mutex_lock(&clients_mutex);
int slot = -1;
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (clients[i] == NULL) {
clients[i] = client;
slot = i;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
if (slot == -1) {
fprintf(stderr, "Max connections reached\n");
if (client->file) fclose(client->file);
free(client->file_path);
free(client);
close(client_sock);
return -1;
}
if (!add_to_io_handler(io_handler, client_sock, EVENT_READ | EVENT_WRITE)) {
close_client(io_handler, client);
return -1;
}
return 0;
}
void handle_client_event(io_handler_t *io_handler, int client_sock, int events) {
client_t *client = NULL;
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (clients[i] && clients[i]->sock == client_sock) {
client = clients[i];
break;
}
}
pthread_mutex_unlock(&clients_mutex);
if (!client) return;
if (events & EVENT_READ) {
char buf[1];
ssize_t recv_result = recv(client_sock, buf, 1, MSG_PEEK);
if (recv_result <= 0) {
if (recv_result == 0 || (recv_result == -1 && errno != EAGAIN && errno != EWOULDBLOCK)) {
close_client(io_handler, client);
return;
}
}
}
if (events & EVENT_WRITE) {
if (client->buffer_pos < client->buffer_size) {
ssize_t sent = send(client_sock, client->buffer + client->buffer_pos, 1, MSG_NOSIGNAL);
if (sent > 0) {
client->buffer_pos += sent;
client->bytes_transferred += sent;
apply_random_delay();
} else if (sent == -1 && (errno != EAGAIN && errno != EWOULDBLOCK)) {
close_client(io_handler, client);
return;
}
} else if (client->file) {
size_t bytes_read = fread(client->buffer, 1, sizeof(client->buffer), client->file);
if (bytes_read > 0) {
client->buffer_size = bytes_read;
client->buffer_pos = 0;
} else {
if (feof(client->file)) {
close_client(io_handler, client);
} else {
perror("fread failed");
close_client(io_handler, client);
}
return;
}
} else {
close_client(io_handler, client);
return;
}
}
}
void cleanup_clients(void) {
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (clients[i]) {
close_client(NULL, clients[i]);
clients[i] = NULL;
}
}
pthread_mutex_unlock(&clients_mutex);
}