blob: a90ce72fbb96303fe280d0ab15640c201156ea46 [file] [log] [blame]
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05001/*
2 * Copyright (c) 2013 Slient Circle LLC. All rights reserved.
3 *
4 *
5 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
6 */
7
8import java.net.*;
9
10import wd.tivi.*;
11
12public class SessionTest {
13 static {
14 System.loadLibrary("zrtptivi");
15 }
16
17 CtZrtpSession session;
18 TestCallbackAudio callback;
19 TestSendCallbackAudio sendCallback;
20 DatagramSocket dgSock;
21
22 InetAddress localAddr;
23 InetAddress remoteAddr;
24
25 long uiSSRC = 0xfeedbacc;
26
27
28 // This is the callback that we use for audio stream
29 private class TestCallbackAudio extends CtZrtpCb {
30
31 @Override
32 public void onNewZrtpStatus(CtZrtpSession session, String p, CtZrtpSession.streamName streamNm) {
33 System.out.println("new status: " + p);
34 }
35
36 @Override
37 public void onNeedEnroll(CtZrtpSession session, CtZrtpSession.streamName streamNm, int info) {
38 System.out.println("Need enroll\n");
39 }
40
41 @Override
42 public void onPeer(CtZrtpSession session, String name, int iIsVerified, CtZrtpSession.streamName streamNm) {
43 System.out.println("onPeer: " + name);
44
45 byte[] buffer = new byte[30];
46
47 session.getInfo("rs1", buffer, buffer.length);
48 System.out.print("RS1: " + new String(buffer) + " ");
49
50 session.getInfo("rs2", buffer, buffer.length);
51 System.out.print("RS2: " + new String(buffer) + " ");
52
53 session.getInfo("pbx", buffer, buffer.length);
54 System.out.print("PBX: " + new String(buffer) + " ");
55
56 session.getInfo("aux", buffer, buffer.length);
57 System.out.println("AUX: " + new String(buffer) + " ");
58
59 session.getInfo("lbClient", buffer, buffer.length);
60 System.out.print("Client: " + new String(buffer) + " ");
61
62 session.getInfo("lbVersion", buffer, buffer.length);
63 System.out.print("Version: " + new String(buffer) + " ");
64
65 session.getInfo("lbChiper", buffer, buffer.length);
66 System.out.print("cipher: " + new String(buffer) + " ");
67
68 session.getInfo("lbHash", buffer, buffer.length);
69 System.out.print("hash: " + new String(buffer) + " ");
70
71 session.getInfo("lbAuthTag", buffer, buffer.length);
72 System.out.print("auth: " + new String(buffer) + " ");
73
74 session.getInfo("lbKeyExchange", buffer, buffer.length);
75 System.out.print("KeyEx: " + new String(buffer) + " ");
76
77 session.getInfo("sc_secure", buffer, buffer.length);
78 System.out.print("SC secure: " + new String(buffer) + " ");
79
80 session.getInfo("sdp_hash", buffer, buffer.length);
81 System.out.println("zrtp-hash: " + new String(buffer) + " ");
82
83 session.setLastPeerNameVerify("TestName", 0);
84 }
85
86 @Override
87 public void onZrtpWarning(CtZrtpSession session, String p, CtZrtpSession.streamName streamNm) {
88 System.out.println("Warning: " + p);
89 }
90 }
91
92 private class TestSendCallbackAudio extends CtZrtpSendCb {
93 @Override
94 public void sendRtp(CtZrtpSession session, byte[] packet, CtZrtpSession.streamName streamNm) {
95// hexdump("ZRTP packet", packet, length);
96 System.out.println("ZRTP send packet, length: " + packet.length);
97 sendData(packet);
98 }
99 }
100
101
102 void sendData(byte[ ]buffer) {
103 DatagramPacket dgram = new DatagramPacket(buffer, buffer.length, remoteAddr, 5004);
104 try {
105 dgSock.send(dgram);
106 }
107 catch (java.io.IOException ex) {
108 System.out.println("cannot send: " + ex);
109 }
110 }
111
112 public void simpleTest(String argv[]) {
113 byte[] helloHash = new byte[100];
114 byte[] dgramBuffer = new byte[2000];
115 long[] newLength = new long[1];
116
117 String local = "127.0.0.1";
118 String remote = "127.0.0.1";
119
120 if (argv.length >= 2) {
121 local = argv[0];
122 remote = argv[1];
123 }
124 // Setup the IP addresses to receive and send packets
125 try {
126 localAddr = InetAddress.getByName(local);
127 remoteAddr = InetAddress.getByName(remote);
128 }
129 catch (java.net.UnknownHostException ex) {
130 System.out.println("Host not known: " + ex);
131 }
132 System.out.println("Address: " + localAddr.getHostAddress() + ", " + remoteAddr.getHostAddress());
133
134 session = new CtZrtpSession();
135 callback = new TestCallbackAudio();
136 sendCallback = new TestSendCallbackAudio();
137
138 session.init(true, true); // audio and video
139
140 session.setUserCallback(callback, CtZrtpSession.streamName.AudioStream);
141 session.setSendCallback(sendCallback, CtZrtpSession.streamName.AudioStream);
142 session.getSignalingHelloHash(helloHash, CtZrtpSession.streamName.AudioStream);
143
144 System.out.println("Our Hello hash: " + new String(helloHash));
145
146 // Our receive datagram socket
147 try {
148 dgSock = new DatagramSocket(5002, localAddr);
149 }
150 catch (java.net.SocketException ex) {
151 System.out.println("cannot create datagram socket: " + ex);
152 }
153
154 if (!session.isStarted(CtZrtpSession.streamName.AudioStream)) {
155 System.out.println("Starting ...");
156 session.start(uiSSRC, CtZrtpSession.streamName.AudioStream);
157 }
158 DatagramPacket dgram = new DatagramPacket(dgramBuffer, dgramBuffer.length);
159 for (;;) {
160 try {
161 dgSock.receive(dgram);
162 }
163 catch (java.io.IOException ex) {
164 System.out.println("cannot receive: " + ex);
165 }
166// hexdump("recv buffer before", dgramBuffer, dgram.getLength());
167 int rc = session.processIncomingRtp(dgramBuffer, (long)dgram.getLength(), newLength, CtZrtpSession.streamName.AudioStream);
168
169 if (rc == 0)
170 continue;
171
172 System.out.println("processing returns: " + rc + ", new length: " + newLength[0]);
173 System.out.println("Received data: "+ new String(dgramBuffer, 12, (int)(newLength[0]-12)));
174// hexdump("recv buffer after", dgramBuffer, dgram.getLength());
175 }
176 }
177
178 public static void main(String argv[]) {
179 CtZrtpSession.initCache("testzid.dat");
180
181 SessionTest stest = new SessionTest();
182 stest.simpleTest(argv);
183 System.out.println("Something works.");
184 try {
185 Thread.sleep(5000);
186 }
187 catch (java.lang.InterruptedException ex) {
188 }
189 }
190
191 private static final char[] hex = "0123456789abcdef".toCharArray();
192
193 /**
194 * Dump a buffer in hex and readable format.
195 *
196 * @param title Printed at the beginning of the dump
197 * @param buf Byte buffer to dump
198 * @param len Number of bytes to dump, should be less or equal
199 * the buffer length
200 */
201 public void hexdump(String title, byte[] buf, int len) {
202 byte b;
203 System.err.println(title);
204 for(int i = 0 ; ; i += 16) {
205 for(int j=0; j < 16; ++j) {
206 if (i+j >= len) {
207 System.err.print(" ");
208 }
209 else {
210 b = buf[i+j];
211 System.err.print(" "+ hex[(b>>>4) &0xf] + hex[b&0xf] );
212 }
213 }
214 System.err.print(" ");
215 for(int j = 0; j < 16; ++j) {
216 if (i+j >= len) break;
217 b = buf[i+j];
218 if ( (byte)(b+1) < 32+1) {
219 System.err.print( '.' );
220 }
221 else {
222 System.err.print( (char)b );
223 }
224 }
225 System.err.println();
226 if (i+16 >= len) {
227 break;
228 }
229 }
230 }
231}