Skip to content

cwebsocket_write_data fails to write over 65535 bytes and htonl64 fails. #4

@vishalhosakere

Description

@vishalhosakere

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) }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions