gethostbyname(my_name) and IL2-Sturmovik
Peter Åstrand
astrand at cendio.se
Tue Dec 13 09:57:31 CST 2005
On Tue, 13 Dec 2005, Pavel Troller wrote:
>> 127.0.0.1 localhost.localdomain localhost Zwirch
> Yes, I know that *MANY* well-known distros are wrong with /etc/hosts and
> similar files..
> I think that it is NOT GOOD to patch wine to fix this case; it behaves
> absolutely correctly and all the problem should be solved by correctly
> configuring your Linux networking. Please see any good document about
> naming principles of network interfaces.
I haven't followed the whole thread, so sorry if I've missed something
obvious, but:
I think you are wrong. POSIX does not say that it's wrong to have the
computer name associated with 127.0.0.1 in /etc/hosts, and it doesn't say
that gethostbyname(my_name) should return a "real" IP. If I remember
correctly, though, Microsoft, specifies that gethostbyname(my_name)
*should* return real IP.
This subtle difference means that Wine must take extra care. Fetching a
real IP on a UNIX host is not trivial, but doable.
Instead of just fetching the IP by looking up the hostname, I suggest
using the approach below instead. It will fail if there is no route to
192.168.1.1 (typically this means that you don't have a default gateway).
When this happens, you can resolv the hostname as a fallback.
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
int s, c, got_sockname = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in my_addr;
char *my_ip;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(1);
/* Even better: Use the server you will connect to */
inet_aton("192.168.1.1", &serv_addr.sin_addr);
s = socket (PF_INET, SOCK_DGRAM, 0);
if (s < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
/* connect fails if if server is unreachable, for example */
if ((c = connect(s, (struct sockaddr*) &serv_addr, sizeof(serv_addr))) >= 0) {
socklen_t my_addrlen;
my_addrlen = sizeof(my_addr);
if (getsockname(s, (struct sockaddr*)&my_addr, &my_addrlen) >= 0) {
got_sockname = 1;
}
}
if (!got_sockname)
/* Use 127.0.0.1 as a fallback. Even better: Resolv the
hostname. */
inet_aton("127.0.0.1", &my_addr.sin_addr);
my_ip = inet_ntoa(my_addr.sin_addr);
printf("%s\n", my_ip);
return 0;
}
Regards,
--
Peter Åstrand Chief Developer
Cendio www.thinlinc.com
Teknikringen 3 www.cendio.se
583 30 Linköping Phone: +46-13-21 46 00
More information about the wine-devel
mailing list