00001 /* 00002 * $Id: data_lump_rpl.c 4597 2008-08-06 11:04:06Z klaus_darilion $ 00003 * 00004 * Copyright (C) 2001-2003 FhG Fokus 00005 * 00006 * This file is part of Kamailio, a free SIP server. 00007 * 00008 * Kamailio is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version 00012 * 00013 * Kamailio is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 * 00022 * History: 00023 * -------- 00024 * 2002-02-14 : created by bogdan 00025 * 2003-09-11 : lump_rpl type added - LUMP_RPL_BODY & LUMP_RPL_HDR (bogdan) 00026 * 2003-11-11 : build_lump_rpl merged into add_lump_rpl; types -> flags ; 00027 * flags LUMP_RPL_NODUP and LUMP_RPL_NOFREE added (bogdan) 00028 */ 00029 00030 /*! 00031 * \file 00032 * \brief Kamailio Generic lump functions for replies 00033 */ 00034 00035 00036 #include <string.h> 00037 #include "dprint.h" 00038 #include "mem/mem.h" 00039 #include "data_lump_rpl.h" 00040 00041 00042 00043 struct lump_rpl* add_lump_rpl(struct sip_msg *msg, char *s, int len, int flags) 00044 { 00045 struct lump_rpl *lump = 0; 00046 struct lump_rpl *foo; 00047 00048 /* some checking */ 00049 if ( (flags&(LUMP_RPL_HDR|LUMP_RPL_BODY))==(LUMP_RPL_HDR|LUMP_RPL_BODY) 00050 || (flags&(LUMP_RPL_HDR|LUMP_RPL_BODY))==0 || (flags&LUMP_RPL_SHMEM) ) { 00051 LM_ERR("bad flags combination (%d)!\n",flags); 00052 goto error; 00053 } 00054 if (len<=0 || s==0) { 00055 LM_ERR("I won't add an empty lump!\n"); 00056 goto error; 00057 } 00058 00059 /* build the lump */ 00060 lump = (struct lump_rpl*) pkg_malloc 00061 ( sizeof(struct lump_rpl) + ((flags&LUMP_RPL_NODUP)?0:len) ); 00062 if (!lump) { 00063 LM_ERR("no free pkg memory !\n"); 00064 goto error; 00065 } 00066 00067 if (flags&LUMP_RPL_NODUP) { 00068 lump->text.s = s; 00069 } else { 00070 lump->text.s = ((char*)lump)+sizeof(struct lump_rpl); 00071 memcpy( lump->text.s, s, len); 00072 } 00073 lump->text.len = len; 00074 lump->flags = flags; 00075 lump->next = 0; 00076 00077 /* add the lump to the msg */ 00078 if (!msg->reply_lump) { 00079 msg->reply_lump = lump; 00080 }else{ 00081 if (!(flags&LUMP_RPL_BODY)) 00082 for(foo=msg->reply_lump;foo->next;foo=foo->next); 00083 else 00084 for(foo=msg->reply_lump; ;foo=foo->next) { 00085 if (foo->flags&LUMP_RPL_BODY) { 00086 LM_ERR("LUMP_RPL_BODY already added!\n"); 00087 pkg_free(lump); 00088 goto error; 00089 } 00090 if (foo->next==0) 00091 break; 00092 } 00093 foo->next = lump; 00094 } 00095 00096 return lump; 00097 error: 00098 return 0; 00099 } 00100 00101 00102 00103 void free_lump_rpl(struct lump_rpl* lump) 00104 { 00105 if (lump) { 00106 if (!((lump->flags)&LUMP_RPL_NOFREE) && ((lump->flags)&LUMP_RPL_NODUP) 00107 && lump->text.s) 00108 pkg_free(lump->text.s); 00109 pkg_free(lump); 00110 } 00111 } 00112 00113 00114 void unlink_lump_rpl(struct sip_msg * msg, struct lump_rpl* lump) 00115 { 00116 struct lump_rpl *foo,*prev; 00117 00118 /* look for the lump to be unlink */ 00119 foo = msg->reply_lump; 00120 prev = 0; 00121 while( foo && foo!=lump ) { 00122 prev = foo; 00123 foo = foo->next; 00124 } 00125 00126 /* if the lump was found into the list -> unlink it */ 00127 if (foo) { 00128 if (prev) 00129 prev->next = foo->next; 00130 else 00131 msg->reply_lump = foo->next; 00132 } 00133 } 00134 00135 void del_nonshm_lump_rpl(struct lump_rpl** list) 00136 { 00137 struct lump_rpl* it, *tmp; 00138 struct lump_rpl** pred; 00139 00140 it = *list; 00141 pred = list; 00142 00143 while(it) { 00144 if (!(it->flags & LUMP_RPL_SHMEM)) { 00145 tmp = it; 00146 *pred = it->next; 00147 it = it->next; 00148 free_lump_rpl(tmp); 00149 continue; 00150 } 00151 00152 pred = &it->next; 00153 it = it->next; 00154 } 00155 } 00156
1.5.6