1 /*
2  * Copyright (C) 2007-2008 1&1 Internet AG
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 /**
22  * \file cr_data.h
23  * \brief Contains the functions to manage routing data.
24  * \ingroup carrierroute
25  * - Module; \ref carrierroute
26  */
27 
28 #ifndef CR_DATA_H
29 #define CR_DATA_H
30 
31 #include <sys/types.h>
32 #include "../../core/locking.h"
33 #include "../../core/flags.h"
34 #include "cr_map.h"
35 
36 
37 /**
38  * contains all routing data.
39  */
40 struct route_data_t {
41 	struct name_map_t * carrier_map; /*!< holds the map between carrier names and numbers */
42 	struct name_map_t * domain_map; /*!< holds the map between domain names and numbers */
43 	struct carrier_data_t ** carriers; /*!< array of carriers */
44 	size_t carrier_num; /*!< number of carriers */
45 	size_t first_empty_carrier; /*!< the index of the first empty entry in carriers */
46 	size_t domain_num; /*!< total number of different domains */
47 	int default_carrier_id;
48 	int proc_cnt; /*!< a ref counter for the shm data */
49 	gen_lock_t lock; /*!< lock for ref counter updates */
50 };
51 
52 /**
53  * initialises the routing data, initialises the global data pointer
54  *
55  * @return 0 on success, -1 on failure
56  */
57 int init_route_data(void);
58 
59 
60 /**
61  * Frees the routing data
62  */
63 void destroy_route_data(void);
64 
65 
66 /**
67  * Clears the complete routing data.
68  *
69  * @param data route data to be cleared
70  */
71 void clear_route_data(struct route_data_t *data);
72 
73 
74 /**
75  * adds a carrier_data struct for given carrier
76  *
77  * @param rd route data to be searched
78  * @param carrier_data the carrier data struct to be inserted
79  *
80  * @return 0 on success, -1 on failure
81  */
82 int add_carrier_data(struct route_data_t * rd, struct carrier_data_t * carrier_data);
83 
84 
85 /**
86  * Loads the routing data into the routing trees and sets the
87  * global_data pointer to the new data. The old_data is removed
88  * when it is not locked anymore.
89  *
90  * @return 0 on success, -1 on failure
91  */
92 int reload_route_data(void);
93 
94 
95 /**
96  * Increases lock counter and returns a pointer to the
97  * current routing data
98  *
99  * @return pointer to the global routing data on success,
100  * NULL on failure
101 */
102 struct route_data_t * get_data(void);
103 
104 
105 /**
106  * decrements the lock counter of the routing data
107  *
108  * @param data data to be released
109  */
110 void release_data(struct route_data_t *data);
111 
112 
113 /**
114  * Returns the carrier data for the given id by doing a binary search.
115  * @note The carrier array must be sorted!
116  *
117  * @param rd route data to be searched
118  * @param carrier_id the id of the desired carrier
119  *
120  * @return a pointer to the desired carrier data, NULL if not found.
121  */
122 struct carrier_data_t *get_carrier_data(struct route_data_t * rd, int carrier_id);
123 
124 
125 /**
126  * Adds the given route information to the routing domain identified by
127  * domain. scan_prefix identifies the number for which the information
128  * is and the rewrite_* parameters define what to do in case of a match.
129  * prob gives the probability with which this rule applies if there are
130  * more than one for a given prefix.
131  *
132  * @param rd the route data to which the route shall be added
133  * @param carrier_id the carrier id of the route to be added
134  * @param domain_id the routing domain id of the new route
135  * @param scan_prefix the number prefix
136  * @param flags user defined flags
137  * @param mask mask for user defined flags
138  * @param max_targets the number of targets
139  * @param prob the weight of the rule
140  * @param strip the number of digits to be stripped off userpart before prepending prefix
141  * @param rewrite_hostpart the rewrite_host of the rule
142  * @param rewrite_local_prefix the rewrite prefix
143  * @param rewrite_local_suffix the rewrite suffix
144  * @param status the status of the rule
145  * @param hash_index the hash index of the rule
146  * @param backup indicates if the route is backed up by another. only
147                  useful if status==0, if set, it is the hash value
148                  of another rule
149   * @param backed_up an -1-termintated array of hash indices of the route
150                     for which this route is backup
151  * @param comment a comment for the route rule
152  *
153  * @return 0 on success, -1 on error in which case it LOGs a message.
154  */
155 int add_route(struct route_data_t * rd, int carrier_id,
156 		int domain_id, const str * scan_prefix, flag_t flags, flag_t mask, int max_targets,
157 		double prob, const str * rewrite_hostpart, int strip, const str * rewrite_local_prefix,
158 		const str * rewrite_local_suffix, int status, int hash_index, int backup, int * backed_up,
159 		const str * comment);
160 
161 
162 /**
163  * Adds the given failure route information to the failure routing domain identified by
164  * domain. scan_prefix, host, reply_code and flags identifies the number for which
165  * the information is and the next_domain parameter defines where to continue routing
166  * in case of a match.
167  *
168  * @param rd the route data to which the route shall be added
169  * @param carrier_id the carrier id of the route to be added
170  * @param domain_id the routing domain id of the new route
171  * @param scan_prefix the number prefix
172  * @param host the hostname last tried
173  * @param reply_code the reply code
174  * @param flags user defined flags
175  * @param mask for user defined flags
176  * @param next_domain_id continue routing with this domain id
177  * @param comment a comment for the failure route rule
178  *
179  * @return 0 on success, -1 on error in which case it LOGs a message.
180  */
181 int add_failure_route(struct route_data_t * rd, int carrier_id, int domain_id,
182 		const str * scan_prefix, const str * host, const str * reply_code,
183 		flag_t flags, flag_t mask, int next_domain_id, const str * comment);
184 
185 
186 /**
187  * Fixes the route rules by creating an array for accessing
188  * route rules by hash index directly
189  *
190  * @param rd route data to be fixed
191  *
192  * @return 0 on success, -1 on failure
193  */
194 int rule_fixup(struct route_data_t * rd);
195 
196 
197 #endif
198