00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00065
00066
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
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
00100 loc->next = *loc_set;
00101 *loc_set = loc;
00102 } else {
00103
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