Merge commit 'origin/ser_modules'
[sip-router] / modules_s / rr / loose.c
1 /*
2  * Route & Record-Route module, loose routing support
3  *
4  * $Id$
5  *
6  * Copyright (C) 2001-2004 FhG Fokus
7  *
8  * This file is part of ser, a free SIP server.
9  *
10  * ser is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version
14  *
15  * For a license to use the ser software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * ser is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  *
29  * History:
30  * ---------
31  * 2003-02-28 scratchpad compatibility abandoned (jiri)
32  * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
33  * 2006-01-09 store user part of Route URI in AVP (if required) (mma)
34  * 2009-05-26 Force the send socket if two RR headers were added (Miklos)
35  */
36
37
38 #include <string.h>
39 #include "../../ut.h"
40 #include "../../str.h"
41 #include "../../dprint.h"
42 #include "../../forward.h"
43 #include "../../data_lump.h"
44 #include "../../parser/parse_rr.h"
45 #include "../../parser/parse_uri.h"
46 #include "../../parser/parse_from.h"
47 #include "../../mem/mem.h"
48 #include "../../dset.h"
49 #include "../../socket_info.h"
50 #include "loose.h"
51 #include "rr_mod.h"
52 #include "avp_cookie.h"
53
54 #define RR_ERROR -1       /* An error occured while processing route set */
55 #define RR_DRIVEN 1       /* The next hop is determined from the route set */
56 #define NOT_RR_DRIVEN -1  /* The next hop is not determined from the route set */
57
58 #define MY_RT_PREFIX ROUTE_PREFIX "<"
59 #define MY_RT_PREFIX_LEN (sizeof(MY_RT_PREFIX)-1)
60
61 #define MY_RT_SUFFIX ">\r\n"
62 #define MY_RT_SUFFIX_LEN (sizeof(MY_RT_SUFFIX)-1)
63
64
65 /*
66  * Check if the To-tag is set in the message
67  */
68 static int has_to_tag(struct sip_msg* msg)
69 {
70         str tag;
71
72         if (!msg->to && parse_headers(msg, HDR_TO_F, 0) == -1) {
73                 LOG(L_ERR, "has_to_tag: Cannot parse To header field\n");
74                 return -1;
75         }
76
77         if (!msg->to) {
78                 LOG(L_ERR, "has_to_tag: To header field not found\n");
79                 return -1;
80         }
81
82         tag = get_to(msg)->tag_value;
83         if (tag.s == 0 || tag.len == 0) {
84                 DBG("has_to_tag: No\n");
85                 return 0;
86         }
87
88         DBG("has_to_tag: Yes\n");
89         return 1;
90 }
91
92
93 /*
94  * Parse the message and find first occurrence of
95  * Route header field. The function returns -1 or -2
96  * on a parser error, 0 if there is a Route HF and
97  * 1 if there is no Route HF.
98  */
99 static inline int find_first_route(struct sip_msg* _m)
100 {
101         if (parse_headers(_m, HDR_ROUTE_F, 0) == -1) {
102                 LOG(L_ERR, "find_first_route: Error while parsing headers\n");
103                 return -1;
104         } else {
105                 if (_m->route) {
106                         if (parse_rr(_m->route) < 0) {
107                                 LOG(L_ERR, "find_first_route: Error while parsing Route HF\n");
108                                 return -2;
109                         }
110                         return 0;
111                 } else {
112                         DBG("find_first_route: No Route headers found\n");
113                         return 1;
114                 }
115         }
116 }
117
118
119 /*
120  * Check if URI is myself
121  */
122 static inline int is_myself(str* _host, unsigned short _port)
123 {
124     str did;
125     int ret;
126     
127     ret = check_self(_host, _port ? _port : SIP_PORT, 0);/* match all protos*/
128     if (ret < 0) return 0;
129     
130     if (ret == 0 && dm_get_did) {
131         ret = dm_get_did(&did, _host);
132         if (ret < 0) return 0;
133         else if (ret > 0) {
134             /* as the domain module does not know anything about ports
135                lets check if the port matches any of our listening ports */
136             ret = check_self_port(_port ? _port : SIP_PORT, 0);
137         }
138     }
139
140     return ret;
141 }
142
143 static inline void store_user_in_avps(str* user)
144 {
145         avp_value_t val;
146
147         if (user_part_avp_ident.name.s.s && user_part_avp_ident.name.s.len && user && user->s && user->len) {
148                 val.s = *user;
149                 add_avp(user_part_avp_ident.flags | AVP_NAME_STR | AVP_VAL_STR, user_part_avp_ident.name, val);
150         }       
151         
152 }
153
154
155 static inline void store_next_route_in_avps(str* uri)
156 {
157         avp_value_t val;
158
159         if (next_route_avp_ident.name.s.s && next_route_avp_ident.name.s.len && uri && uri->s && uri->len) {
160                 val.s = *uri;
161                 add_avp(next_route_avp_ident.flags | AVP_NAME_STR | AVP_VAL_STR, next_route_avp_ident.name, val);
162         }       
163         
164 }
165
166 /*
167  * Find and parse next Route header field
168  */
169 static inline int find_next_route(struct sip_msg* _m, struct hdr_field** _hdr)
170 {
171         struct hdr_field* ptr;
172
173         ptr = (*_hdr)->next;
174
175              /* Try to find already parsed Route headers */
176         while(ptr) {
177                 if (ptr->type == HDR_ROUTE_T) goto found;
178                 ptr = ptr->next;
179         }
180
181              /* There are no already parsed Route headers, try to find next
182               * occurrence of Route header
183               */
184         if (parse_headers(_m, HDR_ROUTE_F, 1) == -1) {
185                 LOG(L_ERR, "find_next_route: Error while parsing headers\n");
186                 return -1;
187         }
188
189         if ((_m->last_header->type != HDR_ROUTE_T) || (_m->last_header == *_hdr)) {
190                 DBG("find_next_route: No next Route HF found\n");
191                 return 1;
192         }
193
194         ptr = _m->last_header;
195
196  found:
197         if (parse_rr(ptr) < 0) {
198                 LOG(L_ERR, "find_next_route: Error while parsing Route body\n");
199                 return -2;
200         }
201
202         *_hdr = ptr;
203         return 0;
204 }
205
206
207 /*
208  * Check if the given uri contains lr parameter which marks loose routers
209  */
210 static inline int is_strict(str* _params)
211 {
212         str s;
213         int i, state = 0;
214
215         if (_params->len == 0) return 1;
216
217         s.s = _params->s;
218         s.len = _params->len;
219
220         for(i = 0; i < s.len; i++) {
221                 switch(state) {
222                 case 0:
223                         switch(s.s[i]) {
224                         case ' ':
225                         case '\r':
226                         case '\n':
227                         case '\t':           break;
228                         case 'l':
229                         case 'L': state = 1; break;
230                         default:  state = 4; break;
231                         }
232                         break;
233
234                 case 1:
235                         switch(s.s[i]) {
236                         case 'r':
237                         case 'R': state = 2; break;
238                         default:  state = 4; break;
239                         }
240                         break;
241
242                 case 2:
243                         switch(s.s[i]) {
244                         case ';':  return 0;
245                         case '=':  return 0;
246                         case ' ':
247                         case '\r':
248                         case '\n':
249                         case '\t': state = 3; break;
250                         default:   state = 4; break;
251                         }
252                         break;
253
254                 case 3:
255                         switch(s.s[i]) {
256                         case ';':  return 0;
257                         case '=':  return 0;
258                         case ' ':
259                         case '\r':
260                         case '\n':
261                         case '\t': break;
262                         default:   state = 4; break;
263                         }
264                         break;
265
266                 case 4:
267                         switch(s.s[i]) {
268                         case '\"': state = 5; break;
269                         case ';':  state = 0; break;
270                         default:              break;
271                         }
272                         break;
273
274                 case 5:
275                         switch(s.s[i]) {
276                         case '\\': state = 6; break;
277                         case '\"': state = 4; break;
278                         default:              break;
279                         }
280                         break;
281
282                 case 6: state = 5; break;
283                 }
284         }
285
286         if ((state == 2) || (state == 3)) return 0;
287         else return 1;
288 }
289
290 /* check direction using ftag */
291 static int get_direction(struct sip_msg* _m, str* _params) {
292         str s, ftag;
293         int i, state = 0;
294         
295         if (_params->len == 0) return 0;
296
297         ftag.len = 0;
298         ftag.s = 0;
299         s.s = _params->s;
300         s.len = _params->len;
301
302         for(i = 0; i < s.len; i++) {
303                 switch(state) {
304                         case 0:
305                                 switch(s.s[i]) {
306                                         case ' ':
307                                         case '\r':
308                                         case '\n':
309                                         case '\t':           break;
310                                         case 'f':
311                                         case 'F': state = 1; break;
312                                         default:  state = 8; break;
313                                 }
314                                 break;
315
316                         case 1:
317                                 switch(s.s[i]) {
318                                         case 't':
319                                         case 'T': state = 2; break;
320                                         default:  state = 8; break;
321                                 }
322                                 break;
323
324                         case 2:
325                                 switch(s.s[i]) {
326                                         case 'a':
327                                         case 'A': state = 3; break;
328                                         default:  state = 8; break;
329                                 }
330                                 break;
331
332                         case 3:
333                                 switch(s.s[i]) {
334                                         case 'g':
335                                         case 'G': state = 4; break;
336                                         default:  state = 8; break;
337                                 }
338                                 break;
339                         case 4:
340                                 switch(s.s[i]) {
341                                         case ';':  return 0;
342                                         case '=':  state = 6; break;
343                                         case ' ':
344                                         case '\r':
345                                         case '\n':
346                                         case '\t': state = 5; break;
347                                         default:   state = 8; break;
348                                 }
349                                 break;
350                         case 5:
351                                 switch(s.s[i]) {
352                                         case ';':  return 0;
353                                         case '=':  state = 6; break;
354                                         case ' ':
355                                         case '\r':
356                                         case '\n':
357                                         case '\t': break;
358                                         default:   state = 8; break;
359                                 }
360                                 break;
361
362                         case 6:
363                                 switch(s.s[i]) {
364                                         case '\"': state=101; break;
365                                         case ';':  return 0;
366                                         case ' ':
367                                         case '\r':
368                                         case '\n':
369                                         case '\t': break;
370                                         default:   state = 100; ftag.s = s.s+i; break;
371                                 }
372                                 break;
373
374                         case 8:
375                                 switch(s.s[i]) {
376                                         case '\"': state = 9; break;
377                                         case ';':  state = 0; break;
378                                         default:              break;
379                                 }
380                                 break;
381
382                         case 9:
383                                 switch(s.s[i]) {
384                                         case '\\': state = 10; break;
385                                         case '\"': state = 8; break;
386                                         default:              break;
387                                 }
388                                 break;
389
390                         case 10: state = 9;
391                                 break;
392
393                         case 100:
394                                 switch(s.s[i]) {
395                                         case '\"':  return 0;
396                                         case ';':
397                                         case ' ':
398                                         case '\r':
399                                         case '\n':
400                                         case '\t': ftag.len = s.s+i - ftag.s; 
401                                                     i = s.len;
402                                                     state = 0;
403                                                     break;
404                                 }
405                                 break;
406
407                         case 101:       /* no escape chars supported in base64 algorithm */
408                                 switch(s.s[i]) {
409                                         case '\"': ftag.len = s.s+i - ftag.s; break;
410                                         case '\\': state = 102; break;
411                                         default:                break;
412                                 }
413                                 break;
414
415                         case 102:
416                                 state = 101;
417                                 break;
418                 }
419         }
420
421         if (state == 100)
422                 ftag.len = s.s+i - ftag.s;
423
424         if (ftag.len) {
425                 if ((parse_from_header(_m)==0) && get_from(_m)) {
426                         /* compare if from.tag == ftag */
427                         if (ftag.len!=get_from(_m)->tag_value.len || strncmp(ftag.s, get_from(_m)->tag_value.s, ftag.len)) return 1;
428                 }
429         }
430         return 0;
431 }
432
433 static void get_avp_cookie_from_uri(str* _params, str *_avp_cookie) {
434         str s;
435         int i, state = 0;
436
437         _avp_cookie->len = 0;
438         _avp_cookie->s = 0;
439         if (_params->len == 0) return;
440
441         s.s = _params->s;
442         s.len = _params->len;
443
444         for(i = 0; i < s.len; i++) {
445                 switch(state) {
446                         case 0:
447                                 switch(s.s[i]) {
448                                         case ' ':
449                                         case '\r':
450                                         case '\n':
451                                         case '\t':           break;
452                                         case 'a':
453                                         case 'A': state = 1; break;
454                                         default:  state = 7; break;
455                                 }
456                                 break;
457
458                         case 1:
459                                 switch(s.s[i]) {
460                                         case 'v':
461                                         case 'V': state = 2; break;
462                                         default:  state = 7; break;
463                                 }
464                                 break;
465
466                         case 2:
467                                 switch(s.s[i]) {
468                                         case 'p':
469                                         case 'P': state = 3; break;
470                                         default:  state = 7; break;
471                                 }
472                                 break;
473
474                         case 3:
475                                 switch(s.s[i]) {
476                                         case ';':  return;
477                                         case '=':  state = 5; break;
478                                         case ' ':
479                                         case '\r':
480                                         case '\n':
481                                         case '\t': state = 4; break;
482                                         default:   state = 7; break;
483                                 }
484                                 break;
485                         case 4:
486                                 switch(s.s[i]) {
487                                         case ';':  return;
488                                         case '=':  state = 5; break;
489                                         case ' ':
490                                         case '\r':
491                                         case '\n':
492                                         case '\t': break;
493                                         default:   state = 7; break;
494                                 }
495                                 break;
496
497                         case 5:
498                                 switch(s.s[i]) {
499                                         case '\"': state=101; break;
500                                         case ';':  return;
501                                         case ' ':
502                                         case '\r':
503                                         case '\n':
504                                         case '\t': break;
505                                         default:   state = 100; _avp_cookie->s = s.s+i; break;
506                                 }
507                                 break;
508
509                         case 7:
510                                 switch(s.s[i]) {
511                                         case '\"': state = 8; break;
512                                         case ';':  state = 0; break;
513                                         default:              break;
514                                 }
515                                 break;
516
517                         case 8:
518                                 switch(s.s[i]) {
519                                         case '\\': state = 9; break;
520                                         case '\"': state = 7; break;
521                                         default:              break;
522                                 }
523                                 break;
524
525                         case 9: state = 8;
526                                 break;
527
528                         case 100:
529                                 switch(s.s[i]) {
530                                         case '\"':  return;
531                                         case ';':
532                                         case ' ':
533                                         case '\r':
534                                         case '\n':
535                                         case '\t': _avp_cookie->len = s.s+i - _avp_cookie->s; 
536                                                     i = s.len;
537                                                     state = 0;
538                                                     break;
539                                 }
540                                 break;
541
542                         case 101:       /* no escape chars supported in base64 algorithm */
543                                 switch(s.s[i]) {
544                                         case '\"': _avp_cookie->len = s.s+i - _avp_cookie->s; break;
545                                         case '\\':  state = 102; break;
546                                         default:                 break;
547                                 }
548                                 break;
549
550                         case 102:
551                                 state = 101;
552                                 break;
553                 }
554         }
555
556         if (state == 100)
557                 _avp_cookie->len = s.s+i - _avp_cookie->s;
558 }
559
560 /*
561  * Put Request-URI as last Route header of a SIP
562  * message, this is necessary when forwarding to
563  * a strict router
564  */
565 static inline int save_ruri(struct sip_msg* _m)
566 {
567         struct lump* anchor;
568         char *s;
569         int len;
570
571              /* We must parse the whole message header here,
572               * because Request-URI must be saved in last
573               * Route HF in the message
574               */
575         if (parse_headers(_m, HDR_EOH_F, 0) == -1) {
576                 LOG(L_ERR, "save_ruri: Error while parsing message\n");
577                 return -1;
578         }
579
580              /* Create an anchor */
581         anchor = anchor_lump(_m, _m->unparsed - _m->buf, 0, 0);
582         if (anchor == 0) {
583                 LOG(L_ERR, "save_ruri: Can't get anchor\n");
584                 return -2;
585         }
586
587              /* Create buffer for new lump */
588         len = MY_RT_PREFIX_LEN + _m->first_line.u.request.uri.len + MY_RT_SUFFIX_LEN;
589         s = (char*)pkg_malloc(len);
590         if (!s) {
591                 LOG(L_ERR, "save_ruri: No memory left\n");
592                 return -3;
593         }
594
595              /* Create new header field */
596         memcpy(s, MY_RT_PREFIX, MY_RT_PREFIX_LEN);
597         memcpy(s + MY_RT_PREFIX_LEN, _m->first_line.u.request.uri.s, _m->first_line.u.request.uri.len);
598         memcpy(s + MY_RT_PREFIX_LEN + _m->first_line.u.request.uri.len, MY_RT_SUFFIX, MY_RT_SUFFIX_LEN);
599
600         DBG("save_ruri: New header: '%.*s'\n", len, ZSW(s));
601
602              /* Insert it */
603         if (insert_new_lump_before(anchor, s, len, 0) == 0) {
604                 pkg_free(s);
605                 LOG(L_ERR, "save_ruri: Cannot insert lump\n");
606                 return -4;
607         }
608
609         return 0;
610 }
611
612
613 /*
614  * Logic necessary to forward request to strict routers
615  *
616  * Returns 0 on success, negative number on an error
617  */
618 static inline int handle_sr(struct sip_msg* _m, struct hdr_field* _hdr, rr_t* _r)
619 {
620         str* uri;
621         char* rem_off;
622         int rem_len;
623
624         uri = &_r->nameaddr.uri;
625
626              /* Next hop is strict router, save R-URI here */
627         if (save_ruri(_m) < 0) {
628                 LOG(L_ERR, "handle_sr: Error while saving Request-URI\n");
629                 return -1;
630         }
631
632              /* Put the first Route in Request-URI */
633         if (rewrite_uri(_m, uri) < 0) {
634                 LOG(L_ERR, "handle_sr: Error while rewriting request URI\n");
635                 return -2;
636         }
637
638         if (!_r->next) {
639                 rem_off = _hdr->name.s;
640                 rem_len = _hdr->len;
641         } else {
642                 rem_off = _hdr->body.s;
643                 rem_len = _r->next->nameaddr.name.s - _hdr->body.s;
644         }
645
646         if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
647                 LOG(L_ERR, "handle_sr: Can't remove Route HF\n");
648                 return -9;
649         }
650
651         return 0;
652 }
653
654
655 /*
656  * Find last route in the last Route header field,
657  * if there was a previous route in the last Route header
658  * field, it will be saved in _p parameter
659  */
660 static inline int find_rem_target(struct sip_msg* _m, struct hdr_field** _h, rr_t** _l, rr_t** _p)
661 {
662         struct hdr_field* ptr, *last;
663
664         if (parse_headers(_m, HDR_EOH_F, 0) == -1) {
665                 LOG(L_ERR, "find_rem_target: Error while parsing message header\n");
666                 return -1;
667         }
668
669         ptr = _m->route;
670         last = 0;
671
672         while(ptr) {
673                 if (ptr->type == HDR_ROUTE_T) last = ptr;
674                 ptr = ptr->next;
675         }
676
677         if (last) {
678                 if (parse_rr(last) < 0) {
679                         LOG(L_ERR, "find_rem_target: Error while parsing last Route HF\n");
680                         return -2;
681                 }
682
683                 *_h = last;
684                 *_p = 0;
685                 *_l = (rr_t*)last->parsed;
686                 while ((*_l)->next) {
687                         *_p = *_l;
688                         *_l = (*_l)->next;
689                 }
690                 return 0;
691         } else {
692                 LOG(L_ERR, "find_rem_target: Can't find last Route HF\n");
693                 return 1;
694         }
695 }
696
697
698 /*
699  * Previous hop was a strict router, handle this case
700  */
701 static inline int after_strict(struct sip_msg* _m, struct sip_uri* _pru, int _route_myself)
702 {
703         int res, rem_len;
704         struct hdr_field* hdr;
705         rr_t* rt, *prev;
706         char* rem_off;
707         str* uri;
708         str avp_cookie;
709
710         get_avp_cookie_from_uri(&_m->parsed_uri.params, &avp_cookie);
711         if (avp_cookie.len > 0)
712                 rr_set_avp_cookies(&avp_cookie, get_direction(_m, &_m->parsed_uri.params));
713
714         hdr = _m->route;
715         rt = (rr_t*)hdr->parsed;
716
717         if (_route_myself == 1) {
718                 store_user_in_avps(&(_pru->user));
719
720                 if (!rt->next) {
721                              /* No next route in the same header, remove the whole header
722                               * field immediately
723                               */
724                         if (!del_lump(_m, hdr->name.s - _m->buf, hdr->len, 0)) {
725                                 LOG(L_ERR, "after_strict: Cannot remove Route HF\n");
726                                 return RR_ERROR;
727                         }
728                         res = find_next_route(_m, &hdr);
729                         if (res < 0) {
730                                 LOG(L_ERR, "after_strict: Error while searching next route\n");
731                                 return RR_ERROR;
732                         }
733                         if (res > 0) { /* No next route found */
734                                 DBG("after_strict: No next URI found\n");
735                                 return NOT_RR_DRIVEN;
736                         }
737                         rt = (rr_t*)hdr->parsed;
738                 } else rt = rt->next;
739         }
740
741         if (rt != _m->route->parsed) {
742                 uri = &rt->nameaddr.uri;
743                 if (parse_uri(uri->s, uri->len, _pru) == -1) {
744                         LOG(L_ERR, "after_strict: Error while parsing URI\n");
745                         return RR_ERROR;
746                 }
747         } else {
748                 uri = &rt->nameaddr.uri;
749         }
750
751         store_next_route_in_avps(uri);
752         if (is_strict(&(_pru->params))) {
753                 DBG("after_strict: Next hop: '%.*s' is strict router\n", uri->len, ZSW(uri->s));
754                      /* Previous hop was a strict router and the next hop is strict
755                       * router too. There is no need to save R-URI again because it
756                       * is saved already. In fact, in this case we will behave exactly
757                       * like a strict router.
758                       */
759
760                      /* Note: when there is only one Route URI left (endpoint), it will
761                       * always be a strict router because endpoints don't use ;lr parameter
762                       * In this case we will simply put the URI in R-URI and forward it, which
763                       * will work perfectly
764                       */
765                 if (rewrite_uri(_m, uri) < 0) {
766                         LOG(L_ERR, "after_strict: Error while rewriting request URI\n");
767                         return RR_ERROR;
768                 }
769
770                 if (rt->next) {
771                         rem_off = hdr->body.s;
772                         rem_len = rt->next->nameaddr.name.s - hdr->body.s;
773                 } else {
774                         rem_off = hdr->name.s;
775                         rem_len = hdr->len;
776                 }
777                 if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
778                         LOG(L_ERR, "after_strict: Cannot remove Route HF\n");
779                         return RR_ERROR;
780                 }
781         } else {
782                 DBG("after_strict: Next hop: '%.*s' is loose router\n", uri->len, ZSW(uri->s));
783
784                 if (set_dst_uri(_m, uri) < 0) {
785                         LOG(L_ERR, "after_strict: Error while setting dst_uri\n");
786                         return RR_ERROR;
787                 }
788
789                      /* Next hop is a loose router - Which means that is is not endpoint yet
790                       * In This case we have to recover from previous strict routing, that
791                       * means we have to find the last Route URI and put in in R-URI and
792                       * remove the last Route URI.
793                       */
794                 if (rt != hdr->parsed) {
795                              /* There is a previous route uri which was 2nd uri of mine
796                               * and must be removed here
797                               */
798                         rem_off = hdr->body.s;
799                         rem_len = rt->nameaddr.name.s - hdr->body.s;
800                         if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
801                                 LOG(L_ERR, "after_strict: Can't remove Route HF\n");
802                                 return RR_ERROR;
803                         }
804                 }
805
806
807                 res = find_rem_target(_m, &hdr, &rt, &prev);
808                 if (res < 0) {
809                         LOG(L_ERR, "after_strict: Error while looking for last Route URI\n");
810                         return RR_ERROR;
811                 } else if (res > 0) {
812                              /* No remote target is an error */
813                         return RR_ERROR;
814                 }
815
816                 uri = &rt->nameaddr.uri;
817                 if (rewrite_uri(_m, uri) < 0) {
818                         LOG(L_ERR, "after_strict: Can't rewrite R-URI\n");
819                         return RR_ERROR;
820                 }
821
822                      /* The first character if uri will be either '<' when it is the only URI in a
823                       * Route header field or ',' if there is more than one URI in the header field
824                       */
825                 DBG("after_strict: The last route URI: '%.*s'\n",
826                     rt->nameaddr.uri.len, ZSW(rt->nameaddr.uri.s));
827
828                 if (prev) {
829                         rem_off = prev->nameaddr.name.s + prev->len;
830                         rem_len = rt->nameaddr.name.s + rt->len - rem_off;
831                 } else {
832                         rem_off = hdr->name.s;
833                         rem_len = hdr->len;
834                 }
835                 if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
836                         LOG(L_ERR, "after_strict: Can't remove Route HF\n");
837                         return RR_ERROR;
838                 }
839         }
840
841         return RR_DRIVEN;
842 }
843
844
845 static inline int after_loose(struct sip_msg* _m, struct sip_uri* _pru, int _route_myself, int _ruri_myself)
846 {
847         struct hdr_field* hdr;
848         rr_t* rt;
849         int res;
850 #ifdef ENABLE_USER_CHECK
851         int ret;
852 #endif
853         str* uri;
854         str avp_cookie;
855         struct sip_uri parsed_uri;
856
857         hdr = _m->route;
858         rt = (rr_t*)hdr->parsed;
859         uri = &rt->nameaddr.uri;
860
861              /* IF the URI was added by me, remove it */
862         if (_route_myself == 1) {
863                 store_user_in_avps(&(_pru->user));
864
865                 DBG("after_loose: Topmost route URI: '%.*s' is me\n", uri->len, ZSW(uri->s));
866                 get_avp_cookie_from_uri(&(_pru->params), &avp_cookie);
867                 if (avp_cookie.len > 0)
868                         rr_set_avp_cookies(&avp_cookie, get_direction(_m, &(_pru->params)));
869                 if (!rt->next) {
870                              /* No next route in the same header, remove the whole header
871                               * field immediately
872                               */
873                         if (!del_lump(_m, hdr->name.s - _m->buf, hdr->len, 0)) {
874                                 LOG(L_ERR, "after_loose: Can't remove Route HF\n");
875                                 return RR_ERROR;
876                         }
877                         res = find_next_route(_m, &hdr);
878                         if (res < 0) {
879                                 LOG(L_ERR, "after_loose: Error while finding next route\n");
880                                 return RR_ERROR;
881                         }
882                         if (res > 0) { /* No next route found */
883                                 DBG("after_loose: No next URI found\n");
884                                 if (_ruri_myself == 1) {
885                                         /* this a preloaded request. we do not check for the To-tag
886                                          * because the ACK for a negative reply will contain such
887                                          * a tag but the original INVITE not */
888                                         return NOT_RR_DRIVEN;
889                                 } else {
890                                         return RR_DRIVEN;
891                                 }
892                         }
893                         rt = (rr_t*)hdr->parsed;
894                 } else rt = rt->next;
895
896                 if (enable_double_rr && is_2rr(&(_pru->params))) {
897                         if (rr_force_send_socket) {
898                                 if (parse_uri(rt->nameaddr.uri.s, rt->nameaddr.uri.len,
899                                                 &parsed_uri) < 0
900                                 ) {
901                                         LOG(L_ERR, "after_loose: Error while parsing the second route header\n");
902                                         return RR_ERROR;
903                                 }
904                                 _m->force_send_socket = grep_sock_info(&parsed_uri.host,
905                                                                 parsed_uri.port_no,
906                                                                 parsed_uri.proto);
907                                 if (_m->force_send_socket == 0)
908                                         LOG(L_WARN, "after_loose: send socket cannot be set"
909                                                 " based on the second route header\n");
910                                         /* Do not return error because there is still a chance
911                                          * that the outgoing socket will be correct, especially
912                                          * if mhomed is turned on. It can happen that the Route HF
913                                          * contains a domain name as opposed to ip address therefore
914                                          * the outgoing socket cannot be determined (easily) from the URI.
915                                          * (Miklos)
916                                          */
917                         }
918                         if (!rt->next) {
919                                      /* No next route in the same header, remove the whole header
920                                       * field immediately
921                                       */
922                                 if (!del_lump(_m, hdr->name.s - _m->buf, hdr->len, 0)) {
923                                         LOG(L_ERR, "after_loose: Can't remove Route HF\n");
924                                         return RR_ERROR;
925                                 }
926                                 res = find_next_route(_m, &hdr);
927                                 if (res < 0) {
928                                         LOG(L_ERR, "after_loose: Error while finding next route\n");
929                                         return RR_ERROR;
930                                 }
931                                 if (res > 0) { /* No next route found */
932                                         DBG("after_loose: No next URI found\n");
933                                         /* preloaded routes can not happen with double_rr, so
934                                          * we were just the last hop with double_rr */
935                                         return RR_DRIVEN;
936                                 }
937                                 rt = (rr_t*)hdr->parsed;
938                         } else rt = rt->next;
939                 }
940
941                 if(rt != _m->route->parsed) {
942                         uri = &rt->nameaddr.uri;
943                         if (parse_uri(uri->s, uri->len, _pru) < 0) {
944                                 LOG(L_ERR, "after_loose: Error while parsing the next route URI\n");
945                                 return RR_ERROR;
946                         }
947                 }
948         } else {
949 #ifdef ENABLE_USER_CHECK
950                 /* check if it the ignored user */
951                 if(ret < 0)
952                         return NOT_RR_DRIVEN;
953 #endif
954                 DBG("after_loose: Topmost URI is NOT myself\n");
955         }
956         
957         store_next_route_in_avps(uri);
958         DBG("after_loose: URI to be processed: '%.*s'\n", uri->len, ZSW(uri->s));
959         if (is_strict(&(_pru->params))) {
960                 DBG("after_loose: Next URI is a strict router\n");
961                 if (handle_sr(_m, hdr, rt) < 0) {
962                         LOG(L_ERR, "after_loose: Error while handling strict router\n");
963                         return RR_ERROR;
964                 }
965         } else {
966                      /* Next hop is loose router */
967                 DBG("after_loose: Next URI is a loose router\n");
968
969                 if (set_dst_uri(_m, uri) < 0) {
970                         LOG(L_ERR, "after_loose: Error while setting dst_uri\n");
971                         return RR_ERROR;
972                 }
973
974                      /* There is a previous route uri which was 2nd uri of mine
975                       * and must be removed here
976                       */
977                 if (rt != hdr->parsed) {
978                         if (!del_lump(_m, hdr->body.s - _m->buf, rt->nameaddr.name.s - hdr->body.s, 0)) {
979                                 LOG(L_ERR, "after_loose: Can't remove Route HF\n");
980                                 return RR_ERROR;
981                         }
982                 }
983         }
984
985         return RR_DRIVEN;
986 }
987
988
989 /*
990  * Do loose routing as defined in RFC3621
991  */
992 int loose_route(struct sip_msg* _m, char* _s1, char* _s2)
993 {
994         struct hdr_field* hdr;
995         struct sip_uri puri;
996         rr_t* rt;
997         int ret;
998         str* uri;
999
1000         if (find_first_route(_m) != 0) {
1001                 DBG("loose_route: There is no Route HF\n");
1002                 return -1;
1003         }
1004
1005         if (parse_sip_msg_uri(_m) == -1) {
1006                 LOG(L_ERR, "loose_route: Error while parsing Request URI\n");
1007                 return -1;
1008         }
1009
1010         hdr = _m->route;
1011         rt = (rr_t*)hdr->parsed;
1012         uri = &rt->nameaddr.uri;
1013
1014         if (parse_uri(uri->s, uri->len, &puri) < 0) {
1015                 LOG(L_ERR, "loose_route: Error while parsing the first route URI\n");
1016                 return -1;
1017         }
1018
1019         if (is_myself(&_m->parsed_uri.host, _m->parsed_uri.port_no)) {
1020                 DBG("loose_route: RURI is myself\n");
1021                 if ((ret = is_myself(&puri.host, puri.port_no)) == 1 &&
1022                         !(enable_double_rr && is_2rr(&puri.params))) {
1023                         DBG("loose_route: found preloaded loose route\n");
1024                         return after_loose(_m, &puri, ret, 1);
1025                 } else {
1026                         if (has_to_tag(_m) == 1) {
1027                                 return after_strict(_m, &puri, ret);
1028                         } else {
1029                                 LOG(L_WARN, "loose_route: pre-loaded strict routing?!\n");
1030                                 return -1;
1031                         }
1032                 }
1033         } else {
1034                 DBG("loose_route: RURI is NOT myself\n");
1035                 if (is_myself(&puri.host, puri.port_no)) {
1036                         return after_loose(_m, &puri, 1, 0);
1037                 } else {
1038                         store_next_route_in_avps(uri);
1039                         LOG(L_WARN, "loose_route: no routing target is local\n");
1040                         return -1;
1041                 }
1042         }
1043 }