| 1 |
|
| 2 |
#include "norp.h" |
| 3 |
#include "protoNet.h" |
| 4 |
#include "protoDebug.h" |
| 5 |
|
| 6 |
|
| 7 |
// Note this "PortPool" was a simple helper class |
| 8 |
// for a test where we needed to traverse a |
| 9 |
// NAT / firewall. We may _later_ refine this |
| 10 |
// as an optional feature where "norp" can be |
| 11 |
// configured to use a specific set of port |
| 12 |
// numbers for NORM traffic. |
| 13 |
|
| 14 |
// Uncomment this to enable new NORM port handling to improve NAT compatibility |
| 15 |
#define NEW_PORT |
| 16 |
|
| 17 |
PortPool::PortPool(UINT16 basePort) |
| 18 |
: base_port(basePort) |
| 19 |
{ |
| 20 |
memset(port_array, 0, 50*sizeof(bool)); |
| 21 |
} |
| 22 |
|
| 23 |
UINT16 PortPool::GrabPort() |
| 24 |
{ |
| 25 |
for (UINT16 i = 0; i < 50; i++) |
| 26 |
{ |
| 27 |
if (!port_array[i]) |
| 28 |
{ |
| 29 |
port_array[i] = true; |
| 30 |
return (base_port + i); |
| 31 |
} |
| 32 |
} |
| 33 |
return 0; // no port was available |
| 34 |
} // end PortPool::GrabPort() |
| 35 |
|
| 36 |
void PortPool::ReleasePort(UINT16 thePort) |
| 37 |
{ |
| 38 |
int index = thePort - base_port; |
| 39 |
if ((index < 50) && (index >=0)) |
| 40 |
port_array[index] = false; |
| 41 |
} // end PortPool::ReleasePort() |
| 42 |
|
| 43 |
NorpMsg::NorpMsg(UINT32* bufferPtr, unsigned int numBytes, bool initFromBuffer, bool freeOnDestruct) |
| 44 |
: ProtoPkt(bufferPtr, numBytes, freeOnDestruct) |
| 45 |
{ |
| 46 |
if (initFromBuffer) InitFromBuffer(numBytes); |
| 47 |
} |
| 48 |
|
| 49 |
NorpMsg::~NorpMsg() |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
const double NorpSession::NORP_RTT_MIN = 1.0e-03; |
| 54 |
const double NorpSession::NORP_RTT_MAX = 5.0; // TBD - assess these values (make runtime configurable?) |
| 55 |
const double NorpSession::NORP_RTT_DEFAULT = 1.0;//e-01; // 100 msec default |
| 56 |
const double Norp::DEFAULT_TX_RATE = 1.0e+06; |
| 57 |
const double Norp::DEFAULT_PERSIST_INTERVAL = 60.0; // times out unsuccessful NORM delivery attempt after successive ACK_FAILURE |
| 58 |
|
| 59 |
|
| 60 |
NorpSession::NorpSession(Norp& theController, UINT16 sessionId, NormNodeId originatorId) |
| 61 |
: controller(theController), is_preset(false), socks_state(SOCKS_IDLE), |
| 62 |
socks_client_socket(ProtoSocket::TCP), socks_remote_socket(ProtoSocket::TCP), |
| 63 |
udp_relay_socket(ProtoSocket::UDP), |
| 64 |
client_pending(0), client_index(0), remote_pending(0), remote_index(0), |
| 65 |
norm_enable(true), norp_tx_socket(ProtoSocket::UDP), norp_rtt_estimate(NORP_RTT_DEFAULT), |
| 66 |
norm_session(NORM_SESSION_INVALID), norm_tx_stream(NORM_OBJECT_INVALID), |
| 67 |
norm_rx_stream(NORM_OBJECT_INVALID), norm_rx_pending(false), norm_sender_heard(false), |
| 68 |
persist_interval(Norp::DEFAULT_PERSIST_INTERVAL), persist_start_time(0, 0), |
| 69 |
norm_rate_min(-1.0), norm_rate_max(-1.0), |
| 70 |
norm_segment_size(0), norm_stream_buffer_max(0), norm_stream_buffer_count(0), |
| 71 |
norm_stream_bytes_remain(0), norm_watermark_pending(false) |
| 72 |
{ |
| 73 |
memset(&session_id, 0, sizeof(Moniker)); |
| 74 |
session_id.originator = originatorId; |
| 75 |
session_id.identifier = sessionId; |
| 76 |
socks_client_socket.SetNotifier(&controller.GetSocketNotifier()); |
| 77 |
//socks_client_socket.SetListener(this, &NorpSession::OnSocksClientEvent); |
| 78 |
socks_remote_socket.SetNotifier(&controller.GetSocketNotifier()); |
| 79 |
socks_remote_socket.SetListener(this, &NorpSession::OnSocksRemoteEvent); |
| 80 |
udp_relay_socket.SetNotifier(&controller.GetSocketNotifier()); |
| 81 |
udp_relay_socket.SetListener(this, &NorpSession::OnUdpRelayEvent); |
| 82 |
|
| 83 |
norp_tx_socket.SetUserData(this); |
| 84 |
norp_tx_socket.SetNotifier(&controller.GetSocketNotifier()); |
| 85 |
norp_tx_socket.SetListener(&controller, &Norp::OnNorpSocketEvent); |
| 86 |
|
| 87 |
norp_msg_timer.SetListener(this, &NorpSession::OnNorpMsgTimeout); |
| 88 |
norp_msg_timer.SetInterval(controller.GetInitialRtt()); |
| 89 |
norp_msg_timer.SetRepeat(20); |
| 90 |
|
| 91 |
norp_msg.AttachBuffer(norp_msg_buffer, NORP_BUFFER_SIZE, false); |
| 92 |
|
| 93 |
close_timer.SetListener(this, &NorpSession::OnCloseTimeout); |
| 94 |
close_timer.SetInterval(0.0); |
| 95 |
close_timer.SetRepeat(0); // one-shot timer |
| 96 |
} |
| 97 |
|
| 98 |
NorpSession::~NorpSession() |
| 99 |
{ |
| 100 |
socks_state = SOCKS_VOID; |
| 101 |
Close(); |
| 102 |
} |
| 103 |
|
| 104 |
void NorpSession::SetNormRateBounds(double rateMin, double rateMax) |
| 105 |
{ |
| 106 |
norm_rate_min = rateMin; |
| 107 |
norm_rate_max = rateMax; |
| 108 |
if (NORM_SESSION_INVALID != norm_session) |
| 109 |
{ |
| 110 |
NormSetTxRateBounds(norm_session, rateMin, rateMax); |
| 111 |
if (Norp::NORM_FIXED == controller.GetNormCC()) |
| 112 |
{ |
| 113 |
// cumulative rate limit may override fixed session rate |
| 114 |
// (i.e. to share pipe among multiple flows) |
| 115 |
if ((rateMin >= 0.0) && (rateMin < controller.GetNormTxRate())) |
| 116 |
NormSetTxRate(norm_session, rateMin); |
| 117 |
else |
| 118 |
NormSetTxRate(norm_session, controller.GetNormTxRate()); |
| 119 |
} |
| 120 |
} |
| 121 |
} // end NorpSession::SetNormRateBounds() |
| 122 |
|
| 123 |
NormNodeId NorpSession::GetNodeId() const |
| 124 |
{ |
| 125 |
return controller.GetNormNodeId(); |
| 126 |
} // end NorpSession::GetNodeId() |
| 127 |
|
| 128 |
void NorpSession::ActivateTimer(ProtoTimer& theTimer) |
| 129 |
{ |
| 130 |
controller.ActivateTimer(theTimer); |
| 131 |
} // end NorpSession::ActivateTimer() |
| 132 |
|
| 133 |
bool NorpSession::AcceptClientConnection(ProtoSocket& serverSocket, bool normEnable) |
| 134 |
{ |
| 135 |
ASSERT(socks_client_socket.IsClosed()); |
| 136 |
if (!serverSocket.Accept(&socks_client_socket)) |
| 137 |
{ |
| 138 |
PLOG(PL_ERROR, "NorpSession::AcceptClientConnection() error: server socket accept failure\n"); |
| 139 |
return false; |
| 140 |
} |
| 141 |
// Assume the same notifier (for now) |
| 142 |
socks_client_socket.SetListener(this, &NorpSession::OnSocksClientEvent); |
| 143 |
norm_enable = normEnable; |
| 144 |
// Init our socks_state |
| 145 |
socks_state = SOCKS_GET_AUTH_REQ; |
| 146 |
// Init client/remote buffer indices |
| 147 |
client_pending = client_index = 0; |
| 148 |
remote_pending = remote_index = 0; |
| 149 |
return true; |
| 150 |
} // end NorpSession::AcceptClientConnection() |
| 151 |
|
| 152 |
bool NorpSession::AcceptPresetClientConnection(NorpPreset& preset, bool normEnable) |
| 153 |
{ |
| 154 |
ASSERT(socks_client_socket.IsClosed()); |
| 155 |
if (!preset.AccessServerSocket().Accept(&socks_client_socket)) |
| 156 |
{ |
| 157 |
PLOG(PL_ERROR, "NorpSession::AcceptPresetClientConnection() error: server socket accept failure\n"); |
| 158 |
return false; |
| 159 |
} |
| 160 |
is_preset = true; |
| 161 |
// Assume the same notifier (for now) |
| 162 |
socks_client_socket.SetListener(this, &NorpSession::OnSocksClientEvent); |
| 163 |
|
| 164 |
norm_enable = normEnable; |
| 165 |
// Jump-start our socks_state to |
| 166 |
socks_state = SOCKS_GET_REQUEST; |
| 167 |
// Init client/remote buffer indices |
| 168 |
client_pending = client_index = 0; |
| 169 |
remote_pending = remote_index = 0; |
| 170 |
|
| 171 |
if (norm_enable) |
| 172 |
{ |
| 173 |
// Make and send SOCKS request to remote peer NORP server (peer will TCP connect to endpoint and reply via NORM) |
| 174 |
ProtoPktSOCKS::Request request(client_buffer, 32, false); |
| 175 |
request.SetVersion(5); |
| 176 |
request.SetCommand(ProtoPktSOCKS::Request::CONNECT); |
| 177 |
request.SetAddress(preset.GetDstAddr()); |
| 178 |
return PutRemoteRequest(request, preset.GetNorpAddr()); |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
return ConnectToRemote(preset.GetDstAddr()); // do direct TCP connect to remote endpoint |
| 183 |
} |
| 184 |
|
| 185 |
return true; |
| 186 |
} // end NorpSession::AcceptPresetSession() |
| 187 |
|
| 188 |
void NorpSession::Shutdown() |
| 189 |
{ |
| 190 |
// Initiate hard session shutdown, informing remote |
| 191 |
if (IsRemoteSession()) |
| 192 |
{ |
| 193 |
norp_msg.Init(); |
| 194 |
if (IsRemoteOriginator()) |
| 195 |
norp_msg.SetType(NorpMsg::ORIG_END); |
| 196 |
else |
| 197 |
norp_msg.SetType(NorpMsg::CORR_END); |
| 198 |
norp_msg.SetSessionId(GetSessionId()); |
| 199 |
norp_msg.SetNodeId(controller.GetNormNodeId()); |
| 200 |
// The remaining fields don't matter, so we don't send them! |
| 201 |
norp_remote_addr.SetPort(controller.GetNorpPort()); |
| 202 |
if (!controller.SendMessage(norp_msg, norp_remote_addr)) |
| 203 |
PLOG(PL_ERROR, "NorpSession::Shutdown() warning: NORP message transmission failed (will try again)!\n"); |
| 204 |
// Set timer to repeat until ACK_END is received |
| 205 |
if (norp_msg_timer.IsActive()) norp_msg_timer.Deactivate(); |
| 206 |
norp_msg_timer.SetInterval(2.0*norp_rtt_estimate); // use most recent RTT estimate |
| 207 |
ActivateTimer(norp_msg_timer); |
| 208 |
// Here we use ProtoSocket::Shutdown() (sockets get a DISCONNECT event upon shutdown completion) |
| 209 |
// TBD - we may need to do something with input/output notification here? |
| 210 |
if (socks_client_socket.IsOpen()) socks_client_socket.Close(); |
| 211 |
if (socks_remote_socket.IsOpen()) socks_remote_socket.Close(); |
| 212 |
socks_state = SOCKS_SHUTDOWN; |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
// No remote, so just do a hard Close() |
| 217 |
Close(); |
| 218 |
} |
| 219 |
} // end NorpSession::Shutdown() |
| 220 |
|
| 221 |
// This returns "true" when the conditions of shutdown completion are met |
| 222 |
bool NorpSession::ShutdownComplete() const |
| 223 |
{ |
| 224 |
if (socks_remote_socket.IsOpen()) return false; |
| 225 |
if (socks_client_socket.IsOpen()) return false; |
| 226 |
if (NORM_OBJECT_INVALID != norm_tx_stream) return false; |
| 227 |
if (NORM_OBJECT_INVALID != norm_rx_stream) return false; |
| 228 |
return true; |
| 229 |
} // end NorpSession::ShutdownComplete() |
| 230 |
|
| 231 |
void NorpSession::Close() |
| 232 |
{ |
| 233 |
if (NORM_SESSION_INVALID != norm_session) |
| 234 |
{ |
| 235 |
NormDestroySession(norm_session); |
| 236 |
norm_session = NORM_SESSION_INVALID; |
| 237 |
} |
| 238 |
norp_tx_socket.Close(); |
| 239 |
socks_client_socket.Close(); |
| 240 |
socks_remote_socket.Close(); |
| 241 |
udp_relay_socket.Close(); |
| 242 |
client_pending = client_index = 0; |
| 243 |
remote_pending = remote_index = 0; |
| 244 |
if (SOCKS_VOID != socks_state) |
| 245 |
{ |
| 246 |
// We make the state void here because controller deletes us! |
| 247 |
socks_state = SOCKS_VOID; |
| 248 |
controller.OnSessionClose(*this); |
| 249 |
} |
| 250 |
} // end NorpSession::Close() |
| 251 |
|
| 252 |
bool NorpSession::OnCloseTimeout(ProtoTimer& /*theTimer*/) |
| 253 |
{ |
| 254 |
close_timer.Deactivate(); |
| 255 |
controller.OnSessionClose(*this); |
| 256 |
return false; // because we've been deleted |
| 257 |
} // end NorpSession::OnCloseTimeout() |
| 258 |
|
| 259 |
|
| 260 |
void NorpSession::OnSocksClientEvent(ProtoSocket& theSocket, |
| 261 |
ProtoSocket::Event theEvent) |
| 262 |
{ |
| 263 |
PLOG(PL_DETAIL, "NorpSession::OnSocksClientEvent("); |
| 264 |
switch (theEvent) |
| 265 |
{ |
| 266 |
case ProtoSocket::INVALID_EVENT: |
| 267 |
PLOG(PL_DETAIL, "INVALID_EVENT) ...\n"); |
| 268 |
break; |
| 269 |
case ProtoSocket::CONNECT: |
| 270 |
PLOG(PL_DETAIL, "CONNECT) ...\n"); |
| 271 |
break; |
| 272 |
case ProtoSocket::ACCEPT: |
| 273 |
PLOG(PL_DETAIL, "ACCEPT) ...\n"); // should never happen (was accepted on server socket) unless remote BIND |
| 274 |
break; |
| 275 |
case ProtoSocket::SEND: |
| 276 |
PLOG(PL_DETAIL, "SEND) ...\n"); |
| 277 |
switch (socks_state) |
| 278 |
{ |
| 279 |
case SOCKS_VOID: |
| 280 |
case SOCKS_IDLE: |
| 281 |
ASSERT(0); |
| 282 |
break; |
| 283 |
case SOCKS_GET_AUTH_REQ: |
| 284 |
ASSERT(0); |
| 285 |
break; |
| 286 |
case SOCKS_PUT_AUTH_REP: |
| 287 |
if (!PutClientAuthReply()) |
| 288 |
{ |
| 289 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: PutClientAuthReply() failure!\n"); |
| 290 |
Close(); |
| 291 |
return; |
| 292 |
} |
| 293 |
break; |
| 294 |
case SOCKS_GET_REQUEST: |
| 295 |
ASSERT(0); |
| 296 |
break; |
| 297 |
case SOCKS_PUT_REQUEST: |
| 298 |
ASSERT(0); |
| 299 |
break; |
| 300 |
case SOCKS_CONNECTING: |
| 301 |
ASSERT(0); |
| 302 |
break; |
| 303 |
case SOCKS_GET_REPLY: |
| 304 |
ASSERT(0); |
| 305 |
break; |
| 306 |
case SOCKS_PUT_REPLY: |
| 307 |
if (!PutClientReply()) |
| 308 |
{ |
| 309 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: PutClientReply() failure!\n"); |
| 310 |
Shutdown(); |
| 311 |
return; |
| 312 |
} |
| 313 |
break; |
| 314 |
case SOCKS_CONNECTED: |
| 315 |
if (!PutClientData()) |
| 316 |
{ |
| 317 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: PutClientData() failure!\n"); |
| 318 |
Shutdown(); |
| 319 |
return; |
| 320 |
} |
| 321 |
break; |
| 322 |
case SOCKS_SHUTDOWN: |
| 323 |
ASSERT(0); |
| 324 |
break; |
| 325 |
} // end switch(socks_state) |
| 326 |
break; |
| 327 |
case ProtoSocket::RECV: |
| 328 |
{ |
| 329 |
PLOG(PL_DETAIL, "RECV) ...\n"); |
| 330 |
switch (socks_state) |
| 331 |
{ |
| 332 |
case SOCKS_VOID: |
| 333 |
case SOCKS_IDLE: // initial, inactive state (socks_client_socket is closed) |
| 334 |
ASSERT(0); |
| 335 |
break; |
| 336 |
case SOCKS_GET_AUTH_REQ: // first active state, getting auth request bytes from client |
| 337 |
if (!GetClientAuthRequest()) |
| 338 |
{ |
| 339 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: GetClientAuthRequest() failure!\n"); |
| 340 |
Close(); |
| 341 |
return; |
| 342 |
} |
| 343 |
break; |
| 344 |
case SOCKS_GET_REQUEST: // next, accumulate SOCKS request bytes from client |
| 345 |
if (!GetClientRequest()) |
| 346 |
{ |
| 347 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: GetClientRequest() failure!\n"); |
| 348 |
Close(); |
| 349 |
return; |
| 350 |
} |
| 351 |
break; |
| 352 |
case SOCKS_PUT_AUTH_REP: |
| 353 |
case SOCKS_PUT_REQUEST: |
| 354 |
case SOCKS_CONNECTING: |
| 355 |
case SOCKS_PUT_REPLY: |
| 356 |
case SOCKS_GET_REPLY: |
| 357 |
case SOCKS_SHUTDOWN: |
| 358 |
{ |
| 359 |
unsigned int numBytes = SOCKS_BUFFER_SIZE; |
| 360 |
// do a recv() - probably means client disconnected (prematurely) |
| 361 |
if (theSocket.Recv((char*)client_buffer, numBytes)) |
| 362 |
{ |
| 363 |
if ((SOCKS_SHUTDOWN != socks_state) && (0 != numBytes)) |
| 364 |
PLOG(PL_WARN, "NorpSession::OnSocksClientEvent() warning: unexpectedly received %u bytes from SOCKS client!\n", numBytes); |
| 365 |
} |
| 366 |
else |
| 367 |
{ |
| 368 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: unexpected receive error from SOCKS client!\n"); |
| 369 |
} |
| 370 |
break; |
| 371 |
} |
| 372 |
case SOCKS_CONNECTED: // finally, reading/writing data from/to client |
| 373 |
if (0 == client_pending) // we need this because we sometimes get extra notifications with old "protolib" |
| 374 |
{ |
| 375 |
if (!GetClientData()) |
| 376 |
{ |
| 377 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: GetClientData() failure!\n"); |
| 378 |
Shutdown(); |
| 379 |
} |
| 380 |
} |
| 381 |
break; |
| 382 |
} // end switch(socks_state) |
| 383 |
break; |
| 384 |
} |
| 385 |
case ProtoSocket::DISCONNECT: |
| 386 |
PLOG(PL_DETAIL, "DISCONNECT) ...\n"); |
| 387 |
switch (socks_state) |
| 388 |
{ |
| 389 |
case SOCKS_VOID: |
| 390 |
case SOCKS_IDLE: |
| 391 |
ASSERT(0); // will never happen |
| 392 |
break; |
| 393 |
case SOCKS_GET_AUTH_REQ: |
| 394 |
case SOCKS_PUT_AUTH_REP: |
| 395 |
case SOCKS_GET_REQUEST: |
| 396 |
case SOCKS_PUT_REQUEST: |
| 397 |
case SOCKS_CONNECTING: |
| 398 |
case SOCKS_PUT_REPLY: |
| 399 |
case SOCKS_GET_REPLY: |
| 400 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: premature SOCKS client disconnect!\n"); |
| 401 |
Shutdown(); // unexpected, premature client disconnect |
| 402 |
break; |
| 403 |
case SOCKS_CONNECTED: |
| 404 |
// Client finished sending (and receiving) data |
| 405 |
socks_state = SOCKS_SHUTDOWN; |
| 406 |
if (0 == client_pending) |
| 407 |
PutRemoteData(); // to shut down socks_remote_socket or norm_tx_stream |
| 408 |
case SOCKS_SHUTDOWN: |
| 409 |
theSocket.Close(); |
| 410 |
if (ShutdownComplete()) |
| 411 |
Close(); |
| 412 |
// else more shutdown actions pending |
| 413 |
break; |
| 414 |
} |
| 415 |
break; |
| 416 |
|
| 417 |
case ProtoSocket::EXCEPTION: |
| 418 |
PLOG(PL_DETAIL, "EXCEPTION) ...\n"); |
| 419 |
ASSERT(0); |
| 420 |
break; |
| 421 |
|
| 422 |
case ProtoSocket::ERROR_: |
| 423 |
PLOG(PL_DETAIL, "ERROR_) ...\n"); |
| 424 |
break; |
| 425 |
} // end switch (theEvent) |
| 426 |
} // end NorpSession::OnSocksClientEvent() |
| 427 |
|
| 428 |
bool NorpSession::GetClientAuthRequest() |
| 429 |
{ |
| 430 |
// This method incrementally receives/processes a SOCKS AuthRequest |
| 431 |
// and updates our "socks_state" and buffer indices accordlingly |
| 432 |
// ("numBytes" is set to how many needed for next socket read) |
| 433 |
ASSERT(SOCKS_GET_AUTH_REQ == socks_state); |
| 434 |
for(;;) // loops until auth request is read or no more input is ready |
| 435 |
{ |
| 436 |
switch (client_index) |
| 437 |
{ |
| 438 |
case 0: // need to read VERSION byte first |
| 439 |
{ |
| 440 |
client_pending = 1; |
| 441 |
break; |
| 442 |
} |
| 443 |
case 1: // validate VERSION and read NMETHODS byte |
| 444 |
{ |
| 445 |
ProtoPktSOCKS::AuthRequest authReq(client_buffer, 1); |
| 446 |
if (5 != authReq.GetVersion()) |
| 447 |
{ |
| 448 |
PLOG(PL_ERROR, "NorpSession::GetClientAuthRequest() received incompatible version %d request!\n", authReq.GetVersion()); |
| 449 |
return false; |
| 450 |
} |
| 451 |
client_pending = 2; |
| 452 |
break; |
| 453 |
} |
| 454 |
case 2: // Check NMETHODS byte to see how much to read. |
| 455 |
{ |
| 456 |
ProtoPktSOCKS::AuthRequest authReq(client_buffer, 2); |
| 457 |
UINT8 numBytes = authReq.GetMethodCount(); |
| 458 |
if (0 == numBytes) |
| 459 |
{ |
| 460 |
PLOG(PL_ERROR, "NorpSession::GetClientAuthRequest() received AuthRequest with zero methods!\n"); |
| 461 |
return false; |
| 462 |
} |
| 463 |
client_pending += numBytes; |
| 464 |
break; |
| 465 |
} |
| 466 |
default: |
| 467 |
{ |
| 468 |
if (client_index == client_pending) |
| 469 |
{ |
| 470 |
PLOG(PL_DETAIL, "NorpSession::GetClientAuthRequest() session %u received SOCKS AuthRequest from client %s/%hu ...\n", |
| 471 |
GetSessionId(), socks_client_socket.GetDestination().GetHostString(), socks_client_socket.GetDestination().GetPort()); |
| 472 |
// We have received the full SOCKS AuthRequest |
| 473 |
// Find best authentication "method" (currently only AUTH_NONE) |
| 474 |
ProtoPktSOCKS::AuthType authType = ProtoPktSOCKS::AUTH_INVALID; |
| 475 |
ProtoPktSOCKS::AuthRequest authReq(client_buffer, client_index); |
| 476 |
for (UINT8 i = 0; i < authReq.GetMethodCount(); i++) |
| 477 |
{ |
| 478 |
if (ProtoPktSOCKS::AUTH_NONE == authReq.GetMethod(i)) |
| 479 |
{ |
| 480 |
authType = ProtoPktSOCKS::AUTH_NONE; |
| 481 |
break; |
| 482 |
} |
| 483 |
} |
| 484 |
if (ProtoPktSOCKS::AUTH_NONE == authType) |
| 485 |
{ |
| 486 |
// Build our SOCKS AuthReply into "remote_buffer" for transmission to client |
| 487 |
socks_state = SOCKS_PUT_AUTH_REP; |
| 488 |
ProtoPktSOCKS::AuthReply authReply(remote_buffer, SOCKS_BUFFER_SIZE, false); |
| 489 |
authReply.SetVersion(5); |
| 490 |
authReply.SetMethod(ProtoPktSOCKS::AUTH_NONE); |
| 491 |
remote_pending = authReply.GetLength(); |
| 492 |
remote_index = 0; |
| 493 |
client_index = client_pending = 0; |
| 494 |
// TBD - should we pause client input notification here? |
| 495 |
return PutClientAuthReply(); |
| 496 |
} |
| 497 |
else |
| 498 |
{ |
| 499 |
PLOG(PL_ERROR, "NorpSession::GetClientAuthRequest() received AuthRequest with no compatible method!\n"); |
| 500 |
return false; |
| 501 |
} |
| 502 |
} |
| 503 |
break; |
| 504 |
} |
| 505 |
} // end switch(client_rx_index) |
| 506 |
|
| 507 |
// Read next chunk of AuthRequest (as long as socket has data to read) |
| 508 |
unsigned int numBytes = client_pending - client_index; |
| 509 |
if (!socks_client_socket.Recv(((char*)client_buffer)+client_index, numBytes)) |
| 510 |
{ |
| 511 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: client socket recv() failure!\n"); |
| 512 |
return false; |
| 513 |
} |
| 514 |
client_index += numBytes; |
| 515 |
if (client_index < client_pending) break; // no more to read, so wait until RECV notification for more |
| 516 |
} // end for(;;) |
| 517 |
return true; |
| 518 |
} // end NorpSession::GetClientAuthRequest() |
| 519 |
|
| 520 |
bool NorpSession::PutClientAuthReply() |
| 521 |
{ |
| 522 |
ASSERT(SOCKS_PUT_AUTH_REP == socks_state); |
| 523 |
// We have a SOCKS AuthReply in our "remote_buffer" pending transmission to the SOCKS client |
| 524 |
unsigned int numBytes = remote_pending - remote_index; |
| 525 |
if (socks_client_socket.Send(((char*)remote_buffer)+remote_index, numBytes)) |
| 526 |
{ |
| 527 |
remote_index += numBytes; |
| 528 |
if (remote_index < remote_pending) |
| 529 |
{ |
| 530 |
// Only partially sent, cue output notification to complete |
| 531 |
socks_client_socket.StartOutputNotification(); |
| 532 |
} |
| 533 |
else |
| 534 |
{ |
| 535 |
// AuthReply was fully sent |
| 536 |
socks_client_socket.StopOutputNotification(); |
| 537 |
socks_state = SOCKS_GET_REQUEST; |
| 538 |
remote_pending = remote_index = 0; |
| 539 |
client_pending = 1; // To get VERSION byte of impending SOCKS Request |
| 540 |
client_index = 0; |
| 541 |
} |
| 542 |
return true; |
| 543 |
} |
| 544 |
else |
| 545 |
{ |
| 546 |
PLOG(PL_ERROR, "NorpSession::PutClientAuthReply() error: auth reply send failure!\n"); |
| 547 |
return false; |
| 548 |
} |
| 549 |
} // end NorpSession::PutClientAuthReply() |
| 550 |
|
| 551 |
bool NorpSession::GetClientRequest() |
| 552 |
{ |
| 553 |
ASSERT(SOCKS_GET_REQUEST == socks_state); |
| 554 |
// SOCKS5 request: VERSION + CMD + RESV + ADDR_TYP + DST.ADDR + DST.PORT |
| 555 |
// bytes: 1 + 1 + 1 + 1 + N + 2 |
| 556 |
for(;;) // loops until client request is read or no more input is ready |
| 557 |
{ |
| 558 |
switch (client_index) |
| 559 |
{ |
| 560 |
case 0: // need to read VERSION byte first |
| 561 |
{ |
| 562 |
client_pending = 1; |
| 563 |
break; |
| 564 |
} |
| 565 |
case 1: // Validate VERSION, and get REQ, if applicable |
| 566 |
{ |
| 567 |
ProtoPktSOCKS::Request req(client_buffer, 1); |
| 568 |
if (5 != req.GetVersion()) |
| 569 |
{ |
| 570 |
PLOG(PL_ERROR, "NorpSession::GetClientRequest() received incompatible version %d request!\n", req.GetVersion()); |
| 571 |
return false; |
| 572 |
} |
| 573 |
client_pending = 4; // ask for bytes up through ADDR_TYP |
| 574 |
break; |
| 575 |
} |
| 576 |
case 2: // Handle request based on command type |
| 577 |
case 3: |
| 578 |
{ |
| 579 |
// no change, just CMD and/or RESV byte coming in |
| 580 |
ASSERT(4 == client_pending); |
| 581 |
break; |
| 582 |
} |
| 583 |
case 4: // Process ADDR_TYP to see how many more to ask for |
| 584 |
{ |
| 585 |
ProtoPktSOCKS::Request req(client_buffer, 4); |
| 586 |
switch (req.GetAddressType()) |
| 587 |
{ |
| 588 |
case ProtoPktSOCKS::IPv4: |
| 589 |
client_pending = 4 + (4 + 2); // IPv4 addr + port |
| 590 |
break; |
| 591 |
case ProtoPktSOCKS::NAME: |
| 592 |
client_pending = 4 + 1; // ask for name length byte |
| 593 |
break; |
| 594 |
case ProtoPktSOCKS::IPv6: |
| 595 |
client_pending = 4 + (16 + 2); // IPv6 addr + port |
| 596 |
break; |
| 597 |
default: |
| 598 |
PLOG(PL_ERROR, "NorpSession::GetClientRequest() error: invalid address type!\n"); |
| 599 |
return false; |
| 600 |
} |
| 601 |
break; |
| 602 |
} |
| 603 |
case 5: // Process NAME length byte |
| 604 |
{ |
| 605 |
ProtoPktSOCKS::Request req(client_buffer, 5); |
| 606 |
if (ProtoPktSOCKS::NAME == req.GetAddressType()) |
| 607 |
client_pending = 5 + (req.GetAddressLength() + 2); // DNS name + port |
| 608 |
break; |
| 609 |
} |
| 610 |
default: |
| 611 |
{ |
| 612 |
if (client_index == client_pending) |
| 613 |
{ |
| 614 |
// Entire SOCKS Request from local client has been received, so handle it ... |
| 615 |
ProtoPktSOCKS::Request request(client_buffer, client_index); |
| 616 |
client_pending = client_index = 0; // finished with client buffer for moment after handing request here |
| 617 |
return OnClientRequest(request, socks_client_socket.GetDestination()); |
| 618 |
} |
| 619 |
break; |
| 620 |
} |
| 621 |
} |
| 622 |
// Read next chunk of client Request (as long as socket has data to read) |
| 623 |
unsigned int numBytes = client_pending - client_index; |
| 624 |
if (!socks_client_socket.Recv(((char*)client_buffer)+client_index, numBytes)) |
| 625 |
{ |
| 626 |
PLOG(PL_ERROR, "NorpSession::OnSocksClientEvent() error: client socket recv() failure!\n"); |
| 627 |
return false; |
| 628 |
} |
| 629 |
client_index += numBytes; |
| 630 |
if (client_index < client_pending) break; // no more to read, so wait until RECV notification for more |
| 631 |
} // end for(;;;) |
| 632 |
return true; |
| 633 |
} // end NorpSession::GetClientRequest() |
| 634 |
|
| 635 |
|
| 636 |
bool NorpSession::OnClientRequest(const ProtoPktSOCKS::Request request, const ProtoAddress& srcAddr) |
| 637 |
{ |
| 638 |
ProtoAddress destAddr; |
| 639 |
if (!request.GetAddress(destAddr)) |
| 640 |
{ |
| 641 |
PLOG(PL_ERROR, "NorpSession::HandleSocksRequest() error: request with invalid destination address!\n"); |
| 642 |
return false; |
| 643 |
} |
| 644 |
switch (request.GetCommand()) |
| 645 |
{ |
| 646 |
case ProtoPktSOCKS::Request::CONNECT: |
| 647 |
PLOG(PL_INFO, "norp: session %u recvd CONNECT request dst addr/port = %s/%hu\n", GetSessionId(), destAddr.GetHostString(), destAddr.GetPort()); |
| 648 |
if (norm_enable) |
| 649 |
{ |
| 650 |
// TBD - Determine "route" for "destAddr" to potentially separate remote "norp" peer |
| 651 |
// (for now we assume "norp" remote is collocated on "destAddr" endpoints unless |
| 652 |
// the controller has been configured for a specific remote 'correspondent') |
| 653 |
ProtoAddress norpAddr; |
| 654 |
controller.GetRemoteNorpAddress(destAddr, norpAddr); |
| 655 |
// Relay SOCKS request to remote peer NORP server (peer will TCP connect to endpoint and reply via NORM) |
| 656 |
return PutRemoteRequest(request, norpAddr); |
| 657 |
} |
| 658 |
else |
| 659 |
{ |
| 660 |
return ConnectToRemote(destAddr); // do direct TCP connect to remote endpoint |
| 661 |
} |
| 662 |
case ProtoPktSOCKS::Request::BIND: |
| 663 |
PLOG(PL_INFO, "norp: session %u recvd BIND request dst addr/port = %s/%hu\n", GetSessionId(), destAddr.GetHostString(), destAddr.GetPort()); |
| 664 |
return BindRemote(destAddr.GetPort()); |
| 665 |
case ProtoPktSOCKS::Request::UDP_ASSOC: |
| 666 |
PLOG(PL_INFO, "norp: session %u recvd UDP_ASSOC for %s/%hu\n", GetSessionId(), destAddr.GetHostString(), destAddr.GetPort()); |
| 667 |
// Save our UDP client's address (use the control socket addr if unspecified in request) |
| 668 |
if (destAddr.IsUnspecified()) |
| 669 |
{ |
| 670 |
udp_client_addr = srcAddr; |
| 671 |
udp_client_addr.SetPort(destAddr.GetPort()); |
| 672 |
} |
| 673 |
else |
| 674 |
{ |
| 675 |
udp_client_addr = destAddr; |
| 676 |
} |
| 677 |
return OpenUdpRelay(); |
| 678 |
default: |
| 679 |
PLOG(PL_ERROR, "NorpSession::OnClientRequest() error: invalid request command!\n"); |
| 680 |
return false; |
| 681 |
} |
| 682 |
} // end NorpSession::OnClientRequest() |
| 683 |
|
| 684 |
bool NorpSession::PutRemoteRequest(const ProtoPktSOCKS::Request& request, const ProtoAddress& remoteAddr) |
| 685 |
{ |
| 686 |
// The "request" here sits in our "client_buffer". We know this buffer has room to append our |
| 687 |
// "session_id.identifier" to the end. This "identifier" gives the command a unique key in |
| 688 |
// the context of the NorpSession "originator" NormNodeId to recognize if it is a repeated |
| 689 |
// request command, etc. |
| 690 |
if (NORM_SESSION_INVALID == norm_session) |
| 691 |
{ |
| 692 |
// This creates the client-side NORM session for a connection. |
| 693 |
// (TBD) Create single-socket NORM session using an ephemeral port and connect() to the remote |
| 694 |
// NORP server NORM port. (Use NormChangeDestination() after NormStartReceiver() to |
| 695 |
// configure this newly-create NormSession as needed) |
| 696 |
#ifdef NEW_PORT |
| 697 |
// We use port zero to get session that will bound to an ephemeral port (single port for tx and rx) upon NormStartReceiver |
| 698 |
norm_session = NormCreateSession(controller.GetNormInstance(), remoteAddr.GetHostString(), 0, controller.GetNormNodeId()); |
| 699 |
#else |
| 700 |
norm_session = NormCreateSession(controller.GetNormInstance(), remoteAddr.GetHostString(), controller.GetNormPort(), controller.GetNormNodeId()); |
| 701 |
#endif |
| 702 |
if (NORM_SESSION_INVALID == norm_session) |
| 703 |
{ |
| 704 |
PLOG(PL_ERROR, "NorpSession::PutRemoteRequest() NormCreateSession() failure!\n"); |
| 705 |
return false; |
| 706 |
} |
| 707 |
NormSetMessageTrace(norm_session, controller.GetNormTrace()); |
| 708 |
NormSetUserData(norm_session, this); |
| 709 |
NormSetDefaultSyncPolicy(norm_session, NORM_SYNC_STREAM); |
| 710 |
|
| 711 |
// We use a 2-step sender/receiver startup process by first opening an idle, "tx-only" NormSession to |
| 712 |
// send the Request command to the remote "controller" NormSession (The remote will get our "tx port" |
| 713 |
// info as part of this request sent). Our controller will get the REQ_ACK and/or SOCKS_REPLY message, |
| 714 |
// then inform us, and so we will get the remote "tx port" info so we can then start our own receiver |
| 715 |
// and "connect" our NORM rx_socket to the remote tx_socket. The sender here is made "idle" by disabling |
| 716 |
// disabling the normal NORM GRTT probing. We don't re-activate that until we know the remote end has |
| 717 |
// a receiver running (This is done upon REQ_ACK or SOCKS_REPLY reception when we also start our own NORM |
| 718 |
// receiver). |
| 719 |
|
| 720 |
char ifaceName[64]; |
| 721 |
ifaceName[63] = '\0'; |
| 722 |
if (ProtoNet::GetInterfaceName(controller.GetProxyAddress(), ifaceName, 63)) |
| 723 |
NormSetMulticastInterface(norm_session, ifaceName); |
| 724 |
else |
| 725 |
PLOG(PL_ERROR, "NorpSession::PutRemoteRequest() warning: unable to get interface name for address %s\n", controller.GetProxyAddress().GetHostString()); |
| 726 |
switch (controller.GetNormCC()) |
| 727 |
{ |
| 728 |
case Norp::NORM_CC: // default TCP-friendly congestion control |
| 729 |
// do nothing, this is NORM's default mode |
| 730 |
break; |
| 731 |
case Norp::NORM_CCE: // "wireless-ready" ECN-only congestion control |
| 732 |
NormSetEcnSupport(norm_session, true, true); |
| 733 |
break; |
| 734 |
case Norp::NORM_CCL: // "loss tolerant", non-ECN congestion control |
| 735 |
NormSetEcnSupport(norm_session, false, false, true); |
| 736 |
break; |
| 737 |
case Norp::NORM_FIXED: // fixed-rate operation |
| 738 |
// if cumulative rate limit has been imposed, it can override the fixed rate |
| 739 |
// (i.e. to share pipe among multiple flows) |
| 740 |
if ((norm_rate_min >= 0) && (norm_rate_min < controller.GetNormTxRate())) |
| 741 |
NormSetTxRate(norm_session, norm_rate_min); |
| 742 |
else |
| 743 |
NormSetTxRate(norm_session, controller.GetNormTxRate()); |
| 744 |
break; |
| 745 |
} |
| 746 |
if ((norm_rate_min >= 0.0) || (norm_rate_max >= 0.0)) |
| 747 |
NormSetTxRateBounds(norm_session, norm_rate_min, norm_rate_max); |
| 748 |
|
| 749 |
// Even though we call "NormStartSender()" to get a "tx port" value, we disable GRTT probing and thus |
| 750 |
// defer "real" sender startup until we get an ACK to our request (see "OriginatorStartNorm()") |
| 751 |
NormSetTxOnly(norm_session, true); |
| 752 |
NormSetGrttProbingMode(norm_session, NORM_PROBE_NONE); // no probing until remote receiver is started |
| 753 |
if (!NormStartSender(norm_session, GetSessionId(), NORM_BUFFER_SIZE, controller.GetNormSegmentSize(), |
| 754 |
controller.GetNormBlockSize(), controller.GetNormParityCount())) |
| 755 |
{ |
| 756 |
PLOG(PL_ERROR, "NorpSession::PutRemoteRequest() error: NormStartSender() failure!\n"); |
| 757 |
return false; |
| 758 |
} |
| 759 |
#ifdef NEW_PORT |
| 760 |
// Now call NormChangeDestination() to set proper destination port and "connect" to it |
| 761 |
NormChangeDestination(norm_session, remoteAddr.GetHostString(), controller.GetNormPort(), true); |
| 762 |
#endif // NEW_PORT |
| 763 |
//NormSetTxSocketBuffer(norm_session, 4096); |
| 764 |
NormSetFlowControl(norm_session, 0.0); // disable timer-based flow control since we are ACK-limiting writes to stream |
| 765 |
norm_segment_size = controller.GetNormSegmentSize(); |
| 766 |
norm_stream_buffer_max = ComputeNormStreamBufferSegmentCount(NORM_BUFFER_SIZE, norm_segment_size, controller.GetNormBlockSize()); |
| 767 |
norm_stream_buffer_max -= controller.GetNormBlockSize(); // a little safety margin |
| 768 |
norm_stream_buffer_count = 0; |
| 769 |
norm_stream_bytes_remain = 0; |
| 770 |
norm_watermark_pending = false; |
| 771 |
|
| 772 |
PLOG(PL_INFO, "norp: originator %lu created NORM session with dest %s/%hu (srcPort %hu)\n", controller.GetNormNodeId(), |
| 773 |
remoteAddr.GetHostString(), controller.GetNormPort(), NormGetTxPort(norm_session)); |
| 774 |
|
| 775 |
// We open a UDP socket for NORP signaling here and "connect() it so we can get ICMP error messages |
| 776 |
// (I.e., an ICMP "port unreachable" indicates the remote destination is not NORP-enabled) |
| 777 |
if (!norp_tx_socket.Open()) |
| 778 |
{ |
| 779 |
PLOG(PL_ERROR, "NorpSession::PutRemoteRequest() error: unable to open norp_tx_socket!\n"); |
| 780 |
return false; |
| 781 |
} |
| 782 |
norp_remote_addr = remoteAddr; |
| 783 |
norp_remote_addr.SetPort(controller.GetNorpPort()); |
| 784 |
if (!norp_tx_socket.Connect(norp_remote_addr)) |
| 785 |
{ |
| 786 |
PLOG(PL_ERROR, "NorpSession::PutRemoteRequest() error: unable to open and connect norp_tx_socket!\n"); |
| 787 |
return false; |
| 788 |
} |
| 789 |
} |
| 790 |
socks_client_socket.StopInputNotification(); // don't accept more from client until connected |
| 791 |
PLOG(PL_INFO, "norp: node %u relaying SOCKS request to NORP destination %s/%hu\n", controller.GetNormNodeId(), |
| 792 |
remoteAddr.GetHostString(), controller.GetNorpPort()); |
| 793 |
// Put the SOCKS request into a NorpMsg and relay it to remote NORP server |
| 794 |
// (Note that the request is "robustly" transmitted via periodic repetition until a request acknowledgment (REQ_ACK) is received |
| 795 |
ASSERT(!norp_msg_timer.IsActive()); |
| 796 |
norp_msg.SetType(NorpMsg::SOCKS_REQ); |
| 797 |
norp_msg.SetSessionId(GetSessionId()); |
| 798 |
norp_msg.SetSourcePort(NormGetTxPort(norm_session)); // Our NORM tx port number (so remote can "connect()" |
| 799 |
norp_msg.SetDestinationPort(NormGetRxPort(norm_session)); // Our NORM rx port (for info) |
| 800 |
norp_msg.SetNodeId(controller.GetNormNodeId()); // our NORM node id |
| 801 |
norp_msg.SetContent((const char*)request.GetBuffer(), request.GetLength()); |
| 802 |
|
| 803 |
struct timeval currentTime; |
| 804 |
ProtoSystemTime(currentTime); |
| 805 |
norp_msg.SetTimestamp(currentTime); |
| 806 |
if (!SendMessage(norp_msg)) |
| 807 |
PLOG(PL_ERROR, "NorpSession::PutRemoteRequest() warning: NORP message transmission failed (will try again)!\n"); |
| 808 |
norp_msg_timer.SetInterval(2.0 * norp_rtt_estimate); |
| 809 |
ActivateTimer(norp_msg_timer); |
| 810 |
socks_state = SOCKS_PUT_REQUEST; |
| 811 |
return true; |
| 812 |
} // end NorpSession::PutRemoteRequest() |
| 813 |
|
| 814 |
bool NorpSession::MakeDirectConnect() |
| 815 |
{ |
| 816 |
TRACE("MAKING DIRECT CONNECTION ..."); |
| 817 |
// This assumes our "norp_msg" previously sent in attempt to connect |
| 818 |
// to a remote "norp" peer is cached (per PutRemoveRequest() |
| 819 |
if (NORM_SESSION_INVALID != norm_session) |
| 820 |
{ |
| 821 |
NormDestroySession(norm_session); |
| 822 |
norm_session = NORM_SESSION_INVALID; |
| 823 |
} |
| 824 |
if (norp_msg_timer.IsActive()) norp_msg_timer.Deactivate(); |
| 825 |
if (NorpMsg::SOCKS_REQ != norp_msg.GetType()) |
| 826 |
{ |
| 827 |
PLOG(PL_ERROR, "NorpSession::MakeDirectConnect() error: invalid norp_msg enqueued!\n"); |
| 828 |
return false; |
| 829 |
} |
| 830 |
|
| 831 |
ProtoPktSOCKS::Request req(norp_msg.AccessContentPtr(), norp_msg.GetContentLength(), true); |
| 832 |
ProtoAddress destAddr; |
| 833 |
if (!req.GetAddress(destAddr)) |
| 834 |
{ |
| 835 |
PLOG(PL_ERROR, "NorpSession::MakeDirectConnect() error: norp_msg SOCKS_REQ with invalid destination address enqueued!\n"); |
| 836 |
return false; |
| 837 |
} |
| 838 |
return ConnectToRemote(destAddr); |
| 839 |
} // end NorpSession::MakeDirectConnect() |
| 840 |
|
| 841 |
bool NorpSession::OnRemoteRequestAcknowledgment(const NorpMsg& theMsg, const ProtoAddress& senderAddr) |
| 842 |
{ |
| 843 |
// Note we update our RTT estimate on ACKs received |
| 844 |
ProtoTime recvTime; |
| 845 |
recvTime.GetCurrentTime(); |
| 846 |
ProtoTime sentTime; |
| 847 |
theMsg.GetTimestamp(sentTime); |
| 848 |
if (SOCKS_PUT_REQUEST == socks_state) |
| 849 |
{ |
| 850 |
norp_rtt_estimate = ProtoTime::Delta(recvTime, sentTime); // initial rtt received |
| 851 |
if (!OriginatorStartNorm(senderAddr, theMsg.GetNodeId(), theMsg.GetSourcePort(), theMsg.GetDestinationPort())) |
| 852 |
{ |
| 853 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequestAcknowledgment() error: OriginatorStartNormSender() failure!\n"); |
| 854 |
return false; |
| 855 |
} |
| 856 |
// Note that successful OriginatorStartNorm() sets socks_state to SOCKS_CONNECTING |
| 857 |
} |
| 858 |
else |
| 859 |
{ |
| 860 |
// Just use duplicative ACKs to update RTT estimate via EWMA |
| 861 |
ASSERT(norp_rtt_estimate > 0.0); |
| 862 |
double rttNew = ProtoTime::Delta(recvTime, sentTime); |
| 863 |
norp_rtt_estimate = 0.5*(norp_rtt_estimate + rttNew); // TBD - is this a reasonable "smoothing" factor |
| 864 |
} |
| 865 |
// Note it's OK if a bona fide measured RTT is less than NORP_RTT_MIN |
| 866 |
if (norp_rtt_estimate <= 0.0) |
| 867 |
{ |
| 868 |
norp_rtt_estimate = NORP_RTT_MIN; |
| 869 |
} |
| 870 |
else if (norp_rtt_estimate > NORP_RTT_MAX) |
| 871 |
{ |
| 872 |
PLOG(PL_WARN, "NorpSession::OnRemoteRequestAcknowledgment() warning: measured RTT exceeds max value of %lf sec.\n", NORP_RTT_MAX); |
| 873 |
norp_rtt_estimate = NORP_RTT_MAX; |
| 874 |
} |
| 875 |
return true; |
| 876 |
} // end NorpSession::OnRemoteRequestAcknowledgment() |
| 877 |
|
| 878 |
bool NorpSession::OnRemoteReply(const NorpMsg& theMsg, const ProtoAddress& senderAddr) |
| 879 |
{ |
| 880 |
ProtoPktSOCKS::Reply reply(theMsg.AccessContentPtr(), theMsg.GetContentLength()); |
| 881 |
PLOG(PL_INFO, "NorpSession::OnRemoteReply() session %hu reply type %d\n", GetSessionId(), reply.GetType()); |
| 882 |
if (ProtoPktSOCKS::Reply::SUCCESS == reply.GetType()) |
| 883 |
{ |
| 884 |
if (SOCKS_PUT_REQUEST == socks_state) |
| 885 |
{ |
| 886 |
// Somehow we missed the NORP REQUEST_ACK and the reply is being received first |
| 887 |
// (TBD - should we init RTT here like above???) |
| 888 |
if (!OriginatorStartNorm(senderAddr, theMsg.GetNodeId(), theMsg.GetSourcePort(), theMsg.GetDestinationPort())) |
| 889 |
{ |
| 890 |
PLOG(PL_ERROR, "NorpSession::OnRemoteReply() error: OriginatorStartNormSender() failure!\n"); |
| 891 |
return false; |
| 892 |
} |
| 893 |
// Note that successful OriginatorStartNorm() sets socks_state to SOCKS_CONNECTING |
| 894 |
} |
| 895 |
if (is_preset) |
| 896 |
{ |
| 897 |
// Go straight to connected state and start |
| 898 |
// accepting recv data from client or remote |
| 899 |
client_index = client_pending = 0; |
| 900 |
socks_client_socket.StartInputNotification(); |
| 901 |
remote_index = remote_pending = 0; |
| 902 |
socks_state = SOCKS_CONNECTED; |
| 903 |
} |
| 904 |
else if (SOCKS_CONNECTING == socks_state) |
| 905 |
{ |
| 906 |
// TBD - we may want to put the reply in the client_buffer instead??? |
| 907 |
// (i.e. in case we start receiving remote data before reply is fully sent?) |
| 908 |
socks_state = SOCKS_PUT_REPLY; |
| 909 |
memcpy(remote_buffer, (char*)reply.GetBuffer(), reply.GetLength()); |
| 910 |
remote_pending = reply.GetLength(); |
| 911 |
remote_index = 0; |
| 912 |
PLOG(PL_INFO, "NorpSession::OnRemoteReply() originator relaying reply to SOCKS client ...\n"); |
| 913 |
if (!PutClientReply()) |
| 914 |
{ |
| 915 |
PLOG(PL_ERROR, "NorpSession::OnRemoteReply() error: unable to relay SOCKS reply to client!\n"); |
| 916 |
return false; |
| 917 |
} |
| 918 |
} |
| 919 |
// Send an ACK to the received reply |
| 920 |
UINT32 buffer[32/4]; |
| 921 |
NorpMsg ack(buffer, 32, false); |
| 922 |
ack.SetType(NorpMsg::REP_ACK); |
| 923 |
ack.SetSessionId(GetSessionId()); // our session id (note it was set by request originator) |
| 924 |
ack.SetSourcePort(NormGetTxPort(norm_session)); // our NORM tx port number (so remote can "connect()" |
| 925 |
ack.SetDestinationPort(NormGetRxPort(norm_session)); // our NORM rx port (for info) |
| 926 |
struct timeval sentTime; |
| 927 |
theMsg.GetTimestamp(sentTime); |
| 928 |
ack.SetTimestamp(sentTime); // echo the sender's timestamp for their RTT measurement |
| 929 |
ack.SetNodeId(controller.GetNormNodeId()); // our NORM node id |
| 930 |
if (!controller.SendMessage(ack, senderAddr)) |
| 931 |
PLOG(PL_ERROR, "NorpSession::OnRemoteReply() warning: unsable to transmit NORP REP_ACK message!\n"); |
| 932 |
} |
| 933 |
else |
| 934 |
{ |
| 935 |
PLOG(PL_ERROR, "NorpSession::OnRemoteReply() received negative reply ...\n"); |
| 936 |
return false; |
| 937 |
} |
| 938 |
return true; |
| 939 |
} // end NorpSession::OnRemoteReply() |
| 940 |
|
| 941 |
// Note the "socks_state" MUST be SOCKS_PUT_REQUEST when this is invoked |
| 942 |
// (starts associated NORM session upon receipt of request ack or reply) |
| 943 |
bool NorpSession::OriginatorStartNorm(const ProtoAddress& senderAddr, |
| 944 |
NormNodeId corrNormId, |
| 945 |
UINT16 normSrcPort, |
| 946 |
UINT16 normDstPort) |
| 947 |
{ |
| 948 |
ASSERT(SOCKS_PUT_REQUEST == socks_state); |
| 949 |
if (norp_msg_timer.IsActive()) norp_msg_timer.Deactivate(); // cancels ongoing request retransmission, if applicable |
| 950 |
// TBD - log the norp_msg_timer repeat count? |
| 951 |
PLOG(PL_INFO, "norp: originator %u starting NORM sender probing (and receiver), connected to remote NORM source port %hu ...\n", |
| 952 |
controller.GetNormNodeId(), normSrcPort); |
| 953 |
NormSetGrttEstimate(norm_session, norp_rtt_estimate); // init NORM with value learned from NORP setup handshake |
| 954 |
NormSetRxPortReuse(norm_session, true, NULL, senderAddr.GetHostString(), normSrcPort); |
| 955 |
if (!NormStartReceiver(norm_session, NORM_BUFFER_SIZE)) |
| 956 |
{ |
| 957 |
PLOG(PL_ERROR, "NorpSession::OriginatorStartNorm() error: NormStartReceiver() failure!\n"); |
| 958 |
return false; |
| 959 |
} |
| 960 |
// The "NormChangeDestination()" call here makes sure our NORM session sends its packets |
| 961 |
// to the destination that the remote correspondent server wants us to use for NORM |
| 962 |
NormChangeDestination(norm_session, senderAddr.GetHostString(), normDstPort); |
| 963 |
if (Norp::NORM_FIXED != controller.GetNormCC()) |
| 964 |
NormSetCongestionControl(norm_session, true); // Note this also re-enables GRTT probing that was disabled at session creation |
| 965 |
else |
| 966 |
NormSetGrttProbingMode(norm_session, NORM_PROBE_ACTIVE); // re-enables previously suspended GRTT probing |
| 967 |
if (NORM_OBJECT_INVALID == (norm_tx_stream = NormStreamOpen(norm_session, NORM_BUFFER_SIZE))) |
| 968 |
{ |
| 969 |
PLOG(PL_ERROR, "NorpSession::OriginatorStartNorm() NormStreamOpen() failure!\n"); |
| 970 |
return false; |
| 971 |
} |
| 972 |
if (!NormAddAckingNode(norm_session, corrNormId)) |
| 973 |
{ |
| 974 |
PLOG(PL_ERROR, "NorpSession::OriginatorStartNorm() NormAddAckingNode() failure!\n"); |
| 975 |
return false; |
| 976 |
} |
| 977 |
socks_state = SOCKS_CONNECTING; |
| 978 |
return true; |
| 979 |
} // end NorpSession::OriginatorStartNorm() |
| 980 |
|
| 981 |
|
| 982 |
bool NorpSession::OnRemoteRequest(const NorpMsg& theMsg, const ProtoAddress& senderAddr) |
| 983 |
{ |
| 984 |
ProtoPktSOCKS::Request request(theMsg.AccessContentPtr(), theMsg.GetContentLength(), true); |
| 985 |
PLOG(PL_INFO, "NorpSession::OnRemoteRequest() session %hu request type %d (source port %hu)\n", |
| 986 |
GetSessionId(), request.GetCommand(), theMsg.GetSourcePort()); |
| 987 |
if (NORM_SESSION_INVALID == norm_session) |
| 988 |
{ |
| 989 |
// 0) Store the sender address to transmit REPLY to when connected to remote |
| 990 |
norp_remote_addr = senderAddr; |
| 991 |
// 1) Create a NormSession with request sender as session (destination) address |
| 992 |
UINT16 normSessionPort = controller.GetNormPort(); |
| 993 |
norm_session = NormCreateSession(controller.GetNormInstance(), senderAddr.GetHostString(), normSessionPort, controller.GetNormNodeId()); |
| 994 |
#ifdef NEW_PORT |
| 995 |
NormSetTxPort(norm_session, normSessionPort); // single port |
| 996 |
#endif // NEW_PORT |
| 997 |
if (NORM_SESSION_INVALID == norm_session) |
| 998 |
{ |
| 999 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() NormCreateSession() failure!\n"); |
| 1000 |
return false; |
| 1001 |
} |
| 1002 |
NormSetMessageTrace(norm_session, controller.GetNormTrace()); |
| 1003 |
// Set the NormSession user data so NORM API events can be directed to this session |
| 1004 |
NormSetUserData(norm_session, this); |
| 1005 |
NormSetDefaultSyncPolicy(norm_session, NORM_SYNC_STREAM); |
| 1006 |
|
| 1007 |
// Start our NORM session as a receiver (we start the sender later when we get a REPLY_ACK, or NORM_DATA) |
| 1008 |
// (First, enable port reuse and "connect()" to the sender's NORM source addr/port) |
| 1009 |
char ifaceName[64]; |
| 1010 |
ifaceName[63] = '\0'; |
| 1011 |
if (ProtoNet::GetInterfaceName(controller.GetProxyAddress(), ifaceName, 63)) |
| 1012 |
NormSetMulticastInterface(norm_session, ifaceName); |
| 1013 |
else |
| 1014 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() warning: unable to get interface name for address %s\n", controller.GetProxyAddress().GetHostString()); |
| 1015 |
switch (controller.GetNormCC()) |
| 1016 |
{ |
| 1017 |
case Norp::NORM_CC: // default TCP-friendly congestion control |
| 1018 |
// do nothing, this is NORM's default mode |
| 1019 |
break; |
| 1020 |
case Norp::NORM_CCE: // "wireless-ready" ECN-only congestion control |
| 1021 |
NormSetEcnSupport(norm_session, true, true); |
| 1022 |
break; |
| 1023 |
case Norp::NORM_CCL: // "loss tolerant", non-ECN congestion control |
| 1024 |
NormSetEcnSupport(norm_session, false, false, true); |
| 1025 |
break; |
| 1026 |
case Norp::NORM_FIXED: // fixed-rate transmission (no congestion control) |
| 1027 |
// if cumulative rate limit has been imposed, it can override the fixed rate |
| 1028 |
// (i.e. to share pipe among multiple flows) |
| 1029 |
if ((norm_rate_min >= 0) && (norm_rate_min < controller.GetNormTxRate())) |
| 1030 |
NormSetTxRate(norm_session, norm_rate_min); |
| 1031 |
else |
| 1032 |
NormSetTxRate(norm_session, controller.GetNormTxRate()); |
| 1033 |
break; |
| 1034 |
} |
| 1035 |
if ((norm_rate_min >= 0.0) || (norm_rate_max >= 0.0)) |
| 1036 |
NormSetTxRateBounds(norm_session, norm_rate_min, norm_rate_max); |
| 1037 |
// The "NormSetRxPortReuse()" call here "connects" us to the remote NORM sender source addr/port |
| 1038 |
// (this creates a proper binding since we reuse the same port number for multiple NORM sessions) |
| 1039 |
#ifdef NEW_PORT |
| 1040 |
NormSetRxPortReuse(norm_session, true); // defer connecting to remote sender until to REMOTE_SENDER_NEW to get correct port |
| 1041 |
#else |
| 1042 |
NormSetRxPortReuse(norm_session, true, NULL, senderAddr.GetHostString(), theMsg.GetSourcePort()); |
| 1043 |
#endif // if/else NEW_PORT |
| 1044 |
if (!NormStartReceiver(norm_session, NORM_BUFFER_SIZE)) |
| 1045 |
{ |
| 1046 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() error: NormStartReceiver() failure!\n"); |
| 1047 |
return false; |
| 1048 |
} |
| 1049 |
// This call makes sure we send our NORM packets to the originator's NORM port (i.e, if different than ours) |
| 1050 |
NormChangeDestination(norm_session, senderAddr.GetHostString(), theMsg.GetDestinationPort()); |
| 1051 |
if (!NormAddAckingNode(norm_session, theMsg.GetNodeId())) |
| 1052 |
{ |
| 1053 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() NormAddAckingNode() failure!\n"); |
| 1054 |
return false; |
| 1055 |
} |
| 1056 |
PLOG(PL_INFO, "norp: correspondent %lu created NORM session with dest %s/%hu (srcPort %hu) connected to NORM source port %hu\n", |
| 1057 |
controller.GetNormNodeId(), senderAddr.GetHostString(), controller.GetNormPort(), NormGetTxPort(norm_session), theMsg.GetSourcePort()); |
| 1058 |
|
| 1059 |
// 2) Initiate TCP connection to indicated endpoint address/port (should we do this first before setting up NORM?) |
| 1060 |
ProtoAddress destAddr; |
| 1061 |
if (!request.GetAddress(destAddr)) |
| 1062 |
{ |
| 1063 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() error: request with invalid address!\n"); |
| 1064 |
return false; |
| 1065 |
} |
| 1066 |
switch (request.GetCommand()) |
| 1067 |
{ |
| 1068 |
case ProtoPktSOCKS::Request::CONNECT: |
| 1069 |
PLOG(PL_INFO, "norp: session %u recvd remote CONNECT request dst add/port = %s/%hu\n", GetSessionId(), destAddr.GetHostString(), destAddr.GetPort()); |
| 1070 |
if (!ConnectToRemote(destAddr)) |
| 1071 |
{ |
| 1072 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() error: ConnectToRemote() failure\n"); |
| 1073 |
// TBD - send failure reply |
| 1074 |
return false; |
| 1075 |
} |
| 1076 |
break; |
| 1077 |
case ProtoPktSOCKS::Request::BIND: |
| 1078 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() error: remote SOCKS BIND not supported\n"); |
| 1079 |
// TBD - send negative reply |
| 1080 |
return false; |
| 1081 |
case ProtoPktSOCKS::Request::UDP_ASSOC: |
| 1082 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() error: remote SOCKS UDP_ASSOC not supported\n"); |
| 1083 |
// TBD - send negative reply |
| 1084 |
return false; |
| 1085 |
default: |
| 1086 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() error: invalid request command!\n"); |
| 1087 |
return false; |
| 1088 |
} |
| 1089 |
} // end if (NORM_SESSION_INVALID == norm_session) |
| 1090 |
else |
| 1091 |
{ |
| 1092 |
// TBD - verify this request matches our existing session info |
| 1093 |
} |
| 1094 |
// Note the "ConnectToRemote()" call above transitioned us to SOCKS_CONNECTING state |
| 1095 |
if (SOCKS_CONNECTING == socks_state) |
| 1096 |
{ |
| 1097 |
ASSERT(!norp_msg_timer.IsActive()); |
| 1098 |
// 3) Send a NormCmd request acknowledged (REQ_ACK) to "squelch" further request resends from remote |
| 1099 |
// (This is sent as a one-shot in response to NorpMsg::SOCKS_REQ messages) |
| 1100 |
UINT32 buffer[32/4]; |
| 1101 |
NorpMsg ack(buffer, 32, false); |
| 1102 |
ack.SetType(NorpMsg::REQ_ACK); |
| 1103 |
ack.SetSessionId(GetSessionId()); |
| 1104 |
ack.SetNodeId(controller.GetNormNodeId()); // our NORM node id |
| 1105 |
ack.SetSourcePort(NormGetTxPort(norm_session)); // Our NORM tx port number (so remote can "connect()" |
| 1106 |
ack.SetDestinationPort(NormGetRxPort(norm_session)); // Our NORM rx port (let's remote where to send NORM pkts) |
| 1107 |
struct timeval sentTime; |
| 1108 |
theMsg.GetTimestamp(sentTime); |
| 1109 |
ack.SetTimestamp(sentTime); |
| 1110 |
if (!controller.SendMessage(ack, senderAddr)) |
| 1111 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() warning: NORP REQ_ACK message transmission failed (will try again)!\n"); |
| 1112 |
} |
| 1113 |
else if (SOCKS_PUT_REPLY == socks_state) |
| 1114 |
{ |
| 1115 |
// The reply already sent upon connect was evidently not |
| 1116 |
// received (nor the REQ_ACK message(s) that were sent) |
| 1117 |
// Note calling this also resets the reply robust retransmission process |
| 1118 |
if (!PutRemoteReply()) |
| 1119 |
{ |
| 1120 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() error: unable to send SOCKS_REPLY\n"); |
| 1121 |
return false; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
else if (SOCKS_CONNECTED == socks_state) |
| 1125 |
{ |
| 1126 |
// Evidently a late arriving request, so we just ignore it |
| 1127 |
PLOG(PL_INFO, "NorpSession::OnRemoteRequest() late arriving remote SOCK_REQUEST\n"); |
| 1128 |
} |
| 1129 |
return true; |
| 1130 |
} // end NorpSession::OnRemoteRequest() |
| 1131 |
|
| 1132 |
bool NorpSession::PutRemoteReply() |
| 1133 |
{ |
| 1134 |
// Send SOCKS reply to remote NORP server via NORM command |
| 1135 |
ASSERT(IsRemoteSession()); |
| 1136 |
norp_msg.Init(); |
| 1137 |
norp_msg.SetType(NorpMsg::SOCKS_REP); |
| 1138 |
norp_msg.SetSessionId(GetSessionId()); // our session id (note was selected by request originator) |
| 1139 |
norp_msg.SetSourcePort(NormGetTxPort(norm_session)); // our NORM tx port number (so remote can "connect()" |
| 1140 |
norp_msg.SetDestinationPort(NormGetRxPort(norm_session)); // our NORM rx port (for info) |
| 1141 |
norp_msg.SetNodeId(controller.GetNormNodeId()); // our NORM node id |
| 1142 |
ProtoPktSOCKS::Reply reply(norp_msg.AccessContentPtr(), norp_msg.GetBufferLength() - norp_msg.GetLength(), false); |
| 1143 |
reply.SetVersion(5); |
| 1144 |
reply.SetType(ProtoPktSOCKS::Reply::SUCCESS); |
| 1145 |
reply.SetAddress(socks_remote_socket.GetSourceAddr()); |
| 1146 |
norp_msg.SetContentLength(reply.GetLength()); |
| 1147 |
|
| 1148 |
// If an existing ack or reply cmd is pending, we cancel it. This |
| 1149 |
// resets the robust transmission of the reply |
| 1150 |
if (norp_msg_timer.IsActive()) norp_msg_timer.Deactivate(); |
| 1151 |
struct timeval currentTime; |
| 1152 |
ProtoSystemTime(currentTime); |
| 1153 |
norp_msg.SetTimestamp(currentTime); |
| 1154 |
if (!controller.SendMessage(norp_msg, norp_remote_addr)) |
| 1155 |
PLOG(PL_ERROR, "NorpSession::OnRemoteRequest() warning: NORP REQ_ACK message transmission failed (will try again)!\n"); |
| 1156 |
norp_msg_timer.SetInterval(2.0 * norp_rtt_estimate); |
| 1157 |
ActivateTimer(norp_msg_timer); |
| 1158 |
socks_state = SOCKS_PUT_REPLY; |
| 1159 |
return true; |
| 1160 |
} // end NorpSession::PutRemoteReply() |
| 1161 |
|
| 1162 |
bool NorpSession::OnRemoteReplyAcknowledgment(const NorpMsg& theMsg) |
| 1163 |
{ |
| 1164 |
// Note we update our RTT estimate in either case |
| 1165 |
ProtoTime recvTime; |
| 1166 |
recvTime.GetCurrentTime(); |
| 1167 |
ProtoTime sentTime; |
| 1168 |
theMsg.GetTimestamp(sentTime); |
| 1169 |
|
| 1170 |
if (SOCKS_PUT_REPLY == socks_state) |
| 1171 |
{ |
| 1172 |
// Initial rtt measurement |
| 1173 |
norp_rtt_estimate = ProtoTime::Delta(recvTime, sentTime); |
| 1174 |
PLOG(PL_INFO, "norp node %u starting sender probing (and sender) ...\n", controller.GetNormNodeId()); |
| 1175 |
if (Norp::NORM_FIXED != controller.GetNormCC()) |
| 1176 |
NormSetCongestionControl(norm_session, true); |
| 1177 |
if (!NormStartSender(norm_session, GetSessionId(), NORM_BUFFER_SIZE, controller.GetNormSegmentSize(), |
| 1178 |
controller.GetNormBlockSize(), controller.GetNormParityCount())) |
| 1179 |
{ |
| 1180 |
PLOG(PL_ERROR, "NorpSession::OnRemoteReplyAcknowledgment() NormStartSender() failure!\n"); |
| 1181 |
return false; |
| 1182 |
} |
| 1183 |
//NormSetTxSocketBuffer(norm_session, 4096); |
| 1184 |
NormSetFlowControl(norm_session, 0.0); // disable timer-based flow control since we are ACK-limiting writes to stream |
| 1185 |
norm_segment_size = controller.GetNormSegmentSize(); |
| 1186 |
norm_stream_buffer_max = ComputeNormStreamBufferSegmentCount(NORM_BUFFER_SIZE, norm_segment_size, controller.GetNormBlockSize()); |
| 1187 |
norm_stream_buffer_max -= controller.GetNormBlockSize(); // a little safety margin |
| 1188 |
norm_stream_buffer_count = 0; |
| 1189 |
norm_stream_bytes_remain = 0; |
| 1190 |
norm_watermark_pending = false; |
| 1191 |
if (NORM_OBJECT_INVALID == (norm_tx_stream = NormStreamOpen(norm_session, NORM_BUFFER_SIZE))) |
| 1192 |
{ |
| 1193 |
PLOG(PL_ERROR, "NorpSession::OnRemoteReplyAcknowledgment() NormStreamOpen() failure!\n"); |
| 1194 |
return false; |
| 1195 |
} |
| 1196 |
if (norp_msg_timer.IsActive()) norp_msg_timer.Deactivate(); // This cancels the robust (repeated) remote reply transmission |
| 1197 |
socks_remote_socket.StartInputNotification(); // enable flow of data from remote TCP socket |
| 1198 |
socks_state = SOCKS_CONNECTED; |
| 1199 |
remote_index = remote_pending = 0; |
| 1200 |
} |
| 1201 |
else |
| 1202 |
{ |
| 1203 |
ASSERT(norp_rtt_estimate > 0.0); |
| 1204 |
// Only use duplicative ACKs to update RTT estimate via EWMA |
| 1205 |
double rttNew = ProtoTime::Delta(recvTime, sentTime); |
| 1206 |
norp_rtt_estimate = 0.5*(norp_rtt_estimate + rttNew); // TBD - is this a reasonable "smoothing" factor |
| 1207 |
} |
| 1208 |
// Note it's OK if a bona fide measured RTT is less than NORP_RTT_MIN |
| 1209 |
if (norp_rtt_estimate <= 0.0) |
| 1210 |
{ |
| 1211 |
norp_rtt_estimate = NORP_RTT_MIN; |
| 1212 |
} |
| 1213 |
else if (norp_rtt_estimate > NORP_RTT_MAX) |
| 1214 |
{ |
| 1215 |
PLOG(PL_WARN, "NorpSession::OnRemoteReplyAcknowledgment() warning: measured RTT exceeds max value of %lf sec.\n", NORP_RTT_MAX); |
| 1216 |
norp_rtt_estimate = NORP_RTT_MAX; |
| 1217 |
} |
| 1218 |
return true; |
| 1219 |
} // end NorpSession::OnRemoteReplyAcknowledgment() |
| 1220 |
|
| 1221 |
bool NorpSession::ConnectToRemote(const ProtoAddress& destAddr) |
| 1222 |
{ |
| 1223 |
// Make a direct TCP connection to remote destAddr |
| 1224 |
ASSERT(socks_remote_socket.IsClosed()); |
| 1225 |
if (!socks_remote_socket.Connect(destAddr)) |
| 1226 |
{ |
| 1227 |
PLOG(PL_ERROR, "NorpSession::ConnectToRemote() error: remote socket connect() failure!\n"); |
| 1228 |
return false; |
| 1229 |
} |
| 1230 |
ASSERT(!socks_remote_socket.IsConnected()); // TBD - handle immediate connection case??? |
| 1231 |
PLOG(PL_DETAIL, "NorpSession::ConnectToRemote() connection to %s/%hu initiated (connected:%d)...\n", |
| 1232 |
destAddr.GetHostString(), destAddr.GetPort(), socks_remote_socket.IsConnected()); |
| 1233 |
if (socks_client_socket.IsOpen()) |
| 1234 |
socks_client_socket.StopInputNotification(); // don't accept more from client until connected |
| 1235 |
socks_state = SOCKS_CONNECTING; |
| 1236 |
return true; |
| 1237 |
} // end NorpSession::ConnectToRemote() |
| 1238 |
|
| 1239 |
bool NorpSession::BindRemote(UINT16 bindPort) |
| 1240 |
{ |
| 1241 |
// Open our remote socket in unbound state |
| 1242 |
if (!socks_remote_socket.Open(0, proxy_addr.GetType(), false)) |
| 1243 |
{ |
| 1244 |
PLOG(PL_ERROR, "NorpSession::BindRemote() error: unable to open socket for remote connection\n"); |
| 1245 |
return false; |
| 1246 |
} |
| 1247 |
// Typically, the BIND request should be for port zero (any available port) |
| 1248 |
// But, in the case that it is not, we set port reuse for when the norp server |
| 1249 |
// is set up as a "local, loopback" server |
| 1250 |
if (0 != bindPort) socks_remote_socket.SetReuse(true); |
| 1251 |
bool result = socks_remote_socket.Bind(bindPort, &proxy_addr); |
| 1252 |
if (!result) result = socks_remote_socket.Bind(0, &proxy_addr); |
| 1253 |
if (!result) |
| 1254 |
{ |
| 1255 |
PLOG(PL_ERROR, "NorpSession::BindRemote() error: unable to bind for remote connection\n"); |
| 1256 |
return false; |
| 1257 |
} |
| 1258 |
if (!socks_remote_socket.Listen()) |
| 1259 |
{ |
| 1260 |
PLOG(PL_ERROR, "NorpSession::BindRemote() error: unable to listen for remote connection\n"); |
| 1261 |
return false; |
| 1262 |
} |
| 1263 |
// Construct the first reply to inform client of bind addr |
| 1264 |
ProtoPktSOCKS::Reply reply(remote_buffer, SOCKS_BUFFER_SIZE, false); |
| 1265 |
reply.SetVersion(5); |
| 1266 |
reply.SetType(ProtoPktSOCKS::Reply::SUCCESS); |
| 1267 |
ProtoAddress bindAddr = proxy_addr; |
| 1268 |
bindAddr.SetPort(socks_remote_socket.GetPort()); |
| 1269 |
PLOG(PL_INFO,"norp proxy listening on addr/port = %s/%hu\n", bindAddr.GetHostString(), bindAddr.GetPort()); |
| 1270 |
reply.SetAddress(bindAddr); |
| 1271 |
remote_pending = reply.GetLength(); |
| 1272 |
remote_index = 0; |
| 1273 |
socks_state = SOCKS_PUT_REPLY; |
| 1274 |
return PutClientReply(); |
| 1275 |
} // NorpSession::BindRemote() |
| 1276 |
|
| 1277 |
bool NorpSession::OpenUdpRelay() |
| 1278 |
{ |
| 1279 |
// 1) open udp_relay_socket |
| 1280 |
if (!udp_relay_socket.Open()) |
| 1281 |
{ |
| 1282 |
PLOG(PL_ERROR, "NorpSession::OpenUdpRelay() error: udp_relay_socket open failed!\n"); |
| 1283 |
return false; |
| 1284 |
} |
| 1285 |
|
| 1286 |
// 2) Build the SOCKS reply into the remote_buffer ... |
| 1287 |
ProtoPktSOCKS::Reply reply(remote_buffer, SOCKS_BUFFER_SIZE, false); |
| 1288 |
reply.SetVersion(5); |
| 1289 |
reply.SetType(ProtoPktSOCKS::Reply::SUCCESS); |
| 1290 |
// We use the bind (local source) addr of our client socket as the SOCKS reply BIND.ADDR |
| 1291 |
// and the port number of our udp relay socket as the SOCKS reply BIND.PORT |
| 1292 |
ProtoAddress bindAddr = socks_client_socket.GetSourceAddr(); |
| 1293 |
bindAddr.SetPort(udp_relay_socket.GetPort()); |
| 1294 |
reply.SetAddress(bindAddr); |
| 1295 |
PLOG(PL_INFO, "udp relay bind addr = %s/%hu\n", bindAddr.GetHostString(), bindAddr.GetPort()); |
| 1296 |
remote_pending = reply.GetLength(); |
| 1297 |
remote_index = 0; |
| 1298 |
socks_state = SOCKS_PUT_REPLY; |
| 1299 |
return PutClientReply(); |
| 1300 |
|
| 1301 |
} // end NorpSession::OpenUdpRelay() |
| 1302 |
|
| 1303 |
bool NorpSession::PutClientReply() |
| 1304 |
{ |
| 1305 |
// Transmit SOCKS reply that's in "remote_buffer" to local SOCKS client |
| 1306 |
// (This buffering and indexing allows for possibly multiple TCP sockets |
| 1307 |
// writes to send the reply, even though that is not very likely given a |
| 1308 |
// SOCKS reply is smaller than the minimum IP MTU of 512 bytes) |
| 1309 |
ASSERT(SOCKS_PUT_REPLY == socks_state); |
| 1310 |
unsigned int numBytes = remote_pending - remote_index; |
| 1311 |
if (socks_client_socket.Send(((char*)remote_buffer)+remote_index, numBytes)) |
| 1312 |
{ |
| 1313 |
remote_index += numBytes; |
| 1314 |
if (remote_index < remote_pending) |
| 1315 |
{ |
| 1316 |
// Still more to send, keep output notification going |
| 1317 |
socks_client_socket.StartOutputNotification(); |
| 1318 |
} |
| 1319 |
else |
| 1320 |
{ |
| 1321 |
// Reply fully sent, now transition to SOCKS_CONNECTED state |
| 1322 |
PLOG(PL_DETAIL, "NorpSession::PutClientReply() session %hu:%08x SOCKS reply sent to client ...\n", |
| 1323 |
GetSessionId(), GetOriginatorId()); |
| 1324 |
remote_index = remote_pending = 0; |
| 1325 |
socks_client_socket.StopOutputNotification(); |
| 1326 |
// Start accepting recv data from client or remote |
| 1327 |
client_index = client_pending = 0; |
| 1328 |
socks_client_socket.StartInputNotification(); |
| 1329 |
if (socks_remote_socket.IsOpen()) |
| 1330 |
socks_remote_socket.StartInputNotification(); |
| 1331 |
socks_state = SOCKS_CONNECTED; |
| 1332 |
} |
| 1333 |
} |
| 1334 |
else |
| 1335 |
{ |
| 1336 |
PLOG(PL_ERROR, "NorpSession::PutClientReply() error: remote socket send failure\n"); |
| 1337 |
return false; |
| 1338 |
} |
| 1339 |
return true; |
| 1340 |
} // end NorpSession::PutClientReply() |
| 1341 |
|
| 1342 |
bool NorpSession::GetClientData() |
| 1343 |
{ |
| 1344 |
if (0 != client_pending) return true; |
| 1345 |
// Get whatever data is ready from client and start sending to remote |
| 1346 |
unsigned int numBytes = SOCKS_BUFFER_SIZE; |
| 1347 |
if (IsRemoteSession() && !IsRemoteOriginator()) |
| 1348 |
{ |
| 1349 |
// We're the remote correspondent, so get client data from remote originator (via NORM) |
| 1350 |
if (norm_rx_pending) |
| 1351 |
{ |
| 1352 |
ASSERT(0 == client_pending); |
| 1353 |
if (NormStreamRead(norm_rx_stream, (char*)client_buffer, &numBytes)) |
| 1354 |
{ |
| 1355 |
client_pending = numBytes; |
| 1356 |
client_index = 0; |
| 1357 |
if (numBytes < SOCKS_BUFFER_SIZE) norm_rx_pending = false; |
| 1358 |
PLOG(PL_DETAIL, "NorpSession::GetClientData() correspondent read %lu bytes from NORM stream ..\n", numBytes); |
| 1359 |
if (0 != numBytes) |
| 1360 |
return PutRemoteData(); |
| 1361 |
// else wait for NORM_RX_OBJECT_UPDATED notification |
| 1362 |
} |
| 1363 |
else |
| 1364 |
{ |
| 1365 |
PLOG(PL_ERROR, "NorpSession::GetClientData() NormStreamRead() error: break in stream!\n"); |
| 1366 |
return false; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
// else wait for NORM_RX_OBJECT_UPDATED notification |
| 1370 |
} |
| 1371 |
else |
| 1372 |
{ |
| 1373 |
// Originator server so get data from SOCKS client via TCP |
| 1374 |
ASSERT(0 == client_pending); |
| 1375 |
if (socks_client_socket.Recv((char*)client_buffer, numBytes)) |
| 1376 |
{ |
| 1377 |
client_pending = numBytes; |
| 1378 |
client_index = 0; |
| 1379 |
if (0 != numBytes) |
| 1380 |
{ |
| 1381 |
PLOG(PL_DETAIL, "NorpSession::GetClientData() originator read %lu bytes from SOCKS client ...\n", numBytes); |
| 1382 |
socks_client_socket.StopInputNotification(); |
| 1383 |
return PutRemoteData(); |
| 1384 |
} |
| 1385 |
// else socket will close itself when appropriate |
| 1386 |
} |
| 1387 |
else |
| 1388 |
{ |
| 1389 |
PLOG(PL_ERROR, "NorpSession::GetClientData() client socket recv failure!\n"); |
| 1390 |
return false; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
return true; |
| 1394 |
} // end NorpSession::GetClientData() |
| 1395 |
|
| 1396 |
bool NorpSession::PutClientData() |
| 1397 |
{ |
| 1398 |
// Transmit data that's in "remote_buffer" to client |
| 1399 |
unsigned int numBytes = remote_pending - remote_index; |
| 1400 |
if (IsRemoteSession() && !IsRemoteOriginator()) |
| 1401 |
{ |
| 1402 |
if (0 != numBytes) |
| 1403 |
{ |
| 1404 |
numBytes = WriteToNormStream(((char*)remote_buffer)+remote_index, numBytes); |
| 1405 |
} |
| 1406 |
PLOG(PL_DETAIL, "correspondent wrote %u bytes to NORM stream ...\n", numBytes); |
| 1407 |
remote_index += numBytes; |
| 1408 |
if (remote_index == remote_pending) |
| 1409 |
{ |
| 1410 |
// All pending client data has been enqueued to remote |
| 1411 |
remote_index = remote_pending = 0; |
| 1412 |
if (SOCKS_CONNECTED == socks_state) |
| 1413 |
{ |
| 1414 |
FlushNormStream(false, NORM_FLUSH_ACTIVE); |
| 1415 |
socks_remote_socket.StartInputNotification(); |
| 1416 |
} |
| 1417 |
else |
| 1418 |
{ |
| 1419 |
ASSERT(SOCKS_SHUTDOWN == socks_state); |
| 1420 |
// No more data to put, so gracefully close norm_tx_stream |
| 1421 |
NormStreamClose(norm_tx_stream, true); // Note our "trick" here to do a graceful close, _then_ watermark |
| 1422 |
NormSetWatermark(norm_session, norm_tx_stream, true); // future NORM API will add "bool watermark" option to graceful close |
| 1423 |
} |
| 1424 |
} |
| 1425 |
// else wait for NORM_TX_QUEUE_VACANCY / EMPTY or watermark acknowledgemnt completion |
| 1426 |
} |
| 1427 |
else |
| 1428 |
{ |
| 1429 |
// Relay data to local SOCKS client via TCP |
| 1430 |
if (0 != numBytes) |
| 1431 |
{ |
| 1432 |
if (!socks_client_socket.Send(((char*)remote_buffer)+remote_index, numBytes)) |
| 1433 |
{ |
| 1434 |
if (SOCKS_CONNECTED == socks_state) |
| 1435 |
{ |
| 1436 |
PLOG(PL_ERROR, "NorpSession::PutClientData() error: remote socket send failure\n"); |
| 1437 |
return false; |
| 1438 |
} |
| 1439 |
else // SOCKS_SHUTDOWN == socks_state |
| 1440 |
{ |
| 1441 |
PLOG(PL_WARN, "NorpSession::PutClientData() warning: remote socket send failure\n"); |
| 1442 |
return true; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
} |
| 1446 |
PLOG(PL_DETAIL, "originator wrote %lu bytes to client TCP socket ...\n", numBytes); |
| 1447 |
remote_index += numBytes; |
| 1448 |
if (remote_index < remote_pending) |
| 1449 |
{ |
| 1450 |
// Still more data to send, keep output notification going |
| 1451 |
socks_client_socket.StartOutputNotification(); |
| 1452 |
} |
| 1453 |
else |
| 1454 |
{ |
| 1455 |
// Data fully sent, re-enable remote_socket input notifcation |
| 1456 |
remote_index = remote_pending = 0; |
| 1457 |
socks_client_socket.StopOutputNotification(); |
| 1458 |
if (SOCKS_CONNECTED == socks_state) |
| 1459 |
{ |
| 1460 |
if (IsRemoteSession()) |
| 1461 |
{ |
| 1462 |
// We're the remote session originator, so get more data if ready |
| 1463 |
if (norm_rx_pending) |
| 1464 |
return GetRemoteData(); |
| 1465 |
// else wait for NORM_RX_OBJECT_UPDATED notification |
| 1466 |
} |
| 1467 |
else |
| 1468 |
{ |
| 1469 |
// Restart accepting recv data from remote |
| 1470 |
socks_remote_socket.StartInputNotification(); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
else // SOCKS_SHUTDOWN == socks_state |
| 1474 |
{ |
| 1475 |
if (socks_client_socket.IsConnected()) |
| 1476 |
{ |
| 1477 |
socks_client_socket.Shutdown(); |
| 1478 |
} |
| 1479 |
else |
| 1480 |
{ |
| 1481 |
socks_client_socket.Close(); |
| 1482 |
if (ShutdownComplete()) Shutdown(); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
} |
| 1486 |
} |
| 1487 |
return true; |
| 1488 |
} // end NorpSession::PutClientData() |
| 1489 |
|
| 1490 |
void NorpSession::OnSocksRemoteEvent(ProtoSocket& theSocket, |
| 1491 |
ProtoSocket::Event theEvent) |
| 1492 |
{ |
| 1493 |
PLOG(PL_DETAIL, "NorpSession::OnSocksRemoteEvent("); |
| 1494 |
switch (theEvent) |
| 1495 |
{ |
| 1496 |
case ProtoSocket::INVALID_EVENT: |
| 1497 |
{ |
| 1498 |
// should never happen, just for completeness of switch cases |
| 1499 |
PLOG(PL_DETAIL, "INVALID_EVENT) ...\n"); |
| 1500 |
break; |
| 1501 |
} |
| 1502 |
case ProtoSocket::CONNECT: |
| 1503 |
{ |
| 1504 |
PLOG(PL_DETAIL, "CONNECT) ...\n"); |
| 1505 |
ASSERT(SOCKS_CONNECTING == socks_state); |
| 1506 |
PLOG(PL_DETAIL, "NorpSession::OnSocksRemoteEvent(CONNECT) session %hu:%08x connected to destination %s/%hu ...\n", |
| 1507 |
GetSessionId(), GetOriginatorId(), |
| 1508 |
socks_remote_socket.GetDestination().GetHostString(), |
| 1509 |
socks_remote_socket.GetDestination().GetPort()); |
| 1510 |
if (is_preset) |
| 1511 |
{ |
| 1512 |
// Go straight to connected state and start |
| 1513 |
// accepting recv data from client or remote |
| 1514 |
client_index = client_pending = 0; |
| 1515 |
socks_client_socket.StartInputNotification(); |
| 1516 |
remote_index = remote_pending = 0; |
| 1517 |
socks_state = SOCKS_CONNECTED; |
| 1518 |
} |
| 1519 |
else |
| 1520 |
{ |
| 1521 |
// Don't get data from remote until reply sent to client |
| 1522 |
socks_remote_socket.StopInputNotification(); |
| 1523 |
socks_state = SOCKS_PUT_REPLY; |
| 1524 |
if (IsRemoteSession()) |
| 1525 |
{ |
| 1526 |
if (!PutRemoteReply()) |
| 1527 |
{ |
| 1528 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent(CONNECT) error: SOCKS CONNECT remote reply send failure!\n"); |
| 1529 |
Shutdown(); |
| 1530 |
} |
| 1531 |
} |
| 1532 |
else |
| 1533 |
{ |
| 1534 |
// Build reply in "remote_buffer" to send to client |
| 1535 |
ProtoPktSOCKS::Reply reply(remote_buffer, SOCKS_BUFFER_SIZE, false); |
| 1536 |
reply.SetVersion(5); |
| 1537 |
reply.SetType(ProtoPktSOCKS::Reply::SUCCESS); |
| 1538 |
reply.SetAddress(socks_remote_socket.GetSourceAddr()); |
| 1539 |
remote_pending = reply.GetLength(); |
| 1540 |
remote_index = 0; |
| 1541 |
// Send SOCKS reply to locally connected client |
| 1542 |
if (!PutClientReply()) |
| 1543 |
{ |
| 1544 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent(CONNECT) error: SOCKS CONNECT local reply send failure!\n"); |
| 1545 |
Shutdown(); |
| 1546 |
} |
| 1547 |
} |
| 1548 |
} |
| 1549 |
break; |
| 1550 |
} |
| 1551 |
case ProtoSocket::ACCEPT: |
| 1552 |
{ |
| 1553 |
PLOG(PL_DETAIL, "ACCEPT) ...\n"); |
| 1554 |
// This will happen for a listening socket put in place for BIND request |
| 1555 |
// (we need to send a second reply indicating the remote connection accepted) |
| 1556 |
ASSERT(SOCKS_CONNECTED == socks_state); |
| 1557 |
if (!socks_remote_socket.Accept()) |
| 1558 |
{ |
| 1559 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent(ACCEPT) error: SOCKS BIND accept failure!\n"); |
| 1560 |
Shutdown(); |
| 1561 |
break; |
| 1562 |
} |
| 1563 |
// TBD - should check that the accepted remote dst addr/port is what it's supposed to be |
| 1564 |
ProtoPktSOCKS::Reply reply(remote_buffer, SOCKS_BUFFER_SIZE, false); |
| 1565 |
reply.SetVersion(5); |
| 1566 |
reply.SetType(ProtoPktSOCKS::Reply::SUCCESS); |
| 1567 |
reply.SetAddress(socks_remote_socket.GetDestination()); |
| 1568 |
remote_pending = reply.GetLength(); |
| 1569 |
remote_index = 0; |
| 1570 |
socks_state = SOCKS_PUT_REPLY; |
| 1571 |
if (!PutClientReply()) |
| 1572 |
{ |
| 1573 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent(ACCEPT) error: SOCKS BIND reply send failure!\n"); |
| 1574 |
Shutdown(); |
| 1575 |
} |
| 1576 |
break; |
| 1577 |
} |
| 1578 |
case ProtoSocket::SEND: |
| 1579 |
PLOG(PL_DETAIL, "SEND) ...\n"); |
| 1580 |
// write pending data from client_buffer to remote |
| 1581 |
if (SOCKS_PUT_REPLY == socks_state) |
| 1582 |
{ |
| 1583 |
if (!PutRemoteReply()) |
| 1584 |
{ |
| 1585 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent() error: PutRemoteReply() failure!\n"); |
| 1586 |
Shutdown(); |
| 1587 |
} |
| 1588 |
} |
| 1589 |
else if (!PutRemoteData()) |
| 1590 |
{ |
| 1591 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent() error: PutRemoteData() failure!\n"); |
| 1592 |
Shutdown(); |
| 1593 |
} |
| 1594 |
break; |
| 1595 |
case ProtoSocket::RECV: |
| 1596 |
{ |
| 1597 |
PLOG(PL_DETAIL, "RECV) ...\n"); |
| 1598 |
if (0 == remote_pending) // we need this check because of occasional extra notifications with old "protolib" |
| 1599 |
{ |
| 1600 |
if (!GetRemoteData()) |
| 1601 |
{ |
| 1602 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent(SEND) error: GetRemoteData() failure!\n"); |
| 1603 |
Shutdown(); |
| 1604 |
} |
| 1605 |
} |
| 1606 |
break; |
| 1607 |
} |
| 1608 |
case ProtoSocket::DISCONNECT: |
| 1609 |
PLOG(PL_DETAIL, "DISCONNECT) ...\n"); |
| 1610 |
switch (socks_state) |
| 1611 |
{ |
| 1612 |
case SOCKS_VOID: |
| 1613 |
case SOCKS_IDLE: |
| 1614 |
case SOCKS_GET_AUTH_REQ: |
| 1615 |
case SOCKS_PUT_AUTH_REP: |
| 1616 |
case SOCKS_GET_REQUEST: |
| 1617 |
case SOCKS_PUT_REQUEST: |
| 1618 |
ASSERT(0); |
| 1619 |
break; |
| 1620 |
case SOCKS_CONNECTING: |
| 1621 |
case SOCKS_PUT_REPLY: |
| 1622 |
case SOCKS_GET_REPLY: |
| 1623 |
PLOG(PL_ERROR, "NorpSession::OnSocksRemoteEvent() error: premature SOCKS client disconnect!\n"); |
| 1624 |
Shutdown(); // unexpected, premature client disconnect |
| 1625 |
break; |
| 1626 |
case SOCKS_CONNECTED: |
| 1627 |
socks_state = SOCKS_SHUTDOWN; |
| 1628 |
if (0 == remote_pending) |
| 1629 |
PutClientData(); // to shutdown NORM stream or socks_client_socket |
| 1630 |
case SOCKS_SHUTDOWN: |
| 1631 |
// Client finished sending (and receiving) data |
| 1632 |
theSocket.Close(); |
| 1633 |
if (ShutdownComplete()) |
| 1634 |
Close(); |
| 1635 |
// else further shutdown actions pending |
| 1636 |
break; |
| 1637 |
} |
| 1638 |
break; |
| 1639 |
|
| 1640 |
case ProtoSocket::EXCEPTION: |
| 1641 |
PLOG(PL_DETAIL, "EXCEPTION) ...\n"); // shouldn't happen |
| 1642 |
break; |
| 1643 |
|
| 1644 |
case ProtoSocket::ERROR_: |
| 1645 |
PLOG(PL_DETAIL, "ERROR_) ...\n"); |
| 1646 |
Shutdown(); |
| 1647 |
break; |
| 1648 |
} |
| 1649 |
} // end NorpSession::OnSocksRemoteEvent() |
| 1650 |
|
| 1651 |
bool NorpSession::GetRemoteData() |
| 1652 |
{ |
| 1653 |
// Receive data from remote socket into remote_buffer |
| 1654 |
// Get some data from remote and start sending it to the client |
| 1655 |
ASSERT(0 == remote_pending); |
| 1656 |
unsigned int numBytes = SOCKS_BUFFER_SIZE; |
| 1657 |
if (IsRemoteSession() && IsRemoteOriginator()) |
| 1658 |
{ |
| 1659 |
// We're the remote correspondent (i.e. not the remote session originator) |
| 1660 |
if (norm_rx_pending) |
| 1661 |
{ |
| 1662 |
ASSERT(0 == remote_pending); |
| 1663 |
if (NormStreamRead(norm_rx_stream, (char*)remote_buffer, &numBytes)) |
| 1664 |
{ |
| 1665 |
remote_pending = numBytes; |
| 1666 |
remote_index = 0; |
| 1667 |
if (numBytes < SOCKS_BUFFER_SIZE) norm_rx_pending = false; |
| 1668 |
if (0 != numBytes) |
| 1669 |
return PutClientData(); |
| 1670 |
// else wait for NORM_RX_OBJECT_UPDATED notification |
| 1671 |
} |
| 1672 |
else |
| 1673 |
{ |
| 1674 |
PLOG(PL_ERROR, "NorpSession::GetRemoteData() NormStreamRead() error: break in stream!\n"); |
| 1675 |
return false; |
| 1676 |
} |
| 1677 |
} |
| 1678 |
// else wait for NORM_RX_OBJECT_UPDATED notification |
| 1679 |
} |
| 1680 |
else |
| 1681 |
{ |
| 1682 |
// Remote correspondent or direct-connect, so use TCP |
| 1683 |
ASSERT(0 == remote_pending); |
| 1684 |
if (socks_remote_socket.Recv((char*)remote_buffer, numBytes)) |
| 1685 |
{ |
| 1686 |
remote_pending = numBytes; |
| 1687 |
remote_index = 0; |
| 1688 |
if (0 != numBytes) |
| 1689 |
{ |
| 1690 |
PLOG(PL_DETAIL, "NorpSession::GetClientData() originator read %lu bytes from remote ...\n", numBytes); |
| 1691 |
socks_remote_socket.StopInputNotification(); |
| 1692 |
return PutClientData(); |
| 1693 |
} |
| 1694 |
// else socket will disconnect itself and session will close |
| 1695 |
} |
| 1696 |
else |
| 1697 |
{ |
| 1698 |
PLOG(PL_ERROR, "NorpSession::GetRemoteData() error: remote socket recv failure!\n"); |
| 1699 |
return false; |
| 1700 |
} |
| 1701 |
} |
| 1702 |
return true; |
| 1703 |
} // end NorpSession::GetRemoteData() |
| 1704 |
|
| 1705 |
bool NorpSession::PutRemoteData() |
| 1706 |
{ |
| 1707 |
// Transmit data in "client_buffer" to remote |
| 1708 |
unsigned int numBytes = client_pending - client_index; |
| 1709 |
if (IsRemoteSession() && IsRemoteOriginator()) |
| 1710 |
{ |
| 1711 |
if (0 != numBytes) |
| 1712 |
numBytes = WriteToNormStream(((char*)client_buffer)+client_index, numBytes); |
| 1713 |
PLOG(PL_DETAIL, "originator wrote %u bytes to NORM stream ...\n", numBytes); |
| 1714 |
client_index += numBytes; |
| 1715 |
if (client_index == client_pending) |
| 1716 |
{ |
| 1717 |
// All pending client data has been enqueued to remote |
| 1718 |
client_index = client_pending = 0; |
| 1719 |
if (SOCKS_CONNECTED == socks_state) |
| 1720 |
{ |
| 1721 |
FlushNormStream(false, NORM_FLUSH_ACTIVE); |
| 1722 |
socks_client_socket.StartInputNotification(); // to get more data |
| 1723 |
} |
| 1724 |
else |
| 1725 |
{ |
| 1726 |
ASSERT(SOCKS_SHUTDOWN == socks_state); |
| 1727 |
// gracefully close our tx stream |
| 1728 |
NormStreamClose(norm_tx_stream, true); // Note our "trick" here to do a graceful close, _then_ watermark |
| 1729 |
NormSetWatermark(norm_session, norm_tx_stream, true); // future NORM API will add "bool watermark" option to graceful close |
| 1730 |
} |
| 1731 |
} |
| 1732 |
// else wait for NORM_TX_QUEUE_VACANCY / EMPTY or watermark acknowledgment completion |
| 1733 |
} |
| 1734 |
else |
| 1735 |
{ |
| 1736 |
// Use TCP to send data directly to remote endpoint |
| 1737 |
if (0 != numBytes) |
| 1738 |
{ |
| 1739 |
if (!socks_remote_socket.Send(((char*)client_buffer)+client_index, numBytes)) |
| 1740 |
{ |
| 1741 |
PLOG(PL_ERROR, "NorpSession::PutRemoteData() error: remote TCP socket send failure\n"); |
| 1742 |
return false; |
| 1743 |
} |
| 1744 |
} |
| 1745 |
PLOG(PL_DETAIL, "%s wrote %lu bytes to remote TCP socket ...\n", IsRemoteSession() ? "correspondent" : "server", numBytes); |
| 1746 |
client_index += numBytes; |
| 1747 |
if (client_index < client_pending) |
| 1748 |
{ |
| 1749 |
socks_remote_socket.StartOutputNotification(); |
| 1750 |
} |
| 1751 |
else |
| 1752 |
{ |
| 1753 |
// All pending client data has been sent to remote |
| 1754 |
socks_remote_socket.StopOutputNotification(); |
| 1755 |
client_index = client_pending = 0; |
| 1756 |
if (SOCKS_CONNECTED == socks_state) |
| 1757 |
{ |
| 1758 |
if (IsRemoteSession()) |
| 1759 |
{ |
| 1760 |
// We're the remote correspondent (i.e. not the remote session originator) |
| 1761 |
if (norm_rx_pending) |
| 1762 |
return GetClientData(); |
| 1763 |
// else wait for NORM_RX_OBJECT_UPDATED notification |
| 1764 |
} |
| 1765 |
else |
| 1766 |
{ |
| 1767 |
socks_client_socket.StartInputNotification(); // prompt for more from client |
| 1768 |
} |
| 1769 |
} |
| 1770 |
else |
| 1771 |
{ |
| 1772 |
ASSERT(SOCKS_SHUTDOWN == socks_state); |
| 1773 |
if (socks_remote_socket.IsConnected()) |
| 1774 |
{ |
| 1775 |
socks_remote_socket.Shutdown(); |
| 1776 |
} |
| 1777 |
else |
| 1778 |
{ |
| 1779 |
socks_remote_socket.Close(); |
| 1780 |
if (ShutdownComplete()) Shutdown(); |
| 1781 |
} |
| 1782 |
} |
| 1783 |
} |
| 1784 |
} |
| 1785 |
return true; |
| 1786 |
} // end NorpSession::PutRemoteData() |
| 1787 |
|
| 1788 |
|
| 1789 |
void NorpSession::OnUdpRelayEvent(ProtoSocket& theSocket, |
| 1790 |
ProtoSocket::Event theEvent) |
| 1791 |
{ |
| 1792 |
switch (theEvent) |
| 1793 |
{ |
| 1794 |
case ProtoSocket::RECV: |
| 1795 |
{ |
| 1796 |
UINT32 buffer[8192/4]; |
| 1797 |
ProtoAddress srcAddr; |
| 1798 |
char* bufPtr = ((char*)buffer) + 22; // allows room for IPv6 header to be applied |
| 1799 |
unsigned int numBytes = (8192 - 22); |
| 1800 |
while (udp_relay_socket.RecvFrom(bufPtr, numBytes, srcAddr)) |
| 1801 |
{ |
| 1802 |
if (0 == numBytes) break; // no more data to read |
| 1803 |
if (srcAddr.IsEqual(udp_client_addr)) |
| 1804 |
{ |
| 1805 |
|
| 1806 |
// Packet is from client, so relay it ... |
| 1807 |
// (First need to parse SOCKS header to get destination, etc |
| 1808 |
// The cast up to UINT32* here is safe because UdpRequest uses byte boundaries |
| 1809 |
ProtoPktSOCKS::UdpRequest udpReq((UINT32*)bufPtr, numBytes); |
| 1810 |
if (0 != udpReq.GetFrag()) |
| 1811 |
{ |
| 1812 |
PLOG(PL_ERROR, "NorpSession::OnUdpRelayEvent() received fragmented packet from client!" |
| 1813 |
" (fragmentation not yet supported.)\n"); |
| 1814 |
numBytes = (8192 - 22); // reset "numBytes" |
| 1815 |
continue; // get next packet |
| 1816 |
} |
| 1817 |
ProtoAddress dstAddr; |
| 1818 |
if (!udpReq.GetAddress(dstAddr)) |
| 1819 |
{ |
| 1820 |
PLOG(PL_ERROR, "NorpSession::OnUdpRelayEvent() received packet with bad destination address from client!\n"); |
| 1821 |
numBytes = 8192; |
| 1822 |
continue; // get next packet |
| 1823 |
} |
| 1824 |
// TBD - for NORM relaying we need to establish a new child NorpSession to hand |
| 1825 |
// the packet stream off to for each unique UDP destination. Since UDP is |
| 1826 |
// connectionless, we will need to have some rules of motion regarding |
| 1827 |
// timeout, etc of the child session? We will have to buffer incoming UDP |
| 1828 |
// packets while the child NorpSession is being setup. We could use the new |
| 1829 |
// NormSocket APIs to signal setup with a SOCKS handshake relayed in-band |
| 1830 |
// at the NORM connection startup .... |
| 1831 |
|
| 1832 |
|
| 1833 |
// Relay the packet (TBD - add buffering, etc?) |
| 1834 |
unsigned int bytesSent = udpReq.GetDataLength(); |
| 1835 |
if (!udp_relay_socket.SendTo(udpReq.GetDataPtr(), bytesSent, dstAddr) || (0 == bytesSent)) |
| 1836 |
{ |
| 1837 |
PLOG(PL_ERROR, "NorpSession::OnUdpRelayEvent() error: unable to send packet to %s/%hu!\n", |
| 1838 |
dstAddr.GetHostString(), dstAddr.GetPort()); |
| 1839 |
numBytes = (8192 - 22); // reset "numBytes" |
| 1840 |
continue; // get next packet |
| 1841 |
} |
| 1842 |
} |
| 1843 |
else |
| 1844 |
{ |
| 1845 |
// TBD - should we make sure the dest addr was for our client? |
| 1846 |
// Relay inbound packet to SOCKS client |
| 1847 |
// Note we left room for our SOCKS UdpRequest header to be applied in front of payload data |
| 1848 |
unsigned int headerOffset = 0; |
| 1849 |
if (ProtoAddress::IPv4 == srcAddr.GetType()) |
| 1850 |
headerOffset = 12; // IPv4 addr is 12 bytes shorter than IPv6 |
| 1851 |
else if (ProtoAddress::IPv6 != srcAddr.GetType()) |
| 1852 |
ASSERT(0); // can only receive IPv4 or IPv6 packets1 |
| 1853 |
char* reqPtr = ((char*)buffer) + headerOffset; |
| 1854 |
// The cast up to UINT32* here is safe because UdpRequest uses byte boundaries |
| 1855 |
ProtoPktSOCKS::UdpRequest udpReq((UINT32*)reqPtr, numBytes + 22 - headerOffset, false); |
| 1856 |
udpReq.SetFragment(0); |
| 1857 |
udpReq.SetAddress(srcAddr); |
| 1858 |
udpReq.SetDataLength(numBytes); |
| 1859 |
// Send to client |
| 1860 |
unsigned int bytesSent = udpReq.GetLength(); |
| 1861 |
if (!udp_relay_socket.SendTo(reqPtr, bytesSent, udp_client_addr) || (0 == bytesSent)) |
| 1862 |
{ |
| 1863 |
PLOG(PL_ERROR, "NorpSession::OnUdpRelayEvent() error: unable to relay inbound packet to %s/%hu!\n", |
| 1864 |
udp_client_addr.GetHostString(), udp_client_addr.GetPort()); |
| 1865 |
numBytes = (8192 - 22); |
| 1866 |
continue; // get next packet |
| 1867 |
} |
| 1868 |
} |
| 1869 |
numBytes = (8192 - 22); // reset "numBytes" |
| 1870 |
} |
| 1871 |
break; |
| 1872 |
} |
| 1873 |
default: |
| 1874 |
PLOG(PL_DETAIL, "NorpSession::OnUdpRelayEvent() unhandled event type %d\n", theEvent); |
| 1875 |
break; |
| 1876 |
} // end switch(theEvent) |
| 1877 |
|
| 1878 |
} // end NorpSession::OnUdpRelayEvent() |
| 1879 |
|
| 1880 |
bool NorpSession::GetNormNodeAddress(NormNodeHandle nodeHandle, ProtoAddress& theAddr) |
| 1881 |
{ |
| 1882 |
char addrBuffer[16]; |
| 1883 |
unsigned int addrLen = 16; |
| 1884 |
UINT16 portNum; |
| 1885 |
if (NormNodeGetAddress(nodeHandle, addrBuffer, &addrLen, &portNum)) |
| 1886 |
{ |
| 1887 |
if (4 == addrLen) |
| 1888 |
theAddr.SetRawHostAddress(ProtoAddress::IPv4, addrBuffer, 4); |
| 1889 |
else |
| 1890 |
theAddr.SetRawHostAddress(ProtoAddress::IPv6, addrBuffer, 16); |
| 1891 |
theAddr.SetPort(portNum); |
| 1892 |
return true; |
| 1893 |
} |
| 1894 |
else |
| 1895 |
{ |
| 1896 |
PLOG(PL_ERROR, "NorpSession::GetNormNodeAddress() error: NormNodeGetAddress() failed!\n"); |
| 1897 |
theAddr.Invalidate(); |
| 1898 |
return false; |
| 1899 |
} |
| 1900 |
} // end NorpSession::GetNormNodeAddress() |
| 1901 |
|
| 1902 |
unsigned int NorpSession::ComputeNormStreamBufferSegmentCount(unsigned int bufferBytes, UINT16 segmentSize, UINT16 blockSize) |
| 1903 |
{ |
| 1904 |
// This same computation is performed in NormStreamObject::Open() in "normObject.cpp" |
| 1905 |
unsigned int numBlocks = bufferBytes / (blockSize * segmentSize); |
| 1906 |
if (numBlocks < 2) numBlocks = 2; // NORM enforces a 2-block minimum buffer size |
| 1907 |
return (numBlocks * blockSize); |
| 1908 |
} // end NorpSession::ComputeNormStreamBufferSegmentCount() |
| 1909 |
|
| 1910 |
unsigned int NorpSession::WriteToNormStream(const char* buffer, |
| 1911 |
unsigned int numBytes) |
| 1912 |
{ |
| 1913 |
// This method uses NormStreamWrite(), but limits writes by explicit ACK-based flow control status |
| 1914 |
if (norm_stream_buffer_count < norm_stream_buffer_max) |
| 1915 |
{ |
| 1916 |
// 1) How many buffer bytes are available? |
| 1917 |
unsigned int bytesAvailable = norm_segment_size * (norm_stream_buffer_max - norm_stream_buffer_count); |
| 1918 |
bytesAvailable -= norm_stream_bytes_remain; // unflushed segment portiomn |
| 1919 |
if (numBytes <= bytesAvailable) |
| 1920 |
{ |
| 1921 |
unsigned int totalBytes = numBytes + norm_stream_bytes_remain; |
| 1922 |
unsigned int numSegments = totalBytes / norm_segment_size; |
| 1923 |
norm_stream_bytes_remain = totalBytes % norm_segment_size; |
| 1924 |
norm_stream_buffer_count += numSegments; |
| 1925 |
} |
| 1926 |
else |
| 1927 |
{ |
| 1928 |
numBytes = bytesAvailable; |
| 1929 |
norm_stream_buffer_count = norm_stream_buffer_max; |
| 1930 |
} |
| 1931 |
// 2) Write to the stream |
| 1932 |
unsigned int bytesWritten = NormStreamWrite(norm_tx_stream, buffer, numBytes); |
| 1933 |
//ASSERT(bytesWritten == numBytes); // this could happen if timer-based flow control is left enabled |
| 1934 |
// or if stream is closing ... ??? |
| 1935 |
// We had a "stream is closing" case here somehow? Need to make sure we don't try |
| 1936 |
// to write to norm stream |
| 1937 |
|
| 1938 |
// 3) Do we need to issue a watermark ACK request? |
| 1939 |
if (!norm_watermark_pending && (norm_stream_buffer_count >= (norm_stream_buffer_max >> 1))) |
| 1940 |
{ |
| 1941 |
//TRACE("NorpSession::WriteToNormStream() initiating watermark ACK request (buffer count:%lu max:%lu usage:%u)...\n", |
| 1942 |
// norm_stream_buffer_count, norm_stream_buffer_max, NormStreamGetBufferUsage(norm_tx_stream)); |
| 1943 |
NormSetWatermark(norm_session, norm_tx_stream); |
| 1944 |
norm_watermark_pending = true; |
| 1945 |
} |
| 1946 |
return bytesWritten; |
| 1947 |
} |
| 1948 |
else |
| 1949 |
{ |
| 1950 |
PLOG(PL_DETAIL, "NorpSession::WriteToNormStream() is blocked pending acknowledgment from receiver\n"); |
| 1951 |
return 0; |
| 1952 |
} |
| 1953 |
} // end NorpSession::WriteToNormStream() |
| 1954 |
|
| 1955 |
void NorpSession::FlushNormStream(bool eom, NormFlushMode flushMode) |
| 1956 |
{ |
| 1957 |
// NormStreamFlush always will transmit pending runt segments, if applicable |
| 1958 |
// (thus we need to manage our buffer counting accordingly if pending bytes remain) |
| 1959 |
NormStreamFlush(norm_tx_stream, eom, flushMode); |
| 1960 |
if (0 != norm_stream_bytes_remain) |
| 1961 |
{ |
| 1962 |
norm_stream_buffer_count++; |
| 1963 |
norm_stream_bytes_remain = 0; |
| 1964 |
if (!norm_watermark_pending && (norm_stream_buffer_count >= (norm_stream_buffer_max >> 1))) |
| 1965 |
{ |
| 1966 |
//TRACE("NorpSession::FlushNormStream() initiating watermark ACK request (buffer count:%lu max:%lu usage:%u)...\n", |
| 1967 |
// norm_stream_buffer_count, norm_stream_buffer_max, NormStreamGetBufferUsage(norm_tx_stream)); |
| 1968 |
NormSetWatermark(norm_session, norm_tx_stream, true); |
| 1969 |
norm_watermark_pending = true; |
| 1970 |
} |
| 1971 |
} |
| 1972 |
} // end NorpSession::FlushNormStream() |
| 1973 |
|
| 1974 |
bool NorpSession::OnNorpMsgTimeout(ProtoTimer& theTimer) |
| 1975 |
{ |
| 1976 |
PLOG(PL_DETAIL, "NorpSession::OnNorpMsgTimeout() repeat count = %d\n", theTimer.GetRepeatCount()); |
| 1977 |
if (0 != theTimer.GetRepeatCount()) |
| 1978 |
{ |
| 1979 |
struct timeval currentTime; |
| 1980 |
ProtoSystemTime(currentTime); |
| 1981 |
norp_msg.SetTimestamp(currentTime); |
| 1982 |
if (SOCKS_PUT_REQUEST == socks_state) |
| 1983 |
{ |
| 1984 |
if (!SendMessage(norp_msg)) |
| 1985 |
PLOG(PL_ERROR, "NorpSession::OnNorpMsgTimeout() warning: unable to send message (retries remaining %d)\n", theTimer.GetRepeatCount()); |
| 1986 |
} |
| 1987 |
else |
| 1988 |
{ |
| 1989 |
if (!controller.SendMessage(norp_msg, norp_remote_addr)) |
| 1990 |
PLOG(PL_ERROR, "NorpSession::OnNorpMsgTimeout() warning: unable to send message (retries remaining %d)\n", theTimer.GetRepeatCount()); |
| 1991 |
} |
| 1992 |
} |
| 1993 |
else |
| 1994 |
{ |
| 1995 |
PLOG(PL_ERROR, "NorpSession::OnNorpMsgTimeout() error: message retransmission timeout with no reply\n"); |
| 1996 |
switch (socks_state) |
| 1997 |
{ |
| 1998 |
case SOCKS_PUT_REQUEST: |
| 1999 |
if (!MakeDirectConnect()) |
| 2000 |
{ |
| 2001 |
PLOG(PL_ERROR, "NorpSession::OnNorpMsgTimeout() error: also unable to make direct TCP connection\n"); |
| 2002 |
Shutdown(); |
| 2003 |
} |
| 2004 |
return false; // MakeDirectConnect() kills timer |
| 2005 |
break; |
| 2006 |
case SOCKS_PUT_REPLY: |
| 2007 |
Shutdown(); |
| 2008 |
break; |
| 2009 |
case SOCKS_SHUTDOWN: |
| 2010 |
Close(); |
| 2011 |
return false; |
| 2012 |
default: |
| 2013 |
ASSERT(0); // shouldn't be messaging in any other states |
| 2014 |
break; |
| 2015 |
} |
| 2016 |
} |
| 2017 |
return true; |
| 2018 |
} // end NorpSession::OnNorpMsgTimeout() |
| 2019 |
|
| 2020 |
void NorpSession::OnNormEvent(NormEvent& theEvent) |
| 2021 |
{ |
| 2022 |
PLOG(PL_DETAIL, "NorpSession::OnNormEvent("); |
| 2023 |
switch (theEvent.type) |
| 2024 |
{ |
| 2025 |
case NORM_TX_QUEUE_VACANCY: |
| 2026 |
PLOG(PL_DETAIL, "NORM_TX_QUEUE_VACANCY)\n"); |
| 2027 |
case NORM_TX_QUEUE_EMPTY: |
| 2028 |
if (NORM_TX_QUEUE_EMPTY == theEvent.type) PLOG(PL_DETAIL, "NORM_TX_QUEUE_EMPTY)\n"); |
| 2029 |
if (IsRemoteOriginator()) |
| 2030 |
{ |
| 2031 |
// If there's pending data from the SOCKS client, try to send it |
| 2032 |
if (0 != client_pending) |
| 2033 |
{ |
| 2034 |
if (!PutRemoteData()) |
| 2035 |
{ |
| 2036 |
PLOG(PL_ERROR, "NorpSession::OnNormEvent(TX_VACANCY) error: PutRemoteData() failure!\n"); |
| 2037 |
Shutdown(); |
| 2038 |
} |
| 2039 |
} |
| 2040 |
} |
| 2041 |
else |
| 2042 |
{ |
| 2043 |
if (0 != remote_pending) |
| 2044 |
{ |
| 2045 |
if (!PutClientData()) |
| 2046 |
{ |
| 2047 |
PLOG(PL_ERROR, "NorpSession::OnNormEvent(TX_VACANCY) error: PutClientData() failure!\n"); |
| 2048 |
Shutdown(); |
| 2049 |
} |
| 2050 |
} |
| 2051 |
} |
| 2052 |
break; |
| 2053 |
case NORM_TX_FLUSH_COMPLETED: |
| 2054 |
PLOG(PL_DETAIL, "NORM_TX_FLUSH_COMPLETED)\n"); |
| 2055 |
break; |
| 2056 |
case NORM_TX_WATERMARK_COMPLETED: |
| 2057 |
PLOG(PL_DETAIL, "NORM_TX_WATERMARK_COMPLETED)\n"); |
| 2058 |
if (NORM_ACK_SUCCESS == NormGetAckingStatus(norm_session)) |
| 2059 |
{ |
| 2060 |
PLOG(PL_DETAIL, " acking status: SUCCESS\n"); |
| 2061 |
//TRACE("successful watermark ACK (buffer count:%lu max:%lu)\n", norm_stream_buffer_count, norm_stream_buffer_max); |
| 2062 |
norm_watermark_pending = false; |
| 2063 |
norm_stream_buffer_count -= (norm_stream_buffer_max >> 1); |
| 2064 |
//TRACE(" (count reduced to %lu)\n", norm_stream_buffer_count); |
| 2065 |
// Use as prompt to send pending data to NORM stream |
| 2066 |
if (IsRemoteOriginator()) |
| 2067 |
{ |
| 2068 |
// If there's pending data from the SOCKS client, try to send it |
| 2069 |
if (0 != client_pending) |
| 2070 |
{ |
| 2071 |
if (!PutRemoteData()) |
| 2072 |
{ |
| 2073 |
PLOG(PL_ERROR, "NorpSession::OnNormEvent(TX_VACANCY) error: PutRemoteData() failure!\n"); |
| 2074 |
Shutdown(); |
| 2075 |
} |
| 2076 |
} |
| 2077 |
} |
| 2078 |
else |
| 2079 |
{ |
| 2080 |
if (0 != remote_pending) |
| 2081 |
{ |
| 2082 |
if (!PutClientData()) |
| 2083 |
{ |
| 2084 |
PLOG(PL_ERROR, "NorpSession::OnNormEvent(TX_VACANCY) error: PutClientData() failure!\n"); |
| 2085 |
Shutdown(); |
| 2086 |
} |
| 2087 |
} |
| 2088 |
} |
| 2089 |
if (!persist_start_time.IsZero()) |
| 2090 |
persist_start_time.Zeroize(); // resets persistence timeout |
| 2091 |
} |
| 2092 |
else if (NORM_ACK_FAILURE == NormGetAckingStatus(norm_session)) |
| 2093 |
{ |
| 2094 |
PLOG(PL_DETAIL, " acking status: FAILURE\n"); |
| 2095 |
if (persist_interval >= 0.0) |
| 2096 |
{ |
| 2097 |
// Finite persistence will be observed |
| 2098 |
if (persist_start_time.IsZero()) |
| 2099 |
{ |
| 2100 |
// Initialize persistence timeout |
| 2101 |
persist_start_time.GetCurrentTime(); |
| 2102 |
} |
| 2103 |
else |
| 2104 |
{ |
| 2105 |
ProtoTime currentTime; |
| 2106 |
currentTime.GetCurrentTime(); |
| 2107 |
if ((currentTime - persist_start_time) > persist_interval) |
| 2108 |
{ |
| 2109 |
NormStreamClose(norm_tx_stream); |
| 2110 |
norm_tx_stream = NORM_OBJECT_INVALID; |
| 2111 |
Shutdown(); |
| 2112 |
break; |
| 2113 |
} |
| 2114 |
} |
| 2115 |
} |
| 2116 |
NormResetWatermark(norm_session); |
| 2117 |
} |
| 2118 |
break; |
| 2119 |
case NORM_TX_CMD_SENT: |
| 2120 |
PLOG(PL_DETAIL, "NORM_TX_CMD_SENT)\n"); |
| 2121 |
break; |
| 2122 |
case NORM_TX_OBJECT_SENT: |
| 2123 |
PLOG(PL_DETAIL, "NORM_TX_OBJECT_SENT)\n"); |
| 2124 |
break; |
| 2125 |
case NORM_TX_OBJECT_PURGED: |
| 2126 |
PLOG(PL_DETAIL, "NORM_TX_OBJECT_PURGED)\n"); |
| 2127 |
if (theEvent.object == norm_tx_stream) |
| 2128 |
{ |
| 2129 |
ASSERT(SOCKS_SHUTDOWN == socks_state); |
| 2130 |
// do we need to call NormStreamClose() here? |
| 2131 |
NormStreamClose(norm_tx_stream); |
| 2132 |
norm_tx_stream = NORM_OBJECT_INVALID; |
| 2133 |
if (ShutdownComplete()) Shutdown(); |
| 2134 |
} |
| 2135 |
break; |
| 2136 |
case NORM_TX_RATE_CHANGED: |
| 2137 |
PLOG(PL_DETAIL, "NORM_TX_RATE_CHANGED)\n"); |
| 2138 |
break; |
| 2139 |
case NORM_LOCAL_SENDER_CLOSED: |
| 2140 |
PLOG(PL_DETAIL, "NORM_LOCAL_SENDER_CLOSED)\n"); |
| 2141 |
break; |
| 2142 |
case NORM_REMOTE_SENDER_NEW: |
| 2143 |
{ |
| 2144 |
PLOG(PL_DETAIL, "NORM_REMOTE_SENDER_NEW)\n"); |
| 2145 |
char addrBuffer[16]; |
| 2146 |
unsigned int addrLen = 16; |
| 2147 |
UINT16 sndrPort; |
| 2148 |
NormNodeGetAddress(theEvent.sender, addrBuffer, &addrLen, &sndrPort); |
| 2149 |
ProtoAddress sndrAddr; |
| 2150 |
switch (addrLen) |
| 2151 |
{ |
| 2152 |
case 4: |
| 2153 |
sndrAddr.SetRawHostAddress(ProtoAddress::IPv4, addrBuffer, 4); |
| 2154 |
break; |
| 2155 |
case 16: |
| 2156 |
sndrAddr.SetRawHostAddress(ProtoAddress::IPv6, addrBuffer, 16); |
| 2157 |
break; |
| 2158 |
default: |
| 2159 |
TRACE("invalid remote sender address ?!\n"); |
| 2160 |
break; |
| 2161 |
} |
| 2162 |
#ifdef NEW_PORT |
| 2163 |
TRACE("new remote sender address %s/%hu (orig:%d) ...\n", sndrAddr.GetHostString(), sndrPort, IsRemoteOriginator()); |
| 2164 |
if (!IsRemoteOriginator() && !norm_sender_heard) |
| 2165 |
{ |
| 2166 |
NormChangeDestination(theEvent.session, sndrAddr.GetHostString(), sndrPort, true); |
| 2167 |
norm_sender_heard = true; |
| 2168 |
//NormSetRxPortReuse(theEvent.session, true, NULL, sndrAddr.GetHostString(), sndrPort); |
| 2169 |
} |
| 2170 |
#endif // NEW_PORT |
| 2171 |
break; |
| 2172 |
} |
| 2173 |
case NORM_REMOTE_SENDER_ACTIVE: |
| 2174 |
PLOG(PL_DETAIL, "NORM_REMOTE_SENDER_ACTIVE)\n"); |
| 2175 |
break; |
| 2176 |
case NORM_REMOTE_SENDER_INACTIVE: |
| 2177 |
PLOG(PL_DETAIL, "NORM_REMOTE_SENDER_INACTIVE)\n"); |
| 2178 |
break; |
| 2179 |
case NORM_REMOTE_SENDER_PURGED: |
| 2180 |
PLOG(PL_DETAIL, "NORM_REMOTE_SENDER_PURGED)\n"); |
| 2181 |
break; |
| 2182 |
case NORM_RX_CMD_NEW: |
| 2183 |
PLOG(PL_DETAIL, "NORM_RX_CMD_NEW)\n"); |
| 2184 |
break; |
| 2185 |
case NORM_RX_OBJECT_NEW: |
| 2186 |
PLOG(PL_DETAIL, "NORM_RX_OBJECT_NEW)\n"); |
| 2187 |
// TBD - confirm it's a stream, etc? |
| 2188 |
norm_rx_stream = theEvent.object; |
| 2189 |
break; |
| 2190 |
case NORM_RX_OBJECT_INFO: |
| 2191 |
PLOG(PL_DETAIL, "NORM_RX_OBJECT_INFO)\n"); |
| 2192 |
break; |
| 2193 |
case NORM_RX_OBJECT_UPDATED: |
| 2194 |
PLOG(PL_DETAIL, "NORM_RX_OBJECT_UPDATED)\n"); |
| 2195 |
ASSERT(theEvent.object == norm_rx_stream); |
| 2196 |
norm_rx_pending = true; // signals there is NORM rx data ready for reading |
| 2197 |
if (IsRemoteOriginator()) |
| 2198 |
{ |
| 2199 |
// we're the remote session originator (read data from NORM and put to local SOCKS client) |
| 2200 |
if (0 == remote_pending) |
| 2201 |
{ |
| 2202 |
// try to read more data from NORM into "remote_buffer" and put to SOCKS client |
| 2203 |
if (!GetRemoteData()) |
| 2204 |
{ |
| 2205 |
PLOG(PL_ERROR, "NorpSession::OnNormEvent(RX_UPDATE) error: GetRemoteData() failure!\n"); |
| 2206 |
Shutdown(); // TBD - shut down more gracefully (informing remote) |
| 2207 |
} |
| 2208 |
} |
| 2209 |
// else there's still data in remote_buffer pending relay to SOCKS client |
| 2210 |
// TBD - should we go ahead and append more data to remote_buffer? |
| 2211 |
// (for now, PutClientData() will call GetRemoteData() for more when relay to client completes |
| 2212 |
} |
| 2213 |
else |
| 2214 |
{ |
| 2215 |
// we're the remote correspondent (read data from NORM and put to remote) |
| 2216 |
if ((0 == client_pending) && ((SOCKS_CONNECTED == socks_state) || (SOCKS_SHUTDOWN == socks_state))) |
| 2217 |
{ |
| 2218 |
// try to read more data from NORM into "client_buffer" and put to SOCKS destination |
| 2219 |
if (!GetClientData()) |
| 2220 |
{ |
| 2221 |
PLOG(PL_ERROR, "NorpSession::OnNormEvent(RX_UPDATE) error: GetRemoteData() failure!\n"); |
| 2222 |
Shutdown(); // TBD - shut down more gracefully (informing remote) |
| 2223 |
} |
| 2224 |
} |
| 2225 |
// else there's still data in client_buffer pending relay to TCP destination |
| 2226 |
// TBD - should we go ahead and append more data to client_buffer? |
| 2227 |
// (for now, PutRemoteData() will call GetClientData() for more when relay to remote completes |
| 2228 |
} |
| 2229 |
break; |
| 2230 |
case NORM_RX_OBJECT_COMPLETED: |
| 2231 |
PLOG(PL_DETAIL, "NORM_RX_OBJECT_COMPLETED)\n"); |
| 2232 |
if (IsRemoteOriginator()) |
| 2233 |
{ |
| 2234 |
if (SOCKS_CONNECTED == socks_state) |
| 2235 |
{ |
| 2236 |
socks_state = SOCKS_SHUTDOWN; |
| 2237 |
// Make sure our socks_client_socket gets shut down |
| 2238 |
if (0 == remote_pending) |
| 2239 |
PutClientData(); // will shutdown our socks_client_socket |
| 2240 |
// else socks_client_socket output notification will call PutClientData() |
| 2241 |
|
| 2242 |
// TBD - if the remote obj is completed, then there's no use in pushing more data their way? |
| 2243 |
// Gracefully shut down our norm_tx_stream (requesting an ACK from the correspondent) |
| 2244 |
NormStreamClose(norm_tx_stream, true); // Note our "trick" here to do a graceful close, _then_ watermark |
| 2245 |
NormSetWatermark(norm_session, norm_tx_stream, true); // future NORM API will add "bool watermark" option to graceful close |
| 2246 |
} |
| 2247 |
else |
| 2248 |
{ |
| 2249 |
ASSERT(!socks_client_socket.IsOpen()); |
| 2250 |
} |
| 2251 |
} |
| 2252 |
else |
| 2253 |
{ |
| 2254 |
if (SOCKS_CONNECTED == socks_state) |
| 2255 |
{ |
| 2256 |
socks_state = SOCKS_SHUTDOWN; |
| 2257 |
if (0 == client_pending) |
| 2258 |
PutRemoteData(); // will shutdown our socks_remote_socket |
| 2259 |
// else socks_remote_socket output notification will call PutClientData() |
| 2260 |
|
| 2261 |
// Gracefully shut down our norm_tx_stream (requesting an ACK from the originator) |
| 2262 |
NormStreamClose(norm_tx_stream, true); // Note our "trick" here to do a graceful close, _then_ watermark |
| 2263 |
NormSetWatermark(norm_session, norm_tx_stream, true); // future NORM API will add "bool watermark" option to graceful close |
| 2264 |
} |
| 2265 |
else |
| 2266 |
{ |
| 2267 |
ASSERT(!socks_remote_socket.IsOpen()); |
| 2268 |
} |
| 2269 |
} |
| 2270 |
norm_rx_stream = NORM_OBJECT_INVALID; |
| 2271 |
norm_rx_pending = false; |
| 2272 |
if (ShutdownComplete()) Close(); |
| 2273 |
break; |
| 2274 |
case NORM_SEND_ERROR: |
| 2275 |
PLOG(PL_ERROR, "NorpSession::OnNormEvent(NORM_SEND_ERROR) error ...\n");//: remote session unexpectedly closed!\n"); |
| 2276 |
Close(); |
| 2277 |
break; |
| 2278 |
|
| 2279 |
case NORM_RX_OBJECT_ABORTED: |
| 2280 |
PLOG(PL_DETAIL, "NORM_RX_OBJECT_ABORTED)\n"); |
| 2281 |
break; |
| 2282 |
case NORM_GRTT_UPDATED: |
| 2283 |
PLOG(PL_DETAIL, "NORM_GRTT_UPDATED)\n"); |
| 2284 |
break; |
| 2285 |
case NORM_CC_ACTIVE: |
| 2286 |
PLOG(PL_DETAIL, "NORM_CC_ACTIVE)\n"); |
| 2287 |
break; |
| 2288 |
case NORM_CC_INACTIVE: |
| 2289 |
PLOG(PL_DETAIL, "NORM_CC_INACTIVE)\n"); |
| 2290 |
break; |
| 2291 |
case NORM_EVENT_INVALID: |
| 2292 |
PLOG(PL_DETAIL, "NORM_EVENT_INVALID)\n"); |
| 2293 |
break; |
| 2294 |
default: |
| 2295 |
PLOG(PL_DETAIL, "UNKNOWN event %d)\n", theEvent.type); |
| 2296 |
break; |
| 2297 |
} |
| 2298 |
} // end NorpSession::OnNormEvent() |
| 2299 |
|
| 2300 |
NorpPreset::NorpPreset(Norp& norpController, UINT16 tcpPort, const ProtoAddress& dstAddr, const ProtoAddress& norpAddr) |
| 2301 |
: controller(norpController), tcp_port(tcpPort), dst_addr(dstAddr), norp_addr(norpAddr), server_socket(ProtoSocket::TCP) |
| 2302 |
{ |
| 2303 |
server_socket.SetNotifier(&(controller.GetSocketNotifier())); |
| 2304 |
server_socket.SetListener(this, &NorpPreset::OnSocketEvent); |
| 2305 |
|
| 2306 |
} |
| 2307 |
|
| 2308 |
NorpPreset::~NorpPreset() |
| 2309 |
{ |
| 2310 |
if (server_socket.IsOpen()) server_socket.Close(); |
| 2311 |
} |
| 2312 |
|
| 2313 |
bool NorpPreset::Listen() |
| 2314 |
{ |
| 2315 |
if (!server_socket.Listen(tcp_port)) |
| 2316 |
{ |
| 2317 |
PLOG(PL_ERROR, "NorpPreset::Listen() error: unable to establish listening TCP socket on port %hu\n", tcp_port); |
| 2318 |
return false; |
| 2319 |
} |
| 2320 |
return true; |
| 2321 |
} // end NorpPreset::Listen(); |
| 2322 |
|
| 2323 |
void NorpPreset::OnSocketEvent(ProtoSocket& theSocket, ProtoSocket::Event theEvent) |
| 2324 |
{ |
| 2325 |
// TBD - dispatch ACCEPT events up to controller to create a corresponding |
| 2326 |
// jump-started NorpSession |
| 2327 |
|
| 2328 |
PLOG(PL_DETAIL, "NorpPreset::OnSocketEvent("); |
| 2329 |
switch (theEvent) |
| 2330 |
{ |
| 2331 |
case ProtoSocket::INVALID_EVENT: |
| 2332 |
PLOG(PL_DETAIL, "INVALID_EVENT) ...\n"); |
| 2333 |
break; |
| 2334 |
case ProtoSocket::CONNECT: |
| 2335 |
PLOG(PL_DETAIL, "CONNECT) ...\n"); |
| 2336 |
break; |
| 2337 |
case ProtoSocket::ACCEPT: |
| 2338 |
{ |
| 2339 |
PLOG(PL_DETAIL, "ACCEPT) ...\n"); |
| 2340 |
if (!controller.AcceptPresetClientConnection(*this)) |
| 2341 |
PLOG(PL_ERROR, "Norp::OnSocketEvent(ACCEPT) error: unable to create new NorpSession\n"); |
| 2342 |
break; |
| 2343 |
} |
| 2344 |
case ProtoSocket::SEND: |
| 2345 |
PLOG(PL_DETAIL, "SEND) ...\n"); |
| 2346 |
break; |
| 2347 |
case ProtoSocket::RECV: |
| 2348 |
{ |
| 2349 |
PLOG(PL_DETAIL, "RECV) ...\n"); |
| 2350 |
break; |
| 2351 |
} |
| 2352 |
case ProtoSocket::DISCONNECT: |
| 2353 |
PLOG(PL_DETAIL, "DISCONNECT) ...\n"); |
| 2354 |
break; |
| 2355 |
|
| 2356 |
case ProtoSocket::EXCEPTION: |
| 2357 |
PLOG(PL_DETAIL, "EXCEPTION) ...\n"); |
| 2358 |
break; |
| 2359 |
|
| 2360 |
case ProtoSocket::ERROR_: |
| 2361 |
PLOG(PL_DETAIL, "ERROR_) ...\n"); |
| 2362 |
break; |
| 2363 |
} |
| 2364 |
|
| 2365 |
} // end NorpPreset::OnSocketEvent() |
| 2366 |
|
| 2367 |
Norp::Norp(ProtoDispatcher& theDispatcher) |
| 2368 |
: dispatcher(theDispatcher), socks_server_socket(ProtoSocket::TCP), socks_port(DEFAULT_SOCKS_PORT), |
| 2369 |
next_session_id(1), session_count(0), norp_rx_socket(ProtoSocket::UDP), norp_local_port(DEFAULT_NORP_PORT), |
| 2370 |
norp_remote_port(DEFAULT_NORP_PORT), norp_rtt_init(NorpSession::NORP_RTT_DEFAULT), |
| 2371 |
norm_enable(true), norm_instance(NORM_INSTANCE_INVALID), norm_node_id(NORM_NODE_ANY), |
| 2372 |
norm_port(DEFAULT_NORM_PORT), norm_cc_mode(NORM_CC), norm_tx_rate(DEFAULT_TX_RATE), norm_tx_limit(-1.0), |
| 2373 |
norm_segment_size(1400), norm_block_size(64), norm_parity_count(0), norm_parity_auto(0), norm_trace(false) |
| 2374 |
// ,port_pool(9000) |
| 2375 |
{ |
| 2376 |
socks_server_socket.SetNotifier(&GetSocketNotifier()); |
| 2377 |
socks_server_socket.SetListener(this, &Norp::OnSocksServerEvent); |
| 2378 |
norp_rx_socket.SetNotifier(&GetSocketNotifier()); |
| 2379 |
norp_rx_socket.SetListener(this, &Norp::OnNorpSocketEvent); |
| 2380 |
} |
| 2381 |
|
| 2382 |
Norp::~Norp() |
| 2383 |
{ |
| 2384 |
StopServer(); |
| 2385 |
} |
| 2386 |
|
| 2387 |
bool Norp::SendMessage(const NorpMsg& msg, const ProtoAddress& dstAddr) |
| 2388 |
{ |
| 2389 |
unsigned int numBytes = msg.GetLength(); |
| 2390 |
if (norp_rx_socket.SendTo((const char*)msg.GetBuffer(), numBytes, dstAddr)) |
| 2391 |
{ |
| 2392 |
if (0 == numBytes) |
| 2393 |
{ |
| 2394 |
PLOG(PL_WARN, "Norp::SendMessage() norp_rx_socket.SendTo() error: %s\n", GetErrorString()); |
| 2395 |
return false; |
| 2396 |
} |
| 2397 |
} |
| 2398 |
else |
| 2399 |
{ |
| 2400 |
PLOG(PL_ERROR, "Norp::SendMessage() norp_rx_socket.SendTo() error: %s\n", GetErrorString()); |
| 2401 |
return false; |
| 2402 |
} |
| 2403 |
return true; |
| 2404 |
} // end Norp::SendMessage() |
| 2405 |
|
| 2406 |
bool Norp::StartServer(bool normEnable) |
| 2407 |
{ |
| 2408 |
if (!socks_server_socket.Listen(socks_port)) |
| 2409 |
{ |
| 2410 |
PLOG(PL_ERROR, "Norp::StartServer() error: socks server socket listen(%d) error\n", socks_port); |
| 2411 |
return false; |
| 2412 |
} |
| 2413 |
norm_enable = normEnable; |
| 2414 |
if (normEnable) |
| 2415 |
{ |
| 2416 |
norm_instance = NormCreateInstance(); |
| 2417 |
if (NORM_INSTANCE_INVALID == norm_instance) |
| 2418 |
{ |
| 2419 |
PLOG(PL_ERROR, "Norp::StartServer() error: NormCreateInstance() failure!\n"); |
| 2420 |
StopServer(); |
| 2421 |
return false; |
| 2422 |
} |
| 2423 |
NormDescriptor normDescriptor = NormGetDescriptor(norm_instance); |
| 2424 |
if (!dispatcher.InstallGenericInput(normDescriptor, NormEventCallback, this)) |
| 2425 |
{ |
| 2426 |
PLOG(PL_ERROR, "Norp::StartServer() error: unable to install NORM event notification!\n"); |
| 2427 |
StopServer(); |
| 2428 |
return false; |
| 2429 |
} |
| 2430 |
if (!norp_rx_socket.Open(norp_local_port)) |
| 2431 |
{ |
| 2432 |
PLOG(PL_ERROR, "Norp::StartServer() error: unable to open NORP signaling socket!\n"); |
| 2433 |
StopServer(); |
| 2434 |
return false; |
| 2435 |
} |
| 2436 |
if (NORM_NODE_ANY == norm_node_id) |
| 2437 |
norm_node_id = proxy_addr.GetEndIdentifier(); // TBD - make sure proxy_addr.IsValid() ??? |
| 2438 |
} |
| 2439 |
else |
| 2440 |
{ |
| 2441 |
norm_node_id = NORM_NODE_NONE; |
| 2442 |
} |
| 2443 |
PLOG(PL_INFO, "norp: created controller on NORP port %hu with local NORM node id: %08x\n", norp_local_port, norm_node_id); |
| 2444 |
return true; |
| 2445 |
} // end Norp::StartServer() |
| 2446 |
|
| 2447 |
void Norp::StopServer() |
| 2448 |
{ |
| 2449 |
norp_rx_socket.Close(); |
| 2450 |
socks_server_socket.Close(); |
| 2451 |
session_list.Destroy(); |
| 2452 |
preset_list.Destroy(); |
| 2453 |
if (NORM_INSTANCE_INVALID != norm_instance) |
| 2454 |
{ |
| 2455 |
dispatcher.RemoveGenericInput(NormGetDescriptor(norm_instance)); |
| 2456 |
NormDestroyInstance(norm_instance); |
| 2457 |
norm_instance = NORM_INSTANCE_INVALID; |
| 2458 |
} |
| 2459 |
} // end Norp::StopServer() |
| 2460 |
|
| 2461 |
void Norp::OnSessionClose(NorpSession& theSession) |
| 2462 |
{ |
| 2463 |
RemoveSession(theSession); |
| 2464 |
PLOG(PL_INFO, "norp: deleting closed session %u\n", theSession.GetSessionId()); |
| 2465 |
delete &theSession; |
| 2466 |
} // end Norp::OnSessionClose(); |
| 2467 |
|
| 2468 |
bool Norp::AcceptPresetClientConnection(NorpPreset& preset) |
| 2469 |
{ |
| 2470 |
// Create a new NorpSession for this prest client connection |
| 2471 |
NorpSession* norpSession = new NorpSession(*this, next_session_id++); |
| 2472 |
if (NULL == norpSession) |
| 2473 |
{ |
| 2474 |
PLOG(PL_ERROR, "Norp::AcceptPresetClientConnection() new NorpSession error: %s\n", GetErrorString()); |
| 2475 |
return false; |
| 2476 |
} |
| 2477 |
if (!norpSession->AcceptPresetClientConnection(preset, norm_enable)) |
| 2478 |
{ |
| 2479 |
PLOG(PL_ERROR, "Norp::AcceptPresetClientConnection() error: unable to accept connection\n"); |
| 2480 |
delete norpSession; |
| 2481 |
return false; |
| 2482 |
} |
| 2483 |
norpSession->SetProxyAddress(proxy_addr); |
| 2484 |
AddSession(*norpSession); |
| 2485 |
PLOG(PL_INFO, "norp: session %hu accepted preset connection from %s\n", norpSession->GetSessionId(), norpSession->GetClientAddress().GetHostString()); |
| 2486 |
return true; |
| 2487 |
} // end Norp::AcceptPresetClientConnection() |
| 2488 |
|
| 2489 |
void Norp::OnSocksServerEvent(ProtoSocket& theSocket, |
| 2490 |
ProtoSocket::Event theEvent) |
| 2491 |
{ |
| 2492 |
PLOG(PL_DETAIL, "Norp::OnServerSocketEvent("); |
| 2493 |
switch (theEvent) |
| 2494 |
{ |
| 2495 |
case ProtoSocket::INVALID_EVENT: |
| 2496 |
PLOG(PL_DETAIL, "INVALID_EVENT) ...\n"); |
| 2497 |
break; |
| 2498 |
case ProtoSocket::CONNECT: |
| 2499 |
PLOG(PL_DETAIL, "CONNECT) ...\n"); |
| 2500 |
break; |
| 2501 |
case ProtoSocket::ACCEPT: |
| 2502 |
{ |
| 2503 |
PLOG(PL_DETAIL, "ACCEPT) ...\n"); |
| 2504 |
// Create a new NorpSession for this potential client |
| 2505 |
NorpSession* norpSession = new NorpSession(*this, next_session_id++); |
| 2506 |
if (NULL == norpSession) |
| 2507 |
{ |
| 2508 |
PLOG(PL_ERROR, "Norp::OnSocksServerEvent() new NorpSession error: %s\n", GetErrorString()); |
| 2509 |
break; |
| 2510 |
} |
| 2511 |
if (!norpSession->AcceptClientConnection(socks_server_socket, norm_enable)) |
| 2512 |
// Accept the socket connection into this session's "socks_client_socket" |
| 2513 |
//if (!socks_server_socket.Accept(&norpSession->AccessSocksClientSocket())) |
| 2514 |
{ |
| 2515 |
PLOG(PL_ERROR, "Norp::OnSocksServerEvent(ACCEPT) error accepting connection\n"); |
| 2516 |
delete norpSession; |
| 2517 |
break; |
| 2518 |
} |
| 2519 |
norpSession->SetProxyAddress(proxy_addr); |
| 2520 |
AddSession(*norpSession); |
| 2521 |
PLOG(PL_INFO, "norp: session %hu accepted connection from %s\n", norpSession->GetSessionId(), norpSession->GetClientAddress().GetHostString()); |
| 2522 |
break; |
| 2523 |
} |
| 2524 |
case ProtoSocket::SEND: |
| 2525 |
PLOG(PL_DETAIL, "SEND) ...\n"); |
| 2526 |
break; |
| 2527 |
case ProtoSocket::RECV: |
| 2528 |
{ |
| 2529 |
PLOG(PL_DETAIL, "RECV) ...\n"); |
| 2530 |
break; |
| 2531 |
} |
| 2532 |
case ProtoSocket::DISCONNECT: |
| 2533 |
PLOG(PL_DETAIL, "DISCONNECT) ...\n"); |
| 2534 |
break; |
| 2535 |
|
| 2536 |
case ProtoSocket::EXCEPTION: |
| 2537 |
PLOG(PL_DETAIL, "EXCEPTION) ...\n"); |
| 2538 |
break; |
| 2539 |
|
| 2540 |
case ProtoSocket::ERROR_: |
| 2541 |
PLOG(PL_DETAIL, "ERROR_) ...\n"); |
| 2542 |
break; |
| 2543 |
} |
| 2544 |
} // end Norp::OnSocksServerEvent() |
| 2545 |
|
| 2546 |
void Norp::AddSession(NorpSession& session) |
| 2547 |
{ |
| 2548 |
session_list.Insert(session); |
| 2549 |
session_count++; |
| 2550 |
if (norm_tx_limit >= 0.0) |
| 2551 |
{ |
| 2552 |
double lowerLimit = 0.9 * (norm_tx_limit / (double)session_count); |
| 2553 |
NorpSessionList::Iterator iterator(session_list); |
| 2554 |
NorpSession* nextSession; |
| 2555 |
while (NULL != (nextSession = iterator.GetNextItem())) |
| 2556 |
nextSession->SetNormRateBounds(lowerLimit, norm_tx_limit); |
| 2557 |
} |
| 2558 |
} // end Norp::AddSession() |
| 2559 |
|
| 2560 |
void Norp::RemoveSession(NorpSession& session) |
| 2561 |
{ |
| 2562 |
session_list.Remove(session); |
| 2563 |
session_count--; |
| 2564 |
if ((session_count > 0) && (norm_tx_limit >= 0.0)) |
| 2565 |
{ |
| 2566 |
double lowerLimit = 0.9 * (norm_tx_limit / (double)session_count); |
| 2567 |
NorpSessionList::Iterator iterator(session_list); |
| 2568 |
NorpSession* nextSession; |
| 2569 |
while (NULL != (nextSession = iterator.GetNextItem())) |
| 2570 |
nextSession->SetNormRateBounds(lowerLimit, norm_tx_limit); |
| 2571 |
} |
| 2572 |
} // end Norp::RemoveSession() |
| 2573 |
|
| 2574 |
|
| 2575 |
bool Norp::AddPreset(UINT16 tcpPort, const ProtoAddress& dstAddr, const ProtoAddress& norpAddr) |
| 2576 |
{ |
| 2577 |
NorpPreset* preset = new NorpPreset(*this, tcpPort, dstAddr, norpAddr); |
| 2578 |
if (NULL == preset) |
| 2579 |
{ |
| 2580 |
PLOG(PL_ERROR, "Norp::AddPreset() new NorpPreset error: %s\n", GetErrorString()); |
| 2581 |
return false; |
| 2582 |
} |
| 2583 |
if (!preset->Listen()) |
| 2584 |
{ |
| 2585 |
PLOG(PL_ERROR, "Norp::AddPreset() error: unable to create listening TCP socket\n"); |
| 2586 |
delete preset; |
| 2587 |
return false; |
| 2588 |
} |
| 2589 |
preset_list.Insert(*preset); |
| 2590 |
return true; |
| 2591 |
} // end Norp::AddPreset() |
| 2592 |
|
| 2593 |
void Norp::OnNorpSocketEvent(ProtoSocket& theSocket, |
| 2594 |
ProtoSocket::Event theEvent) |
| 2595 |
{ |
| 2596 |
if (ProtoSocket::RECV == theEvent) |
| 2597 |
{ |
| 2598 |
UINT32 buffer[1024/4]; |
| 2599 |
ProtoAddress senderAddr; |
| 2600 |
for(;;) |
| 2601 |
{ |
| 2602 |
unsigned int numBytes = 1024; |
| 2603 |
if (!theSocket.RecvFrom((char*)buffer, numBytes, senderAddr)) |
| 2604 |
{ |
| 2605 |
NorpSession* theSession = (NorpSession*)(theSocket.GetUserData()); |
| 2606 |
if ((NULL != theSession) && (NorpSession::SOCKS_PUT_REQUEST == theSession->GetSocksState())) |
| 2607 |
{ |
| 2608 |
// Assume it is an ICMP "port unreachable" error in response to NORP request |
| 2609 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent() remote NORP server port unreachable?\n"); |
| 2610 |
if (!theSession->MakeDirectConnect()) |
| 2611 |
{ |
| 2612 |
PLOG(PL_ERROR, "Norp::OnNorpSocketEvent(RECV) error: unable to make direct connection\n"); |
| 2613 |
theSession->Shutdown(); |
| 2614 |
} |
| 2615 |
break; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
if (0 == numBytes) break; // nothing left to read |
| 2619 |
// TBD - make sure received command is at least 20 bytes (NORP header) |
| 2620 |
NorpMsg msg(buffer, numBytes); |
| 2621 |
switch (msg.GetType()) |
| 2622 |
{ |
| 2623 |
case NorpMsg::SOCKS_REQ: |
| 2624 |
{ |
| 2625 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent(RECV) received SOCKS_REQ session %d:%hu\n", msg.GetNodeId(), msg.GetSessionId()); |
| 2626 |
OnRemoteRequest(msg, senderAddr); |
| 2627 |
break; |
| 2628 |
} |
| 2629 |
case NorpMsg::REQ_ACK: |
| 2630 |
{ |
| 2631 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent(RECV) received REQ_ACK session %d:%hu\n", msg.GetNodeId(), msg.GetSessionId()); |
| 2632 |
NorpSession* session = session_list.FindSession(msg.GetSessionId()); |
| 2633 |
// TBD - confirm the session found is for the given node id, etc |
| 2634 |
if (NULL != session) |
| 2635 |
{ |
| 2636 |
if (!session->OnRemoteRequestAcknowledgment(msg, senderAddr)) |
| 2637 |
session->Shutdown(); |
| 2638 |
} |
| 2639 |
else |
| 2640 |
{ |
| 2641 |
PLOG(PL_ERROR, "Norp::OnNorpSocketEvent(RECV) warning: received remote REQ_ACK for unknown session\n"); |
| 2642 |
} |
| 2643 |
break; |
| 2644 |
} |
| 2645 |
case NorpMsg::SOCKS_REP: |
| 2646 |
{ |
| 2647 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent(RECV) received SOCKS_REP session %d:%hu (length:%d)\n", msg.GetNodeId(), msg.GetSessionId(), msg.GetLength()); |
| 2648 |
NorpSession* session = session_list.FindSession(msg.GetSessionId()); |
| 2649 |
if (NULL != session) |
| 2650 |
{ |
| 2651 |
// TBD - confirm the session found is for the given node id, etc |
| 2652 |
if (!session->OnRemoteReply(msg, senderAddr)) |
| 2653 |
session->Shutdown(); |
| 2654 |
} |
| 2655 |
else |
| 2656 |
{ |
| 2657 |
PLOG(PL_ERROR, "Norp::OnNorpSocketEvent(RECV) warning: received remote SOCKS_REPLY for unknown session\n"); |
| 2658 |
} |
| 2659 |
break; |
| 2660 |
} |
| 2661 |
case NorpMsg::REP_ACK: |
| 2662 |
{ |
| 2663 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent(RECV) received REP_ACK session %d:%hu\n", msg.GetNodeId(), msg.GetSessionId()); |
| 2664 |
NorpSession* session = session_list.FindSession(msg.GetSessionId(), msg.GetNodeId()); |
| 2665 |
if (NULL != session) |
| 2666 |
{ |
| 2667 |
if (!session->OnRemoteReplyAcknowledgment(msg)) |
| 2668 |
session->Shutdown(); |
| 2669 |
} |
| 2670 |
else |
| 2671 |
{ |
| 2672 |
PLOG(PL_ERROR, "Norp::OnNorpSocketEvent(RECV) warning: received remote REP_ACK for unknown session\n"); |
| 2673 |
} |
| 2674 |
break; |
| 2675 |
} |
| 2676 |
case NorpMsg::ORIG_END: |
| 2677 |
{ |
| 2678 |
|
| 2679 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent(RECV) received ORIG_END session %d:%hu\n", msg.GetNodeId(), msg.GetSessionId()); |
| 2680 |
NorpSession* session = session_list.FindSession(msg.GetSessionId(), msg.GetNodeId()); |
| 2681 |
if (NULL != session) |
| 2682 |
{ |
| 2683 |
// TBD - mroe gently end things (e.g. let socket shutdowns commence)??? |
| 2684 |
session->Close(); // the session will make an upcall and get itself removed/deleted |
| 2685 |
} |
| 2686 |
UINT32 buffer[20/sizeof(UINT32)]; |
| 2687 |
NorpMsg ack(buffer, 20, false); |
| 2688 |
ack.SetType(NorpMsg::ACK_END); |
| 2689 |
ack.SetSessionId(msg.GetSessionId()); |
| 2690 |
ack.SetNodeId(NORM_NODE_NONE); |
| 2691 |
if (!SendMessage(ack, senderAddr)) |
| 2692 |
PLOG(PL_ERROR, "Norp::OnNorpSocketEvent(RECV) warning: unable to send ACK_END message to originator\n"); |
| 2693 |
break; |
| 2694 |
} |
| 2695 |
case NorpMsg::CORR_END: |
| 2696 |
{ |
| 2697 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent(RECV) received CORR_END session %d:%hu\n", msg.GetNodeId(), msg.GetSessionId()); |
| 2698 |
NorpSession* session = session_list.FindSession(msg.GetSessionId(), NORM_NODE_NONE); |
| 2699 |
if (NULL != session) |
| 2700 |
{ |
| 2701 |
// TBD - mroe gently end things (e.g. let socket shutdowns commence)??? |
| 2702 |
session->Close(); // the session will make an upcall and get itself removed/deleted |
| 2703 |
} |
| 2704 |
UINT32 buffer[20/sizeof(UINT32)]; |
| 2705 |
NorpMsg ack(buffer, 20, false); |
| 2706 |
ack.SetType(NorpMsg::ACK_END); |
| 2707 |
ack.SetSessionId(msg.GetSessionId()); |
| 2708 |
ack.SetNodeId(GetNormNodeId()); |
| 2709 |
if (!SendMessage(ack, senderAddr)) |
| 2710 |
PLOG(PL_ERROR, "Norp::OnNorpSocketEvent(RECV) warning: unable to send ACK_END message to correspondent\n"); |
| 2711 |
break; |
| 2712 |
} |
| 2713 |
case NorpMsg::ACK_END: |
| 2714 |
{ |
| 2715 |
PLOG(PL_INFO, "Norp::OnNorpSocketEvent(RECV) received ACK_END session %d:%hu\n", msg.GetNodeId(), msg.GetSessionId()); |
| 2716 |
NorpSession* session = session_list.FindSession(msg.GetSessionId(), msg.GetNodeId()); |
| 2717 |
if (NULL != session) session->Close(); // the session will make an upcall and get itself removed/deleted |
| 2718 |
break; |
| 2719 |
} |
| 2720 |
default: |
| 2721 |
{ |
| 2722 |
PLOG(PL_ERROR, "Norp::OnNormEvent(NORM_RX_CMD_NEW) error: received invalid NORP command: %d\n", msg.GetType()); |
| 2723 |
break; |
| 2724 |
} |
| 2725 |
} // end switch(msg.GetType()) |
| 2726 |
numBytes = 1024; |
| 2727 |
} // end while(theSocket.RecvFrom()) |
| 2728 |
} // end if (ProtoSocket::RECV == theEvent) |
| 2729 |
else |
| 2730 |
{ |
| 2731 |
PLOG(PL_DETAIL, "unhandled NORP socket event!\n"); |
| 2732 |
} |
| 2733 |
} // end Norp::OnNorpSocketEvent() |
| 2734 |
|
| 2735 |
void Norp::OnRemoteRequest(const NorpMsg& msg, const ProtoAddress& senderAddr) |
| 2736 |
{ |
| 2737 |
NorpSession* session = session_list.FindSession(msg.GetSessionId(), msg.GetNodeId()); |
| 2738 |
if (NULL == session) |
| 2739 |
{ |
| 2740 |
PLOG(PL_INFO, "norp: new remote session id %hu:%08x\n", msg.GetSessionId(), msg.GetNodeId()); |
| 2741 |
// It's a new remote session |
| 2742 |
session = new NorpSession(*this, msg.GetSessionId(), msg.GetNodeId()); |
| 2743 |
if (NULL == session) |
| 2744 |
{ |
| 2745 |
PLOG(PL_ERROR, "Norp::OnRemoteRequest() new NorpSession error: %s\n", GetErrorString()); |
| 2746 |
// TBD - should we somehow signal the remote of our failure? |
| 2747 |
// (just let it time out for now) |
| 2748 |
return; |
| 2749 |
} |
| 2750 |
AddSession(*session); |
| 2751 |
} |
| 2752 |
if (!session->OnRemoteRequest(msg, senderAddr)) |
| 2753 |
{ |
| 2754 |
PLOG(PL_ERROR, "Norp::OnRemoteRequest() error: unable to initiate new remote NorpSession\n"); |
| 2755 |
// TBD - signal failure to remote |
| 2756 |
RemoveSession(*session); |
| 2757 |
delete session; |
| 2758 |
} |
| 2759 |
} // end Norp::OnRemoteRequest() |
| 2760 |
|
| 2761 |
|
| 2762 |
void Norp::NormEventCallback(ProtoDispatcher::Descriptor descriptor, |
| 2763 |
ProtoDispatcher::Event theEvent, |
| 2764 |
const void* userData) |
| 2765 |
{ |
| 2766 |
if (ProtoDispatcher::EVENT_INPUT == theEvent) |
| 2767 |
{ |
| 2768 |
Norp* norp = (Norp*)(userData); |
| 2769 |
norp->OnNormEvent(); |
| 2770 |
} |
| 2771 |
else |
| 2772 |
{ |
| 2773 |
ASSERT(0); // should never happen |
| 2774 |
} |
| 2775 |
} // end Norp::NormEventCallback() |
| 2776 |
|
| 2777 |
void Norp::OnNormEvent() |
| 2778 |
{ |
| 2779 |
NormEvent theEvent; |
| 2780 |
// We make a looping, non-blocking call to NormGetNextEvent() here |
| 2781 |
// to consume all available NORM notification events |
| 2782 |
while (NormGetNextEvent(norm_instance, &theEvent, false)) |
| 2783 |
{ |
| 2784 |
PLOG(PL_DETAIL, "Norp::OnNormEvent("); |
| 2785 |
// See if this event is for a "child" NorpSession |
| 2786 |
if (NORM_SESSION_INVALID != theEvent.session) |
| 2787 |
{ |
| 2788 |
NorpSession* norpSession = (NorpSession*)NormGetUserData(theEvent.session); |
| 2789 |
if (NULL != norpSession) |
| 2790 |
{ |
| 2791 |
PLOG(PL_DETAIL, "session %hu event)\n", norpSession->GetSessionId()); |
| 2792 |
norpSession->OnNormEvent(theEvent); |
| 2793 |
continue; |
| 2794 |
} |
| 2795 |
} |
| 2796 |
else |
| 2797 |
{ |
| 2798 |
//ASSERT(0); // should never happen |
| 2799 |
} |
| 2800 |
} |
| 2801 |
} // end Norp::OnNormEvent() |
| 2802 |
|
| 2803 |
|
| 2804 |
|