dset.c
caf80ae6
 /*
  * $Id$
  *
  * destination set
7dd0b342
  *
035f593c
  * Copyright (C) 2001-2004 FhG FOKUS
7dd0b342
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
caf80ae6
  */
 
 #include <string.h>
 #include "dprint.h"
 #include "config.h"
 #include "parser/parser_f.h"
 #include "parser/msg_parser.h"
 #include "ut.h"
 #include "hash_func.h"
30449150
 #include "error.h"
035f593c
 #include "dset.h"
e3d05bb9
 #include "mem/mem.h"
dc4b13e8
 #include "ip_addr.h"
caf80ae6
 
035f593c
 #define CONTACT "Contact: "
 #define CONTACT_LEN (sizeof(CONTACT) - 1)
caf80ae6
 
035f593c
 #define CONTACT_DELIM ", "
 #define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
 
 #define Q_PARAM ">;q="
 #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
 
 struct branch
 {
 	char uri[MAX_URI_SIZE];
 	unsigned int len;
02333eaa
 
 	     /* Real destination of the request */
 	char dst_uri[MAX_URI_SIZE];
 	unsigned int dst_uri_len;
 
035f593c
 	int q; /* Preference of the contact among
 		* contact within the array */
dc4b13e8
 	struct socket_info* force_send_socket;
035f593c
 };
 
 
 /* 
  * Where we store URIs of additional transaction branches
  * (-1 because of the default branch, #0)
  */
 static struct branch branches[MAX_BRANCHES - 1];
caf80ae6
 
 /* how many of them we have */
87962c1a
 unsigned int nr_branches = 0;
035f593c
 
caf80ae6
 /* branch iterator */
035f593c
 static int branch_iterator = 0;
 
 /* The q parameter of the Request-URI */
 static qvalue_t ruri_q = Q_UNSPECIFIED; 
 
caf80ae6
 
035f593c
 /*
53c7e0f1
  * Initialize the branch iterator, the next
035f593c
  * call to next_branch will return the first
  * contact from the dset array
  */
caf80ae6
 void init_branch_iterator(void)
 {
035f593c
 	branch_iterator = 0;
caf80ae6
 }
 
035f593c
 
 /*
  * Return the next branch from the dset
  * array, 0 is returned if there are no
  * more branches
  */
dc4b13e8
 char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket)
caf80ae6
 {
 	unsigned int i;
 
035f593c
 	i = branch_iterator;
 	if (i < nr_branches) {
caf80ae6
 		branch_iterator++;
035f593c
 		*len = branches[i].len;
 		*q = branches[i].q;
02333eaa
 		if (dst_uri && dst_len) {
 			*dst_uri = branches[i].dst_uri;
 			*dst_len = branches[i].dst_uri_len;
 		}
dc4b13e8
 		if (force_socket) {
 			*force_socket = branches[i].force_send_socket;
 		}
caf80ae6
 		return branches[i].uri;
 	} else {
035f593c
 		*len = 0;
 		*q = Q_UNSPECIFIED;
02333eaa
 		if (dst_uri && dst_len) {
 			*dst_uri = 0;
 			*dst_len = 0;
 		}
dc4b13e8
 		if (force_socket) {
 			*force_socket = 0;
 		}
caf80ae6
 		return 0;
 	}
 }
 
035f593c
 
 /*
  * Empty the dset array
  */
 void clear_branches(void)
caf80ae6
 {
035f593c
 	nr_branches = 0;
 	ruri_q = Q_UNSPECIFIED;
caf80ae6
 }
 
035f593c
 
 /* 
  * Add a new branch to current transaction 
  */
dc4b13e8
 int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, int dst_uri_len, 
 		  qvalue_t q, struct socket_info* force_socket)
caf80ae6
 {
035f593c
 	     /* if we have already set up the maximum number
 	      * of branches, don't try new ones 
 	      */
 	if (nr_branches == MAX_BRANCHES - 1) {
caf80ae6
 		LOG(L_ERR, "ERROR: append_branch: max nr of branches exceeded\n");
035f593c
 		ser_error = E_TOO_MANY_BRANCHES;
caf80ae6
 		return -1;
 	}
 
035f593c
 	if (uri_len > MAX_URI_SIZE - 1) {
caf80ae6
 		LOG(L_ERR, "ERROR: append_branch: too long uri: %.*s\n",
035f593c
 		    uri_len, uri);
caf80ae6
 		return -1;
 	}
1f1d845d
 	
 	if (dst_uri_len > MAX_URI_SIZE - 1) {
 		LOG(L_ERR, "ERROR: append_branch: too long dst_uri: %.*s\n",
 		    dst_uri_len, ZSW(dst_uri));
 		return -1;
 	}
caf80ae6
 
035f593c
 	     /* if not parameterized, take current uri */
 	if (uri == 0) {
caf80ae6
 		if (msg->new_uri.s) { 
035f593c
 			uri = msg->new_uri.s;
 			uri_len = msg->new_uri.len;
caf80ae6
 		} else {
035f593c
 			uri = msg->first_line.u.request.uri.s;
 			uri_len = msg->first_line.u.request.uri.len;
caf80ae6
 		}
 	}
 	
035f593c
 	memcpy(branches[nr_branches].uri, uri, uri_len);
 	     /* be safe -- add zero termination */
 	branches[nr_branches].uri[uri_len] = 0;
 	branches[nr_branches].len = uri_len;
 	branches[nr_branches].q = q;
caf80ae6
 	
13050579
  	if (dst_uri && dst_uri_len) {
   		memcpy(branches[nr_branches].dst_uri, dst_uri, dst_uri_len);
   		branches[nr_branches].dst_uri[dst_uri_len] = 0;
   		branches[nr_branches].dst_uri_len = dst_uri_len;
  	} else {
  		branches[nr_branches].dst_uri[0] = '\0';
  		branches[nr_branches].dst_uri_len = 0;
02333eaa
 	}
 
dc4b13e8
 	branches[nr_branches].force_send_socket = force_socket;
 	
caf80ae6
 	nr_branches++;
 	return 1;
 }
2a78db5c
 
 
035f593c
 /*
  * Create a Contact header field from the dset
  * array
  */
 char* print_dset(struct sip_msg* msg, int* len) 
2a78db5c
 {
d7a3fdea
 	int cnt, i;
 	unsigned int qlen;
035f593c
 	qvalue_t q;
2a78db5c
 	str uri;
b0edf8f9
 	char* p, *qbuf;
2a78db5c
 	static char dset[MAX_REDIRECTION_LEN];
 
 	if (msg->new_uri.s) {
035f593c
 		cnt = 1;
 		*len = msg->new_uri.len;
 		if (ruri_q != Q_UNSPECIFIED) {
 			*len += 1 + Q_PARAM_LEN + len_q(ruri_q);
 		}
2a78db5c
 	} else {
035f593c
 		cnt = 0;
 		*len = 0;
2a78db5c
 	}
 
 	init_branch_iterator();
dc4b13e8
 	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
2a78db5c
 		cnt++;
035f593c
 		*len += uri.len;
 		if (q != Q_UNSPECIFIED) {
 			*len += 1 + Q_PARAM_LEN + len_q(q);
 		}
2a78db5c
 	}
 
035f593c
 	if (cnt == 0) return 0;	
2a78db5c
 
035f593c
 	*len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
2a78db5c
 
035f593c
 	if (*len + 1 > MAX_REDIRECTION_LEN) {
2a78db5c
 		LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
 		return 0;
 	}
 
035f593c
 	memcpy(dset, CONTACT, CONTACT_LEN);
 	p = dset + CONTACT_LEN;
2a78db5c
 	if (msg->new_uri.s) {
035f593c
 		if (ruri_q != Q_UNSPECIFIED) {
 			*p++ = '<';
 		}
 
2a78db5c
 		memcpy(p, msg->new_uri.s, msg->new_uri.len);
035f593c
 		p += msg->new_uri.len;
 
 		if (ruri_q != Q_UNSPECIFIED) {
 			memcpy(p, Q_PARAM, Q_PARAM_LEN);
 			p += Q_PARAM_LEN;
b0edf8f9
 
 			qbuf = q2str(ruri_q, &qlen);
 			memcpy(p, qbuf, qlen);
 			p += qlen;
035f593c
 		}
 		i = 1;
 	} else {
 		i = 0;
 	}
2a78db5c
 
 	init_branch_iterator();
dc4b13e8
 	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
2a78db5c
 		if (i) {
035f593c
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
 			p += CONTACT_DELIM_LEN;
 		}
 
 		if (q != Q_UNSPECIFIED) {
 			*p++ = '<';
2a78db5c
 		}
035f593c
 
2a78db5c
 		memcpy(p, uri.s, uri.len);
035f593c
 		p += uri.len;
 		if (q != Q_UNSPECIFIED) {
 			memcpy(p, Q_PARAM, Q_PARAM_LEN);
 			p += Q_PARAM_LEN;
b0edf8f9
 
 			qbuf = q2str(q, &qlen);
 			memcpy(p, qbuf, qlen);
 			p += qlen;
035f593c
 		}
2a78db5c
 		i++;
 	}
035f593c
 
 	memcpy(p, CRLF " ", CRLF_LEN + 1);
2a78db5c
 	return dset;
 }
 
035f593c
 
 /*
  * Sets the q parameter of the Request-URI
  */
 void set_ruri_q(qvalue_t q)
 {
 	ruri_q = q;
 }
 
 
 /*
  * Return the q value of the Request-URI
  */
 qvalue_t get_ruri_q(void)
 {
 	return ruri_q;
 }
7c8890f0
 
 
 
 /*
  * Get actual Request-URI
  */
 int get_request_uri(struct sip_msg* _m, str* _u)
 {
 	     /* Use new_uri if present */
 	if (_m->new_uri.s) {
 		_u->s = _m->new_uri.s;
 		_u->len = _m->new_uri.len;
 	} else {
 		_u->s = _m->first_line.u.request.uri.s;
 		_u->len = _m->first_line.u.request.uri.len;
 	}
 
 	return 0;
 }
 
 
 /*
  * Rewrite Request-URI
  */
 int rewrite_uri(struct sip_msg* _m, str* _s)
 {
         char* buf;
 
         buf = (char*)pkg_malloc(_s->len + 1);
         if (!buf) {
fcf655d0
                 LOG(L_ERR, "ERROR: rewrite_uri: No memory left\n");
7c8890f0
                 return -1;
         }
 
         memcpy(buf, _s->s, _s->len);
         buf[_s->len] = '\0';
 
         _m->parsed_uri_ok = 0;
         if (_m->new_uri.s) {
                 pkg_free(_m->new_uri.s);
         }
 
         _m->new_uri.s = buf;
         _m->new_uri.len = _s->len;
 
fcf655d0
         DBG("rewrite_uri: Rewriting Request-URI with '%.*s'\n", _s->len, 
7c8890f0
 																		   buf);
607468e1
         return 1;
7c8890f0
 }