reSIProcate/stack  9694
testTlsConnection.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #include <iostream>
00006 
00007 #ifndef WIN32
00008 #include <sys/time.h>
00009 #include <sys/types.h>
00010 #include <unistd.h>
00011 #endif
00012 
00013 #include "rutil/Socket.hxx"
00014 #include "rutil/Logger.hxx"
00015 
00016 #include "resip/stack/SipStack.hxx"
00017 #include "resip/stack/ssl/Security.hxx"
00018 
00019 
00020 using namespace resip;
00021 using namespace std;
00022 
00023 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00024 
00025 int
00026 main(int argc, char* argv[])
00027 {  
00028    Log::initialize(Log::Cout, Log::Err, argv[0]);
00029    int port = 0;
00030    int dest = 0;
00031    
00032    InfoLog(<<"Test Driver for IM Starting");
00033     
00034    for ( int i=1; i<argc; i++)
00035    {
00036       if (!strcmp(argv[i],"-vv"))
00037       {
00038          Log::setLevel(Log::Debug);
00039       }
00040       else if (!strcmp(argv[i],"-v"))
00041       {
00042          Log::setLevel(Log::Info);
00043       }
00044       else if (!strcmp(argv[i],"-port"))
00045       {
00046          i++;
00047          assert( i<argc );
00048          port = atoi( argv[i] );
00049       } 
00050       else if (!strcmp(argv[i],"-to"))
00051       {
00052          i++;
00053          assert( i<argc );
00054          dest = atoi( argv[i] );
00055       } 
00056       else
00057       { 
00058          ErrLog(<<"Bad command line opion: " << argv[i] );
00059          ErrLog(<<"options are: [-v] [-vv] [-port 1234] [-aor sip:flffuy@flouf.com] [-to sip:1@foo.com]" << argv[i] );
00060          assert(0);
00061       }
00062    }
00063    
00064    InfoLog( << "Using port " << port );
00065    
00066    SipStack sipStack;  
00067 
00068 #ifdef USE_SSL
00069    assert( sipStack.security );
00070    bool ok = sipStack.security->loadAllCerts( Data("password") );
00071    if ( !ok )
00072    {
00073       ErrLog( << "Could not load the certificates" );
00074       assert( ok );
00075    }
00076 #endif
00077 
00078    if ( port != 0 )
00079    {
00080       // do the server side
00081       Socket mFd = socket(PF_INET, SOCK_STREAM, 0);
00082 
00083       if ( mFd == INVALID_SOCKET )
00084       {
00085          InfoLog (<< "Failed to open socket: " << port);
00086       }
00087    
00088       sockaddr_in addr;
00089       addr.sin_family = AF_INET;
00090       addr.sin_addr.s_addr = htonl(INADDR_ANY); 
00091       addr.sin_port = htons(port);
00092    
00093       if ( bind( mFd, (struct sockaddr*) &addr, sizeof(addr)) == SOCKET_ERROR )
00094       {
00095          int err = errno;
00096          if ( err == EADDRINUSE )
00097          {
00098             InfoLog (<< "Address already in use");
00099          }
00100          else
00101          {
00102             InfoLog (<< "Could not bind to port: " << port);
00103          }
00104       
00105          assert(0);
00106       }
00107 
00108       // bind it to the local addr
00109 
00110       // make non blocking 
00111 #if 0
00112 #if WIN32
00113       unsigned long block = 0;
00114       int errNoBlock = ioctlsocket( mFd, FIONBIO , &block );
00115       assert( errNoBlock == 0 );
00116 #else
00117       int flags  = fcntl( mFd, F_GETFL, 0);
00118       int errNoBlock = fcntl(mFd,F_SETFL, flags| O_NONBLOCK );
00119       assert( errNoBlock == 0 );
00120 #endif
00121 #endif
00122 
00123       ErrLog( << "Listening for connections " );;
00124 
00125       // do the listen
00126       int e = listen( mFd , /* qued requests */ 64 );
00127       if (e != 0 )
00128       {
00129          //int err = errno;
00130          // !jf! deal with errors
00131          assert(0);
00132       }
00133       struct sockaddr_in peer;
00134                 
00135 #ifdef __MACH__
00136       int peerLen=sizeof(peer);
00137 #else
00138       socklen_t peerLen=sizeof(peer);
00139 #endif
00140       Socket s = accept( mFd, (struct sockaddr*)&peer,&peerLen);
00141       if ( s == -1 )
00142       {
00143          //int err = errno;
00144          // !jf!
00145          assert(0);
00146       }
00147       ErrLog( << "did accept"  );
00148 
00149       TlsConnection tls( sipStack.security, s, /*server*/ true );
00150       ErrLog( << "Started TLS server"  );
00151 
00152       Data p =  tls.peerName();
00153       ErrLog( << "Connected to " << p  );
00154      
00155       ErrLog( << "Ready to read "  );
00156 
00157       while (true)
00158       {
00159          char buf[1024];
00160          
00161          int r = tls.read( buf, sizeof(buf) );
00162          buf[r] = 0;
00163          
00164          ErrLog( << "read " << r << " bytes" );
00165          ErrLog( << buf );
00166       }
00167        
00168    }
00169    else
00170    {
00171       // do the client side   
00172   
00173       // attempt to open
00174       int sock = socket( AF_INET, SOCK_STREAM, 0 );
00175       if ( sock == -1 )
00176       {
00177          assert(0);
00178       }
00179       
00180       struct sockaddr_in servaddr;
00181       
00182       memset( &servaddr, sizeof(servaddr), 0 );
00183       servaddr.sin_family = AF_INET;
00184       servaddr.sin_port = htons(dest);
00185       servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
00186       
00187       ErrLog("trying to connect ");
00188 
00189       int e = connect( sock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
00190       if ( e == -1 ) 
00191       {
00192          ErrLog("connected failed");
00193          assert(0);
00194       }
00195 
00196       ErrLog("connected ");
00197 
00198       TlsConnection tls( sipStack.security, sock );
00199       ErrLog("Started TLS client ");
00200 
00201       Data p =  tls.peerName();
00202       ErrLog( << "Connected to " << p  );
00203 
00204       ErrLog( << "Ready to go "  );
00205       char* msg = "Hello World";
00206       int r = tls.write(msg,strlen(msg) );
00207       assert( r == (int)strlen(msg) );
00208       
00209       ErrLog( << "Ready to read "  );
00210       while (true)
00211       {
00212          char buf[1024];
00213          
00214          int r = tls.read( buf, sizeof(buf) );
00215          buf[r] =0;
00216          
00217          ErrLog( << "read " << r  );
00218       } 
00219    }
00220 }
00221 
00222 /* ====================================================================
00223  * The Vovida Software License, Version 1.0 
00224  * 
00225  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00226  * 
00227  * Redistribution and use in source and binary forms, with or without
00228  * modification, are permitted provided that the following conditions
00229  * are met:
00230  * 
00231  * 1. Redistributions of source code must retain the above copyright
00232  *    notice, this list of conditions and the following disclaimer.
00233  * 
00234  * 2. Redistributions in binary form must reproduce the above copyright
00235  *    notice, this list of conditions and the following disclaimer in
00236  *    the documentation and/or other materials provided with the
00237  *    distribution.
00238  * 
00239  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00240  *    and "Vovida Open Communication Application Library (VOCAL)" must
00241  *    not be used to endorse or promote products derived from this
00242  *    software without prior written permission. For written
00243  *    permission, please contact vocal@vovida.org.
00244  *
00245  * 4. Products derived from this software may not be called "VOCAL", nor
00246  *    may "VOCAL" appear in their name, without prior written
00247  *    permission of Vovida Networks, Inc.
00248  * 
00249  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00250  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00251  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00252  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00253  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00254  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00255  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00256  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00257  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00258  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00259  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00260  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00261  * DAMAGE.
00262  * 
00263  * ====================================================================
00264  * 
00265  * This software consists of voluntary contributions made by Vovida
00266  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00267  * Inc.  For more information on Vovida Networks, Inc., please see
00268  * <http://www.vovida.org/>.
00269  *
00270  */