cwebsocket icon indicating copy to clipboard operation
cwebsocket copied to clipboard

cwebsocket_write_data fails to write over 65535 bytes and htonl64 fails.

Open vishalhosakere opened this issue 6 years ago • 1 comments

The frame headers are wrong for frames with payload length over 65535. The below line in cwebsocket_write_data adds 10 additional bytes to header length when it should be only 8.

uint32_t header_length = 6 + (payload_len > 125 ? 2 : 0) + (payload_len > 0xffff ? 8 : 0);

Changing this to the below solves it.

uint32_t header_length = 6 + (payload_len > 125 ? 2 : 0) + (payload_len > 0xffff ? 6 : 0);

In common.h

#define htonl64(p) {\
	(char)(((p & ((uint64_t)0xff <<  0)) >>  0) & 0xff), (char)(((p & ((uint64_t)0xff <<  8)) >>  8) & 0xff), \
	(char)(((p & ((uint64_t)0xff << 16)) >> 16) & 0xff), (char)(((p & ((uint64_t)0xff << 24)) >> 24) & 0xff), \
	(char)(((p & ((uint64_t)0xff << 32)) >> 32) & 0xff), (char)(((p & ((uint64_t)0xff << 40)) >> 40) & 0xff), \
(char)(((p & ((uint64_t)0xff << 48)) >> 48) & 0xff), (char)(((p & ((uint64_t)0xff << 56)) >> 56) & 0xff) }

needs to be changed to

#define htonl64(p) {\
    (char)(((p & ((uint64_t)0xff << 56)) >> 56) & 0xff), (char)(((p & ((uint64_t)0xff << 48)) >> 48) & 0xff), \
    (char)(((p & ((uint64_t)0xff << 40)) >> 40) & 0xff), (char)(((p & ((uint64_t)0xff << 32)) >> 32) & 0xff), \
    (char)(((p & ((uint64_t)0xff << 24)) >> 24) & 0xff), (char)(((p & ((uint64_t)0xff << 16)) >> 16) & 0xff), \
    (char)(((p & ((uint64_t)0xff <<  8)) >>  8) & 0xff), (char)(((p & ((uint64_t)0xff <<  0)) >>  0) & 0xff) }

vishalhosakere avatar Sep 12 '18 21:09 vishalhosakere

Thanks for your report. Are you able to open a pull request?

jeremyhahn avatar Sep 18 '18 14:09 jeremyhahn