-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex8-recv.c
184 lines (148 loc) · 4.44 KB
/
ex8-recv.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
/*---------------------------------------------------------------------------
* Programmazione di rete
*
* Listener UDP
*
* Questo programma termina quanto riceve un pacchetto con il messaggio
* pari a QUIT_STRING.
* Il fatto che invia strutture dati lo rende particolarmente fragile.
* Leggi l'articolo.
*
* avviare con: ./recv numero_porta
*
* Ing. Alessio Palma
*
* $Id$
*
*/
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<sys/socket.h>
#include<fcntl.h>
#include"esempi.h"
/*------------------------------------------------------------------------
*
* Dichiarazioni variabili
*/
int socket_descriptor, i, bytes_read;
unsigned int counter;
struct sockaddr_in socket_address, remote_address;
socklen_t addr_len;
struct type_pacchetto_v2 pacchetto;
/*------------------------------------------------------------------------
*
* prototipi
*/
void intialize_socket( int );
void print_message( void );
void read_message( void );
int user_exit( void );
unsigned long int checksum( char *, unsigned int);
void message_delete( void );
/*------------------------------------------------------------------------
*
* initialize_socket( numero di porta )
*
* inizializza per prepara il socket collegandolo alla porta desiderata
*/
void initialize_socket( int port )
{
if ( (socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
printf("Errore: non posso aprire il socket\n");
exit(1);
}
socket_address.sin_family = AF_INET;
socket_address.sin_addr.s_addr = htonl(INADDR_ANY);
socket_address.sin_port = htons( port );
if ( bind( socket_descriptor, (struct sockaddr *) &socket_address, sizeof(socket_address) ) ) {
printf("Errore: non posso collegare il socket\n");
exit(1);
}
}
/*----------------------------------------------------------------------
* unsigned long int checksum( char *, unsigned int )
*
* calcola il checksum di un vettore di caratteri
*
*/
unsigned long int checksum( char *vettore, unsigned int uint_lunghezza)
{
unsigned long int ulint_tempChk = 0;
char *pointer = vettore;
unsigned int uint_i;
for (uint_i=0; uint_i<uint_lunghezza; uint_i++) {
ulint_tempChk += *(pointer++);
}
return ulint_tempChk;
}
/*------------------------------------------------------------------------
*
* void delete_message( void )
*
* annulla il pacchetto corrente perchè non valido.
*/
void delete_message( void )
{
strcpy(pacchetto.nome,"ERRORE\n");
strcpy(pacchetto.messaggio,"E' stato ricevuto un pacchetto anomalo!!\n");
pacchetto.uint_seqNum = 0;
}
/*------------------------------------------------------------------------
*
* read_message()
*
* legge il messaggio presente nel buffer
*/
void read_message()
{
addr_len = sizeof(remote_address)/sizeof(char);
bytes_read = recvfrom( socket_descriptor,(void *)&pacchetto, sizeof(pacchetto)/sizeof(char),0,(struct sockaddr *)&remote_address, &addr_len );
if ( bytes_read != -1 ) {
if (
( pacchetto.int_versione != 2 )
&& ( checksum( pacchetto.messaggio, 100 ) != pacchetto.ulint_crc )
&& ( bytes_read != (sizeof(pacchetto)/sizeof(char)) )
)
delete_message();
else {
pacchetto.messaggio[MAXLEN_MSG-1] = 0x00;
pacchetto.nome[MAXLEN_USRNAME-1] = 0x00;
}
}
else { printf("(!) recvfrom error\n"); }
}
/*------------------------------------------------------------------------
*
* print_message()
*
* stampa il messaggio caricato nelle strutture
*/
void print_message( void )
{
printf("seqNum: %4u da %s porta %d\n", pacchetto.uint_seqNum, inet_ntoa(remote_address.sin_addr), ntohs(remote_address.sin_port));
printf("Mittente: %s ", pacchetto.nome);
printf("Testo: %s", pacchetto.messaggio);
printf("------------------------------------------------------------\n");
}
/*------------------------------------------------------------------------
*
* int user_exit()
*
* se il programma ha ricevuto QUIT_STRING restuisce 1
*/
int user_exit( void )
{
return ( strncmp( pacchetto.messaggio, QUIT_STRING, 4) == 0 );
}
main(int argc, char **argv )
{
initialize_socket( atoi(argv[1]) );
while( ! user_exit() ) {
read_message();
print_message();
}
close(socket_descriptor);
}