modules_s/cpl-c/loc_set.h

00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of ser, a free SIP server.
00007  *
00008  * ser 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  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * ser is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00028 
00029 #ifndef _CPL_LOC_SET_H_
00030 #define _CPL_LOC_SET_H_
00031 
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <time.h>
00035 #include <stdlib.h>
00036 
00037 #include "../../mem/shm_mem.h"
00038 #include "../../str.h"
00039 #include "../../dprint.h"
00040 
00041 
00042 #define CPL_LOC_DUPL    (1<<0)
00043 #define CPL_LOC_NATED   (1<<1)
00044 
00045 
00046 struct location {
00047         struct address {
00048                 str uri;
00049                 unsigned int priority;
00050         }addr;
00051         int flags;
00052         struct location *next;
00053 };
00054 
00055 
00056 
00057 static inline void free_location( struct location *loc)
00058 {
00059         shm_free( loc );
00060 }
00061 
00062 
00063 
00064 /* insert a new location into the set maintaining order by the prio val - the
00065  * list starts with the smallest prio!
00066  * For locations having the same prio val, the adding order will be kept */
00067 static inline int add_location(struct location **loc_set, str *uri,
00068                                                                                         unsigned int prio, int flags)
00069 {
00070         struct location *loc;
00071         struct location *foo, *bar;
00072 
00073         loc = (struct location*)shm_malloc(
00074                 sizeof(struct location)+((flags&CPL_LOC_DUPL)?uri->len+1:0) );
00075         if (!loc) {
00076                 LOG(L_ERR,"ERROR:add_location: 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         /* find the proper place for the new location */
00092         foo = *loc_set;
00093         bar = 0;
00094         while(foo && foo->addr.priority<=prio) {
00095                 bar = foo;
00096                 foo = foo->next;
00097         }
00098         if (!bar) {
00099                 /* insert at the beginning */
00100                 loc->next = *loc_set;
00101                 *loc_set = loc;
00102         } else {
00103                 /* insert after bar, before foo  */
00104                 loc->next = foo;
00105                 bar->next = loc;
00106         }
00107 
00108         return 0;
00109 }
00110 
00111 
00112 
00113 static inline void remove_location(struct location **loc_set, char *uri_s,
00114                                                                                                                                 int uri_len)
00115 {
00116         struct location *loc = *loc_set;
00117         struct location *prev_loc = 0;
00118 
00119         for( ; loc ; prev_loc=loc,loc=loc->next ) {
00120                 if (loc->addr.uri.len==uri_len &&
00121                 !strncasecmp(loc->addr.uri.s,uri_s,uri_len) )
00122                         break;
00123         }
00124 
00125         if (loc) {
00126                 DBG("DEBUG:remove_location: removing from loc_set <%.*s>\n",
00127                         uri_len,uri_s);
00128                 if (prev_loc)
00129                         prev_loc->next=loc->next;
00130                 else
00131                         (*loc_set)=loc->next;
00132                 shm_free( loc );
00133         } else {
00134                 DBG("DEBUG:remove_location: no matching in loc_set for <%.*s>\n",
00135                         uri_len,uri_s);
00136         }
00137 }
00138 
00139 
00140 
00141 static inline struct location *remove_first_location(struct location **loc_set)
00142 {
00143         struct location *loc;
00144 
00145         if (!*loc_set)
00146                 return 0;
00147 
00148         loc = *loc_set;
00149         *loc_set = (*loc_set)->next;
00150         loc->next = 0;
00151         DBG("DEBUG:remove_first_location: removing <%.*s>\n",
00152                 loc->addr.uri.len,loc->addr.uri.s);
00153 
00154         return loc;
00155 }
00156 
00157 
00158 
00159 static inline void empty_location_set(struct location **loc_set)
00160 {
00161         struct location *loc;
00162 
00163         while (*loc_set) {
00164                 loc = (*loc_set)->next;
00165                 shm_free(*loc_set);
00166                 *loc_set = loc;
00167         }
00168         *loc_set = 0;
00169 }
00170 
00171 
00172 static inline void print_location_set(struct location *loc_set)
00173 {
00174         while (loc_set) {
00175                 DBG("DEBUG:cpl_c:print_loc_set: uri=<%s> q=%d\n",loc_set->addr.uri.s,
00176                         loc_set->addr.priority);
00177                 loc_set=loc_set->next;
00178         }
00179 }
00180 
00181 #endif
00182 
00183