rr: catch uri without host part when matching local addresses
[kamailio] / src / modules / rr / loose.c
1 /*
2  * Copyright (C) 2001-2004 FhG Fokus
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  * Kamailio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License 
17  * along with this program; if not, write to the Free Software 
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 /*!
22  * \file
23  * \brief Route & Record-Route module, loose routing support
24  * \ingroup rr
25  */
26
27
28
29 #include <string.h>
30 #include "../../core/ut.h"
31 #include "../../core/str.h"
32 #include "../../core/dprint.h"
33 #include "../../core/forward.h"
34 #include "../../core/data_lump.h"
35 #include "../../core/socket_info.h"
36 #include "../../core/parser/parse_rr.h"
37 #include "../../core/parser/parse_uri.h"
38 #include "../../core/parser/parse_from.h"
39 #include "../../core/parser/parse_param.h"
40 #include "../../core/mem/mem.h"
41 #include "../../core/dset.h"
42 #include "loose.h"
43 #include "rr_cb.h"
44 #include "rr_mod.h"
45
46
47 #define RR_ROUTE_PREFIX ROUTE_PREFIX "<"
48 #define RR_ROUTE_PREFIX_LEN (sizeof(RR_ROUTE_PREFIX)-1)
49
50 #define ROUTE_SUFFIX ">\r\n"  /*!< SIP header suffix */
51 #define ROUTE_SUFFIX_LEN (sizeof(ROUTE_SUFFIX)-1)
52
53 /*! variables used to hook the param part of the local route */
54 static msg_ctx_id_t routed_msg_id = {0};
55 static str routed_params = {0,0};
56
57 extern int rr_force_send_socket;
58 extern int rr_sockname_mode;
59
60 /*!
61  * \brief Test whether we are processing pre-loaded route set by looking at the To tag
62  * \param msg SIP message
63  * \return -1 on failure, 0 on success
64  */
65 static int is_preloaded(struct sip_msg* msg)
66 {
67         str tag;
68
69         if (!msg->to && parse_headers(msg, HDR_TO_F, 0) == -1) {
70                 LM_ERR("failed to parse To header field\n");
71                 return -1;
72         }
73
74         if (!msg->to) {
75                 LM_ERR("To header field not found\n");
76                 return -1;
77         }
78
79         tag = get_to(msg)->tag_value;
80         if (tag.s == 0 || tag.len == 0) {
81                 LM_DBG("is_preloaded: Yes\n");
82                 return 1;
83         }
84
85         LM_DBG("is_preloaded: No\n");
86         return 0;
87 }
88
89
90 /*!
91  * \brief Parse the message and find first occurrence of Route header field.
92  * \param _m SIP message 
93  * \return -1 or -2 on a parser error, 0 if there is a Route HF and 1 if there is no Route HF
94  */
95 static inline int find_first_route(struct sip_msg* _m)
96 {
97         if (parse_headers(_m, HDR_ROUTE_F, 0) == -1) {
98                 LM_ERR("failed to parse headers\n");
99                 return -1;
100         } else {
101                 if (_m->route) {
102                         if (parse_rr(_m->route) < 0) {
103                                 LM_ERR("failed to parse Route HF\n");
104                                 return -2;
105                         }
106                         return 0;
107                 } else {
108                         LM_DBG("No Route headers found\n");
109                         return 1;
110                 }
111         }
112 }
113
114
115 /*!
116  * \brief Check if URI is myself
117  * \param _host host
118  * \param _port port
119  * \return 0 if the URI is not myself, 1 otherwise
120  */
121 static inline int is_myself(sip_uri_t *_puri)
122 {
123         int ret;
124
125         if(_puri->host.len==0) {
126                 /* catch uri without host (e.g., tel uri) */
127                 return 0;
128         }
129
130         ret = check_self(&_puri->host,
131                         _puri->port_no?_puri->port_no:SIP_PORT, 0);/* match all protos*/
132         if (ret < 0) return 0;
133
134 #ifdef ENABLE_USER_CHECK
135         if(ret==1 && i_user.len && i_user.len==_puri->user.len
136                         && strncmp(i_user.s, _puri->user.s, _puri->user.len)==0)
137         {
138                 LM_DBG("ignore user matched - URI is not to the server itself\n");
139                 return 0;
140         }
141 #endif
142
143         if(ret==1) {
144                 /* match on host:port, but if gruu, then fail */
145                 if(_puri->gr.s!=NULL)
146                         return 0;
147         }
148
149         return ret;
150 }
151
152
153 /*!
154  * \brief Find and parse next Route header field
155  * \param _m SIP message
156  * \param _hdr SIP header
157  * \return negative on failure, 0 if the Route header was already parsed, 1 if no next
158  * Route header could be found
159  */
160 static inline int find_next_route(struct sip_msg* _m, struct hdr_field** _hdr)
161 {
162         struct hdr_field* ptr;
163
164         ptr = (*_hdr)->next;
165
166         /* Try to find already parsed Route headers */
167         while(ptr) {
168                 if (ptr->type == HDR_ROUTE_T) goto found;
169                 ptr = ptr->next;
170         }
171
172         /* There are no already parsed Route headers, try to find next
173          * occurrence of Route header
174          */
175         if (parse_headers(_m, HDR_ROUTE_F, 1) == -1) {
176                 LM_ERR("failed to parse headers\n");
177                 return -1;
178         }
179
180         if ((_m->last_header->type!=HDR_ROUTE_T) || (_m->last_header==*_hdr)) {
181                 LM_DBG("No next Route HF found\n");
182                 return 1;
183         }
184
185         ptr = _m->last_header;
186
187  found:
188         if (parse_rr(ptr) < 0) {
189                 LM_ERR("failed to parse Route body\n");
190                 return -2;
191         }
192
193         *_hdr = ptr;
194         return 0;
195 }
196
197
198 /*!
199  * \brief Check if the given uri contains lr parameter which marks loose routers
200  * \param _params URI string
201  * \return 1 if URI contains no lr parameter, 0 if it contains a lr parameter
202  */
203 static inline int is_strict(str* _params)
204 {
205         str s;
206         int i, state = 0;
207
208         if (_params->len == 0) return 1;
209
210         s.s = _params->s;
211         s.len = _params->len;
212
213         for(i = 0; i < s.len; i++) {
214                 switch(state) {
215                 case 0:
216                         switch(s.s[i]) {
217                         case ' ':
218                         case '\r':
219                         case '\n':
220                         case '\t':           break;
221                         case 'l':
222                         case 'L': state = 1; break;
223                         default:  state = 4; break;
224                         }
225                         break;
226
227                 case 1:
228                         switch(s.s[i]) {
229                         case 'r':
230                         case 'R': state = 2; break;
231                         default:  state = 4; break;
232                         }
233                         break;
234
235                 case 2:
236                         switch(s.s[i]) {
237                         case ';':  return 0;
238                         case '=':  return 0;
239                         case ' ':
240                         case '\r':
241                         case '\n':
242                         case '\t': state = 3; break;
243                         default:   state = 4; break;
244                         }
245                         break;
246
247                 case 3:
248                         switch(s.s[i]) {
249                         case ';':  return 0;
250                         case '=':  return 0;
251                         case ' ':
252                         case '\r':
253                         case '\n':
254                         case '\t': break;
255                         default:   state = 4; break;
256                         }
257                         break;
258
259                 case 4:
260                         switch(s.s[i]) {
261                         case '\"': state = 5; break;
262                         case ';':  state = 0; break;
263                         default:              break;
264                         }
265                         break;
266                         
267                 case 5:
268                         switch(s.s[i]) {
269                         case '\\': state = 6; break;
270                         case '\"': state = 4; break;
271                         default:              break;
272                         }
273                         break;
274
275                 case 6: state = 5; break;
276                 }
277         }
278         
279         if ((state == 2) || (state == 3)) return 0;
280         else return 1;
281 }
282
283
284 /*!
285  * \brief Set Request-URI as last Route header of a SIP
286  *
287  * Set Request-URI as last Route header of a SIP message,
288  * this is necessary when forwarding to a strict router.
289  * Allocates memory for message lump in private memory.
290  * \param _m SIP message
291  * \return negative on failure, 0 on success
292  */
293 static inline int save_ruri(struct sip_msg* _m)
294 {
295         struct lump* anchor;
296         char *s;
297         int len;
298
299         /* We must parse the whole message header here, because
300          * the Request-URI must be saved in last Route HF in the message
301          */
302         if (parse_headers(_m, HDR_EOH_F, 0) == -1) {
303                 LM_ERR("failed to parse message\n");
304                 return -1;
305         }
306
307         /* Create an anchor */
308         anchor = anchor_lump(_m, _m->unparsed - _m->buf, 0, 0);
309         if (anchor == 0) {
310                 LM_ERR("failed to get anchor\n");
311                 return -2;
312         }
313
314         /* Create buffer for new lump */
315         len = RR_ROUTE_PREFIX_LEN + _m->first_line.u.request.uri.len
316                         + ROUTE_SUFFIX_LEN;
317         s = (char*)pkg_malloc(len);
318         if (!s) {
319                 LM_ERR("No memory pkg left\n");
320                 return -3;
321         }
322
323         /* Create new header field */
324         memcpy(s, RR_ROUTE_PREFIX, RR_ROUTE_PREFIX_LEN);
325         memcpy(s + RR_ROUTE_PREFIX_LEN, _m->first_line.u.request.uri.s,
326                         _m->first_line.u.request.uri.len);
327         memcpy(s + RR_ROUTE_PREFIX_LEN + _m->first_line.u.request.uri.len,
328                         ROUTE_SUFFIX, ROUTE_SUFFIX_LEN);
329
330         LM_DBG("New header: '%.*s'\n", len, ZSW(s));
331
332         /* Insert it */
333         if (insert_new_lump_before(anchor, s, len, 0) == 0) {
334                 pkg_free(s);
335                 LM_ERR("failed to insert lump\n");
336                 return -4;
337         }
338
339         return 0;
340 }
341
342
343 /*!
344  * \brief Parse URI and check if it has a maddr parameter
345  * \param uri URI to be checked if it has maddr, and also the output URI
346  * \param puri parsed URI
347  * \return -1 on failure, 0 on success
348  */
349 static inline int get_maddr_uri(str *uri, struct sip_uri *puri)
350 {
351         /* The max length of the maddr parameter is 127 */
352         static char builturi[127+1];
353         struct sip_uri turi;
354
355         if(uri==NULL || uri->s==NULL)
356                 return RR_ERROR;
357         if(puri==NULL)
358         {
359                 if (parse_uri(uri->s, uri->len, &turi) < 0)
360                 {
361                         LM_ERR("failed to parse the URI\n");
362                         return RR_ERROR;
363                 }
364                 puri = &turi;
365         }
366
367         if(puri->maddr.s==NULL)
368                 return 0;
369
370         /* sip: + maddr + : + port */
371         if( (puri->maddr_val.len) > (127 - 10) )
372         {
373                 LM_ERR( "Too long maddr parameter\n");
374                 return RR_ERROR;
375         }
376         memcpy( builturi, "sip:", 4 );
377         memcpy( builturi+4, puri->maddr_val.s, puri->maddr_val.len );
378                 
379         if( puri->port.len > 0 )
380         {
381                 builturi[4+puri->maddr_val.len] =':';
382                 memcpy(builturi+5+puri->maddr_val.len, puri->port.s,
383                                 puri->port.len);
384         }
385
386         uri->len = 4+puri->maddr_val.len
387                                         + ((puri->port.len>0)?(1+puri->port.len):0);
388         builturi[uri->len]='\0';
389         uri->s = builturi;
390
391         LM_DBG("uri is %s\n", builturi );
392         return 0;
393 }
394
395
396 /*!
397  * \brief Necessary logic to forward request to strict routers
398  * \param _m SIP message
399  * \param _hdr SIP header field
400  * \param _r Route & Record-Route header field body
401  * \return 0 on success, negative on an error
402  */
403 static inline int handle_sr(struct sip_msg* _m, struct hdr_field* _hdr, rr_t* _r)
404 {
405         str uri;
406         char* rem_off;
407         int rem_len;
408
409         /* Next hop is strict router, save R-URI here */
410         if (save_ruri(_m) < 0) {
411                 LM_ERR("failed to save Request-URI\n");
412                 return -1;
413         }
414         
415         /* Put the first Route in Request-URI */
416         uri = _r->nameaddr.uri;
417         if(get_maddr_uri(&uri, 0)!=0) {
418                 LM_ERR("failed to check maddr\n");
419                 return RR_ERROR;
420         }
421         if (rewrite_uri(_m, &uri) < 0) {
422                 LM_ERR("failed to rewrite request URI\n");
423                 return -2;
424         }
425
426         if (!_r->next) {
427                 rem_off = _hdr->name.s;
428                 rem_len = _hdr->len;
429         } else {
430                 rem_off = _hdr->body.s;
431                 rem_len = _r->next->nameaddr.name.s - _hdr->body.s;
432         }
433
434         if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
435                 LM_ERR("failed to remove Route HF\n");
436                 return -9;
437         }                       
438
439         return 0;
440 }
441
442
443 /*!
444  * \brief Find last route in the last Route header field
445  *
446  * Find last route in the last Route header field, if there was a previous
447  * route in the last Route header field, it will be saved in _p parameter
448  * \param _m SIP message
449  * \param _h SIP header field
450  * \param _l Route & Record-Route header field body
451  * \param _p Route & Record-Route header field body
452  * \return negative on failure, 0 on success
453  */
454 static inline int find_rem_target(struct sip_msg* _m, struct hdr_field** _h, rr_t** _l, rr_t** _p)
455 {
456         struct hdr_field* ptr, *last;
457
458         if (parse_headers(_m, HDR_EOH_F, 0) == -1) {
459                 LM_ERR("failed to parse message header\n");
460                 return -1;
461         }
462
463         ptr = _m->route;
464         last = 0;
465
466         while(ptr) {
467                 if (ptr->type == HDR_ROUTE_T) last = ptr;
468                 ptr = ptr->next;
469         }
470
471         if (last) {
472                 if (parse_rr(last) < 0) {
473                         LM_ERR("failed to parse last Route HF\n");
474                         return -2;
475                 }
476
477                 *_p = 0;
478                 *_l = (rr_t*)last->parsed;
479                 *_h = last;
480                 while ((*_l)->next) {
481                         *_p = *_l;
482                         *_l = (*_l)->next;
483                 }
484                 return 0;
485         } else {
486                 LM_ERR("search for last Route HF failed\n");
487                 return 1;
488         }
489 }
490
491 /* Largest route URI is of the form:
492         sip:[1234:5678:9012:3456:7890:1234:5678:9012]:12345;transport=sctp
493    this is 66 characters long */
494 #define MAX_ROUTE_URI_LEN       66
495 static char uri_buf[MAX_ROUTE_URI_LEN];
496
497 /*!
498  * \brief Perform outbound processing - force local socket and set destination URI
499  * \param _m SIP message
500  * \param flow_token string containing the flow-token extracted from the Route: header
501  * \param dst_uri string to write the destination URI to (extracted from flow-token)
502  * \return -1 on error, 0 when outbound not in use, 1 when outbound in use
503  */
504 static inline int process_outbound(struct sip_msg *_m, str flow_token)
505 {
506         int ret;
507         struct receive_info *rcv = NULL;
508         struct socket_info *si;
509         str dst_uri;
510
511         if (!rr_obb.decode_flow_token)
512                 return 0;
513
514         ret = rr_obb.decode_flow_token(_m, &rcv, flow_token);
515
516         if (ret == -2) {
517                 LM_DBG("no flow token found - outbound not in use\n");
518                 return 0;
519         } else if (ret == -1) {
520                 LM_INFO("failed to decode flow token\n");
521                 return -1;
522         } else if (!ip_addr_cmp(&rcv->src_ip, &_m->rcv.src_ip)
523                         || rcv->src_port != _m->rcv.src_port) {
524                 LM_DBG("\"incoming\" request found. Using flow-token for "
525                         "routing\n");
526
527                 /* First, force the local socket */
528                 si = find_si(&rcv->dst_ip, rcv->dst_port, rcv->proto);
529                 if (si)
530                         set_force_socket(_m, si);
531                 else {
532                         LM_INFO("cannot find socket from flow-token\n");
533                         return -1;
534                 }
535
536                 /* Second, override the destination URI */
537                 dst_uri.s = uri_buf;
538                 dst_uri.len = 0;
539
540                 dst_uri.len += snprintf(dst_uri.s + dst_uri.len,
541                                         MAX_ROUTE_URI_LEN - dst_uri.len,
542                                         "sip:%s",
543                                         rcv->src_ip.af == AF_INET6 ? "[" : "");
544                 dst_uri.len += ip_addr2sbuf(&rcv->src_ip,
545                                         dst_uri.s + dst_uri.len,
546                                         MAX_ROUTE_URI_LEN - dst_uri.len);
547                 dst_uri.len += snprintf(dst_uri.s + dst_uri.len,
548                                         MAX_ROUTE_URI_LEN - dst_uri.len,
549                                         "%s:%d;transport=%s",
550                                         rcv->src_ip.af == AF_INET6 ? "]" : "",
551                                         rcv->src_port,
552                                         get_proto_name(rcv->proto));
553
554                 if (set_dst_uri(_m, &dst_uri) < 0) {
555                         LM_ERR("failed to set dst_uri\n");
556                         return -1;
557                 }
558                 ruri_mark_new();
559
560                 return 1;
561         }
562
563         LM_DBG("Not using flow-token for routing\n");
564         return 0;
565 }
566
567 /*!
568  * \brief Previous hop was a strict router, handle this case
569  * \param _m SIP message
570  * \return -1 on error, 1 on success
571  */
572 static inline int after_strict(struct sip_msg* _m)
573 {
574         int res, rem_len;
575         struct hdr_field* hdr;
576         struct sip_uri puri;
577         rr_t* rt, *prev;
578         char* rem_off;
579         str uri;
580         struct socket_info *si;
581
582         hdr = _m->route;
583         rt = (rr_t*)hdr->parsed;
584         uri = rt->nameaddr.uri;
585
586         /* reset rr handling static vars for safety in error case */
587         routed_msg_id.msgid = 0;
588         routed_msg_id.pid = 0;
589         routed_params.s = NULL;
590         routed_params.len = 0;
591
592         if (parse_uri(uri.s, uri.len, &puri) < 0) {
593                 LM_ERR("failed to parse the first route URI\n");
594                 return RR_ERROR;
595         }
596
597         if ( enable_double_rr && is_2rr(&puri.params) && is_myself(&puri)) {
598                 /* double route may occure due different IP and port, so force as
599                  * send interface the one advertise in second Route */
600                 si = grep_sock_info( &puri.host, puri.port_no, puri.proto);
601                 if (si) {
602                         set_force_socket(_m, si);
603                 } else {
604                         if (enable_socket_mismatch_warning)
605                                 LM_WARN("no socket found for match second RR\n");
606                 }
607
608                 if (!rt->next) {
609                         /* No next route in the same header, remove the whole header
610                          * field immediately
611                          */
612                         if (!del_lump(_m, hdr->name.s - _m->buf, hdr->len, 0)) {
613                                 LM_ERR("failed to remove Route HF\n");
614                                 return RR_ERROR;
615                         }
616                         res = find_next_route(_m, &hdr);
617                         if (res < 0) {
618                                 LM_ERR("searching next route failed\n");
619                                 return RR_ERROR;
620                         }
621                         if (res > 0) { /* No next route found */
622                                 LM_DBG("after_strict: No next URI found\n");
623                                 return RR_NOT_DRIVEN;
624                         }
625                         rt = (rr_t*)hdr->parsed;
626                 } else rt = rt->next;
627
628                 /* parse the new found uri */
629                 uri = rt->nameaddr.uri;
630                 if (parse_uri(uri.s, uri.len, &puri) < 0) {
631                         LM_ERR("failed to parse URI\n");
632                         return RR_ERROR;
633                 }
634         }
635
636         /* set the hooks for the param
637          * important note: RURI is already parsed by the above function, so 
638          * we just used it without any checking */
639         routed_msg_id.msgid = _m->id;
640         routed_msg_id.pid = _m->pid;
641         routed_params = _m->parsed_uri.params;
642
643         if (is_strict(&puri.params)) {
644                 LM_DBG("Next hop: '%.*s' is strict router\n", uri.len, ZSW(uri.s));
645                 /* Previous hop was a strict router and the next hop is strict
646                  * router too. There is no need to save R-URI again because it
647                  * is saved already. In fact, in this case we will behave exactly
648                  * like a strict router. */
649
650                 /* Note: when there is only one Route URI left (endpoint), it will
651                  * always be a strict router because endpoints don't use ;lr parameter
652                  * In this case we will simply put the URI in R-URI and forward it, 
653                  * which will work perfectly */
654                 if(get_maddr_uri(&uri, &puri)!=0) {
655                         LM_ERR("failed to check maddr\n");
656                         return RR_ERROR;
657                 }
658                 if (rewrite_uri(_m, &uri) < 0) {
659                         LM_ERR("failed to rewrite request URI\n");
660                         return RR_ERROR;
661                 }
662                 
663                 if (rt->next) {
664                         rem_off = hdr->body.s;
665                         rem_len = rt->next->nameaddr.name.s - hdr->body.s;
666                 } else {
667                         rem_off = hdr->name.s;
668                         rem_len = hdr->len;
669                 }
670                 if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
671                         LM_ERR("failed to remove Route HF\n");
672                         return RR_ERROR;
673                 }
674         } else {
675                 LM_DBG("Next hop: '%.*s' is loose router\n",
676                         uri.len, ZSW(uri.s));
677
678                 if(get_maddr_uri(&uri, &puri)!=0) {
679                         LM_ERR("failed to check maddr\n");
680                         return RR_ERROR;
681                 }
682                 if (set_dst_uri(_m, &uri) < 0) {
683                         LM_ERR("failed to set dst_uri\n");
684                         return RR_ERROR;
685                 }
686
687                 /* Next hop is a loose router - Which means that is is not endpoint yet
688                  * In This case we have to recover from previous strict routing, that 
689                  * means we have to find the last Route URI and put in in R-URI and 
690                  * remove the last Route URI. */
691                 if (rt != hdr->parsed) {
692                         /* There is a previous route uri which was 2nd uri of mine
693                          * and must be removed here */
694                         rem_off = hdr->body.s;
695                         rem_len = rt->nameaddr.name.s - hdr->body.s;
696                         if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
697                                 LM_ERR("failed to remove Route HF\n");
698                                 return RR_ERROR;
699                         }
700                 }
701
702
703                 res = find_rem_target(_m, &hdr, &rt, &prev);
704                 if (res < 0) {
705                         LM_ERR("searching for last Route URI failed\n");
706                         return RR_ERROR;
707                 } else if (res > 0) {
708                         /* No remote target is an error */
709                         return RR_ERROR;
710                 }
711
712                 uri = rt->nameaddr.uri;
713                 if(get_maddr_uri(&uri, 0)!=0) {
714                         LM_ERR("checking maddr failed\n");
715                         return RR_ERROR;
716                 }
717                 if (rewrite_uri(_m, &uri) < 0) {
718                         LM_ERR("failed to rewrite R-URI\n");
719                         return RR_ERROR;
720                 }
721
722                 /* The first character if uri will be either '<' when it is the 
723                  * only URI in a Route header field or ',' if there is more than 
724                  * one URI in the header field */
725                 LM_DBG("The last route URI: '%.*s'\n", rt->nameaddr.uri.len,
726                                 ZSW(rt->nameaddr.uri.s));
727
728                 if (prev) {
729                         rem_off = prev->nameaddr.name.s + prev->len;
730                         rem_len = rt->nameaddr.name.s + rt->len - rem_off;
731                 } else {
732                         rem_off = hdr->name.s;
733                         rem_len = hdr->len;
734                 }
735                 if (!del_lump(_m, rem_off - _m->buf, rem_len, 0)) {
736                         LM_ERR("failed to remove Route HF\n");
737                         return RR_ERROR;
738                 }
739         }
740
741         /* run RR callbacks only if we have Route URI parameters */
742         if(routed_params.len > 0)
743                 run_rr_callbacks( _m, &routed_params );
744
745         return RR_DRIVEN;
746 }
747
748
749 static inline void rr_do_force_send_socket(sip_msg_t *_m, sip_uri_t *puri,
750                 rr_t* rt, int rr2on)
751 {
752         socket_info_t *si = NULL;
753         param_hooks_t phooks;
754         param_t* plist = NULL;
755         param_t *pit=NULL;
756         str s;
757
758
759         if(rr_sockname_mode!=0 && puri->params.len>0) {
760                 s = puri->params;
761                 if(s.s[s.len-1]==';') {
762                         s.len--;
763                 }
764                 if (parse_params(&s, CLASS_ANY, &phooks, &plist)<0) {
765                         LM_ERR("bad sip uri parameters: %.*s\n", s.len, s.s);
766                         return;
767                 }
768                 for (pit = plist; pit; pit=pit->next) {
769                         if (pit->name.len==SOCKNAME_ATTR_LEN
770                                         && strncasecmp(pit->name.s, SOCKNAME_ATTR,
771                                                         SOCKNAME_ATTR_LEN)==0) {
772                                 if(pit->body.len>0) {
773                                         si = ksr_get_socket_by_name(&pit->body);
774                                         if(si != NULL) {
775                                                 LM_DBG("found socket with name: %.*s\n",
776                                                                 pit->body.len, pit->body.s);
777                                                 set_force_socket(_m, si);
778                                                 free_params(plist);
779                                                 return;
780                                         } else {
781                                                 LM_DBG("failed to find socket with name: %.*s\n",
782                                                                 pit->body.len, pit->body.s);
783                                         }
784                                 }
785                         }
786                 }
787                 LM_DBG("use of sockname parameter enabled, but failed to find it\n");
788                 free_params(plist);
789         }
790
791         if ((si = grep_sock_info(&puri->host,
792                                 puri->port_no?puri->port_no:proto_default_port(puri->proto),
793                                 puri->proto)) != 0) {
794                 LM_DBG("set send socket %p for local route uri: %.*s\n", si,
795                                 rt->nameaddr.uri.len, ZSW(rt->nameaddr.uri.s));
796                 set_force_socket(_m, si);
797         } else if ((si = grep_sock_info(&puri->host, puri->port_no,
798                                         puri->proto)) != 0) {
799                 LM_DBG("set send socket %p for local route uri: %.*s\n", si,
800                                 rt->nameaddr.uri.len, ZSW(rt->nameaddr.uri.s));
801                 set_force_socket(_m, si);
802         } else {
803                 if (enable_socket_mismatch_warning && rr2on) {
804                         LM_WARN("no socket found to match second RR (%.*s)\n",
805                                         rt->nameaddr.uri.len, ZSW(rt->nameaddr.uri.s));
806                         if(!is_myself(puri)) {
807                                 LM_WARN("second RR uri is not myself (%.*s)\n",
808                                                 rt->nameaddr.uri.len, ZSW(rt->nameaddr.uri.s));
809                         }
810                 } else {
811                         LM_DBG("no socket found to match second RR (%.*s)\n",
812                                         rt->nameaddr.uri.len, ZSW(rt->nameaddr.uri.s));
813                 }
814         }
815 }
816
817 /*!
818  * \brief Previous hop was a loose router, handle this case
819  * \param _m SIP message
820  * \param preloaded do we have a preloaded route set
821  * \return -1 on failure, 1 on success
822  */
823 static inline int after_loose(struct sip_msg* _m, int preloaded)
824 {
825         struct hdr_field* hdr;
826         struct sip_uri puri;
827         rr_t* rt;
828         int res;
829         int status = RR_DRIVEN;
830         str uri;
831         int uri_is_myself;
832         int use_ob = 0;
833         str rparams;
834
835         hdr = _m->route;
836         rt = (rr_t*)hdr->parsed;
837         uri = rt->nameaddr.uri;
838
839         /* reset rr handling static vars for safety in error case */
840         routed_msg_id.msgid = 0;
841         routed_msg_id.pid = 0;
842
843         if (parse_uri(uri.s, uri.len, &puri) < 0) {
844                 LM_ERR("failed to parse the first route URI (%.*s)\n",
845                                 uri.len, ZSW(uri.s));
846                 return RR_ERROR;
847         }
848
849         routed_params = puri.params;
850         uri_is_myself = is_myself(&puri);
851
852         /* IF the URI was added by me, remove it */
853         if (uri_is_myself>0)
854         {
855                 LM_DBG("Topmost route URI: '%.*s' is me\n",
856                         uri.len, ZSW(uri.s));
857                 /* set the hooks for the params */
858                 routed_msg_id.msgid = _m->id;
859                 routed_msg_id.pid = _m->pid;
860
861                 if ((use_ob = process_outbound(_m, puri.user)) < 0) {
862                         LM_INFO("failed to process outbound flow-token\n");
863                         return RR_FLOW_TOKEN_BROKEN;
864                 }
865
866                 if (rr_force_send_socket && !use_ob) {
867                         if (!enable_double_rr || !is_2rr(&puri.params)) {
868                                 rr_do_force_send_socket(_m, &puri, rt, 0);
869                         }
870                 }
871                 if (!rt->next) {
872                         /* No next route in the same header, remove the whole header
873                          * field immediately
874                          */
875                         if (!del_lump(_m, hdr->name.s - _m->buf, hdr->len, 0)) {
876                                 LM_ERR("failed to remove Route HF\n");
877                                 return RR_ERROR;
878                         }
879
880                         res = find_next_route(_m, &hdr);
881                         if (res < 0) {
882                                 LM_ERR("failed to find next route\n");
883                                 return RR_ERROR;
884                         }
885                         if (res > 0) { /* No next route found */
886                                 LM_DBG("No next URI found\n");
887                                 status = (preloaded ? RR_PRELOADED : RR_DRIVEN);
888                                 goto done;
889                         }
890                         rt = (rr_t*)hdr->parsed;
891                 } else rt = rt->next;
892
893                 if (enable_double_rr && is_2rr(&puri.params)) {
894                         /* double route may occure due different IP and port, so force as
895                          * send interface the one advertise in second Route */
896                         if (parse_uri(rt->nameaddr.uri.s,rt->nameaddr.uri.len,&puri)<0) {
897                                 LM_ERR("failed to parse the double route URI (%.*s)\n",
898                                                 rt->nameaddr.uri.len, ZSW(rt->nameaddr.uri.s));
899                                 return RR_ERROR;
900                         }
901
902                         if (!use_ob) {
903                                 rr_do_force_send_socket(_m, &puri, rt, 1);
904                         }
905
906                         if (!rt->next) {
907                                 /* No next route in the same header, remove the whole header
908                                  * field immediately */
909                                 if (!del_lump(_m, hdr->name.s - _m->buf, hdr->len, 0)) {
910                                         LM_ERR("failed to remove Route HF\n");
911                                         return RR_ERROR;
912                                 }
913                                 res = find_next_route(_m, &hdr);
914                                 if (res < 0) {
915                                         LM_ERR("failed to find next route\n");
916                                         return RR_ERROR;
917                                         }
918                                 if (res > 0) { /* No next route found */
919                                         LM_DBG("no next URI found\n");
920                                         status = (preloaded ? RR_PRELOADED : RR_DRIVEN);
921                                         goto done;
922                                 }
923                                 rt = (rr_t*)hdr->parsed;
924                         } else rt = rt->next;
925                 }
926
927                 uri = rt->nameaddr.uri;
928                 if (parse_uri(uri.s, uri.len, &puri) < 0) {
929                         LM_ERR("failed to parse the next route URI (%.*s)\n",
930                                         uri.len, ZSW(uri.s));
931                         return RR_ERROR;
932                 }
933                 _m->msg_flags |= FL_ROUTE_ADDR;
934         } else {
935 #ifdef ENABLE_USER_CHECK
936                 /* check if it the ignored user */
937                 if(uri_is_myself < 0)
938                         return RR_NOT_DRIVEN;
939 #endif
940                 LM_DBG("Topmost URI is NOT myself\n");
941                 routed_params.s = NULL;
942                 routed_params.len = 0;
943         }
944
945         LM_DBG("URI to be processed: '%.*s'\n", uri.len, ZSW(uri.s));
946         if (is_strict(&puri.params)) {
947                 LM_DBG("Next URI is a strict router\n");
948                 if (handle_sr(_m, hdr, rt) < 0) {
949                         LM_ERR("failed to handle strict router\n");
950                         return RR_ERROR;
951                 }
952         } else {
953                 /* Next hop is loose router */
954                 LM_DBG("Next URI is a loose router\n");
955
956                 if (!use_ob) {
957                         if(get_maddr_uri(&uri, &puri)!=0) {
958                                 LM_ERR("checking maddr failed\n");
959                                 return RR_ERROR;
960                         }
961
962                         if (set_dst_uri(_m, &uri) < 0) {
963                                 LM_ERR("failed to set dst_uri\n");
964                                 return RR_ERROR;
965                         }
966                         /* dst_uri changed, so it makes sense to re-use the current uri for
967                         forking */
968                         ruri_mark_new(); /* re-use uri for serial forking */
969                 }
970
971                 /* There is a previous route uri which was 2nd uri of mine
972                  * and must be removed here */
973                 if (rt != hdr->parsed) {
974                         if (!del_lump(_m, hdr->body.s - _m->buf,
975                         rt->nameaddr.name.s - hdr->body.s, 0)) {
976                                 LM_ERR("failed to remove Route HF\n");
977                                 return RR_ERROR;
978                         }
979                 }
980         }
981
982 done:
983         if (use_ob == 1)
984                 status = RR_OB_DRIVEN;
985
986         /* run RR callbacks only if we have Route URI parameters */
987         if(routed_params.len > 0) {
988                 rparams = routed_params;
989                 run_rr_callbacks( _m, &rparams );
990         }
991         return status;
992 }
993
994
995 /*!
996  * \brief Do loose routing as per RFC3261
997  * \param _m SIP message
998  * \return negative on failure or preloaded, 1 on success
999  */
1000 int loose_route(struct sip_msg* _m)
1001 {
1002         int ret;
1003
1004         if (find_first_route(_m) != 0) {
1005                 LM_DBG("There is no Route HF\n");
1006                 return -1;
1007         }
1008
1009         if (parse_sip_msg_uri(_m)<0) {
1010                 LM_ERR("failed to parse Request URI\n");
1011                 return -1;
1012         }
1013
1014         ret = is_preloaded(_m);
1015         if (ret < 0) {
1016                 return -1;
1017         } else if (ret == 1) {
1018                 return after_loose(_m, 1);
1019         } else {
1020                 if (is_myself(&_m->parsed_uri)) {
1021                         return after_strict(_m);
1022                 } else {
1023                         return after_loose(_m, 0);
1024                 }
1025         }
1026 }
1027
1028 /**
1029  *
1030  */
1031 int redo_route_params(sip_msg_t *msg)
1032 {
1033         hdr_field_t *hdr;
1034         sip_uri_t puri;
1035         rr_t* rt;
1036         str uri;
1037         int uri_is_myself;
1038
1039         int redo = 0;
1040
1041         if(msg->first_line.type != SIP_REQUEST) {
1042                 return -1;
1043         }
1044
1045         if(msg->route==NULL) {
1046                 return -1;
1047         }
1048
1049         if(msg->route->parsed==NULL) {
1050                 if (parse_rr(msg->route) < 0) {
1051                         LM_ERR("failed to parse Route HF\n");
1052                         return -1;
1053                 }
1054         }
1055
1056         if(msg->route->parsed==NULL) {
1057                 LM_ERR("NULL parsed Route header\n");
1058                 return -1;
1059         }
1060
1061         /* check if the hooked params belong to the same message */
1062         if (routed_msg_id.msgid != msg->id || routed_msg_id.pid != msg->pid) {
1063                 redo = 1;
1064         }
1065         if((redo==0) && (routed_params.s==NULL || routed_params.len<=0)) {
1066                 redo = 1;
1067         }
1068         if((redo==0) && (routed_params.s<msg->buf
1069                                 || routed_params.s>msg->buf+msg->len)) {
1070                 redo = 1;
1071         }
1072         if(redo==1) {
1073                 hdr = msg->route;
1074                 rt = (rr_t*)hdr->parsed;
1075                 uri = rt->nameaddr.uri;
1076
1077                 /* reset rr handling static vars for safety in error case */
1078                 routed_msg_id.msgid = 0;
1079                 routed_msg_id.pid = 0;
1080
1081                 if (parse_uri(uri.s, uri.len, &puri) < 0) {
1082                         LM_ERR("failed to parse the first route URI (%.*s)\n",
1083                                         uri.len, ZSW(uri.s));
1084                         return -1;
1085                 }
1086
1087                 uri_is_myself = is_myself(&puri);
1088
1089                 /* if the URI was added by me, remove it */
1090                 if (uri_is_myself>0) {
1091                         LM_DBG("Topmost route URI: '%.*s' is me\n",
1092                                 uri.len, ZSW(uri.s));
1093                         /* set the hooks for the params */
1094                         routed_msg_id.msgid = msg->id;
1095                         routed_msg_id.pid = msg->pid;
1096                         routed_params = puri.params;
1097                         return 0;
1098                 } else {
1099                         return -1;
1100                 }
1101         }
1102         return 0;
1103 }
1104
1105 /*!
1106  * \brief Check if the route hdr has the required parameter
1107  *
1108  * The function checks for the request "msg" if the URI parameters
1109  * of the local Route header (corresponding to the local server)
1110  * matches the given regular expression "re". It must be call
1111  * after the loose_route was done.
1112  *
1113  * \param msg SIP message request that will has the Route header parameters checked
1114  * \param re compiled regular expression to be checked against the Route header parameters
1115  * \return -1 on failure, 1 on success
1116  */
1117 int check_route_param(sip_msg_t * msg, regex_t* re)
1118 {
1119         regmatch_t pmatch;
1120         char bk;
1121         str params;
1122         str rruri;
1123
1124         /* check if the hooked params belong to the same message */
1125         if(redo_route_params(msg)<0) {
1126                 return -1;
1127         }
1128
1129         /* check if params are present */
1130         if ( !routed_params.s || routed_params.len<=0 ) {
1131                 return -1;
1132         }
1133         rruri = ((rr_t*)(msg->route->parsed))->nameaddr.uri;
1134
1135         /* include also the first ';' */
1136         for( params=routed_params ;
1137                         params.s>rruri.s && params.s[0]!=';' ;
1138                         params.s--,params.len++ );
1139
1140         LM_DBG("route params checking against [%.*s] (orig: [%.*s])\n",
1141                         params.len, params.s, routed_params.len, routed_params.s);
1142
1143         /* do the well-known trick to convert to null terminted */
1144         bk = params.s[params.len];
1145         params.s[params.len] = 0;
1146         LM_DBG("params are <%s>\n", params.s);
1147         if (regexec( re, params.s, 1, &pmatch, 0)!=0) {
1148                 params.s[params.len] = bk;
1149                 return -1;
1150         } else {
1151                 params.s[params.len] = bk;
1152                 return 0;
1153         }
1154 }
1155
1156
1157 /*!
1158  * \brief Gets the value of a route parameter
1159  *
1160  * The function search in to the "msg"'s Route header parameters
1161  * the parameter called "name" and returns its value into "val".
1162  * It must be call only after the loose_route is done.
1163  *
1164  * \param msg - request that will have the Route header parameter searched
1165  * \param name - contains the Route header parameter to be serached
1166  * \param val returns the value of the searched Route header parameter if found.
1167  * It might be an empty string if the parameter had no value.
1168  * \return 0 if parameter was found (even if it has no value), -1 otherwise
1169  */
1170 int get_route_param(sip_msg_t *msg, str *name, str *val)
1171 {
1172         char *p;
1173         char *end;
1174         char c;
1175         int quoted;
1176
1177         /* check if the hooked params belong to the same message */
1178         if(redo_route_params(msg)<0) {
1179                 goto notfound;
1180         }
1181
1182         /* check if params are present */
1183         if ( !routed_params.s || routed_params.len<=0 )
1184                 goto notfound;
1185
1186         end = routed_params.s + routed_params.len;
1187         p = routed_params.s;
1188
1189
1190         /* parse the parameters string and find the "name" param */
1191         while ( end-p>name->len+2 ) {
1192                 if (p!=routed_params.s) {
1193                         /* go to first ';' char */
1194                         for( quoted=0 ; p<end && !(*p==';' && !quoted) ; p++ )
1195                                 if ( (*p=='"' || *p=='\'') && *(p-1)!='\\' )
1196                                         quoted ^= 0x1;
1197                         if (p==end)
1198                                 goto notfound;
1199                         p++;
1200                 }
1201                 /* get first non space char */
1202                 while( p<end && (*p==' ' || *p=='\t') )
1203                         p++;
1204                 /* check the name - length first and content after */
1205                 if ( end-p<name->len+2 )
1206                         goto notfound;
1207                 if ( memcmp(p,name->s,name->len)!=0 ) {
1208                         p++;
1209                         continue;
1210                 }
1211                 p+=name->len;
1212                 while( p<end && (*p==' ' || *p=='\t') )
1213                         p++;
1214                 if (p==end|| *p==';') {
1215                         /* empty val */
1216                         val->len = 0;
1217                         val->s = 0;
1218                         goto found;
1219                 }
1220                 if (*(p++)!='=')
1221                         continue;
1222                 while( p<end && (*p==' ' || *p=='\t') )
1223                         p++;
1224                 if (p==end)
1225                         goto notfound;
1226                 /* get value */
1227                 if ( *p=='\'' || *p=='"') {
1228                         for( val->s = ++p ; p<end ; p++) {
1229                                 if ((*p=='"' || *p=='\'') && *(p-1)!='\\' )
1230                                         break;
1231                         }
1232                 } else {
1233                         for( val->s=p ; p<end ; p++) {
1234                                 if ( (c=*p)==';' || c==' ' || c=='\t' )
1235                                         break;
1236                         }
1237                 }
1238                 val->len = p-val->s;
1239                 if (val->len==0)
1240                         val->s = 0;
1241                 goto found;
1242         }
1243
1244 notfound:
1245         return -1;
1246 found:
1247         return 0;
1248 }
1249
1250
1251 /*!
1252  * \brief Check the direction of the message
1253  *
1254  * The function checks the flow direction of the request "msg". As
1255  * for checking it's used the "ftag" Route header parameter, the
1256  * append_fromtag module parameter must be enables.
1257  * Also this must be call only after the loose_route is done.
1258
1259  * \param msg SIP message request that will have the direction checked
1260  * \param dir direction to be checked against. It may be RR_FLOW_UPSTREAM or RR_FLOW_DOWNSTREAM
1261  * \return 0 if the request flow direction is the same as the given direction, -1 otherwise
1262  */
1263 int is_direction(struct sip_msg * msg, int dir)
1264 {
1265         static str ftag_param = {"ftag",4};
1266         static msg_ctx_id_t last_id = {0};
1267         static unsigned int last_dir = 0;
1268         str ftag_val;
1269         str tag;
1270
1271         if ( last_id.msgid==msg->id && last_id.pid==msg->pid && last_dir!=0) {
1272                 if (last_dir==RR_FLOW_UPSTREAM)
1273                         goto upstream;
1274                 else
1275                         goto downstream;
1276         }
1277
1278         ftag_val.s = 0;
1279         ftag_val.len = 0;
1280
1281         if (get_route_param( msg, &ftag_param, &ftag_val)!=0) {
1282                 LM_DBG("param ftag not found\n");
1283                 goto downstream;
1284         }
1285
1286         if ( ftag_val.s==0 || ftag_val.len==0 ) {
1287                 LM_DBG("param ftag has empty val\n");
1288                 goto downstream;
1289         }
1290
1291         /* get the tag value from FROM hdr */
1292         if ( parse_from_header(msg)!=0 )
1293                 goto downstream;
1294
1295         tag = ((struct to_body*)msg->from->parsed)->tag_value;
1296         if (tag.s==0 || tag.len==0)
1297                 goto downstream;
1298
1299         /* compare the 2 strings */
1300         if (tag.len!=ftag_val.len || memcmp(tag.s,ftag_val.s,ftag_val.len))
1301                 goto upstream;
1302
1303 downstream:
1304         last_id.msgid = msg->id;
1305         last_id.pid = msg->pid;
1306         last_dir = RR_FLOW_DOWNSTREAM;
1307         return (dir==RR_FLOW_DOWNSTREAM)?0:-1;
1308 upstream:
1309         last_id.msgid = msg->id;
1310         last_id.pid = msg->pid;
1311         last_dir = RR_FLOW_UPSTREAM;
1312         return (dir==RR_FLOW_UPSTREAM)?0:-1;
1313 }