reSIProcate/stack  9694
testStun.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #if defined (HAVE_POPT_H) 
00006 #include <popt.h>
00007 #else
00008 #ifndef WIN32
00009 #warning "will not work very well without libpopt"
00010 #endif
00011 #endif
00012 
00013 #include <sys/types.h>
00014 #include <iostream>
00015 #include <memory>
00016 
00017 #include "rutil/Logger.hxx"
00018 #include "resip/stack/SipStack.hxx"
00019 #include "resip/stack/StackThread.hxx"
00020 #include "resip/stack/stun/stun.h"
00021 #include "resip/stack/stun/udp.h"
00022 
00023 using namespace resip;
00024 using namespace std;
00025 
00026 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00027 
00028 int
00029 main(int argc, char* argv[])
00030 {
00031 
00032    char* logType = "cout";
00033    char* logLevel = "ALERT";
00034    char* proto = "tcp";
00035    char* bindAddr = 0;
00036 
00037    int runs = 10000;
00038    int window = 100;
00039    int seltime = 0;
00040    int v6 = 0;
00041    int invite=0;
00042    
00043 #if defined(HAVE_POPT_H)
00044    struct poptOption table[] = {
00045       {"log-type",    'l', POPT_ARG_STRING, &logType,   0, "where to send logging messages", "syslog|cerr|cout"},
00046       {"log-level",   'v', POPT_ARG_STRING, &logLevel,  0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
00047       {"num-runs",    'r', POPT_ARG_INT,    &runs,      0, "number of calls in test", 0},
00048       {"window-size", 'w', POPT_ARG_INT,    &window,    0, "number of concurrent transactions", 0},
00049       { "select-time", 's', POPT_ARG_INT,    &seltime,   0, "number of runs in test", 0},
00050       {"protocol",    'p', POPT_ARG_STRING, &proto,     0, "protocol to use (tcp | udp)", 0},
00051       {"bind",        'b', POPT_ARG_STRING, &bindAddr,  0, "interface address to bind to",0},
00052       {"v6",          '6', POPT_ARG_NONE,   &v6     ,   0, "ipv6", 0},
00053       {"invite",      'i', POPT_ARG_NONE,   &invite     ,   0, "send INVITE/BYE instead of REGISTER", 0},
00054       POPT_AUTOHELP
00055       { NULL, 0, 0, NULL, 0 }
00056    };
00057    
00058    poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
00059    poptGetNextOpt(context);
00060 #endif
00061    Log::initialize(logType, logLevel, argv[0]);
00062    cout << "Performing " << runs << " runs." << endl;
00063 
00064    IpVersion version = (v6 ? V6 : V4);
00065    SipStack receiver;
00066    
00067    int stunPort = 25080 + rand()& 0x7fff;   
00068    receiver.addTransport(UDP, stunPort, version, StunEnabled);
00069 
00070    StackThread sthread(receiver);
00071    sthread.run();
00072    
00073    UInt64 startTime = Timer::getTimeMs();
00074    int outstanding=0;
00075    int sent = 0;
00076 
00077    int count=0;
00078    StunMessage stun;
00079    StunAtrString username;
00080    memset(&username, 0, sizeof(StunAtrString));
00081    memset(&stun, 0, sizeof(StunMessage));
00082 
00083    bool verbose = false;
00084 
00085    StunAddress4 stunServer;
00086    stunParseServerName( "127.0.0.1", stunServer);
00087    stunServer.port = stunPort;
00088 
00089    int sendPort = 25080 + rand()& 0x7fff;   
00090    int myFd = openPort(sendPort, stunServer.addr , verbose);
00091    
00092    for (count=0; count<runs; ++count)
00093    {
00094       stunBuildReqSimple(&stun, username, false, false, 1);
00095       char buf[STUN_MAX_MESSAGE_SIZE];
00096       int len = STUN_MAX_MESSAGE_SIZE;
00097       len = stunEncodeMessage( stun, buf, len, username, verbose );
00098       sendMessage( myFd, buf, len, stunServer.addr, stunServer.port, verbose );
00099 
00100       StunAddress4 from;
00101 
00102       char msg[STUN_MAX_MESSAGE_SIZE];
00103       int msgLen = STUN_MAX_MESSAGE_SIZE;
00104       getMessage( myFd,
00105                   msg,
00106                   &msgLen,
00107                   &from.addr,
00108                   &from.port,verbose );
00109       
00110       StunMessage resp;
00111       memset(&resp, 0, sizeof(StunMessage));
00112       
00113       if ( verbose ) clog << "Got a response" << endl;
00114       bool ok = stunParseMessage( msg,msgLen, resp,verbose );
00115       
00116       if ( verbose )
00117       {
00118          clog << "\t ok=" << ok << endl;
00119          clog << "\t id=" << resp.msgHdr.id << endl;
00120          clog << "\t mappedAddr=" << resp.mappedAddress.ipv4 << endl;
00121          clog << "\t changedAddr=" << resp.changedAddress.ipv4 << endl;
00122          clog << endl;
00123       }
00124    }
00125 
00126    InfoLog (<< "Finished " << count << " runs");
00127    
00128    UInt64 elapsed = Timer::getTimeMs() - startTime;
00129    if (!invite)
00130    {
00131       cout << runs << " registrations peformed in " << elapsed << " ms, a rate of " 
00132            << runs / ((float) elapsed / 1000.0) << " transactions per second.]" << endl;
00133    }
00134    else
00135    {
00136       cout << runs << " calls peformed in " << elapsed << " ms, a rate of " 
00137            << runs / ((float) elapsed / 1000.0) << " calls per second.]" << endl;
00138    }
00139    cout << "Note: this test runs both sides (client and server)" << endl;
00140    
00141    sthread.shutdown();
00142    sthread.join();
00143 
00144 #if defined(HAVE_POPT_H)
00145    poptFreeContext(context);
00146 #endif
00147    return 0;
00148 }