tm/sip_msg.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 FhG Fokus
00003  *
00004  * This file is part of ser, a free SIP server.
00005  *
00006  * ser is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version
00010  *
00011  * For a license to use the ser software under conditions
00012  * other than those described here, or to purchase support for this
00013  * software, please contact iptel.org by e-mail at the following addresses:
00014  *    info@iptel.org
00015  *
00016  * ser is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  * History:
00026  * --------
00027  *  2003-01-23 - msg_cloner clones msg->from->parsed too (janakj)
00028  *  2003-01-29 - scratchpad removed (jiri)
00029  *  2003-02-25 - auth_body cloner added (janakj)
00030  *  2003-02-28  scratchpad compatibility abandoned (jiri)
00031  *  2003-03-31  removed msg->repl_add_rm (andrei)
00032  *  2003-04-04  parsed uris are recalculated on cloning (jiri)
00033  *  2003-05-07  received, rport & i via shortcuts are also translated (andrei)
00034  *  2003-11-11  updated cloning of lump_rpl (bogdan)
00035  *  2004-03-31  alias shortcuts are also translated (andrei)
00036  *  2006-04-20  via->comp is also translated (andrei)
00037  *  2006-10-16  HDR_{PROXY,WWW}_AUTHENTICATE_T cloned (andrei)
00038  *  2007-01-26  HDR_DATE_T, HDR_IDENTITY_T, HDR_IDENTITY_INFO_T added (gergo)
00039  *  2007-09-05  A separate memory block is allocated for the lumps
00040  *              in case of requests in order to allow cloning them
00041  *              later than the SIP msg. (Miklos)
00042  * 2009-07-22  moved most of the functions to core sip_msg_clone.c  (andrei)*/
00043 
00066 #include "defs.h"
00067 
00068 
00069 #include "sip_msg.h"
00070 #include "../../dprint.h"
00071 #include "../../mem/mem.h"
00072 #include "../../data_lump.h"
00073 #include "../../data_lump_rpl.h"
00074 #include "../../ut.h"
00075 #include "../../sip_msg_clone.h"
00076 #include "../../fix_lumps.h"
00077 
00078 
00086 struct sip_msg*  sip_msg_cloner( struct sip_msg *org_msg, int *sip_msg_len )
00087 {
00088         /* take care of the lumps only for replies if the msg cloning is 
00089            postponed */
00090         if (org_msg->first_line.type==SIP_REPLY)
00091                 /*cloning all the lumps*/
00092                 return sip_msg_shm_clone(org_msg, sip_msg_len, 1);
00093         /* don't clone the lumps */
00094         return sip_msg_shm_clone(org_msg, sip_msg_len, 0);
00095 }
00096 
00100 unsigned char lumps_are_cloned = 0;
00101 
00102 
00103 
00110 int save_msg_lumps( struct sip_msg *shm_msg, struct sip_msg *pkg_msg)
00111 {
00112         int ret;
00113         struct lump* add_rm;
00114         struct lump* body_lumps;
00115         struct lump_rpl* reply_lump;
00116         
00117         /* make sure that we do not clone the lumps twice */
00118         if (lumps_are_cloned) {
00119                 LOG(L_DBG, "DEBUG: save_msg_lumps: lumps have been already cloned\n" );
00120                 return 0;
00121         }
00122         /* sanity checks */
00123         if (unlikely(!shm_msg || ((shm_msg->msg_flags & FL_SHM_CLONE)==0))) {
00124                 LOG(L_ERR, "ERROR: save_msg_lumps: BUG, there is no shmem-ized message"
00125                         " (shm_msg=%p)\n", shm_msg);
00126                 return -1;
00127         }
00128         if (unlikely(shm_msg->first_line.type!=SIP_REQUEST)) {
00129                 LOG(L_ERR, "ERROR: save_msg_lumps: BUG, the function should be called only for requests\n" );
00130                 return -1;
00131         }
00132 
00133 #ifdef EXTRA_DEBUG
00134         membar_depends();
00135         if (shm_msg->add_rm || shm_msg->body_lumps || shm_msg->reply_lump) {
00136                 LOG(L_ERR, "ERROR: save_msg_lumps: BUG, trying to overwrite the already cloned lumps\n");
00137                 return -1;
00138         }
00139 #endif
00140 
00141         /* needless to clone the lumps for ACK, they will not be used again */
00142         if (shm_msg->REQ_METHOD == METHOD_ACK)
00143                 return 0;
00144 
00145         /* clean possible previous added vias/clen header or else they would 
00146          * get propagated in the failure routes */
00147         free_via_clen_lump(&pkg_msg->add_rm);
00148 
00149         lumps_are_cloned = 1;
00150         ret=msg_lump_cloner(pkg_msg, &add_rm, &body_lumps, &reply_lump);
00151         if (likely(ret==0)){
00152                 /* make sure the lumps are fully written before adding them to
00153                    shm_msg (in case someone accesses it in the same time) */
00154                 membar_write();
00155                 shm_msg->add_rm = add_rm;
00156                 shm_msg->body_lumps = body_lumps;
00157                 shm_msg->reply_lump = reply_lump;
00158         }
00159         return ret<0?-1:0;
00160 }