Pages

Wednesday, April 28, 2010

DNS Tunnelling on Android

I have a Motorola Milestone, its somewhat awesome, but what would make it awesome-er is ... tunnelling TCP/IP over DNS! Any device that can support covert, high latency, text based network protocols should. And rooting the phone isn't required.

It turns out it is suprisingly easy to get simple C code running on an Android phone, if you don't mind statically compiling and don't have many dependencies. The results are sort of bloated though.

You can download a precompiled sods client (sdt) for Android (the binary will also work on Nokia tablets running Maemo) from here. I compiled sdt using scratchbox. You'll need a sods server and a domain set up, of course (here are instructions).

Copy sdt to your phone. I moved it to the sd card.

If you don't have ConnectBot installed, get it from the Android market now. Start up ConnectBot and create a new local host. This will start a shell on the phone.

Change to the ConnectBot data directory; we'll use it to hold the sods client.
cd /data/data/org.connectbot
Create a new directory or use the files directory (I have no idea what will happen to these directories when ConnectBot is upgraded).
mkdir bin
cd bin
Copy sdt and change the permissions:
cp /sdcard/tmp/sdt .
chmod 744 sdt
./sdt -h
Now create a new local host to run sdt. Do a long press on the new entry and select "Edit Host".
Choose the "Post-login automation" item. This will let us run commands when the shell is started.
The flags to sdt can be manually specified or wrapped in a shell script.
Here is an example shell script (called sd):
#!/system/bin/sh

PORT=22220

S=${1:-0}
PORT=$((PORT+$S))

T=${2:-TXT}

NS1=`getprop net.dns1`
NS=${3:-$NS1}
./sdt -t $T -r $NS -p $PORT -s $S -vvvv x.a.example.com x.b.example.com x.n.example.com
Create a new ssh host, connecting to localhost and whatever port sdt has been configured to listen on (using the -p flag).

Test it out. In the ConnectBot host list, select the sdt host.
Hit the back button, then select the ssh host.
If everything works out, a glorious, if slow, ssh session over DNS should be the reward.
After login, run GNU screen (in case the connection drops). I usually run console apps, but ConnectBot supports port forwarding for those times you need to run send data from other apps over the tunnel (forwarding through a proxy like ziproxy works well).
centerim: IRC over SSH over TCP/IP over DNS
Web browsing over DNS on a phone using w3m

Update: 2011-03-13

I've switched phones to an HTC Desire Z (running Android 2.2) and one of my first tasks was to get sods running on it.
  1. I was getting permission errors copying from the sdcard to the connectbot directory so I resorted to using "cat":
    
    cd /data/data/org.connectbot/bin
    cat /sdcard/tmp/sdt > sdt
    chmod 755 sdt
    ./sdt -h
    
  2. If you are having problems connecting to the sods server, make sure another sdt process is not running. To do this, run "ps" and "kill" from the ConnectBot shell or kill the ConnectBot application.

Monday, April 12, 2010

Accessing an Arduino from Erlang

(2013-01-08: There is a native Erlang interface to Unix serial ports here: srly. See the readme for the API. It should work on Mac OS X, BSD and Linux.
And, if you want to load code to the Arduino directly from Erlang, try this: stk500)

First we'll need to upload a sketch to the Arduino. There are many examples floating around. Here is a simple one to control LED's. Yes, not exactly novel.

You will need to modify the definitions of FPIN and LPIN to match the first and last digital pins the LED's are plugged into. Compile and upload the sketch.

The lights are set as a bitmask. For example, to activate the first LED, send the integer 1; for the third LED, send 4; and for both the first and third LED, send 5. If you defined the DEBUG macro, you can test from the serial monitor by sending the ASCII representation of the numbers.

Now download and compile the erlang-serial port driver. Start up Erlang:
1> Pid = serial:start([{open, "/dev/ttyUSB0"}, {speed, 9600}]).
<0.47.0>
2> Pid ! {send, 1 bsl 2}. % LED at position 3

Doing Something Useful


Switching LED's on and off programmatically is hours of fun. But, like many others have discovered before, hook it up to a monitoring system and it becomes sort of useful.

I have a monitoring service running Nagios. Yeah, I think Nagios sucks too. The Erlang code does a request for the tactical overview page, parses out the statistics and sets the alert status accordingly.

Monitoring Ambient Light


Here is another simple project: creating a graph of ambient light levels using an LDR and the really quite awesome eplot library.

Connect the LDR to an analog pin and upload a sketch: The Erlang code has 2 components: a process that periodically reads and stores the sensor data and a web server that displays a graph in PNG format.
light:start().
sensor:start().
Reading the sensor:

Displaying the graph:

After the web server is running, the web page can be found by going to:
http://localhost:8889/web/sensor:graph

(I think the spikiness was caused by a blinking LED somewhere around the LDR).