int
osip_content_type_clone(const osip_content_type_t * ctt,
						osip_content_type_t ** dest)
{
	int i;
	osip_content_type_t *ct;

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

	i = osip_content_type_init(&ct);
	if (i != 0)					/* allocation failed */
		return i;
	if (ctt->type != NULL)
		ct->type = osip_strdup(ctt->type);
	if (ctt->subtype != NULL)
		ct->subtype = osip_strdup(ctt->subtype);

	{
		int pos = 0;
		osip_generic_param_t *u_param;
		osip_generic_param_t *dest_param;

		while (!osip_list_eol(&ctt->gen_params, pos)) {
			u_param =
				(osip_generic_param_t *) osip_list_get(&ctt->gen_params, pos);
			i = osip_generic_param_clone(u_param, &dest_param);
			if (i != 0) {
				osip_content_type_free(ct);
				//osip_free(ct);
				return i;
			}
			osip_list_add(&ct->gen_params, dest_param, -1);
			pos++;
		}
	}
	*dest = ct;
	return OSIP_SUCCESS;
}
/* content-Encoding = token
   token possible values are gzip,compress,deflate
*/
int
osip_message_set_content_encoding (osip_message_t * sip, const char *hvalue)
{
  osip_content_encoding_t *content_encoding;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_content_encoding_init (&content_encoding);
  if (i != 0)
    return -1;
  i = osip_content_encoding_parse (content_encoding, hvalue);
  if (i != 0)
    {
      osip_content_encoding_free (content_encoding);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (sip->content_encodings, content_encoding, -1);
  return 0;
}
/* returns -1 on error. */
int
osip_message_set_via (osip_message_t * sip, const char *hvalue)
{
  osip_via_t *via;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_via_init (&via);
  if (i != 0)
    return -1;
  i = osip_via_parse (via, hvalue);
  if (i != 0)
    {
      osip_via_free (via);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (sip->vias, via, -1);
  return 0;
}
/* returns -1 on error. */
int
osip_message_set_record_route (osip_message_t * sip, const char *hvalue)
{
  osip_record_route_t *record_route;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_record_route_init (&record_route);
  if (i != 0)
    return -1;
  i = osip_record_route_parse (record_route, hvalue);
  if (i != 0)
    {
      osip_record_route_free (record_route);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (sip->record_routes, record_route, -1);
  return 0;
}
int
osip_message_set_error_info (osip_message_t * sip, const char *hvalue)
{
  osip_error_info_t *error_info;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_error_info_init (&error_info);
  if (i != 0)
    return -1;
  i = osip_error_info_parse (error_info, hvalue);
  if (i != 0)
    {
      osip_error_info_free (error_info);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (sip->error_infos, error_info, -1);
  return 0;
}
/* returns -1 on error. */
int
osip_message_set_proxy_authenticate (osip_message_t * sip, const char *hvalue)
{
  osip_proxy_authenticate_t *proxy_authenticate;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_proxy_authenticate_init (&(proxy_authenticate));
  if (i != 0)
    return -1;
  i = osip_proxy_authenticate_parse (proxy_authenticate, hvalue);
  if (i != 0)
    {
      osip_proxy_authenticate_free (proxy_authenticate);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (sip->proxy_authenticates, proxy_authenticate, -1);
  return 0;
}
Exemple #7
0
int
osip_message_set_allow (osip_message_t * sip, const char *hvalue)
{
  osip_allow_t *allow;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_allow_init (&allow);
  if (i != 0)
    return -1;
  i = osip_allow_parse (allow, hvalue);
  if (i != 0)
    {
      osip_allow_free (allow);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (&sip->allows, allow, -1);
  return 0;
}
int
osip_message_set_call_info (osip_message_t * sip, const char *hvalue)
{
  osip_call_info_t *call_info;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_call_info_init (&call_info);
  if (i != 0)
    return -1;
  i = osip_call_info_parse (call_info, hvalue);
  if (i != 0)                   /* allocation failed */
    {
      osip_call_info_free (call_info);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (sip->call_infos, call_info, -1);
  return 0;
}
int
osip_message_set_accept_language (osip_message_t * sip, const char *hvalue)
{
  osip_accept_language_t *accept_language;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return 0;

  i = osip_accept_language_init (&accept_language);
  if (i != 0)
    return -1;
  i = osip_accept_language_parse (accept_language, hvalue);
  if (i != 0)
    {
      osip_accept_language_free (accept_language);
      return -1;
    }
  sip->message_property = 2;
  osip_list_add (sip->accept_languages, accept_language, -1);
  return 0;
}
Exemple #10
0
int eXosip_options_send_request(struct eXosip_t *excontext, osip_message_t * options)
{
	osip_transaction_t *transaction;
	osip_event_t *sipevent;
	int i;

	i = _eXosip_transaction_init(excontext, &transaction, NICT, excontext->j_osip, options);
	if (i != 0) {
		osip_message_free(options);
		return i;
	}

	osip_list_add(&excontext->j_transactions, transaction, 0);

	sipevent = osip_new_outgoing_sipmessage(options);
	sipevent->transactionid = transaction->transactionid;

	osip_transaction_add_event(transaction, sipevent);

	_eXosip_wakeup(excontext);
	return OSIP_SUCCESS;
}
int
osip_call_info_clone (const osip_call_info_t * ctt, osip_call_info_t ** dest)
{
  int i;
  osip_call_info_t *ct;

  *dest = NULL;
  if (ctt == NULL)
    return -1;
  if (ctt->element == NULL)
    return -1;

  i = osip_call_info_init (&ct);
  if (i != 0)                   /* allocation failed */
    return -1;
  ct->element = osip_strdup (ctt->element);

  {
    int pos = 0;
    osip_generic_param_t *u_param;
    osip_generic_param_t *dest_param;

    while (!osip_list_eol (ctt->gen_params, pos))
      {
        u_param = (osip_generic_param_t *) osip_list_get (ctt->gen_params, pos);
        i = osip_generic_param_clone (u_param, &dest_param);
        if (i != 0)
          {
            osip_call_info_free (ct);
            return -1;
          }
        osip_list_add (ct->gen_params, dest_param, -1);
        pos++;
      }
  }
  *dest = ct;
  return 0;
}
/* returns -1 on error. */
int
osip_message_set_header (osip_message_t * sip, const char *hname,
                         const char *hvalue)
{
  osip_header_t *h;
  int i;

  if (sip == NULL || hname == NULL)
    return OSIP_BADPARAMETER;

  i = osip_header_init (&h);
  if (i != 0)
    return i;

  h->hname = (char *) osip_malloc (strlen (hname) + 1);

  if (h->hname == NULL)
    {
      osip_header_free (h);
      return OSIP_NOMEM;
    }
  osip_clrncpy (h->hname, hname, strlen (hname));

  if (hvalue != NULL)
    {                           /* some headers can be null ("subject:") */
      h->hvalue = (char *) osip_malloc (strlen (hvalue) + 1);
      if (h->hvalue == NULL)
        {
          osip_header_free (h);
          return OSIP_NOMEM;
        }
      osip_clrncpy (h->hvalue, hvalue, strlen (hvalue));
  } else
    h->hvalue = NULL;
  sip->message_property = 2;
  osip_list_add (&sip->headers, h, -1);
  return OSIP_SUCCESS;                     /* ok */
}
/* returns -1 on error. */
int
osip_message_set_authorization (osip_message_t * sip, const char *hvalue)
{
  osip_authorization_t *authorization;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return OSIP_SUCCESS;

  if (sip == NULL)
    return OSIP_BADPARAMETER;
  i = osip_authorization_init (&authorization);
  if (i != 0)
    return i;
  i = osip_authorization_parse (authorization, hvalue);
  if (i != 0) {
    osip_authorization_free (authorization);
    return i;
  }
  sip->message_property = 2;
  osip_list_add (&sip->authorizations, authorization, -1);
  return OSIP_SUCCESS;
}
/* returns -1 on error. */
int
osip_message_set_www_authenticate (osip_message_t * sip, const char *hvalue)
{
  osip_www_authenticate_t *www_authenticate;
  int i;

  if (hvalue == NULL || hvalue[0] == '\0')
    return OSIP_SUCCESS;

  if (sip == NULL)
    return OSIP_BADPARAMETER;
  i = osip_www_authenticate_init (&www_authenticate);
  if (i != 0)
    return i;
  i = osip_www_authenticate_parse (www_authenticate, hvalue);
  if (i != 0) {
    osip_www_authenticate_free (www_authenticate);
    return i;
  }
  sip->message_property = 2;
  osip_list_add (&sip->www_authenticates, www_authenticate, -1);
  return OSIP_SUCCESS;
}
int
osip_accept_encoding_clone (const osip_accept_encoding_t * ctt, osip_accept_encoding_t ** dest)
{
  int i;
  osip_accept_encoding_t *ct;

  *dest = NULL;
  if (ctt == NULL)
    return OSIP_BADPARAMETER;
  if (ctt->element == NULL)
    return OSIP_BADPARAMETER;

  i = osip_accept_encoding_init (&ct);
  if (i != 0)                   /* allocation failed */
    return i;
  ct->element = osip_strdup (ctt->element);
  if (ct->element == NULL) {
    osip_accept_encoding_free (ct);
    return OSIP_NOMEM;
  }
  {
    osip_generic_param_t *dest_param;
    osip_list_iterator_t it;
    osip_generic_param_t *u_param = (osip_generic_param_t*) osip_list_get_first(&ctt->gen_params, &it);
    while (u_param != OSIP_SUCCESS) {
      i = osip_generic_param_clone (u_param, &dest_param);
      if (i != 0) {
        osip_accept_encoding_free (ct);
        return i;
      }
      osip_list_add (&ct->gen_params, dest_param, -1);
      u_param = (osip_generic_param_t *) osip_list_get_next(&it);
    }
  }
  *dest = ct;
  return OSIP_SUCCESS;
}
Exemple #16
0
/* returns -1 on error. */
int osip_message_set_route(osip_message_t * sip, const char *hvalue)
{
	osip_route_t *route;
	int i;

	if (hvalue == NULL || hvalue[0] == '\0')
		return OSIP_SUCCESS;

#ifdef __VXWORKS_OS__
	i = osip_route_init2(&route);
#else
	i = osip_route_init(&route);
#endif
	if (i != 0)
		return i;
	i = osip_route_parse(route, hvalue);
	if (i != 0) {
		osip_route_free(route);
		return i;
	}
	sip->message_property = 2;
	osip_list_add(&sip->routes, route, -1);
	return OSIP_SUCCESS;
}
int
osip_body_set_header (osip_body_t * body, const char *hname,
		      const char *hvalue)
{
  osip_header_t *h;
  int i;

  if (body == NULL)
    return -1;
  if (hname == NULL)
    return -1;
  if (hvalue == NULL)
    return -1;

  i = osip_header_init (&h);
  if (i != 0)
    return -1;

  h->hname = osip_strdup (hname);
  h->hvalue = osip_strdup (hvalue);

  osip_list_add (body->headers, h, -1);
  return 0;
}
Exemple #18
0
osip_message_t* init_sip_msg_from_src (const osip_message_t *sipmsg, const int code)
{
    __tri(init_sip_msg_from_src);

    if (sipmsg->to && sipmsg->from)
    {
       osip_message_t* sipgen;
       osip_message_init (&sipgen);

       if (sipgen)
       {
           sipgen->message = NULL;
           osip_message_set_version (sipgen, osip_strdup ("SIP/2.0"));
           osip_message_set_status_code (sipgen, code);
           osip_message_set_reason_phrase (sipgen,
                                   osip_strdup(osip_message_get_reason (code)));


           if (code == SIP_MOVED_TEMPORARILY)
           {
               char contact[100];
                snprintf (contact, sizeof(contact), "<sip:%s@%s:%s>",
                          sipmsg->to->url->username, "sip.voipcheap.com", "5060");

                osip_message_set_contact(sipgen, contact);

                osip_to_clone   (sipmsg->to,   &sipgen->from);
                osip_from_clone (sipmsg->from, &sipgen->to);

           }
           else
           {
               /*include 1st contact header  if 3xx*/
               if (code < SIP_BAD_REQUEST && (SIP_OK == code || code >= SIP_MULTIPLE_CHOICES) )
               {
                   osip_contact_t* src_contact = NULL;
                   osip_message_get_contact(sipmsg, 0, &src_contact);

                   if (src_contact)
                   {
                       osip_contact_t* res_contact = NULL;
                       osip_contact_clone (src_contact, &res_contact);

                       if (res_contact)
                       {
                           osip_list_add(&(sipgen->contacts),res_contact,0);
                       }
                   }
               }

               osip_to_clone   (sipmsg->to,   &sipgen->to);
               osip_from_clone (sipmsg->from, &sipgen->from);
           }

           /* via headers */
           int pos = 0;
           while (!osip_list_eol (&sipmsg->vias, pos))
           {
               osip_via_t*via = (osip_via_t*)osip_list_get (&sipmsg->vias, pos);
               char *tmp;
               osip_via_to_str (via, &tmp);
               osip_message_set_via (sipgen, tmp);
               osip_free (tmp);
               pos++;
           }

           osip_call_id_clone (sipmsg->call_id, &sipgen->call_id);
           osip_cseq_clone    (sipmsg->cseq,    &sipgen->cseq);
           __tre(return) sipgen;
       }
   }
Exemple #19
0
static int
_eXosip_retry_with_auth (eXosip_dialog_t * jd, osip_transaction_t ** ptr,
                         int *retry)
{
  osip_transaction_t *out_tr = NULL;
  osip_transaction_t *tr = NULL;
  osip_message_t *msg = NULL;
  osip_event_t *sipevent;
  jinfo_t *ji = NULL;

  int cseq;
  osip_via_t *via;
  int i;

  if (!ptr)
    return -1;

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

  out_tr = *ptr;

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

  if (retry && (*retry >= 3))
    return -1;

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

  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 -1;
    }

  /* 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);
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: unsupported protocol\n"));
      return -1;
    }

  if (eXosip_add_authentication_information (msg, out_tr->last_response) < 0)
    {
      osip_message_free (msg);
      return -1;
    }

  osip_message_force_update (msg);

  if (MSG_IS_INVITE (msg))
    i = _eXosip_transaction_init (&tr, ICT, eXosip.j_osip, msg);
  else
    i = _eXosip_transaction_init (&tr, NICT, eXosip.j_osip, msg);

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

  /* replace with the new tr */
  if (MSG_IS_PUBLISH(msg))
    {
      /* old transaction is put in the garbage list */
      osip_list_add (eXosip.j_transactions, out_tr, 0);
      /* new transaction is put in the publish context */
      *ptr = tr;
    }
  else
    osip_list_add (eXosip.j_transactions, tr, 0);

  sipevent = osip_new_outgoing_sipmessage (msg);

  ji = osip_transaction_get_your_instance (out_tr);

  osip_transaction_set_your_instance (out_tr, NULL);
  osip_transaction_set_your_instance (tr, ji);
  osip_transaction_add_event (tr, sipevent);

  if (retry)
    (*retry)++;

  eXosip_update ();             /* fixed? */
  __eXosip_wakeup ();
  return 0;
}
Exemple #20
0
int
eXosip_add_authentication_information (osip_message_t * req,
                                       osip_message_t * last_response)
{
  osip_authorization_t *aut = NULL;
  osip_www_authenticate_t *wwwauth = NULL;
  osip_proxy_authorization_t *proxy_aut = NULL;
  osip_proxy_authenticate_t *proxyauth = NULL;
  jauthinfo_t *authinfo = NULL;
  int pos;
  int i;

  if (req == NULL
      || req->from == NULL
      || req->from->url == NULL || req->from->url->username == NULL)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_INFO2, NULL,
                   "authinfo: Invalid message\n"));
      return -1;
   }

  if (last_response==NULL)
  {
	  /* we can add all credential that belongs to the same call-id */
	struct eXosip_http_auth *http_auth;
	int pos;

	/* update entries with same call_id */
	for (pos=0;pos<MAX_EXOSIP_HTTP_AUTH;pos++)
	{
		http_auth = &eXosip.http_auths[pos];
		if (http_auth->pszCallId[0]=='\0')
			continue;
		if (osip_strcasecmp(http_auth->pszCallId, req->call_id->number)==0)
		{
			char *uri;

			authinfo = eXosip_find_authentication_info (req->from->url->username,
												  http_auth->wa->realm);
			if (authinfo == NULL)
			{
				OSIP_TRACE (osip_trace
						  (__FILE__, __LINE__, OSIP_INFO2, NULL,
						   "authinfo: No authentication found for %s %s\n",
						   req->from->url->username, http_auth->wa->realm));
				return -1;
			}

			i = osip_uri_to_str (req->req_uri, &uri);
			if (i != 0)
				return -1;

			http_auth->iNonceCount++;
			if (osip_strcasecmp(req->sip_method, "REGISTER")==0)
				i = __eXosip_create_authorization_header (http_auth->wa, uri,
													authinfo->userid,
													authinfo->passwd,
													authinfo->ha1, &aut,
													req->sip_method,
													http_auth->pszCNonce,
													http_auth->iNonceCount);
			else
				i = __eXosip_create_proxy_authorization_header (http_auth->wa, uri,
													authinfo->userid,
													authinfo->passwd,
													authinfo->ha1, &aut,
													req->sip_method,
													http_auth->pszCNonce,
													http_auth->iNonceCount);

			osip_free (uri);
			if (i != 0)
				return -1;

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

  pos = 0;
  osip_message_get_www_authenticate (last_response, pos, &wwwauth);
  osip_message_get_proxy_authenticate (last_response, pos, &proxyauth);
  if (wwwauth == NULL && proxyauth == NULL)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_INFO2, NULL,
                   "authinfo: No WWW-Authenticate or Proxy-Authenticate\n"));
      return -1;
    }

  while (wwwauth != NULL)
    {
      char *uri;

      authinfo = eXosip_find_authentication_info (req->from->url->username,
                                                  wwwauth->realm);
      if (authinfo == NULL)
        {
          OSIP_TRACE (osip_trace
                      (__FILE__, __LINE__, OSIP_INFO2, NULL,
                       "authinfo: No authentication found for %s %s\n",
                       req->from->url->username, wwwauth->realm));
          return -1;
        }

      i = osip_uri_to_str (req->req_uri, &uri);
      if (i != 0)
        return -1;

      i = __eXosip_create_authorization_header (wwwauth, uri,
                                                authinfo->userid,
                                                authinfo->passwd,
                                                authinfo->ha1, &aut,
                                                req->sip_method,
												"0a4f113b",
												1);
      osip_free (uri);
      if (i != 0)
        return -1;

      if (aut != NULL)
        {
          osip_list_add (&req->authorizations, aut, -1);
          osip_message_force_update (req);
        }

	  if (wwwauth->qop_options!=NULL)
	  {
		  if (osip_strcasecmp(req->sip_method, "REGISTER")==0
			  || osip_strcasecmp(req->sip_method, "INVITE")==0
  			  || osip_strcasecmp(req->sip_method, "SUBSCRIBE")==0)
		  	  _eXosip_store_nonce(req->call_id->number, wwwauth);
		  else
		  {
			  osip_generic_param_t *to_tag=NULL;
			  osip_from_param_get_byname (req->to, "tag", &to_tag);
			  if (to_tag!=NULL)
			  {
				  /* if message is part of a dialog */
			  	  _eXosip_store_nonce(req->call_id->number, wwwauth);
			  }
		  }
	  }

      pos++;
      osip_message_get_www_authenticate (last_response, pos, &wwwauth);
    }

  pos = 0;
  while (proxyauth != NULL)
    {
      char *uri;

      authinfo = eXosip_find_authentication_info (req->from->url->username,
                                                  proxyauth->realm);
      if (authinfo == NULL)
        {
          OSIP_TRACE (osip_trace
                      (__FILE__, __LINE__, OSIP_INFO2, NULL,
                       "authinfo: No authentication found for %s %s\n",
                       req->from->url->username, proxyauth->realm));
          return -1;
        }
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_INFO1, NULL,
                   "authinfo: %s\n", authinfo->username));
      i = osip_uri_to_str (req->req_uri, &uri);
      if (i != 0)
        return -1;

      i = __eXosip_create_proxy_authorization_header (proxyauth, uri,
                                                      authinfo->userid,
                                                      authinfo->passwd,
                                                      authinfo->ha1,
                                                      &proxy_aut, req->sip_method,
													  "0a4f113b",
													  1);
      osip_free (uri);
      if (i != 0)
        return -1;

      if (proxy_aut != NULL)
        {
          osip_list_add (&req->proxy_authorizations, proxy_aut, -1);
          osip_message_force_update (req);
        }

	  if (proxyauth->qop_options!=NULL)
	  {
		  if (osip_strcasecmp(req->sip_method, "REGISTER")==0
			  || osip_strcasecmp(req->sip_method, "INVITE")==0
  			  || osip_strcasecmp(req->sip_method, "SUBSCRIBE")==0)
		  	  _eXosip_store_nonce(req->call_id->number, proxyauth);
		  else
		  {
			  osip_generic_param_t *to_tag=NULL;
			  osip_from_param_get_byname (req->to, "tag", &to_tag);
			  if (to_tag!=NULL)
			  {
				  /* if message is part of a dialog */
			  	  _eXosip_store_nonce(req->call_id->number, proxyauth);
			  }
		  }
	  }

      pos++;
      osip_message_get_proxy_authenticate (last_response, pos, &proxyauth);
    }

  return 0;
}
void
__nict_load_fsm ()
{
  transition_t *transition;

  nict_fsm = (osip_statemachine_t *) osip_malloc (sizeof (osip_statemachine_t));
  nict_fsm->transitions = (osip_list_t *) osip_malloc (sizeof (osip_list_t));
  osip_list_init (nict_fsm->transitions);

  /* to avoid race conditions between timers and first request */
  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_PRE_TRYING;
  transition->type = SND_REQUEST;
  transition->method = (void (*)(void *, void *)) &nict_snd_request;
  osip_list_add (nict_fsm->transitions, transition, -1);
  /*
     transition         = (transition_t *) osip_malloc(sizeof(transition_t));
     transition->state  = NICT_TRYING;
     transition->type   = SND_REQUEST;
     transition->method = (void(*)(void *,void *))&nict_snd_request;
     osip_list_add(nict_fsm->transitions,transition,-1);
   */
  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = TIMEOUT_F;
  transition->method = (void (*)(void *, void *)) &osip_nict_timeout_f_event;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = TIMEOUT_E;
  transition->method = (void (*)(void *, void *)) &osip_nict_timeout_e_event;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = RCV_STATUS_1XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_1xx;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = RCV_STATUS_2XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_TRYING;
  transition->type = RCV_STATUS_3456XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = TIMEOUT_F;
  transition->method = (void (*)(void *, void *)) &osip_nict_timeout_f_event;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = TIMEOUT_E;
  transition->method = (void (*)(void *, void *)) &osip_nict_timeout_e_event;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = RCV_STATUS_1XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_1xx;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = RCV_STATUS_2XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_PROCEEDING;
  transition->type = RCV_STATUS_3456XX;
  transition->method = (void (*)(void *, void *)) &nict_rcv_23456xx;
  osip_list_add (nict_fsm->transitions, transition, -1);

  transition = (transition_t *) osip_malloc (sizeof (transition_t));
  transition->state = NICT_COMPLETED;
  transition->type = TIMEOUT_K;
  transition->method = (void (*)(void *, void *)) &osip_nict_timeout_k_event;
  osip_list_add (nict_fsm->transitions, transition, -1);

  /* these ccan be used to announce retransmission of 2xx and 3456xx
     For the state machine, it is completely useless...
     transition         = (transition_t *) osip_malloc(sizeof(transition_t));
     transition->state  = NICT_COMPLETED;
     transition->type   = RCV_STATUS_2XX;
     transition->method = (void(*)(void *,void *))&nict_rcv_23456xx2;
     osip_list_add(nict_fsm->transitions,transition,-1);

     transition         = (transition_t *) osip_malloc(sizeof(transition_t));
     transition->state  = NICT_COMPLETED;
     transition->type   = RCV_STATUS_3456XX;
     transition->method = (void(*)(void *,void *))&nict_rcv_23456xx2;
     osip_list_add(nict_fsm->transitions,transition,-1);
   */

}
Exemple #22
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;
}
Exemple #23
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;
}
Exemple #25
0
/* It is RECOMMENDED to only cancel INVITE request */
int
generating_cancel (osip_message_t ** dest, osip_message_t * request_cancelled)
{
  int i;
  osip_message_t *request;

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

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

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

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

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

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

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

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

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

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

  *dest = request;
  return OSIP_SUCCESS;
}
Exemple #26
0
static int
dialog_fill_route_set (osip_dialog_t * dialog, osip_message_t * request)
{
  /* if the pre-existing route set contains a "lr" (compliance
     with bis-08) then the req_uri should contains the remote target
     URI */
  int i;
  int pos = 0;
  osip_uri_param_t *lr_param;
  osip_route_t *route;
  char *last_route;

  /* AMD bug: fixed 17/06/2002 */

  route = (osip_route_t *) osip_list_get (&dialog->route_set, 0);

  osip_uri_uparam_get_byname (route->url, "lr", &lr_param);
  if (lr_param != NULL)         /* the remote target URI is the req_uri! */
    {
      i = osip_uri_clone (dialog->remote_contact_uri->url, &(request->req_uri));
      if (i != 0)
        return i;
      /* "[request] MUST includes a Route header field containing
         the route set values in order." */
      /* AMD bug: fixed 17/06/2002 */
      pos = 0;                  /* first element is at index 0 */
      while (!osip_list_eol (&dialog->route_set, pos))
        {
          osip_route_t *route2;

          route = osip_list_get (&dialog->route_set, pos);
          i = osip_route_clone (route, &route2);
          if (i != 0)
            return i;
          osip_list_add (&request->routes, route2, -1);
          pos++;
        }
      return OSIP_SUCCESS;
    }

  /* if the first URI of route set does not contain "lr", the req_uri
     is set to the first uri of route set */


  i = osip_uri_clone (route->url, &(request->req_uri));
  if (i != 0)
    return i;
  /* add the route set */
  /* "The UAC MUST add a route header field containing
     the remainder of the route set values in order. */
  pos = 0;                      /* yes it is */

  while (!osip_list_eol (&dialog->route_set, pos))      /* not the first one in the list */
    {
      osip_route_t *route2;

      route = osip_list_get (&dialog->route_set, pos);
      i = osip_route_clone (route, &route2);
      if (i != 0)
        return i;
      if (!osip_list_eol (&dialog->route_set, pos + 1))
        osip_list_add (&request->routes, route2, -1);
      else
        osip_route_free (route2);
      pos++;
    }

  /* The UAC MUST then place the remote target URI into
     the route header field as the last value */
  i = osip_uri_to_str (dialog->remote_contact_uri->url, &last_route);
  if (i != 0)
    return i;
  i = osip_message_set_route (request, last_route);
  osip_free (last_route);
  if (i != 0)
    {
      return i;
    }

  /* route header and req_uri set */
  return OSIP_SUCCESS;
}
Exemple #27
0
int
generating_register (eXosip_reg_t * jreg, osip_message_t ** reg, char *transport,
                     char *from, char *proxy, char *contact, int expires)
{
  int i;
  char locip[65];
  char firewall_ip[65];
  char firewall_port[10];
  if (eXosip.eXtl == NULL)
    return OSIP_NO_NETWORK;

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

  i =
    generating_request_out_of_dialog (reg, "REGISTER", NULL, transport, from,
                                      proxy);
  if (i != 0)
    return i;

  memset (locip, '\0', sizeof (locip));
  eXosip_guess_ip_for_via (eXosip.eXtl->proto_family, locip, 49);

  if (locip[0] == '\0')
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: no default interface defined\n"));
      osip_message_free (*reg);
      *reg = NULL;
      return OSIP_NO_NETWORK;
    }

  if (contact == NULL)
    {
      osip_contact_t *new_contact = NULL;
      osip_uri_t *new_contact_url = NULL;

      i = osip_contact_init (&new_contact);
      if (i == 0)
        i = osip_uri_init (&new_contact_url);

      new_contact->url = new_contact_url;

      if (i == 0 && (*reg)->from != NULL
          && (*reg)->from->url != NULL && (*reg)->from->url->username != NULL)
        {
          new_contact_url->username = osip_strdup ((*reg)->from->url->username);
        }

      if (i == 0 && (*reg)->from != NULL && (*reg)->from->url != NULL)
        {
          /* serach for correct ip */
          if (firewall_ip[0] != '\0')
            {
              char *c_address = (*reg)->req_uri->host;

              struct addrinfo *addrinfo;
              struct __eXosip_sockaddr addr;

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

              if (eXosip_is_public_address (c_address))
                {
                  new_contact_url->host = osip_strdup (firewall_ip);
                  new_contact_url->port = osip_strdup (firewall_port);
              } else
                {
                  new_contact_url->host = osip_strdup (locip);
                  new_contact_url->port = osip_strdup (firewall_port);
                }
          } else
            {
              new_contact_url->host = osip_strdup (locip);
              new_contact_url->port = osip_strdup (firewall_port);
            }

          if (transport != NULL && osip_strcasecmp (transport, "UDP") != 0)
            {
              osip_uri_uparam_add (new_contact_url, osip_strdup ("transport"),
                                   osip_strdup (transport));
            }

          if (jreg->r_line[0] != '\0')
            {
              osip_uri_uparam_add (new_contact_url, osip_strdup ("line"),
                                   osip_strdup (jreg->r_line));
            }

          osip_list_add (&(*reg)->contacts, new_contact, -1);
        }
  } else
    {
      osip_message_set_contact (*reg, contact);
    }

  {
    char exp[10];               /* MUST never be ouside 1 and 3600 */

    snprintf (exp, 9, "%i", expires);
    osip_message_set_expires (*reg, exp);
  }

  osip_message_set_content_length (*reg, "0");

  return OSIP_SUCCESS;
}
Exemple #28
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;
}
Exemple #29
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;
}
Exemple #30
0
/* 
   method is the type of request. ("INVITE", "REGISTER"...)
   to is the remote target URI
   transport is either "TCP" or "UDP" (by now, only UDP is implemented!)
*/
int
generating_request_out_of_dialog (osip_message_t ** dest, const char *method,
                                  const char *to, const char *transport,
                                  const char *from, const char *proxy)
{
  /* Section 8.1:
     A valid request contains at a minimum "To, From, Call-iD, Cseq,
     Max-Forwards and Via
   */
  int i;
  osip_message_t *request;
  char locip[65];
  int doing_register;
  char *register_callid_number = NULL;

  *dest = NULL;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    request->call_id = callid;

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

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

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

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

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

  osip_message_set_user_agent (request, eXosip.user_agent);
  /*  else if ... */
  *dest = request;
  return OSIP_SUCCESS;
}