(三)pjsip 监听

1、新建项目 -> 在 "pjproject-2.8" 根目录同级;

2、附加包含目录 "../../pjproject-2.8/pjsip/include/;../../pjproject-2.8/pjlib/include/;../../pjproject-2.8/pjlib-util/include/;../../pjproject-2.8/pjnath/include/"

3、链接器 -> 附加库目录 "../../pjproject-2.8/lib/;../../pjproject-2.8/pjlib/lib/;../../pjproject-2.8/pjlib-util/lib/;../../pjproject-2.8/pjsip/lib/;../../pjproject-2.8/pjnath/lib/"

4、链接器 -> 输入 -> 附加依赖项 " ws2_32.lib;pjlib-i386-Win32-vc14-Debug.lib;pjlib-util-i386-Win32-vc14-Debug.lib;pjnath-i386-Win32-vc14-Debug.lib;pjsip-core-i386-Win32-vc14-Debug.lib;"

5、ip转 pj_unit_32


pj_in_addr in_addr;	
pj_str_t host = pj_str("192.168.0.55");	
pj_inet_aton(&host, &in_addr);

// string ip 转 pj_uint32_t
addr.sin_addr.s_addr = in_addr.s_addr; //0;

6、完整代码


#include "stdafx.h"	 
#include 	 
#include 	 
#include 


/* If this macro is set, UDP transport will be initialized at port 5060 */
#define HAS_UDP_TRANSPORT

/* If this macro is set, TCP transport will be initialized at port 5060 */
#define HAS_TCP_TRANSPORT   (1 && PJ_HAS_TCP)

/* Log identification */
#define THIS_FILE       "sipstateless.c"	

/* Global SIP endpoint */
static pjsip_endpoint *sip_endpt;

/* What response code to be sent (default is 501/Not Implemented) */
static int code = PJSIP_SC_NOT_IMPLEMENTED;

/* Additional header list */
struct pjsip_hdr hdr_list;

/* usage() */
static void usage(void)
{
	puts("Usage:");
	puts("  sipstateless [code] [-H HDR] ..");
	puts("");
	puts("Options:");
	puts("  code     SIP status code to send (default: 501/Not Implemented");
	puts("  -H HDR   Specify additional headers to send with response");
	puts("           This option may be specified more than once.");
	puts("           Example:");
	puts("              -H 'Expires: 300' -H 'Contact: '");
}


/* Callback to handle incoming requests. */
static pj_bool_t on_rx_request(pjsip_rx_data *rdata)
{
	/* Respond (statelessly) all incoming requests (except ACK!)			        
		* with 501 (Not Implemented)
	*/

	if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD)
	{
		pjsip_endpt_respond_stateless(sip_endpt, rdata, code, NULL, &hdr_list, NULL);

	}
	return PJ_TRUE;
}



/*
	   * main()
	   *
	   */
int main(int argc, char *argv[])
{
	pj_caching_pool cp;
	pj_pool_t *pool = NULL;
	pjsip_module mod_app =
	{
		NULL,
		NULL,                 /* prev, next.              */			
		{ "mod-app", 7 },           /* Name.                    */			
		-1,                                 /* Id               */			
		PJSIP_MOD_PRIORITY_APPLICATION, /* Priority             */
		NULL,                       /* load()                   */
		NULL,                       /* start()                  */
		NULL,                       /* stop()                   */
		NULL,                       /* unload()                 */
		&on_rx_request,             /* on_rx_request()          */
		NULL,                       /* on_rx_response()         */
		NULL,                       /* on_tx_request.           */
		NULL,                       /* on_tx_response()         */
		NULL,                       /* on_tsx_state()           */
	};
	int c;
	pj_status_t status;

	/* Must init PJLIB first: */
	status = pj_init();
	PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);


	/* Then init PJLIB-UTIL: */
	status = pjlib_util_init();
	PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);

	/* Must create a pool factory before we can allocate any memory. */
	pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);

	/* Create global endpoint: */
	{
		/* Endpoint MUST be assigned a globally unique name.
					   * Ideally we should put hostname or public IP address, but
					   * we'll just use an arbitrary name here.
					   */

					   /* Create the endpoint: */
		status = pjsip_endpt_create(&cp.factory, "sipstateless", &sip_endpt);
		PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
	}

	/* Parse arguments */
	pj_optind = 0;
	pj_list_init(&hdr_list);
	while ((c = pj_getopt(argc, argv, "H:")) != -1) {
		switch (c) {
		case 'H':
			if (pool == NULL) {
				pool = pj_pool_create(&cp.factory, "sipstateless", 1000,
					1000, NULL);

			}

			if (pool) {
				char *name;
				name = strtok(pj_optarg, ":");
				if (name == NULL) {
					puts("Error: invalid header format");
					return 1;

				}
				else {
					char *val = strtok(NULL, "\r\n");
					pjsip_generic_string_hdr *h;
					pj_str_t hname, hvalue;

					hname = pj_str(name);
					hvalue = pj_str(val);

					h = pjsip_generic_string_hdr_create(pool, &hname, &hvalue);

					pj_list_push_back(&hdr_list, h);

					PJ_LOG(4, (THIS_FILE, "Header %s: %s added", name, val));

				}

			}
			break;
		default:
			puts("Error: invalid argument");
			usage();
			return 1;

		}

	}

	if (pj_optind != argc) {
		code = atoi(argv[pj_optind]);
		if (code < 200 || code > 699) {
			puts("Error: invalid status code");
			usage();
			return 1;

		}

	}

	PJ_LOG(4, (THIS_FILE, "Returning %d to incoming requests", code));

	pj_in_addr in_addr;
	pj_str_t host = pj_str("192.168.0.55");
	pj_inet_aton(&host, &in_addr);

	/*
			   * Add UDP transport, with hard-coded port
			   */
#ifdef HAS_UDP_TRANSPORT
	{
		

		pj_sockaddr_in addr;
		addr.sin_family = pj_AF_INET();
		addr.sin_addr.s_addr = in_addr.s_addr; //0;		
		addr.sin_port = pj_htons(5060);

		status = pjsip_udp_transport_start(sip_endpt, &addr, NULL, 1, NULL);
		if (status != PJ_SUCCESS) {
			PJ_LOG(3, (THIS_FILE,
				"Error starting UDP transport (port in use?)"));
			return 1;

		}
	}
#endif

#if HAS_TCP_TRANSPORT
	/*
			   * Add UDP transport, with hard-coded port
			   */
	{
		pj_sockaddr_in addr;

		addr.sin_family = pj_AF_INET();
		addr.sin_addr.s_addr = in_addr.s_addr; //0;	
		addr.sin_port = pj_htons(5060);

		status = pjsip_tcp_transport_start(sip_endpt, &addr, 1, NULL);
		if (status != PJ_SUCCESS) {
			PJ_LOG(3, (THIS_FILE,
				"Error starting TCP transport (port in use?)"));
			return 1;

		}
	}
#endif

	//delete in_addr;

	/*
			   * Register our module to receive incoming requests.
			   */
	status = pjsip_endpt_register_module(sip_endpt, &mod_app);
	PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);


	/* Done. Loop forever to handle incoming events. */
	PJ_LOG(3, (THIS_FILE, "Press Ctrl-C to quit.."));

	for (;;) {
		pjsip_endpt_handle_events(sip_endpt, NULL);

	}
}

 

你可能感兴趣的:(pjsip)