示例#1
0
int
osip_dialog_match_as_uac (osip_dialog_t * dlg, osip_message_t * answer)
{
    osip_generic_param_t *tag_param_local;
    osip_generic_param_t *tag_param_remote;
    char *tmp;
    int i;

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

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

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

    /* we don't have to compare
       remote_uri with from
       && local_uri with to.    ----> we have both tag recognized, it's enough..
     */
    if (0 == strcmp (tag_param_remote->gvalue, dlg->remote_tag))
        return 0;
    return -1;
}
示例#2
0
int main(int argc, char **argv)
{
	FILE *callids_file;


	osip_call_id_t *callid;
	char *a_callid;
	char *dest;
	char *res;

	callids_file = fopen(argv[1], "r");
	if (callids_file == NULL) {
		fprintf(stdout,
				"Failed to open %s file.\nUsage: tcallid callids.txt\n", argv[1]);
		exit(0);
	}

	a_callid = (char *) osip_malloc(200);
	res = fgets(a_callid, 200, callids_file);	/* lines are under 200 */
	while (res != NULL) {

		int errcode;

		/* remove the last '\n' before parsing */
		strncpy(a_callid + strlen(a_callid) - 1, "\0", 1);

		if (0 != strncmp(a_callid, "#", 1)) {
			/* allocate & init callid */
			osip_call_id_init(&callid);
			printf("=================================================\n");
			printf("CALLID TO PARSE: |%s|\n", a_callid);
			errcode = osip_call_id_parse(callid, a_callid);
			if (errcode != -1) {
				if (osip_call_id_to_str(callid, &dest) != -1) {
					printf("result:        |%s|\n", dest);
					osip_free(dest);
				}
			} else
				printf("Bad callid format: %s\n", a_callid);
			osip_call_id_free(callid);
			printf("=================================================\n");
		}
		res = fgets(a_callid, 200, callids_file);	/* lines are under 200 */
	}
	osip_free(a_callid);
	return 0;
}
static int
__osip_dialog_init (osip_dialog_t ** dialog, osip_message_t * invite, osip_message_t * response, osip_from_t * local, osip_to_t * remote, osip_message_t * remote_msg)
{
  int i;
  int pos;
  osip_generic_param_t *tag;

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

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

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

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

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

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

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

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

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

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

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

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

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

    pos++;
  }

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

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

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

  {
    osip_contact_t *contact;

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

  return OSIP_SUCCESS;
}
int
osip_dialog_match_as_uas (osip_dialog_t * dlg, osip_message_t * request)
{
  osip_generic_param_t *tag_param_remote;
  int i;
  char *tmp;

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

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

  /* for INCOMING REQUEST:
     To: local_uri;local_tag           <- LOCAL TAG ALWAYS EXIST
     From: remote_uri;remote_tag
   */

  if (dlg->local_tag == NULL)
    /* NOT POSSIBLE BECAUSE I MANAGE REMOTE_TAG AND I ALWAYS ADD IT! */
    return OSIP_SYNTAXERROR;

#if 0
  /* VR-2785: use line param to distinguish between two registrations by the same user */
  if (dlg->line_param) {
    osip_uri_param_t *line_param;

    i = osip_uri_param_get_byname (&request->req_uri->url_params, "line", &line_param);
    if (i == 0 && strcmp (dlg->line_param, line_param->gvalue))
      return OSIP_UNDEFINED_ERROR;      /* both dlg and req_uri have line params and they do not match */
  }
#endif

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

  if (dlg->remote_tag == NULL) {        /* tag in response BUT no tag in dialog */
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "Remote UA is not compliant: missing a tag in To feilds!\n"));
    if (0 == osip_from_compare ((osip_from_t *) dlg->remote_uri, (osip_from_t *) request->from)
        && 0 == osip_from_compare (dlg->local_uri, request->to))
      return OSIP_SUCCESS;
    return OSIP_UNDEFINED_ERROR;
  }
  /* we don't have to compare
     remote_uri with from
     && local_uri with to.    ----> we have both tag recognized, it's enough..
   */
  if (0 == strcmp (tag_param_remote->gvalue, dlg->remote_tag))
    return OSIP_SUCCESS;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

    {
        osip_contact_t *contact;

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

    return 0;

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

    return -1;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

    {
        osip_contact_t *contact;

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

    return 0;

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

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

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

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

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

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

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

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

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

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

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

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

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

    return 0;

diau_error_4:
    osip_from_free ((*dialog)->remote_uri);
diau_error_3:
    osip_free ((*dialog)->remote_tag);
    osip_free ((*dialog)->local_tag);
diau_error_1:
    osip_free ((*dialog)->call_id);
diau_error_0:
    OSIP_TRACE (osip_trace
                (__FILE__, __LINE__, OSIP_ERROR, NULL,
                 "Could not establish dialog!\n"));
    osip_free (*dialog);
    *dialog = NULL;
    return -1;
}
示例#8
0
int
osip_dialog_match_as_uas (osip_dialog_t * dlg, osip_message_t * request)
{
  osip_generic_param_t *tag_param_remote;
  int i;
  char *tmp;

  if (dlg == NULL)
    return -1;
  if (request == NULL || request->call_id == NULL ||
      request->from == NULL || request->to == NULL)
    return -1;

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

  /* for INCOMING REQUEST:
     To: local_uri;local_tag           <- LOCAL TAG ALWAYS EXIST
     From: remote_uri;remote_tag
   */

  if (dlg->local_tag == NULL)
    /* NOT POSSIBLE BECAUSE I MANAGE REMOTE_TAG AND I ALWAYS ADD IT! */
    return -1;

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

  if (dlg->remote_tag == NULL)  /* tag in response BUT no tag in dialog */
    {
      OSIP_TRACE (osip_trace
                  (__FILE__, __LINE__, OSIP_WARNING, NULL,
                   "Remote UA is not compliant: missing a tag in To feilds!\n"));
      if (0 ==
          osip_from_compare ((osip_from_t *) dlg->remote_uri,
                             (osip_from_t *) request->from)
          && 0 == osip_from_compare (dlg->local_uri, request->to))
        return 0;
      return -1;
    }
  /* we don't have to compare
     remote_uri with from
     && local_uri with to.    ----> we have both tag recognized, it's enough..
   */
  if (0 == strcmp (tag_param_remote->gvalue, dlg->remote_tag))
    return 0;

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

   hashstring[0]='\0';

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

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

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

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

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

      MD5Init(&Md5Ctx);

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

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

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

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

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

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

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

   return STS_SUCCESS;
}