summaryrefslogtreecommitdiffstats
path: root/lib/ipv4.c
blob: 8488a2860b2b50f3c3f6afc921962a7107cbe459 (plain)
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
/*
 *	BIRD Library -- IPv4 Address Manipulation Functions
 *
 *	(c) 1998 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdlib.h>

#include "nest/bird.h"
#include "lib/ip.h"
#include "lib/string.h"

int
ipv4_classify(u32 a)
{
  u32 b = a >> 24U;

  if (b && b <= 0xdf)
    {
      if (b == 0x7f)
	return IADDR_HOST | SCOPE_HOST;
      else if (b == 0x0a ||
	       (a & 0xffff0000) == 0xc0a80000 ||
	       (a & 0xfff00000) == 0xac100000)
	return IADDR_HOST | SCOPE_SITE;
      else
	return IADDR_HOST | SCOPE_UNIVERSE;
    }
  if (b >= 0xe0 && b <= 0xef)
    return IADDR_MULTICAST | SCOPE_UNIVERSE;
  if (a == 0xffffffff)
    return IADDR_BROADCAST | SCOPE_LINK;
  return IADDR_INVALID;
}

char *
ip_ntop(ip_addr a, char *b)
{
  u32 x = _I(a);

  return b + bsprintf(b, "%d.%d.%d.%d",
		      ((x >> 24) & 0xff),
		      ((x >> 16) & 0xff),
		      ((x >> 8) & 0xff),
		      (x & 0xff));
}

char *
ip_ntox(ip_addr a, char *b)
{
  return b + bsprintf(b, "%08x", _I(a));
}

u32
ipv4_class_mask(u32 a)
{
	u32 m;

	if (a < 0x80000000)
		m = 0xff000000;
	if (a < 0xc0000000)
		m = 0xffff0000;
	else
		m = 0xffffff00;
	while (a & ~m)
		m |= m >> 1;
	return m;
}

int
ip_pton(char *a, ip_addr *o)
{
  int i;
  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 = ipa_from_u32(ia);
  return 1;
}

byte *
ipv4_skip_header(byte *pkt, int *len)
{
  int l = *len;
  int q;

  if (l < 20 || (*pkt & 0xf0) != 0x40)
    return NULL;
  q = (*pkt & 0x0f) * 4;
  if (q > l)
    return NULL;
  *len -= q;
  return pkt + q;
}