usr_avp.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * History:
  26. * ---------
  27. * 2004-07-21 created (bogdan)
  28. * 2004-10-09 interface more flexible - more function available (bogdan)
  29. * 2004-11-07 AVP string values are kept 0 terminated (bogdan)
  30. * 2004-11-14 global aliases support added
  31. * 2005-01-05 parse avp name according new syntax
  32. */
  33. /*!
  34. * \file
  35. * \brief SIP-router core ::
  36. * \ingroup core
  37. * Module: \ref core
  38. */
  39. #include <assert.h>
  40. #include <ctype.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include "sr_module.h"
  45. #include "dprint.h"
  46. #include "str.h"
  47. #include "ut.h"
  48. #include "mem/shm_mem.h"
  49. #include "mem/mem.h"
  50. #include "usr_avp.h"
  51. enum idx {
  52. IDX_FROM_URI = 0,
  53. IDX_TO_URI,
  54. IDX_FROM_USER,
  55. IDX_TO_USER,
  56. IDX_FROM_DOMAIN,
  57. IDX_TO_DOMAIN,
  58. IDX_MAX
  59. };
  60. struct avp_galias {
  61. str alias;
  62. struct avp_spec avp;
  63. struct avp_galias *next;
  64. };
  65. static struct avp_galias *galiases = 0;
  66. static avp_list_t def_list[IDX_MAX]; /* Default AVP lists */
  67. static avp_list_t* crt_list[IDX_MAX]; /* Pointer to current AVP lists */
  68. /* Global AVP related variables go to shm mem */
  69. static avp_list_t* def_glist;
  70. static avp_list_t** crt_glist;
  71. /* AVP flags */
  72. int registered_avpflags_no = 0;
  73. static char *registered_avpflags[MAX_AVPFLAG];
  74. /* Initialize AVP lists in private memory and allocate memory
  75. * for shared lists
  76. */
  77. int init_avps(void)
  78. {
  79. int i;
  80. /* Empty default lists */
  81. memset(def_list, 0, sizeof(avp_list_t) * IDX_MAX);
  82. /* Point current pointers to default lists */
  83. for(i = 0; i < IDX_MAX; i++) {
  84. crt_list[i] = &def_list[i];
  85. }
  86. def_glist = (avp_list_t*)shm_malloc(sizeof(avp_list_t));
  87. crt_glist = (avp_list_t**)shm_malloc(sizeof(avp_list_t*));
  88. if (!def_glist || !crt_glist) {
  89. LM_ERR("No memory to allocate default global AVP list\n");
  90. return -1;
  91. }
  92. *def_glist = 0;
  93. *crt_glist = def_glist;
  94. return 0;
  95. }
  96. /*
  97. * Select active AVP list based on the value of flags
  98. */
  99. static avp_list_t* select_list(avp_flags_t flags)
  100. {
  101. if (flags & AVP_CLASS_URI) {
  102. if (flags & AVP_TRACK_TO) {
  103. return crt_list[IDX_TO_URI];
  104. } else {
  105. return crt_list[IDX_FROM_URI];
  106. }
  107. } else if (flags & AVP_CLASS_USER) {
  108. if (flags & AVP_TRACK_TO) {
  109. return crt_list[IDX_TO_USER];
  110. } else {
  111. return crt_list[IDX_FROM_USER];
  112. }
  113. } else if (flags & AVP_CLASS_DOMAIN) {
  114. if (flags & AVP_TRACK_TO) {
  115. return crt_list[IDX_TO_DOMAIN];
  116. } else {
  117. return crt_list[IDX_FROM_DOMAIN];
  118. }
  119. } else if (flags & AVP_CLASS_GLOBAL) {
  120. return *crt_glist;
  121. }
  122. return NULL;
  123. }
  124. inline static avp_id_t compute_ID( str *name )
  125. {
  126. char *p;
  127. avp_id_t id;
  128. id=0;
  129. for( p=name->s+name->len-1 ; p>=name->s ; p-- )
  130. id ^= *p;
  131. return id;
  132. }
  133. avp_t *create_avp (avp_flags_t flags, avp_name_t name, avp_value_t val)
  134. {
  135. avp_t *avp;
  136. str *s;
  137. struct str_int_data *sid;
  138. struct str_str_data *ssd;
  139. int len;
  140. if (name.s.s == 0 && name.s.len == 0) {
  141. LM_ERR("0 ID or NULL NAME AVP!");
  142. goto error;
  143. }
  144. /* compute the required mem size */
  145. len = sizeof(struct usr_avp);
  146. if (flags&AVP_NAME_STR) {
  147. if ( name.s.s==0 || name.s.len==0) {
  148. LM_ERR("EMPTY NAME AVP!");
  149. goto error;
  150. }
  151. if (flags&AVP_VAL_STR) {
  152. len += sizeof(struct str_str_data)-sizeof(union usr_avp_data)
  153. + name.s.len + 1 /* Terminating zero for regex search */
  154. + val.s.len + 1; /* Value is zero terminated */
  155. } else {
  156. len += sizeof(struct str_int_data)-sizeof(union usr_avp_data)
  157. + name.s.len + 1; /* Terminating zero for regex search */
  158. }
  159. } else if (flags&AVP_VAL_STR) {
  160. len += sizeof(str)-sizeof(union usr_avp_data) + val.s.len + 1;
  161. }
  162. avp = (struct usr_avp*)shm_malloc( len );
  163. if (avp==0) {
  164. LM_ERR("no more shm mem\n");
  165. return 0;
  166. }
  167. avp->flags = flags;
  168. avp->id = (flags&AVP_NAME_STR)? compute_ID(&name.s) : name.n ;
  169. avp->next = NULL;
  170. switch ( flags&(AVP_NAME_STR|AVP_VAL_STR) )
  171. {
  172. case 0:
  173. /* avp type ID, int value */
  174. avp->d.l = val.n;
  175. break;
  176. case AVP_NAME_STR:
  177. /* avp type str, int value */
  178. sid = (struct str_int_data*)&avp->d.data[0];
  179. sid->val = val.n;
  180. sid->name.len =name.s.len;
  181. sid->name.s = (char*)sid + sizeof(struct str_int_data);
  182. memcpy( sid->name.s , name.s.s, name.s.len);
  183. sid->name.s[name.s.len] = '\0'; /* Zero terminator */
  184. break;
  185. case AVP_VAL_STR:
  186. /* avp type ID, str value */
  187. s = (str*)&avp->d.data[0];
  188. s->len = val.s.len;
  189. s->s = (char*)s + sizeof(str);
  190. memcpy( s->s, val.s.s , s->len);
  191. s->s[s->len] = 0;
  192. break;
  193. case AVP_NAME_STR|AVP_VAL_STR:
  194. /* avp type str, str value */
  195. ssd = (struct str_str_data*)&avp->d.data[0];
  196. ssd->name.len = name.s.len;
  197. ssd->name.s = (char*)ssd + sizeof(struct str_str_data);
  198. memcpy( ssd->name.s , name.s.s, name.s.len);
  199. ssd->name.s[name.s.len]='\0'; /* Zero terminator */
  200. ssd->val.len = val.s.len;
  201. ssd->val.s = ssd->name.s + ssd->name.len + 1;
  202. memcpy( ssd->val.s , val.s.s, val.s.len);
  203. ssd->val.s[ssd->val.len] = 0;
  204. break;
  205. }
  206. return avp;
  207. error:
  208. return 0;
  209. }
  210. int add_avp_list(avp_list_t* list, avp_flags_t flags, avp_name_t name, avp_value_t val)
  211. {
  212. avp_t *avp;
  213. assert(list != 0);
  214. if ((avp = create_avp(flags, name, val))) {
  215. avp->next = *list;
  216. *list = avp;
  217. return 0;
  218. }
  219. return -1;
  220. }
  221. int add_avp(avp_flags_t flags, avp_name_t name, avp_value_t val)
  222. {
  223. avp_flags_t avp_class;
  224. avp_list_t* list;
  225. /* Add avp to uri class if no class has been
  226. * specified by the caller
  227. */
  228. if ((flags & AVP_CLASS_ALL) == 0) flags |= AVP_CLASS_URI;
  229. if ((flags & AVP_TRACK_ALL) == 0) flags |= AVP_TRACK_FROM;
  230. if (!(list = select_list(flags)))
  231. return -1;
  232. if (flags & AVP_CLASS_URI) avp_class = AVP_CLASS_URI;
  233. else if (flags & AVP_CLASS_USER) avp_class = AVP_CLASS_USER;
  234. else if (flags & AVP_CLASS_DOMAIN) avp_class = AVP_CLASS_DOMAIN;
  235. else avp_class = AVP_CLASS_GLOBAL;
  236. /* Make that only the selected class is set
  237. * if the caller set more classes in flags
  238. */
  239. return add_avp_list(list, flags & (~(AVP_CLASS_ALL) | avp_class), name, val);
  240. }
  241. int add_avp_before(avp_t *avp, avp_flags_t flags, avp_name_t name, avp_value_t val)
  242. {
  243. avp_t *new_avp;
  244. if (!avp) {
  245. return add_avp(flags, name, val);
  246. }
  247. if ((flags & AVP_CLASS_ALL) == 0) flags |= (avp->flags & AVP_CLASS_ALL);
  248. if ((flags & AVP_TRACK_ALL) == 0) flags |= (avp->flags & AVP_TRACK_ALL);
  249. if ((avp->flags & (AVP_CLASS_ALL|AVP_TRACK_ALL)) != (flags & (AVP_CLASS_ALL|AVP_TRACK_ALL))) {
  250. LM_ERR("Source and target AVPs have different CLASS/TRACK\n");
  251. return -1;
  252. }
  253. if ((new_avp=create_avp(flags, name, val))) {
  254. new_avp->next=avp->next;
  255. avp->next=new_avp;
  256. return 0;
  257. }
  258. return -1;
  259. }
  260. /* get value functions */
  261. inline str* get_avp_name(avp_t *avp)
  262. {
  263. struct str_int_data *sid;
  264. struct str_str_data *ssd;
  265. switch ( avp->flags&(AVP_NAME_STR|AVP_VAL_STR) )
  266. {
  267. case 0:
  268. /* avp type ID, int value */
  269. case AVP_VAL_STR:
  270. /* avp type ID, str value */
  271. return 0;
  272. case AVP_NAME_STR:
  273. /* avp type str, int value */
  274. sid = (struct str_int_data*)&avp->d.data[0];
  275. return &sid->name;
  276. case AVP_NAME_STR|AVP_VAL_STR:
  277. /* avp type str, str value */
  278. ssd = (struct str_str_data*)&avp->d.data[0];
  279. return &ssd->name;
  280. }
  281. LM_ERR("unknown avp type (name&val) %d\n", avp->flags&(AVP_NAME_STR|AVP_VAL_STR));
  282. return 0;
  283. }
  284. inline void get_avp_val(avp_t *avp, avp_value_t *val)
  285. {
  286. str *s;
  287. struct str_int_data *sid;
  288. struct str_str_data *ssd;
  289. if (avp==0 || val==0)
  290. return;
  291. switch ( avp->flags&(AVP_NAME_STR|AVP_VAL_STR) ) {
  292. case 0:
  293. /* avp type ID, int value */
  294. val->n = avp->d.l;
  295. break;
  296. case AVP_NAME_STR:
  297. /* avp type str, int value */
  298. sid = (struct str_int_data*)&avp->d.data[0];
  299. val->n = sid->val;
  300. break;
  301. case AVP_VAL_STR:
  302. /* avp type ID, str value */
  303. s = (str*)&avp->d.data[0];
  304. val->s = *s;
  305. break;
  306. case AVP_NAME_STR|AVP_VAL_STR:
  307. /* avp type str, str value */
  308. ssd = (struct str_str_data*)&avp->d.data[0];
  309. val->s = ssd->val;
  310. break;
  311. }
  312. }
  313. /* Return the current list of user attributes */
  314. avp_list_t get_avp_list(avp_flags_t flags)
  315. {
  316. avp_list_t *list;
  317. list = select_list(flags);
  318. return (list ? *list : NULL);
  319. }
  320. /*
  321. * Compare given id with id in avp, return true if they match
  322. */
  323. static inline int match_by_id(avp_t* avp, avp_id_t id)
  324. {
  325. if (avp->id == id && (avp->flags&AVP_NAME_STR)==0) {
  326. return 1;
  327. }
  328. return 0;
  329. }
  330. /*
  331. * Compare given name with name in avp, return true if they are same
  332. */
  333. static inline int match_by_name(avp_t* avp, avp_id_t id, str* name)
  334. {
  335. str* avp_name;
  336. if (id==avp->id && avp->flags&AVP_NAME_STR &&
  337. (avp_name=get_avp_name(avp))!=0 && avp_name->len==name->len
  338. && !strncasecmp( avp_name->s, name->s, name->len) ) {
  339. return 1;
  340. }
  341. return 0;
  342. }
  343. /*
  344. * Compare name with name in AVP using regular expressions, return
  345. * true if they match
  346. */
  347. static inline int match_by_re(avp_t* avp, regex_t* re)
  348. {
  349. regmatch_t pmatch;
  350. str * avp_name;
  351. /* AVP identifiable by name ? */
  352. if (!(avp->flags&AVP_NAME_STR)) return 0;
  353. if ((avp_name=get_avp_name(avp))==0) /* valid AVP name ? */
  354. return 0;
  355. if (!avp_name->s) /* AVP name validation */
  356. return 0;
  357. if (regexec(re, avp_name->s, 1, &pmatch,0)==0) { /* re match ? */
  358. return 1;
  359. }
  360. return 0;
  361. }
  362. avp_t *search_first_avp(avp_flags_t flags, avp_name_t name, avp_value_t *val, struct search_state* s)
  363. {
  364. avp_ident_t id;
  365. id.flags = flags;
  366. id.name = name;
  367. id.index = 0;
  368. return search_avp (id, val, s);
  369. }
  370. avp_t *search_avp (avp_ident_t ident, avp_value_t* val, struct search_state* state)
  371. {
  372. avp_t* ret;
  373. static struct search_state st;
  374. avp_list_t* list;
  375. if (ident.name.s.s==0 && ident.name.s.len == 0) {
  376. LM_ERR("0 ID or NULL NAME AVP!");
  377. return 0;
  378. }
  379. switch (ident.flags & AVP_INDEX_ALL) {
  380. case AVP_INDEX_BACKWARD:
  381. case AVP_INDEX_FORWARD:
  382. WARN("AVP specified with index, but not used for search\n");
  383. break;
  384. }
  385. if (!state) state = &st;
  386. if ((ident.flags & AVP_CLASS_ALL) == 0) {
  387. /* The caller did not specify any class to search in, so enable
  388. * all of them by default
  389. */
  390. ident.flags |= AVP_CLASS_ALL;
  391. if ((ident.flags & AVP_TRACK_ALL) == 0) {
  392. /* The caller did not specify even the track to search in, so search
  393. * in the track_from
  394. */
  395. ident.flags |= AVP_TRACK_FROM;
  396. }
  397. }
  398. if (!(list = select_list(ident.flags)))
  399. return NULL;
  400. state->flags = ident.flags;
  401. state->avp = *list;
  402. state->name = ident.name;
  403. if (ident.flags & AVP_NAME_STR) {
  404. state->id = compute_ID(&ident.name.s);
  405. }
  406. ret = search_next_avp(state, val);
  407. /* Make sure that search next avp stays in the same class as the first
  408. * avp found
  409. */
  410. if (state && ret) state->flags = (ident.flags & ~AVP_CLASS_ALL) | (ret->flags & AVP_CLASS_ALL);
  411. return ret;
  412. }
  413. avp_t *search_next_avp(struct search_state* s, avp_value_t *val )
  414. {
  415. int matched;
  416. avp_t* avp;
  417. avp_list_t *list;
  418. if (s == 0) {
  419. LM_ERR("Invalid parameter value\n");
  420. return 0;
  421. }
  422. switch (s->flags & AVP_INDEX_ALL) {
  423. case AVP_INDEX_BACKWARD:
  424. case AVP_INDEX_FORWARD:
  425. WARN("AVP specified with index, but not used for search\n");
  426. break;
  427. }
  428. while(1) {
  429. for( ; s->avp; s->avp = s->avp->next) {
  430. if (s->flags & AVP_NAME_RE) {
  431. matched = match_by_re(s->avp, s->name.re);
  432. } else if (s->flags & AVP_NAME_STR) {
  433. matched = match_by_name(s->avp, s->id, &s->name.s);
  434. } else {
  435. matched = match_by_id(s->avp, s->name.n);
  436. }
  437. if (matched) {
  438. avp = s->avp;
  439. s->avp = s->avp->next;
  440. if (val) get_avp_val(avp, val);
  441. return avp;
  442. }
  443. }
  444. if (s->flags & AVP_CLASS_URI) {
  445. s->flags &= ~AVP_CLASS_URI;
  446. list = select_list(s->flags);
  447. } else if (s->flags & AVP_CLASS_USER) {
  448. s->flags &= ~AVP_CLASS_USER;
  449. list = select_list(s->flags);
  450. } else if (s->flags & AVP_CLASS_DOMAIN) {
  451. s->flags &= ~AVP_CLASS_DOMAIN;
  452. list = select_list(s->flags);
  453. } else {
  454. s->flags &= ~AVP_CLASS_GLOBAL;
  455. return 0;
  456. }
  457. if (!list) return 0;
  458. s->avp = *list;
  459. }
  460. return 0;
  461. }
  462. int search_reverse( avp_t *cur, struct search_state* st,
  463. avp_index_t index, avp_list_t *ret)
  464. {
  465. avp_index_t lvl;
  466. if (!cur)
  467. return 0;
  468. lvl = search_reverse(search_next_avp(st, NULL), st, index, ret)+1;
  469. if (index==lvl)
  470. *ret=cur;
  471. return lvl;
  472. }
  473. avp_t *search_avp_by_index( avp_flags_t flags, avp_name_t name,
  474. avp_value_t *val, avp_index_t index)
  475. {
  476. avp_t *ret, *cur;
  477. struct search_state st;
  478. if (flags & AVP_NAME_RE) {
  479. BUG("search_by_index not supported for AVP_NAME_RE\n");
  480. return 0;
  481. }
  482. switch (flags & AVP_INDEX_ALL) {
  483. case 0:
  484. ret = search_first_avp(flags, name, val, &st);
  485. if (!ret || search_next_avp(&st, NULL))
  486. return 0;
  487. else
  488. return ret;
  489. case AVP_INDEX_ALL:
  490. BUG("search_by_index not supported for anonymous index []\n");
  491. return 0;
  492. case AVP_INDEX_FORWARD:
  493. ret = NULL;
  494. cur = search_first_avp(flags & ~AVP_INDEX_ALL, name, NULL, &st);
  495. search_reverse(cur, &st, index, &ret);
  496. if (ret && val)
  497. get_avp_val(ret, val);
  498. return ret;
  499. case AVP_INDEX_BACKWARD:
  500. ret = search_first_avp(flags & ~AVP_INDEX_ALL, name, val, &st);
  501. for (index--; (ret && index); ret=search_next_avp(&st, val), index--);
  502. return ret;
  503. }
  504. return 0;
  505. }
  506. /* FIXME */
  507. /********* free functions ********/
  508. void destroy_avp(avp_t *avp_del)
  509. {
  510. int i;
  511. avp_t *avp, *avp_prev;
  512. for (i = 0; i < IDX_MAX; i++) {
  513. for( avp_prev=0,avp=*crt_list[i] ; avp ;
  514. avp_prev=avp,avp=avp->next ) {
  515. if (avp==avp_del) {
  516. if (avp_prev) {
  517. avp_prev->next=avp->next;
  518. } else {
  519. *crt_list[i] = avp->next;
  520. }
  521. shm_free(avp);
  522. return;
  523. }
  524. }
  525. }
  526. for( avp_prev=0,avp=**crt_glist ; avp ;
  527. avp_prev=avp,avp=avp->next ) {
  528. if (avp==avp_del) {
  529. if (avp_prev) {
  530. avp_prev->next=avp->next;
  531. } else {
  532. **crt_glist = avp->next;
  533. }
  534. shm_free(avp);
  535. return;
  536. }
  537. }
  538. }
  539. void destroy_avp_list_unsafe(avp_list_t* list)
  540. {
  541. avp_t *avp, *foo;
  542. avp = *list;
  543. while( avp ) {
  544. foo = avp;
  545. avp = avp->next;
  546. shm_free_unsafe( foo );
  547. }
  548. *list = 0;
  549. }
  550. inline void destroy_avp_list(avp_list_t* list)
  551. {
  552. avp_t *avp, *foo;
  553. DBG("DEBUG:destroy_avp_list: destroying list %p\n", *list);
  554. avp = *list;
  555. while( avp ) {
  556. foo = avp;
  557. avp = avp->next;
  558. shm_free( foo );
  559. }
  560. *list = 0;
  561. }
  562. int reset_avp_list(int flags)
  563. {
  564. int i;
  565. if (flags & AVP_CLASS_URI) {
  566. if (flags & AVP_TRACK_FROM) i = IDX_FROM_URI;
  567. else i = IDX_TO_URI;
  568. } else if (flags & AVP_CLASS_USER) {
  569. if (flags & AVP_TRACK_FROM) i = IDX_FROM_USER;
  570. else i = IDX_TO_USER;
  571. } else if (flags & AVP_CLASS_DOMAIN) {
  572. if (flags & AVP_TRACK_FROM) i = IDX_FROM_DOMAIN;
  573. else i = IDX_TO_DOMAIN;
  574. } else return -1;
  575. crt_list[i] = &def_list[i];
  576. destroy_avp_list(crt_list[i]);
  577. return 0;
  578. }
  579. void reset_avps(void)
  580. {
  581. int i;
  582. for(i = 0; i < IDX_MAX; i++) {
  583. crt_list[i] = &def_list[i];
  584. destroy_avp_list(crt_list[i]);
  585. }
  586. }
  587. avp_list_t* set_avp_list( avp_flags_t flags, avp_list_t* list )
  588. {
  589. avp_list_t* prev;
  590. if (flags & AVP_CLASS_URI) {
  591. if (flags & AVP_TRACK_FROM) {
  592. prev = crt_list[IDX_FROM_URI];
  593. crt_list[IDX_FROM_URI] = list;
  594. } else {
  595. prev = crt_list[IDX_TO_URI];
  596. crt_list[IDX_TO_URI] = list;
  597. }
  598. } else if (flags & AVP_CLASS_USER) {
  599. if (flags & AVP_TRACK_FROM) {
  600. prev = crt_list[IDX_FROM_USER];
  601. crt_list[IDX_FROM_USER] = list;
  602. } else {
  603. prev = crt_list[IDX_TO_USER];
  604. crt_list[IDX_TO_USER] = list;
  605. }
  606. } else if (flags & AVP_CLASS_DOMAIN) {
  607. if (flags & AVP_TRACK_FROM) {
  608. prev = crt_list[IDX_FROM_DOMAIN];
  609. crt_list[IDX_FROM_DOMAIN] = list;
  610. } else {
  611. prev = crt_list[IDX_TO_DOMAIN];
  612. crt_list[IDX_TO_DOMAIN] = list;
  613. }
  614. } else {
  615. prev = *crt_glist;
  616. *crt_glist = list;
  617. }
  618. return prev;
  619. }
  620. /********* global aliases functions ********/
  621. static inline int check_avp_galias(str *alias, int type, int_str avp_name)
  622. {
  623. struct avp_galias *ga;
  624. type &= AVP_NAME_STR;
  625. for( ga=galiases ; ga ; ga=ga->next ) {
  626. /* check for duplicated alias names */
  627. if ( alias->len==ga->alias.len &&
  628. (strncasecmp( alias->s, ga->alias.s, alias->len)==0) )
  629. return -1;
  630. /*check for duplicated avp names */
  631. if (type==ga->avp.type) {
  632. if (type&AVP_NAME_STR){
  633. if (avp_name.s.len==ga->avp.name.s.len &&
  634. (strncasecmp(avp_name.s.s, ga->avp.name.s.s,
  635. avp_name.s.len)==0) )
  636. return -1;
  637. } else {
  638. if (avp_name.n==ga->avp.name.n)
  639. return -1;
  640. }
  641. }
  642. }
  643. return 0;
  644. }
  645. int add_avp_galias(str *alias, int type, int_str avp_name)
  646. {
  647. struct avp_galias *ga;
  648. if ((type&AVP_NAME_STR && (!avp_name.s.s ||
  649. !avp_name.s.len)) ||!alias || !alias->s ||
  650. !alias->len ){
  651. LM_ERR("null params received\n");
  652. goto error;
  653. }
  654. if (check_avp_galias(alias,type,avp_name)!=0) {
  655. LM_ERR("duplicate alias/avp entry\n");
  656. goto error;
  657. }
  658. ga = (struct avp_galias*)pkg_malloc( sizeof(struct avp_galias) );
  659. if (ga==0) {
  660. LM_ERR("no more pkg memory\n");
  661. goto error;
  662. }
  663. ga->alias.s = (char*)pkg_malloc( alias->len+1 );
  664. if (ga->alias.s==0) {
  665. LM_ERR("no more pkg memory\n");
  666. goto error1;
  667. }
  668. memcpy( ga->alias.s, alias->s, alias->len);
  669. ga->alias.len = alias->len;
  670. ga->avp.type = type&AVP_NAME_STR;
  671. if (type&AVP_NAME_STR) {
  672. ga->avp.name.s.s = (char*)pkg_malloc( avp_name.s.len+1 );
  673. if (ga->avp.name.s.s==0) {
  674. LM_ERR("no more pkg memory\n");
  675. goto error2;
  676. }
  677. ga->avp.name.s.len = avp_name.s.len;
  678. memcpy( ga->avp.name.s.s, avp_name.s.s, avp_name.s.len);
  679. ga->avp.name.s.s[avp_name.s.len] = 0;
  680. DBG("DEBUG:add_avp_galias: registering <%s> for avp name <%s>\n",
  681. ga->alias.s, ga->avp.name.s.s);
  682. } else {
  683. ga->avp.name.n = avp_name.n;
  684. DBG("DEBUG:add_avp_galias: registering <%s> for avp id <%d>\n",
  685. ga->alias.s, ga->avp.name.n);
  686. }
  687. ga->next = galiases;
  688. galiases = ga;
  689. return 0;
  690. error2:
  691. pkg_free(ga->alias.s);
  692. error1:
  693. pkg_free(ga);
  694. error:
  695. return -1;
  696. }
  697. int lookup_avp_galias(str *alias, int *type, int_str *avp_name)
  698. {
  699. struct avp_galias *ga;
  700. for( ga=galiases ; ga ; ga=ga->next )
  701. if (alias->len==ga->alias.len &&
  702. (strncasecmp( alias->s, ga->alias.s, alias->len)==0) ) {
  703. *type = ga->avp.type;
  704. *avp_name = ga->avp.name;
  705. return 0;
  706. }
  707. return -1;
  708. }
  709. /* parsing functions */
  710. #define ERR_IF_CONTAINS(name,chr) \
  711. if (memchr(name->s,chr,name->len)) { \
  712. LM_ERR("Unexpected control character '%c' in AVP name\n", chr); \
  713. goto error; \
  714. }
  715. int parse_avp_name( str *name, int *type, int_str *avp_name, int *index)
  716. {
  717. int ret;
  718. avp_ident_t attr;
  719. ret=parse_avp_ident(name, &attr);
  720. if (!ret) {
  721. if (type) *type = attr.flags;
  722. if (avp_name) *avp_name = attr.name;
  723. if (index) *index = attr.index;
  724. }
  725. return ret;
  726. }
  727. /** parse an avp indentifier.
  728. *
  729. * Parses the following avp indentifier forms:
  730. * - "i:<number>" - old form, deprecated (e.g. i:42)
  731. * - "s:<string>" - old form, deprecated (e.g. s:foo)
  732. * - "<track>.<name>" (e.g.: f.bar)
  733. * - "<track>.<name>[<index>]" (e.g.: f.bar[1])
  734. * - "<track><class>.<name>" (e.g: tu.bar)
  735. * - "<track><class>.<name>[<index>]" (e.g: fd.bar[2])
  736. * - "<string>" (e.g.: foo)
  737. * Where:
  738. * \<string\> = ascii string
  739. * \<id\> = ascii string w/o '[', ']', '.' and '/'
  740. * \<name\> = \<id\> | '/' regex '/'
  741. * (Note: regex use is deprecated)
  742. * \<track\> = 'f' | 't'
  743. * (from or to)
  744. * \<class\> = 'r' | 'u' | 'd' | 'g'
  745. * (uri, user, domain or global)
  746. * \<index\> = \<number\> | '-' \<number\> | ''
  747. * (the avp index, if missing it means AVP_INDEX_ALL, but
  748. * it's use is deprecated)
  749. * More examples:
  750. * "fr.bar[1]" - from track, uri class, avp "bar", the value 1.
  751. * "tu./^foo/" - to track, user class, all avps for which the name
  752. * starts with foo (note RE in avp names are deprecated).
  753. * "t.did" - to track, "did" avp
  754. *
  755. * @param name - avp identifier
  756. * @param *attr - the result will be stored here
  757. * @return 0 on success, -1 on error
  758. */
  759. int parse_avp_ident( str *name, avp_ident_t* attr)
  760. {
  761. unsigned int id;
  762. char c;
  763. char *p;
  764. str s;
  765. if (name==0 || name->s==0 || name->len==0) {
  766. LM_ERR("NULL name or name->s or name->len\n");
  767. goto error;
  768. }
  769. attr->index = 0;
  770. DBG("Parsing '%.*s'\n", name->len, name->s);
  771. if (name->len>=2 && name->s[1]==':') { /* old fashion i: or s: */
  772. /* WARN("i: and s: avp name syntax is deprecated!\n"); */
  773. c = name->s[0];
  774. name->s += 2;
  775. name->len -= 2;
  776. if (name->len==0)
  777. goto error;
  778. switch (c) {
  779. case 's': case 'S':
  780. attr->flags = AVP_NAME_STR;
  781. attr->name.s = *name;
  782. break;
  783. case 'i': case 'I':
  784. attr->flags = 0;
  785. if (str2int( name, &id)!=0) {
  786. LM_ERR("invalid ID <%.*s> - not a number\n",
  787. name->len, name->s);
  788. goto error;
  789. }
  790. attr->name.n = (int)id;
  791. break;
  792. default:
  793. LM_ERR("unsupported type [%c]\n", c);
  794. goto error;
  795. }
  796. } else if ((p=memchr(name->s, '.', name->len))) {
  797. if (p-name->s==1) {
  798. id=name->s[0];
  799. name->s +=2;
  800. name->len -=2;
  801. } else if (p-name->s==2) {
  802. id=name->s[0]<<8 | name->s[1];
  803. name->s +=3;
  804. name->len -=3;
  805. } else {
  806. LM_ERR("AVP unknown class prefix '%.*s'\n", name->len, name->s);
  807. goto error;
  808. }
  809. if (name->len==0) {
  810. LM_ERR("AVP name not specified after the prefix separator\n");
  811. goto error;
  812. }
  813. switch (id) {
  814. case 'f':
  815. attr->flags = AVP_TRACK_FROM;
  816. break;
  817. case 't':
  818. attr->flags = AVP_TRACK_TO;
  819. break;
  820. case 0x6672: /* 'fr' */
  821. attr->flags = AVP_TRACK_FROM | AVP_CLASS_URI;
  822. break;
  823. case 0x7472: /* 'tr' */
  824. attr->flags = AVP_TRACK_TO | AVP_CLASS_URI;
  825. break;
  826. case 0x6675: /* 'fu' */
  827. attr->flags = AVP_TRACK_FROM | AVP_CLASS_USER;
  828. break;
  829. case 0x7475: /* 'tu' */
  830. attr->flags = AVP_TRACK_TO | AVP_CLASS_USER;
  831. break;
  832. case 0x6664: /* 'fd' */
  833. attr->flags = AVP_TRACK_FROM | AVP_CLASS_DOMAIN;
  834. break;
  835. case 0x7464: /* 'td' */
  836. attr->flags = AVP_TRACK_TO | AVP_CLASS_DOMAIN;
  837. break;
  838. case 'g':
  839. attr->flags = AVP_TRACK_ALL | AVP_CLASS_GLOBAL;
  840. break;
  841. default:
  842. if (id < 1<<8)
  843. LM_ERR("AVP unknown class prefix '%c'\n", id);
  844. else
  845. LM_ERR("AVP unknown class prefix '%c%c'\n", id>>8,id);
  846. goto error;
  847. }
  848. if (name->s[name->len-1]==']') {
  849. p=memchr(name->s, '[', name->len);
  850. if (!p) {
  851. LM_ERR("missing '[' for AVP index\n");
  852. goto error;
  853. }
  854. s.s=p+1;
  855. s.len=name->len-(p-name->s)-2; /* [ and ] */
  856. if (s.len == 0) {
  857. attr->flags |= AVP_INDEX_ALL;
  858. } else {
  859. if (s.s[0]=='-') {
  860. attr->flags |= AVP_INDEX_BACKWARD;
  861. s.s++;s.len--;
  862. } else {
  863. attr->flags |= AVP_INDEX_FORWARD;
  864. }
  865. if ((str2int(&s, &id) != 0)||(id==0)) {
  866. LM_ERR("Invalid AVP index '%.*s'\n", s.len, s.s);
  867. goto error;
  868. }
  869. attr->index = id;
  870. }
  871. name->len=p-name->s;
  872. }
  873. ERR_IF_CONTAINS(name,'.');
  874. ERR_IF_CONTAINS(name,'[');
  875. ERR_IF_CONTAINS(name,']');
  876. if ((name->len > 2) && (name->s[0]=='/') && (name->s[name->len-1]=='/')) {
  877. attr->name.re=pkg_malloc(sizeof(regex_t));
  878. if (!attr->name.re) {
  879. BUG("No free memory to allocate AVP_NAME_RE regex\n");
  880. goto error;
  881. }
  882. name->s[name->len-1]=0;
  883. if (regcomp(attr->name.re, name->s+1, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
  884. pkg_free(attr->name.re);
  885. attr->name.re=0;
  886. name->s[name->len-1] = '/';
  887. goto error;
  888. }
  889. name->s[name->len-1] = '/';
  890. attr->flags |= AVP_NAME_RE;
  891. } else {
  892. ERR_IF_CONTAINS(name,'/');
  893. attr->flags |= AVP_NAME_STR;
  894. attr->name.s = *name;
  895. }
  896. } else {
  897. /*default is string name*/
  898. attr->flags = AVP_NAME_STR;
  899. attr->name.s = *name;
  900. }
  901. return 0;
  902. error:
  903. return -1;
  904. }
  905. void free_avp_ident(avp_ident_t* attr)
  906. {
  907. if (attr->flags & AVP_NAME_RE) {
  908. if (! attr->name.re) {
  909. BUG("attr ident @%p has the regexp flag set, but no regexp.\n",
  910. attr);
  911. #ifdef EXTRA_DEBUG
  912. abort();
  913. #endif
  914. } else {
  915. regfree(attr->name.re);
  916. pkg_free(attr->name.re);
  917. }
  918. }
  919. }
  920. int km_parse_avp_spec( str *name, int *type, int_str *avp_name)
  921. {
  922. char *p;
  923. int index = 0;
  924. if (name==0 || name->s==0 || name->len==0)
  925. return -1;
  926. p = (char*)memchr((void*)name->s, ':', name->len);
  927. if (p==NULL) {
  928. /* might be kamailio avp alias or ser avp name style */
  929. if(lookup_avp_galias( name, type, avp_name)==0)
  930. return 0; /* found */
  931. }
  932. return parse_avp_name( name, type, avp_name, &index);
  933. }
  934. int parse_avp_spec( str *name, int *type, int_str *avp_name, int *index)
  935. {
  936. str alias;
  937. if (name==0 || name->s==0 || name->len==0)
  938. return -1;
  939. if (name->s[0]==GALIAS_CHAR_MARKER) {
  940. /* it's an avp alias */
  941. if (name->len==1) {
  942. LM_ERR("empty alias\n");
  943. return -1;
  944. }
  945. alias.s = name->s+1;
  946. alias.len = name->len-1;
  947. return lookup_avp_galias( &alias, type, avp_name);
  948. } else {
  949. return parse_avp_name( name, type, avp_name, index);
  950. }
  951. }
  952. void free_avp_name(avp_flags_t *type, int_str *avp_name)
  953. {
  954. if ((*type & AVP_NAME_RE) && (avp_name->re)){
  955. regfree(avp_name->re);
  956. pkg_free(avp_name->re);
  957. avp_name->re=0;
  958. }
  959. }
  960. int add_avp_galias_str(char *alias_definition)
  961. {
  962. int_str avp_name;
  963. char *s;
  964. str name;
  965. str alias;
  966. int type;
  967. int index;
  968. s = alias_definition;
  969. while(*s && isspace((int)*s))
  970. s++;
  971. while (*s) {
  972. /* parse alias name */
  973. alias.s = s;
  974. while(*s && *s!=';' && !isspace((int)*s) && *s!='=')
  975. s++;
  976. if (alias.s==s || *s==0 || *s==';')
  977. goto parse_error;
  978. alias.len = s-alias.s;
  979. while(*s && isspace((int)*s))
  980. s++;
  981. /* equal sign */
  982. if (*s!='=')
  983. goto parse_error;
  984. s++;
  985. while(*s && isspace((int)*s))
  986. s++;
  987. /* avp name */
  988. name.s = s;
  989. while(*s && *s!=';' && !isspace((int)*s))
  990. s++;
  991. if (name.s==s)
  992. goto parse_error;
  993. name.len = s-name.s;
  994. while(*s && isspace((int)*s))
  995. s++;
  996. /* check end */
  997. if (*s!=0 && *s!=';')
  998. goto parse_error;
  999. if (*s==';') {
  1000. for( s++ ; *s && isspace((int)*s) ; s++ );
  1001. if (*s==0)
  1002. goto parse_error;
  1003. }
  1004. if (parse_avp_name( &name, &type, &avp_name, &index)!=0) {
  1005. LM_ERR("<%.*s> not a valid AVP name\n", name.len, name.s);
  1006. goto error;
  1007. }
  1008. if (add_avp_galias( &alias, type, avp_name)!=0) {
  1009. LM_ERR("add global alias failed\n");
  1010. goto error;
  1011. }
  1012. } /*end while*/
  1013. return 0;
  1014. parse_error:
  1015. LM_ERR("parse error in <%s> around pos %ld\n",
  1016. alias_definition, (long)(s-alias_definition));
  1017. error:
  1018. return -1;
  1019. }
  1020. int destroy_avps(avp_flags_t flags, avp_name_t name, int all)
  1021. {
  1022. struct search_state st;
  1023. avp_t* avp;
  1024. int n;
  1025. n = 0;
  1026. avp = search_first_avp(flags, name, 0, &st);
  1027. while (avp) {
  1028. destroy_avp(avp);
  1029. n++;
  1030. if (!all) break;
  1031. avp = search_next_avp(&st, 0);
  1032. }
  1033. return n;
  1034. }
  1035. void delete_avp(avp_flags_t flags, avp_name_t name)
  1036. {
  1037. struct search_state st;
  1038. avp_t* avp;
  1039. avp = search_first_avp(flags, name, 0, &st);
  1040. while(avp) {
  1041. destroy_avp(avp);
  1042. avp = search_next_avp(&st, 0);
  1043. }
  1044. }
  1045. /* AVP flags functions */
  1046. /* name2id conversion is intended to use during fixup (cfg parsing and modinit) only therefore no hash is used */
  1047. avp_flags_t register_avpflag(char* name) {
  1048. avp_flags_t ret;
  1049. ret = get_avpflag_no(name);
  1050. if (ret == 0) {
  1051. if (registered_avpflags_no >= MAX_AVPFLAG) {
  1052. LM_ERR("cannot register new avp flag ('%s'), max.number of flags (%d) reached\n",
  1053. name, MAX_AVPFLAG);
  1054. return -1;
  1055. }
  1056. ret = 1<<(AVP_CUSTOM_FLAGS+registered_avpflags_no);
  1057. registered_avpflags[registered_avpflags_no++] = name;
  1058. }
  1059. return ret;
  1060. }
  1061. avp_flags_t get_avpflag_no(char* name) {
  1062. int i;
  1063. for (i=0; i<registered_avpflags_no; i++) {
  1064. if (strcasecmp(name, registered_avpflags[i])==0)
  1065. return 1<<(AVP_CUSTOM_FLAGS+i);
  1066. }
  1067. return 0;
  1068. }