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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
* Marek Senderski <msend@o2.pl>
* changes: Krzysztof Wnuk <keczi@poczta.onet.pl>
* Michal Kowalczuk <michal@kowalczuk.eu>
*
* released under GNU GPL v2 only licence
*
* some of those functions are taken form GNU libc6 library
*
* $Id: addrpack.c,v 1.12 2008-10-12 11:28:21 thomson Exp $
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef WIN32
#include <winsock2.h>
#endif
#ifdef LINUX
#include <netinet/in.h>
#endif
#include "stdint.h"
void print_packed(char addr[]);
#define NS_INADDRSZ 4 /* IPv4 T_A */
#define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
#define NS_INT16SZ 2 /* #/bytes of data in a u_int16_t */
#define u_char unsigned char
#define u_int unsigned int
static int inet_pton4(const char * src, char * dst)
{
int saw_digit, octets, ch;
u_char tmp[NS_INADDRSZ], *tp;
saw_digit = 0;
octets = 0;
*(tp = tmp) = 0;
while ((ch = *src++) != '\0') {
if (ch >= '0' && ch <= '9') {
u_int new = *tp * 10 + (ch - '0');
if (new > 255)
return (0);
*tp = new;
if (! saw_digit) {
if (++octets > 4)
return (0);
saw_digit = 1;
}
} else if (ch == '.' && saw_digit) {
if (octets == 4)
return (0);
*++tp = 0;
saw_digit = 0;
} else
return (0);
}
if (octets < 4)
return (0);
memcpy(dst, tmp, NS_INADDRSZ);
return (1);
}
int inet_pton6(const char *src, char * dst)
{
static char xdigits[] = "0123456789abcdef";
u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
const char *curtok;
int ch, saw_xdigit;
u_int val;
memset(tmp, '\0', NS_IN6ADDRSZ);
tp = tmp;
endp = tp + NS_IN6ADDRSZ;
colonp = NULL;
/* Leading :: requires some special handling. */
if (*src == ':')
if (*++src != ':')
return (0);
curtok = src;
saw_xdigit = 0;
val = 0;
while ((ch = tolower (*src++)) != '\0') {
char * pch;
pch = strchr(xdigits, ch);
if (pch != NULL) {
val <<= 4;
val |= (pch - xdigits);
if (val > 0xffff)
return (0);
saw_xdigit = 1;
continue;
}
if (ch == ':') {
curtok = src;
if (!saw_xdigit) {
if (colonp)
return (0);
colonp = tp;
continue;
} else if (*src == '\0') {
return (0);
}
if (tp + NS_INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
saw_xdigit = 0;
val = 0;
continue;
}
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
inet_pton4(curtok, (char*)tp) > 0) {
tp += NS_INADDRSZ;
saw_xdigit = 0;
break; /* '\0' was seen by inet_pton4(). */
}
return (0);
}
if (saw_xdigit) {
if (tp + NS_INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
}
if (colonp != NULL) {
/*
* Since some memmove()'s erroneously fail to handle
* overlapping regions, we'll do the shift by hand.
*/
const int n = tp - colonp;
int i;
if (tp == endp)
return (0);
for (i = 1; i <= n; i++) {
endp[- i] = colonp[n - i];
colonp[n - i] = 0;
}
tp = endp;
}
if (tp != endp)
return (0);
memcpy(dst, tmp, NS_IN6ADDRSZ);
return (1);
}
char * inet_ntop4(const char * src, char * dst)
{
char tmp[sizeof "255.255.255.255"];
sprintf(tmp,"%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
return strcpy(dst, tmp);
}
char * inet_ntop6(const unsigned char * src, char * dst)
{
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
struct { int base, len; } best, cur;
u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
int i;
best.len = cur.len = 0;
/*
* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
memset(words, '\0', sizeof words);
for (i = 0; i < NS_IN6ADDRSZ; i += 2)
words[i / 2] = (src[i] << 8) | src[i + 1];
best.base = -1;
cur.base = -1;
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
else
cur.len++;
} else {
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
cur.base = -1;
}
}
}
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
}
if (best.base != -1 && best.len < 2)
best.base = -1;
/*
* Format the result.
*/
tp = tmp;
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
/* Are we inside the best run of 0x00's? */
if (best.base != -1 && i >= best.base &&
i < (best.base + best.len)) {
if (i == best.base)
*tp++ = ':';
continue;
}
/* Are we following an initial run of 0x00s or any real hex? */
if (i != 0)
*tp++ = ':';
#if 0
/* encapsulated IPv4 addresses are no concern in Dibbler */
/* Is this address an encapsulated IPv4? */
if (i == 6 && best.base == 0 &&
(best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
if (!inet_ntop4((char*)src+12, tp))
return (NULL);
tp += strlen(tp);
break;
}
#endif
tp += sprintf(tp, "%x", words[i]);
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) ==
(NS_IN6ADDRSZ / NS_INT16SZ))
*tp++ = ':';
*tp++ = '\0';
return strcpy(dst, tmp);
}
void truncatePrefixFromConfig( char * src, char * dst, char length){
int i=0;
dst[0]=0;
for(i=0;i<length/8;i++) {
sprintf(dst + strlen(dst), "%02x", src[i]);
if (i && (i%2))
sprintf(dst+strlen(dst), ":");
}
if (i%2)
sprintf(dst+strlen(dst), "::");
else
sprintf(dst+strlen(dst), ":");
}
/**
* converts packed address to a reverse string used in DNS Updates
*
* @param src - packed address
* @param dst - output buffer (e.g. 1.2.3.0.0.0.0.0.0.0.0.0.0.e.f.f.3.ip6.arpa)
*/
void doRevDnsAddress( char * src, char * dst){
int i=0;
dst[0]=0;
for(i=15;i>=0;i--) {
sprintf(dst + strlen(dst), "%x.%x.", (src[i] & 0x0f), ( (src[i] & 0xf0 ) >> 4 ) );
}
sprintf(dst + strlen(dst), "ip6.arpa.");
}
/**
* function converts address to a DNS zone root with specified length
*
* @param src - packed address, e.g. 3ffe::123
* @param dst - dns zone root, e.g. 0.0.0.0.e.f.f.3.ip6.arpa
* @param length - zone length, e.g. 96
*/
void doRevDnsZoneRoot( char * src, char * dst, int length){
int i=0;
dst[0]=0;
i = 15 - length/8; /* skip whole bytes */
/* FIXME: what to do with prefixes which do not divide by 4? */
switch (length%8) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
sprintf(dst + strlen(dst), "%x.", (src[i]&0xf0) >> 4);
break;
case 5:
break;
case 6:
break;
case 7:
default:
break;
}
if (length%8) i--;
/* print the rest */
for(; i>=0 ; i--) {
sprintf(dst + strlen(dst), "%x.%x.", (src[i] & 0x0f), ( (src[i] & 0xf0 ) >> 4 ) );
}
sprintf(dst + strlen(dst), "ip6.arpa.");
}
void print_packed(char * addr)
{
int i=0;
for (;i<16;i++) {
printf("%02x",*(unsigned char*)(addr+i));
if ((i%2) && i<15)
printf(":");
}
printf("\n");
}
uint64_t htonll(uint64_t n) {
#if __BYTE_ORDER == __BIG_ENDIAN
return n;
#else
return (((uint64_t)htonl(n)) << 32) + htonl(n >> 32);
#endif
}
uint64_t ntohll(uint64_t n) {
#if __BYTE_ORDER == __BIG_ENDIAN
return n;
#else
return (((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32);
#endif
}
|