views:

460

answers:

2

I need to obtain the IP address of the default gateway in an iPhone application. I cannot find the proper API. Anyone know if iPhone exposes this information?

A: 

If I'm not mistaken, the gateway IP is always whatever your device IP address is, with the last octet set to "1", isn't it? If that's what you're looking for, you should be able to find the hints you need from the localhostAddresses.m file which is part of the samples in CocoaHTTPServer sources. I use the project to add an internal webserver within my app and I get the devices IP from that source file's methods.

Dr. Touch talks about it at this link.

wkw
This is incorrect. While it is common convention, placing the router at x.y.z.1 it is not required or guaranteed by the spec. I have seen many live networks where the router was x.y.z.254, and I have seen live networks where the the default gateway is NOT in the device's subnet (most OSes will attempt to send to the gateway IP address unconditionally if the IP they sent to is not routable, even if the gateway is unroutable... some ISPs depend on this behavior). Also, things tend to be configured differently on Class A and B subnets.
Louis Gerbarg
Thanks for enlightening me. I look forward (for curiosity's sake) to hearing what you discover.
wkw
A: 

I was successful obtaining the address from SSDP protocol by sending a UDP packet out looking for service type "urn:schemas-upnp-org:device:InternetGatewayDevice:1" and noting the first device (if any) that replies (ignoring the payload, since I only want the IP address of the gateway).

This works for my application, but requires that the router implement SSDP, which is not perfect, although works in my case.

Since this is a special-purpose iPhone App (in-house, only), I am going to go with this. I won't mark this as "the answer" because it is not a general purpose solution. If I come back to this and look for a general purpose solution (such as using ICMP), or figuring out how to use the iPhone SDK configuration APIs to query for this information, I will post here.

Bill
Hi Bill, any pointers on sending and and receiving UDP packets? I'm getting lost in the docs at the moment and some clarity would really help. I am trying to use the iPhone to discover SSDP devices and it looks like you've done just that. Cheers Dave.
Magic Bullet Dave