Monday, October 28, 2013

Serial Port Tricks

Bidirectional

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

Unidirectional

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 decourous, but one or two depictions struck me as being functional - 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 the destination village.


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


Only in the village of Lab in Slovakia, did I see 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 and then reading off the value inscribed on the staff.

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.

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.

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 travelling 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 a amateur geologist and collected stones. He had one of these and told me that it was used in medieval times for something, but he did not know what for.  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 sunstone 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 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 simply impaled people on a stick same as Vlad The Impaler did with many thousands of Turkish invaders in Romania.

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 icons around their necks, with hundreds of gothic churches displaying gory icons on decourous 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.

(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 192.168.111.4
 
and with bad packets:
$ nping -badsum-ip -c 500000 --rate 10000 --tcp --data-length 1500 -g 2000 -p 2000 192.168.111.4

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 0.0.0.0 promisc up
/sbin/ifconfig eth2 0.0.0.0 promisc up
echo "1" > /proc/sys/net/ipv4/ip_forward

echo The bridge IP address is 192.168.111.1
ifconfig br0 192.168.111.1 netmask 255.255.255.0 up

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

echo Configuration:
iptables -L
ebtables -L
ifconfig


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!