* [Qemu-devel] Patch for qemu-0.9.1/slirp/misc.c:getouraddr
@ 2008-11-07 18:42 Wan-Teh Chang
2008-11-09 11:43 ` Blue Swirl
0 siblings, 1 reply; 3+ messages in thread
From: Wan-Teh Chang @ 2008-11-07 18:42 UTC (permalink / raw)
To: qemu-devel
[-- Attachment #1: Type: text/plain, Size: 566 bytes --]
Attached is a patch for qemu-0.9.1/slirp/misc.c to implement
the getouraddr() function using the BSD function getifaddrs(),
which is also available in glibc on Linux.
This patch does not include the necessary change to define
HAVE_GETIFADDRS on the platforms that have getifaddrs().
You will need to either use an autoconf test for getifaddrs(),
or do something like
#if defined(__GLIBC__) || defined(__FreeBSD__) ||
defined(__NetBSD__) || defined(__OpenBSD__)
#define HAVE_GETIFADDRS 1
#endif
at the top of the qemu-0.9.1/slirp/misc.c file.
Wan-Teh Chang
[-- Attachment #2: getouraddr.txt --]
[-- Type: text/plain, Size: 1153 bytes --]
--- qemu-0.9.1/slirp/misc.c 2008-10-29 17:33:03.000000000 -0700
+++ qemu-0.9.1/slirp/misc.c 2008-11-03 17:15:51.000000000 -0800
@@ -7,6 +7,9 @@
#define WANT_SYS_IOCTL_H
#include <slirp.h>
+#ifdef HAVE_GETIFADDRS
+#include <ifaddrs.h>
+#endif
u_int curtime, time_fasttimo, last_slowtimo;
@@ -86,6 +89,26 @@
void
getouraddr()
{
+#ifdef HAVE_GETIFADDRS
+ struct ifaddrs *ifp;
+
+ if (getifaddrs(&ifp) == 0) {
+ struct ifaddrs *ifa;
+ for (ifa = ifp; ifa; ifa = ifa->ifa_next) {
+ struct sockaddr *sa;
+
+ sa = ifa->ifa_addr;
+ if (sa->sa_family == AF_INET) {
+ struct sockaddr_in *sin = (struct sockaddr_in *) sa;
+ if (sin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
+ our_addr = sin->sin_addr;
+ break;
+ }
+ }
+ }
+ freeifaddrs(ifp);
+ }
+#else
char buff[256];
struct hostent *he = NULL;
@@ -93,6 +116,7 @@
he = gethostbyname(buff);
if (he)
our_addr = *(struct in_addr *)he->h_addr;
+#endif
if (our_addr.s_addr == 0)
our_addr.s_addr = loopback_addr.s_addr;
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] Patch for qemu-0.9.1/slirp/misc.c:getouraddr
2008-11-07 18:42 [Qemu-devel] Patch for qemu-0.9.1/slirp/misc.c:getouraddr Wan-Teh Chang
@ 2008-11-09 11:43 ` Blue Swirl
2008-11-10 18:04 ` Wan-Teh Chang
0 siblings, 1 reply; 3+ messages in thread
From: Blue Swirl @ 2008-11-09 11:43 UTC (permalink / raw)
To: qemu-devel
[-- Attachment #1: Type: text/plain, Size: 635 bytes --]
On 11/7/08, Wan-Teh Chang <wtc@google.com> wrote:
> Attached is a patch for qemu-0.9.1/slirp/misc.c to implement
> the getouraddr() function using the BSD function getifaddrs(),
> which is also available in glibc on Linux.
How is this better than the current way? Does this change something on
hosts with multiple interfaces? What if some interfaces are down?
> This patch does not include the necessary change to define
> HAVE_GETIFADDRS on the platforms that have getifaddrs().
> You will need to either use an autoconf test for getifaddrs(),
I added probing for getifaddrs to configure in the attached patch,
seems to work.
[-- Attachment #2: getifaddrs_wtc.diff --]
[-- Type: plain/text, Size: 1866 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] Patch for qemu-0.9.1/slirp/misc.c:getouraddr
2008-11-09 11:43 ` Blue Swirl
@ 2008-11-10 18:04 ` Wan-Teh Chang
0 siblings, 0 replies; 3+ messages in thread
From: Wan-Teh Chang @ 2008-11-10 18:04 UTC (permalink / raw)
To: qemu-devel
On Sun, Nov 9, 2008 at 3:43 AM, Blue Swirl <blauwirbel@gmail.com> wrote:
> On 11/7/08, Wan-Teh Chang <wtc@google.com> wrote:
>> Attached is a patch for qemu-0.9.1/slirp/misc.c to implement
>> the getouraddr() function using the BSD function getifaddrs(),
>> which is also available in glibc on Linux.
>
> How is this better than the current way? Does this change something on
> hosts with multiple interfaces? What if some interfaces are down?
The current way gets the local host's IP addresses by calling
gethostbyname, which is usually implemented with a DNS
lookup. So the current way usually requires a network request,
and it cannot get the IP addresses of a host that doesn't have
a name.
On hosts with multiple interfaces, the patch may change the
order in which the addresses are listed. Since both the current
way and the patch just take the first (non-loopback) address
on the list, the patch could change the address chosen by
getouraddr.
If some interfaces are down, I believe the current way can't
detect that, whereas this patch can be enhanced to detect
that. I believe that we can test the ifa_flags field of the
ifaddrs structure to see if the IFF_UP flag (or the
IFF_RUNNING flag?) is set.
Wan-Teh Chang
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-11-10 18:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-07 18:42 [Qemu-devel] Patch for qemu-0.9.1/slirp/misc.c:getouraddr Wan-Teh Chang
2008-11-09 11:43 ` Blue Swirl
2008-11-10 18:04 ` Wan-Teh Chang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).