• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

dset.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * destination set
00005  *
00006  * Copyright (C) 2001-2004 FhG FOKUS
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License 
00026  * along with this program; if not, write to the Free Software 
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  */
00029 
00036 #include <string.h>
00037 #include "dprint.h"
00038 #include "config.h"
00039 #include "parser/parser_f.h"
00040 #include "parser/msg_parser.h"
00041 #include "ut.h"
00042 #include "hash_func.h"
00043 #include "error.h"
00044 #include "dset.h"
00045 #include "mem/mem.h"
00046 #include "ip_addr.h"
00047 
00048 #define CONTACT "Contact: "
00049 #define CONTACT_LEN (sizeof(CONTACT) - 1)
00050 
00051 #define CONTACT_DELIM ", "
00052 #define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
00053 
00054 #define Q_PARAM ">;q="
00055 #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
00056 
00057 
00058 /* 
00059  * Where we store URIs of additional transaction branches
00060  * (-1 because of the default branch, #0)
00061  */
00062 static struct branch branches[MAX_BRANCHES - 1];
00063 
00064 /* how many of them we have */
00065 unsigned int nr_branches = 0;
00066 
00067 /* branch iterator */
00068 static int branch_iterator = 0;
00069 
00070 /* used to mark ruris "consumed" when branching (1 new, 0 consumed) */
00071 int ruri_is_new = 0;
00072 
00073 /* The q parameter of the Request-URI */
00074 static qvalue_t ruri_q = Q_UNSPECIFIED;
00075 
00076 /* Branch flags of the Request-URI */
00077 static flag_t ruri_bflags;
00078 
00079 
00086 branch_t *get_sip_branch(int idx)
00087 {
00088         if(nr_branches==0)
00089                 return NULL;
00090         if(idx<0)
00091         {
00092                 if(nr_branches + idx >= 0)
00093                         return &branches[nr_branches+idx];
00094                 return NULL;
00095         }
00096         if(idx < nr_branches)
00097                 return &branches[idx];
00098         return 0;
00099 }
00100 
00107 int drop_sip_branch(int idx)
00108 {
00109         if(nr_branches==0 || idx>=nr_branches)
00110                 return 0;
00111         if(idx<0 && nr_branches+idx<0)
00112                 return 0;
00113         /* last branch */
00114         if(idx==nr_branches-1)
00115         {
00116                 nr_branches--;
00117                 return 0;
00118         }
00119         if(idx<0)
00120                 idx = nr_branches+idx;
00121         /* shift back one position */
00122         for(; idx<nr_branches-1; idx++)
00123                 memcpy(&branches[idx], &branches[idx+1], sizeof(branch_t));
00124         nr_branches--;
00125         return 0;
00126 }
00127 
00128 static inline flag_t* get_bflags_ptr(unsigned int branch)
00129 {
00130         if (branch == 0) return &ruri_bflags;
00131         if (branch - 1 < nr_branches) return &branches[branch - 1].flags;
00132         return NULL;
00133 }
00134 
00135 
00136 int setbflag(unsigned int branch, flag_t flag)
00137 {
00138         flag_t* flags;
00139 
00140         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
00141         (*flags) |= 1 << flag;
00142         return 1;
00143 }
00144 
00145 
00146 int isbflagset(unsigned int branch, flag_t flag)
00147 {
00148         flag_t* flags;
00149 
00150         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
00151         return ((*flags) & (1 << flag)) ? 1 : -1;
00152 }
00153 
00154 
00155 int resetbflag(unsigned int branch, flag_t flag)
00156 {
00157         flag_t* flags;
00158 
00159         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
00160         (*flags) &= ~ (1 << flag);
00161         return 1;
00162 }
00163 
00164 
00165 int getbflagsval(unsigned int branch, flag_t* res)
00166 {
00167         flag_t* flags;
00168         if (res == NULL) return -1;
00169         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
00170         *res = *flags;
00171         return 1;
00172 }
00173 
00174 
00175 int setbflagsval(unsigned int branch, flag_t val)
00176 {
00177         flag_t* flags;
00178         if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
00179         *flags = val;
00180         return 1;
00181 }
00182 
00183 
00184 /*
00185  * Initialize the branch iterator, the next
00186  * call to next_branch will return the first
00187  * contact from the dset array
00188  */
00189 void init_branch_iterator(void)
00190 {
00191         branch_iterator = 0;
00192 }
00193 
00197 int get_branch_iterator(void)
00198 {
00199         return branch_iterator;
00200 }
00201 
00205 void set_branch_iterator(int n)
00206 {
00207         branch_iterator = n;
00208 }
00209 
00210 
00216 char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
00217                                  str* path, unsigned int *flags,
00218                                  struct socket_info** force_socket)
00219 {
00220         if (i < nr_branches) {
00221                 *len = branches[i].len;
00222                 *q = branches[i].q;
00223                 if (dst_uri) {
00224                         dst_uri->len = branches[i].dst_uri_len;
00225                         dst_uri->s = (dst_uri->len)?branches[i].dst_uri:0;
00226                 }
00227                 if (path) {
00228                         path->len = branches[i].path_len;
00229                         path->s = (path->len)?branches[i].path:0;
00230                 }
00231                 if (force_socket)
00232                         *force_socket = branches[i].force_send_socket;
00233                 if (flags)
00234                         *flags = branches[i].flags;
00235                 return branches[i].uri;
00236         } else {
00237                 *len = 0;
00238                 *q = Q_UNSPECIFIED;
00239                 if (dst_uri) {
00240                         dst_uri->s = 0;
00241                         dst_uri->len = 0;
00242                 }
00243                 if (path) {
00244                         path->s = 0;
00245                         path->len = 0;
00246                 }
00247                 if (force_socket)
00248                         *force_socket = 0;
00249                 if (flags)
00250                         *flags = 0;
00251                 return 0;
00252         }
00253 }
00254 
00255 
00256 
00260 char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
00261                                         unsigned int* flags, struct socket_info** force_socket)
00262 {
00263         char* ret;
00264         
00265         ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
00266                                         force_socket);
00267         if (likely(ret))
00268                 branch_iterator++;
00269         return ret;
00270 }
00271 
00272 
00273 /*
00274  * Empty the dset array
00275  */
00276 void clear_branches(void)
00277 {
00278         nr_branches = 0;
00279         ruri_q = Q_UNSPECIFIED;
00280         ruri_bflags = 0;
00281         ruri_mark_consumed();
00282 }
00283 
00284 
00285 
00297 int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
00298                 qvalue_t q, unsigned int flags, struct socket_info* force_socket)
00299 {
00300         str luri;
00301 
00302         /* if we have already set up the maximum number
00303          * of branches, don't try new ones 
00304          */
00305         if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
00306                 LOG(L_ERR, "max nr of branches exceeded\n");
00307                 ser_error = E_TOO_MANY_BRANCHES;
00308                 return -1;
00309         }
00310 
00311         /* if not parameterized, take current uri */
00312         if (uri==0 || uri->len==0 || uri->s==0) {
00313                 if (msg->new_uri.s)
00314                         luri = msg->new_uri;
00315                 else
00316                         luri = msg->first_line.u.request.uri;
00317         } else {
00318                 luri = *uri;
00319         }
00320 
00321         if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
00322                 LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
00323                 return -1;
00324         }
00325 
00326         /* copy the dst_uri */
00327         if (dst_uri && dst_uri->len && dst_uri->s) {
00328                 if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
00329                         LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
00330                         return -1;
00331                 }
00332                 memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
00333                 branches[nr_branches].dst_uri[dst_uri->len] = 0;
00334                 branches[nr_branches].dst_uri_len = dst_uri->len;
00335         } else {
00336                 branches[nr_branches].dst_uri[0] = '\0';
00337                 branches[nr_branches].dst_uri_len = 0;
00338         }
00339 
00340         /* copy the path string */
00341         if (unlikely(path && path->len && path->s)) {
00342                 if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
00343                         LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
00344                         return -1;
00345                 }
00346                 memcpy(branches[nr_branches].path, path->s, path->len);
00347                 branches[nr_branches].path[path->len] = 0;
00348                 branches[nr_branches].path_len = path->len;
00349         } else {
00350                 branches[nr_branches].path[0] = '\0';
00351                 branches[nr_branches].path_len = 0;
00352         }
00353 
00354         /* copy the ruri */
00355         memcpy(branches[nr_branches].uri, luri.s, luri.len);
00356         branches[nr_branches].uri[luri.len] = 0;
00357         branches[nr_branches].len = luri.len;
00358         branches[nr_branches].q = q;
00359 
00360         branches[nr_branches].force_send_socket = force_socket;
00361         branches[nr_branches].flags = flags;
00362 
00363         nr_branches++;
00364         return 1;
00365 }
00366 
00367 
00368 /*
00369  * Create a Contact header field from the dset
00370  * array
00371  */
00372 char* print_dset(struct sip_msg* msg, int* len) 
00373 {
00374         int cnt, i;
00375         unsigned int qlen;
00376         qvalue_t q;
00377         str uri;
00378         char* p, *qbuf;
00379         int crt_branch;
00380         static char dset[MAX_REDIRECTION_LEN];
00381 
00382         if (msg->new_uri.s) {
00383                 cnt = 1;
00384                 *len = msg->new_uri.len;
00385                 if (ruri_q != Q_UNSPECIFIED) {
00386                         *len += 1 + Q_PARAM_LEN + len_q(ruri_q);
00387                 }
00388         } else {
00389                 cnt = 0;
00390                 *len = 0;
00391         }
00392 
00393         /* backup current branch index to restore it later */
00394         crt_branch = get_branch_iterator();
00395 
00396         init_branch_iterator();
00397         while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
00398                 cnt++;
00399                 *len += uri.len;
00400                 if (q != Q_UNSPECIFIED) {
00401                         *len += 1 + Q_PARAM_LEN + len_q(q);
00402                 }
00403         }
00404 
00405         if (cnt == 0) return 0; 
00406 
00407         *len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
00408 
00409         if (*len + 1 > MAX_REDIRECTION_LEN) {
00410                 LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
00411                 goto error;
00412         }
00413 
00414         memcpy(dset, CONTACT, CONTACT_LEN);
00415         p = dset + CONTACT_LEN;
00416         if (msg->new_uri.s) {
00417                 if (ruri_q != Q_UNSPECIFIED) {
00418                         *p++ = '<';
00419                 }
00420 
00421                 memcpy(p, msg->new_uri.s, msg->new_uri.len);
00422                 p += msg->new_uri.len;
00423 
00424                 if (ruri_q != Q_UNSPECIFIED) {
00425                         memcpy(p, Q_PARAM, Q_PARAM_LEN);
00426                         p += Q_PARAM_LEN;
00427 
00428                         qbuf = q2str(ruri_q, &qlen);
00429                         memcpy(p, qbuf, qlen);
00430                         p += qlen;
00431                 }
00432                 i = 1;
00433         } else {
00434                 i = 0;
00435         }
00436 
00437         init_branch_iterator();
00438         while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
00439                 if (i) {
00440                         memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
00441                         p += CONTACT_DELIM_LEN;
00442                 }
00443 
00444                 if (q != Q_UNSPECIFIED) {
00445                         *p++ = '<';
00446                 }
00447 
00448                 memcpy(p, uri.s, uri.len);
00449                 p += uri.len;
00450                 if (q != Q_UNSPECIFIED) {
00451                         memcpy(p, Q_PARAM, Q_PARAM_LEN);
00452                         p += Q_PARAM_LEN;
00453 
00454                         qbuf = q2str(q, &qlen);
00455                         memcpy(p, qbuf, qlen);
00456                         p += qlen;
00457                 }
00458                 i++;
00459         }
00460 
00461         memcpy(p, CRLF " ", CRLF_LEN + 1);
00462         set_branch_iterator(crt_branch);
00463         return dset;
00464 
00465 error:
00466         set_branch_iterator(crt_branch);
00467         return 0;
00468 }
00469 
00470 
00471 /*
00472  * Sets the q parameter of the Request-URI
00473  */
00474 void set_ruri_q(qvalue_t q)
00475 {
00476         ruri_q = q;
00477 }
00478 
00479 
00480 /*
00481  * Return the q value of the Request-URI
00482  */
00483 qvalue_t get_ruri_q(void)
00484 {
00485         return ruri_q;
00486 }
00487 
00488 
00489 
00490 /*
00491  * Rewrite Request-URI
00492  */
00493 int rewrite_uri(struct sip_msg* _m, str* _s)
00494 {
00495         char* buf;
00496 
00497         buf = (char*)pkg_malloc(_s->len + 1);
00498         if (!buf) {
00499                 LOG(L_ERR, "ERROR: rewrite_uri: No memory left\n");
00500                 return -1;
00501         }
00502 
00503         memcpy(buf, _s->s, _s->len);
00504         buf[_s->len] = '\0';
00505 
00506         _m->parsed_uri_ok = 0;
00507         if (_m->new_uri.s) {
00508                 pkg_free(_m->new_uri.s);
00509         }
00510 
00511         _m->new_uri.s = buf;
00512         _m->new_uri.len = _s->len;
00513         /* mark ruri as new and available for forking */
00514         ruri_mark_new();
00515 
00516         return 1;
00517 }
00518 

Generated on Tue May 22 2012 13:10:06 for SIP Router by  doxygen 1.7.1