esx_util.c 13.4 KB
Newer Older
1 2

/*
3
 * esx_util.c: utility functions for the VMware ESX driver
4
 *
5
 * Copyright (C) 2010 Red Hat, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
 * Copyright (C) 2009 Maximilian Wilhelm <max@rfc2324.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

#include <config.h>

#include <netdb.h>

#include "internal.h"
#include "datatypes.h"
#include "qparams.h"
#include "util.h"
#include "memory.h"
#include "logging.h"
#include "uuid.h"
36
#include "esx_private.h"
37 38 39 40
#include "esx_util.h"

#define VIR_FROM_THIS VIR_FROM_ESX

41 42 43 44
/* AI_ADDRCONFIG is missing on some systems. */
#ifndef AI_ADDRCONFIG
# define AI_ADDRCONFIG 0
#endif
45 46


47

48
int
49
esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter,
50
                   int *noVerify, int *autoAnswer)
51 52 53 54 55 56 57 58 59 60
{
    int result = 0;
    int i;
    struct qparam_set *queryParamSet = NULL;
    struct qparam *queryParam = NULL;

    if (transport != NULL) {
        *transport = NULL;
    }

M
Matthias Bolte 已提交
61 62
    if (vCenter != NULL) {
        *vCenter = NULL;
63 64
    }

65 66 67 68 69 70 71 72
    if (noVerify != NULL) {
        *noVerify = 0;
    }

    if (autoAnswer != NULL) {
        *autoAnswer = 0;
    }

73
#ifdef HAVE_XMLURI_QUERY_RAW
74
    queryParamSet = qparam_query_parse(uri->query_raw);
75
#else
76
    queryParamSet = qparam_query_parse(uri->query);
77 78 79 80 81 82 83 84 85
#endif

    if (queryParamSet == NULL) {
        goto failure;
    }

    for (i = 0; i < queryParamSet->n; i++) {
        queryParam = &queryParamSet->p[i];

86 87 88 89 90
        if (STRCASEEQ(queryParam->name, "transport")) {
            if (transport == NULL) {
                continue;
            }

91 92 93
            *transport = strdup(queryParam->value);

            if (*transport == NULL) {
94
                virReportOOMError();
95 96 97 98
                goto failure;
            }

            if (STRNEQ(*transport, "http") && STRNEQ(*transport, "https")) {
99
                ESX_ERROR(VIR_ERR_INVALID_ARG,
100 101
                          _("Query parameter 'transport' has unexpected value "
                            "'%s' (should be http|https)"), *transport);
102 103
                goto failure;
            }
104 105 106 107 108
        } else if (STRCASEEQ(queryParam->name, "vcenter")) {
            if (vCenter == NULL) {
                continue;
            }

M
Matthias Bolte 已提交
109
            *vCenter = strdup(queryParam->value);
110

M
Matthias Bolte 已提交
111
            if (*vCenter == NULL) {
112
                virReportOOMError();
113 114
                goto failure;
            }
115 116 117 118 119
        } else if (STRCASEEQ(queryParam->name, "no_verify")) {
            if (noVerify == NULL) {
                continue;
            }

120 121
            if (virStrToLong_i(queryParam->value, NULL, 10, noVerify) < 0 ||
                (*noVerify != 0 && *noVerify != 1)) {
122
                ESX_ERROR(VIR_ERR_INVALID_ARG,
123 124
                          _("Query parameter 'no_verify' has unexpected value "
                            "'%s' (should be 0 or 1)"), queryParam->value);
125 126
                goto failure;
            }
127 128 129 130 131 132 133
        } else if (STRCASEEQ(queryParam->name, "auto_answer")) {
            if (autoAnswer == NULL) {
                continue;
            }

            if (virStrToLong_i(queryParam->value, NULL, 10, autoAnswer) < 0 ||
                (*autoAnswer != 0 && *autoAnswer != 1)) {
134
                ESX_ERROR(VIR_ERR_INVALID_ARG,
135 136
                          _("Query parameter 'auto_answer' has unexpected "
                            "value '%s' (should be 0 or 1)"), queryParam->value);
137 138
                goto failure;
            }
139 140 141 142 143 144 145 146 147 148
        } else {
            VIR_WARN("Ignoring unexpected query parameter '%s'",
                     queryParam->name);
        }
    }

    if (transport != NULL && *transport == NULL) {
        *transport = strdup("https");

        if (*transport == NULL) {
149
            virReportOOMError();
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            goto failure;
        }
    }

  cleanup:
    if (queryParamSet != NULL) {
        free_qparam_set(queryParamSet);
    }

    return result;

  failure:
    if (transport != NULL) {
        VIR_FREE(*transport);
    }

M
Matthias Bolte 已提交
166 167
    if (vCenter != NULL) {
        VIR_FREE(*vCenter);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    }

    result = -1;

    goto cleanup;
}



int
esxUtil_ParseVirtualMachineIDString(const char *id_string, int *id)
{
    /* Try to parse an integer from the complete string. */
    if (virStrToLong_i(id_string, NULL, 10, id) == 0) {
        return 0;
    }

    /*
     * If that fails try to parse an integer from the string tail
     * assuming the naming scheme Virtual Center seems to use.
     */
    if (STRPREFIX(id_string, "vm-")) {
        if (virStrToLong_i(id_string + 3, NULL, 10, id) == 0) {
            return 0;
        }
    }

    return -1;
}



M
Matthias Bolte 已提交
200
int
201
esxUtil_ParseDatastoreRelatedPath(const char *datastoreRelatedPath,
M
Matthias Bolte 已提交
202 203 204 205 206 207 208 209 210 211
                                  char **datastoreName,
                                  char **directoryName, char **fileName)
{
    int result = 0;
    char *directoryAndFileName = NULL;
    char *separator = NULL;

    if (datastoreName == NULL || *datastoreName != NULL ||
        directoryName == NULL || *directoryName != NULL ||
        fileName == NULL || *fileName != NULL) {
212
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
213
        return -1;
M
Matthias Bolte 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    }

    /*
     * Parse string as '[<datastore>] <path>'. '%as' is similar to '%s', but
     * sscanf() will allocate the memory for the string, so the caller doesn't
     * need to preallocate a buffer that's large enough.
     *
     * The s in '%as' can be replaced with a character set, e.g. [a-z].
     *
     * '%a[^]%]' matches <datastore>. '[^]%]' excludes ']' from the accepted
     * characters, otherwise sscanf() wont match what it should.
     *
     * '%a[^\n]' matches <path>. '[^\n]' excludes '\n' from the accepted
     * characters, otherwise sscanf() would only match up to the first space,
     * but spaces are valid in <path>.
     */
    if (sscanf(datastoreRelatedPath, "[%a[^]%]] %a[^\n]", datastoreName,
               &directoryAndFileName) != 2) {
232
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
233 234
                  _("Datastore related path '%s' doesn't have expected format "
                    "'[<datastore>] <path>'"), datastoreRelatedPath);
M
Matthias Bolte 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247
        goto failure;
    }

    /* Split <path> into <directory>/<file>, where <directory> is optional */
    separator = strrchr(directoryAndFileName, '/');

    if (separator != NULL) {
        *separator++ = '\0';

        *directoryName = directoryAndFileName;
        directoryAndFileName = NULL;

        if (*separator == '\0') {
248
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
249
                      _("Datastore related path '%s' doesn't reference a file"),
M
Matthias Bolte 已提交
250 251 252 253 254 255 256
                      datastoreRelatedPath);
            goto failure;
        }

        *fileName = strdup(separator);

        if (*fileName == NULL) {
257
            virReportOOMError();
M
Matthias Bolte 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
            goto failure;
        }
    } else {
        *fileName = directoryAndFileName;
        directoryAndFileName = NULL;
    }

  cleanup:
    VIR_FREE(directoryAndFileName);

    return result;

  failure:
    VIR_FREE(*datastoreName);
    VIR_FREE(*directoryName);
    VIR_FREE(*fileName);

    result = -1;

    goto cleanup;
}



282
int
283
esxUtil_ResolveHostname(const char *hostname,
M
Matthias Bolte 已提交
284
                        char *ipAddress, size_t ipAddress_length)
285 286 287 288 289
{
    struct addrinfo hints;
    struct addrinfo *result = NULL;
    int errcode;

M
Matthias Bolte 已提交
290
    memset(&hints, 0, sizeof(struct addrinfo));
291 292 293 294 295 296 297 298 299

    hints.ai_flags = AI_ADDRCONFIG;
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

    errcode = getaddrinfo(hostname, NULL, &hints, &result);

    if (errcode != 0) {
300
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
301
                  _("IP address lookup for host '%s' failed: %s"), hostname,
302 303 304 305 306
                  gai_strerror(errcode));
        return -1;
    }

    if (result == NULL) {
307
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
308
                  _("No IP address for host '%s' found: %s"), hostname,
309 310 311 312
                  gai_strerror(errcode));
        return -1;
    }

M
Matthias Bolte 已提交
313 314
    errcode = getnameinfo(result->ai_addr, result->ai_addrlen, ipAddress,
                          ipAddress_length, NULL, 0, NI_NUMERICHOST);
315 316

    if (errcode != 0) {
317
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
318
                  _("Formating IP address for host '%s' failed: %s"), hostname,
319 320 321 322 323 324 325 326 327 328 329 330 331
                  gai_strerror(errcode));
        freeaddrinfo(result);
        return -1;
    }

    freeaddrinfo(result);

    return 0;
}



int
332 333
esxUtil_GetConfigString(virConfPtr conf, const char *name, char **string,
                        int optional)
334 335 336 337 338 339 340 341 342 343 344
{
    virConfValuePtr value;

    *string = NULL;
    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        }

345
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
346
                  _("Missing essential config entry '%s'"), name);
347 348 349 350
        return -1;
    }

    if (value->type != VIR_CONF_STRING) {
351
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
352
                  _("Config entry '%s' must be a string"), name);
353 354 355 356 357 358 359 360
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        }

361
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
362
                  _("Missing essential config entry '%s'"), name);
363 364 365 366 367 368
        return -1;
    }

    *string = strdup(value->str);

    if (*string == NULL) {
369
        virReportOOMError();
370 371 372 373 374 375 376 377 378
        return -1;
    }

    return 0;
}



int
379 380
esxUtil_GetConfigUUID(virConfPtr conf, const char *name, unsigned char *uuid,
                      int optional)
381 382 383 384 385 386 387 388 389
{
    virConfValuePtr value;

    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
390
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
391
                      _("Missing essential config entry '%s'"), name);
392 393 394 395 396
            return -1;
        }
    }

    if (value->type != VIR_CONF_STRING) {
397
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
398
                  _("Config entry '%s' must be a string"), name);
399 400 401 402 403 404 405
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        } else {
406
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
407
                      _("Missing essential config entry '%s'"), name);
408 409 410 411
            return -1;
        }
    }

412
    if (virUUIDParse(value->str, uuid) < 0) {
413
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
414
                  _("Could not parse UUID from string '%s'"), value->str);
415 416
        return -1;
    }
417 418 419 420 421 422 423

    return 0;
}



int
424 425
esxUtil_GetConfigLong(virConfPtr conf, const char *name, long long *number,
                      long long default_, int optional)
426 427 428 429 430 431 432 433 434 435
{
    virConfValuePtr value;

    *number = default_;
    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
436
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
437
                      _("Missing essential config entry '%s'"), name);
438 439 440 441 442 443 444 445 446
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
447
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
448
                          _("Missing essential config entry '%s'"), name);
449 450 451 452 453 454 455
                return -1;
            }
        }

        if (STREQ(value->str, "unlimited")) {
            *number = -1;
        } else if (virStrToLong_ll(value->str, NULL, 10, number) < 0) {
456
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
457
                      _("Config entry '%s' must represent an integer value"),
458 459 460 461
                      name);
            return -1;
        }
    } else {
462
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
463
                  _("Config entry '%s' must be a string"), name);
464 465 466 467 468 469 470 471 472
        return -1;
    }

    return 0;
}



int
473 474
esxUtil_GetConfigBoolean(virConfPtr conf, const char *name, int *boolean_,
                         int default_, int optional)
475 476 477
{
    virConfValuePtr value;

M
Matthias Bolte 已提交
478
    *boolean_ = default_;
479 480 481 482 483 484
    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
485
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
486
                      _("Missing essential config entry '%s'"), name);
487 488 489 490 491 492 493 494 495
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
496
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
497
                          _("Missing essential config entry '%s'"), name);
498 499 500 501 502
                return -1;
            }
        }

        if (STRCASEEQ(value->str, "true")) {
M
Matthias Bolte 已提交
503
            *boolean_ = 1;
504
        } else if (STRCASEEQ(value->str, "false")) {
M
Matthias Bolte 已提交
505
            *boolean_ = 0;
506
        } else {
507
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
508 509
                      _("Config entry '%s' must represent a boolean value "
                        "(true|false)"), name);
510 511 512
            return -1;
        }
    } else {
513
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
514
                  _("Config entry '%s' must be a string"), name);
515 516 517 518 519
        return -1;
    }

    return 0;
}