xmlrpc.c 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * xmlrpc.c: XML-RPC protocol handler for libvir library
 *
 * Copyright (C) 2006  IBM, Corp.
 *
 * See COPYING.LIB for the License of this software
 *
 * Anthony Liguori <aliguori@us.ibm.com>
 */

J
Jim Meyering 已提交
11 12
#include "config.h"

13
#include "xmlrpc.h"
K
Karel Zak 已提交
14
#include "internal.h"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include <libxml/nanohttp.h>

#include <string.h>
#include <errno.h>

/* TODO
   1) Lots of error checking
   2) xmlRpcValueToSexpr
*/

static xmlNodePtr xmlFirstElement(xmlNodePtr node);
static xmlNodePtr xmlNextElement(xmlNodePtr node);

struct _xmlRpcContext
{
    char *uri;
    int faultCode;
    char *faultMessage;
};

K
Karel Zak 已提交
36 37 38 39 40 41 42 43
static void xmlRpcError(virErrorNumber error, const char *info, int value)
{
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
44
    __virRaiseError(NULL, NULL, NULL, VIR_FROM_RPC, error, VIR_ERR_ERROR,
K
Karel Zak 已提交
45 46 47
                    errmsg, info, NULL, value, 0, errmsg, info, value);
}

48 49 50
static xmlRpcValuePtr xmlRpcValueNew(xmlRpcValueType type)
{
    xmlRpcValuePtr ret = malloc(sizeof(*ret));
K
Karel Zak 已提交
51 52 53 54 55
    
    if (!ret)
        xmlRpcError(VIR_ERR_NO_MEMORY, "allocate value", sizeof(*ret));
    else
        ret->kind = type;
56 57 58 59 60 61
    return ret;
}

static char *xmlGetText(xmlNodePtr node)
{
    for (node = node->children; node; node = node->next)
K
Karel Zak 已提交
62 63 64
	if (node->type == XML_TEXT_NODE) {
	    char *x = strdup((const char *)node->content);
	    if (!x)
65 66
                xmlRpcError(VIR_ERR_NO_MEMORY, _("copying node content"),
                            strlen((const char *)node->content));
K
Karel Zak 已提交
67 68
	    return x;
	}
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    return NULL;
}

static xmlNodePtr xmlFirstElement(xmlNodePtr node)
{
    for (node = node->children; node; node = node->next)
	if (node->type == XML_ELEMENT_NODE)
	    break;
    return node;
}

static xmlNodePtr xmlNextElement(xmlNodePtr node)
{
    for (node = node->next; node; node = node->next)
	if (node->type == XML_ELEMENT_NODE)
	    break;
    return node;
}

K
Karel Zak 已提交
88
static xmlRpcValuePtr xmlRpcValueUnmarshalDateTime(xmlNodePtr node ATTRIBUTE_UNUSED)
89 90
{
    /* we don't need this */
K
Karel Zak 已提交
91
    TODO
92 93 94 95 96 97
    return NULL;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalString(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_STRING);
K
Karel Zak 已提交
98 99 100
    
    if (ret)
        ret->value.string = xmlGetText(node);
101 102 103
    return ret;
}

K
Karel Zak 已提交
104
static xmlRpcValuePtr xmlRpcValueUnmarshalBase64(xmlNodePtr node ATTRIBUTE_UNUSED)
105 106
{
    /* we don't need this */
K
Karel Zak 已提交
107
    TODO
108 109 110 111 112 113 114
    return NULL;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalInteger(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_INTEGER);
    char *value = xmlGetText(node);
K
Karel Zak 已提交
115 116 117 118 119
    
    if (ret && value)
        ret->value.integer = atoi(value);
    if (value)
        free(value);
120 121 122 123 124 125 126 127
    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalBoolean(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_BOOLEAN);
    char *value = xmlGetText(node);

K
Karel Zak 已提交
128 129 130
    if (!ret)
        return NULL;
    if (value && atoi(value))
131 132 133
	ret->value.boolean = true;
    else
	ret->value.boolean = false;
K
Karel Zak 已提交
134 135
    if (value)
        free(value);
136 137 138 139 140 141 142 143
    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalDouble(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_DOUBLE);
    char *value = xmlGetText(node);

K
Karel Zak 已提交
144 145 146 147
    if (ret && value)
        ret->value.real = atof(value);
    if (value)
        free(value);
148 149 150 151 152 153 154 155 156
    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalArray(xmlNodePtr node)
{
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_ARRAY);
    xmlNodePtr cur;
    int n_elements = 0;

K
Karel Zak 已提交
157 158
    if (!ret)
        return NULL;
159

160 161 162 163
    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur))
	n_elements += 1;

    ret->value.array.elements = malloc(n_elements * sizeof(xmlRpcValue));
K
Karel Zak 已提交
164
    if (!ret->value.array.elements) {
165 166
        xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate value array"),
                    n_elements * sizeof(xmlRpcValue));
K
Karel Zak 已提交
167 168 169
	free(ret);
	return NULL;
    }
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    n_elements = 0;
    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur)) {
	ret->value.array.elements[n_elements] = xmlRpcValueUnmarshal(cur);
	n_elements += 1;
    }

    ret->value.array.n_elements = n_elements;

    return ret;
}

static xmlRpcValueDictElementPtr xmlRpcValueUnmarshalDictElement(xmlNodePtr node)
{
    xmlRpcValueDictElementPtr ret = malloc(sizeof(*ret));
    xmlNodePtr cur;

K
Karel Zak 已提交
186 187 188 189
    if (!ret) {
        xmlRpcError(VIR_ERR_NO_MEMORY, "allocate dict", sizeof(*ret));
	return NULL;
    }
190 191 192 193 194 195 196 197
    memset(ret, 0, sizeof(*ret));

    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur)) {
	if (xmlStrEqual(cur->name, BAD_CAST "name")) {
	    ret->name = xmlGetText(cur);
	} else if (xmlStrEqual(cur->name, BAD_CAST "value")) {
	    ret->value = xmlRpcValueUnmarshal(cur);
	} else {
198
            xmlRpcError(VIR_ERR_XML_ERROR, _("unexpected dict node"), 0);
K
Karel Zak 已提交
199 200 201 202 203 204
	    if (ret->name)
		free(ret->name);
	    if (ret->value)
		xmlRpcValueFree(ret->value);
	    free(ret);
	    return NULL;
205 206 207 208 209 210 211 212 213 214 215 216 217 218
	}
    }

    ret->next = NULL;

    return ret;
}

static xmlRpcValuePtr xmlRpcValueUnmarshalDict(xmlNodePtr node)
{
    xmlRpcValueDictElementPtr root = NULL, *elem = &root;
    xmlRpcValuePtr ret = xmlRpcValueNew(XML_RPC_STRUCT);
    xmlNodePtr cur;

K
Karel Zak 已提交
219 220 221 222 223
    if (!ret)
	return NULL;
    
    ret->value.dict.root = root;
    
224 225
    for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur)) {
	*elem = xmlRpcValueUnmarshalDictElement(cur);
K
Karel Zak 已提交
226 227 228 229
	if (*elem==NULL) {
	    xmlRpcValueFree(ret);
	    return NULL;
	} 
230 231 232 233 234 235 236 237 238
	elem = &(*elem)->next;
    }

    return ret;
}

xmlRpcValuePtr xmlRpcValueUnmarshal(xmlNodePtr node)
{
    xmlNodePtr n;
K
Karel Zak 已提交
239
    xmlRpcValuePtr ret = NULL;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

    if (xmlStrEqual(node->name, BAD_CAST "value")) {
	n = xmlFirstElement(node);
	if (n == NULL) {
	    ret = xmlRpcValueUnmarshalString(node);
	} else {
	    ret = xmlRpcValueUnmarshal(n);
	}
    } else if (xmlStrEqual(node->name, BAD_CAST "dateTime.iso8601")) {
	ret = xmlRpcValueUnmarshalDateTime(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "string")) {
	ret = xmlRpcValueUnmarshalString(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "base64")) {
	ret = xmlRpcValueUnmarshalBase64(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "i4") ||
	       xmlStrEqual(node->name, BAD_CAST "int")) {
	ret = xmlRpcValueUnmarshalInteger(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "boolean")) {
	ret = xmlRpcValueUnmarshalBoolean(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "double")) {
	ret = xmlRpcValueUnmarshalDouble(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "array")) {
	ret = xmlRpcValueUnmarshal(xmlFirstElement(node));
    } else if (xmlStrEqual(node->name, BAD_CAST "data")) {
	ret = xmlRpcValueUnmarshalArray(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "struct")) {
	ret = xmlRpcValueUnmarshalDict(node);
    } else if (xmlStrEqual(node->name, BAD_CAST "nil")) {
	ret = xmlRpcValueNew(XML_RPC_NIL);
    } else {
270
        xmlRpcError(VIR_ERR_XML_ERROR, _("unexpected value node"), 0);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    }

    return ret;
}

void xmlRpcValueFree(xmlRpcValuePtr value)
{
    int i;
    xmlRpcValueDictElementPtr cur, next;

    if (value == NULL)
	return;

    switch (value->kind) {
    case XML_RPC_ARRAY:
	for (i = 0; i < value->value.array.n_elements; i++)
	    xmlRpcValueFree(value->value.array.elements[i]);
	free(value->value.array.elements);
	break;
    case XML_RPC_STRUCT:
	next = value->value.dict.root;
	while (next) {
	    cur = next;
	    next = next->next;
	    free(cur->name);
	    xmlRpcValueFree(cur->value);
	    free(cur);
	}
	break;
    case XML_RPC_STRING:
	free(value->value.string);
	break;
    default:
	break;
    }

    free(value);
}

void xmlRpcValueMarshal(xmlRpcValuePtr value, virBufferPtr buf, int indent)
{
    int i;
    xmlRpcValueDictElement *elem;

    virBufferVSprintf(buf, "%*s<value>", indent, "");
    switch (value->kind) {
    case XML_RPC_ARRAY:
K
Karel Zak 已提交
318
	virBufferStrcat(buf, "<array><data>\n", NULL);
319 320 321 322 323
	for (i = 0; i < value->value.array.n_elements; i++)
	    xmlRpcValueMarshal(value->value.array.elements[i], buf, indent+2);
	virBufferVSprintf(buf, "%*s</data></array>", indent, "");
	break;
    case XML_RPC_STRUCT:
K
Karel Zak 已提交
324
	virBufferStrcat(buf, "<struct>\n", NULL);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	indent += 2;
	for (elem = value->value.dict.root; elem; elem = elem->next) {
	    virBufferVSprintf(buf, "%*s<member>\n", indent, "");
	    virBufferVSprintf(buf, "%*s<name>%s</name>\n",
			      indent + 2, "", elem->name);
	    xmlRpcValueMarshal(elem->value, buf, indent + 2);
	    virBufferVSprintf(buf, "%*s</member>\n", indent, "");
	}
	indent -= 2;
	virBufferVSprintf(buf, "%*s</struct>", indent, "");
	break;
    case XML_RPC_INTEGER:
	virBufferVSprintf(buf, "<int>%d</int>", value->value.integer);
	break;
    case XML_RPC_DOUBLE:
	virBufferVSprintf(buf, "<double>%f</double>", value->value.real);
	break;
    case XML_RPC_BOOLEAN:
	if (value->value.boolean)
	    i = 1;
	else
	    i = 0;
	virBufferVSprintf(buf, "<boolean>%d</boolean>", i);
	break;
    case XML_RPC_DATE_TIME:
	/* FIXME */
K
Karel Zak 已提交
351
	TODO
352 353 354
	break;
    case XML_RPC_BASE64:
	/* FIXME */
K
Karel Zak 已提交
355
	TODO
356 357
	break;
    case XML_RPC_STRING:
K
Karel Zak 已提交
358 359
	virBufferStrcat(buf, 
		"<string>", value->value.string, "</string>", NULL);
360 361
	break;
    case XML_RPC_NIL:
K
Karel Zak 已提交
362
	virBufferStrcat(buf, "<nil> </nil>", NULL);
363 364
	break;
    }
K
Karel Zak 已提交
365
    virBufferStrcat(buf, "</value>\n", NULL);
366 367 368 369 370 371 372 373
}

virBufferPtr xmlRpcMarshalRequest(const char *request,
				  int argc, xmlRpcValuePtr *argv)
{
    virBufferPtr buf;
    int i;

K
Karel Zak 已提交
374 375 376 377 378 379 380
    buf = virBufferNew(1024);

    virBufferStrcat(buf,
		    "<?xml version=\"1.0\"?>\n"
		    "<methodCall>\n"
		    "  <methodName>", request, "</methodName>\n"
		    "  <params>\n", NULL);
381
    for (i = 0; i < argc; i++) {
K
Karel Zak 已提交
382 383
	virBufferStrcat(buf,  
                    "    <param>\n", NULL);
384
	xmlRpcValueMarshal(argv[i], buf, 6);
K
Karel Zak 已提交
385 386
	virBufferStrcat(buf,  
                    "    </param>\n", NULL);
387
    }
K
Karel Zak 已提交
388 389 390
    virBufferStrcat(buf,
                    "  </params>\n"
		    "</methodCall>\n", NULL);
391 392 393 394 395
    return buf;
}

xmlRpcValuePtr xmlRpcUnmarshalResponse(xmlNodePtr node, bool *is_fault)
{
K
Karel Zak 已提交
396 397 398
    if (!node)
	return NULL;

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    if (!xmlStrEqual(node->name, BAD_CAST "methodResponse"))
	return NULL;

    node = xmlFirstElement(node);
    if (xmlStrEqual(node->name, BAD_CAST "params")) {
	node = xmlFirstElement(node);

	if (!xmlStrEqual(node->name, BAD_CAST "param"))
	    return NULL;

	*is_fault = false;
	return xmlRpcValueUnmarshal(xmlFirstElement(node));
    } else if (xmlStrEqual(node->name, BAD_CAST "fault")) {
	*is_fault = true;
	return xmlRpcValueUnmarshal(xmlFirstElement(node));
    } else
	return NULL;
}

static char *xmlRpcCallRaw(const char *url, const char *request)
{
	void *cxt;
K
Karel Zak 已提交
421
	char *contentType = (char *) "text/xml";
422 423 424 425 426 427 428 429 430
	int len, ret, serrno;
	char *response = NULL;

	cxt = xmlNanoHTTPMethod(url,
				"POST",
				request,
				&contentType,
				NULL,
				strlen(request));
431

K
Karel Zak 已提交
432
	if (cxt == NULL) {
433
                xmlRpcError(VIR_ERR_POST_FAILED, _("send request"), 0);
434
		goto error;
K
Karel Zak 已提交
435
	}
436 437 438

	if (contentType && strcmp(contentType, "text/xml") != 0) {
		errno = EINVAL;
439
		xmlRpcError(VIR_ERR_POST_FAILED, _("unexpected mime type"), 0);
440 441 442 443 444
		goto error;
	}

	len = xmlNanoHTTPContentLength(cxt);
	response = malloc(len + 1);
K
Karel Zak 已提交
445
	if (response == NULL) {
446
		xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate response"), len);
447
		goto error;
K
Karel Zak 已提交
448
	}
449 450 451 452 453
	ret = xmlNanoHTTPRead(cxt, response, len);
	if (ret != len) {
		errno = EINVAL;
		free(response);
		response = NULL;
454
		xmlRpcError(VIR_ERR_POST_FAILED, _("read response"), 0);
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	}

	response[len] = 0;

 error:
	serrno = errno;
	if (cxt) {
		xmlNanoHTTPClose(cxt);
		free(contentType);
	}
	errno = serrno;

	return response;
}

static char **xmlRpcStringArray(xmlRpcValuePtr value)
{
    char **ret, *ptr;
    int i;
    size_t size = 0;

    if (value->kind != XML_RPC_ARRAY)
	return NULL;

    size = sizeof(char *) * (value->value.array.n_elements + 1);

    for (i = 0; i < value->value.array.n_elements; i++)
	if (value->value.array.elements[i]->kind == XML_RPC_STRING)
	    size += strlen(value->value.array.elements[i]->value.string) + 1;

K
Karel Zak 已提交
485
    if (!(ptr = malloc(size))) {
486
	xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate string array"), size);
K
Karel Zak 已提交
487 488
	return NULL;
    }
489 490 491 492 493 494 495 496 497 498
    ret = (char **)ptr;
    ptr += sizeof(char *) * (value->value.array.n_elements + 1);

    for (i = 0; i < value->value.array.n_elements; i++) {
	if (value->value.array.elements[i]->kind == XML_RPC_STRING) {
	    char *s = value->value.array.elements[i]->value.string;
	    strcpy(ptr, s);
	    ret[i] = ptr;
	    ptr += strlen(s) + 1;
	} else
K
Karel Zak 已提交
499
	    ret[i] = (char *) "";
500 501 502 503 504 505 506
    }

    ret[i] = NULL;

    return ret;
}

K
Karel Zak 已提交
507 508
xmlRpcValuePtr *
xmlRpcArgvNew(const char *fmt, va_list ap, int *argc)
509 510
{
    xmlRpcValuePtr *argv;
K
Karel Zak 已提交
511
    const char *ptr;
512
    int i;
K
Karel Zak 已提交
513 514
    
    *argc = strlen(fmt);
K
Karel Zak 已提交
515
    if (!(argv = malloc(sizeof(*argv) * *argc))) {
516
        xmlRpcError(VIR_ERR_NO_MEMORY, _("read response"), sizeof(*argv) * *argc);
K
Karel Zak 已提交
517 518
        return NULL;
    }
519 520 521 522
    i = 0;
    for (ptr = fmt; *ptr; ptr++) {
	switch (*ptr) {
	case 'i':
K
Karel Zak 已提交
523 524
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_INTEGER)))
		argv[i]->value.integer = va_arg(ap, int32_t);
525 526
	    break;
	case 'f':
K
Karel Zak 已提交
527 528
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_DOUBLE)))
		argv[i]->value.real = va_arg(ap, double);
529 530
	    break;
	case 'b':
K
Karel Zak 已提交
531 532
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_BOOLEAN)))
	        argv[i]->value.boolean = va_arg(ap, int);
533 534
	    break;
	case 's':
K
Karel Zak 已提交
535 536
	    if ((argv[i] = xmlRpcValueNew(XML_RPC_STRING)))
  	        argv[i]->value.string = strdup(va_arg(ap, const char *));
537 538
	    break;
	default:
K
Karel Zak 已提交
539 540 541 542 543
	    argv[i] = NULL;
	    break;
	}
	if (argv[i]==NULL) {
	    xmlRpcArgvFree(i, argv);
K
Karel Zak 已提交
544
	    return NULL;
545 546 547
	}
	i++;
    }
K
Karel Zak 已提交
548 549
    return argv;
}
550

K
Karel Zak 已提交
551 552 553 554
void
xmlRpcArgvFree(int argc, xmlRpcValuePtr *argv)
{
    int i;
K
Karel Zak 已提交
555 556
    if (!argv)
	return;
557 558 559 560
    for (i = 0; i < argc; i++)
	xmlRpcValueFree(argv[i]);

    free(argv);
K
Karel Zak 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
}

int xmlRpcCall(xmlRpcContextPtr context, const char *method,
	       const char *retfmt, const char *fmt, ...)
{
    va_list ap;
    int argc;
    xmlRpcValuePtr *argv;
    virBufferPtr buf;
    char *ret;
    xmlDocPtr xml;
    xmlNodePtr node;
    bool fault;
    xmlRpcValuePtr value;
    void *retval = NULL;

    va_start(ap, fmt);
    
    if (retfmt && *retfmt)
	retval = va_arg(ap, void *);
 
K
Karel Zak 已提交
582 583
    if (!(argv = xmlRpcArgvNew(fmt, ap, &argc)))
	return -1;
K
Karel Zak 已提交
584 585 586 587 588 589
    
    va_end(ap);

    buf = xmlRpcMarshalRequest(method, argc, argv);

    xmlRpcArgvFree(argc, argv);
590
	
K
Karel Zak 已提交
591 592 593
    if (!buf)
	return -1;
    
594 595
    ret = xmlRpcCallRaw(context->uri, buf->content);

K
Karel Zak 已提交
596
    virBufferFree(buf);
597

K
Karel Zak 已提交
598 599 600
    if (!ret)
	return -1;

601 602 603 604 605 606 607
    xml = xmlReadDoc((const xmlChar *)ret, "response.xml", NULL,
		     XML_PARSE_NOENT | XML_PARSE_NONET |
		     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    free(ret);

    if (xml == NULL) {
	errno = EINVAL;
608
	xmlRpcError(VIR_ERR_XML_ERROR, _("parse server response failed"), 0);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	return -1;
    }

    node = xmlDocGetRootElement(xml);

    value = xmlRpcUnmarshalResponse(node, &fault);

    if (!fault) {
	switch (*retfmt) {
	case 'i':
	    if (value->kind == XML_RPC_INTEGER)
		*(int32_t *)retval = value->value.integer;
	    break;
	case 'b':
	    if (value->kind == XML_RPC_BOOLEAN)
		*(bool *)retval = value->value.boolean;
	    break;
	case 'f':
	    if (value->kind == XML_RPC_DOUBLE)
		*(double *)retval = value->value.real;
	    break;
	case 's':
	    if (value->kind == XML_RPC_STRING)
		*(char **)retval = strdup(value->value.string);
	    break;
	case 'S':
	    *(char ***)retval = xmlRpcStringArray(value);
	    break;
	case 'V':
	    *(xmlRpcValuePtr *)retval = value;
	    value = NULL;
	    break;
	default:
	    printf("not supported yet\n");
	    break;
	}
    }

    xmlFreeDoc(xml);

K
Karel Zak 已提交
649 650 651
    if (fault) { 
	/* FIXME we need generic dict routines */
	/* FIXME we need faultMessage propagate to libvirt error API */
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	context->faultCode = value->value.dict.root->value->value.integer;
	context->faultMessage = strdup(value->value.dict.root->next->value->value.string);
	xmlRpcValueFree(value);
	errno = EFAULT;
	return -1;
    }

    xmlRpcValueFree(value);

    return 0;
}

xmlRpcContextPtr xmlRpcContextNew(const char *uri)
{
    xmlRpcContextPtr ret = malloc(sizeof(*ret));

    if (ret) {
	ret->uri = strdup(uri);
	ret->faultMessage = NULL;
K
Karel Zak 已提交
671
    } else
672
        xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate new context"), sizeof(*ret));
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

    return ret;
}

void xmlRpcContextFree(xmlRpcContextPtr context)
{
    if (context) {
	if (context->uri)
	    free(context->uri);

	if (context->faultMessage)
	    free(context->faultMessage);

	free(context);
    }
}

int xmlRpcContextFaultCode(xmlRpcContextPtr context)
{
    return context->faultCode;
}

const char *xmlRpcContextFaultMessage(xmlRpcContextPtr context)
{
    return context->faultMessage;
}