Exemple #1
0
int
osip_from_clone (const osip_from_t * from, osip_from_t ** dest)
{
  int i;
  osip_from_t *fr;

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

  i = osip_from_init (&fr);
  if (i != 0)                   /* allocation failed */
    return -1;
  if (from->displayname != NULL)
    fr->displayname = osip_strdup (from->displayname);

  if (from->url != NULL)
    {
      i = osip_uri_clone (from->url, &(fr->url));
      if (i != 0)
        {
          osip_from_free (fr);
          return -1;
        }
    }

  i = osip_list_clone(&from->gen_params, &fr->gen_params, (int *(*)(void *, void *)) &osip_generic_param_clone);
  if (i != 0)
    {
      osip_from_free (fr);
      return -1;
    }
  *dest = fr;
  return 0;
}
Exemple #2
0
int
eXosip_call_get_referto (int did, char *refer_to, size_t refer_to_len)
{
  eXosip_dialog_t *jd = NULL;
  eXosip_call_t *jc = NULL;
  osip_transaction_t *tr = NULL;
  osip_uri_t *referto_uri;
  char atmp[256];
  char *referto_tmp = NULL;
  int i;

  if (did <= 0)
    return OSIP_BADPARAMETER;

  eXosip_call_dialog_find (did, &jc, &jd);
  if (jc == NULL || jd == NULL || jd->d_dialog == NULL)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: No call here?\n"));
      return OSIP_NOTFOUND;
    }

  tr = eXosip_find_last_invite (jc, jd);

  if (tr == NULL || tr->orig_request == NULL)
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_ERROR, NULL,
                   "eXosip: No transaction for call?\n"));
      return OSIP_NOTFOUND;
    }

  i = osip_uri_clone (jd->d_dialog->remote_uri->url, &referto_uri);
  if (i != 0)
    return i;

  snprintf (atmp, sizeof (atmp), "%s;to-tag=%s;from-tag=%s",
            jd->d_dialog->call_id,
            jd->d_dialog->remote_tag, jd->d_dialog->local_tag);

  osip_uri_uheader_add (referto_uri, osip_strdup ("Replaces"), osip_strdup (atmp));
  i = osip_uri_to_str (referto_uri, &referto_tmp);
  if (i != 0)
    {
      osip_uri_free (referto_uri);
      return i;
    }

  snprintf (refer_to, refer_to_len, "%s", referto_tmp);
  osip_uri_free (referto_uri);

  return OSIP_SUCCESS;
}
Exemple #3
0
uri_t::uri_t ( uri_t * src_u )
{
    if ( src_u != NULL )
    {
	osip_uri_t *temp_uri ;
	
	int i = osip_uri_clone ( src_u->uri , &temp_uri ) ;
	if ( i != 0 )
	{
		perror ("osip_uri_clone method goes wrong") ;
		return ;
	}
	
	uri = temp_uri ;
    }
}
Exemple #4
0
uri_t::uri_t ( osip_uri_t * osip_u )
{
   uri = NULL ;
   osip_uri_t *temp_uri ;
   
   int i = osip_uri_clone ( osip_u , &temp_uri ) ;
   
   if ( i != 0 )
   {
	perror ("osip_uri_clone goes wrong") ;
	return ;
   }
	
   uri = temp_uri ;
     
}
Exemple #5
0
int
_eXosip_build_request_within_dialog (osip_message_t ** dest,
                                     const char *method,
                                     osip_dialog_t * dialog, const char *transport)
{
  int i;
  osip_message_t *request;
  char locip[65];
  char firewall_ip[65];
  char firewall_port[10];

  *dest = NULL;

  if (dialog == NULL)
    return OSIP_BADPARAMETER;

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

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

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

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


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

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

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

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

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

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

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

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

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


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

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

  {
    char contact[200];

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

        struct addrinfo *addrinfo;
        struct __eXosip_sockaddr addr;

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

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

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

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

  osip_message_set_user_agent (request, eXosip.user_agent);
  /*  else if ... */
  *dest = request;
  return OSIP_SUCCESS;
}
Exemple #6
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 #7
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;
}
Exemple #8
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;
}
Exemple #9
0
int
osip_message_clone (const osip_message_t * sip, osip_message_t ** dest)
{
  osip_message_t *copy;
  int pos = 0;
  int i;

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

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

  copy->sip_method = osip_strdup (sip->sip_method);
  if (sip->sip_method != NULL && copy->sip_method == NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  copy->sip_version = osip_strdup (sip->sip_version);
  if (sip->sip_version != NULL && copy->sip_version == NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  copy->status_code = sip->status_code;
  copy->reason_phrase = osip_strdup (sip->reason_phrase);
  if (sip->reason_phrase != NULL && copy->reason_phrase == NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  if (sip->req_uri != NULL) {
    i = osip_uri_clone (sip->req_uri, &(copy->req_uri));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
#ifndef MINISIZE
  {
    osip_accept_t *accept;
    osip_accept_t *accept2;

    pos = 0;
    while (!osip_list_eol (&sip->accepts, pos)) {
      accept = (osip_accept_t *) osip_list_get (&sip->accepts, pos);
      i = osip_accept_clone (accept, &accept2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->accepts, accept2, -1);      /* insert as last element */
      pos++;
    }
  }
  {
    osip_accept_encoding_t *accept_encoding;
    osip_accept_encoding_t *accept_encoding2;

    pos = 0;
    while (!osip_list_eol (&sip->accept_encodings, pos)) {
      accept_encoding = (osip_accept_encoding_t *) osip_list_get (&sip->accept_encodings, pos);
      i = osip_accept_encoding_clone (accept_encoding, &accept_encoding2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->accept_encodings, accept_encoding2, -1);
      pos++;
    }
  }
  {
    osip_accept_language_t *accept_language;
    osip_accept_language_t *accept_language2;

    pos = 0;
    while (!osip_list_eol (&sip->accept_languages, pos)) {
      accept_language = (osip_accept_language_t *) osip_list_get (&sip->accept_languages, pos);
      i = osip_accept_language_clone (accept_language, &accept_language2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->accept_languages, accept_language2, -1);
      pos++;
    }
  }
  {
    osip_alert_info_t *alert_info;
    osip_alert_info_t *alert_info2;

    pos = 0;
    while (!osip_list_eol (&sip->alert_infos, pos)) {
      alert_info = (osip_alert_info_t *) osip_list_get (&sip->alert_infos, pos);
      i = osip_alert_info_clone (alert_info, &alert_info2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->alert_infos, alert_info2, -1);
      pos++;
    }
  }
  {
    osip_allow_t *allow;
    osip_allow_t *allow2;

    pos = 0;
    while (!osip_list_eol (&sip->allows, pos)) {
      allow = (osip_allow_t *) osip_list_get (&sip->allows, pos);
      i = osip_allow_clone (allow, &allow2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->allows, allow2, -1);
      pos++;
    }
  }
  {
    osip_authentication_info_t *authentication_info;
    osip_authentication_info_t *authentication_info2;

    pos = 0;
    while (!osip_list_eol (&sip->authentication_infos, pos)) {
      authentication_info = (osip_authentication_info_t *)
        osip_list_get (&sip->authentication_infos, pos);
      i = osip_authentication_info_clone (authentication_info, &authentication_info2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->authentication_infos, authentication_info2, -1);
      pos++;
    }
  }
  {
    osip_call_info_t *call_info;
    osip_call_info_t *call_info2;

    pos = 0;
    while (!osip_list_eol (&sip->call_infos, pos)) {
      call_info = (osip_call_info_t *) osip_list_get (&sip->call_infos, pos);
      i = osip_call_info_clone (call_info, &call_info2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->call_infos, call_info2, -1);
      pos++;
    }
  }
  {
    osip_content_encoding_t *content_encoding;
    osip_content_encoding_t *content_encoding2;

    pos = 0;
    while (!osip_list_eol (&sip->content_encodings, pos)) {
      content_encoding = (osip_content_encoding_t *) osip_list_get (&sip->content_encodings, pos);
      i = osip_content_encoding_clone (content_encoding, &content_encoding2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->content_encodings, content_encoding2, -1);
      pos++;
    }
  }
  {
    osip_error_info_t *error_info;
    osip_error_info_t *error_info2;

    pos = 0;
    while (!osip_list_eol (&sip->error_infos, pos)) {
      error_info = (osip_error_info_t *) osip_list_get (&sip->error_infos, pos);
      i = osip_error_info_clone (error_info, &error_info2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->error_infos, error_info2, -1);
      pos++;
    }
  }
  {
    osip_proxy_authentication_info_t *proxy_authentication_info;
    osip_proxy_authentication_info_t *proxy_authentication_info2;

    pos = 0;
    while (!osip_list_eol (&sip->proxy_authentication_infos, pos)) {
      proxy_authentication_info = (osip_proxy_authentication_info_t *)
        osip_list_get (&sip->proxy_authentication_infos, pos);
      i = osip_proxy_authentication_info_clone (proxy_authentication_info, &proxy_authentication_info2);
      if (i != 0) {
        osip_message_free (copy);
        return i;
      }
      osip_list_add (&copy->proxy_authentication_infos, proxy_authentication_info2, -1);
      pos++;
    }
  }
#endif
  i = osip_list_clone (&sip->authorizations, &copy->authorizations, (int (*)(void *, void **)) &osip_authorization_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  if (sip->call_id != NULL) {
    i = osip_call_id_clone (sip->call_id, &(copy->call_id));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  i = osip_list_clone (&sip->contacts, &copy->contacts, (int (*)(void *, void **)) &osip_contact_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  if (sip->content_length != NULL) {
    i = osip_content_length_clone (sip->content_length, &(copy->content_length));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->content_type != NULL) {
    i = osip_content_type_clone (sip->content_type, &(copy->content_type));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->cseq != NULL) {
    i = osip_cseq_clone (sip->cseq, &(copy->cseq));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->from != NULL) {
    i = osip_from_clone (sip->from, &(copy->from));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->mime_version != NULL) {
    i = osip_mime_version_clone (sip->mime_version, &(copy->mime_version));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  i = osip_list_clone (&sip->proxy_authenticates, &copy->proxy_authenticates, (int (*)(void *, void **)) &osip_proxy_authenticate_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->proxy_authorizations, &copy->proxy_authorizations, (int (*)(void *, void **))
                       &osip_proxy_authorization_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->record_routes, &copy->record_routes, (int (*)(void *, void **)) &osip_record_route_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->routes, &copy->routes, (int (*)(void *, void **)) &osip_route_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  if (sip->to != NULL) {
    i = osip_to_clone (sip->to, &(copy->to));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  i = osip_list_clone (&sip->vias, &copy->vias, (int (*)(void *, void **)) &osip_via_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->www_authenticates, &copy->www_authenticates, (int (*)(void *, void **)) &osip_www_authenticate_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->headers, &copy->headers, (int (*)(void *, void **)) &osip_header_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->bodies, &copy->bodies, (int (*)(void *, void **)) &osip_body_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }

  copy->message_length = sip->message_length;
  copy->message = osip_strdup (sip->message);
  if (copy->message == NULL && sip->message != NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  copy->message_property = sip->message_property;
  copy->application_data = sip->application_data;

  *dest = copy;
  return OSIP_SUCCESS;
}
Exemple #10
0
/*
  This method returns:
  -2 if plugin consider this request should be totally discarded!
  -1 on error
  0  nothing has been done
  1  things has been done on psp_req element
*/
int
cb_ls_localdb_search_user_location (psp_request_t * psp_req)
{
  osip_route_t *route;
  ppl_uinfo_t *uinfo;
  int i;
  int numlocs = 0 /* DAB */ ;
  osip_message_t *request;
  request = psp_request_get_request(psp_req);

  /* default OUTPUT */
  if (ISSET_R_ROUTE_MODE (ls_localdb_context->flag))
    psp_request_set_property (psp_req, PSP_STAY_ON_PATH);
  else
    psp_request_set_property (psp_req, 0);

  /* mode */
  if (ISSET_FORKING_MODE (ls_localdb_context->flag))
    psp_request_set_mode (psp_req, PSP_FORK_MODE);
  else if (ISSET_SEQUENTIAL_MODE (ls_localdb_context->flag))
    psp_request_set_mode (psp_req, PSP_SEQ_MODE);
  else if (ISSET_REDIRECT_MODE (ls_localdb_context->flag))
    {
      psp_request_set_uas_status (psp_req, 302);
      psp_request_set_mode (psp_req, PSP_UAS_MODE);
    }
  else
    psp_request_set_mode (psp_req, PSP_FORK_MODE);

  psp_request_set_state (psp_req, PSP_MANDATE);

  /* THIS IS MANDATORY FOR ALL PLUGINS TO CHECK THAT!
     this is rarely used as a previous plugin 'ls_localdb' should have
     checked for it. In case you remove it. */
  osip_message_get_route (request, 1, &route);
  if (route != NULL)
    {
      if (ISSET_SEQUENTIAL_MODE (ls_localdb_context->flag))
	psp_request_set_mode (psp_req, PSP_SEQ_MODE);
      else
	psp_request_set_mode (psp_req, PSP_FORK_MODE);
      psp_request_set_state (psp_req, PSP_MANDATE);
      return 0;			/* do nothing.. */
    }

  if (request->req_uri->username == NULL)
    {
      /* Propose 484, but let's keep searching if another plugin can find
	 the destination. */
      psp_request_set_uas_status (psp_req, 484);
      psp_request_set_state (psp_req, PSP_CONTINUE);
      psp_request_set_mode (psp_req, PSP_UAS_MODE);
      return 0;
    }

  /* look for this URI in our list of users */
  uinfo = ppl_uinfo_find_by_aor (request->req_uri);
  if (uinfo != NULL)
    {
      /* add all locations to the psp_req element */
      binding_t *bind;
      binding_t *bindnext;
      location_t *loc;
      osip_uri_t *url;

      bind = uinfo->bindings;
      bindnext = uinfo->bindings;
      for (; bind != NULL; bind = bindnext)
	{
	  bindnext = bind->next;
	  i = ppl_uinfo_check_binding (bind);
	  if (i != 0)
	    {			/* binding is expired */
	      ppl_uinfo_remove_binding (uinfo, bind);
	    }
	}
      bind = uinfo->bindings;
      if (bind == NULL)
	{			/* user is not around... */
	  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,
				  "ls_localdb plugin: User Exist but has no valid registration!\n"));
	  psp_request_set_uas_status (psp_req, 480);
	  psp_request_set_mode (psp_req, PSP_UAS_MODE);
	  psp_request_set_state (psp_req, PSP_MANDATE);
	  return 0;
	}

      bindnext = uinfo->bindings;	/* DAB */
      for (; bind != NULL; bind = bindnext)	/* DAB */
	{			/* DAB */
	  /* If this is an INVITE Request,  collect all locations  DAB */
#ifdef EXPERIMENTAL_FORK
	  if (MSG_IS_INVITE (request))
	    bindnext = bind->next;	/* DAB */
	  else
	    bindnext = NULL;	/* DAB */
#else
	  /* always accept only ONE location even for INVITE */
	  /* this is a limitation for stability reason as the forking mode
	     is unfinished (calculation of the best response is not compliant
	     to the rfc3261.txt behavior. */
	  bindnext = NULL;	/* loop will be execute onece only */
#endif

	  i = osip_uri_clone (bind->contact->url, &url);
	  if (i != 0)
	    {
	      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL,
				      "ls_localdb plugin: Could not clone contact info!\n"));
	      psp_request_set_uas_status (psp_req, 400);
	      psp_request_set_mode (psp_req, PSP_UAS_MODE);
	      psp_request_set_state (psp_req, PSP_MANDATE);
	      return -1;
	    }
	  i = location_init (&loc, url, 3600);
	  if (i != 0)
	    {
	      osip_uri_free (url);
	      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_BUG, NULL,
				      "ls_localdb plugin: Could not create location info!\n"));
	      psp_request_set_uas_status (psp_req, 400);
	      psp_request_set_mode (psp_req, PSP_UAS_MODE);
	      psp_request_set_state (psp_req, PSP_MANDATE);
	      return -1;
	    }

	  /* new support for rfc3327.txt and Path header */
	  if (bind->path!=NULL)
	    location_set_path(loc, osip_strdup(bind->path));

	  ADD_ELEMENT (psp_req->locations, loc);
	  numlocs++;		/* DAB */
	}			/* DAB */

      OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,
			      "ls_localdb plugin: %d locations found!\n",
			      numlocs));
      return 0;
    }

  /* no user location found in local database... */
  psp_request_set_uas_status (psp_req, 404);
  psp_request_set_state (psp_req, PSP_CONTINUE);
  psp_request_set_mode (psp_req, PSP_UAS_MODE);
  OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL,
			  "ls_localdb plugin: No location found for known user: return 404 Not found!!\n"));
  return 0;
}
/*
 * SIP_REWRITE_CONTACT
 *
 * rewrite the Contact header
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_rewrite_contact (sip_ticket_t *ticket, int direction,
			 struct in_addr *local_ip) {
   osip_message_t *sip_msg=ticket->sipmsg;
   osip_contact_t *contact;
   int i;

#if UNREG_WITHOUT_REG_HG522
   char tmpUserName[128] = {0};
   char tmpPort[6] = {0};
#endif
   if (sip_msg == NULL) return STS_FAILURE;

   osip_message_get_contact(sip_msg, 0, &contact);
   if (contact == NULL)
   {
       printsip("sip: rewrite_contact contact is NULL \r\n");
       return STS_FAILURE;
   }

   for (i=0;i<URLMAP_SIZE;i++){
      if (urlmap[i].active == 0) continue;
      if ((direction == DIR_OUTGOING) &&
          (compare_url(contact->url, urlmap[i].true_url)==STS_SUCCESS)) break;
      if ((direction == DIR_INCOMING) &&
          (compare_url(contact->url, urlmap[i].masq_url)==STS_SUCCESS)) break;
#if UNREG_WITHOUT_REG_HG522
      if ((direction == DIR_OUTGOING) && (NULL != contact->url) && (NULL != urlmap[i].true_url)
          && (NULL != contact->url->host) && (NULL != urlmap[i].true_url->host)
          && (strcmp(contact->url->host, urlmap[i].true_url->host) == 0))
      {
          strcpy(tmpUserName, contact->url->username);
          if (NULL != contact->url->port)
          {
              strncpy(tmpPort, contact->url->port, 6);
          }
          break;
      }
#endif
   }

   /* found a mapping entry */
   if (i<URLMAP_SIZE) {
      char *tmp;
      if (NULL != contact->url)
      {
          printsip("************* sip: rewrote Contact header %s@%s:%s -> %s@%s:%s \r\n",
                 (contact->url->username)? contact->url->username : "******",
                 (contact->url->host)? contact->url->host : "*NULL*",
                 (contact->url->port)? contact->url->port : "NULL port",
                 urlmap[i].masq_url->username, urlmap[i].masq_url->host,
                 (urlmap[i].masq_url->port)? urlmap[i].masq_url->port : "NULL port");
      }

      /* remove old entry */
      osip_list_remove(sip_msg->contacts,0);
      osip_contact_to_str(contact, &tmp);
      osip_contact_free(contact);

      /* clone the url from urlmap */
      osip_contact_init(&contact);
      osip_contact_parse(contact,tmp);
      osip_free(tmp);
      osip_uri_free(contact->url);
      if (direction == DIR_OUTGOING) {
         /* outgoing, use masqueraded url */
         osip_uri_clone(urlmap[i].masq_url, &contact->url);
	     if (local_ip != NULL) {
            char *ip = malloc(20);
            strncpy(ip, inet_ntoa(*local_ip), 20);
            free(contact->url->host);
            contact->url->host = ip;
         }

#if UNREG_WITHOUT_REG_HG522
         if ('\0' != tmpUserName[0])
         {
             char *username = malloc(strlen(tmpUserName)+1);
             strncpy(username, tmpUserName, strlen(tmpUserName)+1);
             free(contact->url->username);
             contact->url->username = username;
         }
         if ((NULL == urlmap[i].masq_url->port) && ('\0' != tmpPort[0]))
         {
             char *port = malloc(strlen(tmpPort)+1);
             strncpy(port, tmpPort, strlen(tmpPort)+1);
             contact->url->port = port;
         }
#endif
      } else {
         /* incoming, use true url */
         osip_uri_clone(urlmap[i].true_url, &contact->url);
         printsip("incoming: osip_uri_clone from %s \r\n",
         urlmap[i].true_url->host);
      }

      osip_list_add(sip_msg->contacts,contact,-1);
   } else {
      return STS_FAILURE;
   } 

   return STS_SUCCESS;
}
int
osip_message_clone (const osip_message_t * sip, osip_message_t ** dest)
{
  osip_message_t *copy;
  int i;

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

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

  copy->sip_method = osip_strdup (sip->sip_method);
  if (sip->sip_method != NULL && copy->sip_method == NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  copy->sip_version = osip_strdup (sip->sip_version);
  if (sip->sip_version != NULL && copy->sip_version == NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  copy->status_code = sip->status_code;
  copy->reason_phrase = osip_strdup (sip->reason_phrase);
  if (sip->reason_phrase != NULL && copy->reason_phrase == NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  if (sip->req_uri != NULL) {
    i = osip_uri_clone (sip->req_uri, &(copy->req_uri));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
#ifndef MINISIZE
  i = osip_list_clone(&sip->accepts, &copy->accepts, (int (*)(void *, void **)) &osip_accept_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->accept_encodings, &copy->accept_encodings, (int (*)(void *, void **)) &osip_accept_encoding_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->accept_languages, &copy->accept_languages, (int (*)(void *, void **)) &osip_accept_language_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->alert_infos, &copy->alert_infos, (int (*)(void *, void **)) &osip_alert_info_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->allows, &copy->allows, (int (*)(void *, void **)) &osip_allow_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->authentication_infos, &copy->authentication_infos, (int (*)(void *, void **)) &osip_authentication_info_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->content_encodings, &copy->content_encodings, (int (*)(void *, void **)) &osip_content_encoding_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->error_infos, &copy->error_infos, (int (*)(void *, void **)) &osip_error_info_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone(&sip->proxy_authentication_infos, &copy->proxy_authentication_infos, (int (*)(void *, void **)) &osip_proxy_authentication_info_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
#endif
  i = osip_list_clone(&sip->call_infos, &copy->call_infos, (int (*)(void *, void **)) &osip_call_info_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->authorizations, &copy->authorizations, (int (*)(void *, void **)) &osip_authorization_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  if (sip->call_id != NULL) {
    i = osip_call_id_clone (sip->call_id, &(copy->call_id));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  i = osip_list_clone (&sip->contacts, &copy->contacts, (int (*)(void *, void **)) &osip_contact_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  if (sip->content_length != NULL) {
    i = osip_content_length_clone (sip->content_length, &(copy->content_length));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->content_type != NULL) {
    i = osip_content_type_clone (sip->content_type, &(copy->content_type));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->cseq != NULL) {
    i = osip_cseq_clone (sip->cseq, &(copy->cseq));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->from != NULL) {
    i = osip_from_clone (sip->from, &(copy->from));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  if (sip->mime_version != NULL) {
    i = osip_mime_version_clone (sip->mime_version, &(copy->mime_version));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  i = osip_list_clone (&sip->proxy_authenticates, &copy->proxy_authenticates, (int (*)(void *, void **)) &osip_proxy_authenticate_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->proxy_authorizations, &copy->proxy_authorizations, (int (*)(void *, void **))
                       &osip_proxy_authorization_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->record_routes, &copy->record_routes, (int (*)(void *, void **)) &osip_record_route_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->routes, &copy->routes, (int (*)(void *, void **)) &osip_route_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  if (sip->to != NULL) {
    i = osip_to_clone (sip->to, &(copy->to));
    if (i != 0) {
      osip_message_free (copy);
      return i;
    }
  }
  i = osip_list_clone (&sip->vias, &copy->vias, (int (*)(void *, void **)) &osip_via_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->www_authenticates, &copy->www_authenticates, (int (*)(void *, void **)) &osip_www_authenticate_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->headers, &copy->headers, (int (*)(void *, void **)) &osip_header_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }
  i = osip_list_clone (&sip->bodies, &copy->bodies, (int (*)(void *, void **)) &osip_body_clone);
  if (i != 0) {
    osip_message_free (copy);
    return i;
  }

  copy->message_length = sip->message_length;
  copy->message = osip_strdup (sip->message);
  if (copy->message == NULL && sip->message != NULL) {
    osip_message_free (copy);
    return OSIP_NOMEM;
  }
  copy->message_property = sip->message_property;
  copy->application_data = sip->application_data;

  *dest = copy;
  return OSIP_SUCCESS;
}
Exemple #13
0
/*
 * SIP_REWRITE_CONTACT
 *
 * rewrite the Contact header
 *
 * RETURNS
 *	STS_SUCCESS on success
 *	STS_FAILURE on error
 */
int sip_rewrite_contact (sip_ticket_t *ticket, int direction) {
   osip_message_t *sip_msg=ticket->sipmsg;
   osip_contact_t *contact;
   int i, j;
   int replaced=0;

   if (sip_msg == NULL) return STS_FAILURE;

   /* at least one contact header present? */
   osip_message_get_contact(sip_msg, 0, &contact);
   if (contact == NULL) return STS_FAILURE;

   /* loop for all existing contact headers in message */
   for (j=0; contact != NULL; j++) {
      osip_message_get_contact(sip_msg, j, &contact);
      if (contact == NULL) break;
      if (contact->url == NULL) continue;

      /* search for an entry */
      for (i=0;i<URLMAP_SIZE;i++){
         if (urlmap[i].active == 0) continue;
         if ((direction == DIR_OUTGOING) &&
             (compare_url(contact->url, urlmap[i].true_url)==STS_SUCCESS)) break;
         if ((direction == DIR_INCOMING) &&
             (compare_url(contact->url, urlmap[i].masq_url)==STS_SUCCESS)) break;
      }

      /* found a mapping entry */
      if (i<URLMAP_SIZE) {
         char *tmp;

         if (direction == DIR_OUTGOING) {
            DEBUGC(DBCLASS_PROXY, "rewriting Contact header %s@%s -> %s@%s",
                   (contact->url->username)? contact->url->username : "******",
                   (contact->url->host)? contact->url->host : "*NULL*",
                   urlmap[i].masq_url->username, urlmap[i].masq_url->host);
         } else {
            DEBUGC(DBCLASS_PROXY, "rewriting Contact header %s@%s -> %s@%s",
                   (contact->url->username)? contact->url->username : "******",
                   (contact->url->host)? contact->url->host : "*NULL*",
                   urlmap[i].true_url->username, urlmap[i].true_url->host);
         }

         /* remove old entry */
         osip_list_remove(sip_msg->contacts,j);
         osip_contact_to_str(contact, &tmp);
         osip_contact_free(contact);

         /* clone the url from urlmap*/
         osip_contact_init(&contact);
         osip_contact_parse(contact,tmp);
         osip_free(tmp);
         osip_uri_free(contact->url);
         if (direction == DIR_OUTGOING) {
            /* outgoing, use masqueraded url */
            osip_uri_clone(urlmap[i].masq_url, &contact->url);
         } else {
            /* incoming, use true url */
            osip_uri_clone(urlmap[i].true_url, &contact->url);
         }

         osip_list_add(sip_msg->contacts,contact,j);
         replaced=1;
      }

   }

   if (replaced == 0) {
      DEBUGC(DBCLASS_PROXY, "no Contact header rewritten");
      return STS_FAILURE;
   }

   return STS_SUCCESS;
}
Exemple #14
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 #15
0
osip_message_t *ict_create_ack(osip_transaction_t * ict, osip_message_t * response)
{
	int i;
	osip_message_t *ack;

	i = osip_message_init(&ack);
	if (i != 0)
		return NULL;

	/* Section 17.1.1.3: Construction of the ACK request: */
	i = osip_from_clone(response->from, &(ack->from));
	if (i != 0) {
		osip_message_free(ack);
		return NULL;
	}
	i = osip_to_clone(response->to, &(ack->to));	/* include the tag! */
	if (i != 0) {
		osip_message_free(ack);
		return NULL;
	}
	i = osip_call_id_clone(response->call_id, &(ack->call_id));
	if (i != 0) {
		osip_message_free(ack);
		return NULL;
	}
	i = osip_cseq_clone(response->cseq, &(ack->cseq));
	if (i != 0) {
		osip_message_free(ack);
		return NULL;
	}
	osip_free(ack->cseq->method);
	ack->cseq->method = osip_strdup("ACK");
	if (ack->cseq->method == NULL) {
		osip_message_free(ack);
		return NULL;
	}

	ack->sip_method = (char *) osip_malloc(5);
	if (ack->sip_method == NULL) {
		osip_message_free(ack);
		return NULL;
	}
	sprintf(ack->sip_method, "ACK");
	ack->sip_version = osip_strdup(ict->orig_request->sip_version);
	if (ack->sip_version == NULL) {
		osip_message_free(ack);
		return NULL;
	}

	ack->status_code = 0;
	ack->reason_phrase = NULL;

	/* MUST copy REQUEST-URI from Contact header! */
	i = osip_uri_clone(ict->orig_request->req_uri, &(ack->req_uri));
	if (i != 0) {
		osip_message_free(ack);
		return NULL;
	}

	/* ACK MUST contain only the TOP Via field from original request */
	{
		osip_via_t *via;
		osip_via_t *orig_via;

		osip_message_get_via(ict->orig_request, 0, &orig_via);
		if (orig_via == NULL) {
			osip_message_free(ack);
			return NULL;
		}
		i = osip_via_clone(orig_via, &via);
		if (i != 0) {
			osip_message_free(ack);
			return NULL;
		}
		osip_list_add(&ack->vias, via, -1);
	}

	/* ack MUST contains the ROUTE headers field from the original request */
	/* IS IT TRUE??? */
	/* if the answers contains a set of route (or record route), then it */
	/* should be used instead?? ......May be not..... */
	{
		int pos = 0;
		osip_route_t *route;
		osip_route_t *orig_route;

		while (!osip_list_eol(&ict->orig_request->routes, pos)) {
			orig_route =
				(osip_route_t *) osip_list_get(&ict->orig_request->routes, pos);
			i = osip_route_clone(orig_route, &route);
			if (i != 0) {
				osip_message_free(ack);
				return NULL;
			}
			osip_list_add(&ack->routes, route, -1);
			pos++;
		}
	}

	/* may be we could add some other headers: */
	/* For example "Max-Forward" */

	return ack;
}
Exemple #16
0
/*
 * handles register requests and updates the URL mapping table
 *
 * RETURNS:
 *    STS_SUCCESS : successfully registered
 *    STS_FAILURE : registration failed
 *    STS_NEED_AUTH : authentication needed
 */
int register_client(sip_ticket_t *ticket, int force_lcl_masq) {
   int i, j, n, sts;
   int expires;
   time_t time_now;
   osip_contact_t *contact;
   osip_uri_t *url1_to, *url1_contact=NULL;
   osip_uri_t *url2_to;
   osip_header_t *expires_hdr;
   osip_uri_param_t *expires_param=NULL;
   
   /*
    * Authorization - do only if I'm not just acting as outbound proxy
    * but am ment to be the registrar
    */
   if (force_lcl_masq == 0) {
      /*
       * RFC 3261, Section 16.3 step 6
       * Proxy Behavior - Request Validation - Proxy-Authorization
       */
      sts = authenticate_proxy(ticket->sipmsg);
      if (sts == STS_FAILURE) {
         /* failed */
         WARN("proxy authentication failed for %s@%s",
              (ticket->sipmsg->to->url->username)? 
              ticket->sipmsg->to->url->username : "******",
              ticket->sipmsg->to->url->host);
         return STS_FAILURE;
      } else if (sts == STS_NEED_AUTH) {
         /* needed */
         DEBUGC(DBCLASS_REG,"proxy authentication needed for %s@%s",
                ticket->sipmsg->to->url->username,
                ticket->sipmsg->to->url->host);
         return STS_NEED_AUTH;
      }
   }

/*
   fetch 1st Via entry and remember this address. Incoming requests
   for the registered address have to be passed on to that host.

   To: -> address to be registered
   Contact: -> host is reachable there
               Note: in case of un-REGISTER, the contact header may
                     contain '*' only - which means "all registrations
                     made by this UA"
   
   => Mapping is
   To: <1--n> Contact
   
*/
   time(&time_now);

   DEBUGC(DBCLASS_BABBLE,"sip_register:");

   /*
    * First make sure, we have a proper Contact header:
    *  - url
    *  - url -> hostname
    *
    * Libosip parses an:
    * "Contact: *"
    * the following way (Note: Display name!! and URL is NULL)
    * (gdb) p *((osip_contact_t*)(sip->contacts.node->element))
    * $5 = {displayname = 0x8af8848 "*", url = 0x0, gen_params = 0x8af8838}
    */

   osip_message_get_contact(ticket->sipmsg, 0, &contact);
   if ((contact == NULL) ||
       (contact->url == NULL) ||
       (contact->url->host == NULL)) {
      /* Don't have required Contact fields.
         This may be a Registration query or unregistering all registered
         records for this UA. We should simply forward this request to its
         destination. However, if this is an unregistration from a client
         that is not registered (Grandstream "unregister at startup" option)
         -> How do I handle this one?
         Right now we do a direction lookup and if this fails we generate
         an OK message by ourself (fake) */
      DEBUGC(DBCLASS_REG, "empty Contact header - "
             "seems to be a registration query");
      sts = sip_find_direction(ticket, NULL);
      if (sts != STS_SUCCESS) {
         /* answer the request myself. Most likely this is an UNREGISTER
          * request when the client just booted */
         sts = register_response(ticket, STS_SUCCESS);
         return STS_SIP_SENT;
      }

      return STS_SUCCESS;
   }

   url1_contact=contact->url;

   /* evaluate Expires Header field */
   osip_message_get_expires(ticket->sipmsg, 0, &expires_hdr);

  /*
   * look for an Contact expires parameter - in case of REGISTER
   * these two are equal. The Contact expires has higher priority!
   */
   if (ticket->sipmsg->contacts.node &&
       ticket->sipmsg->contacts.node->element) {
      osip_contact_param_get_byname(
              (osip_contact_t*) ticket->sipmsg->contacts.node->element,
              EXPIRES, &expires_param);
   }

   if (expires_param && expires_param->gvalue) {
      /* get expires from contact Header */
      expires=atoi(expires_param->gvalue);
      if ((expires < 0) || (expires >= UINT_MAX ))
         expires=configuration.default_expires;
   } else if (expires_hdr && expires_hdr->hvalue) {
      /* get expires from expires Header */
      expires=atoi(expires_hdr->hvalue);
      if ((expires < 0) || (expires >= UINT_MAX ))
         expires=configuration.default_expires;
   } else {
      char tmp[16];
      /* it seems, the expires field is not present everywhere... */
      DEBUGC(DBCLASS_REG,"no 'expires' header found - set time to %i sec",
             configuration.default_expires);
      expires=configuration.default_expires;
      sprintf(tmp,"%i",expires);
      osip_message_set_expires(ticket->sipmsg, tmp);
   }

   url1_to=ticket->sipmsg->to->url;



   /*
    * REGISTER
    */
   if (expires > 0) {
      DEBUGC(DBCLASS_REG,"register: %s@%s expires=%i seconds",
          (url1_contact->username) ? url1_contact->username : "******",
          (url1_contact->host) ? url1_contact->host : "*NULL*",
          expires);

      /*
       * Update registration. There are two possibilities:
       * - already registered, then update the existing record
       * - not registered, then create a new record
       */

      j=-1;
      for (i=0; i<URLMAP_SIZE; i++) {
         if (urlmap[i].active == 0) {
            if (j < 0) j=i; /* remember first hole */
            continue;
         }

         url2_to=urlmap[i].reg_url;

         /* check address-of-record ("public address" of user) */
         if (compare_url(url1_to, url2_to)==STS_SUCCESS) {
            DEBUGC(DBCLASS_REG, "found entry for %s@%s <-> %s@%s at "
                   "slot=%i, exp=%li",
                   (url1_contact->username) ? url1_contact->username : "******",
                   (url1_contact->host) ? url1_contact->host : "*NULL*",
                   (url2_to->username) ? url2_to->username : "******",
                   (url2_to->host) ? url2_to->host : "*NULL*",
                   i, (long)urlmap[i].expires-time_now);
            break;
         }
      }

      if ( (j < 0) && (i >= URLMAP_SIZE) ) {
         /* oops, no free entries left... */
         ERROR("URLMAP is full - registration failed");
         return STS_FAILURE;
      }

      if (i >= URLMAP_SIZE) {
         /* entry not existing, create new one */
         i=j;

         /* write entry */
         urlmap[i].active=1;
         /* Contact: field */
         osip_uri_clone( ((osip_contact_t*)
                         (ticket->sipmsg->contacts.node->element))->url, 
                         &urlmap[i].true_url);
         /* To: field */
         osip_uri_clone( ticket->sipmsg->to->url, 
                    &urlmap[i].reg_url);

         DEBUGC(DBCLASS_REG,"create new entry for %s@%s <-> %s@%s at slot=%i",
                (url1_contact->username) ? url1_contact->username : "******",
                (url1_contact->host) ? url1_contact->host : "*NULL*",
                (urlmap[i].reg_url->username) ? urlmap[i].reg_url->username : "******",
                (urlmap[i].reg_url->host) ? urlmap[i].reg_url->host : "*NULL*",
                i);

         /*
          * try to figure out if we ought to do some masquerading
          */
         osip_uri_clone( ticket->sipmsg->to->url, 
                         &urlmap[i].masq_url);

         n=configuration.mask_host.used;
         if (n != configuration.masked_host.used) {
            ERROR("# of mask_host is not equal to # of masked_host in config!");
            n=0;
         }

         DEBUG("%i entries in MASK config table", n);
         for (j=0; j<n; j++) {
            DEBUG("compare [%s] <-> [%s]",configuration.mask_host.string[j],
                  ticket->sipmsg->to->url->host);
            if (strcmp(configuration.mask_host.string[j],
                ticket->sipmsg->to->url->host)==0)
               break;
         }
         if (j<n) { 
            /* we are masquerading this UA, replace the host part of the url */
            DEBUGC(DBCLASS_REG,"masquerading UA %s@%s as %s@%s",
                   (url1_contact->username) ? url1_contact->username : "******",
                   (url1_contact->host) ? url1_contact->host : "*NULL*",
                   (url1_contact->username) ? url1_contact->username : "******",
                   configuration.masked_host.string[j]);
            urlmap[i].masq_url->host=realloc(urlmap[i].masq_url->host,
                                    strlen(configuration.masked_host.string[j])+1);
            strcpy(urlmap[i].masq_url->host, configuration.masked_host.string[j]);
         }
      } else { /* if new entry */
         /* This is an existing entry */
         /*
          * Some phones (like BudgeTones *may* dynamically grab a SIP port
          * so we might want to update the true_url and reg_url each time
          * we get an REGISTER
          */

         /* Contact: field (true_url) */
         osip_uri_free(urlmap[i].true_url);
         osip_uri_clone( ((osip_contact_t*)
                         (ticket->sipmsg->contacts.node->element))->url, 
                         &urlmap[i].true_url);
         /* To: field (reg_url) */
         osip_uri_free(urlmap[i].reg_url);
         osip_uri_clone( ticket->sipmsg->to->url, 
                         &urlmap[i].reg_url);
      }

      /*
       * for proxying: force device to be masqueraded
       * as with the outbound IP (masq_url)
       */
      if (force_lcl_masq) {
         struct in_addr addr;
         char *addrstr;

         if (get_interface_ip(IF_OUTBOUND, &addr) != STS_SUCCESS) {
            return STS_FAILURE;
         }

         /* host part */
         addrstr = utils_inet_ntoa(addr);
         DEBUGC(DBCLASS_REG,"masquerading UA %s@%s local %s@%s",
                (url1_contact->username) ? url1_contact->username : "******",
                (url1_contact->host) ? url1_contact->host : "*NULL*",
                (url1_contact->username) ? url1_contact->username : "******",
                addrstr);
         urlmap[i].masq_url->host=realloc(urlmap[i].masq_url->host,
                                          strlen(addrstr)+1);
         strcpy(urlmap[i].masq_url->host, addrstr);

         /* port number if required */
         if (configuration.sip_listen_port != SIP_PORT) {
            urlmap[i].masq_url->port=realloc(urlmap[i].masq_url->port, 16);
            sprintf(urlmap[i].masq_url->port, "%i",
                    configuration.sip_listen_port);
         }
      }

      /* give some safety margin for the next update */
      if (expires > 0) expires+=30;

      /* update registration timeout */
      urlmap[i].expires=time_now+expires;

   /*
    * un-REGISTER
    */
   } else { /* expires > 0 */
      /*
       * Remove registration
       * Siproxd will ALWAYS remove ALL bindings for a given
       * address-of-record
       */
      for (i=0; i<URLMAP_SIZE; i++) {
         if (urlmap[i].active == 0) continue;

         url2_to=urlmap[i].reg_url;

         if (compare_url(url1_to, url2_to)==STS_SUCCESS) {
            DEBUGC(DBCLASS_REG, "removing registration for %s@%s at slot=%i",
                   (url2_to->username) ? url2_to->username : "******",
                   (url2_to->host) ? url2_to->host : "*NULL*", i);
            urlmap[i].expires=0;
            break;
         }
      }
   }

   return STS_SUCCESS;
}
Exemple #17
0
int
osip_message_clone (const osip_message_t * sip, osip_message_t ** dest)
{
    osip_message_t *copy;
    int pos = 0;
    int i;

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

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

    copy->sip_method = osip_strdup (sip->sip_method);
    copy->sip_version = osip_strdup (sip->sip_version);
    copy->status_code = sip->status_code;
    copy->reason_phrase = osip_strdup (sip->reason_phrase);
    if (sip->req_uri != NULL)
    {
        i = osip_uri_clone (sip->req_uri, &(copy->req_uri));
        if (i != 0)
            goto mc_error1;
    }

    {
        osip_accept_t *accept;
        osip_accept_t *accept2;

        pos = 0;
        while (!osip_list_eol (&sip->accepts, pos))
        {
            accept = (osip_accept_t *) osip_list_get (&sip->accepts, pos);
            i = osip_accept_clone (accept, &accept2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->accepts, accept2, -1);     /* insert as last element */
            pos++;
        }
    }
    {
        osip_accept_encoding_t *accept_encoding;
        osip_accept_encoding_t *accept_encoding2;

        pos = 0;
        while (!osip_list_eol (&sip->accept_encodings, pos))
        {
            accept_encoding =
                (osip_accept_encoding_t *) osip_list_get (&sip->accept_encodings, pos);
            i = osip_accept_encoding_clone (accept_encoding, &accept_encoding2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->accept_encodings, accept_encoding2, -1);
            pos++;
        }
    }
    {
        osip_accept_language_t *accept_language;
        osip_accept_language_t *accept_language2;

        pos = 0;
        while (!osip_list_eol (&sip->accept_languages, pos))
        {
            accept_language =
                (osip_accept_language_t *) osip_list_get (&sip->accept_languages, pos);
            i = osip_accept_language_clone (accept_language, &accept_language2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->accept_languages, accept_language2, -1);
            pos++;
        }
    }
    {
        osip_alert_info_t *alert_info;
        osip_alert_info_t *alert_info2;

        pos = 0;
        while (!osip_list_eol (&sip->alert_infos, pos))
        {
            alert_info = (osip_alert_info_t *) osip_list_get (&sip->alert_infos, pos);
            i = osip_alert_info_clone (alert_info, &alert_info2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->alert_infos, alert_info2, -1);
            pos++;
        }
    }
    {
        osip_allow_t *allow;
        osip_allow_t *allow2;

        pos = 0;
        while (!osip_list_eol (&sip->allows, pos))
        {
            allow = (osip_allow_t *) osip_list_get (&sip->allows, pos);
            i = osip_allow_clone (allow, &allow2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->allows, allow2, -1);
            pos++;
        }
    }
    {
        osip_authentication_info_t *authentication_info;
        osip_authentication_info_t *authentication_info2;

        pos = 0;
        while (!osip_list_eol (&sip->authentication_infos, pos))
        {
            authentication_info =
                (osip_authentication_info_t *) osip_list_get (&sip->
                        authentication_infos, pos);
            i =
                osip_authentication_info_clone (authentication_info,
                                                &authentication_info2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->authentication_infos, authentication_info2, -1);
            pos++;
        }
    }
    {
        osip_authorization_t *authorization;
        osip_authorization_t *authorization2;

        pos = 0;
        while (!osip_list_eol (&sip->authorizations, pos))
        {
            authorization =
                (osip_authorization_t *) osip_list_get (&sip->authorizations, pos);
            i = osip_authorization_clone (authorization, &authorization2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->authorizations, authorization2, -1);
            pos++;
        }
    }
    if (sip->call_id != NULL)
    {
        i = osip_call_id_clone (sip->call_id, &(copy->call_id));
        if (i != 0)
            goto mc_error1;
    }
    {
        osip_call_info_t *call_info;
        osip_call_info_t *call_info2;

        pos = 0;
        while (!osip_list_eol (&sip->call_infos, pos))
        {
            call_info = (osip_call_info_t *) osip_list_get (&sip->call_infos, pos);
            i = osip_call_info_clone (call_info, &call_info2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->call_infos, call_info2, -1);
            pos++;
        }
    }
    {
        osip_contact_t *contact;
        osip_contact_t *contact2;

        pos = 0;
        while (!osip_list_eol (&sip->contacts, pos))
        {
            contact = (osip_contact_t *) osip_list_get (&sip->contacts, pos);
            i = osip_contact_clone (contact, &contact2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->contacts, contact2, -1);
            pos++;
        }
    }
    {
        osip_content_encoding_t *content_encoding;
        osip_content_encoding_t *content_encoding2;

        pos = 0;
        while (!osip_list_eol (&sip->content_encodings, pos))
        {
            content_encoding =
                (osip_content_encoding_t *) osip_list_get (&sip->content_encodings, pos);
            i = osip_content_encoding_clone (content_encoding, &content_encoding2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->content_encodings, content_encoding2, -1);
            pos++;
        }
    }
    if (sip->content_length != NULL)
    {
        i = osip_content_length_clone (sip->content_length, &(copy->content_length));
        if (i != 0)
            goto mc_error1;
    }
    if (sip->content_type != NULL)
    {
        i = osip_content_type_clone (sip->content_type, &(copy->content_type));
        if (i != 0)
            goto mc_error1;
    }
    if (sip->cseq != NULL)
    {
        i = osip_cseq_clone (sip->cseq, &(copy->cseq));
        if (i != 0)
            goto mc_error1;
    }
    {
        osip_error_info_t *error_info;
        osip_error_info_t *error_info2;

        pos = 0;
        while (!osip_list_eol (&sip->error_infos, pos))
        {
            error_info = (osip_error_info_t *) osip_list_get (&sip->error_infos, pos);
            i = osip_error_info_clone (error_info, &error_info2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->error_infos, error_info2, -1);
            pos++;
        }
    }
    if (sip->from != NULL)
    {
        i = osip_from_clone (sip->from, &(copy->from));
        if (i != 0)
            goto mc_error1;
    }
    if (sip->mime_version != NULL)
    {
        i = osip_mime_version_clone (sip->mime_version, &(copy->mime_version));
        if (i != 0)
            goto mc_error1;
    }
    {
        osip_proxy_authenticate_t *proxy_authenticate;
        osip_proxy_authenticate_t *proxy_authenticate2;

        pos = 0;
        while (!osip_list_eol (&sip->proxy_authenticates, pos))
        {
            proxy_authenticate =
                (osip_proxy_authenticate_t *) osip_list_get (&sip->
                        proxy_authenticates, pos);
            i =
                osip_proxy_authenticate_clone (proxy_authenticate, &proxy_authenticate2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->proxy_authenticates, proxy_authenticate2, -1);
            pos++;
        }
    }
    {
        osip_proxy_authentication_info_t *proxy_authentication_info;
        osip_proxy_authentication_info_t *proxy_authentication_info2;

        pos = 0;
        while (!osip_list_eol (&sip->proxy_authentication_infos, pos))
        {
            proxy_authentication_info =
                (osip_proxy_authentication_info_t *) osip_list_get (&sip->
                        proxy_authentication_infos,
                        pos);
            i =
                osip_proxy_authentication_info_clone (proxy_authentication_info,
                        &proxy_authentication_info2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->proxy_authentication_infos,
                           proxy_authentication_info2, -1);
            pos++;
        }
    }
    {
        osip_proxy_authorization_t *proxy_authorization;
        osip_proxy_authorization_t *proxy_authorization2;

        pos = 0;
        while (!osip_list_eol (&sip->proxy_authorizations, pos))
        {
            proxy_authorization =
                (osip_proxy_authorization_t *) osip_list_get (&sip->
                        proxy_authorizations, pos);
            i =
                osip_proxy_authorization_clone (proxy_authorization,
                                                &proxy_authorization2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->proxy_authorizations, proxy_authorization2, -1);
            pos++;
        }
    }
    {
        osip_record_route_t *record_route;
        osip_record_route_t *record_route2;

        pos = 0;
        while (!osip_list_eol (&sip->record_routes, pos))
        {
            record_route =
                (osip_record_route_t *) osip_list_get (&sip->record_routes, pos);
            i = osip_record_route_clone (record_route, &record_route2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->record_routes, record_route2, -1);
            pos++;
        }
    }
    {
        osip_route_t *route;
        osip_route_t *route2;

        pos = 0;
        while (!osip_list_eol (&sip->routes, pos))
        {
            route = (osip_route_t *) osip_list_get (&sip->routes, pos);
            i = osip_route_clone (route, &route2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->routes, route2, -1);
            pos++;
        }
    }
    if (sip->to != NULL)
    {
        i = osip_to_clone (sip->to, &(copy->to));
        if (i != 0)
            goto mc_error1;
    }
    {
        osip_via_t *via;
        osip_via_t *via2;

        pos = 0;
        while (!osip_list_eol (&sip->vias, pos))
        {
            via = (osip_via_t *) osip_list_get (&sip->vias, pos);
            i = osip_via_clone (via, &via2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->vias, via2, -1);
            pos++;
        }
    }
    {
        osip_www_authenticate_t *www_authenticate;
        osip_www_authenticate_t *www_authenticate2;

        pos = 0;
        while (!osip_list_eol (&sip->www_authenticates, pos))
        {
            www_authenticate =
                (osip_www_authenticate_t *) osip_list_get (&sip->www_authenticates, pos);
            i = osip_www_authenticate_clone (www_authenticate, &www_authenticate2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->www_authenticates, www_authenticate2, -1);
            pos++;
        }
    }

    {
        osip_header_t *header;
        osip_header_t *header2;

        pos = 0;
        while (!osip_list_eol (&sip->headers, pos))
        {
            header = (osip_header_t *) osip_list_get (&sip->headers, pos);
            i = osip_header_clone (header, &header2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->headers, header2, -1);
            pos++;
        }
    }

    {
        osip_body_t *body;
        osip_body_t *body2;

        pos = 0;
        while (!osip_list_eol (&sip->bodies, pos))
        {
            body = (osip_body_t *) osip_list_get (&sip->bodies, pos);
            i = osip_body_clone (body, &body2);
            if (i != 0)
                goto mc_error1;
            osip_list_add (&copy->bodies, body2, -1);
            pos++;
        }
    }

    copy->message_length = sip->message_length;
    copy->message = osip_strdup (sip->message);
    copy->message_property = sip->message_property;

    *dest = copy;
    return 0;
mc_error1:
    osip_message_free (copy);
    return -1;

}