Example #1
0
/*
 * create a reply template from an given SIP request
 *
 * RETURNS a pointer to osip_message_t
 */
osip_message_t *msg_make_template_reply (sip_ticket_t *ticket, int code) {
   osip_message_t *request=ticket->sipmsg;
   osip_message_t *response;
   int pos;

   osip_message_init (&response);
   response->message=NULL;
   osip_message_set_version (response, osip_strdup ("SIP/2.0"));
   osip_message_set_status_code (response, code);
   osip_message_set_reason_phrase (response, 
                                   osip_strdup(osip_message_get_reason (code)));

   if (request->to==NULL) {
      ERROR("msg_make_template_reply: empty To in request header");
      return NULL;
   }

   if (request->from==NULL) {
      ERROR("msg_make_template_reply: empty From in request header");
      return NULL;
   }

   osip_to_clone (request->to, &response->to);
   osip_from_clone (request->from, &response->from);

   /* if 3xx, also include 1st contact header */
   if ((code==200) || ((code>=300) && (code<400))) {
      osip_contact_t *req_contact = NULL;
      osip_contact_t *res_contact = NULL;
      osip_message_get_contact(request, 0, &req_contact);
      if (req_contact) osip_contact_clone (req_contact, &res_contact);
      if (res_contact) osip_list_add(response->contacts,res_contact,0);
   }

   /* via headers */
   pos = 0;
   while (!osip_list_eol (request->vias, pos)) {
      char *tmp;
      osip_via_t *via;
      via = (osip_via_t *) osip_list_get (request->vias, pos);
      osip_via_to_str (via, &tmp);

      osip_message_set_via (response, tmp);
      osip_free (tmp);
      pos++;
   }

   osip_call_id_clone(request->call_id,&response->call_id);
   
   osip_cseq_clone(request->cseq,&response->cseq);

   return response;
}
Example #2
0
/* Get SIP message of the call leg */
osip_message_t *eXosipua_extract_message(eXosipua_t *jua, eXosip_event_t *je)
{
   osip_message_t *message;

   if(osip_message_init(&message) != 0)
   {
      jua_debug(("eXosipua_extract_message: init sip body error\n"));
      return NULL;
   }

   if(osip_message_parse(message, je->sdp_body, strlen(je->sdp_body)) != 0)
   {
      osip_message_free(message);
      return NULL;
   }

   return message;
}
Example #3
0
int BuildResponse(const osip_message_t *request, osip_message_t **response)
{
	osip_message_t *msg = NULL;
	char port[6];
	char* pContact = osip_malloc(strlen(SERV_IP_ADDR) + strlen(USER_NAME) + sizeof(port) + 7 + 10 ); // 7 is "sip:...@..:. \0"    10 - zapas
	osip_message_init(&msg);

	osip_from_clone(request->from, &msg->from);
	osip_to_clone(request->to, &msg->to);
	osip_cseq_clone(request->cseq, &msg->cseq);
	osip_call_id_clone(request->call_id, &msg->call_id);

	int pos = 0;//copy vias from request to response
	while (!osip_list_eol(&request->vias, pos))
	{
		osip_via_t *via;
		osip_via_t *via2;

		via = (osip_via_t *) osip_list_get(&request->vias, pos);
		int i = osip_via_clone(via, &via2);
		if (i != 0)
		{
			osip_message_free(msg);
			return i;
		}
		osip_list_add(&(msg->vias), via2, -1);
		pos++;
	}	
	osip_message_set_max_forwards(msg, osip_strdup("70"));
	osip_to_set_tag(msg->to, osip_strdup("4893693")); // set to tag in response. todo: randomize
	osip_message_set_version(msg, osip_strdup("SIP/2.0"));
	// create contact
	strcat(pContact, osip_strdup("sip:"));
	strcat(pContact, osip_strdup(USER_NAME));
	strcat(pContact, osip_strdup("@"));
	strcat(pContact, osip_strdup(SERV_IP_ADDR));
	osip_message_set_contact(msg, osip_strdup(pContact));
	
	osip_free(pContact);
	osip_message_set_user_agent(msg, osip_strdup("SipMore/0.1"));

	*response = msg;
	return 0;
}
Example #4
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;
}
Example #5
0
int
_eXosip_build_response_default (osip_message_t ** dest,
                                osip_dialog_t * dialog, int status,
                                osip_message_t * request)
{
  osip_generic_param_t *tag;
  osip_message_t *response;
  int pos;
  int i;

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

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

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

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

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

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

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

  pos = 0;
  while (!osip_list_eol (request->vias, pos))
    {
      osip_via_t *via;
      osip_via_t *via2;

      via = (osip_via_t *) osip_list_get (request->vias, pos);
      i = osip_via_clone (via, &via2);
      if (i != -0)
        goto grd_error_1;
      osip_list_add (response->vias, via2, -1);
      pos++;
    }

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

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

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

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

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

  *dest = response;
  return 0;

grd_error_1:
  osip_message_free (response);
  return -1;
}
Example #6
0
File: ict_fsm.c Project: avis/osip
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;
}
Example #7
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;

}
Example #8
0
int Node::SndRegister(REG_TYPE type , 
					  ChordId req_uri ,  
					  ChordId to , 
					  ChordId contact , 
					  BOOL IncludeSuccList) 
{
	char *message1;
	unsigned int len = 0;



	osip_transaction_t *transaction;
	osip_event_t *sipevent;
	osip_message_t *request;

	/* temp uri */
	char * tmp_uri;
	
	int i;

	i = osip_message_init(&request);
	if (i!=0) 
		goto si2perror1;
	/* prepare the request-line */
	osip_message_set_method(request, osip_strdup("REGISTER"));
	osip_message_set_version(request, osip_strdup("SIP/2.0"));
	osip_message_set_status_code(request, 0);
	osip_message_set_reason_phrase(request, NULL);

	//requset uri
	if(type == MULTICAST)
	{
		tmp_uri = osip_strdup("sip:224.0.1.75") ;
	}
	else
		tmp_uri = ChordId2Uri(req_uri,false) ;

	osip_uri_t *uri;

	i=osip_uri_init(&uri);
	if (i!=0) 
		goto si2perror2;
	i=osip_uri_parse(uri, tmp_uri );
	if(tmp_uri)	osip_free(tmp_uri) ;
	if (i!=0)
		goto si2perror2;

	osip_message_set_uri(request , uri) ;

	if(type == JOIN)
	{
		tmp_uri = ChordId2Uri(to,false) ;

		/* when JOIN , to and from are same */
		osip_message_set_to(request, tmp_uri );
		osip_message_set_from(request, tmp_uri);
		if (tmp_uri) osip_free(tmp_uri) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("join"));
	}

	else if(type == FINDSUCC)
	{
		tmp_uri = ChordId2Uri(to,true) ;
		osip_message_set_to(request, tmp_uri );
		if (tmp_uri) osip_free(tmp_uri) ;

		tmp_uri = ChordId2Uri(chordId,false);
		osip_message_set_from(request, tmp_uri  );
		if (tmp_uri) osip_free(tmp_uri) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("findsucc"));
		
		//has no contact
	}

	else if(type == STABILIZE)
	{
		tmp_uri = ChordId2Uri(to,false);
		osip_message_set_to(request, tmp_uri);
		if (tmp_uri) osip_free(tmp_uri) ;

		tmp_uri = ChordId2Uri(chordId,false)  ;
		osip_message_set_from(request, tmp_uri );
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("stabilize"));	
		if (tmp_uri) osip_free(tmp_uri) ;
		
		//contact
		tmp_uri = ChordId2Uri(chordId,false);
		char * pre_uri = ChordId2Uri(getFingerTable()->getPredecessor(),false) ;

		char *ctt = (char *) osip_malloc(strlen(tmp_uri) + strlen(";predecessor=") + strlen(pre_uri) +1) ;
		if (ctt == NULL)
			return NULL;
		sprintf (ctt, "%s;predecessor=%s", tmp_uri,pre_uri);
		osip_free(tmp_uri) ;	osip_free(pre_uri) ;

		osip_message_set_contact(request, ctt );
		osip_free(ctt) ;
	}

	else if(type == LEAVE)
	{
		tmp_uri = ChordId2Uri(to,false) ;
		osip_message_set_to(request, tmp_uri );
		if (tmp_uri) osip_free(tmp_uri) ;

		tmp_uri = ChordId2Uri(chordId,false) ;
		osip_message_set_from(request, tmp_uri );
		if (tmp_uri) osip_free(tmp_uri) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("leave"));
		
		//contact
		tmp_uri = ChordId2Uri(chordId,false);
		char * pre_uri = ChordId2Uri(getFingerTable()->getPredecessor(),false) ;

		char *ctt = (char *) osip_malloc(strlen(tmp_uri) + strlen(";predecessor=") + strlen(pre_uri) +1) ;
		if (ctt == NULL)
			return NULL;
		sprintf (ctt, "%s;predecessor=%s", tmp_uri,pre_uri);
		osip_free(tmp_uri) ;	osip_free(pre_uri) ;

		osip_message_set_contact(request, ctt );
		osip_free(ctt) ;
		
		//succlist
		if(IncludeSuccList)
		{
			for(i = 0  ; i < getFingerTable()->getSuccNum() ; i++)
			{
				tmp_uri = ChordId2Uri(getFingerTable()->getSuccessor(i),false) ;
				osip_message_set_contact(request, tmp_uri );
				osip_free(tmp_uri) ;
			}
		}
	}//type == LEAVE
	
	if(type == MULTICAST)
	{
		
		tmp_uri = ChordId2Uri(chordId,false);
		/* when JOIN , to and from are same */
		osip_message_set_to(request, tmp_uri );
		osip_message_set_from(request, tmp_uri);
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("multicast"));
		osip_free(tmp_uri) ;
		//no contact
	}//type == MULTIPL


	//---set call_id and cseq
	osip_call_id_t *callid;
	osip_cseq_t *cseq;
	char *num;
	char  *cidrand;
	char *register_callid_number ;

	/* call-id is always the same for REGISTRATIONS */
	i = osip_call_id_init(&callid);
	if (i!=0) 
		goto si2perror2;
	cidrand = osip_strdup("BF9598C48B184EBBAFADFE527EED8186") ;
	osip_call_id_set_number(callid, cidrand);
	register_callid_number = cidrand;

	osip_call_id_set_host(callid, osip_strdup("SI2P.COM"));
	request->call_id = callid;

	//cseq
	i = osip_cseq_init(&cseq);
	if (i!=0) 
		goto si2perror2 ;
	num = osip_strdup("1");
	osip_cseq_set_number(cseq, num);
	osip_cseq_set_method(cseq, osip_strdup("REGISTER"));
	request->cseq = cseq;
	 
	/*the Max-Forward header */
	osip_message_set_max_forwards(request, "5"); /* a UA should start a request with 70 */

	/* the via header */
	char tmp[200];
	unsigned int branch;
	branch=osip_build_random_number();
    snprintf(tmp, 200, "SIP/2.0/%s %s:%s;rport;branch=z9hG4bK%u", "UDP",
	      localip,
	      localport,
	      branch );

    osip_message_set_via(request, tmp);

	/* the UA header */
	osip_message_set_user_agent(request, user_agent);

	/*  the expires header */
	char exp[10]; /* MUST never be ouside 1 and 3600 */
	snprintf(exp, 9, "%i", expires);
	osip_message_set_expires(request, exp);
	osip_message_set_content_length(request, "0");


	/*** then must wake up osip ***/
	i = osip_transaction_init(&transaction,
		       NICT,
		       adosip->j_osip,
		       request);
	if (i!=0)
		goto si2perror2 ;

	//jr->r_last_tr = transaction;

	/* send REGISTER */
	
	i = osip_message_to_str(request, &message1, &len);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1)	osip_free(message1) ;
//	printf("SEND======================================>>\n") ;
//	printf(message1) ;

	sipevent = osip_new_outgoing_sipmessage(request);
	sipevent->transactionid =  transaction->transactionid;
	osip_message_force_update(request);
  
	osip_transaction_add_event(transaction, sipevent);

	adosip->ThreadWakeUp();
	return 0;

si2perror1:
	if(request != NULL)osip_message_free(request);
	return -1 ;
si2perror2:
	if(request != NULL)osip_message_free(request);
	return -1;
}
Example #9
0
int Node::SndUserRegisterRequest(char *aor ,const char *requri)
{
	char *message1;
	unsigned int len = 0;



	osip_transaction_t *transaction;
	osip_event_t *sipevent;
	osip_message_t *request;


	int i;

	i = osip_message_init(&request);
	if (i!=0) 
		goto si2perror1;
	/* prepare the request-line */
	osip_message_set_method(request, osip_strdup("REGISTER"));
	osip_message_set_version(request, osip_strdup("SIP/2.0"));
	osip_message_set_status_code(request, 0);
	osip_message_set_reason_phrase(request, NULL);

	//requset uri


	osip_uri_t *uri;

	i=osip_uri_init(&uri);
	if (i!=0) 
		goto si2perror2;
	i=osip_uri_parse(uri, requri);
	if (i!=0)
		goto si2perror2;

	osip_message_set_uri(request , uri) ;


	//to from ,no contact
	osip_message_set_to(request,aor);
	osip_message_set_from(request,aor);

	osip_from_param_add(request->from,osip_strdup("user"),osip_strdup("user_query"));

	//---set call_id and cseq
	osip_call_id_t *callid;
	osip_cseq_t *cseq;
	char *num;
	char  *cidrand;
	char *register_callid_number ;

	/* call-id is always the same for REGISTRATIONS */
	i = osip_call_id_init(&callid);
	if (i!=0) 
		goto si2perror2;
	cidrand = osip_strdup("BF9598C48B184EBBAFADFE527EED8186") ;
	osip_call_id_set_number(callid, cidrand);
	register_callid_number = cidrand;

	osip_call_id_set_host(callid, osip_strdup("SI2P.COM"));
	request->call_id = callid;

	//cseq
	i = osip_cseq_init(&cseq);
	if (i!=0) 
		goto si2perror2 ;
	num = osip_strdup("1");
	osip_cseq_set_number(cseq, num);
	osip_cseq_set_method(cseq, osip_strdup("REGISTER"));
	request->cseq = cseq;
	 
	/*the Max-Forward header */
	osip_message_set_max_forwards(request, "5"); /* a UA should start a request with 70 */

	/* the via header */
	char tmp[200];
	unsigned int branch;
	branch=osip_build_random_number();
    snprintf(tmp, 200, "SIP/2.0/%s %s:%s;rport;branch=z9hG4bK%u", "UDP",
	      localip,
	      localport,
	      branch );

    osip_message_set_via(request, tmp);

	/* the UA header */
	osip_message_set_user_agent(request, user_agent);

	/*  the expires header */
	char exp[10]; /* MUST never be ouside 1 and 3600 */
	snprintf(exp, 9, "%i", expires);
	osip_message_set_expires(request, exp);
	osip_message_set_content_length(request, "0");


	/*** then must wake up osip ***/
	i = osip_transaction_init(&transaction,
		       NICT,
		       adosip->j_osip,
		       request);
	if (i!=0)
		goto si2perror2 ;

	/* send REGISTER */
	i = osip_message_to_str(request, &message1, &len);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1) osip_free(message1) ;

	sipevent = osip_new_outgoing_sipmessage(request);
	sipevent->transactionid =  transaction->transactionid;
	osip_message_force_update(request);
  
	osip_transaction_add_event(transaction, sipevent);

	adosip->ThreadWakeUp();
	return 0;

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

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

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

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

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

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

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

	i = osip_from_clone (request->from, &(response->from));
	if (i != 0)
		goto si2perror1;
	
	pos = 0;
	while (!osip_list_eol (request->vias, pos))
    {
		osip_via_t *via;
		osip_via_t *via2;

		via = (osip_via_t *) osip_list_get (request->vias, pos);
		i = osip_via_clone (via, &via2);
		if (i != -0)
			goto si2perror1;
		osip_list_add (response->vias, via2, -1);
		pos++;
    }

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

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

	/*add contact*/
	//predecessor
	if(status == 200)
	{
		if(type == USER_REGISTRATION || type == TRANSFER_REGISTRATION || type == THIRD_PARTY)
		{
			osip_contact_t *tmp;
			char *dest;
			int pos=0;
			while(pos<osip_list_size(request->contacts))
			{
				pos=osip_message_get_contact(request,0,&tmp);
				osip_contact_to_str(tmp,&dest);
				osip_message_set_contact(response,dest);
				if(dest)	osip_free(dest) ;
				pos++;
			}
		}
	
		else
		{
			tmp_uri = ChordId2Uri(chordId,false);
			char * pre_uri = ChordId2Uri(getFingerTable()->getPredecessor(),false) ;

			char *ctt = (char *) osip_malloc(strlen(tmp_uri) + strlen(";predecessor=") + strlen(pre_uri) +1) ;
			if (ctt == NULL)
				return NULL;
			sprintf (ctt, "%s;predecessor=%s", tmp_uri,pre_uri);
			osip_free(tmp_uri) ;	osip_free(pre_uri) ;

			osip_message_set_contact(response, ctt );
			osip_free(ctt) ;
		}
	}
	else//302
	{
		if(type == USER_REGISTRATION || type == TRANSFER_REGISTRATION || type == THIRD_PARTY)
		{
			tmp_uri = ChordId2Uri(contact,false);
			osip_message_set_contact(response,tmp_uri );
			osip_free(tmp_uri) ;
		}
		else
		{
			tmp_uri = ChordId2Uri(contact,false) ;
			char *ctt = (char *) osip_malloc(strlen(tmp_uri) + strlen(";predecessor=notprovided") +1) ;
			if (ctt == NULL)
				return NULL;
			sprintf (ctt, "%s;predecessor=notprovided", tmp_uri);
			if(tmp_uri) osip_free(tmp_uri) ;	
			osip_message_set_contact(response, ctt );
			if(tmp_uri) osip_free(ctt) ;
		}
		
	}

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

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

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

  	adosip->ThreadWakeUp();

	return 0;

si2perror1:
	osip_message_free (response);
	return -1;
}
Example #11
0
int
test_message (char *msg, size_t len, int verbose, int clone)
{
  osip_message_t *sip;

  {
    char *result;

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

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

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

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

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

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

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

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

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

                osip_free (tmp);
              }
              osip_message_free (copy);
            }
          }
          if (verbose)
            fprintf (stdout, "sequentials calls: done\n");
        }
        osip_free (result);
      }
      osip_message_free (sip);
    }
  }
  return 0;
}
Example #12
0
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;
}
Example #13
0
int bSipRegisterBuild(osip_message_t **regMsgPtrPtr)
{
	static int gSeqNum = 1;
	int status;
	char *callidNumberStr = NULL;
	char *seqNumStr = NULL;
	osip_call_id_t *callidPtr;
	char temp[MESSAGE_ENTRY_MAX_LENGTH];
	char sipPort[MESSAGE_ENTRY_MAX_LENGTH];
	osip_cseq_t *cseqPtr;
	unsigned int number;
	osip_message_t     *regMsgPtr;
	char expires[10];

	if((status = osip_message_init(&regMsgPtr)) != 0){
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"Can't init message!\n"));
		return -1;
	}
	osip_message_set_method(regMsgPtr, osip_strdup("OPTIONS"));

	osip_uri_init(&(regMsgPtr->req_uri));
	if ( ( status = osip_uri_parse(regMsgPtr->req_uri, SIP_PROXY) ) != 0)
	{
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"uri parse failed!\n"));
		osip_message_free(regMsgPtr);
		return -1;
	}
	osip_message_set_version(regMsgPtr, osip_strdup("SIP/2.0"));
	osip_message_set_status_code(regMsgPtr, 0);
	osip_message_set_reason_phrase(regMsgPtr, NULL);

	osip_message_set_to(regMsgPtr, SIP_TO);
	osip_message_set_from(regMsgPtr, SIP_FROM);

	if((status = osip_call_id_init(&callidPtr)) != 0 ){
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"call id failed!\n"));
		osip_message_free(regMsgPtr);
		return -1;
	}
	callidNumberStr = (char *)osip_malloc(MAX_ADDR_STR);
	number = osip_build_random_number();
	sprintf(callidNumberStr,"%u",number);
	osip_call_id_set_number(callidPtr, callidNumberStr);

	osip_call_id_set_host(callidPtr, osip_strdup("10.1.1.63"));

	regMsgPtr->call_id = callidPtr;

	if((status = osip_cseq_init(&cseqPtr)) != 0 ){
		OSIP_TRACE(osip_trace(__FILE__,__LINE__,OSIP_BUG,NULL,"seq init failed!\n"));
		osip_message_free(regMsgPtr);
		return -1;
	}
	gSeqNum++;
	seqNumStr = (char *)osip_malloc(MAX_ADDR_STR);
	sprintf(seqNumStr,"%i", gSeqNum);
	osip_cseq_set_number(cseqPtr, seqNumStr);
	osip_cseq_set_method(cseqPtr, osip_strdup("OPTIONS"));
	regMsgPtr->cseq = cseqPtr;

	osip_message_set_max_forwards(regMsgPtr, "70");

	sprintf(sipPort, "%i", SIP_PORT);
	sprintf(temp, "SIP/2.0/%s %s;branch=z9hG4bK%u", "UDP",LOCAL_IP,osip_build_random_number() );
	osip_message_set_via(regMsgPtr, temp);

	osip_message_set_contact(regMsgPtr, SIP_CONTACT);
	sprintf(expires, "%i", EXPIRES_TIME_INSECS);
	osip_message_set_expires(regMsgPtr, expires);

	osip_message_set_content_length(regMsgPtr, "0");

	osip_message_set_user_agent(regMsgPtr, "TotalView 1.0");

	AddSupportedMethods(regMsgPtr);
	*regMsgPtrPtr = regMsgPtr;
	return 0;
}
Example #14
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;
}
Example #15
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;
}
Example #16
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;
}
Example #17
0
int
_eXosip_build_response_default (struct eXosip_t *excontext, osip_message_t ** dest, osip_dialog_t * dialog, int status, osip_message_t * request)
{
  osip_generic_param_t *tag;
  osip_message_t *response;
  int i;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  osip_message_set_user_agent (response, excontext->user_agent);

  *dest = response;
  return OSIP_SUCCESS;
}
Example #18
0
int
Node::SndUserRegisterRequest(REG_TYPE type,
				uinfo_t *user_info,
				const char *registrar,
				int expires)
{
	char *message1;
	unsigned int len = 0;

	osip_message_t *request;
	osip_event_t *sipevent;
	osip_transaction_t *transaction;
	osip_uri_t *uri;
	
	char *tmp_uri;
	int i;

	i=osip_message_init(&request);
	if(i!=0)
		goto si2perror1;
	osip_message_set_method(request,strdup("REGISTER"));
	osip_message_set_version(request,strdup("SIP/2.0"));
	osip_message_set_status_code(request,0);
	osip_message_set_reason_phrase(request,NULL);

	i=osip_uri_init(&uri);
	if(i!=0)
		goto si2perror2;
	i=osip_uri_parse(uri,osip_strdup(registrar));
	osip_message_set_uri(request,uri);

	//*set to,from and header
	if(type==USER_REGISTRATION)
	{	
		char *dest1;
		char *dest2;
		
		osip_uri_to_str(user_info->aor->uri,&dest1);
		i=osip_message_set_to(request,dest1);
		i=osip_message_set_from(request,dest1);
		if(dest1)	osip_free(dest1) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("user_registration"));

		osip_contact_to_str(user_info->bindings->contact,&dest2);
		i=osip_message_set_contact(request,osip_strdup(dest2));
		if(dest2)	osip_free(dest2) ;
	}

	if(type == RED_REGISTER)
	{
		char *dest1;
		char *dest2;
		osip_uri_to_str(user_info->aor->uri,&dest1);
		i=osip_message_set_to(request,dest1);
		i=osip_message_set_from(request,dest1);
		if(dest1)	osip_free(dest1) ;
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("red_register"));

		osip_contact_to_str(user_info->bindings->contact,&dest2);
		i=osip_message_set_contact(request,osip_strdup(dest2));
		if(dest2)	osip_free(dest2) ;
	}

	if(type==TRANSFER_REGISTRATION)
	{
		char *dest1;
		char *dest2;
		//string tmp_uri;
		osip_uri_to_str(user_info->aor->uri,&dest1);
		osip_message_set_to(request,dest1);
		if(dest1)	osip_free(dest1) ;

		tmp_uri = ChordId2Uri(chordId,false);
		osip_message_set_from(request,tmp_uri);
		osip_from_param_add (request->from, osip_strdup ("user"), osip_strdup("transfer_registration"));
		if(tmp_uri)	osip_free(tmp_uri) ;

		osip_contact_to_str(user_info->bindings->contact,&dest2);
		osip_message_set_contact(request,dest2);
		if(dest2)	osip_free(dest2) ;
	}
	if(type==THIRD_PARTY)
	{
		//todo
	}
		//---set call_id and cseq
	osip_call_id_t *callid;
	osip_cseq_t *cseq;
	char *num;
	char  *cidrand;
	char *register_callid_number ;

	//* call-id is always the same for REGISTRATIONS 
	i = osip_call_id_init(&callid);
	if (i!=0) 
		goto si2perror2;
	cidrand = osip_strdup("BF9598C48B184EBBAFADFE527EED8186") ;
	osip_call_id_set_number(callid, cidrand);
	register_callid_number = cidrand;

	osip_call_id_set_host(callid, osip_strdup("SI2P.COM"));
	request->call_id = callid;

	//cseq
	i = osip_cseq_init(&cseq);
	if (i!=0) 
		goto si2perror2 ;
	num = osip_strdup("1");
	osip_cseq_set_number(cseq, num);
	osip_cseq_set_method(cseq, osip_strdup("REGISTER"));
	request->cseq = cseq;
	 
	//*the Max-Forward header 
	osip_message_set_max_forwards(request, "5"); //* a UA should start a request with 70 

	//* the via header 
	char tmp[200];
    snprintf(tmp, 200, "SIP/2.0/%s %s:%s;rport;branch=z9hG4bK%u", "UDP",
	      localip,
	      localport,
	      via_branch_new_random() );

    osip_message_set_via(request, tmp);

	//* the UA header 
	osip_message_set_user_agent(request, user_agent);

	//*  the expires header 
	char exp[10]; //* MUST never be ouside 1 and 3600 
	snprintf(exp, 9, "%i", expires);
	osip_message_set_expires(request, exp);
	osip_message_set_content_length(request, "0");


	//*** then must wake up osip 
	i = osip_transaction_init(&transaction,
		       NICT,
		       adosip->j_osip,
		       request);
	if (i!=0)
		goto si2perror3 ;

	//* send REGISTER 
	i = osip_message_to_str(request, &message1, &len);
	LogStream("SEND======================================>>\n") ;
	LogStream(message1) ;
	if(message1) osip_free(message1) ;
//	printf("SEND======================================>>\n") ;
//	printf(message1) ;
	sipevent = osip_new_outgoing_sipmessage(request);
	sipevent->transactionid =  transaction->transactionid;
	osip_message_force_update(request);
  
	osip_transaction_add_event(transaction, sipevent);

	adosip->ThreadWakeUp();
	return 0;

si2perror1:
	
	return -1;

si2perror2:
	if(request!=NULL)
		osip_message_free(request);
	return -1;

si2perror3:
	if(transaction!=NULL)
		osip_message_free(request);
	return -1;


}
Example #19
0
int main (int argc, char *argv[]) 
{
   int sts;
   int i;
   int buflen;
   int access;
   char buff [BUFFER_SIZE];
   sip_ticket_t ticket;

   extern char *optarg;         /* Defined in libc getopt and unistd.h */
   int ch1;
   
   char configfile[64]="siproxd";       /* basename of configfile */
   int  config_search=1;                /* search the config file */
   int  cmdline_debuglevel=0;
   char *pidfilename=NULL;
   struct sigaction act;

   log_set_stderr(1);

/*
 * setup signal handlers
 */
   act.sa_handler=sighandler;
   sigemptyset(&act.sa_mask);
   act.sa_flags=SA_RESTART;
   if (sigaction(SIGTERM, &act, NULL)) {
      ERROR("Failed to install SIGTERM handler");
   }
   if (sigaction(SIGINT, &act, NULL)) {
      ERROR("Failed to install SIGINT handler");
   }
   if (sigaction(SIGUSR2, &act, NULL)) {
      ERROR("Failed to install SIGUSR2 handler");
   }


/*
 * prepare default configuration
 */
   make_default_config();

   log_set_pattern(configuration.debuglevel);

/*
 * parse command line
 */
{
#ifdef  HAVE_GETOPT_LONG
   int option_index = 0;
   static struct option long_options[] = {
      {"help", no_argument, NULL, 'h'},
      {"config", required_argument, NULL, 'c'},
      {"debug", required_argument, NULL, 'd'},
      {"pid-file", required_argument, NULL,'p'},
      {0,0,0,0}
   };

    while ((ch1 = getopt_long(argc, argv, "hc:d:p:",
                  long_options, &option_index)) != -1) {
#else   /* ! HAVE_GETOPT_LONG */
    while ((ch1 = getopt(argc, argv, "hc:d:p:")) != -1) {
#endif
      switch (ch1) {
      case 'h': /* help */
         DEBUGC(DBCLASS_CONFIG,"option: help");
         fprintf(stderr,str_helpmsg);
         exit(0);
         break;

      case 'c': /* load config file */
         DEBUGC(DBCLASS_CONFIG,"option: config file=%s",optarg);
         i=sizeof(configfile)-1;
         strncpy(configfile,optarg,i-1);
         configfile[i]='\0';
         config_search=0;
         break; 

      case 'd': /* set debug level */
         DEBUGC(DBCLASS_CONFIG,"option: set debug level: %s",optarg);
         cmdline_debuglevel=atoi(optarg);
         log_set_pattern(cmdline_debuglevel);
         break;

      case 'p':
         pidfilename = optarg;
         break;

      default:
         DEBUGC(DBCLASS_CONFIG,"no command line options");
         break; 
      }
   }
}

/*
 * Init stuff
 */
   INFO(PACKAGE"-"VERSION"-"BUILDSTR" "UNAME" starting up");

   /* read the config file */
   if (read_config(configfile, config_search) == STS_FAILURE) exit(1);

   /* if a debug level > 0 has been given on the commandline use its
      value and not what is in the config file */
   if (cmdline_debuglevel != 0) {
      configuration.debuglevel=cmdline_debuglevel;
   }

/*
 * open a the pwfile instance, so we still have access after
 * we possibly have chroot()ed to somewhere.
 */
   if (configuration.proxy_auth_pwfile) {
      siproxd_passwordfile = fopen(configuration.proxy_auth_pwfile, "r");
   } else {
      siproxd_passwordfile = NULL;
   }

   /* set debug level as desired */
   log_set_pattern(configuration.debuglevel);
   log_set_listen_port(configuration.debugport);

   /* change user and group IDs */
   secure_enviroment();

   /* daemonize if requested to */
   if (configuration.daemonize) {
      DEBUGC(DBCLASS_CONFIG,"daemonizing");
      if (fork()!=0) exit(0);
      setsid();
      if (fork()!=0) exit(0);

      log_set_stderr(0);
      INFO("daemonized, pid=%i", getpid());
   }

   /* write PID file of main thread */
   if (pidfilename == NULL) pidfilename = configuration.pid_file;
   if (pidfilename) {
      FILE *pidfile;
      DEBUGC(DBCLASS_CONFIG,"creating PID file [%s]", pidfilename);
      sts=unlink(configuration.pid_file);
      if ((sts==0) ||(errno == ENOENT)) {
         if ((pidfile=fopen(pidfilename, "w"))) {
            fprintf(pidfile,"%i\n",(int)getpid());
            fclose(pidfile);
         } else {
            WARN("couldn't create new PID file: %s", strerror(errno));
         }
      } else {
         WARN("couldn't delete old PID file: %s", strerror(errno));
      }
   }

   /* initialize the RTP proxy */
   sts=rtpproxy_init();
   if (sts != STS_SUCCESS) {
      ERROR("unable to initialize RTP proxy - aborting"); 
      exit(1);
   }

   /* init the oSIP parser */
   parser_init();

   /* listen for incoming messages */
   sts=sipsock_listen();
   if (sts == STS_FAILURE) {
      /* failure to allocate SIP socket... */
      ERROR("unable to bind to SIP listening socket - aborting"); 
      exit(1);
   }

   /* initialize the registration facility */
   register_init();

/*
 * silence the log - if so required...
 */
   log_set_silence(configuration.silence_log);

   INFO(PACKAGE"-"VERSION"-"BUILDSTR" "UNAME" started");

/*****************************
 * Main loop
 *****************************/
   while (!exit_program) {

      DEBUGC(DBCLASS_BABBLE,"going into sipsock_wait\n");
      while (sipsock_wait()<=0) {
         /* got no input, here by timeout. do aging */
         register_agemap();

         /* TCP log: check for a connection */
         log_tcp_connect();

         /* dump memory stats if requested to do so */
         if (dmalloc_dump) {
            dmalloc_dump=0;
#ifdef DMALLOC
            INFO("SIGUSR2 - DMALLOC statistics is dumped");
            dmalloc_log_stats();
            dmalloc_log_unfreed();
#else
            INFO("SIGUSR2 - DMALLOC support is not compiled in");
#endif
         }

         if (exit_program) goto exit_prg;
      }

      /*
       * got input, process
       */
      DEBUGC(DBCLASS_BABBLE,"back from sipsock_wait");
      ticket.direction=0;

      buflen=sipsock_read(&buff, sizeof(buff)-1, &ticket.from,
                           &ticket.protocol);
      buff[buflen]='\0';

      /*
       * evaluate the access lists (IP based filter)
       */
      access=accesslist_check(ticket.from);
      if (access == 0) {
         DEBUGC(DBCLASS_ACCESS,"access for this packet was denied");
         continue; /* there are no resources to free */
      }

      /*
       * integrity checks
       */
      sts=security_check_raw(buff, buflen);
      if (sts != STS_SUCCESS) {
         DEBUGC(DBCLASS_SIP,"security check (raw) failed");
         continue; /* there are no resources to free */
      }

      /*
       * init sip_msg
       */
      sts=osip_message_init(&ticket.sipmsg);
      ticket.sipmsg->message=NULL;
      if (sts != 0) {
         ERROR("osip_message_init() failed... this is not good");
         continue; /* skip, there are no resources to free */
      }

      /*
       * RFC 3261, Section 16.3 step 1
       * Proxy Behavior - Request Validation - Reasonable Syntax
       * (parse the received message)
       */
      sts=sip_message_parse(ticket.sipmsg, buff, buflen);
      if (sts != 0) {
         ERROR("sip_message_parse() failed... this is not good");
         DUMP_BUFFER(-1, buff, buflen);
         goto end_loop; /* skip and free resources */
      }

      /*
       * integrity checks - parsed buffer
       */
      sts=security_check_sip(&ticket);
      if (sts != STS_SUCCESS) {
         ERROR("security_check_sip() failed... this is not good");
         DUMP_BUFFER(-1, buff, buflen);
         goto end_loop; /* skip and free resources */
      }

      /*
       * RFC 3261, Section 16.3 step 2
       * Proxy Behavior - Request Validation - URI scheme
       * (check request URI and refuse with 416 if not understood)
       */
      /* NOT IMPLEMENTED */

      /*
       * RFC 3261, Section 16.3 step 3
       * Proxy Behavior - Request Validation - Max-Forwards check
       * (check Max-Forwards header and refuse with 483 if too many hops)
       */
      {
         osip_header_t *max_forwards;
         int forwards_count = DEFAULT_MAXFWD;

         osip_message_get_max_forwards(ticket.sipmsg, 0, &max_forwards);
         if (max_forwards && max_forwards->hvalue) {
            forwards_count = atoi(max_forwards->hvalue);
         }

         DEBUGC(DBCLASS_PROXY,"checking Max-Forwards (=%i)",forwards_count);
         if (forwards_count <= 0) {
            DEBUGC(DBCLASS_SIP, "Forward count reached 0 -> 483 response");
            sip_gen_response(&ticket, 483 /*Too many hops*/);
            goto end_loop; /* skip and free resources */
         }
      }

      /*
       * RFC 3261, Section 16.3 step 4
       * Proxy Behavior - Request Validation - Loop Detection check
       * (check for loop and return 482 if a loop is detected)
       */
      if (check_vialoop(&ticket) == STS_TRUE) {
         /* make sure we don't end up in endless loop when detecting
          * an loop in an "loop detected" message - brrr */
         if (MSG_IS_RESPONSE(ticket.sipmsg) && 
             MSG_TEST_CODE(ticket.sipmsg, 482)) {
            DEBUGC(DBCLASS_SIP,"loop in loop-response detected, ignoring");
         } else {
            DEBUGC(DBCLASS_SIP,"via loop detected, ignoring request");
            sip_gen_response(&ticket, 482 /*Loop detected*/);
         }
         goto end_loop; /* skip and free resources */
      }

      /*
       * RFC 3261, Section 16.3 step 5
       * Proxy Behavior - Request Validation - Proxy-Require check
       * (check Proxy-Require header and return 420 if unsupported option)
       */
      /* NOT IMPLEMENTED */

      /*
       * RFC 3261, Section 16.5
       * Proxy Behavior - Determining Request Targets
       */
      /* NOT IMPLEMENTED */

      DEBUGC(DBCLASS_SIP,"received SIP type %s:%s",
             (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES",
             (MSG_IS_REQUEST(ticket.sipmsg) ?
                ((ticket.sipmsg->sip_method)?
                   ticket.sipmsg->sip_method : "NULL") :
                ((ticket.sipmsg->reason_phrase) ? 
                   ticket.sipmsg->reason_phrase : "NULL")));

      /*********************************
       * The message did pass all the
       * tests above and is now ready
       * to be proxied.
       * Before we do so, we apply some
       * additional preprocessing
       *********************************/
      /* Dial shortcuts */
      if (configuration.pi_shortdial) {
         sts = plugin_shortdial(&ticket);
         if (sts == STS_SIP_SENT) goto end_loop;
      }


      /*********************************
       * finally proxy the message.
       * This includes the masquerading
       * of the local UA and starting/
       * stopping the RTP proxy for this
       * call
       *********************************/

      /*
       * if a REQ REGISTER, check if it is directed to myself,
       * or am I just the outbound proxy but no registrar.
       * - If I'm the registrar, register & generate answer
       * - If I'm just the outbound proxy, register, rewrite & forward
       */
      if (MSG_IS_REGISTER(ticket.sipmsg) && 
          MSG_IS_REQUEST(ticket.sipmsg)) {
         if (access & ACCESSCTL_REG) {
            osip_uri_t *url;
            struct in_addr addr1, addr2, addr3;
            int dest_port;

            url = osip_message_get_uri(ticket.sipmsg);
            dest_port= (url->port)?atoi(url->port):SIP_PORT;

            if ( (get_ip_by_host(url->host, &addr1) == STS_SUCCESS) &&
                 (get_interface_ip(IF_INBOUND,&addr2) == STS_SUCCESS) &&
                 (get_interface_ip(IF_OUTBOUND,&addr3) == STS_SUCCESS)) {

               if ((configuration.sip_listen_port == dest_port) &&
                   ((memcmp(&addr1, &addr2, sizeof(addr1)) == 0) ||
                    (memcmp(&addr1, &addr3, sizeof(addr1)) == 0))) {
                  /* I'm the registrar, send response myself */
                  sts = register_client(&ticket, 0);
                  sts = register_response(&ticket, sts);
               } else {
                  /* I'm just the outbound proxy */
                  DEBUGC(DBCLASS_SIP,"proxying REGISTER request to:%s",
                         url->host);
                  sts = register_client(&ticket, 1);
                  if (sts == STS_SUCCESS) {
                     sts = proxy_request(&ticket);
                  }
               }
            } else {
               sip_gen_response(&ticket, 408 /*request timeout*/);
            }
         } else {
            WARN("non-authorized registration attempt from %s",
                 utils_inet_ntoa(ticket.from.sin_addr));
         }

      /*
       * check if outbound interface is UP.
       * If not, send back error to UA and
       * skip any proxying attempt
       */
      } else if (get_interface_ip(IF_OUTBOUND,NULL) !=
                 STS_SUCCESS) {
         DEBUGC(DBCLASS_SIP, "got a %s to proxy, but outbound interface "
                "is down", (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES");

         if (MSG_IS_REQUEST(ticket.sipmsg))
            sip_gen_response(&ticket, 408 /*request timeout*/);
      
      /*
       * MSG is a request, add current via entry,
       * do a lookup in the URLMAP table and
       * send to the final destination
       */
      } else if (MSG_IS_REQUEST(ticket.sipmsg)) {
         if (access & ACCESSCTL_SIP) {
            sts = proxy_request(&ticket);
         } else {
            INFO("non-authorized request received from %s",
                 utils_inet_ntoa(ticket.from.sin_addr));
         }

      /*
       * MSG is a response, remove current via and
       * send to the next VIA in chain
       */
      } else if (MSG_IS_RESPONSE(ticket.sipmsg)) {
         if (access & ACCESSCTL_SIP) {
            sts = proxy_response(&ticket);
         } else {
            INFO("non-authorized response received from %s",
                 utils_inet_ntoa(ticket.from.sin_addr));
         }
         
      /*
       * unsupported message
       */
      } else {
         ERROR("received unsupported SIP type %s %s",
               (MSG_IS_REQUEST(ticket.sipmsg))? "REQ" : "RES",
               ticket.sipmsg->sip_method);
      }

      /*********************************
       * Done with proxying. Message
       * has been sent to its destination.
       *********************************/
/*
 * free the SIP message buffers
 */
      end_loop:
      osip_message_free(ticket.sipmsg);

   } /* while TRUE */
   exit_prg:

   /* save current known SIP registrations */
   register_save();
   INFO("properly terminating siproxd");

   /* remove PID file */
   if (pidfilename) {
      DEBUGC(DBCLASS_CONFIG,"deleting PID file [%s]", pidfilename);
      sts=unlink(pidfilename);
      if (sts != 0) {
         WARN("couldn't delete old PID file: %s", strerror(errno));
      }
   }

   /* END */
   return 0;
} /* main */

/*
 * Signal handler
 *
 * this one is called asynchronously whevener a registered
 * signal is applied. Just set a flag and don't do any funny
 * things here.
 */
static void sighandler(int sig) {
   if (sig==SIGTERM) exit_program=1;
   if (sig==SIGINT)  exit_program=1;
   if (sig==SIGUSR2) dmalloc_dump=1;
   return;
}
Example #20
0
//---------------------------------------------------------------------
int
Node::SndResponse4Query(int status,
						const char *contact,
						osip_transaction_t *tr,
						osip_message_t *request)
{
	char *message1;
	size_t length = 0;
	
	//
	osip_generic_param_t *tag;
	osip_message_t *response;
	osip_event_t *evt ;
	char *tmp;
	int pos;
	int i;

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

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

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

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

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

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

	i = osip_from_clone (request->from, &(response->from));
	if (i != 0)
		goto si2perror1;
	
	pos = 0;
	while (!osip_list_eol (request->vias, pos))
    {
		osip_via_t *via;
		osip_via_t *via2;

		via = (osip_via_t *) osip_list_get (request->vias, pos);
		i = osip_via_clone (via, &via2);
		if (i != -0)
			goto si2perror1;
		osip_list_add (response->vias, via2, -1);
		pos++;
    }

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

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

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

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

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

  	adosip->ThreadWakeUp();

	return 0;

si2perror1:
	osip_message_free (response);
	return -1;
}
Example #21
0
static struct session			*sip_callback(struct session *sip, u_char *data, uint32_t len)
{
  osip_message_t			*msg;
  struct tuple4				addr;
  struct session			*start;
  struct session			*rtp;
  osip_call_id_t			*call_id;

  osip_message_init(&msg);
  if (!osip_message_parse(msg, (char *)data, len)) {
    if (NULL == sip->u.sip_params.call_id) {
      /*
       * If the session object was created by udp_callback
       * we need to fill in the call_id field here because
       * udp_callback doesn't know anything about SIP
       */
      if (NULL != (call_id = osip_message_get_call_id(msg)))
	osip_call_id_clone(call_id, &sip->u.sip_params.call_id);
    } else {
      /*
       * Otherwise check if the session object passed to this
       * function call was really the one corresponding to the
       * call ID in the SIP packet, in case several SIP calls
       * are passed upon the same transport layer protocol,
       * source and destination IPv4 address & port combination.
       * udp_callback has no way of knowing how to distinguish
       * SIP session objects based on call ID, so we have to do
       * it here. We just continue searching in the list of all
       * tracked sessions for similar SIP objects until we find
       * one that has the same call ID, or else we create a new
       * SIP session object that corresponds to the new call.
       */
      start = sip;
      do {
	if (NULL == (call_id = osip_message_get_call_id(msg)))
	  continue;
	if (!osip_call_id_match(sip->u.sip_params.call_id, call_id))
	  break;
      } while ((sip = sessions_find(sip->next, TYPE_SIP, 0, &sip->addr)));
      if (NULL == sip) {
	if (bonus_time) {
	  osip_message_free(msg);
	  return start;
	}
	sip = sessions_add(start->type, &start->addr, NULL);
	if (NULL != (call_id = osip_message_get_call_id(msg)))
	  osip_call_id_clone(call_id, &sip->u.sip_params.call_id);
      }
    }
    /*
     * If the current SIP packet is an INVITE message, store the
     * advertised source port and IPv4 address. It is not very
     * important, since we can do only with the destination part
     * (useful in case the capture missed the INVITE packet), but
     * it helps discriminating from unrelated packets.
     *
     * Unfortunately, some SIP implementations such as the one in
     * Audiocodes Mediant 1000 SIP gateways actually use a source
     * port different from the one they advertised in the INVITE
     * message parameters - how outrageous! - so we have to make
     * our sessions search engine ignore the source port part by
     * zeroing it :-/
     */
    if (MSG_IS_INVITE(msg)) {
      if (!bonus_time) {
	sip_get_address(msg, &sip->u.sip_params.rtp_addr.saddr, &sip->u.sip_params.rtp_addr.source);
#ifndef USING_NON_STUPID_SIP_IMPLEMENTATIONS
	sip->u.sip_params.rtp_addr.source = 0;
#endif
      }
    } else
      if (MSG_TEST_CODE(msg, 200)) {
	if (MSG_IS_RESPONSE_FOR(msg, "INVITE")) {
	  if (!bonus_time && sip_get_address(msg, &sip->u.sip_params.rtp_addr.daddr, &sip->u.sip_params.rtp_addr.dest)) {
	    sessions_add(TYPE_UDP | TYPE_RTP, &sip->u.sip_params.rtp_addr, sip);
	    sip->u.sip_params.picked_up = 1;
	  }
	} else
	  if (MSG_IS_RESPONSE_FOR(msg, "BYE") ||
	      MSG_IS_RESPONSE_FOR(msg, "CANCEL")) {
	    start = first_session;
	    while (NULL != (rtp = sessions_find(start, TYPE_RTP, sip->id, NULL))) {
	      sessions_del(rtp);
	      start = rtp->next;
	    }
	    /*
	     * Mark for deletion in 2 seconds, in order to give some
	     * time to the extra ACK packets that might be exchanged
	     */
	    if (sip->type & TYPE_UDP)
	      sip->timeout = nids_last_pcap_header->ts.tv_sec + 2;
	  }
      }
  }
  osip_message_free(msg);
  return sip;
}
Example #22
0
osip_message_t* init_sip_msg_from_src (const osip_message_t *sipmsg, const int code)
{
    __tri(init_sip_msg_from_src);

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

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


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

                osip_message_set_contact(sipgen, contact);

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

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

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

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

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

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

           osip_call_id_clone (sipmsg->call_id, &sipgen->call_id);
           osip_cseq_clone    (sipmsg->cseq,    &sipgen->cseq);
           __tre(return) sipgen;
       }
   }
Example #23
0
/* return NULL  if message cannot be parsed            */
osip_event_t *
osip_parse (char *buf)
{
  osip_event_t *se = __osip_event_new (UNKNOWN_EVT, 0);
  int i;

#ifdef TEST_PARSER_SPEED
  {
    int kk;
    int pstime1, pstime;
    struct timespec tv1;

    clock_get_time (CLOCK_REALTIME, &tv1);
    pstime = ((tv1.tv_sec * 1000) + (tv1.tv_nsec / 1000000));
    for (kk = 0; kk < 10000; kk++)
      {

	i = osip_message_init (&(se->sip));

	if (osip_message_parse (se->sip, buf) == -1)
	  {
	    fprintf (stdout, "osip_message_parse retrun -1\n");
	    osip_message_free (se->sip);
	  }
	else
	  {			/* msg is parsed */
	    osip_message_free (se->sip);
	  }
      }
    clock_get_time (CLOCK_REALTIME, &tv1);
    pstime1 = ((tv1.tv_sec * 1000) + (tv1.tv_nsec / 1000000));
    fprintf (stdout, "CPU clock ticks for 10000 messages - T1: %i - T2: %i\n",
	     pstime1, pstime);
    fprintf (stdout, "CPU time for 10000 messages - %d\n",
	     (pstime1 - pstime));
  }
  osip_free (se);
  return NULL;
#endif
  /* parse message and set up an event */
  i = osip_message_init (&(se->sip));
  if (osip_message_parse (se->sip, buf) == -1)
    {
      OSIP_TRACE (osip_trace
		  (__FILE__, __LINE__, OSIP_ERROR, NULL,
		   "could not parse message\n"));
      osip_message_free (se->sip);
      osip_free (se);
      return NULL;
    }
  else
    {
      if (se->sip->call_id!=NULL && se->sip->call_id->number!=NULL)
	{
	  OSIP_TRACE (osip_trace
		      (__FILE__, __LINE__, OSIP_INFO3, NULL,
		       "MESSAGE REC. CALLID:%s\n", se->sip->call_id->number));
	}

      if (MSG_IS_REQUEST(se->sip))
	{
	  if (se->sip->sip_method==NULL || se->sip->req_uri==NULL)
	    {
	      osip_message_free (se->sip);
	      osip_free (se);
	      return NULL;
	    }
	}

      se->type = evt_set_type_incoming_sipmessage (se->sip);
      return se;
    }
}