core: added fields to message structure and branch structure to hold user-agent strin...
[sip-router] / dset.c
1 /*
2  * $Id$
3  *
4  * destination set
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
30 /** destination set / branches support.
31  * @file dset.c
32  * @ingroup core
33  * Module: @ref core
34  */
35
36 #include <string.h>
37 #include "dprint.h"
38 #include "config.h"
39 #include "parser/parser_f.h"
40 #include "parser/msg_parser.h"
41 #include "ut.h"
42 #include "hash_func.h"
43 #include "error.h"
44 #include "dset.h"
45 #include "mem/mem.h"
46 #include "ip_addr.h"
47
48 #define CONTACT "Contact: "
49 #define CONTACT_LEN (sizeof(CONTACT) - 1)
50
51 #define CONTACT_DELIM ", "
52 #define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
53
54 #define Q_PARAM ">;q="
55 #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
56
57
58 /* 
59  * Where we store URIs of additional transaction branches
60  * (-1 because of the default branch, #0)
61  */
62 static struct branch branches[MAX_BRANCHES - 1];
63
64 /* how many of them we have */
65 unsigned int nr_branches = 0;
66
67 /* branch iterator */
68 static int branch_iterator = 0;
69
70 /* used to mark ruris "consumed" when branching (1 new, 0 consumed) */
71 int ruri_is_new = 0;
72
73 /* The q parameter of the Request-URI */
74 static qvalue_t ruri_q = Q_UNSPECIFIED;
75
76 /* Branch flags of the Request-URI */
77 static flag_t ruri_bflags;
78
79
80 /*! \brief
81  * Return pointer to branch[idx] structure
82  * @param idx - branch index
83  *
84  * @return  pointer to branch or NULL if invalid branch
85  */
86 branch_t *get_sip_branch(int idx)
87 {
88         if(nr_branches==0)
89                 return NULL;
90         if(idx<0)
91         {
92                 if(nr_branches + idx >= 0)
93                         return &branches[nr_branches+idx];
94                 return NULL;
95         }
96         if(idx < nr_branches)
97                 return &branches[idx];
98         return 0;
99 }
100
101 /*! \brief
102  * Drop branch[idx]
103  * @param idx - branch index
104  *
105  * @return  0 on success, -1 on error
106  */
107 int drop_sip_branch(int idx)
108 {
109         if(nr_branches==0 || idx>=nr_branches)
110                 return 0;
111         if(idx<0 && nr_branches+idx<0)
112                 return 0;
113         /* last branch */
114         if(idx==nr_branches-1)
115         {
116                 nr_branches--;
117                 return 0;
118         }
119         if(idx<0)
120                 idx = nr_branches+idx;
121         /* shift back one position */
122         for(; idx<nr_branches-1; idx++)
123                 memcpy(&branches[idx], &branches[idx+1], sizeof(branch_t));
124         nr_branches--;
125         return 0;
126 }
127
128 static inline flag_t* get_bflags_ptr(unsigned int branch)
129 {
130         if (branch == 0) return &ruri_bflags;
131         if (branch - 1 < nr_branches) return &branches[branch - 1].flags;
132         return NULL;
133 }
134
135
136 int setbflag(unsigned int branch, flag_t flag)
137 {
138         flag_t* flags;
139
140         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
141         (*flags) |= 1 << flag;
142         return 1;
143 }
144
145
146 int isbflagset(unsigned int branch, flag_t flag)
147 {
148         flag_t* flags;
149
150         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
151         return ((*flags) & (1 << flag)) ? 1 : -1;
152 }
153
154
155 int resetbflag(unsigned int branch, flag_t flag)
156 {
157         flag_t* flags;
158
159         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
160         (*flags) &= ~ (1 << flag);
161         return 1;
162 }
163
164
165 int getbflagsval(unsigned int branch, flag_t* res)
166 {
167         flag_t* flags;
168         if (res == NULL) return -1;
169         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
170         *res = *flags;
171         return 1;
172 }
173
174
175 int setbflagsval(unsigned int branch, flag_t val)
176 {
177         flag_t* flags;
178         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
179         *flags = val;
180         return 1;
181 }
182
183
184 /*
185  * Initialize the branch iterator, the next
186  * call to next_branch will return the first
187  * contact from the dset array
188  */
189 void init_branch_iterator(void)
190 {
191         branch_iterator = 0;
192 }
193
194 /**
195  * return the value of current branch iterator
196  */
197 int get_branch_iterator(void)
198 {
199         return branch_iterator;
200 }
201
202 /**
203  * set the value of current branch interator
204  */
205 void set_branch_iterator(int n)
206 {
207         branch_iterator = n;
208 }
209
210
211 /** \brief Get a branch from the destination set
212  * \return Return the 'i' branch from the dset
213  * array, 0 is returned if there are no
214  * more branches
215  */
216 char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
217                  str* path, unsigned int *flags,
218                  struct socket_info** force_socket,
219                  str *ruid, str *instance, str *location_ua)
220 {
221         if (i < nr_branches) {
222                 *len = branches[i].len;
223                 *q = branches[i].q;
224                 if (dst_uri) {
225                         dst_uri->len = branches[i].dst_uri_len;
226                         dst_uri->s = (dst_uri->len)?branches[i].dst_uri:0;
227                 }
228                 if (path) {
229                         path->len = branches[i].path_len;
230                         path->s = (path->len)?branches[i].path:0;
231                 }
232                 if (force_socket)
233                         *force_socket = branches[i].force_send_socket;
234                 if (flags)
235                         *flags = branches[i].flags;
236                 if (ruid) {
237                         ruid->len = branches[i].ruid_len;
238                         ruid->s = (ruid->len)?branches[i].ruid:0;
239                 }
240                 if (instance) {
241                         instance->len = branches[i].instance_len;
242                         instance->s = (instance->len)?branches[i].instance:0;
243                 }
244                 if (location_ua) {
245                         location_ua->len = branches[i].location_ua_len;
246                         location_ua->s
247                                 = (location_ua->len)?branches[i].location_ua:0;
248                 }
249                 return branches[i].uri;
250         } else {
251                 *len = 0;
252                 *q = Q_UNSPECIFIED;
253                 if (dst_uri) {
254                         dst_uri->s = 0;
255                         dst_uri->len = 0;
256                 }
257                 if (path) {
258                         path->s = 0;
259                         path->len = 0;
260                 }
261                 if (force_socket)
262                         *force_socket = 0;
263                 if (flags)
264                         *flags = 0;
265                 if (ruid) {
266                         ruid->s = 0;
267                         ruid->len = 0;
268                 }
269                 if (instance) {
270                         instance->s = 0;
271                         instance->len = 0;
272                 }
273                 if (location_ua) {
274                         location_ua->s = 0;
275                         location_ua->len = 0;
276                 }
277                 return 0;
278         }
279 }
280
281
282
283 /** Return the next branch from the dset array.
284  * 0 is returned if there are no more branches
285  */
286 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
287                   unsigned int* flags, struct socket_info** force_socket,
288                   str* ruid, str *instance, str *location_ua)
289 {
290         char* ret;
291         
292         ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
293                        force_socket, ruid, instance, location_ua);
294         if (likely(ret))
295                 branch_iterator++;
296         return ret;
297 }
298
299
300 /*
301  * Empty the dset array
302  */
303 void clear_branches(void)
304 {
305         nr_branches = 0;
306         ruri_q = Q_UNSPECIFIED;
307         ruri_bflags = 0;
308         ruri_mark_consumed();
309 }
310
311
312
313 /**  Add a new branch to the current transaction.
314  * @param msg - sip message, used for getting the uri if not specified (0).
315  * @param uri - uri, can be 0 (in which case the uri is taken from msg)
316  * @param dst_uri - destination uri, can be 0.
317  * @param path - path vector (passed in a string), can be 0.
318  * @param q  - q value.
319  * @param flags - per branch flags.
320  * @param force_socket - socket that should be used when sending.
321  *
322  * @return  <0 (-1) on failure, 1 on success (script convention).
323  */
324 int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
325                   qvalue_t q, unsigned int flags,
326                   struct socket_info* force_socket,
327                   str* instance, unsigned int reg_id,
328                   str* ruid, str* location_ua)
329 {
330         str luri;
331
332         /* if we have already set up the maximum number
333          * of branches, don't try new ones 
334          */
335         if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
336                 LOG(L_ERR, "max nr of branches exceeded\n");
337                 ser_error = E_TOO_MANY_BRANCHES;
338                 return -1;
339         }
340
341         /* if not parameterized, take current uri */
342         if (uri==0 || uri->len==0 || uri->s==0) {
343                 if (msg->new_uri.s)
344                         luri = msg->new_uri;
345                 else
346                         luri = msg->first_line.u.request.uri;
347         } else {
348                 luri = *uri;
349         }
350
351         if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
352                 LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
353                 return -1;
354         }
355
356         /* copy the dst_uri */
357         if (dst_uri && dst_uri->len && dst_uri->s) {
358                 if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
359                         LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
360                         return -1;
361                 }
362                 memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
363                 branches[nr_branches].dst_uri[dst_uri->len] = 0;
364                 branches[nr_branches].dst_uri_len = dst_uri->len;
365         } else {
366                 branches[nr_branches].dst_uri[0] = '\0';
367                 branches[nr_branches].dst_uri_len = 0;
368         }
369
370         /* copy the path string */
371         if (unlikely(path && path->len && path->s)) {
372                 if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
373                         LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
374                         return -1;
375                 }
376                 memcpy(branches[nr_branches].path, path->s, path->len);
377                 branches[nr_branches].path[path->len] = 0;
378                 branches[nr_branches].path_len = path->len;
379         } else {
380                 branches[nr_branches].path[0] = '\0';
381                 branches[nr_branches].path_len = 0;
382         }
383
384         /* copy the ruri */
385         memcpy(branches[nr_branches].uri, luri.s, luri.len);
386         branches[nr_branches].uri[luri.len] = 0;
387         branches[nr_branches].len = luri.len;
388         branches[nr_branches].q = q;
389
390         branches[nr_branches].force_send_socket = force_socket;
391         branches[nr_branches].flags = flags;
392
393         /* copy instance string */
394         if (unlikely(instance && instance->len && instance->s)) {
395                 if (unlikely(instance->len > MAX_INSTANCE_SIZE - 1)) {
396                         LOG(L_ERR, "too long instance: %.*s\n",
397                             instance->len, instance->s);
398                         return -1;
399                 }
400                 memcpy(branches[nr_branches].instance, instance->s,
401                        instance->len);
402                 branches[nr_branches].instance[instance->len] = 0;
403                 branches[nr_branches].instance_len = instance->len;
404         } else {
405                 branches[nr_branches].instance[0] = '\0';
406                 branches[nr_branches].instance_len = 0;
407         }
408
409         /* copy reg_id */
410         branches[nr_branches].reg_id = reg_id;
411
412         /* copy ruid string */
413         if (unlikely(ruid && ruid->len && ruid->s)) {
414                 if (unlikely(ruid->len > MAX_RUID_SIZE - 1)) {
415                         LOG(L_ERR, "too long ruid: %.*s\n",
416                             ruid->len, ruid->s);
417                         return -1;
418                 }
419                 memcpy(branches[nr_branches].ruid, ruid->s,
420                        ruid->len);
421                 branches[nr_branches].ruid[ruid->len] = 0;
422                 branches[nr_branches].ruid_len = ruid->len;
423         } else {
424                 branches[nr_branches].ruid[0] = '\0';
425                 branches[nr_branches].ruid_len = 0;
426         }
427
428         if (unlikely(location_ua && location_ua->len && location_ua->s)) {
429                 if (unlikely(location_ua->len > MAX_UA_SIZE)) {
430                         LOG(L_ERR, "too long location_ua: %.*s\n",
431                             location_ua->len, location_ua->s);
432                         return -1;
433                 }
434                 memcpy(branches[nr_branches].location_ua, location_ua->s,
435                        location_ua->len);
436                 branches[nr_branches].location_ua[location_ua->len] = 0;
437                 branches[nr_branches].location_ua_len = location_ua->len;
438         } else {
439                 branches[nr_branches].location_ua[0] = '\0';
440                 branches[nr_branches].location_ua_len = 0;
441         }
442         
443         nr_branches++;
444         return 1;
445 }
446
447
448 /*
449  * Create a Contact header field from the dset
450  * array
451  */
452 char* print_dset(struct sip_msg* msg, int* len) 
453 {
454         int cnt, i;
455         unsigned int qlen;
456         qvalue_t q;
457         str uri;
458         char* p, *qbuf;
459         int crt_branch;
460         static char dset[MAX_REDIRECTION_LEN];
461
462         if (msg->new_uri.s) {
463                 cnt = 1;
464                 *len = msg->new_uri.len;
465                 if (ruri_q != Q_UNSPECIFIED) {
466                         *len += 1 + Q_PARAM_LEN + len_q(ruri_q);
467                 }
468         } else {
469                 cnt = 0;
470                 *len = 0;
471         }
472
473         /* backup current branch index to restore it later */
474         crt_branch = get_branch_iterator();
475
476         init_branch_iterator();
477         while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
478                 cnt++;
479                 *len += uri.len;
480                 if (q != Q_UNSPECIFIED) {
481                         *len += 1 + Q_PARAM_LEN + len_q(q);
482                 }
483         }
484
485         if (cnt == 0) return 0; 
486
487         *len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
488
489         if (*len + 1 > MAX_REDIRECTION_LEN) {
490                 LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
491                 goto error;
492         }
493
494         memcpy(dset, CONTACT, CONTACT_LEN);
495         p = dset + CONTACT_LEN;
496         if (msg->new_uri.s) {
497                 if (ruri_q != Q_UNSPECIFIED) {
498                         *p++ = '<';
499                 }
500
501                 memcpy(p, msg->new_uri.s, msg->new_uri.len);
502                 p += msg->new_uri.len;
503
504                 if (ruri_q != Q_UNSPECIFIED) {
505                         memcpy(p, Q_PARAM, Q_PARAM_LEN);
506                         p += Q_PARAM_LEN;
507
508                         qbuf = q2str(ruri_q, &qlen);
509                         memcpy(p, qbuf, qlen);
510                         p += qlen;
511                 }
512                 i = 1;
513         } else {
514                 i = 0;
515         }
516
517         init_branch_iterator();
518         while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0, 0, 0, 0))) {
519                 if (i) {
520                         memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
521                         p += CONTACT_DELIM_LEN;
522                 }
523
524                 if (q != Q_UNSPECIFIED) {
525                         *p++ = '<';
526                 }
527
528                 memcpy(p, uri.s, uri.len);
529                 p += uri.len;
530                 if (q != Q_UNSPECIFIED) {
531                         memcpy(p, Q_PARAM, Q_PARAM_LEN);
532                         p += Q_PARAM_LEN;
533
534                         qbuf = q2str(q, &qlen);
535                         memcpy(p, qbuf, qlen);
536                         p += qlen;
537                 }
538                 i++;
539         }
540
541         memcpy(p, CRLF " ", CRLF_LEN + 1);
542         set_branch_iterator(crt_branch);
543         return dset;
544
545 error:
546         set_branch_iterator(crt_branch);
547         return 0;
548 }
549
550
551 /*
552  * Sets the q parameter of the Request-URI
553  */
554 void set_ruri_q(qvalue_t q)
555 {
556         ruri_q = q;
557 }
558
559
560 /*
561  * Return the q value of the Request-URI
562  */
563 qvalue_t get_ruri_q(void)
564 {
565         return ruri_q;
566 }
567
568
569
570 /*
571  * Rewrite Request-URI
572  */
573 int rewrite_uri(struct sip_msg* _m, str* _s)
574 {
575         char *buf = NULL;
576
577         if(_m->new_uri.s==NULL || _m->new_uri.len<_s->len) {
578                 buf = (char*)pkg_malloc(_s->len + 1);
579                 if (!buf) {
580                         LM_ERR("No memory left to rewrite r-uri\n");
581                         return -1;
582                 }
583         }
584         if(buf!=NULL) {
585                 if(_m->new_uri.s)
586                         pkg_free(_m->new_uri.s);
587         } else {
588                 buf = _m->new_uri.s;
589         }
590
591         memcpy(buf, _s->s, _s->len);
592         buf[_s->len] = '\0';
593
594         _m->parsed_uri_ok = 0;
595
596         _m->new_uri.s = buf;
597         _m->new_uri.len = _s->len;
598         /* mark ruri as new and available for forking */
599         ruri_mark_new();
600
601         return 1;
602 }
603