Tuesday, March 01, 2011

Host Specific Routing Via the iPhone on OSX

If you are working as a consultant, it is sometimes not easy to get proper network access when you are using your customer's network. The easiest way to solve this problem is to connect via your mobile phone. But, the 3G network is not always the fastest and it would be nice to use the normal network for certain tasks and the 3G network for others.

The problem in my case was that I wanted to connect to Github but, SSH traffic was blocked on the network where I was working. So what to do? I quick mail to the Jayway tech mailing list, lets me know that in Linux there is a route command, that can solve this problem. (Un)Fortunately I am not using Linux on my development machine, but it turns out there is a route(8) command in BSD too, with slightly different syntax.

Find the IP-address of the host I wish to connect to

# Find the IP-address of the host I wish to connect to 
$ nslookup github.com
Server:  195.58.103.21 
Address: 195.58.103.21#53 
 
Non-authoritative answer: 
Name: github.com
Address: 207.97.227.239 
 

Find out the gateway of the interface of the cell phone

# Find out the gateway of the interface of the cell phone 
$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384 
 inet 127.0.0.1 netmask 0xff000000 
 inet6 ::1 prefixlen 128 
 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
... 
vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 
 ether 00:50:56:c0:00:08 
 inet 192.168.105.1 netmask 0xffffff00 broadcast 192.168.105.255 
en3: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 
 ether 06:1e:64:00:34:1a 
 inet6 fe80::41e:64ff:fe00:341a%en3 prefixlen 64 scopeid 0xa 
 inet 172.20.10.3 netmask 0xfffffff0 broadcast 172.20.10.15 
 media: 10baseT/UTP
 status: active

In my case the interface I want to use is the last one, en3 with inet 172.20.10.3 but if you are unsure, you can open the network preference pane and look it up there.

Now I need to find out what the gateway for this interface is. I can do this with netstat -r.

# Show the routing table 
$ netstat -r
Routing tables
 
Internet: 
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            10.67.10.254       UGSc           10        0     en1
default            172.20.10.1        UGScI           0        0     en3
10.67.10/24        link#5             UCS             3        0     en1 
10.67.10.88        localhost          UHS             1      811     lo0
10.67.10.254       0:14:4f:69:da:13   UHLWI          10       41     en1    661 
10.67.10.255       link#5             UHLWbI          1       78     en1 
127                localhost          UCS             0        0     lo0
... 
 
Internet6: 
Destination        Gateway            Flags         Netif Expire
localhost          localhost          UH              lo0
fe80::%lo0         localhost          Uc              lo0
localhost          link#1             UHL             lo0 
... 
 

Close to the top we find the entry for our interface en3.

default            172.20.10.1        UGScI           0        0     en3

Now we have the ip to the gateway that we wanted. So now the last step is to make sure that access to github.com is routed via this gateway.

#route command -host destination gateway 
$ sudo route add -host 207.97.227.239 172.20.10.1 
add host 207.97.227.239: gateway 172.20.10.1 
 

That's it, I'm done. To make sure the routing is changed I check the routing table with netstat -r again, to make sure the new entry is in it.

$ netstat -r
Routing tables
 
Internet: 
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            10.67.10.254       UGSc           30        0     en1
default            172.20.10.1        UGScI           0        0     en3
10.67.10/24        link#5             UCS             4        0     en1 
10.67.10.88        localhost          UHS             0      973     lo0
10.67.10.254       0:14:4f:69:da:13   UHLWI          27       11     en1    708 
10.67.10.255       ff:ff:ff:ff:ff:ff  UHLWbI          1       14     en1
127                localhost          UCS             0        0     lo0
... 
github.com         172.20.10.1        UGHS            0       43     en3
 
Internet6: 
Destination        Gateway            Flags         Netif Expire
localhost          localhost          UH              lo0
fe80::%lo0         localhost          Uc              lo0
localhost          link#1             UHL             lo0 
fe80::%en0         link#4             UC              en0 
 

Sure enough, there it is, github.com. Finally I can now git pull and everything works :).

If I want to remove the entry, when I'm working elsewhere, I do that with

 
$ sudo route delete -host 207.97.227.239 172.20.10.1 
 
 
  
 

No comments: