| 1 |
#include "protoApp.h" |
| 2 |
#include "protoNet.h" |
| 3 |
#include "norp.h" |
| 4 |
#include "norpVersion.h" |
| 5 |
|
| 6 |
#include <stdio.h> // for stderr |
| 7 |
#include <stdlib.h> // for atoi() |
| 8 |
|
| 9 |
class NorpApp : public ProtoApp |
| 10 |
{ |
| 11 |
public: |
| 12 |
NorpApp(); |
| 13 |
~NorpApp(); |
| 14 |
|
| 15 |
// Required verrides from ProtoApp base |
| 16 |
bool OnStartup(int argc, const char*const* argv); |
| 17 |
bool ProcessCommands(int argc, const char*const* argv); |
| 18 |
void OnShutdown(); |
| 19 |
|
| 20 |
private: |
| 21 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 22 |
static CmdType GetCmdType(const char* string); |
| 23 |
bool OnCommand(const char* cmd, const char* val); |
| 24 |
void Usage(); |
| 25 |
|
| 26 |
static const char* const CMD_LIST[]; |
| 27 |
|
| 28 |
Norp norp; |
| 29 |
bool norm_enable; |
| 30 |
|
| 31 |
}; // end class NorpApp |
| 32 |
|
| 33 |
// Our application instance |
| 34 |
PROTO_INSTANTIATE_APP(NorpApp) |
| 35 |
|
| 36 |
|
| 37 |
NorpApp::NorpApp() |
| 38 |
: norp(dispatcher), norm_enable(true) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
NorpApp::~NorpApp() |
| 43 |
{ |
| 44 |
norp.StopServer(); |
| 45 |
} |
| 46 |
|
| 47 |
void NorpApp::Usage() |
| 48 |
{ |
| 49 |
fprintf(stderr, "Usage: norp [interface <ifaceName>][address <publicAddr>][sport <socksPort>][port <norpPort>]\n" |
| 50 |
" [norm {on|off}][id <normId>][nport <normPort>][cce | ccl | rate <bits/sec>]\n" |
| 51 |
" [limit <bits/sec>][persist <seconds>][segment <segmentSize>]\n" |
| 52 |
" [correspondent <remoteNorpAddr>][forward <tcpPort>,<destAddr>/<destPort>[,<remoteNorpAddr>]]\n" |
| 53 |
" [version][debug <level>][trace][dlog <debugLog>][lport <localNorpPort>][rport <remoteNorpPort>]\n"); |
| 54 |
} |
| 55 |
|
| 56 |
const char* const NorpApp::CMD_LIST[] = |
| 57 |
{ |
| 58 |
"+interface", // "public facing" interface (its address is used for bindings) |
| 59 |
"+address", // explicit "public facing" address to use for bindings |
| 60 |
"+sport", // SOCKS server port (7000 by default) |
| 61 |
"+port", // NORP UDP signaling port number (7001 by default) |
| 62 |
"+norm", // "on" or "off" to enable/disable NORM proxying ("on" by default) |
| 63 |
"+id", // set local NormNodeId (auto set from IP address by default) |
| 64 |
"+nport", // NORM port (UDP) (7002 by default) |
| 65 |
"-cce", // Use NORM-CCE instead of NORM-CC |
| 66 |
"-ccl", // Use NORM-CCL instead of NORM-CC |
| 67 |
"+rate", // set fixed transmit rate (no congestion control) |
| 68 |
"+limit", // set _cumulative_ NORP transmit rate limit |
| 69 |
"+segment", // Set NORM packet segment size (impacts MTU of NORM packets, UDP packets w/ (40 + <segmentSize>) bytes of payload) |
| 70 |
"+persist", // <seconds> how long to persist NORM data delivery to receiver after TCP socket closure |
| 71 |
"+debug", // set debug level |
| 72 |
"-trace", // enables NORM protocol packet send/recv trace in debug output |
| 73 |
"+dlog", // specify a file for debug logging |
| 74 |
"+lport", // "local" NORP port (for loopback debugging) |
| 75 |
"+rport", // "remote" NORP port (for loopback debugging) |
| 76 |
"+forward", // <port>,<dstAddr>/<dstPort>[,<remoteNorpAddr>] to set up a preset proxy TCP listener |
| 77 |
"+correspondent", // <remoteNorpAddr> to route proxied connections through remote NORP proxy at given addr |
| 78 |
"-version", // print version number and exit |
| 79 |
NULL |
| 80 |
}; |
| 81 |
|
| 82 |
NorpApp::CmdType NorpApp::GetCmdType(const char* cmd) |
| 83 |
{ |
| 84 |
if (!cmd) return CMD_INVALID; |
| 85 |
unsigned int len = strlen(cmd); |
| 86 |
bool matched = false; |
| 87 |
CmdType type = CMD_INVALID; |
| 88 |
const char* const* nextCmd = CMD_LIST; |
| 89 |
while (*nextCmd) |
| 90 |
{ |
| 91 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 92 |
{ |
| 93 |
if (matched) |
| 94 |
{ |
| 95 |
// ambiguous command (command should match only once) |
| 96 |
return CMD_INVALID; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
matched = true; |
| 101 |
if ('+' == *nextCmd[0]) |
| 102 |
type = CMD_ARG; |
| 103 |
else |
| 104 |
type = CMD_NOARG; |
| 105 |
} |
| 106 |
} |
| 107 |
nextCmd++; |
| 108 |
} |
| 109 |
return type; |
| 110 |
} // end NorpApp::GetCmdType() |
| 111 |
|
| 112 |
bool NorpApp::OnStartup(int argc, const char*const* argv) |
| 113 |
{ |
| 114 |
if (!ProcessCommands(argc, argv)) |
| 115 |
{ |
| 116 |
PLOG(PL_ERROR, "NorpApp::OnStartup() error: bad command line\n"); |
| 117 |
Usage(); |
| 118 |
return false; |
| 119 |
} |
| 120 |
// If a proxy address wasn't set, use "default" local address for host |
| 121 |
if (!norp.GetProxyAddress().IsValid()) |
| 122 |
{ |
| 123 |
ProtoAddress theAddr; |
| 124 |
if (theAddr.ResolveLocalAddress()) |
| 125 |
norp.SetProxyAddress(theAddr); |
| 126 |
else |
| 127 |
PLOG(PL_WARN, "NorpApp::OnStartup() warning: proxy address undetermined!\n"); |
| 128 |
} |
| 129 |
PLOG(PL_INFO, "norp: starting proxy with address %s\n", norp.GetProxyAddress().GetHostString()); |
| 130 |
|
| 131 |
if (!norp.StartServer(norm_enable)) |
| 132 |
{ |
| 133 |
PLOG(PL_ERROR, "NorpApp::OnStartup() error: norp server startup failure!\n"); |
| 134 |
return false; |
| 135 |
} |
| 136 |
return true; |
| 137 |
} // end NorpApp::OnStartup() |
| 138 |
|
| 139 |
void NorpApp::OnShutdown() |
| 140 |
{ |
| 141 |
norp.StopServer(); |
| 142 |
CloseDebugLog(); |
| 143 |
} // end Norp::OnShutdown() |
| 144 |
|
| 145 |
bool NorpApp::ProcessCommands(int argc, const char*const* argv) |
| 146 |
{ |
| 147 |
// Dispatch command-line commands to our OnCommand() method |
| 148 |
int i = 1; |
| 149 |
while ( i < argc) |
| 150 |
{ |
| 151 |
// Is it a class NorpApp command? |
| 152 |
switch (GetCmdType(argv[i])) |
| 153 |
{ |
| 154 |
case CMD_INVALID: |
| 155 |
{ |
| 156 |
PLOG(PL_ERROR, "NorpApp::ProcessCommands() Invalid command:%s\n", argv[i]); |
| 157 |
return false; |
| 158 |
} |
| 159 |
case CMD_NOARG: |
| 160 |
if (!OnCommand(argv[i], NULL)) |
| 161 |
{ |
| 162 |
PLOG(PL_ERROR, "NorpApp::ProcessCommands() ProcessCommand(%s) error\n", argv[i]); |
| 163 |
return false; |
| 164 |
} |
| 165 |
i++; |
| 166 |
break; |
| 167 |
case CMD_ARG: |
| 168 |
if (!OnCommand(argv[i], argv[i+1])) |
| 169 |
{ |
| 170 |
PLOG(PL_ERROR, "NorpApp::ProcessCommands() ProcessCommand(%s, %s) error\n", argv[i], argv[i+1]); |
| 171 |
return false; |
| 172 |
} |
| 173 |
i += 2; |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
return true; |
| 178 |
} // end NorpApp::ProcessCommands() |
| 179 |
|
| 180 |
bool NorpApp::OnCommand(const char* cmd, const char* val) |
| 181 |
{ |
| 182 |
// (TBD) move command processing into Mgen class ??? |
| 183 |
CmdType type = GetCmdType(cmd); |
| 184 |
ASSERT(CMD_INVALID != type); |
| 185 |
unsigned int len = strlen(cmd); |
| 186 |
if ((CMD_ARG == type) && !val) |
| 187 |
{ |
| 188 |
PLOG(PL_ERROR, "NorpApp::ProcessCommand(%s) missing argument\n", cmd); |
| 189 |
return false; |
| 190 |
} |
| 191 |
else if (!strncmp("version", cmd, len)) |
| 192 |
{ |
| 193 |
fprintf(stdout, "norp version %s\n", NORP_VERSION); |
| 194 |
exit(0); |
| 195 |
} |
| 196 |
else if (!strncmp("debug", cmd, len)) |
| 197 |
{ |
| 198 |
SetDebugLevel(atoi(val)); |
| 199 |
} |
| 200 |
else if (!strncmp("trace", cmd, len)) |
| 201 |
{ |
| 202 |
norp.SetNormTrace(true); |
| 203 |
} |
| 204 |
else if (!strncmp("dlog", cmd, len)) |
| 205 |
{ |
| 206 |
OpenDebugLog(val); |
| 207 |
} |
| 208 |
else if (!strncmp("interface", cmd, len)) |
| 209 |
{ |
| 210 |
ProtoAddress ifaceAddr; |
| 211 |
if (!ProtoNet::GetInterfaceAddress(val, ProtoAddress::IPv4, ifaceAddr)) |
| 212 |
{ |
| 213 |
PLOG(PL_ERROR, "NorpApp::OnCommand(interface) error: unable to get address for interface \"%s\"\n", val); |
| 214 |
return false; |
| 215 |
} |
| 216 |
norp.SetProxyAddress(ifaceAddr); |
| 217 |
} |
| 218 |
else if (!strncmp("address", cmd, len)) |
| 219 |
{ |
| 220 |
ProtoAddress theAddr; |
| 221 |
if (!theAddr.ResolveFromString(val)) |
| 222 |
{ |
| 223 |
PLOG(PL_ERROR, "NorpApp::OnCommand(interface) error: invalid address \"%s\"\n", val); |
| 224 |
return false; |
| 225 |
} |
| 226 |
norp.SetProxyAddress(theAddr); |
| 227 |
} |
| 228 |
else if (!strncmp("sport", cmd, len)) |
| 229 |
{ |
| 230 |
norp.SetSocksPort(atoi(val)); |
| 231 |
} |
| 232 |
else if (!strncmp("port", cmd, len)) |
| 233 |
{ |
| 234 |
UINT16 norpPort = atoi(val); |
| 235 |
norp.SetLocalNorpPort(norpPort); |
| 236 |
norp.SetRemoteNorpPort(norpPort); |
| 237 |
} |
| 238 |
else if (!strncmp("norm", cmd, len)) |
| 239 |
{ |
| 240 |
if (0 == strcmp("on", val)) |
| 241 |
norm_enable = true; |
| 242 |
else |
| 243 |
norm_enable = false; |
| 244 |
} |
| 245 |
else if (!strncmp("id", cmd, len)) |
| 246 |
{ |
| 247 |
norp.SetNormNodeId(atoi(val)); |
| 248 |
} |
| 249 |
else if (!strncmp("nport", cmd, len)) |
| 250 |
{ |
| 251 |
norp.SetNormPort(atoi(val)); |
| 252 |
} |
| 253 |
else if (!strncmp("cce", cmd, len)) |
| 254 |
{ |
| 255 |
norp.SetNormCC(Norp::NORM_CCE); |
| 256 |
} |
| 257 |
else if (!strncmp("ccl", cmd, len)) |
| 258 |
{ |
| 259 |
norp.SetNormCC(Norp::NORM_CCL); |
| 260 |
} |
| 261 |
else if (!strncmp("rate", cmd, len)) |
| 262 |
{ |
| 263 |
double txRate; |
| 264 |
if (1 != sscanf(val, "%lf", &txRate)) |
| 265 |
{ |
| 266 |
PLOG(PL_ERROR, "NorpApp::OnCommand(rate) error: invalid rate \"%s\" bps\n", val); |
| 267 |
return false; |
| 268 |
} |
| 269 |
norp.SetNormCC(Norp::NORM_FIXED); |
| 270 |
norp.SetNormTxRate(txRate); |
| 271 |
} |
| 272 |
else if (!strncmp("limit", cmd, len)) |
| 273 |
{ |
| 274 |
double txLimit; |
| 275 |
if (1 != sscanf(val, "%lf", &txLimit)) |
| 276 |
{ |
| 277 |
PLOG(PL_ERROR, "NorpApp::OnCommand(limit) error: invalid limit rate \"%s\" bps\n", val); |
| 278 |
return false; |
| 279 |
} |
| 280 |
norp.SetNormTxLimit(txLimit); |
| 281 |
} |
| 282 |
else if (!strncmp("segment", cmd, len)) |
| 283 |
{ |
| 284 |
UINT16 segmentSize; |
| 285 |
if (1 != sscanf(val, "%hu", &segmentSize)) |
| 286 |
{ |
| 287 |
PLOG(PL_ERROR, "NorpApp::OnCommand(segment) error: invalid segment size \"%s\" bps\n", val); |
| 288 |
return false; |
| 289 |
} |
| 290 |
norp.SetNormSegmentSize(segmentSize); |
| 291 |
} |
| 292 |
else if (!strncmp("lport", cmd, len)) |
| 293 |
{ |
| 294 |
norp.SetLocalNorpPort(atoi(val)); |
| 295 |
} |
| 296 |
else if (!strncmp("rport", cmd, len)) |
| 297 |
{ |
| 298 |
norp.SetRemoteNorpPort(atoi(val)); |
| 299 |
} |
| 300 |
else if (!strncmp("forward", cmd, len)) |
| 301 |
{ |
| 302 |
// <port>,<dstAddr>/<dstPort>[,<remoteNorpAddr>] |
| 303 |
char* text = new char[strlen(val) + 1]; // copy so we can slice and dice |
| 304 |
if (NULL == text) |
| 305 |
{ |
| 306 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s) new char[] error: %s\n", cmd, GetErrorString()); |
| 307 |
return false; |
| 308 |
} |
| 309 |
strcpy(text, val); |
| 310 |
char* dstAddrPtr = strchr(text, ','); |
| 311 |
if (NULL == dstAddrPtr) |
| 312 |
{ |
| 313 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: missing <dstAddr>!\n", cmd, val); |
| 314 |
delete[] text; |
| 315 |
return false; |
| 316 |
} |
| 317 |
*dstAddrPtr++ = '\0'; |
| 318 |
char* dstPortPtr = strchr(dstAddrPtr, '/'); |
| 319 |
if (NULL == dstPortPtr) |
| 320 |
{ |
| 321 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: missing <dstPort>!\n", cmd, val); |
| 322 |
delete[] text; |
| 323 |
return false; |
| 324 |
} |
| 325 |
UINT16 tcpPort; |
| 326 |
int result = sscanf(text, "%hu", &tcpPort); |
| 327 |
if ((1 != result) || (0 == tcpPort)) |
| 328 |
{ |
| 329 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: invalid <port> \"%s\"!\n", cmd, val, text); |
| 330 |
delete[] text; |
| 331 |
return false; |
| 332 |
} |
| 333 |
*dstPortPtr++ = '\0'; |
| 334 |
char* norpAddrPtr = strchr(dstPortPtr, ','); |
| 335 |
if (NULL != norpAddrPtr) *norpAddrPtr++ = '\0'; |
| 336 |
ProtoAddress dstAddr; |
| 337 |
if (!dstAddr.ResolveFromString(dstAddrPtr)) |
| 338 |
{ |
| 339 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: invalid <dstAddr> \"%s\"!\n", cmd, val, dstAddrPtr); |
| 340 |
delete[] text; |
| 341 |
return false; |
| 342 |
} |
| 343 |
UINT16 dstPort; |
| 344 |
result = sscanf(dstPortPtr, "%hu", &dstPort); |
| 345 |
if ((1 != result) || (0 == dstPort)) |
| 346 |
{ |
| 347 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: invalid <dstPort> \"%s\"!\n", cmd, val, text); |
| 348 |
delete[] text; |
| 349 |
return false; |
| 350 |
} |
| 351 |
dstAddr.SetPort(dstPort); |
| 352 |
ProtoAddress norpAddr; |
| 353 |
if (NULL != norpAddrPtr) |
| 354 |
{ |
| 355 |
if (!norpAddr.ResolveFromString(norpAddrPtr)) |
| 356 |
{ |
| 357 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: invalid <norpAddr> \"%s\"!\n", cmd, val, norpAddrPtr); |
| 358 |
delete[] text; |
| 359 |
return false; |
| 360 |
} |
| 361 |
} |
| 362 |
if (!norp.AddPreset(tcpPort, dstAddr, norpAddr)) |
| 363 |
{ |
| 364 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: unable to add preset!\n", cmd, val); |
| 365 |
delete[] text; |
| 366 |
return false; |
| 367 |
} |
| 368 |
delete[] text; |
| 369 |
} |
| 370 |
else if (!strncmp("correspondent", cmd, len)) |
| 371 |
{ |
| 372 |
// <remoteNorpAddr> |
| 373 |
ProtoAddress norpAddr; |
| 374 |
if (!norpAddr.ResolveFromString(val)) |
| 375 |
{ |
| 376 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s, %s) error: invalid <remoteNorpAddr>!\n", cmd, val); |
| 377 |
return false; |
| 378 |
} |
| 379 |
norp.SetRemoteNorpAddress(norpAddr); |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
PLOG(PL_ERROR, "NorpApp::OnCommand(%s) error: unimplemented command!\n", cmd); |
| 384 |
return false; |
| 385 |
} |
| 386 |
return true; |
| 387 |
} // end NorpApp::OnCommand() |
| 388 |
|
| 389 |
|
| 390 |
|