[PATCH] host: Support EC2 instance ID on Windows.
by Russell Bryant
Support getting the EC2 instance ID on Windows when the "hardware" UUID
is requested from the Host agent.
Signed-off-by: Russell Bryant <rbryant(a)redhat.com>
---
src/lib/CMakeLists.txt | 2 +-
src/lib/host_windows.c | 38 ++++++++++++++++++++++++++++++++++++--
2 files changed, 37 insertions(+), 3 deletions(-)
diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt
index a4c7702..68a60ea 100644
--- a/src/lib/CMakeLists.txt
+++ b/src/lib/CMakeLists.txt
@@ -32,7 +32,7 @@ target_link_libraries(msysconfig ${pcre_LIBRARIES} mcommon mservice ${glib_LIBRA
if(WIN32)
foreach (lib ${LIBS})
- target_link_libraries(${lib} wsock32 dnsapi rpcrt4)
+ target_link_libraries(${lib} wsock32 dnsapi rpcrt4 wininet)
install(TARGETS ${lib} DESTINATION sbin)
endforeach (lib ${LIBS})
else(WIN32)
diff --git a/src/lib/host_windows.c b/src/lib/host_windows.c
index 23435ec..f53567c 100644
--- a/src/lib/host_windows.c
+++ b/src/lib/host_windows.c
@@ -21,6 +21,7 @@
#include <winsock.h>
#include <windows.h>
#include <winbase.h>
+#include <wininet.h>
#include <stdio.h>
#include <stdlib.h>
@@ -156,9 +157,42 @@ host_os_machine_uuid(void)
char *
host_os_ec2_instance_id(void)
{
- /* XXX */
+ static const char URI[] = "http://169.254.169.254/latest/meta-data/instance-id";
+ HINTERNET internet = NULL;
+ HINTERNET open_url = NULL;
+ DWORD bytes_read = 0;
+ char buf[256] = "";
+
+ internet = InternetOpenA("Matahari", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
+ if (!internet) {
+ mh_err("Failed to open the internets (%lu)", (unsigned long) GetLastError());
+ return NULL;
+ }
+
+ open_url = InternetOpenUrlA(internet, URI, NULL, 0, INTERNET_FLAG_RELOAD, 0);
+ if (!open_url) {
+ mh_err("Failed to open URL '%s' (%lu)", URI, (unsigned long) GetLastError());
+ goto return_cleanup;
+ }
+
+ while (InternetReadFile(open_url, buf + strlen(buf),
+ sizeof(buf) - strlen(buf) - 1,
+ &bytes_read) == FALSE || bytes_read != 0)
+ {
+ /* Mad commenting in a while loop, what what */
+ }
+
+return_cleanup:
+
+ if (open_url && InternetCloseHandle(open_url) == FALSE) {
+ mh_err("Failed to close the HTTP request. (%lu)", (unsigned long) GetLastError());
+ }
+
+ if (internet && InternetCloseHandle(internet) == FALSE) {
+ mh_err("Failed to close the internets. (%lu)", (unsigned long) GetLastError());
+ }
- return NULL;
+ return mh_strlen_zero(buf) ? NULL : strdup(buf);
}
/**
--
1.7.6.2
12 years, 7 months