all: updated FSF address in GPL text
[sip-router] / modules / cpl-c / loc_set.h
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23
24 #ifndef _CPL_LOC_SET_H_
25 #define _CPL_LOC_SET_H_
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <time.h>
30 #include <stdlib.h>
31
32 #include "../../mem/shm_mem.h"
33 #include "../../str.h"
34 #include "../../dprint.h"
35
36
37 #define CPL_LOC_DUPL    (1<<0)
38 #define CPL_LOC_NATED   (1<<1)
39
40
41 struct location {
42         struct address {
43                 str uri;
44                 str received;
45                 unsigned int priority;
46         }addr;
47         int flags;
48         struct location *next;
49 };
50
51
52
53 static inline void free_location( struct location *loc)
54 {
55         shm_free( loc );
56 }
57
58
59
60 /* insert a new location into the set maintaining order by the prio val - the
61  * list starts with the smallest prio!
62  * For locations having the same prio val, the adding order will be kept */
63 static inline int add_location(struct location **loc_set, str *uri,
64                                                                 str *received, unsigned int prio, int flags)
65 {
66         struct location *loc;
67         struct location *foo, *bar;
68
69         if(received && received->s && received->len)
70                 loc = (struct location*)shm_malloc(sizeof(struct location)+
71                                 ((flags&CPL_LOC_DUPL)?uri->len+1+received->len+1:0) );
72         else 
73                 loc = (struct location*)shm_malloc(
74                         sizeof(struct location)+((flags&CPL_LOC_DUPL)?uri->len+1:0) );
75         if (!loc) {
76                 LM_ERR("no more free shm memory!\n");
77                 return -1;
78         }
79
80         if (flags&CPL_LOC_DUPL) {
81                 loc->addr.uri.s = ((char*)loc)+sizeof(struct location);
82                 memcpy(loc->addr.uri.s,uri->s,uri->len);
83                 loc->addr.uri.s[uri->len] = 0;
84         } else {
85                 loc->addr.uri.s = uri->s;
86         }
87         loc->addr.uri.len = uri->len;
88         loc->addr.priority = prio;
89         loc->flags = flags;
90
91         if(received && received->s && received->len) {
92                 if (flags&CPL_LOC_DUPL) {
93                         loc->addr.received.s = ((char*)loc)+sizeof(struct location)+
94                                 uri->len+1;
95                         memcpy(loc->addr.received.s,received->s,received->len);
96                         loc->addr.received.s[received->len] = 0;
97                 }
98                 else {
99                         loc->addr.received.s = received->s;
100                 }
101                 loc->addr.received.len = received->len;
102         }
103         else {
104                 loc->addr.received.s = 0;
105                 loc->addr.received.len = 0;
106         }
107
108         /* find the proper place for the new location */
109         foo = *loc_set;
110         bar = 0;
111         while(foo && foo->addr.priority>prio) {
112                 bar = foo;
113                 foo = foo->next;
114         }
115         if (!bar) {
116                 /* insert at the beginning */
117                 loc->next = *loc_set;
118                 *loc_set = loc;
119         } else {
120                 /* insert after bar, before foo  */
121                 loc->next = foo;
122                 bar->next = loc;
123         }
124
125         return 0;
126 }
127
128
129
130 static inline void remove_location(struct location **loc_set, char *uri_s,
131                                                                                                                                 int uri_len)
132 {
133         struct location *loc = *loc_set;
134         struct location *prev_loc = 0;
135
136         for( ; loc ; prev_loc=loc,loc=loc->next ) {
137                 if (loc->addr.uri.len==uri_len &&
138                 !strncasecmp(loc->addr.uri.s,uri_s,uri_len) )
139                         break;
140         }
141
142         if (loc) {
143                 LM_DBG("removing from loc_set <%.*s>\n",
144                         uri_len,uri_s);
145                 if (prev_loc)
146                         prev_loc->next=loc->next;
147                 else
148                         (*loc_set)=loc->next;
149                 shm_free( loc );
150         } else {
151                 LM_DBG("no matching in loc_set for <%.*s>\n",
152                         uri_len,uri_s);
153         }
154 }
155
156
157
158 static inline struct location *remove_first_location(struct location **loc_set)
159 {
160         struct location *loc;
161
162         if (!*loc_set)
163                 return 0;
164
165         loc = *loc_set;
166         *loc_set = (*loc_set)->next;
167         loc->next = 0;
168         LM_DBG("removing <%.*s>\n",
169                 loc->addr.uri.len,loc->addr.uri.s);
170
171         return loc;
172 }
173
174
175
176 static inline void empty_location_set(struct location **loc_set)
177 {
178         struct location *loc;
179
180         while (*loc_set) {
181                 loc = (*loc_set)->next;
182                 shm_free(*loc_set);
183                 *loc_set = loc;
184         }
185         *loc_set = 0;
186 }
187
188
189 static inline void print_location_set(struct location *loc_set)
190 {
191         while (loc_set) {
192                 LM_DBG("uri=<%s> received=<%s> q=%d\n",
193                         loc_set->addr.uri.s, loc_set->addr.received.s,
194                         loc_set->addr.priority);
195                 loc_set=loc_set->next;
196         }
197 }
198
199 #endif
200
201