Debian Codegraph @

norp/src/common/protoPktSOCKS.cpp

raw
1 #include "protoPktSOCKS.h"
2 #include "protoDebug.h"
3
4 ProtoPktSOCKS::AuthRequest::AuthRequest(UINT32* bufferPtr, unsigned int numBytes, bool initFromBuffer, bool freeOnDestruct)
5 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
6 {
7 if (initFromBuffer) InitFromBuffer(numBytes);
8 }
9
10 ProtoPktSOCKS::AuthRequest::~AuthRequest()
11 {
12 }
13
14 ProtoPktSOCKS::AuthReply::AuthReply(UINT32* bufferPtr, unsigned int numBytes, bool initFromBuffer, bool freeOnDestruct)
15 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
16 {
17 if (initFromBuffer) InitFromBuffer(numBytes);
18 }
19
20 ProtoPktSOCKS::AuthReply::~AuthReply()
21 {
22 }
23
24 ProtoPktSOCKS::Request::Request(UINT32* bufferPtr, unsigned int numBytes, bool initFromBuffer, bool freeOnDestruct)
25 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
26 {
27 if (initFromBuffer) InitFromBuffer(numBytes);
28 }
29
30 ProtoPktSOCKS::Request::~Request()
31 {
32 }
33
34 UINT8 ProtoPktSOCKS::Request::GetAddressLength() const
35 {
36 switch (GetAddressType())
37 {
38 case IPv4:
39 return 4;
40 case NAME:
41 // First byte of "name" is length value
42 return ((UINT8*)buffer_ptr)[OFFSET_ADDR];
43 case IPv6:
44 return 16;
45 default:
46 return 0;
47 }
48 } // end ProtoPktSOCKS::Request::GetAddressLength()
49
50 UINT16 ProtoPktSOCKS::Request::GetPort() const
51 {
52 const char* portPtr = GetAddressPtr() + GetAddressLength();
53 UINT16 temp16;
54 memcpy(&temp16, portPtr, 2);
55 return ntohs(temp16);
56 } // end ProtoPktSOCKS::Request::GetPort()
57
58
59 bool ProtoPktSOCKS::Request::GetAddress(ProtoAddress& theAddr) const
60 {
61 switch (GetAddressType())
62 {
63 case IPv4:
64 {
65 theAddr.SetRawHostAddress(ProtoAddress::IPv4, GetAddressPtr(), 4);
66 break;
67 }
68 case NAME:
69 {
70 char name[256];
71 UINT8 addrLen = GetAddressLength();
72 strncpy(name, GetAddressPtr(), addrLen);
73 name[addrLen] = '\0';
74 if (!theAddr.ResolveFromString(name))
75 {
76 PLOG(PL_ERROR, "ProtoPktSOCKS::Request::GetAddress() error: unable to resolve name \"%s\" to addr!\n", name);
77 return false;
78 } break;
79 }
80 case IPv6:
81 {
82 theAddr.SetRawHostAddress(ProtoAddress::IPv6, GetAddressPtr(), 16);
83 break;
84 }
85 default:
86 {
87 PLOG(PL_ERROR, "ProtoPktSOCKS::Request::GetAddress() error: invalid address type!\n");
88 return false;
89 }
90 }
91 theAddr.SetPort(GetPort());
92 return true;
93 } // end ProtoPktSOCKS::Request::GetAddress()
94
95 bool ProtoPktSOCKS::Request::SetAddress(const ProtoAddress& theAddr)
96 {
97 AddrType addrType;
98 UINT8 addrLength;
99 switch (theAddr.GetType())
100 {
101 case ProtoAddress::IPv4:
102 addrType = IPv4;
103 addrLength = 4;
104 break;
105 case ProtoAddress::IPv6:
106 addrType = IPv6;
107 addrLength = 16;
108 break;
109 default:
110 PLOG(PL_ERROR, "ProtoPktSOCKS::Request::SetAddress() error: invalid address type!\n");
111 return false;
112 }
113 SetAddressType(addrType);
114 memcpy(AccessAddressPtr(), theAddr.GetRawHostAddress(), addrLength);
115 pkt_length = OFFSET_ADDR + addrLength;
116
117 SetPort(theAddr.GetPort());
118
119 return true;
120 } // end ProtoPktSOCKS::Request::SetAddress()
121
122 bool ProtoPktSOCKS::Request::SetPort(UINT16 thePort)
123 {
124 char* portPtr = AccessAddressPtr() + GetAddressLength();
125 UINT16 temp16 = htons(thePort);
126 memcpy(portPtr, (char*)&temp16, 2);
127 pkt_length = OFFSET_ADDR + GetAddressLength() + 2;
128 return true;
129 } // end ProtoPktSOCKS::Request::SetPort()
130
131
132 bool ProtoPktSOCKS::Request::SetName(const char* theName)
133 {
134 SetAddressType(NAME);
135 unsigned int nameLength = strlen(theName);
136 // TBD - validate there is sufficient buffer space
137 memcpy(AccessAddressPtr(), theName, nameLength);
138 pkt_length += nameLength;
139 return true;
140 } // end ProtoPktSOCKS::Request::SetAddress()
141
142 ProtoPktSOCKS::Reply::Reply(UINT32* bufferPtr, unsigned int numBytes, bool initFromBuffer, bool freeOnDestruct)
143 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
144 {
145 if (initFromBuffer) InitFromBuffer(numBytes);
146 }
147
148 ProtoPktSOCKS::Reply::~Reply()
149 {
150 }
151
152 UINT8 ProtoPktSOCKS::Reply::GetAddressLength() const
153 {
154 switch (GetAddressType())
155 {
156 case IPv4:
157 return 4;
158 case NAME:
159 // First byte of "name" is length value
160 return ((UINT8*)buffer_ptr)[OFFSET_ADDR];
161 case IPv6:
162 return 16;
163 default:
164 return 0;
165 }
166 } // end ProtoPktSOCKS::Reply::GetAddressLength()
167
168 UINT16 ProtoPktSOCKS::Reply::GetPort() const
169 {
170 const char* portPtr = GetAddressPtr() + GetAddressLength();
171 UINT16 temp16;
172 memcpy(&temp16, portPtr, 2);
173 return ntohs(temp16);
174 } // end ProtoPktSOCKS::Reply::GetPort()
175
176 bool ProtoPktSOCKS::Reply::GetAddress(ProtoAddress& theAddr) const
177 {
178 switch (GetAddressType())
179 {
180 case IPv4:
181 {
182 theAddr.SetRawHostAddress(ProtoAddress::IPv4, GetAddressPtr(), 4);
183 break;
184 }
185 case NAME:
186 {
187 char name[256];
188 UINT8 addrLen = GetAddressLength();
189 strncpy(name, GetAddressPtr(), addrLen);
190 name[addrLen] = '\0';
191 if (!theAddr.ResolveFromString(name))
192 {
193 PLOG(PL_ERROR, "ProtoPktSOCKS::Reply::GetAddress() error: unable to resolve name \"%s\" to addr!\n", name);
194 return false;
195 } break;
196 }
197 case IPv6:
198 {
199 theAddr.SetRawHostAddress(ProtoAddress::IPv6, GetAddressPtr(), 16);
200 break;
201 }
202 default:
203 {
204 PLOG(PL_ERROR, "ProtoPktSOCKS::Reply::GetAddress() error: invalid address type!\n");
205 return false;
206 }
207 }
208 theAddr.SetPort(GetPort());
209 return true;
210 } // end ProtoPktSOCKS::Reply::GetAddress()
211
212 void ProtoPktSOCKS::Reply::SetAddress(AddrType addrType, const char* addrPtr, UINT8 addrLen)
213 {
214 switch (addrType)
215 {
216 case ProtoPktSOCKS::IPv4:
217 ((UINT8*)buffer_ptr)[OFFSET_ATYPE] = (UINT8)ProtoPktSOCKS::IPv4;
218 memcpy(((char*)buffer_ptr) + OFFSET_ADDR, addrPtr, 4);
219 pkt_length = OFFSET_ADDR + 4;
220 break;
221 case ProtoPktSOCKS::NAME:
222 ((UINT8*)buffer_ptr)[OFFSET_ATYPE] = (UINT8)ProtoPktSOCKS::NAME;
223 ((UINT8*)buffer_ptr)[OFFSET_ADDR] = (UINT8)addrLen;
224 memcpy(((char*)buffer_ptr) + OFFSET_ADDR + 1, addrPtr, addrLen);
225 pkt_length = OFFSET_ADDR + 1 + addrLen;
226 break;
227 case ProtoPktSOCKS::IPv6:
228 ((UINT8*)buffer_ptr)[OFFSET_ATYPE] = (UINT8)ProtoPktSOCKS::IPv6;
229 memcpy(((char*)buffer_ptr) + OFFSET_ADDR, addrPtr, 16);
230 pkt_length = OFFSET_ADDR + 16;
231 break;
232 default:
233 PLOG(PL_ERROR, "ProtoPktSOCKS::Reply::SetAddress() invalid SOCKS address type\n");
234 break;
235 }
236 } // end ProtoPktSOCKS::Reply::SetAddress()
237
238 void ProtoPktSOCKS::Reply::SetPort(UINT16 thePort)
239 {
240 UINT16 temp16 = htons(thePort);
241 char* portPtr = AccessAddressPtr() + GetAddressLength();
242 memcpy(portPtr, &temp16, 2);
243 pkt_length = (GetAddressPtr() + GetAddressLength() + 2 - (char*)buffer_ptr);
244 } // end ProtoPktSOCKS::Reply::SetPort()
245
246 void ProtoPktSOCKS::Reply::SetAddress(const ProtoAddress& theAddr)
247 {
248 switch (theAddr.GetType())
249 {
250 case ProtoAddress::IPv4:
251 SetAddress(ProtoPktSOCKS::IPv4, theAddr.GetRawHostAddress(), 4);
252 SetPort(theAddr.GetPort());
253 break;
254 case ProtoAddress::IPv6:
255 SetAddress(ProtoPktSOCKS::IPv6, theAddr.GetRawHostAddress(), 16);
256 SetPort(theAddr.GetPort());
257 break;
258 default:
259 PLOG(PL_ERROR, "ProtoPktSOCKS::Reply::SetAddress() invalid ProtoAddress type\n");
260 break;
261 }
262 } // end ProtoPktSOCKS::Reply::SetAddress()
263
264 ProtoPktSOCKS::UdpRequest::UdpRequest(UINT32* bufferPtr, unsigned int numBytes, bool initFromBuffer, bool freeOnDestruct)
265 : ProtoPkt(bufferPtr, numBytes, freeOnDestruct)
266 {
267 if (initFromBuffer) InitFromBuffer(numBytes);
268 }
269
270 ProtoPktSOCKS::UdpRequest::~UdpRequest()
271 {
272 }
273
274 UINT8 ProtoPktSOCKS::UdpRequest::GetAddressLength() const
275 {
276 switch (GetAddressType())
277 {
278 case IPv4:
279 return 4;
280 case NAME:
281 // First byte of "name" is length value
282 return ((UINT8*)buffer_ptr)[OFFSET_ADDR];
283 case IPv6:
284 return 16;
285 default:
286 return 0;
287 }
288 } // end ProtoPktSOCKS::UdpRequest::GetAddressLength()
289
290 UINT16 ProtoPktSOCKS::UdpRequest::GetPort() const
291 {
292 const char* portPtr = GetAddressPtr() + GetAddressLength();
293 UINT16 temp16;
294 memcpy(&temp16, portPtr, 2);
295 return ntohs(temp16);
296 } // end ProtoPktSOCKS::UdpRequest::GetPort()
297
298
299 bool ProtoPktSOCKS::UdpRequest::GetAddress(ProtoAddress& theAddr) const
300 {
301 switch (GetAddressType())
302 {
303 case IPv4:
304 {
305 theAddr.SetRawHostAddress(ProtoAddress::IPv4, GetAddressPtr(), 4);
306 break;
307 }
308 case NAME:
309 {
310 char name[256];
311 UINT8 addrLen = GetAddressLength();
312 strncpy(name, GetAddressPtr(), addrLen);
313 name[addrLen] = '\0';
314 if (!theAddr.ResolveFromString(name))
315 {
316 PLOG(PL_ERROR, "ProtoPktSOCKS::UdpRequest::GetAddress() error: unable to resolve name \"%s\" to addr!\n", name);
317 return false;
318 }
319 break;
320 }
321 case IPv6:
322 {
323 theAddr.SetRawHostAddress(ProtoAddress::IPv6, GetAddressPtr(), 16);
324 break;
325 }
326 default:
327 {
328 PLOG(PL_ERROR, "ProtoPktSOCKS::UdpRequest::GetAddress() error: invalid address type!\n");
329 return false;
330 }
331 }
332 theAddr.SetPort(GetPort());
333 return true;
334 } // end ProtoPktSOCKS::UdpRequest::GetAddress()
335
336
337 void ProtoPktSOCKS::UdpRequest::SetAddress(AddrType addrType, const char* addrPtr, UINT8 addrLen)
338 {
339 switch (addrType)
340 {
341 case ProtoPktSOCKS::IPv4:
342 ((UINT8*)buffer_ptr)[OFFSET_ATYPE] = (UINT8)ProtoPktSOCKS::IPv4;
343 memcpy(((char*)buffer_ptr) + OFFSET_ADDR, addrPtr, 4);
344 pkt_length = OFFSET_ADDR + 4;
345 break;
346 case ProtoPktSOCKS::NAME:
347 ((UINT8*)buffer_ptr)[OFFSET_ATYPE] = (UINT8)ProtoPktSOCKS::NAME;
348 ((UINT8*)buffer_ptr)[OFFSET_ADDR] = (UINT8)addrLen;
349 memcpy(((char*)buffer_ptr) + OFFSET_ADDR + 1, addrPtr, addrLen);
350 pkt_length = OFFSET_ADDR + 1 + addrLen;
351 break;
352 case ProtoPktSOCKS::IPv6:
353 ((UINT8*)buffer_ptr)[OFFSET_ATYPE] = (UINT8)ProtoPktSOCKS::IPv6;
354 memcpy(((char*)buffer_ptr) + OFFSET_ADDR, addrPtr, 16);
355 pkt_length = OFFSET_ADDR + 16;
356 break;
357 default:
358 PLOG(PL_ERROR, "ProtoPktSOCKS::UdpRequest::SetAddress() invalid SOCKS address type\n");
359 break;
360 }
361 } // end ProtoPktSOCKS::UdpRequest::SetAddress()
362
363 void ProtoPktSOCKS::UdpRequest::SetPort(UINT16 thePort)
364 {
365 UINT16 temp16 = htons(thePort);
366 char* portPtr = AccessAddressPtr() + GetAddressLength();
367 memcpy(portPtr, &temp16, 2);
368 pkt_length = (GetAddressPtr() + GetAddressLength() + 2 - (char*)buffer_ptr);
369 } // end ProtoPktSOCKS::UdpRequest::SetPort()
370
371 void ProtoPktSOCKS::UdpRequest::SetAddress(const ProtoAddress& theAddr)
372 {
373 switch (theAddr.GetType())
374 {
375 case ProtoAddress::IPv4:
376 SetAddress(ProtoPktSOCKS::IPv4, theAddr.GetRawHostAddress(), 4);
377 SetPort(theAddr.GetPort());
378 break;
379 case ProtoAddress::IPv6:
380 SetAddress(ProtoPktSOCKS::IPv6, theAddr.GetRawHostAddress(), 16);
381 SetPort(theAddr.GetPort());
382 break;
383 default:
384 PLOG(PL_ERROR, "ProtoPktSOCKS::UdpRequest::SetAddress() invalid ProtoAddress type\n");
385 break;
386 }
387 } // end ProtoPktSOCKS::UdpRequest::SetAddress()
388