ipv6.c 6.49 KB
Newer Older
1
2
3
/*
 *	BIRD Library -- IPv6 Address Manipulation Functions
 *
4
 *	(c) 1999 Martin Mares <mj@ucw.cz>
5
6
7
8
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

9
10
#include <stdlib.h>

11
12
#include "nest/bird.h"
#include "lib/ip.h"
13
14
#include "lib/bitops.h"
#include "lib/endian.h"
15
#include "lib/string.h"
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

/*
 *  See RFC 2373 for explanation of IPv6 addressing issues.
 */

ip_addr
ipv6_mkmask(unsigned n)
{
  ip_addr a;
  int i;

  for(i=0; i<4; i++)
    {
      if (!n)
	a.addr[i] = 0;
      else if (n >= 32)
	{
	  a.addr[i] = ~0;
	  n -= 32;
	}
      else
	{
	  a.addr[i] = u32_mkmask(n);
	  n = 0;
	}
    }
  return a;
}

unsigned
ipv6_mklen(ip_addr *a)
{
  int i, j, n;

  for(i=0, n=0; i<4; i++, n+=32)
    if (a->addr[i] != ~0U)
      {
	j = u32_masklen(a->addr[i]);
	if (j < 0)
	  return j;
	n += j;
	while (++i < 4)
	  if (a->addr[i])
	    return -1;
	break;
      }
  return n;
}

int
ipv6_classify(ip_addr *a)
{
  u32 x = a->addr[0];

  if ((x & 0xe0000000) == 0x20000000)		/* Aggregatable Global Unicast Address */
    return IADDR_HOST | SCOPE_UNIVERSE;
72
  if ((x & 0xffc00000) == 0xfe800000)		/* Link-Local Address */
73
    return IADDR_HOST | SCOPE_LINK;
74
  if ((x & 0xffc00000) == 0xfec00000)		/* Site-Local Address */
75
76
77
78
79
80
81
82
83
84
85
86
87
    return IADDR_HOST | SCOPE_SITE;
  if ((x & 0xff000000) == 0xff000000)		/* Multicast Address */
    {
      unsigned int scope = (x >> 16) & 0x0f;
      switch (scope)
	{
	case 1:	 return IADDR_MULTICAST | SCOPE_HOST;
	case 2:  return IADDR_MULTICAST | SCOPE_LINK;
	case 5:  return IADDR_MULTICAST | SCOPE_SITE;
	case 8:  return IADDR_MULTICAST | SCOPE_ORGANIZATION;
	case 14: return IADDR_MULTICAST | SCOPE_UNIVERSE;
	}
    }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  if (!x && !a->addr[1] && !a->addr[2])
    {
      u32 y = a->addr[3];
      if (y == 1)
	return IADDR_HOST | SCOPE_HOST;		/* Loopback address */
      /* IPv4 compatible addresses */
      if (y >= 0x7f000000 && y < 0x80000000)
	return IADDR_HOST | SCOPE_HOST;
      if ((y & 0xff000000) == 0x0a000000 ||
	  (y & 0xffff0000) == 0xc0a80000 ||
	  (y & 0xfff00000) == 0xac100000)
	return IADDR_HOST | SCOPE_SITE;
      if (y >= 0x01000000 && y < 0xe0000000)
	return IADDR_HOST | SCOPE_UNIVERSE;
    }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  return IADDR_INVALID;
}

void
ipv6_hton(ip_addr *a)
{
  int i;

  for(i=0; i<4; i++)
    a->addr[i] = htonl(a->addr[i]);
}

void
ipv6_ntoh(ip_addr *a)
{
  int i;

  for(i=0; i<4; i++)
    a->addr[i] = ntohl(a->addr[i]);
}

int
125
ipv6_compare(ip_addr X, ip_addr Y)
126
127
{
  int i;
128
129
  ip_addr *x = &X;
  ip_addr *y = &Y;
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

  for(i=0; i<4; i++)
    if (x->addr[i] > y->addr[i])
      return 1;
    else if (x->addr[i] < y->addr[i])
      return -1;
  return 0;
}

/*
 *  Conversion of IPv6 address to presentation format and vice versa.
 *  Heavily inspired by routines written by Paul Vixie for the BIND project
 *  and of course by RFC 2373.
 */

char *
ip_ntop(ip_addr a, char *b)
{
  u16 words[8];
  int bestpos, bestlen, curpos, curlen, i;

  /* First of all, preprocess the address and find the longest run of zeros */
  bestlen = bestpos = curpos = curlen = 0;
  for(i=0; i<8; i++)
    {
      u32 x = a.addr[i/2];
      words[i] = ((i%2) ? x : (x >> 16)) & 0xffff;
      if (words[i])
	curlen = 0;
      else
	{
	  if (!curlen)
	    curpos = i;
	  curlen++;
	  if (curlen > bestlen)
	    {
	      bestpos = curpos;
	      bestlen = curlen;
	    }
	}
    }
  if (bestlen < 2)
    bestpos = -1;

  /* Is it an encapsulated IPv4 address? */
  if (!bestpos &&
      (bestlen == 5 && a.addr[2] == 0xffff ||
       bestlen == 6))
    {
      u32 x = a.addr[3];
180
181
182
183
184
185
      b += bsprintf(b, "::%s%d.%d.%d.%d",
		    a.addr[2] ? "ffff:" : "",
		    ((x >> 24) & 0xff),
		    ((x >> 16) & 0xff),
		    ((x >> 8) & 0xff),
		    (x & 0xff));
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
      return b;
    }

  /* Normal IPv6 formatting, compress the largest sequence of zeros */
  for(i=0; i<8; i++)
    {
      if (i == bestpos)
	{
	  i += bestlen - 1;
	  *b++ = ':';
	  if (i == 7)
	    *b++ = ':';
	}
      else
	{
	  if (i)
	    *b++ = ':';
203
	  b += bsprintf(b, "%x", words[i]);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	}
    }
  *b = 0;
  return b;
}

char *
ip_ntox(ip_addr a, char *b)
{
  int i;

  for(i=0; i<4; i++)
    {
      if (i)
	*b++ = '.';
219
      b += bsprintf(b, "%08x", a.addr[i]);
220
221
222
223
224
225
226
    }
  return b;
}

int
ipv4_pton_u32(char *a, u32 *o)
{
Martin Mareš's avatar
Martin Mareš committed
227
  int i;
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
  unsigned long int l;
  u32 ia = 0;

  i=4;
  while (i--)
    {
      char *d, *c = strchr(a, '.');
      if (!c != !i)
	return 0;
      l = strtoul(a, &d, 10);
      if (d != c && *d || l > 255)
	return 0;
      ia = (ia << 8) | l;
      if (c)
	c++;
      a = c;
    }
  *o = ia;
  return 1;
}

int
ip_pton(char *a, ip_addr *o)
{
  u16 words[8];
  int i, j, k, l, hfil;
  char *start;

  if (a[0] == ':')			/* Leading :: */
    {
      if (a[1] != ':')
	return 0;
      a++;
    }
  hfil = -1;
  i = 0;
  while (*a)
    {
      if (*a == ':')			/* :: */
	{
	  if (hfil >= 0)
	    return 0;
	  hfil = i;
	  a++;
	  continue;
	}
      j = 0;
      l = 0;
      start = a;
      for(;;)
	{
	  if (*a >= '0' && *a <= '9')
	    k = *a++ - '0';
	  else if (*a >= 'A' && *a <= 'F')
	    k = *a++ - 'A' + 10;
	  else if (*a >= 'a' && *a <= 'f')
	    k = *a++ - 'a' + 10;
	  else
	    break;
	  j = (j << 4) + k;
	  if (j >= 0x10000 || ++l > 4)
	    return 0;
	}
      if (*a == ':' && a[1])
	a++;
      else if (*a == '.' && (i == 6 || i < 6 && hfil >= 0))
	{				/* Embedded IPv4 address */
	  u32 x;
	  if (!ipv4_pton_u32(start, &x))
	    return 0;
	  words[i++] = x >> 16;
	  words[i++] = x;
	  break;
	}
      else if (*a)
	return 0;
      if (i >= 8)
	return 0;
      words[i++] = j;
    }

  /* Replace :: with an appropriate number of zeros */
  if (hfil >= 0)
    {
      j = 8 - i;
      for(i=7; i-j >= hfil; i--)
	words[i] = words[i-j];
      for(; i>=hfil; i--)
	words[i] = 0;
    }

  /* Convert the address to ip_addr format */
  for(i=0; i<4; i++)
    o->addr[i] = (words[2*i] << 16) | words[2*i+1];
  return 1;
}

#ifdef TEST

#include "bitops.c"

static void test(char *x)
{
  ip_addr a;
  char c[STD_ADDRESS_P_LENGTH+1];

  printf("%-40s ", x);
  if (!ip_pton(x, &a))
    {
      puts("BAD");
      return;
    }
  ip_ntop(a, c);
  printf("%-40s %04x\n", c, ipv6_classify(&a));
}

int main(void)
{
  puts("Positive tests:");
  test("1:2:3:4:5:6:7:8");
  test("dead:beef:DEAD:BEEF::f00d");
  test("::");
  test("::1");
  test("1::");
  test("::1.234.5.6");
  test("::ffff:1.234.5.6");
  test("::fffe:1.234.5.6");
  test("1:2:3:4:5:6:7::8");
  test("2080::8:800:200c:417a");
  test("ff01::101");

  puts("Negative tests:");
  test(":::");
  test("1:2:3:4:5:6:7:8:");
  test("1::2::3");
  test("::12345");
  test("::1.2.3.4:5");
  test(":1:2:3:4:5:6:7:8");
  test("g:1:2:3:4:5:6:7");
  return 0;
}
369

370
#endif