-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpacket.c
More file actions
77 lines (67 loc) · 1.5 KB
/
packet.c
File metadata and controls
77 lines (67 loc) · 1.5 KB
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
#include "packet.h"
#include <stdio.h>
void init_packet(packet *pckt){
//Initializes a packet structure
pckt->opcode = -1;
pckt->mode = -1;
pckt->block_number = -1;
pckt->error_code = -1;
}
/*helper functions*/
int is_read_request(int opcode){
return opcode == RRQ;
}
int is_write_request(int opcode){
return opcode == WRQ;
}
int is_data_packet(int opcode){
return opcode == DATA;
}
int is_error_packet(int opcode){
return opcode == ERROR;
}
int is_ack_packet(int opcode){
return (opcode == ACK);
}
/*creates the packet structure from buffer*/
/*Calls the corresponding functions for each type of packet*/
int strip_raw_packet(char *buffer, int blen, packet *pckt){
int opcode;
if(buffer[0] != '\0'){
printf("Malformed packet\n");
return 0;
}
else{
if( ! (blen > 1 ) ){
printf("Malformed packet\n");
return 0;
}
opcode = (int) buffer[1];
switch(opcode){
case RRQ:
//printf("Read request packet");
break;
case WRQ:
//printf("Write request packet\n");
break;
case DATA:
//printf("Data packet\n");
pckt->opcode=DATA;
return create_data_packet_from_raw(buffer+2, blen - 2, pckt);
break;
case ACK:
//printf("Acknowledgement packet\n");
pckt->opcode = ACK;
return create_ack_packet_from_raw(buffer+2, blen-2, pckt);
break;
case ERROR:
//printf("Error packet\n");
pckt->opcode = ERROR;
return create_error_packet_from_raw(buffer+2, blen-2,pckt);
break;
default:
//printf("Malformed packet\n");
return 0;
}
}
}