modules_k/cpl-c/loc_set.h

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 
00024 #ifndef _CPL_LOC_SET_H_
00025 #define _CPL_LOC_SET_H_
00026 
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <time.h>
00030 #include <stdlib.h>
00031 
00032 #include "../../mem/shm_mem.h"
00033 #include "../../str.h"
00034 #include "../../dprint.h"
00035 
00036 
00037 #define CPL_LOC_DUPL    (1<<0)
00038 #define CPL_LOC_NATED   (1<<1)
00039 
00040 
00041 struct location {
00042         struct address {
00043                 str uri;
00044                 str received;
00045                 unsigned int priority;
00046         }addr;
00047         int flags;
00048         struct location *next;
00049 };
00050 
00051 
00052 
00053 static inline void free_location( struct location *loc)
00054 {
00055         shm_free( loc );
00056 }
00057 
00058 
00059 
00060 /* insert a new location into the set maintaining order by the prio val - the
00061  * list starts with the smallest prio!
00062  * For locations having the same prio val, the adding order will be kept */
00063 static inline int add_location(struct location **loc_set, str *uri,
00064                                                                 str *received, unsigned int prio, int flags)
00065 {
00066         struct location *loc;
00067         struct location *foo, *bar;
00068 
00069         if(received && received->s && received->len)
00070                 loc = (struct location*)shm_malloc(sizeof(struct location)+
00071                                 ((flags&CPL_LOC_DUPL)?uri->len+1+received->len+1:0) );
00072         else 
00073                 loc = (struct location*)shm_malloc(
00074                         sizeof(struct location)+((flags&CPL_LOC_DUPL)?uri->len+1:0) );
00075         if (!loc) {
00076                 LM_ERR("no more free shm memory!\n");
00077                 return -1;
00078         }
00079 
00080         if (flags&CPL_LOC_DUPL) {
00081                 loc->addr.uri.s = ((char*)loc)+sizeof(struct location);
00082                 memcpy(loc->addr.uri.s,uri->s,uri->len);
00083                 loc->addr.uri.s[uri->len] = 0;
00084         } else {
00085                 loc->addr.uri.s = uri->s;
00086         }
00087         loc->addr.uri.len = uri->len;
00088         loc->addr.priority = prio;
00089         loc->flags = flags;
00090 
00091         if(received && received->s && received->len) {
00092                 if (flags&CPL_LOC_DUPL) {
00093                         loc->addr.received.s = ((char*)loc)+sizeof(struct location)+
00094                                 uri->len+1;
00095                         memcpy(loc->addr.received.s,received->s,received->len);
00096                         loc->addr.received.s[received->len] = 0;
00097                 }
00098                 else {
00099                         loc->addr.received.s = received->s;
00100                 }
00101                 loc->addr.received.len = received->len;
00102         }
00103         else {
00104                 loc->addr.received.s = 0;
00105                 loc->addr.received.len = 0;
00106         }
00107 
00108         /* find the proper place for the new location */
00109         foo = *loc_set;
00110         bar = 0;
00111         while(foo && foo->addr.priority>prio) {
00112                 bar = foo;
00113                 foo = foo->next;
00114         }
00115         if (!bar) {
00116                 /* insert at the beginning */
00117                 loc->next = *loc_set;
00118                 *loc_set = loc;
00119         } else {
00120                 /* insert after bar, before foo  */
00121                 loc->next = foo;
00122                 bar->next = loc;
00123         }
00124 
00125         return 0;
00126 }
00127 
00128 
00129 
00130 static inline void remove_location(struct location **loc_set, char *uri_s,
00131                                                                                                                                 int uri_len)
00132 {
00133         struct location *loc = *loc_set;
00134         struct location *prev_loc = 0;
00135 
00136         for( ; loc ; prev_loc=loc,loc=loc->next ) {
00137                 if (loc->addr.uri.len==uri_len &&
00138                 !strncasecmp(loc->addr.uri.s,uri_s,uri_len) )
00139                         break;
00140         }
00141 
00142         if (loc) {
00143                 LM_DBG("removing from loc_set <%.*s>\n",
00144                         uri_len,uri_s);
00145                 if (prev_loc)
00146                         prev_loc->next=loc->next;
00147                 else
00148                         (*loc_set)=loc->next;
00149                 shm_free( loc );
00150         } else {
00151                 LM_DBG("no matching in loc_set for <%.*s>\n",
00152                         uri_len,uri_s);
00153         }
00154 }
00155 
00156 
00157 
00158 static inline struct location *remove_first_location(struct location **loc_set)
00159 {
00160         struct location *loc;
00161 
00162         if (!*loc_set)
00163                 return 0;
00164 
00165         loc = *loc_set;
00166         *loc_set = (*loc_set)->next;
00167         loc->next = 0;
00168         LM_DBG("removing <%.*s>\n",
00169                 loc->addr.uri.len,loc->addr.uri.s);
00170 
00171         return loc;
00172 }
00173 
00174 
00175 
00176 static inline void empty_location_set(struct location **loc_set)
00177 {
00178         struct location *loc;
00179 
00180         while (*loc_set) {
00181                 loc = (*loc_set)->next;
00182                 shm_free(*loc_set);
00183                 *loc_set = loc;
00184         }
00185         *loc_set = 0;
00186 }
00187 
00188 
00189 static inline void print_location_set(struct location *loc_set)
00190 {
00191         while (loc_set) {
00192                 LM_DBG("uri=<%s> received=<%s> q=%d\n",
00193                         loc_set->addr.uri.s, loc_set->addr.received.s,
00194                         loc_set->addr.priority);
00195                 loc_set=loc_set->next;
00196         }
00197 }
00198 
00199 #endif
00200 
00201