/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package model; import gov.nist.javax.sip.DialogTimeoutEvent; import gov.nist.javax.sip.SipListenerExt; import java.net.InetAddress; import java.text.ParseException; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.sip.DialogTerminatedEvent; import javax.sip.IOExceptionEvent; import javax.sip.InvalidArgumentException; import javax.sip.ListeningPoint; import javax.sip.RequestEvent; import javax.sip.ResponseEvent; import javax.sip.SipException; import javax.sip.SipFactory; import javax.sip.SipProvider; import javax.sip.SipStack; import javax.sip.TimeoutEvent; import javax.sip.TransactionTerminatedEvent; import javax.sip.address.*; import javax.sip.header.*; import javax.sip.message.*; /** * * @author Normal */ public class VoIP implements SipListenerExt { SipFactory sipFactory; // Used to access the SIP API. SipStack sipStack; // The SIP stack. SipProvider sipProvider; // Used to send SIP messages. MessageFactory messageFactory; // Used to create SIP message factory. HeaderFactory headerFactory; // Used to create SIP headers. AddressFactory addressFactory; // Used to create SIP URIs. ListeningPoint listeningPoint; // SIP listening IP address/port. Properties properties; // Other properties. private int counter; private int myPort = 5060; private String myName = "129"; private String protocolTyp = "UDP"; private String myIPAddress; int tag = (new Random()).nextInt(); private int branch = hashCode(); Address contactAddress; private ContactHeader contactHeader; public VoIP() { try { //wird an sich erstmal nicht gebraucht aber später sinnvoll um in die unteren Daten zu ergänzen String abc = InetAddress.getLocalHost().getHostAddress(); this.myIPAddress = InetAddress.getLocalHost().getHostAddress(); this.sipFactory = SipFactory.getInstance(); this.sipFactory.setPathName("gov.nist"); this.properties = new Properties(); this.properties.setProperty("javax.sip.STACK_NAME", "stack"); this.sipStack = this.sipFactory.createSipStack(this.properties); this.messageFactory = this.sipFactory.createMessageFactory(); this.headerFactory = this.sipFactory.createHeaderFactory(); this.addressFactory = this.sipFactory.createAddressFactory(); this.listeningPoint = this.sipStack.createListeningPoint(this.myIPAddress, this.myPort, this.protocolTyp); this.sipProvider = this.sipStack.createSipProvider(this.listeningPoint); this.sipProvider.addSipListener(this); this.contactAddress = this.addressFactory.createAddress("sip:129@" + this.myIPAddress+ ":" + this.myPort); //+ ":" + this.myPort this.contactHeader = this.headerFactory.createContactHeader(contactAddress); } catch (Exception e) { System.out.println("Shit"); System.exit(-1); } } public int hashCode() { int temp = tag*(new Random(100)).nextInt(); return temp; } public void sendRegister(String serverIP, int serverPort) throws ParseException, InvalidArgumentException, SipException { System.out.println("-----------SendRegister-----------"); Address addressto = this.addressFactory.createAddress("sip:192.168.100.11"); //evtl. mit Port: sip:192.168.100.11:5060 Address addresstob = this.addressFactory.createAddress("sip:192.168.100.11"); //to get example maybe needed ToHeader to = this.headerFactory.createToHeader(addresstob, null); // Integer.toString(tag) address + tag URI requestURI = addressto.getURI(); CallIdHeader callId = this.sipProvider.getNewCallId(); FromHeader from = this.headerFactory.createFromHeader(addresstob, String.valueOf(this.tag)); //this.contactAddress ArrayList via = new ArrayList(); //Via needs a List as input ViaHeader viaheader = this.headerFactory.createViaHeader(this.myIPAddress, this.myPort, "UDP", String.valueOf(branch)); via.add(viaheader); CSeqHeader cSeq = this.headerFactory.createCSeqHeader(1L, "REGISTER"); MaxForwardsHeader maxForwards = this.headerFactory.createMaxForwardsHeader(70); AllowHeader allow = this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE"); ExpiresHeader expire = this.headerFactory.createExpiresHeader(3600); //int value //Create Request Header System.out.println("----REQUEST HEADER----"); Request request = this.messageFactory.createRequest(requestURI, "REGISTER", callId, cSeq, from, to, via, maxForwards); request.addHeader(contactHeader); request.addHeader(allow); request.addHeader(expire); System.out.println(request); this.sipProvider.sendRequest(request); } @Override public void processDialogTimeout(DialogTimeoutEvent timeoutEvent) { } @Override public void processRequest(RequestEvent requestEvent) { System.out.println("RequestEvent " + requestEvent.getServerTransaction().getDialog()); } @Override public void processResponse(ResponseEvent responseEvent) { System.out.println("---Response---"); System.out.println(responseEvent.getResponse()); if (responseEvent.getResponse().getStatusCode() == Response.UNAUTHORIZED && counter ==0) { counter++; try { Address addresstob = this.addressFactory.createAddress("sip:129@192.168.100.11"); //"sip:Jan@192.168.100.11" ToHeader temp = (ToHeader)responseEvent.getResponse().getHeader("To"); ToHeader to = this.headerFactory.createToHeader(addresstob, temp.getTag()); // Integer.toString(tag) address + tag URI requestURI = addresstob.getURI(); //CallIdHeader tempcall = (CallIdHeader)responseEvent.getResponse().getHeader("Call-ID"); CallIdHeader callId = (CallIdHeader)responseEvent.getResponse().getHeader("Call-ID"); FromHeader from = this.headerFactory.createFromHeader(addresstob, String.valueOf(this.tag)); //this.contactAddress //set Authorization header evtl. user Name benutzen den es gibt WWWAuthenticateHeader abc = (WWWAuthenticateHeader)responseEvent.getResponse().getHeader("WWW-Authenticate"); AuthorizationHeader auth = this.headerFactory.createAuthorizationHeader("Digest");//was kommt in den string auth.setRealm(abc.getRealm()); auth.setNonce(abc.getNonce()); Address addresstest = this.addressFactory.createAddress("sip:"+myName+"@192.168.100.11"); //vielleicht passwort setzen "sip:"+myName+":\"\"@192.168.100.11" auth.setURI(addresstest.getURI()); //System.out.println("Test"); //auth.setQop(abc.getQop()); System.out.println("Test"); //auth.setOpaque(abc.getOpaque()); auth.setUsername(myName); System.out.println("-----auth------"); System.out.println(auth); ArrayList via = new ArrayList(); //Via needs a List as input ViaHeader viaheader = this.headerFactory.createViaHeader(this.myIPAddress, this.myPort, "UDP", String.valueOf(branch+2));// String.valueOf(hashCode())string branch via.add(viaheader); CSeqHeader cSeq = this.headerFactory.createCSeqHeader(2L, "REGISTER"); MaxForwardsHeader maxForwards = this.headerFactory.createMaxForwardsHeader(70); AllowHeader allow = this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE"); ExpiresHeader expire = this.headerFactory.createExpiresHeader(3600); //int value //Zusätzliche Register Header nach Unauthorzied //Create Request Header System.out.println("----REQUEST HEADER2----"); Request request = this.messageFactory.createRequest(requestURI, "REGISTER", callId, cSeq, from, to, via, maxForwards); request.addHeader(contactHeader); request.addHeader(allow); request.addHeader(expire); request.addHeader(auth); System.out.println(request); this.sipProvider.sendRequest(request); } catch (ParseException | SipException |InvalidArgumentException ex) { Logger.getLogger(VoIP.class.getName()).log(Level.SEVERE, null, ex); } } else if (responseEvent.getResponse().getStatusCode() == Response.OK) { System.out.println("responseEvent.getResponse()" + responseEvent.getResponse()); } } @Override public void processTimeout(TimeoutEvent timeoutEvent) { } @Override public void processIOException(IOExceptionEvent exceptionEvent) { } @Override public void processTransactionTerminated( TransactionTerminatedEvent transactionTerminatedEvent) { } @Override public void processDialogTerminated( DialogTerminatedEvent dialogTerminatedEvent) { } }