Monday, October 12, 2009

Mac OS X 10.6 Snow Leopard, iPhone USB Tethering and Bad Carriers

Since iPhone OS 3.0 it is possible to [officially] use your 3G internet connection right into your laptop. That works very well for the all new Snow Leopard, with one single drawback: it is not possible to set name resolution servers when tethering via USB.

You might think it is a small issue but I would say you can be wrong, sorry. Relying on carrier's default DNS can be very annoying when they are faulty. And thats exactly what happens with me all the time. My carrier DNS frequently stops resolving top hit domains such as google.com and many more.

Of course I can switch to bluetooth, in which case Snow Leopard "weirdly" allows you to set your own DNS. But keep in mind that sometimes the phone might have no battery for that. Again, I could just let the phone plugged in a power outlet and bring bluetooth up, but that would have any fun ! I like to have choices, and not being able to tether via USB is something that annoys me and challenged me for quite some time.

No more bla bla bla, I could fix my problem when I found about something called configd and scutil. You can find a bit of information about them here and here. Going straight to the point, I could manage to overwrite my default DNS (with the address for opendns) using scutil and a little of bash scripting. That is what I came up with:


#!/bin/sh
#
# Replaces current DNS with the one you want in Mac OS X
#
# Author: Igor Feghali 
# 

#DNS="208.67.222.222 208.67.220.220"
DNS="8.8.8.8 8.8.4.4"
STATE=`echo "list State:/Network/Service/[^/]+/DNS" | scutil | awk '{print $4}'`

(echo "d.init"; echo "d.add ServerAddresses * $DNS"; echo "set $STATE";) | sudo scutil


You can set the contents of the variable DNS in the above script to whichever you want. But don't get too excited. This solution will work only for a few moments. I guess that's because configd rebuilds the network configuration periodically.

A short visit to configd man page led me to the right place: /Library/Preferences/SystemConfiguration/preferences.plist stores the preferences for all the network interfaces. The first thing to do is to find out the HEX code of which one we want to modify.

That would be BB6EBCF-861B-4A73-94FE-E08F7EEEE5EE for me. Now open preferences.plist (in the above path) and look for the key string somewhere in the file between a <key> and a </key>. Now replace:


<key>EBB6EBCF-861B-4A73-94FE-E08F7EEEE5EE</key>
<dict>
<key>AppleTalk</key>
<dict/>
<key>DNS</key>
<dict/>

with:

<key>EBB6EBCF-861B-4A73-94FE-E08F7EEEE5EE</key>
<dict>
<key>AppleTalk</key>
<dict/>
<key>DNS</key>
<dict>
<key>ServerAddresses</key>
<array>
<string>208.67.222.222</string>
<string>208.67.220.220</string>
</array>
</dict>


Just be sure to edit the file as root ($sudo vim for instance), save and reboot. That should do the trick.

I couldn't manage to reload the system configuration without a reboot, just let me know if you do.

No comments :