-
Notifications
You must be signed in to change notification settings - Fork 0
/
netint.c
104 lines (84 loc) · 1.62 KB
/
netint.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <string.h>
#include <sys/types.h>
#include <stdint.h>
#include <arpa/inet.h>
#include "netint.h"
#define MAX_OCTET_1 0x00000000000000ff
#define MAX_OCTET_2 0x000000000000ffff
#define MAX_OCTET_3 0x0000000000ffffff
#define MAX_OCTET_4 0x00000000ffffffff
#define MAX_OCTET_5 0x000000ffffffffff
#define MAX_OCTET_6 0x0000ffffffffffff
#define MAX_OCTET_7 0x00ffffffffffffff
#define MAX_OCTET_8 0xffffffffffffffff
uint32_t data_ntohl(const void *data, size_t size)
{
uint32_t value;
void *p;
if( size > sizeof(uint32_t) ) {
size = sizeof(uint32_t);
}
value = 0;
p = (void *)&value;
memcpy(p + sizeof(uint32_t) - size, data, size);
return ntohl(value);
}
int data_htonl(void *buf, uint32_t val)
{
int len;
uint32_t nval;
len = int_max_octet(val);
nval = htonl(val);
//memcpy(buf, &nval, len);
memcpy(buf, (void *)&nval+(sizeof(nval)-len), len);
return len;
}
int int_max_octet(uint64_t value)
{
if( value <= MAX_OCTET_1 ) {
return 1;
}
else if( value <= MAX_OCTET_2 ) {
return 2;
}
else if( value <= MAX_OCTET_3 ) {
return 3;
}
else if( value <= MAX_OCTET_4 ) {
return 4;
}
/*else if( value <= MAX_OCTET_5 ) {
return 5;
}
else if( value <= MAX_OCTET_6 ) {
return 6;
}
else if( value <= MAX_OCTET_7 ) {
return 7;
}
else if( value <= MAX_OCTET_8 ) {
return 8;
}*/
return -1;
}
int byteorder(void)
{
union {
uint16_t s;
uint8_t c[sizeof(short)];
} un;
un.s = 0x0102;
// big-endian
if( un.c[0] == 1 && un.c[1] == 2 ) {
return BOBIG;
}
// little-endian
else if( un.c[0] == 2 && un.c[1] == 1 ) {
return BOLITTLE;
}
// error
else {
return BOERROR;
}
return BOERROR;
}