-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
127 lines (97 loc) · 2.82 KB
/
utils.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
/*
* conexiones.c
*
* Created on: 3 mar. 2019
* Author: utnso
*/
#include"utils.h"
void iniciar_servidor(void)
{
int socket_servidor;
struct addrinfo hints, *servinfo, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(IP, PUERTO, &hints, &servinfo);
for (p=servinfo; p != NULL; p = p->ai_next)
{
if ((socket_servidor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
continue;
if (bind(socket_servidor, p->ai_addr, p->ai_addrlen) == -1) {
close(socket_servidor);
continue;
}
break;
}
listen(socket_servidor, SOMAXCONN);
freeaddrinfo(servinfo);
while(1)
esperar_cliente(socket_servidor);
}
void esperar_cliente(int socket_servidor)
{
struct sockaddr_in dir_cliente;
int tam_direccion = sizeof(struct sockaddr_in);
int socket_cliente = accept(socket_servidor, (void*) &dir_cliente, &tam_direccion);
pthread_create(&thread,NULL,(void*)serve_client,&socket_cliente);
pthread_detach(thread);
}
void serve_client(int* socket)
{
int cod_op;
if(recv(*socket, &cod_op, sizeof(int), MSG_WAITALL) == -1)
cod_op = -1;
process_request(cod_op, *socket);
}
void process_request(int cod_op, int cliente_fd) {
int size;
void* msg;
switch (cod_op) {
case MENSAJE:
msg = recibir_mensaje(cliente_fd, &size);
devolver_mensaje(msg, size, cliente_fd);
free(msg);
break;
case 0:
pthread_exit(NULL);
case -1:
pthread_exit(NULL);
}
}
void* recibir_mensaje(int socket_cliente, int* size)
{
void * buffer;
recv(socket_cliente, size, sizeof(int), MSG_WAITALL);
buffer = malloc(*size);
recv(socket_cliente, buffer, *size, MSG_WAITALL);
return buffer;
}
void* serializar_paquete(t_paquete* paquete, int bytes)
{
void * magic = malloc(bytes);
int desplazamiento = 0;
memcpy(magic + desplazamiento, &(paquete->codigo_operacion), sizeof(int));
desplazamiento+= sizeof(int);
memcpy(magic + desplazamiento, &(paquete->buffer->size), sizeof(int));
desplazamiento+= sizeof(int);
memcpy(magic + desplazamiento, paquete->buffer->stream, paquete->buffer->size);
desplazamiento+= paquete->buffer->size;
return magic;
}
void devolver_mensaje(void* payload, int size, int socket_cliente)
{
t_paquete* paquete = malloc(sizeof(t_paquete));
paquete->codigo_operacion = MENSAJE;
paquete->buffer = malloc(sizeof(t_buffer));
paquete->buffer->size = size;
paquete->buffer->stream = malloc(paquete->buffer->size);
memcpy(paquete->buffer->stream, payload, paquete->buffer->size);
int bytes = paquete->buffer->size + 2*sizeof(int);
void* a_enviar = serializar_paquete(paquete, bytes);
send(socket_cliente, a_enviar, bytes, 0);
free(a_enviar);
free(paquete->buffer->stream);
free(paquete->buffer);
free(paquete);
}