summaryrefslogtreecommitdiff
path: root/pjsip
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2007-10-12 12:14:27 +0000
committerBenny Prijono <bennylp@teluu.com>2007-10-12 12:14:27 +0000
commitb0f62471ea72f5767d859f18e41d4326f57b85c5 (patch)
tree5be22dd454663b8e8daee9e322ed794ccf54e8c4 /pjsip
parent9ca491d9d373e4f9114b429bcd61b72b1ff97048 (diff)
Ticket #399: Initial implementation of tool to perform NAT type detection/classification
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1495 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjsip')
-rw-r--r--pjsip/include/pjsua-lib/pjsua.h21
-rw-r--r--pjsip/src/pjsua-lib/pjsua_core.c55
2 files changed, 64 insertions, 12 deletions
diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h
index 91856d2c..ea16d6b9 100644
--- a/pjsip/include/pjsua-lib/pjsua.h
+++ b/pjsip/include/pjsua-lib/pjsua.h
@@ -40,6 +40,9 @@
/* Include all PJSIP-SIMPLE headers */
#include <pjsip_simple.h>
+/* Include all PJNATH headers */
+#include <pjnath.h>
+
/* Include all PJLIB-UTIL headers. */
#include <pjlib-util.h>
@@ -1310,6 +1313,24 @@ PJ_DECL(pj_pool_factory*) pjsua_get_pool_factory(void);
*/
/**
+ * This is a utility function to detect NAT type in front of this
+ * endpoint. Once invoked successfully, this function will complete
+ * asynchronously and report the result in the callback.
+ *
+ * @param srv_port Optional STUN server and port, in "SERVER[:PORT]"
+ * format. If this option is NULL, the function will use
+ * the STUN server that has been set in the pjsua
+ * configuration.
+ * @param user_data User data to be returned back in the callback.
+ * @param cb Optional callback to report the detection result.
+ *
+ * @return PJ_SUCCESS if detection is started successfully.
+ */
+PJ_DECL(pj_status_t) pjsua_detect_nat_type(void *user_data,
+ pj_stun_nat_detect_cb *cb);
+
+
+/**
* This is a utility function to verify that valid SIP url is given. If the
* URL is valid, PJ_SUCCESS will be returned.
*
diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c
index 29cee191..321d3405 100644
--- a/pjsip/src/pjsua-lib/pjsua_core.c
+++ b/pjsip/src/pjsua-lib/pjsua_core.c
@@ -678,6 +678,7 @@ PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
/* Start resolving STUN server */
+
status = pjsua_resolve_stun_server(PJ_FALSE);
if (status != PJ_SUCCESS && status != PJ_EPENDING) {
pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
@@ -938,22 +939,27 @@ pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
port = 3478;
}
+ pjsua_var.stun_status =
+ pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, &str_host,
+ (pj_uint16_t)port);
- pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
-
- if (pjsua_var.stun_status == PJ_SUCCESS) {
- pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
- pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
- pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
+ if (pjsua_var.stun_status != PJ_SUCCESS) {
+ pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
- PJ_LOG(3,(THIS_FILE,
- "STUN server %.*s resolved, address is %s:%d",
- (int)pjsua_var.ua_cfg.stun_host.slen,
- pjsua_var.ua_cfg.stun_host.ptr,
- pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
- (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
+ if (pjsua_var.stun_status == PJ_SUCCESS) {
+ pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
+ pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
+ pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
+ }
}
+ PJ_LOG(3,(THIS_FILE,
+ "STUN server %.*s resolved, address is %s:%d",
+ (int)pjsua_var.ua_cfg.stun_host.slen,
+ pjsua_var.ua_cfg.stun_host.ptr,
+ pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
+ (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
+
}
/* Otherwise disable STUN. */
else {
@@ -1853,6 +1859,31 @@ void pjsua_init_tpselector(pjsua_transport_id tp_id,
/*
+ * Detect NAT type.
+ */
+PJ_DEF(pj_status_t) pjsua_detect_nat_type( void *user_data,
+ pj_stun_nat_detect_cb *cb)
+{
+ pj_status_t status;
+
+ /* Make sure STUN server resolution has completed */
+ status = pjsua_resolve_stun_server(PJ_TRUE);
+ if (status != PJ_SUCCESS) {
+ return status;
+ }
+
+ /* Make sure we have STUN */
+ if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
+ return PJ_EINVALIDOP;
+ }
+
+ return pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
+ &pjsua_var.stun_cfg,
+ user_data, cb);
+}
+
+
+/*
* Verify that valid SIP url is given.
*/
PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)