/[resiprocate]/branches/b-directory-reorg/resip/dum/test/testSMIMEInvite.cxx
ViewVC logotype

Contents of /branches/b-directory-reorg/resip/dum/test/testSMIMEInvite.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5273 - (show annotations) (download)
Thu Aug 18 23:50:54 2005 UTC (14 years, 3 months ago) by jason
File size: 14561 byte(s)
initial reorg - more to come
1 #include "resiprocate/dum/DialogUsageManager.hxx"
2 #include "resiprocate/dum/ClientRegistration.hxx"
3 #include "resiprocate/dum/ClientInviteSession.hxx"
4 #include "resiprocate/dum/ServerInviteSession.hxx"
5 #include "resiprocate/dum/ClientAuthManager.hxx"
6
7 #include "resiprocate/dum/RegistrationHandler.hxx"
8 #include "resiprocate/dum/InviteSessionHandler.hxx"
9 #include "resiprocate/dum/DumShutdownHandler.hxx"
10
11 #include "resiprocate/dum/MasterProfile.hxx"
12 #include "resiprocate/ShutdownMessage.hxx"
13
14 #include "resiprocate/SdpContents.hxx"
15 #include "resiprocate/Pkcs7Contents.hxx"
16 #include "resiprocate/MultipartSignedContents.hxx"
17 #include "resiprocate/MultipartAlternativeContents.hxx"
18 #include "resiprocate/Mime.hxx"
19
20 #include "resiprocate/SecurityAttributes.hxx"
21 #include "resiprocate/Helper.hxx"
22
23 #include "resiprocate/os/Log.hxx"
24 #include "resiprocate/os/Logger.hxx"
25
26 #ifdef WIN32
27 #include "resiprocate/WinSecurity.hxx"
28 #endif
29
30 #include "TestDumHandlers.hxx"
31
32 #include <time.h>
33 #include <sstream>
34
35 using namespace std;
36 using namespace resip;
37
38 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
39
40
41 class TestSMIMEInviteHandler : public TestClientRegistrationHandler,
42 public TestInviteSessionHandler,
43 public TestDumShutdownHandler
44 {
45 public:
46
47 TestSMIMEInviteHandler(Security *sec) : handles(0), security(sec), registered(0), done(0), dumShutDown(0), connected(0), callFailed(false)
48 {
49 }
50
51 virtual ~TestSMIMEInviteHandler()
52 {
53 }
54
55 void resetRegistered(void)
56 {
57 registered = 0;
58 }
59
60 bool isConnected(void)
61 {
62 return (connected == 2);
63 }
64
65 bool isDumShutDown(void)
66 {
67 return (dumShutDown == 2);
68 }
69
70 bool isRegistered(void)
71 {
72 return (registered == 2);
73 }
74
75 bool isDone(void)
76 {
77 return (done == 2);
78
79 }
80
81 bool isCallFailed(void)
82 {
83 return callFailed;
84 }
85
86 SdpContents* generateBody()
87 {
88 HeaderFieldValue* hfv;
89 Data* txt = new Data("v=0\r\n"
90 "o=1900 369696545 369696545 IN IP4 192.168.2.15\r\n"
91 "s=X-Lite\r\n"
92 "c=IN IP4 192.168.2.15\r\n"
93 "t=0 0\r\n"
94 "m=audio 8000 RTP/AVP 8 3 101\r\n"
95 "a=rtpmap:8 pcma/8000\r\n"
96 "a=rtpmap:3 gsm/8000\r\n"
97 "a=rtpmap:101 telephone-event/8000\r\n"
98 "a=fmtp:101 0-15\r\n");
99
100 hfv = new HeaderFieldValue(txt->data(), txt->size());
101 SdpContents *sdp = new SdpContents(hfv, Mime("application", "sdp"));
102 return sdp;
103 }
104
105
106 virtual void onSuccess(ClientRegistrationHandle r,
107 const SipMessage& response)
108 {
109 InfoLog( << "ClientRegistrationHandler::onSuccess" );
110 handles.push_back(r);
111 registered++;
112 }
113
114 virtual void onConnected(ClientInviteSessionHandle,
115 const SipMessage& msg)
116 {
117 InfoLog( << "ClientInviteSessionHandler::onConnected" );
118 connected++;
119 }
120
121 virtual void onFailure(ClientInviteSessionHandle,
122 const SipMessage& msg)
123 {
124 InfoLog( << "TestInviteSessionHander::onFailure" );
125 callFailed = true;
126 }
127
128 virtual void onNewSession(ServerInviteSessionHandle is,
129 InviteSession::OfferAnswerType oat,
130 const SipMessage& msg)
131 {
132 InfoLog( << "ServerInviteSessionHandler::onNewSession" );
133 sis = is;
134 is->provisional(180);
135 }
136
137 virtual void onConnected(InviteSessionHandle,
138 const SipMessage& msg)
139 {
140 InfoLog( << "InviteSessionHandler::onConnected()" );
141 connected++;
142 }
143
144 virtual void onTerminated(InviteSessionHandle,
145 InviteSessionHandler::TerminatedReason reason,
146 const SipMessage* msg)
147 {
148 InfoLog( << "InviteSessionHandler::onTerminated");
149 done++;
150 }
151
152 virtual void onOffer(InviteSessionHandle is,
153 const SipMessage& msg,
154 const SdpContents& sdp)
155 {
156 InfoLog( << "InviteSessionHandler::onOffer" );
157 InfoLog( << "Server received SDP: " << sdp );
158
159 const SecurityAttributes *attr = msg.getSecurityAttributes();
160 if (attr)
161 {
162 InfoLog( << *attr );
163 }
164 else
165 {
166 InfoLog( << "no Security Attributes" );
167 }
168
169 if (sis.isValid())
170 {
171 NameAddr fromAor(msg.header(h_From).uri());
172 NameAddr toAor(msg.header(h_To).uri());
173 is->provideAnswer(*generateBody());
174 sis->accept();
175 }
176 }
177
178 virtual void onAnswer(InviteSessionHandle,
179 const SipMessage& msg,
180 const SdpContents& sdp)
181 {
182 InfoLog( << "InviteSessionHandler::onAnswer");
183 InfoLog( << "Client received SDP: " << sdp );
184
185 const SecurityAttributes *attr = msg.getSecurityAttributes();
186 if (attr)
187 {
188 InfoLog( << *attr );
189 }
190 else
191 {
192 InfoLog( << "no Security Attributes" );
193 }
194
195 }
196
197 virtual void onDumCanBeDeleted()
198 {
199 InfoLog( << "DumShutDownHandler::onDumCanBeDeleted" );
200 dumShutDown++;
201 }
202
203 public:
204 std::vector<ClientRegistrationHandle> handles;
205 ServerInviteSessionHandle sis;
206
207 private:
208 Security *security;
209 int registered;
210 int dumShutDown;
211 int done;
212 int connected;
213 bool callFailed;
214
215 };
216
217
218 int
219 main (int argc, char** argv)
220 {
221 if ( argc < 5 ) {
222 cout << "usage: " << argv[0] << " sip:user1 passwd1 sip:user2 passwd2" << endl;
223 return 0;
224 }
225
226 Log::initialize(Log::Cout, Log::Debug, argv[0]);
227
228 NameAddr clientAor(argv[1]);
229 Data clientPasswd(argv[2]);
230 NameAddr serverAor(argv[3]);
231 Data serverPasswd(argv[4]);
232
233 #ifdef WIN32
234 Security* security = new WinSecurity;
235 #else
236 Security* security = new Security;
237 #endif
238
239 TestSMIMEInviteHandler handler(security);
240
241 // set up UAC
242 SipStack clientStack(security);
243 DialogUsageManager clientDum(clientStack);
244 srand(time(NULL));
245 clientDum.addTransport(UDP, 10000 + rand()&0x7fff, V4);
246 clientDum.addTransport(TCP, 10000 + rand()&0x7fff, V4);
247 clientDum.addTransport(TLS, 10000 + rand()&0x7fff, V4);
248 #ifdef USE_IPV6
249 clientDum.addTransport(UDP, 10000 + rand()&0x7fff, V6);
250 clientDum.addTransport(TCP, 10000 + rand()&0x7fff, V6);
251 clientDum.addTransport(TLS, 10000 + rand()&0x7fff, V6);
252 #endif
253
254 SharedPtr<MasterProfile> clientProfile(new MasterProfile);
255 auto_ptr<ClientAuthManager> clientAuth(new ClientAuthManager());
256
257 clientDum.setClientAuthManager(clientAuth);
258 clientDum.setClientRegistrationHandler(&handler);
259 clientDum.setInviteSessionHandler(&handler);
260
261 clientProfile->setDefaultFrom(clientAor);
262 clientProfile->setDigestCredential(clientAor.uri().host(),clientAor.uri().user(),clientPasswd);
263 clientProfile->setDefaultRegistrationTime(60);
264 clientProfile->addSupportedMethod(INVITE);
265 clientProfile->addSupportedMimeType(INVITE, Mime("application", "pkcs7-mime"));
266 clientProfile->addSupportedMimeType(INVITE, Mime("multipart", "signed"));
267 clientProfile->addSupportedMimeType(INVITE, Mime("multipart", "alternative"));
268 clientDum.setMasterProfile(clientProfile);
269
270 //set up UAS
271 SipStack serverStack(security);
272 DialogUsageManager serverDum(serverStack);
273 //serverDum.addTransport(UDP, 10000 + rand()&0x7fff, V4);
274 serverDum.addTransport(TCP, 10000 + rand()&0x7fff, V4);
275 //serverDum.addTransport(TLS, 10000 + rand()&0x7fff, V4);
276
277 SharedPtr<MasterProfile> serverProfile(new MasterProfile);
278 std::auto_ptr<ClientAuthManager> serverAuth(new ClientAuthManager);
279
280 serverDum.setClientAuthManager(serverAuth);
281 serverDum.setClientRegistrationHandler(&handler);
282 serverDum.setInviteSessionHandler(&handler);
283
284 serverProfile->setDefaultFrom(serverAor);
285 serverProfile->setDigestCredential(serverAor.uri().host(),serverAor.uri().user(),serverPasswd);
286 serverProfile->setDefaultRegistrationTime(60);
287 serverProfile->addSupportedMethod(INVITE);
288 serverProfile->addSupportedMimeType(INVITE, Mime("application", "pkcs7-mime"));
289 serverProfile->addSupportedMimeType(INVITE, Mime("multipart", "signed"));
290 serverProfile->addSupportedMimeType(INVITE, Mime("multipart", "alternative"));
291 serverDum.setMasterProfile(serverProfile);
292
293 enum
294 {
295 Registering,
296 Inviting,
297 Waiting,
298 HangingUp,
299 Unregistering,
300 ShuttingDown,
301 Finished
302 } state;
303 time_t endTime;
304
305 // register client and server
306 SipMessage& clientRegMessage = clientDum.makeRegistration(clientAor);
307 clientDum.send(clientRegMessage);
308 SipMessage& serverRegMessage = serverDum.makeRegistration(serverAor);
309 serverDum.send(serverRegMessage);
310 state = Registering;
311
312 while (state != Finished)
313 {
314 FdSet fdset;
315
316 clientStack.buildFdSet(fdset);
317 serverStack.buildFdSet(fdset);
318
319 int err = fdset.selectMilliSeconds(resipMin((int)clientStack.getTimeTillNextProcessMS(), 50));
320 assert ( err != -1 );
321
322 clientStack.process(fdset);
323 serverStack.process(fdset);
324 while(clientDum.process() || serverDum.process());
325
326 switch (state)
327 {
328 case Registering:
329 {
330 if (handler.isRegistered())
331 {
332 InfoLog( << "Sending INVITE request" );
333 clientDum.send(clientDum.makeInviteSession(serverAor,
334 handler.generateBody()));
335 state = Inviting;
336 }
337 break;
338 }
339
340 case Inviting:
341 {
342 if (handler.isConnected())
343 {
344 InfoLog( << "Starting timer, waiting for 5 seconds" );
345 endTime = time(NULL) + 5;
346 state = Waiting;
347 }
348 break;
349 }
350
351 case Waiting:
352 {
353 if (handler.isCallFailed())
354 {
355 InfoLog( << "Call Failed" );
356 for (std::vector<ClientRegistrationHandle>::iterator it = handler.handles.begin();
357 it != handler.handles.end(); it++)
358 {
359 (*it)->end();
360 }
361 state = Unregistering;
362 }
363
364 if (time(NULL) > endTime)
365 {
366 InfoLog( << "Timer expired, hanging up" );
367 handler.sis->end();
368 state = HangingUp;
369 }
370 break;
371 }
372
373 case HangingUp:
374 {
375 if (handler.isDone())
376 {
377 for (std::vector<ClientRegistrationHandle>::iterator it = handler.handles.begin();
378 it != handler.handles.end(); it++)
379 {
380 (*it)->end();
381 }
382 state = Unregistering;
383 }
384 break;
385 }
386
387 case Unregistering:
388 {
389 if (handler.isRegistered())
390 {
391 InfoLog( << "Shutting down" );
392 serverDum.shutdown(&handler);
393 clientDum.shutdown(&handler);
394 state = ShuttingDown;
395 }
396 break;
397 }
398
399 case ShuttingDown:
400 {
401 if (handler.isDumShutDown())
402 {
403 InfoLog( << "Finished" );
404 state = Finished;
405 }
406
407 break;
408 }
409
410 default:
411 {
412 InfoLog( << "Unrecognised state" );
413 assert(0);
414 }
415 }
416
417 }
418
419 }
420
421 /* ====================================================================
422 * The Vovida Software License, Version 1.0
423 *
424 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
425 *
426 * Redistribution and use in source and binary forms, with or without
427 * modification, are permitted provided that the following conditions
428 * are met:
429 *
430 * 1. Redistributions of source code must retain the above copyright
431 * notice, this list of conditions and the following disclaimer.
432 *
433 * 2. Redistributions in binary form must reproduce the above copyright
434 * notice, this list of conditions and the following disclaimer in
435 * the documentation and/or other materials provided with the
436 * distribution.
437 *
438 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
439 * and "Vovida Open Communication Application Library (VOCAL)" must
440 * not be used to endorse or promote products derived from this
441 * software without prior written permission. For written
442 * permission, please contact vocal@vovida.org.
443 *
444 * 4. Products derived from this software may not be called "VOCAL", nor
445 * may "VOCAL" appear in their name, without prior written
446 * permission of Vovida Networks, Inc.
447 *
448 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
449 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
450 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
451 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
452 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
453 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
454 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
455 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
456 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
457 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
458 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
459 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
460 * DAMAGE.
461 *
462 * ====================================================================
463 *
464 * This software consists of voluntary contributions made by Vovida
465 * Networks, Inc. and many individuals on behalf of Vovida Networks,
466 * Inc. For more information on Vovida Networks, Inc., please see
467 * <http://www.vovida.org/>.
468 *
469 */

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27