-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinet.c
More file actions
79 lines (63 loc) · 1.93 KB
/
Copy pathinet.c
File metadata and controls
79 lines (63 loc) · 1.93 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
78
79
#include "inet.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "ip.h"
int host_byte_order(void){
int x=0x00000001;
if (*(char*)&x) {
return __LITTLE_ENDIAN;
}else{
return __BIG_ENDIAN;
}
}
// this implementation referrd to byteswap.h
uint16_t byte_swap16(uint16_t x){
return (x & 0x00ff) << 8 | (x & 0xff00) >> 8;
}
uint32_t byte_swap32(uint32_t x){
return (x & 0x000000ff) << 24 | (x & 0x0000ff00) << 8 | (x & 0x00ff0000) >> 8 | (x & 0xff000000) >> 24;
}
uint16_t ntohs(uint16_t network_short){
uint16_t tmp = 0;
tmp = (network_short >> 8) | (network_short << 8);
return tmp;
}
uint32_t ntohl(uint32_t network_long){
if(host_byte_order() == LITTLE_ENDIAN){
return byte_swap32(network_long);
}else{
return network_long;
}
}
uint16_t htons(uint16_t host_short){
uint16_t tmp = 0;
tmp = (host_short << 0) & 0xff00;
tmp = tmp >> 8;
tmp |= (host_short << 8) & 0xff00;
return tmp;
}
uint32_t htonl(uint32_t host_long){
uint32_t tmp = 0;
for(int i = 0; i < 4; i++){
tmp |= (host_long << i * 8) &0xff000000;
if(i == 3) break;
tmp = tmp >> 8;
}
return tmp;
}
void ntop(u_int32_t src, char *dest){
u_char addr[16];
sprintf(addr,"%u.%u.%u.%u",(src >> 24) & 0xFF,(src >> 16) & 0xFF,(src >> 8) & 0xFF,(src >> 0) & 0xFF);
strcpy(dest, addr);
}
void ip_hdr_ntoh(struct ip_hdr *ip_header){
uint8_t tmp_ip_ver = ip_header->version;
ip_header->version = ip_header->ihl;
ip_header->ihl = tmp_ip_ver;
ip_header->total_length = ntohs(ip_header->total_length);
ip_header->fragment_offset = ntohl(ip_header->fragment_offset);
ip_header->hdr_checksum = ntohs(ip_header->hdr_checksum);
ip_header->source_address = ntohl(ip_header->source_address);
ip_header->destination_address = ntohl(ip_header->destination_address);
}