示例#1
0
//  对接收到的消息进行应答
int GB_Send_Reply(GB_CONNECT_STATE *gb_cons, osip_event_t * osip_event, int status_code)
{
	int ret = -1;
	osip_message_t *rsp = NULL;
	char *result = NULL;
	size_t length;

	if(gb_cons == NULL || osip_event == NULL)
	{
		return -1;
	}
	
	ret = gb_build_response_message(&rsp, NULL, status_code,osip_event->sip, NULL,NULL,0);
	if(ret != 0)
	{
		osip_message_free (rsp);
		printf("%s  line=%d\n",__FUNCTION__,__LINE__);
		return -1;
	}

	ret = osip_message_to_str(rsp, &result, &length);

	if (ret == -1) 
	{
		printf("ERROR: failed while printing message!\n");
		osip_message_free (rsp);
		return -1;
	}	
	
	GB_SocketSendData(gb_cons->connfd,inet_ntoa(gb_cons->remoteAddr.sin_addr), ntohs(gb_cons->remoteAddr.sin_port), result, length, 0);
	SN_FREE(result);
	osip_message_free(rsp);

	return 0;
}
示例#2
0
int GB_sipd_Keepalive(GB_CONNECT_STATE *gb_cons, gb_Keepalive_Struct *cmd)
{
	osip_message_t *reg = NULL;
	PRM_GB_SIPD_CFG gb_cfg;
	char to[GB_URI_MAX_LEN] = {0};
	char from[GB_URI_MAX_LEN] = {0};
	char proxy[GB_URI_MAX_LEN] = {0};
	int ret;
	char *result = NULL;
	size_t length;
	char localip[20];
	char sipserver_ip[20] = {0};
	GB_Record_Node record;
	
	SN_MEMSET(&gb_cfg, 0, sizeof(gb_cfg));	
	SN_MEMSET(&record, 0, sizeof(record));
	
	GB_Get_GBCfg(&gb_cfg);

	SN_MEMSET(localip,0,sizeof(localip));
	SN_STRCPY(localip,sizeof(localip),GB_Get_LocalIP());
	
	SN_SPRINTF(sipserver_ip,sizeof(sipserver_ip),"%d.%d.%d.%d",
		gb_cfg.sipserver_ip[0],gb_cfg.sipserver_ip[1],gb_cfg.sipserver_ip[2],gb_cfg.sipserver_ip[3]);

	SN_SPRINTF(from, GB_URI_MAX_LEN, "sip:%s@%s", gb_cfg.deviceID, localip);
	SN_SPRINTF(to, GB_URI_MAX_LEN, "sip:%s@%s", gb_cfg.sipserver_ID, sipserver_ip);
	SN_SPRINTF(proxy, GB_URI_MAX_LEN, "sip:%s@%s:%d", gb_cfg.sipserver_ID,sipserver_ip, gb_cfg.sipserver_port);
	
	ret = gb_generating_MESSAGE(&reg, Transport_Str[gb_cons->transfer_protocol], from, to, proxy, 
					localip,gb_cfg.local_port, gb_cons->local_cseq, (void *)cmd, gb_CommandType_KeepAlive);
		
	if (ret < 0)
	{
		osip_message_free (reg);
		printf("%s  line=%d\n",__FUNCTION__,__LINE__);
		return -1;
	}

	record.cmd = gb_CommandType_KeepAlive;
	osip_call_id_clone(reg->call_id,&(record.call_id));
	
	GB_Add_Record_Node(gb_cons, &record);
	
	ret = osip_message_to_str(reg, &result, &length);

	if (ret == -1) 
	{
		printf("ERROR: failed while printing message!\n");
		osip_message_free (reg);
		return -1;
	}	
	gb_cons->local_cseq++;
	GB_SocketSendData(gb_cons->connfd,inet_ntoa(gb_cons->remoteAddr.sin_addr), ntohs(gb_cons->remoteAddr.sin_port), result, length, 0);
	SN_FREE(result);
	osip_message_free(reg);
	
	return 0;
}
void aiptv_log_message(osip_message_t *sip_message)
{
char *dest = NULL;
size_t length = 0;
osip_message_to_str (sip_message, &dest, &length);
aiptv_log(dest);
osip_free(dest);
}
示例#4
0
/*
 * SIP_GEN_RESPONSE
 *
 * send an proxy generated response back to the client.
 * Only errors are reported from the proxy itself.
 *  code =  SIP result code to deliver
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_gen_response(sip_ticket_t *ticket, int code) {
   osip_message_t *response;
   int sts;
   osip_via_t *via;
   int port;
   char *buffer;
   struct in_addr addr;

   /* create the response template */
   if ((response=msg_make_template_reply(ticket, code))==NULL) {
      ERROR("sip_gen_response: error in msg_make_template_reply");
      return STS_FAILURE;
   }

   /* we must check if first via has x.x.x.x address. If not, we must resolve it */
   osip_message_get_via (response, 0, &via);
   if (via == NULL)
   {
      ERROR("sip_gen_response: Cannot send response - no via field");
      return STS_FAILURE;
   }


   /* name resolution */
   if (utils_inet_aton(via->host, &addr) == 0)
   {
      /* need name resolution */
      DEBUGC(DBCLASS_DNS,"resolving name:%s",via->host);
      sts = get_ip_by_host(via->host, &addr);
      if (sts == STS_FAILURE) {
         DEBUGC(DBCLASS_PROXY, "sip_gen_response: cannot resolve via [%s]",
                via->host);
         return STS_FAILURE;
      }
   }   

   sts = osip_message_to_str(response, &buffer);
   if (sts != 0) {
      ERROR("sip_gen_response: msg_2char failed");
      return STS_FAILURE;
   }


   if (via->port) {
      port=atoi(via->port);
   } else {
      port=SIP_PORT;
   }
   printsip("Sip:sip_gen_response(), Error.\r\n");
   /* send to destination */
   sipsock_send(addr, port, ticket->protocol, buffer, strlen(buffer));

   /* free the resources */
   osip_message_free(response);
   osip_free(buffer);
   return STS_SUCCESS;
}
示例#5
0
int GB_sipd_register(GB_CONNECT_STATE *gb_cons, int flag)
{
	osip_message_t *reg = NULL;
	PRM_GB_SIPD_CFG gb_cfg;
	char from[GB_URI_MAX_LEN] = {0};
	char proxy[GB_URI_MAX_LEN] = {0};
	int ret;
	char *result = NULL;
	size_t length;
	char localip[20];
	char sipserver_ip[20] = {0};
	
	SN_MEMSET(&gb_cfg, 0, sizeof(gb_cfg));

	GB_Get_GBCfg(&gb_cfg);

	SN_MEMSET(localip,0,sizeof(localip));
	SN_STRCPY(localip,sizeof(localip),GB_Get_LocalIP());
	
	SN_SPRINTF(sipserver_ip,sizeof(sipserver_ip),"%d.%d.%d.%d",
		gb_cfg.sipserver_ip[0],gb_cfg.sipserver_ip[1],gb_cfg.sipserver_ip[2],gb_cfg.sipserver_ip[3]);

	SN_SPRINTF(from, GB_URI_MAX_LEN, "sip:%s@%s", gb_cfg.deviceID, localip);
	SN_SPRINTF(proxy, GB_URI_MAX_LEN, "sip:%s@%s:%d", gb_cfg.sipserver_ID,sipserver_ip, gb_cfg.sipserver_port);

	if(flag == 0)  // 注册
	{
		ret = gb_generating_register(&reg, Transport_Str[gb_cons->transfer_protocol], from, proxy, NULL, gb_cfg.register_period, localip,gb_cfg.local_port, gb_cons->local_cseq);
	}
	else  //  注销
	{
		ret = gb_generating_register(&reg, Transport_Str[gb_cons->transfer_protocol], from, proxy, NULL, 0, localip,gb_cfg.local_port, gb_cons->local_cseq);	
	}
	
	if (ret < 0)
	{
		osip_message_free (reg);
		return -1;
	}
	
	//printf("gb_cons->callID:%s\n", gb_cons->callID);
	ret = osip_message_to_str(reg, &result, &length);
	if (ret == -1) 
	{
		printf("ERROR: failed while printing message!\n");
		osip_message_free (reg);
		return -1;
	}	
	gb_cons->local_cseq++;
	GB_SocketSendData(gb_cons->connfd,inet_ntoa(gb_cons->remoteAddr.sin_addr), ntohs(gb_cons->remoteAddr.sin_port), result, length, 0);
	SN_FREE(result);
	osip_message_free(reg);
	
	return 0;
}
示例#6
0
void JoiningState::onJoinRedirect(
					Node* node,
					osip_transaction_t * tr ,
					osip_message *response)
{
	char *message;
	size_t length = 0;
	int i = osip_message_to_str(response, &message, &length);
	node->LogStream("RECEIVE<<======================================\n") ;
	node->LogStream(message) ;
//	printf("RECEIVE<<======================================\n") ;
//	printf(message) ;

	/* to , from , contact are same as orig_request ;
	req_uri is the contact of the response */
	ChordId to(
			atoi(tr->orig_request->to->url->username),
			node->getConstants(),
			tr->orig_request->to->url->host,
			tr->orig_request->to->url->port
			) ;
	ChordId from(
			atoi(tr->orig_request->from->url->username),
			node->getConstants(),
			tr->orig_request->from->url->host,
			tr->orig_request->from->url->port
			) ;

		
	osip_contact_t *osip_contact ;
	osip_message_get_contact(response,0,&osip_contact) ;
	if(!osip_contact)	
	{return ;}//error
	ChordId contact(
		atoi(osip_contact->url->username),
		node->getConstants(),
		osip_contact->url->host,
		osip_contact->url->port
		) ;


	ChordId chordId = node->getChordId() ;
	//******************************


	node->SndRegister(JOIN,contact,to,chordId) ; 	
}
示例#7
0
int SendMsg(osip_transaction_t *tr,osip_message_t *sip, char *host,int port, int out_socket)
{
	int len = 0;
	char *msgP;
	int msgLen;
	int i;
	int status;

	printf("SendMsg\n");

	if((i = osip_message_to_str(sip, &msgP, &msgLen)) != 0){
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"failed to convert message\n"));
		return -1;
	}
	if(!networkMsgSend(sipSock,msgP,strlen(msgP),host,5080))
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_INFO1,NULL,"Time: Udp message sent: \n%s\n",msgP));

	return 0;
}
示例#8
0
static int
tcp_tl_send_message(osip_transaction_t * tr, osip_message_t * sip, char *host,
                    int port, int out_socket)
{
  size_t length = 0;
  char *message;
  int i;

  if (host == NULL)
    {
      host = sip->req_uri->host;
      if (sip->req_uri->port != NULL)
        port = osip_atoi (sip->req_uri->port);
      else
        port = 5060;
    }

  /* remove preloaded route if there is no tag in the To header
   */
    {
      osip_route_t *route=NULL;
      osip_generic_param_t *tag=NULL;
      osip_message_get_route (sip, 0, &route);
      
      osip_to_get_tag (sip->to, &tag);
      if (tag==NULL && route != NULL && route->url != NULL)
	{
	  osip_list_remove(&sip->routes, 0);
	}
      i = osip_message_to_str (sip, &message, &length);
      if (tag==NULL && route != NULL && route->url != NULL)
	{
	  osip_list_add(&sip->routes, route, 0);
	}
    }

  if (i != 0 || length <= 0)
    {
      return -1;
    }

  /* Step 1: find existing socket to send message */
  if (out_socket <= 0)
    {
      out_socket = _tcp_tl_find_socket (host, port);

      /* Step 2: create new socket with host:port */
      if (out_socket <= 0)
        {
          out_socket = _tcp_tl_connect_socket (host, port);
        }

      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,
                              "Message sent: \n%s (to dest=%s:%i)\n",
                              message, host, port));
  } else
    {
      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,
                              "Message sent: \n%s (reusing REQUEST connection)\n",
                              message));
    }

  if (out_socket <= 0)
    {
      return -1;
    }


  if (0 > send (out_socket, (const void *) message, length, 0))
    {
#ifdef WIN32
      if (WSAECONNREFUSED == WSAGetLastError ())
#else
      if (ECONNREFUSED == errno)
#endif
        {
          /* This can be considered as an error, but for the moment,
             I prefer that the application continue to try sending
             message again and again... so we are not in a error case.
             Nevertheless, this error should be announced!
             ALSO, UAS may not have any other options than retry always
             on the same port.
           */
          osip_free (message);
          return 1;
      } else
        {
          /* SIP_NETWORK_ERROR; */
#if !defined(_WIN32_WCE)
          OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,
                                  "TCP error: \n%s\n", strerror (errno)));
#endif
          osip_free (message);
          return -1;
        }
    }

  osip_free (message);
  return 0;
}
示例#9
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;
}
示例#10
0
static int
udp_tl_send_message (osip_transaction_t * tr, osip_message_t * sip, char *host,
                     int port, int out_socket)
{
  int len = 0;
  size_t length = 0;
  struct addrinfo *addrinfo;
  struct __eXosip_sockaddr addr;
  char *message;
  char *crypt_date;
  char ipbuf[INET6_ADDRSTRLEN];
  int i;

  if (udp_socket <= 0)
    return -1;

  if (host == NULL)
    {
      host = sip->req_uri->host;
      if (sip->req_uri->port != NULL)
        port = osip_atoi (sip->req_uri->port);
      else
        port = 5060;
    }

  eXtl_update_local_target (sip);

  i = -1;
#ifndef MINISIZE
  if (tr != NULL && tr->record.name[0] != '\0'
      && tr->record.srventry[0].srv[0] != '\0')
    {
      /* always choose the first here.
         if a network error occur, remove first entry and
         replace with next entries.
       */
      osip_srv_entry_t *srv;
      int n = 0;
      for (srv = &tr->record.srventry[0];
           n < 10 && tr->record.srventry[0].srv[0]; srv = &tr->record.srventry[0])
        {
          i = eXosip_get_addrinfo (&addrinfo, srv->srv, srv->port, IPPROTO_UDP);
          if (i == 0)
            {
              host = srv->srv;
              port = srv->port;
              break;
            }
          memmove (&tr->record.srventry[0], &tr->record.srventry[1],
                   9 * sizeof (osip_srv_entry_t));
          memset (&tr->record.srventry[9], 0, sizeof (osip_srv_entry_t));
          i = -1;
          /* copy next element */
          n++;
        }
    }
#endif

  /* if SRV was used, distination may be already found */
  if (i != 0)
    {
      i = eXosip_get_addrinfo (&addrinfo, host, port, IPPROTO_UDP);
    }

  if (i != 0)
    {
      return -1;
    }

  memcpy (&addr, addrinfo->ai_addr, addrinfo->ai_addrlen);
  len = addrinfo->ai_addrlen;

  eXosip_freeaddrinfo (addrinfo);

  /* remove preloaded route if there is no tag in the To header
   */
  {
    osip_route_t *route = NULL;
    osip_generic_param_t *tag = NULL;
    osip_message_get_route (sip, 0, &route);

    osip_to_get_tag (sip->to, &tag);
    if (tag == NULL && route != NULL && route->url != NULL)
      {
        osip_list_remove (&sip->routes, 0);
      }
    i = osip_message_to_str (sip, &message, &length);
    if (tag == NULL && route != NULL && route->url != NULL)
      {
        osip_list_add (&sip->routes, route, 0);
      }
  }

  if (i != 0 || length <= 0)
    {
      return -1;
    }

  switch (((struct sockaddr *) &addr)->sa_family)
    {
      case AF_INET:
        inet_ntop (((struct sockaddr *) &addr)->sa_family,
                   &(((struct sockaddr_in *) &addr)->sin_addr), ipbuf,
                   sizeof (ipbuf));
        break;
      case AF_INET6:
        inet_ntop (((struct sockaddr *) &addr)->sa_family,
                   &(((struct sockaddr_in6 *) &addr)->sin6_addr), ipbuf,
                   sizeof (ipbuf));
        break;
      default:
        strncpy (ipbuf, "(unknown)", sizeof (ipbuf));
        break;
    }

  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,
                          "Message sent: (to dest=%s:%i)\n%s\n",
                          ipbuf, port, message));

  if (tr != NULL)
    {
      if (tr->ict_context != NULL)
        osip_ict_set_destination (tr->ict_context, osip_strdup (ipbuf), port);
      if (tr->nict_context != NULL)
        osip_nict_set_destination (tr->nict_context, osip_strdup (ipbuf), port);
    }

  //SIP加密点

  crypt_date = (char *)malloc(length);

  memcpy(crypt_date,(const char *)message, length);

#ifdef ENABLE_MYCRYPT
  CRYPT_DATA_SIP((unsigned char *)crypt_date,length);
#endif // ENABLE_MYCRYPT

  if (0 >
      sendto (udp_socket, (const void *) crypt_date, length, 0,
              (struct sockaddr *) &addr, len))
    {
#ifdef WIN32
      if (WSAECONNREFUSED == WSAGetLastError ())
#else
      if (ECONNREFUSED == errno)
#endif
        {
          /* This can be considered as an error, but for the moment,
             I prefer that the application continue to try sending
             message again and again... so we are not in a error case.
             Nevertheless, this error should be announced!
             ALSO, UAS may not have any other options than retry always
             on the same port.
           */
          osip_free (message);
          return 1;
      } else
        {

#ifndef MINISIZE
          /* delete first SRV entry that is not reachable */
          if (tr != NULL && tr->record.name[0] != '\0'
              && tr->record.srventry[0].srv[0] != '\0')
            {
              memmove (&tr->record.srventry[0], &tr->record.srventry[1],
                       9 * sizeof (osip_srv_entry_t));
              memset (&tr->record.srventry[9], 0, sizeof (osip_srv_entry_t));
              osip_free (message);
              return OSIP_SUCCESS;      /* retry for next retransmission! */
            }
#endif
          /* SIP_NETWORK_ERROR; */
          osip_free (message);
          return -1;
        }
    }

  if (eXosip.keep_alive > 0)
    {
      if (MSG_IS_REGISTER (sip))
        {
          eXosip_reg_t *reg = NULL;

          if (_eXosip_reg_find (&reg, tr) == 0)
            {
              memcpy (&(reg->addr), &addr, len);
              reg->len = len;
            }
        }
    }

  osip_free (message);
  return OSIP_SUCCESS;
}
示例#11
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;
}
示例#12
0
//---------------------------------------------------------------------
int
Node::SndResponse4Query(int status,
						const char *contact,
						osip_transaction_t *tr,
						osip_message_t *request)
{
	char *message1;
	size_t length = 0;
	
	//
	osip_generic_param_t *tag;
	osip_message_t *response;
	osip_event_t *evt ;
	char *tmp;
	int pos;
	int i;

	i = osip_message_init (&response);
	if (i != 0)
		return -1;

	osip_message_set_version (response, osip_strdup ("SIP/2.0"));
	osip_message_set_status_code (response, status);

	tmp = osip_strdup(osip_message_get_reason (status));
	if (tmp == NULL)
		osip_message_set_reason_phrase (response, osip_strdup ("Unknown status code"));
	else
		osip_message_set_reason_phrase (response, tmp);

	osip_message_set_method (response, NULL);
	osip_message_set_uri (response, NULL);

	i = osip_to_clone (request->to, &(response->to));
	if (i != 0)
		goto si2perror1;

	i = osip_to_get_tag (response->to, &tag);
	if (i != 0)
    {	/* we only add a tag if it does not already contains one! */
		if (status == 200 && MSG_IS_REGISTER (request))
		{
			osip_to_set_tag (response->to, osip_to_tag_new_random ());
		}
		else if (status >= 200)
		{
			osip_to_set_tag (response->to, osip_to_tag_new_random ());
		}
    }

	i = osip_from_clone (request->from, &(response->from));
	if (i != 0)
		goto si2perror1;
	
	pos = 0;
	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);
		i = osip_via_clone (via, &via2);
		if (i != -0)
			goto si2perror1;
		osip_list_add (response->vias, via2, -1);
		pos++;
    }

	i = osip_call_id_clone (request->call_id, &(response->call_id));
	if (i != 0)
		goto si2perror1;
	i = osip_cseq_clone (request->cseq, &(response->cseq));
	if (i != 0)
		goto si2perror1;

	//set server
	osip_message_set_server (response, osip_strdup ("SI2P"));

	/*add contact*/
	if(contact !=NULL)	
	osip_message_set_contact(response,contact);

	i = osip_message_to_str(response, &message1, &length);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1)	osip_free(message1) ;

	evt = osip_new_outgoing_sipmessage (response);
  	evt->transactionid = tr->transactionid;
	osip_transaction_add_event(tr, evt);

  	adosip->ThreadWakeUp();

	return 0;

si2perror1:
	osip_message_free (response);
	return -1;
}
示例#13
0
int Node::SndResponse(int status , 
					  osip_message_t *request , 
					  osip_transaction_t *tr,
					  REG_TYPE type , 
					  ChordId contact, 
					  BOOL IncludeSuccList)  
{
	char *message1;
	size_t length = 0;
	
	//
	osip_generic_param_t *tag;
	osip_message_t *response;
	osip_event_t *evt ;
	char *tmp;
	char * tmp_uri;
	int pos;
	int i;

	i = osip_message_init (&response);
	if (i != 0)
		return -1;

	osip_message_set_version (response, osip_strdup ("SIP/2.0"));
	osip_message_set_status_code (response, status);

	tmp = osip_strdup(osip_message_get_reason (status));
	if (tmp == NULL)
		osip_message_set_reason_phrase (response, osip_strdup ("Unknown status code"));
	else
		osip_message_set_reason_phrase (response, tmp);

	osip_message_set_method (response, NULL);
	osip_message_set_uri (response, NULL);

	i = osip_to_clone (request->to, &(response->to));
	if (i != 0)
		goto si2perror1;

	i = osip_to_get_tag (response->to, &tag);
	if (i != 0)
    {	/* we only add a tag if it does not already contains one! */
		if (status == 200 && MSG_IS_REGISTER (request))
		{
			osip_to_set_tag (response->to, osip_to_tag_new_random ());
		}
		else if (status >= 200)
		{
			osip_to_set_tag (response->to, osip_to_tag_new_random ());
		}
    }

	i = osip_from_clone (request->from, &(response->from));
	if (i != 0)
		goto si2perror1;
	
	pos = 0;
	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);
		i = osip_via_clone (via, &via2);
		if (i != -0)
			goto si2perror1;
		osip_list_add (response->vias, via2, -1);
		pos++;
    }

	i = osip_call_id_clone (request->call_id, &(response->call_id));
	if (i != 0)
		goto si2perror1;
	i = osip_cseq_clone (request->cseq, &(response->cseq));
	if (i != 0)
		goto si2perror1;

	//set server
	osip_message_set_server (response, osip_strdup ("SI2P"));

	/*add contact*/
	//predecessor
	if(status == 200)
	{
		if(type == USER_REGISTRATION || type == TRANSFER_REGISTRATION || type == THIRD_PARTY)
		{
			osip_contact_t *tmp;
			char *dest;
			int pos=0;
			while(pos<osip_list_size(request->contacts))
			{
				pos=osip_message_get_contact(request,0,&tmp);
				osip_contact_to_str(tmp,&dest);
				osip_message_set_contact(response,dest);
				if(dest)	osip_free(dest) ;
				pos++;
			}
		}
	
		else
		{
			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(response, ctt );
			osip_free(ctt) ;
		}
	}
	else//302
	{
		if(type == USER_REGISTRATION || type == TRANSFER_REGISTRATION || type == THIRD_PARTY)
		{
			tmp_uri = ChordId2Uri(contact,false);
			osip_message_set_contact(response,tmp_uri );
			osip_free(tmp_uri) ;
		}
		else
		{
			tmp_uri = ChordId2Uri(contact,false) ;
			char *ctt = (char *) osip_malloc(strlen(tmp_uri) + strlen(";predecessor=notprovided") +1) ;
			if (ctt == NULL)
				return NULL;
			sprintf (ctt, "%s;predecessor=notprovided", tmp_uri);
			if(tmp_uri) osip_free(tmp_uri) ;	
			osip_message_set_contact(response, ctt );
			if(tmp_uri) osip_free(ctt) ;
		}
		
	}

	if(IncludeSuccList)
	{
		for(i = 0  ; i < getFingerTable()->getSuccNum() ; i++)
		{
			tmp_uri = ChordId2Uri(getFingerTable()->getSuccessor(i),false) ;
			osip_message_set_contact(response, tmp_uri );
			osip_free(tmp_uri) ;
		}
	}

	
	i = osip_message_to_str(response, &message1, &length);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1)	osip_free(message1) ;
//	printf("SEND======================================>>\n") ;
//	printf(message1) ;

	evt = osip_new_outgoing_sipmessage (response);
  	evt->transactionid = tr->transactionid;
	osip_transaction_add_event(tr, evt);

  	adosip->ThreadWakeUp();

	return 0;

si2perror1:
	osip_message_free (response);
	return -1;
}
示例#14
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;


}
示例#15
0
int
test_message (char *msg, size_t len, int verbose, int clone)
{
  osip_message_t *sip;

  {
    char *result;

    /* int j=10000; */
    int j = 1;

    if (verbose)
      fprintf (stdout, "Trying %i sequentials calls to osip_message_init(), osip_message_parse() and osip_message_free()\n", j);
    while (j != 0) {
      j--;
      osip_message_init (&sip);
      if (osip_message_parse (sip, msg, len) != 0) {
        fprintf (stdout, "ERROR: failed while parsing!\n");
        osip_message_free (sip);
        return -1;
      }
      osip_message_free (sip);
    }

    osip_message_init (&sip);
    if (osip_message_parse (sip, msg, len) != 0) {
      fprintf (stdout, "ERROR: failed while parsing!\n");
      osip_message_free (sip);
      return -1;
    }
    else {
      int i;
      size_t length;

#if 0
      sdp_message_t *sdp;
      osip_body_t *oldbody;
      int pos;

      pos = 0;
      while (!osip_list_eol (&sip->bodies, pos)) {
        oldbody = (osip_body_t *) osip_list_get (&sip->bodies, pos);
        pos++;
        sdp_message_init (&sdp);
        i = sdp_message_parse (sdp, oldbody->body);
        if (i != 0) {
          fprintf (stdout, "ERROR: Bad SDP!\n");
        }
        else
          fprintf (stdout, "SUCCESS: Correct SDP!\n");
        sdp_message_free (sdp);
        sdp = NULL;
      }
#endif

      osip_message_force_update (sip);
      i = osip_message_to_str (sip, &result, &length);
      if (i == -1) {
        fprintf (stdout, "ERROR: failed while printing message!\n");
        osip_message_free (sip);
        return -1;
      }
      else {
        if (verbose)
          fwrite (result, 1, length, stdout);
        if (clone) {
          /* create a clone of message */
          /* int j = 10000; */
          int j = 1;

          if (verbose)
            fprintf (stdout, "Trying %i sequentials calls to osip_message_clone() and osip_message_free()\n", j);
          while (j != 0) {
            osip_message_t *copy;

            j--;
            i = osip_message_clone (sip, &copy);
            if (i != 0) {
              fprintf (stdout, "ERROR: failed while creating copy of message!\n");
            }
            else {
              char *tmp;
              size_t length;

              osip_message_force_update (copy);
              i = osip_message_to_str (copy, &tmp, &length);
              if (i != 0) {
                fprintf (stdout, "ERROR: failed while printing message!\n");
              }
              else {
                if (0 == strcmp (result, tmp)) {
                  if (verbose)
                    printf ("The osip_message_clone method works perfectly\n");
                }
                else
                  printf ("ERROR: The osip_message_clone method DOES NOT works\n");
                if (verbose) {
                  printf ("Here is the copy: \n");
                  fwrite (tmp, 1, length, stdout);
                  printf ("\n");
                }

                osip_free (tmp);
              }
              osip_message_free (copy);
            }
          }
          if (verbose)
            fprintf (stdout, "sequentials calls: done\n");
        }
        osip_free (result);
      }
      osip_message_free (sip);
    }
  }
  return 0;
}
示例#16
0
//------------------------------------------------------------------
void JoiningState::onJoinOK(
					Node* node,
					osip_transaction_t * tr ,
					osip_message *response)
{
	
	char *message;
	size_t length = 0;
	int i = osip_message_to_str(response, &message, &length);
	node->LogStream("RECEIVE<<======================================\n") ;
	node->LogStream(message) ;
	printf("RECEIVE<<======================================\n") ;
	printf(message) ;

	ChordId to(
			atoi(response->to->url->username),
			node->getConstants(),
			response->to->url->host,
			response->to->url->port
			) ;

	osip_contact_t * osip_contact ;
	osip_message_get_contact(response,0,&osip_contact) ;
	if(!osip_contact)	
	{return ;}//error
	ChordId contact(
		atoi(osip_contact->url->username),
		node->getConstants(),
		osip_contact->url->host,
		osip_contact->url->port
		) ;
	
	ChordId chordId = node->getChordId() ;
	//***********************************

	int k = node->Set_Fingers(1,contact) + 1 ;	
	node->setSuccListWithContacts(response,true) ;
	node->setPredWithContacts(response) ;
	
	/* state convert to  FTComputingState */
	if (k <= node->getFingerTable()->getTableLength())		//update of fingertable is not completed
	{
		int i = node->SndRegister(FINDSUCC,contact,node->getFingerTable()->getFinger(k)->start,chordId) ; 
		
		//- change state ...
		ChangeState(node,new FTComputingState()) ;
		return ;
	}
	
	/* state convert to  NormalState */
	if (k == node->getFingerTable()->getTableLength() + 1)
	{
		
		//- Notify ...
		Notify(node) ;		
		//- change state ...
		ChangeState(node,new NormalState(node)) ;
		
		return ;

	}

}
示例#17
0
int GB_handle_RCV_REQINVITE(GB_CONNECT_STATE *gb_cons, osip_event_t * osip_event, int *flag)
{
	int ret = -1;
	osip_message_t *rsp = NULL;
	char *result = NULL;
	size_t length;
	int status_code = 200;
	char sdpStr_response[1024] = {0};
	char *pRsp = NULL;
//	struct sipd_media_session sdpInfo;
	GB_media_session media_session;
	int chn = -1;
	PRM_GB_SIPD_CFG gb_cfg;
	osip_uri_param_t *tag = NULL;
	
//	SN_MEMSET(&sdpInfo,0,sizeof(sdpInfo));
	SN_MEMSET(&media_session,0,sizeof(media_session));
	SN_MEMSET(&gb_cfg,0,sizeof(gb_cfg));

	GB_Get_GBCfg(&gb_cfg);

	if(gb_cons == NULL || osip_event == NULL || osip_event->sip == NULL || 
		osip_event->sip->req_uri == NULL || osip_event->sip->req_uri->username == NULL ||
		osip_event->sip->from == NULL || osip_event->sip->from->url == NULL || 
		osip_event->sip->from->url->username == NULL)
	{
		TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s  line=%d Something is NULL\n",__FUNCTION__,__LINE__);
		return -1;
	}
	
	DeviceID2LocalChn(osip_event->sip->req_uri->username,&chn);

	TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s  line=%d INVITE from:%s  to:%s\n",__FUNCTION__,__LINE__,
			osip_event->sip->from->url->username,osip_event->sip->req_uri->username);
	
	if(chn <= 0)
	{
		TRACE(SCI_TRACE_NORMAL,MOD_GB,"%s  Unknow DeviceID(%s)\n",__FUNCTION__,osip_event->sip->req_uri->username);
		status_code = 404;  // 没找到
		pRsp = NULL;
	}
	else
	{
		// 流量控制判断等接口调用

		osip_from_get_tag(osip_event->sip->from,&tag);

		if(tag == NULL || tag->gvalue == NULL)
		{
			status_code = 400;  // 错误的请求
			pRsp = NULL;
		}
		else
		{
			media_session.chn = chn - 1;
			media_session.media_session_opt = MEDIA_SESSION_OPT_INVITE;
			SN_STRCPY(media_session.TagID,sizeof(media_session.TagID),tag->gvalue);
						
			write(gb_msg_sock.client_fd, (void *)&media_session, sizeof(media_session));

			snprintf(sdpStr_response,1024,
			"v=0\r\n"\
			"o=34020000001330000001 0 0 IN IP4 %s\r\n"\
			"s=Play\r\n"\
			"c=IN IP4 %s\r\n"\
			"t=0 0\r\n"\
			"m=video %d RTP/AVP 96\r\n"\
			"a=recvonly\r\n"\
			"a=rtpmap:96 PS/90000\r\n"\
			"y=0100000001\r\n"\
			"f=\r\n",
			GB_Get_LocalIP(), GB_Get_LocalIP(),LISTEN_PORT);

			ret = 0;
			if(ret == 0)
			{
				pRsp = sdpStr_response;
			}
			else
			{
				pRsp = NULL;
				status_code = 488;  // 这里不能接受 
			}
		}
	}
	
	ret = gb_build_response_message(&rsp, NULL, status_code,osip_event->sip, GB_MANSCDP_SDP,pRsp,SN_STRLEN(sdpStr_response));

	if(ret < 0)
	{
		osip_message_free (rsp);
		printf("%s  line=%d\n",__FUNCTION__,__LINE__);
		return -1;
	}

	gb_build_response_Contact(rsp, GB_Get_LocalIP(), gb_cfg.local_port, (char *)(gb_cfg.deviceID));

	ret = osip_message_to_str(rsp, &result, &length);

	if (ret == -1) 
	{
		printf("ERROR: failed while printing message!\n");
		osip_message_free (rsp);
		return -1;
	}	
	
	GB_SocketSendData(gb_cons->connfd,inet_ntoa(gb_cons->remoteAddr.sin_addr), ntohs(gb_cons->remoteAddr.sin_port), result, length, 0);
	SN_FREE(result);
	osip_message_free(rsp);

	return 0;
}
示例#18
0
void FTComputingState::onFindSuccRedirect(
					Node* node,
					osip_transaction_t * tr ,
					osip_message *response) 
{
	char *message;
	size_t length = 0;
	int i = osip_message_to_str(response, &message, &length);
	node->LogStream("RECEIVE<<======================================\n") ;
	node->LogStream(message) ;
//	printf("RECEIVE<<======================================\n") ;
//	printf(message) ;

	/* to , from , contact are same as orig_request ;
	req_uri is the contact of the response */
	ChordId to(
			atoi(tr->orig_request->to->url->username),
			node->getConstants(),
			tr->orig_request->to->url->host,
			tr->orig_request->to->url->port
			) ;
	ChordId from(
			atoi(tr->orig_request->from->url->username),
			node->getConstants(),
			tr->orig_request->from->url->host,
			tr->orig_request->from->url->port
			) ;

		
	osip_contact_t *osip_contact ;
	osip_message_get_contact(response,0,&osip_contact) ;
	if(!osip_contact)	
	{return ;}//error
	ChordId contact(
		atoi(osip_contact->url->username),
		node->getConstants(),
		osip_contact->url->host,
		osip_contact->url->port
		) ;


	ChordId chordId = node->getChordId() ;
	//******************************

	//如果重定向到自己,那么自身就是对应to的finger
	if(contact.equals(chordId))
	{
		//scan fingertable
		for(int i = 1 ; i <= node->getFingerTable()->getTableLength() ; i++)
		{
			if(node->getFingerTable()->getFinger(i)->start.equals(to)) 
				break ;
		}

		int k ;
		if(i <= node->getFingerTable()->getTableLength())
			k = node->Set_Fingers(i, contact) + 1 ;

		//A.finger表更新完毕
		if (k == node->getFingerTable()->getTableLength() + 1)
		{
			//- Notify ...
			Notify(node) ;			
			//- change state ...
			ChangeState(node,new NormalState(node)) ;
		}

		//B.finger表未更新完
		if (k<= node->getFingerTable()->getTableLength())	//update of fingertalbe if not completed
		{
			int i = node->SndRegister(FINDSUCC,contact,node->getFingerTable()->getFinger(k)->start,chordId) ; 
			
			//- not change state...
			return ;
		}	
	}

	//继续重定向
	else
	{
		node->SndRegister(FINDSUCC,contact,to,chordId) ; 		
	}

}
示例#19
0
//------------------------------------------------------------------
void FTComputingState::onFindSuccOK(	Node* node,
					osip_transaction_t * tr ,
					osip_message *response) 
{
	char *message;
	size_t length = 0;
	int i = osip_message_to_str(response, &message, &length);
	node->LogStream("RECEIVE<<======================================\n") ;
	node->LogStream(message) ;
//	printf("RECEIVE<<======================================\n") ;
//	printf(message) ;

	ChordId to(
			atoi(response->to->url->username),
			node->getConstants(),
			response->to->url->host,
			response->to->url->port
			) ;

	osip_contact_t * osip_contact ;
	osip_message_get_contact(response,0,&osip_contact) ;
	if(!osip_contact)	
	{return ;}//error
	ChordId contact(
		atoi(osip_contact->url->username),
		node->getConstants(),
		osip_contact->url->host,
		osip_contact->url->port
		) ;

	ChordId chordId = node->getChordId() ;
	//******************************

	//scan fingertable
	for(i = 1 ; i <= node->getFingerTable()->getTableLength() ; i++)
	{
		if(node->getFingerTable()->getFinger(i)->start.equals(to)) 
			break ;
	}
	
	int k ;
	if(i <= node->getFingerTable()->getTableLength())
		k = node->Set_Fingers(i, contact) + 1 ;

	//A.finger表更新完毕,有两种情况
	if (k == node->getFingerTable()->getTableLength() + 1)
	{
		//- Notify ...
		Notify(node) ;			
		//- change state ...
		ChangeState(node,new NormalState(node)) ;

		return ;
	}

	//B.finger表未更新完
	if (k<= node->getFingerTable()->getTableLength())	//update of fingertalbe if not completed
	{
		int i = node->SndRegister(FINDSUCC,contact,node->getFingerTable()->getFinger(k)->start,chordId) ;

		//- not change state...
		return ;
	}
}
示例#20
0
int GB_sipd_register_auth(GB_CONNECT_STATE *gb_cons, int flag)
{
	osip_message_t *reg = NULL;
	PRM_GB_SIPD_CFG gb_cfg;
	char from[GB_URI_MAX_LEN] = {0};
	char proxy[GB_URI_MAX_LEN] = {0};
	int ret;
	char *result = NULL;
	size_t length;
	char localip[20];
	char sipserver_ip[20] = {0};
	char *uri = NULL;
	osip_authorization_t *aut = NULL;
	
	SN_MEMSET(&gb_cfg, 0, sizeof(gb_cfg));
	
	GB_Get_GBCfg(&gb_cfg);

	SN_MEMSET(localip,0,sizeof(localip));
	SN_STRCPY(localip,sizeof(localip),GB_Get_LocalIP());
		
	SN_SPRINTF(sipserver_ip,sizeof(sipserver_ip),"%d.%d.%d.%d",
		gb_cfg.sipserver_ip[0],gb_cfg.sipserver_ip[1],gb_cfg.sipserver_ip[2],gb_cfg.sipserver_ip[3]);

	SN_SPRINTF(from, GB_URI_MAX_LEN, "sip:%s@%s", gb_cfg.deviceID, localip);
	SN_SPRINTF(proxy, GB_URI_MAX_LEN, "sip:%s@%s:%d", gb_cfg.sipserver_ID,sipserver_ip, gb_cfg.sipserver_port);

	if(flag == 0)  // 注册
	{
		ret = gb_generating_register(&reg, Transport_Str[gb_cons->transfer_protocol], from, proxy, NULL, gb_cfg.register_period, localip,gb_cfg.local_port, gb_cons->local_cseq);
	}
	else  //  注销
	{
		ret = gb_generating_register(&reg, Transport_Str[gb_cons->transfer_protocol], from, proxy, NULL, 0, localip,gb_cfg.local_port, gb_cons->local_cseq);
	}
		
	if (ret < 0)
	{
		osip_message_free (reg);
		return -1;
	}

	ret = osip_uri_to_str (reg->req_uri, &uri);
	if (ret != 0)
		return ret;
	
	ret = gb_create_authorization_header (gb_cons->wwwa, uri, ADMIN_NAME, (char *)gb_cfg.reg_pwd, NULL, &aut, "REGISTER", NULL, 0);

	osip_free (uri);
	if (ret != 0)
		return ret;

	if (aut != NULL) 
	{
		if (osip_strcasecmp (reg->sip_method, "REGISTER") == 0)
			osip_list_add (&reg->authorizations, aut, -1);
		else
			osip_list_add (&reg->proxy_authorizations, aut, -1);
		osip_message_force_update (reg);	
	}

	ret = osip_message_to_str(reg, &result, &length);
	if (ret == -1) 
	{
		printf("ERROR: failed while printing message!\n");
		osip_message_free (reg);
		return -1;
	}	
	gb_cons->local_cseq++;
	GB_SocketSendData(gb_cons->connfd,inet_ntoa(gb_cons->remoteAddr.sin_addr), ntohs(gb_cons->remoteAddr.sin_port), result, length, 0);
	SN_FREE(result);
	osip_message_free(reg);
	
	return 0;
}
示例#21
0
static int
dtls_tl_send_message(osip_transaction_t * tr, osip_message_t * sip, char *host,
                    int port, int out_socket)
{
  int len = 0;
  size_t length = 0;
  struct addrinfo *addrinfo;
  struct __eXosip_sockaddr addr;
  char *message;

  char ipbuf[INET6_ADDRSTRLEN];
  int i;

  int pos;
  struct socket_tab *socket_tab_used=NULL;
  BIO *sbio=NULL;

  if (dtls_socket <= 0)
    return -1;

  if (host == NULL)
    {
      host = sip->req_uri->host;
      if (sip->req_uri->port != NULL)
        port = osip_atoi (sip->req_uri->port);
      else
        port = 5061;
    }

  if (port == 5060)
    port = 5061;

  if (MSG_IS_REQUEST(sip))
    {
      if (MSG_IS_REGISTER(sip)
	  ||MSG_IS_INVITE(sip)
	  ||MSG_IS_SUBSCRIBE(sip)
	  || MSG_IS_NOTIFY(sip))
	eXtl_update_local_target(sip);
    }

  i=-1;
#ifndef MINISIZE
  if (tr!=NULL && tr->record.name[0]!='\0' && tr->record.srventry[0].srv[0]!='\0')
    {
      /* always choose the first here.
	 if a network error occur, remove first entry and
	 replace with next entries.
      */
      osip_srv_entry_t *srv;
      int n=0;
      for (srv = &tr->record.srventry[0];
	   n<10 && tr->record.srventry[0].srv[0];
	   srv = &tr->record.srventry[0])
	{
	  i = eXosip_get_addrinfo (&addrinfo, srv->srv, srv->port, IPPROTO_UDP);
	  if (i == 0)
	    {
	      host = srv->srv;
	      port = srv->port;
	      break;
	    }
	  memmove(&tr->record.srventry[0], &tr->record.srventry[1], 9*sizeof(osip_srv_entry_t));
	  memset(&tr->record.srventry[9], 0, sizeof(osip_srv_entry_t));
	  i=-1;
	  /* copy next element */
	  n++;
	}
    }
#endif
  
  /* if SRV was used, distination may be already found */
  if (i != 0)
    {
      i = eXosip_get_addrinfo (&addrinfo, host, port, IPPROTO_UDP);
    }
  
  if (i != 0)
    {
      return -1;
    }
  
  memcpy (&addr, addrinfo->ai_addr, addrinfo->ai_addrlen);
  len = addrinfo->ai_addrlen;
  
  eXosip_freeaddrinfo (addrinfo);
  
  /* remove preloaded route if there is no tag in the To header
   */
  {
    osip_route_t *route=NULL;
    osip_generic_param_t *tag=NULL;
    osip_message_get_route (sip, 0, &route);
    
    osip_to_get_tag (sip->to, &tag);
    if (tag==NULL && route != NULL && route->url != NULL)
      {
	osip_list_remove(&sip->routes, 0);
      }
    i = osip_message_to_str (sip, &message, &length);
    if (tag==NULL && route != NULL && route->url != NULL)
      {
	osip_list_add(&sip->routes, route, 0);
      }
  }
  
  if (i != 0 || length <= 0)
    {
      return -1;
    }
  
  switch ( ((struct sockaddr *)&addr)->sa_family )
    {
    case AF_INET:
      inet_ntop (((struct sockaddr *)&addr)->sa_family, &(((struct sockaddr_in *) &addr)->sin_addr),
		 ipbuf, sizeof (ipbuf));
      break;
    case AF_INET6:
      inet_ntop (((struct sockaddr *)&addr)->sa_family,
		 &(((struct sockaddr_in6 *) &addr)->sin6_addr), ipbuf,
		 sizeof (ipbuf));
      break;
    default:
      strncpy (ipbuf, "(unknown)", sizeof (ipbuf));
      break;
    }
  
  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,
                          "Message sent: \n%s (to dest=%s:%i)\n",
                          message, ipbuf, port));

  for (pos = 0; pos < EXOSIP_MAX_SOCKETS; pos++)
    {
      if (dtls_socket_tab[pos].ssl_conn != NULL
	  && dtls_socket_tab[pos].ssl_type == EXOSIP_AS_A_SERVER)
	{
	  if (dtls_socket_tab[pos].remote_port == port &&
	      (strcmp (dtls_socket_tab[pos].remote_ip, ipbuf) == 0))
	    {
	      BIO *rbio;
	      socket_tab_used = &dtls_socket_tab[pos];
	      rbio = BIO_new_dgram (dtls_socket, BIO_NOCLOSE);
	      BIO_dgram_set_peer (rbio, &addr);
	      dtls_socket_tab[pos].ssl_conn->rbio = rbio;
	      break;
	    }
	}
    }
  if (socket_tab_used==NULL)
    {
      for (pos = 0; pos < EXOSIP_MAX_SOCKETS; pos++)
	{
	  if (dtls_socket_tab[pos].ssl_conn != NULL
	      && dtls_socket_tab[pos].ssl_type == EXOSIP_AS_A_CLIENT)
	    {
	      if (dtls_socket_tab[pos].remote_port == port &&
		  (strcmp (dtls_socket_tab[pos].remote_ip, ipbuf) == 0))
		{
		  BIO *rbio;
		  socket_tab_used = &dtls_socket_tab[pos];
		  rbio = BIO_new_dgram (dtls_socket, BIO_NOCLOSE);
		  BIO_dgram_set_peer (rbio, &addr);
		  dtls_socket_tab[pos].ssl_conn->rbio = rbio;
		  break;
		}
	    }
	}
    }

  if (socket_tab_used==NULL)
    {
      /* delete an old one! */
      pos=0;
      if (dtls_socket_tab[pos].ssl_conn != NULL)
	{
	  shutdown_free_client_dtls (pos);
	  shutdown_free_server_dtls (pos);
	}
      
      memset(&dtls_socket_tab[pos], 0, sizeof(struct socket_tab));
    }
  
  if (dtls_socket_tab[pos].ssl_conn == NULL)
    {
      /* create a new one */
      SSL_CTX_set_read_ahead (client_ctx, 1);
      dtls_socket_tab[pos].ssl_conn = SSL_new (client_ctx);

      if (dtls_socket_tab[pos].ssl_conn == NULL)
	{
	  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,
				  "DTLS SSL_new error\n"));

	  if (dtls_socket_tab[pos].ssl_conn != NULL)
	    {
	      shutdown_free_client_dtls (pos);
	      shutdown_free_server_dtls (pos);
	    }
	  
	  memset(&dtls_socket_tab[pos], 0, sizeof(struct socket_tab));

	  osip_free (message);
	  return -1;
	}

      if (connect (dtls_socket, (struct sockaddr *) &addr, sizeof (addr)) == -1)
	{
	  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,
				  "DTLS connect error\n"));
	  if (dtls_socket_tab[pos].ssl_conn != NULL)
	    {
	      shutdown_free_client_dtls (pos);
	      shutdown_free_server_dtls (pos);
	    }
	  
	  memset(&dtls_socket_tab[pos], 0, sizeof(struct socket_tab));

	  osip_free (message);
	  return -1;
	}
      
      SSL_set_options (dtls_socket_tab[pos].ssl_conn, SSL_OP_NO_QUERY_MTU);
      SSL_set_mtu (dtls_socket_tab[pos].ssl_conn, 2000);
      SSL_set_connect_state (dtls_socket_tab[pos].ssl_conn);
      sbio = BIO_new_dgram (dtls_socket, BIO_NOCLOSE);
      BIO_ctrl_set_connected (sbio, 1, (struct sockaddr *) &addr);
      SSL_set_bio (dtls_socket_tab[pos].ssl_conn, sbio, sbio);

      dtls_socket_tab[pos].ssl_type = 2;
      dtls_socket_tab[pos].ssl_state = 2;
      
      osip_strncpy (dtls_socket_tab[pos].remote_ip, ipbuf,
		    sizeof (dtls_socket_tab[pos].remote_ip));
      dtls_socket_tab[pos].remote_port = port;
    }

  i = SSL_write (dtls_socket_tab[pos].ssl_conn, message, length);

  if (i<0)
    {
      i = SSL_get_error (dtls_socket_tab[pos].ssl_conn, i);
      print_ssl_error (i);
      if (i==SSL_ERROR_SSL
	  || i==SSL_ERROR_SYSCALL)
	{
	  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,
				  "DTLS SSL_write error\n"));
	  if (dtls_socket_tab[pos].ssl_conn != NULL)
	      {
		shutdown_free_client_dtls (pos);
		shutdown_free_server_dtls (pos);
	      }
	  
	  memset(&dtls_socket_tab[pos], 0, sizeof(struct socket_tab));
	  
	}

#ifndef MINISIZE
      /* delete first SRV entry that is not reachable */
      if (tr->record.name[0]!='\0' && tr->record.srventry[0].srv[0]!='\0')
	{
	  memmove(&tr->record.srventry[0], &tr->record.srventry[1], 9*sizeof(osip_srv_entry_t));
	  memset(&tr->record.srventry[9], 0, sizeof(osip_srv_entry_t));
	  osip_free (message);
	  return 0; /* retry for next retransmission! */
	}
#endif
      /* SIP_NETWORK_ERROR; */
      osip_free (message);
      return -1;
    }

  if (eXosip.keep_alive > 0)
    {
      if (MSG_IS_REGISTER (sip))
        {
          eXosip_reg_t *reg = NULL;
	  
          if (_eXosip_reg_find (&reg, tr) == 0)
            {
              memcpy (&(reg->addr), &addr, len);
              reg->len = len;
            }
        }
    }
  
  osip_free (message);
  return 0;
}
示例#22
0
int GB_Send_Sub_Reply(GB_CONNECT_STATE *gb_cons, osip_event_t * osip_event, gb_BaseInfo_Query *BaseInfo,int status_code)
{
	int ret = -1;
	osip_message_t *rsp = NULL;
	char *result = NULL;
	size_t length;
	char body_str[GB_MAX_PLAYLOAD_BUF];

	if(gb_cons == NULL || osip_event == NULL || BaseInfo == NULL)
	{
		return -1;
	}	

	SN_MEMSET(&body_str,0,sizeof(body_str));

	if(BaseInfo->Cmdtype == gb_CommandType_Catalog)
	{
		if(status_code == 200)
			SN_SPRINTF(body_str,sizeof(body_str),
				"<?xml version=\"1.0\"?>\r\n"\
				"<Response>\r\n"\
				"<CmdType>Catalog</CmdType>\r\n"\
				"<SN>%d</SN>\r\n"\
				"<DeviceID>%s</DeviceID>\r\n"\
				"<Result>OK</Result>\r\n"\
				"</Response>\r\n",
				BaseInfo->SN,BaseInfo->DeviceID.deviceID);
		else
			SN_SPRINTF(body_str,sizeof(body_str),
				"<?xml version=\"1.0\"?>\r\n"\
				"<Response>\r\n"\
				"<CmdType>Catalog</CmdType>\r\n"\
				"<SN>%d</SN>\r\n"\
				"<DeviceID>%s</DeviceID>\r\n"\
				"<Result>ERROR</Result>\r\n"\
				"</Response>\r\n",
				BaseInfo->SN,BaseInfo->DeviceID.deviceID);
	}
	else if(BaseInfo->Cmdtype == gb_CommandType_Alarm)
	{
		if(status_code == 200)
			SN_SPRINTF(body_str,sizeof(body_str),
				"<?xml version=\"1.0\"?>\r\n"\
				"<Response>\r\n"\
				"<CmdType>Alarm</CmdType>\r\n"\
				"<SN>%d</SN>\r\n"\
				"<DeviceID>%s</DeviceID>\r\n"\
				"<Result>OK</Result>\r\n"\
				"</Response>\r\n",
				BaseInfo->SN,BaseInfo->DeviceID.deviceID);
		else
			SN_SPRINTF(body_str,sizeof(body_str),
				"<?xml version=\"1.0\"?>\r\n"\
				"<Response>\r\n"\
				"<CmdType>Alarm</CmdType>\r\n"\
				"<SN>%d</SN>\r\n"\
				"<DeviceID>%s</DeviceID>\r\n"\
				"<Result>ERROR</Result>\r\n"\
				"</Response>\r\n",
				BaseInfo->SN,BaseInfo->DeviceID.deviceID);
	}
	
	ret = gb_build_response_message(&rsp, NULL, status_code,osip_event->sip, NULL, body_str,strlen(body_str));
	if(ret != 0)
	{
		osip_message_free (rsp);
		printf("%s  line=%d\n",__FUNCTION__,__LINE__);
		return -1;
	}

	ret = osip_message_to_str(rsp, &result, &length);

	if (ret == -1) 
	{
		printf("ERROR: failed while printing message!\n");
		osip_message_free (rsp);
		return -1;
	}	
	
	GB_SocketSendData(gb_cons->connfd,inet_ntoa(gb_cons->remoteAddr.sin_addr), ntohs(gb_cons->remoteAddr.sin_port), result, length, 0);
	SN_FREE(result);
	osip_message_free(rsp);

	return 0;
}
示例#23
0
static int
udp_tl_send_message(osip_transaction_t * tr, osip_message_t * sip, char *host,
					int port, int out_socket)
{
	int len = 0;
	size_t length = 0;
	struct addrinfo *addrinfo;
	struct __eXosip_sockaddr addr;
	char *message = NULL;

	char ipbuf[INET6_ADDRSTRLEN];
	int i;
	osip_naptr_t *naptr_record=NULL;

	if (udp_socket <= 0)
		return -1;

	if (host == NULL) {
		host = sip->req_uri->host;
		if (sip->req_uri->port != NULL)
			port = osip_atoi(sip->req_uri->port);
		else
			port = 5060;
	}

	eXtl_update_local_target(sip);

	i = -1;
#ifndef MINISIZE
	if (tr==NULL)
	{
		_eXosip_srv_lookup(sip, &naptr_record);

		if (naptr_record!=NULL) {
			eXosip_dnsutils_dns_process(naptr_record, 1);
			if (naptr_record->naptr_state==OSIP_NAPTR_STATE_NAPTRDONE
				||naptr_record->naptr_state==OSIP_NAPTR_STATE_SRVINPROGRESS)
				eXosip_dnsutils_dns_process(naptr_record, 1);
		}

		if (naptr_record!=NULL && naptr_record->naptr_state==OSIP_NAPTR_STATE_SRVDONE)
		{
			/* 4: check if we have the one we want... */
			if (naptr_record->sipudp_record.name[0] != '\0'
				&& naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index].srv[0] != '\0') {
					/* always choose the first here.
					if a network error occur, remove first entry and
					replace with next entries.
					*/
					osip_srv_entry_t *srv;
					int n = 0;
					for (srv = &naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index];
						n < 10 && naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index].srv[0];
						srv = &naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index]) {
							if (srv->ipaddress[0])
								i = eXosip_get_addrinfo(&addrinfo, srv->ipaddress, srv->port, IPPROTO_UDP);
							else
								i = eXosip_get_addrinfo(&addrinfo, srv->srv, srv->port, IPPROTO_UDP);
							if (i == 0) {
								host = srv->srv;
								port = srv->port;
								break;
							}

							i = eXosip_dnsutils_rotate_srv(&naptr_record->sipudp_record);
							if (i<=0)
							{
								return -1;
							}
							if (i>=n)
							{
								return -1;
							}
							i = -1;
							/* copy next element */
							n++;
					}
			}
		}

		if (naptr_record!=NULL && naptr_record->keep_in_cache==0)
			osip_free(naptr_record);
		naptr_record=NULL;
	}
	else
	{
		naptr_record = tr->naptr_record;
	}

	if (naptr_record!=NULL)
	{
		/* 1: make sure there is no pending DNS */
		eXosip_dnsutils_dns_process(naptr_record, 0);
		if (naptr_record->naptr_state==OSIP_NAPTR_STATE_NAPTRDONE
			||naptr_record->naptr_state==OSIP_NAPTR_STATE_SRVINPROGRESS)
			eXosip_dnsutils_dns_process(naptr_record, 0);

		if (naptr_record->naptr_state==OSIP_NAPTR_STATE_UNKNOWN)
		{
			/* fallback to DNS A */
			if (naptr_record->keep_in_cache==0)
				osip_free(naptr_record);
			naptr_record=NULL;
			if (tr!=NULL)
				tr->naptr_record=NULL;
			/* must never happen? */
		}
		else if (naptr_record->naptr_state==OSIP_NAPTR_STATE_INPROGRESS)
		{
			/* 2: keep waiting (naptr answer not received) */
			return OSIP_SUCCESS + 1;
		}
		else if (naptr_record->naptr_state==OSIP_NAPTR_STATE_NAPTRDONE)
		{
			/* 3: keep waiting (naptr answer received/no srv answer received) */
			return OSIP_SUCCESS + 1;
		}
		else if (naptr_record->naptr_state==OSIP_NAPTR_STATE_SRVINPROGRESS)
		{
			/* 3: keep waiting (naptr answer received/no srv answer received) */
			return OSIP_SUCCESS + 1;
		}
		else if (naptr_record->naptr_state==OSIP_NAPTR_STATE_SRVDONE)
		{
			/* 4: check if we have the one we want... */
			if (naptr_record->sipudp_record.name[0] != '\0'
				&& naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index].srv[0] != '\0') {
					/* always choose the first here.
					if a network error occur, remove first entry and
					replace with next entries.
					*/
					osip_srv_entry_t *srv;
					int n = 0;
					for (srv = &naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index];
						n < 10 && naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index].srv[0];
						srv = &naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index]) {
							if (srv->ipaddress[0])
								i = eXosip_get_addrinfo(&addrinfo, srv->ipaddress, srv->port, IPPROTO_UDP);
							else
								i = eXosip_get_addrinfo(&addrinfo, srv->srv, srv->port, IPPROTO_UDP);
							if (i == 0) {
								host = srv->srv;
								port = srv->port;
								break;
							}

							i = eXosip_dnsutils_rotate_srv(&naptr_record->sipudp_record);
							if (i<=0)
							{
								return -1;
							}
							if (i>=n)
							{
								return -1;
							}
							i = -1;
							/* copy next element */
							n++;
					}
			}
		}
		else if (naptr_record->naptr_state==OSIP_NAPTR_STATE_NOTSUPPORTED
			||naptr_record->naptr_state==OSIP_NAPTR_STATE_RETRYLATER)
		{
			/* 5: fallback to DNS A */
			if (naptr_record->keep_in_cache==0)
				osip_free(naptr_record);
			naptr_record=NULL;
			if (tr!=NULL)
				tr->naptr_record=NULL;
		}
	}
#endif

	/* if SRV was used, destination may be already found */
	if (i != 0) {
		i = eXosip_get_addrinfo(&addrinfo, host, port, IPPROTO_UDP);
	}

	if (i != 0) {
		return -1;
	}

	memcpy(&addr, addrinfo->ai_addr, addrinfo->ai_addrlen);
	len = addrinfo->ai_addrlen;

	eXosip_freeaddrinfo(addrinfo);

	/* remove preloaded route if there is no tag in the To header
	 */
	{
		osip_route_t *route = NULL;
		osip_generic_param_t *tag = NULL;
		osip_message_get_route(sip, 0, &route);

		osip_to_get_tag(sip->to, &tag);
		if (tag == NULL && route != NULL && route->url != NULL) {
			osip_list_remove(&sip->routes, 0);
		}
		i = osip_message_to_str(sip, &message, &length);
		if (tag == NULL && route != NULL && route->url != NULL) {
			osip_list_add(&sip->routes, route, 0);
		}
	}

	if (i != 0 || length <= 0) {
		osip_free(message);
		return -1;
	}

	switch (((struct sockaddr *) &addr)->sa_family) {
	case AF_INET:
		inet_ntop(((struct sockaddr *) &addr)->sa_family,
				  &(((struct sockaddr_in *) &addr)->sin_addr), ipbuf,
				  sizeof(ipbuf));
		break;
	case AF_INET6:
		inet_ntop(((struct sockaddr *) &addr)->sa_family,
				  &(((struct sockaddr_in6 *) &addr)->sin6_addr), ipbuf,
				  sizeof(ipbuf));
		break;
	default:
		strncpy(ipbuf, "(unknown)", sizeof(ipbuf));
		break;
	}

	OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_INFO1, NULL,
						  "Message sent: (to dest=%s:%i)\n%s\n",
						  ipbuf, port, message));

	if (osip_strcasecmp(host, ipbuf)!=0 && MSG_IS_REQUEST(sip)) {
		if (MSG_IS_REGISTER(sip)) {
			struct eXosip_dns_cache entry;

			memset(&entry, 0, sizeof(struct eXosip_dns_cache));
			snprintf(entry.host, sizeof(entry.host), "%s", host);
			snprintf(entry.ip, sizeof(entry.ip), "%s", ipbuf);
			eXosip_set_option(EXOSIP_OPT_ADD_DNS_CACHE, (void *) &entry);
		}
	}

	if (tr != NULL) {
		if (tr->ict_context != NULL)
			osip_ict_set_destination(tr->ict_context, osip_strdup(ipbuf), port);
		if (tr->nict_context != NULL)
			osip_nict_set_destination(tr->nict_context, osip_strdup(ipbuf), port);
	}

	if (0 >
		sendto(udp_socket, (const void *) message, length, 0,
			   (struct sockaddr *) &addr, len))
	{
#ifndef MINISIZE
		if (naptr_record!=NULL)
		{
			/* rotate on failure! */
			if (eXosip_dnsutils_rotate_srv(&naptr_record->sipudp_record)>0)
			{
				osip_free(message);
				return OSIP_SUCCESS + 1;	/* retry for next retransmission! */
			}
		}
#endif
		/* SIP_NETWORK_ERROR; */
		osip_free(message);
		return -1;
	}
	
	if (eXosip.keep_alive > 0) {
		if (MSG_IS_REGISTER(sip)) {
			eXosip_reg_t *reg = NULL;

			if (_eXosip_reg_find(&reg, tr) == 0) {
				memcpy(&(reg->addr), &addr, len);
				reg->len = len;
			}
		}
	}

#ifndef MINISIZE
	if (naptr_record!=NULL)
	{
		if (tr!=NULL && MSG_IS_REGISTER(sip) && tr->last_response==NULL)
		{
			/* failover for outgoing transaction */
			time_t now;
			now = time(NULL);
			OSIP_TRACE(osip_trace
				(__FILE__, __LINE__, OSIP_INFO2, NULL,
				"not yet answered\n"));
			if (tr != NULL && now - tr->birth_time > 10 && now - tr->birth_time < 13)
			{
				/* avoid doing this twice... */
				if (eXosip_dnsutils_rotate_srv(&naptr_record->sipudp_record)>0)
				{
					OSIP_TRACE(osip_trace(__FILE__, __LINE__, OSIP_INFO1, NULL,
						"Doing failover: %s:%i->%s:%i\n",
						host, port,
						naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index].srv,
						naptr_record->sipudp_record.srventry[naptr_record->sipudp_record.index].port));
					osip_free(message);
					return OSIP_SUCCESS + 1;	/* retry for next retransmission! */
				}
			}
		}
	}
#endif

	osip_free(message);
	return OSIP_SUCCESS;
}