VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NetLib/VBoxNetPortForwardString.cpp@ 74886

Last change on this file since 74886 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1/* $Id: VBoxNetPortForwardString.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
2/** @file
3 * VBoxNetPortForwardString - Routines for managing port-forward strings.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#ifndef RT_OS_WINDOWS
23# include <netinet/in.h>
24#else
25# include <iprt/win/winsock2.h>
26# include <Ws2ipdef.h>
27#endif
28
29#include <iprt/cdefs.h>
30#include <iprt/cidr.h>
31#include <iprt/param.h>
32#include <iprt/path.h>
33#include <iprt/stream.h>
34#include <iprt/string.h>
35#include <iprt/net.h>
36#include <iprt/getopt.h>
37#include <iprt/ctype.h>
38
39
40#include <VBox/log.h>
41
42#include "VBoxPortForwardString.h"
43
44
45#define PF_FIELD_SEPARATOR ':'
46#define PF_ADDRESS_FIELD_STARTS '['
47#define PF_ADDRESS_FIELD_ENDS ']'
48
49#define PF_STR_FIELD_SEPARATOR ":"
50#define PF_STR_ADDRESS_FIELD_STARTS "["
51#define PF_STR_ADDRESS_FIELD_ENDS "]"
52
53
54static int netPfStrAddressParse(char *pszRaw, size_t cchRaw,
55 char *pszAddress, int cbAddress,
56 bool fEmptyAcceptable)
57{
58 size_t cchField = 0;
59
60 AssertPtrReturn(pszRaw, -1);
61 AssertPtrReturn(pszAddress, -1);
62 AssertReturn(pszRaw[0] == PF_ADDRESS_FIELD_STARTS, -1);
63
64 if (pszRaw[0] == PF_ADDRESS_FIELD_STARTS)
65 {
66 /* shift pszRaw to next symbol */
67 pszRaw++;
68 cchRaw--;
69
70
71 /* we shouldn't face with ending here */
72 AssertReturn(cchRaw > 0, VERR_INVALID_PARAMETER);
73
74 char *pszEndOfAddress = RTStrStr(pszRaw, PF_STR_ADDRESS_FIELD_ENDS);
75
76 /* no pair closing sign */
77 AssertPtrReturn(pszEndOfAddress, VERR_INVALID_PARAMETER);
78
79 cchField = pszEndOfAddress - pszRaw;
80
81 /* field should be less then the rest of the string */
82 AssertReturn(cchField < cchRaw, VERR_INVALID_PARAMETER);
83
84 if (cchField != 0)
85 RTStrCopy(pszAddress, RT_MIN(cchField + 1, (size_t)cbAddress), pszRaw);
86 else if (!fEmptyAcceptable)
87 return -1;
88 }
89
90 AssertReturn(pszRaw[cchField] == PF_ADDRESS_FIELD_ENDS, -1);
91
92 return (int)cchField + 2; /* length of the field and closing braces */
93}
94
95
96/**
97 * Parses a port something.
98 *
99 * @returns Offset relative to @a pszRaw of the end of the port field.
100 * -1 on failure.
101 * @param pszRaw The zero terminated string to parse. Points a field
102 * separator.
103 * @param pu16Port Where to store the port number on success.
104 */
105static int netPfStrPortParse(char *pszRaw, uint16_t *pu16Port)
106{
107#if 1
108 AssertPtrReturn(pszRaw, -1);
109 AssertPtrReturn(pu16Port, -1);
110 AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, -1);
111
112 char *pszNext = NULL;
113 int rc = RTStrToUInt16Ex(&pszRaw[1], &pszNext, 0, pu16Port);
114 if (rc == VWRN_TRAILING_CHARS)
115 AssertReturn(*pszNext == PF_FIELD_SEPARATOR, -1);
116 else if (rc == VINF_SUCCESS)
117 Assert(*pszNext == '\0');
118 else
119 AssertMsgFailedReturn(("rc=%Rrc\n", rc), -1);
120 if (*pu16Port == 0)
121 return -1;
122 return (int)(pszNext - pszRaw);
123
124#else /* The same code, just a little more verbose: */
125 char *pszEndOfPort = NULL;
126 uint16_t u16Port = 0;
127 int idxRaw = 1; /* we increment pszRaw after checks. */
128 int cbRest = 0;
129 size_t cbPort = 0;
130
131 AssertPtrReturn(pszRaw, -1);
132 AssertPtrReturn(pu16Port, -1);
133 AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, -1);
134
135 pszRaw++; /* skip field separator */
136 cchRaw --;
137
138 char *pszEndOfPort = RTStrStr(pszRaw, ":");
139 if (!pszEndOfPort)
140 {
141 cbRest = strlen(pszRaw);
142
143 Assert(cchRaw == cbRest);
144
145 /* XXX: Assumption that if string is too big, it will be reported by
146 * RTStrToUint16.
147 */
148 if (cbRest > 0)
149 {
150 pszEndOfPort = pszRaw + cbRest;
151 cbPort = cbRest;
152 }
153 else
154 return -1;
155 }
156 else
157 cbPort = pszEndOfPort - pszRaw;
158
159
160 idxRaw += cbPort;
161
162 Assert(cbRest || pszRaw[idxRaw - 1] == PF_FIELD_SEPARATOR); /* we are 1 char ahead */
163
164 char szPort[10];
165 RT_ZERO(szPort);
166
167 Assert(idxRaw > 0);
168 RTStrCopy(szPort, RT_MIN(sizeof(szPort), (size_t)(cbPort) + 1), pszRaw);
169
170 if (!(u16Port = RTStrToUInt16(szPort)))
171 return -1;
172
173 *pu16Port = u16Port;
174
175 return idxRaw;
176#endif
177}
178
179
180static int netPfStrAddressPortPairParse(char *pszRaw, size_t cchRaw,
181 char *pszAddress, int cbAddress,
182 bool fEmptyAddressAcceptable,
183 uint16_t *pu16Port)
184{
185 int idxRaw = 0;
186 int idxRawTotal = 0;
187
188 AssertPtrReturn(pszRaw, -1);
189 AssertPtrReturn(pszAddress, -1);
190 AssertPtrReturn(pu16Port, -2);
191
192 /* XXX: Here we should check 0 - ':' and 1 - '[' */
193 Assert( pszRaw[0] == PF_FIELD_SEPARATOR
194 && pszRaw[1] == PF_ADDRESS_FIELD_STARTS);
195
196 pszRaw++; /* field separator skip */
197 cchRaw--;
198 AssertReturn(cchRaw > 0, VERR_INVALID_PARAMETER);
199
200 idxRaw = 0;
201
202 if (pszRaw[0] == PF_ADDRESS_FIELD_STARTS)
203 {
204 idxRaw += netPfStrAddressParse(pszRaw,
205 cchRaw - idxRaw,
206 pszAddress,
207 cbAddress,
208 fEmptyAddressAcceptable);
209 if (idxRaw == -1)
210 return -1;
211
212 Assert(pszRaw[idxRaw] == PF_FIELD_SEPARATOR);
213 }
214 else return -1;
215
216 pszRaw += idxRaw;
217 idxRawTotal += idxRaw;
218 cchRaw -= idxRaw;
219
220 AssertReturn(cchRaw > 0, VERR_INVALID_PARAMETER);
221
222 idxRaw = 0;
223
224 Assert(pszRaw[0] == PF_FIELD_SEPARATOR);
225
226 if (pszRaw[0] == PF_FIELD_SEPARATOR)
227 {
228 idxRaw = netPfStrPortParse(pszRaw, pu16Port);
229
230 Assert(strlen(&pszRaw[idxRaw]) == 0 || pszRaw[idxRaw] == PF_FIELD_SEPARATOR);
231
232 if (idxRaw == -1)
233 return -2;
234
235 idxRawTotal += idxRaw;
236
237 return idxRawTotal + 1;
238 }
239 else return -1; /* trailing garbage in the address */
240}
241
242/* XXX: Having fIPv6 we might emprove adress verification comparing address length
243 * with INET[6]_ADDRLEN
244 *
245 */
246int netPfStrToPf(const char *pcszStrPortForward, bool fIPv6, PPORTFORWARDRULE pPfr)
247{
248/** r=bird: Redo from scratch? This is very hard to read. And it's going about
249 * things in a very complicated, potentially leaky (pszRaw) fashion. */
250
251 int proto;
252 uint16_t u16HostPort;
253 uint16_t u16GuestPort;
254 bool fTcpProto = false;
255
256 int idxRaw = 0;
257 int cbToken = 0;
258
259 AssertPtrReturn(pcszStrPortForward, VERR_INVALID_PARAMETER);
260 AssertPtrReturn(pPfr, VERR_INVALID_PARAMETER);
261
262 RT_ZERO(*pPfr);
263
264 char *pszHostAddr = &pPfr->szPfrHostAddr[0];
265 char *pszGuestAddr = &pPfr->szPfrGuestAddr[0];
266 char *pszName = &pPfr->szPfrName[0];
267
268 size_t cchRaw = strlen(pcszStrPortForward);
269
270 /* Minimal rule ":tcp:[]:0:[]:0" has got lenght 14 */
271 AssertReturn(cchRaw > 14, VERR_INVALID_PARAMETER);
272
273 char *pszRaw = RTStrDup(pcszStrPortForward);
274 AssertReturn(pszRaw, VERR_NO_MEMORY);
275
276 char *pszRawBegin = pszRaw;
277
278 /* name */
279 if (pszRaw[idxRaw] == PF_FIELD_SEPARATOR)
280 idxRaw = 1; /* begin of the next segment */
281 else
282 {
283 char *pszEndOfName = RTStrStr(pszRaw + 1, PF_STR_FIELD_SEPARATOR);
284 if (!pszEndOfName)
285 goto invalid_parameter;
286
287 cbToken = pszEndOfName - pszRaw; /* don't take : into account */
288 /* XXX it's unacceptable to have only name entry in PF */
289 AssertReturn(cbToken < (ssize_t)cchRaw, VERR_INVALID_PARAMETER);
290
291 if ( cbToken < 0
292 || (size_t)cbToken >= PF_NAMELEN)
293 goto invalid_parameter;
294
295 RTStrCopy(pszName,
296 RT_MIN((size_t)cbToken + 1, PF_NAMELEN),
297 pszRaw);
298 pszRaw += cbToken; /* move to separator */
299 cchRaw -= cbToken;
300 }
301
302 AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, VERR_INVALID_PARAMETER);
303 /* protocol */
304
305 pszRaw++; /* skip separator */
306 cchRaw--;
307 idxRaw = 0;
308
309 if ( ( (fTcpProto = (RTStrNICmp(pszRaw, "tcp", 3) == 0))
310 || RTStrNICmp(pszRaw, "udp", 3) == 0)
311 && pszRaw[3] == PF_FIELD_SEPARATOR)
312 {
313 proto = (fTcpProto ? IPPROTO_TCP : IPPROTO_UDP);
314 idxRaw = 3;
315 }
316 else
317 goto invalid_parameter;
318
319 pszRaw += idxRaw;
320 cchRaw -= idxRaw;
321
322 idxRaw = netPfStrAddressPortPairParse(pszRaw, cchRaw,
323 pszHostAddr, INET6_ADDRSTRLEN,
324 true, &u16HostPort);
325 if (idxRaw < 0)
326 return VERR_INVALID_PARAMETER;
327
328 pszRaw += idxRaw;
329 cchRaw -= idxRaw;
330
331 Assert(pszRaw[0] == PF_FIELD_SEPARATOR);
332
333 idxRaw = netPfStrAddressPortPairParse(pszRaw, cchRaw,
334 pszGuestAddr, INET6_ADDRSTRLEN,
335 false, &u16GuestPort);
336
337 if (idxRaw < 0)
338 goto invalid_parameter;
339
340 /* XXX: fill the rule */
341 pPfr->fPfrIPv6 = fIPv6;
342 pPfr->iPfrProto = proto;
343
344 pPfr->u16PfrHostPort = u16HostPort;
345
346 if (*pszGuestAddr == '\0')
347 goto invalid_parameter; /* guest address should be defined */
348
349 pPfr->u16PfrGuestPort = u16GuestPort;
350
351 Log(("name: %s\n"
352 "proto: %d\n"
353 "host address: %s\n"
354 "host port: %d\n"
355 "guest address: %s\n"
356 "guest port:%d\n",
357 pszName, proto,
358 pszHostAddr, u16HostPort,
359 pszGuestAddr, u16GuestPort));
360
361 RTStrFree(pszRawBegin);
362 return VINF_SUCCESS;
363
364invalid_parameter:
365 RTStrFree(pszRawBegin);
366 if (pPfr)
367 RT_ZERO(*pPfr);
368 return VERR_INVALID_PARAMETER;
369}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette