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

📄 sipmsgdump.c

📁 这是HPOC SIP栈的SIPUA
💻 C
字号:
/* @@ WARNING: This example is automatically included in the user
   @@ documentation. (Note: The lines containing '@@' are removed
   @@ automatically). */

#include <strings.h>

#include "sip_api.h"

/*
 * FACILITY:
 *
 *      sip_trp_msg
 *
 * ABSTRACT:
 *
 *      The SIP transport message reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance   : The SIP transport instance
 *      msg        : The SIP message structure as defined in sip_message_api.h
 *      trp_type   : The transport type
 *      multicast  : if the message was received on multicast or not
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_trp_msg ( sip_trp_inst_t      instance,
                           sip_msg_message_t * msg,
                           sip_trp_type_t      trp_type,
                           sip_bool_t          multicast)
{
  unsigned int    len = 0;
  char            buffer[65535];
  sip_msg_error_t error;

  int i = 0;

  /* Build a string from the message structure */
  len = sip_msg_print_message(msg,
                              buffer,
                              sizeof(buffer),
                              SIP_FALSE,
                              &error);
  //此函数出现在paser中,含义是编码一个MESSAGE为一个等价的串
  //by luoshizhang

  if (!len)
  {
    printf("Error while printing the message.\n");
  }
  else
  {
    printf("Message received is:\n");
    for (i = 0; i < len; i++)
    {
      printf("%c", buffer[i]);
    }
  }

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_trp_local_srv_err
 *
 * ABSTRACT:
 *
 *      The SIP transport local server error callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance   : The SIP transport instance
 *      addr       : The failed local address
 *      port       : The local port
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_trp_local_srv_err
  (
    sip_trp_inst_t  instance,
    struct sockaddr addr,
    sip_uint16_t    port
  )
{
  printf("Local server error received\n");
  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      sip_trp_msg_rcv_error
 *
 * ABSTRACT:
 *
 *      The SIP transport message reception callback
 *
 * FORMAL PARAMETERS:
 *
 *      instance   : The SIP transport instance
 *      msg        : The SIP message string
 *      len        : The SIP message len
 *      msg_error  : The message error if any as defined in sip_message_api.h
 *      error_type : the error type
 *
 * RETURN VALUE:
 *
 *      SIP_NORMAL : successful completion
 *
 */

sip_status_t sip_trp_msg_rcv_error
  (
    sip_trp_inst_t      instance,
    char              * msg,
    sip_int32_t         len,
    sip_msg_message_t * msg_message,
    sip_msg_error_t   * msg_error,
    sip_status_t        error_type
  )
{
  int i = 0;

  printf("Message reception error %d\n", error_type);
  for (i = 0; i < len; i++)
  {
    printf("%c", msg[i]);
  }

  return SIP_NORMAL;
}

/*
 * FACILITY:
 *
 *      usage
 *
 * ABSTRACT:
 *
 *      The help string displayed in case of error when
 *      parsing the comand line.
 *
 * FORMAL PARAMETERS:
 *
 *      The name of the command (argv[0]).
 *
 * RETURN VALUE:
 *
 *      None
 *
 */

void usage(char * command_name) 
{
  printf("\n%s [-p port] [-h]"
         "\n"
         "\n  Displays SIP messages received on the local UDP port."
         "\n"
         "\n  -p port       : The -p option specifies the UDP port number"
         "\n                  used to listen for messages. Default is 5060."
         "\n"
         "\n  -h            : Displays this help screen."
         "\n"
         "\n",
         command_name);
}

/*
 * FACILITY:
 *
 *      main
 *
 * ABSTRACT:
 *
 *      The main entry of the process
 *
 * FORMAL PARAMETERS:
 *
 *      argc, argv command line parameters
 *
 * RETURN VALUE:
 *
 *      None
 *
 */

int main(int argc, char *argv[])
{
  sip_status_t              sip_status = SIP_NORMAL;
  sip_trp_inst_t            sip_trp_inst;
  sip_trp_info_t            sip_trp_info[1];
  sip_uint32_t              sip_nb_trp = 1;
  sip_uint32_t              remaining_indic = 0;

  /* The transport callback vector */
  sip_trp_callback_vector_t sip_trp_callback;

  int argi;
  int port = 5060;

  for(argi = 1; argi < argc; argi++) {// 3

    if(!strcmp(argv[argi], "-p")) {// 2
      if((++argi) < argc) {// 1
        port = atol(argv[argi]);
      } else {// 1
        usage(argv[0]);
        exit(1);
      }// end 1
    }// 2
    
    if(!strcmp(argv[argi], "-h")) {
      usage(argv[0]);
      exit(0);
    }
    
  }// 3

  sip_status = sip_init_lib("sipmsgdump",
                            0,
                            NULL,
                            SIP_API_VERSION);/**/

  printf("SIP messages received on local UDP port %d:\n", port);

  /* Fill the transport callback vector */
  memset(&sip_trp_callback, 0, sizeof(sip_trp_callback_vector_t));
  sip_trp_callback.sip_trp_msg              = sip_trp_msg;
  sip_trp_callback.sip_trp_local_srv_err    = sip_trp_local_srv_err;
  sip_trp_callback.sip_trp_msg_rcv_error    = sip_trp_msg_rcv_error;

  /* Fill the transport info array */
  sip_trp_info[0].trp_name = (char *) strdup("my_address");
  sip_trp_info[0].address  = NULL;
  sip_trp_info[0].port     = port;
  sip_trp_info[0].trp_type = SIP_TRP_UDP;

  /* Create the SIP transport instance *//*手册第31页*/
  sip_status = sip_trp_create_instance(&sip_trp_inst,
                                       "sipmsgdump",
                                       &sip_trp_callback,
                                       sip_trp_info,
                                       sip_nb_trp,
                                       NULL);/*此函数为每个transport information创建一个服务器,等待入连接和消息*/
  if (sip_status != SIP_NORMAL)
  {
    printf("Error in sip_trp_create_instance function: %d\n", sip_status);
    exit(1);
  }

  /* Enable the SIP transport instance */
  sip_status = sip_trp_enable_instance(sip_trp_inst);/*开始了相关的本地服务器*/
  if (sip_status != SIP_NORMAL)
  {
    printf("Error in sip_trp_enable_instance function: %d\n", sip_status);
    exit(1);
  }

  /* Waiting for incoming messages */
  while (1)
  {
    sip_status = sip_trp_deliver_indic(sip_trp_inst,
                                       SIP_FALSE,
                                       SIP_DELIVER_ALL_MSG,
                                       &remaining_indic);
    if (sip_status != SIP_NORMAL)
    {
      printf("Error %d in delivering indication\n", sip_status);
      exit(1);
    }
  }
}

⌨️ 快捷键说明

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