Statistics
| Branch: | Revision:

iof-bird-daemon / lib / ipv4.c @ dfeef5d8

History | View | Annotate | Download (1.45 KB)

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

    
9
#ifndef IPV6
10

    
11
#include <string.h>
12
#include <stdlib.h>
13

    
14
#include "nest/bird.h"
15
#include "lib/ip.h"
16
#include "lib/string.h"
17

    
18
int
19
ipv4_classify(u32 a)
20
{
21
  u32 b = a >> 24U;
22

    
23
  if (b && b <= 0xdf)
24
    {
25
      if (b == 0x7f)
26
        return IADDR_HOST | SCOPE_HOST;
27
      else
28
        return IADDR_HOST | SCOPE_UNIVERSE;
29
    }
30
  if (b >= 0xe0 && b <= 0xef)
31
    return IADDR_MULTICAST | SCOPE_UNIVERSE;
32
  if (a == 0xffffffff)
33
    return IADDR_BROADCAST | SCOPE_LINK;
34
  return IADDR_INVALID;
35
}
36

    
37
char *
38
ip_ntop(ip_addr a, char *b)
39
{
40
  u32 x = _I(a);
41

    
42
  return b + bsprintf(b, "%d.%d.%d.%d",
43
                      ((x >> 24) & 0xff),
44
                      ((x >> 16) & 0xff),
45
                      ((x >> 8) & 0xff),
46
                      (x & 0xff));
47
}
48

    
49
char *
50
ip_ntox(ip_addr a, char *b)
51
{
52
  return b + bsprintf(b, "%08x", _I(a));
53
}
54

    
55
u32
56
ipv4_class_mask(u32 a)
57
{
58
        u32 m;
59

    
60
        if (a < 0x80000000)
61
                m = 0xff000000;
62
        if (a < 0xc0000000)
63
                m = 0xffff0000;
64
        else
65
                m = 0xffffff00;
66
        while (a & ~m)
67
                m |= m >> 1;
68
        return m;
69
}
70

    
71
int
72
ip_pton(char *a, ip_addr *o)
73
{
74
  int i,j;
75
  unsigned long int l;
76
  u32 ia = 0;
77

    
78
  i=4;
79
  while (i--)
80
    {
81
      char *d, *c = strchr(a, '.');
82
      if (!c != !i)
83
        return 0;
84
      if (c)
85
        *c++ = 0;
86
      l = strtoul(a, &d, 10);
87
      if (d && *d || l > 255)
88
        return 0;
89
      ia = (ia << 8) | l;
90
      a = c;
91
    }
92
  *o = ipa_from_u32(ia);
93
  return 1;
94
}
95

    
96
#endif