summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/glue/tests/TestVBoxNATEngine.java
blob: a54f163c081e259d461d37dafbbddd19a8a05893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* $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 <https://www.gnu.org/licenses>.
 *
 * 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<String> portForward = nat.getRedirects();
                    String pf = null;
                    Iterator<String> itPortForward = portForward.iterator();
                    for (;itPortForward.hasNext();)
                    {
                        pf = itPortForward.next();
                        System.out.println(name + ":NIC" + n.getSlot() /* name:NIC<slot number>*/
                                           + " 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>";
            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<args.length; i++)
            {
                if ("-w".equals(args[i]))
                    ws = true;
                else if ("-url".equals(args[i]))
                    url = args[++i];
                else if ("-user".equals(args[i]))
                    user = args[++i];
                else if ("-passwd".equals(args[i]))
                    passwd = args[++i];
                else if ("-vm".equals(args[i]))
                    vmname = args[++i];
            }

        if (ws)
            {
                try {
                    mgr.connect(url, user, passwd);
                } catch (VBoxException e) {
                    e.printStackTrace();
                    System.out.println("Cannot connect, start webserver first!");
                }
            }

        try
            {
                IVirtualBox vbox = mgr.getVBox();
                if (vbox != null)
                {
                    if (vmname != null)
                    {
                        for (IMachine m:vbox.getMachines())
                        {
                            if (m.getName().equals(vmname))
                            {
                                vm = m;
                                break;
                            }
                        }

                    }
                    else
                        vm = vbox.getMachines().get(0);
                    System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
                    if (vm != null)
                    {
                        testEnumeration(mgr, vbox, vm);
                        testStart(mgr, vbox, vm);
                    }
                    System.out.println("done, press Enter...");
                    int ch = System.in.read();
                }
            }
        catch (VBoxException e)
            {
                System.out.println("VBox error: "+e.getMessage()+" original="+e.getWrapped());
                e.printStackTrace();
            }
        catch (java.io.IOException e)
            {
                e.printStackTrace();
            }

        // process system event queue
        mgr.waitForEvents(0);

        if (ws)
            {
                try {
                    mgr.disconnect();
                } catch (VBoxException e) {
                    e.printStackTrace();
                }
            }
        /* cleanup do the disconnect */
        mgr.cleanup();

    }
    public static void main(String[] args)
    {
        new TestVBoxNATEngine(args);
    }

}