.gethostbyaddr() does not work on all devices. Why? (Python) - sockets

I wrote a script that goes through all local IPs in the '0' network (192.168.0.*).
I use a loop with socket.gethostbyaddr(ip_modified_by_the_loop_each_time).
The loop returns me 3 devices their hostnames.
Now, I found that it does not find 4 devices (at least).
It does wind my Windows Laptop, not my Android Phone. It does find
one MacBook, not the iMac, not the other MacBook. It also does not
find any phone.
I know that all the devices are online and have an IPv4 address.
I can ping them very well, it responds just fine. But when I use socket.gethostbyaddr(ip_modified_by_the_loop_each_time) it throws an error.
I used a try:/except: statement to get around the error, but it still can not find my phone by IP and get the hostname, but I know it is there.
I am using python 3.3
Why is that happening, how to fix that?
Thank you.

gethostbyaddr tries to find out the hostname for a given IP address by using either locally stored information (i.e. /etc/hosts) or doing a reverse DNS lookup. The result does not depend on the host being online or not, they only depend on the availability of these information.
And these information can also not be trusted. For example the DNS operator of a domain could add the following records:
test.example.com A public.ip.of.test
public.ip.of.test PTR localhost.
In this case a lookup (gethostbyname) of test.example.com would return the IP address of this host while a reverse lookup (gethostbyaddr) would claim that the hostname is localhost. And if the PTR record would be missing gethostbyaddr would fail, even if the host is online.
See also wikipedia: reverse dns lookup.

Related

How to communicate between two embedded devices over ethernet?

I am not expert on this subject, need your suggestions. I have a few devices on ethernet/LAN. I wanted to have these devices communicate over LAN without going into their network configurations. Below is my plan, please let me know if this can be achievable:
1) I install TCP/IP stack on these devices
2) I give them their hostnames like device1, device 2 etc....
3) I configure these devices for local-link networking, so that they can configure themselves and get their IPs.
4) I use socket programming and access each device by using hostnames.
Please let me know if this can be done without much hassle, and let me know if I have missed out something in above mentioned points.
Thanks in advance.
This is basically a good approach.
If your embedded devices have a good IPv6 stack, I recommend using it instead of IPv4. link local addresses work better with IPv6. Most IPv4 stacks are configured to try to obtain an address from a DHCP server first, and only fall back to link local addresses if they can't get one. The link local address and DHCP address might be mutually exclusive, so the link local address doesn't become operational until after DHCP has been tried and timed out. The link local address might even be disrupted if DHCP periodically retries. Although the devices will normally be connected to each other and there won't be a DHCP server, you don't want to disable DHCP, because the devices probably should obtain a DHCP address if they are ever connected to a wider network. With IPv6 on the other hand, the link local addresses come up immediately when the interface comes up, and stay up continuously while other IP addresses (from autoconf or from DHCPv6) may come and go.
Link local addresses alone won't let the devices contact each other by hostname, but dns-sd (mDNS, zeroconf) will. If you are using Linux then you can use avahi as a dns-sd stack.
Better than using hostnames, consider having the devices probe for each other by dns-sd service type. If you are using a custom protocol between your devices, make up a unique service tpe name and have the devices advertise themselves it and probe for it with dns-sd.

iPhone: add entry to /etc/hosts without jailbreaking

For my development process I need to access a webserver which is behind a VPN and has no DNS entry.
What I was doing on 4.x was to edit /etc/hosts on the iPhone, and add it to the hosts file.
Now I'm on 5.0 beta, and don't want to jailbreak for now just for this purpose.
Is there a way I can add a line to /etc/hosts, just for development purposes (the final, distribution application does not need this hack), without jailbreaking? Can I use other means (declare a fake DNS entry by some unknown means at application launch, for example)?
EDIT: If you're willing to purchase a small license, I recommend using Charles Proxy, a web debugging proxy tool. It will also resolve domains from your local /etc/hosts, and it gives a lot of bonus features (i.e. inspect requests/responses and throttle network speeds). I only stumbled upon this tool from a WWDC video and I'm not affiliated with the product at all. I recommend reading Chris Ching's tutorial for iPhone and Charles Proxy to get you started.
To add to Ramon's answer, a way around it is to setup your local computer as a DNS server and have your iPhone point to your computer as a DNS server. This would also work for Android devices as well
The instructions are for Mac OSX via Homebrew:
brew install dnsmasq
dnsmasq is a lightweight dns server that will fallback to the original DNS server when it encounters an unknown domain
Add the line address=/.your.domain.com/10.0.0.5 to the file /usr/local/etc/dnsmasq.conf
The IP Address 10.0.0.5 is whatever the IP address assigned to your local computer by your router. You can find this via Network Utility (if you want to be fancy, you can assign a static IP to your local computer in your router)
sudo dnsmasq
This starts dnsmasq process, and it will listen on the DNS ports
Assign your local computer and your router as your DNS servers for your computer via System Preferences -> Network -> Advanced -> DNS Tab
You'll have two entries, one for your local computer (127.0.0.1) and one for your router. The reason why you include your router's IP is dnsmasq will fulfill unknown entries through the other known DNS servers. Without the router entry, you're whatever devices connected to you dnsmasq won't know how to connect to the internet.
Set your local computer's IP Address as your DNS Server your iPhone, go to Settings -> Wi-Fi -> Info icon for your connected router -> DNS
Some things to consider:
If you shut down your machine, your iPhone won't connect to the internet anymore. Make sure to reset your iPhone's DNS server to your router's IP
By default dnsmasq will look at your /etc/hosts, so if you had pointed your.domain.com to 127.0.0.1, your iPhone will resolve your.domain.com to 127.0.0.1, which means you won't connect to anything. To change this behaviour edit uncomment the #no-hosts line in the dnsmasq config.
Sources
http://www.davesouth.org/stories/how-to-set-up-dnsmasq-on-snow-leopard-for-local-wildcard-domains
Set up a real DNS entry, either by setting up a local DNS server on your wireless network, or by using a dynamic DNS service, or by adding an A record to a domain you control DNS for.
You can also set up dnsmasq (available from macports/brew), it acts as a DNS forwarder which allows you to set all kinds of alternative records.
You can then set up the DNS on the iphone/ipad to point to the box running DNSmasq, and any host on /etc/hosts on the dnsmasq box will be returned first. If not found, dnsmasq will send the query to the upstream DNS.
Also you can add SRV records to dnsmasq.conf:
srv-host=_sip._udp.devel.foo.com,devel.foo.com,5070
And many other niceties.

gethostbyname() only returns the address of local host on linux

I'm trying to portably (Windows & Linux) find all of the IP addresses of the local machine. The method I am using is to first call gethostname(), and then pass the result of that to gethostbyname(), which returns an array of ip addresses.
The problem is that on linux, the only address I get back is 127.0.0.1. This works on Windows, and I've seen a few people state that this will not work on Linux if your network was configured by DHCP (don't know if that's a true statement).
Is this not the correct way to do this on Linux?
It is not the correct way on unix/linux. The correct way involves ioctls to pull the necessary information.
struct ifreq ifc_buffer[MAX_NUM_IFREQ];
ioctl(s, SIOCGIFCONF, &ifc) # Interface list
num_ifreq = ifc.ifc_len / sizeof(struct ifreq);
for(cnt=0;cnt<num_ifreq;cnt++)
struct ifreq *ifr = &ifc.ifc_req[cnt]
ioctl(s, SIOCGIFADDR, ifr); # get ip address
There are also more modern methods involving:
if_nameindex()
Doing a SO search for if_nameindex and SIOCGIFCONF will yield a number of questions similar to this one.
This happens because on most distributions you have this in /etc/hosts:
127.0.0.1 localhost.localdomain localhost aiur
gethostbyname simply resolves the hostname (aiure in this example) to an address. If it finds it in /etc/hosts it's more than happy to give you that.
Back to the question. Unfortunately I don't believe you can get all the addresses of your machine in a portable way. You can do it in a Unix-portable way, like ifconfig does. Open a socket s and do an ioctl(..., SIOCGIFCONF, ...).
By the way, gethostbyname is obsolete if you believe kernel.org and deprecated if you believe MSDN.

Viewing a local web site on the LAN under a different hostname

In short I'm trying to browse a Mac's web site on the local wifi network under a .local hostname that is not the same as the machine's 'computer name' and think I'm missing a setup step.
I have a local install of nginx on my Macbook, with the proper /etc/hosts and nginx.conf entries to serve multiple sites, each with their own distinct local hostname. Assume the Macbook's network name is computername.local, and I have 2 sites running, one at http://computername.local and another at http://servicename.local. I can access each of these sites just fine from the local machine, but also want to be able to access http://servicename.local from an iPhone on the same WiFi network. I'm getting a timeout for that URL, but the other one works just fine.
I'm guessing something has to be done to allow servicename.local to be used on the local network, which I've left out. What is required to do that? Do I need to use Bonjour for that? Where would I add this new local hostname?
Another Mac on the same network can access this one under servicename.local just fine if I define the IP in its /etc/hosts file too, but I can't modify that file on the iPhone obviously. It's not jailbroken, and I'm not really interested in doing that just to get this working.
Not sure if it will help the OP, but another way of doing this - besides running a DNS server or jailbreaking the phone - is to run an HTTP proxy on the Mac, and configure the iPhone to use the proxy. Then the iPhone will pick up the Mac's local hosts file entries because it resolves DNS queries through the proxy. I've blogged about how to do this using the free Mac proxy "SquidMan" here: http://egalo.com/99j

What is "Attempt to lookup host entry for bad IP address" error?

Check this error and please help me.
2009-07-24 15:58:34.209 LBS[2636] Host 'staging.common.virtualearth.net' not found using 'gethostbyname()' - perhaps the hostname is wrong or networking is not set up on your machine
2009-07-24 15:58:34.209 LBS[2636] Attempt to lookup host entry for bad IP address (staging.common.virtualearth.net)
NOTE: you should run 'diskperf -y' to enable the disk statistics
I am running the objC codes for hitting a webservice on GNU for Windows.
Why is this error?
The first line says it can't find the IP address off the DNS servers.
The second is some kind of fallback with an incredibly cryptic error. Looks like it's trying reverse DNS using hostname as the IP address (hence bad IP address) or ARP resolution using hostname as IP address.
Basically fix the DNS to that host and both will be solved.