data_lump.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* $Id$
  2. *
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History:
  28. * --------
  29. * 2003-01-19 support for duplication lump lists added (jiri)
  30. * 2003-03-31 added subst lumps --they expand in ip addr, port a.s.o (andrei)
  31. * 2003-04-01 added conditional lump support functions (andrei)
  32. * 2003-10-20 anchor_lump & del_lump will automatically choose the lump list
  33. * based on msg->eoh comparisons (andrei)
  34. * 2003-10-28 added extra checks (paranoia) for {anchor,del}_lump (andrei)
  35. * 2005-03-24 the type of type attribute changed to enum _hdr_types_t (janakj)
  36. */
  37. #include "data_lump.h"
  38. #include "dprint.h"
  39. #include "mem/mem.h"
  40. #include "globals.h"
  41. #include "error.h"
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #ifdef DEBUG_DMALLOC
  45. #include <dmalloc.h>
  46. #endif
  47. /* WARNING: all lump add/insert operations expect a pkg_malloc'ed char*
  48. * pointer the will be DEALLOCATED when the sip_msg is destroyed! */
  49. enum lump_dir { LD_NEXT, LD_BEFORE, LD_AFTER };
  50. /* adds a header to the end
  51. * returns pointer on success, 0 on error */
  52. struct lump* append_new_lump(struct lump** list, char* new_hdr,
  53. int len, enum _hdr_types_t type)
  54. {
  55. struct lump** t;
  56. struct lump* tmp;
  57. for (t=list;*t;t=&((*t)->next));
  58. tmp=pkg_malloc(sizeof(struct lump));
  59. if (tmp==0){
  60. LOG(L_ERR, "ERROR: append_new_lump: out of memory\n");
  61. return 0;
  62. }
  63. memset(tmp,0,sizeof(struct lump));
  64. tmp->type=type;
  65. tmp->op=LUMP_ADD;
  66. tmp->u.value=new_hdr;
  67. tmp->len=len;
  68. *t=tmp;
  69. return tmp;
  70. }
  71. /* inserts a header to the beginning
  72. * returns pointer if success, 0 on error */
  73. struct lump* insert_new_lump(struct lump** list, char* new_hdr,
  74. int len, enum _hdr_types_t type)
  75. {
  76. struct lump* tmp;
  77. tmp=pkg_malloc(sizeof(struct lump));
  78. if (tmp==0){
  79. LOG(L_ERR, "ERROR: insert_new_lump: out of memory\n");
  80. return 0;
  81. }
  82. memset(tmp,0,sizeof(struct lump));
  83. tmp->next=*list;
  84. tmp->type=type;
  85. tmp->op=LUMP_ADD;
  86. tmp->u.value=new_hdr;
  87. tmp->len=len;
  88. *list=tmp;
  89. return tmp;
  90. }
  91. /* inserts a header/data lump immediately after hdr
  92. * returns pointer on success, 0 on error */
  93. struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
  94. int len, enum _hdr_types_t type)
  95. {
  96. struct lump* tmp;
  97. tmp=pkg_malloc(sizeof(struct lump));
  98. if (tmp==0){
  99. ser_error=E_OUT_OF_MEM;
  100. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  101. return 0;
  102. }
  103. memset(tmp,0,sizeof(struct lump));
  104. tmp->after=after->after;
  105. tmp->type=type;
  106. tmp->op=LUMP_ADD;
  107. tmp->u.value=new_hdr;
  108. tmp->len=len;
  109. after->after=tmp;
  110. return tmp;
  111. }
  112. /* inserts a header/data lump immediately before "before"
  113. * returns pointer on success, 0 on error */
  114. struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
  115. int len, enum _hdr_types_t type)
  116. {
  117. struct lump* tmp;
  118. tmp=pkg_malloc(sizeof(struct lump));
  119. if (tmp==0){
  120. ser_error=E_OUT_OF_MEM;
  121. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  122. return 0;
  123. }
  124. memset(tmp,0,sizeof(struct lump));
  125. tmp->before=before->before;
  126. tmp->type=type;
  127. tmp->op=LUMP_ADD;
  128. tmp->u.value=new_hdr;
  129. tmp->len=len;
  130. before->before=tmp;
  131. return tmp;
  132. }
  133. /* inserts a subst lump immediately after hdr
  134. * returns pointer on success, 0 on error */
  135. struct lump* insert_subst_lump_after( struct lump* after, enum lump_subst subst,
  136. enum _hdr_types_t type)
  137. {
  138. struct lump* tmp;
  139. tmp=pkg_malloc(sizeof(struct lump));
  140. if (tmp==0){
  141. ser_error=E_OUT_OF_MEM;
  142. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  143. return 0;
  144. }
  145. memset(tmp,0,sizeof(struct lump));
  146. tmp->after=after->after;
  147. tmp->type=type;
  148. tmp->op=LUMP_ADD_SUBST;
  149. tmp->u.subst=subst;
  150. tmp->len=0;
  151. after->after=tmp;
  152. return tmp;
  153. }
  154. /* inserts a subst lump immediately before "before"
  155. * returns pointer on success, 0 on error */
  156. struct lump* insert_subst_lump_before( struct lump* before,
  157. enum lump_subst subst,
  158. enum _hdr_types_t type)
  159. {
  160. struct lump* tmp;
  161. tmp=pkg_malloc(sizeof(struct lump));
  162. if (tmp==0){
  163. ser_error=E_OUT_OF_MEM;
  164. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  165. return 0;
  166. }
  167. memset(tmp,0,sizeof(struct lump));
  168. tmp->before=before->before;
  169. tmp->type=type;
  170. tmp->op=LUMP_ADD_SUBST;
  171. tmp->u.subst=subst;
  172. tmp->len=0;
  173. before->before=tmp;
  174. return tmp;
  175. }
  176. /* inserts a cond lump immediately after hdr
  177. * returns pointer on success, 0 on error */
  178. struct lump* insert_cond_lump_after( struct lump* after, enum lump_conditions c,
  179. enum _hdr_types_t type)
  180. {
  181. struct lump* tmp;
  182. tmp=pkg_malloc(sizeof(struct lump));
  183. if (tmp==0){
  184. ser_error=E_OUT_OF_MEM;
  185. LOG(L_ERR, "ERROR: insert_new_lump_after: out of memory\n");
  186. return 0;
  187. }
  188. memset(tmp,0,sizeof(struct lump));
  189. tmp->after=after->after;
  190. tmp->type=type;
  191. tmp->op=LUMP_ADD_OPT;
  192. tmp->u.cond=c;
  193. tmp->len=0;
  194. after->after=tmp;
  195. return tmp;
  196. }
  197. /* inserts a conditional lump immediately before "before"
  198. * returns pointer on success, 0 on error */
  199. struct lump* insert_cond_lump_before( struct lump* before,
  200. enum lump_conditions c,
  201. enum _hdr_types_t type)
  202. {
  203. struct lump* tmp;
  204. tmp=pkg_malloc(sizeof(struct lump));
  205. if (tmp==0){
  206. ser_error=E_OUT_OF_MEM;
  207. LOG(L_ERR,"ERROR: insert_new_lump_before: out of memory\n");
  208. return 0;
  209. }
  210. memset(tmp,0,sizeof(struct lump));
  211. tmp->before=before->before;
  212. tmp->type=type;
  213. tmp->op=LUMP_ADD_OPT;
  214. tmp->u.cond=c;
  215. tmp->len=0;
  216. before->before=tmp;
  217. return tmp;
  218. }
  219. /* removes an already existing header/data lump */
  220. /* WARNING: this function adds the lump either to the msg->add_rm or
  221. * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  222. * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  223. * might affect the body!! */
  224. struct lump* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
  225. {
  226. struct lump* tmp;
  227. struct lump* prev, *t;
  228. struct lump** list;
  229. /* extra checks */
  230. if (offset>msg->len){
  231. LOG(L_CRIT, "BUG: del_lump: offset exceeds message size (%d > %d)"
  232. " aborting...\n", offset, msg->len);
  233. abort();
  234. }
  235. if (offset+len>msg->len){
  236. LOG(L_CRIT, " BUG: del_lump: offset + len exceeds message"
  237. " size (%d + %d > %d)\n", offset, len, msg->len);
  238. abort();
  239. }
  240. if (len==0){
  241. LOG(L_WARN, "WARNING: del_lump: called with 0 len (offset =%d)\n",
  242. offset);
  243. }
  244. tmp=pkg_malloc(sizeof(struct lump));
  245. if (tmp==0){
  246. LOG(L_ERR, "ERROR: del_lump: out of memory\n");
  247. return 0;
  248. }
  249. memset(tmp,0,sizeof(struct lump));
  250. tmp->op=LUMP_DEL;
  251. tmp->type=type;
  252. tmp->u.offset=offset;
  253. tmp->len=len;
  254. prev=0;
  255. /* check to see whether this might be a body lump */
  256. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  257. list=&msg->body_lumps;
  258. else
  259. list=&msg->add_rm;
  260. for (t=*list;t; prev=t, t=t->next){
  261. /* insert it sorted after offset */
  262. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  263. break;
  264. }
  265. tmp->next=t;
  266. if (prev) prev->next=tmp;
  267. else *list=tmp;
  268. return tmp;
  269. }
  270. /* add an anchor
  271. * WARNING: this function adds the lump either to the msg->add_rm or
  272. * msg->body_lumps list, depending on the offset being greater than msg->eoh,
  273. * so msg->eoh must be parsed (parse with HDR_EOH) if you think your lump
  274. * might affect the body!! */
  275. struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_t type)
  276. {
  277. struct lump* tmp;
  278. struct lump* prev, *t;
  279. struct lump** list;
  280. /* extra checks */
  281. if (offset>msg->len){
  282. LOG(L_CRIT, "BUG: anchor_lump: offset exceeds message size (%d > %d)"
  283. " aborting...\n", offset, msg->len);
  284. abort();
  285. }
  286. if (len){
  287. LOG(L_WARN, "WARNING: anchor_lump: called with len !=0 (%d)\n", len);
  288. if (offset+len>msg->len)
  289. LOG(L_WARN, "WARNING: anchor_lump: offset + len exceeds message"
  290. " size (%d + %d > %d)\n", offset, len, msg->len);
  291. }
  292. tmp=pkg_malloc(sizeof(struct lump));
  293. if (tmp==0){
  294. ser_error=E_OUT_OF_MEM;
  295. LOG(L_ERR, "ERROR: insert_new_lump_before: out of memory\n");
  296. return 0;
  297. }
  298. memset(tmp,0,sizeof(struct lump));
  299. tmp->op=LUMP_NOP;
  300. tmp->type=type;
  301. tmp->u.offset=offset;
  302. tmp->len=len;
  303. prev=0;
  304. /* check to see whether this might be a body lump */
  305. if ((msg->eoh) && (offset> (int)(msg->eoh-msg->buf)))
  306. list=&msg->body_lumps;
  307. else
  308. list=&msg->add_rm;
  309. for (t=*list;t; prev=t, t=t->next){
  310. /* insert it sorted after offset */
  311. if (((t->op==LUMP_DEL)||(t->op==LUMP_NOP))&&(t->u.offset>offset))
  312. break;
  313. }
  314. tmp->next=t;
  315. if (prev) prev->next=tmp;
  316. else *list=tmp;
  317. return tmp;
  318. }
  319. void free_lump(struct lump* lmp)
  320. {
  321. if (lmp && (lmp->op==LUMP_ADD)){
  322. if (lmp->u.value){
  323. if (lmp->flags &(LUMPFLAG_DUPED|LUMPFLAG_SHMEM)){
  324. LOG(L_CRIT, "BUG: free_lump: called on a not free-able lump:"
  325. "%p flags=%x\n", lmp, lmp->flags);
  326. abort();
  327. }else{
  328. pkg_free(lmp->u.value);
  329. lmp->u.value=0;
  330. lmp->len=0;
  331. }
  332. }
  333. }
  334. }
  335. void free_lump_list(struct lump* l)
  336. {
  337. struct lump* t, *r, *foo,*crt;
  338. t=l;
  339. while(t){
  340. crt=t;
  341. t=t->next;
  342. /*
  343. dangerous recursive clean
  344. if (crt->before) free_lump_list(crt->before);
  345. if (crt->after) free_lump_list(crt->after);
  346. */
  347. /* no more recursion, clean after and before and that's it */
  348. r=crt->before;
  349. while(r){
  350. foo=r; r=r->before;
  351. free_lump(foo);
  352. pkg_free(foo);
  353. }
  354. r=crt->after;
  355. while(r){
  356. foo=r; r=r->after;
  357. free_lump(foo);
  358. pkg_free(foo);
  359. }
  360. /*clean current elem*/
  361. free_lump(crt);
  362. pkg_free(crt);
  363. }
  364. }
  365. /* free (shallow-ly) a lump and its after/before lists */
  366. static void free_shallow_lump( struct lump *l )
  367. {
  368. struct lump *r, *foo;
  369. r=l->before;
  370. while(r){
  371. foo=r; r=r->before;
  372. pkg_free(foo);
  373. }
  374. r=l->after;
  375. while(r){
  376. foo=r; r=r->after;
  377. pkg_free(foo);
  378. }
  379. pkg_free(l);
  380. }
  381. /* duplicate (shallow-ly) a lump list into pkg memory */
  382. static struct lump *dup_lump_list_r( struct lump *l,
  383. enum lump_dir dir, int *error)
  384. {
  385. int deep_error;
  386. struct lump *new_lump;
  387. deep_error=0; /* optimist: assume success in recursion */
  388. /* if at list end, terminate recursion successfully */
  389. if (!l) { *error=0; return 0; }
  390. /* otherwise duplicate current element */
  391. new_lump=pkg_malloc(sizeof(struct lump));
  392. if (!new_lump) { *error=1; return 0; }
  393. memcpy(new_lump, l, sizeof(struct lump));
  394. new_lump->flags=LUMPFLAG_DUPED;
  395. new_lump->next=new_lump->before=new_lump->after=0;
  396. switch(dir) {
  397. case LD_NEXT:
  398. new_lump->before=dup_lump_list_r(l->before,
  399. LD_BEFORE, &deep_error);
  400. if (deep_error) goto deeperror;
  401. new_lump->after=dup_lump_list_r(l->after,
  402. LD_AFTER, &deep_error);
  403. if (deep_error) goto deeperror;
  404. new_lump->next=dup_lump_list_r(l->next,
  405. LD_NEXT, &deep_error);
  406. break;
  407. case LD_BEFORE:
  408. new_lump->before=dup_lump_list_r(l->before,
  409. LD_BEFORE, &deep_error);
  410. break;
  411. case LD_AFTER:
  412. new_lump->after=dup_lump_list_r(l->after,
  413. LD_AFTER, &deep_error);
  414. break;
  415. default:
  416. LOG(L_CRIT, "BUG: dup_limp_list_r: unknown dir: "
  417. "%d\n", dir );
  418. deep_error=1;
  419. }
  420. if (deep_error) goto deeperror;
  421. *error=0;
  422. return new_lump;
  423. deeperror:
  424. LOG(L_ERR, "ERROR: dup_lump_list_r: out of mem\n");
  425. free_shallow_lump(new_lump);
  426. *error=1;
  427. return 0;
  428. }
  429. /* shallow pkg copy of a lump list
  430. *
  431. * if either original list empty or error occur returns, 0
  432. * is returned, pointer to the copy otherwise
  433. */
  434. struct lump* dup_lump_list( struct lump *l )
  435. {
  436. int deep_error;
  437. deep_error=0;
  438. return dup_lump_list_r(l, LD_NEXT, &deep_error);
  439. }
  440. void free_duped_lump_list(struct lump* l)
  441. {
  442. struct lump *r, *foo,*crt;
  443. while(l){
  444. crt=l;
  445. l=l->next;
  446. r=crt->before;
  447. while(r){
  448. foo=r; r=r->before;
  449. /* (+): if a new item was introduced to the shallow-ly
  450. * duped list, remove it completely, preserve it
  451. * otherwise (it is still referred by original list)
  452. */
  453. if (foo->flags!=LUMPFLAG_DUPED)
  454. free_lump(foo);
  455. pkg_free(foo);
  456. }
  457. r=crt->after;
  458. while(r){
  459. foo=r; r=r->after;
  460. if (foo->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  461. free_lump(foo);
  462. pkg_free(foo);
  463. }
  464. /*clean current elem*/
  465. if (crt->flags!=LUMPFLAG_DUPED) /* (+) ... see above */
  466. free_lump(crt);
  467. pkg_free(crt);
  468. }
  469. }
  470. void del_nonshm_lump( struct lump** lump_list )
  471. {
  472. struct lump *r, *foo, *crt, **prev, *prev_r;
  473. prev = lump_list;
  474. crt = *lump_list;
  475. while (crt) {
  476. if (crt->flags!=LUMPFLAG_SHMEM) {
  477. /* unlink it */
  478. foo = crt;
  479. crt = crt->next;
  480. foo->next = 0;
  481. /* update the 'next' link of the previous lump */
  482. *prev = crt;
  483. /* entire before/after list must be removed */
  484. free_lump_list( foo );
  485. } else {
  486. /* check on before and prev list for non-shmem lumps */
  487. r = crt->after;
  488. prev_r = crt;
  489. while(r){
  490. foo=r; r=r->after;
  491. if (foo->flags!=LUMPFLAG_SHMEM) {
  492. prev_r->after = r;
  493. free_lump(foo);
  494. pkg_free(foo);
  495. } else {
  496. prev_r = foo;
  497. }
  498. }
  499. /* before */
  500. r = crt->before;
  501. prev_r = crt;
  502. while(r){
  503. foo=r; r=r->before;
  504. if (foo->flags!=LUMPFLAG_SHMEM) {
  505. prev_r->before = r;
  506. free_lump(foo);
  507. pkg_free(foo);
  508. } else {
  509. prev_r = foo;
  510. }
  511. }
  512. /* go to next lump */
  513. prev = &(crt->next);
  514. crt = crt->next;
  515. }
  516. }
  517. }