vbox_tmpl.c 217.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/** @file vbox_tmpl.c
 * Template File to support multiple versions of VirtualBox
 * at runtime :).
 *
 * IMPORTANT:
 * Please dont include this file in the src/Makefile.am, it
 * is automatically include by other files.
 */

/*
11
 * Copyright (C) 2010-2014 Red Hat, Inc.
12 13 14 15 16
 * Copyright (C) 2008-2009 Sun Microsystems, Inc.
 *
 * This file is part of a free software library; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General
 * Public License version 2.1 as published by the Free Software
17
 * Foundation and shipped in the "COPYING.LESSER" file with this library.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * The library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY of any kind.
 *
 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if
 * any license choice other than GPL or LGPL is available it will
 * apply instead, Sun elects to use only the Lesser General Public
 * License version 2.1 (LGPLv2) at this time for any software where
 * a choice of LGPL license versions is made available with the
 * language indicating that LGPLv2 or any later version may be used,
 * or where a choice of which version of the LGPL is applied is
 * otherwise unspecified.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 USA or visit http://www.sun.com if you need
 * additional information or have any questions.
 */

#include <config.h>

37
#include <unistd.h>
38 39 40
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
41 42 43 44

#include "internal.h"
#include "datatypes.h"
#include "domain_conf.h"
45
#include "snapshot_conf.h"
46
#include "vbox_snapshot_conf.h"
47
#include "network_conf.h"
48
#include "virerror.h"
49
#include "domain_event.h"
50
#include "storage_conf.h"
51
#include "virstoragefile.h"
52
#include "viruuid.h"
53
#include "viralloc.h"
54
#include "nodeinfo.h"
55
#include "virlog.h"
56
#include "vbox_driver.h"
57
#include "configmake.h"
E
Eric Blake 已提交
58
#include "virfile.h"
59
#include "fdstream.h"
M
Martin Kletzander 已提交
60
#include "viruri.h"
61
#include "virstring.h"
62 63
#include "virtime.h"
#include "virutil.h"
64 65

/* This one changes from version to version. */
66
#if VBOX_API_VERSION == 2002000
67
# include "vbox_CAPI_v2_2.h"
68
#elif VBOX_API_VERSION == 3000000
69
# include "vbox_CAPI_v3_0.h"
70
#elif VBOX_API_VERSION == 3001000
71
# include "vbox_CAPI_v3_1.h"
72
#elif VBOX_API_VERSION == 3002000
73
# include "vbox_CAPI_v3_2.h"
74
#elif VBOX_API_VERSION == 4000000
75
# include "vbox_CAPI_v4_0.h"
76
#elif VBOX_API_VERSION == 4001000
77
# include "vbox_CAPI_v4_1.h"
78
#elif VBOX_API_VERSION == 4002000
79
# include "vbox_CAPI_v4_2.h"
80 81 82
#elif VBOX_API_VERSION == 4002020
# include "vbox_CAPI_v4_2_20.h"
#elif VBOX_API_VERSION == 4003000
R
Ryota Ozaki 已提交
83
# include "vbox_CAPI_v4_3.h"
84 85
#elif VBOX_API_VERSION == 4003004
# include "vbox_CAPI_v4_3_4.h"
86 87
#else
# error "Unsupport VBOX_API_VERSION"
88 89
#endif

90
/* Include this *last* or we'll get the wrong vbox_CAPI_*.h. */
91
#include "vbox_glue.h"
T
Taowei 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104

#if VBOX_API_VERSION < 4000000
typedef IVRDPServer IVRDxServer;
#else /* VBOX_API_VERSION >= 4000000 */
typedef IVRDEServer IVRDxServer;
#endif /* VBOX_API_VERSION >= 4000000 */

#if VBOX_API_VERSION < 4003000
typedef IUSBController IUSBCommon;
#else /* VBOX_API_VERSION >= 4003000 */
typedef IUSBDeviceFilters IUSBCommon;
#endif /* VBOX_API_VERSION >= 4003000 */

T
Taowei 已提交
105 106 107 108
#if VBOX_API_VERSION < 3001000
typedef IHardDiskAttachment IMediumAttachment;
#endif /* VBOX_API_VERSION < 3001000 */

T
Taowei 已提交
109
#include "vbox_uniformed_api.h"
110

111
#define VIR_FROM_THIS                   VIR_FROM_VBOX
112 113 114

VIR_LOG_INIT("vbox.vbox_tmpl");

T
Taowei 已提交
115 116 117
#define vboxUnsupported() \
    VIR_WARN("No %s in current vbox version %d.", __FUNCTION__, VBOX_API_VERSION);

J
John Ferlan 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#define VBOX_UTF16_FREE(arg)                                            \
    do {                                                                \
        if (arg) {                                                      \
            data->pFuncs->pfnUtf16Free(arg);                            \
            (arg) = NULL;                                               \
        }                                                               \
    } while (0)

#define VBOX_UTF8_FREE(arg)                                             \
    do {                                                                \
        if (arg) {                                                      \
            data->pFuncs->pfnUtf8Free(arg);                             \
            (arg) = NULL;                                               \
        }                                                               \
    } while (0)

#define VBOX_COM_UNALLOC_MEM(arg)                                       \
    do {                                                                \
        if (arg) {                                                      \
            data->pFuncs->pfnComUnallocMem(arg);                        \
            (arg) = NULL;                                               \
        }                                                               \
    } while (0)

142 143 144
#define VBOX_UTF16_TO_UTF8(arg1, arg2)  data->pFuncs->pfnUtf16ToUtf8(arg1, arg2)
#define VBOX_UTF8_TO_UTF16(arg1, arg2)  data->pFuncs->pfnUtf8ToUtf16(arg1, arg2)

145 146
#define VBOX_ADDREF(arg) (arg)->vtbl->nsisupports.AddRef((nsISupports *)(arg))

147 148 149 150 151 152 153
#define VBOX_RELEASE(arg)                                                     \
    do {                                                                      \
        if (arg) {                                                            \
            (arg)->vtbl->nsisupports.Release((nsISupports *)(arg));           \
            (arg) = NULL;                                                     \
        }                                                                     \
    } while (0)
154 155 156 157

#define VBOX_OBJECT_CHECK(conn, type, value) \
vboxGlobalData *data = conn->privateData;\
type ret = value;\
158
if (!data->vboxObj) {\
159 160 161 162 163 164 165
    return ret;\
}

#define VBOX_OBJECT_HOST_CHECK(conn, type, value) \
vboxGlobalData *data = conn->privateData;\
type ret = value;\
IHost *host = NULL;\
166
if (!data->vboxObj) {\
167 168 169 170 171 172 173
    return ret;\
}\
data->vboxObj->vtbl->GetHost(data->vboxObj, &host);\
if (!host) {\
    return ret;\
}

174
#if VBOX_API_VERSION < 3001000
175

176
# define VBOX_MEDIUM_RELEASE(arg) \
177
if (arg)\
178
    (arg)->vtbl->imedium.nsisupports.Release((nsISupports *)(arg))
179
# define VBOX_MEDIUM_FUNC_ARG1(object, func, arg1) \
180
    (object)->vtbl->imedium.func((IMedium *)(object), arg1)
181
# define VBOX_MEDIUM_FUNC_ARG2(object, func, arg1, arg2) \
182 183
    (object)->vtbl->imedium.func((IMedium *)(object), arg1, arg2)

184
#else  /* VBOX_API_VERSION >= 3001000 */
185 186 187

typedef IMedium IHardDisk;
typedef IMediumAttachment IHardDiskAttachment;
188 189 190 191 192
# define MediaState_Inaccessible     MediumState_Inaccessible
# define HardDiskVariant_Standard    MediumVariant_Standard
# define HardDiskVariant_Fixed       MediumVariant_Fixed
# define VBOX_MEDIUM_RELEASE(arg) VBOX_RELEASE(arg)
# define VBOX_MEDIUM_FUNC_ARG1(object, func, arg1) \
193
    (object)->vtbl->func(object, arg1)
194
# define VBOX_MEDIUM_FUNC_ARG2(object, func, arg1, arg2) \
195 196
    (object)->vtbl->func(object, arg1, arg2)

197
#endif /* VBOX_API_VERSION >= 3001000 */
198

199 200 201 202 203 204
#define DEBUGPRUnichar(msg, strUtf16) \
if (strUtf16) {\
    char *strUtf8 = NULL;\
\
    g_pVBoxGlobalData->pFuncs->pfnUtf16ToUtf8(strUtf16, &strUtf8);\
    if (strUtf8) {\
205
        VIR_DEBUG("%s: %s", msg, strUtf8);\
206 207 208 209 210 211
        g_pVBoxGlobalData->pFuncs->pfnUtf8Free(strUtf8);\
    }\
}

#define DEBUGUUID(msg, iid) \
{\
T
Taowei 已提交
212
    VIR_DEBUG("%s: {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", msg,\
213 214 215 216 217 218 219 220 221 222 223 224 225
          (unsigned)(iid)->m0,\
          (unsigned)(iid)->m1,\
          (unsigned)(iid)->m2,\
          (unsigned)(iid)->m3[0],\
          (unsigned)(iid)->m3[1],\
          (unsigned)(iid)->m3[2],\
          (unsigned)(iid)->m3[3],\
          (unsigned)(iid)->m3[4],\
          (unsigned)(iid)->m3[5],\
          (unsigned)(iid)->m3[6],\
          (unsigned)(iid)->m3[7]);\
}\

T
Taowei 已提交
226
#if VBOX_API_VERSION > 2002000
227

228 229 230 231 232 233 234 235 236 237
/* g_pVBoxGlobalData has to be global variable,
 * there is no other way to make the callbacks
 * work other then having g_pVBoxGlobalData as
 * global, because the functions namely AddRef,
 * Release, etc consider it as global and you
 * can't change the function definition as it
 * is XPCOM nsISupport::* function and it expects
 * them that way
 */

238
static vboxGlobalData *g_pVBoxGlobalData = NULL;
239

240
#endif /* !(VBOX_API_VERSION == 2002000) */
241

242
#if VBOX_API_VERSION < 4000000
243 244 245 246 247 248 249 250 251 252 253 254 255

# define VBOX_OBJECT_GET_MACHINE(/* in */ iid_value, /* out */ machine) \
    data->vboxObj->vtbl->GetMachine(data->vboxObj, iid_value, machine)

# define VBOX_SESSION_OPEN(/* in */ iid_value, /* unused */ machine) \
    data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, iid_value)

# define VBOX_SESSION_OPEN_EXISTING(/* in */ iid_value, /* unused */ machine) \
    data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, iid_value)

# define VBOX_SESSION_CLOSE() \
    data->vboxSession->vtbl->Close(data->vboxSession)

256
#else /* VBOX_API_VERSION >= 4000000 */
257 258 259 260 261 262 263 264 265 266 267 268 269

# define VBOX_OBJECT_GET_MACHINE(/* in */ iid_value, /* out */ machine) \
    data->vboxObj->vtbl->FindMachine(data->vboxObj, iid_value, machine)

# define VBOX_SESSION_OPEN(/* unused */ iid_value, /* in */ machine) \
    machine->vtbl->LockMachine(machine, data->vboxSession, LockType_Write)

# define VBOX_SESSION_OPEN_EXISTING(/* unused */ iid_value, /* in */ machine) \
    machine->vtbl->LockMachine(machine, data->vboxSession, LockType_Shared)

# define VBOX_SESSION_CLOSE() \
    data->vboxSession->vtbl->UnlockMachine(data->vboxSession)

270
#endif /* VBOX_API_VERSION >= 4000000 */
271

272 273 274 275 276
#if VBOX_API_VERSION > 2002000 && VBOX_API_VERSION < 4000000
/* Since vboxConnectGetCapabilities has been rewritten,
 * vboxDriverLock and vboxDriverUnlock only be used in code for
 * 3.x release. */

277 278
static void vboxDriverLock(vboxGlobalData *data)
{
279 280 281
    virMutexLock(&data->lock);
}

282 283
static void vboxDriverUnlock(vboxGlobalData *data)
{
284 285 286
    virMutexUnlock(&data->lock);
}

287 288
#endif

289
#if VBOX_API_VERSION == 2002000
290

291 292
static void nsIDtoChar(unsigned char *uuid, const nsID *iid)
{
293 294 295
    char uuidstrsrc[VIR_UUID_STRING_BUFLEN];
    char uuidstrdst[VIR_UUID_STRING_BUFLEN];
    unsigned char uuidinterim[VIR_UUID_BUFLEN];
296
    size_t i;
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

    memcpy(uuidinterim, iid, VIR_UUID_BUFLEN);
    virUUIDFormat(uuidinterim, uuidstrsrc);

    uuidstrdst[0]  = uuidstrsrc[6];
    uuidstrdst[1]  = uuidstrsrc[7];
    uuidstrdst[2]  = uuidstrsrc[4];
    uuidstrdst[3]  = uuidstrsrc[5];
    uuidstrdst[4]  = uuidstrsrc[2];
    uuidstrdst[5]  = uuidstrsrc[3];
    uuidstrdst[6]  = uuidstrsrc[0];
    uuidstrdst[7]  = uuidstrsrc[1];

    uuidstrdst[8]  = uuidstrsrc[8];

    uuidstrdst[9]  = uuidstrsrc[11];
    uuidstrdst[10] = uuidstrsrc[12];
    uuidstrdst[11] = uuidstrsrc[9];
    uuidstrdst[12] = uuidstrsrc[10];

    uuidstrdst[13] = uuidstrsrc[13];

    uuidstrdst[14] = uuidstrsrc[16];
    uuidstrdst[15] = uuidstrsrc[17];
    uuidstrdst[16] = uuidstrsrc[14];
    uuidstrdst[17] = uuidstrsrc[15];

324
    for (i = 18; i < VIR_UUID_STRING_BUFLEN; i++) {
325 326 327 328
        uuidstrdst[i] = uuidstrsrc[i];
    }

    uuidstrdst[VIR_UUID_STRING_BUFLEN-1] = '\0';
329
    ignore_value(virUUIDParse(uuidstrdst, uuid));
330 331
}

332 333
static void nsIDFromChar(nsID *iid, const unsigned char *uuid)
{
334 335 336
    char uuidstrsrc[VIR_UUID_STRING_BUFLEN];
    char uuidstrdst[VIR_UUID_STRING_BUFLEN];
    unsigned char uuidinterim[VIR_UUID_BUFLEN];
337
    size_t i;
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

    virUUIDFormat(uuid, uuidstrsrc);

    uuidstrdst[0]  = uuidstrsrc[6];
    uuidstrdst[1]  = uuidstrsrc[7];
    uuidstrdst[2]  = uuidstrsrc[4];
    uuidstrdst[3]  = uuidstrsrc[5];
    uuidstrdst[4]  = uuidstrsrc[2];
    uuidstrdst[5]  = uuidstrsrc[3];
    uuidstrdst[6]  = uuidstrsrc[0];
    uuidstrdst[7]  = uuidstrsrc[1];

    uuidstrdst[8]  = uuidstrsrc[8];

    uuidstrdst[9]  = uuidstrsrc[11];
    uuidstrdst[10] = uuidstrsrc[12];
    uuidstrdst[11] = uuidstrsrc[9];
    uuidstrdst[12] = uuidstrsrc[10];

    uuidstrdst[13] = uuidstrsrc[13];

    uuidstrdst[14] = uuidstrsrc[16];
    uuidstrdst[15] = uuidstrsrc[17];
    uuidstrdst[16] = uuidstrsrc[14];
    uuidstrdst[17] = uuidstrsrc[15];

364
    for (i = 18; i < VIR_UUID_STRING_BUFLEN; i++) {
365 366 367 368
        uuidstrdst[i] = uuidstrsrc[i];
    }

    uuidstrdst[VIR_UUID_STRING_BUFLEN-1] = '\0';
369
    ignore_value(virUUIDParse(uuidstrdst, uuidinterim));
370 371 372
    memcpy(iid, uuidinterim, VIR_UUID_BUFLEN);
}

373
# ifdef WIN32
374

375 376 377 378
typedef struct _vboxIID_v2_x_WIN32 vboxIID;
typedef struct _vboxIID_v2_x_WIN32 vboxIID_v2_x_WIN32;

#  define VBOX_IID_INITIALIZER { { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } } }
T
Taowei 已提交
379
#  define IID_MEMBER(name) (iidu->vboxIID_v2_x_WIN32.name)
380 381 382 383 384 385

static void
vboxIIDUnalloc_v2_x_WIN32(vboxGlobalData *data ATTRIBUTE_UNUSED,
                          vboxIID_v2_x_WIN32 *iid ATTRIBUTE_UNUSED)
{
    /* Nothing to free */
386
}
387

T
Taowei 已提交
388 389 390 391 392 393 394
static void
_vboxIIDUnalloc(vboxGlobalData *data ATTRIBUTE_UNUSED,
                vboxIIDUnion *iid ATTRIBUTE_UNUSED)
{
    /* Nothing to free */
}

395 396 397 398 399
static void
vboxIIDToUUID_v2_x_WIN32(vboxIID_v2_x_WIN32 *iid, unsigned char *uuid)
{
    nsIDtoChar(uuid, (nsID *)&iid->value);
}
400

T
Taowei 已提交
401 402 403 404 405 406
static void
_vboxIIDToUUID(vboxGlobalData *data ATTRIBUTE_UNUSED, vboxIIDUnion *iidu, unsigned char *uuid)
{
    vboxIIDToUUID_v2_x_WIN32(&iidu->vboxIID_v2_x_WIN32, uuid);
}

407 408 409 410 411
static void
vboxIIDFromUUID_v2_x_WIN32(vboxGlobalData *data, vboxIID_v2_x_WIN32 *iid,
                           const unsigned char *uuid)
{
    vboxIIDUnalloc_v2_x_WIN32(data, iid);
412

413
    nsIDFromChar((nsID *)&iid->value, uuid);
414 415
}

T
Taowei 已提交
416 417 418 419 420 421 422
static void
_vboxIIDFromUUID(vboxGlobalData *data, vboxIIDUnion *iidu,
                 const unsigned char *uuid)
{
    vboxIIDFromUUID_v2_x_WIN32(data, &iidu->vboxIID_v2_x_WIN32, uuid);
}

423 424 425
static bool
vboxIIDIsEqual_v2_x_WIN32(vboxIID_v2_x_WIN32 *iid1, vboxIID_v2_x_WIN32 *iid2)
{
426
    return memcmp(&iid1->value, &iid2->value, sizeof(GUID)) == 0;
427
}
428

T
Taowei 已提交
429 430 431 432 433 434
static bool
_vboxIIDIsEqual(vboxGlobalData *data ATTRIBUTE_UNUSED, vboxIIDUnion *iidu1, vboxIIDUnion *iidu2)
{
    return vboxIIDIsEqual_v2_x_WIN32(&iidu1->vboxIID_v2_x_WIN32, &iidu2->vboxIID_v2_x_WIN32);
}

435 436 437 438 439 440 441 442
static void
vboxIIDFromArrayItem_v2_x_WIN32(vboxGlobalData *data, vboxIID_v2_x_WIN32 *iid,
                                vboxArray *array, int idx)
{
    GUID *items = (GUID *)array->items;

    vboxIIDUnalloc_v2_x_WIN32(data, iid);

443
    memcpy(&iid->value, &items[idx], sizeof(GUID));
444 445
}

T
Taowei 已提交
446 447 448 449 450 451 452
static void
_vboxIIDFromArrayItem(vboxGlobalData *data, vboxIIDUnion *iidu,
                      vboxArray *array, int idx)
{
    vboxIIDFromArrayItem_v2_x_WIN32(data, &iidu->vboxIID_v2_x_WIN32, array, idx);
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466
#  define vboxIIDUnalloc(iid) vboxIIDUnalloc_v2_x_WIN32(data, iid)
#  define vboxIIDToUUID(iid, uuid) vboxIIDToUUID_v2_x_WIN32(iid, uuid)
#  define vboxIIDFromUUID(iid, uuid) vboxIIDFromUUID_v2_x_WIN32(data, iid, uuid)
#  define vboxIIDIsEqual(iid1, iid2) vboxIIDIsEqual_v2_x_WIN32(iid1, iid2)
#  define vboxIIDFromArrayItem(iid, array, idx) \
    vboxIIDFromArrayItem_v2_x_WIN32(data, iid, array, idx)
#  define DEBUGIID(msg, iid) DEBUGUUID(msg, (nsID *)&(iid))

# else /* !WIN32 */

typedef struct _vboxIID_v2_x vboxIID;
typedef struct _vboxIID_v2_x vboxIID_v2_x;

#  define VBOX_IID_INITIALIZER { NULL, { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } } }
T
Taowei 已提交
467
#  define IID_MEMBER(name) (iidu->vboxIID_v2_x.name)
468 469 470 471 472

static void
vboxIIDUnalloc_v2_x(vboxGlobalData *data, vboxIID_v2_x *iid)
{
    if (iid->value == NULL) {
473 474 475
        return;
    }

476 477 478 479 480 481 482
    if (iid->value != &iid->backing) {
        data->pFuncs->pfnComUnallocMem(iid->value);
    }

    iid->value = NULL;
}

T
Taowei 已提交
483 484 485 486 487 488
static void
_vboxIIDUnalloc(vboxGlobalData *data, vboxIIDUnion *iidu)
{
    vboxIIDUnalloc_v2_x(data, &iidu->vboxIID_v2_x);
}

489 490 491 492 493 494
static void
vboxIIDToUUID_v2_x(vboxIID_v2_x *iid, unsigned char *uuid)
{
    nsIDtoChar(uuid, iid->value);
}

T
Taowei 已提交
495 496 497 498 499 500 501
static void
_vboxIIDToUUID(vboxGlobalData *data ATTRIBUTE_UNUSED,
               vboxIIDUnion *iidu, unsigned char *uuid)
{
    vboxIIDToUUID_v2_x(&iidu->vboxIID_v2_x, uuid);
}

502 503 504 505 506 507 508 509
static void
vboxIIDFromUUID_v2_x(vboxGlobalData *data, vboxIID_v2_x *iid,
                     const unsigned char *uuid)
{
    vboxIIDUnalloc_v2_x(data, iid);

    iid->value = &iid->backing;

510
    sa_assert(iid->value);
511
    nsIDFromChar(iid->value, uuid);
512 513
}

T
Taowei 已提交
514 515 516 517 518 519 520
static void
_vboxIIDFromUUID(vboxGlobalData *data, vboxIIDUnion *iidu,
                 const unsigned char *uuid)
{
    vboxIIDFromUUID_v2_x(data, &iidu->vboxIID_v2_x, uuid);
}

521 522 523
static bool
vboxIIDIsEqual_v2_x(vboxIID_v2_x *iid1, vboxIID_v2_x *iid2)
{
524
    return memcmp(iid1->value, iid2->value, sizeof(nsID)) == 0;
525 526
}

T
Taowei 已提交
527 528 529 530 531 532 533
static bool
_vboxIIDIsEqual(vboxGlobalData *data ATTRIBUTE_UNUSED,
                vboxIIDUnion *iidu1, vboxIIDUnion *iidu2)
{
    return vboxIIDIsEqual_v2_x(&iidu1->vboxIID_v2_x, &iidu2->vboxIID_v2_x);
}

534 535 536 537 538 539 540 541
static void
vboxIIDFromArrayItem_v2_x(vboxGlobalData *data, vboxIID_v2_x *iid,
                          vboxArray *array, int idx)
{
    vboxIIDUnalloc_v2_x(data, iid);

    iid->value = &iid->backing;

542
    memcpy(iid->value, array->items[idx], sizeof(nsID));
543 544
}

T
Taowei 已提交
545 546 547 548 549 550 551
static void
_vboxIIDFromArrayItem(vboxGlobalData *data, vboxIIDUnion *iidu,
                      vboxArray *array, int idx)
{
    vboxIIDFromArrayItem_v2_x(data, &iidu->vboxIID_v2_x, array, idx);
}

552 553 554 555 556 557 558 559 560 561
#  define vboxIIDUnalloc(iid) vboxIIDUnalloc_v2_x(data, iid)
#  define vboxIIDToUUID(iid, uuid) vboxIIDToUUID_v2_x(iid, uuid)
#  define vboxIIDFromUUID(iid, uuid) vboxIIDFromUUID_v2_x(data, iid, uuid)
#  define vboxIIDIsEqual(iid1, iid2) vboxIIDIsEqual_v2_x(iid1, iid2)
#  define vboxIIDFromArrayItem(iid, array, idx) \
    vboxIIDFromArrayItem_v2_x(data, iid, array, idx)
#  define DEBUGIID(msg, iid) DEBUGUUID(msg, iid)

# endif /* !WIN32 */

562
#else /* VBOX_API_VERSION != 2002000 */
563

564 565 566 567
typedef struct _vboxIID_v3_x vboxIID;
typedef struct _vboxIID_v3_x vboxIID_v3_x;

# define VBOX_IID_INITIALIZER { NULL, true }
T
Taowei 已提交
568
# define IID_MEMBER(name) (iidu->vboxIID_v3_x.name)
569 570 571 572 573 574 575 576 577 578

static void
vboxIIDUnalloc_v3_x(vboxGlobalData *data, vboxIID_v3_x *iid)
{
    if (iid->value != NULL && iid->owner) {
        data->pFuncs->pfnUtf16Free(iid->value);
    }

    iid->value = NULL;
    iid->owner = true;
579 580
}

T
Taowei 已提交
581 582 583 584 585 586
static void
_vboxIIDUnalloc(vboxGlobalData *data, vboxIIDUnion *iidu)
{
    vboxIIDUnalloc_v3_x(data, &iidu->vboxIID_v3_x);
}

587 588 589 590 591 592 593 594
static void
vboxIIDToUUID_v3_x(vboxGlobalData *data, vboxIID_v3_x *iid,
                   unsigned char *uuid)
{
    char *utf8 = NULL;

    data->pFuncs->pfnUtf16ToUtf8(iid->value, &utf8);

595
    ignore_value(virUUIDParse(utf8, uuid));
596 597

    data->pFuncs->pfnUtf8Free(utf8);
598 599
}

T
Taowei 已提交
600 601 602 603 604 605 606
static void
_vboxIIDToUUID(vboxGlobalData *data, vboxIIDUnion *iidu,
               unsigned char *uuid)
{
    vboxIIDToUUID_v3_x(data, &iidu->vboxIID_v3_x, uuid);
}

607 608 609 610 611
static void
vboxIIDFromUUID_v3_x(vboxGlobalData *data, vboxIID_v3_x *iid,
                     const unsigned char *uuid)
{
    char utf8[VIR_UUID_STRING_BUFLEN];
612

613
    vboxIIDUnalloc_v3_x(data, iid);
614

615
    virUUIDFormat(uuid, utf8);
616

617 618
    data->pFuncs->pfnUtf8ToUtf16(utf8, &iid->value);
}
619

T
Taowei 已提交
620 621 622 623 624 625 626
static void
_vboxIIDFromUUID(vboxGlobalData *data, vboxIIDUnion *iidu,
                 const unsigned char *uuid)
{
    vboxIIDFromUUID_v3_x(data, &iidu->vboxIID_v3_x, uuid);
}

627 628 629 630 631 632
static bool
vboxIIDIsEqual_v3_x(vboxGlobalData *data, vboxIID_v3_x *iid1,
                    vboxIID_v3_x *iid2)
{
    unsigned char uuid1[VIR_UUID_BUFLEN];
    unsigned char uuid2[VIR_UUID_BUFLEN];
633 634

    /* Note: we can't directly compare the utf8 strings here
E
Eric Blake 已提交
635
     * cause the two UUID's may have separators as space or '-'
636 637
     * or mixture of both and we don't want to fail here by
     * using direct string comparison. Here virUUIDParse() takes
638 639 640
     * care of these cases. */
    vboxIIDToUUID_v3_x(data, iid1, uuid1);
    vboxIIDToUUID_v3_x(data, iid2, uuid2);
641

642 643
    return memcmp(uuid1, uuid2, VIR_UUID_BUFLEN) == 0;
}
644

T
Taowei 已提交
645 646 647 648 649 650
static bool
_vboxIIDIsEqual(vboxGlobalData *data, vboxIIDUnion *iidu1,
                vboxIIDUnion *iidu2)
{
    return vboxIIDIsEqual_v3_x(data, &iidu1->vboxIID_v3_x, &iidu2->vboxIID_v3_x);
}
651

652 653 654 655 656
static void
vboxIIDFromArrayItem_v3_x(vboxGlobalData *data, vboxIID_v3_x *iid,
                          vboxArray *array, int idx)
{
    vboxIIDUnalloc_v3_x(data, iid);
657

658 659
    iid->value = array->items[idx];
    iid->owner = false;
660 661
}

T
Taowei 已提交
662 663 664 665 666 667 668
static void
_vboxIIDFromArrayItem(vboxGlobalData *data, vboxIIDUnion *iidu,
                      vboxArray *array, int idx)
{
    vboxIIDFromArrayItem_v3_x(data, &iidu->vboxIID_v3_x, array, idx);
}

669 670 671 672 673 674 675
# define vboxIIDUnalloc(iid) vboxIIDUnalloc_v3_x(data, iid)
# define vboxIIDToUUID(iid, uuid) vboxIIDToUUID_v3_x(data, iid, uuid)
# define vboxIIDFromUUID(iid, uuid) vboxIIDFromUUID_v3_x(data, iid, uuid)
# define vboxIIDIsEqual(iid1, iid2) vboxIIDIsEqual_v3_x(data, iid1, iid2)
# define vboxIIDFromArrayItem(iid, array, idx) \
    vboxIIDFromArrayItem_v3_x(data, iid, array, idx)
# define DEBUGIID(msg, strUtf16) DEBUGPRUnichar(msg, strUtf16)
676

T
Taowei 已提交
677 678
#endif /* !(VBOX_API_VERSION == 2002000) */

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
/**
 * function to get the StorageBus, Port number
 * and Device number for the given devicename
 * e.g: hda has StorageBus = IDE, port = 0,
 *      device = 0
 *
 * @returns     true on Success, false on failure.
 * @param       deviceName      Input device name
 * @param       aMaxPortPerInst Input array of max port per device instance
 * @param       aMaxSlotPerPort Input array of max slot per device port
 * @param       storageBus      Input storage bus type
 * @param       deviceInst      Output device instance number
 * @param       devicePort      Output port number
 * @param       deviceSlot      Output slot number
 *
 */
T
Taowei 已提交
695
#if VBOX_API_VERSION >= 3001000
T
Taowei 已提交
696 697
# if VBOX_API_VERSION < 4000000
/* Only 3.x will use this function. */
698 699 700 701 702 703 704 705 706 707 708
static bool vboxGetDeviceDetails(const char *deviceName,
                                 PRUint32   *aMaxPortPerInst,
                                 PRUint32   *aMaxSlotPerPort,
                                 PRUint32    storageBus,
                                 PRInt32    *deviceInst,
                                 PRInt32    *devicePort,
                                 PRInt32    *deviceSlot) {
    int total = 0;
    PRUint32 maxPortPerInst = 0;
    PRUint32 maxSlotPerPort = 0;

709 710 711 712 713 714
    if (!deviceName ||
        !deviceInst ||
        !devicePort ||
        !deviceSlot ||
        !aMaxPortPerInst ||
        !aMaxSlotPerPort)
715 716
        return false;

717 718
    if ((storageBus < StorageBus_IDE) ||
        (storageBus > StorageBus_Floppy))
719 720 721 722 723 724 725
        return false;

    total = virDiskNameToIndex(deviceName);

    maxPortPerInst = aMaxPortPerInst[storageBus];
    maxSlotPerPort = aMaxSlotPerPort[storageBus];

726 727 728
    if (!maxPortPerInst ||
        !maxSlotPerPort ||
        (total < 0))
729 730 731 732 733 734
        return false;

    *deviceInst = total / (maxPortPerInst * maxSlotPerPort);
    *devicePort = (total % (maxPortPerInst * maxSlotPerPort)) / maxSlotPerPort;
    *deviceSlot = (total % (maxPortPerInst * maxSlotPerPort)) % maxSlotPerPort;

735
    VIR_DEBUG("name=%s, total=%d, storageBus=%u, deviceInst=%d, "
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
          "devicePort=%d deviceSlot=%d, maxPortPerInst=%u maxSlotPerPort=%u",
          deviceName, total, storageBus, *deviceInst, *devicePort,
          *deviceSlot, maxPortPerInst, maxSlotPerPort);

    return true;
}

/**
 * function to get the values for max port per
 * instance and max slots per port for the devices
 *
 * @returns     true on Success, false on failure.
 * @param       vbox            Input IVirtualBox pointer
 * @param       maxPortPerInst  Output array of max port per instance
 * @param       maxSlotPerPort  Output array of max slot per port
 *
 */

754 755
/* This function would not be used in 4.1 and later since
 * vboxDomainSnapshotGetXMLDesc is written*/
756 757
static bool vboxGetMaxPortSlotValues(IVirtualBox *vbox,
                                     PRUint32 *maxPortPerInst,
758 759
                                     PRUint32 *maxSlotPerPort)
{
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
    ISystemProperties *sysProps = NULL;

    if (!vbox)
        return false;

    vbox->vtbl->GetSystemProperties(vbox, &sysProps);

    if (!sysProps)
        return false;

    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
                                                 StorageBus_IDE,
                                                 &maxPortPerInst[StorageBus_IDE]);
    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
                                                 StorageBus_SATA,
                                                 &maxPortPerInst[StorageBus_SATA]);
    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
                                                 StorageBus_SCSI,
                                                 &maxPortPerInst[StorageBus_SCSI]);
    sysProps->vtbl->GetMaxPortCountForStorageBus(sysProps,
                                                 StorageBus_Floppy,
                                                 &maxPortPerInst[StorageBus_Floppy]);

    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
                                                      StorageBus_IDE,
                                                      &maxSlotPerPort[StorageBus_IDE]);
    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
                                                      StorageBus_SATA,
                                                      &maxSlotPerPort[StorageBus_SATA]);
    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
                                                      StorageBus_SCSI,
                                                      &maxSlotPerPort[StorageBus_SCSI]);
    sysProps->vtbl->GetMaxDevicesPerPortForStorageBus(sysProps,
                                                      StorageBus_Floppy,
                                                      &maxSlotPerPort[StorageBus_Floppy]);

    VBOX_RELEASE(sysProps);

    return true;
}
800
# endif /* VBOX_API_VERSION < 4000000 */
801 802 803 804

/**
 * Converts Utf-16 string to int
 */
805 806
static int PRUnicharToInt(PRUnichar *strUtf16)
{
807 808 809 810 811 812 813 814 815 816
    char *strUtf8 = NULL;
    int ret = 0;

    if (!strUtf16)
        return -1;

    g_pVBoxGlobalData->pFuncs->pfnUtf16ToUtf8(strUtf16, &strUtf8);
    if (!strUtf8)
        return -1;

817 818 819
    if (virStrToLong_i(strUtf8, NULL, 10, &ret) < 0)
        ret = -1;

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    g_pVBoxGlobalData->pFuncs->pfnUtf8Free(strUtf8);

    return ret;
}

/**
 * Converts int to Utf-16 string
 */
static PRUnichar *PRUnicharFromInt(int n) {
    PRUnichar *strUtf16 = NULL;
    char s[24];

    snprintf(s, sizeof(s), "%d", n);

    g_pVBoxGlobalData->pFuncs->pfnUtf8ToUtf16(s, &strUtf16);

    return strUtf16;
}

T
Taowei 已提交
839
#endif /* VBOX_API_VERSION >= 3001000 */
840

841 842 843 844 845 846
static PRUnichar *
vboxSocketFormatAddrUtf16(vboxGlobalData *data, virSocketAddrPtr addr)
{
    char *utf8 = NULL;
    PRUnichar *utf16 = NULL;

847
    utf8 = virSocketAddrFormat(addr);
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

    if (utf8 == NULL) {
        return NULL;
    }

    VBOX_UTF8_TO_UTF16(utf8, &utf16);
    VIR_FREE(utf8);

    return utf16;
}

static int
vboxSocketParseAddrUtf16(vboxGlobalData *data, const PRUnichar *utf16,
                         virSocketAddrPtr addr)
{
    int result = -1;
    char *utf8 = NULL;

    VBOX_UTF16_TO_UTF8(utf16, &utf8);

868
    if (virSocketAddrParse(addr, utf8, AF_UNSPEC) < 0) {
869 870 871 872 873
        goto cleanup;
    }

    result = 0;

874
 cleanup:
875 876 877 878 879
    VBOX_UTF8_FREE(utf8);

    return result;
}

T
Taowei 已提交
880
static virDomainState _vboxConvertState(PRUint32 state)
881
{
882 883 884 885 886 887 888 889 890 891
    switch (state) {
        case MachineState_Running:
            return VIR_DOMAIN_RUNNING;
        case MachineState_Stuck:
            return VIR_DOMAIN_BLOCKED;
        case MachineState_Paused:
            return VIR_DOMAIN_PAUSED;
        case MachineState_Stopping:
            return VIR_DOMAIN_SHUTDOWN;
        case MachineState_PoweredOff:
R
Ryota Ozaki 已提交
892
        case MachineState_Saved:
893 894 895 896 897 898 899 900 901
            return VIR_DOMAIN_SHUTOFF;
        case MachineState_Aborted:
            return VIR_DOMAIN_CRASHED;
        case MachineState_Null:
        default:
            return VIR_DOMAIN_NOSTATE;
    }
}

T
Taowei 已提交
902
#if VBOX_API_VERSION < 3001000
903

E
Eric Blake 已提交
904
static void
T
Taowei 已提交
905
_vboxAttachDrivesOld(virDomainDefPtr def, vboxGlobalData *data, IMachine *machine)
E
Eric Blake 已提交
906
{
907
    size_t i;
E
Eric Blake 已提交
908
    nsresult rc;
909

E
Eric Blake 已提交
910 911 912 913
    if (def->ndisks == 0)
        return;

    for (i = 0; i < def->ndisks; i++) {
914 915 916 917 918
        const char *src = virDomainDiskGetSource(def->disks[i]);
        int type = virDomainDiskGetType(def->disks[i]);
        int format = virDomainDiskGetFormat(def->disks[i]);

        VIR_DEBUG("disk(%zu) type:       %d", i, type);
919 920
        VIR_DEBUG("disk(%zu) device:     %d", i, def->disks[i]->device);
        VIR_DEBUG("disk(%zu) bus:        %d", i, def->disks[i]->bus);
921
        VIR_DEBUG("disk(%zu) src:        %s", i, src);
922
        VIR_DEBUG("disk(%zu) dst:        %s", i, def->disks[i]->dst);
923 924
        VIR_DEBUG("disk(%zu) driverName: %s", i,
                  virDomainDiskGetDriver(def->disks[i]));
925
        VIR_DEBUG("disk(%zu) driverType: %s", i,
926
                  virStorageFileFormatTypeToString(format));
927
        VIR_DEBUG("disk(%zu) cachemode:  %d", i, def->disks[i]->cachemode);
928
        VIR_DEBUG("disk(%zu) readonly:   %s", i, (def->disks[i]->src->readonly
E
Eric Blake 已提交
929
                                             ? "True" : "False"));
930
        VIR_DEBUG("disk(%zu) shared:     %s", i, (def->disks[i]->src->shared
E
Eric Blake 已提交
931 932 933
                                             ? "True" : "False"));

        if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
E
Eric Blake 已提交
934
            if (type == VIR_STORAGE_TYPE_FILE && src) {
E
Eric Blake 已提交
935 936 937 938 939 940 941
                IDVDDrive *dvdDrive = NULL;
                /* Currently CDROM/DVD Drive is always IDE
                 * Secondary Master so neglecting the following
                 * parameters:
                 *      def->disks[i]->bus
                 *      def->disks[i]->dst
                 */
942

E
Eric Blake 已提交
943 944 945 946
                machine->vtbl->GetDVDDrive(machine, &dvdDrive);
                if (dvdDrive) {
                    IDVDImage *dvdImage          = NULL;
                    PRUnichar *dvdfileUtf16      = NULL;
947 948
                    vboxIID dvduuid = VBOX_IID_INITIALIZER;
                    vboxIID dvdemptyuuid = VBOX_IID_INITIALIZER;
949

950
                    VBOX_UTF8_TO_UTF16(src, &dvdfileUtf16);
951

E
Eric Blake 已提交
952 953 954 955 956
                    data->vboxObj->vtbl->FindDVDImage(data->vboxObj,
                                                      dvdfileUtf16, &dvdImage);
                    if (!dvdImage) {
                        data->vboxObj->vtbl->OpenDVDImage(data->vboxObj,
                                                          dvdfileUtf16,
957
                                                          dvdemptyuuid.value,
E
Eric Blake 已提交
958 959 960 961
                                                          &dvdImage);
                    }
                    if (dvdImage) {
                        rc = dvdImage->vtbl->imedium.GetId((IMedium *)dvdImage,
962
                                                           &dvduuid.value);
E
Eric Blake 已提交
963
                        if (NS_FAILED(rc)) {
964 965 966
                            virReportError(VIR_ERR_INTERNAL_ERROR,
                                           _("can't get the uuid of the file to "
                                             "be attached to cdrom: %s, rc=%08x"),
967
                                           src, (unsigned)rc);
E
Eric Blake 已提交
968
                        } else {
969
                            rc = dvdDrive->vtbl->MountImage(dvdDrive, dvduuid.value);
E
Eric Blake 已提交
970
                            if (NS_FAILED(rc)) {
971 972
                                virReportError(VIR_ERR_INTERNAL_ERROR,
                                               _("could not attach the file to cdrom: %s, rc=%08x"),
973
                                               src, (unsigned)rc);
E
Eric Blake 已提交
974
                            } else {
975
                                DEBUGIID("CD/DVDImage UUID:", dvduuid.value);
976
                            }
977
                        }
E
Eric Blake 已提交
978 979

                        VBOX_MEDIUM_RELEASE(dvdImage);
980
                    }
981
                    vboxIIDUnalloc(&dvduuid);
E
Eric Blake 已提交
982 983 984
                    VBOX_UTF16_FREE(dvdfileUtf16);
                    VBOX_RELEASE(dvdDrive);
                }
E
Eric Blake 已提交
985
            } else if (type == VIR_STORAGE_TYPE_BLOCK) {
E
Eric Blake 已提交
986 987
            }
        } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
E
Eric Blake 已提交
988
            if (type == VIR_STORAGE_TYPE_FILE && src) {
E
Eric Blake 已提交
989 990
                IHardDisk *hardDisk     = NULL;
                PRUnichar *hddfileUtf16 = NULL;
991
                vboxIID hdduuid = VBOX_IID_INITIALIZER;
E
Eric Blake 已提交
992 993 994 995 996 997
                PRUnichar *hddEmpty     = NULL;
                /* Current Limitation: Harddisk can't be connected to
                 * Secondary Master as Secondary Master is always used
                 * for CD/DVD Drive, so don't connect the harddisk if it
                 * is requested to be connected to Secondary master
                 */
998

999
                VBOX_UTF8_TO_UTF16(src, &hddfileUtf16);
E
Eric Blake 已提交
1000
                VBOX_UTF8_TO_UTF16("", &hddEmpty);
1001

E
Eric Blake 已提交
1002 1003
                data->vboxObj->vtbl->FindHardDisk(data->vboxObj, hddfileUtf16,
                                                  &hardDisk);
1004

E
Eric Blake 已提交
1005
                if (!hardDisk) {
1006
# if VBOX_API_VERSION == 2002000
E
Eric Blake 已提交
1007 1008 1009 1010
                    data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
                                                      hddfileUtf16,
                                                      AccessMode_ReadWrite,
                                                      &hardDisk);
1011
# else
E
Eric Blake 已提交
1012 1013 1014 1015 1016 1017 1018 1019
                    data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
                                                      hddfileUtf16,
                                                      AccessMode_ReadWrite,
                                                      0,
                                                      hddEmpty,
                                                      0,
                                                      hddEmpty,
                                                      &hardDisk);
1020
# endif
E
Eric Blake 已提交
1021
                }
1022

E
Eric Blake 已提交
1023 1024
                if (hardDisk) {
                    rc = hardDisk->vtbl->imedium.GetId((IMedium *)hardDisk,
1025
                                                       &hdduuid.value);
E
Eric Blake 已提交
1026
                    if (NS_FAILED(rc)) {
1027 1028 1029
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("can't get the uuid of the file to be "
                                         "attached as harddisk: %s, rc=%08x"),
1030
                                       src, (unsigned)rc);
E
Eric Blake 已提交
1031
                    } else {
1032
                        if (def->disks[i]->src->readonly) {
E
Eric Blake 已提交
1033 1034
                            hardDisk->vtbl->SetType(hardDisk,
                                                    HardDiskType_Immutable);
1035
                            VIR_DEBUG("setting harddisk to readonly");
1036
                        } else if (!def->disks[i]->src->readonly) {
E
Eric Blake 已提交
1037 1038
                            hardDisk->vtbl->SetType(hardDisk,
                                                    HardDiskType_Normal);
1039
                            VIR_DEBUG("setting harddisk type to normal");
E
Eric Blake 已提交
1040 1041 1042
                        }
                        if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_IDE) {
                            if (STREQ(def->disks[i]->dst, "hdc")) {
1043
                                VIR_DEBUG("Not connecting harddisk to hdc as hdc"
E
Eric Blake 已提交
1044
                                       " is taken by CD/DVD Drive");
1045
                            } else {
E
Eric Blake 已提交
1046 1047 1048 1049
                                PRInt32 channel          = 0;
                                PRInt32 device           = 0;
                                PRUnichar *hddcnameUtf16 = NULL;

1050 1051
                                char *hddcname;
                                ignore_value(VIR_STRDUP(hddcname, "IDE"));
E
Eric Blake 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
                                VBOX_UTF8_TO_UTF16(hddcname, &hddcnameUtf16);
                                VIR_FREE(hddcname);

                                if (STREQ(def->disks[i]->dst, "hda")) {
                                    channel = 0;
                                    device  = 0;
                                } else if (STREQ(def->disks[i]->dst, "hdb")) {
                                    channel = 0;
                                    device  = 1;
                                } else if (STREQ(def->disks[i]->dst, "hdd")) {
                                    channel = 1;
                                    device  = 1;
1064
                                }
E
Eric Blake 已提交
1065 1066

                                rc = machine->vtbl->AttachHardDisk(machine,
1067
                                                                   hdduuid.value,
E
Eric Blake 已提交
1068 1069 1070 1071 1072 1073
                                                                   hddcnameUtf16,
                                                                   channel,
                                                                   device);
                                VBOX_UTF16_FREE(hddcnameUtf16);

                                if (NS_FAILED(rc)) {
1074 1075 1076
                                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                                   _("could not attach the file as "
                                                     "harddisk: %s, rc=%08x"),
1077
                                                   src, (unsigned)rc);
E
Eric Blake 已提交
1078
                                } else {
1079
                                    DEBUGIID("Attached HDD with UUID", hdduuid.value);
1080 1081 1082
                                }
                            }
                        }
1083
                    }
E
Eric Blake 已提交
1084 1085
                    VBOX_MEDIUM_RELEASE(hardDisk);
                }
1086
                vboxIIDUnalloc(&hdduuid);
E
Eric Blake 已提交
1087 1088
                VBOX_UTF16_FREE(hddEmpty);
                VBOX_UTF16_FREE(hddfileUtf16);
E
Eric Blake 已提交
1089
            } else if (type == VIR_STORAGE_TYPE_BLOCK) {
E
Eric Blake 已提交
1090 1091
            }
        } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
E
Eric Blake 已提交
1092
            if (type == VIR_STORAGE_TYPE_FILE && src) {
E
Eric Blake 已提交
1093 1094 1095 1096 1097 1098 1099
                IFloppyDrive *floppyDrive;
                machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
                if (floppyDrive) {
                    rc = floppyDrive->vtbl->SetEnabled(floppyDrive, 1);
                    if (NS_SUCCEEDED(rc)) {
                        IFloppyImage *floppyImage   = NULL;
                        PRUnichar *fdfileUtf16      = NULL;
1100 1101
                        vboxIID fduuid = VBOX_IID_INITIALIZER;
                        vboxIID fdemptyuuid = VBOX_IID_INITIALIZER;
1102

1103
                        VBOX_UTF8_TO_UTF16(src, &fdfileUtf16);
E
Eric Blake 已提交
1104 1105 1106
                        rc = data->vboxObj->vtbl->FindFloppyImage(data->vboxObj,
                                                                  fdfileUtf16,
                                                                  &floppyImage);
1107

E
Eric Blake 已提交
1108 1109 1110
                        if (!floppyImage) {
                            data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
                                                                 fdfileUtf16,
1111
                                                                 fdemptyuuid.value,
E
Eric Blake 已提交
1112 1113
                                                                 &floppyImage);
                        }
1114

E
Eric Blake 已提交
1115 1116
                        if (floppyImage) {
                            rc = floppyImage->vtbl->imedium.GetId((IMedium *)floppyImage,
1117
                                                                  &fduuid.value);
E
Eric Blake 已提交
1118
                            if (NS_FAILED(rc)) {
1119 1120 1121
                                virReportError(VIR_ERR_INTERNAL_ERROR,
                                               _("can't get the uuid of the file to "
                                                 "be attached to floppy drive: %s, rc=%08x"),
1122
                                               src, (unsigned)rc);
E
Eric Blake 已提交
1123 1124
                            } else {
                                rc = floppyDrive->vtbl->MountImage(floppyDrive,
1125
                                                                   fduuid.value);
E
Eric Blake 已提交
1126
                                if (NS_FAILED(rc)) {
1127 1128 1129
                                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                                   _("could not attach the file to "
                                                     "floppy drive: %s, rc=%08x"),
1130
                                                   src, (unsigned)rc);
E
Eric Blake 已提交
1131
                                } else {
1132
                                    DEBUGIID("floppyImage UUID", fduuid.value);
1133 1134
                                }
                            }
E
Eric Blake 已提交
1135
                            VBOX_MEDIUM_RELEASE(floppyImage);
1136
                        }
1137
                        vboxIIDUnalloc(&fduuid);
E
Eric Blake 已提交
1138
                        VBOX_UTF16_FREE(fdfileUtf16);
1139
                    }
E
Eric Blake 已提交
1140
                    VBOX_RELEASE(floppyDrive);
1141
                }
E
Eric Blake 已提交
1142
            } else if (type == VIR_STORAGE_TYPE_BLOCK) {
1143
            }
1144
        }
E
Eric Blake 已提交
1145
    }
T
Taowei 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
}

#elif VBOX_API_VERSION < 4000000

static void
_vboxAttachDrivesOld(virDomainDefPtr def, vboxGlobalData *data, IMachine *machine)
{
    size_t i;
    nsresult rc;

E
Eric Blake 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    PRUint32 maxPortPerInst[StorageBus_Floppy + 1] = {};
    PRUint32 maxSlotPerPort[StorageBus_Floppy + 1] = {};
    PRUnichar *storageCtlName = NULL;
    bool error = false;

    /* get the max port/slots/etc for the given storage bus */
    error = !vboxGetMaxPortSlotValues(data->vboxObj, maxPortPerInst,
                                      maxSlotPerPort);

    /* add a storage controller for the mediums to be attached */
    /* this needs to change when multiple controller are supported for
     * ver > 3.1 */
1168
    {
E
Eric Blake 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
        IStorageController *storageCtl = NULL;
        PRUnichar *sName = NULL;

        VBOX_UTF8_TO_UTF16("IDE Controller", &sName);
        machine->vtbl->AddStorageController(machine,
                                            sName,
                                            StorageBus_IDE,
                                            &storageCtl);
        VBOX_UTF16_FREE(sName);
        VBOX_RELEASE(storageCtl);

        VBOX_UTF8_TO_UTF16("SATA Controller", &sName);
        machine->vtbl->AddStorageController(machine,
                                            sName,
                                            StorageBus_SATA,
                                            &storageCtl);
        VBOX_UTF16_FREE(sName);
        VBOX_RELEASE(storageCtl);

        VBOX_UTF8_TO_UTF16("SCSI Controller", &sName);
        machine->vtbl->AddStorageController(machine,
                                            sName,
                                            StorageBus_SCSI,
                                            &storageCtl);
        VBOX_UTF16_FREE(sName);
        VBOX_RELEASE(storageCtl);

        VBOX_UTF8_TO_UTF16("Floppy Controller", &sName);
        machine->vtbl->AddStorageController(machine,
                                            sName,
                                            StorageBus_Floppy,
                                            &storageCtl);
        VBOX_UTF16_FREE(sName);
        VBOX_RELEASE(storageCtl);
    }
1204

E
Eric Blake 已提交
1205
    for (i = 0; i < def->ndisks && !error; i++) {
1206 1207 1208 1209 1210
        const char *src = virDomainDiskGetSource(def->disks[i]);
        int type = virDomainDiskGetType(def->disks[i]);
        int format = virDomainDiskGetFormat(def->disks[i]);

        VIR_DEBUG("disk(%zu) type:       %d", i, type);
1211 1212
        VIR_DEBUG("disk(%zu) device:     %d", i, def->disks[i]->device);
        VIR_DEBUG("disk(%zu) bus:        %d", i, def->disks[i]->bus);
1213
        VIR_DEBUG("disk(%zu) src:        %s", i, src);
1214
        VIR_DEBUG("disk(%zu) dst:        %s", i, def->disks[i]->dst);
1215 1216
        VIR_DEBUG("disk(%zu) driverName: %s", i,
                  virDomainDiskGetDriver(def->disks[i]));
1217
        VIR_DEBUG("disk(%zu) driverType: %s", i,
1218
                  virStorageFileFormatTypeToString(format));
1219
        VIR_DEBUG("disk(%zu) cachemode:  %d", i, def->disks[i]->cachemode);
1220
        VIR_DEBUG("disk(%zu) readonly:   %s", i, (def->disks[i]->src->readonly
E
Eric Blake 已提交
1221
                                             ? "True" : "False"));
1222
        VIR_DEBUG("disk(%zu) shared:     %s", i, (def->disks[i]->src->shared
E
Eric Blake 已提交
1223 1224
                                             ? "True" : "False"));

E
Eric Blake 已提交
1225
        if (type == VIR_STORAGE_TYPE_FILE && src) {
E
Eric Blake 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234
            IMedium   *medium          = NULL;
            PRUnichar *mediumUUID      = NULL;
            PRUnichar *mediumFileUtf16 = NULL;
            PRUint32   storageBus      = StorageBus_Null;
            PRUint32   deviceType      = DeviceType_Null;
            PRInt32    deviceInst      = 0;
            PRInt32    devicePort      = 0;
            PRInt32    deviceSlot      = 0;

1235
            VBOX_UTF8_TO_UTF16(src, &mediumFileUtf16);
E
Eric Blake 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

            if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
                deviceType = DeviceType_HardDisk;
                data->vboxObj->vtbl->FindHardDisk(data->vboxObj,
                                                  mediumFileUtf16, &medium);
            } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
                deviceType = DeviceType_DVD;
                data->vboxObj->vtbl->FindDVDImage(data->vboxObj,
                                                  mediumFileUtf16, &medium);
            } else if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
                deviceType = DeviceType_Floppy;
                data->vboxObj->vtbl->FindFloppyImage(data->vboxObj,
                                                     mediumFileUtf16, &medium);
            } else {
                VBOX_UTF16_FREE(mediumFileUtf16);
                continue;
            }
1253

E
Eric Blake 已提交
1254 1255
            if (!medium) {
                PRUnichar *mediumEmpty = NULL;
1256

E
Eric Blake 已提交
1257
                VBOX_UTF8_TO_UTF16("", &mediumEmpty);
1258 1259

                if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
1260 1261 1262 1263 1264 1265 1266 1267
                    rc = data->vboxObj->vtbl->OpenHardDisk(data->vboxObj,
                                                           mediumFileUtf16,
                                                           AccessMode_ReadWrite,
                                                           false,
                                                           mediumEmpty,
                                                           false,
                                                           mediumEmpty,
                                                           &medium);
E
Eric Blake 已提交
1268 1269
                } else if (def->disks[i]->device ==
                           VIR_DOMAIN_DISK_DEVICE_CDROM) {
1270 1271 1272 1273
                    rc = data->vboxObj->vtbl->OpenDVDImage(data->vboxObj,
                                                           mediumFileUtf16,
                                                           mediumEmpty,
                                                           &medium);
E
Eric Blake 已提交
1274 1275
                } else if (def->disks[i]->device ==
                           VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
1276 1277 1278 1279 1280 1281
                    rc = data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
                                                              mediumFileUtf16,
                                                              mediumEmpty,
                                                              &medium);
                } else {
                    rc = 0;
1282
                }
E
Eric Blake 已提交
1283 1284
                VBOX_UTF16_FREE(mediumEmpty);
            }
1285

E
Eric Blake 已提交
1286
            if (!medium) {
1287 1288 1289
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Failed to attach the following disk/dvd/floppy "
                                 "to the machine: %s, rc=%08x"),
1290
                               src, (unsigned)rc);
E
Eric Blake 已提交
1291 1292 1293
                VBOX_UTF16_FREE(mediumFileUtf16);
                continue;
            }
1294

E
Eric Blake 已提交
1295 1296
            rc = medium->vtbl->GetId(medium, &mediumUUID);
            if (NS_FAILED(rc)) {
1297 1298 1299
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("can't get the uuid of the file to be attached "
                                 "as harddisk/dvd/floppy: %s, rc=%08x"),
1300
                               src, (unsigned)rc);
E
Eric Blake 已提交
1301 1302 1303 1304
                VBOX_RELEASE(medium);
                VBOX_UTF16_FREE(mediumFileUtf16);
                continue;
            }
1305

E
Eric Blake 已提交
1306
            if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
1307
                if (def->disks[i]->src->readonly) {
E
Eric Blake 已提交
1308
                    medium->vtbl->SetType(medium, MediumType_Immutable);
1309
                    VIR_DEBUG("setting harddisk to immutable");
1310
                } else if (!def->disks[i]->src->readonly) {
E
Eric Blake 已提交
1311
                    medium->vtbl->SetType(medium, MediumType_Normal);
1312
                    VIR_DEBUG("setting harddisk type to normal");
1313
                }
E
Eric Blake 已提交
1314
            }
1315

E
Eric Blake 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
            if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_IDE) {
                VBOX_UTF8_TO_UTF16("IDE Controller", &storageCtlName);
                storageBus = StorageBus_IDE;
            } else if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_SATA) {
                VBOX_UTF8_TO_UTF16("SATA Controller", &storageCtlName);
                storageBus = StorageBus_SATA;
            } else if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
                VBOX_UTF8_TO_UTF16("SCSI Controller", &storageCtlName);
                storageBus = StorageBus_SCSI;
            } else if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_FDC) {
                VBOX_UTF8_TO_UTF16("Floppy Controller", &storageCtlName);
                storageBus = StorageBus_Floppy;
            }
1329

E
Eric Blake 已提交
1330 1331 1332 1333 1334 1335 1336 1337
            /* get the device details i.e instance, port and slot */
            if (!vboxGetDeviceDetails(def->disks[i]->dst,
                                      maxPortPerInst,
                                      maxSlotPerPort,
                                      storageBus,
                                      &deviceInst,
                                      &devicePort,
                                      &deviceSlot)) {
1338
                virReportError(VIR_ERR_INTERNAL_ERROR,
1339 1340 1341
                               _("can't get the port/slot number of "
                                 "harddisk/dvd/floppy to be attached: "
                                 "%s, rc=%08x"),
1342
                               src, (unsigned)rc);
1343 1344 1345
                VBOX_RELEASE(medium);
                VBOX_UTF16_FREE(mediumUUID);
                VBOX_UTF16_FREE(mediumFileUtf16);
E
Eric Blake 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
                continue;
            }

            /* attach the harddisk/dvd/Floppy to the storage controller */
            rc = machine->vtbl->AttachDevice(machine,
                                             storageCtlName,
                                             devicePort,
                                             deviceSlot,
                                             deviceType,
                                             mediumUUID);

            if (NS_FAILED(rc)) {
1358
                virReportError(VIR_ERR_INTERNAL_ERROR,
1359 1360
                               _("could not attach the file as "
                                 "harddisk/dvd/floppy: %s, rc=%08x"),
1361
                               src, (unsigned)rc);
E
Eric Blake 已提交
1362 1363
            } else {
                DEBUGIID("Attached HDD/DVD/Floppy with UUID", mediumUUID);
1364
            }
E
Eric Blake 已提交
1365 1366 1367 1368 1369

            VBOX_RELEASE(medium);
            VBOX_UTF16_FREE(mediumUUID);
            VBOX_UTF16_FREE(mediumFileUtf16);
            VBOX_UTF16_FREE(storageCtlName);
1370 1371
        }
    }
E
Eric Blake 已提交
1372
}
1373

T
Taowei 已提交
1374
#else /* VBOX_API_VERSION >= 4000000 */
E
Eric Blake 已提交
1375 1376

static void
T
Taowei 已提交
1377 1378 1379
_vboxAttachDrivesOld(virDomainDefPtr def ATTRIBUTE_UNUSED,
                     vboxGlobalData *data ATTRIBUTE_UNUSED,
                     IMachine *machine ATTRIBUTE_UNUSED)
E
Eric Blake 已提交
1380
{
T
Taowei 已提交
1381 1382
    vboxUnsupported();
}
1383

1384
#endif /* VBOX_API_VERSION >= 4000000 */
E
Eric Blake 已提交
1385

1386
#if VBOX_API_VERSION < 3001000
J
Jiri Denemark 已提交
1387
static int
1388
_vboxDomainSnapshotRestore(virDomainPtr dom,
J
Jiri Denemark 已提交
1389 1390 1391 1392
                          IMachine *machine,
                          ISnapshot *snapshot)
{
    VBOX_OBJECT_CHECK(dom->conn, int, -1);
1393
    vboxIID iid = VBOX_IID_INITIALIZER;
J
Jiri Denemark 已提交
1394 1395
    nsresult rc;

1396 1397
    rc = snapshot->vtbl->GetId(snapshot, &iid.value);
    if (NS_FAILED(rc)) {
1398 1399
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not get snapshot UUID"));
J
Jiri Denemark 已提交
1400 1401 1402
        goto cleanup;
    }

1403
    rc = machine->vtbl->SetCurrentSnapshot(machine, iid.value);
J
Jiri Denemark 已提交
1404
    if (NS_FAILED(rc)) {
1405 1406
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("could not restore snapshot for domain %s"), dom->name);
J
Jiri Denemark 已提交
1407 1408 1409 1410 1411
        goto cleanup;
    }

    ret = 0;

1412
 cleanup:
1413
    vboxIIDUnalloc(&iid);
J
Jiri Denemark 已提交
1414 1415 1416 1417
    return ret;
}
#else
static int
1418
_vboxDomainSnapshotRestore(virDomainPtr dom,
J
Jiri Denemark 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427
                          IMachine *machine,
                          ISnapshot *snapshot)
{
    VBOX_OBJECT_CHECK(dom->conn, int, -1);
    IConsole *console = NULL;
    IProgress *progress = NULL;
    PRUint32 state;
    nsresult rc;
    PRInt32 result;
1428
    vboxIID domiid = VBOX_IID_INITIALIZER;
J
Jiri Denemark 已提交
1429

1430 1431
    rc = machine->vtbl->GetId(machine, &domiid.value);
    if (NS_FAILED(rc)) {
1432 1433
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not get domain UUID"));
J
Jiri Denemark 已提交
1434 1435 1436 1437 1438
        goto cleanup;
    }

    rc = machine->vtbl->GetState(machine, &state);
    if (NS_FAILED(rc)) {
1439 1440
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("could not get domain state"));
J
Jiri Denemark 已提交
1441 1442 1443 1444 1445
        goto cleanup;
    }

    if (state >= MachineState_FirstOnline
        && state <= MachineState_LastOnline) {
1446 1447
        virReportError(VIR_ERR_OPERATION_INVALID,
                       _("domain %s is already running"), dom->name);
J
Jiri Denemark 已提交
1448 1449 1450
        goto cleanup;
    }

1451
    rc = VBOX_SESSION_OPEN(domiid.value, machine);
J
Jiri Denemark 已提交
1452 1453 1454
    if (NS_SUCCEEDED(rc))
        rc = data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
    if (NS_FAILED(rc)) {
1455 1456 1457
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("could not open VirtualBox session with domain %s"),
                       dom->name);
J
Jiri Denemark 已提交
1458 1459 1460 1461 1462 1463
        goto cleanup;
    }

    rc = console->vtbl->RestoreSnapshot(console, snapshot, &progress);
    if (NS_FAILED(rc) || !progress) {
        if (rc == VBOX_E_INVALID_VM_STATE) {
1464 1465
            virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                           _("cannot restore domain snapshot for running domain"));
J
Jiri Denemark 已提交
1466
        } else {
1467 1468 1469
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("could not restore snapshot for domain %s"),
                           dom->name);
J
Jiri Denemark 已提交
1470 1471 1472 1473 1474 1475 1476
        }
        goto cleanup;
    }

    progress->vtbl->WaitForCompletion(progress, -1);
    progress->vtbl->GetResultCode(progress, &result);
    if (NS_FAILED(result)) {
1477 1478
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("could not restore snapshot for domain %s"), dom->name);
J
Jiri Denemark 已提交
1479 1480 1481 1482 1483
        goto cleanup;
    }

    ret = 0;

1484
 cleanup:
J
Jiri Denemark 已提交
1485 1486
    VBOX_RELEASE(progress);
    VBOX_RELEASE(console);
1487
    VBOX_SESSION_CLOSE();
1488
    vboxIIDUnalloc(&domiid);
J
Jiri Denemark 已提交
1489 1490 1491 1492
    return ret;
}
#endif

1493
#if VBOX_API_VERSION <= 2002000 || VBOX_API_VERSION >= 4000000
1494
    /* No Callback support for VirtualBox 2.2.* series */
1495
    /* No Callback support for VirtualBox 4.* series */
T
Taowei 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505

static void
_registerDomainEvent(virDriverPtr driver)
{
    driver->connectDomainEventRegister = NULL;
    driver->connectDomainEventDeregister = NULL;
    driver->connectDomainEventRegisterAny = NULL;
    driver->connectDomainEventDeregisterAny = NULL;
}

1506
#else /* !(VBOX_API_VERSION == 2002000 || VBOX_API_VERSION >= 4000000) */
1507 1508

/* Functions needed for Callbacks */
1509
static nsresult PR_COM_METHOD
1510
vboxCallbackOnMachineStateChange(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
1511 1512
                                 PRUnichar *machineId, PRUint32 state)
{
1513 1514 1515 1516 1517 1518
    virDomainPtr dom = NULL;
    int event        = 0;
    int detail       = 0;

    vboxDriverLock(g_pVBoxGlobalData);

1519
    VIR_DEBUG("IVirtualBoxCallback: %p, State: %d", pThis, state);
1520 1521 1522 1523 1524 1525 1526
    DEBUGPRUnichar("machineId", machineId);

    if (machineId) {
        char *machineIdUtf8       = NULL;
        unsigned char uuid[VIR_UUID_BUFLEN];

        g_pVBoxGlobalData->pFuncs->pfnUtf16ToUtf8(machineId, &machineIdUtf8);
1527
        ignore_value(virUUIDParse(machineIdUtf8, uuid));
1528 1529 1530

        dom = vboxDomainLookupByUUID(g_pVBoxGlobalData->conn, uuid);
        if (dom) {
1531
            virObjectEventPtr ev;
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

            if (state == MachineState_Starting) {
                event  = VIR_DOMAIN_EVENT_STARTED;
                detail = VIR_DOMAIN_EVENT_STARTED_BOOTED;
            } else if (state == MachineState_Restoring) {
                event  = VIR_DOMAIN_EVENT_STARTED;
                detail = VIR_DOMAIN_EVENT_STARTED_RESTORED;
            } else if (state == MachineState_Paused) {
                event  = VIR_DOMAIN_EVENT_SUSPENDED;
                detail = VIR_DOMAIN_EVENT_SUSPENDED_PAUSED;
            } else if (state == MachineState_Running) {
                event  = VIR_DOMAIN_EVENT_RESUMED;
                detail = VIR_DOMAIN_EVENT_RESUMED_UNPAUSED;
            } else if (state == MachineState_PoweredOff) {
                event  = VIR_DOMAIN_EVENT_STOPPED;
                detail = VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN;
            } else if (state == MachineState_Stopping) {
                event  = VIR_DOMAIN_EVENT_STOPPED;
                detail = VIR_DOMAIN_EVENT_STOPPED_DESTROYED;
            } else if (state == MachineState_Aborted) {
                event  = VIR_DOMAIN_EVENT_STOPPED;
                detail = VIR_DOMAIN_EVENT_STOPPED_CRASHED;
            } else if (state == MachineState_Saving) {
                event  = VIR_DOMAIN_EVENT_STOPPED;
                detail = VIR_DOMAIN_EVENT_STOPPED_SAVED;
            } else {
                event  = VIR_DOMAIN_EVENT_STOPPED;
                detail = VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN;
            }

1562
            ev = virDomainEventLifecycleNewFromDom(dom, event, detail);
1563

1564
            if (ev)
1565
                virObjectEventStateQueue(g_pVBoxGlobalData->domainEvents, ev);
1566 1567 1568 1569 1570 1571 1572 1573
        }
    }

    vboxDriverUnlock(g_pVBoxGlobalData);

    return NS_OK;
}

1574
static nsresult PR_COM_METHOD
1575
vboxCallbackOnMachineDataChange(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
1576 1577
                                PRUnichar *machineId)
{
1578
    VIR_DEBUG("IVirtualBoxCallback: %p", pThis);
1579 1580 1581 1582 1583
    DEBUGPRUnichar("machineId", machineId);

    return NS_OK;
}

1584
static nsresult PR_COM_METHOD
1585
vboxCallbackOnExtraDataCanChange(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
1586 1587 1588
                                 PRUnichar *machineId, PRUnichar *key,
                                 PRUnichar *value,
                                 PRUnichar **error ATTRIBUTE_UNUSED,
1589
                                 PRBool *allowChange ATTRIBUTE_UNUSED)
1590
{
1591
    VIR_DEBUG("IVirtualBoxCallback: %p, allowChange: %s", pThis, *allowChange ? "true" : "false");
1592 1593 1594 1595 1596 1597 1598
    DEBUGPRUnichar("machineId", machineId);
    DEBUGPRUnichar("key", key);
    DEBUGPRUnichar("value", value);

    return NS_OK;
}

1599
static nsresult PR_COM_METHOD
1600 1601
vboxCallbackOnExtraDataChange(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
                              PRUnichar *machineId,
1602 1603
                              PRUnichar *key, PRUnichar *value)
{
1604
    VIR_DEBUG("IVirtualBoxCallback: %p", pThis);
1605 1606 1607 1608 1609 1610 1611
    DEBUGPRUnichar("machineId", machineId);
    DEBUGPRUnichar("key", key);
    DEBUGPRUnichar("value", value);

    return NS_OK;
}

1612
# if VBOX_API_VERSION < 3001000
1613
static nsresult PR_COM_METHOD
1614 1615 1616 1617
vboxCallbackOnMediaRegistered(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
                              PRUnichar *mediaId,
                              PRUint32 mediaType ATTRIBUTE_UNUSED,
                              PRBool registered ATTRIBUTE_UNUSED)
1618
{
1619 1620
    VIR_DEBUG("IVirtualBoxCallback: %p, registered: %s", pThis, registered ? "true" : "false");
    VIR_DEBUG("mediaType: %d", mediaType);
1621 1622 1623 1624
    DEBUGPRUnichar("mediaId", mediaId);

    return NS_OK;
}
1625 1626
# else  /* VBOX_API_VERSION >= 3001000 */
# endif /* VBOX_API_VERSION >= 3001000 */
1627

1628
static nsresult PR_COM_METHOD
1629
vboxCallbackOnMachineRegistered(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
1630 1631
                                PRUnichar *machineId, PRBool registered)
{
1632 1633 1634 1635 1636 1637
    virDomainPtr dom = NULL;
    int event        = 0;
    int detail       = 0;

    vboxDriverLock(g_pVBoxGlobalData);

1638
    VIR_DEBUG("IVirtualBoxCallback: %p, registered: %s", pThis, registered ? "true" : "false");
1639 1640 1641 1642 1643 1644 1645
    DEBUGPRUnichar("machineId", machineId);

    if (machineId) {
        char *machineIdUtf8       = NULL;
        unsigned char uuid[VIR_UUID_BUFLEN];

        g_pVBoxGlobalData->pFuncs->pfnUtf16ToUtf8(machineId, &machineIdUtf8);
1646
        ignore_value(virUUIDParse(machineIdUtf8, uuid));
1647 1648 1649

        dom = vboxDomainLookupByUUID(g_pVBoxGlobalData->conn, uuid);
        if (dom) {
1650
            virObjectEventPtr ev;
1651 1652

            /* CURRENT LIMITATION: we never get the VIR_DOMAIN_EVENT_UNDEFINED
J
Ján Tomko 已提交
1653
             * event because the when the machine is de-registered the call
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
             * to vboxDomainLookupByUUID fails and thus we don't get any
             * dom pointer which is necessary (null dom pointer doesn't work)
             * to show the VIR_DOMAIN_EVENT_UNDEFINED event
             */
            if (registered) {
                event  = VIR_DOMAIN_EVENT_DEFINED;
                detail = VIR_DOMAIN_EVENT_DEFINED_ADDED;
            } else {
                event  = VIR_DOMAIN_EVENT_UNDEFINED;
                detail = VIR_DOMAIN_EVENT_UNDEFINED_REMOVED;
            }

1666
            ev = virDomainEventLifecycleNewFromDom(dom, event, detail);
1667

1668
            if (ev)
1669
                virObjectEventStateQueue(g_pVBoxGlobalData->domainEvents, ev);
1670 1671 1672 1673 1674 1675 1676 1677
        }
    }

    vboxDriverUnlock(g_pVBoxGlobalData);

    return NS_OK;
}

1678
static nsresult PR_COM_METHOD
1679 1680 1681
vboxCallbackOnSessionStateChange(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
                                 PRUnichar *machineId,
                                 PRUint32 state ATTRIBUTE_UNUSED)
1682
{
1683
    VIR_DEBUG("IVirtualBoxCallback: %p, state: %d", pThis, state);
1684 1685 1686 1687 1688
    DEBUGPRUnichar("machineId", machineId);

    return NS_OK;
}

1689
static nsresult PR_COM_METHOD
1690 1691
vboxCallbackOnSnapshotTaken(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
                            PRUnichar *machineId,
1692 1693
                            PRUnichar *snapshotId)
{
1694
    VIR_DEBUG("IVirtualBoxCallback: %p", pThis);
1695 1696 1697 1698 1699 1700
    DEBUGPRUnichar("machineId", machineId);
    DEBUGPRUnichar("snapshotId", snapshotId);

    return NS_OK;
}

1701
static nsresult PR_COM_METHOD
1702 1703
vboxCallbackOnSnapshotDiscarded(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
                                PRUnichar *machineId,
1704 1705
                                PRUnichar *snapshotId)
{
1706
    VIR_DEBUG("IVirtualBoxCallback: %p", pThis);
1707 1708 1709 1710 1711 1712
    DEBUGPRUnichar("machineId", machineId);
    DEBUGPRUnichar("snapshotId", snapshotId);

    return NS_OK;
}

1713
static nsresult PR_COM_METHOD
1714 1715
vboxCallbackOnSnapshotChange(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
                             PRUnichar *machineId,
1716 1717
                             PRUnichar *snapshotId)
{
1718
    VIR_DEBUG("IVirtualBoxCallback: %p", pThis);
1719 1720 1721 1722 1723 1724
    DEBUGPRUnichar("machineId", machineId);
    DEBUGPRUnichar("snapshotId", snapshotId);

    return NS_OK;
}

1725
static nsresult PR_COM_METHOD
1726
vboxCallbackOnGuestPropertyChange(IVirtualBoxCallback *pThis ATTRIBUTE_UNUSED,
1727 1728 1729
                                  PRUnichar *machineId, PRUnichar *name,
                                  PRUnichar *value, PRUnichar *flags)
{
1730
    VIR_DEBUG("IVirtualBoxCallback: %p", pThis);
1731 1732 1733 1734 1735 1736 1737 1738
    DEBUGPRUnichar("machineId", machineId);
    DEBUGPRUnichar("name", name);
    DEBUGPRUnichar("value", value);
    DEBUGPRUnichar("flags", flags);

    return NS_OK;
}

1739
static nsresult PR_COM_METHOD
1740
vboxCallbackAddRef(nsISupports *pThis ATTRIBUTE_UNUSED)
1741
{
1742 1743 1744 1745
    nsresult c;

    c = ++g_pVBoxGlobalData->vboxCallBackRefCount;

1746
    VIR_DEBUG("pThis: %p, vboxCallback AddRef: %d", pThis, c);
1747 1748 1749 1750

    return c;
}

1751 1752 1753
static nsresult PR_COM_METHOD
vboxCallbackRelease(nsISupports *pThis)
{
1754 1755 1756 1757 1758 1759 1760 1761 1762
    nsresult c;

    c = --g_pVBoxGlobalData->vboxCallBackRefCount;
    if (c == 0) {
        /* delete object */
        VIR_FREE(pThis->vtbl);
        VIR_FREE(pThis);
    }

1763
    VIR_DEBUG("pThis: %p, vboxCallback Release: %d", pThis, c);
1764 1765 1766 1767

    return c;
}

1768 1769 1770
static nsresult PR_COM_METHOD
vboxCallbackQueryInterface(nsISupports *pThis, const nsID *iid, void **resultp)
{
1771 1772 1773 1774 1775
    IVirtualBoxCallback *that = (IVirtualBoxCallback *)pThis;
    static const nsID ivirtualboxCallbackUUID = IVIRTUALBOXCALLBACK_IID;
    static const nsID isupportIID = NS_ISUPPORTS_IID;

    /* Match UUID for IVirtualBoxCallback class */
1776 1777
    if (memcmp(iid, &ivirtualboxCallbackUUID, sizeof(nsID)) == 0 ||
        memcmp(iid, &isupportIID, sizeof(nsID)) == 0) {
1778 1779 1780
        g_pVBoxGlobalData->vboxCallBackRefCount++;
        *resultp = that;

1781
        VIR_DEBUG("pThis: %p, vboxCallback QueryInterface: %d", pThis, g_pVBoxGlobalData->vboxCallBackRefCount);
1782 1783 1784 1785 1786

        return NS_OK;
    }


1787
    VIR_DEBUG("pThis: %p, vboxCallback QueryInterface didn't find a matching interface", pThis);
1788 1789 1790 1791 1792 1793
    DEBUGUUID("The UUID Callback Interface expects", iid);
    DEBUGUUID("The UUID Callback Interface got", &ivirtualboxCallbackUUID);
    return NS_NOINTERFACE;
}


1794
static IVirtualBoxCallback *vboxAllocCallbackObj(void) {
1795 1796
    IVirtualBoxCallback *vboxCallback = NULL;

1797
    /* Allocate, Initialize and return a valid
1798 1799 1800
     * IVirtualBoxCallback object here
     */
    if ((VIR_ALLOC(vboxCallback) < 0) || (VIR_ALLOC(vboxCallback->vtbl) < 0)) {
1801
        VIR_FREE(vboxCallback);
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
        return NULL;
    }

    {
        vboxCallback->vtbl->nsisupports.AddRef          = &vboxCallbackAddRef;
        vboxCallback->vtbl->nsisupports.Release         = &vboxCallbackRelease;
        vboxCallback->vtbl->nsisupports.QueryInterface  = &vboxCallbackQueryInterface;
        vboxCallback->vtbl->OnMachineStateChange        = &vboxCallbackOnMachineStateChange;
        vboxCallback->vtbl->OnMachineDataChange         = &vboxCallbackOnMachineDataChange;
        vboxCallback->vtbl->OnExtraDataCanChange        = &vboxCallbackOnExtraDataCanChange;
        vboxCallback->vtbl->OnExtraDataChange           = &vboxCallbackOnExtraDataChange;
1813
# if VBOX_API_VERSION < 3001000
1814
        vboxCallback->vtbl->OnMediaRegistered           = &vboxCallbackOnMediaRegistered;
1815 1816
# else  /* VBOX_API_VERSION >= 3001000 */
# endif /* VBOX_API_VERSION >= 3001000 */
1817 1818 1819
        vboxCallback->vtbl->OnMachineRegistered         = &vboxCallbackOnMachineRegistered;
        vboxCallback->vtbl->OnSessionStateChange        = &vboxCallbackOnSessionStateChange;
        vboxCallback->vtbl->OnSnapshotTaken             = &vboxCallbackOnSnapshotTaken;
1820
# if VBOX_API_VERSION < 3002000
1821
        vboxCallback->vtbl->OnSnapshotDiscarded         = &vboxCallbackOnSnapshotDiscarded;
1822
# else /* VBOX_API_VERSION >= 3002000 */
1823
        vboxCallback->vtbl->OnSnapshotDeleted           = &vboxCallbackOnSnapshotDiscarded;
1824
# endif /* VBOX_API_VERSION >= 3002000 */
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
        vboxCallback->vtbl->OnSnapshotChange            = &vboxCallbackOnSnapshotChange;
        vboxCallback->vtbl->OnGuestPropertyChange       = &vboxCallbackOnGuestPropertyChange;
        g_pVBoxGlobalData->vboxCallBackRefCount = 1;

    }

    return vboxCallback;
}

static void vboxReadCallback(int watch ATTRIBUTE_UNUSED,
                             int fd,
                             int events ATTRIBUTE_UNUSED,
1837 1838
                             void *opaque ATTRIBUTE_UNUSED)
{
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
    if (fd >= 0) {
        g_pVBoxGlobalData->vboxQueue->vtbl->ProcessPendingEvents(g_pVBoxGlobalData->vboxQueue);
    } else {
        nsresult rc;
        PLEvent *pEvent = NULL;

        rc = g_pVBoxGlobalData->vboxQueue->vtbl->WaitForEvent(g_pVBoxGlobalData->vboxQueue, &pEvent);
        if (NS_SUCCEEDED(rc))
            g_pVBoxGlobalData->vboxQueue->vtbl->HandleEvent(g_pVBoxGlobalData->vboxQueue, pEvent);
    }
}

1851 1852 1853 1854 1855 1856
static int
vboxConnectDomainEventRegister(virConnectPtr conn,
                               virConnectDomainEventCallback callback,
                               void *opaque,
                               virFreeCallback freecb)
{
1857
    VBOX_OBJECT_CHECK(conn, int, -1);
1858
    int vboxRet          = -1;
1859
    nsresult rc;
1860 1861 1862 1863 1864 1865

    /* Locking has to be there as callbacks are not
     * really fully thread safe
     */
    vboxDriverLock(data);

1866
    if (data->vboxCallback == NULL) {
1867
        data->vboxCallback = vboxAllocCallbackObj();
1868 1869 1870 1871
        if (data->vboxCallback != NULL) {
            rc = data->vboxObj->vtbl->RegisterCallback(data->vboxObj, data->vboxCallback);
            if (NS_SUCCEEDED(rc)) {
                vboxRet = 0;
1872 1873
            }
        }
1874 1875 1876
    } else {
        vboxRet = 0;
    }
1877

1878 1879 1880 1881 1882 1883 1884
    /* Get the vbox file handle and add a event handle to it
     * so that the events can be passed down to the user
     */
    if (vboxRet == 0) {
        if (data->fdWatch < 0) {
            PRInt32 vboxFileHandle;
            vboxFileHandle = data->vboxQueue->vtbl->GetEventQueueSelectFD(data->vboxQueue);
1885

1886 1887
            data->fdWatch = virEventAddHandle(vboxFileHandle, VIR_EVENT_HANDLE_READABLE, vboxReadCallback, NULL, NULL);
        }
1888

1889 1890 1891 1892 1893
        if (data->fdWatch >= 0) {
            /* Once a callback is registered with virtualbox, use a list
             * to store the callbacks registered with libvirt so that
             * later you can iterate over them
             */
1894

1895
            ret = virDomainEventStateRegister(conn, data->domainEvents,
1896
                                              callback, opaque, freecb);
1897
            VIR_DEBUG("virObjectEventStateRegister (ret = %d) (conn: %p, "
1898
                      "callback: %p, opaque: %p, "
1899
                      "freecb: %p)", ret, conn, callback,
1900
                      opaque, freecb);
1901 1902 1903 1904 1905 1906
        }
    }

    vboxDriverUnlock(data);

    if (ret >= 0) {
1907
        return 0;
1908 1909 1910 1911 1912 1913 1914 1915
    } else {
        if (data->vboxObj && data->vboxCallback) {
            data->vboxObj->vtbl->UnregisterCallback(data->vboxObj, data->vboxCallback);
        }
        return -1;
    }
}

1916 1917 1918 1919
static int
vboxConnectDomainEventDeregister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback)
{
1920
    VBOX_OBJECT_CHECK(conn, int, -1);
1921
    int cnt;
1922 1923 1924 1925 1926 1927

    /* Locking has to be there as callbacks are not
     * really fully thread safe
     */
    vboxDriverLock(data);

1928 1929
    cnt = virDomainEventStateDeregister(conn, data->domainEvents,
                                        callback);
1930

1931 1932 1933
    if (data->vboxCallback && cnt == 0) {
        data->vboxObj->vtbl->UnregisterCallback(data->vboxObj, data->vboxCallback);
        VBOX_RELEASE(data->vboxCallback);
1934

1935 1936 1937
        /* Remove the Event file handle on which we are listening as well */
        virEventRemoveHandle(data->fdWatch);
        data->fdWatch = -1;
1938 1939 1940 1941
    }

    vboxDriverUnlock(data);

1942 1943 1944
    if (cnt >= 0)
        ret = 0;

1945 1946 1947
    return ret;
}

1948 1949 1950 1951 1952
static int vboxConnectDomainEventRegisterAny(virConnectPtr conn,
                                             virDomainPtr dom,
                                             int eventID,
                                             virConnectDomainEventGenericCallback callback,
                                             void *opaque,
1953 1954
                                             virFreeCallback freecb)
{
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
    VBOX_OBJECT_CHECK(conn, int, -1);
    int vboxRet          = -1;
    nsresult rc;

    /* Locking has to be there as callbacks are not
     * really fully thread safe
     */
    vboxDriverLock(data);

    if (data->vboxCallback == NULL) {
        data->vboxCallback = vboxAllocCallbackObj();
        if (data->vboxCallback != NULL) {
            rc = data->vboxObj->vtbl->RegisterCallback(data->vboxObj, data->vboxCallback);
            if (NS_SUCCEEDED(rc)) {
                vboxRet = 0;
            }
        }
    } else {
        vboxRet = 0;
    }

    /* Get the vbox file handle and add a event handle to it
     * so that the events can be passed down to the user
     */
    if (vboxRet == 0) {
        if (data->fdWatch < 0) {
            PRInt32 vboxFileHandle;
            vboxFileHandle = data->vboxQueue->vtbl->GetEventQueueSelectFD(data->vboxQueue);

            data->fdWatch = virEventAddHandle(vboxFileHandle, VIR_EVENT_HANDLE_READABLE, vboxReadCallback, NULL, NULL);
        }

        if (data->fdWatch >= 0) {
            /* Once a callback is registered with virtualbox, use a list
             * to store the callbacks registered with libvirt so that
             * later you can iterate over them
             */

1993
            if (virDomainEventStateRegisterID(conn, data->domainEvents,
1994 1995
                                              dom, eventID,
                                              callback, opaque, freecb, &ret) < 0)
1996
                ret = -1;
1997
            VIR_DEBUG("virDomainEventStateRegisterID (ret = %d) (conn: %p, "
1998
                      "callback: %p, opaque: %p, "
1999
                      "freecb: %p)", ret, conn, callback,
2000
                      opaque, freecb);
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
        }
    }

    vboxDriverUnlock(data);

    if (ret >= 0) {
        return ret;
    } else {
        if (data->vboxObj && data->vboxCallback) {
            data->vboxObj->vtbl->UnregisterCallback(data->vboxObj, data->vboxCallback);
        }
        return -1;
    }
}

2016 2017 2018 2019
static int
vboxConnectDomainEventDeregisterAny(virConnectPtr conn,
                                    int callbackID)
{
2020
    VBOX_OBJECT_CHECK(conn, int, -1);
2021
    int cnt;
2022 2023 2024 2025 2026 2027

    /* Locking has to be there as callbacks are not
     * really fully thread safe
     */
    vboxDriverLock(data);

2028
    cnt = virObjectEventStateDeregisterID(conn, data->domainEvents,
2029
                                          callbackID);
2030

2031 2032 2033
    if (data->vboxCallback && cnt == 0) {
        data->vboxObj->vtbl->UnregisterCallback(data->vboxObj, data->vboxCallback);
        VBOX_RELEASE(data->vboxCallback);
2034

2035 2036 2037
        /* Remove the Event file handle on which we are listening as well */
        virEventRemoveHandle(data->fdWatch);
        data->fdWatch = -1;
2038 2039 2040 2041
    }

    vboxDriverUnlock(data);

2042 2043 2044
    if (cnt >= 0)
        ret = 0;

2045 2046 2047
    return ret;
}

T
Taowei 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056
static void
_registerDomainEvent(virDriverPtr driver)
{
    driver->connectDomainEventRegister = vboxConnectDomainEventRegister; /* 0.7.0 */
    driver->connectDomainEventDeregister = vboxConnectDomainEventDeregister; /* 0.7.0 */
    driver->connectDomainEventRegisterAny = vboxConnectDomainEventRegisterAny; /* 0.8.0 */
    driver->connectDomainEventDeregisterAny = vboxConnectDomainEventDeregisterAny; /* 0.8.0 */
}

2057
#endif /* !(VBOX_API_VERSION == 2002000 || VBOX_API_VERSION >= 4000000) */
2058

2059 2060 2061 2062 2063
/**
 * The Network Functions here on
 */
static virDrvOpenStatus vboxNetworkOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
E
Eric Blake 已提交
2064 2065
                                        unsigned int flags)
{
2066 2067
    vboxGlobalData *data = conn->privateData;

E
Eric Blake 已提交
2068 2069
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2070 2071 2072 2073 2074 2075 2076 2077
    if (STRNEQ(conn->driver->name, "VBOX"))
        goto cleanup;

    if ((data->pFuncs      == NULL) ||
        (data->vboxObj     == NULL) ||
        (data->vboxSession == NULL))
        goto cleanup;

2078
    VIR_DEBUG("network initialized");
2079 2080 2081
    /* conn->networkPrivateData = some network specific data */
    return VIR_DRV_OPEN_SUCCESS;

2082
 cleanup:
2083 2084 2085
    return VIR_DRV_OPEN_DECLINED;
}

2086 2087
static int vboxNetworkClose(virConnectPtr conn)
{
2088
    VIR_DEBUG("network uninitialized");
2089 2090 2091 2092
    conn->networkPrivateData = NULL;
    return 0;
}

2093 2094
static int vboxConnectNumOfNetworks(virConnectPtr conn)
{
2095
    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
2096
    vboxArray networkInterfaces = VBOX_ARRAY_INITIALIZER;
2097
    size_t i = 0;
2098

2099
    vboxArrayGet(&networkInterfaces, host, host->vtbl->GetNetworkInterfaces);
2100

2101 2102 2103 2104
    for (i = 0; i < networkInterfaces.count; i++) {
        IHostNetworkInterface *networkInterface = networkInterfaces.items[i];

        if (networkInterface) {
2105
            PRUint32 interfaceType = 0;
2106

2107
            networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2108 2109
            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
2110

2111
                networkInterface->vtbl->GetStatus(networkInterface, &status);
2112

2113 2114
                if (status == HostNetworkInterfaceStatus_Up)
                    ret++;
2115 2116 2117 2118
            }
        }
    }

2119 2120
    vboxArrayRelease(&networkInterfaces);

2121 2122
    VBOX_RELEASE(host);

2123
    VIR_DEBUG("numActive: %d", ret);
2124
    return ret;
2125 2126
}

2127
static int vboxConnectListNetworks(virConnectPtr conn, char **const names, int nnames) {
2128
    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
2129
    vboxArray networkInterfaces = VBOX_ARRAY_INITIALIZER;
2130
    size_t i = 0;
2131

2132 2133 2134 2135
    vboxArrayGet(&networkInterfaces, host, host->vtbl->GetNetworkInterfaces);

    for (i = 0; (ret < nnames) && (i < networkInterfaces.count); i++) {
        IHostNetworkInterface *networkInterface = networkInterfaces.items[i];
2136

2137
        if (networkInterface) {
2138
            PRUint32 interfaceType = 0;
2139

2140
            networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2141

2142 2143
            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
2144

2145
                networkInterface->vtbl->GetStatus(networkInterface, &status);
2146

2147 2148 2149
                if (status == HostNetworkInterfaceStatus_Up) {
                    char *nameUtf8       = NULL;
                    PRUnichar *nameUtf16 = NULL;
2150

2151
                    networkInterface->vtbl->GetName(networkInterface, &nameUtf16);
2152
                    VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
2153

2154
                    VIR_DEBUG("nnames[%d]: %s", ret, nameUtf8);
J
Ján Tomko 已提交
2155
                    if (VIR_STRDUP(names[ret], nameUtf8) >= 0)
2156
                        ret++;
2157

2158 2159
                    VBOX_UTF8_FREE(nameUtf8);
                    VBOX_UTF16_FREE(nameUtf16);
2160 2161 2162 2163 2164
                }
            }
        }
    }

2165
    vboxArrayRelease(&networkInterfaces);
2166

2167
    VBOX_RELEASE(host);
2168

2169 2170
    return ret;
}
2171

2172 2173
static int vboxConnectNumOfDefinedNetworks(virConnectPtr conn)
{
2174
    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
2175
    vboxArray networkInterfaces = VBOX_ARRAY_INITIALIZER;
2176
    size_t i = 0;
2177

2178
    vboxArrayGet(&networkInterfaces, host, host->vtbl->GetNetworkInterfaces);
2179

2180 2181 2182 2183
    for (i = 0; i < networkInterfaces.count; i++) {
        IHostNetworkInterface *networkInterface = networkInterfaces.items[i];

        if (networkInterface) {
2184
            PRUint32 interfaceType = 0;
2185

2186
            networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2187 2188
            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
2189

2190
                networkInterface->vtbl->GetStatus(networkInterface, &status);
2191

2192 2193
                if (status == HostNetworkInterfaceStatus_Down)
                    ret++;
2194 2195 2196 2197
            }
        }
    }

2198 2199
    vboxArrayRelease(&networkInterfaces);

2200 2201
    VBOX_RELEASE(host);

2202
    VIR_DEBUG("numActive: %d", ret);
2203
    return ret;
2204 2205
}

2206
static int vboxConnectListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
2207
    VBOX_OBJECT_HOST_CHECK(conn, int, 0);
2208
    vboxArray networkInterfaces = VBOX_ARRAY_INITIALIZER;
2209
    size_t i = 0;
2210

2211 2212 2213 2214
    vboxArrayGet(&networkInterfaces, host, host->vtbl->GetNetworkInterfaces);

    for (i = 0; (ret < nnames) && (i < networkInterfaces.count); i++) {
        IHostNetworkInterface *networkInterface = networkInterfaces.items[i];
2215

2216
        if (networkInterface) {
2217
            PRUint32 interfaceType = 0;
2218

2219
            networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2220

2221 2222
            if (interfaceType == HostNetworkInterfaceType_HostOnly) {
                PRUint32 status = HostNetworkInterfaceStatus_Unknown;
2223

2224
                networkInterface->vtbl->GetStatus(networkInterface, &status);
2225

2226 2227 2228
                if (status == HostNetworkInterfaceStatus_Down) {
                    char *nameUtf8       = NULL;
                    PRUnichar *nameUtf16 = NULL;
2229

2230
                    networkInterface->vtbl->GetName(networkInterface, &nameUtf16);
2231
                    VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
2232

2233
                    VIR_DEBUG("nnames[%d]: %s", ret, nameUtf8);
J
Ján Tomko 已提交
2234
                    if (VIR_STRDUP(names[ret], nameUtf8) >= 0)
2235
                        ret++;
2236

2237 2238
                    VBOX_UTF8_FREE(nameUtf8);
                    VBOX_UTF16_FREE(nameUtf16);
2239 2240 2241 2242 2243
                }
            }
        }
    }

2244
    vboxArrayRelease(&networkInterfaces);
2245 2246 2247 2248

    VBOX_RELEASE(host);

    return ret;
2249 2250
}

2251 2252 2253
static virNetworkPtr
vboxNetworkLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
2254
    VBOX_OBJECT_HOST_CHECK(conn, virNetworkPtr, NULL);
2255
    vboxIID iid = VBOX_IID_INITIALIZER;
2256

2257
    vboxIIDFromUUID(&iid, uuid);
2258

2259 2260 2261
    /* TODO: "internal" networks are just strings and
     * thus can't do much with them
     */
2262
    IHostNetworkInterface *networkInterface = NULL;
2263

2264
    host->vtbl->FindHostNetworkInterfaceById(host, iid.value, &networkInterface);
2265 2266
    if (networkInterface) {
        PRUint32 interfaceType = 0;
2267

2268
        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2269

2270 2271 2272
        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
            char *nameUtf8       = NULL;
            PRUnichar *nameUtf16 = NULL;
2273

2274 2275
            networkInterface->vtbl->GetName(networkInterface, &nameUtf16);
            VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
2276

2277
            ret = virGetNetwork(conn, nameUtf8, uuid);
2278

2279
            VIR_DEBUG("Network Name: %s", nameUtf8);
2280
            DEBUGIID("Network UUID", iid.value);
2281

2282 2283
            VBOX_UTF8_FREE(nameUtf8);
            VBOX_UTF16_FREE(nameUtf16);
2284
        }
2285 2286

        VBOX_RELEASE(networkInterface);
2287 2288
    }

2289 2290
    VBOX_RELEASE(host);

2291
    vboxIIDUnalloc(&iid);
2292 2293 2294
    return ret;
}

2295 2296 2297
static virNetworkPtr
vboxNetworkLookupByName(virConnectPtr conn, const char *name)
{
2298 2299 2300
    VBOX_OBJECT_HOST_CHECK(conn, virNetworkPtr, NULL);
    PRUnichar *nameUtf16                    = NULL;
    IHostNetworkInterface *networkInterface = NULL;
2301

2302
    VBOX_UTF8_TO_UTF16(name, &nameUtf16);
2303

2304
    host->vtbl->FindHostNetworkInterfaceByName(host, nameUtf16, &networkInterface);
2305

2306 2307
    if (networkInterface) {
        PRUint32 interfaceType = 0;
2308

2309
        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2310

2311 2312
        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
            unsigned char uuid[VIR_UUID_BUFLEN];
2313
            vboxIID iid = VBOX_IID_INITIALIZER;
2314

2315 2316
            networkInterface->vtbl->GetId(networkInterface, &iid.value);
            vboxIIDToUUID(&iid, uuid);
2317
            ret = virGetNetwork(conn, name, uuid);
2318
            VIR_DEBUG("Network Name: %s", name);
2319

2320 2321
            DEBUGIID("Network UUID", iid.value);
            vboxIIDUnalloc(&iid);
2322
        }
2323 2324

        VBOX_RELEASE(networkInterface);
2325 2326
    }

2327 2328 2329
    VBOX_UTF16_FREE(nameUtf16);
    VBOX_RELEASE(host);

2330 2331 2332
    return ret;
}

2333 2334 2335
static virNetworkPtr
vboxNetworkDefineCreateXML(virConnectPtr conn, const char *xml, bool start)
{
2336 2337 2338 2339
    VBOX_OBJECT_HOST_CHECK(conn, virNetworkPtr, NULL);
    PRUnichar *networkInterfaceNameUtf16    = NULL;
    char      *networkInterfaceNameUtf8     = NULL;
    IHostNetworkInterface *networkInterface = NULL;
2340
    nsresult rc;
2341

2342
    virNetworkDefPtr def = virNetworkDefParseString(xml);
2343 2344
    virNetworkIpDefPtr ipdef;
    virSocketAddr netmask;
2345

2346
    if ((!def) ||
2347
        (def->forward.type != VIR_NETWORK_FORWARD_NONE) ||
2348
        (def->nips == 0 || !def->ips))
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
        goto cleanup;

    /* Look for the first IPv4 IP address definition and use that.
     * If there weren't any IPv4 addresses, ignore the network (since it's
     * required below to have an IPv4 address)
    */
    ipdef = virNetworkDefGetIpByIndex(def, AF_INET, 0);
    if (!ipdef)
        goto cleanup;

    if (virNetworkIpDefNetmask(ipdef, &netmask) < 0)
2360
        goto cleanup;
2361

2362 2363 2364 2365 2366 2367
    /* the current limitation of hostonly network is that you can't
     * assign a name to it and it defaults to vboxnet*, for e.g:
     * vboxnet0, vboxnet1, etc. Also the UUID is assigned to it
     * automatically depending on the mac address and thus both
     * these paramters are ignored here for now.
     */
2368

2369
#if VBOX_API_VERSION == 2002000
2370
    if (STREQ(def->name, "vboxnet0")) {
2371
        PRUint32 interfaceType = 0;
2372

2373 2374
        VBOX_UTF8_TO_UTF16(def->name, &networkInterfaceNameUtf16);
        host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
2375

2376 2377 2378 2379 2380 2381
        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
        if (interfaceType != HostNetworkInterfaceType_HostOnly) {
            VBOX_RELEASE(networkInterface);
            networkInterface = NULL;
        }
    }
2382
#else /* VBOX_API_VERSION != 2002000 */
2383 2384 2385 2386
    {
        IProgress *progress = NULL;
        host->vtbl->CreateHostOnlyNetworkInterface(host, &networkInterface,
                                                   &progress);
2387

2388 2389 2390 2391
        if (progress) {
            progress->vtbl->WaitForCompletion(progress, -1);
            VBOX_RELEASE(progress);
        }
2392
    }
2393
#endif /* VBOX_API_VERSION != 2002000 */
2394

2395 2396 2397 2398
    if (networkInterface) {
        unsigned char uuid[VIR_UUID_BUFLEN];
        char      *networkNameUtf8  = NULL;
        PRUnichar *networkNameUtf16 = NULL;
2399
        vboxIID vboxnetiid = VBOX_IID_INITIALIZER;
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410

        networkInterface->vtbl->GetName(networkInterface, &networkInterfaceNameUtf16);
        if (networkInterfaceNameUtf16) {
            VBOX_UTF16_TO_UTF8(networkInterfaceNameUtf16, &networkInterfaceNameUtf8);

            if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", networkInterfaceNameUtf8) < 0) {
                VBOX_RELEASE(host);
                VBOX_RELEASE(networkInterface);
                goto cleanup;
            }
        }
2411

E
Eric Blake 已提交
2412
        VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
2413

2414 2415 2416
        /* Currently support only one dhcp server per network
         * with contigious address space from start to end
         */
2417
        if ((ipdef->nranges >= 1) &&
2418 2419
            VIR_SOCKET_ADDR_VALID(&ipdef->ranges[0].start) &&
            VIR_SOCKET_ADDR_VALID(&ipdef->ranges[0].end)) {
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
            IDHCPServer *dhcpServer = NULL;

            data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
                                                             networkNameUtf16,
                                                             &dhcpServer);
            if (!dhcpServer) {
                /* create a dhcp server */
                data->vboxObj->vtbl->CreateDHCPServer(data->vboxObj,
                                                      networkNameUtf16,
                                                      &dhcpServer);
2430
                VIR_DEBUG("couldn't find dhcp server so creating one");
2431 2432 2433 2434 2435 2436 2437 2438
            }
            if (dhcpServer) {
                PRUnichar *ipAddressUtf16     = NULL;
                PRUnichar *networkMaskUtf16   = NULL;
                PRUnichar *fromIPAddressUtf16 = NULL;
                PRUnichar *toIPAddressUtf16   = NULL;
                PRUnichar *trunkTypeUtf16     = NULL;

2439 2440 2441 2442
                ipAddressUtf16 = vboxSocketFormatAddrUtf16(data, &ipdef->address);
                networkMaskUtf16 = vboxSocketFormatAddrUtf16(data, &netmask);
                fromIPAddressUtf16 = vboxSocketFormatAddrUtf16(data, &ipdef->ranges[0].start);
                toIPAddressUtf16 = vboxSocketFormatAddrUtf16(data, &ipdef->ranges[0].end);
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452

                if (ipAddressUtf16 == NULL || networkMaskUtf16 == NULL ||
                    fromIPAddressUtf16 == NULL || toIPAddressUtf16 == NULL) {
                    VBOX_UTF16_FREE(ipAddressUtf16);
                    VBOX_UTF16_FREE(networkMaskUtf16);
                    VBOX_UTF16_FREE(fromIPAddressUtf16);
                    VBOX_UTF16_FREE(toIPAddressUtf16);
                    VBOX_RELEASE(dhcpServer);
                    goto cleanup;
                }
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477

                VBOX_UTF8_TO_UTF16("netflt", &trunkTypeUtf16);

                dhcpServer->vtbl->SetEnabled(dhcpServer, PR_TRUE);

                dhcpServer->vtbl->SetConfiguration(dhcpServer,
                                                   ipAddressUtf16,
                                                   networkMaskUtf16,
                                                   fromIPAddressUtf16,
                                                   toIPAddressUtf16);

                if (start)
                    dhcpServer->vtbl->Start(dhcpServer,
                                            networkNameUtf16,
                                            networkInterfaceNameUtf16,
                                            trunkTypeUtf16);

                VBOX_UTF16_FREE(ipAddressUtf16);
                VBOX_UTF16_FREE(networkMaskUtf16);
                VBOX_UTF16_FREE(fromIPAddressUtf16);
                VBOX_UTF16_FREE(toIPAddressUtf16);
                VBOX_UTF16_FREE(trunkTypeUtf16);
                VBOX_RELEASE(dhcpServer);
            }
        }
2478

2479
        if ((ipdef->nhosts >= 1) &&
2480
            VIR_SOCKET_ADDR_VALID(&ipdef->hosts[0].ip)) {
2481 2482
            PRUnichar *ipAddressUtf16   = NULL;
            PRUnichar *networkMaskUtf16 = NULL;
2483

2484 2485
            ipAddressUtf16 = vboxSocketFormatAddrUtf16(data, &ipdef->hosts[0].ip);
            networkMaskUtf16 = vboxSocketFormatAddrUtf16(data, &netmask);
2486 2487 2488 2489 2490 2491

            if (ipAddressUtf16 == NULL || networkMaskUtf16 == NULL) {
                VBOX_UTF16_FREE(ipAddressUtf16);
                VBOX_UTF16_FREE(networkMaskUtf16);
                goto cleanup;
            }
2492

2493 2494 2495 2496
            /* Current drawback is that since EnableStaticIpConfig() sets
             * IP and enables the interface so even if the dhcpserver is not
             * started the interface is still up and running
             */
2497
#if VBOX_API_VERSION < 4002000
2498 2499 2500
            networkInterface->vtbl->EnableStaticIpConfig(networkInterface,
                                                         ipAddressUtf16,
                                                         networkMaskUtf16);
2501 2502 2503 2504 2505
#else
            networkInterface->vtbl->EnableStaticIPConfig(networkInterface,
                                                         ipAddressUtf16,
                                                         networkMaskUtf16);
#endif
2506

2507 2508 2509
            VBOX_UTF16_FREE(ipAddressUtf16);
            VBOX_UTF16_FREE(networkMaskUtf16);
        } else {
2510
#if VBOX_API_VERSION < 4002000
2511 2512
            networkInterface->vtbl->EnableDynamicIpConfig(networkInterface);
            networkInterface->vtbl->DhcpRediscover(networkInterface);
2513 2514 2515 2516
#else
            networkInterface->vtbl->EnableDynamicIPConfig(networkInterface);
            networkInterface->vtbl->DHCPRediscover(networkInterface);
#endif
2517
        }
2518

2519 2520 2521 2522 2523
        rc = networkInterface->vtbl->GetId(networkInterface, &vboxnetiid.value);
        if (NS_SUCCEEDED(rc)) {
            vboxIIDToUUID(&vboxnetiid, uuid);
            DEBUGIID("Real Network UUID", vboxnetiid.value);
            vboxIIDUnalloc(&vboxnetiid);
2524
            ret = virGetNetwork(conn, networkInterfaceNameUtf8, uuid);
2525
        }
2526 2527 2528 2529

        VIR_FREE(networkNameUtf8);
        VBOX_UTF16_FREE(networkNameUtf16);
        VBOX_RELEASE(networkInterface);
2530 2531
    }

2532 2533 2534 2535
    VBOX_UTF8_FREE(networkInterfaceNameUtf8);
    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
    VBOX_RELEASE(host);

2536
 cleanup:
2537 2538 2539 2540
    virNetworkDefFree(def);
    return ret;
}

2541 2542
static virNetworkPtr vboxNetworkCreateXML(virConnectPtr conn, const char *xml)
{
2543 2544 2545
    return vboxNetworkDefineCreateXML(conn, xml, true);
}

2546 2547
static virNetworkPtr vboxNetworkDefineXML(virConnectPtr conn, const char *xml)
{
2548 2549 2550
    return vboxNetworkDefineCreateXML(conn, xml, false);
}

2551 2552 2553
static int
vboxNetworkUndefineDestroy(virNetworkPtr network, bool removeinterface)
{
2554
    VBOX_OBJECT_HOST_CHECK(network->conn, int, -1);
2555
    char *networkNameUtf8 = NULL;
2556 2557
    PRUnichar *networkInterfaceNameUtf16    = NULL;
    IHostNetworkInterface *networkInterface = NULL;
2558 2559 2560 2561 2562 2563 2564 2565 2566

    /* Current limitation of the function for VirtualBox 2.2.* is
     * that you can't delete the default hostonly adaptor namely:
     * vboxnet0 and thus all this functions does is remove the
     * dhcp server configuration, but the network can still be used
     * by giving the machine static IP and also it will still
     * show up in the net-list in virsh
     */

2567
    if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
2568 2569
        goto cleanup;

2570
    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
2571

2572
    host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
2573

2574 2575
    if (networkInterface) {
        PRUint32 interfaceType = 0;
2576

2577
        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2578

2579 2580 2581
        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
            PRUnichar *networkNameUtf16 = NULL;
            IDHCPServer *dhcpServer     = NULL;
2582

2583
#if VBOX_API_VERSION != 2002000
2584 2585 2586
            if (removeinterface) {
                PRUnichar *iidUtf16 = NULL;
                IProgress *progress = NULL;
2587

2588
                networkInterface->vtbl->GetId(networkInterface, &iidUtf16);
2589

2590
                if (iidUtf16) {
2591
# if VBOX_API_VERSION == 3000000
2592 2593 2594
                    IHostNetworkInterface *netInt = NULL;
                    host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &netInt, &progress);
                    VBOX_RELEASE(netInt);
2595
# else  /* VBOX_API_VERSION > 3000000 */
2596
                    host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &progress);
2597
# endif /* VBOX_API_VERSION > 3000000 */
2598 2599
                    VBOX_UTF16_FREE(iidUtf16);
                }
2600

2601 2602 2603 2604 2605
                if (progress) {
                    progress->vtbl->WaitForCompletion(progress, -1);
                    VBOX_RELEASE(progress);
                }
            }
2606
#endif /* VBOX_API_VERSION != 2002000 */
2607

E
Eric Blake 已提交
2608
            VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618

            data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
                                                             networkNameUtf16,
                                                             &dhcpServer);
            if (dhcpServer) {
                dhcpServer->vtbl->SetEnabled(dhcpServer, PR_FALSE);
                dhcpServer->vtbl->Stop(dhcpServer);
                if (removeinterface)
                    data->vboxObj->vtbl->RemoveDHCPServer(data->vboxObj, dhcpServer);
                VBOX_RELEASE(dhcpServer);
2619 2620
            }

2621 2622
            VBOX_UTF16_FREE(networkNameUtf16);

2623
        }
2624
        VBOX_RELEASE(networkInterface);
2625 2626
    }

2627 2628 2629
    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
    VBOX_RELEASE(host);

2630 2631
    ret = 0;

2632
 cleanup:
2633 2634 2635 2636
    VIR_FREE(networkNameUtf8);
    return ret;
}

2637 2638
static int vboxNetworkUndefine(virNetworkPtr network)
{
2639 2640 2641
    return vboxNetworkUndefineDestroy(network, true);
}

2642 2643
static int vboxNetworkCreate(virNetworkPtr network)
{
2644
    VBOX_OBJECT_HOST_CHECK(network->conn, int, -1);
2645
    char *networkNameUtf8 = NULL;
2646 2647
    PRUnichar *networkInterfaceNameUtf16    = NULL;
    IHostNetworkInterface *networkInterface = NULL;
2648 2649 2650 2651 2652 2653 2654 2655

    /* Current limitation of the function for VirtualBox 2.2.* is
     * that the default hostonly network "vboxnet0" is always active
     * and thus all this functions does is start the dhcp server,
     * but the network can still be used without starting the dhcp
     * server by giving the machine static IP
     */

2656
    if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
2657 2658
        goto cleanup;

2659
    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
2660

2661
    host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
2662

2663 2664
    if (networkInterface) {
        PRUint32 interfaceType = 0;
2665

2666
        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2667

2668 2669 2670
        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
            PRUnichar *networkNameUtf16 = NULL;
            IDHCPServer *dhcpServer     = NULL;
2671 2672


E
Eric Blake 已提交
2673
            VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
2674

2675 2676 2677 2678 2679
            data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
                                                             networkNameUtf16,
                                                             &dhcpServer);
            if (dhcpServer) {
                PRUnichar *trunkTypeUtf16 = NULL;
2680

2681
                dhcpServer->vtbl->SetEnabled(dhcpServer, PR_TRUE);
2682

2683
                VBOX_UTF8_TO_UTF16("netflt", &trunkTypeUtf16);
2684

2685 2686 2687 2688
                dhcpServer->vtbl->Start(dhcpServer,
                                        networkNameUtf16,
                                        networkInterfaceNameUtf16,
                                        trunkTypeUtf16);
2689

2690 2691
                VBOX_UTF16_FREE(trunkTypeUtf16);
                VBOX_RELEASE(dhcpServer);
2692 2693
            }

2694
            VBOX_UTF16_FREE(networkNameUtf16);
2695
        }
2696 2697

        VBOX_RELEASE(networkInterface);
2698 2699
    }

2700 2701 2702
    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
    VBOX_RELEASE(host);

2703 2704
    ret = 0;

2705
 cleanup:
2706 2707 2708 2709
    VIR_FREE(networkNameUtf8);
    return ret;
}

2710 2711
static int vboxNetworkDestroy(virNetworkPtr network)
{
2712
    return vboxNetworkUndefineDestroy(network, false);
2713 2714
}

2715
static char *vboxNetworkGetXMLDesc(virNetworkPtr network,
E
Eric Blake 已提交
2716 2717
                                   unsigned int flags)
{
2718
    VBOX_OBJECT_HOST_CHECK(network->conn, char *, NULL);
2719
    virNetworkDefPtr def  = NULL;
2720
    virNetworkIpDefPtr ipdef = NULL;
2721
    char *networkNameUtf8 = NULL;
2722 2723
    PRUnichar *networkInterfaceNameUtf16    = NULL;
    IHostNetworkInterface *networkInterface = NULL;
2724

E
Eric Blake 已提交
2725 2726
    virCheckFlags(0, NULL);

2727
    if (VIR_ALLOC(def) < 0)
2728
        goto cleanup;
2729
    if (VIR_ALLOC(ipdef) < 0)
2730 2731 2732
        goto cleanup;
    def->ips = ipdef;
    def->nips = 1;
2733

2734
    if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
2735 2736
        goto cleanup;

2737
    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
2738

2739
    host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
2740

2741 2742
    if (networkInterface) {
        PRUint32 interfaceType = 0;
2743

2744
        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
2745

2746
        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
J
Ján Tomko 已提交
2747
            if (VIR_STRDUP(def->name, network->name) >= 0) {
2748 2749
                PRUnichar *networkNameUtf16 = NULL;
                IDHCPServer *dhcpServer     = NULL;
2750
                vboxIID vboxnet0IID = VBOX_IID_INITIALIZER;
2751

2752 2753
                networkInterface->vtbl->GetId(networkInterface, &vboxnet0IID.value);
                vboxIIDToUUID(&vboxnet0IID, def->uuid);
2754

E
Eric Blake 已提交
2755
                VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
2756

2757
                def->forward.type = VIR_NETWORK_FORWARD_NONE;
2758

2759 2760 2761 2762
                data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
                                                                 networkNameUtf16,
                                                                 &dhcpServer);
                if (dhcpServer) {
2763
                    ipdef->nranges = 1;
2764
                    if (VIR_ALLOC_N(ipdef->ranges, ipdef->nranges) >= 0) {
2765 2766 2767 2768
                        PRUnichar *ipAddressUtf16     = NULL;
                        PRUnichar *networkMaskUtf16   = NULL;
                        PRUnichar *fromIPAddressUtf16 = NULL;
                        PRUnichar *toIPAddressUtf16   = NULL;
2769
                        bool errorOccurred = false;
2770

2771 2772 2773 2774 2775 2776 2777
                        dhcpServer->vtbl->GetIPAddress(dhcpServer, &ipAddressUtf16);
                        dhcpServer->vtbl->GetNetworkMask(dhcpServer, &networkMaskUtf16);
                        dhcpServer->vtbl->GetLowerIP(dhcpServer, &fromIPAddressUtf16);
                        dhcpServer->vtbl->GetUpperIP(dhcpServer, &toIPAddressUtf16);
                        /* Currently virtualbox supports only one dhcp server per network
                         * with contigious address space from start to end
                         */
2778
                        if (vboxSocketParseAddrUtf16(data, ipAddressUtf16,
2779
                                                     &ipdef->address) < 0 ||
2780
                            vboxSocketParseAddrUtf16(data, networkMaskUtf16,
2781
                                                     &ipdef->netmask) < 0 ||
2782
                            vboxSocketParseAddrUtf16(data, fromIPAddressUtf16,
2783
                                                     &ipdef->ranges[0].start) < 0 ||
2784
                            vboxSocketParseAddrUtf16(data, toIPAddressUtf16,
2785
                                                     &ipdef->ranges[0].end) < 0) {
2786 2787
                            errorOccurred = true;
                        }
2788 2789 2790 2791 2792

                        VBOX_UTF16_FREE(ipAddressUtf16);
                        VBOX_UTF16_FREE(networkMaskUtf16);
                        VBOX_UTF16_FREE(fromIPAddressUtf16);
                        VBOX_UTF16_FREE(toIPAddressUtf16);
2793 2794 2795 2796

                        if (errorOccurred) {
                            goto cleanup;
                        }
2797
                    } else {
2798
                        ipdef->nranges = 0;
2799
                    }
2800

2801
                    ipdef->nhosts = 1;
2802
                    if (VIR_ALLOC_N(ipdef->hosts, ipdef->nhosts) >= 0) {
2803
                        if (VIR_STRDUP(ipdef->hosts[0].name, network->name) < 0) {
2804 2805
                            VIR_FREE(ipdef->hosts);
                            ipdef->nhosts = 0;
2806
                        } else {
2807 2808
                            PRUnichar *macAddressUtf16 = NULL;
                            PRUnichar *ipAddressUtf16  = NULL;
2809
                            bool errorOccurred = false;
2810

2811
                            networkInterface->vtbl->GetHardwareAddress(networkInterface, &macAddressUtf16);
2812 2813
                            networkInterface->vtbl->GetIPAddress(networkInterface, &ipAddressUtf16);

2814
                            VBOX_UTF16_TO_UTF8(macAddressUtf16, &ipdef->hosts[0].mac);
2815 2816

                            if (vboxSocketParseAddrUtf16(data, ipAddressUtf16,
2817
                                                         &ipdef->hosts[0].ip) < 0) {
2818 2819
                                errorOccurred = true;
                            }
2820

2821 2822
                            VBOX_UTF16_FREE(macAddressUtf16);
                            VBOX_UTF16_FREE(ipAddressUtf16);
2823 2824 2825 2826

                            if (errorOccurred) {
                                goto cleanup;
                            }
2827 2828
                        }
                    } else {
2829
                        ipdef->nhosts = 0;
2830
                    }
2831 2832 2833 2834 2835

                    VBOX_RELEASE(dhcpServer);
                } else {
                    PRUnichar *networkMaskUtf16 = NULL;
                    PRUnichar *ipAddressUtf16   = NULL;
2836
                    bool errorOccurred = false;
2837 2838 2839 2840

                    networkInterface->vtbl->GetNetworkMask(networkInterface, &networkMaskUtf16);
                    networkInterface->vtbl->GetIPAddress(networkInterface, &ipAddressUtf16);

2841
                    if (vboxSocketParseAddrUtf16(data, networkMaskUtf16,
2842
                                                 &ipdef->netmask) < 0 ||
2843
                        vboxSocketParseAddrUtf16(data, ipAddressUtf16,
2844
                                                 &ipdef->address) < 0) {
2845 2846
                        errorOccurred = true;
                    }
2847 2848 2849

                    VBOX_UTF16_FREE(networkMaskUtf16);
                    VBOX_UTF16_FREE(ipAddressUtf16);
2850 2851 2852 2853

                    if (errorOccurred) {
                        goto cleanup;
                    }
2854 2855
                }

2856 2857
                DEBUGIID("Network UUID", vboxnet0IID.value);
                vboxIIDUnalloc(&vboxnet0IID);
2858
                VBOX_UTF16_FREE(networkNameUtf16);
2859 2860
            }
        }
2861 2862

        VBOX_RELEASE(networkInterface);
2863 2864
    }

2865 2866 2867
    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
    VBOX_RELEASE(host);

2868
    ret = virNetworkDefFormat(def, 0);
2869

2870
 cleanup:
2871
    virNetworkDefFree(def);
2872 2873 2874 2875
    VIR_FREE(networkNameUtf8);
    return ret;
}

2876 2877 2878 2879
/**
 * The Storage Functions here on
 */

2880 2881 2882
static virDrvOpenStatus vboxStorageOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                        unsigned int flags)
E
Eric Blake 已提交
2883
{
2884 2885
    vboxGlobalData *data = conn->privateData;

E
Eric Blake 已提交
2886 2887
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

2888
    if (STRNEQ(conn->driver->name, "VBOX"))
2889
        return VIR_DRV_OPEN_DECLINED;
2890 2891 2892 2893

    if ((data->pFuncs      == NULL) ||
        (data->vboxObj     == NULL) ||
        (data->vboxSession == NULL))
2894
        return VIR_DRV_OPEN_ERROR;
2895

2896
    VIR_DEBUG("vbox storage initialized");
2897 2898 2899 2900
    /* conn->storagePrivateData = some storage specific data */
    return VIR_DRV_OPEN_SUCCESS;
}

2901 2902
static int vboxStorageClose(virConnectPtr conn)
{
2903
    VIR_DEBUG("vbox storage uninitialized");
2904 2905 2906 2907
    conn->storagePrivateData = NULL;
    return 0;
}

2908 2909
static int vboxConnectNumOfStoragePools(virConnectPtr conn ATTRIBUTE_UNUSED)
{
2910 2911 2912 2913 2914 2915 2916 2917

    /** Currently only one pool supported, the default one
     * given by ISystemProperties::defaultHardDiskFolder()
     */

    return 1;
}

2918 2919
static int vboxConnectListStoragePools(virConnectPtr conn ATTRIBUTE_UNUSED,
                                       char **const names, int nnames) {
2920 2921
    int numActive = 0;

2922 2923 2924
    if (nnames == 1 &&
        VIR_STRDUP(names[numActive], "default-pool") > 0)
        numActive++;
2925 2926 2927
    return numActive;
}

2928 2929 2930
static virStoragePoolPtr
vboxStoragePoolLookupByName(virConnectPtr conn, const char *name)
{
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
    virStoragePoolPtr ret = NULL;

    /** Current limitation of the function: since
     * the default pool doesn't have UUID just assign
     * one till vbox can handle pools
     */
    if (STREQ("default-pool", name)) {
        unsigned char uuid[VIR_UUID_BUFLEN];
        const char *uuidstr = "1deff1ff-1481-464f-967f-a50fe8936cc4";

2941
        ignore_value(virUUIDParse(uuidstr, uuid));
2942

2943
        ret = virGetStoragePool(conn, name, uuid, NULL, NULL);
2944 2945 2946 2947 2948
    }

    return ret;
}

2949 2950
static int vboxStoragePoolNumOfVolumes(virStoragePoolPtr pool)
{
2951
    VBOX_OBJECT_CHECK(pool->conn, int, -1);
2952
    vboxArray hardDisks = VBOX_ARRAY_INITIALIZER;
2953 2954
    PRUint32 hardDiskAccessible = 0;
    nsresult rc;
2955
    size_t i;
2956

2957
    rc = vboxArrayGet(&hardDisks, data->vboxObj, data->vboxObj->vtbl->GetHardDisks);
2958
    if (NS_SUCCEEDED(rc)) {
2959 2960
        for (i = 0; i < hardDisks.count; ++i) {
            IHardDisk *hardDisk = hardDisks.items[i];
2961 2962
            if (hardDisk) {
                PRUint32 hddstate;
2963

2964 2965 2966
                VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
                if (hddstate != MediaState_Inaccessible)
                    hardDiskAccessible++;
2967 2968
            }
        }
2969 2970 2971 2972

        vboxArrayRelease(&hardDisks);

        ret = hardDiskAccessible;
2973
    } else {
2974
        ret = -1;
2975 2976 2977
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("could not get number of volumes in the pool: %s, rc=%08x"),
                       pool->name, (unsigned)rc);
2978 2979
    }

2980
    return ret;
2981 2982 2983
}

static int vboxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names, int nnames) {
2984
    VBOX_OBJECT_CHECK(pool->conn, int, -1);
2985
    vboxArray hardDisks = VBOX_ARRAY_INITIALIZER;
2986 2987
    PRUint32 numActive     = 0;
    nsresult rc;
2988
    size_t i;
2989

2990
    rc = vboxArrayGet(&hardDisks, data->vboxObj, data->vboxObj->vtbl->GetHardDisks);
2991
    if (NS_SUCCEEDED(rc)) {
2992 2993
        for (i = 0; i < hardDisks.count && numActive < nnames; ++i) {
            IHardDisk *hardDisk = hardDisks.items[i];
2994

2995 2996 2997 2998
            if (hardDisk) {
                PRUint32 hddstate;
                char      *nameUtf8  = NULL;
                PRUnichar *nameUtf16 = NULL;
2999

3000 3001 3002
                VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
                if (hddstate != MediaState_Inaccessible) {
                    VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &nameUtf16);
3003

3004 3005
                    VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
                    VBOX_UTF16_FREE(nameUtf16);
3006

3007
                    if (nameUtf8) {
3008
                        VIR_DEBUG("nnames[%d]: %s", numActive, nameUtf8);
3009
                        if (VIR_STRDUP(names[numActive], nameUtf8) > 0)
3010 3011 3012
                            numActive++;

                        VBOX_UTF8_FREE(nameUtf8);
3013 3014 3015 3016
                    }
                }
            }
        }
3017 3018 3019 3020

        vboxArrayRelease(&hardDisks);

        ret = numActive;
3021
    } else {
3022
        ret = -1;
3023 3024 3025
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("could not get the volume list in the pool: %s, rc=%08x"),
                       pool->name, (unsigned)rc);
3026 3027
    }

3028
    return ret;
3029 3030
}

3031 3032 3033
static virStorageVolPtr
vboxStorageVolLookupByName(virStoragePoolPtr pool, const char *name)
{
3034
    VBOX_OBJECT_CHECK(pool->conn, virStorageVolPtr, NULL);
3035
    vboxArray hardDisks = VBOX_ARRAY_INITIALIZER;
3036
    nsresult rc;
3037
    size_t i;
3038

3039
    if (!name)
3040
        return ret;
3041

3042
    rc = vboxArrayGet(&hardDisks, data->vboxObj, data->vboxObj->vtbl->GetHardDisks);
3043
    if (NS_SUCCEEDED(rc)) {
3044 3045
        for (i = 0; i < hardDisks.count; ++i) {
            IHardDisk *hardDisk = hardDisks.items[i];
3046

3047 3048 3049 3050
            if (hardDisk) {
                PRUint32 hddstate;
                char      *nameUtf8  = NULL;
                PRUnichar *nameUtf16 = NULL;
3051

3052 3053 3054
                VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
                if (hddstate != MediaState_Inaccessible) {
                    VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &nameUtf16);
3055

3056 3057 3058 3059
                    if (nameUtf16) {
                        VBOX_UTF16_TO_UTF8(nameUtf16, &nameUtf8);
                        VBOX_UTF16_FREE(nameUtf16);
                    }
3060

3061
                    if (nameUtf8 && STREQ(nameUtf8, name)) {
3062 3063 3064
                        vboxIID hddIID = VBOX_IID_INITIALIZER;
                        unsigned char uuid[VIR_UUID_BUFLEN];
                        char key[VIR_UUID_STRING_BUFLEN] = "";
3065

3066 3067 3068 3069
                        rc = VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetId, &hddIID.value);
                        if (NS_SUCCEEDED(rc)) {
                            vboxIIDToUUID(&hddIID, uuid);
                            virUUIDFormat(uuid, key);
3070

3071 3072
                            ret = virGetStorageVol(pool->conn, pool->name, name, key,
                                                   NULL, NULL);
3073

3074 3075 3076 3077
                            VIR_DEBUG("virStorageVolPtr: %p", ret);
                            VIR_DEBUG("Storage Volume Name: %s", name);
                            VIR_DEBUG("Storage Volume key : %s", key);
                            VIR_DEBUG("Storage Volume Pool: %s", pool->name);
3078 3079
                        }

3080
                        vboxIIDUnalloc(&hddIID);
3081 3082
                        VBOX_UTF8_FREE(nameUtf8);
                        break;
3083
                    }
3084

J
John Ferlan 已提交
3085
                    VBOX_UTF8_FREE(nameUtf8);
3086 3087 3088
                }
            }
        }
3089

3090
        vboxArrayRelease(&hardDisks);
3091 3092 3093 3094 3095
    }

    return ret;
}

T
Taowei 已提交
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
static virStorageVolPtr
vboxStorageVolLookupByKey(virConnectPtr conn, const char *key)
{
    VBOX_OBJECT_CHECK(conn, virStorageVolPtr, NULL);
    vboxIID hddIID = VBOX_IID_INITIALIZER;
    unsigned char uuid[VIR_UUID_BUFLEN];
    IHardDisk *hardDisk  = NULL;
    nsresult rc;

    if (!key)
        return ret;

    if (virUUIDParse(key, uuid) < 0) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Could not parse UUID from '%s'"), key);
        return NULL;
    }

    vboxIIDFromUUID(&hddIID, uuid);
#if VBOX_API_VERSION < 4000000
    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID.value, &hardDisk);
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
    rc = data->vboxObj->vtbl->FindMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, &hardDisk);
#else
    rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, AccessMode_ReadWrite,
                                         PR_FALSE, &hardDisk);
#endif /* VBOX_API_VERSION >= 4000000 */
    if (NS_SUCCEEDED(rc)) {
        PRUint32 hddstate;

        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
        if (hddstate != MediaState_Inaccessible) {
            PRUnichar *hddNameUtf16 = NULL;
            char      *hddNameUtf8  = NULL;

            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &hddNameUtf16);
            VBOX_UTF16_TO_UTF8(hddNameUtf16, &hddNameUtf8);

            if (hddNameUtf8) {
                if (vboxConnectNumOfStoragePools(conn) == 1) {
                    ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, key,
                                           NULL, NULL);
                    VIR_DEBUG("Storage Volume Pool: %s", "default-pool");
                } else {
                    /* TODO: currently only one default pool and thus
                     * nothing here, change it when pools are supported
                     */
                }

                VIR_DEBUG("Storage Volume Name: %s", key);
                VIR_DEBUG("Storage Volume key : %s", hddNameUtf8);

                VBOX_UTF8_FREE(hddNameUtf8);
                VBOX_UTF16_FREE(hddNameUtf16);
            }
        }

        VBOX_MEDIUM_RELEASE(hardDisk);
    }

    vboxIIDUnalloc(&hddIID);
    return ret;
}

static virStorageVolPtr
vboxStorageVolLookupByPath(virConnectPtr conn, const char *path)
{
    VBOX_OBJECT_CHECK(conn, virStorageVolPtr, NULL);
    PRUnichar *hddPathUtf16 = NULL;
    IHardDisk *hardDisk     = NULL;
    nsresult rc;

    if (!path)
        return ret;

    VBOX_UTF8_TO_UTF16(path, &hddPathUtf16);

    if (!hddPathUtf16)
        return ret;

#if VBOX_API_VERSION < 4000000
    rc = data->vboxObj->vtbl->FindHardDisk(data->vboxObj, hddPathUtf16, &hardDisk);
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
    rc = data->vboxObj->vtbl->FindMedium(data->vboxObj, hddPathUtf16,
                                         DeviceType_HardDisk, &hardDisk);
#else
    rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, hddPathUtf16,
                                         DeviceType_HardDisk, AccessMode_ReadWrite,
                                         PR_FALSE, &hardDisk);
#endif /* VBOX_API_VERSION >= 4000000 */
    if (NS_SUCCEEDED(rc)) {
        PRUint32 hddstate;

        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
        if (hddstate != MediaState_Inaccessible) {
            PRUnichar *hddNameUtf16 = NULL;
            char      *hddNameUtf8  = NULL;

            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &hddNameUtf16);

            if (hddNameUtf16) {
                VBOX_UTF16_TO_UTF8(hddNameUtf16, &hddNameUtf8);
                VBOX_UTF16_FREE(hddNameUtf16);
            }

            if (hddNameUtf8) {
                vboxIID hddIID = VBOX_IID_INITIALIZER;
                unsigned char uuid[VIR_UUID_BUFLEN];
                char key[VIR_UUID_STRING_BUFLEN] = "";

                rc = VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetId, &hddIID.value);
                if (NS_SUCCEEDED(rc)) {
                    vboxIIDToUUID(&hddIID, uuid);
                    virUUIDFormat(uuid, key);

                    /* TODO: currently only one default pool and thus
                     * the check below, change it when pools are supported
                     */
                    if (vboxConnectNumOfStoragePools(conn) == 1)
                        ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, key,
                                               NULL, NULL);

                    VIR_DEBUG("Storage Volume Pool: %s", "default-pool");
                    VIR_DEBUG("Storage Volume Name: %s", hddNameUtf8);
                    VIR_DEBUG("Storage Volume key : %s", key);
                }

                vboxIIDUnalloc(&hddIID);
            }

            VBOX_UTF8_FREE(hddNameUtf8);
        }

        VBOX_MEDIUM_RELEASE(hardDisk);
    }

    VBOX_UTF16_FREE(hddPathUtf16);

    return ret;
}

static virStorageVolPtr vboxStorageVolCreateXML(virStoragePoolPtr pool,
                                                const char *xml,
                                                unsigned int flags)
{
    VBOX_OBJECT_CHECK(pool->conn, virStorageVolPtr, NULL);
    virStorageVolDefPtr  def  = NULL;
    PRUnichar *hddFormatUtf16 = NULL;
    PRUnichar *hddNameUtf16   = NULL;
    virStoragePoolDef poolDef;
    nsresult rc;

    virCheckFlags(0, NULL);

    /* since there is currently one default pool now
     * and virStorageVolDefFormat() just checks it type
     * so just assign it for now, change the behaviour
     * when vbox supports pools.
     */
    memset(&poolDef, 0, sizeof(poolDef));
    poolDef.type = VIR_STORAGE_POOL_DIR;

    if ((def = virStorageVolDefParseString(&poolDef, xml)) == NULL)
        goto cleanup;

    if (!def->name ||
        (def->type != VIR_STORAGE_VOL_FILE))
        goto cleanup;

    /* For now only the vmdk, vpc and vdi type harddisk
     * variants can be created.  For historical reason, we default to vdi */
    if (def->target.format == VIR_STORAGE_FILE_VMDK) {
        VBOX_UTF8_TO_UTF16("VMDK", &hddFormatUtf16);
    } else if (def->target.format == VIR_STORAGE_FILE_VPC) {
        VBOX_UTF8_TO_UTF16("VHD", &hddFormatUtf16);
    } else {
        VBOX_UTF8_TO_UTF16("VDI", &hddFormatUtf16);
    }

    VBOX_UTF8_TO_UTF16(def->name, &hddNameUtf16);

    if (hddFormatUtf16 && hddNameUtf16) {
        IHardDisk *hardDisk = NULL;

        rc = data->vboxObj->vtbl->CreateHardDisk(data->vboxObj, hddFormatUtf16, hddNameUtf16, &hardDisk);
        if (NS_SUCCEEDED(rc)) {
            IProgress *progress    = NULL;
            PRUint64   logicalSize = VIR_DIV_UP(def->target.capacity,
                                                1024 * 1024);
            PRUint32   variant     = HardDiskVariant_Standard;

            if (def->target.capacity == def->target.allocation)
                variant = HardDiskVariant_Fixed;

#if VBOX_API_VERSION < 4003000
            rc = hardDisk->vtbl->CreateBaseStorage(hardDisk, logicalSize, variant, &progress);
#else
            rc = hardDisk->vtbl->CreateBaseStorage(hardDisk, logicalSize, 1, &variant, &progress);
#endif
            if (NS_SUCCEEDED(rc) && progress) {
#if VBOX_API_VERSION == 2002000
                nsresult resultCode;
#else
                PRInt32  resultCode;
#endif

                progress->vtbl->WaitForCompletion(progress, -1);
                progress->vtbl->GetResultCode(progress, &resultCode);

                if (NS_SUCCEEDED(resultCode)) {
                    vboxIID hddIID = VBOX_IID_INITIALIZER;
                    unsigned char uuid[VIR_UUID_BUFLEN];
                    char key[VIR_UUID_STRING_BUFLEN] = "";

                    rc = VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetId, &hddIID.value);
                    if (NS_SUCCEEDED(rc)) {
                        vboxIIDToUUID(&hddIID, uuid);
                        virUUIDFormat(uuid, key);

                        ret = virGetStorageVol(pool->conn, pool->name, def->name, key,
                                               NULL, NULL);
                    }

                    vboxIIDUnalloc(&hddIID);
                }

                VBOX_RELEASE(progress);
            }
        }
    }

    VBOX_UTF16_FREE(hddFormatUtf16);
    VBOX_UTF16_FREE(hddNameUtf16);

 cleanup:
    virStorageVolDefFree(def);
    return ret;
}

static int vboxStorageVolDelete(virStorageVolPtr vol,
                                unsigned int flags)
3339
{
T
Taowei 已提交
3340
    VBOX_OBJECT_CHECK(vol->conn, int, -1);
3341 3342
    vboxIID hddIID = VBOX_IID_INITIALIZER;
    unsigned char uuid[VIR_UUID_BUFLEN];
3343
    IHardDisk *hardDisk  = NULL;
T
Taowei 已提交
3344
    int deregister = 0;
3345
    nsresult rc;
T
Taowei 已提交
3346 3347
    size_t i = 0;
    size_t j = 0;
3348

T
Taowei 已提交
3349
    virCheckFlags(0, -1);
3350

T
Taowei 已提交
3351
    if (virUUIDParse(vol->key, uuid) < 0) {
3352
        virReportError(VIR_ERR_INVALID_ARG,
T
Taowei 已提交
3353 3354
                       _("Could not parse UUID from '%s'"), vol->key);
        return -1;
3355 3356
    }

3357
    vboxIIDFromUUID(&hddIID, uuid);
3358
#if VBOX_API_VERSION < 4000000
3359
    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID.value, &hardDisk);
3360
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
3361 3362
    rc = data->vboxObj->vtbl->FindMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, &hardDisk);
3363 3364 3365 3366
#else
    rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, AccessMode_ReadWrite,
                                         PR_FALSE, &hardDisk);
3367
#endif /* VBOX_API_VERSION >= 4000000 */
3368 3369
    if (NS_SUCCEEDED(rc)) {
        PRUint32 hddstate;
3370

3371 3372
        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
        if (hddstate != MediaState_Inaccessible) {
T
Taowei 已提交
3373 3374
            PRUint32  machineIdsSize = 0;
            vboxArray machineIds = VBOX_ARRAY_INITIALIZER;
3375

T
Taowei 已提交
3376 3377 3378 3379 3380
#if VBOX_API_VERSION < 3001000
            vboxArrayGet(&machineIds, hardDisk, hardDisk->vtbl->imedium.GetMachineIds);
#else  /* VBOX_API_VERSION >= 3001000 */
            vboxArrayGet(&machineIds, hardDisk, hardDisk->vtbl->GetMachineIds);
#endif /* VBOX_API_VERSION >= 3001000 */
3381

T
Taowei 已提交
3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
#if VBOX_API_VERSION == 2002000 && defined WIN32
            /* VirtualBox 2.2 on Windows represents IIDs as GUIDs and the
             * machineIds array contains direct instances of the GUID struct
             * instead of pointers to the actual struct instances. But there
             * is no 128bit width simple item type for a SafeArray to fit a
             * GUID in. The largest simple type it 64bit width and VirtualBox
             * uses two of this 64bit items to represents one GUID. Therefore,
             * we divide the size of the SafeArray by two, to compensate for
             * this workaround in VirtualBox */
            machineIds.count /= 2;
#endif /* VBOX_API_VERSION >= 2002000 */

            machineIdsSize = machineIds.count;

            for (i = 0; i < machineIds.count; i++) {
                IMachine *machine = NULL;
                vboxIID machineId = VBOX_IID_INITIALIZER;

                vboxIIDFromArrayItem(&machineId, &machineIds, i);

#if VBOX_API_VERSION >= 4000000
                rc = VBOX_OBJECT_GET_MACHINE(machineId.value, &machine);
                if (NS_FAILED(rc)) {
                    virReportError(VIR_ERR_NO_DOMAIN, "%s",
                                   _("no domain with matching uuid"));
                    break;
3408
                }
T
Taowei 已提交
3409
#endif
3410

T
Taowei 已提交
3411
                rc = VBOX_SESSION_OPEN(machineId.value, machine);
3412

T
Taowei 已提交
3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
                if (NS_SUCCEEDED(rc)) {

                    rc = data->vboxSession->vtbl->GetMachine(data->vboxSession, &machine);
                    if (NS_SUCCEEDED(rc)) {
                        vboxArray hddAttachments = VBOX_ARRAY_INITIALIZER;

#if VBOX_API_VERSION < 3001000
                        vboxArrayGet(&hddAttachments, machine,
                                     machine->vtbl->GetHardDiskAttachments);
#else  /* VBOX_API_VERSION >= 3001000 */
                        vboxArrayGet(&hddAttachments, machine,
                                     machine->vtbl->GetMediumAttachments);
#endif /* VBOX_API_VERSION >= 3001000 */
                        for (j = 0; j < hddAttachments.count; j++) {
                            IHardDiskAttachment *hddAttachment = hddAttachments.items[j];

                            if (hddAttachment) {
                                IHardDisk *hdd = NULL;

#if VBOX_API_VERSION < 3001000
                                rc = hddAttachment->vtbl->GetHardDisk(hddAttachment, &hdd);
#else  /* VBOX_API_VERSION >= 3001000 */
                                rc = hddAttachment->vtbl->GetMedium(hddAttachment, &hdd);
#endif /* VBOX_API_VERSION >= 3001000 */
                                if (NS_SUCCEEDED(rc) && hdd) {
                                    vboxIID iid = VBOX_IID_INITIALIZER;

                                    rc = VBOX_MEDIUM_FUNC_ARG1(hdd, GetId, &iid.value);
                                    if (NS_SUCCEEDED(rc)) {

                                            DEBUGIID("HardDisk (to delete) UUID", hddIID.value);
                                            DEBUGIID("HardDisk (currently processing) UUID", iid.value);

                                        if (vboxIIDIsEqual(&hddIID, &iid)) {
                                            PRUnichar *controller = NULL;
                                            PRInt32    port       = 0;
                                            PRInt32    device     = 0;

                                            DEBUGIID("Found HardDisk to delete, UUID", hddIID.value);

                                            hddAttachment->vtbl->GetController(hddAttachment, &controller);
                                            hddAttachment->vtbl->GetPort(hddAttachment, &port);
                                            hddAttachment->vtbl->GetDevice(hddAttachment, &device);

#if VBOX_API_VERSION < 3001000
                                            rc = machine->vtbl->DetachHardDisk(machine, controller, port, device);
#else  /* VBOX_API_VERSION >= 3001000 */
                                            rc = machine->vtbl->DetachDevice(machine, controller, port, device);
#endif /* VBOX_API_VERSION >= 3001000 */
                                            if (NS_SUCCEEDED(rc)) {
                                                rc = machine->vtbl->SaveSettings(machine);
                                                VIR_DEBUG("saving machine settings");
                                            }

                                            if (NS_SUCCEEDED(rc)) {
                                                deregister++;
                                                VIR_DEBUG("deregistering hdd:%d", deregister);
                                            }

                                            VBOX_UTF16_FREE(controller);
                                        }
                                        vboxIIDUnalloc(&iid);
                                    }
                                    VBOX_MEDIUM_RELEASE(hdd);
                                }
                            }
                        }
                        vboxArrayRelease(&hddAttachments);
                        VBOX_RELEASE(machine);
                    }
                    VBOX_SESSION_CLOSE();
                }

                vboxIIDUnalloc(&machineId);
            }

            vboxArrayUnalloc(&machineIds);

            if (machineIdsSize == 0 || machineIdsSize == deregister) {
                IProgress *progress = NULL;
                rc = hardDisk->vtbl->DeleteStorage(hardDisk, &progress);

                if (NS_SUCCEEDED(rc) && progress) {
                    progress->vtbl->WaitForCompletion(progress, -1);
                    VBOX_RELEASE(progress);
                    DEBUGIID("HardDisk deleted, UUID", hddIID.value);
                    ret = 0;
                }
3501 3502
            }
        }
3503 3504

        VBOX_MEDIUM_RELEASE(hardDisk);
3505 3506
    }

3507
    vboxIIDUnalloc(&hddIID);
T
Taowei 已提交
3508

3509 3510 3511
    return ret;
}

T
Taowei 已提交
3512 3513
static int
vboxStorageVolGetInfo(virStorageVolPtr vol, virStorageVolInfoPtr info)
3514
{
T
Taowei 已提交
3515 3516 3517 3518
    VBOX_OBJECT_CHECK(vol->conn, int, -1);
    IHardDisk *hardDisk  = NULL;
    unsigned char uuid[VIR_UUID_BUFLEN];
    vboxIID hddIID = VBOX_IID_INITIALIZER;
3519 3520
    nsresult rc;

T
Taowei 已提交
3521
    if (!info)
3522
        return ret;
3523

T
Taowei 已提交
3524 3525 3526
    if (virUUIDParse(vol->key, uuid) < 0) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Could not parse UUID from '%s'"), vol->key);
3527
        return ret;
T
Taowei 已提交
3528
    }
3529

T
Taowei 已提交
3530
    vboxIIDFromUUID(&hddIID, uuid);
3531
#if VBOX_API_VERSION < 4000000
T
Taowei 已提交
3532
    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID.value, &hardDisk);
3533
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
T
Taowei 已提交
3534
    rc = data->vboxObj->vtbl->FindMedium(data->vboxObj, hddIID.value,
3535
                                         DeviceType_HardDisk, &hardDisk);
3536
#else
T
Taowei 已提交
3537
    rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, hddIID.value,
3538 3539
                                         DeviceType_HardDisk, AccessMode_ReadWrite,
                                         PR_FALSE, &hardDisk);
3540
#endif /* VBOX_API_VERSION >= 4000000 */
3541 3542
    if (NS_SUCCEEDED(rc)) {
        PRUint32 hddstate;
3543

3544 3545
        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
        if (hddstate != MediaState_Inaccessible) {
T
Taowei 已提交
3546 3547 3548 3549 3550 3551 3552
#if VBOX_API_VERSION < 4000000
            PRUint64 hddLogicalSize;
            PRUint64 hddActualSize;
#else /* VBOX_API_VERSION >= 4000000 */
            PRInt64 hddLogicalSize;
            PRInt64 hddActualSize;
#endif /* VBOX_API_VERSION >= 4000000 */
3553

T
Taowei 已提交
3554
            info->type = VIR_STORAGE_VOL_FILE;
3555

T
Taowei 已提交
3556 3557 3558 3559 3560 3561
            hardDisk->vtbl->GetLogicalSize(hardDisk, &hddLogicalSize);
#if VBOX_API_VERSION < 4000000
            info->capacity = hddLogicalSize * 1024 * 1024; /* MB => Bytes */
#else /* VBOX_API_VERSION >= 4000000 */
            info->capacity = hddLogicalSize;
#endif /* VBOX_API_VERSION >= 4000000 */
3562

T
Taowei 已提交
3563 3564
            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetSize, &hddActualSize);
            info->allocation = hddActualSize;
3565

T
Taowei 已提交
3566
            ret = 0;
3567

T
Taowei 已提交
3568 3569 3570 3571
            VIR_DEBUG("Storage Volume Name: %s", vol->name);
            VIR_DEBUG("Storage Volume Type: %s", info->type == VIR_STORAGE_VOL_BLOCK ? "Block" : "File");
            VIR_DEBUG("Storage Volume Capacity: %llu", info->capacity);
            VIR_DEBUG("Storage Volume Allocation: %llu", info->allocation);
3572
        }
3573 3574

        VBOX_MEDIUM_RELEASE(hardDisk);
3575 3576
    }

T
Taowei 已提交
3577
    vboxIIDUnalloc(&hddIID);
3578

3579 3580 3581
    return ret;
}

T
Taowei 已提交
3582
static char *vboxStorageVolGetXMLDesc(virStorageVolPtr vol, unsigned int flags)
E
Eric Blake 已提交
3583
{
T
Taowei 已提交
3584 3585 3586 3587 3588 3589 3590
    VBOX_OBJECT_CHECK(vol->conn, char *, NULL);
    IHardDisk *hardDisk  = NULL;
    unsigned char uuid[VIR_UUID_BUFLEN];
    vboxIID hddIID = VBOX_IID_INITIALIZER;
    virStoragePoolDef pool;
    virStorageVolDef def;
    int defOk = 0;
3591 3592
    nsresult rc;

E
Eric Blake 已提交
3593 3594
    virCheckFlags(0, NULL);

T
Taowei 已提交
3595 3596
    memset(&pool, 0, sizeof(pool));
    memset(&def, 0, sizeof(def));
3597

T
Taowei 已提交
3598 3599 3600 3601 3602
    if (virUUIDParse(vol->key, uuid) < 0) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Could not parse UUID from '%s'"), vol->key);
        return ret;
    }
3603

T
Taowei 已提交
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616
    vboxIIDFromUUID(&hddIID, uuid);
#if VBOX_API_VERSION < 4000000
    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID.value, &hardDisk);
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
    rc = data->vboxObj->vtbl->FindMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, &hardDisk);
#else
    rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, AccessMode_ReadWrite,
                                         PR_FALSE, &hardDisk);
#endif /* VBOX_API_VERSION >= 4000000 */
    if (NS_SUCCEEDED(rc)) {
        PRUint32 hddstate;
3617

T
Taowei 已提交
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627
        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
        if (NS_SUCCEEDED(rc) && hddstate != MediaState_Inaccessible) {
            PRUnichar *hddFormatUtf16 = NULL;
#if VBOX_API_VERSION < 4000000
            PRUint64 hddLogicalSize;
            PRUint64 hddActualSize;
#else /* VBOX_API_VERSION >= 4000000 */
            PRInt64 hddLogicalSize;
            PRInt64 hddActualSize;
#endif /* VBOX_API_VERSION >= 4000000 */
3628

T
Taowei 已提交
3629 3630 3631 3632 3633 3634 3635 3636
            /* since there is currently one default pool now
             * and virStorageVolDefFormat() just checks it type
             * so just assign it for now, change the behaviour
             * when vbox supports pools.
             */
            pool.type = VIR_STORAGE_POOL_DIR;
            def.type = VIR_STORAGE_VOL_FILE;
            defOk = 1;
3637

T
Taowei 已提交
3638 3639 3640 3641 3642 3643 3644 3645 3646
            rc = hardDisk->vtbl->GetLogicalSize(hardDisk, &hddLogicalSize);
            if (NS_SUCCEEDED(rc) && defOk) {
#if VBOX_API_VERSION < 4000000
                def.target.capacity = hddLogicalSize * 1024 * 1024; /* MB => Bytes */
#else /* VBOX_API_VERSION >= 4000000 */
                def.target.capacity = hddLogicalSize;
#endif /* VBOX_API_VERSION >= 4000000 */
            } else
                defOk = 0;
3647

T
Taowei 已提交
3648 3649 3650 3651 3652
            rc = VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetSize, &hddActualSize);
            if (NS_SUCCEEDED(rc) && defOk)
                def.target.allocation = hddActualSize;
            else
                defOk = 0;
3653

T
Taowei 已提交
3654 3655
            if (VIR_STRDUP(def.name, vol->name) < 0)
                defOk = 0;
3656

T
Taowei 已提交
3657 3658
            if (VIR_STRDUP(def.key, vol->key) < 0)
                defOk = 0;
3659

T
Taowei 已提交
3660 3661 3662
            rc = hardDisk->vtbl->GetFormat(hardDisk, &hddFormatUtf16);
            if (NS_SUCCEEDED(rc) && defOk) {
                char *hddFormatUtf8 = NULL;
3663

T
Taowei 已提交
3664 3665
                VBOX_UTF16_TO_UTF8(hddFormatUtf16, &hddFormatUtf8);
                if (hddFormatUtf8) {
3666

T
Taowei 已提交
3667
                    VIR_DEBUG("Storage Volume Format: %s", hddFormatUtf8);
3668

T
Taowei 已提交
3669 3670 3671 3672 3673 3674 3675 3676
                    if (STRCASEEQ("vmdk", hddFormatUtf8))
                        def.target.format = VIR_STORAGE_FILE_VMDK;
                    else if (STRCASEEQ("vhd", hddFormatUtf8))
                        def.target.format = VIR_STORAGE_FILE_VPC;
                    else if (STRCASEEQ("vdi", hddFormatUtf8))
                        def.target.format = VIR_STORAGE_FILE_VDI;
                    else
                        def.target.format = VIR_STORAGE_FILE_RAW;
3677

T
Taowei 已提交
3678
                    VBOX_UTF8_FREE(hddFormatUtf8);
3679 3680
                }

T
Taowei 已提交
3681 3682 3683
                VBOX_UTF16_FREE(hddFormatUtf16);
            } else {
                defOk = 0;
3684
            }
3685
        }
T
Taowei 已提交
3686 3687

        VBOX_MEDIUM_RELEASE(hardDisk);
3688 3689
    }

T
Taowei 已提交
3690 3691 3692 3693
    vboxIIDUnalloc(&hddIID);

    if (defOk)
        ret = virStorageVolDefFormat(&pool, &def);
3694

3695 3696 3697
    return ret;
}

T
Taowei 已提交
3698 3699
static char *vboxStorageVolGetPath(virStorageVolPtr vol) {
    VBOX_OBJECT_CHECK(vol->conn, char *, NULL);
3700
    IHardDisk *hardDisk  = NULL;
T
Taowei 已提交
3701 3702
    unsigned char uuid[VIR_UUID_BUFLEN];
    vboxIID hddIID = VBOX_IID_INITIALIZER;
3703
    nsresult rc;
E
Eric Blake 已提交
3704

3705
    if (virUUIDParse(vol->key, uuid) < 0) {
3706 3707
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Could not parse UUID from '%s'"), vol->key);
T
Taowei 已提交
3708
        return ret;
3709
    }
3710

3711
    vboxIIDFromUUID(&hddIID, uuid);
3712
#if VBOX_API_VERSION < 4000000
3713
    rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID.value, &hardDisk);
3714
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
3715 3716
    rc = data->vboxObj->vtbl->FindMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, &hardDisk);
3717 3718 3719 3720
#else
    rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, hddIID.value,
                                         DeviceType_HardDisk, AccessMode_ReadWrite,
                                         PR_FALSE, &hardDisk);
3721
#endif /* VBOX_API_VERSION >= 4000000 */
3722 3723
    if (NS_SUCCEEDED(rc)) {
        PRUint32 hddstate;
3724

3725 3726
        VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
        if (hddstate != MediaState_Inaccessible) {
T
Taowei 已提交
3727 3728
            PRUnichar *hddLocationUtf16 = NULL;
            char      *hddLocationUtf8  = NULL;
3729

T
Taowei 已提交
3730
            VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetLocation, &hddLocationUtf16);
3731

T
Taowei 已提交
3732 3733
            VBOX_UTF16_TO_UTF8(hddLocationUtf16, &hddLocationUtf8);
            if (hddLocationUtf8) {
3734

T
Taowei 已提交
3735
                ignore_value(VIR_STRDUP(ret, hddLocationUtf8));
3736

T
Taowei 已提交
3737 3738 3739
                VIR_DEBUG("Storage Volume Name: %s", vol->name);
                VIR_DEBUG("Storage Volume Path: %s", hddLocationUtf8);
                VIR_DEBUG("Storage Volume Pool: %s", vol->pool);
3740

T
Taowei 已提交
3741 3742
                VBOX_UTF8_FREE(hddLocationUtf8);
            }
3743

T
Taowei 已提交
3744 3745
            VBOX_UTF16_FREE(hddLocationUtf16);
        }
3746

T
Taowei 已提交
3747 3748
        VBOX_MEDIUM_RELEASE(hardDisk);
    }
3749

T
Taowei 已提交
3750
    vboxIIDUnalloc(&hddIID);
3751

T
Taowei 已提交
3752 3753
    return ret;
}
3754

T
Taowei 已提交
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
static int _pfnInitialize(vboxGlobalData *data)
{
    data->pFuncs = g_pfnGetFunctions(VBOX_XPCOMC_VERSION);
    if (data->pFuncs == NULL)
        return -1;
#if VBOX_XPCOMC_VERSION == 0x00010000U
    data->pFuncs->pfnComInitialize(&data->vboxObj, &data->vboxSession);
#else  /* !(VBOX_XPCOMC_VERSION == 0x00010000U) */
    data->pFuncs->pfnComInitialize(IVIRTUALBOX_IID_STR, &data->vboxObj, ISESSION_IID_STR, &data->vboxSession);
#endif /* !(VBOX_XPCOMC_VERSION == 0x00010000U) */
    return 0;
}
3767

T
Taowei 已提交
3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785
static int
_initializeDomainEvent(vboxGlobalData *data ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION <= 2002000 || VBOX_API_VERSION >= 4000000
    /* No event queue functionality in 2.2.* and 4.* as of now */
    vboxUnsupported();
#else /* VBOX_API_VERSION > 2002000 || VBOX_API_VERSION < 4000000 */
    /* Initialize the fWatch needed for Event Callbacks */
    data->fdWatch = -1;
    data->pFuncs->pfnGetEventQueue(&data->vboxQueue);
    if (data->vboxQueue == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("nsIEventQueue object is null"));
        return -1;
    }
#endif /* VBOX_API_VERSION > 2002000 || VBOX_API_VERSION < 4000000 */
    return 0;
}
3786

T
Taowei 已提交
3787 3788 3789 3790 3791 3792 3793 3794 3795
static
void _registerGlobalData(vboxGlobalData *data ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION == 2002000
    vboxUnsupported();
#else /* VBOX_API_VERSION != 2002000 */
    g_pVBoxGlobalData = data;
#endif /* VBOX_API_VERSION != 2002000 */
}
3796

T
Taowei 已提交
3797
#if VBOX_API_VERSION < 4000000
3798

T
Taowei 已提交
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818
# if VBOX_API_VERSION < 3001000
static void
_detachDevices(vboxGlobalData *data ATTRIBUTE_UNUSED,
               IMachine *machine, PRUnichar *hddcnameUtf16)
{
    /* Disconnect all the drives if present */
    machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 0, 0);
    machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 0, 1);
    machine->vtbl->DetachHardDisk(machine, hddcnameUtf16, 1, 1);
}
# else  /* VBOX_API_VERSION >= 3001000 */
static void
_detachDevices(vboxGlobalData *data, IMachine *machine,
               PRUnichar *hddcnameUtf16 ATTRIBUTE_UNUSED)
{
    /* get all the controller first, then the attachments and
    * remove them all so that the machine can be undefined
    */
   vboxArray storageControllers = VBOX_ARRAY_INITIALIZER;
   size_t i = 0, j = 0;
3819

T
Taowei 已提交
3820 3821
   vboxArrayGet(&storageControllers, machine,
                machine->vtbl->GetStorageControllers);
3822

T
Taowei 已提交
3823 3824 3825 3826
   for (i = 0; i < storageControllers.count; i++) {
       IStorageController *strCtl = storageControllers.items[i];
       PRUnichar *strCtlName = NULL;
       vboxArray mediumAttachments = VBOX_ARRAY_INITIALIZER;
3827

T
Taowei 已提交
3828 3829
       if (!strCtl)
           continue;
3830

T
Taowei 已提交
3831 3832 3833 3834
       strCtl->vtbl->GetName(strCtl, &strCtlName);
       vboxArrayGetWithPtrArg(&mediumAttachments, machine,
                              machine->vtbl->GetMediumAttachmentsOfController,
                              strCtlName);
3835

T
Taowei 已提交
3836 3837 3838 3839
       for (j = 0; j < mediumAttachments.count; j++) {
           IMediumAttachment *medAtt = mediumAttachments.items[j];
           PRInt32 port = ~0U;
           PRInt32 device = ~0U;
3840

T
Taowei 已提交
3841 3842
           if (!medAtt)
               continue;
3843

T
Taowei 已提交
3844 3845
           medAtt->vtbl->GetPort(medAtt, &port);
           medAtt->vtbl->GetDevice(medAtt, &device);
3846

T
Taowei 已提交
3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860
           if ((port != ~0U) && (device != ~0U)) {
               machine->vtbl->DetachDevice(machine,
                                           strCtlName,
                                           port,
                                           device);
           }
       }
       vboxArrayRelease(&storageControllers);
       machine->vtbl->RemoveStorageController(machine, strCtlName);
       VBOX_UTF16_FREE(strCtlName);
   }
   vboxArrayRelease(&storageControllers);
}
# endif /* VBOX_API_VERSION >= 3001000 */
3861

T
Taowei 已提交
3862 3863 3864 3865 3866
static nsresult
_unregisterMachine(vboxGlobalData *data, vboxIIDUnion *iidu, IMachine **machine)
{
    return data->vboxObj->vtbl->UnregisterMachine(data->vboxObj, IID_MEMBER(value), machine);
}
3867

T
Taowei 已提交
3868 3869 3870 3871 3872
static void
_deleteConfig(IMachine *machine)
{
    machine->vtbl->DeleteSettings(machine);
}
3873

T
Taowei 已提交
3874
#else /* VBOX_API_VERSION >= 4000000 */
3875

T
Taowei 已提交
3876 3877 3878 3879 3880 3881 3882
static void
_detachDevices(vboxGlobalData *data ATTRIBUTE_UNUSED,
               IMachine *machine ATTRIBUTE_UNUSED,
               PRUnichar *hddcnameUtf16 ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
}
3883

T
Taowei 已提交
3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894
static nsresult
_unregisterMachine(vboxGlobalData *data, vboxIIDUnion *iidu, IMachine **machine)
{
    nsresult rc;
    vboxArray media = VBOX_ARRAY_INITIALIZER;
    rc = VBOX_OBJECT_GET_MACHINE(IID_MEMBER(value), machine);
    if (NS_FAILED(rc)) {
        virReportError(VIR_ERR_NO_DOMAIN, "%s",
                       _("no domain with matching uuid"));
        return rc;
    }
3895

T
Taowei 已提交
3896 3897 3898 3899 3900 3901 3902 3903 3904
    /* We're not interested in the array returned by the Unregister method,
     * but in the side effect of unregistering the virtual machine. In order
     * to call the Unregister method correctly we need to use the vboxArray
     * wrapper here. */
    rc = vboxArrayGetWithUintArg(&media, *machine, (*machine)->vtbl->Unregister,
                                 CleanupMode_DetachAllReturnNone);
    vboxArrayUnalloc(&media);
    return rc;
}
3905

T
Taowei 已提交
3906 3907 3908 3909
static void
_deleteConfig(IMachine *machine)
{
    IProgress *progress = NULL;
3910

T
Taowei 已提交
3911 3912 3913 3914 3915 3916 3917 3918 3919 3920
    /* The IMachine Delete method takes an array of IMedium items to be
     * deleted along with the virtual machine. We just want to pass an
     * empty array. But instead of adding a full vboxArraySetWithReturn to
     * the glue layer (in order to handle the required signature of the
     * Delete method) we use a local solution here. */
# ifdef WIN32
    SAFEARRAY *safeArray = NULL;
    typedef HRESULT __stdcall (*IMachine_Delete)(IMachine *self,
                                                 SAFEARRAY **media,
                                                 IProgress **progress);
3921

T
Taowei 已提交
3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939
#  if VBOX_API_VERSION < 4003000
    ((IMachine_Delete)machine->vtbl->Delete)(machine, &safeArray, &progress);
#  else
    ((IMachine_Delete)machine->vtbl->DeleteConfig)(machine, &safeArray, &progress);
#  endif
# else
    /* XPCOM doesn't like NULL as an array, even when the array size is 0.
     * Instead pass it a dummy array to avoid passing NULL. */
    IMedium *array[] = { NULL };
#  if VBOX_API_VERSION < 4003000
    machine->vtbl->Delete(machine, 0, array, &progress);
#  else
    machine->vtbl->DeleteConfig(machine, 0, array, &progress);
#  endif
# endif
    if (progress != NULL) {
        progress->vtbl->WaitForCompletion(progress, -1);
        VBOX_RELEASE(progress);
3940
    }
T
Taowei 已提交
3941
}
3942

T
Taowei 已提交
3943
#endif /* VBOX_API_VERSION >= 4000000 */
3944

T
Taowei 已提交
3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111
#if VBOX_API_VERSION < 3001000

static void
_dumpIDEHDDsOld(virDomainDefPtr def,
                vboxGlobalData *data,
                IMachine *machine)
{
    PRInt32       hddNum                = 0;
    IHardDisk    *hardDiskPM            = NULL;
    IHardDisk    *hardDiskPS            = NULL;
    IHardDisk    *hardDiskSS            = NULL;
    const char   *hddBus                = "IDE";
    PRUnichar    *hddBusUtf16           = NULL;

    /* dump IDE hdds if present */
    VBOX_UTF8_TO_UTF16(hddBus, &hddBusUtf16);

    def->ndisks = 0;
    machine->vtbl->GetHardDisk(machine, hddBusUtf16, 0, 0,  &hardDiskPM);
    if (hardDiskPM)
        def->ndisks++;

    machine->vtbl->GetHardDisk(machine, hddBusUtf16, 0, 1,  &hardDiskPS);
    if (hardDiskPS)
        def->ndisks++;

    machine->vtbl->GetHardDisk(machine, hddBusUtf16, 1, 1,  &hardDiskSS);
    if (hardDiskSS)
        def->ndisks++;

    VBOX_UTF16_FREE(hddBusUtf16);

    if ((def->ndisks > 0) && (VIR_ALLOC_N(def->disks, def->ndisks) >= 0)) {
        size_t i;
        for (i = 0; i < def->ndisks; i++) {
            if ((def->disks[i] = virDomainDiskDefNew())) {
                def->disks[i]->device = VIR_DOMAIN_DISK_DEVICE_DISK;
                def->disks[i]->bus = VIR_DOMAIN_DISK_BUS_IDE;
                virDomainDiskSetType(def->disks[i],
                                     VIR_STORAGE_TYPE_FILE);
            }
        }
    }

    if (hardDiskPM) {
        PRUnichar *hddlocationUtf16 = NULL;
        char *hddlocation           = NULL;
        PRUint32 hddType            = HardDiskType_Normal;

        hardDiskPM->vtbl->imedium.GetLocation((IMedium *)hardDiskPM, &hddlocationUtf16);
        VBOX_UTF16_TO_UTF8(hddlocationUtf16, &hddlocation);

        hardDiskPM->vtbl->GetType(hardDiskPM, &hddType);

        if (hddType == HardDiskType_Immutable)
            def->disks[hddNum]->src->readonly = true;
        ignore_value(virDomainDiskSetSource(def->disks[hddNum],
                                            hddlocation));
        ignore_value(VIR_STRDUP(def->disks[hddNum]->dst, "hda"));
        hddNum++;

        VBOX_UTF8_FREE(hddlocation);
        VBOX_UTF16_FREE(hddlocationUtf16);
        VBOX_MEDIUM_RELEASE(hardDiskPM);
    }

    if (hardDiskPS) {
        PRUnichar *hddlocationUtf16 = NULL;
        char *hddlocation           = NULL;
        PRUint32 hddType            = HardDiskType_Normal;

        hardDiskPS->vtbl->imedium.GetLocation((IMedium *)hardDiskPS, &hddlocationUtf16);
        VBOX_UTF16_TO_UTF8(hddlocationUtf16, &hddlocation);

        hardDiskPS->vtbl->GetType(hardDiskPS, &hddType);

        if (hddType == HardDiskType_Immutable)
            def->disks[hddNum]->src->readonly = true;
        ignore_value(virDomainDiskSetSource(def->disks[hddNum],
                                            hddlocation));
        ignore_value(VIR_STRDUP(def->disks[hddNum]->dst, "hdb"));
        hddNum++;

        VBOX_UTF8_FREE(hddlocation);
        VBOX_UTF16_FREE(hddlocationUtf16);
        VBOX_MEDIUM_RELEASE(hardDiskPS);
    }

    if (hardDiskSS) {
        PRUnichar *hddlocationUtf16 = NULL;
        char *hddlocation           = NULL;
        PRUint32 hddType            = HardDiskType_Normal;

        hardDiskSS->vtbl->imedium.GetLocation((IMedium *)hardDiskSS, &hddlocationUtf16);
        VBOX_UTF16_TO_UTF8(hddlocationUtf16, &hddlocation);

        hardDiskSS->vtbl->GetType(hardDiskSS, &hddType);

        if (hddType == HardDiskType_Immutable)
            def->disks[hddNum]->src->readonly = true;
        ignore_value(virDomainDiskSetSource(def->disks[hddNum],
                                            hddlocation));
        ignore_value(VIR_STRDUP(def->disks[hddNum]->dst, "hdd"));
        hddNum++;

        VBOX_UTF8_FREE(hddlocation);
        VBOX_UTF16_FREE(hddlocationUtf16);
        VBOX_MEDIUM_RELEASE(hardDiskSS);
    }
}

static void
_dumpDVD(virDomainDefPtr def,
         vboxGlobalData *data,
         IMachine *machine)
{
    IDVDDrive *dvdDrive      = NULL;
    IDVDImage *dvdImage      = NULL;
    PRUnichar *locationUtf16 = NULL;
    char *location           = NULL;


    /* dump CDROM/DVD if the drive is attached and has DVD/CD in it */
    machine->vtbl->GetDVDDrive(machine, &dvdDrive);
    if (!dvdDrive)
        return;

    PRUint32 state = DriveState_Null;

    dvdDrive->vtbl->GetState(dvdDrive, &state);
    if (state != DriveState_ImageMounted)
        goto cleanupDVDDrive;


    dvdDrive->vtbl->GetImage(dvdDrive, &dvdImage);
    if (!dvdImage)
        goto cleanupDVDDrive;

    dvdImage->vtbl->imedium.GetLocation((IMedium *)dvdImage, &locationUtf16);
    VBOX_UTF16_TO_UTF8(locationUtf16, &location);

    def->ndisks++;
    if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) {
        if ((def->disks[def->ndisks - 1] = virDomainDiskDefNew())) {
            def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
            def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_IDE;
            virDomainDiskSetType(def->disks[def->ndisks - 1],
                                 VIR_STORAGE_TYPE_FILE);
            def->disks[def->ndisks - 1]->src->readonly = true;
            ignore_value(virDomainDiskSetSource(def->disks[def->ndisks - 1], location));
            ignore_value(VIR_STRDUP(def->disks[def->ndisks - 1]->dst, "hdc"));
            def->ndisks--;
        } else {
            def->ndisks--;
        }
    } else {
        def->ndisks--;
    }

    VBOX_UTF8_FREE(location);
    VBOX_UTF16_FREE(locationUtf16);
    VBOX_MEDIUM_RELEASE(dvdImage);

 cleanupDVDDrive:
    VBOX_RELEASE(dvdDrive);
}

T
Taowei 已提交
4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168
static int
_attachDVD(vboxGlobalData *data, IMachine *machine, const char *src)
{
    IDVDDrive *dvdDrive     = NULL;
    IDVDImage *dvdImage     = NULL;
    PRUnichar *dvdfileUtf16 = NULL;
    vboxIID dvduuid = VBOX_IID_INITIALIZER;
    vboxIID dvdemptyuuid = VBOX_IID_INITIALIZER;
    nsresult rc;
    int ret = -1;

    /* Currently CDROM/DVD Drive is always IDE
     * Secondary Master so neglecting the following
     * parameter dev->data.disk->bus
     */
    machine->vtbl->GetDVDDrive(machine, &dvdDrive);
    if (!dvdDrive)
        return ret;

    VBOX_UTF8_TO_UTF16(src, &dvdfileUtf16);

    data->vboxObj->vtbl->FindDVDImage(data->vboxObj, dvdfileUtf16, &dvdImage);
    if (!dvdImage) {
        data->vboxObj->vtbl->OpenDVDImage(data->vboxObj, dvdfileUtf16, dvdemptyuuid.value, &dvdImage);
    }

    if (!dvdImage)
        goto cleanup;

    rc = dvdImage->vtbl->imedium.GetId((IMedium *)dvdImage, &dvduuid.value);
    if (NS_FAILED(rc)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("can't get the uuid of the file to "
                         "be attached to cdrom: %s, rc=%08x"),
                       src, (unsigned)rc);
    } else {
        /* unmount the previous mounted image */
        dvdDrive->vtbl->Unmount(dvdDrive);
        rc = dvdDrive->vtbl->MountImage(dvdDrive, dvduuid.value);
        if (NS_FAILED(rc)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("could not attach the file to cdrom: %s, rc=%08x"),
                           src, (unsigned)rc);
        } else {
            ret = 0;
            DEBUGIID("CD/DVD Image UUID:", dvduuid.value);
        }
    }

    VBOX_MEDIUM_RELEASE(dvdImage);
 cleanup:
    vboxIIDUnalloc(&dvduuid);
    VBOX_UTF16_FREE(dvdfileUtf16);
    VBOX_RELEASE(dvdDrive);
    return ret;
}

T
Taowei 已提交
4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195
static int
_detachDVD(IMachine *machine)
{
    IDVDDrive *dvdDrive = NULL;
    int ret = -1;
    nsresult rc;
    /* Currently CDROM/DVD Drive is always IDE
     * Secondary Master so neglecting the following
     * parameter dev->data.disk->bus
     */
    machine->vtbl->GetDVDDrive(machine, &dvdDrive);
    if (!dvdDrive)
        return ret;

    rc = dvdDrive->vtbl->Unmount(dvdDrive);
    if (NS_FAILED(rc)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("could not de-attach the mounted ISO, rc=%08x"),
                       (unsigned)rc);
    } else {
        ret = 0;
    }
    VBOX_RELEASE(dvdDrive);

    return ret;
}

T
Taowei 已提交
4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
static void
_dumpFloppy(virDomainDefPtr def,
            vboxGlobalData *data,
            IMachine *machine)
{
    IFloppyDrive *floppyDrive = NULL;
    IFloppyImage *floppyImage = NULL;
    PRUnichar *locationUtf16  = NULL;
    char *location            = NULL;
    PRBool enabled            = PR_FALSE;
    PRUint32 state            = DriveState_Null;

    /* dump Floppy if the drive is attached and has floppy in it */
    machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
    if (!floppyDrive)
        return;

    floppyDrive->vtbl->GetEnabled(floppyDrive, &enabled);
    if (!enabled)
        goto cleanupFloppyDrive;


    floppyDrive->vtbl->GetState(floppyDrive, &state);
    if (state != DriveState_ImageMounted)
        goto cleanupFloppyDrive;

    floppyDrive->vtbl->GetImage(floppyDrive, &floppyImage);
    if (!floppyImage)
        goto cleanupFloppyDrive;

    floppyImage->vtbl->imedium.GetLocation((IMedium *)floppyImage, &locationUtf16);
    VBOX_UTF16_TO_UTF8(locationUtf16, &location);

    def->ndisks++;
    if (VIR_REALLOC_N(def->disks, def->ndisks) >= 0) {
        if ((def->disks[def->ndisks - 1] = virDomainDiskDefNew())) {
            def->disks[def->ndisks - 1]->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
            def->disks[def->ndisks - 1]->bus = VIR_DOMAIN_DISK_BUS_FDC;
            virDomainDiskSetType(def->disks[def->ndisks - 1],
                                 VIR_STORAGE_TYPE_FILE);
            def->disks[def->ndisks - 1]->src->readonly = false;
            ignore_value(virDomainDiskSetSource(def->disks[def->ndisks - 1], location));
            ignore_value(VIR_STRDUP(def->disks[def->ndisks - 1]->dst, "fda"));
            def->ndisks--;
        } else {
            def->ndisks--;
        }
    } else {
        def->ndisks--;
    }

    VBOX_UTF8_FREE(location);
    VBOX_UTF16_FREE(locationUtf16);
    VBOX_MEDIUM_RELEASE(floppyImage);

 cleanupFloppyDrive:
    VBOX_RELEASE(floppyDrive);
}

T
Taowei 已提交
4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313
static int
_attachFloppy(vboxGlobalData *data, IMachine *machine, const char *src)
{
    IFloppyDrive *floppyDrive;
    IFloppyImage *floppyImage   = NULL;
    PRUnichar *fdfileUtf16      = NULL;
    vboxIID fduuid = VBOX_IID_INITIALIZER;
    vboxIID fdemptyuuid = VBOX_IID_INITIALIZER;
    nsresult rc;
    int ret = -1;

    machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
    if (!floppyDrive)
        return ret;

    rc = floppyDrive->vtbl->SetEnabled(floppyDrive, 1);
    if (NS_FAILED(rc))
        goto cleanup;

    VBOX_UTF8_TO_UTF16(src, &fdfileUtf16);
    rc = data->vboxObj->vtbl->FindFloppyImage(data->vboxObj,
                                              fdfileUtf16,
                                              &floppyImage);

    if (!floppyImage) {
        data->vboxObj->vtbl->OpenFloppyImage(data->vboxObj,
                                             fdfileUtf16,
                                             fdemptyuuid.value,
                                             &floppyImage);
    }

    if (floppyImage) {
        rc = floppyImage->vtbl->imedium.GetId((IMedium *)floppyImage, &fduuid.value);
        if (NS_FAILED(rc)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("can't get the uuid of the file to be "
                             "attached to floppy drive: %s, rc=%08x"),
                           src, (unsigned)rc);
        } else {
            rc = floppyDrive->vtbl->MountImage(floppyDrive, fduuid.value);
            if (NS_FAILED(rc)) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("could not attach the file to floppy drive: %s, rc=%08x"),
                               src, (unsigned)rc);
            } else {
                ret = 0;
                DEBUGIID("attached floppy, UUID:", fduuid.value);
            }
        }
        VBOX_MEDIUM_RELEASE(floppyImage);
    }
    vboxIIDUnalloc(&fduuid);
    VBOX_UTF16_FREE(fdfileUtf16);

 cleanup:
    VBOX_RELEASE(floppyDrive);
    return ret;
}

T
Taowei 已提交
4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
static int
_detachFloppy(IMachine *machine)
{
    IFloppyDrive *floppyDrive;
    int ret = -1;
    nsresult rc;

    machine->vtbl->GetFloppyDrive(machine, &floppyDrive);
    if (!floppyDrive)
        return ret;

    PRBool enabled = PR_FALSE;

    floppyDrive->vtbl->GetEnabled(floppyDrive, &enabled);
    if (enabled) {
        rc = floppyDrive->vtbl->Unmount(floppyDrive);
        if (NS_FAILED(rc)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("could not attach the file "
                             "to floppy drive, rc=%08x"),
                           (unsigned)rc);
        } else {
            ret = 0;
        }
    } else {
        /* If you are here means floppy drive is already unmounted
         * so don't flag error, just say everything is fine and quit
         */
        ret = 0;
    }
    VBOX_RELEASE(floppyDrive);

    return ret;
}

T
Taowei 已提交
4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366
#else  /* VBOX_API_VERSION >= 3001000 */

static void
_dumpIDEHDDsOld(virDomainDefPtr def ATTRIBUTE_UNUSED,
                vboxGlobalData *data ATTRIBUTE_UNUSED,
                IMachine *machine ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
}

static void
_dumpDVD(virDomainDefPtr def ATTRIBUTE_UNUSED,
         vboxGlobalData *data ATTRIBUTE_UNUSED,
         IMachine *machine ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
}

T
Taowei 已提交
4367 4368 4369 4370 4371 4372 4373 4374 4375
static int
_attachDVD(vboxGlobalData *data ATTRIBUTE_UNUSED,
           IMachine *machine ATTRIBUTE_UNUSED,
           const char *src ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

T
Taowei 已提交
4376 4377 4378 4379 4380 4381 4382
static int
_detachDVD(IMachine *machine ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

T
Taowei 已提交
4383 4384 4385 4386 4387 4388 4389 4390
static void
_dumpFloppy(virDomainDefPtr def ATTRIBUTE_UNUSED,
            vboxGlobalData *data ATTRIBUTE_UNUSED,
            IMachine *machine ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
}

T
Taowei 已提交
4391 4392 4393 4394 4395 4396 4397 4398 4399
static int
_attachFloppy(vboxGlobalData *data ATTRIBUTE_UNUSED,
              IMachine *machine ATTRIBUTE_UNUSED,
              const char *src ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

T
Taowei 已提交
4400 4401 4402 4403 4404 4405 4406
static int
_detachFloppy(IMachine *machine ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

T
Taowei 已提交
4407 4408
#endif  /* VBOX_API_VERSION >= 3001000 */

T
Taowei 已提交
4409 4410 4411 4412
static void _pfnUninitialize(vboxGlobalData *data)
{
    if (data->pFuncs)
        data->pFuncs->pfnComUninitialize();
4413
}
4414

T
Taowei 已提交
4415
static void _pfnComUnallocMem(PCVBOXXPCOM pFuncs, void *pv)
4416
{
T
Taowei 已提交
4417 4418
    pFuncs->pfnComUnallocMem(pv);
}
4419

T
Taowei 已提交
4420 4421 4422 4423
static void _pfnUtf16Free(PCVBOXXPCOM pFuncs, PRUnichar *pwszString)
{
    pFuncs->pfnUtf16Free(pwszString);
}
E
Eric Blake 已提交
4424

T
Taowei 已提交
4425 4426 4427 4428
static void _pfnUtf8Free(PCVBOXXPCOM pFuncs, char *pszString)
{
    pFuncs->pfnUtf8Free(pszString);
}
4429

T
Taowei 已提交
4430 4431 4432 4433
static int _pfnUtf16ToUtf8(PCVBOXXPCOM pFuncs, const PRUnichar *pwszString, char **ppszString)
{
    return pFuncs->pfnUtf16ToUtf8(pwszString, ppszString);
}
4434

T
Taowei 已提交
4435 4436 4437 4438
static int _pfnUtf8ToUtf16(PCVBOXXPCOM pFuncs, const char *pszString, PRUnichar **ppwszString)
{
    return pFuncs->pfnUtf8ToUtf16(pszString, ppwszString);
}
4439

T
Taowei 已提交
4440
#if VBOX_API_VERSION == 2002000
4441

T
Taowei 已提交
4442 4443 4444 4445
static void _vboxIIDInitialize(vboxIIDUnion *iidu)
{
    memset(iidu, 0, sizeof(vboxIIDUnion));
}
4446

T
Taowei 已提交
4447 4448 4449 4450 4451 4452 4453 4454
static void _DEBUGIID(const char *msg, vboxIIDUnion *iidu)
{
# ifdef WIN32
    DEBUGUUID(msg, (nsID *)&IID_MEMBER(value));
# else /* !WIN32 */
    DEBUGUUID(msg, IID_MEMBER(value));
# endif /* !WIN32 */
}
4455

T
Taowei 已提交
4456
#else /* VBOX_API_VERSION != 2002000 */
4457

T
Taowei 已提交
4458 4459 4460 4461 4462
static void _vboxIIDInitialize(vboxIIDUnion *iidu)
{
    memset(iidu, 0, sizeof(vboxIIDUnion));
    IID_MEMBER(owner) = true;
}
4463

T
Taowei 已提交
4464 4465 4466 4467
static void _DEBUGIID(const char *msg, vboxIIDUnion *iidu)
{
    DEBUGPRUnichar(msg, IID_MEMBER(value));
}
4468

T
Taowei 已提交
4469
#endif /* VBOX_API_VERSION != 2002000 */
4470

4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482
static void
_vboxIIDToUtf8(vboxGlobalData *data ATTRIBUTE_UNUSED,
               vboxIIDUnion *iidu ATTRIBUTE_UNUSED,
               char **utf8 ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION == 2002000
    vboxUnsupported();
#else /* !(VBOX_API_VERSION == 2002000) */
    data->pFuncs->pfnUtf16ToUtf8(IID_MEMBER(value), utf8);
#endif /* !(VBOX_API_VERSION == 2002000) */
}

4483 4484 4485 4486 4487 4488
static nsresult
_vboxArrayGetWithIIDArg(vboxArray *array, void *self, void *getter, vboxIIDUnion *iidu)
{
    return vboxArrayGetWithPtrArg(array, self, getter, IID_MEMBER(value));
}

T
Taowei 已提交
4489 4490 4491 4492
static void* _handleGetMachines(IVirtualBox *vboxObj)
{
    return vboxObj->vtbl->GetMachines;
}
4493

T
Taowei 已提交
4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512
static void* _handleUSBGetDeviceFilters(IUSBCommon *USBCommon)
{
    return USBCommon->vtbl->GetDeviceFilters;
}

static void* _handleMachineGetMediumAttachments(IMachine *machine)
{
#if VBOX_API_VERSION < 3001000
    return machine->vtbl->GetHardDiskAttachments;
#else /* VBOX_API_VERSION >= 3001000 */
    return machine->vtbl->GetMediumAttachments;
#endif /* VBOX_API_VERSION >= 3001000 */
}

static void* _handleMachineGetSharedFolders(IMachine *machine)
{
    return machine->vtbl->GetSharedFolders;
}

4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532
static void* _handleSnapshotGetChildren(ISnapshot *snapshot)
{
    return snapshot->vtbl->GetChildren;
}

static void* _handleMediumGetChildren(IMedium *medium ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 3001000
    vboxUnsupported();
    return 0;
#else /* VBOX_API_VERSION >= 3001000 */
    return medium->vtbl->GetChildren;
#endif /* VBOX_API_VERSION >= 3001000 */
}

static void* _handleMediumGetSnapshotIds(IMedium *medium)
{
    return medium->vtbl->GetSnapshotIds;
}

T
Taowei 已提交
4533 4534 4535 4536
static nsresult _nsisupportsRelease(nsISupports *nsi)
{
    return nsi->vtbl->Release(nsi);
}
4537

4538 4539 4540 4541 4542
static nsresult _nsisupportsAddRef(nsISupports *nsi)
{
    return nsi->vtbl->AddRef(nsi);
}

T
Taowei 已提交
4543 4544 4545 4546 4547
static nsresult
_virtualboxGetVersion(IVirtualBox *vboxObj, PRUnichar **versionUtf16)
{
    return vboxObj->vtbl->GetVersion(vboxObj, versionUtf16);
}
4548

T
Taowei 已提交
4549
#if VBOX_API_VERSION < 4000000
4550

T
Taowei 已提交
4551 4552 4553 4554 4555
static nsresult
_virtualboxGetMachine(IVirtualBox *vboxObj, vboxIIDUnion *iidu, IMachine **machine)
{
    return vboxObj->vtbl->GetMachine(vboxObj, IID_MEMBER(value), machine);
}
4556

T
Taowei 已提交
4557
#else /* VBOX_API_VERSION >= 4000000 */
4558

T
Taowei 已提交
4559 4560 4561 4562
static nsresult
_virtualboxGetMachine(IVirtualBox *vboxObj, vboxIIDUnion *iidu, IMachine **machine)
{
    return vboxObj->vtbl->FindMachine(vboxObj, IID_MEMBER(value), machine);
4563
}
T
Taowei 已提交
4564

4565
#endif /* VBOX_API_VERSION >= 4000000 */
4566

4567 4568 4569 4570 4571 4572
static nsresult
_virtualboxOpenMachine(IVirtualBox *vboxObj, PRUnichar *settingsFile, IMachine **machine)
{
    return vboxObj->vtbl->OpenMachine(vboxObj, settingsFile, machine);
}

T
Taowei 已提交
4573 4574 4575 4576 4577
static nsresult
_virtualboxGetSystemProperties(IVirtualBox *vboxObj, ISystemProperties **systemProperties)
{
    return vboxObj->vtbl->GetSystemProperties(vboxObj, systemProperties);
}
4578

T
Taowei 已提交
4579 4580
static nsresult
_virtualboxCreateMachine(vboxGlobalData *data, virDomainDefPtr def, IMachine **machine, char *uuidstr ATTRIBUTE_UNUSED)
4581 4582
{
    vboxIID iid = VBOX_IID_INITIALIZER;
T
Taowei 已提交
4583
    PRUnichar *machineNameUtf16 = NULL;
4584 4585
    nsresult rc;

T
Taowei 已提交
4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636
    VBOX_UTF8_TO_UTF16(def->name, &machineNameUtf16);
    vboxIIDFromUUID(&iid, def->uuid);
    {
#if VBOX_API_VERSION < 3002000
        rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
                                                    machineNameUtf16,
                                                    NULL,
                                                    NULL,
                                                    iid.value,
                                                    machine);
#elif VBOX_API_VERSION < 4000000 /* 3002000 <= VBOX_API_VERSION < 4000000 */
        PRBool override             = PR_FALSE;
        rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
                                                machineNameUtf16,
                                                NULL,
                                                NULL,
                                                iid.value,
                                                override,
                                                machine);
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
        PRBool override             = PR_FALSE;
        rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
                                                NULL,
                                                machineNameUtf16,
                                                NULL,
                                                iid.value,
                                                override,
                                                machine);
#else /* VBOX_API_VERSION >= 4002000 */
        const char *flagsUUIDPrefix = "UUID=";
        const char *flagsForceOverwrite = "forceOverwrite=0";
        const char *flagsSeparator = ",";
        char createFlags[strlen(flagsUUIDPrefix) + VIR_UUID_STRING_BUFLEN + strlen(flagsSeparator) + strlen(flagsForceOverwrite) + 1];
        PRUnichar *createFlagsUtf16 = NULL;

        snprintf(createFlags, sizeof(createFlags), "%s%s%s%s",
                 flagsUUIDPrefix,
                 uuidstr,
                 flagsSeparator,
                 flagsForceOverwrite
                );
        VBOX_UTF8_TO_UTF16(createFlags, &createFlagsUtf16);
        rc = data->vboxObj->vtbl->CreateMachine(data->vboxObj,
                                                NULL,
                                                machineNameUtf16,
                                                0,
                                                nsnull,
                                                nsnull,
                                                createFlagsUtf16,
                                                machine);
#endif /* VBOX_API_VERSION >= 4002000 */
4637
    }
T
Taowei 已提交
4638 4639 4640 4641
    VBOX_UTF16_FREE(machineNameUtf16);
    vboxIIDUnalloc(&iid);
    return rc;
}
4642

4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656
static nsresult
_virtualboxCreateHardDiskMedium(IVirtualBox *vboxObj ATTRIBUTE_UNUSED,
                                PRUnichar *format ATTRIBUTE_UNUSED,
                                PRUnichar *location ATTRIBUTE_UNUSED,
                                IMedium **medium ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 3001000
    vboxUnsupported();
    return 0;
#else /* VBOX_API_VERSION >= 3001000 */
    return vboxObj->vtbl->CreateHardDisk(vboxObj, format, location, medium);
#endif /* VBOX_API_VERSION >= 3001000 */
}

T
Taowei 已提交
4657 4658 4659 4660 4661
static nsresult
_virtualboxRegisterMachine(IVirtualBox *vboxObj, IMachine *machine)
{
    return vboxObj->vtbl->RegisterMachine(vboxObj, machine);
}
4662

T
Taowei 已提交
4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680
static nsresult
_virtualboxFindMedium(IVirtualBox *vboxObj ATTRIBUTE_UNUSED,
                      PRUnichar *location ATTRIBUTE_UNUSED,
                      PRUint32 deviceType ATTRIBUTE_UNUSED,
                      PRUint32 accessMode ATTRIBUTE_UNUSED,
                      IMedium **medium ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
    return vboxObj->vtbl->FindMedium(vboxObj, location,
                                     deviceType, medium);
#elif VBOX_API_VERSION >= 4002000
    return vboxObj->vtbl->OpenMedium(vboxObj, location,
                                     deviceType, accessMode, PR_FALSE, medium);
#else
    vboxUnsupported();
    return 0;
#endif
}
4681

T
Taowei 已提交
4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704
static nsresult
_virtualboxOpenMedium(IVirtualBox *vboxObj ATTRIBUTE_UNUSED,
                      PRUnichar *location ATTRIBUTE_UNUSED,
                      PRUint32 deviceType ATTRIBUTE_UNUSED,
                      PRUint32 accessMode ATTRIBUTE_UNUSED,
                      IMedium **medium ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION == 4000000
    return vboxObj->vtbl->OpenMedium(vboxObj,
                                     location,
                                     deviceType, accessMode,
                                     medium);
#elif VBOX_API_VERSION >= 4001000
    return vboxObj->vtbl->OpenMedium(vboxObj,
                                     location,
                                     deviceType, accessMode,
                                     false,
                                     medium);
#else
    vboxUnsupported();
    return 0;
#endif
}
4705

T
Taowei 已提交
4706 4707 4708 4709 4710 4711 4712 4713
static nsresult
_machineAddStorageController(IMachine *machine, PRUnichar *name,
                             PRUint32 connectionType,
                             IStorageController **controller)
{
    return machine->vtbl->AddStorageController(machine, name, connectionType,
                                               controller);
}
4714

T
Taowei 已提交
4715 4716 4717 4718 4719 4720 4721 4722
static nsresult
_machineGetStorageControllerByName(IMachine *machine, PRUnichar *name,
                                   IStorageController **storageController)
{
    return machine->vtbl->GetStorageControllerByName(machine, name,
                                                     storageController);
}

T
Taowei 已提交
4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738
static nsresult
_machineAttachDevice(IMachine *machine ATTRIBUTE_UNUSED,
                     PRUnichar *name ATTRIBUTE_UNUSED,
                     PRInt32 controllerPort ATTRIBUTE_UNUSED,
                     PRInt32 device ATTRIBUTE_UNUSED,
                     PRUint32 type ATTRIBUTE_UNUSED,
                     IMedium * medium ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION >= 4000000
    return machine->vtbl->AttachDevice(machine, name, controllerPort,
                                       device, type, medium);
#else /* VBOX_API_VERSION < 4000000 */
    vboxUnsupported();
    return 0;
#endif /* VBOX_API_VERSION < 4000000 */
}
4739

T
Taowei 已提交
4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752
static nsresult
_machineCreateSharedFolder(IMachine *machine, PRUnichar *name,
                           PRUnichar *hostPath, PRBool writable,
                           PRBool automount ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 4000000
    return machine->vtbl->CreateSharedFolder(machine, name, hostPath,
                                             writable);
#else /* VBOX_API_VERSION >= 4000000 */
    return machine->vtbl->CreateSharedFolder(machine, name, hostPath,
                                             writable, automount);
#endif /* VBOX_API_VERSION >= 4000000 */
}
4753

T
Taowei 已提交
4754 4755 4756 4757 4758 4759
static nsresult
_machineRemoveSharedFolder(IMachine *machine, PRUnichar *name)
{
    return machine->vtbl->RemoveSharedFolder(machine, name);
}

T
Taowei 已提交
4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778
static nsresult
_machineLaunchVMProcess(vboxGlobalData *data,
                        IMachine *machine ATTRIBUTE_UNUSED,
                        vboxIIDUnion *iidu ATTRIBUTE_UNUSED,
                        PRUnichar *sessionType, PRUnichar *env,
                        IProgress **progress)
{
#if VBOX_API_VERSION < 4000000
    return data->vboxObj->vtbl->OpenRemoteSession(data->vboxObj,
                                                  data->vboxSession,
                                                  IID_MEMBER(value),
                                                  sessionType,
                                                  env,
                                                  progress);
#else /* VBOX_API_VERSION >= 4000000 */
    return machine->vtbl->LaunchVMProcess(machine, data->vboxSession,
                                          sessionType, env, progress);
#endif /* VBOX_API_VERSION >= 4000000 */
}
4779

4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793
static nsresult
_machineUnregister(IMachine *machine ATTRIBUTE_UNUSED,
                   PRUint32 cleanupMode ATTRIBUTE_UNUSED,
                   PRUint32 *aMediaSize ATTRIBUTE_UNUSED,
                   IMedium ***aMedia ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 4000000
    vboxUnsupported();
    return 0;
#else /* VBOX_API_VERSION >= 4000000 */
    return machine->vtbl->Unregister(machine, cleanupMode, aMediaSize, aMedia);
#endif /* VBOX_API_VERSION >= 4000000 */
}

4794 4795 4796 4797 4798 4799 4800 4801 4802 4803
static nsresult
_machineFindSnapshot(IMachine *machine, vboxIIDUnion *iidu, ISnapshot **snapshot)
{
#if VBOX_API_VERSION < 4000000
    return machine->vtbl->GetSnapshot(machine, IID_MEMBER(value), snapshot);
#else /* VBOX_API_VERSION >= 4000000 */
    return machine->vtbl->FindSnapshot(machine, IID_MEMBER(value), snapshot);
#endif /* VBOX_API_VERSION >= 4000000 */
}

T
Taowei 已提交
4804 4805 4806 4807 4808
static nsresult
_machineGetAccessible(IMachine *machine, PRBool *isAccessible)
{
    return machine->vtbl->GetAccessible(machine, isAccessible);
}
4809

T
Taowei 已提交
4810 4811 4812 4813 4814
static nsresult
_machineGetState(IMachine *machine, PRUint32 *state)
{
    return machine->vtbl->GetState(machine, state);
}
4815

T
Taowei 已提交
4816 4817 4818 4819 4820
static nsresult
_machineGetName(IMachine *machine, PRUnichar **name)
{
    return machine->vtbl->GetName(machine, name);
}
4821

T
Taowei 已提交
4822 4823 4824 4825 4826
static nsresult
_machineGetId(IMachine *machine, vboxIIDUnion *iidu)
{
    return machine->vtbl->GetId(machine, &IID_MEMBER(value));
}
4827

T
Taowei 已提交
4828 4829 4830 4831 4832
static nsresult
_machineGetBIOSSettings(IMachine *machine, IBIOSSettings **bios)
{
    return machine->vtbl->GetBIOSSettings(machine, bios);
}
4833

T
Taowei 已提交
4834 4835 4836 4837 4838
static nsresult
_machineGetAudioAdapter(IMachine *machine, IAudioAdapter **audioadapter)
{
    return machine->vtbl->GetAudioAdapter(machine, audioadapter);
}
4839

T
Taowei 已提交
4840 4841 4842 4843 4844
static nsresult
_machineGetNetworkAdapter(IMachine *machine, PRUint32 slot, INetworkAdapter **adapter)
{
    return machine->vtbl->GetNetworkAdapter(machine, slot, adapter);
}
4845

T
Taowei 已提交
4846 4847 4848 4849 4850 4851 4852 4853 4854 4855
static nsresult
_machineGetChipsetType(IMachine *machine ATTRIBUTE_UNUSED, PRUint32 *chipsetType ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION >= 4001000
    return machine->vtbl->GetChipsetType(machine, chipsetType);
#else /* VBOX_API_VERSION < 4001000 */
    vboxUnsupported();
    return 0;
#endif /* VBOX_API_VERSION < 4001000 */
}
4856

T
Taowei 已提交
4857 4858 4859 4860
static nsresult
_machineGetSerialPort(IMachine *machine, PRUint32 slot, ISerialPort **port)
{
    return machine->vtbl->GetSerialPort(machine, slot, port);
4861 4862
}

T
Taowei 已提交
4863 4864 4865 4866 4867
static nsresult
_machineGetParallelPort(IMachine *machine, PRUint32 slot, IParallelPort **port)
{
    return machine->vtbl->GetParallelPort(machine, slot, port);
}
4868

T
Taowei 已提交
4869 4870
static nsresult
_machineGetVRDxServer(IMachine *machine, IVRDxServer **VRDxServer)
4871
{
T
Taowei 已提交
4872 4873 4874 4875 4876
#if VBOX_API_VERSION < 4000000
    return machine->vtbl->GetVRDPServer(machine, VRDxServer);
#else /* VBOX_API_VERSION >= 4000000 */
    return machine->vtbl->GetVRDEServer(machine, VRDxServer);
#endif /* VBOX_API_VERSION >= 4000000 */
4877 4878
}

T
Taowei 已提交
4879 4880 4881 4882 4883 4884 4885 4886 4887
static nsresult
_machineGetUSBCommon(IMachine *machine, IUSBCommon **USBCommon)
{
#if VBOX_API_VERSION < 4003000
    return machine->vtbl->GetUSBController(machine, USBCommon);
#else
    return machine->vtbl->GetUSBDeviceFilters(machine, USBCommon);
#endif
}
4888

4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900
static nsresult
_machineGetCurrentSnapshot(IMachine *machine, ISnapshot **currentSnapshot)
{
    return machine->vtbl->GetCurrentSnapshot(machine, currentSnapshot);
}

static nsresult
_machineGetSettingsFilePath(IMachine *machine, PRUnichar **settingsFilePath)
{
    return machine->vtbl->GetSettingsFilePath(machine, settingsFilePath);
}

T
Taowei 已提交
4901 4902 4903 4904 4905 4906
static nsresult
_machineGetCPUCount(IMachine *machine, PRUint32 *CPUCount)
{
    return machine->vtbl->GetCPUCount(machine, CPUCount);
}

T
Taowei 已提交
4907 4908
static nsresult
_machineSetCPUCount(IMachine *machine, PRUint32 CPUCount)
4909
{
T
Taowei 已提交
4910
    return machine->vtbl->SetCPUCount(machine, CPUCount);
4911 4912
}

T
Taowei 已提交
4913 4914 4915 4916 4917 4918
static nsresult
_machineGetMemorySize(IMachine *machine, PRUint32 *memorySize)
{
    return machine->vtbl->GetMemorySize(machine, memorySize);
}

T
Taowei 已提交
4919 4920 4921 4922 4923
static nsresult
_machineSetMemorySize(IMachine *machine, PRUint32 memorySize)
{
    return machine->vtbl->SetMemorySize(machine, memorySize);
}
4924

T
Taowei 已提交
4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936
static nsresult
_machineGetCPUProperty(IMachine *machine, PRUint32 property ATTRIBUTE_UNUSED, PRBool *value)
{
#if VBOX_API_VERSION < 3001000
    return machine->vtbl->GetPAEEnabled(machine, value);
#elif VBOX_API_VERSION == 3001000
    return machine->vtbl->GetCpuProperty(machine, property, value);
#elif VBOX_API_VERSION >= 3002000
    return machine->vtbl->GetCPUProperty(machine, property, value);
#endif
}

T
Taowei 已提交
4937 4938
static nsresult
_machineSetCPUProperty(IMachine *machine, PRUint32 property ATTRIBUTE_UNUSED, PRBool value)
4939
{
T
Taowei 已提交
4940 4941 4942 4943 4944 4945 4946
#if VBOX_API_VERSION < 3001000
    return machine->vtbl->SetPAEEnabled(machine, value);
#elif VBOX_API_VERSION == 3001000
    return machine->vtbl->SetCpuProperty(machine, property, value);
#elif VBOX_API_VERSION >= 3002000
    return machine->vtbl->SetCPUProperty(machine, property, value);
#endif
4947 4948
}

T
Taowei 已提交
4949 4950 4951 4952 4953 4954
static nsresult
_machineGetBootOrder(IMachine *machine, PRUint32 position, PRUint32 *device)
{
    return machine->vtbl->GetBootOrder(machine, position, device);
}

T
Taowei 已提交
4955 4956 4957 4958 4959
static nsresult
_machineSetBootOrder(IMachine *machine, PRUint32 position, PRUint32 device)
{
    return machine->vtbl->SetBootOrder(machine, position, device);
}
4960

T
Taowei 已提交
4961 4962 4963 4964 4965 4966
static nsresult
_machineGetVRAMSize(IMachine *machine, PRUint32 *VRAMSize)
{
    return machine->vtbl->GetVRAMSize(machine, VRAMSize);
}

T
Taowei 已提交
4967 4968
static nsresult
_machineSetVRAMSize(IMachine *machine, PRUint32 VRAMSize)
4969
{
T
Taowei 已提交
4970 4971
    return machine->vtbl->SetVRAMSize(machine, VRAMSize);
}
4972

T
Taowei 已提交
4973 4974 4975 4976 4977 4978
static nsresult
_machineGetMonitorCount(IMachine *machine, PRUint32 *monitorCount)
{
    return machine->vtbl->GetMonitorCount(machine, monitorCount);
}

T
Taowei 已提交
4979 4980 4981 4982
static nsresult
_machineSetMonitorCount(IMachine *machine, PRUint32 monitorCount)
{
    return machine->vtbl->SetMonitorCount(machine, monitorCount);
4983 4984
}

T
Taowei 已提交
4985 4986 4987 4988 4989 4990
static nsresult
_machineGetAccelerate3DEnabled(IMachine *machine, PRBool *accelerate3DEnabled)
{
    return machine->vtbl->GetAccelerate3DEnabled(machine, accelerate3DEnabled);
}

T
Taowei 已提交
4991 4992
static nsresult
_machineSetAccelerate3DEnabled(IMachine *machine, PRBool accelerate3DEnabled)
T
Taowei 已提交
4993
{
T
Taowei 已提交
4994
    return machine->vtbl->SetAccelerate3DEnabled(machine, accelerate3DEnabled);
T
Taowei 已提交
4995 4996
}

T
Taowei 已提交
4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008
static nsresult
_machineGetAccelerate2DVideoEnabled(IMachine *machine ATTRIBUTE_UNUSED,
                                    PRBool *accelerate2DVideoEnabled ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION >= 3001000
    return machine->vtbl->GetAccelerate2DVideoEnabled(machine, accelerate2DVideoEnabled);
#else /* VBOX_API_VERSION < 3001000 */
    vboxUnsupported();
    return 0;
#endif /* VBOX_API_VERSION < 3001000 */
}

T
Taowei 已提交
5009 5010 5011
static nsresult
_machineSetAccelerate2DVideoEnabled(IMachine *machine ATTRIBUTE_UNUSED,
                                    PRBool accelerate2DVideoEnabled ATTRIBUTE_UNUSED)
T
Taowei 已提交
5012
{
T
Taowei 已提交
5013 5014 5015
#if VBOX_API_VERSION >= 3001000
    return machine->vtbl->SetAccelerate2DVideoEnabled(machine, accelerate2DVideoEnabled);
#else /* VBOX_API_VERSION < 3001000 */
T
Taowei 已提交
5016 5017
    vboxUnsupported();
    return 0;
T
Taowei 已提交
5018
#endif /* VBOX_API_VERSION < 3001000 */
T
Taowei 已提交
5019 5020
}

T
Taowei 已提交
5021 5022 5023 5024 5025 5026
static nsresult
_machineGetExtraData(IMachine *machine, PRUnichar *key, PRUnichar **value)
{
    return machine->vtbl->GetExtraData(machine, key, value);
}

T
Taowei 已提交
5027 5028
static nsresult
_machineSetExtraData(IMachine *machine, PRUnichar *key, PRUnichar *value)
T
Taowei 已提交
5029
{
T
Taowei 已提交
5030 5031 5032
    return machine->vtbl->SetExtraData(machine, key, value);
}

5033 5034 5035 5036 5037 5038
static nsresult
_machineGetSnapshotCount(IMachine *machine, PRUint32 *snapshotCount)
{
    return machine->vtbl->GetSnapshotCount(machine, snapshotCount);
}

T
Taowei 已提交
5039 5040 5041 5042
static nsresult
_machineSaveSettings(IMachine *machine)
{
    return machine->vtbl->SaveSettings(machine);
T
Taowei 已提交
5043 5044
}

T
Taowei 已提交
5045 5046
#if VBOX_API_VERSION < 4000000

T
Taowei 已提交
5047 5048
static nsresult
_sessionOpen(vboxGlobalData *data, vboxIIDUnion *iidu, IMachine *machine ATTRIBUTE_UNUSED)
T
Taowei 已提交
5049
{
T
Taowei 已提交
5050
    return data->vboxObj->vtbl->OpenSession(data->vboxObj, data->vboxSession, IID_MEMBER(value));
T
Taowei 已提交
5051
}
T
Taowei 已提交
5052 5053 5054

static nsresult
_sessionOpenExisting(vboxGlobalData *data, vboxIIDUnion *iidu, IMachine *machine ATTRIBUTE_UNUSED)
T
Taowei 已提交
5055
{
T
Taowei 已提交
5056 5057
    return data->vboxObj->vtbl->OpenExistingSession(data->vboxObj, data->vboxSession, IID_MEMBER(value));
}
T
Taowei 已提交
5058

T
Taowei 已提交
5059 5060 5061 5062 5063
static nsresult
_sessionClose(ISession *session)
{
    return session->vtbl->Close(session);
}
T
Taowei 已提交
5064

T
Taowei 已提交
5065
#else /* VBOX_API_VERSION >= 4000000 */
T
Taowei 已提交
5066

T
Taowei 已提交
5067 5068 5069 5070 5071
static nsresult
_sessionOpen(vboxGlobalData *data, vboxIIDUnion *iidu ATTRIBUTE_UNUSED, IMachine *machine)
{
    return machine->vtbl->LockMachine(machine, data->vboxSession, LockType_Write);
}
T
Taowei 已提交
5072

T
Taowei 已提交
5073 5074 5075 5076 5077
static nsresult
_sessionOpenExisting(vboxGlobalData *data, vboxIIDUnion *iidu ATTRIBUTE_UNUSED, IMachine *machine)
{
    return machine->vtbl->LockMachine(machine, data->vboxSession, LockType_Shared);
}
T
Taowei 已提交
5078

T
Taowei 已提交
5079 5080 5081 5082 5083
static nsresult
_sessionClose(ISession *session)
{
    return session->vtbl->UnlockMachine(session);
}
T
Taowei 已提交
5084

T
Taowei 已提交
5085
#endif /* VBOX_API_VERSION >= 4000000 */
T
Taowei 已提交
5086

T
Taowei 已提交
5087 5088 5089 5090 5091
static nsresult
_sessionGetConsole(ISession *session, IConsole **console)
{
    return session->vtbl->GetConsole(session, console);
}
T
Taowei 已提交
5092

T
Taowei 已提交
5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104
static nsresult
_sessionGetMachine(ISession *session, IMachine **machine)
{
    return session->vtbl->GetMachine(session, machine);
}

static nsresult
_consoleSaveState(IConsole *console, IProgress **progress)
{
    return console->vtbl->SaveState(console, progress);
}

T
Taowei 已提交
5105 5106 5107 5108 5109 5110
static nsresult
_consolePause(IConsole *console)
{
    return console->vtbl->Pause(console);
}

T
Taowei 已提交
5111 5112 5113 5114 5115 5116
static nsresult
_consoleResume(IConsole *console)
{
    return console->vtbl->Resume(console);
}

T
Taowei 已提交
5117 5118 5119 5120 5121 5122
static nsresult
_consolePowerButton(IConsole *console)
{
    return console->vtbl->PowerButton(console);
}

T
Taowei 已提交
5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139
static nsresult
_consolePowerDown(IConsole *console)
{
    nsresult rc;
#if VBOX_API_VERSION == 2002000
    rc = console->vtbl->PowerDown(console);
#else
    IProgress *progress = NULL;
    rc = console->vtbl->PowerDown(console, &progress);
    if (progress) {
        rc = progress->vtbl->WaitForCompletion(progress, -1);
        VBOX_RELEASE(progress);
    }
#endif
    return rc;
}

T
Taowei 已提交
5140 5141 5142 5143 5144 5145
static nsresult
_consoleReset(IConsole *console)
{
    return console->vtbl->Reset(console);
}

5146 5147 5148 5149 5150 5151 5152
static nsresult
_consoleTakeSnapshot(IConsole *console, PRUnichar *name,
                     PRUnichar *description, IProgress **progress)
{
    return console->vtbl->TakeSnapshot(console, name, description, progress);
}

T
Taowei 已提交
5153 5154 5155 5156 5157 5158 5159 5160 5161 5162
static nsresult
_consoleDeleteSnapshot(IConsole *console, vboxIIDUnion *iidu, IProgress **progress)
{
#if VBOX_API_VERSION < 3001000
    return console->vtbl->DiscardSnapshot(console, IID_MEMBER(value), progress);
#else /* VBOX_API_VERSION >= 3001000 */
    return console->vtbl->DeleteSnapshot(console, IID_MEMBER(value), progress);
#endif /* VBOX_API_VERSION >= 3001000 */
}

T
Taowei 已提交
5163 5164 5165 5166 5167 5168
static nsresult
_consoleGetDisplay(IConsole *console, IDisplay **display)
{
    return console->vtbl->GetDisplay(console, display);
}

T
Taowei 已提交
5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184
static nsresult
_progressWaitForCompletion(IProgress *progress, PRInt32 timeout)
{
    return progress->vtbl->WaitForCompletion(progress, timeout);
}

static nsresult
_progressGetResultCode(IProgress *progress, resultCodeUnion *resultCode)
{
#if VBOX_API_VERSION == 2002000
    return progress->vtbl->GetResultCode(progress, &resultCode->uResultCode);
#else /* VBOX_API_VERSION != 2002000 */
    return progress->vtbl->GetResultCode(progress, &resultCode->resultCode);
#endif /* VBOX_API_VERSION != 2002000 */
}

T
Taowei 已提交
5185 5186 5187 5188 5189 5190
static nsresult
_progressGetCompleted(IProgress *progress, PRBool *completed)
{
    return progress->vtbl->GetCompleted(progress, completed);
}

T
Taowei 已提交
5191 5192 5193 5194
static nsresult
_systemPropertiesGetMaxGuestCPUCount(ISystemProperties *systemProperties, PRUint32 *maxCPUCount)
{
    return systemProperties->vtbl->GetMaxGuestCPUCount(systemProperties, maxCPUCount);
T
Taowei 已提交
5195 5196 5197
}

static nsresult
T
Taowei 已提交
5198
_systemPropertiesGetMaxBootPosition(ISystemProperties *systemProperties, PRUint32 *maxBootPosition)
T
Taowei 已提交
5199
{
T
Taowei 已提交
5200
    return systemProperties->vtbl->GetMaxBootPosition(systemProperties, maxBootPosition);
T
Taowei 已提交
5201 5202
}

T
Taowei 已提交
5203 5204 5205
static nsresult
_systemPropertiesGetMaxNetworkAdapters(ISystemProperties *systemProperties, PRUint32 chipset ATTRIBUTE_UNUSED,
                                       PRUint32 *maxNetworkAdapters)
T
Taowei 已提交
5206
{
T
Taowei 已提交
5207 5208 5209 5210 5211 5212 5213
#if VBOX_API_VERSION < 4001000
        return systemProperties->vtbl->GetNetworkAdapterCount(systemProperties,
                                                              maxNetworkAdapters);
#else  /* VBOX_API_VERSION >= 4000000 */
        return systemProperties->vtbl->GetMaxNetworkAdapters(systemProperties, chipset,
                                                             maxNetworkAdapters);
#endif /* VBOX_API_VERSION >= 4000000 */
T
Taowei 已提交
5214 5215
}

T
Taowei 已提交
5216 5217
static nsresult
_systemPropertiesGetSerialPortCount(ISystemProperties *systemProperties, PRUint32 *SerialPortCount)
T
Taowei 已提交
5218
{
T
Taowei 已提交
5219
    return systemProperties->vtbl->GetSerialPortCount(systemProperties, SerialPortCount);
T
Taowei 已提交
5220 5221 5222
}

static nsresult
T
Taowei 已提交
5223
_systemPropertiesGetParallelPortCount(ISystemProperties *systemProperties, PRUint32 *ParallelPortCount)
T
Taowei 已提交
5224
{
T
Taowei 已提交
5225 5226
    return systemProperties->vtbl->GetParallelPortCount(systemProperties, ParallelPortCount);
}
T
Taowei 已提交
5227

T
Taowei 已提交
5228 5229 5230 5231 5232 5233
#if VBOX_API_VERSION >= 3001000
static nsresult
_systemPropertiesGetMaxPortCountForStorageBus(ISystemProperties *systemProperties, PRUint32 bus,
                                              PRUint32 *maxPortCount)
{
    return systemProperties->vtbl->GetMaxPortCountForStorageBus(systemProperties, bus, maxPortCount);
T
Taowei 已提交
5234 5235
}

T
Taowei 已提交
5236 5237 5238
static nsresult
_systemPropertiesGetMaxDevicesPerPortForStorageBus(ISystemProperties *systemProperties,
                                                   PRUint32 bus, PRUint32 *maxDevicesPerPort)
T
Taowei 已提交
5239
{
T
Taowei 已提交
5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251
    return systemProperties->vtbl->GetMaxDevicesPerPortForStorageBus(systemProperties,
                                                                     bus, maxDevicesPerPort);
}
#else /* VBOX_API_VERSION < 3001000 */
static nsresult
_systemPropertiesGetMaxPortCountForStorageBus(ISystemProperties *systemProperties ATTRIBUTE_UNUSED,
                                              PRUint32 bus ATTRIBUTE_UNUSED,
                                              PRUint32 *maxPortCount ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}
T
Taowei 已提交
5252

T
Taowei 已提交
5253 5254 5255 5256 5257 5258 5259 5260 5261
static nsresult
_systemPropertiesGetMaxDevicesPerPortForStorageBus(ISystemProperties *systemProperties ATTRIBUTE_UNUSED,
                                                   PRUint32 bus ATTRIBUTE_UNUSED,
                                                   PRUint32 *maxDevicesPerPort ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}
#endif
T
Taowei 已提交
5262

T
Taowei 已提交
5263 5264 5265 5266 5267 5268
static nsresult
_systemPropertiesGetMaxGuestRAM(ISystemProperties *systemProperties, PRUint32 *maxGuestRAM)
{
    return systemProperties->vtbl->GetMaxGuestRAM(systemProperties, maxGuestRAM);
}

T
Taowei 已提交
5269 5270 5271 5272 5273 5274
static nsresult
_biosSettingsGetACPIEnabled(IBIOSSettings *bios, PRBool *ACPIEnabled)
{
    return bios->vtbl->GetACPIEnabled(bios, ACPIEnabled);
}

T
Taowei 已提交
5275 5276 5277 5278
static nsresult
_biosSettingsSetACPIEnabled(IBIOSSettings *bios, PRBool ACPIEnabled)
{
    return bios->vtbl->SetACPIEnabled(bios, ACPIEnabled);
T
Taowei 已提交
5279 5280
}

T
Taowei 已提交
5281 5282 5283 5284 5285 5286
static nsresult
_biosSettingsGetIOAPICEnabled(IBIOSSettings *bios, PRBool *IOAPICEnabled)
{
    return bios->vtbl->GetIOAPICEnabled(bios, IOAPICEnabled);
}

T
Taowei 已提交
5287 5288 5289 5290 5291
static nsresult
_biosSettingsSetIOAPICEnabled(IBIOSSettings *bios, PRBool IOAPICEnabled)
{
    return bios->vtbl->SetIOAPICEnabled(bios, IOAPICEnabled);
}
T
Taowei 已提交
5292

T
Taowei 已提交
5293 5294 5295 5296 5297 5298
static nsresult
_audioAdapterGetEnabled(IAudioAdapter *audioAdapter, PRBool *enabled)
{
    return audioAdapter->vtbl->GetEnabled(audioAdapter, enabled);
}

T
Taowei 已提交
5299 5300
static nsresult
_audioAdapterSetEnabled(IAudioAdapter *audioAdapter, PRBool enabled)
T
Taowei 已提交
5301
{
T
Taowei 已提交
5302
    return audioAdapter->vtbl->SetEnabled(audioAdapter, enabled);
T
Taowei 已提交
5303 5304
}

T
Taowei 已提交
5305 5306 5307 5308 5309 5310
static nsresult
_audioAdapterGetAudioController(IAudioAdapter *audioAdapter, PRUint32 *audioController)
{
    return audioAdapter->vtbl->GetAudioController(audioAdapter, audioController);
}

T
Taowei 已提交
5311 5312
static nsresult
_audioAdapterSetAudioController(IAudioAdapter *audioAdapter, PRUint32 audioController)
T
Taowei 已提交
5313
{
T
Taowei 已提交
5314
    return audioAdapter->vtbl->SetAudioController(audioAdapter, audioController);
T
Taowei 已提交
5315 5316
}

T
Taowei 已提交
5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328
static nsresult
_networkAdapterGetAttachmentType(INetworkAdapter *adapter, PRUint32 *attachmentType)
{
    return adapter->vtbl->GetAttachmentType(adapter, attachmentType);
}

static nsresult
_networkAdapterGetEnabled(INetworkAdapter *adapter, PRBool *enabled)
{
    return adapter->vtbl->GetEnabled(adapter, enabled);
}

T
Taowei 已提交
5329 5330
static nsresult
_networkAdapterSetEnabled(INetworkAdapter *adapter, PRBool enabled)
T
Taowei 已提交
5331
{
T
Taowei 已提交
5332
    return adapter->vtbl->SetEnabled(adapter, enabled);
T
Taowei 已提交
5333 5334
}

T
Taowei 已提交
5335 5336 5337 5338 5339 5340
static nsresult
_networkAdapterGetAdapterType(INetworkAdapter *adapter, PRUint32 *adapterType)
{
    return adapter->vtbl->GetAdapterType(adapter, adapterType);
}

T
Taowei 已提交
5341 5342
static nsresult
_networkAdapterSetAdapterType(INetworkAdapter *adapter, PRUint32 adapterType)
T
Taowei 已提交
5343
{
T
Taowei 已提交
5344
    return adapter->vtbl->SetAdapterType(adapter, adapterType);
T
Taowei 已提交
5345 5346
}

T
Taowei 已提交
5347 5348 5349 5350 5351 5352
static nsresult
_networkAdapterGetInternalNetwork(INetworkAdapter *adapter, PRUnichar **internalNetwork)
{
    return adapter->vtbl->GetInternalNetwork(adapter, internalNetwork);
}

T
Taowei 已提交
5353 5354
static nsresult
_networkAdapterSetInternalNetwork(INetworkAdapter *adapter, PRUnichar *internalNetwork)
T
Taowei 已提交
5355
{
T
Taowei 已提交
5356
    return adapter->vtbl->SetInternalNetwork(adapter, internalNetwork);
T
Taowei 已提交
5357 5358
}

T
Taowei 已提交
5359 5360 5361 5362 5363 5364
static nsresult
_networkAdapterGetMACAddress(INetworkAdapter *adapter, PRUnichar **MACAddress)
{
    return adapter->vtbl->GetMACAddress(adapter, MACAddress);
}

T
Taowei 已提交
5365 5366
static nsresult
_networkAdapterSetMACAddress(INetworkAdapter *adapter, PRUnichar *MACAddress)
T
Taowei 已提交
5367
{
T
Taowei 已提交
5368
    return adapter->vtbl->SetMACAddress(adapter, MACAddress);
T
Taowei 已提交
5369 5370
}

T
Taowei 已提交
5371
#if VBOX_API_VERSION < 4001000
T
Taowei 已提交
5372

T
Taowei 已提交
5373 5374 5375 5376 5377 5378
static nsresult
_networkAdapterGetBridgedInterface(INetworkAdapter *adapter, PRUnichar **hostInterface)
{
    return adapter->vtbl->GetHostInterface(adapter, hostInterface);
}

T
Taowei 已提交
5379 5380
static nsresult
_networkAdapterSetBridgedInterface(INetworkAdapter *adapter, PRUnichar *hostInterface)
T
Taowei 已提交
5381
{
T
Taowei 已提交
5382
    return adapter->vtbl->SetHostInterface(adapter, hostInterface);
T
Taowei 已提交
5383 5384
}

T
Taowei 已提交
5385 5386 5387 5388 5389 5390
static nsresult
_networkAdapterGetHostOnlyInterface(INetworkAdapter *adapter, PRUnichar **hostOnlyInterface)
{
    return adapter->vtbl->GetHostInterface(adapter, hostOnlyInterface);
}

T
Taowei 已提交
5391 5392
static nsresult
_networkAdapterSetHostOnlyInterface(INetworkAdapter *adapter, PRUnichar *hostOnlyInterface)
T
Taowei 已提交
5393
{
T
Taowei 已提交
5394
    return adapter->vtbl->SetHostInterface(adapter, hostOnlyInterface);
T
Taowei 已提交
5395 5396
}

T
Taowei 已提交
5397 5398
static nsresult
_networkAdapterAttachToBridgedInterface(INetworkAdapter *adapter)
T
Taowei 已提交
5399
{
T
Taowei 已提交
5400
    return adapter->vtbl->AttachToBridgedInterface(adapter);
T
Taowei 已提交
5401 5402
}

T
Taowei 已提交
5403 5404
static nsresult
_networkAdapterAttachToInternalNetwork(INetworkAdapter *adapter)
T
Taowei 已提交
5405
{
T
Taowei 已提交
5406
    return adapter->vtbl->AttachToInternalNetwork(adapter);
T
Taowei 已提交
5407 5408
}

T
Taowei 已提交
5409 5410 5411 5412 5413
static nsresult
_networkAdapterAttachToHostOnlyInterface(INetworkAdapter *adapter)
{
    return adapter->vtbl->AttachToHostOnlyInterface(adapter);
}
T
Taowei 已提交
5414

T
Taowei 已提交
5415 5416
static nsresult
_networkAdapterAttachToNAT(INetworkAdapter *adapter)
T
Taowei 已提交
5417
{
T
Taowei 已提交
5418
    return adapter->vtbl->AttachToNAT(adapter);
T
Taowei 已提交
5419 5420
}

T
Taowei 已提交
5421 5422
#else /* VBOX_API_VERSION >= 4001000 */

T
Taowei 已提交
5423 5424 5425 5426 5427 5428
static nsresult
_networkAdapterGetBridgedInterface(INetworkAdapter *adapter, PRUnichar **bridgedInterface)
{
    return adapter->vtbl->GetBridgedInterface(adapter, bridgedInterface);
}

T
Taowei 已提交
5429 5430
static nsresult
_networkAdapterSetBridgedInterface(INetworkAdapter *adapter, PRUnichar *bridgedInterface)
T
Taowei 已提交
5431
{
T
Taowei 已提交
5432
    return adapter->vtbl->SetBridgedInterface(adapter, bridgedInterface);
T
Taowei 已提交
5433 5434
}

T
Taowei 已提交
5435 5436 5437 5438 5439 5440
static nsresult
_networkAdapterGetHostOnlyInterface(INetworkAdapter *adapter, PRUnichar **hostOnlyInterface)
{
    return adapter->vtbl->GetHostOnlyInterface(adapter, hostOnlyInterface);
}

T
Taowei 已提交
5441
static nsresult
T
Taowei 已提交
5442
_networkAdapterSetHostOnlyInterface(INetworkAdapter *adapter, PRUnichar *hostOnlyInterface)
T
Taowei 已提交
5443
{
T
Taowei 已提交
5444
    return adapter->vtbl->SetHostOnlyInterface(adapter, hostOnlyInterface);
T
Taowei 已提交
5445 5446
}

T
Taowei 已提交
5447 5448 5449 5450 5451
static nsresult
_networkAdapterAttachToBridgedInterface(INetworkAdapter *adapter)
{
    return adapter->vtbl->SetAttachmentType(adapter, NetworkAttachmentType_Bridged);
}
T
Taowei 已提交
5452 5453

static nsresult
T
Taowei 已提交
5454
_networkAdapterAttachToInternalNetwork(INetworkAdapter *adapter)
T
Taowei 已提交
5455
{
T
Taowei 已提交
5456
    return adapter->vtbl->SetAttachmentType(adapter, NetworkAttachmentType_Internal);
T
Taowei 已提交
5457 5458
}

T
Taowei 已提交
5459 5460 5461 5462 5463
static nsresult
_networkAdapterAttachToHostOnlyInterface(INetworkAdapter *adapter)
{
    return adapter->vtbl->SetAttachmentType(adapter, NetworkAttachmentType_HostOnly);
}
T
Taowei 已提交
5464 5465

static nsresult
T
Taowei 已提交
5466
_networkAdapterAttachToNAT(INetworkAdapter *adapter)
T
Taowei 已提交
5467
{
T
Taowei 已提交
5468
    return adapter->vtbl->SetAttachmentType(adapter, NetworkAttachmentType_NAT);
T
Taowei 已提交
5469 5470
}

T
Taowei 已提交
5471
#endif /* VBOX_API_VERSION >= 4001000 */
T
Taowei 已提交
5472

T
Taowei 已提交
5473 5474 5475 5476 5477 5478
static nsresult
_serialPortGetEnabled(ISerialPort *port, PRBool *enabled)
{
    return port->vtbl->GetEnabled(port, enabled);
}

T
Taowei 已提交
5479
static nsresult
T
Taowei 已提交
5480
_serialPortSetEnabled(ISerialPort *port, PRBool enabled)
T
Taowei 已提交
5481
{
T
Taowei 已提交
5482
    return port->vtbl->SetEnabled(port, enabled);
T
Taowei 已提交
5483 5484
}

T
Taowei 已提交
5485 5486 5487 5488 5489 5490
static nsresult
_serialPortGetPath(ISerialPort *port, PRUnichar **path)
{
    return port->vtbl->GetPath(port, path);
}

T
Taowei 已提交
5491
static nsresult
T
Taowei 已提交
5492
_serialPortSetPath(ISerialPort *port, PRUnichar *path)
T
Taowei 已提交
5493
{
T
Taowei 已提交
5494
    return port->vtbl->SetPath(port, path);
T
Taowei 已提交
5495 5496
}

T
Taowei 已提交
5497 5498 5499 5500 5501 5502
static nsresult
_serialPortGetIRQ(ISerialPort *port, PRUint32 *IRQ)
{
    return port->vtbl->GetIRQ(port, IRQ);
}

T
Taowei 已提交
5503
static nsresult
T
Taowei 已提交
5504
_serialPortSetIRQ(ISerialPort *port, PRUint32 IRQ)
T
Taowei 已提交
5505
{
T
Taowei 已提交
5506
    return port->vtbl->SetIRQ(port, IRQ);
T
Taowei 已提交
5507 5508
}

T
Taowei 已提交
5509 5510 5511 5512 5513 5514
static nsresult
_serialPortGetIOBase(ISerialPort *port, PRUint32 *IOBase)
{
    return port->vtbl->GetIOBase(port, IOBase);
}

T
Taowei 已提交
5515
static nsresult
T
Taowei 已提交
5516
_serialPortSetIOBase(ISerialPort *port, PRUint32 IOBase)
T
Taowei 已提交
5517
{
T
Taowei 已提交
5518
    return port->vtbl->SetIOBase(port, IOBase);
T
Taowei 已提交
5519 5520
}

T
Taowei 已提交
5521 5522 5523 5524 5525 5526
static nsresult
_serialPortGetHostMode(ISerialPort *port, PRUint32 *hostMode)
{
    return port->vtbl->GetHostMode(port, hostMode);
}

T
Taowei 已提交
5527
static nsresult
T
Taowei 已提交
5528
_serialPortSetHostMode(ISerialPort *port, PRUint32 hostMode)
T
Taowei 已提交
5529
{
T
Taowei 已提交
5530
    return port->vtbl->SetHostMode(port, hostMode);
T
Taowei 已提交
5531 5532
}

T
Taowei 已提交
5533 5534 5535 5536 5537 5538
static nsresult
_parallelPortGetEnabled(IParallelPort *port, PRBool *enabled)
{
    return port->vtbl->GetEnabled(port, enabled);
}

T
Taowei 已提交
5539
static nsresult
T
Taowei 已提交
5540
_parallelPortSetEnabled(IParallelPort *port, PRBool enabled)
T
Taowei 已提交
5541
{
T
Taowei 已提交
5542
    return port->vtbl->SetEnabled(port, enabled);
T
Taowei 已提交
5543 5544
}

T
Taowei 已提交
5545 5546 5547 5548 5549 5550
static nsresult
_parallelPortGetPath(IParallelPort *port, PRUnichar **path)
{
    return port->vtbl->GetPath(port, path);
}

T
Taowei 已提交
5551
static nsresult
T
Taowei 已提交
5552
_parallelPortSetPath(IParallelPort *port, PRUnichar *path)
T
Taowei 已提交
5553
{
T
Taowei 已提交
5554
    return port->vtbl->SetPath(port, path);
T
Taowei 已提交
5555 5556
}

T
Taowei 已提交
5557 5558 5559 5560 5561 5562
static nsresult
_parallelPortGetIRQ(IParallelPort *port, PRUint32 *IRQ)
{
    return port->vtbl->GetIRQ(port, IRQ);
}

T
Taowei 已提交
5563
static nsresult
T
Taowei 已提交
5564
_parallelPortSetIRQ(IParallelPort *port, PRUint32 IRQ)
T
Taowei 已提交
5565
{
T
Taowei 已提交
5566
    return port->vtbl->SetIRQ(port, IRQ);
T
Taowei 已提交
5567 5568
}

T
Taowei 已提交
5569 5570 5571 5572 5573 5574
static nsresult
_parallelPortGetIOBase(IParallelPort *port, PRUint32 *IOBase)
{
    return port->vtbl->GetIOBase(port, IOBase);
}

T
Taowei 已提交
5575
static nsresult
T
Taowei 已提交
5576
_parallelPortSetIOBase(IParallelPort *port, PRUint32 IOBase)
T
Taowei 已提交
5577
{
T
Taowei 已提交
5578
    return port->vtbl->SetIOBase(port, IOBase);
T
Taowei 已提交
5579 5580
}

T
Taowei 已提交
5581 5582 5583 5584 5585 5586
static nsresult
_vrdxServerGetEnabled(IVRDxServer *VRDxServer, PRBool *enabled)
{
    return VRDxServer->vtbl->GetEnabled(VRDxServer, enabled);
}

T
Taowei 已提交
5587 5588 5589 5590 5591
static nsresult
_vrdxServerSetEnabled(IVRDxServer *VRDxServer, PRBool enabled)
{
    return VRDxServer->vtbl->SetEnabled(VRDxServer, enabled);
}
T
Taowei 已提交
5592

T
Taowei 已提交
5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632
static nsresult
_vrdxServerGetPorts(vboxGlobalData *data ATTRIBUTE_UNUSED,
                    IVRDxServer *VRDxServer, virDomainGraphicsDefPtr graphics)
{
    nsresult rc;
#if VBOX_API_VERSION < 3001000
    PRUint32 VRDPport = 0;
    rc = VRDxServer->vtbl->GetPort(VRDxServer, &VRDPport);
    if (VRDPport) {
        graphics->data.rdp.port = VRDPport;
    } else {
        graphics->data.rdp.autoport = true;
    }
#elif VBOX_API_VERSION < 4000000 /* 3001000 <= VBOX_API_VERSION < 4000000 */
    PRUnichar *VRDPport = NULL;
    rc = VRDxServer->vtbl->GetPorts(VRDxServer, &VRDPport);
    if (VRDPport) {
        /* even if vbox supports mutilpe ports, single port for now here */
        graphics->data.rdp.port = PRUnicharToInt(VRDPport);
        VBOX_UTF16_FREE(VRDPport);
    } else {
        graphics->data.rdp.autoport = true;
    }
#else /* VBOX_API_VERSION >= 4000000 */
    PRUnichar *VRDEPortsKey = NULL;
    PRUnichar *VRDEPortsValue = NULL;
    VBOX_UTF8_TO_UTF16("TCP/Ports", &VRDEPortsKey);
    rc = VRDxServer->vtbl->GetVRDEProperty(VRDxServer, VRDEPortsKey, &VRDEPortsValue);
    VBOX_UTF16_FREE(VRDEPortsKey);
    if (VRDEPortsValue) {
        /* even if vbox supports mutilpe ports, single port for now here */
        graphics->data.rdp.port = PRUnicharToInt(VRDEPortsValue);
        VBOX_UTF16_FREE(VRDEPortsValue);
    } else {
        graphics->data.rdp.autoport = true;
    }
#endif /* VBOX_API_VERSION >= 4000000 */
    return rc;
}

T
Taowei 已提交
5633
static nsresult
T
Taowei 已提交
5634 5635
_vrdxServerSetPorts(vboxGlobalData *data ATTRIBUTE_UNUSED,
                    IVRDxServer *VRDxServer, virDomainGraphicsDefPtr graphics)
T
Taowei 已提交
5636
{
T
Taowei 已提交
5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666
    nsresult rc = 0;
#if VBOX_API_VERSION < 3001000
    if (graphics->data.rdp.port) {
        rc = VRDxServer->vtbl->SetPort(VRDxServer,
                                       graphics->data.rdp.port);
        VIR_DEBUG("VRDP Port changed to: %d",
                  graphics->data.rdp.port);
    } else if (graphics->data.rdp.autoport) {
        /* Setting the port to 0 will reset its value to
         * the default one which is 3389 currently
         */
        rc = VRDxServer->vtbl->SetPort(VRDxServer, 0);
        VIR_DEBUG("VRDP Port changed to default, which is 3389 currently");
    }
#elif VBOX_API_VERSION < 4000000 /* 3001000 <= VBOX_API_VERSION < 4000000 */
    PRUnichar *portUtf16 = NULL;
    portUtf16 = PRUnicharFromInt(graphics->data.rdp.port);
    rc = VRDxServer->vtbl->SetPorts(VRDxServer, portUtf16);
    VBOX_UTF16_FREE(portUtf16);
#else /* VBOX_API_VERSION >= 4000000 */
    PRUnichar *VRDEPortsKey = NULL;
    PRUnichar *VRDEPortsValue = NULL;
    VBOX_UTF8_TO_UTF16("TCP/Ports", &VRDEPortsKey);
    VRDEPortsValue = PRUnicharFromInt(graphics->data.rdp.port);
    rc = VRDxServer->vtbl->SetVRDEProperty(VRDxServer, VRDEPortsKey,
                                           VRDEPortsValue);
    VBOX_UTF16_FREE(VRDEPortsKey);
    VBOX_UTF16_FREE(VRDEPortsValue);
#endif /* VBOX_API_VERSION >= 4000000 */
    return rc;
T
Taowei 已提交
5667 5668
}

T
Taowei 已提交
5669 5670 5671 5672 5673 5674
static nsresult
_vrdxServerGetReuseSingleConnection(IVRDxServer *VRDxServer, PRBool *enabled)
{
    return VRDxServer->vtbl->GetReuseSingleConnection(VRDxServer, enabled);
}

T
Taowei 已提交
5675
static nsresult
T
Taowei 已提交
5676
_vrdxServerSetReuseSingleConnection(IVRDxServer *VRDxServer, PRBool enabled)
T
Taowei 已提交
5677
{
T
Taowei 已提交
5678
    return VRDxServer->vtbl->SetReuseSingleConnection(VRDxServer, enabled);
T
Taowei 已提交
5679 5680
}

T
Taowei 已提交
5681 5682 5683 5684 5685 5686
static nsresult
_vrdxServerGetAllowMultiConnection(IVRDxServer *VRDxServer, PRBool *enabled)
{
    return VRDxServer->vtbl->GetAllowMultiConnection(VRDxServer, enabled);
}

T
Taowei 已提交
5687
static nsresult
T
Taowei 已提交
5688
_vrdxServerSetAllowMultiConnection(IVRDxServer *VRDxServer, PRBool enabled)
T
Taowei 已提交
5689
{
T
Taowei 已提交
5690
    return VRDxServer->vtbl->SetAllowMultiConnection(VRDxServer, enabled);
T
Taowei 已提交
5691 5692
}

T
Taowei 已提交
5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708
static nsresult
_vrdxServerGetNetAddress(vboxGlobalData *data ATTRIBUTE_UNUSED,
                         IVRDxServer *VRDxServer, PRUnichar **netAddress)
{
#if VBOX_API_VERSION >= 4000000
    PRUnichar *VRDENetAddressKey = NULL;
    nsresult rc;
    VBOX_UTF8_TO_UTF16("TCP/Address", &VRDENetAddressKey);
    rc = VRDxServer->vtbl->GetVRDEProperty(VRDxServer, VRDENetAddressKey, netAddress);
    VBOX_UTF16_FREE(VRDENetAddressKey);
    return rc;
#else /* VBOX_API_VERSION < 4000000 */
    return VRDxServer->vtbl->GetNetAddress(VRDxServer, netAddress);
#endif /* VBOX_API_VERSION < 4000000 */
}

T
Taowei 已提交
5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723
static nsresult
_vrdxServerSetNetAddress(vboxGlobalData *data ATTRIBUTE_UNUSED,
                         IVRDxServer *VRDxServer, PRUnichar *netAddress)
{
#if VBOX_API_VERSION < 4000000
    return VRDxServer->vtbl->SetNetAddress(VRDxServer,
                                           netAddress);
#else /* VBOX_API_VERSION >= 4000000 */
    PRUnichar *netAddressKey = NULL;
    nsresult rc;
    VBOX_UTF8_TO_UTF16("TCP/Address", &netAddressKey);
    rc = VRDxServer->vtbl->SetVRDEProperty(VRDxServer, netAddressKey,
                                           netAddress);
    VBOX_UTF16_FREE(netAddressKey);
    return rc;
T
Taowei 已提交
5724
#endif /* VBOX_API_VERSION >= 4000000 */
T
Taowei 已提交
5725
}
T
Taowei 已提交
5726 5727

static nsresult
T
Taowei 已提交
5728
_usbCommonEnable(IUSBCommon *USBCommon ATTRIBUTE_UNUSED)
T
Taowei 已提交
5729
{
T
Taowei 已提交
5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740
    nsresult rc = 0;
#if VBOX_API_VERSION < 4003000
    USBCommon->vtbl->SetEnabled(USBCommon, 1);
# if VBOX_API_VERSION < 4002000
    rc = USBCommon->vtbl->SetEnabledEhci(USBCommon, 1);
# else /* VBOX_API_VERSION >= 4002000 */
    rc = USBCommon->vtbl->SetEnabledEHCI(USBCommon, 1);
# endif /* VBOX_API_VERSION >= 4002000 */
#endif /* VBOX_API_VERSION >= 4003000 */
    /* We don't need to set usb enabled for vbox 4.3 and later */
    return rc;
T
Taowei 已提交
5741 5742
}

T
Taowei 已提交
5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753
static nsresult
_usbCommonGetEnabled(IUSBCommon *USBCommon ATTRIBUTE_UNUSED, PRBool *enabled)
{
#if VBOX_API_VERSION < 4003000
    return USBCommon->vtbl->GetEnabled(USBCommon, enabled);
#else /* VBOX_API_VERSION >= 4003000 */
    *enabled = true;
    return 0;
#endif /* VBOX_API_VERSION >= 4003000 */
}

T
Taowei 已提交
5754
static nsresult
T
Taowei 已提交
5755 5756
_usbCommonCreateDeviceFilter(IUSBCommon *USBCommon, PRUnichar *name,
                             IUSBDeviceFilter **filter)
T
Taowei 已提交
5757
{
T
Taowei 已提交
5758
    return USBCommon->vtbl->CreateDeviceFilter(USBCommon, name, filter);
T
Taowei 已提交
5759 5760
}

T
Taowei 已提交
5761
static nsresult
T
Taowei 已提交
5762 5763
_usbCommonInsertDeviceFilter(IUSBCommon *USBCommon, PRUint32 position,
                             IUSBDeviceFilter *filter)
T
Taowei 已提交
5764
{
T
Taowei 已提交
5765
    return USBCommon->vtbl->InsertDeviceFilter(USBCommon, position, filter);
T
Taowei 已提交
5766 5767
}

T
Taowei 已提交
5768 5769 5770 5771 5772 5773
static nsresult
_usbDeviceFilterGetProductId(IUSBDeviceFilter *USBDeviceFilter, PRUnichar **productId)
{
    return USBDeviceFilter->vtbl->GetProductId(USBDeviceFilter, productId);
}

T
Taowei 已提交
5774
static nsresult
T
Taowei 已提交
5775
_usbDeviceFilterSetProductId(IUSBDeviceFilter *USBDeviceFilter, PRUnichar *productId)
T
Taowei 已提交
5776
{
T
Taowei 已提交
5777
    return USBDeviceFilter->vtbl->SetProductId(USBDeviceFilter, productId);
T
Taowei 已提交
5778 5779
}

T
Taowei 已提交
5780 5781 5782 5783 5784 5785
static nsresult
_usbDeviceFilterGetActive(IUSBDeviceFilter *USBDeviceFilter, PRBool *active)
{
    return USBDeviceFilter->vtbl->GetActive(USBDeviceFilter, active);
}

T
Taowei 已提交
5786
static nsresult
T
Taowei 已提交
5787
_usbDeviceFilterSetActive(IUSBDeviceFilter *USBDeviceFilter, PRBool active)
T
Taowei 已提交
5788
{
T
Taowei 已提交
5789
    return USBDeviceFilter->vtbl->SetActive(USBDeviceFilter, active);
T
Taowei 已提交
5790 5791
}

T
Taowei 已提交
5792 5793 5794 5795 5796 5797
static nsresult
_usbDeviceFilterGetVendorId(IUSBDeviceFilter *USBDeviceFilter, PRUnichar **vendorId)
{
    return USBDeviceFilter->vtbl->GetVendorId(USBDeviceFilter, vendorId);
}

T
Taowei 已提交
5798
static nsresult
T
Taowei 已提交
5799
_usbDeviceFilterSetVendorId(IUSBDeviceFilter *USBDeviceFilter, PRUnichar *vendorId)
T
Taowei 已提交
5800
{
T
Taowei 已提交
5801 5802 5803 5804 5805 5806 5807 5808
    return USBDeviceFilter->vtbl->SetVendorId(USBDeviceFilter, vendorId);
}

static nsresult _mediumGetId(IMedium *medium, vboxIIDUnion *iidu)
{
    return medium->vtbl->GetId(medium, &IID_MEMBER(value));
}

T
Taowei 已提交
5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824
static nsresult _mediumGetLocation(IMedium *medium, PRUnichar **location)
{
    return medium->vtbl->GetLocation(medium, location);
}

static nsresult _mediumGetReadOnly(IMedium *medium ATTRIBUTE_UNUSED,
                                   PRBool *readOnly ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 3001000
    vboxUnsupported();
    return 0;
#else /* VBOX_API_VERSION >= 3001000 */
    return medium->vtbl->GetReadOnly(medium, readOnly);
#endif /* VBOX_API_VERSION >= 3001000 */
}

5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884
#if VBOX_API_VERSION < 3001000

static nsresult _mediumGetParent(IMedium *medium ATTRIBUTE_UNUSED,
                                 IMedium **parent ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

static nsresult _mediumGetChildren(IMedium *medium ATTRIBUTE_UNUSED,
                                   PRUint32 *childrenSize ATTRIBUTE_UNUSED,
                                   IMedium ***children ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

static nsresult _mediumGetFormat(IMedium *medium ATTRIBUTE_UNUSED,
                                 PRUnichar **format ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

static nsresult _mediumDeleteStorage(IMedium *medium ATTRIBUTE_UNUSED,
                                     IProgress **progress ATTRIBUTE_UNUSED)
{
    vboxUnsupported();
    return 0;
}

#else /* VBOX_API_VERSION >= 3001000 */

static nsresult _mediumGetParent(IMedium *medium,
                                 IMedium **parent)
{
    return medium->vtbl->GetParent(medium, parent);
}

static nsresult _mediumGetChildren(IMedium *medium,
                                   PRUint32 *childrenSize,
                                   IMedium ***children)
{
    return medium->vtbl->GetChildren(medium, childrenSize, children);
}

static nsresult _mediumGetFormat(IMedium *medium,
                                 PRUnichar **format)
{
    return medium->vtbl->GetFormat(medium, format);
}

static nsresult _mediumDeleteStorage(IMedium *medium,
                                     IProgress **progress)
{
    return medium->vtbl->DeleteStorage(medium, progress);
}

#endif /* VBOX_API_VERSION >= 3001000 */

T
Taowei 已提交
5885 5886 5887 5888
static nsresult _mediumRelease(IMedium *medium)
{
    return medium->vtbl->nsisupports.Release((nsISupports *)medium);
}
5889 5890 5891 5892
static nsresult _mediumClose(IMedium *medium)
{
    return medium->vtbl->Close(medium);
}
T
Taowei 已提交
5893 5894 5895 5896 5897 5898 5899 5900 5901 5902

static nsresult _mediumSetType(IMedium *medium ATTRIBUTE_UNUSED,
                               PRUint32 type ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION > 3000000
    return medium->vtbl->SetType(medium, type);
#else
    vboxUnsupported();
    return 0;
#endif
T
Taowei 已提交
5903 5904
}

5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925
static nsresult
_mediumCreateDiffStorage(IMedium *medium ATTRIBUTE_UNUSED,
                         IMedium *target ATTRIBUTE_UNUSED,
                         PRUint32 variantSize ATTRIBUTE_UNUSED,
                         PRUint32 *variant ATTRIBUTE_UNUSED,
                         IProgress **progress ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 3001000
    vboxUnsupported();
    return 0;
#elif VBOX_API_VERSION < 4003000
    if (variantSize == 0)
        return 0;
    if (variantSize > 1)
        VIR_WARN("Only one variant is avaible in current version");
    return medium->vtbl->CreateDiffStorage(medium, target, variant[0], progress);
#else /* VBOX_API_VERSION >= 4003000 */
    return medium->vtbl->CreateDiffStorage(medium, target, variantSize, variant, progress);
#endif /* VBOX_API_VERSION >= 4003000 */
}

T
Taowei 已提交
5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992
static nsresult
_mediumAttachmentGetMedium(IMediumAttachment *mediumAttachment ATTRIBUTE_UNUSED,
                           IMedium **medium ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 3001000
    vboxUnsupported();
    return 0;
#else /* VBOX_API_VERSION >= 3001000 */
    return mediumAttachment->vtbl->GetMedium(mediumAttachment, medium);
#endif /* VBOX_API_VERSION >= 3001000 */
}

static nsresult
_mediumAttachmentGetController(IMediumAttachment *mediumAttachment,
                               PRUnichar **controller)
{
    return mediumAttachment->vtbl->GetController(mediumAttachment, controller);
}

static nsresult
_mediumAttachmentGetType(IMediumAttachment *mediumAttachment ATTRIBUTE_UNUSED,
                         PRUint32 *type ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 3001000
    vboxUnsupported();
    return 0;
#else /* VBOX_API_VERSION >= 3001000 */
    return mediumAttachment->vtbl->GetType(mediumAttachment, type);
#endif /* VBOX_API_VERSION >= 3001000 */
}

static nsresult
_mediumAttachmentGetPort(IMediumAttachment *mediumAttachment, PRInt32 *port)
{
    return mediumAttachment->vtbl->GetPort(mediumAttachment, port);
}

static nsresult
_mediumAttachmentGetDevice(IMediumAttachment *mediumAttachment, PRInt32 *device)
{
    return mediumAttachment->vtbl->GetDevice(mediumAttachment, device);
}

static nsresult
_storageControllerGetBus(IStorageController *storageController, PRUint32 *bus)
{
    return storageController->vtbl->GetBus(storageController, bus);
}

static nsresult
_sharedFolderGetHostPath(ISharedFolder *sharedFolder, PRUnichar **hostPath)
{
    return sharedFolder->vtbl->GetHostPath(sharedFolder, hostPath);
}

static nsresult
_sharedFolderGetName(ISharedFolder *sharedFolder, PRUnichar **name)
{
    return sharedFolder->vtbl->GetName(sharedFolder, name);
}

static nsresult
_sharedFolderGetWritable(ISharedFolder *sharedFolder, PRBool *writable)
{
    return sharedFolder->vtbl->GetWritable(sharedFolder, writable);
}

5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034
static nsresult
_snapshotGetName(ISnapshot *snapshot, PRUnichar **name)
{
    return snapshot->vtbl->GetName(snapshot, name);
}

static nsresult
_snapshotGetId(ISnapshot *snapshot, vboxIIDUnion *iidu)
{
    return snapshot->vtbl->GetId(snapshot, &IID_MEMBER(value));
}

static nsresult
_snapshotGetMachine(ISnapshot *snapshot, IMachine **machine)
{
    return snapshot->vtbl->GetMachine(snapshot, machine);
}

static nsresult
_snapshotGetDescription(ISnapshot *snapshot, PRUnichar **description)
{
    return snapshot->vtbl->GetDescription(snapshot, description);
}

static nsresult
_snapshotGetTimeStamp(ISnapshot *snapshot, PRInt64 *timeStamp)
{
    return snapshot->vtbl->GetTimeStamp(snapshot, timeStamp);
}

static nsresult
_snapshotGetParent(ISnapshot *snapshot, ISnapshot **parent)
{
    return snapshot->vtbl->GetParent(snapshot, parent);
}

static nsresult
_snapshotGetOnline(ISnapshot *snapshot, PRBool *online)
{
    return snapshot->vtbl->GetOnline(snapshot, online);
}

T
Taowei 已提交
6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074
static nsresult
_displayGetScreenResolution(IDisplay *display ATTRIBUTE_UNUSED,
                            PRUint32 screenId ATTRIBUTE_UNUSED,
                            PRUint32 *width ATTRIBUTE_UNUSED,
                            PRUint32 *height ATTRIBUTE_UNUSED,
                            PRUint32 *bitsPerPixel ATTRIBUTE_UNUSED,
                            PRInt32 *xOrigin ATTRIBUTE_UNUSED,
                            PRInt32 *yOrigin ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 3002000
    vboxUnsupported();
    return 0;
#elif VBOX_API_VERSION < 4003000
    return display->vtbl->GetScreenResolution(display, screenId, width,
                                              height, bitsPerPixel);
#else /* VBOX_API_VERSION >= 4003000 */
    return display->vtbl->GetScreenResolution(display, screenId, width,
                                              height, bitsPerPixel,
                                              xOrigin, yOrigin);
#endif /* VBOX_API_VERSION >= 4003000 */
}

static nsresult
_displayTakeScreenShotPNGToArray(IDisplay *display ATTRIBUTE_UNUSED,
                                 PRUint32 screenId ATTRIBUTE_UNUSED,
                                 PRUint32 width ATTRIBUTE_UNUSED,
                                 PRUint32 height ATTRIBUTE_UNUSED,
                                 PRUint32 *screenDataSize ATTRIBUTE_UNUSED,
                                 PRUint8** screenData ATTRIBUTE_UNUSED)
{
#if VBOX_API_VERSION < 4000000
    vboxUnsupported();
    return 0;
#else /* VBOX_API_VERSION >= 4000000 */
    return display->vtbl->TakeScreenShotPNGToArray(display, screenId, width,
                                                   height, screenDataSize,
                                                   screenData);
#endif /* VBOX_API_VERSION >= 4000000 */
}

T
Taowei 已提交
6075 6076 6077 6078 6079 6080
static bool _machineStateOnline(PRUint32 state)
{
    return ((state >= MachineState_FirstOnline) &&
            (state <= MachineState_LastOnline));
}

6081 6082 6083 6084 6085 6086
static bool _machineStateInactive(PRUint32 state)
{
    return ((state > MachineState_FirstOnline) &&
            (state < MachineState_LastOnline));
}

T
Taowei 已提交
6087 6088 6089 6090 6091 6092 6093
static bool _machineStateNotStart(PRUint32 state)
{
    return ((state == MachineState_PoweredOff) ||
            (state == MachineState_Saved) ||
            (state == MachineState_Aborted));
}

T
Taowei 已提交
6094 6095 6096 6097 6098
static bool _machineStateRunning(PRUint32 state)
{
    return state == MachineState_Running;
}

T
Taowei 已提交
6099 6100 6101 6102 6103
static bool _machineStatePaused(PRUint32 state)
{
    return state == MachineState_Paused;
}

T
Taowei 已提交
6104 6105 6106 6107 6108
static bool _machineStatePoweredOff(PRUint32 state)
{
    return state == MachineState_PoweredOff;
}

T
Taowei 已提交
6109 6110 6111 6112 6113 6114 6115 6116 6117 6118
static vboxUniformedPFN _UPFN = {
    .Initialize = _pfnInitialize,
    .Uninitialize = _pfnUninitialize,
    .ComUnallocMem = _pfnComUnallocMem,
    .Utf16Free = _pfnUtf16Free,
    .Utf8Free = _pfnUtf8Free,
    .Utf16ToUtf8 = _pfnUtf16ToUtf8,
    .Utf8ToUtf16 = _pfnUtf8ToUtf16,
};

T
Taowei 已提交
6119 6120 6121 6122 6123 6124 6125
static vboxUniformedIID _UIID = {
    .vboxIIDInitialize = _vboxIIDInitialize,
    .vboxIIDUnalloc = _vboxIIDUnalloc,
    .vboxIIDToUUID = _vboxIIDToUUID,
    .vboxIIDFromUUID = _vboxIIDFromUUID,
    .vboxIIDIsEqual = _vboxIIDIsEqual,
    .vboxIIDFromArrayItem = _vboxIIDFromArrayItem,
6126
    .vboxIIDToUtf8 = _vboxIIDToUtf8,
T
Taowei 已提交
6127 6128 6129
    .DEBUGIID = _DEBUGIID,
};

T
Taowei 已提交
6130 6131
static vboxUniformedArray _UArray = {
    .vboxArrayGet = vboxArrayGet,
6132
    .vboxArrayGetWithIIDArg = _vboxArrayGetWithIIDArg,
T
Taowei 已提交
6133 6134
    .vboxArrayRelease = vboxArrayRelease,
    .handleGetMachines = _handleGetMachines,
T
Taowei 已提交
6135 6136 6137
    .handleUSBGetDeviceFilters = _handleUSBGetDeviceFilters,
    .handleMachineGetMediumAttachments = _handleMachineGetMediumAttachments,
    .handleMachineGetSharedFolders = _handleMachineGetSharedFolders,
6138 6139 6140
    .handleSnapshotGetChildren = _handleSnapshotGetChildren,
    .handleMediumGetChildren = _handleMediumGetChildren,
    .handleMediumGetSnapshotIds = _handleMediumGetSnapshotIds,
T
Taowei 已提交
6141 6142
};

T
Taowei 已提交
6143 6144
static vboxUniformednsISupports _nsUISupports = {
    .Release = _nsisupportsRelease,
6145
    .AddRef = _nsisupportsAddRef,
T
Taowei 已提交
6146 6147
};

T
Taowei 已提交
6148 6149
static vboxUniformedIVirtualBox _UIVirtualBox = {
    .GetVersion = _virtualboxGetVersion,
T
Taowei 已提交
6150
    .GetMachine = _virtualboxGetMachine,
6151
    .OpenMachine = _virtualboxOpenMachine,
T
Taowei 已提交
6152
    .GetSystemProperties = _virtualboxGetSystemProperties,
T
Taowei 已提交
6153
    .CreateMachine = _virtualboxCreateMachine,
6154
    .CreateHardDiskMedium = _virtualboxCreateHardDiskMedium,
T
Taowei 已提交
6155 6156 6157
    .RegisterMachine = _virtualboxRegisterMachine,
    .FindMedium = _virtualboxFindMedium,
    .OpenMedium = _virtualboxOpenMedium,
T
Taowei 已提交
6158 6159
};

T
Taowei 已提交
6160
static vboxUniformedIMachine _UIMachine = {
T
Taowei 已提交
6161
    .AddStorageController = _machineAddStorageController,
T
Taowei 已提交
6162
    .GetStorageControllerByName = _machineGetStorageControllerByName,
T
Taowei 已提交
6163 6164
    .AttachDevice = _machineAttachDevice,
    .CreateSharedFolder = _machineCreateSharedFolder,
T
Taowei 已提交
6165
    .RemoveSharedFolder = _machineRemoveSharedFolder,
T
Taowei 已提交
6166
    .LaunchVMProcess = _machineLaunchVMProcess,
6167
    .Unregister = _machineUnregister,
6168
    .FindSnapshot = _machineFindSnapshot,
T
Taowei 已提交
6169 6170
    .GetAccessible = _machineGetAccessible,
    .GetState = _machineGetState,
T
Taowei 已提交
6171 6172
    .GetName = _machineGetName,
    .GetId = _machineGetId,
T
Taowei 已提交
6173 6174 6175 6176 6177 6178 6179 6180
    .GetBIOSSettings = _machineGetBIOSSettings,
    .GetAudioAdapter = _machineGetAudioAdapter,
    .GetNetworkAdapter = _machineGetNetworkAdapter,
    .GetChipsetType = _machineGetChipsetType,
    .GetSerialPort = _machineGetSerialPort,
    .GetParallelPort = _machineGetParallelPort,
    .GetVRDxServer = _machineGetVRDxServer,
    .GetUSBCommon = _machineGetUSBCommon,
6181 6182
    .GetCurrentSnapshot = _machineGetCurrentSnapshot,
    .GetSettingsFilePath = _machineGetSettingsFilePath,
T
Taowei 已提交
6183
    .GetCPUCount = _machineGetCPUCount,
T
Taowei 已提交
6184
    .SetCPUCount = _machineSetCPUCount,
T
Taowei 已提交
6185
    .GetMemorySize = _machineGetMemorySize,
T
Taowei 已提交
6186
    .SetMemorySize = _machineSetMemorySize,
T
Taowei 已提交
6187
    .GetCPUProperty = _machineGetCPUProperty,
T
Taowei 已提交
6188
    .SetCPUProperty = _machineSetCPUProperty,
T
Taowei 已提交
6189
    .GetBootOrder = _machineGetBootOrder,
T
Taowei 已提交
6190
    .SetBootOrder = _machineSetBootOrder,
T
Taowei 已提交
6191
    .GetVRAMSize = _machineGetVRAMSize,
T
Taowei 已提交
6192
    .SetVRAMSize = _machineSetVRAMSize,
T
Taowei 已提交
6193
    .GetMonitorCount = _machineGetMonitorCount,
T
Taowei 已提交
6194
    .SetMonitorCount = _machineSetMonitorCount,
T
Taowei 已提交
6195
    .GetAccelerate3DEnabled = _machineGetAccelerate3DEnabled,
T
Taowei 已提交
6196
    .SetAccelerate3DEnabled = _machineSetAccelerate3DEnabled,
T
Taowei 已提交
6197
    .GetAccelerate2DVideoEnabled = _machineGetAccelerate2DVideoEnabled,
T
Taowei 已提交
6198
    .SetAccelerate2DVideoEnabled = _machineSetAccelerate2DVideoEnabled,
T
Taowei 已提交
6199
    .GetExtraData = _machineGetExtraData,
T
Taowei 已提交
6200
    .SetExtraData = _machineSetExtraData,
6201
    .GetSnapshotCount = _machineGetSnapshotCount,
T
Taowei 已提交
6202
    .SaveSettings = _machineSaveSettings,
T
Taowei 已提交
6203 6204
};

T
Taowei 已提交
6205
static vboxUniformedISession _UISession = {
T
Taowei 已提交
6206
    .Open = _sessionOpen,
T
Taowei 已提交
6207 6208
    .OpenExisting = _sessionOpenExisting,
    .GetConsole = _sessionGetConsole,
T
Taowei 已提交
6209
    .GetMachine = _sessionGetMachine,
T
Taowei 已提交
6210 6211 6212 6213 6214
    .Close = _sessionClose,
};

static vboxUniformedIConsole _UIConsole = {
    .SaveState = _consoleSaveState,
T
Taowei 已提交
6215
    .Pause = _consolePause,
T
Taowei 已提交
6216
    .Resume = _consoleResume,
T
Taowei 已提交
6217
    .PowerButton = _consolePowerButton,
T
Taowei 已提交
6218
    .PowerDown = _consolePowerDown,
T
Taowei 已提交
6219
    .Reset = _consoleReset,
6220
    .TakeSnapshot = _consoleTakeSnapshot,
T
Taowei 已提交
6221
    .DeleteSnapshot = _consoleDeleteSnapshot,
T
Taowei 已提交
6222
    .GetDisplay = _consoleGetDisplay,
T
Taowei 已提交
6223 6224 6225 6226 6227
};

static vboxUniformedIProgress _UIProgress = {
    .WaitForCompletion = _progressWaitForCompletion,
    .GetResultCode = _progressGetResultCode,
T
Taowei 已提交
6228
    .GetCompleted = _progressGetCompleted,
T
Taowei 已提交
6229 6230
};

T
Taowei 已提交
6231 6232
static vboxUniformedISystemProperties _UISystemProperties = {
    .GetMaxGuestCPUCount = _systemPropertiesGetMaxGuestCPUCount,
T
Taowei 已提交
6233 6234 6235 6236 6237 6238
    .GetMaxBootPosition = _systemPropertiesGetMaxBootPosition,
    .GetMaxNetworkAdapters = _systemPropertiesGetMaxNetworkAdapters,
    .GetSerialPortCount = _systemPropertiesGetSerialPortCount,
    .GetParallelPortCount = _systemPropertiesGetParallelPortCount,
    .GetMaxPortCountForStorageBus = _systemPropertiesGetMaxPortCountForStorageBus,
    .GetMaxDevicesPerPortForStorageBus = _systemPropertiesGetMaxDevicesPerPortForStorageBus,
T
Taowei 已提交
6239
    .GetMaxGuestRAM = _systemPropertiesGetMaxGuestRAM,
T
Taowei 已提交
6240 6241 6242
};

static vboxUniformedIBIOSSettings _UIBIOSSettings = {
T
Taowei 已提交
6243
    .GetACPIEnabled = _biosSettingsGetACPIEnabled,
T
Taowei 已提交
6244
    .SetACPIEnabled = _biosSettingsSetACPIEnabled,
T
Taowei 已提交
6245
    .GetIOAPICEnabled = _biosSettingsGetIOAPICEnabled,
T
Taowei 已提交
6246 6247 6248 6249
    .SetIOAPICEnabled = _biosSettingsSetIOAPICEnabled,
};

static vboxUniformedIAudioAdapter _UIAudioAdapter = {
T
Taowei 已提交
6250
    .GetEnabled = _audioAdapterGetEnabled,
T
Taowei 已提交
6251
    .SetEnabled = _audioAdapterSetEnabled,
T
Taowei 已提交
6252
    .GetAudioController = _audioAdapterGetAudioController,
T
Taowei 已提交
6253 6254 6255 6256
    .SetAudioController = _audioAdapterSetAudioController,
};

static vboxUniformedINetworkAdapter _UINetworkAdapter = {
T
Taowei 已提交
6257 6258
    .GetAttachmentType = _networkAdapterGetAttachmentType,
    .GetEnabled = _networkAdapterGetEnabled,
T
Taowei 已提交
6259
    .SetEnabled = _networkAdapterSetEnabled,
T
Taowei 已提交
6260
    .GetAdapterType = _networkAdapterGetAdapterType,
T
Taowei 已提交
6261
    .SetAdapterType = _networkAdapterSetAdapterType,
T
Taowei 已提交
6262
    .GetBridgedInterface = _networkAdapterGetBridgedInterface,
T
Taowei 已提交
6263
    .SetBridgedInterface = _networkAdapterSetBridgedInterface,
T
Taowei 已提交
6264
    .GetInternalNetwork = _networkAdapterGetInternalNetwork,
T
Taowei 已提交
6265
    .SetInternalNetwork = _networkAdapterSetInternalNetwork,
T
Taowei 已提交
6266
    .GetHostOnlyInterface = _networkAdapterGetHostOnlyInterface,
T
Taowei 已提交
6267
    .SetHostOnlyInterface = _networkAdapterSetHostOnlyInterface,
T
Taowei 已提交
6268
    .GetMACAddress = _networkAdapterGetMACAddress,
T
Taowei 已提交
6269 6270 6271 6272 6273 6274 6275 6276
    .SetMACAddress = _networkAdapterSetMACAddress,
    .AttachToBridgedInterface = _networkAdapterAttachToBridgedInterface,
    .AttachToInternalNetwork = _networkAdapterAttachToInternalNetwork,
    .AttachToHostOnlyInterface = _networkAdapterAttachToHostOnlyInterface,
    .AttachToNAT = _networkAdapterAttachToNAT,
};

static vboxUniformedISerialPort _UISerialPort = {
T
Taowei 已提交
6277
    .GetEnabled = _serialPortGetEnabled,
T
Taowei 已提交
6278
    .SetEnabled = _serialPortSetEnabled,
T
Taowei 已提交
6279
    .GetPath = _serialPortGetPath,
T
Taowei 已提交
6280
    .SetPath = _serialPortSetPath,
T
Taowei 已提交
6281
    .GetIRQ = _serialPortGetIRQ,
T
Taowei 已提交
6282
    .SetIRQ = _serialPortSetIRQ,
T
Taowei 已提交
6283
    .GetIOBase = _serialPortGetIOBase,
T
Taowei 已提交
6284
    .SetIOBase = _serialPortSetIOBase,
T
Taowei 已提交
6285
    .GetHostMode = _serialPortGetHostMode,
T
Taowei 已提交
6286 6287 6288 6289
    .SetHostMode = _serialPortSetHostMode,
};

static vboxUniformedIParallelPort _UIParallelPort = {
T
Taowei 已提交
6290
    .GetEnabled = _parallelPortGetEnabled,
T
Taowei 已提交
6291
    .SetEnabled = _parallelPortSetEnabled,
T
Taowei 已提交
6292
    .GetPath = _parallelPortGetPath,
T
Taowei 已提交
6293
    .SetPath = _parallelPortSetPath,
T
Taowei 已提交
6294
    .GetIRQ = _parallelPortGetIRQ,
T
Taowei 已提交
6295
    .SetIRQ = _parallelPortSetIRQ,
T
Taowei 已提交
6296
    .GetIOBase = _parallelPortGetIOBase,
T
Taowei 已提交
6297 6298 6299 6300
    .SetIOBase = _parallelPortSetIOBase,
};

static vboxUniformedIVRDxServer _UIVRDxServer = {
T
Taowei 已提交
6301
    .GetEnabled = _vrdxServerGetEnabled,
T
Taowei 已提交
6302
    .SetEnabled = _vrdxServerSetEnabled,
T
Taowei 已提交
6303
    .GetPorts = _vrdxServerGetPorts,
T
Taowei 已提交
6304
    .SetPorts = _vrdxServerSetPorts,
T
Taowei 已提交
6305
    .GetReuseSingleConnection = _vrdxServerGetReuseSingleConnection,
T
Taowei 已提交
6306
    .SetReuseSingleConnection = _vrdxServerSetReuseSingleConnection,
T
Taowei 已提交
6307
    .GetAllowMultiConnection = _vrdxServerGetAllowMultiConnection,
T
Taowei 已提交
6308
    .SetAllowMultiConnection = _vrdxServerSetAllowMultiConnection,
T
Taowei 已提交
6309
    .GetNetAddress = _vrdxServerGetNetAddress,
T
Taowei 已提交
6310 6311 6312 6313 6314
    .SetNetAddress = _vrdxServerSetNetAddress,
};

static vboxUniformedIUSBCommon _UIUSBCommon = {
    .Enable = _usbCommonEnable,
T
Taowei 已提交
6315
    .GetEnabled = _usbCommonGetEnabled,
T
Taowei 已提交
6316 6317 6318 6319 6320
    .CreateDeviceFilter = _usbCommonCreateDeviceFilter,
    .InsertDeviceFilter = _usbCommonInsertDeviceFilter,
};

static vboxUniformedIUSBDeviceFilter _UIUSBDeviceFilter = {
T
Taowei 已提交
6321
    .GetProductId = _usbDeviceFilterGetProductId,
T
Taowei 已提交
6322
    .SetProductId = _usbDeviceFilterSetProductId,
T
Taowei 已提交
6323
    .GetActive = _usbDeviceFilterGetActive,
T
Taowei 已提交
6324
    .SetActive = _usbDeviceFilterSetActive,
T
Taowei 已提交
6325
    .GetVendorId = _usbDeviceFilterGetVendorId,
T
Taowei 已提交
6326 6327 6328 6329 6330
    .SetVendorId = _usbDeviceFilterSetVendorId,
};

static vboxUniformedIMedium _UIMedium = {
    .GetId = _mediumGetId,
T
Taowei 已提交
6331 6332
    .GetLocation = _mediumGetLocation,
    .GetReadOnly = _mediumGetReadOnly,
6333 6334 6335 6336
    .GetParent = _mediumGetParent,
    .GetChildren = _mediumGetChildren,
    .GetFormat = _mediumGetFormat,
    .DeleteStorage = _mediumDeleteStorage,
T
Taowei 已提交
6337
    .Release = _mediumRelease,
6338
    .Close = _mediumClose,
T
Taowei 已提交
6339
    .SetType = _mediumSetType,
6340
    .CreateDiffStorage = _mediumCreateDiffStorage,
T
Taowei 已提交
6341 6342
};

T
Taowei 已提交
6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360
static vboxUniformedIMediumAttachment _UIMediumAttachment = {
    .GetMedium = _mediumAttachmentGetMedium,
    .GetController = _mediumAttachmentGetController,
    .GetType = _mediumAttachmentGetType,
    .GetPort = _mediumAttachmentGetPort,
    .GetDevice = _mediumAttachmentGetDevice,
};

static vboxUniformedIStorageController _UIStorageController = {
    .GetBus = _storageControllerGetBus,
};

static vboxUniformedISharedFolder _UISharedFolder = {
    .GetHostPath = _sharedFolderGetHostPath,
    .GetName = _sharedFolderGetName,
    .GetWritable = _sharedFolderGetWritable,
};

6361 6362 6363 6364 6365 6366 6367 6368 6369 6370
static vboxUniformedISnapshot _UISnapshot = {
    .GetName = _snapshotGetName,
    .GetId = _snapshotGetId,
    .GetMachine = _snapshotGetMachine,
    .GetDescription = _snapshotGetDescription,
    .GetTimeStamp = _snapshotGetTimeStamp,
    .GetParent = _snapshotGetParent,
    .GetOnline = _snapshotGetOnline,
};

T
Taowei 已提交
6371 6372 6373 6374 6375
static vboxUniformedIDisplay _UIDisplay = {
    .GetScreenResolution = _displayGetScreenResolution,
    .TakeScreenShotPNGToArray = _displayTakeScreenShotPNGToArray,
};

T
Taowei 已提交
6376 6377
static uniformedMachineStateChecker _machineStateChecker = {
    .Online = _machineStateOnline,
6378
    .Inactive = _machineStateInactive,
T
Taowei 已提交
6379
    .NotStart = _machineStateNotStart,
T
Taowei 已提交
6380
    .Running = _machineStateRunning,
T
Taowei 已提交
6381
    .Paused = _machineStatePaused,
T
Taowei 已提交
6382
    .PoweredOff = _machineStatePoweredOff,
T
Taowei 已提交
6383 6384
};

T
Taowei 已提交
6385 6386 6387 6388 6389 6390
void NAME(InstallUniformedAPI)(vboxUniformedAPI *pVBoxAPI)
{
    pVBoxAPI->APIVersion = VBOX_API_VERSION;
    pVBoxAPI->XPCOMCVersion = VBOX_XPCOMC_VERSION;
    pVBoxAPI->initializeDomainEvent = _initializeDomainEvent;
    pVBoxAPI->registerGlobalData = _registerGlobalData;
T
Taowei 已提交
6391 6392 6393
    pVBoxAPI->detachDevices = _detachDevices;
    pVBoxAPI->unregisterMachine = _unregisterMachine;
    pVBoxAPI->deleteConfig = _deleteConfig;
T
Taowei 已提交
6394
    pVBoxAPI->vboxAttachDrivesOld = _vboxAttachDrivesOld;
T
Taowei 已提交
6395
    pVBoxAPI->vboxConvertState = _vboxConvertState;
T
Taowei 已提交
6396 6397
    pVBoxAPI->dumpIDEHDDsOld = _dumpIDEHDDsOld;
    pVBoxAPI->dumpDVD = _dumpDVD;
T
Taowei 已提交
6398
    pVBoxAPI->attachDVD = _attachDVD;
T
Taowei 已提交
6399
    pVBoxAPI->detachDVD = _detachDVD;
T
Taowei 已提交
6400
    pVBoxAPI->dumpFloppy = _dumpFloppy;
T
Taowei 已提交
6401
    pVBoxAPI->attachFloppy = _attachFloppy;
T
Taowei 已提交
6402
    pVBoxAPI->detachFloppy = _detachFloppy;
6403
    pVBoxAPI->snapshotRestore = _vboxDomainSnapshotRestore;
T
Taowei 已提交
6404
    pVBoxAPI->registerDomainEvent = _registerDomainEvent;
T
Taowei 已提交
6405
    pVBoxAPI->UPFN = _UPFN;
T
Taowei 已提交
6406
    pVBoxAPI->UIID = _UIID;
T
Taowei 已提交
6407
    pVBoxAPI->UArray = _UArray;
T
Taowei 已提交
6408
    pVBoxAPI->nsUISupports = _nsUISupports;
T
Taowei 已提交
6409
    pVBoxAPI->UIVirtualBox = _UIVirtualBox;
T
Taowei 已提交
6410
    pVBoxAPI->UIMachine = _UIMachine;
T
Taowei 已提交
6411 6412 6413
    pVBoxAPI->UISession = _UISession;
    pVBoxAPI->UIConsole = _UIConsole;
    pVBoxAPI->UIProgress = _UIProgress;
T
Taowei 已提交
6414
    pVBoxAPI->UISystemProperties = _UISystemProperties;
T
Taowei 已提交
6415 6416 6417 6418 6419 6420 6421 6422 6423
    pVBoxAPI->UIBIOSSettings = _UIBIOSSettings;
    pVBoxAPI->UIAudioAdapter = _UIAudioAdapter;
    pVBoxAPI->UINetworkAdapter = _UINetworkAdapter;
    pVBoxAPI->UISerialPort = _UISerialPort;
    pVBoxAPI->UIParallelPort = _UIParallelPort;
    pVBoxAPI->UIVRDxServer = _UIVRDxServer;
    pVBoxAPI->UIUSBCommon = _UIUSBCommon;
    pVBoxAPI->UIUSBDeviceFilter = _UIUSBDeviceFilter;
    pVBoxAPI->UIMedium = _UIMedium;
T
Taowei 已提交
6424 6425 6426
    pVBoxAPI->UIMediumAttachment = _UIMediumAttachment;
    pVBoxAPI->UIStorageController = _UIStorageController;
    pVBoxAPI->UISharedFolder = _UISharedFolder;
6427
    pVBoxAPI->UISnapshot = _UISnapshot;
T
Taowei 已提交
6428
    pVBoxAPI->UIDisplay = _UIDisplay;
T
Taowei 已提交
6429
    pVBoxAPI->machineStateChecker = _machineStateChecker;
T
Taowei 已提交
6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442

#if VBOX_API_VERSION <= 2002000 || VBOX_API_VERSION >= 4000000
    pVBoxAPI->domainEventCallbacks = 0;
#else /* VBOX_API_VERSION > 2002000 || VBOX_API_VERSION < 4000000 */
    pVBoxAPI->domainEventCallbacks = 1;
#endif /* VBOX_API_VERSION > 2002000 || VBOX_API_VERSION < 4000000 */

#if VBOX_API_VERSION == 2002000
    pVBoxAPI->hasStaticGlobalData = 0;
#else /* VBOX_API_VERSION > 2002000 */
    pVBoxAPI->hasStaticGlobalData = 1;
#endif /* VBOX_API_VERSION > 2002000 */

T
Taowei 已提交
6443 6444 6445
#if VBOX_API_VERSION >= 4000000
    /* Get machine for the call to VBOX_SESSION_OPEN_EXISTING */
    pVBoxAPI->getMachineForSession = 1;
T
Taowei 已提交
6446
    pVBoxAPI->detachDevicesExplicitly = 0;
T
Taowei 已提交
6447
    pVBoxAPI->vboxAttachDrivesUseOld = 0;
T
Taowei 已提交
6448
    pVBoxAPI->supportScreenshot = 1;
T
Taowei 已提交
6449 6450
#else /* VBOX_API_VERSION < 4000000 */
    pVBoxAPI->getMachineForSession = 0;
T
Taowei 已提交
6451
    pVBoxAPI->detachDevicesExplicitly = 1;
T
Taowei 已提交
6452
    pVBoxAPI->vboxAttachDrivesUseOld = 1;
T
Taowei 已提交
6453
    pVBoxAPI->supportScreenshot = 0;
T
Taowei 已提交
6454
#endif /* VBOX_API_VERSION < 4000000 */
T
Taowei 已提交
6455 6456 6457 6458 6459 6460 6461 6462 6463

#if VBOX_API_VERSION >= 4001000
    pVBoxAPI->chipsetType = 1;
#else /* VBOX_API_VERSION < 4001000 */
    pVBoxAPI->chipsetType = 0;
#endif /* VBOX_API_VERSION < 4001000 */

#if VBOX_API_VERSION >= 3001000
    pVBoxAPI->accelerate2DVideo = 1;
T
Taowei 已提交
6464
    pVBoxAPI->oldMediumInterface = 0;
T
Taowei 已提交
6465 6466
#else /* VBOX_API_VERSION < 3001000 */
    pVBoxAPI->accelerate2DVideo = 0;
T
Taowei 已提交
6467
    pVBoxAPI->oldMediumInterface = 1;
T
Taowei 已提交
6468
#endif /* VBOX_API_VERSION < 3001000 */
6469 6470 6471 6472 6473 6474

#if VBOX_API_VERSION >= 4002000
    pVBoxAPI->vboxSnapshotRedefine = 1;
#else /* VBOX_API_VERSION < 4002000 */
    pVBoxAPI->vboxSnapshotRedefine = 0;
#endif /* VBOX_API_VERSION < 4002000 */
T
Taowei 已提交
6475
}
6476

6477 6478 6479 6480 6481 6482
/**
 * Function Tables
 */

virNetworkDriver NAME(NetworkDriver) = {
    "VBOX",
6483 6484
    .networkOpen = vboxNetworkOpen, /* 0.6.4 */
    .networkClose = vboxNetworkClose, /* 0.6.4 */
6485 6486 6487 6488
    .connectNumOfNetworks = vboxConnectNumOfNetworks, /* 0.6.4 */
    .connectListNetworks = vboxConnectListNetworks, /* 0.6.4 */
    .connectNumOfDefinedNetworks = vboxConnectNumOfDefinedNetworks, /* 0.6.4 */
    .connectListDefinedNetworks = vboxConnectListDefinedNetworks, /* 0.6.4 */
6489 6490 6491 6492 6493 6494 6495 6496
    .networkLookupByUUID = vboxNetworkLookupByUUID, /* 0.6.4 */
    .networkLookupByName = vboxNetworkLookupByName, /* 0.6.4 */
    .networkCreateXML = vboxNetworkCreateXML, /* 0.6.4 */
    .networkDefineXML = vboxNetworkDefineXML, /* 0.6.4 */
    .networkUndefine = vboxNetworkUndefine, /* 0.6.4 */
    .networkCreate = vboxNetworkCreate, /* 0.6.4 */
    .networkDestroy = vboxNetworkDestroy, /* 0.6.4 */
    .networkGetXMLDesc = vboxNetworkGetXMLDesc, /* 0.6.4 */
6497
};
6498 6499 6500

virStorageDriver NAME(StorageDriver) = {
    .name               = "VBOX",
6501 6502
    .storageOpen = vboxStorageOpen, /* 0.7.1 */
    .storageClose = vboxStorageClose, /* 0.7.1 */
6503 6504
    .connectNumOfStoragePools = vboxConnectNumOfStoragePools, /* 0.7.1 */
    .connectListStoragePools = vboxConnectListStoragePools, /* 0.7.1 */
6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516
    .storagePoolLookupByName = vboxStoragePoolLookupByName, /* 0.7.1 */
    .storagePoolNumOfVolumes = vboxStoragePoolNumOfVolumes, /* 0.7.1 */
    .storagePoolListVolumes = vboxStoragePoolListVolumes, /* 0.7.1 */

    .storageVolLookupByName = vboxStorageVolLookupByName, /* 0.7.1 */
    .storageVolLookupByKey = vboxStorageVolLookupByKey, /* 0.7.1 */
    .storageVolLookupByPath = vboxStorageVolLookupByPath, /* 0.7.1 */
    .storageVolCreateXML = vboxStorageVolCreateXML, /* 0.7.1 */
    .storageVolDelete = vboxStorageVolDelete, /* 0.7.1 */
    .storageVolGetInfo = vboxStorageVolGetInfo, /* 0.7.1 */
    .storageVolGetXMLDesc = vboxStorageVolGetXMLDesc, /* 0.7.1 */
    .storageVolGetPath = vboxStorageVolGetPath /* 0.7.1 */
6517
};