route_struct.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * $Id$
  3. *
  4. * route structures helping functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /* History:
  30. * --------
  31. * 2003-01-29 src_port introduced (jiri)
  32. * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
  33. * 2003-04-12 FORCE_RPORT_T added (andrei)
  34. * 2003-10-02 added SET_ADV_ADDRESS & SET_ADV_PORT (andrei)
  35. * 2004-02-24 added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
  36. * 2005-12-19 select framework added SELECT_O and SELECT_ST (mma)
  37. */
  38. #include "route_struct.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <stdarg.h>
  43. #include "dprint.h"
  44. #include "ip_addr.h"
  45. #include "mem/mem.h"
  46. #include "usr_avp.h"
  47. #include "ut.h" /* ZSW() */
  48. struct expr* mk_exp(int op, struct expr* left, struct expr* right)
  49. {
  50. struct expr * e;
  51. e=(struct expr*)pkg_malloc(sizeof (struct expr));
  52. if (e==0) goto error;
  53. e->type=EXP_T;
  54. e->op=op;
  55. e->l.expr=left;
  56. e->r.expr=right;
  57. return e;
  58. error:
  59. LOG(L_CRIT, "ERROR: mk_exp: memory allocation failure\n");
  60. return 0;
  61. }
  62. struct expr* mk_elem(int op, int ltype, void* lparam, int rtype, void* rparam)
  63. {
  64. struct expr * e;
  65. e=(struct expr*)pkg_malloc(sizeof (struct expr));
  66. if (e==0) goto error;
  67. e->type=ELEM_T;
  68. e->op=op;
  69. e->l_type=ltype;
  70. e->l.param=lparam;
  71. e->r_type = rtype;
  72. e->r.param=rparam;
  73. return e;
  74. error:
  75. LOG(L_CRIT, "ERROR: mk_elem: memory allocation failure\n");
  76. return 0;
  77. }
  78. struct action* mk_action(int type, int count/* of couples {type,val} */, .../* int type1, void *val1 [, int type2, void *val2, ...] */) {
  79. va_list args;
  80. int i;
  81. struct action* a;
  82. a = (struct action*)pkg_malloc(sizeof(struct action));
  83. if (a==0) goto error;
  84. memset(a, 0, sizeof(struct action));
  85. a->type=type;
  86. a->count = (count > MAX_ACTIONS)?MAX_ACTIONS:count;
  87. va_start(args, count);
  88. for (i=0; i<a->count; i++) {
  89. a->val[i].type = va_arg(args, int);
  90. a->val[i].u.data = va_arg(args, void *);
  91. DBG("ACTION_#%d #%d/%d: %d(%x)/ %p\n", a->type, i, a->count, a->val[i].type, a->val[i].type, a->val[i].u.data);
  92. }
  93. va_end(args);
  94. a->next=0;
  95. return a;
  96. error:
  97. LOG(L_CRIT, "ERROR: mk_action: memory allocation failure\n");
  98. return 0;
  99. }
  100. struct action* append_action(struct action* a, struct action* b)
  101. {
  102. struct action *t;
  103. if (b==0) return a;
  104. if (a==0) return b;
  105. for(t=a; t->next; t=t->next);
  106. t->next=b;
  107. return a;
  108. }
  109. void print_expr(struct expr* exp)
  110. {
  111. if (exp==0){
  112. LOG(L_CRIT, "ERROR: print_expr: null expression!\n");
  113. return;
  114. }
  115. if (exp->type==ELEM_T){
  116. switch(exp->l_type){
  117. case METHOD_O:
  118. DBG("method");
  119. break;
  120. case URI_O:
  121. DBG("uri");
  122. break;
  123. case FROM_URI_O:
  124. DBG("from_uri");
  125. break;
  126. case TO_URI_O:
  127. DBG("to_uri");
  128. break;
  129. case SRCIP_O:
  130. DBG("srcip");
  131. break;
  132. case SRCPORT_O:
  133. DBG("srcport");
  134. break;
  135. case DSTIP_O:
  136. DBG("dstip");
  137. break;
  138. case DSTPORT_O:
  139. DBG("dstport");
  140. break;
  141. case NUMBER_O:
  142. break;
  143. case ACTION_O:
  144. break;
  145. case AVP_ST:
  146. DBG("attr");
  147. break;
  148. case SELECT_ST:
  149. DBG("select");
  150. break;
  151. default:
  152. DBG("UNKNOWN");
  153. }
  154. switch(exp->op){
  155. case EQUAL_OP:
  156. DBG("==");
  157. break;
  158. case MATCH_OP:
  159. DBG("=~");
  160. break;
  161. case NO_OP:
  162. break;
  163. case GT_OP:
  164. DBG(">");
  165. break;
  166. case GTE_OP:
  167. DBG(">=");
  168. break;
  169. case LT_OP:
  170. DBG("<");
  171. break;
  172. case LTE_OP:
  173. DBG("<=");
  174. break;
  175. case DIFF_OP:
  176. DBG("!=");
  177. break;
  178. default:
  179. DBG("<UNKNOWN>");
  180. }
  181. switch(exp->r_type){
  182. case NOSUBTYPE:
  183. DBG("N/A");
  184. break;
  185. case STRING_ST:
  186. DBG("\"%s\"", ZSW((char*)exp->r.param));
  187. break;
  188. case NET_ST:
  189. print_net((struct net*)exp->r.param);
  190. break;
  191. case IP_ST:
  192. print_ip("", (struct ip_addr*)exp->r.param, "");
  193. break;
  194. case ACTIONS_ST:
  195. print_actions((struct action*)exp->r.param);
  196. break;
  197. case NUMBER_ST:
  198. DBG("%ld",exp->r.numval);
  199. break;
  200. case MYSELF_ST:
  201. DBG("_myself_");
  202. break;
  203. case AVP_ST:
  204. DBG("attr");
  205. break;
  206. case SELECT_ST:
  207. DBG("select");
  208. break;
  209. default:
  210. DBG("type<%d>", exp->r_type);
  211. }
  212. }else if (exp->type==EXP_T){
  213. switch(exp->op){
  214. case LOGAND_OP:
  215. DBG("AND( ");
  216. print_expr(exp->l.expr);
  217. DBG(", ");
  218. print_expr(exp->r.expr);
  219. DBG(" )");
  220. break;
  221. case LOGOR_OP:
  222. DBG("OR( ");
  223. print_expr(exp->l.expr);
  224. DBG(", ");
  225. print_expr(exp->r.expr);
  226. DBG(" )");
  227. break;
  228. case NOT_OP:
  229. DBG("NOT( ");
  230. print_expr(exp->l.expr);
  231. DBG(" )");
  232. break;
  233. default:
  234. DBG("UNKNOWN_EXP ");
  235. }
  236. }else{
  237. DBG("ERROR:print_expr: unknown type\n");
  238. }
  239. }
  240. void print_action(struct action* t)
  241. {
  242. switch(t->type){
  243. case FORWARD_T:
  244. DBG("forward(");
  245. break;
  246. case FORWARD_TCP_T:
  247. DBG("forward_tcp(");
  248. break;
  249. case FORWARD_UDP_T:
  250. DBG("forward_udp(");
  251. break;
  252. case SEND_T:
  253. DBG("send(");
  254. break;
  255. case SEND_TCP_T:
  256. DBG("send_tcp(");
  257. break;
  258. case DROP_T:
  259. DBG("drop(");
  260. break;
  261. case LOG_T:
  262. DBG("log(");
  263. break;
  264. case ERROR_T:
  265. DBG("error(");
  266. break;
  267. case ROUTE_T:
  268. DBG("route(");
  269. break;
  270. case EXEC_T:
  271. DBG("exec(");
  272. break;
  273. case REVERT_URI_T:
  274. DBG("revert_uri(");
  275. break;
  276. case STRIP_T:
  277. DBG("strip(");
  278. break;
  279. case APPEND_BRANCH_T:
  280. DBG("append_branch(");
  281. break;
  282. case PREFIX_T:
  283. DBG("prefix(");
  284. break;
  285. case LEN_GT_T:
  286. DBG("len_gt(");
  287. break;
  288. case SETFLAG_T:
  289. DBG("setflag(");
  290. break;
  291. case RESETFLAG_T:
  292. DBG("resetflag(");
  293. break;
  294. case ISFLAGSET_T:
  295. DBG("isflagset(");
  296. break;
  297. case AVPFLAG_OPER_T:
  298. DBG("avpflagoper(");
  299. break;
  300. case SET_HOST_T:
  301. DBG("sethost(");
  302. break;
  303. case SET_HOSTPORT_T:
  304. DBG("sethostport(");
  305. break;
  306. case SET_HOSTPORTTRANS_T:
  307. DBG("sethostporttrans(");
  308. break;
  309. case SET_USER_T:
  310. DBG("setuser(");
  311. break;
  312. case SET_USERPASS_T:
  313. DBG("setuserpass(");
  314. break;
  315. case SET_PORT_T:
  316. DBG("setport(");
  317. break;
  318. case SET_URI_T:
  319. DBG("seturi(");
  320. break;
  321. case IF_T:
  322. DBG("if (");
  323. break;
  324. case MODULE_T:
  325. DBG(" external_module_call(");
  326. break;
  327. case FORCE_RPORT_T:
  328. DBG("force_rport(");
  329. break;
  330. case SET_ADV_ADDR_T:
  331. DBG("set_advertised_address(");
  332. break;
  333. case SET_ADV_PORT_T:
  334. DBG("set_advertised_port(");
  335. break;
  336. case FORCE_TCP_ALIAS_T:
  337. DBG("force_tcp_alias(");
  338. break;
  339. case LOAD_AVP_T:
  340. DBG("load_avp(");
  341. break;
  342. case AVP_TO_URI_T:
  343. DBG("avp_to_attr");
  344. break;
  345. case FORCE_SEND_SOCKET_T:
  346. DBG("force_send_socket");
  347. break;
  348. case ASSIGN_T
  349. : DBG("assign(");
  350. break;
  351. case ADD_T:
  352. DBG("assign_add(");
  353. break;
  354. default:
  355. DBG("UNKNOWN(");
  356. }
  357. switch(t->val[0].type){
  358. case STRING_ST:
  359. DBG("\"%s\"", ZSW(t->val[0].u.string));
  360. break;
  361. case NUMBER_ST:
  362. DBG("%lu",t->val[0].u.number);
  363. break;
  364. case IP_ST:
  365. print_ip("", (struct ip_addr*)t->val[0].u.data, "");
  366. break;
  367. case EXPR_ST:
  368. print_expr((struct expr*)t->val[0].u.data);
  369. break;
  370. case ACTIONS_ST:
  371. print_actions((struct action*)t->val[0].u.data);
  372. break;
  373. case MODEXP_ST:
  374. DBG("f_ptr<%p>",t->val[0].u.data);
  375. break;
  376. case SOCKID_ST:
  377. DBG("%d:%s:%d",
  378. ((struct socket_id*)t->val[0].u.data)->proto,
  379. ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
  380. ((struct socket_id*)t->val[0].u.data)->port
  381. );
  382. break;
  383. case AVP_ST:
  384. DBG("avp(%u,%.*s)", t->val[0].u.attr->type, t->val[0].u.attr->name.s.len, ZSW(t->val[0].u.attr->name.s.s));
  385. break;
  386. case SELECT_ST:
  387. DBG("select");
  388. break;
  389. default:
  390. DBG("type<%d>", t->val[0].type);
  391. }
  392. if (t->type==IF_T) DBG(") {");
  393. switch(t->val[1].type){
  394. case NOSUBTYPE:
  395. break;
  396. case STRING_ST:
  397. DBG(", \"%s\"", ZSW(t->val[1].u.string));
  398. break;
  399. case NUMBER_ST:
  400. DBG(", %lu",t->val[1].u.number);
  401. break;
  402. case EXPR_ST:
  403. print_expr((struct expr*)t->val[1].u.data);
  404. break;
  405. case ACTION_ST:
  406. case ACTIONS_ST:
  407. print_actions((struct action*)t->val[1].u.data);
  408. break;
  409. case SOCKID_ST:
  410. DBG("%d:%s:%d",
  411. ((struct socket_id*)t->val[0].u.data)->proto,
  412. ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
  413. ((struct socket_id*)t->val[0].u.data)->port
  414. );
  415. break;
  416. case AVP_ST:
  417. DBG(", avp(%u,%.*s)", t->val[1].u.attr->type, t->val[1].u.attr->name.s.len, ZSW(t->val[1].u.attr->name.s.s));
  418. break;
  419. case SELECT_ST:
  420. DBG("select");
  421. break;
  422. default:
  423. DBG(", type<%d>", t->val[1].type);
  424. }
  425. if (t->type==IF_T) DBG("} else {");
  426. switch(t->val[2].type){
  427. case NOSUBTYPE:
  428. break;
  429. case STRING_ST:
  430. DBG(", \"%s\"", ZSW(t->val[2].u.string));
  431. break;
  432. case NUMBER_ST:
  433. DBG(", %lu",t->val[2].u.number);
  434. break;
  435. case EXPR_ST:
  436. print_expr((struct expr*)t->val[2].u.data);
  437. break;
  438. case ACTIONS_ST:
  439. print_actions((struct action*)t->val[2].u.data);
  440. break;
  441. case SOCKID_ST:
  442. DBG("%d:%s:%d",
  443. ((struct socket_id*)t->val[0].u.data)->proto,
  444. ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
  445. ((struct socket_id*)t->val[0].u.data)->port
  446. );
  447. break;
  448. default:
  449. DBG(", type<%d>", t->val[2].type);
  450. }
  451. if (t->type==IF_T) DBG("}; ");
  452. else DBG("); ");
  453. }
  454. void print_actions(struct action* a)
  455. {
  456. while(a) {
  457. print_action(a);
  458. a = a->next;
  459. }
  460. }