VirtualBox

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

Last change on this file since 53343 was 52938, checked in by vboxsync, 10 years ago

VBoxNetPortForwardString.cpp: fixed braces to properly differ between 'tcp' and 'udp' when parsing NAT port forwarding rules

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/* $Id: VBoxNetPortForwardString.cpp 52938 2014-10-02 16:43:35Z vboxsync $ */
2/** @file
3 * VBoxNetPortForwardString - Routines for managing port-forward strings.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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* Header Files *
20*******************************************************************************/
21#ifndef RT_OS_WINDOWS
22#include <netinet/in.h>
23#else
24# include <Winsock2.h>
25# include <Ws2ipdef.h>
26#endif
27
28#include <iprt/cdefs.h>
29#include <iprt/cidr.h>
30#include <iprt/param.h>
31#include <iprt/path.h>
32#include <iprt/stream.h>
33#include <iprt/string.h>
34#include <iprt/net.h>
35#include <iprt/getopt.h>
36#include <iprt/ctype.h>
37
38
39#include <VBox/log.h>
40
41#include "VBoxPortForwardString.h"
42
43
44#define PF_FIELD_SEPARATOR ':'
45#define PF_ADDRESS_FIELD_STARTS '['
46#define PF_ADDRESS_FIELD_ENDS ']'
47
48#define PF_STR_FIELD_SEPARATOR ":"
49#define PF_STR_ADDRESS_FIELD_STARTS "["
50#define PF_STR_ADDRESS_FIELD_ENDS "]"
51
52static int netPfStrAddressParse(char *pszRaw, int cbRaw,
53 char *pszAddress, int cbAddress,
54 bool fEmptyAcceptable)
55{
56 int idxRaw = 0;
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 memset(pPfr, 0, sizeof(PORTFORWARDRULE));
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
249 AssertPtrReturn(pszRaw, VERR_NO_MEMORY);
250
251 pszRawBegin = pszRaw;
252
253 /* name */
254 if (pszRaw[idxRaw] == PF_FIELD_SEPARATOR)
255 idxRaw = 1; /* begin of the next segment */
256 else
257 {
258 char *pszEndOfName = RTStrStr(pszRaw + 1, PF_STR_FIELD_SEPARATOR);
259 if (!pszEndOfName)
260 goto invalid_parameter;
261
262 cbToken = (pszEndOfName) - pszRaw; /* don't take : into account */
263 /* XXX it's unacceptable to have only name entry in PF */
264 AssertReturn(cbToken < cbRaw, VERR_INVALID_PARAMETER);
265
266 if ( cbToken < 0
267 || (size_t)cbToken >= PF_NAMELEN)
268 goto invalid_parameter;
269
270 RTStrCopy(pszName,
271 RT_MIN((size_t)cbToken + 1, PF_NAMELEN),
272 pszRaw);
273 pszRaw += cbToken; /* move to separator */
274 }
275
276 AssertReturn(pszRaw[0] == PF_FIELD_SEPARATOR, VERR_INVALID_PARAMETER);
277 /* protocol */
278
279 pszRaw++; /* skip separator */
280 idxRaw = 0;
281
282 cbRaw--;
283
284 if ( ( (fTcpProto = (RTStrNICmp(pszRaw, "tcp", 3) == 0))
285 || RTStrNICmp(pszRaw, "udp", 3) == 0)
286 && pszRaw[3] == PF_FIELD_SEPARATOR)
287 {
288 proto = (fTcpProto ? IPPROTO_TCP : IPPROTO_UDP);
289 idxRaw = 3;
290 }
291 else
292 goto invalid_parameter;
293
294 pszRaw += idxRaw;
295 cbRaw -= idxRaw;
296 idxRaw = 0;
297
298 idxRaw = netPfStrAddressPortPairParse(pszRaw, cbRaw,
299 pszHostAddr, INET6_ADDRSTRLEN,
300 true, &u16HostPort);
301 if (idxRaw < 0)
302 return VERR_INVALID_PARAMETER;
303
304 pszRaw += idxRaw;
305 cbRaw -= idxRaw;
306
307 Assert(pszRaw[0] == PF_FIELD_SEPARATOR);
308
309 idxRaw = 0;
310
311 idxRaw = netPfStrAddressPortPairParse(pszRaw, cbRaw,
312 pszGuestAddr,
313 INET6_ADDRSTRLEN,
314 false,
315 &u16GuestPort);
316
317 if (idxRaw < 0)
318 goto invalid_parameter;
319
320 /* XXX: fill the rule */
321 pPfr->fPfrIPv6 = fIPv6;
322 pPfr->iPfrProto = proto;
323
324 pPfr->u16PfrHostPort = u16HostPort;
325
326 if (*pszGuestAddr == '\0')
327 goto invalid_parameter; /* guest address should be defined */
328
329 pPfr->u16PfrGuestPort = u16GuestPort;
330
331 Log(("name: %s\n"
332 "proto: %d\n"
333 "host address: %s\n"
334 "host port: %d\n"
335 "guest address: %s\n"
336 "guest port:%d\n",
337 pszName, proto,
338 pszHostAddr, u16HostPort,
339 pszGuestAddr, u16GuestPort));
340
341 RTStrFree(pszRawBegin);
342 return VINF_SUCCESS;
343
344invalid_parameter:
345 RTStrFree(pszRawBegin);
346 if (pPfr)
347 memset(pPfr, 0, sizeof(PORTFORWARDRULE));
348 return VERR_INVALID_PARAMETER;
349}
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