001: package examples.tls;
002:
003: import javax.sip.*;
004: import javax.sip.address.*;
005: import javax.sip.header.*;
006: import javax.sip.message.*;
007: import java.util.*;
008:
009: /**
010: * This class is a UAC template. Shootist is the guy that shoots and shootme
011: * is the guy that gets shot.
012: *
013: *@author Daniel Martinez
014: *@author Ivelin Ivanov
015: */
016:
017: public class Shootist implements SipListener {
018:
019: private static SipProvider tlsProvider;
020: private static AddressFactory addressFactory;
021: private static MessageFactory messageFactory;
022: private static HeaderFactory headerFactory;
023: private static SipStack sipStack;
024: private int reInviteCount;
025: private ContactHeader contactHeader;
026: private ListeningPoint tlsListeningPoint;
027: private int counter;
028:
029: protected ClientTransaction inviteTid;
030:
031: protected static final String usageString = "java "
032: + "examples.shootistTLS.Shootist \n"
033: + ">>>> is your class path set to the root?";
034:
035: private static void usage() {
036: System.out.println(usageString);
037: System.exit(0);
038:
039: }
040:
041: private void shutDown() {
042: try {
043: try {
044: Thread.sleep(2000);
045: } catch (InterruptedException e) {
046: }
047: System.out.println("nulling reference");
048: sipStack.deleteListeningPoint(tlsListeningPoint);
049: // This will close down the stack and exit all threads
050: tlsProvider.removeSipListener(this );
051: while (true) {
052: try {
053: sipStack.deleteSipProvider(tlsProvider);
054: break;
055: } catch (ObjectInUseException ex) {
056: try {
057: Thread.sleep(2000);
058: } catch (InterruptedException e) {
059: continue;
060: }
061: }
062: }
063: sipStack = null;
064: tlsProvider = null;
065: this .inviteTid = null;
066: this .contactHeader = null;
067: addressFactory = null;
068: headerFactory = null;
069: messageFactory = null;
070: this .tlsListeningPoint = null;
071: this .reInviteCount = 0;
072: System.gc();
073: //Redo this from the start.
074: if (counter < 10)
075: this .init();
076: else
077: counter++;
078: } catch (Exception ex) {
079: ex.printStackTrace();
080: }
081: }
082:
083: public void processRequest(RequestEvent requestReceivedEvent) {
084: Request request = requestReceivedEvent.getRequest();
085: ServerTransaction serverTransactionId = requestReceivedEvent
086: .getServerTransaction();
087:
088: System.out.println("\n\nRequest " + request.getMethod()
089: + " received at " + sipStack.getStackName()
090: + " with server transaction id " + serverTransactionId);
091:
092: // We are the UAC so the only request we get is the BYE.
093: if (request.getMethod().equals(Request.BYE))
094: processBye(request, serverTransactionId);
095:
096: }
097:
098: public void processBye(Request request,
099: ServerTransaction serverTransactionId) {
100: try {
101: System.out.println("shootist: got a bye .");
102: if (serverTransactionId == null) {
103: System.out.println("shootist: null TID.");
104: return;
105: }
106: Dialog dialog = serverTransactionId.getDialog();
107: System.out.println("Dialog State = " + dialog.getState());
108: Response response = messageFactory.createResponse(200,
109: request);
110: serverTransactionId.sendResponse(response);
111: System.out.println("shootist: Sending OK.");
112: System.out.println("Dialog State = " + dialog.getState());
113:
114: this .shutDown();
115:
116: } catch (Exception ex) {
117: ex.printStackTrace();
118: System.exit(0);
119:
120: }
121: }
122:
123: public void processResponse(ResponseEvent responseReceivedEvent) {
124: System.out.println("Got a response");
125: Response response = (Response) responseReceivedEvent
126: .getResponse();
127: Transaction tid = responseReceivedEvent.getClientTransaction();
128:
129: System.out
130: .println("Response received with client transaction id "
131: + tid + ":\n" + response.getStatusCode());
132: if (tid == null) {
133: System.out.println("Stray response -- dropping ");
134: return;
135: }
136: System.out.println("transaction state is " + tid.getState());
137: System.out.println("Dialog = " + tid.getDialog());
138: System.out.println("Dialog State is "
139: + tid.getDialog().getState());
140:
141: try {
142: if (response.getStatusCode() == Response.OK
143: && ((CSeqHeader) response
144: .getHeader(CSeqHeader.NAME)).getMethod()
145: .equals(Request.INVITE)) {
146: // Request cancel = inviteTid.createCancel();
147: // ClientTransaction ct =
148: // sipProvider.getNewClientTransaction(cancel);
149: // ct.sendRequest();
150: Dialog dialog = tid.getDialog();
151: Request ackRequest = dialog.createRequest(Request.ACK);
152: System.out.println("Sending ACK");
153: dialog.sendAck(ackRequest);
154:
155: // Send a Re INVITE
156: if (reInviteCount == 0) {
157: Request inviteRequest = dialog
158: .createRequest(Request.INVITE);
159: //((SipURI)inviteRequest.getRequestURI()).removeParameter("transport");
160: //((ViaHeader)inviteRequest.getHeader(ViaHeader.NAME)).setTransport("tls");
161: // inviteRequest.addHeader(contactHeader);
162:
163: try {
164: Thread.sleep(100);
165: } catch (Exception ex) {
166: }
167: ClientTransaction ct = tlsProvider
168: .getNewClientTransaction(inviteRequest);
169: dialog.sendRequest(ct);
170: reInviteCount++;
171: }
172:
173: }
174: } catch (Exception ex) {
175: ex.printStackTrace();
176: System.exit(0);
177: }
178:
179: }
180:
181: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
182: System.out.println("Transaction Time out");
183: }
184:
185: public void init() {
186: SipFactory sipFactory = null;
187: sipStack = null;
188: sipFactory = SipFactory.getInstance();
189: sipFactory.setPathName("gov.nist");
190: Properties properties = new Properties();
191: // If you want to try TCP transport change the following to
192: String transport = "tcp";
193: String peerHostPort = "127.0.0.1:5071";
194: properties.setProperty("javax.sip.IP_ADDRESS", "127.0.0.1");
195: properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort
196: + "/" + transport);
197: // If you want to use UDP then uncomment this.
198: //properties.setProperty(
199: // "javax.sip.ROUTER_PATH",
200: // "examples.shootistTLS.MyRouter");
201: properties.setProperty("javax.sip.STACK_NAME", "shootist");
202: properties.setProperty("javax.sip.RETRANSMISSION_FILTER", "on");
203:
204: // The following properties are specific to nist-sip
205: // and are not necessarily part of any other jain-sip
206: // implementation.
207: // You can set a max message size for tcp transport to
208: // guard against denial of service attack.
209: properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
210: "1048576");
211: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
212: "shootistdebug.txt");
213: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
214: "shootistlog.txt");
215:
216: // Drop the client connection after we are done with the transaction.
217: properties.setProperty(
218: "gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS", "false");
219: // Set to 0 in your production code for max speed.
220: // You need 16 for logging traces. 32 for debug + traces.
221: // Your code will limp at 32 but it is best for debugging.
222: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "16");
223:
224: try {
225: // Create SipStack object
226: sipStack = sipFactory.createSipStack(properties);
227: System.out.println("createSipStack " + sipStack);
228: } catch (PeerUnavailableException e) {
229: // could not find
230: // gov.nist.jain.protocol.ip.sip.SipStackImpl
231: // in the classpath
232: e.printStackTrace();
233: System.err.println(e.getMessage());
234: System.exit(0);
235: }
236:
237: try {
238: headerFactory = sipFactory.createHeaderFactory();
239: addressFactory = sipFactory.createAddressFactory();
240: messageFactory = sipFactory.createMessageFactory();
241: Shootist listener = this ;
242:
243: tlsListeningPoint = sipStack.createListeningPoint(sipStack
244: .getIPAddress(), 5060, "tcp");
245: tlsProvider = sipStack.createSipProvider(tlsListeningPoint);
246: tlsProvider.addSipListener(listener);
247:
248: SipProvider sipProvider = tlsProvider;
249:
250: String fromName = "BigGuy";
251: String fromSipAddress = "here.com";
252: String fromDisplayName = "The Master Blaster";
253:
254: String toSipAddress = "there.com";
255: String toUser = "LittleGuy";
256: String toDisplayName = "The Little Blister";
257:
258: // create >From Header
259: SipURI fromAddress = addressFactory.createSipURI(fromName,
260: fromSipAddress);
261: //fromAddress.setSecure(true);
262:
263: Address fromNameAddress = addressFactory
264: .createAddress(fromAddress);
265: fromNameAddress.setDisplayName(fromDisplayName);
266: FromHeader fromHeader = headerFactory.createFromHeader(
267: fromNameAddress, "12345");
268:
269: // create To Header
270: SipURI toAddress = addressFactory.createSipURI(toUser,
271: toSipAddress);
272: //toAddress.setSecure(true);
273: Address toNameAddress = addressFactory
274: .createAddress(toAddress);
275: toNameAddress.setDisplayName(toDisplayName);
276: ToHeader toHeader = headerFactory.createToHeader(
277: toNameAddress, null);
278:
279: // create Request URI
280: SipURI requestURI = addressFactory.createSipURI(toUser,
281: peerHostPort);
282: //requestURI.setSecure( true );
283:
284: // Create ViaHeaders
285:
286: ArrayList viaHeaders = new ArrayList();
287: int port = sipProvider.getListeningPoint(transport)
288: .getPort();
289: ViaHeader viaHeader = headerFactory.createViaHeader(
290: sipStack.getIPAddress(), port, transport, null);
291:
292: // add via headers
293: viaHeaders.add(viaHeader);
294:
295: // Create ContentTypeHeader
296: ContentTypeHeader contentTypeHeader = headerFactory
297: .createContentTypeHeader("application", "sdp");
298:
299: // Create a new CallId header
300: CallIdHeader callIdHeader = sipProvider.getNewCallId();
301:
302: // Create a new Cseq header
303: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
304: Request.INVITE);
305:
306: // Create a new MaxForwardsHeader
307: MaxForwardsHeader maxForwards = headerFactory
308: .createMaxForwardsHeader(70);
309:
310: // Create the request.
311: Request request = messageFactory.createRequest(requestURI,
312: Request.INVITE, callIdHeader, cSeqHeader,
313: fromHeader, toHeader, viaHeaders, maxForwards);
314: // Create contact headers
315: String host = sipStack.getIPAddress();
316:
317: //SipURI contactUrl = addressFactory.createSipURI(fromName, host);
318: //contactUrl.setPort(tlsListeningPoint.getPort());
319:
320: // Create the contact name address.
321: SipURI contactURI = addressFactory.createSipURI(fromName,
322: host);
323: //contactURI.setSecure( true );
324: contactURI.setPort(port);
325: contactURI.setTransportParam("tcp");
326:
327: Address contactAddress = addressFactory
328: .createAddress(contactURI);
329:
330: // Add the contact address.
331: contactAddress.setDisplayName(fromName);
332:
333: contactHeader = headerFactory
334: .createContactHeader(contactAddress);
335: request.addHeader(contactHeader);
336:
337: // Add the extension header.
338: Header extensionHeader = headerFactory.createHeader(
339: "My-Header", "my header value");
340: request.addHeader(extensionHeader);
341:
342: String sdpData = "v=0\r\n"
343: + "o=4855 13760799956958020 13760799956958020"
344: + " IN IP4 129.6.55.78\r\n"
345: + "s=mysession session\r\n"
346: + "p=+46 8 52018010\r\n"
347: + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n"
348: + "m=audio 6022 RTP/AVP 0 4 18\r\n"
349: + "a=rtpmap:0 PCMU/8000\r\n"
350: + "a=rtpmap:4 G723/8000\r\n"
351: + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
352: byte[] contents = sdpData.getBytes();
353: //byte[] contents = sdpBuff.toString().getBytes();
354:
355: request.setContent(contents, contentTypeHeader);
356:
357: extensionHeader = headerFactory.createHeader(
358: "My-Other-Header", "my new header value ");
359: request.addHeader(extensionHeader);
360:
361: Header callInfoHeader = headerFactory.createHeader(
362: "Call-Info", "<http://www.antd.nist.gov>");
363: request.addHeader(callInfoHeader);
364:
365: // Create the client transaction.
366: listener.inviteTid = sipProvider
367: .getNewClientTransaction(request);
368:
369: // send the request out.
370: listener.inviteTid.sendRequest();
371:
372: } catch (Exception ex) {
373: System.out.println(ex.getMessage());
374: ex.printStackTrace();
375: usage();
376: }
377: }
378:
379: public static void main(String args[]) {
380: new Shootist().init();
381:
382: }
383:
384: public void processIOException(IOExceptionEvent exceptionEvent) {
385: System.out
386: .println("IOException occured while retransmitting requests:"
387: + exceptionEvent);
388: }
389:
390: public void processTransactionTerminated(
391: TransactionTerminatedEvent transactionTerminatedEvent) {
392: System.out.println("Transaction Terminated event: "
393: + transactionTerminatedEvent);
394: }
395:
396: public void processDialogTerminated(
397: DialogTerminatedEvent dialogTerminatedEvent) {
398: System.out.println("Dialog Terminated event: "
399: + dialogTerminatedEvent);
400: }
401: }
|