int
osip_dialog_update_tag_as_uac (osip_dialog_t * dialog, osip_message_t * response)
{
  osip_generic_param_t *tag;
  int i;

  if (dialog == NULL)
    return OSIP_BADPARAMETER;
  if (response == NULL || response->to == NULL)
    return OSIP_BADPARAMETER;

  if (dialog->remote_tag != NULL) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "This dialog already have a remote tag: it can't be changed!\n"));
    return OSIP_WRONG_STATE;
  }

  i = osip_to_get_tag (response->to, &tag);
  if (i != 0 || tag == NULL || tag->gvalue == NULL) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "Remote UA is not compliant: missing a tag in response!\n"));
    dialog->remote_tag = NULL;
  }
  else
    dialog->remote_tag = osip_strdup (tag->gvalue);
  return OSIP_SUCCESS;
}
Example #2
0
int
osip_dialog_match_as_uac (osip_dialog_t * dlg, osip_message_t * answer)
{
    osip_generic_param_t *tag_param_local;
    osip_generic_param_t *tag_param_remote;
    char *tmp;
    int i;

    osip_call_id_to_str (answer->call_id, &tmp);
    if (0 != strcmp (dlg->call_id, tmp))
    {
        osip_free (tmp);
        return -1;
    }
    osip_free (tmp);

    /* for INCOMING RESPONSE:
       To: remote_uri;remote_tag
       From: local_uri;local_tag           <- LOCAL TAG ALWAYS EXIST
     */
    i = osip_from_get_tag (answer->from, &tag_param_local);
    if (i != 0)
        return -1;
    if (dlg->local_tag == NULL)
        /* NOT POSSIBLE BECAUSE I MANAGE REMOTE_TAG AND I ALWAYS ADD IT! */
        return -1;
    if (0 != strcmp (tag_param_local->gvalue, dlg->local_tag))
        return -1;

    i = osip_to_get_tag (answer->to, &tag_param_remote);
    if (i != 0 && dlg->remote_tag != NULL)	/* no tag in response but tag in dialog */
        return -1;			/* impossible... */
    if (i != 0 && dlg->remote_tag == NULL)	/* no tag in response AND no tag in dialog */
    {
        if (0 ==
                osip_from_compare ((osip_from_t *) dlg->local_uri, (osip_from_t *) answer->from)
                && 0 == osip_from_compare (dlg->remote_uri, answer->to))
            return 0;
        return -1;
    }

    /* we don't have to compare
       remote_uri with from
       && local_uri with to.    ----> we have both tag recognized, it's enough..
     */
    if (0 == strcmp (tag_param_remote->gvalue, dlg->remote_tag))
        return 0;
    return -1;
}
Example #3
0
int
osip_dialog_update_tag_as_uac (osip_dialog_t * dialog, osip_message_t * response)
{
    osip_generic_param_t *tag;
    int i;

    i = osip_to_get_tag (response->to, &tag);
    if (i != 0)
    {
        OSIP_TRACE (osip_trace
                    (__FILE__, __LINE__, OSIP_WARNING, NULL,
                     "Remote UA is not compliant: missing a tag in response!\n"));
        dialog->remote_tag = NULL;
    }
    else
        dialog->remote_tag = osip_strdup (tag->gvalue);
    return 0;
}
Example #4
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;
}
Example #5
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;
}
static int
__osip_dialog_init (osip_dialog_t ** dialog, osip_message_t * invite, osip_message_t * response, osip_from_t * local, osip_to_t * remote, osip_message_t * remote_msg)
{
  int i;
  int pos;
  osip_generic_param_t *tag;

  *dialog = NULL;
  if (response == NULL)
    return OSIP_BADPARAMETER;
  if (response->cseq == NULL || local == NULL || remote == NULL)
    return OSIP_SYNTAXERROR;

  (*dialog) = (osip_dialog_t *) osip_malloc (sizeof (osip_dialog_t));
  if (*dialog == NULL)
    return OSIP_NOMEM;

  memset (*dialog, 0, sizeof (osip_dialog_t));
  (*dialog)->your_instance = NULL;

  if (MSG_IS_STATUS_2XX (response))
    (*dialog)->state = DIALOG_CONFIRMED;
  else                          /* 1XX */
    (*dialog)->state = DIALOG_EARLY;

  i = osip_call_id_to_str (response->call_id, &((*dialog)->call_id));
  if (i != 0) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not establish dialog!\n"));
    osip_dialog_free (*dialog);
    *dialog = NULL;
    return i;
  }

  i = osip_to_get_tag (local, &tag);
  if (i != 0) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not establish dialog!\n"));
    osip_dialog_free (*dialog);
    *dialog = NULL;
    return i;
  }

  (*dialog)->local_tag = osip_strdup (tag->gvalue);

  i = osip_from_get_tag (remote, &tag);
  if (i == 0)
    (*dialog)->remote_tag = osip_strdup (tag->gvalue);

  /* VR-2785: remember line value */
  if (invite) {
    osip_uri_param_t *line_param;

    i = osip_uri_param_get_byname (&invite->req_uri->url_params, "line", &line_param);
    if (i == 0 && line_param != NULL && line_param->gvalue != NULL)
      (*dialog)->line_param = osip_strdup (line_param->gvalue);
  }

  osip_list_init (&(*dialog)->route_set);

  pos = 0;
  while (!osip_list_eol (&response->record_routes, pos)) {
    osip_record_route_t *rr;
    osip_record_route_t *rr2;

    rr = (osip_record_route_t *) osip_list_get (&response->record_routes, pos);
    i = osip_record_route_clone (rr, &rr2);
    if (i != 0) {
      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not establish dialog!\n"));
      osip_dialog_free (*dialog);
      *dialog = NULL;
      return i;
    }
    if (invite == NULL)
      osip_list_add (&(*dialog)->route_set, rr2, 0);
    else
      osip_list_add (&(*dialog)->route_set, rr2, -1);

    pos++;
  }

  /* local_cseq is set to response->cseq->number for better
     handling of bad UA */
  (*dialog)->local_cseq = osip_atoi (response->cseq->number);

  i = osip_from_clone (remote, &((*dialog)->remote_uri));
  if (i != 0) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not establish dialog!\n"));
    osip_dialog_free (*dialog);
    *dialog = NULL;
    return i;
  }

  i = osip_to_clone (local, &((*dialog)->local_uri));
  if (i != 0) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not establish dialog!\n"));
    osip_dialog_free (*dialog);
    *dialog = NULL;
    return i;
  }

  {
    osip_contact_t *contact;

    if (!osip_list_eol (&remote_msg->contacts, 0)) {
      contact = osip_list_get (&remote_msg->contacts, 0);
      i = osip_contact_clone (contact, &((*dialog)->remote_contact_uri));
      if (i != 0) {
        OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not establish dialog!\n"));
        osip_dialog_free (*dialog);
        *dialog = NULL;
        return i;
      }
    }
    else {
      (*dialog)->remote_contact_uri = NULL;
      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "Remote UA is not compliant: missing a contact in remote message!\n"));
    }
  }
  (*dialog)->secure = -1;       /* non secure */

  return OSIP_SUCCESS;
}
int
osip_dialog_match_as_uac (osip_dialog_t * dlg, osip_message_t * answer)
{
  osip_generic_param_t *tag_param_local;
  osip_generic_param_t *tag_param_remote;
  char *tmp;
  int i;

  if (dlg == NULL || dlg->call_id == NULL)
    return OSIP_BADPARAMETER;
  if (answer == NULL || answer->call_id == NULL || answer->from == NULL || answer->to == NULL)
    return OSIP_BADPARAMETER;

  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "Using this method is discouraged. See source code explanations!\n"));
  /*
     When starting a new transaction and when receiving several answers,
     you must be prepared to receive several answers from different sources.
     (because of forking).

     Because some UAs are not compliant (a to tag is missing!), this method
     may match the wrong dialog when a dialog has been created with an empty
     tag in the To header.

     Personnaly, I would recommend to discard 1xx>=101 answers without To tags!
     Just my own feelings.
   */
  osip_call_id_to_str (answer->call_id, &tmp);
  if (0 != strcmp (dlg->call_id, tmp)) {
    osip_free (tmp);
    return OSIP_UNDEFINED_ERROR;
  }
  osip_free (tmp);

  /* for INCOMING RESPONSE:
     To: remote_uri;remote_tag
     From: local_uri;local_tag           <- LOCAL TAG ALWAYS EXIST
   */
  i = osip_from_get_tag (answer->from, &tag_param_local);
  if (i != 0)
    return OSIP_SYNTAXERROR;
  if (dlg->local_tag == NULL)
    /* NOT POSSIBLE BECAUSE I MANAGE REMOTE_TAG AND I ALWAYS ADD IT! */
    return OSIP_SYNTAXERROR;
  if (0 != strcmp (tag_param_local->gvalue, dlg->local_tag))
    return OSIP_UNDEFINED_ERROR;

  i = osip_to_get_tag (answer->to, &tag_param_remote);
  if (i != 0 && dlg->remote_tag != NULL)        /* no tag in response but tag in dialog */
    return OSIP_SYNTAXERROR;    /* impossible... */
  if (i != 0 && dlg->remote_tag == NULL) {      /* no tag in response AND no tag in dialog */
    if (0 == osip_from_compare ((osip_from_t *) dlg->local_uri, (osip_from_t *) answer->from)
        && 0 == osip_from_compare (dlg->remote_uri, answer->to))
      return OSIP_SUCCESS;
    return OSIP_UNDEFINED_ERROR;
  }

  if (dlg->remote_tag == NULL) {        /* tag in response BUT no tag in dialog */
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "Remote UA is not compliant: missing a tag in To fields!\n"));
    if (0 == osip_from_compare ((osip_from_t *) dlg->local_uri, (osip_from_t *) answer->from)
        && 0 == osip_from_compare (dlg->remote_uri, answer->to))
      return OSIP_SUCCESS;
    return OSIP_UNDEFINED_ERROR;
  }

  /* we don't have to compare
     remote_uri with from
     && local_uri with to.    ----> we have both tag recognized, it's enough..
   */
  if (0 == strcmp (tag_param_remote->gvalue, dlg->remote_tag))
    return OSIP_SUCCESS;
  return OSIP_UNDEFINED_ERROR;
}
Example #8
0
int
_eXosip_build_response_default (osip_message_t ** dest,
                                osip_dialog_t * dialog, int status,
                                osip_message_t * request)
{
  osip_generic_param_t *tag;
  osip_message_t *response;
  int pos;
  int i;

  *dest = NULL;
  if (request == NULL)
    return -1;

  i = osip_message_init (&response);
  if (i != 0)
    return -1;
  /* initialise osip_message_t structure */
  /* yet done... */

  response->sip_version = (char *) osip_malloc (8 * sizeof (char));
  sprintf (response->sip_version, "SIP/2.0");
  osip_message_set_status_code (response, status);

  /* handle some internal reason definitions. */
  if (MSG_IS_NOTIFY (request) && status == 481)
    {
      response->reason_phrase = osip_strdup ("Subcription Does Not Exist");
  } else if (MSG_IS_SUBSCRIBE (request) && status == 202)
    {
      response->reason_phrase = osip_strdup ("Accepted subscription");
  } else
    {
      response->reason_phrase = osip_strdup (osip_message_get_reason (status));
      if (response->reason_phrase == NULL)
        {
          if (response->status_code == 101)
            response->reason_phrase = osip_strdup ("Dialog Establishement");
          else
            response->reason_phrase = osip_strdup ("Unknown code");
        }
      response->req_uri = NULL;
      response->sip_method = NULL;
    }

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

  i = osip_to_get_tag (response->to, &tag);
  if (i != 0)
    {                           /* we only add a tag if it does not already contains one! */
      if ((dialog != NULL) && (dialog->local_tag != NULL))
        /* it should contain the local TAG we created */
        {
          osip_to_set_tag (response->to, osip_strdup (dialog->local_tag));
      } else
        {
          if (status != 100)
            osip_to_set_tag (response->to, osip_to_tag_new_random ());
        }
    }

  i = osip_from_clone (request->from, &(response->from));
  if (i != 0)
    goto grd_error_1;

  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 grd_error_1;
      osip_list_add (response->vias, via2, -1);
      pos++;
    }

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

  if (MSG_IS_SUBSCRIBE (request))
    {
      osip_header_t *exp;
      osip_header_t *evt_hdr;

      osip_message_header_get_byname (request, "event", 0, &evt_hdr);
      if (evt_hdr != NULL && evt_hdr->hvalue != NULL)
        osip_message_set_header (response, "Event", evt_hdr->hvalue);
      else
        osip_message_set_header (response, "Event", "presence");
      i = osip_message_get_expires (request, 0, &exp);
      if (exp == NULL)
        {
          osip_header_t *cp;

          i = osip_header_clone (exp, &cp);
          if (cp != NULL)
            osip_list_add (response->headers, cp, 0);
        }
    }

  osip_message_set_allow (response, "INVITE");
  osip_message_set_allow (response, "ACK");
  osip_message_set_allow (response, "OPTIONS");
  osip_message_set_allow (response, "CANCEL");
  osip_message_set_allow (response, "BYE");
  osip_message_set_allow (response, "SUBSCRIBE");
  osip_message_set_allow (response, "NOTIFY");
  osip_message_set_allow (response, "MESSAGE");
  osip_message_set_allow (response, "INFO");
  osip_message_set_allow (response, "REFER");
  osip_message_set_allow (response, "UPDATE");

  *dest = response;
  return 0;

grd_error_1:
  osip_message_free (response);
  return -1;
}
Example #9
0
int
_eXosip_build_response_default (struct eXosip_t *excontext, osip_message_t ** dest, osip_dialog_t * dialog, int status, osip_message_t * request)
{
  osip_generic_param_t *tag;
  osip_message_t *response;
  int i;

  *dest = NULL;
  if (request == NULL)
    return OSIP_BADPARAMETER;

  i = osip_message_init (&response);
  if (i != 0)
    return i;
  /* initialise osip_message_t structure */
  /* yet done... */

  response->sip_version = (char *) osip_malloc (8 * sizeof (char));
  if (response->sip_version == NULL) {
    osip_message_free (response);
    return OSIP_NOMEM;
  }
  sprintf (response->sip_version, "SIP/2.0");
  osip_message_set_status_code (response, status);

#ifndef MINISIZE
  /* handle some internal reason definitions. */
  if (MSG_IS_NOTIFY (request) && status == 481) {
    response->reason_phrase = osip_strdup ("Subscription Does Not Exist");
  }
  else if (MSG_IS_SUBSCRIBE (request) && status == 202) {
    response->reason_phrase = osip_strdup ("Accepted subscription");
  }
  else {
    response->reason_phrase = osip_strdup (osip_message_get_reason (status));
    if (response->reason_phrase == NULL) {
      if (response->status_code == 101)
        response->reason_phrase = osip_strdup ("Dialog Establishement");
      else
        response->reason_phrase = osip_strdup ("Unknown code");
    }
    response->req_uri = NULL;
    response->sip_method = NULL;
  }
#else
  response->reason_phrase = osip_strdup (osip_message_get_reason (status));
  if (response->reason_phrase == NULL) {
    if (response->status_code == 101)
      response->reason_phrase = osip_strdup ("Dialog Establishement");
    else
      response->reason_phrase = osip_strdup ("Unknown code");
  }
  response->req_uri = NULL;
  response->sip_method = NULL;
#endif

  if (response->reason_phrase == NULL) {
    osip_message_free (response);
    return OSIP_NOMEM;
  }

  i = osip_to_clone (request->to, &(response->to));
  if (i != 0) {
    osip_message_free (response);
    return i;
  }

  i = osip_to_get_tag (response->to, &tag);
  if (i != 0) {                 /* we only add a tag if it does not already contains one! */
    if ((dialog != NULL) && (dialog->local_tag != NULL))
      /* it should contain the local TAG we created */
    {
      osip_to_set_tag (response->to, osip_strdup (dialog->local_tag));
    }
    else {
      if (status != 100)
        osip_to_set_tag (response->to, _eXosip_malloc_new_random ());
    }
  }

  i = osip_from_clone (request->from, &(response->from));
  if (i != 0) {
    osip_message_free (response);
    return i;
  }

  {
    osip_list_iterator_t it;
    osip_via_t *via = (osip_via_t*)osip_list_get_first(&request->vias, &it);

    while (via != NULL) {
      osip_via_t *via2;

      i = osip_via_clone (via, &via2);
      if (i != 0) {
        osip_message_free (response);
        return i;
      }
      osip_list_add (&response->vias, via2, -1);
      via = (osip_via_t *)osip_list_get_next(&it);
    }
  }

  i = osip_call_id_clone (request->call_id, &(response->call_id));
  if (i != 0) {
    osip_message_free (response);
    return i;
  }
  i = osip_cseq_clone (request->cseq, &(response->cseq));
  if (i != 0) {
    osip_message_free (response);
    return i;
  }
#ifndef MINISIZE
  if (MSG_IS_SUBSCRIBE (request)) {
    osip_header_t *exp;
    osip_header_t *evt_hdr;

    osip_message_header_get_byname (request, "event", 0, &evt_hdr);
    if (evt_hdr != NULL && evt_hdr->hvalue != NULL)
      osip_message_set_header (response, "Event", evt_hdr->hvalue);
    else
      osip_message_set_header (response, "Event", "presence");
    i = osip_message_get_expires (request, 0, &exp);
    if (exp == NULL) {
      osip_header_t *cp;

      i = osip_header_clone (exp, &cp);
      if (cp != NULL)
        osip_list_add (&response->headers, cp, 0);
    }
  }
#endif

  osip_message_set_user_agent (response, excontext->user_agent);

  *dest = response;
  return OSIP_SUCCESS;
}
Example #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 = 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;
}
Example #11
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;
}
Example #12
0
int
osip_dialog_init_as_uas (osip_dialog_t ** dialog, osip_message_t * invite, osip_message_t * response)
{
    int i;
    int pos;
    osip_generic_param_t *tag;

    (*dialog) = (osip_dialog_t *) osip_malloc (sizeof (osip_dialog_t));
    if (*dialog == NULL)
        return -1;

    (*dialog)->type = CALLEE;
    if (MSG_IS_STATUS_2XX (response))
        (*dialog)->state = DIALOG_CONFIRMED;
    else				/* 1XX */
        (*dialog)->state = DIALOG_EARLY;

    i = osip_call_id_to_str (response->call_id, &((*dialog)->call_id));
    if (i != 0)
        goto diau_error_0;

    i = osip_to_get_tag (response->to, &tag);
    if (i != 0)
        goto diau_error_1;
    (*dialog)->local_tag = osip_strdup (tag->gvalue);

    i = osip_from_get_tag (response->from, &tag);
    if (i != 0)
    {
        OSIP_TRACE (osip_trace
                    (__FILE__, __LINE__, OSIP_WARNING, NULL,
                     "Remote UA is not compliant: missing a tag in response!\n"));
        (*dialog)->remote_tag = NULL;
    }
    else
        (*dialog)->remote_tag = osip_strdup (tag->gvalue);

    (*dialog)->route_set = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
    osip_list_init ((*dialog)->route_set);

    pos = 0;
    while (!osip_list_eol (response->record_routes, pos))
    {
        osip_record_route_t *rr;
        osip_record_route_t *rr2;

        rr = (osip_record_route_t *) osip_list_get (response->record_routes, pos);
        i = osip_record_route_clone (rr, &rr2);
        if (i != 0)
            goto diau_error_2;
        osip_list_add ((*dialog)->route_set, rr2, -1);
        pos++;
    }

    /* local_cseq is set to response->cseq->number for better
       handling of bad UA */
    (*dialog)->local_cseq = osip_atoi (response->cseq->number);
    (*dialog)->remote_cseq = osip_atoi (response->cseq->number);


    i = osip_from_clone (response->from, &((*dialog)->remote_uri));
    if (i != 0)
        goto diau_error_3;

    i = osip_to_clone (response->to, &((*dialog)->local_uri));
    if (i != 0)
        goto diau_error_4;

    {
        osip_contact_t *contact;

        if (!osip_list_eol (invite->contacts, 0))
        {
            contact = osip_list_get (invite->contacts, 0);
            i = osip_contact_clone (contact, &((*dialog)->remote_contact_uri));
            if (i != 0)
                goto diau_error_5;
        }
        else
        {
            (*dialog)->remote_contact_uri = NULL;
            OSIP_TRACE (osip_trace
                        (__FILE__, __LINE__, OSIP_WARNING, NULL,
                         "Remote UA is not compliant: missing a contact in response!\n"));
        }
    }
    (*dialog)->secure = -1;	/* non secure */

    return 0;

diau_error_5:
    osip_from_free ((*dialog)->local_uri);
diau_error_4:
    osip_from_free ((*dialog)->remote_uri);
diau_error_3:
diau_error_2:
    osip_list_special_free ((*dialog)->route_set,
                            (void *(*)(void *)) &osip_record_route_free);
    osip_free ((*dialog)->remote_tag);
    osip_free ((*dialog)->local_tag);
diau_error_1:
    osip_free ((*dialog)->call_id);
diau_error_0:
    OSIP_TRACE (osip_trace
                (__FILE__, __LINE__, OSIP_ERROR, NULL,
                 "Could not establish dialog!\n"));
    osip_free (*dialog);
    *dialog = NULL;
    return -1;
}
Example #13
0
    /* we don't have to compare
       remote_uri with from
       && local_uri with to.    ----> we have both tag recognized, it's enough..
     */
    if (0 == strcmp (tag_param_remote->gvalue, dlg->remote_tag))
        return 0;

    return -1;
}

int
osip_dialog_init_as_uac (osip_dialog_t ** dialog, osip_message_t * response)
{
    int i;
    int pos;
    osip_generic_param_t *tag;

    *dialog = NULL;
    i = osip_to_get_tag (response->to, &tag);
    if (i != 0)
    {
        OSIP_TRACE (osip_trace
                    (__FILE__, __LINE__, OSIP_WARNING, NULL,
                     "Remote UA is not compliant: missing a tag in response!\n"));
        return -1;
    }

    (*dialog) = (osip_dialog_t *) osip_malloc (sizeof (osip_dialog_t));
    if (*dialog == NULL)
        return -1;

    (*dialog)->type = CALLER;
    if (MSG_IS_STATUS_2XX (response))
        (*dialog)->state = DIALOG_CONFIRMED;
    else				/* 1XX */
        (*dialog)->state = DIALOG_EARLY;

    i = osip_call_id_to_str (response->call_id, &((*dialog)->call_id));
    if (i != 0)
        goto diau_error_0;

    i = osip_from_get_tag (response->from, &tag);
    if (i != 0)
        goto diau_error_1;
    (*dialog)->local_tag = osip_strdup (tag->gvalue);

    i = osip_to_get_tag (response->to, &tag);
    if (i != 0)
    {
        OSIP_TRACE (osip_trace
                    (__FILE__, __LINE__, OSIP_WARNING, NULL,
                     "Remote UA is not compliant: missing a tag in response!\n"));
        (*dialog)->remote_tag = NULL;
    }
    else
        (*dialog)->remote_tag = osip_strdup (tag->gvalue);

    (*dialog)->route_set = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
    osip_list_init ((*dialog)->route_set);

    pos = 0;
    while (!osip_list_eol (response->record_routes, pos))
    {
        osip_record_route_t *rr;
        osip_record_route_t *rr2;

        rr = (osip_record_route_t *) osip_list_get (response->record_routes, pos);
        i = osip_record_route_clone (rr, &rr2);
        if (i != 0)
            goto diau_error_2;
        osip_list_add ((*dialog)->route_set, rr2, -1);
        pos++;
    }

    (*dialog)->local_cseq = osip_atoi (response->cseq->number);
    (*dialog)->remote_cseq = -1;

    i = osip_to_clone (response->to, &((*dialog)->remote_uri));
    if (i != 0)
        goto diau_error_3;

    i = osip_from_clone (response->from, &((*dialog)->local_uri));
    if (i != 0)
        goto diau_error_4;

    {
        osip_contact_t *contact;

        if (!osip_list_eol (response->contacts, 0))
        {
            contact = osip_list_get (response->contacts, 0);
            i = osip_contact_clone (contact, &((*dialog)->remote_contact_uri));
            if (i != 0)
                goto diau_error_5;
        }
        else
        {
            (*dialog)->remote_contact_uri = NULL;
            OSIP_TRACE (osip_trace
                        (__FILE__, __LINE__, OSIP_WARNING, NULL,
                         "Remote UA is not compliant: missing a contact in response!\n"));
        }
    }
    (*dialog)->secure = -1;	/* non secure */

    return 0;

diau_error_5:
    osip_from_free ((*dialog)->local_uri);
diau_error_4:
    osip_from_free ((*dialog)->remote_uri);
diau_error_3:
diau_error_2:
    osip_list_special_free ((*dialog)->route_set,
                            (void *(*)(void *)) &osip_record_route_free);
    osip_free ((*dialog)->remote_tag);
    osip_free ((*dialog)->local_tag);
diau_error_1:
    osip_free ((*dialog)->call_id);
diau_error_0:
    OSIP_TRACE (osip_trace
                (__FILE__, __LINE__, OSIP_ERROR, NULL,
                 "Could not establish dialog!\n"));
    osip_free (*dialog);
    *dialog = NULL;
    return -1;
}

#if 1 /* SIPIT13 */
int
osip_dialog_init_as_uac_with_remote_request (osip_dialog_t ** dialog, osip_message_t *next_request, int local_cseq)
{
    int i;
    osip_generic_param_t *tag;

    *dialog = NULL;
    i = osip_to_get_tag (next_request->from, &tag);
    if (i != 0)
    {
        OSIP_TRACE (osip_trace
                    (__FILE__, __LINE__, OSIP_WARNING, NULL,
                     "Remote UA is not compliant: missing a tag in next request!\n"));
        return -1;
    }

    (*dialog) = (osip_dialog_t *) osip_malloc (sizeof (osip_dialog_t));
    if (*dialog == NULL)
        return -1;

    (*dialog)->type = CALLER;
#if 0
    (*dialog)->state = DIALOG_CONFIRMED;
#endif
    (*dialog)->state = DIALOG_EARLY;

    i = osip_call_id_to_str (next_request->call_id, &((*dialog)->call_id));
    if (i != 0)
        goto diau_error_0;

    i = osip_from_get_tag (next_request->to, &tag);
    if (i != 0)
        goto diau_error_1;
    (*dialog)->local_tag = osip_strdup (tag->gvalue);

    i = osip_to_get_tag (next_request->from, &tag);
    if (i != 0)
    {
        OSIP_TRACE (osip_trace
                    (__FILE__, __LINE__, OSIP_WARNING, NULL,
                     "Remote UA is not compliant: missing a tag in next request!\n"));
        (*dialog)->remote_tag = NULL;
    }
    else
        (*dialog)->remote_tag = osip_strdup (tag->gvalue);

    (*dialog)->route_set = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
    osip_list_init ((*dialog)->route_set);

    (*dialog)->local_cseq = local_cseq; /* -1 osip_atoi (xxx->cseq->number); */
    (*dialog)->remote_cseq = osip_atoi (next_request->cseq->number);

    i = osip_to_clone (next_request->from, &((*dialog)->remote_uri));
    if (i != 0)
        goto diau_error_3;

    i = osip_from_clone (next_request->to, &((*dialog)->local_uri));
    if (i != 0)
        goto diau_error_4;

    (*dialog)->secure = -1;	/* non secure */

    return 0;

diau_error_4:
    osip_from_free ((*dialog)->remote_uri);
diau_error_3:
    osip_free ((*dialog)->remote_tag);
    osip_free ((*dialog)->local_tag);
diau_error_1:
    osip_free ((*dialog)->call_id);
diau_error_0:
    OSIP_TRACE (osip_trace
                (__FILE__, __LINE__, OSIP_ERROR, NULL,
                 "Could not establish dialog!\n"));
    osip_free (*dialog);
    *dialog = NULL;
    return -1;
}
Example #14
0
int
_eXosip_subscribe_send_request_with_credential (struct eXosip_t *excontext, eXosip_subscribe_t * js, eXosip_dialog_t * jd, osip_transaction_t * out_tr)
{
  osip_transaction_t *tr = NULL;
  osip_message_t *msg = NULL;
  osip_event_t *sipevent;

  int cseq;
  osip_via_t *via;
  int i;

  if (js == NULL)
    return OSIP_BADPARAMETER;
  if (jd != NULL) {
    if (jd->d_out_trs == NULL)
      return OSIP_BADPARAMETER;
  }

  if (out_tr == NULL) {
    out_tr = _eXosip_find_last_out_subscribe (js, jd);
  }

  if (out_tr == NULL || out_tr->orig_request == NULL || out_tr->last_response == NULL)
    return OSIP_NOTFOUND;

  i = osip_message_clone (out_tr->orig_request, &msg);
  if (i != 0) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for authentication\n"));
    return i;
  }

  {
    osip_generic_param_t *tag = NULL;

    osip_to_get_tag (msg->to, &tag);
    if (NULL == tag && jd != NULL && jd->d_dialog != NULL && jd->d_dialog->remote_tag != NULL) {
      osip_to_set_tag (msg->to, osip_strdup (jd->d_dialog->remote_tag));
    }
  }

  via = (osip_via_t *) osip_list_get (&msg->vias, 0);
  if (via == NULL || msg->cseq == NULL || msg->cseq->number == NULL) {
    osip_message_free (msg);
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: missing via or cseq header\n"));
    return OSIP_SYNTAXERROR;
  }

  /* increment cseq */
  cseq = atoi (msg->cseq->number);
  osip_free (msg->cseq->number);
  msg->cseq->number = _eXosip_strdup_printf ("%i", cseq + 1);
  if (msg->cseq->number == NULL) {
    osip_message_free (msg);
    return OSIP_NOMEM;
  }

  if (jd != NULL && jd->d_dialog != NULL) {
    jd->d_dialog->local_cseq++;
  }

  i = _eXosip_update_top_via (msg);
  if (i != 0) {
    osip_message_free (msg);
    return i;
  }

  osip_list_special_free (&msg->authorizations, (void (*)(void *)) &osip_authorization_free);
  osip_list_special_free (&msg->proxy_authorizations, (void (*)(void *)) &osip_proxy_authorization_free);

  if (out_tr->last_response->status_code == 401 || out_tr->last_response->status_code == 407) {
    _eXosip_add_authentication_information (excontext, msg, out_tr->last_response);
  }
  else
    _eXosip_add_authentication_information (excontext, msg, NULL);


  if (out_tr != NULL && out_tr->last_response != NULL && out_tr->last_response->status_code == 423) {
    /* increase expires value to "min-expires" value */
    osip_header_t *exp;
    osip_header_t *min_exp;

    osip_message_header_get_byname (msg, "expires", 0, &exp);
    osip_message_header_get_byname (out_tr->last_response, "min-expires", 0, &min_exp);
    if (exp != NULL && exp->hvalue != NULL && min_exp != NULL && min_exp->hvalue != NULL) {
      osip_free (exp->hvalue);
      exp->hvalue = osip_strdup (min_exp->hvalue);
    }
    else {
      osip_message_free (msg);
      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: missing Min-Expires or Expires in PUBLISH\n"));
      return OSIP_SYNTAXERROR;
    }
  }


  osip_message_force_update (msg);

  i = _eXosip_transaction_init (excontext, &tr, NICT, excontext->j_osip, msg);

  if (i != 0) {
    osip_message_free (msg);
    return i;
  }

  if (out_tr == js->s_out_tr) {
    /* replace with the new tr */
    osip_list_add (&excontext->j_transactions, js->s_out_tr, 0);
    js->s_out_tr = tr;
  }
  else {
    /* add the new tr for the current dialog */
    osip_list_add (jd->d_out_trs, tr, 0);
  }

  sipevent = osip_new_outgoing_sipmessage (msg);

  osip_transaction_set_reserved5 (tr, js);
  osip_transaction_set_reserved3 (tr, jd);

  osip_transaction_add_event (tr, sipevent);

  _eXosip_update (excontext);   /* fixed? */
  _eXosip_wakeup (excontext);
  return OSIP_SUCCESS;
}
Example #15
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;
}
Example #16
0
int
_eXosip_call_retry_request (eXosip_call_t * jc,
                            eXosip_dialog_t * jd, osip_transaction_t * out_tr)
{
  osip_transaction_t *tr = NULL;
  osip_message_t *msg = NULL;
  osip_event_t *sipevent;

  int cseq;
  osip_via_t *via;
  osip_contact_t *co;
  int pos;
  int i;
  int protocol = IPPROTO_UDP;

  if (jc == NULL)
    return OSIP_BADPARAMETER;
  if (jd != NULL)
    {
      if (jd->d_out_trs == NULL)
        return OSIP_BADPARAMETER;
    }
  if (out_tr == NULL
      || out_tr->orig_request == NULL || out_tr->last_response == NULL)
    return OSIP_BADPARAMETER;

  i = osip_message_clone (out_tr->orig_request, &msg);
  if (i != 0)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: could not clone msg for authentication\n"));
      return i;
    }

  via = (osip_via_t *) osip_list_get (&msg->vias, 0);
  if (via == NULL || msg->cseq == NULL || msg->cseq->number == NULL)
    {
      osip_message_free (msg);
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: missing via or cseq header\n"));
      return OSIP_SYNTAXERROR;
    }

  if (MSG_IS_STATUS_3XX (out_tr->last_response))
    {
      co = NULL;
      pos = 0;
      while (!osip_list_eol (&out_tr->last_response->contacts, pos))
        {
          co =
            (osip_contact_t *) osip_list_get (&out_tr->last_response->contacts,
                                              pos);
          if (co != NULL && co->url != NULL)
            {
              /* check tranport? Only allow UDP, right now */
              osip_uri_param_t *u_param;
              int pos2;

              u_param = NULL;
              pos2 = 0;
              while (!osip_list_eol (&co->url->url_params, pos2))
                {
                  u_param =
                    (osip_uri_param_t *) osip_list_get (&co->url->url_params,
                                                        pos2);
                  if (u_param == NULL || u_param->gname == NULL
                      || u_param->gvalue == NULL)
                    {
                      u_param = NULL;
                      /* skip */
                  } else if (0 == osip_strcasecmp (u_param->gname, "transport"))
                    {
                      if (0 == osip_strcasecmp (u_param->gvalue, "udp"))
                        {
                          u_param = NULL;
                          protocol = IPPROTO_UDP;
                          break;        /* ok */
                      } else if (0 == osip_strcasecmp (u_param->gvalue, "tcp"))
                        {
                          protocol = IPPROTO_TCP;
                          u_param = NULL;
                        }
                      break;
                    }
                  pos2++;
                }

              if (u_param == NULL || u_param->gname == NULL
                  || u_param->gvalue == NULL)
                {
                  break;        /* default is udp! */
                }
            }
          pos++;
          co = NULL;
        }

      if (co == NULL || co->url == NULL)
        {
          osip_message_free (msg);
          OSIP_TRACE (osip_trace
                      (__FILE__, __LINE__, OSIP_ERROR, NULL,
                       "eXosip: contact header\n"));
          return OSIP_SYNTAXERROR;
        }

      /* TODO:
         remove extra parameter from new request-uri
         check usual parameter like "transport"
       */

      if (msg->req_uri != NULL && msg->req_uri->host != NULL
          && co->url->host != NULL
          && 0 == osip_strcasecmp (co->url->host, msg->req_uri->host))
        {
          osip_uri_param_t *maddr_param = NULL;
          osip_uri_uparam_get_byname (co->url, "maddr", &maddr_param);
          if (maddr_param != NULL && maddr_param->gvalue != NULL)
            {
              /* This is a redirect server, the route should probably be removed? */
              osip_route_t *route = NULL;
              osip_generic_param_t *tag = NULL;
              osip_message_get_route (msg, 0, &route);
              if (route != NULL)
                {
                  osip_to_get_tag (msg->to, &tag);
                  if (tag == NULL && route != NULL && route->url != NULL)
                    {
                      osip_list_remove (&msg->routes, 0);
                      osip_route_free (route);
                    }
                }
            }
        }

      /* replace request-uri with NEW contact address */
      osip_uri_free (msg->req_uri);
      msg->req_uri = NULL;
      osip_uri_clone (co->url, &msg->req_uri);

      /* support for diversions headers/draft! */
      {
        int count = 0;
        pos = 0;
        while (!osip_list_eol (&out_tr->last_response->headers, pos))
          {
            osip_header_t *copy = NULL;
            osip_header_t *head =
              osip_list_get (&out_tr->last_response->headers, pos);
            if (head != NULL && 0 == osip_strcasecmp (head->hname, "diversion"))
              {
                i = osip_header_clone (head, &copy);
                if (i == 0)
                  {
                    osip_list_add (&msg->headers, copy, count);
                    count++;
                  }
              }
            pos++;
          }
      }

    }
  /* remove all previous authentication headers */
  osip_list_special_free (&msg->authorizations,
                          (void *(*)(void *)) &osip_authorization_free);
  osip_list_special_free (&msg->proxy_authorizations,
                          (void *(*)(void *)) &osip_proxy_authorization_free);

  /* increment cseq */
  cseq = atoi (msg->cseq->number);
  osip_free (msg->cseq->number);
  msg->cseq->number = strdup_printf ("%i", cseq + 1);
  if (jd != NULL && jd->d_dialog != NULL)
    {
      jd->d_dialog->local_cseq++;
    }

  i = eXosip_update_top_via (msg);
  if (i != 0)
    {
      osip_message_free (msg);
      return i;
    }

  if (out_tr->last_response->status_code == 401
      || out_tr->last_response->status_code == 407)
    eXosip_add_authentication_information (msg, out_tr->last_response);
  else
    eXosip_add_authentication_information (msg, NULL);
  osip_message_force_update (msg);

  if (0 != osip_strcasecmp (msg->sip_method, "INVITE"))
    {
      i = _eXosip_transaction_init (&tr, NICT, eXosip.j_osip, msg);
  } else
    {
      i = _eXosip_transaction_init (&tr, ICT, eXosip.j_osip, msg);
    }

  if (i != 0)
    {
      osip_message_free (msg);
      return i;
    }

  if (out_tr == jc->c_out_tr)
    {
      /* replace with the new tr */
      osip_list_add (&eXosip.j_transactions, jc->c_out_tr, 0);
      jc->c_out_tr = tr;

      /* fix dialog issue */
      if (jd != NULL)
        {
          REMOVE_ELEMENT (jc->c_dialogs, jd);
          eXosip_dialog_free (jd);
          jd = NULL;
        }
  } else
    {
      /* add the new tr for the current dialog */
      osip_list_add (jd->d_out_trs, tr, 0);
    }

  sipevent = osip_new_outgoing_sipmessage (msg);

#ifndef MINISIZE
  osip_transaction_set_your_instance (tr, __eXosip_new_jinfo (jc, jd, NULL, NULL));
#else
  osip_transaction_set_your_instance (tr, __eXosip_new_jinfo (jc, jd));
#endif
  osip_transaction_add_event (tr, sipevent);

  eXosip_update ();             /* fixed? */
  __eXosip_wakeup ();
  return OSIP_SUCCESS;
}
Example #17
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;
}
Example #18
0
/*
 * SIP_CALCULATE_BRANCH
 *
 * Calculates a branch parameter according to RFC3261 section 16.11
 *
 * The returned 'id' will be HASHHEXLEN + strlen(magic_cookie)
 * characters (32 + 7) long. The caller must supply at least this
 * amount of space in 'id'.
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int  sip_calculate_branch_id (sip_ticket_t *ticket, char *id) {
/* RFC3261 section 16.11 recommends the following procedure:
 *   The stateless proxy MAY use any technique it likes to guarantee
 *   uniqueness of its branch IDs across transactions.  However, the
 *   following procedure is RECOMMENDED.  The proxy examines the
 *   branch ID in the topmost Via header field of the received
 *   request.  If it begins with the magic cookie, the first
 *   component of the branch ID of the outgoing request is computed
 *   as a hash of the received branch ID.  Otherwise, the first
 *   component of the branch ID is computed as a hash of the topmost
 *   Via, the tag in the To header field, the tag in the From header
 *   field, the Call-ID header field, the CSeq number (but not
 *   method), and the Request-URI from the received request.  One of
 *   these fields will always vary across two different
 *   transactions.
 *
 * The branch value will consist of:
 * - magic cookie "z9hG4bK"
 * - 1st part (unique calculated ID
 * - 2nd part (value for loop detection) <<- not yet used by siproxd
 */
   osip_message_t *sip_msg=ticket->sipmsg;
   static char *magic_cookie="z9hG4bK";
   osip_via_t *via;
   osip_uri_param_t *param=NULL;
   osip_call_id_t *call_id=NULL;
   HASHHEX hashstring;

   hashstring[0]='\0';

   /*
    * Examine topmost via and look for a magic cookie.
    * If it is there, I use THIS branch parameter as input for
    * our hash calculation
    */
   via = osip_list_get (sip_msg->vias, 0);
   if (via == NULL) {
      ERROR("have a SIP message without any via header");
      return STS_FAILURE;
   }

   param=NULL;
   osip_via_param_get_byname(via, "branch", &param);
   if (param && param->gvalue) {
      DEBUGC(DBCLASS_BABBLE, "looking for magic cookie [%s]",param->gvalue);
      if (strncmp(param->gvalue, magic_cookie,
                  strlen(magic_cookie))==0) {
         /* calculate MD5 hash */
         MD5_CTX Md5Ctx;
         HASH HA1;

         MD5Init(&Md5Ctx);
         MD5Update(&Md5Ctx, param->gvalue,
                   strlen(param->gvalue));
         MD5Final(HA1, &Md5Ctx);
         CvtHex(HA1, hashstring);

         DEBUGC(DBCLASS_BABBLE, "existing branch -> branch hash [%s]",
                hashstring);
      }
   }

   /*
    * If I don't have a branch parameter in the existing topmost via,
    * then I need:
    *   - the topmost via
    *   - the tag in the To header field
    *   - the tag in the From header field
    *   - the Call-ID header field
    *   - the CSeq number (but not method)
    *   - the Request-URI from the received request
    */
   if (hashstring[0] == '\0') {
      /* calculate MD5 hash */
      MD5_CTX Md5Ctx;
      HASH HA1;
      char *tmp;

      MD5Init(&Md5Ctx);

      /* topmost via */
      osip_via_to_str(via, &tmp);
      if (tmp) {
         MD5Update(&Md5Ctx, tmp, strlen(tmp));
         osip_free(tmp);
      }
     
      /* Tag in To header */
      osip_to_get_tag(sip_msg->to, &param);
      if (param && param->gvalue) {
         MD5Update(&Md5Ctx, param->gvalue, strlen(param->gvalue));
      }

      /* Tag in From header */
      osip_from_get_tag(sip_msg->from, &param);
      if (param && param->gvalue) {
         MD5Update(&Md5Ctx, param->gvalue, strlen(param->gvalue));
      }

      /* Call-ID */
      call_id = osip_message_get_call_id(sip_msg);
      osip_call_id_to_str(call_id, &tmp);
      if (tmp) {
         MD5Update(&Md5Ctx, tmp, strlen(tmp));
         osip_free(tmp);
      }

      /* CSeq number (but not method) */
      tmp = osip_cseq_get_number(sip_msg->cseq);
      if (tmp) {
         MD5Update(&Md5Ctx, tmp, strlen(tmp));
      }
 
      /* Request URI */
      osip_uri_to_str(sip_msg->req_uri, &tmp);
      if (tmp) {
         MD5Update(&Md5Ctx, tmp, strlen(tmp));
         osip_free(tmp);
      }

      MD5Final(HA1, &Md5Ctx);
      CvtHex(HA1, hashstring);

      DEBUGC(DBCLASS_BABBLE, "non-existing branch -> branch hash [%s]",
             hashstring);
   }

   /* include the magic cookie */
   sprintf(id, "%s%s", magic_cookie, hashstring);

   return STS_SUCCESS;
}