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

📄 callapiproxy.c

📁 未改进的经典的SIP NETWORK API。 请尽快给下载权限
💻 C
📖 第 1 页 / 共 3 页
字号:
/* @@ WARNING: This example is automatically included in the user
   @@ documentation. (Note: The lines containing '@@' are removed
   @@ automatically). */

/*    
 *                        WARNING
 *
 * This sample application uses the sleep() system call
 * in callback routines, for the sake of simplicity. This
 * allows simple coding of the sample, to introcuce user
 * delays or call duration.
 *
 * BUT, a real-world application should never stop during 
 * a callback routine. During the processing of a callback, 
 * no other indications can be delivered, incoming SIP 
 * messages are stored in the SIP stack and cause congestion.
 *
 * If you need to stop or wait for I/O before answering the 
 * request, you must return from the callback and trigger
 * an asynchronous mechanisn (threads, timers, ...) to answer
 * the request at a later time.
 */


#include <string.h>
#include <stdio.h>
#include <sys/signal.h>
#include <pthread.h>
#include <arpa/inet.h>

#include "sip_api.h"

char * forward_to_uri = NULL;

sip_bool_t     sip_exit               = SIP_FALSE;
sip_bool_t     interactive            = SIP_FALSE;
sip_bool_t     no_bye                 = SIP_FALSE;
sip_bool_t     no_delay               = SIP_FALSE;
sip_bool_t     auto_exit              = SIP_TRUE;
int            cancel_delay           = 0;
int            uas_bye_delay          = 0;
int            uas_invite_delay       = 0;
char         * subscribe_event_name   = NULL;
int            unscuscribe_counter    = 0;
/*
 * FACILITY:
 *
 *      sip_call_on_initiate
 *
 * ABSTRACT:
 *
 *      The SIP call initiate reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      request_uri   : The request URI to forward the INVITE
 *      from          : The from header
 *      to            : The to header
 *      contact       : The contact 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_initiate
 (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_t            * from,
    sip_msg_header_t            * to,
    sip_msg_header_t            * contact,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  sip_status_t    sip_status;
  
  sip_msg_uri_t * uri[1];

  /* Modify the request URI */
  uri[0] = sip_msg_parse_uri(forward_to_uri, NULL);
  printf("invite trans_id %d\n",transaction);
  
  if (!uri[0]) {
    printf("Parse error in the forward URI.\n");
    return SIP_NORMAL;
  }

  char from_string[255];

  if(sip_msg_print_header(from, from_string, 255, SIP_FALSE, NULL) > 0) {
    printf("Call received, %s.\n",from_string);
  }
  
  /* We forward the INVITE */
  sip_status = sip_trans_forward_request(instance,
                                         transaction,
                                         SIP_MSG_INVITE,
                                         uri,
                                         uri,
                                         NULL,
                                         1,
                                         request,
                                         body,
                                         SIP_FALSE,
                                         SIP_BEHAVIOR_TRANS_STATEFUL_RR,
                                         NULL);
  if (sip_status != SIP_NORMAL)
    {
      printf("Error %d in forwarding an INVITE\n", sip_status);
    }
  else
    {
      printf("INVITE request forwarded to %s.\n",
             forward_to_uri);
    }
  
  sip_msg_free_uri(uri[0]);

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_connect
 *
 * ABSTRACT:
 *
 *      The SIP call connect reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call handling instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      request_uri   : The request URI to forward the INVITE
 *      request       : The request headers
 *      body          : The message body
 *      user_info     : The user info
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_call_on_connect
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  printf("The call is established\n");

  sip_status_t    sip_status;
  sip_msg_uri_t * uri[1];

  /* Modify the request URI */
  uri[0] = sip_msg_parse_uri(forward_to_uri, NULL);

  printf("ack trans_id %d\n",transaction);
  
  if (!uri[0]) {
    printf("Parse error in the forward URI.\n");
    return SIP_NORMAL;
  }

  /* We forward the ACK */
  sip_status = sip_trans_forward_request(instance,
                                         transaction,
                                         SIP_MSG_ACK,
                                         uri,
                                         uri,
                                         NULL,
                                         1,
                                         request,
                                         body,
                                         SIP_FALSE,
                                         SIP_BEHAVIOR_TRANS_STATEFUL_RR,
                                         NULL);
  if (sip_status != SIP_NORMAL)
    {
      printf("Error %d in forwarding an ACK\n", sip_status);
    }
  else
    {
      printf("ACK request forwarded to %s.\n",
             forward_to_uri);
    }

  sip_msg_free_uri(uri[0]);


  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_progress
 *
 * ABSTRACT:
 *
 *      The SIP call Ringing reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      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_progress
  (
    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
  )
{
  printf("Progressing: %s...\n", reason_phrase);
  
  sip_status_t    sip_status;
  /* We should forward the response */
  sip_status = sip_trans_forward_response(instance,
                                          transaction,
                                          status_code,
                                          reason_phrase,
                                          response,
                                          body,
                                          NULL);
  if (sip_status != SIP_NORMAL)
    {
      printf("Error %d in forwarding a response\n", sip_status);
    } 
  else 
    {
      printf("%d %s response forwarded.\n",
             status_code, reason_phrase);
    }
  
  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_accept
 *
 * ABSTRACT:
 *
 *      The SIP call accept reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      status_code   : The status code (200) 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_accept
  (
    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_trans_t new_trans;

  printf("The call has been accepted\n");

sip_status_t    sip_status;
  /* We should forward the response */
  sip_status = sip_trans_forward_response(instance,
                                          transaction,
                                          status_code,
                                          reason_phrase,
                                          response,
                                          body,
                                          NULL);
  if (sip_status != SIP_NORMAL)
    {
      printf("Error %d in forwarding a response 200\n", sip_status);
    } 
  else 
    {
      printf("%d %s response 200 forwarded.\n",
             status_code, reason_phrase);
    }
  
  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_release
 *
 * ABSTRACT:
 *
 *      The SIP call release reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance      : The SIP call handling instance
 *      call          : The call identifier
 *      dialog        : The dialog identifier
 *      transaction   : The transaction identifier
 *      request_uri   : The request URI in the message
 *      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_release
  (
    sip_call_inst_t               instance,
    sip_call_t                    call,
    sip_dialog_t                  dialog,
    sip_trans_t                   transaction,
    sip_msg_uri_t               * request_uri,
    sip_msg_header_vector_t     * request,
    sip_msg_body_t              * body,
    sip_user_info_t             * user_info
  )
{
  sip_status_t    sip_status;
  sip_msg_uri_t * uri[1];

  /* Modify the request URI */
  uri[0] = sip_msg_parse_uri(forward_to_uri, NULL);

  printf("bye trans_id %d\n",transaction);
  
  if (!uri[0]) {
    printf("Parse error in the forward URI.\n");
    return SIP_NORMAL;
  }

  /* We forward the BYE */
  sip_status = sip_trans_forward_request(instance,
                                         transaction,
                                         SIP_MSG_BYE,
                                         uri,
                                         uri,
                                         NULL,
                                         1,
                                         request,
                                         body,
                                         SIP_FALSE,
                                         SIP_BEHAVIOR_TRANS_STATEFUL_RR,
                                         NULL);
  if (sip_status != SIP_NORMAL)
    {
      printf("Error %d in forwarding a BYE\n", sip_status);
    }
  else
    {
      printf("BYE request forwarded to %s.\n",
             forward_to_uri);
    }

  sip_msg_free_uri(uri[0]);
  

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_call_on_cancel
 *

⌨️ 快捷键说明

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