-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.c
334 lines (294 loc) · 8.38 KB
/
packet.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
/*******************************************
* socket.c *
* Catinae, Matheus da Rosa, Pedro Vanzella*
* *
* Redes 2 *
*******************************************/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "packet.h"
#include "usuario.h"
#include "aviao.h"
extern struct aviao* voos[50];
void pacote_pretty_print(Packet* pkt, char op)
{
struct usuario* usr;
return;
printf("\n\e[1m\e[32mpacote_pretty_print(): ");
switch(op)
{
case 1:
printf("Servidor Enviando\e[0m\n");
break;
case 2:
printf("Servidor Recebendo\e[0m\n");
break;
case 3:
printf("Cliente Enviando\e[0m\n");
break;
case 4:
printf("Cliente Recebendo\e[0m\n");
break;
}
printf("\tOperacao: %d\n", pkt->operacao);
printf("\tIP: %s\n", pkt->IP);
printf("\tUsuario:\n");
printf("\t\tNome:\t%s\n", pkt->usr.nome);
printf("\t\tSenha:\t%s\n", pkt->usr.senha);
printf("\tAviao:\n");
printf("\t\tNome:\t%s\n", pkt->voo.nome);
printf("\t\tPartida:\t%4d\n", pkt->voo.partida);
printf("\t\tChegada:\t%4d\n", pkt->voo.chegada);
printf("\t\tStatus:\t%s\n", pkt->voo.status);
printf("\t\tAssentos alocados:\n");
int i;
for(i = 0; i <= 150; i++)
{
/*if(pkt->voo.assentos[i] != 0)
{
usr = find_by_id(pkt->voo.assentos[i]);
printf("\t\t\t[%3d] [%d] %s\n", i, pkt->voo.assentos[i], usr->nome);
}*/
}
printf("\n");
}
void Cliente()
{
int sock, bytes_recieved;
char buffer[1024];
struct hostent *host;
struct sockaddr_in server_addr;
char serverIP[20];
char op;
int ass;
int usr_id = 0;
Packet* pkt;
pkt = (Packet*)malloc(sizeof(Packet));
memset(pkt, '\0', sizeof(Packet));
printf("IP do servidor> ");
scanf("%s", serverIP);
host = gethostbyname(serverIP);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
perror("Connect");
exit(1);
}
while(1)
{
/*********** RECEBE DO SERVER **************/
bytes_recieved = recv(sock, buffer, sizeof(buffer), 0);
pkt = (Packet*)buffer;
pacote_pretty_print(pkt, 4);
switch(pkt->operacao)
{
case 0: // HELLO
printf("HELLO>\n> ");
break;
case 2: // LOGIN FALHOU
pkt->operacao = 1; // Pedido de login
printf("LOGIN> ");
scanf("%s", pkt->usr.nome);
printf("SENHA> ");
scanf("%s", pkt->usr.senha);
send(sock, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 3);
break;
case 4: // FALHA
printf("FALHA>\n> ");
break;
case 3: // SUCESSO
printf("SUCESSO>\n> ");
break;
case 7: // RESPOSTA DE CONSULTA
printf("PARTIDA: %d\tCHEGADA: %d\nSTATUS: %s\n> ", pkt->voo.partida, pkt->voo.chegada, pkt->voo.status);
break;
}
/***************** ENVIA PARA O SERVER ****************/
memset(pkt, '\0', sizeof(Packet)); // Zerar meu pacote depois que li ele
op = getchar();
switch(op)
{
case 'l': // LOGIN
pkt->operacao = 1; // Pedido de login
printf("LOGIN> ");
scanf("%s", pkt->usr.nome);
printf("SENHA> ");
scanf("%s", pkt->usr.senha);
send(sock, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 3);
break;
case 'r': // RESERVA ASSENTO
pkt->operacao = 5; // Pedido de reserva
printf("VOO> ");
scanf("%s", pkt->voo.nome);
printf("ASSENTO> ");
scanf("%d", &ass);
pkt->voo.assentos[ass] = usr_id;
send(sock, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 3);
break;
case 'c': // CONSULTA VOO
pkt->operacao = 6; // Pedido de consulta
printf("VOO> ");
scanf("%s", pkt->voo.nome);
send(sock, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 3);
break;
default:
// printf("Operacao desconhecida!\n");
continue;
}
}
}
void Servidor()
{
int sock, connected, bytes_recieved , true = 1;
char buffer[1024];
Packet* pkt;
pkt = (Packet*)malloc(sizeof(Packet));
memset(pkt, '\0', sizeof(Packet));
memset(buffer, '\0', sizeof(buffer));
struct aviao* av;
struct aviao* av2;
int ass = 0;
int i;
struct sockaddr_in server_addr,client_addr;
int sin_size;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket");
exit(1);
}
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1)
{
perror("Setsockopt");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
perror("Unable to bind");
exit(1);
}
if (listen(sock, 5) == -1) {
perror("Listen");
exit(1);
}
printf("\nTCPServer Waiting for client on port 5000\n");
fflush(stdout);
while(1)
{
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
if(connected)
{
/* Envia HELLO */
memset(buffer, '\0', sizeof(buffer));
pkt = (Packet *)buffer;
send(connected, buffer, sizeof(buffer), 0); // Size seria zero aqui com strlen...
pacote_pretty_print(pkt, 1);
}
printf("\n\e[1m\e[32mI got a connection from (%s , %d)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
loop:
while (1)
{
/********************* RECEBE DO CLIENTE ******************/
bytes_recieved = recv(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 2);
switch(pkt->operacao)
{
case 1: // Pedido de Login
if(login(pkt->usr.nome, pkt->usr.senha))
{
pkt->operacao = 3; // SUCESSO!
pkt->usr.id = find_by_name(pkt->usr.nome)->id;
}
else
{
pkt->operacao = 2; // Falha de login
}
send(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 1);
break;
case 5: // Pedido de reserva
/* Achar qual assento */
av = find_by_voo(pkt->voo.nome);
if(!av) // Nao temos aviao com esse nome
{
pkt->operacao = 4;
send(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 1);
break;
}
for(i = 0; i <= 150; i++)
{
if(pkt->voo.assentos[i] != 0)
{
ass = i;
}
}
if(ass == 0) // pacote invalido
{
pkt->operacao = 4;
send(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 1);
break;
}
av = reserva_assento(av, ass, pkt->usr.id);
if(!av) // Assento ocupado
{
pkt->operacao = 4;
send(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 1);
break;
}
av2 = &pkt->voo;
memcpy(av2, av, sizeof(struct aviao));
break;
case 6: // Pedido de consulta
av = find_by_voo(pkt->voo.nome);
if(!av) // Aviao nao encontrado
{
pkt->operacao = 4;
send(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 1);
}
else // Achei o voo
{
memcpy(&pkt->voo, av, sizeof(struct aviao)); // Copia voo achado pro pacote
pkt->operacao = 7; // Manda resposta
send(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 1);
}
break;
default:
pkt->operacao = 4;
send(connected, buffer, sizeof(buffer), 0);
pacote_pretty_print(pkt, 1);
break;
}
}
goto loop;
}
close(sock);
}