1 | /*************************************** 2 | $Revision: 1.14 $ 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 | #include <sys/types.h> 40 | 41 | /*+ the key type (for ascii keys - tells what it was before it was 42 | converted into prefixes in smart_conv() +*/ 43 | 44 | typedef enum { 45 | IPK_UNDEF = 0, 46 | IPK_RANGE, 47 | IPK_PREFIX, 48 | IPK_IP 49 | } ip_keytype_t; 50 | 51 | /*+ the space type +*/ 52 | typedef enum { 53 | IP_V4 = 1, 54 | IP_V6 55 | } ip_space_t; 56 | 57 | 58 | typedef unsigned int ip_limb_t; /* the limb must be at least 32 bit wide */ 59 | typedef uint64_t ip_v6word_t; 60 | /* should use 64bit for ipv6: 61 | u_int64_t (bsd,linux) 62 | uint64_t (solaris) 63 | */ 64 | #define IPLIMBNUM (16/sizeof(ip_limb_t)) 65 | 66 | /*+ address structure +*/ 67 | typedef struct { 68 | ip_limb_t words[IPLIMBNUM]; /*+ 32/128 bit ip addr. SUBJECT TO CHANGE +*/ 69 | 70 | ip_space_t space; /*+ MUST NOT BE char ! prefixes are compared with 71 | memcmp, so there may be absolutely no unitialised 72 | bytes +*/ 73 | } ip_addr_internal_t; 74 | 75 | /*+ prefix structure +*/ 76 | typedef struct { 77 | unsigned bits; /*+ length in bits. +*/ 78 | ip_addr_internal_t ip; /*+ the IP of the prefix +*/ 79 | } ip_prefix_internal_t; 80 | 81 | /*+ range structure +*/ 82 | typedef struct { 83 | ip_addr_internal_t begin; /*+ IP where the range begins. +*/ 84 | ip_addr_internal_t end; /*+ IP where it ends +*/ 85 | } ip_range_internal_t; 86 | 87 | #if 0/* #ifndef IP_IMPL -- set this to see accesses to structure members */ 88 | /* hide the internals */ 89 | typedef struct {char a[sizeof(ip_addr_internal_t)];} ip_addr_t; 90 | typedef struct {char a[sizeof(ip_range_internal_t)];} ip_range_t; 91 | typedef struct {char a[sizeof(ip_prefix_internal_t)];} ip_prefix_t; 92 | #else 93 | typedef ip_addr_internal_t ip_addr_t; 94 | typedef ip_range_internal_t ip_range_t; 95 | typedef ip_prefix_internal_t ip_prefix_t; 96 | #endif 97 | 98 | 99 | /*+ 100 | stores size/span of an allocation 101 | SUBJECT TO CHANGE: will be bigger for IPv6 102 | +*/ 103 | typedef unsigned int ip_rangesize_t; 104 | 105 | /*+ the length of a string that should be able to hold a prefix / range 106 | when used with b2a functions. 107 | +*/ 108 | #define IP_ADDRSTR_MAX 20 /* XXX watch out for IPv6 !! */ 109 | #define IP_PREFSTR_MAX 24 110 | #define IP_RANGSTR_MAX 48 111 | 112 | /*+ 113 | IP expansion mode - for use with t2b functions, they control 114 | whether the input is supposed to be fully expanded or contain shortcuts 115 | (eg. enabling saying 0/0 instead 0.0.0.0/0) 116 | +*/ 117 | typedef enum { 118 | IP_PLAIN = 1, 119 | IP_EXPN 120 | } ip_exp_t; 121 | 122 | /* prototypes */ 123 | /* text to binary */ 124 | er_ret_t IP_addr_t2b(ip_addr_t *ipptr, char *addr, ip_exp_t expf); 125 | er_ret_t IP_pref_t2b(ip_prefix_t *prefptr, char *prefstr, ip_exp_t expf); 126 | er_ret_t IP_rang_t2b(ip_range_t *rangptr, char *rangstr, ip_exp_t expf); 127 | er_ret_t IP_revd_t2b(ip_prefix_t *prefptr, char *prefstr, ip_exp_t expf); 128 | /* convenience (or call it backward compatibility) macros */ 129 | 130 | #define IP_addr_e2b(a,b) IP_addr_t2b(a,b,IP_PLAIN) 131 | #define IP_pref_e2b(a,b) IP_pref_t2b(a,b,IP_PLAIN) 132 | #define IP_rang_e2b(a,b) IP_rang_t2b(a,b,IP_PLAIN) 133 | #define IP_revd_e2b(a,b) IP_revd_t2b(a,b,IP_PLAIN) 134 | 135 | #define IP_addr_a2b(a,b) IP_addr_t2b(a,b,IP_EXPN) 136 | #define IP_pref_a2b(a,b) IP_pref_t2b(a,b,IP_EXPN) 137 | #define IP_rang_a2b(a,b) IP_rang_t2b(a,b,IP_EXPN) 138 | #define IP_revd_a2b(a,b) IP_revd_t2b(a,b,IP_EXPN) 139 | 140 | /* text fragments to binary */ 141 | er_ret_t IP_addr_f2b_v4(ip_addr_t *addrptr, char *adrstr); 142 | er_ret_t IP_rang_f2b_v4(ip_range_t *rangptr, char *beginstr, char *endstr); 143 | er_ret_t IP_pref_f2b_v4(ip_prefix_t *prefptr, char *prefixstr, 144 | char *lengthstr); 145 | er_ret_t IP_addr_f2b_v6(ip_addr_t *addrptr, char *msbstr, char *lsbstr ); 146 | er_ret_t IP_pref_f2b_v6(ip_prefix_t *prefptr, char *msbstr, char *lsbstr, 147 | char *lengthstr); 148 | 149 | er_ret_t IP_addr_b2a(ip_addr_t *binaddr, char *ascaddr, int strmax ); 150 | er_ret_t IP_pref_b2a(ip_prefix_t *prefptr, char *ascaddr, int strmax); 151 | er_ret_t IP_rang_b2a(ip_range_t *rangptr, char *ascaddr, int strmax); 152 | er_ret_t IP_rang_classful(ip_range_t *rangptr, ip_addr_t *addrptr); 153 | er_ret_t IP_pref_2_rang( ip_range_t *rangptr, ip_prefix_t *prefptr ); 154 | 155 | /* utility functions: testers/converters */ 156 | int IP_addr_bit_get(ip_addr_t *binaddr, int bitnum); 157 | void IP_addr_bit_set(ip_addr_t *binaddr, int bitnum, int bitval); 158 | int IP_addr_cmp(ip_addr_t *ptra, ip_addr_t *ptrb, int len); 159 | int IP_sizebits(ip_space_t spc_id); 160 | void IP_pref_bit_fix( ip_prefix_t *prefix ); 161 | int IP_addr_in_pref(ip_addr_t *ptra, ip_prefix_t *prefix); 162 | int IP_addr_in_rang(ip_addr_t *ptra, ip_range_t *rangptr); 163 | er_ret_t IP_smart_conv(char *key, int justcheck, int encomp, 164 | GList **preflist, ip_exp_t expf, ip_keytype_t *keytype); 165 | er_ret_t IP_smart_range(char *key, ip_range_t *rangptr, ip_exp_t expf, 166 | ip_keytype_t *keytype); 167 | 168 | 169 | ip_rangesize_t IP_rang_span( ip_range_t *rangptr ); 170 | er_ret_t IP_addr_s2b(ip_addr_t *addrptr, void *addr_in, int addr_len); 171 | 172 | /* accessor functions */ 173 | unsigned IP_addr_b2_space(ip_addr_t *addrptr); 174 | unsigned IP_pref_b2_space(ip_prefix_t *prefix); 175 | unsigned IP_rang_b2_space(ip_range_t *myrang); 176 | 177 | unsigned IP_addr_b2v4_addr(ip_addr_t *addrptr); 178 | ip_v6word_t IP_addr_b2v6_hi(ip_addr_t *addrptr); 179 | ip_v6word_t IP_addr_b2v6_lo(ip_addr_t *addrptr); 180 | 181 | unsigned IP_pref_b2_space(ip_prefix_t *prefix); 182 | unsigned IP_pref_b2_len(ip_prefix_t *prefix); 183 | #define IP_pref_b2v4_len(prefix) IP_pref_b2_len(prefix) 184 | #define IP_pref_b2v6_len(prefix) IP_pref_b2_len(prefix) 185 | 186 | unsigned IP_pref_b2v4_addr(ip_prefix_t *prefix); 187 | void IP_addr_b2v4(ip_addr_t *addrptr, unsigned *address); 188 | void IP_pref_b2v4(ip_prefix_t *prefptr, 189 | unsigned int *prefix, 190 | unsigned int *prefix_length); 191 | #define IP_revd_b2v4(a,b,c) IP_pref_b2v4(a,b,c) 192 | void IP_pref_b2v6(ip_prefix_t *prefptr, 193 | ip_v6word_t *high, 194 | ip_v6word_t *low, 195 | unsigned int *prefix_length); 196 | #define IP_revd_b2v6(a,b,c,d) IP_pref_b2v6(a,b,c,d) 197 | void IP_rang_b2v4(ip_range_t *myrang, 198 | unsigned *begin, 199 | unsigned *end); 200 | 201 | /******** constructing from raw values **********/ 202 | er_ret_t IP_addr_v4_mk(ip_addr_t *addrptr, unsigned addrval); 203 | er_ret_t IP_addr_v6_mk(ip_addr_t *addrptr, 204 | ip_v6word_t high, ip_v6word_t low); 205 | er_ret_t IP_pref_v4_mk(ip_prefix_t *prefix, 206 | unsigned prefval, unsigned preflen); 207 | er_ret_t IP_rang_v4_mk(ip_range_t *rangptr, 208 | unsigned addrbegin, unsigned addrend); 209 | /* a2v4 functions to convert the ascii to binary, and 210 | then set the raw values at the pointers provided. */ 211 | er_ret_t IP_pref_a2v4(char *avalue, ip_prefix_t *pref, 212 | unsigned *prefix, unsigned *prefix_length); 213 | er_ret_t IP_pref_a2v6(char *avalue, ip_prefix_t *pref, 214 | ip_v6word_t *high, ip_v6word_t *low, 215 | unsigned *prefix_length); 216 | er_ret_t IP_revd_a2v4(char *avalue, ip_prefix_t *pref, 217 | unsigned int *prefix, unsigned int *prefix_length); 218 | er_ret_t IP_addr_a2v4(char *avalue,ip_addr_t *ipaddr, unsigned int *address); 219 | er_ret_t IP_rang_a2v4(char *rangstr, ip_range_t *myrang, 220 | unsigned int *begin_in, unsigned int *end_in); 221 | 222 | /* decompose/find encompasssing prefix */ 223 | void IP_rang_encomp(ip_range_t *rangptr); 224 | unsigned IP_rang_decomp(ip_range_t *rangptr, GList **preflist); 225 | 226 | /* 227 | this is to define a constant struct for comparisons. 228 | */ 229 | #ifdef IP_IMPL 230 | const ip_addr_t IP_ADDR_UNSPEC={{0,0,0,0},0}; /* unlikely to be real :-) 231 | as there is no space 0 232 | bonus: it's a natural state after 233 | initializing to 0 */ 234 | #else 235 | extern ip_addr_t IP_ADDR_UNSPEC; 236 | #endif 237 | 238 | #endif /* _IP_H */