11.6 A Little More Code

There is another small detail you may have noticed while studying the captured SMB packets or perhaps you remember this from one of the Alert boxes in the NBT section: SMBs are written using little-endian byte order. If your target platform is big-endian, or if you want your code to be portable to big-endian systems, you will need to be able to handle the conversion between host and SMB byte order.

The htonl () , htons() , ntohl () , and ntohs() functions won't help us here. They convert between host and network byte order. We need to be able to convert between host and SMB order (and SMB order is definitely not the same as network order).

So, to solve the problem, we need a little bit of code, which is presented here mostly to get it out of the way so that we won't have to bother with it when we are dealing with more complex issues. The functions in Listing 11.1 read short and long integer values directly from incoming message buffers and write them directly to outgoing message buffers.

Listing 11.1 Reading and writing integer values
 ushort smb_GetShort( uchar *src, int offset )   /* ---------------------------------------------------- **    * Read a short integer converting it to host byte    * order from a byte array in SMB byte order.    * ---------------------------------------------------- **    */   {   ushort tmp;   /* Low order byte is first in the buffer. */   tmp  = (ushort)(src[offset]);   /* High order byte is next in the buffer. */   tmp = ( (ushort)(src[offset+1]) << 8 );   return( tmp );   } /* smb_GetShort */ void smb_SetShort( uchar *dst, int offset, ushort val )   /* ---------------------------------------------------- **    * Write a short integer in host byte order to the    * buffer in SMB byte order.    * ---------------------------------------------------- **    */   {   /* Low order byte first. */   dst[offset]   = (uchar)(val & 0xFF);   /* High order byte next. */   dst[offset+1] = (uchar)((val >> 8) & 0xFF);   } /* smb_SetShort */ ulong smb_GetLong( uchar *src, int offset )   /* ---------------------------------------------------- **    * Read a long integer converting it to host byte order    * from a byte array in SMB byte order.    * ---------------------------------------------------- **    */   {   ulong tmp;   tmp  = (ulong)(src[offset]);   tmp = ( (ulong)(src[offset+1]) << 8 );   tmp = ( (ulong)(src[offset+2]) << 16 );   tmp = ( (ulong)(src[offset+3]) << 24 );   return( tmp );   } /* smb_GetLong */ void smb_SetLong( uchar *dst, int offset, ulong val )   /* ---------------------------------------------------- **    * Write a long integer in host byte order to the    * buffer in SMB byte order.    * ---------------------------------------------------- **    */   {   dst[offset]   = (uchar)(val & 0xFF);   dst[offset+1] = (uchar)((val >> 8) & 0xFF);   dst[offset+2] = (uchar)((val >> 16) & 0xFF);   dst[offset+3] = (uchar)((val >> 24) & 0xFF);   } /* smb_SetLong */ 


Implementing CIFS. The Common Internet File System
Implementing CIFS: The Common Internet File System
ISBN: 013047116X
EAN: 2147483647
Year: 2002
Pages: 210

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net