Get in touch:
01524 851 877
07718 896 553

Structure Packing in GCC

Posted on Mar 26 2008

I have recently been doing a lot of coding using the NEPL NEMO Implementation as a part of a project I am working on. This has proved to be very interesting work and has brought about lots of interesting problems.

One of the most recent problems I faced involved a packet I was sending over the network using a header I had constructed using a struct as follows.

struct Nino
{
uint8_t type;
uint8_t length;
uint8_t prefix_len;
uint8_t res_l_4;
uint32_t nino_lifetime;
uint32_t reserved2;
uint8_t nino_depth;
uint8_t reserved3;
uint16_t nino_seq;
struct in6_addr _prefix_in6_addr;
};

This was the exact packet header format that I required but when I received a packet and checked it using a version of Wireshark I had modified to recognise the header format, I was getting what seemed like an erroneous 2 Bytes of data (set to 0’s) at the beginning of the in6_addr. This puzzled me for a while until I started to think about how GCC would lay out the fields in the struct. Of course GCC tries to optimise the struct for passing across the hardware and pads it if it feels it is necessary.

For my purposes this ruins the whole layout of the packet header for processing so I had a look around and found that GCC has an attribute named __packed__ that will force it not to pad the struct out. Or in the terms of the GCC Manual :

  • “a variable or structure field should have the smallest possible alignment–one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.”

This was exactly what I wanted. So by changing the struct definition from that above, to :

struct Nino
{
uint8_t type;
uint8_t length;
uint8_t prefix_len;
uint8_t res_l_4;
uint32_t nino_lifetime;
uint32_t reserved2;
uint8_t nino_depth;
uint8_t reserved3;
uint16_t nino_seq;
struct in6_addr _prefix_in6_addr;
}__attribute__((__packed__));

My code behaved exactly as I required. Problem solved. This is a handy little function of GCC especially when dealing with network packets to make sure you know exactly how the compiler is behaving and hence avoiding any unwanted surprised.

A slightly more in depth look at the packed attribute can be found here.

Ubuntu 8.04 Beta Released

Posted on Mar 22 2008

Ubuntu ImageThe team over at Ubuntu have released the latest beta of Ubuntu 8.04 (Hardy Heron). There are lots of upgrades to the existing software packages including the following :

  • Xorg 7.3
  • Linux kernel 2.6.24
  • GNOME 2.22
  • PolicyKit
  • PulseAudio
  • Firefox 3 Beta 4
  • Transmission BitTorrent Client
  • Wubi


This is the best release yet for ‘converting’ windows users to the world of open source software using Wubi to enable it to be easily installed and used as a dual boot system without any complicated partitioning needed. This is a great way to try Linux without having to compromise the integrity of your windows partition in any way.

I will be upgrading to 8.04 on all of my machines as soon as it goes to stable. I have been totally converted to Ubuntu for my desktop OS over the past year. As an avid Linux user for the past 6 or 7 years using a myriad of different distributions. College Linux whilst I was on the development team there before it was dissolved, Debian for years, a bit of Gentoo mixed in as well but none have come close to Ubuntu for ease of usability and I will continue using it as my primary desktop OS for the foreseeable future.

Convert MySql timestamp field to Unix Time Stamp

Posted on Mar 20 2008

As anyone who has used MySql and PHP in the past to deal with time and date will probably know MySql timestamp fields are not compatible in anyway with PHP’s date() function. Because of this you need to do conversions in order to use the information from the MySql database. The following function will convert a value from a database into a unix timestamp format for easier manipulation within your PHP code. I have found it quite useful to have around so am putting it up here for others to take advantage of.

/*
* Convert MySql timestamp to unix timestamp
*/
function ts2unix($ts)
{
$year = substr($ts,0,4);
$month = substr($ts,4,2);
$day = substr($ts,6,2);
$hour = substr($ts,8,2);
$minute = substr($ts,10,2);
$second = substr($ts,12,2);
$uts = mktime($hour,$minute,$second,$month,$day,$year);
return ($uts);
}

Categories and RSS Feed

Posted on Mar 18 2008

Another quick update on the state of this blog!

I have added a category system to the posts. All the posts are now sorted by category as well as the original tagging system. This enables quicker referencing when trying to look back through the old posts. Posts can be viewed by category by going here.

There is also now an RSS feed for new posts. This can be viewed by going here. Or paste the link into your favourite RSS reader.

That’s all for now.

RSS Script

Posted on Mar 18 2008

I decided this morning that I wanted to add an RSS feed to my blog for the hell of it. A quick trip over to google and I stumbled upon a nice little PHP class to do it all for me called FeedCreator.

This class enables many different types of feeds to be created and is extremely simple to use so thanks to Kai Blankenhorn for this I’m sure I will be using it again in the future.

Blog Tags

Posted on Mar 14 2008

Just a quick entry to say that the tag system should now be functional. It is very basic but as I said in my previous posting hopefully I will get time to add more functionality eventually.

New Blog

Posted on Mar 14 2008

Hi to anyone that happens to be reading this. This is my new blog to document my random ramblings and thoughts. This will mostly contain a lot of random thoughts of mine regarding computers, programming, scripting, the world in general etc etc.

Of course this blog was also an exercise to see if I could knock up a basic blogging PHP script in a few hours. No doubt there will be a few bugs but I will hopefully iron those out relatively quickly. Time will tell on that front I suppose. If you do happen to find any bugs feel free to use the contact link to email these through to me so I can sort them out.

The blog will be undergoing many updates in the near future. At the moment it is an extrememly simple script (and the tags don’t even do anything yet). Soon it will be a lot more functional with archiving etc to make it easier to navigate. Hopefully some of my ramblings will come in useful enough to some people to make this worthwhile :)

Matt