-
Notifications
You must be signed in to change notification settings - Fork 0
/
KVS-lib.c
421 lines (381 loc) · 10.9 KB
/
KVS-lib.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "KVS-lib.h"
#include "KVS-libmod.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/un.h>
#include <pthread.h>
int establish_connection(char *group_id, char *secret)
{
int flag = 0;
struct sockaddr_un server_socket_addr, socket_addr_client;
if (sizeof(group_id) > 512 || sizeof(secret) > 512)
{
printf("Size of group and secret must be below 512 bytes.\n");
return -5;
}
if (group_id == NULL || secret == NULL)
return -500;
pid_t pid = getpid();
snprintf(client_addr, 1000, "/tmp/client_socket_%d", pid);
printf("%s\n", client_addr);
socket_addr_client.sun_family = AF_UNIX;
strcpy(socket_addr_client.sun_path, client_addr);
send_socket = socket(AF_UNIX, SOCK_STREAM, 0);
if (send_socket == -1)
{
exit(-1);
}
if (bind(send_socket, (struct sockaddr *)&socket_addr_client, sizeof(socket_addr_client)) == -1)
{
printf("Socket for client %d already created\n", pid);
exit(-1);
}
strcpy(server_socket_addr.sun_path, SERVER_SOCKET_ADDR);
server_socket_addr.sun_family = AF_UNIX;
int err_c = connect(send_socket, (struct sockaddr *)&server_socket_addr, sizeof(server_socket_addr));
if (err_c == -1)
{
perror("connect");
remove(client_addr);
exit(-1);
}
char command[5] = "EST_";
int size_group = strlen(group_id) + 1;
int size_secret = strlen(secret) + 1;
write(send_socket, &command, sizeof(command));
write(send_socket, &size_group, sizeof(size_group));
write(send_socket, group_id, size_group);
write(send_socket, &size_secret, sizeof(size_secret));
write(send_socket, secret, size_secret);
int err_rcv = recv(send_socket, &flag, sizeof(int), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
//switch case para os erros
if (flag == 1)
{
hooked = 1;
printf("You established connection with the group %s\n", group_id);
return flag;
}
else if (flag == -4)
{
printf("Establish failed with the group %s,so you will remain connected to your previous group\n", group_id);
return flag;
}
else if (flag == -7)
{
printf("Establish failed with the group %s, group is deleted. Not connected to any group\n", group_id);
remove(client_addr);
return flag;
}
else
{
remove(client_addr);
printf("Establish failed: Not connected to any group\n");
return flag;
}
}
int put_value(char *key, char *value)
{
int flag = 0;
if (sizeof(key) < 0) //ainda a definir
{
printf("Size of group and secret must be below 512 bytes.\n");
return -11;
}
if (key == NULL || value == NULL)
return -500;
if (hooked == 1) //verificar que a socket esta ligada ao server
{
char command[5] = "PUT_";
int size_key = strlen(key) + 1;
int size_value = strlen(value) + 1;
write(send_socket, &command, sizeof(command));
write(send_socket, &size_key, sizeof(size_key));
write(send_socket, key, size_key);
write(send_socket, &size_value, sizeof(size_value));
write(send_socket, value, size_value);
int err_rcv = recv(send_socket, &flag, sizeof(int), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
//switch case para os erros
if (flag == 1)
{
printf("The Key/Value has been placed\n");
return flag;
}
else if (flag == -3)
{
printf("Put failed\n");
return flag;
}
else
{
printf("Put: Unknown flag\n");
return -6;
}
}
else
{
printf("You are not connected to any group.\n");
return -1;
}
}
int get_value(char *key, char **value)
{
int flag = 0, size_buffer = 0;
if (sizeof(key) < 0) //ainda a definir
{
printf("Size of group and secret must be below 512 bytes.\n");
return -11;
}
if (key == NULL || value == NULL)
return -500;
if (hooked == 1) //verificar que a socket esta ligada ao server
{
char command[5] = "GET_";
int size_key = strlen(key) + 1;
write(send_socket, &command, sizeof(command));
write(send_socket, &size_key, sizeof(size_key));
write(send_socket, key, size_key);
int err_rcv = recv(send_socket, &flag, sizeof(int), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
if (flag == 1)
{
err_rcv = recv(send_socket, &size_buffer, sizeof(size_buffer), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
char buffer[size_buffer];
err_rcv = recv(send_socket, buffer, size_buffer, 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
printf("Value has been fetched\n");
*value = (char *)malloc((size_buffer) * sizeof(char));
strcpy(*value, buffer);
return flag;
}
else if (flag == -2)
{
printf("GET | Key not found\n");
return flag;
}
else
{
printf("Get: Unknown flag\n");
return -6;
}
}
else
{
printf("You are not connected to any group.\n");
return -1;
}
}
int delete_value(char *key)
{
int flag = 0, size_buffer;
char *buffer;
if (sizeof(key) < 0) //ainda a definir
{
printf("Size of group and secret must be below 512 bytes.\n");
return -11;
}
if (key == NULL)
return -500;
if (hooked == 1) //verificar que a socket esta ligada ao server
{
char command[5] = "DEL_";
int size_key = strlen(key) + 1;
write(send_socket, &command, sizeof(command));
write(send_socket, &size_key, sizeof(size_key));
write(send_socket, key, size_key);
int err_rcv = recv(send_socket, &flag, sizeof(int), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
if (flag == 1)
{
printf("Key entry %s has been deleted\n", key);
return flag;
}
else if (flag == -2)
{
printf("DEL | Key not found\n");
return flag;
}
else if (flag == -3)
{
printf("Delete failed\n");
return flag;
}
else
{
printf("Del: Unknown flag\n");
return -6;
}
}
else
{
printf("You are not connected to any group.\n");
return -1;
}
}
int register_callback(char *key, void (*callback_function)(char *))
{
int flag = 0, err_rcv, trigger;
char *buffer;
int plug;
char *plug_addr;
struct sockaddr_un callback_addr_server, callback_addr_client;
if (key == NULL)
return -500;
if (hooked == 1) //verificar que a socket esta ligada ao server
{
callback_addr_client.sun_family = AF_UNIX;
plug_addr = (char *)malloc((strlen(client_addr) + strlen(key) + 1) * sizeof(char));
strcpy(plug_addr, client_addr);
strcat(plug_addr, key);
strcpy(callback_addr_client.sun_path, plug_addr);
free(plug_addr);
plug = socket(AF_UNIX, SOCK_STREAM, 0);
if (plug == -1)
{
exit(-1);
}
if (bind(plug, (struct sockaddr *)&callback_addr_client, sizeof(callback_addr_client)) == -1)
{
printf("CallbackSocket for key %s already created\n", key);
return -69;
}
strcpy(callback_addr_server.sun_path, CALLBACK_SOCKET_ADDR);
callback_addr_server.sun_family = AF_UNIX;
if (fork() != 0)
{
int err_c = connect(plug, (struct sockaddr *)&callback_addr_server, sizeof(callback_addr_server));
if (err_c == -1)
{
perror("connect");
exit(-1);
}
while (1)
{
err_rcv = recv(plug, &trigger, sizeof(trigger), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
else if (err_rcv == 0)
{
printf("Closing %s callback pipe (Local went down)\n", key);
remove(callback_addr_client.sun_path);
exit(EXIT_SUCCESS);
}
if (trigger == 100)
{
callback_function(key);
}
else if (trigger == -5)
{
printf("Closing %s callback pipe\n", key);
remove(callback_addr_client.sun_path);
exit(EXIT_SUCCESS);
}
}
}
else
{
char command[5] = "RCL_";
int size_key = strlen(key) + 1;
write(send_socket, &command, sizeof(command));
write(send_socket, &size_key, sizeof(size_key));
write(send_socket, key, size_key);
err_rcv = recv(send_socket, &flag, sizeof(int), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
if (flag == 1)
{
printf("Callback function for %s has been mounted\n", key);
return flag;
}
else if (flag == -2)
{
printf("RCL | Key not found\n");
return flag;
}
else if (flag == -10)
{
printf("Couldn´t save the callback socket\n");
return flag;
}
else
{
printf("Rcl: Unknown flag\n");
return -6;
}
}
}
else
{
printf("You are not connected to any group.\n");
return -1;
}
}
int close_connection()
{
int flag = 0;
if (hooked == 1) //verificar que a socket esta ligada ao server
{
char command[5] = "CLS_";
write(send_socket, &command, sizeof(command));
int err_rcv = recv(send_socket, &flag, sizeof(int), 0);
if (err_rcv == -1)
{
perror("recieve");
exit(-1);
}
if (flag == 1)
{
printf("Connection closed\n");
hooked = 0;
remove(client_addr);
return flag;
}
else
{
printf("Close failed\n");
return -6;
}
}
else
{
printf("You are not connected to any group.\n");
return -1;
}
}