k/permissions/hash.h

00001 /*
00002  * $Id$
00003  *
00004  * Header file for trusted and address hash table functions
00005  *
00006  * Copyright (C) 2003-2006 Juha Heinanen
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #ifndef _PERM_HASH_H_
00026 #define _PERM_HASH_H_
00027 
00028 #include <stdio.h>
00029 #include "../../parser/msg_parser.h"
00030 #include "../../str.h"
00031 #include "../../usr_avp.h"
00032 #include "../../lib/kmi/mi.h"
00033 
00034 #define PERM_HASH_SIZE 128
00035 
00036 /*
00037  * Structure stored in trusted hash table
00038  */
00039 struct trusted_list {
00040         str src_ip;                 /* Source IP of SIP message */
00041         int proto;                  /* Protocol -- UDP, TCP, TLS, or SCTP */
00042         char *pattern;              /* Pattern matching From header field */
00043         str tag;                    /* Tag to be assigned to AVP */
00044         struct trusted_list *next;  /* Next element in the list */
00045 };
00046 
00047 
00048 /*
00049  * Parse and init tag avp specification
00050  */
00051 int init_tag_avp(str *tag_avp_param);
00052 
00053 
00054 /*
00055  * Gets tag avp specs
00056  */
00057 void get_tag_avp(int_str *tag_avp_p, int *tag_avp_type_p);
00058 
00059 
00060 /*
00061  * Create and initialize a hash table
00062  */
00063 struct trusted_list** new_hash_table(void);
00064 
00065 
00066 /*
00067  * Release all memory allocated for a hash table
00068  */
00069 void free_hash_table(struct trusted_list** table);
00070 
00071 
00072 /*
00073  * Destroy a hash table
00074  */
00075 void destroy_hash_table(struct trusted_list** table);
00076 
00077 
00078 /* 
00079  * Add <src_ip, proto, pattern> into hash table, where proto is integer
00080  * representation of string argument proto.
00081  */
00082 int hash_table_insert(struct trusted_list** hash_table, char* src_ip,
00083                       char* proto, char* pattern, char* tag);
00084 
00085 
00086 /* 
00087  * Check if an entry exists in hash table that has given src_ip and protocol
00088  * value and pattern that matches to From URI.
00089  */
00090 int match_hash_table(struct trusted_list** table, struct sip_msg* msg,
00091                      char *scr_ip, int proto);
00092 
00093 
00094 /* 
00095  * Print entries stored in hash table 
00096  */
00097 void hash_table_print(struct trusted_list** hash_table, FILE* reply_file);
00098 int hash_table_mi_print(struct trusted_list **hash_table, struct mi_node* rpl);
00099 
00100 /* 
00101  * Empty hash table
00102  */
00103 void empty_hash_table(struct trusted_list** hash_table);
00104 
00105 
00106 /*
00107  * Structure stored in address hash table
00108  */
00109 struct addr_list {
00110     unsigned int grp;
00111     ip_addr_t addr;
00112     unsigned int port;
00113         str tag;
00114     struct addr_list *next;  /* Next element in the list */
00115 };
00116 
00117 
00118 /*
00119  * Create and initialize a hash table
00120  */
00121 struct addr_list** new_addr_hash_table(void);
00122 
00123 
00124 /*
00125  * Release all memory allocated for a hash table
00126  */
00127 void free_addr_hash_table(struct addr_list** table);
00128 
00129 
00130 /*
00131  * Destroy a hash table
00132  */
00133 void destroy_addr_hash_table(struct addr_list** table);
00134 
00135 
00136 /* 
00137  * Add <group, ip_addr, port> into hash table
00138  */
00139 int addr_hash_table_insert(struct addr_list** hash_table, unsigned int grp,
00140                             ip_addr_t *addr, unsigned int port, char *tagv);
00141 
00142 
00143 /* 
00144  * Check if an entry exists in hash table that has given group, ip_addr, and
00145  * port.  Port 0 in hash table matches any port.
00146  */
00147 int match_addr_hash_table(struct addr_list** table, unsigned int grp,
00148                           ip_addr_t *addr, unsigned int port);
00149 
00150 
00151 /* 
00152  * Checks if an ip_addr/port entry exists in address hash table in any group.
00153  * Port 0 in hash table matches any port.   Returns group of the first match
00154  * or -1 if no match is found.
00155  */
00156 int find_group_in_addr_hash_table(struct addr_list** table,
00157                                   ip_addr_t *addr, unsigned int port);
00158 
00159 
00160 /* 
00161  * Print addresses stored in hash table
00162  */
00163 void addr_hash_table_print(struct addr_list** hash_table, FILE* reply_file);
00164 int addr_hash_table_mi_print(struct addr_list** hash_table,
00165                              struct mi_node* rpl);
00166 
00167 
00168 /* 
00169  * Empty hash table
00170  */
00171 void empty_addr_hash_table(struct addr_list** hash_table);
00172 
00173 
00174 #define PERM_MAX_SUBNETS 128 
00175 
00176 
00177 /*
00178  * Structure used to store a subnet
00179  */
00180 struct subnet {
00181     unsigned int grp;        /* address group, subnet count in last record */
00182     ip_addr_t  subnet;       /* IP subnet in host byte order with host bits shifted out */
00183     unsigned int port;       /* port or 0 */
00184     unsigned int mask;       /* how many bits belong to network part */
00185         str tag;
00186 };
00187 
00188 
00189 /*
00190  * Create a subnet table
00191  */
00192 struct subnet* new_subnet_table(void);
00193 
00194 
00195 /* 
00196  * Check if an entry exists in subnet table that matches given group, ip_addr,
00197  * and port.  Port 0 in subnet table matches any port.
00198  */
00199 int match_subnet_table(struct subnet* table, unsigned int group,
00200                        ip_addr_t *addr, unsigned int port);
00201 
00202 
00203 /* 
00204  * Checks if an entry exists in subnet table that matches given ip_addr,
00205  * and port.  Port 0 in subnet table matches any port.  Returns group of
00206  * the first match or -1 if no match is found.
00207  */
00208 int find_group_in_subnet_table(struct subnet* table,
00209                                ip_addr_t *addr, unsigned int port);
00210 
00211 /* 
00212  * Empty contents of subnet table
00213  */
00214 void empty_subnet_table(struct subnet *table);
00215 
00216 
00217 /*
00218  * Release memory allocated for a subnet table
00219  */
00220 void free_subnet_table(struct subnet* table);
00221 
00222 
00223 /* 
00224  * Add <grp, subnet, mask, port> into subnet table so that table is
00225  * kept ordered according to subnet, port, grp.
00226  */
00227 int subnet_table_insert(struct subnet* table, unsigned int grp,
00228                         ip_addr_t *subnet, unsigned int mask,
00229                         unsigned int port, char *tagv);
00230 
00231 
00232 /* 
00233  * Print subnets stored in subnet table
00234  */
00235 void subnet_table_print(struct subnet* table, FILE* reply_file);
00236 int subnet_table_mi_print(struct subnet* table, struct mi_node* rpl);
00237 
00238 
00239 #endif /* _PERM_HASH_H_ */