-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendian.c
84 lines (63 loc) · 2.17 KB
/
endian.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
/*
* ECE 463 Introduction to Computer Networks LAB 2 SOURCE FILE
*
* File Name: endian.c
*
* Purpose: Defines routines that are useful in endian conversions
*
*/
#include "ne.h"
/*
* This function converts struct pkt_INIT_RESPONSE
* from network to host byte order.
*/
void ntoh_pkt_INIT_RESPONSE (struct pkt_INIT_RESPONSE *ne_resp) {
int i;
ne_resp->no_nbr = ntohl(ne_resp->no_nbr);
for(i=0;i<(ne_resp->no_nbr);i++) {
(ne_resp->nbrcost[i]).nbr = ntohl((ne_resp->nbrcost[i]).nbr);
(ne_resp->nbrcost[i]).cost = ntohl((ne_resp->nbrcost[i]).cost);
}
}
/*
* This function converts struct pkt_RT_UPDATE
* from host to network byte order.
*/
void hton_pkt_RT_UPDATE (struct pkt_RT_UPDATE *send_upd) {
int i;
int j;
send_upd->sender_id = htonl (send_upd->sender_id);
send_upd->dest_id = htonl (send_upd->dest_id);
for (i = 0; i < send_upd->no_routes; i++)
{
(send_upd->route[i]).dest_id = htonl ((send_upd->route[i]).dest_id);
(send_upd->route[i]).next_hop = htonl ((send_upd->route[i]).next_hop);
(send_upd->route[i]).cost = htonl ((send_upd->route[i]).cost);
for(j = 0; j < send_upd->route[i].path_len; j++){
send_upd->route[i].path[j] = htonl(send_upd->route[i].path[j]);
}
send_upd->route[i].path_len = htonl(send_upd->route[i].path_len);
}
send_upd->no_routes = htonl (send_upd->no_routes);
}
/*
* This function converts struct pkt_RT_UPDATE
* from network to host byte order.
*/
void ntoh_pkt_RT_UPDATE (struct pkt_RT_UPDATE *recv_upd) {
int i;
int j;
recv_upd->sender_id = ntohl (recv_upd->sender_id);
recv_upd->dest_id = ntohl (recv_upd->dest_id);
recv_upd->no_routes = ntohl (recv_upd->no_routes);
for (i = 0; i < recv_upd->no_routes; i++)
{
(recv_upd->route[i]).dest_id = ntohl ((recv_upd->route[i]).dest_id);
(recv_upd->route[i]).next_hop = ntohl ((recv_upd->route[i]).next_hop);
(recv_upd->route[i]).cost = ntohl ((recv_upd->route[i]).cost);
recv_upd->route[i].path_len = ntohl(recv_upd->route[i].path_len);
for(j = 0; j < recv_upd->route[i].path_len; j++){
recv_upd->route[i].path[j] = ntohl(recv_upd->route[i].path[j]);
}
}
}