📄 sipproxyserver.c
字号:
sip_msg_uri_t * branch_next_hop,
sip_msg_uri_t * branch_request_uri,
sip_uint32_t status_code,
char * reason_phrase,
sip_msg_header_t * from,
sip_msg_header_t * to,
sip_msg_header_t * call_id,
sip_msg_header_t * cseq,
sip_msg_header_vector_t * response,
sip_msg_body_t * body,
sip_user_info_t * user_info,
sip_uint8_t transport_id
)
{
sip_status_t sip_status;
int index;
sip_msg_error_t VS_error;
/* We should forward the response */
sip_status = sip_trans_forward_response(instance,
trans,
status_code,
reason_phrase,
response,
body,
NULL);/*此例程与sip trans forward request相似,功能为前转一个响应*/
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);
}
if (status_code >= 200)
{
sip_exit = SIP_TRUE;
}
return SIP_NORMAL;
}
/*
* FACILITY:
*
* sip_trans_on_event
*
* ABSTRACT:
*
* The SIP transaction event reception callback
*
* FORMAL PARAMETERS:
*
* instance : The SIP transaction instance
* trans : The transaction ID
* event : The transaction event
* user_info : the user info
*
* RETURN VALUE:
*
* SIP_NORMAL : successful completion
*
*/
sip_status_t sip_trans_on_event
(
sip_trans_inst_t instance,
sip_trans_t trans,
sip_trans_event_t * event,
sip_user_info_t * user_info
)/*这个函数仅仅只解析了event的type*/
{
sip_status_t sip_status;
int index;
sip_msg_error_t VS_error;
switch(event->type)
{
case SIP_TRANS_EVENT_LOCAL_SRV_ERROR:
printf("Local server error\n");
break;
case SIP_TRANS_EVENT_TRP_ERROR:
printf("Transport error\n");
break;
case SIP_TRANS_EVENT_PARSER_ERROR:
printf("Parser error\n");
break;
case SIP_TRANS_EVENT_TRANSACTION_DELETED:
printf("Transaction deleted\n");
break;
case SIP_TRANS_EVENT_TIME_OUT:
printf("Transaction deleted\n");
break;
default:
printf("Event type:%d received\n",event->type);
break;
}
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, char *forward_uri)
{
printf("\n%s [-p port] [-h] [-f request_URI]"
"\n"
"\n Starts a SIP proxy server on the specified port, which forwards"
"\n any incoming requests to the URI given in the -f option."
"\n"
"\n -p port : The -p option specifies the UDP port number for"
"\n the proxy server. The default port is 6060."
"\n"
"\n -f request_URI: The -f option specifies the URI use to forward"
"\n the received SIP requests. The default value"
"\n is '%s'."
"\n"
"\n -h : Displays this help screen."
"\n"
"\n",
command_name,
forward_uri);
}
/*
* 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_trans_inst_t sip_trans_inst;
sip_trp_info_t sip_trp_info[1];
sip_uint32_t sip_nb_trp = 1;
struct hostent * sip_local_host = NULL;
char * sip_local_ip = NULL;
char sip_pid[16] = { 0 };
char sip_appli_name[80] = { 0 };
sip_trans_callback_vector_t sip_trans_callback;
int argi;
int port = 6060;
if ((gethostname(sip_appli_name,64) != 0)
|| (!(sip_local_host = gethostbyname(sip_appli_name))))
{
printf("cannot get local host informations\n");
exit(1);
}
sip_local_ip = (char *)
inet_ntoa (*(unsigned long *)(sip_local_host->h_addr_list[0]));
forward_to_uri = (char *) malloc (80 * sizeof(char));
memset(forward_to_uri,0,(80 * sizeof(char)));
strcat(forward_to_uri,"sip:user@");
strcat(forward_to_uri,sip_local_ip);
strcat(forward_to_uri,":5060");
for(argi = 1; argi < argc; argi++) {
if(!strcmp(argv[argi], "-p")) {
if((++argi) < argc) {
port = atol(argv[argi]);
} else {
usage(argv[0],forward_to_uri);
free(forward_to_uri);
exit(1);
}
}
if(!strcmp(argv[argi], "-f")) {
if((++argi) < argc) {
forward_to_uri = argv[argi];
} else {
usage(argv[0],forward_to_uri);
free(forward_to_uri);
exit(1);
}
}
if(!strcmp(argv[argi], "-h")) {
usage(argv[0],forward_to_uri);
free(forward_to_uri);
exit(0);
}
}
strcat(sip_appli_name,"_sipproxyserver_");
sprintf(sip_pid,"%d",getpid());
strcat(sip_appli_name,sip_pid);
sip_status = sip_init_lib(sip_appli_name,
0,
NULL,
SIP_API_VERSION);
printf("SIP Proxy server (%s) IP:%s started on UDP port %d\n",
sip_appli_name,
sip_local_ip,
port);
/* Fill the SIP transaction callback */
memset(&sip_trans_callback, 0, sizeof(sip_trans_callback_vector_t));
sip_trans_callback.sip_trans_on_invite = sip_trans_on_invite;
sip_trans_callback.sip_trans_on_bye = sip_trans_on_bye;
sip_trans_callback.sip_trans_on_ack = sip_trans_on_ack;
sip_trans_callback.sip_trans_on_response = sip_trans_on_response;
sip_trans_callback.sip_trans_on_event = sip_trans_on_event;
/* Fill the transport info array */
sip_trp_info[0].trp_name = (char *) strdup(sip_appli_name);
sip_trp_info[0].address = sip_local_ip;
sip_trp_info[0].port = port;
sip_trp_info[0].trp_type = SIP_TRP_UDP;
/* Create the SIP transaction instance *//*用给定的transport信息创建新的SIPtransaction实例*/
sip_status = sip_trans_create_instance(&sip_trans_inst,
sip_appli_name,
&sip_trans_callback,
sip_trp_info,
sip_nb_trp,
NULL);
if (sip_status != SIP_NORMAL)
{
printf("Error in sip_trans_create_instance function: %d\n", sip_status);
free(forward_to_uri);
exit(1);
}
/* Enable the SIP transaction instance *//*它开始处理底层的transport实例传递的indication,使用户能调用此实例的transaction例程*/
sip_status = sip_trans_enable_instance(sip_trans_inst);
if (sip_status != SIP_NORMAL)
{
sip_trans_delete_instance(sip_trans_inst);
printf("Error in sip_trans_enable_instance function: %d\n", sip_status);
free(forward_to_uri);
exit(1);
}
/* Waiting for incoming messages */
while (1)
{
sip_status = sip_trans_deliver_indic(sip_trans_inst,
SIP_FALSE, /* non blocking */
SIP_DELIVER_ALL_MSG,
NULL);/*此例程传递与SIPtransaction实例相关的indication。通过调用相关的transaction回调例程来传递indication.*/
if (sip_status != SIP_NORMAL)
{
sip_trans_delete_instance(sip_trans_inst);
printf("Error %d in delivering indication\n", sip_status);
free(forward_to_uri);
exit(1);
}
if (sip_exit)
{
sleep(3);
sip_trans_delete_instance(sip_trans_inst);
printf("Exiting\n");
free(forward_to_uri);
exit(1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -