/[resiprocate]/main/resip/dum/test/testSMIMEInvite.cxx
ViewVC logotype

Contents of /main/resip/dum/test/testSMIMEInvite.cxx

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27