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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5879 - (hide annotations) (download)
Sat Feb 11 00:12:43 2006 UTC (13 years, 9 months ago) by derek
File size: 14523 byte(s)
update eol property
1 jason 5276 #include "resip/dum/DialogUsageManager.hxx"
2     #include "resip/dum/ClientRegistration.hxx"
3     #include "resip/dum/ClientInviteSession.hxx"
4     #include "resip/dum/ServerInviteSession.hxx"
5     #include "resip/dum/ClientAuthManager.hxx"
6 greg 5016
7 jason 5276 #include "resip/dum/RegistrationHandler.hxx"
8     #include "resip/dum/InviteSessionHandler.hxx"
9     #include "resip/dum/DumShutdownHandler.hxx"
10 greg 5016
11 jason 5276 #include "resip/dum/MasterProfile.hxx"
12 derek 5283 #include "resip/stack/ShutdownMessage.hxx"
13 greg 5016
14 derek 5283 #include "resip/stack/SdpContents.hxx"
15     #include "resip/stack/Pkcs7Contents.hxx"
16     #include "resip/stack/MultipartSignedContents.hxx"
17     #include "resip/stack/MultipartAlternativeContents.hxx"
18     #include "resip/stack/Mime.hxx"
19 greg 5016
20 derek 5283 #include "resip/stack/SecurityAttributes.hxx"
21     #include "resip/stack/Helper.hxx"
22 greg 5016
23 jason 5276 #include "rutil/Log.hxx"
24     #include "rutil/Logger.hxx"
25 greg 5016
26     #ifdef WIN32
27 derek 5283 #include "resip/stack/WinSecurity.hxx"
28 greg 5016 #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 greg 5308 int done;
211 greg 5016 int dumShutDown;
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 greg 5308 return 1;
224 greg 5016 }
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 daniel 5759 SharedPtr<SipMessage> clientRegMessage = clientDum.makeRegistration(clientAor);
307 greg 5016 clientDum.send(clientRegMessage);
308 daniel 5759 SharedPtr<SipMessage> serverRegMessage = serverDum.makeRegistration(serverAor);
309 greg 5016 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 greg 5308 return 0;
420    
421 greg 5016 }
422    
423     /* ====================================================================
424     * The Vovida Software License, Version 1.0
425     *
426     * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
427     *
428     * Redistribution and use in source and binary forms, with or without
429     * modification, are permitted provided that the following conditions
430     * are met:
431     *
432     * 1. Redistributions of source code must retain the above copyright
433     * notice, this list of conditions and the following disclaimer.
434     *
435     * 2. Redistributions in binary form must reproduce the above copyright
436     * notice, this list of conditions and the following disclaimer in
437     * the documentation and/or other materials provided with the
438     * distribution.
439     *
440     * 3. The names "VOCAL", "Vovida Open Communication Application Library",
441     * and "Vovida Open Communication Application Library (VOCAL)" must
442     * not be used to endorse or promote products derived from this
443     * software without prior written permission. For written
444     * permission, please contact vocal@vovida.org.
445     *
446     * 4. Products derived from this software may not be called "VOCAL", nor
447     * may "VOCAL" appear in their name, without prior written
448     * permission of Vovida Networks, Inc.
449     *
450     * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
451     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
452     * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
453     * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
454     * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
455     * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
456     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
457     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
458     * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
459     * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
460     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
461     * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
462     * DAMAGE.
463     *
464     * ====================================================================
465     *
466     * This software consists of voluntary contributions made by Vovida
467     * Networks, Inc. and many individuals on behalf of Vovida Networks,
468     * Inc. For more information on Vovida Networks, Inc., please see
469     * <http://www.vovida.org/>.
470     *
471     */

Properties

Name Value
svn:eol-style LF

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27