-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcommons.h
119 lines (100 loc) · 1.75 KB
/
commons.h
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
#include <arpa/inet.h>
/*
for:
htons()
htonl()
ntohs()
inet_aton()
inet_ntoa()
*/
#include <netinet/in.h>
/*
for:
inet_aton()
inet_ntoa()
*/
#include <sys/types.h>
/*
for:
socket()
bind()
recvfrom()
sendto()
stat()
*/
#include <sys/socket.h>
/*
for:
socket()
bind()
recvfrom()
sendto()
inet_aton()
inet_ntoa()
AF_INET
SOCK_DGRAM
*/
#include <errno.h>
/*
for:
return type of system calls
*/
#include <stdio.h>
/*
for:
printf()
sprintf()
fflush()
perror()
NULL
*/
#include <stdlib.h>
/*
for:
exit()
malloc()
*/
#include <string.h>
/*
for:
memset()
strlen()
strcpy()
*/
#include <unistd.h>
/*
for:
close()
sleep()
stat()
*/
#define DEBUG 1 // used to toggle "debug mode".
#define LENBUFFER 506 // so as to make the whole packet well-rounded ( = 512 bytes)
#define PORTSERVER 8484
#define REQU 1
#define RACK 2
#define DONE 3
#define DACK 4
#define NP 0 // network packet
#define HP 1 // host packet
// print errors and error codes to stderr
#define er(e, x) \
do \
{ \
perror("ERROR IN: " #e "\n"); \
fprintf(stderr, "%d\n", x); \
exit(-1); \
} \
while(0)
// the following packet structure serves for both host and network packet, the subsequent conversion functions handle the nature of the packet.
struct packet
{
short int conid;
short int type;
short int status;
char buffer[LENBUFFER];
};
void set0(struct packet*); // sets memory allocated to a packet to 0.
struct packet* ntohp(struct packet*); // converts a packet from network to host.
struct packet* htonp(struct packet*); // converts a packet from host to network.
void printpacket(struct packet*, int); // prints the contents and the nature of a packet. prints only in the debug mode.