From 7671e9862b33bbd5059ae3638353bb4470ead125 Mon Sep 17 00:00:00 2001 From: Nick Kelsey Date: Sat, 27 Aug 2016 12:53:12 -0700 Subject: [PATCH] support initializing ip info for winrt from string set by higher layer --- hdhomerun_discover.c | 3 +++ hdhomerun_sock.h | 1 + hdhomerun_sock_windows.c | 50 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/hdhomerun_discover.c b/hdhomerun_discover.c index c079fc9..edac9b0 100644 --- a/hdhomerun_discover.c +++ b/hdhomerun_discover.c @@ -205,6 +205,9 @@ static bool_t hdhomerun_discover_send_target_ip(struct hdhomerun_discover_t *ds, unsigned int i; for (i = 1; i < ds->sock_count; i++) { struct hdhomerun_discover_sock_t *dss = &ds->socks[i]; + if (dss->subnet_mask == 0) { + continue; + } if ((target_ip & dss->subnet_mask) != (dss->local_ip & dss->subnet_mask)) { continue; } diff --git a/hdhomerun_sock.h b/hdhomerun_sock.h index 0c252db..405fdbf 100644 --- a/hdhomerun_sock.h +++ b/hdhomerun_sock.h @@ -27,6 +27,7 @@ struct hdhomerun_local_ip_info_t { }; extern LIBHDHOMERUN_API int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count); +extern LIBHDHOMERUN_API void hdhomerun_local_ip_info_set_str(const char *ip_info_str); /* WinRT only */ struct hdhomerun_sock_t; diff --git a/hdhomerun_sock_windows.c b/hdhomerun_sock_windows.c index 0d3ecbb..2d8bd8c 100644 --- a/hdhomerun_sock_windows.c +++ b/hdhomerun_sock_windows.c @@ -28,11 +28,57 @@ struct hdhomerun_sock_t { }; #if defined(_WINRT) +static const char *hdhomerun_local_ip_info_str = NULL; + +/* + * String format: ip address '/' subnet mask bits ... + * Example: "192.168.0.100/24 169.254.0.100/16" + */ +void hdhomerun_local_ip_info_set_str(const char *ip_info_str) +{ + if (hdhomerun_local_ip_info_str) { + free(hdhomerun_local_ip_info_str); + } + + hdhomerun_local_ip_info_str = strdup(ip_info_str); +} + int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count) { - return 0; + const char *ptr = hdhomerun_local_ip_info_str; + if (!ptr) { + return 0; + } + + struct hdhomerun_local_ip_info_t *ip_info = ip_info_list; + int count = 0; + + while (count < max_count) { + unsigned int a[4]; + unsigned int mask_bitcount; + if (sscanf(ptr, "%u.%u.%u.%u/%u", &a[0], &a[1], &a[2], &a[3], &mask_bitcount) != 5) { + break; + } + + ip_info->ip_addr = (uint32_t)((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | (a[3] << 0)); + ip_info->subnet_mask = 0xFFFFFFFF << (32 - mask_bitcount); + ip_info++; + + count++; + + ptr = strchr(ptr, ' '); + if (!ptr) { + break; + } + + ptr++; + } + + return count; } -#else +#endif + +#if !defined(_WINRT) int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count) { PIP_ADAPTER_INFO AdapterInfo;