1 | /***************************************
2 | $Revision: 1.6 $
3 |
4 | IP handling (ip). iproutines.h - header file for conversions routines.
5 | defines data structures for IP module.
6 |
7 | Status: NOT REVUED, TESTED
8 |
9 | Design and implementation by: Marek Bukowy
10 |
11 | ******************/ /******************
12 | Copyright (c) 1999 RIPE NCC
13 |
14 | All Rights Reserved
15 |
16 | Permission to use, copy, modify, and distribute this software and its
17 | documentation for any purpose and without fee is hereby granted,
18 | provided that the above copyright notice appear in all copies and that
19 | both that copyright notice and this permission notice appear in
20 | supporting documentation, and that the name of the author not be
21 | used in advertising or publicity pertaining to distribution of the
22 | software without specific, written prior permission.
23 |
24 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
26 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
27 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 | ***************************************/
31 |
32 |
33 | #ifndef _IP_H
34 | #define _IP_H
35 |
36 | #include <glib.h>
37 | #include <erroutines.h>
38 |
39 | /*+ the space type +*/
40 | typedef enum {
41 | IP_V4 = 1,
42 | IP_V6,
43 | } ip_space_t;
44 |
45 | /*+ address structure +*/
46 | typedef struct {
47 | unsigned int words[4]; /*+ 32/128 bit ip addr. SUBJECT TO CHANGE +*/
48 | char space; /*+ char is shorter than ip_space_t but still compatible +*/
49 | } ip_addr_t;
50 |
51 | /*+ prefix structure +*/
52 | typedef struct {
53 | unsigned bits; /*+ length in bits. +*/
54 | ip_addr_t ip; /*+ the IP of the prefix +*/
55 | } ip_prefix_t;
56 |
57 | /*+ range structure +*/
58 | typedef struct {
59 | ip_addr_t begin; /*+ IP where the range begins. +*/
60 | ip_addr_t end; /*+ IP where it ends +*/
61 | } ip_range_t;
62 |
63 | /*+
64 | stores size/span of an allocation
65 | SUBJECT TO CHANGE: will be bigger for IPv6
66 | +*/
67 | typedef unsigned int ip_rangesize_t;
68 |
69 | /*+ the length of a string that should be able to hold a prefix / range
70 | when used with b2a functions.
71 | +*/
72 | #define IP_ADDRSTR_MAX 20 /* XXX watch out for IPv6 !! */
73 | #define IP_PREFSTR_MAX 24
74 | #define IP_RANGSTR_MAX 48
75 |
76 | /*+
77 | IP expansion mode - for use with t2b functions, they control
78 | whether the input is supposed to be fully expanded or contain shortcuts
79 | (eg. enabling saying 0/0 instead 0.0.0.0/0)
80 | +*/
81 | typedef enum {
82 | IP_PLAIN = 1,
83 | IP_EXPN
84 | } ip_exp_t;
85 |
86 | /* prototypes */
87 |
88 | er_ret_t IP_addr_t2b(ip_addr_t *ipptr, char *addr, ip_exp_t expf);
89 | er_ret_t IP_pref_t2b(ip_prefix_t *prefptr, char *prefstr, ip_exp_t expf);
90 | er_ret_t IP_rang_t2b(ip_range_t *rangptr, char *rangstr, ip_exp_t expf);
91 |
92 | /* convenience (or call it backward compatibility) macros */
93 |
94 | #define IP_addr_e2b(a,b) IP_addr_t2b(a,b,IP_PLAIN)
95 | #define IP_pref_e2b(a,b) IP_pref_t2b(a,b,IP_PLAIN)
96 | #define IP_rang_e2b(a,b) IP_rang_t2b(a,b,IP_PLAIN)
97 |
98 | #define IP_addr_a2b(a,b) IP_addr_t2b(a,b,IP_EXPN)
99 | #define IP_pref_a2b(a,b) IP_pref_t2b(a,b,IP_EXPN)
100 | #define IP_rang_a2b(a,b) IP_rang_t2b(a,b,IP_EXPN)
101 |
102 | er_ret_t IP_addr_b2a(ip_addr_t *binaddr, char *ascaddr, int strmax );
103 | er_ret_t IP_pref_b2a(ip_prefix_t *prefptr, char *ascaddr, int strmax);
104 | er_ret_t IP_rang_b2a(ip_range_t *rangptr, char *ascaddr, int strmax);
105 |
106 | int IP_addr_bit_get(ip_addr_t *binaddr, int bitnum);
107 | void IP_addr_bit_set(ip_addr_t *binaddr, int bitnum, int bitval);
108 | int IP_addr_cmp(ip_addr_t *ptra, ip_addr_t *ptrb, int len);
109 | int IP_sizebits(ip_space_t spc_id);
110 | void IP_pref_bit_fix( ip_prefix_t *prefix );
111 |
112 | er_ret_t IP_smart_conv(char *key, int justcheck, int encomp,
113 | GList **preflist, ip_exp_t expf);
114 |
115 | ip_rangesize_t IP_rang_span( ip_range_t *rangptr );
116 |
117 | /*
118 | this is to define a constant struct for comparisons.
119 | */
120 | #ifdef IP_IMPL
121 | const ip_addr_t IP_ADDR_UNSPEC={{0,0,0,0},0}; /* unlikely to be real :-)
122 | as there is no space 0
123 | and natural state after
124 | initializing to 0 */
125 | #else
126 | extern ip_addr_t IP_ADDR_UNSPEC;
127 | #endif
128 |
129 | #endif /* _IP_H */