-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.c
196 lines (147 loc) · 5.3 KB
/
serializer.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
#include <string.h>
#include <unistd.h>
#include "global.h"
#include "serializer.h"
#include <stdio.h>
// Criação de sockets
static int getBindedSocket(int socket_type, in_port_t port, struct in_addr addr){
int fd;
struct sockaddr_in socket_addr;
// Abre socket TCP
fd = socket(AF_INET, socket_type, 0);
if (fd == -1) return ERROR;
// Define propriedades da socket
socket_addr.sin_family = AF_INET;
socket_addr.sin_port = port;
socket_addr.sin_addr = addr;
if (bind(fd, (struct sockaddr *) &socket_addr, sizeof(socket_addr)) == ERROR) return ERROR;
// Faz bind e listen à socket criada
return fd;
}
int getBindedUDPSocket(in_port_t port, struct in_addr addr){
return getBindedSocket(SOCK_DGRAM, port, addr);
}
int getBindedTCPSocket(in_port_t port, struct in_addr addr){
int fd;
if ((fd = getBindedSocket(SOCK_STREAM, port, addr)) == ERROR) return ERROR;
if (listen(fd, MAX_QUEUED_CONNECTIONS) == ERROR) return ERROR;
return fd;
}
// Funções de leitura personalizadas
int TCPRead(int fd, void * buf, size_t nbytes, int timeout){
fd_set rfds;
struct timeval timer;
int read_size;
int offset = 0;
do {
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
memset(&timer, 0, sizeof(struct timeval));
timer.tv_sec = timeout;
select(fd + 1, &rfds, NULL, NULL, timeout < 0 ? NULL : &timer);
if (!FD_ISSET(fd, &rfds)) return ERROR; // Timeout ou interrupção
read_size = read(fd, buf + offset, nbytes - offset);
if(read_size <= 0) return ERROR; // A ligação foi termianda ou ocorreu um erro ao ler
offset += read_size;
} while(offset != nbytes);
return TRUE;
}
int UDPRead(int fd, void * buf, size_t nbytes, int timeout){
fd_set rfds;
struct timeval timer;
int n_tries = 0;
int check;
do {
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
memset(&timer, 0, sizeof(struct timeval));
timer.tv_sec = timeout;
select(fd + 1, &rfds, NULL, NULL, &timer);
if (FD_ISSET(fd, &rfds)){
check = recv(fd, buf, nbytes, 0);
if (check == -1) return ERROR;
if (check == nbytes){
// Foi recebido a quantidade de bytes prevista
return TRUE;
} else {
return FALSE;
}
}
n_tries ++;
} while(n_tries < UDP_RETRY_ATTEMPS);
return ERROR;
}
// ---------------- PEER / GATEWAY messages ----------------
// Peer pretende registar-se na gateway (Peer -> Gateway)
// Gateway fornece um peer ao client (Gateway -> Client)
int serializePeerInfo(int fd, struct sockaddr_in destination, int peer_id, in_port_t peer_port, struct in_addr peer_addr){
char * buffer;
int msg_id, offset;
offset = 0;
msg_id = MSG_GATEWAY_PEER_INFO;
buffer = (char *) malloc(2*sizeof(int) + sizeof(in_port_t) + sizeof(struct in_addr));
memcpy(buffer + offset, &msg_id, sizeof(int));
offset += sizeof(int);
memcpy(buffer + offset, &peer_id, sizeof(int));
offset += sizeof(int);
memcpy(buffer + offset, &peer_port, sizeof(in_port_t));
offset += sizeof(in_port_t);
memcpy(buffer + offset, &peer_addr, sizeof(struct in_addr));
offset += sizeof(struct in_addr);
return sendto(fd, buffer, offset, 0, (struct sockaddr *) &destination, sizeof(destination));
}
int deserializePeerInfo(char * buffer, int * peer_id, in_port_t * peer_port, struct in_addr * peer_addr){
int msg_id, offset;
memcpy(&msg_id, buffer, sizeof(int));
if (msg_id != MSG_GATEWAY_PEER_INFO) {free(buffer); return ERROR;}
// Lança erro se a mensagem não tinha o id previsto
offset = sizeof(int);
memcpy(peer_id, buffer + offset, sizeof(int));
offset += sizeof(int);
memcpy(peer_port, buffer + offset, sizeof(in_port_t));
offset += sizeof(in_port_t);
memcpy(peer_addr, buffer + offset, sizeof(struct in_addr));
return TRUE;
}
int serializePeerDeath(int fd, struct sockaddr_in destination, int peer_id){
int msg_id = MSG_GATEWAY_PEER_DEATH;
char * buffer = malloc(2*sizeof(int));
memcpy(buffer, &msg_id, sizeof(int));
memcpy(buffer + sizeof(int), &peer_id, sizeof(int));
return sendto(fd, buffer, 2*sizeof(int), 0, (struct sockaddr *) &destination, sizeof(destination));
}
int deserializePeerDeath(char * buffer, int * peer_id){
int msg_id;
memcpy(&msg_id, buffer, sizeof(int));
if (msg_id != MSG_GATEWAY_PEER_DEATH) {free(buffer); return ERROR;}
// Lança erro se a mensagem não tinha o id previsto
memcpy(peer_id, buffer + sizeof(int), sizeof(int));
return TRUE;
}
// Utilizado para respostas a requests
int serializeInteger(int fd, struct sockaddr_in destination, int integer){
return sendto(fd, &integer, sizeof(int), 0, (struct sockaddr *) &destination, sizeof(destination));
}
int deserializeInteger(int fd, int * integer){
int check;
char * buffer = malloc(sizeof(int));
check = UDPRead(fd, buffer, sizeof(int), TIMEOUT);
if (check == ERROR || check == FALSE) return check;
memcpy(integer, buffer, sizeof(int));
free(buffer);
return TRUE;
}
int serializeUint32(int fd, struct sockaddr_in destination, uint32_t integer){
return sendto(fd, &integer, sizeof(uint32_t), 0, (struct sockaddr *) &destination, sizeof(destination));
}
int deserializeUint32(int fd, uint32_t * integer){
int check;
char * buffer = malloc(sizeof(uint32_t));
check = UDPRead(fd, buffer, sizeof(uint32_t), TIMEOUT);
if (check == ERROR || check == FALSE) return check;
memcpy(integer, buffer, sizeof(uint32_t));
free(buffer);
return TRUE;
}
// ---------------- CLIENT / GATEWAY messages ----------------
// ---------------- PEER / PEER messages ----------------