route_struct.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "route_struct.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "dprint.h"
  34. #include "ip_addr.h"
  35. #ifdef DEBUG_DMALLOC
  36. #include <dmalloc.h>
  37. #endif
  38. struct expr* mk_exp(int op, struct expr* left, struct expr* right)
  39. {
  40. struct expr * e;
  41. e=(struct expr*)malloc(sizeof (struct expr));
  42. if (e==0) goto error;
  43. e->type=EXP_T;
  44. e->op=op;
  45. e->l.expr=left;
  46. e->r.expr=right;
  47. return e;
  48. error:
  49. LOG(L_CRIT, "ERROR: mk_exp: memory allocation failure\n");
  50. return 0;
  51. }
  52. struct expr* mk_elem(int op, int subtype, int operand, void* param)
  53. {
  54. struct expr * e;
  55. e=(struct expr*)malloc(sizeof (struct expr));
  56. if (e==0) goto error;
  57. e->type=ELEM_T;
  58. e->op=op;
  59. e->subtype=subtype;
  60. e->l.operand=operand;
  61. e->r.param=param;
  62. return e;
  63. error:
  64. LOG(L_CRIT, "ERROR: mk_elem: memory allocation failure\n");
  65. return 0;
  66. }
  67. struct action* mk_action(int type, int p1_type, int p2_type,
  68. void* p1, void* p2)
  69. {
  70. struct action* a;
  71. a=(struct action*)malloc(sizeof(struct action));
  72. if (a==0) goto error;
  73. memset(a,0,sizeof(struct action));
  74. a->type=type;
  75. a->p1_type=p1_type;
  76. a->p2_type=p2_type;
  77. a->p1.string=(char*) p1;
  78. a->p2.string=(char*) p2;
  79. a->next=0;
  80. return a;
  81. error:
  82. LOG(L_CRIT, "ERROR: mk_action: memory allocation failure\n");
  83. return 0;
  84. }
  85. struct action* mk_action3(int type, int p1_type, int p2_type, int p3_type,
  86. void* p1, void* p2, void* p3)
  87. {
  88. struct action* a;
  89. a=mk_action(type, p1_type, p2_type, p1, p2);
  90. if (a){
  91. a->p3_type=p3_type;
  92. a->p3.data=p3;
  93. }
  94. return a;
  95. }
  96. struct action* append_action(struct action* a, struct action* b)
  97. {
  98. struct action *t;
  99. if (b==0) return a;
  100. if (a==0) return b;
  101. for(t=a;t->next;t=t->next);
  102. t->next=b;
  103. return a;
  104. }
  105. void print_expr(struct expr* exp)
  106. {
  107. if (exp==0){
  108. LOG(L_CRIT, "ERROR: print_expr: null expression!\n");
  109. return;
  110. }
  111. if (exp->type==ELEM_T){
  112. switch(exp->l.operand){
  113. case METHOD_O:
  114. DBG("method");
  115. break;
  116. case URI_O:
  117. DBG("uri");
  118. break;
  119. case SRCIP_O:
  120. DBG("srcip");
  121. break;
  122. case DSTIP_O:
  123. DBG("dstip");
  124. break;
  125. case NUMBER_O:
  126. break;
  127. case ACTION_O:
  128. print_action((struct action*) exp->r.param);
  129. break;
  130. default:
  131. DBG("UNKNOWN");
  132. }
  133. switch(exp->op){
  134. case EQUAL_OP:
  135. DBG("==");
  136. break;
  137. case MATCH_OP:
  138. DBG("=~");
  139. break;
  140. case NO_OP:
  141. break;
  142. default:
  143. DBG("<UNKNOWN>");
  144. }
  145. switch(exp->subtype){
  146. case NOSUBTYPE:
  147. DBG("N/A");
  148. break;
  149. case STRING_ST:
  150. DBG("\"%s\"", (char*)exp->r.param);
  151. break;
  152. case NET_ST:
  153. print_net((struct net*)exp->r.param);
  154. break;
  155. case IP_ST:
  156. print_ip((struct ip_addr*)exp->r.param);
  157. break;
  158. case ACTIONS_ST:
  159. print_action((struct action*)exp->r.param);
  160. break;
  161. case NUMBER_ST:
  162. DBG("%d",exp->r.intval);
  163. break;
  164. case MYSELF_ST:
  165. DBG("_myself_");
  166. break;
  167. default:
  168. DBG("type<%d>", exp->subtype);
  169. }
  170. }else if (exp->type==EXP_T){
  171. switch(exp->op){
  172. case AND_OP:
  173. DBG("AND( ");
  174. print_expr(exp->l.expr);
  175. DBG(", ");
  176. print_expr(exp->r.expr);
  177. DBG(" )");
  178. break;
  179. case OR_OP:
  180. DBG("OR( ");
  181. print_expr(exp->l.expr);
  182. DBG(", ");
  183. print_expr(exp->r.expr);
  184. DBG(" )");
  185. break;
  186. case NOT_OP:
  187. DBG("NOT( ");
  188. print_expr(exp->l.expr);
  189. DBG(" )");
  190. break;
  191. default:
  192. DBG("UNKNOWN_EXP ");
  193. }
  194. }else{
  195. DBG("ERROR:print_expr: unknown type\n");
  196. }
  197. }
  198. void print_action(struct action* a)
  199. {
  200. struct action* t;
  201. for(t=a; t!=0;t=t->next){
  202. switch(t->type){
  203. case FORWARD_T:
  204. DBG("forward(");
  205. break;
  206. case FORWARD_TCP_T:
  207. DBG("forward_tcp(");
  208. break;
  209. case FORWARD_UDP_T:
  210. DBG("forward_udp(");
  211. break;
  212. case SEND_T:
  213. DBG("send(");
  214. break;
  215. case SEND_TCP_T:
  216. DBG("send_tcp(");
  217. break;
  218. case DROP_T:
  219. DBG("drop(");
  220. break;
  221. case LOG_T:
  222. DBG("log(");
  223. break;
  224. case ERROR_T:
  225. DBG("error(");
  226. break;
  227. case ROUTE_T:
  228. DBG("route(");
  229. break;
  230. case EXEC_T:
  231. DBG("exec(");
  232. break;
  233. case REVERT_URI_T:
  234. DBG("revert_uri(");
  235. break;
  236. case STRIP_T:
  237. DBG("strip(");
  238. break;
  239. case APPEND_BRANCH_T:
  240. DBG("append_branch(");
  241. break;
  242. case PREFIX_T:
  243. DBG("prefix(");
  244. break;
  245. case LEN_GT_T:
  246. DBG("len_gt(");
  247. break;
  248. case SETFLAG_T:
  249. DBG("setflag(");
  250. break;
  251. case RESETFLAG_T:
  252. DBG("resetflag(");
  253. break;
  254. case ISFLAGSET_T:
  255. DBG("isflagset(");
  256. break;
  257. case SET_HOST_T:
  258. DBG("sethost(");
  259. break;
  260. case SET_HOSTPORT_T:
  261. DBG("sethostport(");
  262. break;
  263. case SET_USER_T:
  264. DBG("setuser(");
  265. break;
  266. case SET_USERPASS_T:
  267. DBG("setuserpass(");
  268. break;
  269. case SET_PORT_T:
  270. DBG("setport(");
  271. break;
  272. case SET_URI_T:
  273. DBG("seturi(");
  274. break;
  275. case IF_T:
  276. DBG("if (");
  277. break;
  278. case MODULE_T:
  279. DBG(" external_module_call(");
  280. break;
  281. default:
  282. DBG("UNKNOWN(");
  283. }
  284. switch(t->p1_type){
  285. case STRING_ST:
  286. DBG("\"%s\"", t->p1.string);
  287. break;
  288. case NUMBER_ST:
  289. DBG("%d",t->p1.number);
  290. break;
  291. case IP_ST:
  292. print_ip((struct ip_addr*)t->p1.data);
  293. break;
  294. case EXPR_ST:
  295. print_expr((struct expr*)t->p1.data);
  296. break;
  297. case ACTIONS_ST:
  298. print_action((struct action*)t->p1.data);
  299. break;
  300. case CMDF_ST:
  301. DBG("f_ptr<%p>",t->p1.data);
  302. break;
  303. default:
  304. DBG("type<%d>", t->p1_type);
  305. }
  306. if (t->type==IF_T) DBG(") {");
  307. switch(t->p2_type){
  308. case NOSUBTYPE:
  309. break;
  310. case STRING_ST:
  311. DBG(", \"%s\"", t->p2.string);
  312. break;
  313. case NUMBER_ST:
  314. DBG(", %d",t->p2.number);
  315. break;
  316. case EXPR_ST:
  317. print_expr((struct expr*)t->p2.data);
  318. break;
  319. case ACTIONS_ST:
  320. print_action((struct action*)t->p2.data);
  321. break;
  322. default:
  323. DBG(", type<%d>", t->p2_type);
  324. }
  325. if (t->type==IF_T) DBG("} else {");
  326. switch(t->p3_type){
  327. case NOSUBTYPE:
  328. break;
  329. case STRING_ST:
  330. DBG(", \"%s\"", t->p3.string);
  331. break;
  332. case NUMBER_ST:
  333. DBG(", %d",t->p3.number);
  334. break;
  335. case EXPR_ST:
  336. print_expr((struct expr*)t->p3.data);
  337. break;
  338. case ACTIONS_ST:
  339. print_action((struct action*)t->p3.data);
  340. break;
  341. default:
  342. DBG(", type<%d>", t->p3_type);
  343. }
  344. if (t->type==IF_T) DBG("}; ");
  345. else DBG("); ");
  346. }
  347. }