1 /*
2  * main.cxx
3  *
4  * PWLib application source file for stunclient
5  *
6  * Main program entry point.
7  *
8  * Copyright (c) 2003 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 20385 $
27  * $Author: rjongbloed $
28  * $Date: 2008-06-04 05:40:38 -0500 (Wed, 04 Jun 2008) $
29  */
30 
31 #include <ptlib.h>
32 #include "main.h"
33 #include "version.h"
34 
35 #include <ptclib/pstun.h>
36 
37 
38 PCREATE_PROCESS(StunClient);
39 
40 
41 
StunClient()42 StunClient::StunClient()
43   : PProcess("Equivalence", "stunclient", MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
44 {
45 }
46 
47 
Main()48 void StunClient::Main()
49 {
50   PArgList & args = GetArguments();
51 #if PTRACING
52   args.Parse("t-trace."       "-no-trace."
53              "o-output:"      "-no-output.");
54 
55   PTrace::Initialise(args.GetOptionCount('t'),
56                    args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL,
57                    PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
58 #endif
59 
60   WORD portbase, portmax;
61 
62   switch (args.GetCount()) {
63     case 0 :
64       cout << "usage: stunclient stunserver [ portbase [ portmax ]]\n";
65       return;
66     case 1 :
67       portbase = 0;
68       portmax = 0;
69       break;
70     case 2 :
71       portbase = (WORD)args[1].AsUnsigned();
72       portmax = (WORD)(portbase+9);
73       break;
74     default :
75       portbase = (WORD)args[1].AsUnsigned();
76       portmax = (WORD)args[2].AsUnsigned();
77   }
78 
79   PSTUNClient stun(args[0], portbase, portmax, portbase, portmax);
80   cout << "NAT type: " << stun.GetNatTypeName() << endl;
81 
82   PIPSocket::Address router;
83   if (!stun.GetExternalAddress(router)) {
84     cout << "Could not get router address!" << endl;
85     return;
86   }
87   cout << "Router address: " << router << endl;
88 
89   PUDPSocket * udp;
90   if (!stun.CreateSocket(udp)) {
91     cout << "Cannot create a socket!" << endl;
92     return;
93   }
94 
95   PIPSocket::Address addr;
96   WORD port;
97   udp->GetLocalAddress(addr, port);
98   cout << "Socket local address reported as " << addr << ":" << port << endl;
99 
100   delete udp;
101 
102   PUDPSocket * udp1, * udp2;
103   if (!stun.CreateSocketPair(udp1, udp2)) {
104     cout << "Cannot create socket pair" << endl;
105     return;
106   }
107 
108   udp1->GetLocalAddress(addr, port);
109   cout << "Socket 1 local address reported as " << addr << ":" << port << endl;
110   udp2->GetLocalAddress(addr, port);
111   cout << "Socket 2 local address reported as " << addr << ":" << port << endl;
112 
113   delete udp1;
114   delete udp2;
115 }
116 
117 
118 // End of File ///////////////////////////////////////////////////////////////
119