Changeset 39557 in vbox for trunk/src/VBox/NetworkServices/NAT/VBoxNetNAT.cpp
- Timestamp:
- Dec 8, 2011 5:55:10 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/NetworkServices/NAT/VBoxNetNAT.cpp
r39498 r39557 72 72 * Structures and Typedefs * 73 73 *******************************************************************************/ 74 static RTGETOPTDEF g_aGetOptDef[] = 75 { 76 { "--pf", 'p', RTGETOPT_REQ_STRING } 77 }; 78 79 typedef struct NATSEVICEPORTFORWARDRULE 80 { 81 char *pszPortForwardRuleName; 82 struct in_addr IpV4HostAddr; 83 uint16_t u16HostPort; 84 struct in_addr IpV4GuestAddr; 85 uint16_t u16GuestPort; 86 bool fUdp; 87 char *pszStrRaw; 88 } NATSEVICEPORTFORWARDRULE, *PNATSEVICEPORTFORWARDRULE; 74 89 75 90 class VBoxNetNAT : public VBoxNetBaseService … … 80 95 void usage(void); 81 96 void run(void); 82 void init(void); 97 virtual int init(void); 98 virtual int parseOpt(int rc, const RTGETOPTUNION& getOptVal); 83 99 84 100 public: … … 107 123 volatile uint32_t cPkt; 108 124 bool fIsRunning; 125 std::vector<PNATSEVICEPORTFORWARDRULE> m_vecPortForwardRuleFromCmdLine; 109 126 }; 110 127 … … 155 172 cPkt = 0; 156 173 cUrgPkt = 0; 174 VBoxNetBaseService::init(); 175 for(unsigned int i = 0; i < RT_ELEMENTS(g_aGetOptDef); ++i) 176 m_vecOptionDefs.push_back(&g_aGetOptDef[i]); 157 177 } 158 178 159 179 VBoxNetNAT::~VBoxNetNAT() { } 160 voidVBoxNetNAT::init()180 int VBoxNetNAT::init() 161 181 { 162 182 int rc; … … 174 194 AssertReleaseRC(rc); 175 195 196 /* Why ? */ 176 197 slirp_set_ethaddr_and_activate_port_forwarding(m_pNATState, &m_MacAddress.au8[0], INADDR_ANY); 198 #if 0 199 in_addr ipv4HostAddr; 200 in_addr ipv4GuestAddr; 201 ipv4GuestAddr.s_addr = RT_H2N_U32_C(RT_BSWAP_U32_C(RT_MAKE_U32_FROM_U8( 10, 0, 2, 15))); 202 ipv4HostAddr.s_addr = INADDR_ANY; 203 slirp_add_redirect(m_pNATState, false, ipv4HostAddr, 2022, ipv4GuestAddr , 22, NULL); 204 #endif 205 std::vector<PNATSEVICEPORTFORWARDRULE>::iterator it; 206 for (it = m_vecPortForwardRuleFromCmdLine.begin(); it != m_vecPortForwardRuleFromCmdLine.end(); ++it) 207 { 208 slirp_add_redirect(m_pNATState, (*it)->fUdp, (*it)->IpV4HostAddr, (*it)->u16HostPort, (*it)->IpV4GuestAddr , (*it)->u16GuestPort, NULL); 209 RTStrFree((*it)->pszStrRaw); 210 RTMemFree((*it)); 211 } 212 m_vecPortForwardRuleFromCmdLine.clear(); 177 213 #ifndef RT_OS_WINDOWS 178 214 /* … … 202 238 rc = RTSemEventCreate(&m_EventUrgSend); 203 239 AssertReleaseRC(rc); 240 return VINF_SUCCESS; 204 241 } 205 242 … … 304 341 RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT, 305 342 (PFNRT)SendWorker, 2, m, cbSegFrame); 343 natNotifyNATThread(); 306 344 AssertReleaseRC(rc); 307 345 } … … 326 364 } 327 365 366 int VBoxNetNAT::parseOpt(int rc, const RTGETOPTUNION& Val) 367 { 368 switch (rc) 369 { 370 case 'p': 371 { 372 #define ITERATE_TO_NEXT_TERM(ch, pRule, strRaw) \ 373 do { \ 374 while (*ch != ',') \ 375 { \ 376 if (*ch == 0) \ 377 { \ 378 if (pRule) \ 379 RTMemFree(pRule); \ 380 if(strRaw) \ 381 RTStrFree(strRaw); \ 382 return VERR_INVALID_PARAMETER; \ 383 } \ 384 ch++; \ 385 } \ 386 *ch = '\0'; \ 387 ch++; \ 388 } while(0) 389 PNATSEVICEPORTFORWARDRULE pRule = (PNATSEVICEPORTFORWARDRULE)RTMemAlloc(sizeof(NATSEVICEPORTFORWARDRULE)); 390 if (!pRule) 391 return VERR_NO_MEMORY; 392 char *strName; 393 char *strProto; 394 char *strHostIp; 395 char *strHostPort; 396 char *strGuestIp; 397 char *strGuestPort; 398 char *strRaw = RTStrDup(Val.psz); 399 char *ch = strRaw; 400 if (!strRaw) 401 { 402 RTMemFree(pRule); 403 return VERR_NO_MEMORY; 404 } 405 406 strName = RTStrStrip(ch); 407 ITERATE_TO_NEXT_TERM(ch, pRule, strRaw); 408 strProto = RTStrStrip(ch); 409 ITERATE_TO_NEXT_TERM(ch, pRule, strRaw); 410 strHostIp = RTStrStrip(ch); 411 ITERATE_TO_NEXT_TERM(ch, pRule, strRaw); 412 strHostPort = RTStrStrip(ch); 413 ITERATE_TO_NEXT_TERM(ch, pRule, strRaw); 414 strGuestIp = RTStrStrip(ch); 415 ITERATE_TO_NEXT_TERM(ch, pRule, strRaw); 416 strGuestPort = RTStrStrip(ch); 417 if (RTStrICmp(strProto, "udp") == 0) 418 pRule->fUdp = true; 419 else if (RTStrICmp(strProto, "tcp") == 0) 420 pRule->fUdp = false; 421 else 422 { 423 RTStrFree(strRaw); 424 RTMemFree(pRule); 425 return VERR_INVALID_PARAMETER; 426 } 427 if ( strHostIp == NULL 428 || inet_aton(strHostIp, &pRule->IpV4HostAddr) == 0) 429 pRule->IpV4HostAddr.s_addr = INADDR_ANY; 430 if ( strGuestIp == NULL 431 || inet_aton(strGuestIp, &pRule->IpV4GuestAddr) == 0) 432 { 433 RTMemFree(pRule); 434 RTMemFree(strRaw); 435 return VERR_INVALID_PARAMETER; 436 } 437 pRule->u16HostPort = RTStrToUInt16(strHostPort); 438 pRule->u16GuestPort = RTStrToUInt16(strGuestPort); 439 if ( !pRule->u16HostPort 440 || !pRule->u16GuestPort) 441 { 442 RTMemFree(pRule); 443 RTMemFree(strRaw); 444 return VERR_INVALID_PARAMETER; 445 } 446 pRule->pszStrRaw = strRaw; 447 m_vecPortForwardRuleFromCmdLine.push_back(pRule); 448 return VINF_SUCCESS; 449 #undef ITERATE_TO_NEXT_TERM 450 } 451 default:; 452 } 453 return VERR_NOT_FOUND; 454 } 455 328 456 /** 329 457 * Entry point. … … 333 461 Log2(("NAT: main\n")); 334 462 g_pNAT = new VBoxNetNAT(); 335 Log2(("NAT: parsing command line\n"));463 Log2(("NAT: initialization\n")); 336 464 int rc = g_pNAT->parseArgs(argc - 1, argv + 1); 337 465 if (!rc) 338 466 { 339 Log2(("NAT: initialization\n"));340 467 g_pNAT->init(); 468 Log2(("NAT: parsing command line\n")); 341 469 Log2(("NAT: try go online\n")); 342 470 g_pNAT->tryGoOnline(); … … 347 475 return 0; 348 476 } 477 349 478 350 479 /** slirp's hooks */
Note:
See TracChangeset
for help on using the changeset viewer.