|
1 /* |
|
2 ============================================================================ |
|
3 Name : Dummy3rdPartyApp.cpp |
|
4 Author : Gurpreet Singh Nagi |
|
5 Copyright : Your copyright notice |
|
6 Description : Exe source file |
|
7 ============================================================================ |
|
8 */ |
|
9 |
|
10 // Include Files |
|
11 |
|
12 #include "Dummy3rdPartyApp.h" |
|
13 #include <e32base.h> |
|
14 #include <e32std.h> |
|
15 #include <e32cons.h> // Console |
|
16 |
|
17 // Constants |
|
18 _LIT(KTextConsoleTitle, "Dummy3rdPartyApp"); |
|
19 _LIT(KTextFailed, " failed, leave code = %d"); |
|
20 _LIT(KTextPressAnyKey, " [press any key]\n"); |
|
21 |
|
22 #define BUF_SIZE 2048 |
|
23 // Global Variables |
|
24 LOCAL_D CConsoleBase* console; // write all messages to this |
|
25 |
|
26 |
|
27 // Local Functions |
|
28 void Dummy3rdPartyApp::InitConn() |
|
29 { |
|
30 if((iStatus = iVirtSocketServ.Connect()) == KErrNone ) |
|
31 console->Printf(_L("Opening Socket Server on Virtual Interface.\n")); |
|
32 |
|
33 if((iStatus = iRealSocketServ.Connect()) == KErrNone ) |
|
34 console->Printf(_L("Opening Socket Server on Real Interface.\n")); |
|
35 |
|
36 if((iStatus = iHookSocketServ.Connect()) == KErrNone ) |
|
37 console->Printf(_L("Opening Socket Server for Hook.\n")); |
|
38 |
|
39 if((iStatus = iVirtConnection.Open(iVirtSocketServ, KAfInet)) == KErrNone ) |
|
40 console->Printf(_L("\nOpening Virtual Connection.")); |
|
41 |
|
42 if((iStatus = iRealConnection.Open(iRealSocketServ, KAfInet)) == KErrNone ) |
|
43 console->Printf(_L("\nOpening Real Connection.\n")); |
|
44 |
|
45 iHookSocket.Open(iHookSocketServ,_L("tun")); |
|
46 |
|
47 } |
|
48 |
|
49 void Dummy3rdPartyApp::StartConn() |
|
50 { |
|
51 iVirtCommDbPref.SetIapId(14); // Dummy Interface |
|
52 iVirtCommDbPref.SetBearerSet(KCommDbBearerVirtual); |
|
53 iVirtCommDbPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt); |
|
54 |
|
55 TInt err = KErrNone; |
|
56 iVirtConnection.Start(iVirtCommDbPref, iStatus); |
|
57 User::WaitForRequest(iStatus); |
|
58 err = iStatus.Int(); |
|
59 if(iStatus == KErrNone) |
|
60 console->Printf(_L("Connection Started on Virtual Interface.\n")); |
|
61 User::LeaveIfError(err); |
|
62 err = KErrNone; |
|
63 //open socket on Virtual interface. |
|
64 iStatus = iVirtSocket.Open(iVirtSocketServ, KAfInet, KSockDatagram, KProtocolInetUdp, iVirtConnection); |
|
65 //if((iStatus = iVirtSocket.Open(iVirtSocketServ, KAfInet, KSockDatagram, KProtocolInetUdp, iVirtConnection)) == KErrNone ) |
|
66 if(iStatus == KErrNone) |
|
67 console->Printf(_L("Socket Opened on Virtual Interface.\n")); |
|
68 |
|
69 iRealCommDbPref.SetIapId(9); //Real Interface i.e. ethernet |
|
70 iRealCommDbPref.SetBearerSet(KCommDbBearerLAN); |
|
71 iRealCommDbPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt); |
|
72 |
|
73 iRealConnection.Start(iRealCommDbPref, iStatus); |
|
74 User::WaitForRequest(iStatus); |
|
75 if(iStatus == KErrNone) |
|
76 console->Printf(_L("Connection Started on Real Interface.\n")); |
|
77 err = iStatus.Int(); |
|
78 //open socket on real interface |
|
79 if((iStatus = iRealSocket.Open(iRealSocketServ, KAfInet, KSockDatagram, KProtocolInetUdp, iRealConnection)) == KErrNone ) |
|
80 console->Printf(_L("Socket Opened on Real Interface.\n")); |
|
81 User::LeaveIfError(err); |
|
82 |
|
83 //set destination port and IP ie. OpenVPN gateway address |
|
84 iRealAddr.SetV4MappedAddress(INET_ADDR(10,225,171,71)); |
|
85 iRealAddr.SetPort(1194); |
|
86 err = KErrNone; |
|
87 //bind local port to socket |
|
88 // To Get any available port from the kernel |
|
89 |
|
90 TInetAddr inetAddrBind; |
|
91 inetAddrBind.SetPort(KInetPortAny); |
|
92 inetAddrBind.SetAddress(KInetAddrAny); |
|
93 iVirtSocket.Bind(inetAddrBind); |
|
94 TUint port1 = iVirtSocket.LocalPort(); |
|
95 |
|
96 TPckgC <TUint> portPckg (port1); |
|
97 iHookSocket.SetOpt(KSolInetIp,KSoTunnelPort,portPckg); |
|
98 |
|
99 console->Printf(_L("Socket bound on Virtual Interface.\n"), err); |
|
100 |
|
101 err = iRealSocket.SetLocalPort(1194); |
|
102 console->Printf(_L("Socket bound on Real Interface.\n"), err); |
|
103 |
|
104 } |
|
105 |
|
106 |
|
107 void Dummy3rdPartyApp::ProcessData() |
|
108 { |
|
109 //Receive data from client on virtual interface |
|
110 iVirtSocket.RecvFrom(iSendBuf, iVirtAddr, NULL, iStatus); |
|
111 User::WaitForRequest(iStatus); |
|
112 |
|
113 if(iStatus == KErrNone) |
|
114 { |
|
115 console->Printf(_L("\nData Received from Virtual Interface: ")); |
|
116 //copy received data to temp buffer and print it |
|
117 //TBuf<BUF_SIZE> buf; |
|
118 //buf.Copy(iSendBuf); |
|
119 //console->Printf(buf); |
|
120 } |
|
121 else |
|
122 console->Printf(_L("\nData receive failed on Virtual Interface.")); |
|
123 |
|
124 //Send data on real interface to OpenVPN Gateway. |
|
125 iRealSocket.SendTo(iSendBuf, iRealAddr, NULL, iStatus); |
|
126 User::WaitForRequest(iStatus); |
|
127 |
|
128 if( iStatus == KErrNone) |
|
129 { |
|
130 console->Printf(_L("\nData Sent on Real interface : ")); |
|
131 |
|
132 //display sent data |
|
133 //TBuf<BUF_SIZE> buf; |
|
134 //buf.Copy(iSendBuf); |
|
135 //console->Printf(buf); |
|
136 } |
|
137 else |
|
138 console->Printf(_L("\nData send failed on Real Interface")); |
|
139 |
|
140 //Receive data from from real interface |
|
141 iRealSocket.RecvFrom(iRecvBuf, iRealAddr, NULL, iStatus); |
|
142 User::WaitForRequest(iStatus); |
|
143 |
|
144 if(iStatus == KErrNone) |
|
145 { |
|
146 console->Printf(_L("\nData Received from Real Interface: ")); |
|
147 //copy received data to temp buffer and print it |
|
148 //TBuf<BUF_SIZE> buf; |
|
149 //buf.Copy(iRecvBuf); |
|
150 //console->Printf(buf); |
|
151 } |
|
152 else |
|
153 console->Printf(_L("\nData receive failed on Real Interface")); |
|
154 |
|
155 //Send data to virtual interface. |
|
156 iVirtSocket.SendTo(iRecvBuf, iVirtAddr, NULL, iStatus); |
|
157 User::WaitForRequest(iStatus); |
|
158 |
|
159 if( iStatus == KErrNone) |
|
160 { |
|
161 console->Printf(_L("\nData Sent on Virtual interface : ")); |
|
162 |
|
163 //display sent data |
|
164 //TBuf<BUF_SIZE> buf; |
|
165 //buf.Copy(iRecvBuf); |
|
166 //console->Printf(buf); |
|
167 } |
|
168 else |
|
169 console->Printf(_L("\nData send failed on Virtual Interface")); |
|
170 } |
|
171 |
|
172 void Dummy3rdPartyApp::CloseConn() |
|
173 { |
|
174 //Close all handles. |
|
175 iVirtSocket.Close(); |
|
176 iRealSocket.Close(); |
|
177 iHookSocket.Close(); |
|
178 |
|
179 iVirtConnection.Close(); |
|
180 iRealConnection.Close(); |
|
181 iVirtSocketServ.Close(); |
|
182 iRealSocketServ.Close(); |
|
183 iHookSocketServ.Close(); |
|
184 } |
|
185 |
|
186 LOCAL_C void MainL() |
|
187 { |
|
188 Dummy3rdPartyApp obj; |
|
189 |
|
190 //intializing connection and socket |
|
191 obj.InitConn(); |
|
192 //start the connection |
|
193 obj.StartConn(); |
|
194 |
|
195 while(1) |
|
196 { |
|
197 obj.ProcessData(); |
|
198 } |
|
199 //close connection, subconnection, socket |
|
200 //TODO: how to explicitly close the loop? |
|
201 // One possible solution is running a timer. |
|
202 obj.CloseConn(); |
|
203 } |
|
204 |
|
205 LOCAL_C void DoStartL() |
|
206 { |
|
207 // Create active scheduler (to run active objects) |
|
208 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler(); |
|
209 CleanupStack::PushL(scheduler); |
|
210 CActiveScheduler::Install(scheduler); |
|
211 |
|
212 MainL(); |
|
213 // Delete active scheduler |
|
214 CleanupStack::PopAndDestroy(scheduler); |
|
215 } |
|
216 |
|
217 // Global Functions |
|
218 |
|
219 GLDEF_C TInt E32Main() |
|
220 { |
|
221 // Create cleanup stack |
|
222 __UHEAP_MARK; |
|
223 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
224 |
|
225 // Create output console |
|
226 TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize( |
|
227 KConsFullScreen, KConsFullScreen))); |
|
228 if (createError) |
|
229 return createError; |
|
230 |
|
231 // Run application code inside TRAP harness, wait keypress when terminated |
|
232 TRAPD(mainError, DoStartL()); |
|
233 if (mainError) |
|
234 console->Printf(KTextFailed, mainError); |
|
235 console->Printf(KTextPressAnyKey); |
|
236 console->Getch(); |
|
237 |
|
238 delete console; |
|
239 delete cleanup; |
|
240 __UHEAP_MARKEND; |
|
241 return KErrNone; |
|
242 } |