-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_str_convert.c
69 lines (60 loc) · 1.5 KB
/
ip_str_convert.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
//STR to IP (little-endian)
static unsigned int str_to_ip(char *str) {
register unsigned int ip, octet;
register char *tail = str;
while(*tail++)
;
tail -= 2;
octet = *tail-- - '0';
octet += (*tail == '.') ? 0 : (*tail-- - '0') * 10;
octet += (*tail == '.') ? 0 : (*tail-- - '0') * 100;
tail -= 1;
ip = octet << 24;
octet = *tail-- - '0';
octet += (*tail == '.') ? 0 : (*tail-- - '0') * 10;
octet += (*tail == '.') ? 0 : (*tail-- - '0') * 100;
tail -= 1;
ip |= octet << 16;
octet = *tail-- - '0';
octet += (*tail == '.') ? 0 : (*tail-- - '0') * 10;
octet += (*tail == '.') ? 0 : (*tail-- - '0') * 100;
tail -= 1;
ip |= octet << 8;
octet = *tail-- - '0';
octet += (tail < str) ? 0 : (*tail-- - '0') * 10;
octet += (tail < str) ? 0 : (*tail - '0') * 100;
ip |= octet;
return ip;
}
//IP (little-endian) to STR
static unsigned int ip_to_str(unsigned int ip, unsigned char* buff) {
register unsigned char *tail = buff, tmp;
register unsigned int n, num, count = 0;
unsigned char octet[4];
octet[0] = ip >> 24;
octet[1] = (ip >> 16) & 255;
octet[2] = (ip >> 8) & 255;
octet[3] = ip & 255;
for(int i = 0; i < 4; i++) {
num = octet[i];
*tail = '0';
tail += !num;
count += !num;
while(num > 0) {
n = num % 10;
num /= 10;
*tail++ = n + '0';
count += 1;
}
*tail++ = '.';
}
count += 3;
tail -= 1;
*tail-- = 0;
while(buff < tail) {
tmp = *buff;
*buff++ = *tail;
*tail-- = tmp;
}
return count;
}