]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/ip.c
2 * BIRD Library -- IP address functions
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 * BIRD uses its own abstraction of IP address in order to share the same
13 * code for both IPv4 and IPv6. IP addresses are represented as entities
14 * of type &ip_addr which are never to be treated as numbers and instead
15 * they must be manipulated using the following functions and macros.
20 #include "nest/bird.h"
25 ip6_compare(ip6_addr a
, ip6_addr b
)
29 if (a
.addr
[i
] > b
.addr
[i
])
31 else if (a
.addr
[i
] < b
.addr
[i
])
53 a
.addr
[i
] = u32_mkmask(n
);
62 ip6_masklen(ip6_addr
*a
)
66 for (i
=0, n
=0; i
<4; i
++, n
+=32)
67 if (a
->addr
[i
] != ~0U)
69 j
= u32_masklen(a
->addr
[i
]);
83 ip4_classify(ip4_addr ad
)
91 return IADDR_HOST
| SCOPE_HOST
;
92 else if ((b
== 0x0a) ||
93 ((a
& 0xffff0000) == 0xc0a80000) ||
94 ((a
& 0xfff00000) == 0xac100000))
95 return IADDR_HOST
| SCOPE_SITE
;
97 return IADDR_HOST
| SCOPE_UNIVERSE
;
100 if (b
>= 0xe0 && b
<= 0xef)
101 return IADDR_MULTICAST
| SCOPE_UNIVERSE
;
104 return IADDR_BROADCAST
| SCOPE_LINK
;
106 return IADDR_INVALID
;
110 ip6_classify(ip6_addr
*a
)
114 if ((x
& 0xe0000000) == 0x20000000) /* 2000::/3 Aggregatable Global Unicast Address */
115 return IADDR_HOST
| SCOPE_UNIVERSE
;
116 if ((x
& 0xffc00000) == 0xfe800000) /* fe80::/10 Link-Local Address */
117 return IADDR_HOST
| SCOPE_LINK
;
118 if ((x
& 0xffc00000) == 0xfec00000) /* fec0::/10 Site-Local Address */
119 return IADDR_HOST
| SCOPE_SITE
;
120 if ((x
& 0xfe000000) == 0xfc000000) /* fc00::/7 Unique Local Unicast Address (RFC 4193) */
121 return IADDR_HOST
| SCOPE_SITE
;
122 if ((x
& 0xff000000) == 0xff000000) /* ff00::/8 Multicast Address */
124 uint scope
= (x
>> 16) & 0x0f;
127 case 1: return IADDR_MULTICAST
| SCOPE_HOST
;
128 case 2: return IADDR_MULTICAST
| SCOPE_LINK
;
129 case 5: return IADDR_MULTICAST
| SCOPE_SITE
;
130 case 8: return IADDR_MULTICAST
| SCOPE_ORGANIZATION
;
131 case 14: return IADDR_MULTICAST
| SCOPE_UNIVERSE
;
132 default: return IADDR_MULTICAST
| SCOPE_UNDEFINED
;
136 if (!x
&& !a
->addr
[1])
141 if (a2
== 0 && a3
== 1)
142 return IADDR_HOST
| SCOPE_HOST
; /* Loopback address */
144 return ip4_classify(_MI4(a3
)); /* IPv4 compatible addresses */
146 return ip4_classify(_MI4(a3
)); /* IPv4 mapped addresses */
148 return IADDR_INVALID
;
151 return IADDR_HOST
| SCOPE_UNDEFINED
;
157 * Conversion of IPv6 address to presentation format and vice versa.
158 * Heavily inspired by routines written by Paul Vixie for the BIND project
159 * and of course by RFC 2373.
164 ip4_ntop(ip4_addr a
, char *b
)
167 return b
+ bsprintf(b
, "%d.%d.%d.%d", (x
>> 24) & 0xff, (x
>> 16) & 0xff, (x
>> 8) & 0xff, x
& 0xff);
172 ip6_ntop(ip6_addr a
, char *b
)
175 int bestpos
, bestlen
, curpos
, curlen
, i
;
177 /* First of all, preprocess the address and find the longest run of zeros */
178 bestlen
= bestpos
= curpos
= curlen
= 0;
182 words
[i
] = ((i
%2) ? x
: (x
>> 16)) & 0xffff;
190 if (curlen
> bestlen
)
201 /* Is it an encapsulated IPv4 address? */
202 if (!bestpos
&& ((bestlen
== 5 && a
.addr
[2] == 0xffff) || (bestlen
== 6)))
205 b
+= bsprintf(b
, "::%s%d.%d.%d.%d",
206 a
.addr
[2] ? "ffff:" : "",
214 /* Normal IPv6 formatting, compress the largest sequence of zeros */
228 b
+= bsprintf(b
, "%x", words
[i
]);
236 ip4_pton(const char *a
, ip4_addr
*o
)
245 char *d
, *c
= strchr(a
, '.');
248 l
= strtoul(a
, &d
, 10);
249 if (((d
!= c
) && *d
) || (l
> 255))
256 *o
= ip4_from_u32(ia
);
261 ip6_pton(const char *a
, ip6_addr
*o
)
264 int i
, j
, k
, l
, hfil
;
267 if (a
[0] == ':') /* Leading :: */
278 if (*a
== ':') /* :: */
293 if (*a
>= '0' && *a
<= '9')
295 else if (*a
>= 'A' && *a
<= 'F')
297 else if (*a
>= 'a' && *a
<= 'f')
303 if (j
>= 0x10000 || ++l
> 4)
307 if (*a
== ':' && a
[1])
309 else if (*a
== '.' && (i
== 6 || (i
< 6 && hfil
>= 0)))
310 { /* Embedded IPv4 address */
312 if (!ip4_pton(start
, &x
))
314 words
[i
++] = _I(x
) >> 16;
327 /* Replace :: with an appropriate number of zeros */
331 for (i
=7; i
-j
>= hfil
; i
--)
332 words
[i
] = words
[i
-j
];
337 /* Convert the address to ip6_addr format */
339 o
->addr
[i
] = (words
[2*i
] << 16) | words
[2*i
+1];
346 * ip_scope_text - get textual representation of address scope
347 * @scope: scope (%SCOPE_xxx)
349 * Returns a pointer to a textual name of the scope given.
352 ip_scope_text(uint scope
)
354 static char *scope_table
[] = { "host", "link", "site", "org", "univ", "undef" };
356 if (scope
> SCOPE_UNDEFINED
)
359 return scope_table
[scope
];
363 ip4_class_mask(ip4_addr ad
)
369 else if (a
< 0x80000000)
371 else if (a
< 0xc0000000)
383 * ipa_equal - compare two IP addresses for equality
387 * ipa_equal() returns 1 if @x and @y represent the same IP address, else 0.
389 int ipa_equal(ip_addr x
, ip_addr y
) { DUMMY
}
392 * ipa_nonzero - test if an IP address is defined
395 * ipa_nonzero returns 1 if @x is a defined IP address (not all bits are zero),
398 * The undefined all-zero address is reachable as a |IPA_NONE| macro.
400 int ipa_nonzero(ip_addr x
) { DUMMY
}
403 * ipa_and - compute bitwise and of two IP addresses
407 * This function returns a bitwise and of @x and @y. It's primarily
408 * used for network masking.
410 ip_addr
ipa_and(ip_addr x
, ip_addr y
) { DUMMY
}
413 * ipa_or - compute bitwise or of two IP addresses
417 * This function returns a bitwise or of @x and @y.
419 ip_addr
ipa_or(ip_addr x
, ip_addr y
) { DUMMY
}
422 * ipa_xor - compute bitwise xor of two IP addresses
426 * This function returns a bitwise xor of @x and @y.
428 ip_addr
ipa_xor(ip_addr x
, ip_addr y
) { DUMMY
}
431 * ipa_not - compute bitwise negation of two IP addresses
434 * This function returns a bitwise negation of @x.
436 ip_addr
ipa_not(ip_addr x
) { DUMMY
}
439 * ipa_mkmask - create a netmask
442 * This function returns an &ip_addr corresponding of a netmask
443 * of an address prefix of size @x.
445 ip_addr
ipa_mkmask(int x
) { DUMMY
}
448 * ipa_masklen - calculate netmask length
451 * This function checks whether @x represents a valid netmask and
452 * returns the size of the associate network prefix or -1 for invalid
455 int ipa_masklen(ip_addr x
) { DUMMY
}
458 * ipa_hash - hash IP addresses
461 * ipa_hash() returns a 16-bit hash value of the IP address @x.
463 int ipa_hash(ip_addr x
) { DUMMY
}
466 * ipa_hton - convert IP address to network order
469 * Converts the IP address @x to the network byte order.
471 * Beware, this is a macro and it alters the argument!
473 void ipa_hton(ip_addr x
) { DUMMY
}
476 * ipa_ntoh - convert IP address to host order
479 * Converts the IP address @x from the network byte order.
481 * Beware, this is a macro and it alters the argument!
483 void ipa_ntoh(ip_addr x
) { DUMMY
}
486 * ipa_classify - classify an IP address
489 * ipa_classify() returns an address class of @x, that is a bitwise or
490 * of address type (%IADDR_INVALID, %IADDR_HOST, %IADDR_BROADCAST, %IADDR_MULTICAST)
491 * with address scope (%SCOPE_HOST to %SCOPE_UNIVERSE) or -1 (%IADDR_INVALID)
492 * for an invalid address.
494 int ipa_classify(ip_addr x
) { DUMMY
}
497 * ip4_class_mask - guess netmask according to address class
500 * This function (available in IPv4 version only) returns a
501 * network mask according to the address class of @x. Although
502 * classful addressing is nowadays obsolete, there still live
503 * routing protocols transferring no prefix lengths nor netmasks
504 * and this function could be useful to them.
506 ip4_addr
ip4_class_mask(ip4_addr x
) { DUMMY
}
509 * ipa_from_u32 - convert IPv4 address to an integer
512 * This function takes an IPv4 address and returns its numeric
515 u32
ipa_from_u32(ip_addr x
) { DUMMY
}
518 * ipa_to_u32 - convert integer to IPv4 address
519 * @x: a 32-bit integer
521 * ipa_to_u32() takes a numeric representation of an IPv4 address
522 * and converts it to the corresponding &ip_addr.
524 ip_addr
ipa_to_u32(u32 x
) { DUMMY
}
527 * ipa_compare - compare two IP addresses for order
531 * The ipa_compare() function takes two IP addresses and returns
532 * -1 if @x is less than @y in canonical ordering (lexicographical
533 * order of the bit strings), 1 if @x is greater than @y and 0
534 * if they are the same.
536 int ipa_compare(ip_addr x
, ip_addr y
) { DUMMY
}
539 * ipa_build6 - build an IPv6 address from parts
545 * ipa_build() takes @a1 to @a4 and assembles them to a single IPv6
546 * address. It's used for example when a protocol wants to bind its
547 * socket to a hard-wired multicast address.
549 ip_addr
ipa_build6(u32 a1
, u32 a2
, u32 a3
, u32 a4
) { DUMMY
}
552 * ip_ntop - convert IP address to textual representation
554 * @buf: buffer of size at least %STD_ADDRESS_P_LENGTH
556 * This function takes an IP address and creates its textual
557 * representation for presenting to the user.
559 char *ip_ntop(ip_addr a
, char *buf
) { DUMMY
}
562 * ip_ntox - convert IP address to hexadecimal representation
564 * @buf: buffer of size at least %STD_ADDRESS_P_LENGTH
566 * This function takes an IP address and creates its hexadecimal
567 * textual representation. Primary use: debugging dumps.
569 char *ip_ntox(ip_addr a
, char *buf
) { DUMMY
}
572 * ip_pton - parse textual representation of IP address
573 * @a: textual representation
574 * @o: where to put the resulting address
576 * This function parses a textual IP address representation and
577 * stores the decoded address to a variable pointed to by @o.
578 * Returns 0 if a parse error has occurred, else 0.
580 int ip_pton(char *a
, ip_addr
*o
) { DUMMY
}