/[resiprocate]/main/resip/stack/test/testTlsConnection.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testTlsConnection.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9485 - (show annotations) (download)
Sat Apr 7 00:34:03 2012 UTC (7 years, 7 months ago) by dpocock
File MIME type: text/plain
File size: 7276 byte(s)
Switch over to autotools build system from dpocock-autotools branch
1 #if defined(HAVE_CONFIG_H)
2 #include "config.h"
3 #endif
4
5 #include <iostream>
6
7 #ifndef WIN32
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #endif
12
13 #include "rutil/Socket.hxx"
14 #include "rutil/Logger.hxx"
15
16 #include "resip/stack/SipStack.hxx"
17 #include "resip/stack/ssl/Security.hxx"
18
19
20 using namespace resip;
21 using namespace std;
22
23 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
24
25 int
26 main(int argc, char* argv[])
27 {
28 Log::initialize(Log::Cout, Log::Err, argv[0]);
29 int port = 0;
30 int dest = 0;
31
32 InfoLog(<<"Test Driver for IM Starting");
33
34 for ( int i=1; i<argc; i++)
35 {
36 if (!strcmp(argv[i],"-vv"))
37 {
38 Log::setLevel(Log::Debug);
39 }
40 else if (!strcmp(argv[i],"-v"))
41 {
42 Log::setLevel(Log::Info);
43 }
44 else if (!strcmp(argv[i],"-port"))
45 {
46 i++;
47 assert( i<argc );
48 port = atoi( argv[i] );
49 }
50 else if (!strcmp(argv[i],"-to"))
51 {
52 i++;
53 assert( i<argc );
54 dest = atoi( argv[i] );
55 }
56 else
57 {
58 ErrLog(<<"Bad command line opion: " << argv[i] );
59 ErrLog(<<"options are: [-v] [-vv] [-port 1234] [-aor sip:flffuy@flouf.com] [-to sip:1@foo.com]" << argv[i] );
60 assert(0);
61 }
62 }
63
64 InfoLog( << "Using port " << port );
65
66 SipStack sipStack;
67
68 #ifdef USE_SSL
69 assert( sipStack.security );
70 bool ok = sipStack.security->loadAllCerts( Data("password") );
71 if ( !ok )
72 {
73 ErrLog( << "Could not load the certificates" );
74 assert( ok );
75 }
76 #endif
77
78 if ( port != 0 )
79 {
80 // do the server side
81 Socket mFd = socket(PF_INET, SOCK_STREAM, 0);
82
83 if ( mFd == INVALID_SOCKET )
84 {
85 InfoLog (<< "Failed to open socket: " << port);
86 }
87
88 sockaddr_in addr;
89 addr.sin_family = AF_INET;
90 addr.sin_addr.s_addr = htonl(INADDR_ANY);
91 addr.sin_port = htons(port);
92
93 if ( bind( mFd, (struct sockaddr*) &addr, sizeof(addr)) == SOCKET_ERROR )
94 {
95 int err = errno;
96 if ( err == EADDRINUSE )
97 {
98 InfoLog (<< "Address already in use");
99 }
100 else
101 {
102 InfoLog (<< "Could not bind to port: " << port);
103 }
104
105 assert(0);
106 }
107
108 // bind it to the local addr
109
110 // make non blocking
111 #if 0
112 #if WIN32
113 unsigned long block = 0;
114 int errNoBlock = ioctlsocket( mFd, FIONBIO , &block );
115 assert( errNoBlock == 0 );
116 #else
117 int flags = fcntl( mFd, F_GETFL, 0);
118 int errNoBlock = fcntl(mFd,F_SETFL, flags| O_NONBLOCK );
119 assert( errNoBlock == 0 );
120 #endif
121 #endif
122
123 ErrLog( << "Listening for connections " );;
124
125 // do the listen
126 int e = listen( mFd , /* qued requests */ 64 );
127 if (e != 0 )
128 {
129 //int err = errno;
130 // !jf! deal with errors
131 assert(0);
132 }
133 struct sockaddr_in peer;
134
135 #ifdef __MACH__
136 int peerLen=sizeof(peer);
137 #else
138 socklen_t peerLen=sizeof(peer);
139 #endif
140 Socket s = accept( mFd, (struct sockaddr*)&peer,&peerLen);
141 if ( s == -1 )
142 {
143 //int err = errno;
144 // !jf!
145 assert(0);
146 }
147 ErrLog( << "did accept" );
148
149 TlsConnection tls( sipStack.security, s, /*server*/ true );
150 ErrLog( << "Started TLS server" );
151
152 Data p = tls.peerName();
153 ErrLog( << "Connected to " << p );
154
155 ErrLog( << "Ready to read " );
156
157 while (true)
158 {
159 char buf[1024];
160
161 int r = tls.read( buf, sizeof(buf) );
162 buf[r] = 0;
163
164 ErrLog( << "read " << r << " bytes" );
165 ErrLog( << buf );
166 }
167
168 }
169 else
170 {
171 // do the client side
172
173 // attempt to open
174 int sock = socket( AF_INET, SOCK_STREAM, 0 );
175 if ( sock == -1 )
176 {
177 assert(0);
178 }
179
180 struct sockaddr_in servaddr;
181
182 memset( &servaddr, sizeof(servaddr), 0 );
183 servaddr.sin_family = AF_INET;
184 servaddr.sin_port = htons(dest);
185 servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
186
187 ErrLog("trying to connect ");
188
189 int e = connect( sock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
190 if ( e == -1 )
191 {
192 ErrLog("connected failed");
193 assert(0);
194 }
195
196 ErrLog("connected ");
197
198 TlsConnection tls( sipStack.security, sock );
199 ErrLog("Started TLS client ");
200
201 Data p = tls.peerName();
202 ErrLog( << "Connected to " << p );
203
204 ErrLog( << "Ready to go " );
205 char* msg = "Hello World";
206 int r = tls.write(msg,strlen(msg) );
207 assert( r == (int)strlen(msg) );
208
209 ErrLog( << "Ready to read " );
210 while (true)
211 {
212 char buf[1024];
213
214 int r = tls.read( buf, sizeof(buf) );
215 buf[r] =0;
216
217 ErrLog( << "read " << r );
218 }
219 }
220 }
221
222 /* ====================================================================
223 * The Vovida Software License, Version 1.0
224 *
225 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
226 *
227 * Redistribution and use in source and binary forms, with or without
228 * modification, are permitted provided that the following conditions
229 * are met:
230 *
231 * 1. Redistributions of source code must retain the above copyright
232 * notice, this list of conditions and the following disclaimer.
233 *
234 * 2. Redistributions in binary form must reproduce the above copyright
235 * notice, this list of conditions and the following disclaimer in
236 * the documentation and/or other materials provided with the
237 * distribution.
238 *
239 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
240 * and "Vovida Open Communication Application Library (VOCAL)" must
241 * not be used to endorse or promote products derived from this
242 * software without prior written permission. For written
243 * permission, please contact vocal@vovida.org.
244 *
245 * 4. Products derived from this software may not be called "VOCAL", nor
246 * may "VOCAL" appear in their name, without prior written
247 * permission of Vovida Networks, Inc.
248 *
249 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
250 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
251 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
252 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
253 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
254 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
255 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
256 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
257 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
258 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
260 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
261 * DAMAGE.
262 *
263 * ====================================================================
264 *
265 * This software consists of voluntary contributions made by Vovida
266 * Networks, Inc. and many individuals on behalf of Vovida Networks,
267 * Inc. For more information on Vovida Networks, Inc., please see
268 * <http://www.vovida.org/>.
269 *
270 */

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