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

Contents of /main/resip/stack/test/testStun.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: 4463 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 #if defined (HAVE_POPT_H)
6 #include <popt.h>
7 #else
8 #ifndef WIN32
9 #warning "will not work very well without libpopt"
10 #endif
11 #endif
12
13 #include <sys/types.h>
14 #include <iostream>
15 #include <memory>
16
17 #include "rutil/Logger.hxx"
18 #include "resip/stack/SipStack.hxx"
19 #include "resip/stack/StackThread.hxx"
20 #include "resip/stack/stun/stun.h"
21 #include "resip/stack/stun/udp.h"
22
23 using namespace resip;
24 using namespace std;
25
26 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
27
28 int
29 main(int argc, char* argv[])
30 {
31
32 char* logType = "cout";
33 char* logLevel = "ALERT";
34 char* proto = "tcp";
35 char* bindAddr = 0;
36
37 int runs = 10000;
38 int window = 100;
39 int seltime = 0;
40 int v6 = 0;
41 int invite=0;
42
43 #if defined(HAVE_POPT_H)
44 struct poptOption table[] = {
45 {"log-type", 'l', POPT_ARG_STRING, &logType, 0, "where to send logging messages", "syslog|cerr|cout"},
46 {"log-level", 'v', POPT_ARG_STRING, &logLevel, 0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
47 {"num-runs", 'r', POPT_ARG_INT, &runs, 0, "number of calls in test", 0},
48 {"window-size", 'w', POPT_ARG_INT, &window, 0, "number of concurrent transactions", 0},
49 { "select-time", 's', POPT_ARG_INT, &seltime, 0, "number of runs in test", 0},
50 {"protocol", 'p', POPT_ARG_STRING, &proto, 0, "protocol to use (tcp | udp)", 0},
51 {"bind", 'b', POPT_ARG_STRING, &bindAddr, 0, "interface address to bind to",0},
52 {"v6", '6', POPT_ARG_NONE, &v6 , 0, "ipv6", 0},
53 {"invite", 'i', POPT_ARG_NONE, &invite , 0, "send INVITE/BYE instead of REGISTER", 0},
54 POPT_AUTOHELP
55 { NULL, 0, 0, NULL, 0 }
56 };
57
58 poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
59 poptGetNextOpt(context);
60 #endif
61 Log::initialize(logType, logLevel, argv[0]);
62 cout << "Performing " << runs << " runs." << endl;
63
64 IpVersion version = (v6 ? V6 : V4);
65 SipStack receiver;
66
67 int stunPort = 25080 + rand()& 0x7fff;
68 receiver.addTransport(UDP, stunPort, version, StunEnabled);
69
70 StackThread sthread(receiver);
71 sthread.run();
72
73 UInt64 startTime = Timer::getTimeMs();
74 int outstanding=0;
75 int sent = 0;
76
77 int count=0;
78 StunMessage stun;
79 StunAtrString username;
80 memset(&username, 0, sizeof(StunAtrString));
81 memset(&stun, 0, sizeof(StunMessage));
82
83 bool verbose = false;
84
85 StunAddress4 stunServer;
86 stunParseServerName( "127.0.0.1", stunServer);
87 stunServer.port = stunPort;
88
89 int sendPort = 25080 + rand()& 0x7fff;
90 int myFd = openPort(sendPort, stunServer.addr , verbose);
91
92 for (count=0; count<runs; ++count)
93 {
94 stunBuildReqSimple(&stun, username, false, false, 1);
95 char buf[STUN_MAX_MESSAGE_SIZE];
96 int len = STUN_MAX_MESSAGE_SIZE;
97 len = stunEncodeMessage( stun, buf, len, username, verbose );
98 sendMessage( myFd, buf, len, stunServer.addr, stunServer.port, verbose );
99
100 StunAddress4 from;
101
102 char msg[STUN_MAX_MESSAGE_SIZE];
103 int msgLen = STUN_MAX_MESSAGE_SIZE;
104 getMessage( myFd,
105 msg,
106 &msgLen,
107 &from.addr,
108 &from.port,verbose );
109
110 StunMessage resp;
111 memset(&resp, 0, sizeof(StunMessage));
112
113 if ( verbose ) clog << "Got a response" << endl;
114 bool ok = stunParseMessage( msg,msgLen, resp,verbose );
115
116 if ( verbose )
117 {
118 clog << "\t ok=" << ok << endl;
119 clog << "\t id=" << resp.msgHdr.id << endl;
120 clog << "\t mappedAddr=" << resp.mappedAddress.ipv4 << endl;
121 clog << "\t changedAddr=" << resp.changedAddress.ipv4 << endl;
122 clog << endl;
123 }
124 }
125
126 InfoLog (<< "Finished " << count << " runs");
127
128 UInt64 elapsed = Timer::getTimeMs() - startTime;
129 if (!invite)
130 {
131 cout << runs << " registrations peformed in " << elapsed << " ms, a rate of "
132 << runs / ((float) elapsed / 1000.0) << " transactions per second.]" << endl;
133 }
134 else
135 {
136 cout << runs << " calls peformed in " << elapsed << " ms, a rate of "
137 << runs / ((float) elapsed / 1000.0) << " calls per second.]" << endl;
138 }
139 cout << "Note: this test runs both sides (client and server)" << endl;
140
141 sthread.shutdown();
142 sthread.join();
143
144 #if defined(HAVE_POPT_H)
145 poptFreeContext(context);
146 #endif
147 return 0;
148 }

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