1 | /*************************************** 2 | $Revision: 1.5 $ 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_PREFSTR_MAX 64 73 | #define IP_RANGSTR_MAX 128 74 | 75 | /*+ 76 | IP expansion mode - for use with t2b functions, they control 77 | whether the input is supposed to be fully expanded or contain shortcuts 78 | (eg. enabling saying 0/0 instead 0.0.0.0/0) 79 | +*/ 80 | typedef enum { 81 | IP_PLAIN = 1, 82 | IP_EXPN 83 | } ip_exp_t; 84 | 85 | /* prototypes */ 86 | 87 | er_ret_t IP_addr_t2b(ip_addr_t *ipptr, char *addr, ip_exp_t expf); 88 | er_ret_t IP_pref_t2b(ip_prefix_t *prefptr, char *prefstr, ip_exp_t expf); 89 | er_ret_t IP_rang_t2b(ip_range_t *rangptr, char *rangstr, ip_exp_t expf); 90 | 91 | /* convenience (or call it backward compatibility) macros */ 92 | 93 | #define IP_addr_e2b(a,b) IP_addr_t2b(a,b,IP_PLAIN) 94 | #define IP_pref_e2b(a,b) IP_pref_t2b(a,b,IP_PLAIN) 95 | #define IP_rang_e2b(a,b) IP_rang_t2b(a,b,IP_PLAIN) 96 | 97 | #define IP_addr_a2b(a,b) IP_addr_t2b(a,b,IP_EXPN) 98 | #define IP_pref_a2b(a,b) IP_pref_t2b(a,b,IP_EXPN) 99 | #define IP_rang_a2b(a,b) IP_rang_t2b(a,b,IP_EXPN) 100 | 101 | er_ret_t IP_addr_b2a(ip_addr_t *binaddr, char *ascaddr, int strmax ); 102 | er_ret_t IP_pref_b2a(ip_prefix_t *prefptr, char *ascaddr, int strmax); 103 | er_ret_t IP_rang_b2a(ip_range_t *rangptr, char *ascaddr, int strmax); 104 | 105 | int IP_addr_bit_get(ip_addr_t *binaddr, int bitnum); 106 | void IP_addr_bit_set(ip_addr_t *binaddr, int bitnum, int bitval); 107 | int IP_addr_cmp(ip_addr_t *ptra, ip_addr_t *ptrb, int len); 108 | int IP_sizebits(ip_space_t spc_id); 109 | void IP_pref_bit_fix( ip_prefix_t *prefix ); 110 | 111 | er_ret_t IP_smart_conv(char *key, int justcheck, int encomp, 112 | GList **preflist, ip_exp_t expf); 113 | #endif /* _IP_H */