00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "route_struct.h"
00041
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045
00046 #include "sr_module.h"
00047 #include "dprint.h"
00048 #include "ip_addr.h"
00049 #include "mem/mem.h"
00050 #include "ut.h"
00051
00052
00053 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
00054 {
00055 struct expr * e;
00056 e=(struct expr*)pkg_malloc(sizeof (struct expr));
00057 if (e==0) goto error;
00058 e->type=EXP_T;
00059 e->op=op;
00060 e->left.v.expr=left;
00061 e->right.v.expr=right;
00062 return e;
00063 error:
00064 LM_CRIT("pkg memory allocation failure\n");
00065 return 0;
00066 }
00067
00068
00069 struct expr* mk_elem(int op, int leftt, void *leftd, int rightt, void *rightd)
00070 {
00071 struct expr * e;
00072 e=(struct expr*)pkg_malloc(sizeof (struct expr));
00073 if (e==0) goto error;
00074 memset(e, 0, sizeof(struct expr));
00075 e->type=ELEM_T;
00076 e->op=op;
00077 e->left.type = leftt;
00078 e->left.v.data = leftd;
00079 if((e->left.type==STRING_ST || e->left.type==STRINGV_O)
00080 && e->left.v.s.s!=NULL)
00081 e->left.v.s.len = strlen(e->left.v.s.s);
00082 e->right.type = rightt;
00083 e->right.v.data = rightd;
00084 if((e->right.type==STRING_ST || e->right.type==STRINGV_O)
00085 && e->right.v.s.s!=0)
00086 e->right.v.s.len = strlen(e->right.v.s.s);
00087 return e;
00088 error:
00089 LM_CRIT("pkg memory allocation failure\n");
00090 return 0;
00091 }
00092
00093
00094
00095 struct action* mk_action(int type, int n, action_elem_t *elem, int line)
00096 {
00097 int i;
00098 struct action* a;
00099
00100 if(n>MAX_ACTION_ELEMS)
00101 {
00102 LM_ERR("too many action elements at line %d for %d", line, type);
00103 return 0;
00104 }
00105
00106 a=(struct action*)pkg_malloc(sizeof(struct action));
00107 if (a==0) goto error;
00108 memset(a,0,sizeof(struct action));
00109 a->type=type;
00110
00111 for(i=0; i<n; i++)
00112 {
00113 a->elem[i].type = elem[i].type;
00114 a->elem[i].u.data = elem[i].u.data;
00115 if(a->elem[i].type==STRING_ST && a->elem[i].u.s.s!=NULL)
00116 a->elem[i].u.s.len = strlen(a->elem[i].u.s.s);
00117 }
00118
00119 a->line = line;
00120 a->next=0;
00121 return a;
00122
00123 error:
00124 LM_CRIT("pkg memory allocation failure\n");
00125 return 0;
00126
00127 }
00128
00129
00130 struct action* append_action(struct action* a, struct action* b)
00131 {
00132 struct action *t;
00133 if (b==0) return a;
00134 if (a==0) return b;
00135
00136 for(t=a;t->next;t=t->next);
00137 t->next=b;
00138 return a;
00139 }
00140
00141
00142
00143 void print_expr(struct expr* exp)
00144 {
00145 if (exp==0){
00146 LM_CRIT("null expression!\n");
00147 return;
00148 }
00149 if (exp->type==ELEM_T){
00150 switch(exp->left.type){
00151 case METHOD_O:
00152 LM_DBG("method");
00153 break;
00154 case URI_O:
00155 LM_DBG("uri");
00156 break;
00157 case FROM_URI_O:
00158 LM_DBG("from_uri");
00159 break;
00160 case TO_URI_O:
00161 LM_DBG("to_uri");
00162 break;
00163 case SRCIP_O:
00164 LM_DBG("srcip");
00165 break;
00166 case SRCPORT_O:
00167 LM_DBG("srcport");
00168 break;
00169 case DSTIP_O:
00170 LM_DBG("dstip");
00171 break;
00172 case DSTPORT_O:
00173 LM_DBG("dstport");
00174 break;
00175 case SCRIPTVAR_O:
00176 LM_DBG("scriptvar[%d]",
00177 (exp->left.v.spec)?exp->left.v.spec->type:0);
00178 break;
00179 case NUMBER_O:
00180 case NUMBERV_O:
00181 LM_DBG("%d",exp->left.v.n);
00182 break;
00183 case STRINGV_O:
00184 LM_DBG("\"%s\"", ZSW((char*)exp->left.v.data));
00185 break;
00186 case ACTION_O:
00187 break;
00188 case EXPR_O:
00189 print_expr((struct expr*)exp->left.v.data);
00190 break;
00191 default:
00192 LM_DBG("UNKNOWN[%d]", exp->left.type);
00193 }
00194 switch(exp->op){
00195 case EQUAL_OP:
00196 LM_DBG("==");
00197 break;
00198 case MATCHD_OP:
00199 case MATCH_OP:
00200 LM_DBG("=~");
00201 break;
00202 case NOTMATCHD_OP:
00203 case NOTMATCH_OP:
00204 LM_DBG("!~");
00205 break;
00206 case GT_OP:
00207 LM_DBG(">");
00208 break;
00209 case GTE_OP:
00210 LM_DBG(">=");
00211 break;
00212 case LT_OP:
00213 LM_DBG("<");
00214 break;
00215 case LTE_OP:
00216 LM_DBG("<=");
00217 break;
00218 case DIFF_OP:
00219 LM_DBG("!=");
00220 break;
00221 case PLUS_OP:
00222 LM_DBG("+");
00223 break;
00224 case MINUS_OP:
00225 LM_DBG("-");
00226 break;
00227 case DIV_OP:
00228 LM_DBG("/");
00229 break;
00230 case MULT_OP:
00231 LM_DBG("*");
00232 break;
00233 case MODULO_OP:
00234 LM_DBG(" mod ");
00235 break;
00236 case BAND_OP:
00237 LM_DBG("&");
00238 break;
00239 case BOR_OP:
00240 LM_DBG("|");
00241 break;
00242 case BXOR_OP:
00243 LM_DBG("^");
00244 break;
00245 case BLSHIFT_OP:
00246 LM_DBG("<<");
00247 break;
00248 case BRSHIFT_OP:
00249 LM_DBG(">>");
00250 break;
00251 case BNOT_OP:
00252 LM_DBG("~");
00253 break;
00254 case VALUE_OP:
00255 case NO_OP:
00256 break;
00257 default:
00258 LM_DBG("<UNKNOWN[%d]>", exp->op);
00259 }
00260 switch(exp->right.type){
00261 case NOSUBTYPE:
00262
00263 break;
00264 case STRING_ST:
00265 LM_DBG("\"%s\"", ZSW((char*)exp->right.v.data));
00266 break;
00267 case NET_ST:
00268 print_net((struct net*)exp->right.v.data);
00269 break;
00270 case IP_ST:
00271 print_ip("", (struct ip_addr*)exp->right.v.data, "");
00272 break;
00273 case ACTIONS_ST:
00274 print_actions((struct action*)exp->right.v.data);
00275 break;
00276 case NUMBER_ST:
00277 LM_DBG("%d",exp->right.v.n);
00278 break;
00279 case MYSELF_ST:
00280 LM_DBG("_myself_");
00281 break;
00282 case SCRIPTVAR_ST:
00283 LM_DBG("scriptvar[%d]", exp->right.v.spec->type);
00284 break;
00285 case NULLV_ST:
00286 LM_DBG("null");
00287 break;
00288 case EXPR_ST:
00289 print_expr((struct expr*)exp->right.v.data);
00290 break;
00291 default:
00292 LM_DBG("type<%d>", exp->right.type);
00293 }
00294 }else if (exp->type==EXP_T){
00295 switch(exp->op){
00296 case AND_OP:
00297 LM_DBG("AND( ");
00298 print_expr(exp->left.v.expr);
00299 LM_DBG(", ");
00300 print_expr(exp->right.v.expr);
00301 LM_DBG(" )");
00302 break;
00303 case OR_OP:
00304 LM_DBG("OR( ");
00305 print_expr(exp->left.v.expr);
00306 LM_DBG(", ");
00307 print_expr(exp->right.v.expr);
00308 LM_DBG(" )");
00309 break;
00310 case NOT_OP:
00311 LM_DBG("NOT( ");
00312 print_expr(exp->left.v.expr);
00313 LM_DBG(" )");
00314 break;
00315 case EVAL_OP:
00316 LM_DBG("EVAL( ");
00317 print_expr(exp->left.v.expr);
00318 LM_DBG(" )");
00319 break;
00320 case PLUS_OP:
00321 LM_DBG("PLUS( ");
00322 print_expr(exp->left.v.expr);
00323 LM_DBG(", ");
00324 print_expr(exp->right.v.expr);
00325 LM_DBG(" )");
00326 break;
00327 case MINUS_OP:
00328 LM_DBG("MINUS( ");
00329 print_expr(exp->left.v.expr);
00330 LM_DBG(", ");
00331 print_expr(exp->right.v.expr);
00332 LM_DBG(" )");
00333 break;
00334 case DIV_OP:
00335 LM_DBG("DIV( ");
00336 print_expr(exp->left.v.expr);
00337 LM_DBG(", ");
00338 print_expr(exp->right.v.expr);
00339 LM_DBG(" )");
00340 break;
00341 case MULT_OP:
00342 LM_DBG("MULT( ");
00343 print_expr(exp->left.v.expr);
00344 LM_DBG(", ");
00345 print_expr(exp->right.v.expr);
00346 LM_DBG(" )");
00347 break;
00348 case MODULO_OP:
00349 LM_DBG("MODULO( ");
00350 print_expr(exp->left.v.expr);
00351 LM_DBG(", ");
00352 print_expr(exp->right.v.expr);
00353 LM_DBG(" )");
00354 break;
00355 case BAND_OP:
00356 LM_DBG("BAND( ");
00357 print_expr(exp->left.v.expr);
00358 LM_DBG(", ");
00359 print_expr(exp->right.v.expr);
00360 LM_DBG(" )");
00361 break;
00362 case BOR_OP:
00363 LM_DBG("BOR( ");
00364 print_expr(exp->left.v.expr);
00365 LM_DBG(", ");
00366 print_expr(exp->right.v.expr);
00367 LM_DBG(" )");
00368 break;
00369 case BXOR_OP:
00370 LM_DBG("BXOR( ");
00371 print_expr(exp->left.v.expr);
00372 LM_DBG(", ");
00373 print_expr(exp->right.v.expr);
00374 LM_DBG(" )");
00375 break;
00376 case BLSHIFT_OP:
00377 LM_DBG("BLSHIFT( ");
00378 print_expr(exp->left.v.expr);
00379 LM_DBG(", ");
00380 print_expr(exp->right.v.expr);
00381 LM_DBG(" )");
00382 break;
00383 case BRSHIFT_OP:
00384 LM_DBG("BRSHIFT( ");
00385 print_expr(exp->left.v.expr);
00386 LM_DBG(", ");
00387 print_expr(exp->right.v.expr);
00388 LM_DBG(" )");
00389 break;
00390 case BNOT_OP:
00391 LM_DBG("BNOT( ");
00392 print_expr(exp->left.v.expr);
00393 LM_DBG(" )");
00394 break;
00395 default:
00396 LM_DBG("UNKNOWN_EXP[%d] ", exp->op);
00397 }
00398
00399 }else{
00400 LM_ERR("unknown type\n");
00401 }
00402 }
00403
00404
00405 void print_action(struct action* t)
00406 {
00407 switch(t->type){
00408 case FORWARD_T:
00409 LM_DBG("forward(");
00410 break;
00411 case SEND_T:
00412 LM_DBG("send(");
00413 break;
00414 case DROP_T:
00415 LM_DBG("drop(");
00416 break;
00417 case LOG_T:
00418 LM_DBG("log(");
00419 break;
00420 case ERROR_T:
00421 LM_DBG("error(");
00422 break;
00423 case ROUTE_T:
00424 LM_DBG("route(");
00425 break;
00426 case EXEC_T:
00427 LM_DBG("exec(");
00428 break;
00429 case REVERT_URI_T:
00430 LM_DBG("revert_uri(");
00431 break;
00432 case STRIP_T:
00433 LM_DBG("strip(");
00434 break;
00435 case APPEND_BRANCH_T:
00436 LM_DBG("append_branch(");
00437 break;
00438 case PREFIX_T:
00439 LM_DBG("prefix(");
00440 break;
00441 case LEN_GT_T:
00442 LM_DBG("len_gt(");
00443 break;
00444 case SET_DEBUG_T:
00445 LM_DBG("setdebug(");
00446 break;
00447 case SETFLAG_T:
00448 LM_DBG("setflag(");
00449 break;
00450 case RESETFLAG_T:
00451 LM_DBG("resetflag(");
00452 break;
00453 case ISFLAGSET_T:
00454 LM_DBG("isflagset(");
00455 break;
00456 case SETBFLAG_T:
00457 LM_DBG("setbflag(");
00458 break;
00459 case RESETBFLAG_T:
00460 LM_DBG("resetbflag(");
00461 break;
00462 case ISBFLAGSET_T:
00463 LM_DBG("isbflagset(");
00464 break;
00465 case SETSFLAG_T:
00466 LM_DBG("setsflag(");
00467 break;
00468 case RESETSFLAG_T:
00469 LM_DBG("resetsflag(");
00470 break;
00471 case ISSFLAGSET_T:
00472 LM_DBG("issflagset(");
00473 break;
00474 case SET_HOST_T:
00475 LM_DBG("sethost(");
00476 break;
00477 case SET_HOSTPORT_T:
00478 LM_DBG("sethostport(");
00479 break;
00480 case SET_USER_T:
00481 LM_DBG("setuser(");
00482 break;
00483 case SET_USERPASS_T:
00484 LM_DBG("setuserpass(");
00485 break;
00486 case SET_PORT_T:
00487 LM_DBG("setport(");
00488 break;
00489 case SET_URI_T:
00490 LM_DBG("seturi(");
00491 break;
00492 case IF_T:
00493 LM_DBG("if (");
00494 break;
00495 case WHILE_T:
00496 LM_DBG("while (");
00497 break;
00498 case MODULE_T:
00499 LM_DBG(" external_module_call(");
00500 break;
00501 case FORCE_RPORT_T:
00502 LM_DBG("force_rport(");
00503 break;
00504 case SET_ADV_ADDR_T:
00505 LM_DBG("set_advertised_address(");
00506 break;
00507 case SET_ADV_PORT_T:
00508 LM_DBG("set_advertised_port(");
00509 break;
00510 case FORCE_TCP_ALIAS_T:
00511 LM_DBG("force_tcp_alias(");
00512 break;
00513 case FORCE_SEND_SOCKET_T:
00514 LM_DBG("force_send_socket");
00515 break;
00516 case RETURN_T:
00517 LM_DBG("return(");
00518 break;
00519 case EXIT_T:
00520 LM_DBG("exit(");
00521 break;
00522 case SWITCH_T:
00523 LM_DBG("switch(");
00524 break;
00525 case CASE_T:
00526 LM_DBG("case(");
00527 break;
00528 case DEFAULT_T:
00529 LM_DBG("default(");
00530 break;
00531 case SBREAK_T:
00532 LM_DBG("sbreak(");
00533 break;
00534 case EQ_T:
00535 LM_DBG("assign(");
00536 break;
00537 default:
00538 LM_DBG("UNKNOWN(");
00539 }
00540 switch(t->elem[0].type){
00541 case STRING_ST:
00542 LM_DBG("\"%s\"", ZSW(t->elem[0].u.string));
00543 break;
00544 case NUMBER_ST:
00545 LM_DBG("%lu",t->elem[0].u.number);
00546 break;
00547 case SCRIPTVAR_ST:
00548 LM_DBG("scriptvar[%d]",t->elem[0].u.item->type);
00549 break;
00550 case IP_ST:
00551 print_ip("", (struct ip_addr*)t->elem[0].u.data, "");
00552 break;
00553 case EXPR_ST:
00554 print_expr((struct expr*)t->elem[0].u.data);
00555 break;
00556 case ACTIONS_ST:
00557 print_actions((struct action*)t->elem[0].u.data);
00558 break;
00559 case CMD_ST:
00560 LM_DBG("f<%s>",((cmd_export_t*)t->elem[0].u.data)->name);
00561 break;
00562 case SOCKID_ST:
00563 LM_DBG("%d:%s:%d",
00564 ((struct socket_id*)t->elem[0].u.data)->proto,
00565 ZSW(((struct socket_id*)t->elem[0].u.data)->name),
00566 ((struct socket_id*)t->elem[0].u.data)->port
00567 );
00568 break;
00569 default:
00570 LM_DBG("type<%d>", t->elem[0].type);
00571 }
00572 if (t->type==IF_T) LM_DBG(") {");
00573 switch(t->elem[1].type){
00574 case NOSUBTYPE:
00575 break;
00576 case STRING_ST:
00577 LM_DBG(", \"%s\"", ZSW(t->elem[1].u.string));
00578 break;
00579 case NUMBER_ST:
00580 LM_DBG(", %lu",t->elem[1].u.number);
00581 break;
00582 case EXPR_ST:
00583 print_expr((struct expr*)t->elem[1].u.data);
00584 break;
00585 case ACTIONS_ST:
00586 print_actions((struct action*)t->elem[1].u.data);
00587 break;
00588 case SOCKID_ST:
00589 LM_DBG("%d:%s:%d",
00590 ((struct socket_id*)t->elem[1].u.data)->proto,
00591 ZSW(((struct socket_id*)t->elem[1].u.data)->name),
00592 ((struct socket_id*)t->elem[1].u.data)->port
00593 );
00594 break;
00595 default:
00596 LM_DBG(", type<%d>", t->elem[1].type);
00597 }
00598 if (t->type==IF_T && t->elem[2].type!=NOSUBTYPE) LM_DBG(" } else { ");
00599 switch(t->elem[2].type){
00600 case NOSUBTYPE:
00601 break;
00602 case STRING_ST:
00603 LM_DBG(", \"%s\"", ZSW(t->elem[2].u.string));
00604 break;
00605 case NUMBER_ST:
00606 LM_DBG(", %lu",t->elem[2].u.number);
00607 break;
00608 case EXPR_ST:
00609 print_expr((struct expr*)t->elem[2].u.data);
00610 break;
00611 case ACTIONS_ST:
00612 print_actions((struct action*)t->elem[2].u.data);
00613 break;
00614 case SOCKID_ST:
00615 LM_DBG("%d:%s:%d",
00616 ((struct socket_id*)t->elem[2].u.data)->proto,
00617 ZSW(((struct socket_id*)t->elem[2].u.data)->name),
00618 ((struct socket_id*)t->elem[2].u.data)->port
00619 );
00620 break;
00621 default:
00622 LM_DBG(", type<%d>", t->elem[2].type);
00623 }
00624 if (t->type==IF_T) LM_DBG("}; ");
00625 else LM_DBG("); ");
00626
00627 }
00628
00629 void print_actions(struct action* a)
00630 {
00631 while(a) {
00632 print_action(a);
00633 a = a->next;
00634 }
00635 }
00636
00637