1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
* $Id: pike_funcs.c,v 1.3 2006/06/29 15:59:31 bogdan_iancu Exp $
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2003-03-11 converted to the new locking interface: locking.h --
* major changes (andrei)
* 2005-05-02 flags field added to node stucture -better sync between timer
* and worker processes; some races eliminated (bogdan)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "../../mem/shm_mem.h"
#include "../../locking.h"
#include "../../timer.h"
#include "../../ip_addr.h"
#include "../../resolve.h"
#include "ip_tree.h"
#include "pike_funcs.h"
#include "timer.h"
extern gen_lock_t* timer_lock;
extern struct list_link* timer;
extern int timeout;
void print_timer_list(struct list_link *head)
{
struct list_link *ll;
DBG("DEBUG:pike:print_timer_list --->\n");
for ( ll=head->next ; ll!=head; ll=ll->next) {
DBG("\t %p [byte=%x](expires=%d)\n",
ll, ll2ipnode(ll)->byte, ll2ipnode(ll)->expires);
}
}
int pike_check_req(struct sip_msg *msg, char *foo, char *bar)
{
struct ip_node *node;
struct ip_node *father;
unsigned char flags;
struct ip_addr* ip;
#ifdef _test
/* get the ip address from second via */
if (parse_headers(msg, HDR_VIA1_F, 0)!=0 )
return -1;
if (msg->via1==0 )
return -1;
/* convert from string to ip_addr */
ip = str2ip( &msg->via1->host );
if (ip==0)
return -1;
#else
ip = &(msg->rcv.src_ip);
#endif
/* first lock the proper tree branch and mark the IP with one more hit*/
lock_tree_branch( ip->u.addr[0] );
node = mark_node( ip->u.addr, ip->len, &father, &flags);
if (node==0) {
/* even if this is an error case, we return true in script to avoid
* considering the IP as marked (bogdan) */
return 1;
}
DBG("DEBUG:pike_check_req: src IP [%s],node=%p; hits=[%d,%d],[%d,%d] "
"node_flags=%d func_flags=%d\n",
ip_addr2a( ip ), node,
node->hits[PREV_POS],node->hits[CURR_POS],
node->leaf_hits[PREV_POS],node->leaf_hits[CURR_POS],
node->flags, flags);
/* update the timer */
lock_get(timer_lock);
if ( flags&NEW_NODE ) {
/* put this node into the timer list and remove its
father only if this has one kid and is not a LEAF_NODE*/
node->expires = get_ticks() + timeout;
append_to_timer( timer, &(node->timer_ll) );
node->flags |= NODE_INTIMER_FLAG;
if (father) {
DBG("DEBUG:pike_check_req: father %p: flags=%d kids->next=%p\n",
father,father->flags,father->kids->next);
if (!(father->flags&NODE_IPLEAF_FLAG) && !father->kids->next){
/* debug */
assert( has_timer_set(&(father->timer_ll))
&& (father->flags&(NODE_EXPIRED_FLAG|NODE_INTIMER_FLAG)) );
/* if the node is maked as expired by timer, let the timer
* to finish and remove the node */
if ( !(father->flags&NODE_EXPIRED_FLAG) ) {
remove_from_timer( timer, &(father->timer_ll) );
father->flags &= ~NODE_INTIMER_FLAG;
} else {
father->flags &= ~NODE_EXPIRED_FLAG;
}
}
}
} else {
/* update the timer -> in timer can be only nodes
* as IP-leaf(complete address) or tree-leaf */
if (node->flags&NODE_IPLEAF_FLAG || node->kids==0) {
/* tree leafs which are not potential red nodes are not update in
* order to make them to expire */
/* debug */
assert( has_timer_set(&(node->timer_ll))
&& (node->flags&(NODE_EXPIRED_FLAG|NODE_INTIMER_FLAG)) );
/* if node exprired, ignore the current hit and let is
* expire in timer process */
if ( !(flags&NO_UPDATE) && !(node->flags&NODE_EXPIRED_FLAG) ) {
node->expires = get_ticks() + timeout;
update_in_timer( timer, &(node->timer_ll) );
}
} else {
/* debug */
assert( !has_timer_set(&(node->timer_ll))
&& !(node->flags&(NODE_INTIMER_FLAG|NODE_EXPIRED_FLAG)) );
/* debug */
assert( !(node->flags&NODE_IPLEAF_FLAG) && node->kids );
}
}
/*print_timer_list( timer );*/ /* debug*/
lock_release(timer_lock);
unlock_tree_branch( ip->u.addr[0] );
/*print_tree( 0 );*/ /* debug */
if (flags&RED_NODE) {
LOG(L_WARN,"DEBUG:pike_check_req: ALARM - TOO MANY HITS on "
"%s !!\n", ip_addr2a( ip ) );
return -1;
}
return 1;
}
void clean_routine(unsigned int ticks , void *param)
{
static unsigned char mask[32]; /* 256 positions mask */
struct list_link head;
struct list_link *ll;
struct ip_node *dad;
struct ip_node *node;
int i;
/* DBG("DEBUG:pike:clean_routine: entering (%d)\n",ticks); */
/* before locking check first if the list is not empty and if can
* be at least one element removed */
if ( is_list_empty( timer ) || ll2ipnode(timer->next)->expires>ticks )
return;
/* get the expired elements */
lock_get( timer_lock );
check_and_split_timer( timer, ticks, &head, mask);
/*print_timer_list(timer);*/ /* debug */
lock_release( timer_lock );
/*print_tree( 0 );*/ /*debug*/
/* got something back? */
if ( is_list_empty(&head) )
return;
/* process what we got -> don't forget to lock the tree!! */
for(i=0;i<MAX_IP_BRANCHES;i++) {
/* if no element from this branch -> skip it */
if ( ((mask[i>>3])&(1<<(i&0x07)))==0 )
continue;
lock_tree_branch( i );
for( ll=head.next ; ll!=&head ; ) {
node = ll2ipnode( ll );
ll = ll->next;
/* skip nodes from a different branch */
if (node->branch!=i)
continue;
/* unlink the node -> the list will get shorter and it will be
* faster for the next branches to process it */
ll->prev->prev->next = ll;
ll->prev = ll->prev->prev;
node->expires = 0;
node->timer_ll.prev = node->timer_ll.next = 0;
if ( node->flags&NODE_EXPIRED_FLAG )
node->flags &= ~NODE_EXPIRED_FLAG;
else
continue;
/* process the node */
DBG("DEBUG:pike:clean_routine: clean node %p (kids=%p;"
"hits=[%d,%d];leaf=[%d,%d])\n", node,node->kids,
node->hits[PREV_POS],node->hits[CURR_POS],
node->leaf_hits[PREV_POS],node->leaf_hits[CURR_POS]);
/* if it's a node, leaf for an ipv4 address inside an
* ipv6 address -> just remove it from timer it will be deleted
* only when all its kids will be deleted also */
if (node->kids) {
assert( node->flags&NODE_IPLEAF_FLAG );
node->flags &= ~NODE_IPLEAF_FLAG;
node->leaf_hits[CURR_POS] = 0;
} else {
/* if the node has no prev, means its a top branch node -> just
* removed and destroy it */
if (node->prev!=0) {
/* if this is the last kid, we have to put the father
* into timer list */
if (node->prev->kids==node && node->next==0) {
/* this is the last kid node */
dad = node->prev;
/* put it in the list only if it's not an IP leaf
* (in this case, it's already there) */
if ( !(dad->flags&NODE_IPLEAF_FLAG) ) {
lock_get(timer_lock);
dad->expires = get_ticks() + timeout;
assert( !has_timer_set(&(dad->timer_ll)) );
append_to_timer( timer, &(dad->timer_ll));
dad->flags |= NODE_INTIMER_FLAG;
lock_release(timer_lock);
} else {
assert( has_timer_set(&(dad->timer_ll)) );
}
}
}
DBG("DEBUG:pike:clean_routine: rmv node %p[%d] \n",
node,node->byte);
/* del the node */
remove_node( node);
}
} /* for all expired elements */
unlock_tree_branch( i );
} /* for all branches */
}
void refresh_node( struct ip_node *node)
{
for( ; node ; node=node->next ) {
node->hits[PREV_POS] = node->hits[CURR_POS];
node->hits[CURR_POS] = 0;
node->leaf_hits[PREV_POS] = node->leaf_hits[CURR_POS];
node->leaf_hits[CURR_POS] = 0;
if (node->kids)
refresh_node( node->kids );
}
}
void swap_routine( unsigned int ticks, void *param)
{
struct ip_node *node;
int i;
/* DBG("DEBUG:pike:swap_routine: entering \n"); */
for(i=0;i<MAX_IP_BRANCHES;i++) {
node = get_tree_branch(i);
if (node) {
lock_tree_branch( i );
refresh_node( node );
unlock_tree_branch( i );
}
}
}
|