-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.c
351 lines (265 loc) · 9.17 KB
/
sockets.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
#include "sockets.h"
#define BACKLOG 100
int crearSocket(t_log* logger)
{
int unSocket;
int si = 1;
//--Crea el socket
if ((unSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
log_error(logger, "Creacion socket: %s", strerror(errno));
return EXIT_FAILURE;
} else {
//--Setea las opciones para que pueda escuchar varios al mismo tiempo
if (setsockopt(unSocket, SOL_SOCKET, SO_REUSEADDR, &si, sizeof(int)) == -1) {
log_error(logger, "Setsockopt: %s", strerror(errno));
return EXIT_FAILURE;
}
return unSocket;
}
}
void bindearSocket(int unSocket, struct sockaddr_in socketInfo, t_log* logger)
{
//--Bindear socket al proceso server
if (bind(unSocket, (struct sockaddr*)&socketInfo, sizeof(socketInfo)) == -1) {
log_error(logger, "Error al bindear socket escucha: %s", strerror(errno));
exit(EXIT_FAILURE);
}
}
void escucharEn(int unSocket)
{
if (listen(unSocket, BACKLOG) == -1) {
perror("Error al poner a escuchar socket");
exit(EXIT_FAILURE);
}
}
int crearSocketEscucha(int puerto, t_log* logger)
{
struct sockaddr_in myAddress;
int socketEscucha;
socketEscucha = crearSocket(logger);
//--Arma la información que necesita para mandar cosas
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = INADDR_ANY;
myAddress.sin_port = htons(puerto);
memset(&(myAddress.sin_zero), '\0', 8); // Poner a cero el resto de la estructura
bindearSocket(socketEscucha, myAddress,logger);
//--Escuchar
escucharEn(socketEscucha);
return socketEscucha;
}
int enviarPaquete(int socketServidor, tPaquete* pPaqueteAEnviar, t_log* logger, char* info)
{
int byteEnviados;
log_debug(logger, ">>> %s", info);
byteEnviados = send(socketServidor, (char *)pPaqueteAEnviar, sizeof(tHeader) + pPaqueteAEnviar->length, 0);
if (byteEnviados == -1) {
log_error(logger, "%s: %s", info, strerror(errno));
return -1;
} else {
return byteEnviados;
}
}
int recibirPaquete(int socketReceptor, tMensaje* tipoMensaje, char** psPayload, t_log* pLogger, char* sMensajeLogger)
{
tHeader header;
int bytesRecibidosHeader = 0;
int bytesRecibidos = 0;
log_debug(pLogger, "<<< %s", sMensajeLogger);
bytesRecibidosHeader = recv(socketReceptor, &header, sizeof(tHeader), MSG_WAITALL);
if (bytesRecibidosHeader == 0) {
return 0; // CERRO CONEXION
} else if (bytesRecibidosHeader < 0) {
log_error(pLogger, "%s: %s", sMensajeLogger, strerror(errno));
return -1; // ERROR
}
//log_debug(pLogger, "Se recibe header de %d bytes y tipo de mensaje %d", bytesRecibidosHeader, header.type); //TODO borrar esta linea
*tipoMensaje = (tMensaje) header.type;
if (header.length > 0) {
*psPayload = malloc(header.length);
bytesRecibidos = recv(socketReceptor, *psPayload, header.length, MSG_WAITALL);
if (bytesRecibidos < 0) {
log_error(pLogger, "%s: %s", sMensajeLogger, strerror(errno));
free(*psPayload); // ERROR, se libera el espacio reservado
return -1;
}
//log_debug(pLogger, "Se reciben %d bytes de payload", bytesRecibidos);//TODO borrar esta linea
}
return bytesRecibidos + bytesRecibidosHeader;
}
/*
* @NAME: getConnection
* @DESC: Multiplexa con Select
*
* Valores de salida:
* =0 = se agrego un nuevo socket
* <0 = Se cerro el socket que devuelve
* >0 = Cambio el socket que devuelve
*/
signed int getConnection(fd_set *setSockets, int *maxSock, int sockListener, tMensaje *tipoMensaje, char** payload, t_log* logger)
{
int iSocket;
int iNewSocket;
int iBytesRecibidos;
fd_set setTemporal;
FD_ZERO(&setTemporal);
setTemporal = *setSockets;
struct sockaddr_in clientAddress;
socklen_t sinClientSize;
sinClientSize = sizeof(clientAddress);
//--Multiplexa conexiones
if (select(*maxSock + 1, &setTemporal, NULL, NULL, NULL) == -1) {
log_error(logger, "select: %s", strerror(errno));
exit(EXIT_FAILURE);
}
//--Cicla las conexiones para ver cual cambio
for (iSocket = 0; iSocket <= *maxSock; iSocket++) {
//--Si el i° socket cambió
if (FD_ISSET(iSocket, &setTemporal)) {
//--Si el que cambió, es el listener
if (iSocket == sockListener) {
//--Gestiona nueva conexión
iNewSocket = accept(sockListener, (struct sockaddr*) &clientAddress, &sinClientSize);
if (iNewSocket == -1) {
log_error(logger, "getConnection :: accept: %s", strerror(errno));
} else {
log_trace(logger, "Nueva conexion socket: %d", iNewSocket);
//--Agrega el nuevo listener
FD_SET(iNewSocket, setSockets);
if (iNewSocket > *maxSock) {
*maxSock = iNewSocket;
}
}
} else {
//--Gestiona un cliente ya conectado
if ((iBytesRecibidos = recibirPaquete(iSocket, tipoMensaje, payload, logger, "Se recibe informacion")) <= 0) {
//--Si cerró la conexión o hubo error
if (iBytesRecibidos == 0) {
log_trace(logger, "Fin de conexion de socket %d.", iSocket);
} else {
log_error(logger, "recv: %s", strerror(errno));
}
//--Cierra la conexión y lo saca de la lista
close(iSocket);
FD_CLR(iSocket, setSockets);
*tipoMensaje = DESCONEXION;
}
return iSocket;
}
}
}
return -1;
}
signed int getConnectionTimeOut(fd_set *setSockets, int *maxSock, int sockListener, tMensaje *tipoMensaje, char** payload, struct timeval *timeout, t_log* logger)
{
int iSocket;
int iNewSocket;
int iBytesRecibidos;
fd_set setTemporal;
FD_ZERO(&setTemporal);
setTemporal = *setSockets;
struct sockaddr_in clientAddress;
socklen_t sinClientSize;
sinClientSize = sizeof(clientAddress);
//--Multiplexa conexiones
if (select(*maxSock + 1, &setTemporal, NULL, NULL, timeout) == -1) {
log_error(logger, "select: %s", strerror(errno));
exit(EXIT_FAILURE);
}
//--Cicla las conexiones para ver cual cambio
for (iSocket = 0; iSocket <= *maxSock; iSocket++) {
//--Si el i° socket cambió
if (FD_ISSET(iSocket, &setTemporal)) {
//--Si el que cambió, es el listener
if (iSocket == sockListener) {
//--Gestiona nueva conexión
iNewSocket = accept(sockListener, (struct sockaddr*) &clientAddress, &sinClientSize);
if (iNewSocket == -1) {
log_error(logger, "getConnection :: accept: %s", strerror(errno));
} else {
log_trace(logger, "Nueva conexion socket: %d", iNewSocket);
//--Agrega el nuevo listener
FD_SET(iNewSocket, setSockets);
if (iNewSocket > *maxSock) {
*maxSock = iNewSocket;
}
}
} else {
//--Gestiona un cliente ya conectado
if ((iBytesRecibidos = recibirPaquete(iSocket, tipoMensaje, payload, logger, "Se recibe informacion")) <= 0) {
//--Si cerró la conexión o hubo error
if (iBytesRecibidos == 0) {
log_debug(logger, "Fin de conexion de socket %d.", iSocket);
} else {
log_error(logger, "recv: %s", strerror(errno));
}
//--Cierra la conexión y lo saca de la lista
close(iSocket);
FD_CLR(iSocket, setSockets);
*tipoMensaje = DESCONEXION;
}
return iSocket;
}
}
}
return -1;
}
signed int multiplexar(fd_set *master, fd_set *temp, int *maxSock, tMensaje* tipoMensaje, char** buffer, t_log* logger)
{
int iSocket;
int nBytes;
memcpy(temp, master, sizeof(fd_set));
if (select(*maxSock + 1, temp, NULL, NULL, NULL) == -1) {
log_error(logger, "select: %s", strerror(errno));
exit(EXIT_FAILURE);
}
//--Cicla las conexiones para ver cual cambió
for (iSocket = 0; iSocket <= *maxSock; iSocket++) {
if (FD_ISSET(iSocket, temp)) {
//--Gestiona un cliente ya conectado
if ((nBytes = recibirPaquete(iSocket, tipoMensaje, buffer, logger, "Se recibe Mensaje")) <= 0) {
//--Si cerró la conexión o hubo error
if (nBytes == 0) {
log_trace(logger, "Fin de conexion de %d.", iSocket);
} else {
log_error(logger, "multiplexar :: recv in %d: %s", iSocket, strerror(errno));
}
//--Cierra la conexión y lo saca de la lista
close(iSocket);
FD_CLR(iSocket, master);
*tipoMensaje = DESCONEXION;
}
return iSocket;
}
}
return -1;
}
signed int connectToServer(char *ip_server, int puerto, t_log *logger)
{
int iSocket; // Escuchar sobre sock_fd, nuevas conexiones sobre new_fd
struct sockaddr_in their_addr; // Información sobre mi dirección
// Seteo IP y Puerto
their_addr.sin_family = AF_INET; // Ordenación de bytes de la máquina
their_addr.sin_port = htons(puerto); // short, Ordenación de bytes de la red
their_addr.sin_addr.s_addr = inet_addr(ip_server);
memset(&(their_addr.sin_zero), '\0', 8); // Poner a cero el resto de la estructura
// Pido socket
if ((iSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
log_error(logger, "socket: %s", strerror(errno));
return EXIT_FAILURE;
}
// Intento conectar
if (connect(iSocket, (struct sockaddr *) &their_addr, sizeof their_addr) == -1) {
log_error(logger, "connect: %s", strerror(errno));
return EXIT_FAILURE;
}
log_trace(logger, "Se realiza conexion con socket %d", iSocket);
return iSocket;
}
int desconectarseDe(int iSocket)
{
if (close(iSocket)) {
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
}