| 1 |
#ifndef _NORP |
| 2 |
#define _NORP |
| 3 |
|
| 4 |
#include "protoSocket.h" |
| 5 |
#include "protoTimer.h" |
| 6 |
#include "protoTree.h" |
| 7 |
#include "protoDispatcher.h" |
| 8 |
#include "protoPktSOCKS.h" |
| 9 |
#include "normApi.h" |
| 10 |
|
| 11 |
class Norp; |
| 12 |
|
| 13 |
// A "NorpSession" associates a SOCKS client session with either a remote |
| 14 |
// NORP endpoint (if one was or can be established) or a "direct connect" |
| 15 |
// (TCP, UDP) if the remote endpoint is not NORP-enabled |
| 16 |
|
| 17 |
// SOCKS requests/replies relayed via NORM command transmissions |
| 18 |
// are prepended with this to uniquely identify the command |
| 19 |
// (SOCKS request or reply and associate it with a NorpSession |
| 20 |
// TBD - or should the endpoint socket binding addrs/ports be used instead?? |
| 21 |
|
| 22 |
// |
| 23 |
// ProtoPktNORP is the packet format that NORP uses to relay SOCKS |
| 24 |
// requests and replies via the NORM application-defined command |
| 25 |
// signaling mechanism. The format of these commands is: |
| 26 |
// |
| 27 |
// 0 1 2 3 |
| 28 |
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 29 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 30 |
// | msgType | reserved | sessionId | |
| 31 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 32 |
// | normNodeId | |
| 33 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 34 |
// | normSrcPort | normDstPort | |
| 35 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 36 |
// | timestamp_sec | |
| 37 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 38 |
// | timestamp_usec | |
| 39 |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 |
// | Content ... | |
| 41 |
// |
| 42 |
|
| 43 |
|
| 44 |
class PortPool |
| 45 |
{ |
| 46 |
public: |
| 47 |
PortPool(UINT16 basePort); |
| 48 |
|
| 49 |
UINT16 GrabPort(); |
| 50 |
void ReleasePort(UINT16 thePort); |
| 51 |
|
| 52 |
private: |
| 53 |
UINT16 base_port; |
| 54 |
bool port_array[50]; |
| 55 |
}; // end class PortPool |
| 56 |
|
| 57 |
class NorpMsg : public ProtoPkt |
| 58 |
{ |
| 59 |
public: |
| 60 |
NorpMsg(UINT32* bufferPtr = NULL, |
| 61 |
unsigned int numBytes = 0, |
| 62 |
bool initFromBuffer = true, |
| 63 |
bool freeOnDestruct = false); |
| 64 |
~NorpMsg(); |
| 65 |
|
| 66 |
bool InitFromBuffer(unsigned int msgLength, |
| 67 |
UINT32* bufferPtr = NULL, |
| 68 |
unsigned int numBytes = 0, |
| 69 |
bool freeOnDestruct = false) |
| 70 |
{return ProtoPkt::InitFromBuffer(msgLength, bufferPtr, numBytes, freeOnDestruct);} |
| 71 |
|
| 72 |
enum Type |
| 73 |
{ |
| 74 |
MSG_INVALID = 0, |
| 75 |
SOCKS_REQ, // "content" is a SOCKS5 request |
| 76 |
REQ_ACK, // request acknowledgment (no content) |
| 77 |
SOCKS_REP, // "content" is a SOCKS5 request |
| 78 |
REP_ACK, // reply acknowledgment (no content) |
| 79 |
ORIG_END, // sent by originator to end session |
| 80 |
CORR_END, // sent by correspondent to end session |
| 81 |
ACK_END // end session acknowledgment (by either) |
| 82 |
}; |
| 83 |
|
| 84 |
Type GetType() const |
| 85 |
{return (Type)((UINT8*)buffer_ptr)[OFFSET_TYPE];} |
| 86 |
|
| 87 |
UINT16 GetSessionId() const |
| 88 |
{return (Type)ntohs(((UINT16*)buffer_ptr)[OFFSET_SESSION_ID]);} |
| 89 |
|
| 90 |
// note "GetUINT32()" does ntohl() conversion |
| 91 |
NormNodeId GetNodeId() const |
| 92 |
{return (NormNodeId)GetUINT32(buffer_ptr + OFFSET_NODE_ID);} |
| 93 |
|
| 94 |
UINT16 GetSourcePort() const |
| 95 |
{return (Type)ntohs(((UINT16*)buffer_ptr)[OFFSET_SRC_PORT]);} |
| 96 |
|
| 97 |
UINT16 GetDestinationPort() const |
| 98 |
{return (Type)ntohs(((UINT16*)buffer_ptr)[OFFSET_DST_PORT]);} |
| 99 |
|
| 100 |
void GetTimestamp(struct timeval& timestamp) const |
| 101 |
{ |
| 102 |
timestamp.tv_sec = GetUINT32(buffer_ptr + OFFSET_SEC); |
| 103 |
timestamp.tv_usec = GetUINT32(buffer_ptr + OFFSET_USEC); |
| 104 |
} |
| 105 |
|
| 106 |
void GetTimestamp(ProtoTime& theTime) const |
| 107 |
{ |
| 108 |
struct timeval& timestamp = theTime.AccessTimeVal(); |
| 109 |
timestamp.tv_sec = GetUINT32(buffer_ptr + OFFSET_SEC); |
| 110 |
timestamp.tv_usec = GetUINT32(buffer_ptr + OFFSET_USEC); |
| 111 |
} |
| 112 |
|
| 113 |
const UINT32* GetContentPtr() const |
| 114 |
{return ((const UINT32*)AccessContentPtr());} |
| 115 |
|
| 116 |
UINT32* AccessContentPtr() const |
| 117 |
{return (buffer_ptr + OFFSET_CONTENT);} |
| 118 |
|
| 119 |
unsigned int GetContentLength() const |
| 120 |
{return (GetLength() - (OFFSET_CONTENT*4));} |
| 121 |
|
| 122 |
// Packet building methods |
| 123 |
|
| 124 |
void Init() |
| 125 |
{ |
| 126 |
SetType(MSG_INVALID); |
| 127 |
SetLength(0); |
| 128 |
} |
| 129 |
|
| 130 |
void SetType(Type msgType) |
| 131 |
{ |
| 132 |
((UINT8*)buffer_ptr)[OFFSET_TYPE] = (UINT8)msgType; |
| 133 |
pkt_length = (pkt_length < 1) ? 1 : pkt_length; |
| 134 |
} |
| 135 |
|
| 136 |
void SetSessionId(UINT16 sessionId) |
| 137 |
{ |
| 138 |
((UINT8*)buffer_ptr)[OFFSET_RESERVED] = 0; |
| 139 |
((UINT16*)buffer_ptr)[OFFSET_SESSION_ID] = htons(sessionId); |
| 140 |
pkt_length = (pkt_length < 4) ? 4 : pkt_length; |
| 141 |
} |
| 142 |
|
| 143 |
void SetNodeId(NormNodeId nodeId) |
| 144 |
{ |
| 145 |
SetUINT32(buffer_ptr + OFFSET_NODE_ID, nodeId); |
| 146 |
pkt_length = (pkt_length<8) ? 8 : pkt_length; |
| 147 |
} |
| 148 |
|
| 149 |
void SetSourcePort(UINT16 srcPort) |
| 150 |
{ |
| 151 |
((UINT16*)buffer_ptr)[OFFSET_SRC_PORT] = htons(srcPort); |
| 152 |
pkt_length = (pkt_length < 10) ? 10 : pkt_length; |
| 153 |
} |
| 154 |
|
| 155 |
void SetDestinationPort(UINT16 dstPort) |
| 156 |
{ |
| 157 |
((UINT16*)buffer_ptr)[OFFSET_DST_PORT] = htons(dstPort); |
| 158 |
pkt_length = (pkt_length < 12) ? 12 : pkt_length; |
| 159 |
} |
| 160 |
|
| 161 |
void SetTimestamp(struct timeval& timestamp) |
| 162 |
{ |
| 163 |
SetUINT32(buffer_ptr+OFFSET_SEC, timestamp.tv_sec); |
| 164 |
SetUINT32(buffer_ptr+OFFSET_USEC, timestamp.tv_usec); |
| 165 |
pkt_length = (pkt_length < 20) ? 20 : pkt_length; |
| 166 |
} |
| 167 |
|
| 168 |
bool SetContent(const char* data, unsigned int numBytes) |
| 169 |
{ |
| 170 |
if ((numBytes + (4*OFFSET_CONTENT)) > GetBufferLength()) return false; |
| 171 |
memcpy((char*)(buffer_ptr+OFFSET_CONTENT), data, numBytes); |
| 172 |
SetContentLength(numBytes); |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
void SetContentLength(unsigned int numBytes) |
| 177 |
{pkt_length = (4*OFFSET_CONTENT) + numBytes;} |
| 178 |
|
| 179 |
private: |
| 180 |
enum |
| 181 |
{ |
| 182 |
OFFSET_TYPE = 0, // UINT8 offset |
| 183 |
OFFSET_RESERVED = OFFSET_TYPE + 1, // UINT8 offset |
| 184 |
OFFSET_SESSION_ID = (OFFSET_RESERVED+1)/2, // UINT16 offset |
| 185 |
OFFSET_NODE_ID = (2*(OFFSET_SESSION_ID+1))/4, // UINT32 offset |
| 186 |
OFFSET_SRC_PORT = (4*(OFFSET_NODE_ID+1))/2, // UINT16 offset |
| 187 |
OFFSET_DST_PORT = OFFSET_SRC_PORT + 1, // UINT16 offset |
| 188 |
OFFSET_SEC = (2*(OFFSET_DST_PORT+1))/4, // UINT32 offset |
| 189 |
OFFSET_USEC = OFFSET_SEC + 1, // UINT32 offset |
| 190 |
OFFSET_CONTENT = OFFSET_USEC + 1 // UINT32 offset |
| 191 |
}; |
| 192 |
|
| 193 |
}; // end class NorpMsg |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
// A NorpPreset is a non-SOCKS proxied session where a |
| 198 |
// local TCP port is monitored and connections are proxied |
| 199 |
// proxied to a preset remote destination |
| 200 |
class NorpPreset : public ProtoTree::Item |
| 201 |
{ |
| 202 |
public: |
| 203 |
NorpPreset(Norp& norpController, UINT16 tcpPort, const ProtoAddress& dstAddr, const ProtoAddress& norpAddr); |
| 204 |
~NorpPreset(); |
| 205 |
|
| 206 |
const ProtoAddress& GetDstAddr() const |
| 207 |
{return dst_addr;} |
| 208 |
const ProtoAddress& GetNorpAddr() const |
| 209 |
{return (norp_addr.IsValid() ? norp_addr : dst_addr);} |
| 210 |
|
| 211 |
bool Listen(); |
| 212 |
|
| 213 |
ProtoSocket& AccessServerSocket() |
| 214 |
{return server_socket;} |
| 215 |
|
| 216 |
private: |
| 217 |
const char* GetKey() const |
| 218 |
{return ((const char*)&tcp_port);} |
| 219 |
unsigned int GetKeysize() const |
| 220 |
{return (sizeof(UINT16) << 3);} |
| 221 |
|
| 222 |
|
| 223 |
void OnSocketEvent(ProtoSocket& theSocket, ProtoSocket::Event theEvent); |
| 224 |
|
| 225 |
Norp& controller; |
| 226 |
UINT16 tcp_port; |
| 227 |
ProtoAddress dst_addr; |
| 228 |
ProtoAddress norp_addr; |
| 229 |
ProtoSocket server_socket; |
| 230 |
|
| 231 |
}; // end class NorpPreset |
| 232 |
|
| 233 |
class NorpPresetList : public ProtoTreeTemplate<NorpPreset> |
| 234 |
{ |
| 235 |
public: |
| 236 |
NorpPreset* FindPreset(UINT16 tcpPort) const |
| 237 |
{return Find((char*)&tcpPort, sizeof(UINT16) << 3);} |
| 238 |
}; // end class NorpPresetList |
| 239 |
|
| 240 |
class NorpSession : public ProtoTree::Item |
| 241 |
{ |
| 242 |
public: |
| 243 |
NorpSession(Norp& norpController, UINT16 sessionId, NormNodeId originatorId = NORM_NODE_NONE); |
| 244 |
~NorpSession(); |
| 245 |
|
| 246 |
enum SocksState |
| 247 |
{ |
| 248 |
SOCKS_VOID, // when session gives self back to "controller" at Close |
| 249 |
SOCKS_IDLE, // initial, inactive state (socks_client_socket is closed) |
| 250 |
SOCKS_GET_AUTH_REQ, // first active state, accumulating auth request bytes from client |
| 251 |
SOCKS_PUT_AUTH_REP, // next, writing auth reply bytes to client |
| 252 |
SOCKS_GET_REQUEST, // next, accumulate SOCKS request bytes from client |
| 253 |
SOCKS_PUT_REQUEST, // relaying request to remote NORP peer |
| 254 |
SOCKS_CONNECTING, // attempt to satisfy request via remote connect, etc (this may need to be refined) |
| 255 |
SOCKS_GET_REPLY, // awaiting reply from remote NORP peer |
| 256 |
SOCKS_PUT_REPLY, // next, writing SOCK reply bytes to client |
| 257 |
SOCKS_CONNECTED, // finally, reading/writing data from/to client |
| 258 |
SOCKS_SHUTDOWN // session ended, in process of shutting down |
| 259 |
}; |
| 260 |
|
| 261 |
void SetProxyAddress(const ProtoAddress& proxyAddr) |
| 262 |
{proxy_addr = proxyAddr;} |
| 263 |
|
| 264 |
void SetNormRateBounds(double rateMin, double rateMax); |
| 265 |
|
| 266 |
NormNodeId GetNodeId() const; |
| 267 |
|
| 268 |
UINT16 GetSessionId() const |
| 269 |
{return session_id.identifier;} |
| 270 |
|
| 271 |
NormNodeId GetOriginatorId() const |
| 272 |
{return session_id.originator;} |
| 273 |
|
| 274 |
bool IsRemoteSession() const |
| 275 |
{return (NORM_SESSION_INVALID != norm_session);} |
| 276 |
|
| 277 |
bool IsRemoteOriginator() const // assumes "IsRemoteSession()" |
| 278 |
{return (NORM_NODE_NONE == GetOriginatorId());} |
| 279 |
|
| 280 |
bool AcceptClientConnection(ProtoSocket& serverSocket, bool normEnable = true); |
| 281 |
|
| 282 |
bool AcceptPresetClientConnection(NorpPreset& preset, bool normEnable); |
| 283 |
|
| 284 |
SocksState GetSocksState() const |
| 285 |
{return socks_state;} |
| 286 |
|
| 287 |
const ProtoAddress& GetClientAddress() const |
| 288 |
{return socks_client_socket.GetDestination();} |
| 289 |
|
| 290 |
void Shutdown(); // hard shutdown, including signaling remote via NORP messaging |
| 291 |
|
| 292 |
bool ShutdownComplete() const; // Tests for inband shutdown completion |
| 293 |
|
| 294 |
void Close(); // hard local shutdown and cleanup including session deletion by controller |
| 295 |
|
| 296 |
void OnNormEvent(NormEvent& theEvent); |
| 297 |
|
| 298 |
bool OnClientRequest(const ProtoPktSOCKS::Request request, const ProtoAddress& srcAddr); |
| 299 |
|
| 300 |
bool MakeDirectConnect(); // convert remote session to direct connect |
| 301 |
|
| 302 |
// handles request relayed from remote NORP server |
| 303 |
bool OnRemoteRequest(const NorpMsg& theMsg, const ProtoAddress& senderAddr); |
| 304 |
|
| 305 |
bool OnRemoteRequestAcknowledgment(const NorpMsg& theMsg, const ProtoAddress& senderAddr); |
| 306 |
|
| 307 |
bool OnRemoteReply(const NorpMsg& theMsg, const ProtoAddress& senderAddr); |
| 308 |
|
| 309 |
bool OnRemoteReplyAcknowledgment(const NorpMsg& theMsg); |
| 310 |
|
| 311 |
typedef struct Moniker |
| 312 |
{ |
| 313 |
NormNodeId originator; |
| 314 |
UINT16 identifier; // TBD - make this UINT32? |
| 315 |
} Moniker; |
| 316 |
|
| 317 |
static bool GetNormNodeAddress(NormNodeHandle nodeHandle, ProtoAddress& theAddr); |
| 318 |
|
| 319 |
static const double NORP_RTT_MIN; |
| 320 |
static const double NORP_RTT_MAX; |
| 321 |
static const double NORP_RTT_DEFAULT; |
| 322 |
|
| 323 |
private: |
| 324 |
void ActivateTimer(ProtoTimer& theTimer); |
| 325 |
|
| 326 |
const char* GetKey() const |
| 327 |
{return ((const char*)&session_id);} |
| 328 |
unsigned int GetKeysize() const |
| 329 |
{return (sizeof(Moniker) << 3);} |
| 330 |
|
| 331 |
void OnSocksClientEvent(ProtoSocket& theSocket, |
| 332 |
ProtoSocket::Event theEvent); |
| 333 |
|
| 334 |
// Note our "norp_tx_socket" is "connected" so we get error message when "port unreachable" |
| 335 |
bool SendMessage(const NorpMsg& msg) |
| 336 |
{ |
| 337 |
unsigned int numBytes = msg.GetLength(); |
| 338 |
return norp_tx_socket.Send((const char*)msg.GetBuffer(), numBytes); |
| 339 |
} |
| 340 |
|
| 341 |
bool OnNorpMsgTimeout(ProtoTimer& theTimer); |
| 342 |
|
| 343 |
bool OnCloseTimeout(ProtoTimer& theTimer); |
| 344 |
|
| 345 |
void OnUdpRelayEvent(ProtoSocket& theSocket, |
| 346 |
ProtoSocket::Event theEvent); |
| 347 |
|
| 348 |
// These methods are invoked as needed upon different events and SOCKS state transitions |
| 349 |
bool GetClientAuthRequest(); |
| 350 |
bool PutClientAuthReply(); |
| 351 |
bool GetClientRequest(); |
| 352 |
bool ConnectToRemote(const ProtoAddress& srcAddr); |
| 353 |
bool BindRemote(UINT16 bindPort); |
| 354 |
bool OpenUdpRelay(); |
| 355 |
bool PutClientReply(); |
| 356 |
bool GetClientData(); |
| 357 |
bool PutClientData(); |
| 358 |
|
| 359 |
bool PutRemoteRequest(const ProtoPktSOCKS::Request& request, const ProtoAddress& remoteAddr); |
| 360 |
bool OriginatorStartNorm(const ProtoAddress& senderAddr, |
| 361 |
NormNodeId corrNormId, |
| 362 |
UINT16 normSrcPort, |
| 363 |
UINT16 normDstPort); |
| 364 |
|
| 365 |
bool PutRemoteReply(); |
| 366 |
|
| 367 |
void OnSocksRemoteEvent(ProtoSocket& theSocket, |
| 368 |
ProtoSocket::Event theEvent); |
| 369 |
// These methods are invoked as needed upon different events and SOCKS state transitions |
| 370 |
bool GetRemoteData(); |
| 371 |
bool PutRemoteData(); |
| 372 |
|
| 373 |
enum {SOCKS_BUFFER_SIZE = 16384}; |
| 374 |
enum {NORP_BUFFER_SIZE = 512}; |
| 375 |
enum {NORM_BUFFER_SIZE = (8192*1024)}; |
| 376 |
|
| 377 |
unsigned int WriteToNormStream(const char* buffer, unsigned int numBytes); |
| 378 |
void FlushNormStream(bool eom, NormFlushMode flushMode); |
| 379 |
unsigned int ComputeNormStreamBufferSegmentCount(unsigned int bufferBytes, UINT16 segmentSize, UINT16 blockSize); |
| 380 |
|
| 381 |
|
| 382 |
// Member variables |
| 383 |
Norp& controller; |
| 384 |
bool is_preset; // special "preset" (non-SOCKS) proxy session |
| 385 |
ProtoTimer close_timer; |
| 386 |
Moniker session_id; |
| 387 |
ProtoAddress proxy_addr; // address to advertise for remote connections |
| 388 |
SocksState socks_state; |
| 389 |
ProtoSocket socks_client_socket; // TCP connection from this server to SOCKS client |
| 390 |
ProtoSocket socks_remote_socket; // TCP connection to remote when "direct connect" is used |
| 391 |
ProtoSocket udp_relay_socket; // UDP relay socket (used for UDP_ASSOC requests) |
| 392 |
ProtoAddress udp_client_addr; // address of local UDP_ASSOC client |
| 393 |
|
| 394 |
UINT32 client_buffer[SOCKS_BUFFER_SIZE/sizeof(UINT32)]; // usually data received from client |
| 395 |
unsigned int client_pending; // total bytes pending reception or transmission |
| 396 |
unsigned int client_index; // current index |
| 397 |
UINT32 remote_buffer[SOCKS_BUFFER_SIZE/sizeof(UINT32)]; // usually for data received from remote |
| 398 |
unsigned int remote_pending; // total bytes pending reception or transmission |
| 399 |
unsigned int remote_index; // current index |
| 400 |
|
| 401 |
// NORM-related state |
| 402 |
bool norm_enable; |
| 403 |
ProtoSocket norp_tx_socket; // used to send NORP signaling messages |
| 404 |
ProtoAddress norp_remote_addr; |
| 405 |
ProtoTimer norp_msg_timer; |
| 406 |
NorpMsg norp_msg; // current message being transmitted |
| 407 |
UINT32 norp_msg_buffer[NORP_BUFFER_SIZE/sizeof(UINT32)]; |
| 408 |
double norp_rtt_estimate; // from REQUEST<->ACK or REPLY<->ACK exchange |
| 409 |
NormSessionHandle norm_session; |
| 410 |
NormObjectHandle norm_tx_stream; |
| 411 |
NormObjectHandle norm_rx_stream; |
| 412 |
bool norm_rx_pending; |
| 413 |
bool norm_sender_heard; |
| 414 |
double persist_interval; // in seconds, to timeout NORM delivery persistence after local TCP disconnect |
| 415 |
ProtoTime persist_start_time; // marks start of persistence timeout |
| 416 |
double norm_rate_min; |
| 417 |
double norm_rate_max; |
| 418 |
|
| 419 |
UINT16 norm_segment_size; |
| 420 |
unsigned int norm_stream_buffer_max; |
| 421 |
unsigned int norm_stream_buffer_count; |
| 422 |
unsigned int norm_stream_bytes_remain; |
| 423 |
bool norm_watermark_pending; |
| 424 |
|
| 425 |
}; // end class NorpSession |
| 426 |
|
| 427 |
class NorpSessionList : public ProtoTreeTemplate<NorpSession> |
| 428 |
{ |
| 429 |
public: |
| 430 |
NorpSession* FindSession(UINT16 sessionId, NormNodeId originatorId = NORM_NODE_NONE) const |
| 431 |
{ |
| 432 |
NorpSession::Moniker id; |
| 433 |
memset(&id, 0, sizeof(NorpSession::Moniker)); |
| 434 |
id.originator = originatorId; |
| 435 |
id.identifier = sessionId; |
| 436 |
return Find((char*)&id, sizeof(NorpSession::Moniker) << 3); |
| 437 |
} // end NorpSessionList::FindSession() |
| 438 |
}; // end class NorpSessionList |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
// The "Norp" class is an instance of a SOCKS server that manages |
| 443 |
// SOCKS/NORM (or direct-connect) sessions |
| 444 |
class Norp |
| 445 |
{ |
| 446 |
public: |
| 447 |
Norp(ProtoDispatcher& theDispatcher); |
| 448 |
~Norp(); |
| 449 |
|
| 450 |
enum |
| 451 |
{ |
| 452 |
DEFAULT_SOCKS_PORT = 7000, // SOCKS server TCP listen port used for NORP |
| 453 |
DEFAULT_NORP_PORT = 7001, // Where NORP listens for UDP signaling, relayed commands, etc |
| 454 |
DEFAULT_NORM_PORT = 7002 // Port used for NORM data transfer |
| 455 |
}; |
| 456 |
|
| 457 |
static const double DEFAULT_TX_RATE; |
| 458 |
static const double DEFAULT_PERSIST_INTERVAL; |
| 459 |
|
| 460 |
void SetSocksPort(UINT16 thePort) |
| 461 |
{socks_port = thePort;} |
| 462 |
|
| 463 |
void SetProxyAddress(const ProtoAddress& proxyAddr) |
| 464 |
{proxy_addr = proxyAddr;} |
| 465 |
|
| 466 |
const ProtoAddress& GetProxyAddress() const |
| 467 |
{return proxy_addr;} |
| 468 |
|
| 469 |
bool StartServer(bool normEnable); |
| 470 |
|
| 471 |
void StopServer(); |
| 472 |
|
| 473 |
void SetLocalNorpPort(UINT16 thePort) |
| 474 |
{norp_local_port = thePort;} |
| 475 |
|
| 476 |
void SetRemoteNorpPort(UINT16 thePort) |
| 477 |
{norp_remote_port = thePort;} |
| 478 |
|
| 479 |
UINT16 GetNorpPort() const |
| 480 |
{return norp_remote_port;} |
| 481 |
|
| 482 |
// In future, this will reference a "routing" |
| 483 |
// table using the "destAddr" |
| 484 |
bool GetRemoteNorpAddress(const ProtoAddress& destAddr, |
| 485 |
ProtoAddress& norpAddr) const |
| 486 |
{ |
| 487 |
norpAddr = norp_remote_addr.IsValid() ? norp_remote_addr : destAddr; |
| 488 |
return true; |
| 489 |
} |
| 490 |
// This sets a remote "norp" peer address that is used instead of |
| 491 |
// connecting directly to destination endpoints |
| 492 |
// TBD - add more routing options than this |
| 493 |
void SetRemoteNorpAddress(const ProtoAddress& remoteAddr) |
| 494 |
{norp_remote_addr = remoteAddr;} |
| 495 |
|
| 496 |
void SetInitialRtt(double seconds) |
| 497 |
{norp_rtt_init = seconds;} |
| 498 |
|
| 499 |
double GetInitialRtt() const |
| 500 |
{return norp_rtt_init;} |
| 501 |
|
| 502 |
// NORM protocol parameters |
| 503 |
void SetNormNodeId(NormNodeId nodeId) |
| 504 |
{norm_node_id = nodeId;} |
| 505 |
NormNodeId GetNormNodeId() const |
| 506 |
{return norm_node_id;} |
| 507 |
|
| 508 |
NormInstanceHandle GetNormInstance() const |
| 509 |
{return norm_instance;} |
| 510 |
|
| 511 |
void SetNormPort(UINT16 thePort) |
| 512 |
{norm_port = thePort;} |
| 513 |
UINT16 GetNormPort() const |
| 514 |
{return norm_port;} |
| 515 |
|
| 516 |
/* |
| 517 |
UINT16 GrabPort() |
| 518 |
{return port_pool.GrabPort();} |
| 519 |
void ReleasePort(UINT16 thePort) |
| 520 |
{port_pool.ReleasePort(thePort);} |
| 521 |
*/ |
| 522 |
|
| 523 |
enum NormCC |
| 524 |
{ |
| 525 |
NORM_CC, |
| 526 |
NORM_CCE, |
| 527 |
NORM_CCL, |
| 528 |
NORM_FIXED |
| 529 |
}; |
| 530 |
|
| 531 |
void SetNormCC(NormCC ccMode) |
| 532 |
{norm_cc_mode = ccMode;} |
| 533 |
NormCC GetNormCC() const |
| 534 |
{return norm_cc_mode;} |
| 535 |
|
| 536 |
// For fixed-rate operation |
| 537 |
void SetNormTxRate(double txRate) |
| 538 |
{norm_tx_rate = txRate;} |
| 539 |
double GetNormTxRate() const |
| 540 |
{return norm_tx_rate;} |
| 541 |
|
| 542 |
// Sets upper bound on _cumulative_ NormSession |
| 543 |
// transmit rates |
| 544 |
void SetNormTxLimit(double txLimit) |
| 545 |
{norm_tx_limit = txLimit;} |
| 546 |
|
| 547 |
double GetNormTxLimit() const |
| 548 |
{return norm_tx_limit;} |
| 549 |
|
| 550 |
void SetNormTrace(bool enable) |
| 551 |
{norm_trace = enable;} |
| 552 |
bool GetNormTrace() const |
| 553 |
{return norm_trace;} |
| 554 |
|
| 555 |
void SetNormSegmentSize(UINT16 numBytes) |
| 556 |
{norm_segment_size = numBytes;} |
| 557 |
UINT16 GetNormSegmentSize() const |
| 558 |
{return norm_segment_size;} |
| 559 |
|
| 560 |
void SetNormBlockSize(UINT32 numSegments) |
| 561 |
{norm_block_size = numSegments;} |
| 562 |
UINT16 GetNormBlockSize() const |
| 563 |
{return norm_block_size;} |
| 564 |
|
| 565 |
void SetNormParityCount(UINT16 numSegments) |
| 566 |
{norm_parity_count = numSegments;} |
| 567 |
UINT16 GetNormParityCount() const |
| 568 |
{return norm_parity_count;} |
| 569 |
|
| 570 |
void SetNormParityAuto(UINT16 numSegments) |
| 571 |
{norm_parity_auto = numSegments;} |
| 572 |
UINT16 GetNormParityAuto() const |
| 573 |
{return norm_parity_auto;} |
| 574 |
|
| 575 |
ProtoSocket::Notifier& GetSocketNotifier() const |
| 576 |
{return static_cast<ProtoSocket::Notifier&>(dispatcher);} |
| 577 |
|
| 578 |
void ActivateTimer(ProtoTimer& theTimer) |
| 579 |
{dispatcher.ActivateTimer(theTimer);} |
| 580 |
|
| 581 |
bool SendMessage(const NorpMsg& msg, const ProtoAddress& dstAddr); |
| 582 |
|
| 583 |
void OnNorpSocketEvent(ProtoSocket& theSocket, |
| 584 |
ProtoSocket::Event theEvent); |
| 585 |
|
| 586 |
void OnSessionClose(NorpSession& theSession); |
| 587 |
|
| 588 |
bool AddPreset(UINT16 tcpPort, const ProtoAddress& dstAddr, const ProtoAddress& norpAddr); |
| 589 |
bool AcceptPresetClientConnection(NorpPreset& preset); |
| 590 |
|
| 591 |
private: |
| 592 |
void AddSession(NorpSession& session); |
| 593 |
void RemoveSession(NorpSession& session); |
| 594 |
|
| 595 |
void OnSocksServerEvent(ProtoSocket& theSocket, |
| 596 |
ProtoSocket::Event theEvent); |
| 597 |
|
| 598 |
static void NormEventCallback(ProtoDispatcher::Descriptor descriptor, |
| 599 |
ProtoDispatcher::Event theEvent, |
| 600 |
const void* userData); |
| 601 |
|
| 602 |
void OnNormEvent(); |
| 603 |
void OnRemoteRequest(const NorpMsg& theMsg, const ProtoAddress& senderAddr); |
| 604 |
void OnRemoteReply(const NorpMsg& theMsg, const ProtoAddress& senderAddr); |
| 605 |
|
| 606 |
// Member variables |
| 607 |
ProtoDispatcher& dispatcher; // TBD - make Norp multi-threaded w/ a pool of dispatchers |
| 608 |
|
| 609 |
ProtoSocket socks_server_socket; // (SOCKS clients connect to this) |
| 610 |
UINT16 socks_port; |
| 611 |
ProtoAddress proxy_addr; |
| 612 |
UINT16 next_session_id; // used to dole out unique session ids |
| 613 |
NorpSessionList session_list; // list of client and remote sessions |
| 614 |
unsigned int session_count; |
| 615 |
NorpPresetList preset_list; // list of preset proxies w/ listening TCP socket |
| 616 |
|
| 617 |
ProtoSocket norp_rx_socket; // UDP socket for receiving NORP signaling |
| 618 |
UINT16 norp_local_port; // For loopback debugging, we allow "local" server port to be different than "remote" |
| 619 |
UINT16 norp_remote_port; |
| 620 |
ProtoAddress norp_remote_addr; // if unspecified, we proxy directly to connection destination address |
| 621 |
double norp_rtt_init; // used as initial roundtrip estimate for NORP signaling |
| 622 |
|
| 623 |
bool norm_enable; |
| 624 |
NormInstanceHandle norm_instance; |
| 625 |
NormNodeId norm_node_id; |
| 626 |
|
| 627 |
// NORM parameters (including some for debugging |
| 628 |
UINT16 norm_port; |
| 629 |
NormCC norm_cc_mode; |
| 630 |
double norm_tx_rate; // in bits/sec, only applicable for cc_mode == NORM_FIXED |
| 631 |
double norm_tx_limit; // in bits/sec (_cumulative_ rate limit option) |
| 632 |
char iface_name[64]; // Network interface |
| 633 |
UINT16 norm_segment_size; // payload bytes per NORM_DATA message |
| 634 |
UINT16 norm_block_size; // number of user data segments per FEC coding block |
| 635 |
UINT16 norm_parity_count; // number of _computed_ parity segments per FEC coding block |
| 636 |
UINT16 norm_parity_auto; // number of proactive (automatically sent) parity segments per block |
| 637 |
|
| 638 |
bool norm_trace; |
| 639 |
|
| 640 |
//PortPool port_pool; |
| 641 |
|
| 642 |
}; // end class Norp |
| 643 |
|
| 644 |
#endif // _NORP |