📄 proxy.h
字号:
/* $Id: proxy.h 1160 2007-04-06 10:35:16Z bennylp $ */
/*
* Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <pjsip.h>
#include <pjlib-util.h>
#include <pjlib.h>
/* Options */
static struct global_struct
{
pj_caching_pool cp;
pjsip_endpoint *endpt;
int port;
pj_pool_t *pool;
pj_thread_t *thread;
pj_bool_t quit_flag;
pj_bool_t record_route;
unsigned name_cnt;
pjsip_host_port name[16];
} global;
static void app_perror(const char *msg, pj_status_t status)
{
char errmsg[PJ_ERR_MSG_SIZE];
pj_strerror(status, errmsg, sizeof(errmsg));
PJ_LOG(1,(THIS_FILE, "%s: %s", msg, errmsg));
}
static void usage(void)
{
puts("Options:\n"
"\n"
" -p, --port N Set local listener port to N\n"
" -R, --rr Perform record routing\n"
" -L, --log-level N Set log level to N (default: 4)\n"
" -h, --help Show this help screen\n"
);
}
static pj_status_t init_options(int argc, char *argv[])
{
struct pj_getopt_option long_opt[] = {
{ "port", 1, 0, 'p'},
{ "rr", 0, 0, 'R'},
{ "log-level", 1, 0, 'L'},
{ "help", 0, 0, 'h'},
{ NULL, 0, 0, 0}
};
int c;
int opt_ind;
pj_optind = 0;
while((c=pj_getopt_long(argc, argv, "p:L:Rh", long_opt, &opt_ind))!=-1) {
switch (c) {
case 'p':
global.port = atoi(pj_optarg);
printf("Port is set to %d\n", global.port);
break;
case 'R':
global.record_route = PJ_TRUE;
printf("Using record route mode\n");
break;
case 'L':
pj_log_set_level(atoi(pj_optarg));
break;
case 'h':
usage();
return -1;
default:
puts("Unknown option. Run with --help for help.");
return -1;
}
}
return PJ_SUCCESS;
}
/*****************************************************************************
* This is a very simple PJSIP module, whose sole purpose is to display
* incoming and outgoing messages to log. This module will have priority
* higher than transport layer, which means:
*
* - incoming messages will come to this module first before reaching
* transaction layer.
*
* - outgoing messages will come to this module last, after the message
* has been 'printed' to contiguous buffer by transport layer and
* appropriate transport instance has been decided for this message.
*
*/
/* Notification on incoming messages */
static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
{
PJ_LOG(5,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
"%.*s\n"
"--end msg--",
rdata->msg_info.len,
pjsip_rx_data_get_info(rdata),
rdata->tp_info.transport->type_name,
rdata->pkt_info.src_name,
rdata->pkt_info.src_port,
(int)rdata->msg_info.len,
rdata->msg_info.msg_buf));
/* Always return false, otherwise messages will not get processed! */
return PJ_FALSE;
}
/* Notification on outgoing messages */
static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
{
/* Important note:
* tp_info field is only valid after outgoing messages has passed
* transport layer. So don't try to access tp_info when the module
* has lower priority than transport layer.
*/
PJ_LOG(5,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
"%.*s\n"
"--end msg--",
(tdata->buf.cur - tdata->buf.start),
pjsip_tx_data_get_info(tdata),
tdata->tp_info.transport->type_name,
tdata->tp_info.dst_name,
tdata->tp_info.dst_port,
(int)(tdata->buf.cur - tdata->buf.start),
tdata->buf.start));
/* Always return success, otherwise message will not get sent! */
return PJ_SUCCESS;
}
/* The module instance. */
static pjsip_module mod_msg_logger =
{
NULL, NULL, /* prev, next. */
{ "mod-msg-logger", 14 }, /* Name. */
-1, /* Id */
PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
NULL, /* load() */
NULL, /* start() */
NULL, /* stop() */
NULL, /* unload() */
&logging_on_rx_msg, /* on_rx_request() */
&logging_on_rx_msg, /* on_rx_response() */
&logging_on_tx_msg, /* on_tx_request. */
&logging_on_tx_msg, /* on_tx_response() */
NULL, /* on_tsx_state() */
};
static pj_status_t init_stack(void)
{
pj_status_t status;
/* Must init PJLIB first: */
status = pj_init();
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Then init PJLIB-UTIL: */
status = pjlib_util_init();
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Must create a pool factory before we can allocate any memory. */
pj_caching_pool_init(&global.cp, &pj_pool_factory_default_policy, 0);
/* Create the endpoint: */
status = pjsip_endpt_create(&global.cp.factory, NULL, &global.endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
/* Init transaction layer for stateful proxy only */
#if STATEFUL
status = pjsip_tsx_layer_init_module(global.endpt);
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
#endif
/* Create listening transport */
{
pj_sockaddr_in addr;
addr.sin_family = PJ_AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = pj_htons((pj_uint16_t)global.port);
status = pjsip_udp_transport_start( global.endpt, &addr,
NULL, 1, NULL);
if (status != PJ_SUCCESS)
return status;
}
/* Create pool for the application */
global.pool = pj_pool_create(&global.cp.factory, "proxyapp",
4000, 4000, NULL);
/* Register the logger module */
pjsip_endpt_register_module(global.endpt, &mod_msg_logger);
return PJ_SUCCESS;
}
static pj_status_t init_proxy(void)
{
pj_in_addr addr;
unsigned i;
/* List all names matching local endpoint.
* Note that PJLIB version 0.6 and newer has a function to
* enumerate local IP interface (pj_enum_ip_interface()), so
* by using it would be possible to list all IP interfaces in
* this host.
*/
/* The first address is important since this would be the one
* to be added in Record-Route.
*/
if (pj_gethostip(&addr) == PJ_SUCCESS) {
pj_strdup2(global.pool, &global.name[global.name_cnt].host,
pj_inet_ntoa(addr));
global.name[global.name_cnt].port = global.port;
global.name_cnt++;
}
global.name[global.name_cnt].host = pj_str("127.0.0.1");
global.name[global.name_cnt].port = global.port;
global.name_cnt++;
global.name[global.name_cnt].host = *pj_gethostname();
global.name[global.name_cnt].port = global.port;
global.name_cnt++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -