示例#1
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;
}
示例#2
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;
}
示例#3
0
static int
_eXosip_register_build_register(eXosip_reg_t * jr, osip_message_t ** _reg)
{
	osip_message_t *reg = NULL;
	int i;

	*_reg = NULL;

	if (jr == NULL)
		return OSIP_BADPARAMETER;

	if (jr->r_last_tr != NULL) {
		if (jr->r_last_tr->state != NICT_TERMINATED
			&& jr->r_last_tr->state != NICT_COMPLETED)
			return OSIP_WRONG_STATE;
		else {
			osip_message_t *last_response = NULL;
			osip_transaction_t *tr;

			i = osip_message_clone(jr->r_last_tr->orig_request, &reg);
			if (i != 0)
				return i;
			if (jr->r_last_tr->last_response != NULL) {
				i = osip_message_clone(jr->r_last_tr->last_response,
									   &last_response);
				if (i != 0) {
					osip_message_free(reg);
					return i;
				}
			}

			__eXosip_delete_jinfo(jr->r_last_tr);
			tr = jr->r_last_tr;
			jr->r_last_tr = NULL;
			osip_list_add(&eXosip.j_transactions, tr, 0);

			/* modify the REGISTER request */
			{
				int osip_cseq_num = osip_atoi(reg->cseq->number);
				int length = strlen(reg->cseq->number);


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


				i = eXosip_update_top_via(reg);
				if (i != 0) {
					osip_message_free(reg);
					if (last_response != NULL)
						osip_message_free(last_response);
					return i;
				}

				osip_cseq_num++;
				osip_free(reg->cseq->number);
				reg->cseq->number = (char *) osip_malloc(length + 2);	/* +2 like for 9 to 10 */
				if (reg->cseq->number == NULL) {
					osip_message_free(reg);
					if (last_response != NULL)
						osip_message_free(last_response);
					return OSIP_NOMEM;
				}
				snprintf(reg->cseq->number, length + 2, "%i", osip_cseq_num);


				if (last_response != NULL && 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(reg, "expires", 0, &exp);
					osip_message_header_get_byname(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);
						jr->r_reg_period = atoi(min_exp->hvalue);
					} else {
						osip_message_free(reg);
						if (last_response != NULL)
							osip_message_free(last_response);
						OSIP_TRACE(osip_trace
								   (__FILE__, __LINE__, OSIP_ERROR, NULL,
									"eXosip: missing Min-Expires or Expires in REGISTER\n"));
						return OSIP_SYNTAXERROR;
					}
				} else {
					osip_header_t *exp;

					osip_message_header_get_byname(reg, "expires", 0, &exp);
					if (exp != NULL) {
						if (exp->hvalue != NULL)
							osip_free(exp->hvalue);
						exp->hvalue = (char *) osip_malloc(10);
						if (exp->hvalue == NULL) {
							osip_message_free(reg);
							if (last_response != NULL)
								osip_message_free(last_response);
							return OSIP_NOMEM;
						}
						snprintf(exp->hvalue, 9, "%i", jr->r_reg_period);
					}
				}

				osip_message_force_update(reg);
			}

			if (last_response != NULL) {
				if (last_response->status_code == 401
					|| last_response->status_code == 407) {
					eXosip_add_authentication_information(reg, last_response);
				} else
					eXosip_add_authentication_information(reg, NULL);
				osip_message_free(last_response);
			}
		}
	}
	if (reg == NULL) {
		i = generating_register(jr, &reg, eXosip.transport,
								jr->r_aor, jr->r_registrar, jr->r_contact,
								jr->r_reg_period);
		if (i != 0)
			return i;
	}

	*_reg = reg;
	return OSIP_SUCCESS;
}
示例#4
0
static int
_eXosip_register_build_register (eXosip_reg_t * jr, osip_message_t ** _reg)
{
    osip_message_t *reg = NULL;
    int i;

    reg = NULL;
    *_reg = NULL;

    if (jr->r_last_tr != NULL)
    {
        if (jr->r_last_tr->state != NICT_TERMINATED
                && jr->r_last_tr->state != NICT_COMPLETED)
            return -1;
        else
        {
            osip_message_t *last_response = NULL;
            osip_transaction_t *tr;

            osip_message_clone (jr->r_last_tr->orig_request, &reg);
            if (reg == NULL)
                return -1;
            /* reg = jr->r_last_tr->orig_request; */
            if (jr->r_last_tr->last_response != NULL)
            {
                osip_message_clone (jr->r_last_tr->last_response, &last_response);
                if (last_response == NULL)
                {
                    osip_message_free (reg);
                    return -1;
                }
            }

            __eXosip_delete_jinfo (jr->r_last_tr);
            tr = jr->r_last_tr;
            jr->r_last_tr = NULL;
            osip_list_add (eXosip.j_transactions, tr, 0);

            /* modify the REGISTER request */
            {
                int osip_cseq_num = osip_atoi (reg->cseq->number);
                int length = strlen (reg->cseq->number);


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


                if (-1 == eXosip_update_top_via (reg))
                {
                    osip_message_free (reg);
                    if (last_response != NULL)
                        osip_message_free (last_response);
                    return -1;
                }

                osip_cseq_num++;
                osip_free (reg->cseq->number);
                reg->cseq->number = (char *) osip_malloc (length + 2);      /* +2 like for 9 to 10 */
                sprintf (reg->cseq->number, "%i", osip_cseq_num);

                {
                    osip_header_t *exp;

                    osip_message_header_get_byname (reg, "expires", 0, &exp);
                    if (exp!=NULL)
                    {
                        if (exp->hvalue!=NULL)
                            osip_free (exp->hvalue);
                        exp->hvalue = (char *) osip_malloc (10);
                        snprintf (exp->hvalue, 9, "%i", jr->r_reg_period);
                    }
                }

                osip_message_force_update (reg);
            }

            if (last_response != NULL)
            {
                if (last_response->status_code==401 || last_response->status_code==407)
                {
                    eXosip_add_authentication_information (reg, last_response);
                }
                else
                    eXosip_add_authentication_information (reg, NULL);
                osip_message_free (last_response);
            }
        }
    }
    if (reg == NULL)
    {
        i = generating_register (jr, &reg, eXosip.transport,
                                 jr->r_aor, jr->r_registrar, jr->r_contact,
                                 jr->r_reg_period);
        if (i != 0)
        {
            return -2;
        }
    }

    *_reg = reg;
    return 0;
}
int
_eXosip_insubscription_send_request_with_credential (eXosip_notify_t * jn,
                                                     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 (jn == 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_notify (jn, 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;
    }

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

  i = _eXosip_transaction_init (&tr, NICT, eXosip.j_osip, msg);

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

  /* 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_your_instance (tr, __eXosip_new_jinfo (NULL, jd, NULL, jn));
  osip_transaction_add_event (tr, sipevent);

  eXosip_update ();             /* fixed? */
  __eXosip_wakeup ();
  return OSIP_SUCCESS;
}