ipset-2.2.8-20051203
[iptables.git] / ipset / ipset.h
1 #ifndef __IPSET_H
2 #define __IPSET_H
3
4 /* Copyright 2000-2004 Joakim Axelsson (gozem@linux.nu)
5  *                     Patrick Schaaf (bof@bof.de)
6  *                     Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
7  *
8  * This program is free software; you can redistribute it and/or modify   
9  * it under the terms of the GNU General Public License as published by   
10  * the Free Software Foundation; either version 2 of the License, or      
11  * (at your option) any later version.                                    
12  *                                                                         
13  * This program is distributed in the hope that it will be useful,        
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of         
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
16  * GNU General Public License for more details.                           
17  *                                                                         
18  * You should have received a copy of the GNU General Public License      
19  * along with this program; if not, write to the Free Software            
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <netdb.h>
26
27 #include <linux/netfilter_ipv4/ip_set.h>
28
29 #define IPSET_LIB_NAME "/libipset_%s.so"
30 #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
31
32 #define LIST_TRIES 5
33
34 #ifdef IPSET_DEBUG
35 extern int option_debug;
36 #define DP(format, args...) if (option_debug)                   \
37         do {                                                    \
38                 fprintf(stderr, "%s: %s (DBG): ", __FILE__, __FUNCTION__);\
39                 fprintf(stderr, format "\n" , ## args);                 \
40         } while (0)
41 #else
42 #define DP(format, args...)
43 #endif
44
45 /* Commands */
46 enum set_commands {
47         CMD_NONE,
48         CMD_CREATE,             /* -N */
49         CMD_DESTROY,            /* -X */
50         CMD_FLUSH,              /* -F */
51         CMD_RENAME,             /* -E */
52         CMD_SWAP,               /* -W */
53         CMD_LIST,               /* -L */
54         CMD_SAVE,               /* -S */
55         CMD_RESTORE,            /* -R */
56         CMD_ADD,                /* -A */
57         CMD_DEL,                /* -D */
58         CMD_TEST,               /* -T */
59         CMD_BIND,               /* -B */
60         CMD_UNBIND,             /* -U */
61         CMD_HELP,               /* -H */
62         CMD_VERSION,            /* -V */
63         NUMBER_OF_CMD = CMD_VERSION,
64         /* Internal commands */
65         CMD_MAX_SETS,
66         CMD_LIST_SIZE,
67         CMD_SAVE_SIZE,
68         CMD_ADT_GET,
69 };
70
71 enum exittype {
72         OTHER_PROBLEM = 1,
73         PARAMETER_PROBLEM,
74         VERSION_PROBLEM
75 };
76
77 /* The view of an ipset in userspace */
78 struct set {
79         char name[IP_SET_MAXNAMELEN];           /* Name of the set */
80         ip_set_id_t id;                         /* Unique set id */
81         ip_set_id_t index;                      /* Array index */
82         unsigned ref;                           /* References in kernel */
83         struct settype *settype;                /* Pointer to set type functions */
84 };
85
86 struct settype {
87         struct settype *next;
88
89         char typename[IP_SET_MAXNAMELEN];
90
91         int protocol_version;
92
93         /*
94          * Create set
95          */
96
97         /* Size of create data. Will be sent to kernel */
98         size_t create_size;
99
100         /* Initialize the create. */
101         void (*create_init) (void *data);
102
103         /* Function which parses command options; returns true if it ate an option */
104         int (*create_parse) (int c, char *argv[], void *data,
105                              unsigned *flags);
106
107         /* Final check; exit if not ok. */
108         void (*create_final) (void *data, unsigned int flags);
109
110         /* Pointer to list of extra command-line options for create */
111         struct option *create_opts;
112
113         /*
114          * Add/del/test IP
115          */
116
117         /* Size of data. Will be sent to kernel */
118         size_t adt_size;
119
120         /* Function which parses command options */
121         ip_set_ip_t (*adt_parser) (unsigned cmd, const char *optarg, void *data);
122
123         /*
124          * Printing
125          */
126
127         /* Size of header. */
128         size_t header_size;
129
130         /* Initialize the type-header */
131         void (*initheader) (struct set *set, const void *data);
132
133         /* Pretty print the type-header */
134         void (*printheader) (struct set *set, unsigned options);
135
136         /* Pretty print all IPs */
137         void (*printips) (struct set *set, void *data, size_t len, unsigned options);
138
139         /* Pretty print all IPs sorted */
140         void (*printips_sorted) (struct set *set, void *data, size_t len, unsigned options);
141
142         /* Print save arguments for creating the set */
143         void (*saveheader) (struct set *set, unsigned options);
144
145         /* Print save for all IPs */
146         void (*saveips) (struct set *set, void *data, size_t len, unsigned options);
147
148         /* Conver a single IP (binding) to string */
149         char * (*bindip_tostring)(struct set *set, ip_set_ip_t ip, unsigned options);
150         
151         /* Parse an IP at restoring bindings. FIXME */
152         void (*bindip_parse) (const char *str, ip_set_ip_t * ip);
153
154         /* Print usage */
155         void (*usage) (void);
156
157         /* Internal data */
158         void *header;
159         void *data;
160         unsigned int option_offset;
161         unsigned int flags;
162 };
163
164 extern void settype_register(struct settype *settype);
165
166 /* extern void unregister_settype(set_type_t *set_type); */
167
168 extern void exit_error(enum exittype status, char *msg, ...);
169
170 extern char *binding_ip_tostring(struct set *set,
171                                  ip_set_ip_t ip, unsigned options);
172 extern char *ip_tostring(ip_set_ip_t ip, unsigned options);
173 extern char *ip_tostring_numeric(ip_set_ip_t ip);
174 extern void parse_ip(const char *str, ip_set_ip_t * ip);
175 extern void parse_mask(const char *str, ip_set_ip_t * mask);
176 extern void parse_ipandmask(const char *str, ip_set_ip_t * ip,
177                             ip_set_ip_t * mask);
178 extern char *port_tostring(ip_set_ip_t port, unsigned options);
179 extern void parse_port(const char *str, ip_set_ip_t * port);
180 extern int string_to_number(const char *str, unsigned int min, unsigned int max,
181                             ip_set_ip_t *port);
182
183 extern void *ipset_malloc(size_t size);
184 extern char *ipset_strdup(const char *);
185 extern void ipset_free(void **data);
186
187 #endif  /* __IPSET_H */