Я разрабатываю клиентское приложение SIP в Eclipse. Я пытаюсь зарегистрироваться, но каждый раз получаю сообщение 401 Unauthorized. Я также включил заголовок авторизации после получения первого сообщения 401.
Мои сообщения следующие:
REGISTER sip:xx.xx.xx.xx SIP/2.0
Call-ID: ef0ef86022c9a70909dfe9d7315c2929@192.168.0.6
CSeq: 1 REGISTER
From: <sip:Santanu@xx.xx.xx.xx>;tag=textclientv1.0
To: <sip:Santanu@xx.xx.xx.xx>
Via: SIP/2.0/UDP 192.168.0.6:5060
Max-Forwards: 70
Expires: 180
Content-Length: 0
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.0.6:5060;branch=z9hG4bK-363031-07183c4bdcc67d2a427747d132335a51;received=115.187.33.88;rport=5060
From: <sip:Santanu@xx.xx.xx.xx>;tag=textclientv1.0
To: <sip:Santanu@xx.xx.xx.xx>;tag=as7fe2c61e
Call-ID: ef0ef86022c9a70909dfe9d7315c2929@192.168.0.6
CSeq: 1 REGISTER
Server: Asterisk PBX 16.7.0
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY,INFO,PUBLISH,MESSAGE
Supported: replaces,timer
WWW-Authenticate: Digest algorithm=MD5,realm="asterisk",nonce="75a0c702"
Content-Length: 0
REGISTER sip:xx.xx.xx.xx SIP/2.0
Call-ID: da592663fd88f0496380a3888844df95@192.168.0.6
CSeq: 1 REGISTER
From: <sip:Santanu@xx.xx.xx.xx>;tag=textclientv1.0
To: <sip:Santanu@xx.xx.xx.xx>
Via: SIP/2.0/UDP 192.168.0.6:5060
Max-Forwards: 70
Expires: 180
Authorization: Digest realm="asterisk",nonce="75a0c702",username="Santanu",uri="sip:xx.xx.xx.xx",algorithm=MD5,response="34c55995ca5bd9e5a1df9d21d1c8bd9e",cnonce="0a4f113b",nc=00000001,qop=auth
Content-Length: 0
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.0.6:5060;branch=z9hG4bK-363031-d081e9dc17d5bdaf73ecd2da30763b51;received=115.187.33.88;rport=5060
From: <sip:Santanu@xx.xx.xx.xx>;tag=textclientv1.0
To: <sip:Santanu@xx.xx.xx.xx>;tag=as2d43d078
Call-ID: da592663fd88f0496380a3888844df95@192.168.0.6
CSeq: 1 REGISTER
Server: Asterisk PBX 16.7.0
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY,INFO,PUBLISH,MESSAGE
Supported: replaces,timer
WWW-Authenticate: Digest algorithm=MD5,realm="asterisk",nonce="788c190c"
Content-Length: 0
REGISTER sip:xx.xx.xx.xx SIP/2.0
Call-ID: 1e89a6010565507d291a10a7412fdf7d@192.168.0.6
CSeq: 1 REGISTER
From: <sip:Santanu@xx.xx.xx.xx>;tag=textclientv1.0
To: <sip:Santanu@xx.xx.xx.xx>
Via: SIP/2.0/UDP 192.168.0.6:5060
Max-Forwards: 70
Expires: 180
Authorization: Digest realm="asterisk",nonce="788c190c",username="Santanu",uri="sip:xx.xx.xx.xx",algorithm=MD5,response="6e1abc053061344d38dfb25211e4af49",cnonce="0a4f113b",nc=00000001,qop=auth
Content-Length: 0
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.0.6:5060;branch=z9hG4bK-363031-e1b94f1f52aa39e35c662fc3ae261d16;received=115.187.33.88;rport=5060
From: <sip:Santanu@xx.xx.xx.xx>;tag=textclientv1.0
To: <sip:Santanu@xx.xx.xx.xx>;tag=as58c8d6ed
Call-ID: 1e89a6010565507d291a10a7412fdf7d@192.168.0.6
CSeq: 1 REGISTER
Server: Asterisk PBX 16.7.0
Allow: INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY,INFO,PUBLISH,MESSAGE
Supported: replaces,timer
WWW-Authenticate: Digest algorithm=MD5,realm="asterisk",nonce="33185dc4"
Content-Length: 0
Мои коды следующие:
private String ip;
private SipFactory sipFactory;
private Properties properties;
private SipStack sipStack;
@SuppressWarnings("unused")
private MessageFactory messageFactory;
private HeaderFactory headerFactory;
private AddressFactory addressFactory;
private ListeningPoint listeningPoint;
private int port;
private String protocol;
private SipProvider sipProvider;
private String username1;
private String username2;
private Address contactAddress;
@SuppressWarnings("unused")
private ContactHeader contactHeader;
private String server;
private char[] tag;
private Request request;
private String password;
private ClientTransaction inviteTid;
private Dialog dialog;
private Response resp;
int cseq = 0;
public void init() throws ParseException {
try {
// Get the local IP address.
this.ip = "192.168.0.6";//InetAddress.getLocalHost().getHostAddress();
//this.tag=" ".toCharArray();
// Create the SIP factory and set the path name.
this.sipFactory = SipFactory.getInstance();
this.port=5060;
this.protocol="udp";
this.server="xx.xx.xx.xx";
this.username1="Santanu";
this.username2="Arijit";
this.password="******";
this.sipFactory.setPathName("gov.nist");
// Create and set the SIP stack properties.
this.properties = new Properties();
this.properties.setProperty("javax.sip.STACK_NAME", "stack");
this.properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
this.properties.setProperty(
"gov.nist.javax.sip.LOG_MESSAGE_CONTENT", "true");
this.properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
"mss-jsip-debuglog.txt");
this.properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
"mss-jsip-messages.xml");
// Create the SIP stack.
this.sipStack = this.sipFactory.createSipStack(this.properties);
// Create the SIP message factory.
this.messageFactory = this.sipFactory.createMessageFactory();
// Create the SIP header factory.
this.headerFactory = this.sipFactory.createHeaderFactory();
// Create the SIP address factory.
this.addressFactory = this.sipFactory.createAddressFactory();
// Create the SIP listening point and bind it to the local IP
// address, port and protocol.
this.listeningPoint = this.sipStack.createListeningPoint(this.ip,
this.port, this.protocol);
// Create the SIP provider.
this.sipProvider = this.sipStack
.createSipProvider(this.listeningPoint);
// Add our application as a SIP listener.
this.sipProvider.addSipListener(this);
// Create the contact address used for all SIP messages.
this.contactAddress = this.addressFactory.createAddress("sip:" + this.username1 + "@"
+ this.ip + ";transport=tcp");
// Create the contact header used for all SIP messages.
this.contactHeader = this.headerFactory
.createContactHeader(contactAddress);
// Display the local IP address and port in the text area.
} catch (Exception e) {
e.printStackTrace();
// If an error occurs, display an error message box and exit.
System.exit(-1);
}
//r.setStatusCode(200);
register();
}
public void register() throws NullPointerException{
Object logger;
try {
cseq=1;
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = this.headerFactory.createViaHeader(this.ip,
this.port, "udp", null);
viaHeaders.add(viaHeader);
// The "Max-Forwards" header.
MaxForwardsHeader maxForwardsHeader = this.headerFactory
.createMaxForwardsHeader(70);
// The "Call-Id" header.
CallIdHeader callIdHeader = this.sipProvider.getNewCallId();
// The "CSeq" header.
@SuppressWarnings("deprecation")
CSeqHeader cSeqHeader = this.headerFactory.createCSeqHeader(cseq,
"REGISTER");
Address fromAddress = addressFactory.createAddress("sip:"
+ username1 + '@' + server);
FromHeader fromHeader = this.headerFactory.createFromHeader(
fromAddress, "textclientv1.0");//String.valueOf(this.tag)//Integer.toString(hashCode())
// The "To" header.
ToHeader toHeader = this.headerFactory.createToHeader(fromAddress,
null);
// this.contactHeader = this.headerFactory
// .createContactHeader(contactAddress);
/*request = this.messageFactory.createRequest("REGISTER sip:"
+ server + " SIP/2.0\r\n\r\n");
request.addHeader(callIdHeader);
request.addHeader(viaHeader);
request.addHeader(cSeqHeader);
request.addHeader(fromHeader);
request.addHeader(toHeader);
request.addHeader(maxForwardsHeader);
request.addHeader(contactHeader);*/
URI requestURI = addressFactory.createURI("sip:" + server);
Request request = messageFactory.createRequest(requestURI,
Request.REGISTER, callIdHeader, cSeqHeader, fromHeader,
toHeader, viaHeaders, maxForwardsHeader);
//Response response=this.messageFactory.createResponse(200, request);
HeaderFactory header=this.sipFactory.createHeaderFactory();
ExpiresHeader eh = header.createExpiresHeader(180);
request.addHeader(eh);
if (resp != null) {
boolean retry = true;
AuthorizationHeader authHeader = Utils.makeAuthHeader(headerFactory, resp,
request, username1, password);
request.addHeader(authHeader);
}
System.out.println(""+ request);
inviteTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteTid.sendRequest();
this.dialog = inviteTid.getDialog();
// Send the request statelessly through the SIP provider.
/ / this.sipProvider.sendRequest (запрос);
// Display the message in the text area.
//debug("Request sent:\n" + request.toString() + "\n\n");
} catch (Exception e) {
// If an error occurred, display the error.
e.printStackTrace();
//debug("Request sent failed: " + e.getMessage() + "\n");
}
}
@Override
public void processDialogTerminated(DialogTerminatedEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void processIOException(IOExceptionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void processRequest(RequestEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void processResponse(ResponseEvent arg0) {
// TODO Auto-generated method stub
resp=arg0.getResponse();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(resp);
if(cseq<=2)
register();
}
@Override
public void processTimeout(TimeoutEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void processTransactionTerminated(TransactionTerminatedEvent arg0) {
// TODO Auto-generated method stub
}
}