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
int
43
esxUtil_ParseQuery(xmlURIPtr uri, char **transport, char **vCenter,
44
                   int *noVerify, int *autoAnswer)
45 46 47 48 49 50 51 52 53 54
{
    int result = 0;
    int i;
    struct qparam_set *queryParamSet = NULL;
    struct qparam *queryParam = NULL;

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

M
Matthias Bolte 已提交
55 56
    if (vCenter != NULL) {
        *vCenter = NULL;
57 58
    }

59 60 61 62 63 64 65 66
    if (noVerify != NULL) {
        *noVerify = 0;
    }

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

67
#ifdef HAVE_XMLURI_QUERY_RAW
68
    queryParamSet = qparam_query_parse(uri->query_raw);
69
#else
70
    queryParamSet = qparam_query_parse(uri->query);
71 72 73 74 75 76 77 78 79
#endif

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

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

80 81 82 83 84
        if (STRCASEEQ(queryParam->name, "transport")) {
            if (transport == NULL) {
                continue;
            }

85 86 87
            *transport = strdup(queryParam->value);

            if (*transport == NULL) {
88
                virReportOOMError();
89 90 91 92
                goto failure;
            }

            if (STRNEQ(*transport, "http") && STRNEQ(*transport, "https")) {
93
                ESX_ERROR(VIR_ERR_INVALID_ARG,
94 95
                          _("Query parameter 'transport' has unexpected value "
                            "'%s' (should be http|https)"), *transport);
96 97
                goto failure;
            }
98 99 100 101 102
        } else if (STRCASEEQ(queryParam->name, "vcenter")) {
            if (vCenter == NULL) {
                continue;
            }

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

M
Matthias Bolte 已提交
105
            if (*vCenter == NULL) {
106
                virReportOOMError();
107 108
                goto failure;
            }
109 110 111 112 113
        } else if (STRCASEEQ(queryParam->name, "no_verify")) {
            if (noVerify == NULL) {
                continue;
            }

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

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

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

        if (*transport == NULL) {
143
            virReportOOMError();
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            goto failure;
        }
    }

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

    return result;

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

M
Matthias Bolte 已提交
160 161
    if (vCenter != NULL) {
        VIR_FREE(*vCenter);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    }

    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 已提交
194
int
195
esxUtil_ParseDatastoreRelatedPath(const char *datastoreRelatedPath,
M
Matthias Bolte 已提交
196 197 198 199
                                  char **datastoreName,
                                  char **directoryName, char **fileName)
{
    int result = 0;
200 201 202 203
    char *copyOfDatastoreRelatedPath = NULL;
    char *tmp = NULL;
    char *saveptr = NULL;
    char *preliminaryDatastoreName = NULL;
M
Matthias Bolte 已提交
204 205 206 207 208 209
    char *directoryAndFileName = NULL;
    char *separator = NULL;

    if (datastoreName == NULL || *datastoreName != NULL ||
        directoryName == NULL || *directoryName != NULL ||
        fileName == NULL || *fileName != NULL) {
210
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
211
        return -1;
M
Matthias Bolte 已提交
212 213
    }

214 215 216 217 218 219 220 221 222
    if (esxVI_String_DeepCopyValue(&copyOfDatastoreRelatedPath,
                                   datastoreRelatedPath) < 0) {
        goto failure;
    }

    /* Expected format: '[<datastore>] <path>' */
    if ((tmp = STRSKIP(copyOfDatastoreRelatedPath, "[")) == NULL ||
        (preliminaryDatastoreName = strtok_r(tmp, "]", &saveptr)) == NULL ||
        (directoryAndFileName = strtok_r(NULL, "", &saveptr)) == NULL) {
223
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
224 225
                  _("Datastore related path '%s' doesn't have expected format "
                    "'[<datastore>] <path>'"), datastoreRelatedPath);
M
Matthias Bolte 已提交
226 227 228
        goto failure;
    }

229 230 231 232 233 234 235
    if (esxVI_String_DeepCopyValue(datastoreName,
                                   preliminaryDatastoreName) < 0) {
        goto failure;
    }

    directoryAndFileName += strspn(directoryAndFileName, " ");

M
Matthias Bolte 已提交
236 237 238 239 240 241 242
    /* Split <path> into <directory>/<file>, where <directory> is optional */
    separator = strrchr(directoryAndFileName, '/');

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

        if (*separator == '\0') {
243
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
244
                      _("Datastore related path '%s' doesn't reference a file"),
M
Matthias Bolte 已提交
245 246 247 248
                      datastoreRelatedPath);
            goto failure;
        }

249 250 251
        if (esxVI_String_DeepCopyValue(directoryName,
                                       directoryAndFileName) < 0 ||
            esxVI_String_DeepCopyValue(fileName, separator) < 0) {
M
Matthias Bolte 已提交
252 253 254
            goto failure;
        }
    } else {
255 256 257
        if (esxVI_String_DeepCopyValue(fileName, directoryAndFileName) < 0) {
            goto failure;
        }
M
Matthias Bolte 已提交
258 259 260
    }

  cleanup:
261
    VIR_FREE(copyOfDatastoreRelatedPath);
M
Matthias Bolte 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

    return result;

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

    result = -1;

    goto cleanup;
}



277
int
278
esxUtil_ResolveHostname(const char *hostname,
M
Matthias Bolte 已提交
279
                        char *ipAddress, size_t ipAddress_length)
280 281 282 283 284
{
    struct addrinfo hints;
    struct addrinfo *result = NULL;
    int errcode;

M
Matthias Bolte 已提交
285
    memset(&hints, 0, sizeof(struct addrinfo));
286 287 288 289 290 291 292 293 294

    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) {
295
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
296
                  _("IP address lookup for host '%s' failed: %s"), hostname,
297 298 299 300 301
                  gai_strerror(errcode));
        return -1;
    }

    if (result == NULL) {
302
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
303
                  _("No IP address for host '%s' found: %s"), hostname,
304 305 306 307
                  gai_strerror(errcode));
        return -1;
    }

M
Matthias Bolte 已提交
308 309
    errcode = getnameinfo(result->ai_addr, result->ai_addrlen, ipAddress,
                          ipAddress_length, NULL, 0, NI_NUMERICHOST);
310 311

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

    freeaddrinfo(result);

    return 0;
}



int
327 328
esxUtil_GetConfigString(virConfPtr conf, const char *name, char **string,
                        int optional)
329 330 331 332 333 334 335 336 337 338 339
{
    virConfValuePtr value;

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

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

340
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
341
                  _("Missing essential config entry '%s'"), name);
342 343 344 345
        return -1;
    }

    if (value->type != VIR_CONF_STRING) {
346
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
347
                  _("Config entry '%s' must be a string"), name);
348 349 350 351 352 353 354 355
        return -1;
    }

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

356
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
357
                  _("Missing essential config entry '%s'"), name);
358 359 360 361 362 363
        return -1;
    }

    *string = strdup(value->str);

    if (*string == NULL) {
364
        virReportOOMError();
365 366 367 368 369 370 371 372 373
        return -1;
    }

    return 0;
}



int
374 375
esxUtil_GetConfigUUID(virConfPtr conf, const char *name, unsigned char *uuid,
                      int optional)
376 377 378 379 380 381 382 383 384
{
    virConfValuePtr value;

    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
385
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
386
                      _("Missing essential config entry '%s'"), name);
387 388 389 390 391
            return -1;
        }
    }

    if (value->type != VIR_CONF_STRING) {
392
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
393
                  _("Config entry '%s' must be a string"), name);
394 395 396 397 398 399 400
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        } else {
401
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
402
                      _("Missing essential config entry '%s'"), name);
403 404 405 406
            return -1;
        }
    }

407
    if (virUUIDParse(value->str, uuid) < 0) {
408
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
409
                  _("Could not parse UUID from string '%s'"), value->str);
410 411
        return -1;
    }
412 413 414 415 416 417 418

    return 0;
}



int
419 420
esxUtil_GetConfigLong(virConfPtr conf, const char *name, long long *number,
                      long long default_, int optional)
421 422 423 424 425 426 427 428 429 430
{
    virConfValuePtr value;

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

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
431
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
432
                      _("Missing essential config entry '%s'"), name);
433 434 435 436 437 438 439 440 441
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
442
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
443
                          _("Missing essential config entry '%s'"), name);
444 445 446 447 448 449 450
                return -1;
            }
        }

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

    return 0;
}



int
468 469
esxUtil_GetConfigBoolean(virConfPtr conf, const char *name, int *boolean_,
                         int default_, int optional)
470 471 472
{
    virConfValuePtr value;

M
Matthias Bolte 已提交
473
    *boolean_ = default_;
474 475 476 477 478 479
    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
480
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
481
                      _("Missing essential config entry '%s'"), name);
482 483 484 485 486 487 488 489 490
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
491
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
492
                          _("Missing essential config entry '%s'"), name);
493 494 495 496 497
                return -1;
            }
        }

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

    return 0;
}