如何连接到带有 DNS 域的 SIP 服务器

How to connect to SIP server with DNS domain

我正在 iOS 中开发 VOIP 应用程序,为此我正在使用 PjSip 库。我使用正常的 IP 或域地址成功连接、拨打和接听电话,这意味着只有一台服务器。当我尝试使用两台服务器连接 DNS(域名系统)时,我遇到了一些错误。任何人都可以帮助我解决这个问题并提供一些示例代码如何连接到 DNS。 这是我尝试过的代码

 char dns = "sample.com";
 while (anObject = [enumerator nextObject])
 {
   NSMutableString *mutableStr = [anObject mutableCopy];
   CFStringTrimWhitespace((CFMutableStringRef)mutableStr);
   srv = [mutableStr UTF8String];
   if (srv && strlen(srv))
   {

      if (app_config->cfg.nameserver_count==PJ_ARRAY_SIZE(app_config->cfg.nameserver))
      {
         PJ_LOG(1,(THIS_FILE, "Error: too many DNS servers"));
         [mutableStr release];
         break;
      }
      for (i=0; i<app_config->cfg.nameserver_count; ++i) {
          pj_ansi_sprintf(line, "--nameserver %.*s\n",(int)app_config->cfg.nameserver[i].slen,app_config->cfg.nameserver[i].ptr);
      }
      pj_strdup2_with_null(app_config->pool, &(app_config->cfg.nameserver[app_config->cfg.nameserver_count++]), srv);
  }
  [mutableStr release];
}

/* Initialize pjsua */
status = pjsua_init(&app_config->cfg, &app_config->log_cfg, &app_config->media_cfg);
uname  = "2000";
authname  = [[[NSUserDefaults standardUserDefaults] stringForKey:@"authname"] UTF8String];
passwd = "abcdef";
sipServer = "sample.com";
pjsua_acc_config_default(&acc_cfg);
acc_cfg.use_rfc5626 = PJ_FALSE;
// ID
acc_cfg.id.ptr = (char*) pj_pool_alloc(/*app_config.*/pool, PJSIP_MAX_URL_SIZE);
if (contactname && strlen(contactname))
  acc_cfg.id.slen = pj_ansi_snprintf(acc_cfg.id.ptr, PJSIP_MAX_URL_SIZE, 
                                   "\"%s\"<sip:%s@%s>", contactname, uname, sipServer);
else
  acc_cfg.id.slen = pj_ansi_snprintf(acc_cfg.id.ptr, PJSIP_MAX_URL_SIZE, 
                                   "sip:%s@%s;transport=tcp;hide", uname, sipServer);
if ((status = pjsua_verify_sip_url(acc_cfg.id.ptr)) != 0) 
{
   PJ_LOG(1,(THIS_FILE, "Error: invalid SIP URL '%s' in local id argument", 
  acc_cfg.id));
  return status;
}
acc_cfg.reg_uri.ptr = (char*) pj_pool_alloc(/*app_config.*/pool,
PJSIP_MAX_URL_SIZE);
acc_cfg.reg_uri.slen = pj_ansi_snprintf(acc_cfg.reg_uri.ptr, 
PJSIP_MAX_URL_SIZE, "sip:%s;transport=tcp;hide", sipServer);
if ((status = pjsua_verify_sip_url(acc_cfg.reg_uri.ptr)) != 0) 
{
   PJ_LOG(1,(THIS_FILE,  "Error: invalid SIP URL '%s' in registrar argument",
  acc_cfg.reg_uri));
   return status;
}

acc_cfg.cred_count = 1;
acc_cfg.cred_info[0].scheme = pj_str("Digest");
acc_cfg.cred_info[0].realm = pj_str("*");//pj_str(realm);
if (authname && strlen(authname))
  acc_cfg.cred_info[0].username = pj_str((char *)authname);
else
  acc_cfg.cred_info[0].username = pj_str((char *)uname);
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableMJ"])
   acc_cfg.cred_info[0].data_type = PJSIP_CRED_DATA_DIGEST;
else
  acc_cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
acc_cfg.cred_info[0].data = pj_str((char *)passwd);
acc_cfg.publish_enabled = PJ_TRUE;
#if defined(MWI) && MWI==1
  acc_cfg.mwi_enabled = PJ_TRUE;
#endif
acc_cfg.allow_contact_rewrite = [[NSUserDefaults standardUserDefaults] boolForKey:@"enableNat"];
acc_cfg.reg_retry_interval = 300;
acc_cfg.reg_timeout = 3600;
if (acc_cfg.reg_timeout < 1 || acc_cfg.reg_timeout > 3600)
{
   PJ_LOG(1,(THIS_FILE, "Error: invalid value for timeout (expecting 1-3600)"));
   return PJ_EINVAL;
}
acc_cfg.ka_interval = 40;
status = pjsua_acc_add(&acc_cfg, PJ_TRUE, acc_id);

这是我在控制台中得到的响应。

2016-07-28 10:12:29.899 MYApp[266:39854] granted
2016-07-28 10:12:29.904 MYApp[266:39854] -[Reachability isReachableViaWiFi] (632)
Reachability Flags: -R -------
10:12:31.842 os_core_unix.c !pjlib 2.3 for POSIX initialized
10:12:31.848 sip_endpoint.c  .Creating endpoint instance...
10:12:31.851          pjlib  .select() I/O Queue created (0x17a75f34)
10:12:31.852 sip_endpoint.c  .Module "mod-msg-print" registered
10:12:31.852 sip_transport.  .Transport manager created.
10:12:31.852   pjsua_core.c  .PJSUA state changed: NULL --> CREATED
2016-07-28 10:12:31.856 MYApp[266:39854] RESOLVED 0:<192.168.0.1>
2016-07-28 10:12:31.856 MYApp[266:39854] RESOLVED 1:<192.168.0.2>
10:12:37.822 sip_endpoint.c  .Module "mod-pjsua-log" registered
10:12:37.826   pjsua_core.c  .Nameserver sample.com added
10:12:37.826 sip_endpoint.c  .Module "mod-tsx-layer" registered
10:12:37.826 sip_endpoint.c  .Module "mod-stateful-util" registered
10:12:37.826 sip_endpoint.c  .Module "mod-ua" registered
10:12:37.826 sip_endpoint.c  .Module "mod-100rel" registered
10:12:37.826 sip_endpoint.c  .Module "mod-pjsua" registered
10:12:37.827 sip_endpoint.c  .Module "mod-invite" registered
10:12:37.871 coreaudio_dev.  .. dev_id 0: iPhone IO device  (in=1, out=1) 8000Hz
10:12:37.871 coreaudio_dev.  ..core audio initialized
10:12:37.871          pjlib  ..select() I/O Queue created (0x17a79214)
10:12:37.901 sip_endpoint.c  .Module "mod-evsub" registered
10:12:37.901 sip_endpoint.c  .Module "mod-presence" registered
10:12:37.901 sip_endpoint.c  .Module "mod-mwi" registered
10:12:37.901 sip_endpoint.c  .Module "mod-refer" registered
10:12:37.901 sip_endpoint.c  .Module "mod-pjsua-pres" registered
10:12:37.901 sip_endpoint.c  .Module "mod-pjsua-im" registered
10:12:37.901 sip_endpoint.c  .Module "mod-pjsua-options" registered
10:12:37.901   pjsua_core.c  .1 SIP worker threads created
10:12:37.901   pjsua_core.c  .pjsua version 2.3 for  initialized
10:12:37.901   pjsua_core.c  .PJSUA state changed: CREATED --> INIT
10:12:37.903   pjsua_core.c  SIP UDP socket reachable at 192.168.1.55:5060
10:12:37.903  udp0x17a80200  SIP UDP transport started, published address is 192.168.1.55:5060
10:12:37.904    tcplis:5060  SIP TCP listener ready for incoming connections at 192.168.1.55:5060
10:12:37.904   tcplis:49851  SIP TCP listener ready for incoming connections at fe80::186d:af8f:4693:2b3d:49851
10:12:37.905   pjsua_core.c  SIP UDP socket reachable at fe80::186d:af8f:4693:2b3d:50425
10:12:37.905 udpv60x1824be0  SIP UDP IPv6 transport started, published address is [fe80::186d:af8f:4693:2b3d]:50425
10:12:37.905   pjsua_core.c  PJSUA state changed: INIT --> STARTING
10:12:37.905 sip_endpoint.c  .Module "mod-unsolicited-mwi" registered
10:12:37.905   pjsua_core.c  .PJSUA state changed: STARTING --> RUNNING
2016-07-28 10:12:43.307 MYApp[266:39854] There IS internet connection
10:12:43.308    pjsua_acc.c  Adding account: id=sip:2000@sample.com;transport=tcp;hide
10:12:43.308    pjsua_acc.c  .Account sip:2000@sample.com;transport=tcp;hide added with id 0
10:12:43.308    pjsua_acc.c  .Acc 0: setting registration..
10:12:43.308    pjsua_acc.c  ..Warning: cannot use source TCP/TLS socket address for Contact when nameserver is configured.
10:12:43.309    pjsua_acc.c  ..Warning: cannot use source TCP/TLS socket address for Contact when nameserver is configured.
10:12:43.309     resolver.c  ...Transmitting 38 bytes to NS 0 (192.168.0.1:53): DNS SRV query for _sip._tcp.sample.com: Success
10:12:43.309    pjsua_acc.c  ..Acc 0: Registration sent
10:12:45.311     resolver.c !Re-transmitting 38 bytes to NS 0 (192.168.0.1:53): DNS SRV query for _sip._tcp.sample.com: Success
10:12:47.311     resolver.c  Re-transmitting 38 bytes to NS 0 (192.168.0.1:53): DNS SRV query for _sip._tcp.sample.com: Success
10:12:49.311     resolver.c  Re-transmitting 38 bytes to NS 0 (192.168.0.1:53): DNS SRV query for _sip._tcp.sample.com: Success
10:12:51.313     resolver.c  Re-transmitting 38 bytes to NS 0 (192.168.0.1:53): DNS SRV query for _sip._tcp.sample.com: Success
10:12:53.313 _sip._tcp.samp  DNS SRV resolution failed for _sip._tcp.sample.com: Operation timed out (PJ_ETIMEDOUT)
10:12:53.313 _sip._tcp.samp  DNS SRV resolution failed for _sip._tcp.sample.com, trying resolving A record for vabsys.com
10:12:53.314     resolver.c  Transmitting 28 bytes to NS 0 (192.168.0.1:53): DNS A query for vabsys.com: Success
10:12:55.314     resolver.c  Re-transmitting 28 bytes to NS 0 (192.168.0.1:53): DNS A query for vabsys.com: Success
10:12:57.315     resolver.c  Error transmitting request: No working DNS nameserver (PJLIB_UTIL_EDNSNOWORKINGNS)
10:12:57.315 _sip._tcp.samp  DNS A record resolution failed: Operation timed out (PJ_ETIMEDOUT)
10:12:57.315  sip_resolve.c  DNS A record resolution failed: Operation timed out (PJ_ETIMEDOUT)
10:12:57.315  tsx0x17a87664  Failed to send Request msg REGISTER/cseq=27562 (tdta0x18252000)! err=70009 (Operation timed out (PJ_ETIMEDOUT))
10:12:57.315    pjsua_acc.c  ..SIP registration failed, status=503 (Operation timed out (PJ_ETIMEDOUT))
10:12:57.315    pjsua_acc.c  ..Scheduling re-registration retry for acc 0 in 0 seconds..
2016-07-28 10:12:57.316 MYApp[266:39945] Status changed acc 0 Service Unavailable (503)
10:12:57.318    pjsua_acc.c !Deleting account 0..
10:12:57.319    pjsua_acc.c  .Account id 0 deleted
10:12:57.319   pjsua_core.c  Shutting down, flags=0...
10:12:57.319   pjsua_core.c  PJSUA state changed: RUNNING --> CLOSING
10:12:57.325   pjsua_call.c  .Hangup all calls..
10:12:57.325  pjsua_media.c  .Call 0: deinitializing media..
10:12:57.325  pjsua_media.c  .Call 1: deinitializing media..
10:12:57.325  pjsua_media.c  .Call 2: deinitializing media..
10:12:57.325  pjsua_media.c  .Call 3: deinitializing media..
10:12:57.325   pjsua_pres.c  .Shutting down presence..
10:12:58.326   pjsua_core.c  .Destroying...
10:12:58.326  pjsua_media.c  .Shutting down media..
10:12:58.440 sip_transactio  .Stopping transaction layer module
10:12:58.440 sip_transactio  .Stopped transaction layer module
10:12:58.440 sip_endpoint.c  .Module "mod-unsolicited-mwi" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-pjsua-options" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-pjsua-im" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-pjsua-pres" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-pjsua" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-stateful-util" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-refer" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-mwi" unregistered
10:12:58.440 sip_endpoint.c  .Module "mod-presence" unregistered
10:12:58.441 sip_endpoint.c  .Module "mod-evsub" unregistered
10:12:58.441 sip_endpoint.c  .Module "mod-invite" unregistered
10:12:58.441 sip_endpoint.c  .Module "mod-100rel" unregistered
10:12:58.441 sip_endpoint.c  .Module "mod-ua" unregistered
10:12:58.441 sip_transactio  .Transaction layer module destroyed
10:12:58.441 sip_endpoint.c  .Module "mod-tsx-layer" unregistered
10:12:58.441 sip_endpoint.c  .Module "mod-msg-print" unregistered
10:12:58.441 sip_endpoint.c  .Module "mod-pjsua-log" unregistered
10:12:58.444    tcplis:5060  .SIP TCP listener destroyed

10:12:58.445   tcplis:49851  .SIP TCP listener destroyed


10:12:58.445 sip_endpoint.c  .Endpoint 0x17a75864 destroyed


10:12:58.445   pjsua_core.c  .PJSUA state changed: CLOSING --> NULL



10:12:58.445   pjsua_core.c  .PJSUA destroyed...

请帮助我如何连接DNS。提前致谢。

如果我查看您的日志,pjsip 无法解析 dns,因为它无法从位于 52.86.229.130 的名称服务器获取已解析的 ip 地址。也许您可以尝试在配置中添加另一个名称服务器?

1) 虽然看起来不错,但请确保您的编译时间标志中有#define PJSIP_HAS_RESOLVER 1。 2) 正如 Bob 指定的那样,它无法从位于 52.86.229.130 的名称服务器获得已解析的 IP 地址 3) 您的名称服务器 (52.86.229.130) 正在响应,但来自服务器的响应被路由器或在网络之间阻止。

所以答案可能因您的网络连接而异。

已修复,上面的代码可以正常工作并连接到名称服务器。问题仅出在服务器端。现在双方合作良好。感谢你们宝贵的时间。