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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5272 - (show annotations) (download)
Thu Aug 18 23:49:50 2005 UTC (14 years, 3 months ago) by jason
File size: 11457 byte(s)
initial reorg - more to come
1 #include "resiprocate/dum/DialogUsageManager.hxx"
2 #include "resiprocate/dum/MasterProfile.hxx"
3 #include "resiprocate/dum/UserProfile.hxx"
4 #include "resiprocate/dum/ClientAuthManager.hxx"
5 #include "resiprocate/dum/ClientRegistration.hxx"
6 #include "resiprocate/dum/ClientPagerMessage.hxx"
7 #include "resiprocate/dum/ServerPagerMessage.hxx"
8 #include "resiprocate/dum/RegistrationHandler.hxx"
9 #include "resiprocate/dum/PagerMessageHandler.hxx"
10
11 #include "resiprocate/PlainContents.hxx"
12 #include "resiprocate/Pkcs7Contents.hxx"
13 #include "resiprocate/MultipartSignedContents.hxx"
14 #include "resiprocate/Mime.hxx"
15
16 #include "resiprocate/SecurityAttributes.hxx"
17 #include "resiprocate/Helper.hxx"
18
19 #include "resiprocate/os/Log.hxx"
20 #include "resiprocate/os/Logger.hxx"
21
22 #ifdef WIN32
23 #include "resiprocate/XWinSecurity.hxx"
24 #endif
25
26 #include "resiprocate/dum/CertMessage.hxx"
27 #include "resiprocate/dum/RemoteCertStore.hxx"
28
29 #include <iostream>
30 #include <string>
31 #include <sstream>
32
33 using namespace std;
34 using namespace resip;
35
36 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
37
38 class TestSMIMEMessageHandler : public ClientPagerMessageHandler,
39 public ServerPagerMessageHandler,
40 public ClientRegistrationHandler
41 {
42 public:
43 TestSMIMEMessageHandler(Security *security) : security(security), _registered(false), _ended(false), _rcvd(false)
44 {}
45
46 virtual ~TestSMIMEMessageHandler()
47 {
48 }
49
50 void reset()
51 {
52 _ended = false;
53 _rcvd = false;
54 }
55
56 bool isRegistered()
57 {
58 return _registered;
59 }
60
61 bool isEnded()
62 {
63 return _ended;
64 }
65
66 bool isRcvd()
67 {
68 return _rcvd;
69 }
70
71 virtual void onMessageArrived(ServerPagerMessageHandle handle,
72 const SipMessage& message)
73 {
74
75 InfoLog( << "ServerPagerMessageHandler::onMessageArrived: " );
76
77 SipMessage ok = handle->accept();
78 handle->send(ok);
79
80 _rcvd = true;
81
82 InfoLog( << "received: " << message);
83 InfoLog( << "received type " << message.header(h_ContentType) );
84 InfoLog( << "Body: " << *message.getContents() << "\n" );
85 }
86
87 virtual void onSuccess(ClientPagerMessageHandle,
88 const SipMessage& status)
89 {
90 InfoLog( << "ClientMessageHandler::onSuccess\n" );
91 _ended = true;
92 }
93
94 virtual void onFailure(ClientPagerMessageHandle,
95 const SipMessage& status,
96 std::auto_ptr<Contents> contents)
97 {
98 InfoLog( << "ClientMessageHandler::onFailure\n" );
99 InfoLog( << endl << status << endl);
100 _ended = true;
101 _rcvd = true;
102 }
103
104 virtual void onSuccess(ClientRegistrationHandle,
105 const SipMessage& response)
106 {
107 InfoLog( << "ClientRegistrationHandler::onSuccess\n" );
108 _registered = true;
109 }
110
111 virtual void onRemoved(ClientRegistrationHandle)
112 {
113 InfoLog( << "ClientRegistrationHander::onRemoved\n" );
114 exit(-1);
115 }
116
117 virtual void onFailure(ClientRegistrationHandle,
118 const SipMessage& response)
119 {
120 InfoLog( << "ClientRegistrationHandler::onFailure\n" );
121 exit(-1);
122 }
123
124 virtual int onRequestRetry(ClientRegistrationHandle,
125 int retrySeconds, const SipMessage& response)
126 {
127 InfoLog( << "ClientRegistrationHandler::onRequestRetry\n" );
128 exit(-1);
129 }
130
131 protected:
132 Security *security;
133 bool _registered;
134 bool _ended;
135 bool _rcvd;
136
137 };
138
139 class MyCertStore : public RemoteCertStore
140 {
141 public:
142 MyCertStore() {}
143 ~MyCertStore() {}
144 void fetch(const Data& target, MessageId::Type type, const MessageId& id, TransactionUser& tu)
145 {
146 Data empty;
147 CertMessage* msg = new CertMessage(id, false, empty);
148 tu.post(msg);
149 }
150 };
151
152 int main(int argc, char *argv[])
153 {
154
155 if ( argc < 3 ) {
156 cout << "usage: " << argv[0] << " sip:user passwd\n";
157 return 0;
158 }
159
160 //Log::initialize(Log::Cout, Log::Debug, argv[0]);
161 Log::initialize(Log::Cout, Log::Info, argv[0]);
162
163 NameAddr userAor(argv[1]);
164 Data passwd(argv[2]);
165
166 InfoLog(<< "user: " << userAor << ", passwd: " << passwd << "\n");
167
168 #ifdef WIN32
169 Security* security = new XWinSecurity;
170 #else
171 Security* security = new Security;
172 #endif
173
174 assert(security);
175
176 SipStack clientStack(security);
177 DialogUsageManager clientDum(clientStack);
178 clientDum.addTransport(UDP, 10000 + rand()&0x7fff, V4);
179 //clientDum.addTransport(TCP, 10000 + rand()&0x7fff, V4);
180 //clientDum.addTransport(TLS, 10000 + rand()&0x7fff, V4);
181 // clientDum.addTransport(UDP, 10000 + rand()&0x7fff, V6);
182 // clientDum.addTransport(TCP, 10000 + rand()&0x7fff, V6);
183 // clientDum.addTransport(TLS, 10000 + rand()&0x7fff, V6);
184
185 MyCertStore* store = new MyCertStore;
186 clientDum.setRemoteCertStore(auto_ptr<RemoteCertStore>(store));
187
188 SharedPtr<MasterProfile> clientProfile(new MasterProfile);
189 auto_ptr<ClientAuthManager> clientAuth(new ClientAuthManager());
190 TestSMIMEMessageHandler clientHandler(security);
191
192 clientDum.setClientAuthManager(clientAuth);
193 clientDum.setClientRegistrationHandler(&clientHandler);
194 clientDum.setClientPagerMessageHandler(&clientHandler);
195 clientDum.setServerPagerMessageHandler(&clientHandler);
196
197 clientProfile->setDefaultFrom(userAor);
198 clientProfile->setDigestCredential(userAor.uri().host(), userAor.uri().user(), passwd);
199 clientProfile->setDefaultRegistrationTime(70);
200 clientProfile->addSupportedMethod(MESSAGE);
201 clientProfile->addSupportedMimeType(MESSAGE, Mime("text", "plain"));
202 clientProfile->addSupportedMimeType(MESSAGE, Mime("application", "pkcs7-mime"));
203 clientProfile->addSupportedMimeType(MESSAGE, Mime("multipart", "signed"));
204 clientDum.setMasterProfile(clientProfile);
205
206 SipMessage& regMessage = clientDum.makeRegistration(userAor);
207
208 InfoLog( << regMessage << "Generated register: " << endl << regMessage );
209 clientDum.send(regMessage);
210
211 int iteration = 0;
212 bool finished = false;
213 while (!finished)
214 {
215
216 clientHandler.reset();
217
218 bool sent = false;
219 while (!clientHandler.isEnded() || !clientHandler.isRcvd() )
220 {
221 FdSet fdset;
222 clientStack.buildFdSet(fdset);
223
224 int err = fdset.selectMilliSeconds(100);
225 assert ( err != -1 );
226
227 clientStack.process(fdset);
228 while (clientDum.process());
229
230 if (!sent && clientHandler.isRegistered())
231 {
232 try
233 {
234 ClientPagerMessageHandle cpmh = clientDum.makePagerMessage(userAor);
235
236 switch (iteration)
237 {
238 case 0:
239 {
240 Contents* contents = new PlainContents(Data("message"));
241 auto_ptr<Contents> content(contents);
242 cpmh.get()->page(content, DialogUsageManager::None);
243 sent = true;
244 break;
245 }
246 case 1:
247 {
248 InfoLog( << "Sending encrypted message" );
249 Contents* contents = new PlainContents(Data("encrypted message"));
250 auto_ptr<Contents> content(contents);
251 cpmh.get()->page(content, DialogUsageManager::Encrypt);
252 sent = true;
253 break;
254 }
255 case 2:
256 {
257 InfoLog( << "Sending signed message" );
258 Contents* contents = new PlainContents(Data("signed message"));
259 auto_ptr<Contents> content(contents);
260 cpmh.get()->page(content, DialogUsageManager::Sign);
261 sent = true;
262 break;
263 }
264 case 3:
265 {
266 InfoLog( << "Sending encrypted and signed message" );
267 Contents* contents = new PlainContents(Data("encrypted and signed message"));
268 auto_ptr<Contents> content(contents);
269 cpmh.get()->page(content, DialogUsageManager::SignAndEncrypt);
270 sent = true;
271 break;
272 }
273 default:
274 {
275 InfoLog( << "Finished!" );
276 finished = true;
277 break;
278 }
279 }
280
281 if (finished)
282 break;
283 }
284
285 catch (...)
286 {
287 InfoLog( << "failure to send message at iteration " << iteration );
288 break;
289 }
290 }
291 }
292 ++iteration;
293 }
294
295 return 0;
296 }
297
298 /* ====================================================================
299 * The Vovida Software License, Version 1.0
300 *
301 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
302 *
303 * Redistribution and use in source and binary forms, with or without
304 * modification, are permitted provided that the following conditions
305 * are met:
306 *
307 * 1. Redistributions of source code must retain the above copyright
308 * notice, this list of conditions and the following disclaimer.
309 *
310 * 2. Redistributions in binary form must reproduce the above copyright
311 * notice, this list of conditions and the following disclaimer in
312 * the documentation and/or other materials provided with the
313 * distribution.
314 *
315 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
316 * and "Vovida Open Communication Application Library (VOCAL)" must
317 * not be used to endorse or promote products derived from this
318 * software without prior written permission. For written
319 * permission, please contact vocal@vovida.org.
320 *
321 * 4. Products derived from this software may not be called "VOCAL", nor
322 * may "VOCAL" appear in their name, without prior written
323 * permission of Vovida Networks, Inc.
324 *
325 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
326 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
327 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
328 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
329 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
330 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
331 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
332 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
333 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
334 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
335 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
336 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
337 * DAMAGE.
338 *
339 * ====================================================================
340 *
341 * This software consists of voluntary contributions made by Vovida
342 * Networks, Inc. and many individuals on behalf of Vovida Networks,
343 * Inc. For more information on Vovida Networks, Inc., please see
344 * <http://www.vovida.org/>.
345 *
346 */

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27