📄 filter.c
字号:
#endif /* reject this too long field */ if (pmatch[i].rm_eo-pmatch[i].rm_so+1>255) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_ERROR,NULL, "filter plugin: url component is too long, I choose to reject it!\n")); osip_free(url); return -1; }#ifdef WIN32 if (i==1) _snprintf(match1, pmatch[i].rm_eo-pmatch[i].rm_so, /* -1 with TRE? */ "%s", url+pmatch[i].rm_so); else if (i==2) _snprintf(match2, pmatch[i].rm_eo-pmatch[i].rm_so, /* -1 with TRE? */ "%s", url+pmatch[i].rm_so);#else if (i==1) snprintf(match1, pmatch[i].rm_eo-pmatch[i].rm_so+1, "%s", url+pmatch[i].rm_so); else if (i==2) snprintf(match2, pmatch[i].rm_eo-pmatch[i].rm_so+1, "%s", url+pmatch[i].rm_so);#endif }#ifdef FILTER_TEST fprintf(stdout, "match1: %s, match2: %s\n", match1, match2);#endif osip_free(url); return 0;}static intfilter_build_dnsresult(tel_rule_t *tel_rule, osip_uri_t *req_uri, char *match1, char *match2, char **dest){ char *tmp; char *_tmp; char *_tmp2; *dest = NULL; if (req_uri->scheme==NULL) /* just in case */ req_uri->scheme=osip_strdup("sip"); /* find %1 in string */ _tmp = strstr(tel_rule->dnsresult, "%1"); /* find %2 in string */ _tmp2 = strstr(tel_rule->dnsresult, "%2"); /* this is always enough: (and match1 and match2 can be empty string '\0') */ tmp = osip_malloc(strlen(tel_rule->dnsresult) +strlen(match1)+strlen(match2) + 3 ); if (_tmp!=NULL) /* contains %s */ { /* copy from the beginning to '%1' */ osip_strncpy(tmp,tel_rule->dnsresult, _tmp-tel_rule->dnsresult); /* copy match1 from the current pos end of match1 */ osip_strncpy(tmp + strlen(tmp), match1, strlen(match1)); if (_tmp2==NULL) /* simple case */ /* copy the rest up to the end */ osip_strncpy(tmp + strlen(tmp), _tmp+2, strlen(_tmp+2)); else { /* copy the rest up to %2 */ osip_strncpy(tmp + strlen(tmp), _tmp+2, _tmp2-_tmp-2); /* copy match2 from the current pos end of match2 */ osip_strncpy(tmp + strlen(tmp), match2, strlen(match2)); /* copy the rest up to the end */ osip_strncpy(tmp + strlen(tmp), _tmp2+2, strlen(_tmp2+2)); } } else if (_tmp2!=NULL) { /* copy from the beginning to '%2' */ osip_strncpy(tmp,tel_rule->dnsresult, _tmp2-tel_rule->dnsresult); /* copy match1 from the current pos end of match2 */ osip_strncpy(tmp + strlen(tmp), match2, strlen(match2)); /* copy the rest up to the end */ osip_strncpy(tmp + strlen(tmp), _tmp2+2, strlen(_tmp2+2)); } else sprintf(tmp, tel_rule->dnsresult); if (tmp!=NULL) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO4,NULL, "filter plugin: Here is the resulted value: %s\n", tmp)); } *dest = tmp; return 0;}#endif/* HOOK METHODS *//* This method returns: -2 if plugin consider this request should be totally discarded! -1 on error 0 nothing has been done 1 things has been done on psp_req element*/int cb_filter_search_location(psp_request_t *psp_req){#if defined(WIN32) && !defined(TRE) OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO4,NULL, "filter plugin: not implemented on WIN32\n")); psp_request_set_state(psp_req, PSP_PROPOSE); psp_request_set_uas_status(psp_req, 404); psp_request_set_mode(psp_req, PSP_UAS_MODE); return 0;#else tel_rule_t *tel_rule; location_t *loc; osip_route_t *route; osip_uri_t *url; int i; osip_uri_param_t *psp_param; osip_message_t *request; request = psp_request_get_request(psp_req); OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: filter_context %p!\n", filter_context)); if (ISSET_INTERNAL_FILTERSCOPE(filter_context->flag)) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: configured to process url for local domain!\n")); } if (ISSET_EXTERNAL_FILTERSCOPE(filter_context->flag)) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: configured to process url for non local domain!\n")); } OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: entering cb_filter_search_location\n")); /* default OUTPUT */ if (ISSET_R_ROUTE_MODE(filter_context->flag)) psp_request_set_property(psp_req, PSP_STAY_ON_PATH); else psp_request_set_property(psp_req, 0); if (!ISSET_REDIRECT_MODE(filter_context->flag)) /* state full or default mode */ psp_request_set_mode(psp_req, PSP_SFULL_MODE); else { psp_request_set_uas_status(psp_req, 302); psp_request_set_mode(psp_req, PSP_UAS_MODE); } i=0; for (;!osip_list_eol(request->routes, i);i++) { osip_message_get_route (request, i, &route); if (0 != psp_core_is_responsible_for_this_route(route->url)) { psp_request_set_mode (psp_req, PSP_SFULL_MODE); psp_request_set_state (psp_req, PSP_MANDATE); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL, "filter plugin: mandate statefull handling for route.\n")); return 0; } } psp_request_set_state(psp_req, PSP_MANDATE); if (i>1) { psp_request_set_uas_status(psp_req, 482); /* loop? */ psp_request_set_mode(psp_req, PSP_UAS_MODE); return 0; } if (i==1) { osip_message_get_route(request, 0, &route); /* should be the first one */ /* if this route header contains the "psp" parameter, it means the element does not come from a pre-route-set header (in this last case, we want to execute the plugin for the initial request) */ /* NON compliant UAs (not returning this parameter) are guilty. */ osip_uri_uparam_get_byname (route->url, "psp", &psp_param); if (psp_param!=NULL) { psp_request_set_state(psp_req, PSP_MANDATE); psp_request_set_mode (psp_req, PSP_SFULL_MODE); /* got it, leave this plugin. */ return 0; } } if (ISSET_INTERNAL_FILTERSCOPE(filter_context->flag) && ISSET_EXTERNAL_FILTERSCOPE(filter_context->flag)) { /* process request in all cases */ } else { if (0 == psp_core_is_responsible_for_this_domain (request-> req_uri)) { if (!ISSET_INTERNAL_FILTERSCOPE(filter_context->flag)) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: uri is for our local domain -> do not process it!\n")); psp_request_set_state(psp_req, PSP_PROPOSE); psp_request_set_uas_status(psp_req, 404); psp_request_set_mode(psp_req, PSP_UAS_MODE); return 0; } } else { if (!ISSET_EXTERNAL_FILTERSCOPE(filter_context->flag)) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: uri is not for our local domain -> do not process it!\n")); psp_request_set_state(psp_req, PSP_PROPOSE); psp_request_set_uas_status(psp_req, 404); psp_request_set_mode(psp_req, PSP_UAS_MODE); return 0; } } } for (tel_rule = filter_context->tel_rules; tel_rule!=NULL; tel_rule=tel_rule->next) { char match1[256]; char match2[256]; /* Add this for TRE */ memset(match1, 0, sizeof(match1)); memset(match2, 0, sizeof(match2)); i = filter_match_rule(tel_rule, request, match1, match2); if (i!=-1) { char *tmp; filter_build_dnsresult(tel_rule, request->req_uri, match1, match2, &tmp); if (tmp==NULL) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_ERROR,NULL, "filter plugin: failed to build new destination url!\n")); psp_request_set_uas_status(psp_req, 400); psp_request_set_mode(psp_req, PSP_UAS_MODE); psp_request_set_state(psp_req, PSP_MANDATE); return -1; } i = osip_uri_init(&url); i = osip_uri_parse(url, tmp); osip_free(tmp); if (i!=0) { OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_ERROR,NULL, "filter plugin: Could not clone request-uri!\n")); psp_request_set_uas_status(psp_req, 400); psp_request_set_mode(psp_req, PSP_UAS_MODE); psp_request_set_state(psp_req, PSP_MANDATE); osip_uri_free(url); return -1; } i = location_init(&loc, url, 3600); if (i!=0) { /* This can only happen in case we don't have enough memory */ osip_uri_free(url); OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL, "filter plugin: Could not create location info!\n")); psp_request_set_uas_status(psp_req, 400); psp_request_set_mode(psp_req, PSP_UAS_MODE); psp_request_set_state(psp_req, PSP_MANDATE); return -1; } ADD_ELEMENT(psp_req->locations, loc); OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: mandate statefull (or redirect) mode for request.\n")); return 0; } } OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL, "filter plugin: Didn't do anything with this request?\n")); psp_request_set_state(psp_req, PSP_PROPOSE); psp_request_set_uas_status(psp_req, 404); psp_request_set_mode(psp_req, PSP_UAS_MODE); return 0;#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -