-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
277 lines (260 loc) · 8.08 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
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
#include "utils.h"
/**
*Envoie le paquet @param:packet à travers @param:socket
* à l'adresse IP @param:adresse et de port @param:port.
* @return 0 | -1 en cas d'erreur.
*/
int send_packet(const tftp_packet *packet, SocketUDP *socket, char *adresse, int port){
char *buffer = (char*) malloc(sizeof(char*));
int16_t type;
int len = 0;
bool fini = false;
int tmp = 0;
type = getType(packet);
while (!fini) {
switch(type) {
//paquets de type RRQ
case 1:
len = init_buff_RRQ(buffer, packet->packet.tftp_packet_RRQ);
fini = true;
break;
//paquets de type WRQ
case 2:
len = init_buff_WRQ(buffer, packet->packet.tftp_packet_WRQ);
fini = true;
break;
//paquets de type DATA
case 3 :
len = init_buff_DATA(buffer, packet->packet.tftp_packet_DATA, &fini, &tmp);
break;
//paquets de type ACK
case 4 :
len = init_buff_ACK(buffer, packet->packet.tftp_packet_ACK);
fini = true;
break;
//paquets de type ERROR
case 5:
len = init_buff_ERR(buffer, packet->packet.tftp_packet_ERROR);
fini = true;
break;
default :
fprintf(stderr,"Type de paquet inconnu\n");
exit(EXIT_FAILURE);
fini = true;
break;
}
if(writeToSocketUDP(socket, adresse, port, buffer, len) == -1) {
perror("WriteToSocketUDP");
return -1;
}
}
return 0;
}
/**
*Attend sur @param:socket de recevoir un paquet TFTP de l'adresse @param:adresse
*et de port @param:port avec un timeout @param:sec. Si un message d'une autre
*autre source, un message d'erreur est envoyé à l'expediteur et l'écoute recommence.
*@return: 0 si le timeout à expiré | -1 en cas d'erreur.
*/
int receive_with_timeout(SocketUDP *socket, char *adresse, int port, int sec, tftp_packet *response_packet, size_t *packet_length){
char *buffer = (char*) malloc (sizeof(char *));
char *adresseRecu = (char*) malloc (sizeof(char *));
int portRecu = 0;
int dataSize = 0;
while(!strcmp(adresse,adresseRecu) && port != portRecu){
dataSize = readFromSocketUDP(socket, buffer, sizeof(buffer), adresseRecu, &portRecu, sec);
if(!strcmp(adresse,adresseRecu) && port != portRecu){
tftp_packet_ERROR *err;
init_packet_ERROR(err,05);
if(send_packet((tftp_packet *)&err,socket,adresseRecu,portRecu) == -1){
fprintf(stderr,"Une erreur est survenue pendant l'envoie\n");
}
}
}
switch(dataSize) {
case -1 :
fprintf(stderr, "readFromSocketUDP: Erreur de lecture\n");
return -1;
break;
case 0 :
fprintf(stderr, "readFromSocketUDP: timeout expiré\n");
break;
default :
return dataSize;
}
return dataSize;
}
/**
* Envoie d'un paquet data et attente de l'accusé de recepetion ack en retour.
**/
int sendDataWaitACK(SocketUDP *socket, tftp_packet_DATA *packetData, tftp_packet_ACK *packetAck, char *adresse, int port){
int i = 0;
bool recu = false;
char *adresseRecu = (char *)malloc(sizeof(char *));
int portRecu = 0;
while(i < 10 && !recu) {
send_packet((tftp_packet *)&packetData, socket,adresse, port);
size_t packetLength = sizeof(packetAck);
receive_with_timeout(socket, adresseRecu, portRecu, 10, (tftp_packet *)&packetAck, &packetLength);
if(getType((tftp_packet *)&packetAck) == 04) {
recu = true;
} else if(getType((tftp_packet *)&packetAck) != 04) {
tftp_packet_ERROR *err;
init_packet_ERROR(err, 04);
send_packet((tftp_packet *)&err, socket, adresseRecu, portRecu);
return -1;
}
i++;
}
if(i == 10 && !recu) {
return -1;
}
return 0;
}
/**
* Envoie d'un accusé de reception ack et attente d'un paquet data.
*/
int sendACKWaitData(SocketUDP *socket, tftp_packet_ACK *packetAck, tftp_packet_DATA *packetData, char *adresse, int port){
int i = 0;
bool recu = false;
char *adresseRecu = (char *)malloc(sizeof(char *));
int portRecu = 0;
char *buffer = (char *)malloc(sizeof(char*));
while(i < 10 && !recu) {
send_packet((tftp_packet *)&packetAck, socket,adresse, port);
readFromSocketUDP(socket, buffer, 516, adresseRecu, &portRecu, 10);
if(getType((tftp_packet *)&packetData) == 03) {
recu = true;
} else if(getType((tftp_packet *)&packetData) != 03) {
tftp_packet_ERROR *err;
init_packet_ERROR(err, 04);
send_packet((tftp_packet *)&err, socket, adresseRecu, portRecu);
return -1;
}
i++;
}
if(i == 10 && !recu) {
return -1;
}
return 0;
}
/**
*Envoie d'une requete de lecture et attend un paquet data.
*/
int sendRRQWaitData(SocketUDP *socket, tftp_packet_RRQ *packetRrq, tftp_packet_DATA *packetData, char *adresse, int port){
int i = 0;
bool recu = false;
char *adresseRecu = (char *)malloc(sizeof(char *));
int portRecu = 0;
while(i < 10 && !recu) {
send_packet((tftp_packet *)&packetRrq, socket,adresse, port);
size_t packetLength = sizeof(packetData);
receive_with_timeout(socket, adresseRecu, portRecu, 10, (tftp_packet *)&packetData, &packetLength);
if(getType((tftp_packet *)&packetData) == 03) {
recu = true;
} else if(getType((tftp_packet *)&packetData) != 03) {
tftp_packet_ERROR *err;
init_packet_ERROR(err, 04);
send_packet((tftp_packet *)&err, socket, adresseRecu, portRecu);
return -1;
}
i++;
}
if(i == 10 && !recu) {
return -1;
}
return 0;
}
/**
*Envoie d'une requete d'écriture et attente de l'accord du serveur pour débuter l'envoie.
*/
int sendWRQWaitACK(SocketUDP *socket, tftp_packet_WRQ *packetWrq, tftp_packet_ACK *packetAck, char *adresse, int port){
int i = 0;
bool recu = false;
char *adresseRecu = (char *)malloc(sizeof(char *));
int portRecu = 0;
while(i < 10 && !recu) {
send_packet((tftp_packet *)&packetWrq, socket,adresse, port);
size_t packetLength = sizeof(packetAck);
receive_with_timeout(socket, adresseRecu, portRecu, 10, (tftp_packet *)&packetAck, &packetLength);
if(getType((tftp_packet *)&packetAck) == 04) {
recu = true;
} else if(getType((tftp_packet *)&packetAck) != 04) {
tftp_packet_ERROR *err;
init_packet_ERROR(err, 04);
send_packet((tftp_packet *)&err, socket, adresseRecu, portRecu);
return -1;
}
i++;
}
if(i == 10 && !recu) {
return -1;
}
return 0;
}
int init_buff_RRQ(char *buffer, tftp_packet_RRQ packet) {
int len = 0;
memcpy(buffer, (char*)&packet.opCode, 2);
len += 2;
memcpy(buffer+len, packet.fileName, strlen(packet.fileName));
len += strlen(packet.fileName);
memcpy(buffer+len,'\0', 1);
len += 1;
memcpy(buffer+len, packet.mode, strlen(packet.mode));
len += strlen(packet.mode);
memcpy(buffer+len,'\0', 1);
len+=1;
return len;
}
int init_buff_WRQ(char *buffer, tftp_packet_WRQ packet) {
int len = 0;
memcpy(buffer, (char*)&packet.opCode, 2);
len += 2;
memcpy(buffer+len, packet.fileName,strlen(packet.fileName));
len += strlen(packet.fileName);
memcpy(buffer+len,'\0', 1);
len += 1;
memcpy(buffer+len, packet.mode, strlen(packet.mode));
len += strlen(packet.mode);
memcpy(buffer+len,'\0', 1);
len += 1;
return len;
}
int init_buff_DATA(char *buffer, tftp_packet_DATA packet, bool *fini, int *tmp) {
int len = 0;
memcpy(buffer, (char*)&packet.opCode, 2);
len += 2;
memcpy(buffer+len, (char*)&packet.blockNb, 2);
len += 2;
size_t length = strlen(packet.data) - (size_t)tmp;
if (length > 512) {
memcpy(buffer+len, packet.data+(*tmp),512);
len += 512;
tmp += 512;
} else {
memcpy(buffer+len, packet.data+(*tmp), length);
len += 512;
*fini = true;
}
return len;
}
int init_buff_ACK(char *buffer, tftp_packet_ACK packet) {
int len = 0;
memcpy(buffer, (char*)&packet.opCode, 2);
len += 2;
memcpy(buffer+len, (char*)&packet.blockNb, 2);
len += 2;
return len;
}
int init_buff_ERR(char *buffer, tftp_packet_ERROR packet) {
int len = 0;
memcpy(buffer,(char*)&packet.opCode, 2);
len += 2;
memcpy(buffer+len,(char*)&packet.errorCode, 2);
len += 2;
memcpy(buffer+len, packet.errMsg, strlen(packet.errMsg));
len += strlen(packet.errMsg);
memcpy(buffer+len,'\0', 1);
len += 1;
return len;
}