⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 siproxd.c

📁 sip proxy
💻 C
📖 第 1 页 / 共 2 页
字号:
      /*       * got input, process       */      DEBUGC(DBCLASS_BABBLE,"back from sipsock_wait");      ticket.direction=0;      buflen=sipsock_read(&buff, sizeof(buff)-1, &ticket.from,                           &ticket.protocol);      buff[buflen]='\0';      /*       * evaluate the access lists (IP based filter)       */      access=accesslist_check(ticket.from);      if (access == 0) {         DEBUGC(DBCLASS_ACCESS,"access for this packet was denied");         continue; /* there are no resources to free */      }      /*       * integrity checks       */      sts=security_check_raw(buff, buflen);      if (sts != STS_SUCCESS) {         DEBUGC(DBCLASS_SIP,"security check (raw) failed");         continue; /* there are no resources to free */      }      /*       * Hacks to fix-up some broken headers       */      sts=sip_fixup_asterisk(buff, &buflen);      /*       * init sip_msg       */      sts=osip_message_init(&ticket.sipmsg);      ticket.sipmsg->message=NULL;      if (sts != 0) {         ERROR("osip_message_init() failed... this is not good");         continue; /* skip, there are no resources to free */      }      /*       * RFC 3261, Section 16.3 step 1       * Proxy Behavior - Request Validation - Reasonable Syntax       * (parse the received message)       */      sts=sip_message_parse(ticket.sipmsg, buff, buflen);      if (sts != 0) {         ERROR("sip_message_parse() failed... this is not good");         DUMP_BUFFER(-1, buff, buflen);         goto end_loop; /* skip and free resources */      }      /*       * integrity checks - parsed buffer       */      sts=security_check_sip(&ticket);      if (sts != STS_SUCCESS) {         ERROR("security_check_sip() failed... this is not good");         DUMP_BUFFER(-1, buff, buflen);         goto end_loop; /* skip and free resources */      }      /*       * RFC 3261, Section 16.3 step 2       * Proxy Behavior - Request Validation - URI scheme       * (check request URI and refuse with 416 if not understood)       */      /* NOT IMPLEMENTED */      /*       * RFC 3261, Section 16.3 step 3       * Proxy Behavior - Request Validation - Max-Forwards check       * (check Max-Forwards header and refuse with 483 if too many hops)       */      {         osip_header_t *max_forwards;         int forwards_count = DEFAULT_MAXFWD;         osip_message_get_max_forwards(ticket.sipmsg, 0, &max_forwards);         if (max_forwards && max_forwards->hvalue) {            forwards_count = atoi(max_forwards->hvalue);            if ((forwards_count<=0)||                (forwards_count>=LONG_MAX)) forwards_count=DEFAULT_MAXFWD;         }         DEBUGC(DBCLASS_PROXY,"checking Max-Forwards (=%i)",forwards_count);         if (forwards_count <= 0) {            DEBUGC(DBCLASS_SIP, "Forward count reached 0 -> 483 response");            sip_gen_response(&ticket, 483 /*Too many hops*/);            goto end_loop; /* skip and free resources */         }      }      /*       * RFC 3261, Section 16.3 step 4       * Proxy Behavior - Request Validation - Loop Detection check       * (check for loop and return 482 if a loop is detected)       */      if (check_vialoop(&ticket) == STS_TRUE) {         /* make sure we don't end up in endless loop when detecting          * an loop in an "loop detected" message - brrr */         if (MSG_IS_RESPONSE(ticket.sipmsg) &&              MSG_TEST_CODE(ticket.sipmsg, 482)) {            DEBUGC(DBCLASS_SIP,"loop in loop-response detected, ignoring");         } else {            DEBUGC(DBCLASS_SIP,"via loop detected, ignoring request");            sip_gen_response(&ticket, 482 /*Loop detected*/);         }         goto end_loop; /* skip and free resources */      }      /*       * RFC 3261, Section 16.3 step 5       * Proxy Behavior - Request Validation - Proxy-Require check       * (check Proxy-Require header and return 420 if unsupported option)       */      /* NOT IMPLEMENTED */      /*       * RFC 3261, Section 16.5       * Proxy Behavior - Determining Request Targets       */      /* NOT IMPLEMENTED */      DEBUGC(DBCLASS_SIP,"received SIP type %s:%s",             (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES",             (MSG_IS_REQUEST(ticket.sipmsg) ?                ((ticket.sipmsg->sip_method)?                   ticket.sipmsg->sip_method : "NULL") :                ((ticket.sipmsg->reason_phrase) ?                    ticket.sipmsg->reason_phrase : "NULL")));      /*********************************       * The message did pass all the       * tests above and is now ready       * to be proxied.       * Before we do so, we apply some       * additional preprocessing       *********************************/      /* Dial shortcuts */      if (configuration.pi_shortdial) {         sts = plugin_shortdial(&ticket);         if (sts == STS_SIP_SENT) goto end_loop;      }      /*********************************       * finally proxy the message.       * This includes the masquerading       * of the local UA and starting/       * stopping the RTP proxy for this       * call       *********************************/      /*       * if a REQ REGISTER, check if it is directed to myself,       * or am I just the outbound proxy but no registrar.       * - If I'm the registrar, register & generate answer       * - If I'm just the outbound proxy, register, rewrite & forward       */      if (MSG_IS_REGISTER(ticket.sipmsg) &&           MSG_IS_REQUEST(ticket.sipmsg)) {         if (access & ACCESSCTL_REG) {            osip_uri_t *url;            struct in_addr addr1, addr2, addr3;            int dest_port;            url = osip_message_get_uri(ticket.sipmsg);            dest_port= (url->port)?atoi(url->port):SIP_PORT;            if ((dest_port <=0) || (dest_port >65535)) dest_port=SIP_PORT;            if ( (get_ip_by_host(url->host, &addr1) == STS_SUCCESS) &&                 (get_interface_ip(IF_INBOUND,&addr2) == STS_SUCCESS) &&                 (get_interface_ip(IF_OUTBOUND,&addr3) == STS_SUCCESS)) {               if ((configuration.sip_listen_port == dest_port) &&                   ((memcmp(&addr1, &addr2, sizeof(addr1)) == 0) ||                    (memcmp(&addr1, &addr3, sizeof(addr1)) == 0))) {                  /* I'm the registrar, send response myself */                  sts = register_client(&ticket, 0);                  sts = register_response(&ticket, sts);               } else {                  /* I'm just the outbound proxy */                  DEBUGC(DBCLASS_SIP,"proxying REGISTER request to:%s",                         url->host);                  sts = register_client(&ticket, 1);                  if (sts == STS_SUCCESS) {                     sts = proxy_request(&ticket);                  }               }            } else {               sip_gen_response(&ticket, 408 /*request timeout*/);            }         } else {            WARN("non-authorized registration attempt from %s",                 utils_inet_ntoa(ticket.from.sin_addr));         }      /*       * check if outbound interface is UP.       * If not, send back error to UA and       * skip any proxying attempt       */      } else if (get_interface_ip(IF_OUTBOUND,NULL) !=                 STS_SUCCESS) {         DEBUGC(DBCLASS_SIP, "got a %s to proxy, but outbound interface "                "is down", (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES");         if (MSG_IS_REQUEST(ticket.sipmsg))            sip_gen_response(&ticket, 408 /*request timeout*/);            /*       * MSG is a request, add current via entry,       * do a lookup in the URLMAP table and       * send to the final destination       */      } else if (MSG_IS_REQUEST(ticket.sipmsg)) {         if (access & ACCESSCTL_SIP) {            sts = proxy_request(&ticket);         } else {            INFO("non-authorized request received from %s",                 utils_inet_ntoa(ticket.from.sin_addr));         }      /*       * MSG is a response, remove current via and       * send to the next VIA in chain       */      } else if (MSG_IS_RESPONSE(ticket.sipmsg)) {         if (access & ACCESSCTL_SIP) {            sts = proxy_response(&ticket);         } else {            INFO("non-authorized response received from %s",                 utils_inet_ntoa(ticket.from.sin_addr));         }               /*       * unsupported message       */      } else {         ERROR("received unsupported SIP type %s %s",               (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES",               ticket.sipmsg->sip_method);      }      /*********************************       * Done with proxying. Message       * has been sent to its destination.       *********************************//* * free the SIP message buffers */      end_loop:      osip_message_free(ticket.sipmsg);   } /* while TRUE */   exit_prg:   /* save current known SIP registrations */   register_save();   INFO("properly terminating siproxd");   /* remove PID file */   if (pidfilename) {      DEBUGC(DBCLASS_CONFIG,"deleting PID file [%s]", pidfilename);      sts=unlink(pidfilename);      if (sts != 0) {         WARN("couldn't delete old PID file: %s", strerror(errno));      }   }   /* END */   log_end();   return 0;} /* main *//* * Signal handler * * this one is called asynchronously whevener a registered * signal is applied. Just set a flag and don't do any funny * things here. */static void sighandler(int sig) {   if (sig==SIGTERM) exit_program=1;   if (sig==SIGINT)  exit_program=1;   if (sig==SIGUSR2) dmalloc_dump=1;   return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -