Byte Ordering - City University of New York
Download
Report
Transcript Byte Ordering - City University of New York
#include<sys/types.h>
Datatype
Description
int8_t
uint8_t
int16_t
uint16_t
int32_t
uint32_t
Signed 8-bit integer
Unsigned 8-bit integer
Signed 16-bit integer
Unsigned 16-bit integer
Signed 32-bit integer
Unsigned 32-bit integer
sa_family_t
socklen_t
Address family of socket address structure
Length of socket address structure, normally
uint32_t
in_addr_t
in_port_t
IPv4 address, normally uint32_t
TCP or UDP port, normally uint16_t
Byte Ordering
• 16-bit integer is made up of 2 bytes. 2 ways
to store the bytes in memory
• Little-endian byte order: low order byte at
the beginning address
• Big-endian byte order: high-order byte at
the starting address
Network byte order and host byte
order
• TCP/IP specifies a standard representation
for binary integers used in protocol headers
called Network byte order that represents
integers with the most significant byte first
• We refer to the byte ordering used by given
system as the host byte order
Byte-ordering functions
• Following function converts integers
• htons--from host to network byte order short
unit16_t htons(unit16_t value);
• ntohs--from network to host byte order short
unit16_t ntohs(unit16_t value);
• htonl—from host to network byte order long
unit32_t htonl(unit32_t value);
• ntohs--from network to host byte order long
unit32_t ntohl(unit32_t value);
bzero function
• Syntax
void bzero( void *dest, size_t nbytes);
• It is provided by most the system to set nbytes of
memory pointed by dest as 0.
• Example
bzero( &servaddr, sizeof(servaddr));
• Will set servaddr ( server address struct) as 0
Group of address conversion
functions
int inet_aton( const char *strptr, struct in_addr *addrptr);
Returns; 1 if string was valid, 0 on error
convert from ASCII string to network byte order binary
values
char * inet_ntoa( struct inaddr inaddr);
Return: pointer to dotted-decimal string
Convert from network byte order binary value to dotted
decimal string
These two functions convert IPv4 address
• int inet_pton( int family, const
char* strptr, void *addrptr);
• Function will convert IP address in string(
strptr) to the numeric value as in address
struct( addptr).
Return 1 if OK, return –1 on error
• Const char *inet_ntop(int family,
const void *addptr, char *strptr,
size_t len);
• return pointer if K, NULL on error
• Does the reverse conversion, from numeric
(addptr) to presentation (strptr). The len is
the size of the destination, to prevent the
function from overflowing the caller’s
buffer.
Wrapper functions
• In any real world program it is essential to check
every function call for error return. Since
terminating on an error is the common case, we
can shorten our programs by defining a wrapper
function that performs the actual function call,
tests, the return value, and terminates on an error
• s = Socket(AF_INET, SOCK_STREAM, 0);
Wrapper function Socket()
int Socket( int family, int type, int protocol)
{
int n;
if( n = socket( family, type, protocol)) < 0)
{
printf(“socket error\n”);
exit(0);
}
return n;
}