Mercurial > hg > freeDiameter
comparison extensions/rt_redirect/redir_entries.c @ 1259:82280e745a89
Remove whitespace at end of line.
author | Thomas Klausner <tk@giga.or.at> |
---|---|
date | Mon, 24 Mar 2014 13:13:38 +0100 |
parents | 4a9f08d6b6ba |
children | 4f6f61e67599 |
comparison
equal
deleted
inserted
replaced
1258:97caad40b665 | 1259:82280e745a89 |
---|---|
40 | 40 |
41 /* Initialize the array */ | 41 /* Initialize the array */ |
42 int redir_entry_init() | 42 int redir_entry_init() |
43 { | 43 { |
44 int i; | 44 int i; |
45 | 45 |
46 TRACE_ENTRY(""); | 46 TRACE_ENTRY(""); |
47 | 47 |
48 /* redirects_usages */ | 48 /* redirects_usages */ |
49 memset(&redirects_usages, 0, sizeof(redirects_usages)); | 49 memset(&redirects_usages, 0, sizeof(redirects_usages)); |
50 | 50 |
51 for (i = 0; i <= H_U_MAX; i++) { | 51 for (i = 0; i <= H_U_MAX; i++) { |
52 CHECK_POSIX( pthread_rwlock_init( &redirects_usages[i].lock, NULL) ); | 52 CHECK_POSIX( pthread_rwlock_init( &redirects_usages[i].lock, NULL) ); |
53 fd_list_init( &redirects_usages[i].sentinel, &redirects_usages[i] ); | 53 fd_list_init( &redirects_usages[i].sentinel, &redirects_usages[i] ); |
54 } | 54 } |
55 | 55 |
56 /* initialize the scores */ | 56 /* initialize the scores */ |
57 redirects_usages[ DONT_CACHE ].score = FD_SCORE_REDIR_ONCE; | 57 redirects_usages[ DONT_CACHE ].score = FD_SCORE_REDIR_ONCE; |
58 redirects_usages[ ALL_SESSION ].score = FD_SCORE_REDIR_SESSION; | 58 redirects_usages[ ALL_SESSION ].score = FD_SCORE_REDIR_SESSION; |
59 redirects_usages[ ALL_REALM ].score = FD_SCORE_REDIR_REALM; | 59 redirects_usages[ ALL_REALM ].score = FD_SCORE_REDIR_REALM; |
60 redirects_usages[ REALM_AND_APPLICATION ].score = FD_SCORE_REDIR_REALM_APP; | 60 redirects_usages[ REALM_AND_APPLICATION ].score = FD_SCORE_REDIR_REALM_APP; |
70 int redir_entry_new(struct redir_entry ** e, struct fd_list * targets, uint32_t rhu, struct msg * qry, DiamId_t nh, size_t nhlen, os0_t oh, size_t ohlen) | 70 int redir_entry_new(struct redir_entry ** e, struct fd_list * targets, uint32_t rhu, struct msg * qry, DiamId_t nh, size_t nhlen, os0_t oh, size_t ohlen) |
71 { | 71 { |
72 struct redir_entry * entry = NULL; | 72 struct redir_entry * entry = NULL; |
73 os0_t s; | 73 os0_t s; |
74 size_t l; | 74 size_t l; |
75 | 75 |
76 TRACE_ENTRY("%p %p %d %p %p %zd %p %zd", e, targets, rhu, qry, nh, nhlen, oh, ohlen) | 76 TRACE_ENTRY("%p %p %d %p %p %zd %p %zd", e, targets, rhu, qry, nh, nhlen, oh, ohlen) |
77 ASSERT(e && targets && (rhu <= H_U_MAX) && qry && nh && nhlen && oh && ohlen); | 77 ASSERT(e && targets && (rhu <= H_U_MAX) && qry && nh && nhlen && oh && ohlen); |
78 | 78 |
79 CHECK_MALLOC( entry = malloc(sizeof(struct redir_entry)) ); | 79 CHECK_MALLOC( entry = malloc(sizeof(struct redir_entry)) ); |
80 memset(entry, 0, sizeof(struct redir_entry)); | 80 memset(entry, 0, sizeof(struct redir_entry)); |
81 | 81 |
82 entry->eyec = REDIR_ENTRY_EYEC; | 82 entry->eyec = REDIR_ENTRY_EYEC; |
83 | 83 |
84 CHECK_MALLOC( entry->from.s = os0dup(nh, nhlen) ); | 84 CHECK_MALLOC( entry->from.s = os0dup(nh, nhlen) ); |
85 entry->from.l = nhlen; | 85 entry->from.l = nhlen; |
86 | 86 |
87 fd_list_init(&entry->target_peers_list, entry); | 87 fd_list_init(&entry->target_peers_list, entry); |
88 fd_list_move_end(&entry->target_peers_list, targets); | 88 fd_list_move_end(&entry->target_peers_list, targets); |
89 | 89 |
90 fd_list_init(&entry->exp_list, entry); | 90 fd_list_init(&entry->exp_list, entry); |
91 | 91 |
92 entry->type = rhu; | 92 entry->type = rhu; |
93 fd_list_init(&entry->redir_list, entry); | 93 fd_list_init(&entry->redir_list, entry); |
94 /* finally initialize the data */ | 94 /* finally initialize the data */ |
95 switch (rhu) { | 95 switch (rhu) { |
96 case DONT_CACHE: | 96 case DONT_CACHE: |
97 entry->data.message.msg = qry; | 97 entry->data.message.msg = qry; |
98 break; | 98 break; |
99 | 99 |
100 case ALL_SESSION: | 100 case ALL_SESSION: |
101 { | 101 { |
102 /* There is a good chance that the session is already cached in the message, so retrieve it */ | 102 /* There is a good chance that the session is already cached in the message, so retrieve it */ |
103 struct session * sess; | 103 struct session * sess; |
104 CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, qry, &sess, NULL) ); | 104 CHECK_FCT( fd_msg_sess_get(fd_g_config->cnf_dict, qry, &sess, NULL) ); |
111 CHECK_FCT( fd_sess_getsid(sess, &s, &l) ); | 111 CHECK_FCT( fd_sess_getsid(sess, &s, &l) ); |
112 CHECK_MALLOC( entry->data.session.s = os0dup(s, l) ); | 112 CHECK_MALLOC( entry->data.session.s = os0dup(s, l) ); |
113 entry->data.session.l = l; | 113 entry->data.session.l = l; |
114 } | 114 } |
115 break; | 115 break; |
116 | 116 |
117 case ALL_REALM: | 117 case ALL_REALM: |
118 { | 118 { |
119 /* Search the Destination-Realm of the message */ | 119 /* Search the Destination-Realm of the message */ |
120 struct avp * dr; | 120 struct avp * dr; |
121 struct avp_hdr * ahdr; | 121 struct avp_hdr * ahdr; |
129 CHECK_FCT( fd_msg_avp_hdr( dr, &ahdr ) ); | 129 CHECK_FCT( fd_msg_avp_hdr( dr, &ahdr ) ); |
130 CHECK_MALLOC( entry->data.realm.s = os0dup(ahdr->avp_value->os.data, ahdr->avp_value->os.len) ); | 130 CHECK_MALLOC( entry->data.realm.s = os0dup(ahdr->avp_value->os.data, ahdr->avp_value->os.len) ); |
131 entry->data.realm.l = ahdr->avp_value->os.len; | 131 entry->data.realm.l = ahdr->avp_value->os.len; |
132 } | 132 } |
133 break; | 133 break; |
134 | 134 |
135 case REALM_AND_APPLICATION: | 135 case REALM_AND_APPLICATION: |
136 { | 136 { |
137 /* Search the Destination-Realm of the message */ | 137 /* Search the Destination-Realm of the message */ |
138 struct avp * dr; | 138 struct avp * dr; |
139 struct avp_hdr * ahdr; | 139 struct avp_hdr * ahdr; |
153 struct msg_hdr * hdr; | 153 struct msg_hdr * hdr; |
154 CHECK_FCT( fd_msg_hdr(qry, &hdr) ); | 154 CHECK_FCT( fd_msg_hdr(qry, &hdr) ); |
155 entry->data.realm_app.a = hdr->msg_appl; | 155 entry->data.realm_app.a = hdr->msg_appl; |
156 } | 156 } |
157 break; | 157 break; |
158 | 158 |
159 case ALL_APPLICATION: | 159 case ALL_APPLICATION: |
160 { | 160 { |
161 struct msg_hdr * hdr; | 161 struct msg_hdr * hdr; |
162 CHECK_FCT( fd_msg_hdr(qry, &hdr) ); | 162 CHECK_FCT( fd_msg_hdr(qry, &hdr) ); |
163 entry->data.app.a = hdr->msg_appl; | 163 entry->data.app.a = hdr->msg_appl; |
164 } | 164 } |
165 break; | 165 break; |
166 | 166 |
167 case ALL_HOST: | 167 case ALL_HOST: |
168 CHECK_MALLOC( entry->data.host.s = os0dup(oh, ohlen) ); | 168 CHECK_MALLOC( entry->data.host.s = os0dup(oh, ohlen) ); |
169 entry->data.host.l = ohlen; | 169 entry->data.host.l = ohlen; |
170 break; | 170 break; |
171 | 171 |
172 case ALL_USER: | 172 case ALL_USER: |
173 { | 173 { |
174 /* Search the User-Name of the message */ | 174 /* Search the User-Name of the message */ |
175 struct avp * un; | 175 struct avp * un; |
176 struct avp_hdr * ahdr; | 176 struct avp_hdr * ahdr; |
184 CHECK_FCT( fd_msg_avp_hdr( un, &ahdr ) ); | 184 CHECK_FCT( fd_msg_avp_hdr( un, &ahdr ) ); |
185 CHECK_MALLOC( entry->data.user.s = os0dup(ahdr->avp_value->os.data, ahdr->avp_value->os.len) ); | 185 CHECK_MALLOC( entry->data.user.s = os0dup(ahdr->avp_value->os.data, ahdr->avp_value->os.len) ); |
186 entry->data.user.l = ahdr->avp_value->os.len; | 186 entry->data.user.l = ahdr->avp_value->os.len; |
187 } | 187 } |
188 break; | 188 break; |
189 | 189 |
190 default: | 190 default: |
191 ASSERT(0); | 191 ASSERT(0); |
192 return EINVAL; | 192 return EINVAL; |
193 } | 193 } |
194 | 194 |
195 /* We're done */ | 195 /* We're done */ |
196 *e = entry; | 196 *e = entry; |
197 return 0; | 197 return 0; |
198 } | 198 } |
199 | 199 |
200 | 200 |
201 /* Compares two pointers (DONT_CACHE) */ | 201 /* Compares two pointers (DONT_CACHE) */ |
202 static int compare_entries_ptr(union matchdata * d1, union matchdata * d2) { | 202 static int compare_entries_ptr(union matchdata * d1, union matchdata * d2) { |
203 unsigned long v1 = (unsigned long) d1->message.msg; | 203 unsigned long v1 = (unsigned long) d1->message.msg; |
204 unsigned long v2 = (unsigned long) d2->message.msg; | 204 unsigned long v2 = (unsigned long) d2->message.msg; |
205 if (v1 > v2) | 205 if (v1 > v2) |
206 return 1; | 206 return 1; |
207 if (v1 < v2) | 207 if (v1 < v2) |
208 return -1; | 208 return -1; |
209 return 0; | 209 return 0; |
210 } | 210 } |
211 /* Compare two applications (REALM_AND_APPLICATION and ALL_APPLICATION) */ | 211 /* Compare two applications (REALM_AND_APPLICATION and ALL_APPLICATION) */ |
212 static int compare_entries_appl(union matchdata * d1, union matchdata * d2) { | 212 static int compare_entries_appl(union matchdata * d1, union matchdata * d2) { |
213 if (d1->app.a > d2->app.a) | 213 if (d1->app.a > d2->app.a) |
214 return 1; | 214 return 1; |
215 if (d1->app.a < d2->app.a) | 215 if (d1->app.a < d2->app.a) |
216 return -1; | 216 return -1; |
217 return 0; | 217 return 0; |
218 } | 218 } |
219 | 219 |
220 /* Compare two strings (ALL_SESSION, ALL_REALM, ALL_HOST, ALL_USER) */ | 220 /* Compare two strings (ALL_SESSION, ALL_REALM, ALL_HOST, ALL_USER) */ |
235 | 235 |
236 /* Link the newly created entry into the correct redirects_usages list. The mutex must be held */ | 236 /* Link the newly created entry into the correct redirects_usages list. The mutex must be held */ |
237 int redir_entry_insert(struct redir_entry * e) | 237 int redir_entry_insert(struct redir_entry * e) |
238 { | 238 { |
239 struct fd_list * li; | 239 struct fd_list * li; |
240 | 240 |
241 TRACE_ENTRY("%p", e); | 241 TRACE_ENTRY("%p", e); |
242 CHECK_PARAMS(e && (e->eyec == REDIR_ENTRY_EYEC)); | 242 CHECK_PARAMS(e && (e->eyec == REDIR_ENTRY_EYEC)); |
243 | 243 |
244 /* Write-Lock the line */ | 244 /* Write-Lock the line */ |
245 CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); | 245 CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); |
246 | 246 |
247 for (li = redirects_usages[e->type].sentinel.next; li != &redirects_usages[e->type].sentinel; li = li->next) { | 247 for (li = redirects_usages[e->type].sentinel.next; li != &redirects_usages[e->type].sentinel; li = li->next) { |
248 struct redir_entry * n = li->o; | 248 struct redir_entry * n = li->o; |
249 int cmp = redir_entry_cmp_key[e->type](&e->data, &n->data); | 249 int cmp = redir_entry_cmp_key[e->type](&e->data, &n->data); |
250 if (cmp <= 0) | 250 if (cmp <= 0) |
251 break; | 251 break; |
252 } | 252 } |
253 | 253 |
254 fd_list_insert_before(li, &e->redir_list); | 254 fd_list_insert_before(li, &e->redir_list); |
255 | 255 |
256 /* unLock the line */ | 256 /* unLock the line */ |
257 CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); | 257 CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); |
258 | 258 |
259 return 0; | 259 return 0; |
260 } | 260 } |
262 /* Destroy -- the exp_peer_lock must be held when this function is called */ | 262 /* Destroy -- the exp_peer_lock must be held when this function is called */ |
263 int redir_entry_destroy(struct redir_entry * e) | 263 int redir_entry_destroy(struct redir_entry * e) |
264 { | 264 { |
265 TRACE_ENTRY("%p", e); | 265 TRACE_ENTRY("%p", e); |
266 CHECK_PARAMS(e && (e->eyec == REDIR_ENTRY_EYEC)); | 266 CHECK_PARAMS(e && (e->eyec == REDIR_ENTRY_EYEC)); |
267 | 267 |
268 /* If the entry is linked, lock the rwlock also */ | 268 /* If the entry is linked, lock the rwlock also */ |
269 if (!FD_IS_LIST_EMPTY(&e->redir_list)) { | 269 if (!FD_IS_LIST_EMPTY(&e->redir_list)) { |
270 CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); | 270 CHECK_POSIX( pthread_rwlock_wrlock( RWLOCK_REDIR(e) ) ); |
271 fd_list_unlink(&e->redir_list); | 271 fd_list_unlink(&e->redir_list); |
272 CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); | 272 CHECK_POSIX( pthread_rwlock_unlock( RWLOCK_REDIR(e) ) ); |
273 } | 273 } |
274 | 274 |
275 /* Now unlink from other list */ | 275 /* Now unlink from other list */ |
276 fd_list_unlink(&e->exp_list); | 276 fd_list_unlink(&e->exp_list); |
277 | 277 |
278 /* Empty the targets list */ | 278 /* Empty the targets list */ |
279 while (!FD_IS_LIST_EMPTY(&e->target_peers_list)) { | 279 while (!FD_IS_LIST_EMPTY(&e->target_peers_list)) { |
280 struct redir_host * h = (struct redir_host *)e->target_peers_list.next->o; | 280 struct redir_host * h = (struct redir_host *)e->target_peers_list.next->o; |
281 | 281 |
282 fd_list_unlink(&h->chain); | 282 fd_list_unlink(&h->chain); |
283 free(h->id); | 283 free(h->id); |
284 free(h); | 284 free(h); |
285 } | 285 } |
286 | 286 |
287 /* Now we can destroy the data safely */ | 287 /* Now we can destroy the data safely */ |
288 switch (e->type) { | 288 switch (e->type) { |
289 case DONT_CACHE: | 289 case DONT_CACHE: |
290 /* nothing special */ | 290 /* nothing special */ |
291 break; | 291 break; |
309 default: | 309 default: |
310 TRACE_DEBUG(INFO, "Invalid redirect type was saved"); | 310 TRACE_DEBUG(INFO, "Invalid redirect type was saved"); |
311 ASSERT(0); | 311 ASSERT(0); |
312 return EINVAL; | 312 return EINVAL; |
313 } | 313 } |
314 | 314 |
315 free(e->from.s); | 315 free(e->from.s); |
316 | 316 |
317 free(e); | 317 free(e); |
318 return 0; | 318 return 0; |
319 } | 319 } |