Added initial ability to connect remote hosts

This commit is contained in:
Daniel P. Berrange 2007-08-27 22:57:59 -04:00
parent fe0424ff1f
commit 2b2e4c498a

View File

@ -31,6 +31,7 @@
#include <getopt.h> #include <getopt.h>
#include <libvirt/libvirt.h> #include <libvirt/libvirt.h>
#include <libxml/xpath.h> #include <libxml/xpath.h>
#include <libxml/uri.h>
#define DEBUG 0 #define DEBUG 0
@ -479,7 +480,7 @@ static virDomainPtr viewer_lookup_domain(virConnectPtr conn, const char *name)
return dom; return dom;
} }
static int viewer_extract_vnc_graphics(virDomainPtr dom, char **host, char **port) static int viewer_extract_vnc_graphics(virDomainPtr dom, char **port)
{ {
char *xmldesc = virDomainGetXMLDesc(dom, 0); char *xmldesc = virDomainGetXMLDesc(dom, 0);
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
@ -512,11 +513,6 @@ static int viewer_extract_vnc_graphics(virDomainPtr dom, char **host, char **por
*port = strdup((const char*)obj->stringval); *port = strdup((const char*)obj->stringval);
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
obj = NULL; obj = NULL;
/*
obj = xmlXPathEval((const xmlChar *)"string(/domain/devices/graphics[@type='vnc']/@listen)", ctxt);
if (!obj || obj->type != XPATH_STRING || !obj->stringval || !obj->stringval[0])
*/
*host = NULL;
missing: missing:
ret = 0; ret = 0;
@ -533,6 +529,44 @@ static int viewer_extract_vnc_graphics(virDomainPtr dom, char **host, char **por
return ret; return ret;
} }
static int viewer_extract_host(const char *uristr, char **host, char **transport)
{
xmlURIPtr uri;
char *offset;
*host = NULL;
*transport = NULL;
if (uristr == NULL ||
!strcasecmp(uristr, "xen"))
uristr = "xen:///";
uri = xmlParseURI(uristr);
if (!uri || !uri->server) {
*host = strdup("localhost");
} else {
*host = strdup(uri->server);
}
if (!*host) {
xmlFreeURI(uri);
return -1;
}
offset = strchr(uri->scheme, '+');
if (offset) {
*transport = strdup(offset+1);
if (!*transport) {
free(*host);
*host = NULL;
xmlFreeURI(uri);
return -1;
}
}
xmlFreeURI(uri);
return 0;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
GtkWidget *window; GtkWidget *window;
@ -554,6 +588,7 @@ int main(int argc, char **argv)
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
char *host = NULL; char *host = NULL;
char *port = NULL; char *port = NULL;
char *transport = NULL;
while ((ch = getopt_long(argc, argv, sopts, lopts, &opt_ind)) != -1) { while ((ch = getopt_long(argc, argv, sopts, lopts, &opt_ind)) != -1) {
switch (ch) { switch (ch) {
@ -603,7 +638,7 @@ int main(int argc, char **argv)
} while (!dom); } while (!dom);
do { do {
viewer_extract_vnc_graphics(dom, &host, &port); viewer_extract_vnc_graphics(dom, &port);
if (!port && !waitvnc) { if (!port && !waitvnc) {
fprintf(stderr, "unable to find vnc graphics for %s\n", argv[optind]); fprintf(stderr, "unable to find vnc graphics for %s\n", argv[optind]);
return 4; return 4;
@ -611,8 +646,11 @@ int main(int argc, char **argv)
usleep(300*1000); usleep(300*1000);
} while (!port); } while (!port);
if (!host) if (viewer_extract_host(uri, &host, &transport) < 0) {
host = strdup("localhost"); fprintf(stderr, "unable to determine hostname for URI %s\n", uri);
return 5;
}
DEBUG_LOG("Remote host is %s and transport %s\n", host, transport ? transport : "");
vnc = vnc_display_new(); vnc = vnc_display_new();
window = viewer_build_window(VNC_DISPLAY(vnc)); window = viewer_build_window(VNC_DISPLAY(vnc));