Mike Chirico on 1 Mar 2009 13:49:12 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] "Endian" Programming Question


On Sun, Mar 01, 2009 at 03:03:13PM -0500, Casey Bralla wrote:
> I'm confused by the notion of "Big Endian" and "Little Endian" formats 
> (actually, I'm confused by a lot of things, but this is the most recent 
> manifestation <grin>)  I was hoping someone on this list could help me better 
> understand this.
> 
> I'm working on a Python application that will read info from some /proc files 

Casey:

I don't know how it works for that specific module. However, the
following simpler example may help - taking and converting output from
/proc/net/tcp.

  $ cat /proc/net/tcp


When executing the command above, IP addresses will be represented as
little-endian four-byte hexadecimal numbers.  The following steps
convert this number into something readable.

Step 1.

     Take an address from the above cat command.

         0100007F:1FB6

Step 2.

     Reverse the ordering of the bytes in the IP address part

        7F 00 00 01 : 0017


Step 3.

      Convert hex-to dec

        127 0 0 1 : 23



The following is an awk program that will do the steps
above. 

Put the following in file awktcp.sc1

NR > 1 {
t="echo \"ibase=16;" substr($2,7,2)"\" |bc";  t | getline a; close(t);
t="echo \"ibase=16;" substr($2,5,2)"\" |bc"; t  | getline b; close(t);
t="echo \"ibase=16;" substr($2,3,2)"\" |bc"; t  | getline c; close(t);
t="echo \"ibase=16;" substr($2,1,2)"\" |bc"; t  | getline d; close(t);
t="echo \"ibase=16;" substr($2,10,4)"\" |bc"; t  | getline e; close(t);
 

t="echo \"ibase=16;" substr($3,7,2)"\" |bc"; t  | getline f; close(t);
t="echo \"ibase=16;" substr($3,5,2)"\" |bc"; t  | getline g; close(t);
t="echo \"ibase=16;" substr($3,3,2)"\" |bc"; t  | getline h; close(t);
t="echo \"ibase=16;" substr($3,1,2)"\" |bc"; t  | getline i; close(t);
t="echo \"ibase=16;" substr($3,10,4)"\" |bc"; t  | getline j; close(t);
if ( a > 0)
printf("%d.%d.%d.%d: %d \t %d.%d.%d.%d: %d\n",a,b,c,d,e,f,g,h,i,j)  }



Now run the following command.

     $ cat /proc/net/tcp |awk -f awktcp.sc1


On my system, I get the following output, which is similar to the
netstat -nt command.


127.0.0.1: 4690          0.0.0.0: 0
127.0.0.1: 631   0.0.0.0: 0
192.168.1.12: 22         192.168.1.1: 64151
192.168.1.106: 36851     192.168.1.81: 22
192.168.1.12: 16001      192.168.1.12: 34138
192.168.1.12: 16001      192.168.1.12: 34139
192.168.1.12: 34139      192.168.1.12: 16001
192.168.1.12: 22         192.168.1.1: 50273
192.168.1.106: 50843     192.168.1.81: 22
127.0.0.1: 57582         127.0.0.1: 4690
192.168.1.12: 22         192.168.1.81: 53659
127.0.0.1: 4690          127.0.0.1: 57582
192.168.1.12: 34138      192.168.1.12: 16001
192.168.1.105: 37730     74.125.47.121: 80
192.168.1.12: 22         192.168.1.1: 64307


Hope this helps.

Regards,

Mike Chirico
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug