Monday, October 28, 2013

Serial Port Tricks


The Netcat program can shovel data bidirectionally to/from a serial port and over a network, which is very handy indeed.

Set the serial port in raw mode and configure it:
# stty -F /dev/ttyUSB0 raw
# stty -F /dev/ttyUSB1 raw
# stty -F /dev/ttyUSB0 19200
# stty -F /dev/ttyUSB1 19200

Set up a netcat listener that will send data to/from the one serial device:
# nc -l 1234 < /dev/ttyUSB1 >/dev/ttyUSB1

Set up a netcat client that will send data to/from the other serial device:
# nc listeneripaddress 1234 < /dev/ttyUSB0 >/dev/ttyUSB0


For debugging and scripting, you can also use ordinary cat, echo, head or even data definition to access the serial ports: 

Send data one way only using the common kitty:
# cat /dev/ttyUSB0 > /dev/ttyUSB1

Send a message out a port using echo:
# echo Hello > /dev/ttyUSB0

Send data denoted as hexadecimal values and suppress the LF at the end of the line:
# echo -en "\x12\x23\x45" > /dev/ttyUSB0

Read one character from a serial port using head:
# $CHAR = head -c 1 /dev/ttyUSB0
# echo $CHAR

Unbuffered Operation

The buffering is done by the tty layer and not by nc.  Use stty together with netcat to reduce the buffer size to zero:
stty -icanon && nc ...

The above will set the buffer size to zero while netcat is running.  it needs to be one command, otherwise the shell may set it back to normal again unexpectedly.

dBm to Watt

I tend to forget some of the values and is too lazy to calculate it.

dBm to Watt Conversion Table
dBm Watts dBm Watts dBm Watts
0 1.0 mW 16 40 mW 32 1.6 W
1 1.3 mW 17 50 mW 33 2.0 W
2 1.6 mW 18 63 mW 34 2.5 W
3 2.0 mW 19 79 mW 35 3.2 W
4 2.5 mW 20 100 mW 36 4.0 W
5 3.2 mW 21 126 mW 37 5.0 W
6 4 mW 22 158 mW 38 6.3 W
7 5 mW 23 200 mW 39 8.0 W
8 6 mW 24 250 mW 40 10 W
9 8 mW 25 316 mW 41 13 W
10 10 mW 26 398 mW 42 16 W
11 13 mW 27 500 mW 43 20 W
12 16 mW 28 630 mW 44 25 W
13 20 mW 29 800 mW 45 32 W
14 25 mW 30 1.0 W 46 40 W
15 32 mW 31 1.3 W 47 50 W

Wednesday, October 16, 2013

The Philosopher's Cross

While touring through Europe, I could not fail to notice all the various styles of crosses displayed everywhere on countless steeples and memorials.  Most are obviously decorous, but one or two depictions struck me as being functional - with an engraved scale - but for what purpose?

Driving around in the green countryside of Bavaria, I thought of the various English proverbs:
Crossed, Double Crossed, Nailing a Philosopher to His Own Cross.

That last one made me think - a Philosopher's Cross - hmm...

Until medieval times, any learned man was called a philosopher and good philosophers learned pretty much everything there was to be learned.  The information explosion and the necessary specialization is a recent thing.  Ancient philosophers learned about astronomy and navigation, in addition to classic literature, logic and mathematics. 

Navigational Aids

Knowledge of navigation was essential in ancient times not only for navigating at sea where the only features are the night sky, but also on land on the plains of Europe - especially in winter, where a miscalculation could mean a painful freezing death if you are trudging along and cannot find your destination village.

There weren't any confusing brown tourist signs in those days and inuksuks are a Canadian invention!

In the village of Lab in Slovakia, I saw a depiction of a philosopher carrying a light weight, but rather large (about a 1 meter long staff) cross and careful inspection of the painting showed some marks on the staff.  At last, a practical Philosopher's Cross.

The Cross Staff and Lode Stone

The cross staff could have one, two or three cross bars (though only one would be used at a time).  The staff was placed below the eye socket and staring directly into the sun, one could then measure the sun angle to the horizon, by moving the bar along the staff, until it touches the sun and the horizon and then reading off the value inscribed on the staff.  Better staffs had two or three cross bars, set at angles, to use multiple engraved scales on the staff.  Only one bar would be used at a time, but many churches show these crosses with all the bars installed - especially in Central and Eastern Europe.  Clearly, nobody now knows that it was a navigation instrument!

At night, the same method could be used to measure the angle of other well known stars, such as polaris.  The angle would give the philosopher his latitude and a lode stone floating on a piece of bark in a ceramic bowl of water, would act as a compass and the combination, would show him which way to go.  The whole process, would of course seem mystical and magical to commoners and a philosopher that could travel far and wide and navigate using the sun and other stars, was held in high regard.
O'l Ptolomy was even depicted holding a cross staff, some 1000 odd years before it was invented by Rabbi Levi ben Gershon (1288-1344) in France.

The cross staff, was in recent times replaced first by the back staff, a staff fitted with a little mirror and white board, which allowed the user to look away from the painful sun and later as technology kept improving by a more precise metal quadrant, followed by a precision metal sextant and nowadays, a GPS receiver in a smart phone.
So, how do you navigate with a cross staff / sextant and a compass?  It is actually not hard at all.  If the sea is unobstructed between you and the harbour you are going to, simply go north (or south) till you get to the harbour latitude (the sun angle measured by the cross staff, and the harbour position read from a navigation almanac), then head east (or west) according to your compass, until you see the harbour.  In practice, you would go zig-zag, with many sightings.  The navigation tables of the 17th and 18th centuries made it easy. Accurate clocks only came later - those made it possible to estimate your longitude.
Arabic sailors used a simplified pocket version: A piece of wood with a string through the middle.  They would bite the end of the string between their teeth and then measure the sun angle against specific knots in the string.  A knot indicated the angle of a port and when sailing regularly between the same two or three ports, they simply adjusted the knot each time they got there, or updated it based on a friend's who was there recently.

The Philosopher's Stone

I also saw many a staff with a spiral and a jewel in the middle, as a prop, used with many statues and even the modern pope and his bishops like to carry one (though I doubt that they have the foggiest idea how to actually use it).  This jewel on a stick is the Philosopher's Stone, or a sun stone.

Two small statues with these spiral staffs and also one little painting with a functional cross staff, are in the twin tower church in Munchen.  That church is a particularly macabre place with the most horrific religious artwork on display - it looks like a place where Stephen King would go, to get inspiration for his horror novels.

A sun stone, was a rare piece of feldspar, with parallel thin crystals, causing it to polarize light (just like a pair of Polaroid sunglasses).  When traveling in the extreme north, a polarized filter can indicate the position of the sun below the horizon.  Inscriptions on the spiral, helped the philosopher measure the sun angle by looking at streaks of polarized light high in the sky while slowly revolving the stone.  The philosopher could then navigate accurately using the sun, even when the sun was invisible.

My father was an amateur geologist and collected stones. He had one of these and told me that it was used in medieval times for navigation, but he did not know how it was used exactly.  I played with it, but never thought of looking at the sky through it.

If you live in the far north of Canada or Norway, give it a try with your polarized sunglasses.  The streaks in the sky will be something else to look at besides the Aurora.

Apparently, a sun stone was also useful to accurately find the sun on a cloudy day, so it is also a bad weather navigation tool, not just for use in the far north and probably prevented many a Viking ship wreck.

Yesterday's Toys 

Statues of learned men included the props of their trade: A book (showing that he read classical literature), a cross staff (showing that he was an astronomer and navigator) and later, a telescope (like the statue of Lord Nelson).  I haven't seen a statue holding a GPS receiver yet, but maybe one day...

Nailing a Philosopher to His Own Cross

Nailing a Philosopher to His Own Cross, is a proverb indicating that the philosopher was refuted, but one cannot possibly nail a person to a cross staff - the thing is too small, light and fragile.  It is a metaphor only.

So, coming back to that well known religious icon nailed to his own cross, the blood, guts and gore.  The simple fact is that the Romans did not nail people to crosses - it is too complicated to make one that would be strong enough.  They impaled people on a stick, same as Vlad III Dracul did with many thousands of Turkish invaders in Romania in 1462.

The mythical icon, was only metaphorically nailed to his cross, but to make a practical statue, the light weight cross staff had to be made much larger and totally impractical and now we have tens of thousands of people wearing macabre navigation icons around their necks, with hundreds of gothic churches displaying gory icons on decorous cross staffs with one, two or three cross bars all uselessly mounted at the same time, while precious few actually know where it all came from.
Nailing a philosopher to his GPS would maybe put an interesting new spin on things.

(The little pictures are borrowed from all over the web - thanks to whoever took them - maybe one day, I'll upload my own).

Wednesday, October 9, 2013

Network Stress Testing

Stressing an Embedded Network Device

Sometimes a new networked device behaves funny and you need to stress it to try and figure out where the bug is - firmware, hardware, software?

If you feel like you need a big hammer to give the infernal device a good whack, then look no further - get nping, made by the friendly folks over at the nmap project.

Get nping

# yum install nmap

While you are at it, you can also try another hammer called hping:
# yum install hping3

Hammer it

Now you can try hammering the device on its control port with good packets:
$ nping -c 500000 --rate 10000 --tcp --data-length 1500 -g 2000 -p 2000
and with bad packets:
$ nping -badsum-ip -c 500000 --rate 10000 --tcp --data-length 1500 -g 2000 -p 2000

And hopefully, that will cause the device to splutter and squeal and show you where the problem lies.  Even if it doesn't show you anything, hammering it will likely release some stress and make you feel better.

La voila!

Packet Sniffing

A Transparent Bridge

It is sometimes necessary to capture packets on a network in order to see why a certain host or device is misbehaving.

Depending on the setup, you could use Ettercap and ARP table spoofing to reroute interesting traffic to your test machine as described in another post, but usually it is best to physically insert the test machine between the unit under test and the ethernet switch as a transparent bridge and then capture or save the data using tcpdump or wireshark.

If your test machine is a laptop computer, then it likely has only one ethernet port.  You can add a second one using either a cardbus or USB ethernet adaptor.

Install the ethernet bridge utilities and create a transparent bridge br0, then assign the two ethernet ports to it (find their names with ifconfig).

Building the Bridge

Here is a bridge script that I used recently:

#! /bin/bash
echo Configure a transparent bridge for Wireshark or tcpdump.

killall NetworkManager

# Install ebtables and bridge-utils
# Ethernet ports: em1 and eth2

echo Create a transparent bridge
brctl addbr br0
brctl stp br0 off
brctl addif br0 em1
brctl addif br0 eth2

echo Enable IP4 forwarding
/sbin/ifconfig em1 promisc up
/sbin/ifconfig eth2 promisc up
echo "1" > /proc/sys/net/ipv4/ip_forward

echo The bridge IP address is
ifconfig br0 netmask up

echo Open ebtables to allow everything, INPUT, OUTPUT and FORWARD  
iptables -F
iptables -P INPUT ACCEPT
ebtables -F
ebtables -P INPUT ACCEPT

echo Configuration:
iptables -L
ebtables -L

Dumping the Packets

Now you can run tcpdump on the br0 device and view or save the traffic for later analysis.  Here are a few examples:

# tcpdump -i br0
# tcpdump -nlA -i br0
# tcpdump -nlX -s 256 host 192.168.x.y and port 1234 -i br0

La voila!