forked from rovinbhandari/RTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommons_functions.c
53 lines (41 loc) · 1.39 KB
/
commons_functions.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
#include "commons.h"
void set0(struct packet* p) // sets memory allocated to a packet to 0.
{
memset(p, 0, sizeof(struct packet));
}
struct packet* ntohp(struct packet* np) // converts a packet from network to host.
{
size_t size_packet = sizeof(struct packet);
struct packet* hp = (struct packet*) malloc(size_packet);
memset(hp, 0, size_packet);
hp->conid = ntohs(np->conid);
hp->type = ntohs(np->type);
hp->status = ntohs(np->status);
strcpy(hp->buffer, np->buffer); // char* datatype doesn't need conversion; it is copied as it is.
return hp;
}
struct packet* htonp(struct packet* hp) // converts a packet from host to network.
{
size_t size_packet = sizeof(struct packet);
struct packet* np = (struct packet*) malloc(size_packet);
memset(np, 0, size_packet);
np->conid = ntohs(hp->conid);
np->type = ntohs(hp->type);
np->status = ntohs(hp->status);
strcpy(np->buffer, hp->buffer); // char* datatype doesn't need conversion; it is copied as it is.
return np;
}
void printpacket(struct packet* p, int ptype) // prints the contents and the nature of a packet. prints only in the debug mode.
{
if(!DEBUG)
return;
if(ptype)
printf("\t\tHOST PACKET\n");
else
printf("\t\tNETWORK PACKET\n");
printf("\t\tconid = %d\n", p->conid);
printf("\t\ttype = %d\n", p->type);
printf("\t\tstatus = %d\n", p->status);
printf("\t\tbuffer = %s\n\n", p->buffer);
fflush(stdout);
}