1 | /* $Id: TestVBoxNATEngine.java 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /*!file
|
---|
3 | * Small sample/testcase which demonstrates that the same source code can
|
---|
4 | * be used to connect to the webservice and (XP)COM APIs.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox base platform packages, as
|
---|
11 | * available from https://www.virtualbox.org.
|
---|
12 | *
|
---|
13 | * This program is free software; you can redistribute it and/or
|
---|
14 | * modify it under the terms of the GNU General Public License
|
---|
15 | * as published by the Free Software Foundation, in version 3 of the
|
---|
16 | * License.
|
---|
17 | *
|
---|
18 | * This program is distributed in the hope that it will be useful, but
|
---|
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
21 | * General Public License for more details.
|
---|
22 | *
|
---|
23 | * You should have received a copy of the GNU General Public License
|
---|
24 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
25 | *
|
---|
26 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
27 | */
|
---|
28 |
|
---|
29 | import org.virtualbox_5_0.*;
|
---|
30 | import java.util.List;
|
---|
31 | import java.util.Arrays;
|
---|
32 | import java.util.Iterator;
|
---|
33 | import java.math.BigInteger;
|
---|
34 |
|
---|
35 | public class TestVBoxNATEngine
|
---|
36 | {
|
---|
37 | void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm)
|
---|
38 | {
|
---|
39 | String name;
|
---|
40 | boolean inaccessible = false;
|
---|
41 | /* different chipsets might have different number of attachments */
|
---|
42 | ChipsetType chipsetType = vm.getChipsetType();
|
---|
43 | INetworkAdapter adapters[] =
|
---|
44 | new INetworkAdapter[
|
---|
45 | vbox.getSystemProperties().getMaxNetworkAdapters(chipsetType).intValue()];
|
---|
46 |
|
---|
47 | try
|
---|
48 | {
|
---|
49 | name = vm.getName();
|
---|
50 | /*
|
---|
51 | * Dump adapters and if it's got NAT attachment
|
---|
52 | * dump it settings
|
---|
53 | */
|
---|
54 |
|
---|
55 | for (int nidx = 0; nidx < adapters.length; ++nidx)
|
---|
56 | {
|
---|
57 | /* select available and NATs only. */
|
---|
58 | adapters[nidx] = vm.getNetworkAdapter(new Long(nidx));
|
---|
59 | INetworkAdapter n = adapters[nidx];
|
---|
60 |
|
---|
61 | if (n == null)
|
---|
62 | continue;
|
---|
63 | NetworkAttachmentType attachmentType = n.getAttachmentType();
|
---|
64 | if (attachmentType.equals(NetworkAttachmentType.NAT))
|
---|
65 | {
|
---|
66 | INATEngine nat = n.getNATEngine();
|
---|
67 | List<String> portForward = nat.getRedirects();
|
---|
68 | String pf = null;
|
---|
69 | Iterator<String> itPortForward = portForward.iterator();
|
---|
70 | for (;itPortForward.hasNext();)
|
---|
71 | {
|
---|
72 | pf = itPortForward.next();
|
---|
73 | System.out.println(name + ":NIC" + n.getSlot() /* name:NIC<slot number>*/
|
---|
74 | + " pf: " + pf); /* port-forward rule */
|
---|
75 | }
|
---|
76 | if (pf != null)
|
---|
77 | {
|
---|
78 | String pfAttributes[] = pf.split(",");
|
---|
79 | /* name,proto,hostip,host,hostport,guestip,guestport */
|
---|
80 | nat.removeRedirect(pfAttributes[0]);
|
---|
81 | nat.addRedirect("",
|
---|
82 | NATProtocol.fromValue(new Integer(pfAttributes[1]).longValue()),
|
---|
83 | pfAttributes[2],
|
---|
84 | new Integer(
|
---|
85 | new Integer(pfAttributes[3]).intValue() + 1),
|
---|
86 | pfAttributes[4],
|
---|
87 | new Integer(pfAttributes[5]));
|
---|
88 | }
|
---|
89 |
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | }
|
---|
94 | catch (VBoxException e)
|
---|
95 | {
|
---|
96 | name = "<inaccessible>";
|
---|
97 | inaccessible = true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | // process system event queue
|
---|
101 | mgr.waitForEvents(0);
|
---|
102 | }
|
---|
103 |
|
---|
104 | static void testStart(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm)
|
---|
105 | {
|
---|
106 | System.out.println("\nAttempting to start VM '" + vm.getName() + "'");
|
---|
107 | mgr.startVm(vm.getName(), null, 7000);
|
---|
108 | // process system event queue
|
---|
109 | mgr.waitForEvents(0);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public TestVBoxNATEngine(String[] args)
|
---|
113 | {
|
---|
114 | VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
|
---|
115 |
|
---|
116 | boolean ws = false;
|
---|
117 | String url = null;
|
---|
118 | String user = null;
|
---|
119 | String passwd = null;
|
---|
120 | String vmname = null;
|
---|
121 | IMachine vm = null;
|
---|
122 |
|
---|
123 | for (int i = 0; i<args.length; i++)
|
---|
124 | {
|
---|
125 | if ("-w".equals(args[i]))
|
---|
126 | ws = true;
|
---|
127 | else if ("-url".equals(args[i]))
|
---|
128 | url = args[++i];
|
---|
129 | else if ("-user".equals(args[i]))
|
---|
130 | user = args[++i];
|
---|
131 | else if ("-passwd".equals(args[i]))
|
---|
132 | passwd = args[++i];
|
---|
133 | else if ("-vm".equals(args[i]))
|
---|
134 | vmname = args[++i];
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (ws)
|
---|
138 | {
|
---|
139 | try {
|
---|
140 | mgr.connect(url, user, passwd);
|
---|
141 | } catch (VBoxException e) {
|
---|
142 | e.printStackTrace();
|
---|
143 | System.out.println("Cannot connect, start webserver first!");
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | try
|
---|
148 | {
|
---|
149 | IVirtualBox vbox = mgr.getVBox();
|
---|
150 | if (vbox != null)
|
---|
151 | {
|
---|
152 | if (vmname != null)
|
---|
153 | {
|
---|
154 | for (IMachine m:vbox.getMachines())
|
---|
155 | {
|
---|
156 | if (m.getName().equals(vmname))
|
---|
157 | {
|
---|
158 | vm = m;
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | }
|
---|
164 | else
|
---|
165 | vm = vbox.getMachines().get(0);
|
---|
166 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
---|
167 | if (vm != null)
|
---|
168 | {
|
---|
169 | testEnumeration(mgr, vbox, vm);
|
---|
170 | testStart(mgr, vbox, vm);
|
---|
171 | }
|
---|
172 | System.out.println("done, press Enter...");
|
---|
173 | int ch = System.in.read();
|
---|
174 | }
|
---|
175 | }
|
---|
176 | catch (VBoxException e)
|
---|
177 | {
|
---|
178 | System.out.println("VBox error: "+e.getMessage()+" original="+e.getWrapped());
|
---|
179 | e.printStackTrace();
|
---|
180 | }
|
---|
181 | catch (java.io.IOException e)
|
---|
182 | {
|
---|
183 | e.printStackTrace();
|
---|
184 | }
|
---|
185 |
|
---|
186 | // process system event queue
|
---|
187 | mgr.waitForEvents(0);
|
---|
188 |
|
---|
189 | if (ws)
|
---|
190 | {
|
---|
191 | try {
|
---|
192 | mgr.disconnect();
|
---|
193 | } catch (VBoxException e) {
|
---|
194 | e.printStackTrace();
|
---|
195 | }
|
---|
196 | }
|
---|
197 | /* cleanup do the disconnect */
|
---|
198 | mgr.cleanup();
|
---|
199 |
|
---|
200 | }
|
---|
201 | public static void main(String[] args)
|
---|
202 | {
|
---|
203 | new TestVBoxNATEngine(args);
|
---|
204 | }
|
---|
205 |
|
---|
206 | }
|
---|