我该如何拨打SIP电话

时间:2015-01-14 15:24:05

标签: java sip telephony jain-sip

我想在我的java应用程序中构建一个电话调用者。为此,我使用了JAIN-SIP库。在第一次INVITE之后,系统需要代理验证。第二个邀请是在" AuthenticationHelperImpl.class":https://gitorious.org/0xdroid/external_nist-sip/source/1e0f37693341071f316852c8e05a08deef2b7fc4:java/gov/nist/javax/sip/clientauthutils/AuthenticationHelperImpl.java#L311的帮助下构建的,包括代理身份验证标头和lloks,如:

INVITE sip:+11111111111@fpbx.de;maddr=fpbx.de SIP/2.0
Call-ID: 1c609509a43b721ab11c396c1e6ea9e7@192.168.17.107
CSeq: 2 INVITE
From: "77735hk6iu" <sip:77735hk6iu@fpbx.de>
To: "+111111111111111" <sip:+11111111111@fpbx.de>
Via: SIP/2.0/UDP 192.168.17.107:34567;rport;branch=z9hG4bK-383337-5bc4fd6b7a616843fce9eaa243bcb10e
Max-Forwards: 70
Contact: <sip:77735hk6iu@192.168.17.107:5060>
Content-Type: application/sdp
Proxy-Authorization: Digest       username="77735hk6iu",realm="fpbx.de",nonce="VLaIxVS2h5muPS30F2zLdXHjup6ELyen",uri="sip:+111111111111@fpbx.de:5060;maddr=fpbx.de",response="47ea578c6b01c99fd3ed2b41c60983df"
Content-Length: 61

v=0
o=- 130565705777141827 1 IN IP4 192.168.17.107
s=call

之后,我在开始时收到代码100消息(&#34;您的通话对我们非常重要&#34;),然后是408代码消息(&#34;请求超时&#34;)。

我采取了哪些措施来改善这种情况:

  1. 尝试了不同的电话号码格式:004930208488480, 04930208488480,049,0049,sdfhajfkhsk。对于所有这些数字我 在消息上成为相同的组合。

  2. 尝试在请求中使用端口uri

  3. 试图从请求uri中删除maddr。

  4. 尝试使用codek设置填充邮件正文。

  5. 从via标头设置和删除rport

  6. 如果你现在我做错了,请帮助我。 提前谢谢。

3 个答案:

答案 0 :(得分:2)

我想,也许您的Proxy-Authorization标头是错误的。也许你算错了。我想分享我的决心。

authUser是你的phoneNumber。 (例如:77735hk6iu) authPass是您的用户密码。 msg是你的邀请请求。(标题!)

AccountManagerImpl accountManagerImp = new AccountManagerImpl(authUser, AuthPass);
        AuthenticationHelperImpl authenticationHelperImpl = new AuthenticationHelperImpl(accountManagerImp);

        try {
           this.authentication = authenticationHelperImpl.handleChallenge(msg, (SIPClientTransaction)trans);

AuthenticationHelperImple.java类:

   public AuthorizationHeader handleChallenge(Response challenge, ClientTransaction challengedTransaction) throws SipException {


  SIPRequest challengedRequest = ((SIPRequest) challengedTransaction.getRequest());

  ListIterator authHeaders = null;

  if (challenge.getStatusCode() == Response.UNAUTHORIZED) {
     authHeaders = challenge.getHeaders(WWWAuthenticateHeader.NAME);
  }
  else {
     if (challenge.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED) {
        authHeaders = challenge.getHeaders(ProxyAuthenticateHeader.NAME);
     }
     else {
        throw new IllegalArgumentException("Unexpected status code ");
     }
  }

  if (authHeaders == null) {
     throw new IllegalArgumentException("Could not find WWWAuthenticate or ProxyAuthenticate headers");
  }

  WWWAuthenticateHeader authHeader = null;
  while (authHeaders.hasNext()) {
     authHeader = (WWWAuthenticateHeader) authHeaders.next();
     String realm = authHeader.getRealm();

     this.uri = challengedRequest.getRequestURI();

     this.requestMethod = challengedRequest.getMethod();
     this.requestBody = (challengedRequest.getContent() == null) ? "" : new String(challengedRequest.getRawContent());

     if (this.accountManager instanceof SecureAccountManager) {
        UserCredentialHash credHash = ((SecureAccountManager) this.accountManager).getCredentialHash(challengedTransaction,
                                                                                                     realm);
        if (credHash == null) {
           logger.logDebug("Could not find creds");
           throw new SipException("Cannot find user creds for the given user name and realm");
        }

        this.authorizationHeader = this.getAuthorization(requestMethod, uri.toString(), requestBody, authHeader, credHash);
     }
     else {
        UserCredentials userCreds = ((AccountManager) this.accountManager).getCredentials(challengedTransaction, realm);
        if (userCreds == null) {
           throw new SipException("Cannot find user creds for the given user name and realm");
        }
        // sipDomain = userCreds.getSipDomain();
        // we haven't yet authenticated this realm since we were
        // started.

       this.authorizationHeader = this.getAuthorization(requestMethod, uri.toString(), requestBody, authHeader, userCreds);
     }
  }

  return this.authorizationHeader;

}

getAuthorization函数:

 public AuthorizationHeader getAuthorization(String method,
                                           String uri,
                                           String requestBody,
                                           WWWAuthenticateHeader authHeader,
                                           UserCredentials userCredentials) throws SecurityException {
  String response = null;
  String qopList = authHeader.getQop();
  String qop = (qopList != null) ? "auth" : null;
  String nc_value = "00000001";
  String cnonce = "xyz";

  try {
     response = MessageDigestAlgorithm.calculateResponse(authHeader.getAlgorithm(),
                                                        userCredentials.getUserName(), authHeader.getRealm(),userCredentials.getPassword(), authHeader.getNonce(), nc_value, // JvB added
                                                 cnonce, // JvB added
                                                 method, uri, requestBody, qop,logger);
  }
  catch (NullPointerException exc) {
     throw new SecurityException("The received authenticate header was malformatted: " + exc.getMessage());
  }

  AuthorizationHeader authorization = null;
  try {
      if (authHeader instanceof ProxyAuthenticateHeader) {
         if (this.headerFactory != null) {
            authorization = headerFactory.createProxyAuthorizationHeader(authHeader.getScheme());
         }
         else {
            authorization = new ProxyAuthorization();
            authorization.setScheme(authHeader.getScheme()); 
         }
      } 
      else {
         if (this.headerFactory != null) {
            authorization = headerFactory.createAuthorizationHeader(authHeader.getScheme());
         }
         else {
            authorization = new Authorization();
            authorization.setScheme(authHeader.getScheme());
         }
      }

      authorization.setUsername(userCredentials.getUserName());
      authorization.setRealm(authHeader.getRealm());
      authorization.setNonce(authHeader.getNonce());
      authorization.setParameter("uri", uri);
      authorization.setResponse(response);
      if (authHeader.getAlgorithm() != null) {
          authorization.setAlgorithm(authHeader.getAlgorithm());
      }

      if (authHeader.getOpaque() != null) {
          authorization.setOpaque(authHeader.getOpaque());
      }

      // jvb added
      if (qop != null) {
          authorization.setQop(qop);
          authorization.setCNonce(cnonce);
          authorization.setNonceCount(Integer.parseInt(nc_value));
      }

      authorization.setResponse(response);

  } catch (ParseException ex) {
      throw new RuntimeException("Failed to create an authorization header!");
  }

  return authorization;

}

最后,您的this.authentication变量是ProxyAuthorizationHeader。您必须将this.authentication放在INVITE消息中。而且您将从事务或对话框将SipMessage发送到JAIN-SIP堆栈。

祝你好运!

答案 1 :(得分:0)

当从请求URI和proxy-auth中删除“maddr = fpbx.de”时,问题部分解决了。 URI

这是一个使用了boolean参数的handleCahllenge方法:

inviteTid = authenticationHelper.handleChallenge(response, tid, sipProvider, 15, **true**);

但我仍然不知道如何获得一个熟练的电话号码。

答案 2 :(得分:0)

100条消息是逐跳的,也就是说它只是意味着下一跳获得了您的请求。其他消息通常是端到端的(因此,如果你有一个180响铃,这通常意味着被调用的端点发送180)。当其中一个跃点发送INVITE但从未得到响应时,408通常会显示(并且当SIP堆栈在合理的时间范围内没有获得临时响应时,您的SIP堆栈可能会在内部产生 - 通常大约32秒默认的SIP定时器)。

我不知道您的网络设置,但该消息中有多个私有IP(192.168.x.x种类)。如果我不得不猜测,你的第一跳是将100发送回它接收到的IP /端口,但下一个响应是跟随Via标头(应该如此),以及你不尊重之后的跳跃rport参数,所以响应会丢失。或者,您的NAT配置不当,并且正在关闭它为INVITE创建的漏洞太快。

如果您的网络边缘有一个代理,此消息即将发出,则要么在消息上放置错误的Via头(可能使用内部IP而不是外部IP),或者发送INVITE错误的地方(导致它永远不会得到回应),408就是来自它。