Debian Codegraph @

norp/include/protoPktSOCKS.h

raw
1
2 #ifndef _PROTO_SOCKS
3 #define _PROTO_SOCKS
4
5 #include "protoPkt.h"
6 #include "protoAddress.h"
7
8 // These classes provide message parsing and building for
9 // the RFC 1928 SOCKS5 protocol specification.
10 // NOTES:
11 // 1) Only the methods needed for SOCKS server operation are defined here so far
12 // 2) Additional external helper logic is needed to incrementally read data from a
13 // SOCKS TCP client-server socket for a full SOCKS message. The message parsing
14 // here assumes the message has been read up to the point of whatever field
15 // is being requested.
16
17 namespace ProtoPktSOCKS
18 {
19 // SOCKS5 message definitions per RFC1928
20
21 // SOCKS5 auth request: VERSION + NMETHODS + METHOD_LIST ...
22 // 1 byte + 1 byte + N bytes
23
24 // SOCKS5 authentication methods
25 enum AuthType
26 {
27 AUTH_NONE = 0x00,
28 AUTH_GSSAPI = 0x01,
29 AUTH_USER = 0x02, // username / password
30 AUTH_INVALID = 0xff // no acceptable method
31 };
32
33 class AuthRequest : public ProtoPkt
34 {
35 public:
36 AuthRequest(UINT32* bufferPtr = NULL,
37 unsigned int numBytes = 0,
38 bool initFromBuffer = true,
39 bool freeOnDestruct = false);
40 ~AuthRequest();
41
42 bool InitFromBuffer(unsigned int reqLength,
43 UINT32* bufferPtr = NULL,
44 unsigned int numBytes = 0,
45 bool freeOnDestruct = false)
46 {return ProtoPkt::InitFromBuffer(reqLength, bufferPtr, numBytes, freeOnDestruct);}
47
48 UINT8 GetVersion() const
49 {return ((pkt_length > OFFSET_VERSION) ?
50 (((UINT8*)buffer_ptr)[OFFSET_VERSION]) : 0);}
51
52 UINT8 GetMethodCount() const
53 {return ((pkt_length > OFFSET_NMETHOD) ?
54 (((UINT8*)buffer_ptr)[OFFSET_NMETHOD]) : 0);}
55
56 AuthType GetMethod(UINT8 index) const
57 {return ((pkt_length > (unsigned int)(OFFSET_METHODS + index)) ?
58 (AuthType)(((UINT8*)buffer_ptr)[OFFSET_METHODS + index]) : AUTH_INVALID);}
59
60 // TBD - implement auth request building (don't need this for a server)
61
62 private:
63 enum
64 {
65 OFFSET_VERSION = 0, // 1 byte
66 OFFSET_NMETHOD = OFFSET_VERSION + 1, // 1 byte
67 OFFSET_METHODS = OFFSET_NMETHOD + 1 // N bytes
68 };
69
70 }; // end class ProtoPktSOCKS::AuthRequest
71
72 // SOCKS5 auth reply: VERSION + METHOD_SELECTED
73 // bytes: 1 + 1
74 class AuthReply : public ProtoPkt
75 {
76 public:
77 AuthReply(UINT32* bufferPtr = NULL,
78 unsigned int numBytes = 0,
79 bool initFromBuffer = true,
80 bool freeOnDestruct = false);
81 ~AuthReply();
82
83 bool InitFromBuffer(unsigned int replyLength,
84 UINT32* bufferPtr = NULL,
85 unsigned int numBytes = 0,
86 bool freeOnDestruct = false)
87 {return ProtoPkt::InitFromBuffer(replyLength, bufferPtr, numBytes, freeOnDestruct);}
88
89 UINT8 GetVersion() const
90 {return ((pkt_length > OFFSET_VERSION) ?
91 (((UINT8*)buffer_ptr)[OFFSET_VERSION]) : 0);}
92
93 UINT8 GetMethod() const
94 {return ((pkt_length > OFFSET_METHOD) ?
95 (((UINT8*)buffer_ptr)[OFFSET_METHOD]) : AUTH_INVALID);}
96
97 void SetVersion(UINT8 version)
98 {
99 ((UINT8*)buffer_ptr)[OFFSET_VERSION] = version;
100 pkt_length = 1;
101 }
102
103 void SetMethod(AuthType method)
104 {
105 ((UINT8*)buffer_ptr)[OFFSET_METHOD] = method;
106 pkt_length = 2;
107 }
108
109 private:
110 enum
111 {
112 OFFSET_VERSION = 0, // 1 byte
113 OFFSET_METHOD = OFFSET_VERSION + 1 // 1 byte
114 };
115
116 }; // end class ProtoPktSOCKS::AuthReply
117
118 // SOCKS5 request: VERSION + REQ + RESV + ADDR_TYP + DST.ADDR + DST.PORT
119 // bytes: 1 + 1 + 1 + 1 + N + 2
120
121 // SOCKS5 address types (1 byte)
122 enum AddrType
123 {
124 ADDR_INVALID = 0x00, // invalid addr type (for implementation)
125 IPv4 = 0x01, // 4-bytes, network byte order
126 NAME = 0x03, // 1-byte of "name" length, followed by "name"
127 IPv6 = 0x04 // 16-bytes, network byte order
128 }; // end ProtoPktSOCKS::AddrType
129
130 class Request : public ProtoPkt
131 {
132 public:
133 Request(UINT32* bufferPtr = NULL,
134 unsigned int numBytes = 0,
135 bool initFromBuffer = true,
136 bool freeOnDestruct = false);
137 ~Request();
138
139 enum Command
140 {
141 CMD_INVALID = 0x00,
142 CONNECT = 0x01,
143 BIND = 0x02,
144 UDP_ASSOC = 0x03 // Setup UDP association
145 }; // end ProtoPktSOCKS::Request::Command
146
147 bool InitFromBuffer(unsigned int replyLength,
148 UINT32* bufferPtr = NULL,
149 unsigned int numBytes = 0,
150 bool freeOnDestruct = false)
151 {return ProtoPkt::InitFromBuffer(replyLength, bufferPtr, numBytes, freeOnDestruct);}
152
153 UINT8 GetVersion() const
154 {return ((pkt_length > OFFSET_VERSION) ?
155 (((UINT8*)buffer_ptr)[OFFSET_VERSION]) : 0);}
156
157 Command GetCommand() const
158 {return ((pkt_length > OFFSET_COMMAND) ?
159 (Command)(((UINT8*)buffer_ptr)[OFFSET_COMMAND]) : CMD_INVALID);}
160
161 AddrType GetAddressType() const
162 {return ((pkt_length > OFFSET_ATYPE) ?
163 (AddrType)(((UINT8*)buffer_ptr)[OFFSET_ATYPE]) : ADDR_INVALID);}
164
165 UINT8 GetAddressLength() const;
166
167 const char* GetAddressPtr() const
168 {return (const char*)AccessAddressPtr();}
169
170 UINT16 GetPort() const;
171
172 bool GetAddress(ProtoAddress& theAddr) const;
173
174 // TBD - implement request building (don't need this for a server)
175
176 void SetVersion(UINT8 version)
177 {
178 ((UINT8*)buffer_ptr)[OFFSET_VERSION] = version;
179 pkt_length = 1;
180 }
181 void SetCommand(Command cmd)
182 {
183 ((UINT8*)buffer_ptr)[OFFSET_COMMAND] = (UINT8)cmd;
184 pkt_length = 2;
185 }
186 void SetAddressType(AddrType addrType)
187 {
188 ((UINT8*)buffer_ptr)[OFFSET_RESERVED] = 0;
189 ((UINT8*)buffer_ptr)[OFFSET_ATYPE] = (UINT8)addrType;
190 pkt_length = 4;
191 }
192 bool SetAddress(const ProtoAddress& theAddr); // note this sets port, too
193 bool SetName(const char* theName);
194 bool SetPort(UINT16 thePort);
195
196 private:
197 enum
198 {
199 OFFSET_VERSION = 0, // 1 byte
200 OFFSET_COMMAND = OFFSET_VERSION + 1, // 1 byte
201 OFFSET_RESERVED = OFFSET_COMMAND + 1, // 1 byte
202 OFFSET_ATYPE = OFFSET_RESERVED + 1, // 1 byte
203 OFFSET_ADDR = OFFSET_ATYPE + 1 // N bytes
204 };
205
206 char* AccessAddressPtr() const
207 {
208 char* addrPtr = ((char*)buffer_ptr) + OFFSET_ADDR;
209 return (NAME == GetAddressType()) ? (addrPtr + 1) : addrPtr;
210 }
211
212 }; // end class ProtoPktSOCKS::Request
213
214 // SOCKS5 REPLY: VERSION + REP + RESV + ATYP + BIND.ADDR + BIND.PORT
215 // bytes: 1 + 1 + 1 + 1 + N + 2
216 class Reply : public ProtoPkt
217 {
218 public:
219 Reply(UINT32* bufferPtr = NULL,
220 unsigned int numBytes = 0,
221 bool initFromBuffer = true,
222 bool freeOnDestruct = false);
223 ~Reply();
224
225 enum Type
226 {
227 SUCCESS = 0x00, // succeeded
228 FAILURE = 0x01, // general failure
229 DENIED = 0x02, // connection not allowed
230 NET_X = 0x03, // network unreachable
231 HOST_X = 0x04, // host unreachable
232 REFUSED = 0x05, // connection refused
233 TTL_X = 0x06, // TTL expired
234 CMD_X = 0x07, // command not supported
235 ADDR_X = 0x08, // address type not supported
236 TYPE_INVALID = 0xff // invalid reply type
237 }; // end ProtoPktSOCKS::Reply::Type
238
239 bool InitFromBuffer(unsigned int replyLength,
240 UINT32* bufferPtr = NULL,
241 unsigned int numBytes = 0,
242 bool freeOnDestruct = false)
243 {return ProtoPkt::InitFromBuffer(replyLength, bufferPtr, numBytes, freeOnDestruct);}
244
245 UINT8 GetVersion() const
246 {return ((pkt_length > OFFSET_VERSION) ?
247 (((UINT8*)buffer_ptr)[OFFSET_VERSION]) : 0);}
248
249 Type GetType() const
250 {return ((pkt_length > OFFSET_TYPE) ?
251 (Type)(((UINT8*)buffer_ptr)[OFFSET_TYPE]) : TYPE_INVALID);}
252
253 AddrType GetAddressType() const
254 {return ((pkt_length > OFFSET_ATYPE) ?
255 (AddrType)(((UINT8*)buffer_ptr)[OFFSET_ATYPE]) : ADDR_INVALID);}
256
257 UINT8 GetAddressLength() const;
258
259 const char* GetAddressPtr() const
260 {return (const char*)AccessAddressPtr();}
261
262 UINT16 GetPort() const;
263
264 bool GetAddress(ProtoAddress& theAddr) const;
265
266 // Reply building (call these in order
267 void SetVersion(UINT8 version)
268 {
269 ((UINT8*)buffer_ptr)[OFFSET_VERSION] = version;
270 pkt_length = 1;
271 }
272 void SetType(Type replyType)
273 {
274 ((UINT8*)buffer_ptr)[OFFSET_TYPE] = (UINT8)replyType;
275 pkt_length = 2;
276 }
277 void SetAddress(AddrType addrType, const char* addrPtr, UINT8 addrLen);
278 void SetPort(UINT16 thePort);
279 void SetAddress(const ProtoAddress& theAddr); // sets address and port from ProtoAddress
280
281 private:
282 enum
283 {
284 OFFSET_VERSION = 0, // 1 byte
285 OFFSET_TYPE = OFFSET_VERSION + 1, // 1 byte
286 OFFSET_RESERVED = OFFSET_TYPE + 1, // 1 byte
287 OFFSET_ATYPE = OFFSET_RESERVED + 1, // 1 byte
288 OFFSET_ADDR = OFFSET_ATYPE + 1 // N bytes
289 };
290
291 char* AccessAddressPtr() const
292 {
293 char* addrPtr = ((char*)buffer_ptr) + OFFSET_ADDR;
294 return (NAME == GetAddressType()) ? (addrPtr + 1) : addrPtr;
295 }
296
297 }; // end class ProtoPktSOCKS::Reply
298
299 // UDP relay header: RESV + FRAG + ATYP + DST.ADDR + DST.PORT + DATA
300 // bytes: 2 + 1 1 + N + 2 + K
301
302 class UdpRequest : public ProtoPkt
303 {
304 public:
305 UdpRequest(UINT32* bufferPtr = NULL,
306 unsigned int numBytes = 0,
307 bool initFromBuffer = true,
308 bool freeOnDestruct = false);
309 ~UdpRequest();
310
311 bool InitFromBuffer(unsigned int requestLength,
312 UINT32* bufferPtr = NULL,
313 unsigned int numBytes = 0,
314 bool freeOnDestruct = false)
315 {return ProtoPkt::InitFromBuffer(requestLength, bufferPtr, numBytes, freeOnDestruct);}
316
317 UINT8 GetFrag() const
318 {return ((pkt_length > OFFSET_FRAG) ?
319 (((UINT8*)buffer_ptr)[OFFSET_FRAG]) : 0);}
320
321 AddrType GetAddressType() const
322 {return ((pkt_length > OFFSET_ATYPE) ?
323 (AddrType)(((UINT8*)buffer_ptr)[OFFSET_ATYPE]) : ADDR_INVALID);}
324
325 UINT8 GetAddressLength() const;
326
327 const char* GetAddressPtr() const
328 {return (const char*)AccessAddressPtr();}
329
330 UINT16 GetPort() const;
331
332 bool GetAddress(ProtoAddress& theAddr) const;
333
334 const char* GetDataPtr() const
335 {return (const char*)AccessDataPtr();}
336
337 unsigned int GetDataLength() const
338 {return (GetLength() - (GetDataPtr() - ((const char*)buffer_ptr)));}
339
340
341 // Packet building (call these in order)
342 void SetFragment(UINT8 fragId)
343 {
344 ((UINT8*)buffer_ptr)[OFFSET_RESERVED] = 0;
345 ((UINT8*)buffer_ptr)[OFFSET_FRAG] = fragId;
346 pkt_length = OFFSET_FRAG + 1;
347 }
348 void SetAddress(AddrType addrType, const char* addrPtr, UINT8 addrLen);
349 void SetPort(UINT16 thePort);
350 void SetAddress(const ProtoAddress& theAddr); // sets address and port from ProtoAddress
351 void SetData(const char* data, unsigned int numBytes)
352 {
353 memcpy(AccessDataPtr(), data, numBytes);
354 SetDataLength(numBytes);
355 }
356 void SetDataLength(unsigned int numBytes)
357 {pkt_length = (GetDataPtr() - ((char*)buffer_ptr)) + numBytes;}
358
359 private:
360 enum
361 {
362 OFFSET_RESERVED = 0, // 2 bytes
363 OFFSET_FRAG = OFFSET_RESERVED + 2, // 1 byte
364 OFFSET_ATYPE = OFFSET_FRAG + 1, // 1 byte
365 OFFSET_ADDR = OFFSET_ATYPE + 1 // N bytes
366 };
367 char* AccessAddressPtr() const
368 {
369 char* addrPtr = ((char*)buffer_ptr) + OFFSET_ADDR;
370 return (NAME == GetAddressType()) ? (addrPtr + 1) : addrPtr;
371 }
372 char* AccessDataPtr() const
373 {return (AccessAddressPtr() + GetAddressLength() + 2);}
374
375 }; // end class ProtoPktSOCKS::UdpRequest
376
377 }; // end namespace ProtoPktSOCKS
378
379 #endif // _PROTO_SOCKS