#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <byteswap.h>
#include <unistd.h>
#include <string.h>
unsigned int func(int sock, int server)
{
unsigned int num; /* assume int is 32-bits */
if (server)
{
/* Server side */
num = 0x17;
/* Endianness of server host may not match endianness of network. */
if (send(sock, (void *)&num, sizeof(num), 0) < (int)sizeof(num))
{
/* Handle error */
}
return 0;
}
else {
/* Endianness of client host may not match endianness of network. */
if (recv (sock, (void *)&num, sizeof(num), 0) < (int) sizeof(num))
{
/* Handle error */
}
/* Comparison may be inaccurate */
if (num> 255)
{
return 255;
}
else
{
return num;
}
}
}
In this example, variable num is assigned hexadecimal value
0x17 and is sent over a network to the client from the server. If the
server host is little endian and the network is big endian, num is
transferred as 0x17000000. The client then reads an incorrect value for
num and compares it to a local numeric value.
Correction — Use Byte Ordering FunctionBefore sending num from the server host, use
htonl() to convert from host to network byte ordering. Similarly,
before reading num on the client host, use ntohl()
to convert from network to host byte ordering.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <byteswap.h>
#include <unistd.h>
#include <string.h>
unsigned int func(int sock, int server)
{
unsigned int num; /* assume int is 32-bits */
if (server)
{
/* Server side */
num = 0x17;
/* Convert to network byte order. */
num = htonl(num);
if (send(sock, (void *)&num, sizeof(num), 0) < (int)sizeof(num))
{
/* Handle error */
}
return 0;
}
else {
if (recv (sock, (void *)&num, sizeof(num), 0) < (int) sizeof(num))
{
/* Handle error */
}
/* Convert to host byte order. */
num = ntohl(num);
if (num > 255)
{
return 255;
}
else
{
return num;
}
}
}