home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.protocols.tcp-ip      TCP and IP network protocols.      14,669 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 14,378 of 14,669   
   Jorgen Grahn to All   
   Re: IPv4 header and memory alignment...   
   15 Mar 19 14:04:02   
   
   From: grahn+nntp@snipabacken.se   
      
   On Fri, 2019-03-15, TDH1978 wrote:   
   > On 2019-03-12 09:20:52 +0000, Jorgen Grahn said:   
   >   
   >> In the Linux kernel, they make sure it's safe by aligning that memory,   
   >> and knowing things about how the compiler treats structs, more than   
   >> what the C language guarantees.   
   >>   
   >> In my own code, I prefer not to rely on such things.  It gets messy,   
   >> and you still need to take care of endianness. I write my own accessor   
   >> functions instead:   
   >>   
   >>    /* assuming buf points to a full IPv4 header   
   >>     */   
   >>    struct in_addr get_saddr(const unsigned char* buf);   
   >>   
   >> As a bonus, you get a better type than uint32_t.   
   >   
   > Thank you for your reply.  I agree your method is safer but I assume   
   > that, by calling your accessor functions, you will incur a slight   
   > run-time overhead.  If you do this for, say, a billion incoming   
   > packets, this overhead may become significant.   
      
   Probably only if you're writing an IP stack.  If you act on the   
   information you extract I think that processing cost will quickly   
   overshadow this inefficiency in the parsing.   
      
   Profiling might be a good idea.   
      
   /Jorgen   
      
   --   
     // Jorgen Grahn    O  o   .   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca