Esempio n. 1
0
int BuildResponse(const osip_message_t *request, osip_message_t **response)
{
	osip_message_t *msg = NULL;
	char port[6];
	char* pContact = osip_malloc(strlen(SERV_IP_ADDR) + strlen(USER_NAME) + sizeof(port) + 7 + 10 ); // 7 is "sip:...@..:. \0"    10 - zapas
	osip_message_init(&msg);

	osip_from_clone(request->from, &msg->from);
	osip_to_clone(request->to, &msg->to);
	osip_cseq_clone(request->cseq, &msg->cseq);
	osip_call_id_clone(request->call_id, &msg->call_id);

	int pos = 0;//copy vias from request to response
	while (!osip_list_eol(&request->vias, pos))
	{
		osip_via_t *via;
		osip_via_t *via2;

		via = (osip_via_t *) osip_list_get(&request->vias, pos);
		int i = osip_via_clone(via, &via2);
		if (i != 0)
		{
			osip_message_free(msg);
			return i;
		}
		osip_list_add(&(msg->vias), via2, -1);
		pos++;
	}	
	osip_message_set_max_forwards(msg, osip_strdup("70"));
	osip_to_set_tag(msg->to, osip_strdup("4893693")); // set to tag in response. todo: randomize
	osip_message_set_version(msg, osip_strdup("SIP/2.0"));
	// create contact
	strcat(pContact, osip_strdup("sip:"));
	strcat(pContact, osip_strdup(USER_NAME));
	strcat(pContact, osip_strdup("@"));
	strcat(pContact, osip_strdup(SERV_IP_ADDR));
	osip_message_set_contact(msg, osip_strdup(pContact));
	
	osip_free(pContact);
	osip_message_set_user_agent(msg, osip_strdup("SipMore/0.1"));

	*response = msg;
	return 0;
}
Esempio n. 2
0
File: proxy.c Progetto: OPSF/uClinux
/*
 * PROXY_REQUEST
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 *
 * RFC3261
 *    Section 16.3: Proxy Behavior - Request Validation
 *    1. Reasonable Syntax
 *    2. URI scheme
 *    3. Max-Forwards
 *    4. (Optional) Loop Detection
 *    5. Proxy-Require
 *    6. Proxy-Authorization
 *
 *    Section 16.6: Proxy Behavior - Request Forwarding
 *    1.  Make a copy of the received request
 *    2.  Update the Request-URI
 *    3.  Update the Max-Forwards header field
 *    4.  Optionally add a Record-route header field value
 *    5.  Optionally add additional header fields
 *    6.  Postprocess routing information
 *    7.  Determine the next-hop address, port, and transport
 *    8.  Add a Via header field value
 *    9.  Add a Content-Length header field if necessary
 *    10. Forward the new request
 *    11. Set timer C
 */
int proxy_request (sip_ticket_t *ticket) {
   int i;
   int sts;
   int type;
   struct in_addr sendto_addr;
   osip_uri_t *url;
   int port;
   char *buffer;
   int buflen;
   osip_message_t *request;
   struct sockaddr_in *from;

   DEBUGC(DBCLASS_PROXY,"proxy_request");

   if (ticket==NULL) {
      ERROR("proxy_request: called with NULL ticket");
      return STS_FAILURE;
   }

   request=ticket->sipmsg;
   from=&ticket->from;

   /*
    * RFC 3261, Section 16.4
    * Proxy Behavior - Route Information Preprocessing
    * (process Route header)
    */
   route_preprocess(ticket);

   /*
    * figure out whether this is an incoming or outgoing request
    * by doing a lookup in the registration table.
    */
#define _OLD_DIRECTION_EVALUATION 0
#if _OLD_DIRECTION_EVALUATION
   type = 0;
   for (i=0; i<URLMAP_SIZE; i++) {
      if (urlmap[i].active == 0) continue;

      /* incoming request ('to' == 'masq') || (('to' == 'reg') && !REGISTER)*/
      if ((compare_url(request->to->url, urlmap[i].masq_url)==STS_SUCCESS) ||
          (!MSG_IS_REGISTER(request) &&
           (compare_url(request->to->url, urlmap[i].reg_url)==STS_SUCCESS))) {
         type=REQTYP_INCOMING;
         DEBUGC(DBCLASS_PROXY,"incoming request from %s@%s from outbound",
	   request->from->url->username? request->from->url->username:"******",
           request->from->url->host? request->from->url->host: "*NULL*");
	 break;
      }

      /* outgoing request ('from' == 'reg') */
      if (compare_url(request->from->url, urlmap[i].reg_url)==STS_SUCCESS) {
         type=REQTYP_OUTGOING;
         DEBUGC(DBCLASS_PROXY,"outgoing request from %s@%s from inbound",
	   request->from->url->username? request->from->url->username:"******",
           request->from->url->host? request->from->url->host: "*NULL*");
	 break;
      }
   }
#else
   type = 0;
   /*
    * did I receive the telegram from a REGISTERED host?
    * -> it must be an OUTGOING request
    */
   for (i=0; i<URLMAP_SIZE; i++) {
      struct in_addr tmp_addr;

      if (urlmap[i].active == 0) continue;
      if (get_ip_by_host(urlmap[i].true_url->host, &tmp_addr) == STS_FAILURE) {
         DEBUGC(DBCLASS_PROXY, "proxy_request: cannot resolve host [%s]",
             urlmap[i].true_url);
      } else {
         DEBUGC(DBCLASS_PROXY, "proxy_request: reghost:%s ip:%s",
                urlmap[i].true_url->host, utils_inet_ntoa(from->sin_addr));
         if (memcmp(&tmp_addr, &from->sin_addr, sizeof(tmp_addr)) == 0) {
            type=REQTYP_OUTGOING;
	    break;
         }
      }
   }

   /*
    * is the telegram directed to an internally registered host?
    * -> it must be an INCOMING request
    */
   if (type == 0) {
      for (i=0; i<URLMAP_SIZE; i++) {
         if (urlmap[i].active == 0) continue;
         /* RFC3261:
          * 'To' contains a display name (Bob) and a SIP or SIPS URI
          * (sip:[email protected]) towards which the request was originally
          * directed.  Display names are described in RFC 2822 [3].
          */

         /* So this means, that we must check the SIP URI supplied with the
          * INVITE method, as this points to the real wanted target.
          * Q: does there exist a situation where the SIP URI itself does
          *    point to "somewhere" but the To: points to the correct UA?
          * So for now, we just look at both of them (SIP URI and To: header)
          */

         /* incoming request (SIP URI == 'masq') || ((SIP URI == 'reg') && !REGISTER)*/
         if ((compare_url(request->req_uri, urlmap[i].masq_url)==STS_SUCCESS) ||
             (!MSG_IS_REGISTER(request) &&
              (compare_url(request->req_uri, urlmap[i].reg_url)==STS_SUCCESS))) {
            type=REQTYP_INCOMING;
	    break;
         }
         /* incoming request ('to' == 'masq') || (('to' == 'reg') && !REGISTER)*/
         if ((compare_url(request->to->url, urlmap[i].masq_url)==STS_SUCCESS) ||
             (!MSG_IS_REGISTER(request) &&
              (compare_url(request->to->url, urlmap[i].reg_url)==STS_SUCCESS))) {
            type=REQTYP_INCOMING;
	    break;
         }
      }
   }
#endif
   ticket->direction=type;

   /*
    * logging of passing calls
    */
   if (configuration.log_calls) {
      osip_uri_t *cont_url = NULL;
      if (!osip_list_eol(request->contacts, 0))
         cont_url = ((osip_contact_t*)(request->contacts->node->element))->url;
      
      /* INVITE */
      if (MSG_IS_INVITE(request)) {
         if (cont_url) {
            INFO("%s Call from: %s@%s",
                 (type==REQTYP_INCOMING) ? "Incoming":"Outgoing",
                 cont_url->username ? cont_url->username:"******",
                 cont_url->host ? cont_url->host : "*NULL*");
         } else {
            INFO("%s Call (w/o contact header) from: %s@%s",
                 (type==REQTYP_INCOMING) ? "Incoming":"Outgoing",
	         request->from->url->username ? 
                    request->from->url->username:"******",
	         request->from->url->host ? 
                    request->from->url->host : "*NULL*");
         }
      /* BYE / CANCEL */
      } else if (MSG_IS_BYE(request) || MSG_IS_CANCEL(request)) {
         if (cont_url) {
            INFO("Ending Call from: %s@%s",
                 cont_url->username ? cont_url->username:"******",
                 cont_url->host ? cont_url->host : "*NULL*");
         } else {
            INFO("Ending Call (w/o contact header) from: %s@%s",
	         request->from->url->username ? 
                    request->from->url->username:"******",
	         request->from->url->host ? 
                    request->from->url->host : "*NULL*");
         }
      }
   } /* log_calls */


   /*
    * RFC 3261, Section 16.6 step 1
    * Proxy Behavior - Request Forwarding - Make a copy
    */
   /* nothing to do here, copy is ready in 'request'*/

   /* get destination address */
   url=osip_message_get_uri(request);

   switch (type) {
  /*
   * from an external host to the internal masqueraded host
   */
   case REQTYP_INCOMING:
      DEBUGC(DBCLASS_PROXY,"incoming request from %s@%s from outbound",
	request->from->url->username? request->from->url->username:"******",
        request->from->url->host? request->from->url->host: "*NULL*");

      /*
       * RFC 3261, Section 16.6 step 2
       * Proxy Behavior - Request Forwarding - Request-URI
       * (rewrite request URI to point to the real host)
       */
      /* 'i' still holds the valid index into the URLMAP table */
      if (check_rewrite_rq_uri(request) == STS_TRUE) {
         proxy_rewrite_request_uri(request, i);
      }

      /* 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);

      /* check for incoming request */
      } else if (MSG_IS_INVITE(request)) {
         /* Rewrite the body */
         sts = proxy_rewrite_invitation_body(request, DIR_INCOMING);

      } else if (MSG_IS_ACK(request)) {
         /* Rewrite the body */
         sts = proxy_rewrite_invitation_body(request, DIR_INCOMING);

      }
      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:"******",
        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:"******",
	    request->from->url->host? request->from->url->host : "*NULL*",
	    url->username? url->username : "******",
	    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);
      } else if (MSG_IS_ACK(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);
      }

      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:"******",
	   request->from->url->host? request->from->url->host : "*NULL*",
	   url->username? url->username : "******",
	   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 or domain outbound proxy defined ?
    */
   if ((type == REQTYP_OUTGOING) &&
       (sip_find_outbound_proxy(ticket, &sendto_addr, &port) == STS_SUCCESS)) {
      DEBUGC(DBCLASS_PROXY, "proxy_request: have outbound proxy %s:%i",
             utils_inet_ntoa(sendto_addr), 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 */
      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) {
      sts = sip_add_myvia(ticket, IF_INBOUND);
      if (sts == STS_FAILURE) {
         ERROR("adding my inbound via failed!");
      }
   } else {
      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 = sip_message_to_str(request, &buffer, &buflen);
   if (sts != 0) {
      ERROR("proxy_request: sip_message_to_str failed");
      return STS_FAILURE;
   }

   sipsock_send(sendto_addr, port, ticket->protocol,
                buffer, buflen); 
   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;
}
Esempio n. 3
0
int Node::SndRegister(REG_TYPE type , 
					  ChordId req_uri ,  
					  ChordId to , 
					  ChordId contact , 
					  BOOL IncludeSuccList) 
{
	char *message1;
	unsigned int len = 0;



	osip_transaction_t *transaction;
	osip_event_t *sipevent;
	osip_message_t *request;

	/* temp uri */
	char * tmp_uri;
	
	int i;

	i = osip_message_init(&request);
	if (i!=0) 
		goto si2perror1;
	/* prepare the request-line */
	osip_message_set_method(request, osip_strdup("REGISTER"));
	osip_message_set_version(request, osip_strdup("SIP/2.0"));
	osip_message_set_status_code(request, 0);
	osip_message_set_reason_phrase(request, NULL);

	//requset uri
	if(type == MULTICAST)
	{
		tmp_uri = osip_strdup("sip:224.0.1.75") ;
	}
	else
		tmp_uri = ChordId2Uri(req_uri,false) ;

	osip_uri_t *uri;

	i=osip_uri_init(&uri);
	if (i!=0) 
		goto si2perror2;
	i=osip_uri_parse(uri, tmp_uri );
	if(tmp_uri)	osip_free(tmp_uri) ;
	if (i!=0)
		goto si2perror2;

	osip_message_set_uri(request , uri) ;

	if(type == JOIN)
	{
		tmp_uri = ChordId2Uri(to,false) ;

		/* when JOIN , to and from are same */
		osip_message_set_to(request, tmp_uri );
		osip_message_set_from(request, tmp_uri);
		if (tmp_uri) osip_free(tmp_uri) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("join"));
	}

	else if(type == FINDSUCC)
	{
		tmp_uri = ChordId2Uri(to,true) ;
		osip_message_set_to(request, tmp_uri );
		if (tmp_uri) osip_free(tmp_uri) ;

		tmp_uri = ChordId2Uri(chordId,false);
		osip_message_set_from(request, tmp_uri  );
		if (tmp_uri) osip_free(tmp_uri) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("findsucc"));
		
		//has no contact
	}

	else if(type == STABILIZE)
	{
		tmp_uri = ChordId2Uri(to,false);
		osip_message_set_to(request, tmp_uri);
		if (tmp_uri) osip_free(tmp_uri) ;

		tmp_uri = ChordId2Uri(chordId,false)  ;
		osip_message_set_from(request, tmp_uri );
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("stabilize"));	
		if (tmp_uri) osip_free(tmp_uri) ;
		
		//contact
		tmp_uri = ChordId2Uri(chordId,false);
		char * pre_uri = ChordId2Uri(getFingerTable()->getPredecessor(),false) ;

		char *ctt = (char *) osip_malloc(strlen(tmp_uri) + strlen(";predecessor=") + strlen(pre_uri) +1) ;
		if (ctt == NULL)
			return NULL;
		sprintf (ctt, "%s;predecessor=%s", tmp_uri,pre_uri);
		osip_free(tmp_uri) ;	osip_free(pre_uri) ;

		osip_message_set_contact(request, ctt );
		osip_free(ctt) ;
	}

	else if(type == LEAVE)
	{
		tmp_uri = ChordId2Uri(to,false) ;
		osip_message_set_to(request, tmp_uri );
		if (tmp_uri) osip_free(tmp_uri) ;

		tmp_uri = ChordId2Uri(chordId,false) ;
		osip_message_set_from(request, tmp_uri );
		if (tmp_uri) osip_free(tmp_uri) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("leave"));
		
		//contact
		tmp_uri = ChordId2Uri(chordId,false);
		char * pre_uri = ChordId2Uri(getFingerTable()->getPredecessor(),false) ;

		char *ctt = (char *) osip_malloc(strlen(tmp_uri) + strlen(";predecessor=") + strlen(pre_uri) +1) ;
		if (ctt == NULL)
			return NULL;
		sprintf (ctt, "%s;predecessor=%s", tmp_uri,pre_uri);
		osip_free(tmp_uri) ;	osip_free(pre_uri) ;

		osip_message_set_contact(request, ctt );
		osip_free(ctt) ;
		
		//succlist
		if(IncludeSuccList)
		{
			for(i = 0  ; i < getFingerTable()->getSuccNum() ; i++)
			{
				tmp_uri = ChordId2Uri(getFingerTable()->getSuccessor(i),false) ;
				osip_message_set_contact(request, tmp_uri );
				osip_free(tmp_uri) ;
			}
		}
	}//type == LEAVE
	
	if(type == MULTICAST)
	{
		
		tmp_uri = ChordId2Uri(chordId,false);
		/* when JOIN , to and from are same */
		osip_message_set_to(request, tmp_uri );
		osip_message_set_from(request, tmp_uri);
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("multicast"));
		osip_free(tmp_uri) ;
		//no contact
	}//type == MULTIPL


	//---set call_id and cseq
	osip_call_id_t *callid;
	osip_cseq_t *cseq;
	char *num;
	char  *cidrand;
	char *register_callid_number ;

	/* call-id is always the same for REGISTRATIONS */
	i = osip_call_id_init(&callid);
	if (i!=0) 
		goto si2perror2;
	cidrand = osip_strdup("BF9598C48B184EBBAFADFE527EED8186") ;
	osip_call_id_set_number(callid, cidrand);
	register_callid_number = cidrand;

	osip_call_id_set_host(callid, osip_strdup("SI2P.COM"));
	request->call_id = callid;

	//cseq
	i = osip_cseq_init(&cseq);
	if (i!=0) 
		goto si2perror2 ;
	num = osip_strdup("1");
	osip_cseq_set_number(cseq, num);
	osip_cseq_set_method(cseq, osip_strdup("REGISTER"));
	request->cseq = cseq;
	 
	/*the Max-Forward header */
	osip_message_set_max_forwards(request, "5"); /* a UA should start a request with 70 */

	/* the via header */
	char tmp[200];
	unsigned int branch;
	branch=osip_build_random_number();
    snprintf(tmp, 200, "SIP/2.0/%s %s:%s;rport;branch=z9hG4bK%u", "UDP",
	      localip,
	      localport,
	      branch );

    osip_message_set_via(request, tmp);

	/* the UA header */
	osip_message_set_user_agent(request, user_agent);

	/*  the expires header */
	char exp[10]; /* MUST never be ouside 1 and 3600 */
	snprintf(exp, 9, "%i", expires);
	osip_message_set_expires(request, exp);
	osip_message_set_content_length(request, "0");


	/*** then must wake up osip ***/
	i = osip_transaction_init(&transaction,
		       NICT,
		       adosip->j_osip,
		       request);
	if (i!=0)
		goto si2perror2 ;

	//jr->r_last_tr = transaction;

	/* send REGISTER */
	
	i = osip_message_to_str(request, &message1, &len);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1)	osip_free(message1) ;
//	printf("SEND======================================>>\n") ;
//	printf(message1) ;

	sipevent = osip_new_outgoing_sipmessage(request);
	sipevent->transactionid =  transaction->transactionid;
	osip_message_force_update(request);
  
	osip_transaction_add_event(transaction, sipevent);

	adosip->ThreadWakeUp();
	return 0;

si2perror1:
	if(request != NULL)osip_message_free(request);
	return -1 ;
si2perror2:
	if(request != NULL)osip_message_free(request);
	return -1;
}
Esempio n. 4
0
int Node::SndUserRegisterRequest(char *aor ,const char *requri)
{
	char *message1;
	unsigned int len = 0;



	osip_transaction_t *transaction;
	osip_event_t *sipevent;
	osip_message_t *request;


	int i;

	i = osip_message_init(&request);
	if (i!=0) 
		goto si2perror1;
	/* prepare the request-line */
	osip_message_set_method(request, osip_strdup("REGISTER"));
	osip_message_set_version(request, osip_strdup("SIP/2.0"));
	osip_message_set_status_code(request, 0);
	osip_message_set_reason_phrase(request, NULL);

	//requset uri


	osip_uri_t *uri;

	i=osip_uri_init(&uri);
	if (i!=0) 
		goto si2perror2;
	i=osip_uri_parse(uri, requri);
	if (i!=0)
		goto si2perror2;

	osip_message_set_uri(request , uri) ;


	//to from ,no contact
	osip_message_set_to(request,aor);
	osip_message_set_from(request,aor);

	osip_from_param_add(request->from,osip_strdup("user"),osip_strdup("user_query"));

	//---set call_id and cseq
	osip_call_id_t *callid;
	osip_cseq_t *cseq;
	char *num;
	char  *cidrand;
	char *register_callid_number ;

	/* call-id is always the same for REGISTRATIONS */
	i = osip_call_id_init(&callid);
	if (i!=0) 
		goto si2perror2;
	cidrand = osip_strdup("BF9598C48B184EBBAFADFE527EED8186") ;
	osip_call_id_set_number(callid, cidrand);
	register_callid_number = cidrand;

	osip_call_id_set_host(callid, osip_strdup("SI2P.COM"));
	request->call_id = callid;

	//cseq
	i = osip_cseq_init(&cseq);
	if (i!=0) 
		goto si2perror2 ;
	num = osip_strdup("1");
	osip_cseq_set_number(cseq, num);
	osip_cseq_set_method(cseq, osip_strdup("REGISTER"));
	request->cseq = cseq;
	 
	/*the Max-Forward header */
	osip_message_set_max_forwards(request, "5"); /* a UA should start a request with 70 */

	/* the via header */
	char tmp[200];
	unsigned int branch;
	branch=osip_build_random_number();
    snprintf(tmp, 200, "SIP/2.0/%s %s:%s;rport;branch=z9hG4bK%u", "UDP",
	      localip,
	      localport,
	      branch );

    osip_message_set_via(request, tmp);

	/* the UA header */
	osip_message_set_user_agent(request, user_agent);

	/*  the expires header */
	char exp[10]; /* MUST never be ouside 1 and 3600 */
	snprintf(exp, 9, "%i", expires);
	osip_message_set_expires(request, exp);
	osip_message_set_content_length(request, "0");


	/*** then must wake up osip ***/
	i = osip_transaction_init(&transaction,
		       NICT,
		       adosip->j_osip,
		       request);
	if (i!=0)
		goto si2perror2 ;

	/* send REGISTER */
	i = osip_message_to_str(request, &message1, &len);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1) osip_free(message1) ;

	sipevent = osip_new_outgoing_sipmessage(request);
	sipevent->transactionid =  transaction->transactionid;
	osip_message_force_update(request);
  
	osip_transaction_add_event(transaction, sipevent);

	adosip->ThreadWakeUp();
	return 0;

si2perror1:
	if(request != NULL)osip_message_free(request);
	return -1 ;
si2perror2:
	if(request != NULL)osip_message_free(request);
	return -1;
	return 0;
}
Esempio n. 5
0
int
Node::SndUserRegisterRequest(REG_TYPE type,
				uinfo_t *user_info,
				const char *registrar,
				int expires)
{
	char *message1;
	unsigned int len = 0;

	osip_message_t *request;
	osip_event_t *sipevent;
	osip_transaction_t *transaction;
	osip_uri_t *uri;
	
	char *tmp_uri;
	int i;

	i=osip_message_init(&request);
	if(i!=0)
		goto si2perror1;
	osip_message_set_method(request,strdup("REGISTER"));
	osip_message_set_version(request,strdup("SIP/2.0"));
	osip_message_set_status_code(request,0);
	osip_message_set_reason_phrase(request,NULL);

	i=osip_uri_init(&uri);
	if(i!=0)
		goto si2perror2;
	i=osip_uri_parse(uri,osip_strdup(registrar));
	osip_message_set_uri(request,uri);

	//*set to,from and header
	if(type==USER_REGISTRATION)
	{	
		char *dest1;
		char *dest2;
		
		osip_uri_to_str(user_info->aor->uri,&dest1);
		i=osip_message_set_to(request,dest1);
		i=osip_message_set_from(request,dest1);
		if(dest1)	osip_free(dest1) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("user_registration"));

		osip_contact_to_str(user_info->bindings->contact,&dest2);
		i=osip_message_set_contact(request,osip_strdup(dest2));
		if(dest2)	osip_free(dest2) ;
	}

	if(type == RED_REGISTER)
	{
		char *dest1;
		char *dest2;
		osip_uri_to_str(user_info->aor->uri,&dest1);
		i=osip_message_set_to(request,dest1);
		i=osip_message_set_from(request,dest1);
		if(dest1)	osip_free(dest1) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("red_register"));

		osip_contact_to_str(user_info->bindings->contact,&dest2);
		i=osip_message_set_contact(request,osip_strdup(dest2));
		if(dest2)	osip_free(dest2) ;
	}

	if(type==TRANSFER_REGISTRATION)
	{
		char *dest1;
		char *dest2;
		//string tmp_uri;
		osip_uri_to_str(user_info->aor->uri,&dest1);
		osip_message_set_to(request,dest1);
		if(dest1)	osip_free(dest1) ;

		tmp_uri = ChordId2Uri(chordId,false);
		osip_message_set_from(request,tmp_uri);
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("transfer_registration"));
		if(tmp_uri)	osip_free(tmp_uri) ;

		osip_contact_to_str(user_info->bindings->contact,&dest2);
		osip_message_set_contact(request,dest2);
		if(dest2)	osip_free(dest2) ;
	}
	if(type==THIRD_PARTY)
	{
		//todo
	}
		//---set call_id and cseq
	osip_call_id_t *callid;
	osip_cseq_t *cseq;
	char *num;
	char  *cidrand;
	char *register_callid_number ;

	//* call-id is always the same for REGISTRATIONS 
	i = osip_call_id_init(&callid);
	if (i!=0) 
		goto si2perror2;
	cidrand = osip_strdup("BF9598C48B184EBBAFADFE527EED8186") ;
	osip_call_id_set_number(callid, cidrand);
	register_callid_number = cidrand;

	osip_call_id_set_host(callid, osip_strdup("SI2P.COM"));
	request->call_id = callid;

	//cseq
	i = osip_cseq_init(&cseq);
	if (i!=0) 
		goto si2perror2 ;
	num = osip_strdup("1");
	osip_cseq_set_number(cseq, num);
	osip_cseq_set_method(cseq, osip_strdup("REGISTER"));
	request->cseq = cseq;
	 
	//*the Max-Forward header 
	osip_message_set_max_forwards(request, "5"); //* a UA should start a request with 70 

	//* the via header 
	char tmp[200];
    snprintf(tmp, 200, "SIP/2.0/%s %s:%s;rport;branch=z9hG4bK%u", "UDP",
	      localip,
	      localport,
	      via_branch_new_random() );

    osip_message_set_via(request, tmp);

	//* the UA header 
	osip_message_set_user_agent(request, user_agent);

	//*  the expires header 
	char exp[10]; //* MUST never be ouside 1 and 3600 
	snprintf(exp, 9, "%i", expires);
	osip_message_set_expires(request, exp);
	osip_message_set_content_length(request, "0");


	//*** then must wake up osip 
	i = osip_transaction_init(&transaction,
		       NICT,
		       adosip->j_osip,
		       request);
	if (i!=0)
		goto si2perror3 ;

	//* send REGISTER 
	i = osip_message_to_str(request, &message1, &len);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1) osip_free(message1) ;
//	printf("SEND======================================>>\n") ;
//	printf(message1) ;
	sipevent = osip_new_outgoing_sipmessage(request);
	sipevent->transactionid =  transaction->transactionid;
	osip_message_force_update(request);
  
	osip_transaction_add_event(transaction, sipevent);

	adosip->ThreadWakeUp();
	return 0;

si2perror1:
	
	return -1;

si2perror2:
	if(request!=NULL)
		osip_message_free(request);
	return -1;

si2perror3:
	if(transaction!=NULL)
		osip_message_free(request);
	return -1;


}
Esempio n. 6
0
/* It is RECOMMENDED to only cancel INVITE request */
int
generating_cancel (osip_message_t ** dest, osip_message_t * request_cancelled)
{
  int i;
  osip_message_t *request;

  i = osip_message_init (&request);
  if (i != 0)
    return i;

  /* prepare the request-line */
  osip_message_set_method (request, osip_strdup ("CANCEL"));
  osip_message_set_version (request, osip_strdup ("SIP/2.0"));
  osip_message_set_status_code (request, 0);
  osip_message_set_reason_phrase (request, NULL);

  i = osip_uri_clone (request_cancelled->req_uri, &(request->req_uri));
  if (i != 0)
    {
      osip_message_free (request);
      *dest = NULL;
      return i;
    }

  i = osip_to_clone (request_cancelled->to, &(request->to));
  if (i != 0)
    {
      osip_message_free (request);
      *dest = NULL;
      return i;
    }
  i = osip_from_clone (request_cancelled->from, &(request->from));
  if (i != 0)
    {
      osip_message_free (request);
      *dest = NULL;
      return i;
    }

  /* set the cseq and call_id header */
  i = osip_call_id_clone (request_cancelled->call_id, &(request->call_id));
  if (i != 0)
    {
      osip_message_free (request);
      *dest = NULL;
      return i;
    }
  i = osip_cseq_clone (request_cancelled->cseq, &(request->cseq));
  if (i != 0)
    {
      osip_message_free (request);
      *dest = NULL;
      return i;
    }
  osip_free (request->cseq->method);
  request->cseq->method = osip_strdup ("CANCEL");
  if (request->cseq->method == NULL)
    {
      osip_message_free (request);
      *dest = NULL;
      return OSIP_NOMEM;
    }

  /* copy ONLY the top most Via Field (this method is also used by proxy) */
  {
    osip_via_t *via;
    osip_via_t *via2;

    i = osip_message_get_via (request_cancelled, 0, &via);
    if (i < 0)
      {
        osip_message_free (request);
        *dest = NULL;
        return i;
      }
    i = osip_via_clone (via, &via2);
    if (i != 0)
      {
        osip_message_free (request);
        *dest = NULL;
        return i;
      }
    osip_list_add (&request->vias, via2, -1);
  }

  /* add the same route-set than in the previous request */
  {
    int pos = 0;
    osip_route_t *route;
    osip_route_t *route2;

    while (!osip_list_eol (&request_cancelled->routes, pos))
      {
        route = (osip_route_t *) osip_list_get (&request_cancelled->routes, pos);
        i = osip_route_clone (route, &route2);
        if (i != 0)
          {
            osip_message_free (request);
            *dest = NULL;
            return i;
          }
        osip_list_add (&request->routes, route2, -1);
        pos++;
      }
  }

  osip_message_set_max_forwards (request, "70");        /* a UA should start a request with 70 */
  osip_message_set_user_agent (request, eXosip.user_agent);

  *dest = request;
  return OSIP_SUCCESS;
}
Esempio n. 7
0
int
_eXosip_build_request_within_dialog (osip_message_t ** dest,
                                     const char *method,
                                     osip_dialog_t * dialog, const char *transport)
{
  int i;
  osip_message_t *request;
  char locip[65];
  char firewall_ip[65];
  char firewall_port[10];

  *dest = NULL;

  if (dialog == NULL)
    return OSIP_BADPARAMETER;

  if (eXosip.eXtl == NULL)
    return OSIP_NO_NETWORK;

  if (eXosip.eXtl->tl_get_masquerade_contact != NULL)
    {
      eXosip.eXtl->tl_get_masquerade_contact (firewall_ip, sizeof (firewall_ip),
                                              firewall_port,
                                              sizeof (firewall_port));
    }

  i = osip_message_init (&request);
  if (i != 0)
    return i;

  if (dialog->remote_contact_uri == NULL)
    {
      /* this dialog is probably not established! or the remote UA
         is not compliant with the latest RFC
       */
      osip_message_free (request);
      return OSIP_SYNTAXERROR;
    }


  memset (locip, '\0', sizeof (locip));
  eXosip_guess_ip_for_via (eXosip.eXtl->proto_family, locip, 49);
  if (locip[0] == '\0')
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: no default interface defined\n"));
      osip_message_free (request);
      return OSIP_NO_NETWORK;
    }

  /* prepare the request-line */
  request->sip_method = osip_strdup (method);
  if (request->sip_method == NULL)
    {
      osip_message_free (request);
      return OSIP_NOMEM;
    }
  request->sip_version = osip_strdup ("SIP/2.0");
  if (request->sip_version == NULL)
    {
      osip_message_free (request);
      return OSIP_NOMEM;
    }
  request->status_code = 0;
  request->reason_phrase = NULL;

  /* and the request uri???? */
  if (osip_list_eol (&dialog->route_set, 0))
    {
      /* The UAC must put the remote target URI (to field) in the req_uri */
      i = osip_uri_clone (dialog->remote_contact_uri->url, &(request->req_uri));
      if (i != 0)
        {
          osip_message_free (request);
          return i;
        }
  } else
    {
      /* fill the request-uri, and the route headers. */
      i = dialog_fill_route_set (dialog, request);
      if (i != 0)
        {
          osip_message_free (request);
          return i;
        }
    }

  /* To and From already contains the proper tag! */
  i = osip_to_clone (dialog->remote_uri, &(request->to));
  if (i != 0)
    {
      osip_message_free (request);
      return i;
    }
  i = osip_from_clone (dialog->local_uri, &(request->from));
  if (i != 0)
    {
      osip_message_free (request);
      return i;
    }

  /* set the cseq and call_id header */
  osip_message_set_call_id (request, dialog->call_id);

  if (0 == strcmp ("ACK", method))
    {
      osip_cseq_t *cseq;
      char *tmp;

      i = osip_cseq_init (&cseq);
      if (i != 0)
        {
          osip_message_free (request);
          return i;
        }
      tmp = osip_malloc (20);
      if (tmp == NULL)
        {
          osip_message_free (request);
          return OSIP_NOMEM;
        }
      sprintf (tmp, "%i", dialog->local_cseq);
      osip_cseq_set_number (cseq, tmp);
      osip_cseq_set_method (cseq, osip_strdup (method));
      request->cseq = cseq;
  } else
    {
      osip_cseq_t *cseq;
      char *tmp;

      i = osip_cseq_init (&cseq);
      if (i != 0)
        {
          osip_message_free (request);
          return i;
        }
      dialog->local_cseq++;     /* we should we do that?? */
      tmp = osip_malloc (20);
      if (tmp == NULL)
        {
          osip_message_free (request);
          return OSIP_NOMEM;
        }
      sprintf (tmp, "%i", dialog->local_cseq);
      osip_cseq_set_number (cseq, tmp);
      osip_cseq_set_method (cseq, osip_strdup (method));
      request->cseq = cseq;
    }

  /* always add the Max-Forward header */
  osip_message_set_max_forwards (request, "70");        /* a UA should start a request with 70 */


  i = _eXosip_request_add_via (request, transport, locip);
  if (i != 0)
    {
      osip_message_free (request);
      return i;
    }

  /* add specific headers for each kind of request... */

  {
    char contact[200];

    if (firewall_ip[0] != '\0')
      {
        char *c_address = request->req_uri->host;

        struct addrinfo *addrinfo;
        struct __eXosip_sockaddr addr;

        i =
          eXosip_get_addrinfo (&addrinfo, request->req_uri->host, 5060,
                               IPPROTO_UDP);
        if (i == 0)
          {
            memcpy (&addr, addrinfo->ai_addr, addrinfo->ai_addrlen);
            eXosip_freeaddrinfo (addrinfo);
            c_address = inet_ntoa (((struct sockaddr_in *) &addr)->sin_addr);
            OSIP_TRACE (osip_trace
                        (__FILE__, __LINE__, OSIP_INFO1, NULL,
                         "eXosip: here is the resolved destination host=%s\n",
                         c_address));
          }

        if (eXosip_is_public_address (c_address))
          {
            sprintf (contact, "<sip:%s@%s:%s>",
                     dialog->local_uri->url->username, firewall_ip, firewall_port);
        } else
          {
            sprintf (contact, "<sip:%s@%s:%s>",
                     dialog->local_uri->url->username, locip, firewall_port);
          }
    } else
      {
        sprintf (contact, "<sip:%s@%s:%s>", dialog->local_uri->url->username,
                 locip, firewall_port);
      }
    osip_message_set_contact (request, contact);
    /* Here we'll add the supported header if it's needed! */
    /* the require header must be added by the upper layer if needed */
  }

  if (0 == strcmp ("NOTIFY", method))
    {
  } else if (0 == strcmp ("INFO", method))
    {

  } else if (0 == strcmp ("OPTIONS", method))
    {
      osip_message_set_accept (request, "application/sdp");
  } else if (0 == strcmp ("ACK", method))
    {
      /* The ACK MUST contains the same credential than the INVITE!! */
      /* TODO... */
    }

  osip_message_set_user_agent (request, eXosip.user_agent);
  /*  else if ... */
  *dest = request;
  return OSIP_SUCCESS;
}
Esempio n. 8
0
/* 
   method is the type of request. ("INVITE", "REGISTER"...)
   to is the remote target URI
   transport is either "TCP" or "UDP" (by now, only UDP is implemented!)
*/
int
generating_request_out_of_dialog (osip_message_t ** dest, const char *method,
                                  const char *to, const char *transport,
                                  const char *from, const char *proxy)
{
  /* Section 8.1:
     A valid request contains at a minimum "To, From, Call-iD, Cseq,
     Max-Forwards and Via
   */
  int i;
  osip_message_t *request;
  char locip[65];
  int doing_register;
  char *register_callid_number = NULL;

  *dest = NULL;

  if (eXosip.eXtl == NULL)
    return OSIP_NO_NETWORK;

  /*guess the local ip since req uri is known */
  memset (locip, '\0', sizeof (locip));
  eXosip_guess_ip_for_via (eXosip.eXtl->proto_family, locip, 49);
  if (locip[0] == '\0')
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: no default interface defined\n"));
      return OSIP_NO_NETWORK;
    }

  i = osip_message_init (&request);
  if (i != 0)
    return i;

  /* prepare the request-line */
  osip_message_set_method (request, osip_strdup (method));
  osip_message_set_version (request, osip_strdup ("SIP/2.0"));
  osip_message_set_status_code (request, 0);
  osip_message_set_reason_phrase (request, NULL);

  doing_register = 0 == strcmp ("REGISTER", method);

  if (doing_register)
    {
      osip_uri_init (&(request->req_uri));
      i = osip_uri_parse (request->req_uri, proxy);
      if (i != 0)
        {
          osip_message_free (request);
          return i;
        }
      i = osip_message_set_to (request, from);
      if (i != 0 || request->to == NULL)
        {
          if (i >= 0)
            i = OSIP_SYNTAXERROR;
          osip_message_free (request);
          return i;
        }
  } else
    {
      /* in any cases except REGISTER: */
      i = osip_message_set_to (request, to);
      if (i != 0 || request->to == NULL)
        {
          if (i >= 0)
            i = OSIP_SYNTAXERROR;
          OSIP_TRACE (osip_trace
                      (__FILE__, __LINE__, OSIP_ERROR, NULL,
                       "ERROR: callee address does not seems to be a sipurl: %s\n",
                       to));
          osip_message_free (request);
          return i;
        }

      /* REMOVE ALL URL PARAMETERS from to->url headers and add them as headers */
      if (request->to != NULL && request->to->url != NULL)
        {
          osip_uri_t *url = request->to->url;
          while (osip_list_size (&url->url_headers) > 0)
            {
              osip_uri_header_t *u_header;
              u_header = (osip_uri_param_t *) osip_list_get (&url->url_headers, 0);
              if (u_header == NULL)
                break;

              if (osip_strcasecmp (u_header->gname, "from") == 0)
                {
              } else if (osip_strcasecmp (u_header->gname, "to") == 0)
                {
              } else if (osip_strcasecmp (u_header->gname, "call-id") == 0)
                {
              } else if (osip_strcasecmp (u_header->gname, "cseq") == 0)
                {
              } else if (osip_strcasecmp (u_header->gname, "via") == 0)
                {
              } else if (osip_strcasecmp (u_header->gname, "contact") == 0)
                {
              } else
                osip_message_set_header (request, u_header->gname,
                                         u_header->gvalue);
              osip_list_remove (&url->url_headers, 0);
              osip_uri_param_free (u_header);
            }
        }

      if (proxy != NULL && proxy[0] != 0)
        {                       /* equal to a pre-existing route set */
          /* if the pre-existing route set contains a "lr" (compliance
             with bis-08) then the req_uri should contains the remote target
             URI */
          osip_uri_param_t *lr_param;
          osip_route_t *o_proxy;

          osip_route_init (&o_proxy);
          i = osip_route_parse (o_proxy, proxy);
          if (i != 0)
            {
              osip_route_free (o_proxy);
              osip_message_free (request);
              return i;
            }

          osip_uri_uparam_get_byname (o_proxy->url, "lr", &lr_param);
          if (lr_param != NULL) /* to is the remote target URI in this case! */
            {
              osip_uri_clone (request->to->url, &(request->req_uri));
              /* "[request] MUST includes a Route header field containing
                 the route set values in order." */
              osip_list_add (&request->routes, o_proxy, 0);
          } else
            /* if the first URI of route set does not contain "lr", the req_uri
               is set to the first uri of route set */
            {
              request->req_uri = o_proxy->url;
              o_proxy->url = NULL;
              osip_route_free (o_proxy);
              /* add the route set */
              /* "The UAC MUST add a route header field containing
                 the remainder of the route set values in order.
                 The UAC MUST then place the remote target URI into
                 the route header field as the last value
               */
              osip_message_set_route (request, to);
            }
      } else                    /* No route set (outbound proxy) is used */
        {
          /* The UAC must put the remote target URI (to field) in the req_uri */
          i = osip_uri_clone (request->to->url, &(request->req_uri));
          if (i != 0)
            {
              osip_message_free (request);
              return i;
            }
        }
    }

  /* set To and From */
  i = osip_message_set_from (request, from);
  if (i != 0 || request->from == NULL)
    {
      if (i >= 0)
        i = OSIP_SYNTAXERROR;
      osip_message_free (request);
      return i;
    }

  /* add a tag */
  osip_from_set_tag (request->from, osip_from_tag_new_random ());

  /* set the cseq and call_id header */
  {
    osip_call_id_t *callid;
    osip_cseq_t *cseq;
    char *num;
    char *cidrand;

    /* call-id is always the same for REGISTRATIONS */
    i = osip_call_id_init (&callid);
    if (i != 0)
      {
        osip_message_free (request);
        return i;
      }
    cidrand = osip_call_id_new_random ();
    osip_call_id_set_number (callid, cidrand);
    if (doing_register)
      register_callid_number = cidrand;

    request->call_id = callid;

    i = osip_cseq_init (&cseq);
    if (i != 0)
      {
        osip_message_free (request);
        return i;
      }
    num = osip_strdup (doing_register ? "1" : "20");
    osip_cseq_set_number (cseq, num);
    osip_cseq_set_method (cseq, osip_strdup (method));
    request->cseq = cseq;

    if (cseq->method == NULL || cseq->number == NULL)
      {
        osip_message_free (request);
        return OSIP_NOMEM;
      }
  }

  i = _eXosip_request_add_via (request, transport, locip);
  if (i != 0)
    {
      osip_message_free (request);
      return i;
    }

  /* always add the Max-Forward header */
  osip_message_set_max_forwards (request, "70");        /* a UA should start a request with 70 */

  if (0 == strcmp ("REGISTER", method))
    {
  } else if (0 == strcmp ("INFO", method))
    {
  } else if (0 == strcmp ("OPTIONS", method))
    {
      osip_message_set_accept (request, "application/sdp");
    }

  osip_message_set_user_agent (request, eXosip.user_agent);
  /*  else if ... */
  *dest = request;
  return OSIP_SUCCESS;
}
Esempio n. 9
0
int bSipRegisterBuild(osip_message_t **regMsgPtrPtr)
{
	static int gSeqNum = 1;
	int status;
	char *callidNumberStr = NULL;
	char *seqNumStr = NULL;
	osip_call_id_t *callidPtr;
	char temp[MESSAGE_ENTRY_MAX_LENGTH];
	char sipPort[MESSAGE_ENTRY_MAX_LENGTH];
	osip_cseq_t *cseqPtr;
	unsigned int number;
	osip_message_t     *regMsgPtr;
	char expires[10];

	if((status = osip_message_init(&regMsgPtr)) != 0){
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"Can't init message!\n"));
		return -1;
	}
	osip_message_set_method(regMsgPtr, osip_strdup("OPTIONS"));

	osip_uri_init(&(regMsgPtr->req_uri));
	if ( ( status = osip_uri_parse(regMsgPtr->req_uri, SIP_PROXY) ) != 0)
	{
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"uri parse failed!\n"));
		osip_message_free(regMsgPtr);
		return -1;
	}
	osip_message_set_version(regMsgPtr, osip_strdup("SIP/2.0"));
	osip_message_set_status_code(regMsgPtr, 0);
	osip_message_set_reason_phrase(regMsgPtr, NULL);

	osip_message_set_to(regMsgPtr, SIP_TO);
	osip_message_set_from(regMsgPtr, SIP_FROM);

	if((status = osip_call_id_init(&callidPtr)) != 0 ){
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"call id failed!\n"));
		osip_message_free(regMsgPtr);
		return -1;
	}
	callidNumberStr = (char *)osip_malloc(MAX_ADDR_STR);
	number = osip_build_random_number();
	sprintf(callidNumberStr,"%u",number);
	osip_call_id_set_number(callidPtr, callidNumberStr);

	osip_call_id_set_host(callidPtr, osip_strdup("10.1.1.63"));

	regMsgPtr->call_id = callidPtr;

	if((status = osip_cseq_init(&cseqPtr)) != 0 ){
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"seq init failed!\n"));
		osip_message_free(regMsgPtr);
		return -1;
	}
	gSeqNum++;
	seqNumStr = (char *)osip_malloc(MAX_ADDR_STR);
	sprintf(seqNumStr,"%i", gSeqNum);
	osip_cseq_set_number(cseqPtr, seqNumStr);
	osip_cseq_set_method(cseqPtr, osip_strdup("OPTIONS"));
	regMsgPtr->cseq = cseqPtr;

	osip_message_set_max_forwards(regMsgPtr, "70");

	sprintf(sipPort, "%i", SIP_PORT);
	sprintf(temp, "SIP/2.0/%s %s;branch=z9hG4bK%u", "UDP",LOCAL_IP,osip_build_random_number() );
	osip_message_set_via(regMsgPtr, temp);

	osip_message_set_contact(regMsgPtr, SIP_CONTACT);
	sprintf(expires, "%i", EXPIRES_TIME_INSECS);
	osip_message_set_expires(regMsgPtr, expires);

	osip_message_set_content_length(regMsgPtr, "0");

	osip_message_set_user_agent(regMsgPtr, "TotalView 1.0");

	AddSupportedMethods(regMsgPtr);
	*regMsgPtrPtr = regMsgPtr;
	return 0;
}