mirror of
https://github.com/Silicondust/libhdhomerun
synced 2025-07-06 13:02:21 -07:00
support multiple ip addresses on same interface
This commit is contained in:
parent
d2de03dc0f
commit
707ad32020
1 changed files with 26 additions and 40 deletions
|
@ -44,26 +44,16 @@ int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ifconf ifc;
|
int ifreq_buffer_size = 128 * sizeof(struct ifreq);
|
||||||
size_t ifreq_buffer_size = 0;
|
char *ifreq_buffer = (char *)calloc(ifreq_buffer_size, 1);
|
||||||
void *ifreq_buffer = NULL;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
ifreq_buffer_size += 64 * sizeof(struct ifreq);
|
|
||||||
|
|
||||||
void *old_buffer = ifreq_buffer;
|
|
||||||
ifreq_buffer = realloc(old_buffer, ifreq_buffer_size);
|
|
||||||
if (!ifreq_buffer) {
|
if (!ifreq_buffer) {
|
||||||
if (old_buffer) {
|
|
||||||
free(old_buffer);
|
|
||||||
}
|
|
||||||
close(sock);
|
close(sock);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ifc.ifc_len = (int)ifreq_buffer_size;
|
struct ifconf ifc;
|
||||||
ifc.ifc_buf = (char *)ifreq_buffer;
|
ifc.ifc_len = ifreq_buffer_size;
|
||||||
memset(ifreq_buffer, 0, ifreq_buffer_size);
|
ifc.ifc_buf = ifreq_buffer;
|
||||||
|
|
||||||
if (ioctl(sock, SIOCGIFCONF, &ifc) != 0) {
|
if (ioctl(sock, SIOCGIFCONF, &ifc) != 0) {
|
||||||
free(ifreq_buffer);
|
free(ifreq_buffer);
|
||||||
|
@ -71,20 +61,27 @@ int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ifc.ifc_len < (int)ifreq_buffer_size) {
|
if (ifc.ifc_len > ifreq_buffer_size) {
|
||||||
break;
|
ifc.ifc_len = ifreq_buffer_size;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct hdhomerun_local_ip_info_t *ip_info = ip_info_list;
|
struct hdhomerun_local_ip_info_t *ip_info = ip_info_list;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
char *ptr = ifc.ifc_buf;
|
char *ptr = ifc.ifc_buf;
|
||||||
char *end = ifc.ifc_buf + ifc.ifc_len;
|
char *end = ifc.ifc_buf + ifc.ifc_len;
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
while (ptr + sizeof(struct ifreq) <= end) {
|
while (ptr + sizeof(struct ifreq) <= end) {
|
||||||
struct ifreq *ifr = (struct ifreq *)ptr;
|
struct ifreq *ifr = (struct ifreq *)ptr;
|
||||||
ptr += sizeof(struct ifreq);
|
ptr += sizeof(struct ifreq);
|
||||||
|
|
||||||
|
/* Local IP address. */
|
||||||
|
struct sockaddr_in *ip_addr_in = (struct sockaddr_in *)&ifr->ifr_addr;
|
||||||
|
uint32_t ip_addr = ntohl(ip_addr_in->sin_addr.s_addr);
|
||||||
|
if (ip_addr == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
/* Flags. */
|
/* Flags. */
|
||||||
if (ioctl(sock, SIOCGIFFLAGS, ifr) != 0) {
|
if (ioctl(sock, SIOCGIFFLAGS, ifr) != 0) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -95,17 +92,6 @@ int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Local IP address. */
|
|
||||||
if (ioctl(sock, SIOCGIFADDR, ifr) != 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sockaddr_in *ip_addr_in = (struct sockaddr_in *)&ifr->ifr_addr;
|
|
||||||
uint32_t ip_addr = ntohl(ip_addr_in->sin_addr.s_addr);
|
|
||||||
if (ip_addr == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Subnet mask. */
|
/* Subnet mask. */
|
||||||
if (ioctl(sock, SIOCGIFNETMASK, ifr) != 0) {
|
if (ioctl(sock, SIOCGIFNETMASK, ifr) != 0) {
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue