Skip to content

Commit

Permalink
checksum is now a run-time option
Browse files Browse the repository at this point in the history
  • Loading branch information
eihrul committed May 20, 2010
1 parent ea5f25d commit f761fc1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 50 deletions.
4 changes: 3 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ENet 1.2.2 (May 13, 2010):
ENet 1.2.2 (May 20, 2010):

* checksum functionality is now enabled by setting a checksum callback
inside ENetHost instead of compile time option
* added totalSentData, totalSentPackets, totalReceivedData, and
totalReceivedPackets counters inside ENetHost for getting usage
statistics
Expand Down
11 changes: 0 additions & 11 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ AC_CHECK_TYPE(socklen_t, [AC_DEFINE(HAS_SOCKLEN_T)], ,
AC_EGREP_HEADER(MSG_MAXIOVLEN, /usr/include/sys/socket.h, AC_DEFINE(ENET_BUFFER_MAXIMUM, [MSG_MAXIOVLEN]))
AC_EGREP_HEADER(MSG_MAXIOVLEN, socket.h, AC_DEFINE(ENET_BUFFER_MAXIMUM, [MSG_MAXIOVLEN]))

AC_MSG_CHECKING(whether to use CRC32)
AC_ARG_ENABLE(crc32,
[ --enable-crc32 enable CRC32 packet verification ],
[if test "$enableval" = yes; then
AC_MSG_RESULT(yes)
AC_DEFINE(USE_CRC32)
else
AC_MSG_RESULT(no)
fi],
[AC_MSG_RESULT(no)])

AC_CONFIG_FILES([Makefile
libenet.pc])
AC_OUTPUT
1 change: 1 addition & 0 deletions host.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ enet_host_create (const ENetAddress * address, size_t peerCount, enet_uint32 inc
host -> peerCount = peerCount;
host -> commandCount = 0;
host -> bufferCount = 0;
host -> checksum = NULL;
host -> receivedAddress.host = ENET_HOST_ANY;
host -> receivedAddress.port = 0;
host -> receivedDataLength = 0;
Expand Down
57 changes: 31 additions & 26 deletions include/enet/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ typedef struct _ENetPeer
enet_uint32 disconnectData;
} ENetPeer;

/** Callback that computes the checksum of the data held in buffers [0..bufferCount-1] */
typedef enet_uint32 (ENET_CALLBACK * ENetChecksumCallback) (const ENetBuffer * buffers, size_t bufferCount);

/** An ENet host for communicating with peers.
*
* No fields should be modified.
Expand All @@ -294,38 +297,40 @@ typedef struct _ENetPeer
@sa enet_host_service()
@sa enet_host_flush()
@sa enet_host_broadcast()
@sa enet_host_checksum()
@sa enet_host_channel_limit()
@sa enet_host_bandwidth_limit()
@sa enet_host_bandwidth_throttle()
*/
typedef struct _ENetHost
{
ENetSocket socket;
ENetAddress address; /**< Internet address of the host */
enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */
enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */
enet_uint32 bandwidthThrottleEpoch;
enet_uint32 mtu;
int recalculateBandwidthLimits;
ENetPeer * peers; /**< array of peers allocated for this host */
size_t peerCount; /**< number of peers allocated for this host */
size_t channelLimit; /**< maximum number of channels allowed for connected peers */
enet_uint32 serviceTime;
ENetList dispatchQueue;
int continueSending;
size_t packetSize;
enet_uint16 headerFlags;
ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
size_t commandCount;
ENetBuffer buffers [ENET_BUFFER_MAXIMUM];
size_t bufferCount;
ENetAddress receivedAddress;
enet_uint8 receivedData [ENET_PROTOCOL_MAXIMUM_MTU];
size_t receivedDataLength;
enet_uint32 totalSentData; /**< total data sent, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalSentPackets; /**< total UDP packets sent, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalReceivedData; /**< total data received, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalReceivedPackets; /**< total UDP packets received, user should reset to 0 as needed to prevent overflow */
ENetSocket socket;
ENetAddress address; /**< Internet address of the host */
enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */
enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */
enet_uint32 bandwidthThrottleEpoch;
enet_uint32 mtu;
int recalculateBandwidthLimits;
ENetPeer * peers; /**< array of peers allocated for this host */
size_t peerCount; /**< number of peers allocated for this host */
size_t channelLimit; /**< maximum number of channels allowed for connected peers */
enet_uint32 serviceTime;
ENetList dispatchQueue;
int continueSending;
size_t packetSize;
enet_uint16 headerFlags;
ENetProtocol commands [ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS];
size_t commandCount;
ENetBuffer buffers [ENET_BUFFER_MAXIMUM];
size_t bufferCount;
ENetChecksumCallback checksum;
ENetAddress receivedAddress;
enet_uint8 receivedData [ENET_PROTOCOL_MAXIMUM_MTU];
size_t receivedDataLength;
enet_uint32 totalSentData; /**< total data sent, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalSentPackets; /**< total UDP packets sent, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalReceivedData; /**< total data received, user should reset to 0 as needed to prevent overflow */
enet_uint32 totalReceivedPackets; /**< total UDP packets received, user should reset to 0 as needed to prevent overflow */
} ENetHost;

/**
Expand Down
21 changes: 9 additions & 12 deletions protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet
ENetPeer * currentPeer;
ENetProtocol verifyCommand;

#ifdef USE_CRC32
if (host -> checksum != NULL)
{
enet_uint32 crc = header -> checksum;
enet_uint32 checksum = header -> checksum;
ENetBuffer buffer;

command -> header.reliableSequenceNumber = ENET_HOST_TO_NET_16 (command -> header.reliableSequenceNumber);
Expand All @@ -257,12 +257,11 @@ enet_protocol_handle_connect (ENetHost * host, ENetProtocolHeader * header, ENet
buffer.data = host -> receivedData;
buffer.dataLength = host -> receivedDataLength;

if (enet_crc32 (& buffer, 1) != crc)
if (host -> checksum (& buffer, 1) != checksum)
return NULL;

command -> header.reliableSequenceNumber = ENET_NET_TO_HOST_16 (command -> header.reliableSequenceNumber);
}
#endif

channelCount = ENET_NET_TO_HOST_32 (command -> connect.channelCount);

Expand Down Expand Up @@ -834,23 +833,22 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
peer -> address.host != ENET_HOST_BROADCAST))
return 0;

#ifdef USE_CRC32
if (host -> checksum != NULL)
{
enet_uint32 crc = header -> checksum;
enet_uint32 checksum = header -> checksum;
ENetBuffer buffer;

header -> checksum = peer -> sessionID;

buffer.data = host -> receivedData;
buffer.dataLength = host -> receivedDataLength;

if (enet_crc32 (& buffer, 1) != crc)
if (host -> checksum (& buffer, 1) != checksum)
return 0;
}
#else
else
if (header -> checksum != peer -> sessionID)
return 0;
#endif

peer -> address.host = host -> receivedAddress.host;
peer -> address.port = host -> receivedAddress.port;
Expand Down Expand Up @@ -1415,9 +1413,8 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch
else
host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;

#ifdef USE_CRC32
header.checksum = enet_crc32 (host -> buffers, host -> bufferCount);
#endif
if (host -> checksum != NULL)
header.checksum = host -> checksum (host -> buffers, host -> bufferCount);

currentPeer -> lastSendTime = host -> serviceTime;

Expand Down

0 comments on commit f761fc1

Please sign in to comment.