⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sipua.c

📁 未改进的经典的SIP NETWORK API。 请尽快给下载权限
💻 C
📖 第 1 页 / 共 3 页
字号:
 *      status_code   : The status code (1xx) received in the response 
 *      reason_phrase : The character string in the received status line
 *      response      : Additional headers in the response
 *      body          : The received body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_get_media
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_uint32_t                  status_code,
    char                        * reason_phrase,
    sip_msg_header_vector_t     * response,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  sip_status_t    sip_status;
  sip_trans_t new_trans;

  printf("Get media received\n");

  /* send ACK request */
  sip_status = sip_call_connect(instance,
                   call,
                   dialog,
                   transaction,
                   NULL,
                   body,
                   NULL);
  if(sip_status == SIP_NORMAL)
  {
  	printf("sip_call_connect is success!\n");
	printf("sending ACK!!\n");
  }
  printf("The call has been accepted (in get_media).\n");

  if(!no_bye) {

    printf("Waiting 3 seconds before releasing the call...\n");
    sleep(3);
    printf("Sending BYE request...\n");
    
    /* send BYE request */
    sip_call_release(instance,
		     call,
		     dialog,
		     &new_trans,
		     NULL,
		     NULL,
		     SIP_CALL_RELEASE_GRACEFUL,
                     user_info);
    
    printf("The call is terminated.\n");
    
    sip_exit = SIP_TRUE;
  }
  
  return SIP_NORMAL;
}


/*
 * FACILITY:
 *
 *      sip_call_on_request
 *
 * ABSTRACT:
 *
 *      Any request that cannot be mapped on defined callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      subscription  : The subscription identifier
 *      method        : The method type
 *      method_name   : The method name
 *      request_uri   : The request URI in the message
 *      from          : The from header
 *      to            : The to header
 *      request       : Additional headers in the INVITE
 *      body          : The received body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_request
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_subs_t                    subscription,
    sip_msg_method_t              method,
    char                        * method_name,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_t            * from,
    sip_msg_header_t            * to,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  printf("Unknown request received\n");
  
  return SIP_NORMAL;
}


/*
 * FACILITY:
 *
 *      sip_call_on_reply
 *
 * ABSTRACT:
 *
 *      Any response that cannot be mapped on defined callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      subscription  : The subscription identifier
 *      method        : The method type
 *      method_name   : The method name
 *      status_code   : The status code (1xx) received in the response 
 *      reason_phrase : The character string in the received status line
 *      response      : Additional headers in the response
 *      body          : The received body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_reply
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_subs_t                    subscription,
    sip_msg_method_t              method,
    char                        * method_name,
    sip_uint32_t                  status_code,
    char                        * reason_phrase,
    sip_msg_header_vector_t     * response,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  printf("Response received: '%d", status_code);
  if(reason_phrase) {
    printf(" %s", reason_phrase);
  }
  printf("'\n");
  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      The SIP call subscription reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      subscription  : The subscription identifier
 *      request_uri   : The request URI to forward the INVITE
 *      from          : The from header
 *      to            : The to header
 *      contact       : The contact header
 *      event         : The event header for subscription
 *      expires       : The event expiration header
 *      request       : Additional headers in the INVITE
 *      body          : The INVITE body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_evt_subscribe
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_subs_t                    subscription,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_t            * from,
    sip_msg_header_t            * to,
    sip_msg_header_t            * contact,
    sip_msg_header_t            * event,
    sip_msg_header_t            * expires,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
)
{
  sip_status_t       sip_status;
  int                index;
  sip_msg_header_t * contact_response;
  sip_msg_header_t * expires_response;
  sip_msg_header_t * subscription_state;
  char from_string[255];

  if(sip_msg_print_header(from, from_string, 255, SIP_FALSE, NULL) > 0) {
    printf("Subscription for event '%s' received  %s\n",
	   event -> structured_header -> content.event.event_type,
	   from_string);
  }
  
  contact_response = sip_msg_new_contact_header
    (request_uri, NULL, -1, -1, NULL, NULL);

  if(!contact_response) {
    printf("Unable to build the Contact: header\n");
    exit(1);
  }
  
  expires_response = sip_msg_new_expires_header(10, NULL, NULL);

  if(!expires_response) {
    printf("Unable to build the Expires: header\n");
    exit(1);
  }

  /* Then send a call accept */
  sip_status = sip_call_evt_accept(instance,
				   call,
				   dialog,
				   transaction,
				   subscription,
				   200,
				   "OK",
				   contact_response,
				   expires_response,
				   NULL,
				   body,
				   NULL);
  
  if (sip_status != SIP_NORMAL) {
    printf("The subscription can't be accepted, rc = %d.\n", sip_status);
  } else {
    printf("Subscription accepted...\n");
  }
  
  sip_msg_free_header(contact_response);
  sip_msg_free_header(expires_response);
  
  for(index = 0; index < 5; index ++) {

    char value[10];
    sip_trans_t new_transaction;

    sprintf(value, "value_%d", index);

    subscription_state = sip_msg_new_subscription_state_header
      (value, NULL, -1, -1, NULL, NULL);
    
    sleep(1);

    sip_status = sip_call_evt_notify(instance,
				     call,
				     dialog,
				     &new_transaction,
				     subscription,
				     subscription_state,
				     NULL,
				     body,
				     NULL);
    
    if (sip_status != SIP_NORMAL) {
      printf("Notification can't be sent, rc = %d.\n", sip_status);
    } else {
      printf("Notify sent with status '%s'.\n", value);
    }

    sip_msg_free_header(subscription_state);
  }

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      The SIP call notify reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance           : The SIP call instance
 *      call               : The call identifier
 *      dialog             : The dialog identifier
 *      transaction        : The transaction identifier
 *      subscription       : The subscription identifier
 *      subscription state : The state of the recieved event
 *      request            : Additional headers in the INVITE
 *      body               : The INVITE body
 *      user_info          : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_evt_notify
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_subs_t                    subscription,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_t            * event,
    sip_msg_header_t            * subscription_state,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
)
{
  sip_status_t       sip_status;
  sip_msg_header_t * contact_response;

  printf("NOTIFY received with status '%s' for event '%s'...",
	 subscription_state -> structured_header -> 
	 content.subscription_state.value,
	 event -> structured_header -> content.event.event_type);
  fflush(stdout);
  
  /* Then send a 200 OK */
  sip_status = sip_call_reply(instance,
			      call,
			      dialog,
			      transaction,
			      subscription,
			      200,
			      NULL,
			      NULL,
			      NULL,
			      SIP_FALSE,
			      NULL);
  
  if (sip_status != SIP_NORMAL) {
    printf("\nError sending the NOTIFY 200 OK, rc = %d.\n", sip_status);
  } else {
    printf(" 200 OK sent.\n");
  }
  
  if (unscuscribe_counter) {
    unscuscribe_counter--;

    if(!unscuscribe_counter) {
      sip_msg_header_t * event;
      sip_subs_t subscription_id;
      
      sip_status = sip_call_evt_unsubscribe
	(instance,
	 call,
	 dialog,
	 &transaction,
	 subscription,
	 SIP_CALL_RELEASE_GRACEFUL,
	 NULL,
	 NULL,
	 NULL);
      
      if(sip_status != SIP_NORMAL) {
	printf("Error %d returned from sip_call_evt_unsubscribe\n", sip_status);
	return(1);
      } else {
	printf("SUBSCRIBE(expires=0) = unsubscription sent...\n");
      } 
    }
  }

  return SIP_NORMAL;
}
  
/*
 * FACILITY:
 *
 *      The SIP call event reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call handling instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      event         : The call event
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_event
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_call_event_t            * event,
    sip_user_info_t             * user_info
  )
{
  switch(event->type)
  {
    case SIP_CALL_EVENT_LOCAL_SRV_ERROR:
      printf("Local server error\n");
      return SIP_CALL_TRANSFAIL;

    case SIP_CALL_EVENT_ENUM_RES:
      printf("Enum resolution error\n");
      return SIP_CALL_TRANSFAIL;

    case SIP_CALL_EVENT_TRANSACTION_DELETED:
      printf("Transaction deleted\n");
      if ( event->parameters.transaction_deleted.status != SIP_NORMAL )
        return SIP_CALL_TRANSFAIL;
      break;

    case SIP_CALL_EVENT_DIALOG_DELETED:
      printf("Dialog deleted\n");
      if ( event->parameters.dialog_deleted.status != SIP_NORMAL )
        return SIP_CALL_TRANSFAIL;
      break;

    default:
      printf("Event type:%d received\n",event->type);
      break;
  }
  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      call_uri
 *
 * ABSTRACT:
 *
 *      The caller code, able to establish a call to the
 *      specified request URI.
 *
 * FORMAL PARAMETERS:
 *
 *      sip_called_uri    : The remote to call
 *      sip_ch_inst       : The call-handling instance 
 *      port              : The local port number (to build the 
 *                          calling URI for the From: and Contact:
 *                          headers.
 *
 * RETURN VALUE:
 *
 *      None
 *
 */

int call_uri(char            * sip_called_uri,
             sip_call_inst_t   sip_ch_inst,
             int               port,
             sip_bool_t        do_body)
{

  printf("entering call_uri\n");

  sip_call_t   call;
  sip_dialog_t dialog;
  sip_trans_t  transaction;
  
  sip_msg_header_t * from;
  sip_msg_header_t * to;
  sip_msg_header_t * contact;

  sip_msg_uri_t * request_uri;
  sip_msg_uri_t * calling_uri;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -