-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
361 lines (326 loc) · 14.1 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
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
350
351
352
353
354
355
356
357
358
359
360
361
// Created by Antonis Karvelas.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include "linked_list.h"
#include "requests.h"
#include "threads.h"
int main(int argc, char** argv)
{
char *dirname, *serverIPString;
int port, workerThreads, bufferSize, serverPort;
for(int i = 0; i < argc; i++)
{
// Get directory name:
if(strcmp(argv[i], "-d") == 0)
{
i++;
dirname = malloc(strlen(argv[i]) * sizeof(char));
strcpy(dirname, argv[i]);
}
// Get port number:
if(strcmp(argv[i], "-p") == 0)
{
i++;
port = ntohs(atoi(argv[i]));
}
// Get worker threads:
if(strcmp(argv[i], "-w") == 0)
{
i++;
workerThreads = atoi(argv[i]);
}
// Get buffer size:
if(strcmp(argv[i], "-b") == 0)
{
i++;
bufferSize = atoi(argv[i]);
}
// Get server port number:
if(strcmp(argv[i], "-sp") == 0)
{
i++;
serverPort = ntohs(atoi(argv[i]));
}
// Get server IP:
if(strcmp(argv[i], "-sip") == 0)
{
i++;
serverIPString = malloc(strlen(argv[i]) * sizeof(char));
strcpy(serverIPString, argv[i]);
}
}
// Create clients linked list:
LinkedList* clientList = initializeLinkedList();
// Convert ip string to binary:
uint32_t address;
inet_pton(AF_INET, serverIPString, &address);
// Convert ip to network order:
address = ntohl(address);
// Make it a string:
char* addressString = malloc(8 * sizeof(char));
sprintf(addressString, "%x", address);
// Convert ip to network order:
// uint16_t portBinary = htons(port);
// Make it a string:
char* portString = malloc(4 * sizeof(char));
sprintf(portString, "%x", port);
// Add this client to the list:
Client* newClient = initializeClient(port, address);
Node* newNode = initializeNode(newClient);
appendToLinkedList(clientList, newNode);
Socket* clientSocket = initializeSocket(serverPort, address);
// Initialize connection:
if(connect(clientSocket->socket, (struct sockaddr*)&clientSocket->socketAddress ,sizeof(clientSocket->socketAddress)) < 0)
{
perror("Error connecting client to server.");
exit(EXIT_FAILURE);
}
// Inform server about our existence:
char logOnRequest[1024];
strcpy(logOnRequest, "LOG_ON ");
strcat(logOnRequest, addressString);
strcat(logOnRequest, " ");
strcat(logOnRequest, portString);
strcat(logOnRequest, " ");
// Ask for connected clients:
strcat(logOnRequest, "GET_CLIENTS ");
strcat(logOnRequest, addressString);
strcat(logOnRequest, " ");
strcat(logOnRequest, portString);
strcat(logOnRequest, " ");
// Send initial request for login and get clients:
send(clientSocket->socket, logOnRequest, 1024, 0);
close(clientSocket->socket);
// Setup server socket:
Socket* serverSocket = initializeSocket(port, address);
printf("client port aa: %d\n", serverSocket->socketAddress.sin_port);
char* buffer = malloc((serverSocket->socketSize+1) * sizeof(char));
if(bind(serverSocket->socket, (struct sockaddr*)&serverSocket->socketAddress, sizeof(serverSocket->socketAddress)) < 0)
{
perror("Error binding client socket.");
exit(EXIT_FAILURE);
}
// Shut-up and LISTEN:
if(listen(serverSocket->socket, 5) < 0)
{
perror("Error listening to port.");
exit(EXIT_FAILURE);
}
// Setup new socket for connection:
Socket* newSocket = malloc(sizeof(Socket));
socklen_t socklen;
char* requestString;
fd_set socketSet;
// Initialize round buffer:
roundBuffer* threadRoundBuffer = initializeRoundBuffer(bufferSize);
// Initialize threadpool:
Threadpool* threads = initializeThreadpool(workerThreads, threadRoundBuffer, address, port, clientList);
// Start accepting requests:
while(1)
{
FD_ZERO(&socketSet);
FD_SET(serverSocket->socket, &socketSet);
select(sizeof(socketSet)*8, &socketSet, NULL, NULL, NULL);
// bzero(buffer, serverSocket->socketSize);
memset(buffer, 0, sizeof(char)*serverSocket->socketSize);
// Accept client connection:
if((newSocket->socket = accept(serverSocket->socket, (struct sockaddr*)&newSocket->socketAddress, &socklen)) < 0)
{
perror("Error accepting client connection.");
exit(EXIT_FAILURE);
}
// Receive request:
if((recv(newSocket->socket, buffer, serverSocket->socketSize, 0)) < 0)
{
perror("Error in recvfrom.");
exit(EXIT_FAILURE);
}
printf("Client buffer: Dir:%s\t\t\t%s\n", dirname, buffer);
// Get clients list from server:
char* bufferCopy = buffer;
while((requestString = strtok(bufferCopy, " ")) != NULL)
{
// Client list information:
if(strcmp(requestString, "CLIENT_LIST") == 0)
{
printf("CLIENT_LIST response received.\n");
char* numString = strtok(NULL, " ");
int numOfClients = atoi(numString);
for(int i = 0; i < numOfClients; i++)
{
// Get strings of binary represantation:
char* IPString = strtok(NULL, " ");
char* PortString = strtok(NULL, " ");
// Convert them to 32bit and 16bit values:
uint32_t clientIP = strtol(IPString, NULL, 16);
// clientIP = ntohl(clientIP);
uint16_t clientPort = strtol(PortString, NULL, 16);
// clientPort = ntohs(clientPort);
// Search for client in clients list:
int found = checkClientInLinkedList(clientList, clientPort, clientIP);
// If it doesn't exist, add client to list:
if(found == 1)
{
printf("------Adding to rb: %d %d\n", htons(clientPort), htons(port));
Client* newClient = initializeClient(clientPort, clientIP);
Node* newNode = initializeNode(newClient);
appendToLinkedList(clientList, newNode);
Item* newItem = initializeItem(NULL, 0, clientIP, clientPort);
pthread_mutex_lock(&(threads->mutexLock));
printf("\n Gained lock to add item.\n");
addToRoundBuffer(threadRoundBuffer, newItem, threads);
pthread_mutex_unlock(&(threads->mutexLock));
pthread_cond_broadcast(&(threads->mutexCond));
}
}
}
// Request to return list with pathnames:
else if(strcmp(requestString, "GET_FILE_LIST") == 0)
{
char* IPString = strtok(NULL, " ");
char* PortString = strtok(NULL, " ");
uint32_t partnerIP = strtol(IPString, NULL, 16);
uint16_t partnerPort = strtol(PortString, NULL, 16);
// partnerPort = ntohs(partnerPort);
Socket* sendFileListSocket = initializeSocket(partnerPort, partnerIP);
sendFilesList(sendFileListSocket, dirname, serverSocket->socketSize, address, port);
close(sendFileListSocket->socket);
}
// Get files and add them to the round buffer:
else if(strcmp(requestString, "FILE_LIST") == 0)
{
char* IPString = strtok(NULL, " ");
char* PortString = strtok(NULL, " ");
uint32_t partnerIP = strtol(IPString, NULL, 16);
uint16_t partnerPort = strtol(PortString, NULL, 16);
// partnerPort = ntohs(partnerPort);
// partnerIP = ntohl(partnerIP);
char* numString = strtok(NULL, " ");
int numOfFiles = atoi(numString);
for(int i = 0; i < numOfFiles; i++)
{
char* filename = strtok(NULL, " ");
char* versionString = strtok(NULL, " ");
int version = atoi(versionString);
Item* newItem = initializeItem(filename, version, partnerIP, partnerPort);
pthread_mutex_lock(&(threads->mutexLock));
printf("\n Gained lock to add item.\n");
addToRoundBuffer(threadRoundBuffer, newItem, threads);
pthread_mutex_unlock(&(threads->mutexLock));
pthread_cond_broadcast(&(threads->mutexCond));
}
}
// Request to return specific file:
else if(strcmp(requestString, "GET_FILE") == 0)
{
char* IPString = strtok(NULL, " ");
char* PortString = strtok(NULL, " ");
char* fileToGet = strtok(NULL, " ");
uint32_t partnerIP = strtol(IPString, NULL, 16);
uint16_t partnerPort = strtol(PortString, NULL, 16);
// partnerPort = ntohs(partnerPort);
char* completeDir = malloc(strlen(dirname) + strlen(fileToGet) + 1);
strcpy(completeDir, dirname);
strcat(completeDir, "/");
strcat(completeDir, fileToGet);
Socket* sendFileSocket = initializeSocket(partnerPort, partnerIP);
sendFile(sendFileSocket, completeDir, dirname, serverSocket->socketSize, address, port);
close(sendFileSocket->socket);
free(completeDir);
}
// Get file data and create file:
else if(strcmp(requestString, "FILE") == 0)
{
// char* IPString = strtok(NULL, " ");
// char* PortString = strtok(NULL, " ");
char* fileToGet = strtok(NULL, " ");
printf("````````FILE TO GET:%s\n", fileToGet);
char* baseDir = strtok(NULL, " ");
char* fileSizeString = strtok(NULL, " ");
// uint32_t partnerIP = strtol(IPString, NULL, 16);
// uint16_t partnerPort = strtol(PortString, NULL, 16);
long fileSize = strtol(fileSizeString, NULL, 10);
createDirectory(dirname, fileToGet+(strlen(baseDir)+1));
int shift = 22 + strlen(fileToGet) + strlen(baseDir) + strlen(fileSizeString);
// Open file with write permissions, create it if it doesn't exist, empty it if it already exists.
// Allow the user to read and write that file.
char* newFile = malloc(strlen(dirname)+strlen(fileToGet));
strcpy(newFile, dirname);
strcat(newFile, "/");
strcat(newFile, fileToGet+(strlen(baseDir)+1));
printf("````````NEW FILENAME:%s %s %s\n", newFile, fileToGet, fileSizeString);
int file = open(newFile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
char* buffer2 = buffer + shift;
while(fileSize >= 0)
{
// printf("SHIFT:%d %d %d %d\n", shift, strlen(fileToGet), strlen(baseDir), strlen(fileSizeString));
long bufferLen = serverSocket->socketSize - shift;
if(fileSize > bufferLen)
{
if ((write(file, buffer2, bufferLen)) == -1)
{
perror ("Error writing to file.");
return 1;
}
// Receive request:
if((recv(newSocket->socket, buffer, serverSocket->socketSize, 0)) < 0)
{
perror("Error in recvfrom.");
exit(EXIT_FAILURE);
}
}
else
{
if ((write(file, buffer2, fileSize)) == -1)
{
perror ("Error writing to file.");
return 1;
}
}
fileSize -= bufferLen;
shift = 0;
buffer2 = buffer;
}
close(file);
}
else if(strcmp(requestString, "USER_ON") == 0)
{
char* IPString = strtok(NULL, " ");
char* PortString = strtok(NULL, " ");
// Convert them to 32bit and 16bit values:
uint32_t clientIP = strtol(IPString, NULL, 16);
// clientIP = ntohl(clientIP);
uint16_t clientPort = strtol(PortString, NULL, 16);
// clientPort = ntohs(clientPort);
// Search for client in clients list:
int found = checkClientInLinkedList(clientList, clientPort, clientIP);
// If it doesn't exist, add client to list:
if(found == 1)
{
// printf("------Adding to rb: %d %d\n", htons(clientPort), htons(port));
Client* newClient = initializeClient(clientPort, clientIP);
Node* newNode = initializeNode(newClient);
appendToLinkedList(clientList, newNode);
Item* newItem = initializeItem(NULL, 0, clientIP, clientPort);
pthread_mutex_lock(&(threads->mutexLock));
printf("\n Gained lock to add item.\n");
addToRoundBuffer(threadRoundBuffer, newItem, threads);
pthread_mutex_unlock(&(threads->mutexLock));
pthread_cond_broadcast(&(threads->mutexCond));
}
}
close(newSocket->socket);
bufferCopy = NULL;
}
}
return 0;
}