Thread: Data Alignment Issues...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Data Alignment Issues...

    I'm a little confused regarding data alignment. On x86, we typically take alignment for granted. However, I'm programming on a system that is very strict and will error out if I try to access unaligned data.

    Heres my problem:

    First, I'm going to show you some structs I have:
    Code:
        struct sniff_ethernet {
          u_char ether_dhost[6]; /* Destination host address */
          u_char ether_shost[6]; /* Source host address */
          u_short ether_type; /* IP? ARP? RARP? etc */
        };
        
        struct sniff_ip {
          u_char ip_vhl;  /* version << 4 | header length >> 2 */
          u_char ip_tos;  /* type of service */
          u_short ip_len;  /* total length */
          u_short ip_id;  /* identification */
          u_short ip_off;  /* fragment offset field */
          u_char ip_ttl;  /* time to live */
          u_char ip_p;  /* protocol */
          u_short ip_sum;  /* checksum */
          struct in_addr ip_src,ip_dst; /* source and dest address */
         };
    I'm dealing with pcap. Pcap will return a pointer to a data packet to me:

    Code:
        u_char *packet;
    Lets pretend the packet is a couple hundred bytes. What I typically do is cast that packet to several struct pointers, so I can access the data directly.

    Code:
      struct sniff_ethernet *seth = (struct sniff_ethernet *) packet;
        struct sniff_ip *sip = (struct sniff_ip *) (packet + 14); // 14 is the size of an ethernet header
    Ok. So everything looks great right? On x86, everything appears to work right. On any other archetecture that has strict alignment, I have issues when accessing some values and it will typically result in a sigbus. For example:

    Code:
        sip->ip_len = 0x32AA;
    or

    Code:
    u_short val = sip->ip_len;
    results in an error. I'm guessing its because it's misaligned in memory from the cast. Whats typically the best way to handle this when doing these kind of casts?
    Last edited by aaba1; 10-02-2010 at 09:17 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Pull packet apart 1 byte at a time, and assign it to each member of your struct.

    Here´s another example.
    memcpy question

    Or you could tell us which OS/Compiler you´re using.
    Some compilers have flags to relax data access conditions, by auto generating the code to fix mis-aligned data.
    Similarly, you might be able to do things with the equivalent of "pragma pack".

    These are techno hacks though. The only robust way is to copy it a byte at a time from the raw buffer to your actual struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  3. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 07:08 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM