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

📄 proxy.c

📁 sip网络电话服务器原代码
💻 C
📖 第 1 页 / 共 4 页
字号:
      break;     /*   * from the internal masqueraded host to an external host   */   case REQTYP_OUTGOING:      DEBUGC(DBCLASS_PROXY,"outgoing request from %s@%s from inbound",	request->from->url->username? request->from->url->username:"*NULL*",        request->from->url->host? request->from->url->host: "*NULL*");      /*       * RFC 3261, Section 16.6 step 2       * Proxy Behavior - Request Forwarding - Request-URI       */      /* nothing to do for an outgoing request */      /* if it is addressed to myself, then it must be some request       * method that I as a proxy do not support. Reject */#if 0/* careful - an internal UA might send an request to another internal UA.   This would be caught here, so don't do this. This situation should be   caught in the default part of the CASE statement below */      if (is_sipuri_local(ticket) == STS_TRUE) {         WARN("unsupported request [%s] directed to proxy from %s@%s -> %s@%s",	    request->sip_method? request->sip_method:"*NULL*",	    request->from->url->username? request->from->url->username:"*NULL*",	    request->from->url->host? request->from->url->host : "*NULL*",	    url->username? url->username : "*NULL*",	    url->host? url->host : "*NULL*");         sip_gen_response(ticket, 403 /*forbidden*/);         return STS_FAILURE;      }#endif      /* rewrite Contact header to represent the masqued address */      sip_rewrite_contact(ticket, DIR_OUTGOING);      /* if an INVITE, rewrite body */      if (MSG_IS_INVITE(request)) {         sts = proxy_rewrite_invitation_body(request, DIR_OUTGOING);      }      /* if this is CANCEL/BYE request, stop RTP proxying */      if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) {         /* stop the RTP proxying stream(s) */         rtp_stop_fwd(osip_message_get_call_id(request), DIR_INCOMING);         rtp_stop_fwd(osip_message_get_call_id(request), DIR_OUTGOING);      }else if(MSG_IS_NOTIFY(request))      {		printf("***********      recv notify message!     outgoing   **********\n");      }else if(MSG_IS_SUBSCRIBE(request))      {		printf("\n\n\n\n************    recv subscribe message!   outgoing  ***********\n\n\n");      }      break;      default:      DEBUGC(DBCLASS_PROXY, "request [%s] from/to unregistered UA "           "(RQ: %s@%s -> %s@%s)",           request->sip_method? request->sip_method:"*NULL*",	   request->from->url->username? request->from->url->username:"*NULL*",	   request->from->url->host? request->from->url->host : "*NULL*",	   url->username? url->username : "*NULL*",	   url->host? url->host : "*NULL*");/* * we may end up here for two reasons: *  1) An incomming request (from outbound) that is directed to *     an unknown (not registered) local UA *  2) an outgoing request from a local UA that is not registered. * * Case 1) we should probably answer with "404 Not Found", * case 2) more likely a "403 Forbidden" *  * How about "408 Request Timeout" ? * */      sip_gen_response(ticket, 408 /* Request Timeout */);      return STS_FAILURE;   }   /*    * RFC 3261, Section 16.6 step 3    * Proxy Behavior - Request Forwarding - Max-Forwards    * (if Max-Forwards header exists, decrement by one, if it does not    * exist, add a new one with value SHOULD be 70)    */   {   osip_header_t *max_forwards;   int forwards_count = DEFAULT_MAXFWD;   char mfwd[8];   osip_message_get_max_forwards(request, 0, &max_forwards);   if (max_forwards == NULL) {      sprintf(mfwd, "%i", forwards_count);      osip_message_set_max_forwards(request, mfwd);   } else {      if (max_forwards->hvalue) {         forwards_count = atoi(max_forwards->hvalue);         forwards_count -=1;         osip_free (max_forwards->hvalue);      }      sprintf(mfwd, "%i", forwards_count);      max_forwards->hvalue = osip_strdup(mfwd);   }   DEBUGC(DBCLASS_PROXY,"setting Max-Forwards=%s",mfwd);   }   /*    * RFC 3261, Section 16.6 step 4    * Proxy Behavior - Request Forwarding - Add a Record-route header    */   /*    * for ALL incoming requests, include my Record-Route header.    * The local UA will probably send its answer to the topmost     * Route Header (8.1.2 of RFC3261)    */   if (type == REQTYP_INCOMING) {      DEBUGC(DBCLASS_PROXY,"Adding my Record-Route");      route_add_recordroute(ticket);   } else {      /*       * outgoing packets must not have my record route header, as       * this likely will contain a private IP address (my inbound).       */      DEBUGC(DBCLASS_PROXY,"Purging Record-Routes (outgoing packet)");      route_purge_recordroute(ticket);   }   /*    * RFC 3261, Section 16.6 step 5    * Proxy Behavior - Request Forwarding - Add Additional Header Fields    */   /* NOT IMPLEMENTED (optional) */   /*    * RFC 3261, Section 16.6 step 6    * Proxy Behavior - Request Forwarding - Postprocess routing information    *    * If the copy contains a Route header field, the proxy MUST    * inspect the URI in its first value.  If that URI does not    * contain an lr parameter, the proxy MUST modify the copy as    * follows:    *    * -  The proxy MUST place the Request-URI into the Route header    *    field as the last value.    *    * -  The proxy MUST then place the first Route header field value    *    into the Request-URI and remove that value from the Route    *    header field.    */#if 0   route_postprocess(ticket);#endif   /*    * RFC 3261, Section 16.6 step 7    * Proxy Behavior - Determine Next-Hop Address    *//*&&&& priority probably should be: * 1) Route header * 2) fixed outbound proxy * 3) SIP URI */   /*    * fixed outbound proxy defined ?    */   if ((type == REQTYP_OUTGOING) && (configuration.outbound_proxy_host)) {      /* I have an outbound proxy configured */      sts = get_ip_by_host(configuration.outbound_proxy_host, &sendto_addr);      if (sts == STS_FAILURE) {         DEBUGC(DBCLASS_PROXY, "proxy_request: cannot resolve outbound "                " proxy host [%s]", configuration.outbound_proxy_host);         return STS_FAILURE;      }      if (configuration.outbound_proxy_port) {         port=configuration.outbound_proxy_port;printf("\n*******port = %d ******\n");      } else {         port = SIP_PORT;      }      DEBUGC(DBCLASS_PROXY, "proxy_request: have outbound proxy %s:%i",             configuration.outbound_proxy_host, port);   /*    * Route present?    * If so, fetch address from topmost Route: header and remove it.    */   } else if ((type == REQTYP_OUTGOING) &&               (request->routes && !osip_list_eol(request->routes, 0))) {      sts=route_determine_nexthop(ticket, &sendto_addr, &port);      if (sts == STS_FAILURE) {         DEBUGC(DBCLASS_PROXY, "proxy_request: route_determine_nexthop failed");         return STS_FAILURE;      }      DEBUGC(DBCLASS_PROXY, "proxy_request: have Route header to %s:%i",             utils_inet_ntoa(sendto_addr), port);   /*    * destination from SIP URI    */   } else {      /* get the destination from the SIP URI */ int flag=0;    for(i=0;i<URLMAP_SIZE;i++)   {	if(urlmap[i].active == 0) continue;   	if( compare_url(url,urlmap[i].reg_url)  == STS_SUCCESS &&		compare_url(url,urlmap[i].masq_url)  == STS_SUCCESS)	{		flag =1;		break;	}   }  if(flag == 1)  {	//memcpy(&sendto_addr, utils_inet_aton(urlmap[i].true_url->host),sizeof(struct in_addr));	utils_inet_aton(urlmap[i].true_url->host, &sendto_addr);	if( urlmap[i].true_url->port)	{		printf("\n**** true_url:port = %s  &&&&&&\n",urlmap[i].true_url->port);		port = osip_atoi(urlmap[i].true_url->port);	}	else	{		port = SIP_PORT;	}  }else{      sts = get_ip_by_host(url->host, &sendto_addr);      if (sts == STS_FAILURE) {         DEBUGC(DBCLASS_PROXY, "proxy_request: cannot resolve URI [%s]",                url->host);         return STS_FAILURE;      }      if (url->port) {         port=atoi(url->port);      } else {         port=SIP_PORT;      }      DEBUGC(DBCLASS_PROXY, "proxy_request: have SIP URI to %s:%i",             url->host, port); }}   /*    * RFC 3261, Section 16.6 step 8    * Proxy Behavior - Add a Via header field value    */   /* add my Via header line (outbound interface)*/   if (type == REQTYP_INCOMING) {	printf("\n\nn\n   ***********  incoming !  1 ***********\n\n\nn\n");      sts = sip_add_myvia(ticket, IF_INBOUND);      if (sts == STS_FAILURE) {         ERROR("adding my inbound via failed!");      }   }    else {      printf("\n\nn\n   ***********  outgoing  !  2 ***********\n\n\nn\n");	      sts = sip_add_myvia(ticket, IF_OUTBOUND);      if (sts == STS_FAILURE) {         ERROR("adding my outbound via failed!");         return STS_FAILURE;      }   }  /*   * RFC 3261, Section 16.6 step 9   * Proxy Behavior - Add a Content-Length header field if necessary   */  /* not necessary, already in message and we do not support TCP */  /*   * RFC 3261, Section 16.6 step 10   * Proxy Behavior - Forward the new request   */   sts = osip_message_to_str(request, &buffer);   if (sts != 0) {      ERROR("proxy_request: osip_message_to_str failed");      return STS_FAILURE;   }   sipsock_send(sendto_addr, port, ticket->protocol,                buffer, strlen(buffer));    osip_free (buffer);  /*   * RFC 3261, Section 16.6 step 11   * Proxy Behavior - Set timer C   */  /* NOT IMPLEMENTED - does this really apply for stateless proxies? */   return STS_SUCCESS;}/* * PROXY_RESPONSE * * RETURNS *	STS_SUCCESS on success *	STS_FAILURE on error * RFC3261 *    Section 16.7: Proxy Behavior - Response Processing *    1.  Find the appropriate response context *    2.  Update timer C for provisional responses *    3.  Remove the topmost Via *    4.  Add the response to the response context *    5.  Check to see if this response should be forwarded immediately *    6.  When necessary, choose the best final response from the *        response context *    7.  Aggregate authorization header field values if necessary *    8.  Optionally rewrite Record-Route header field values *    9.  Forward the response *    10. Generate any necessary CANCEL requests  * */int proxy_response (sip_ticket_t *ticket) {   int i;   int sts;   int type;   struct in_addr sendto_addr;   osip_via_t *via;   int port;   char *buffer;   osip_message_t *response;   struct sockaddr_in *from;   DEBUGC(DBCLASS_PROXY,"proxy_response");   if (ticket==NULL) {      ERROR("proxy_response: called with NULL ticket");      return STS_FAILURE;   }   response=ticket->sipmsg;   from=&ticket->from;   /*    * RFC 3261, Section 16.7 step 3    * Proxy Behavior - Response Processing - Remove my Via header field value    */   /* remove my Via header line */   sts = sip_del_myvia(ticket);printf("\n****************     dellet my via       *********************\n");   if (sts == STS_FAILURE) {      DEBUGC(DBCLASS_PROXY,"not addressed to my VIA, ignoring response");      return STS_FAILURE;   }   /*    * figure out if this is an request coming from the outside    * world to one of our registered clients    */   /* Ahhrghh...... a response seems to have NO contact information... 

⌨️ 快捷键说明

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