VirtualBox

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

Last change on this file since 63267 was 63267, checked in by vboxsync, 8 years ago

NetworkServices: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: VBoxNetPortForwardString.cpp 63267 2016-08-10 13:34:15Z vboxsync $ */
2/** @file
3 * VBoxNetPortForwardString - Routines for managing port-forward strings.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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
53static int netPfStrAddressParse(char *pszRaw, int cbRaw,
54 char *pszAddress, int cbAddress,
55 bool fEmptyAcceptable)
56{
57 int cbField = 0;
58
59 AssertPtrReturn(pszRaw, -1);
60 AssertPtrReturn(pszAddress, -1);
61 AssertReturn(pszRaw[0] == PF_ADDRESS_FIELD_STARTS, -1);
62
63 if (pszRaw[0] == PF_ADDRESS_FIELD_STARTS)
64 {
65 /* shift pszRaw to next symbol */
66 pszRaw++;
67 cbRaw--;
68
69
70 /* we shouldn't face with ending here */
71 AssertReturn(cbRaw > 0, VERR_INVALID_PARAMETER);
72
73 char *pszEndOfAddress = RTStrStr(pszRaw, PF_STR_ADDRESS_FIELD_ENDS);
74
75 /* no pair closing sign */
76 AssertPtrReturn(pszEndOfAddress, VERR_INVALID_PARAMETER);
77
78 cbField = pszEndOfAddress - pszRaw;
79
80 /* field should be less then the rest of the string */
81 AssertReturn(cbField < cbRaw, VERR_INVALID_PARAMETER);
82
83 if (cbField != 0)
84 RTStrCopy(pszAddress, RT_MIN(cbField + 1, cbAddress), pszRaw);
85 else if (!fEmptyAcceptable)
86 return -1;
87 }
88
89 AssertReturn(pszRaw[cbField] == PF_ADDRESS_FIELD_ENDS, -1);
90
91 return cbField + 2; /* length of the field and closing braces */
92}
93
94
95static int netPfStrPortParse(char *pszRaw, int cbRaw, uint16_t *pu16Port)
96{
97 char *pszEndOfPort = NULL;
98 uint16_t u16Port = 0;
99 int idxRaw = 1; /* we increment pszRaw after checks. */
100 int cbRest = 0;
101 size_t cbPort = 0;
102
103 AssertPtrReturn(pszRaw, -1);
104 AssertPtrReturn(pu16Port, -1);
105 AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, -1);
106
107 pszRaw++; /* skip line separator */
108 cbRaw --;
109
110 pszEndOfPort = RTStrStr(pszRaw, ":");
111 if (!pszEndOfPort)
112 {
113 cbRest = strlen(pszRaw);
114
115 Assert(cbRaw == cbRest);
116
117 /* XXX: Assumption that if string is too big, it will be reported by
118 * RTStrToUint16.
119 */
120 if (cbRest > 0)
121 {
122 pszEndOfPort = pszRaw + cbRest;
123 cbPort = cbRest;
124 }
125 else
126 return -1;
127 }
128 else
129 cbPort = pszEndOfPort - pszRaw;
130
131
132 idxRaw += cbPort;
133
134 Assert(cbRest || pszRaw[idxRaw - 1] == PF_FIELD_SEPARATOR); /* we are 1 char ahead */
135
136 char szPort[10];
137 RT_ZERO(szPort);
138
139 Assert(idxRaw > 0);
140 RTStrCopy(szPort, RT_MIN(sizeof(szPort), (size_t)(cbPort) + 1), pszRaw);
141
142 if (!(u16Port = RTStrToUInt16(szPort)))
143 return -1;
144
145 *pu16Port = u16Port;
146
147 return idxRaw;
148}
149
150
151static int netPfStrAddressPortPairParse(char *pszRaw, int cbRaw,
152 char *pszAddress, int cbAddress,
153 bool fEmptyAddressAcceptable,
154 uint16_t *pu16Port)
155{
156 int idxRaw = 0;
157 int idxRawTotal = 0;
158
159 AssertPtrReturn(pszRaw, -1);
160 AssertPtrReturn(pszAddress, -1);
161 AssertPtrReturn(pu16Port, -2);
162
163 /* XXX: Here we should check 0 - ':' and 1 - '[' */
164 Assert( pszRaw[0] == PF_FIELD_SEPARATOR
165 && pszRaw[1] == PF_ADDRESS_FIELD_STARTS);
166
167 pszRaw++; /* field separator skip */
168 cbRaw--;
169 AssertReturn(cbRaw > 0, VERR_INVALID_PARAMETER);
170
171 idxRaw = 0;
172
173 if (pszRaw[0] == PF_ADDRESS_FIELD_STARTS)
174 {
175 idxRaw += netPfStrAddressParse(pszRaw,
176 cbRaw - idxRaw,
177 pszAddress,
178 cbAddress,
179 fEmptyAddressAcceptable);
180 if (idxRaw == -1)
181 return -1;
182
183 Assert(pszRaw[idxRaw] == PF_FIELD_SEPARATOR);
184 }
185 else return -1;
186
187 pszRaw += idxRaw;
188 idxRawTotal += idxRaw;
189 cbRaw -= idxRaw;
190
191 AssertReturn(cbRaw > 0, VERR_INVALID_PARAMETER);
192
193 idxRaw = 0;
194
195 Assert(pszRaw[0] == PF_FIELD_SEPARATOR);
196
197 if (pszRaw[0] == PF_FIELD_SEPARATOR)
198 {
199 idxRaw = netPfStrPortParse(pszRaw, strlen(pszRaw), pu16Port);
200
201 Assert(strlen(&pszRaw[idxRaw]) == 0 || pszRaw[idxRaw] == PF_FIELD_SEPARATOR);
202
203 if (idxRaw == -1)
204 return -2;
205
206 idxRawTotal += idxRaw;
207
208 return idxRawTotal + 1;
209 }
210 else return -1; /* trailing garbage in the address */
211}
212
213/* XXX: Having fIPv6 we might emprove adress verification comparing address length
214 * with INET[6]_ADDRLEN
215 */
216int netPfStrToPf(const char *pcszStrPortForward, int fIPv6, PPORTFORWARDRULE pPfr)
217{
218 char *pszName;
219 int proto;
220 char *pszHostAddr;
221 char *pszGuestAddr;
222 uint16_t u16HostPort;
223 uint16_t u16GuestPort;
224 bool fTcpProto = false;
225
226 char *pszRawBegin = NULL;
227 char *pszRaw = NULL;
228 int idxRaw = 0;
229 int cbToken = 0;
230 int cbRaw = 0;
231 int rc = VINF_SUCCESS;
232
233 AssertPtrReturn(pcszStrPortForward, VERR_INVALID_PARAMETER);
234 AssertPtrReturn(pPfr, VERR_INVALID_PARAMETER);
235
236 RT_ZERO(*pPfr);
237
238 pszHostAddr = &pPfr->szPfrHostAddr[0];
239 pszGuestAddr = &pPfr->szPfrGuestAddr[0];
240 pszName = &pPfr->szPfrName[0];
241
242 cbRaw = strlen(pcszStrPortForward);
243
244 /* Minimal rule ":tcp:[]:0:[]:0" has got lenght 14 */
245 AssertReturn(cbRaw > 14, VERR_INVALID_PARAMETER);
246
247 pszRaw = RTStrDup(pcszStrPortForward);
248 AssertReturn(pszRaw, VERR_NO_MEMORY);
249
250 pszRawBegin = pszRaw;
251
252 /* name */
253 if (pszRaw[idxRaw] == PF_FIELD_SEPARATOR)
254 idxRaw = 1; /* begin of the next segment */
255 else
256 {
257 char *pszEndOfName = RTStrStr(pszRaw + 1, PF_STR_FIELD_SEPARATOR);
258 if (!pszEndOfName)
259 goto invalid_parameter;
260
261 cbToken = (pszEndOfName) - pszRaw; /* don't take : into account */
262 /* XXX it's unacceptable to have only name entry in PF */
263 AssertReturn(cbToken < cbRaw, VERR_INVALID_PARAMETER);
264
265 if ( cbToken < 0
266 || (size_t)cbToken >= PF_NAMELEN)
267 goto invalid_parameter;
268
269 RTStrCopy(pszName,
270 RT_MIN((size_t)cbToken + 1, PF_NAMELEN),
271 pszRaw);
272 pszRaw += cbToken; /* move to separator */
273 cbRaw -= cbToken;
274 }
275
276 AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, VERR_INVALID_PARAMETER);
277 /* protocol */
278
279 pszRaw++; /* skip separator */
280 cbRaw--;
281 idxRaw = 0;
282
283 if ( ( (fTcpProto = (RTStrNICmp(pszRaw, "tcp", 3) == 0))
284 || RTStrNICmp(pszRaw, "udp", 3) == 0)
285 && pszRaw[3] == PF_FIELD_SEPARATOR)
286 {
287 proto = (fTcpProto ? IPPROTO_TCP : IPPROTO_UDP);
288 idxRaw = 3;
289 }
290 else
291 goto invalid_parameter;
292
293 pszRaw += idxRaw;
294 cbRaw -= idxRaw;
295
296 idxRaw = netPfStrAddressPortPairParse(pszRaw, cbRaw,
297 pszHostAddr, INET6_ADDRSTRLEN,
298 true, &u16HostPort);
299 if (idxRaw < 0)
300 return VERR_INVALID_PARAMETER;
301
302 pszRaw += idxRaw;
303 cbRaw -= idxRaw;
304
305 Assert(pszRaw[0] == PF_FIELD_SEPARATOR);
306
307 idxRaw = netPfStrAddressPortPairParse(pszRaw, cbRaw,
308 pszGuestAddr, INET6_ADDRSTRLEN,
309 false, &u16GuestPort);
310
311 if (idxRaw < 0)
312 goto invalid_parameter;
313
314 /* XXX: fill the rule */
315 pPfr->fPfrIPv6 = fIPv6;
316 pPfr->iPfrProto = proto;
317
318 pPfr->u16PfrHostPort = u16HostPort;
319
320 if (*pszGuestAddr == '\0')
321 goto invalid_parameter; /* guest address should be defined */
322
323 pPfr->u16PfrGuestPort = u16GuestPort;
324
325 Log(("name: %s\n"
326 "proto: %d\n"
327 "host address: %s\n"
328 "host port: %d\n"
329 "guest address: %s\n"
330 "guest port:%d\n",
331 pszName, proto,
332 pszHostAddr, u16HostPort,
333 pszGuestAddr, u16GuestPort));
334
335 RTStrFree(pszRawBegin);
336 return VINF_SUCCESS;
337
338invalid_parameter:
339 RTStrFree(pszRawBegin);
340 if (pPfr)
341 RT_ZERO(*pPfr);
342 return VERR_INVALID_PARAMETER;
343}
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