SIP Register Request using JAIN SIP

This tutorial uses NIST SIP JAIN Reference Implementation. Download the NIST source code from here.

Note: To understand the Authorization process for handling 401 response code from server please go through this tutorial.

REGISTER request is used to update the current location of a user on the REGISTRAR server. The application sends a REGISTER message informing the server its current location which in turn is stored in location server. When a caller calls a user the proxy server uses this information to find the location of the callee.

Register request should be sent by the client periodically. The validity of the REGISTER request is determined by the Expires header.

Flow

image

Sample Request

REGISTER sip:sip.linphone.org SIP/2.0
Call-ID: 0a0f14355fa76ae6435b98bfe085baf9@223.1.1.128
CSeq: 1 REGISTER
From: <sip:vkslabs@sip.linphone.org>;tag=-1427592833
To: <sip:vkslabs@sip.linphone.org>
Max-Forwards: 70
Via: SIP/2.0/TCP 223.1.1.128:5060;branch=z9hG4bK-323532-2a454f4ec2a4213f6d6928eba479521d
Contact: <sip:vkslabs@223.1.1.128;transport=tcp>
Content-Length: 0

Now lets see how to construct the above request using the NIST SIP Stack.

First step is to create a class that implements the SIPListener. Make sure your SIP Stack is Initializing NIST JAIN SIP Stack.

1. Create Call-ID header

 CallIdHeader callIdHeader = this.sipProvider.getNewCallId(); 

2. Create CSeq header

 CSeqHeader cSeqHeader = this.headerFactory.createCSeqHeader(cseq, "REGISTER"); 

3. Create From header

 Address fromAddress = addressFactory.createAddress("sip:" + username + '@' + server);
FromHeader fromHeader = this.headerFactory.createFromHeader(fromAddress, String.valueOf(this.tag));

4. Create To Header

 ToHeader toHeader = this.headerFactory.createToHeader(fromAddress, null); 

5. Create Max-Forwards header

 MaxForwardsHeader maxForwardsHeader = this.headerFactory.createMaxForwardsHeader(70); 

6. Create a Via header

ArrayList viaHeaders = new ArrayList();
ViaHeader viaHeader = this.headerFactory.createViaHeader(this.ip, this.port, "tcp", null);
viaHeaders.add(viaHeader);

7. Create contact header

this.contactAddress = this.addressFactory.createAddress("sip:" + this.username + '@' + this.ip + "transport=tcp");
// Create the contact header used for all SIP messages.
this.contactHeader = this.headerFactory.createContactHeader(contactAddress);

Once all the headers are created it is time to create the request itself.

request = this.messageFactory.createRequest("REGISTER sip:" + server + "SIP/2.0\r\n\r\n");
request.addHeader(callIdHeader);
request.addHeader(cSeqHeader);
request.addHeader(fromHeader);
request.addHeader(toHeader);
request.addHeader(maxForwardsHeader);
request.addHeader(viaHeader);
request.addHeader(contactHeader);

Now that the request object is created with all the necessary headers it is time to send the request.

   inviteTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteTid.sendRequest();

Once the request is sent successfully the response will be passed to the application using the processResponse callback in SIPListener.

public void processResponse(ResponseEvent responseEvent) {
int statusCode = responseEvent.getResponse().getStatusCode();
}

Code

public void register(Response response) {
try {
cseq++;
ArrayList viaHeaders = new ArrayList();
ViaHeader viaHeader = this.headerFactory.createViaHeader(this.ip,
this.port, "tcp", 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.
CSeqHeader cSeqHeader = this.headerFactory.createCSeqHeader(cseq,
"REGISTER");

Address fromAddress = addressFactory.createAddress("sip:"
+ username + '@' + server);

FromHeader fromHeader = this.headerFactory.createFromHeader(
fromAddress, String.valueOf(this.tag));
// 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(cSeqHeader);
request.addHeader(fromHeader);
request.addHeader(toHeader);
request.addHeader(maxForwardsHeader);
request.addHeader(viaHeader);
request.addHeader(contactHeader);
if (response != null) {
retry = true;
AuthorizationHeader authHeader = Utils.makeAuthHeader(headerFactory, response,
request, username, password);
request.addHeader(authHeader);
}
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(request);

// Display the message in the text area.
logger.debug("Request sent:\n" + request.toString() + "\n\n");
} catch (Exception e) {
// If an error occurred, display the error.
e.printStackTrace();
logger.debug("Request sent failed: " + e.getMessage() + "\n");
}
}

Download the complete working code from here.

9375 Total Views 15 Views Today
  • Ahmed

    in line 40, utils.makeAuthHeader , this line is causing me a problem saying that there is no such method for Utils, can any1 help please

  • esprittn

    Please could repaire the Download the complete working code link

  • Everton Barros

    Hi, could you please send me the complete code. For page not found when clicking on the link occurs.

  • Manish

    Guys there is one very important header is missing in above code..add this in your code it will work fine:
    ExpiresHeader expireHeader = this.headerFactory.createExpiresHeader(120);
    request.addHeader(expireHeader);