VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

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