/* $Id: TestVBoxNATEngine.java $ */ /*!file * Small sample/testcase which demonstrates that the same source code can * be used to connect to the webservice and (XP)COM APIs. */ /* * Copyright (C) 2013-2023 Oracle and/or its affiliates. * * This file is part of VirtualBox base platform packages, as * available from https://www.virtualbox.org. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, in version 3 of the * License. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . * * SPDX-License-Identifier: GPL-3.0-only */ import org.virtualbox_5_0.*; import java.util.List; import java.util.Arrays; import java.util.Iterator; import java.math.BigInteger; public class TestVBoxNATEngine { void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm) { String name; boolean inaccessible = false; /* different chipsets might have different number of attachments */ ChipsetType chipsetType = vm.getChipsetType(); INetworkAdapter adapters[] = new INetworkAdapter[ vbox.getSystemProperties().getMaxNetworkAdapters(chipsetType).intValue()]; try { name = vm.getName(); /* * Dump adapters and if it's got NAT attachment * dump it settings */ for (int nidx = 0; nidx < adapters.length; ++nidx) { /* select available and NATs only. */ adapters[nidx] = vm.getNetworkAdapter(new Long(nidx)); INetworkAdapter n = adapters[nidx]; if (n == null) continue; NetworkAttachmentType attachmentType = n.getAttachmentType(); if (attachmentType.equals(NetworkAttachmentType.NAT)) { INATEngine nat = n.getNATEngine(); List portForward = nat.getRedirects(); String pf = null; Iterator itPortForward = portForward.iterator(); for (;itPortForward.hasNext();) { pf = itPortForward.next(); System.out.println(name + ":NIC" + n.getSlot() /* name:NIC*/ + " pf: " + pf); /* port-forward rule */ } if (pf != null) { String pfAttributes[] = pf.split(","); /* name,proto,hostip,host,hostport,guestip,guestport */ nat.removeRedirect(pfAttributes[0]); nat.addRedirect("", NATProtocol.fromValue(new Integer(pfAttributes[1]).longValue()), pfAttributes[2], new Integer( new Integer(pfAttributes[3]).intValue() + 1), pfAttributes[4], new Integer(pfAttributes[5])); } } } } catch (VBoxException e) { name = ""; inaccessible = true; } // process system event queue mgr.waitForEvents(0); } static void testStart(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm) { System.out.println("\nAttempting to start VM '" + vm.getName() + "'"); mgr.startVm(vm.getName(), null, 7000); // process system event queue mgr.waitForEvents(0); } public TestVBoxNATEngine(String[] args) { VirtualBoxManager mgr = VirtualBoxManager.createInstance(null); boolean ws = false; String url = null; String user = null; String passwd = null; String vmname = null; IMachine vm = null; for (int i = 0; i