forked from cjdelisle/cjdns
/
AddrTools.h
177 lines (160 loc) · 4.45 KB
/
AddrTools.h
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef AddrTools_H
#define AddrTools_H
#include "util/Bits.h"
#include "util/Endian.h"
#include "util/Hex.h"
#include <stdint.h>
/** Takes the path in host byte order. */
static inline void AddrTools_printPath(uint8_t out[20], uint64_t path)
{
uint64_t path_be = Endian_hostToBigEndian64(path);
uint8_t bytes[16];
Hex_encode(bytes, 16, (uint8_t*) &path_be, 8);
out[ 0] = bytes[ 0];
out[ 1] = bytes[ 1];
out[ 2] = bytes[ 2];
out[ 3] = bytes[ 3];
out[ 4] = '.';
out[ 5] = bytes[ 4];
out[ 6] = bytes[ 5];
out[ 7] = bytes[ 6];
out[ 8] = bytes[ 7];
out[ 9] = '.';
out[10] = bytes[ 8];
out[11] = bytes[ 9];
out[12] = bytes[10];
out[13] = bytes[11];
out[14] = '.';
out[15] = bytes[12];
out[16] = bytes[13];
out[17] = bytes[14];
out[18] = bytes[15];
out[19] = '\0';
}
/**
* Parse out a path.
*
* @param out a pointer to a number which will be set to the path in HOST BYTE ORDER.
* @param netAddr a string representation of the path such as "0000.1111.2222.3333" in Big Endian.
* @return 0 if successful, -1 if the netAddr is malformed.
*/
static inline int AddrTools_parsePath(uint64_t* out, const uint8_t netAddr[20])
{
if (netAddr[4] != '.' || netAddr[9] != '.' || netAddr[14] != '.') {
return -1;
}
uint8_t hex[16] = {
netAddr[ 0],
netAddr[ 1],
netAddr[ 2],
netAddr[ 3],
netAddr[ 5],
netAddr[ 6],
netAddr[ 7],
netAddr[ 8],
netAddr[10],
netAddr[11],
netAddr[12],
netAddr[13],
netAddr[15],
netAddr[16],
netAddr[17],
netAddr[18]
};
uint8_t numberBytes[8];
if (Hex_decode(numberBytes, 8, hex, 16) != 8) {
return -1;
}
uint64_t out_be;
Bits_memcpyConst(&out_be, numberBytes, 8);
*out = Endian_bigEndianToHost64(out_be);
return 0;
}
static inline void AddrTools_printIp(uint8_t output[40], const uint8_t binIp[16])
{
uint8_t hex[32];
Hex_encode(hex, 32, binIp, 16);
output[ 0] = hex[ 0];
output[ 1] = hex[ 1];
output[ 2] = hex[ 2];
output[ 3] = hex[ 3];
output[ 4] = ':';
output[ 5] = hex[ 4];
output[ 6] = hex[ 5];
output[ 7] = hex[ 6];
output[ 8] = hex[ 7];
output[ 9] = ':';
output[10] = hex[ 8];
output[11] = hex[ 9];
output[12] = hex[10];
output[13] = hex[11];
output[14] = ':';
output[15] = hex[12];
output[16] = hex[13];
output[17] = hex[14];
output[18] = hex[15];
output[19] = ':';
output[20] = hex[16];
output[21] = hex[17];
output[22] = hex[18];
output[23] = hex[19];
output[24] = ':';
output[25] = hex[20];
output[26] = hex[21];
output[27] = hex[22];
output[28] = hex[23];
output[29] = ':';
output[30] = hex[24];
output[31] = hex[25];
output[32] = hex[26];
output[33] = hex[27];
output[34] = ':';
output[35] = hex[28];
output[36] = hex[29];
output[37] = hex[30];
output[38] = hex[31];
output[39] = '\0';
}
/**
* Parse out an address.
*
* @param out a pointer to a byte array which will be set to the bytes of the ipv6 address.
* @param hexAddr a string representation of the ipv6 address such as:
* "fc4f:630d:e499:8f5b:c49f:6e6b:01ae:3120".
* @return 0 if successful, -1 if the hexAddr is malformed.
*/
static inline int AddrTools_parseIp(uint8_t out[16], const uint8_t hexAddr[40])
{
for (int i = 4; i < 39; i += 5) {
if (hexAddr[i] != ':') {
return -1;
}
}
uint8_t hex[32];
int j = 0;
for (int i = 0; i < 40; i++) {
hex[j++] = hexAddr[i++];
hex[j++] = hexAddr[i++];
hex[j++] = hexAddr[i++];
hex[j++] = hexAddr[i++];
}
if (Hex_decode(out, 16, hex, 32) != 16) {
return -1;
}
return 0;
}
#endif