Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
websocketpp::frame Namespace Reference

Data structures and utility functions for manipulating WebSocket frames. More...

Namespaces

namespace  limits
 Constants related to frame and payload limits.
 
namespace  opcode
 Constants and utility functions related to WebSocket opcodes.
 

Classes

struct  basic_header
 The constant size component of a WebSocket frame header. More...
 
struct  extended_header
 The variable size component of a WebSocket frame header. More...
 
union  uint16_converter
 Two byte conversion union. More...
 
union  uint32_converter
 Four byte conversion union. More...
 
union  uint64_converter
 Eight byte conversion union. More...
 

Typedefs

typedef uint32_converter masking_key_type
 

Functions

bool get_fin (basic_header const &h)
 Check whether the frame's FIN bit is set.
 
void set_fin (basic_header &h, bool value)
 Set the frame's FIN bit.
 
bool get_rsv1 (const basic_header &h)
 check whether the frame's RSV1 bit is set
 
void set_rsv1 (basic_header &h, bool value)
 Set the frame's RSV1 bit.
 
bool get_rsv2 (const basic_header &h)
 check whether the frame's RSV2 bit is set
 
void set_rsv2 (basic_header &h, bool value)
 Set the frame's RSV2 bit.
 
bool get_rsv3 (const basic_header &h)
 check whether the frame's RSV3 bit is set
 
void set_rsv3 (basic_header &h, bool value)
 Set the frame's RSV3 bit.
 
opcode::value get_opcode (const basic_header &h)
 Extract opcode from basic header.
 
bool get_masked (basic_header const &h)
 check whether the frame is masked
 
void set_masked (basic_header &h, bool value)
 Set the frame's MASK bit.
 
uint8_t get_basic_size (const basic_header &h)
 Extracts the raw payload length specified in the basic header.
 
size_t get_header_len (basic_header const &h)
 Calculates the full length of the header based on the first bytes.
 
unsigned int get_masking_key_offset (const basic_header &h)
 Calculate the offset location of the masking key within the extended header.
 
std::string write_header (basic_header const &, extended_header const &)
 
masking_key_type get_masking_key (const basic_header &h, const extended_header &e)
 Extract the masking key from a frame header.
 
uint16_t get_extended_size (const extended_header &e)
 Extract the extended size field from an extended header.
 
uint64_t get_jumbo_size (const extended_header &e)
 Extract the jumbo size field from an extended header.
 
uint64_t get_payload_size (const basic_header &h, const extended_header &e)
 Extract the full payload size field from a WebSocket header.
 
size_t prepare_masking_key (const masking_key_type &key)
 Extract a masking key into a value the size of a machine word.
 
size_t circshift_prepared_key (size_t prepared_key, size_t offset)
 circularly shifts the supplied prepared masking key by offset bytes
 
template<typename input_iter , typename output_iter >
void byte_mask (input_iter first, input_iter last, output_iter result, masking_key_type const &key, size_t key_offset)
 Byte by byte mask/unmask.
 
template<typename iter_type >
void byte_mask (iter_type b, iter_type e, masking_key_type const &key, size_t key_offset)
 Byte by byte mask/unmask (in place)
 
void word_mask_exact (uint8_t *input, uint8_t *output, size_t length, const masking_key_type &key)
 Exact word aligned mask/unmask.
 
void word_mask_exact (uint8_t *data, size_t length, const masking_key_type &key)
 Exact word aligned mask/unmask (in place)
 
size_t word_mask_circ (uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
 Circular word aligned mask/unmask.
 
size_t word_mask_circ (uint8_t *data, size_t length, size_t prepared_key)
 Circular word aligned mask/unmask (in place)
 
std::string prepare_header (const basic_header &h, const extended_header &e)
 Generate a properly sized contiguous string that encodes a full frame header.
 
size_t byte_mask_circ (uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
 Circular byte aligned mask/unmask.
 
size_t byte_mask_circ (uint8_t *data, size_t length, size_t prepared_key)
 Circular byte aligned mask/unmask (in place)
 

Detailed Description

namespace frame provides a number of data structures and utility functions for reading, writing, and manipulating binary encoded WebSocket frames.

Typedef Documentation

◆ masking_key_type

Function Documentation

◆ byte_mask() [1/2]

template<typename input_iter , typename output_iter >
void websocketpp::frame::byte_mask ( input_iter first,
input_iter last,
output_iter result,
masking_key_type const & key,
size_t key_offset )

Iterator based byte by byte masking and unmasking for WebSocket payloads. Performs masking in place using the supplied key offset by the supplied offset number of bytes.

This function is simple and can be done in place on input with arbitrary lengths and does not vary based on machine word size. It is slow.

Parameters
bBeginning iterator to start masking
eEnding iterator to end masking
oBeginning iterator to store masked results
key32 bit key to mask with.
key_offsetoffset value to start masking at.

Definition at line 642 of file frame.hpp.

644{
645 size_t key_index = key_offset%4;
646 while (first != last) {
647 *result = *first ^ key.c[key_index++];
648 key_index %= 4;
649 ++result;
650 ++first;
651 }
652}
Here is the caller graph for this function:

◆ byte_mask() [2/2]

template<typename iter_type >
void websocketpp::frame::byte_mask ( iter_type b,
iter_type e,
masking_key_type const & key,
size_t key_offset )

Iterator based byte by byte masking and unmasking for WebSocket payloads. Performs masking in place using the supplied key offset by the supplied offset number of bytes.

This function is simple and can be done in place on input with arbitrary lengths and does not vary based on machine word size. It is slow.

Parameters
bBeginning iterator to start masking
eEnding iterator to end masking
key32 bit key to mask with.
key_offsetoffset value to start masking at.

Definition at line 672 of file frame.hpp.

674{
675 byte_mask(b,e,b,key,key_offset);
676}
void byte_mask(input_iter b, input_iter e, output_iter o, masking_key_type const &key, size_t key_offset=0)
Byte by byte mask/unmask.
Definition frame.hpp:642
Here is the call graph for this function:

◆ byte_mask_circ() [1/2]

size_t websocketpp::frame::byte_mask_circ ( uint8_t * data,
size_t length,
size_t prepared_key )
inline

In place version of byte_mask_circ

See also
byte_mask_circ
Parameters
dataCharacter buffer to read from and write to
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 854 of file frame.hpp.

854 {
855 return byte_mask_circ(data,data,length,prepared_key);
856}
size_t byte_mask_circ(uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
Circular byte aligned mask/unmask.
Definition frame.hpp:827
Here is the call graph for this function:

◆ byte_mask_circ() [2/2]

size_t websocketpp::frame::byte_mask_circ ( uint8_t * input,
uint8_t * output,
size_t length,
size_t prepared_key )
inline

Performs a circular mask/unmask in byte sized chunks using pre-prepared keys that store state between calls. Best for providing streaming masking or unmasking of small chunks at a time of a larger message. Requires that the underlying allocated size of the data buffer be a multiple of the word size. Data in the buffer after length will be overwritten only with the same values that were originally present.

word_mask returns a copy of prepared_key circularly shifted based on the length value. The returned value may be fed back into byte_mask when more data is available.

Parameters
dataCharacter buffer to mask
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 827 of file frame.hpp.

829{
831 key.i = prepared_key;
832
833 for (size_t i = 0; i < length; ++i) {
834 output[i] = input[i] ^ key.c[i % 4];
835 }
836
837 return circshift_prepared_key(prepared_key,length % 4);
838}
Four byte conversion union.
Definition frame.hpp:61
uint8_t key[16]
Definition yubico_otp.c:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ circshift_prepared_key()

size_t websocketpp::frame::circshift_prepared_key ( size_t prepared_key,
size_t offset )
inline

Prepared_key must be the output of prepare_masking_key with the associated restrictions on the machine word size. offset must be greater than or equal to zero and less than sizeof(size_t).

Definition at line 612 of file frame.hpp.

612 {
613 if (lib::net::is_little_endian()) {
614 size_t temp = prepared_key << (sizeof(size_t)-offset)*8;
615 return (prepared_key >> offset*8) | temp;
616 } else {
617 size_t temp = prepared_key >> (sizeof(size_t)-offset)*8;
618 return (prepared_key << offset*8) | temp;
619 }
620}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_basic_size()

uint8_t websocketpp::frame::get_basic_size ( const basic_header & h)
inline

A basic WebSocket frame header contains a 7 bit value that represents the payload size. There are two reserved values that are used to indicate that the actual payload size will not fit in 7 bits and that the full payload size is included in a separate field. The values are as follows:

PAYLOAD_SIZE_CODE_16BIT (0x7E) indicates that the actual payload is less than 16 bit

PAYLOAD_SIZE_CODE_64BIT (0x7F) indicates that the actual payload is less than 63 bit

Parameters
[in]hBasic header to read value from.
Returns
The exact size encoded in h.

Definition at line 431 of file frame.hpp.

431 {
432 return h.b1 & BHB1_PAYLOAD;
433}
Here is the caller graph for this function:

◆ get_extended_size()

uint16_t websocketpp::frame::get_extended_size ( const extended_header & e)
inline

It is the responsibility of the caller to verify that e is a valid extended header. This function assumes that e contains an extended payload size.

Parameters
eThe extended header to extract from
Returns
The size encoded in the extended header in host byte order

Definition at line 540 of file frame.hpp.

540 {
541 uint16_converter temp16;
542 std::copy(e.bytes,e.bytes+2,temp16.c);
543 return ntohs(temp16.i);
544}
uint8_t bytes[MAX_EXTENDED_HEADER_LENGTH]
Definition frame.hpp:258
Two byte conversion union.
Definition frame.hpp:55
Here is the caller graph for this function:

◆ get_fin()

bool websocketpp::frame::get_fin ( basic_header const & h)
inline
Parameters
[in]hThe basic header to extract from.
Returns
True if the header's fin bit is set.

Definition at line 321 of file frame.hpp.

321 {
322 return ((h.b0 & BHB0_FIN) == BHB0_FIN);
323}
Here is the caller graph for this function:

◆ get_header_len()

size_t websocketpp::frame::get_header_len ( basic_header const & h)
inline

A WebSocket frame header always has at least two bytes. Encoded within the first two bytes is all the information necessary to calculate the full (variable) header length. get_header_len() calculates the full header length for the given two byte basic header.

Parameters
hBasic frame header to extract size from.
Returns
Full length of the extended header.

Definition at line 445 of file frame.hpp.

445 {
446 // TODO: check extensions?
447
448 // masking key offset represents the space used for the extended length
449 // fields
450 size_t size = BASIC_HEADER_LENGTH + get_masking_key_offset(h);
451
452 // If the header is masked there is a 4 byte masking key
453 if (get_masked(h)) {
454 size += 4;
455 }
456
457 return size;
458}
unsigned int get_masking_key_offset(basic_header const &)
Calculate the offset location of the masking key within the extended header.
Definition frame.hpp:469
bool get_masked(basic_header const &h)
check whether the frame is masked
Definition frame.hpp:402
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_jumbo_size()

uint64_t websocketpp::frame::get_jumbo_size ( const extended_header & e)
inline

It is the responsibility of the caller to verify that e is a valid extended header. This function assumes that e contains a jumbo payload size.

Parameters
eThe extended header to extract from
Returns
The size encoded in the extended header in host byte order

Definition at line 555 of file frame.hpp.

555 {
556 uint64_converter temp64;
557 std::copy(e.bytes,e.bytes+8,temp64.c);
558 return lib::net::_ntohll(temp64.i);
559}
Eight byte conversion union.
Definition frame.hpp:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_masked()

bool websocketpp::frame::get_masked ( basic_header const & h)
inline
Parameters
[in]hThe basic header to extract from.
Returns
True if the header mask bit is set.

Definition at line 402 of file frame.hpp.

402 {
403 return ((h.b1 & BHB1_MASK) == BHB1_MASK);
404}
Here is the caller graph for this function:

◆ get_masking_key()

masking_key_type websocketpp::frame::get_masking_key ( const basic_header & h,
const extended_header & e )
inline

Note that while read and written as an integer at times, this value is not an integer and should never be interpreted as one. Big and little endian machines will generate and store masking keys differently without issue as long as the integer values remain irrelivant.

Parameters
hThe basic header to extract from
eThe extended header to extract from
Returns
The masking key as an integer.

Definition at line 516 of file frame.hpp.

518{
519 masking_key_type temp32;
520
521 if (!get_masked(h)) {
522 temp32.i = 0;
523 } else {
524 unsigned int offset = get_masking_key_offset(h);
525 std::copy(e.bytes+offset,e.bytes+offset+4,temp32.c);
526 }
527
528 return temp32;
529}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_masking_key_offset()

unsigned int websocketpp::frame::get_masking_key_offset ( const basic_header & h)
inline

Calculate the offset location of the masking key within the extended header using information from its corresponding basic header

Parameters
hCorresponding basic header to calculate from.
Returns
byte offset of the first byte of the masking key

Definition at line 469 of file frame.hpp.

469 {
470 if (get_basic_size(h) == payload_size_code_16bit) {
471 return 2;
472 } else if (get_basic_size(h) == payload_size_code_64bit) {
473 return 8;
474 } else {
475 return 0;
476 }
477}
uint8_t get_basic_size(basic_header const &)
Extracts the raw payload length specified in the basic header.
Definition frame.hpp:431
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_opcode()

opcode::value websocketpp::frame::get_opcode ( const basic_header & h)
inline
Parameters
[in]hThe basic header to extract from.
Returns
The opcode value of the header.

Definition at line 393 of file frame.hpp.

393 {
394 return opcode::value(h.b0 & BHB0_OPCODE);
395}
Here is the caller graph for this function:

◆ get_payload_size()

uint64_t websocketpp::frame::get_payload_size ( const basic_header & h,
const extended_header & e )
inline

It is the responsibility of the caller to verify that h and e together represent a valid WebSocket frame header. This function assumes only that h and e are valid. It uses information in the basic header to determine where to look for the payload_size

Parameters
hThe basic header to extract from
eThe extended header to extract from
Returns
The size encoded in the combined header in host byte order.

Definition at line 573 of file frame.hpp.

575{
576 uint8_t val = get_basic_size(h);
577
578 if (val <= limits::payload_size_basic) {
579 return val;
580 } else if (val == payload_size_code_16bit) {
581 return get_extended_size(e);
582 } else {
583 return get_jumbo_size(e);
584 }
585}
unsigned char uint8_t
Definition stdint.h:124
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_rsv1()

bool websocketpp::frame::get_rsv1 ( const basic_header & h)
inline
Parameters
[in]hThe basic header to extract from.
Returns
True if the header's RSV1 bit is set.

Definition at line 339 of file frame.hpp.

339 {
340 return ((h.b0 & BHB0_RSV1) == BHB0_RSV1);
341}
Here is the caller graph for this function:

◆ get_rsv2()

bool websocketpp::frame::get_rsv2 ( const basic_header & h)
inline
Parameters
[in]hThe basic header to extract from.
Returns
True if the header's RSV2 bit is set.

Definition at line 357 of file frame.hpp.

357 {
358 return ((h.b0 & BHB0_RSV2) == BHB0_RSV2);
359}
Here is the caller graph for this function:

◆ get_rsv3()

bool websocketpp::frame::get_rsv3 ( const basic_header & h)
inline
Parameters
[in]hThe basic header to extract from.
Returns
True if the header's RSV3 bit is set.

Definition at line 375 of file frame.hpp.

375 {
376 return ((h.b0 & BHB0_RSV3) == BHB0_RSV3);
377}
Here is the caller graph for this function:

◆ prepare_header()

std::string websocketpp::frame::prepare_header ( const basic_header & h,
const extended_header & e )
inline

Copy the basic header h and extended header e into a properly sized contiguous frame header string for the purposes of writing out to the wire.

Parameters
hThe basic header to include
eThe extended header to include
Returns
A contiguous string containing h and e

Definition at line 489 of file frame.hpp.

491{
492 std::string ret;
493
494 ret.push_back(char(h.b0));
495 ret.push_back(char(h.b1));
496 ret.append(
497 reinterpret_cast<const char*>(e.bytes),
498 get_header_len(h)-BASIC_HEADER_LENGTH
499 );
500
501 return ret;
502}
size_t get_header_len(basic_header const &)
Calculates the full length of the header based on the first bytes.
Definition frame.hpp:445
CK_RV ret
Here is the call graph for this function:
Here is the caller graph for this function:

◆ prepare_masking_key()

size_t websocketpp::frame::prepare_masking_key ( const masking_key_type & key)
inline

Machine word size must be 4 or 8.

Parameters
keyMasking key to extract from
Returns
prepared key as a machine word

Definition at line 595 of file frame.hpp.

595 {
596 size_t low_bits = static_cast<size_t>(key.i);
597
598 if (sizeof(size_t) == 8) {
599 uint64_t high_bits = static_cast<size_t>(key.i);
600 return static_cast<size_t>((high_bits << 32) | low_bits);
601 } else {
602 return low_bits;
603 }
604}
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the caller graph for this function:

◆ set_fin()

void websocketpp::frame::set_fin ( basic_header & h,
bool value )
inline
Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 330 of file frame.hpp.

330 {
331 h.b0 = (value ? h.b0 | BHB0_FIN : h.b0 & ~BHB0_FIN);
332}
#define value
Definition pkcs11.h:157
Here is the caller graph for this function:

◆ set_masked()

void websocketpp::frame::set_masked ( basic_header & h,
bool value )
inline
Parameters
[out]hHeader to set.
valueValue to set it to.

Definition at line 411 of file frame.hpp.

411 {
412 h.b1 = (value ? h.b1 | BHB1_MASK : h.b1 & ~BHB1_MASK);
413}
Here is the caller graph for this function:

◆ set_rsv1()

void websocketpp::frame::set_rsv1 ( basic_header & h,
bool value )
inline
Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 348 of file frame.hpp.

348 {
349 h.b0 = (value ? h.b0 | BHB0_RSV1 : h.b0 & ~BHB0_RSV1);
350}
Here is the caller graph for this function:

◆ set_rsv2()

void websocketpp::frame::set_rsv2 ( basic_header & h,
bool value )
inline
Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 366 of file frame.hpp.

366 {
367 h.b0 = (value ? h.b0 | BHB0_RSV2 : h.b0 & ~BHB0_RSV2);
368}
Here is the caller graph for this function:

◆ set_rsv3()

void websocketpp::frame::set_rsv3 ( basic_header & h,
bool value )
inline
Parameters
[out]hHeader to set.
[in]valueValue to set it to.

Definition at line 384 of file frame.hpp.

384 {
385 h.b0 = (value ? h.b0 | BHB0_RSV3 : h.b0 & ~BHB0_RSV3);
386}
Here is the caller graph for this function:

◆ word_mask_circ() [1/2]

size_t websocketpp::frame::word_mask_circ ( uint8_t * data,
size_t length,
size_t prepared_key )
inline

In place version of word_mask_circ

See also
word_mask_circ
Parameters
dataCharacter buffer to read from and write to
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 802 of file frame.hpp.

802 {
803 return word_mask_circ(data,data,length,prepared_key);
804}
size_t word_mask_circ(uint8_t *input, uint8_t *output, size_t length, size_t prepared_key)
Circular word aligned mask/unmask.
Definition frame.hpp:765
Here is the call graph for this function:

◆ word_mask_circ() [2/2]

size_t websocketpp::frame::word_mask_circ ( uint8_t * input,
uint8_t * output,
size_t length,
size_t prepared_key )
inline

Performs a circular mask/unmask in word sized chunks using pre-prepared keys that store state between calls. Best for providing streaming masking or unmasking of small chunks at a time of a larger message. Requires that the underlying allocated size of the data buffer be a multiple of the word size. Data in the buffer after length will be overwritten only with the same values that were originally present.

Buffer based word by word masking and unmasking for WebSocket payloads. Performs masking in place using the supplied key. Casts the data buffer to an array of size_t's and performs masking word by word. The underlying buffer size must be a muliple of the word size.

word_mask returns a copy of prepared_key circularly shifted based on the length value. The returned value may be fed back into word_mask when more data is available.

input and output must both have length at least: ceil(length/sizeof(size_t))*sizeof(size_t) Exactly that many bytes will be written, although only exactly length bytes will be changed (trailing bytes will be replaced without masking)

Parameters
dataCharacter buffer to mask
lengthLength of data
prepared_keyPrepared key to use.
Returns
the prepared_key shifted to account for the input length

Definition at line 765 of file frame.hpp.

767{
768 size_t n = length / sizeof(size_t); // whole words
769 size_t l = length - (n * sizeof(size_t)); // remaining bytes
770 size_t * input_word = reinterpret_cast<size_t *>(input);
771 size_t * output_word = reinterpret_cast<size_t *>(output);
772
773 // mask word by word
774 for (size_t i = 0; i < n; i++) {
775 output_word[i] = input_word[i] ^ prepared_key;
776 }
777
778 // mask partial word at the end
779 size_t start = length - l;
780 uint8_t * byte_key = reinterpret_cast<uint8_t *>(&prepared_key);
781 for (size_t i = 0; i < l; ++i) {
782 output[start+i] = input[start+i] ^ byte_key[i];
783 }
784
785 return circshift_prepared_key(prepared_key,l);
786}
int l
Here is the call graph for this function:
Here is the caller graph for this function:

◆ word_mask_exact() [1/2]

void websocketpp::frame::word_mask_exact ( uint8_t * data,
size_t length,
const masking_key_type & key )
inline

In place version of word_mask_exact

See also
word_mask_exact
Parameters
databuffer to read and write from
lengthlength of data buffer
keyMasking key to use

Definition at line 728 of file frame.hpp.

730{
731 word_mask_exact(data,data,length,key);
732}
void word_mask_exact(uint8_t *input, uint8_t *output, size_t length, masking_key_type const &key)
Exact word aligned mask/unmask.
Definition frame.hpp:699
Here is the call graph for this function:

◆ word_mask_exact() [2/2]

void websocketpp::frame::word_mask_exact ( uint8_t * input,
uint8_t * output,
size_t length,
const masking_key_type & key )
inline

Balanced combination of byte by byte and circular word by word masking. Best used to mask complete messages at once. Has much higher setup costs than word_mask_circ but works with exact sized buffers.

Buffer based word by word masking and unmasking for WebSocket payloads. Masking is done in word by word chunks with the remainder not divisible by the word size done byte by byte.

input and output must both be at least length bytes. Exactly length bytes will be written.

Parameters
inputbuffer to mask or unmask
outputbuffer to store the output. May be the same as input.
lengthlength of data buffer
keyMasking key to use

Definition at line 699 of file frame.hpp.

701{
702 size_t prepared_key = prepare_masking_key(key);
703 size_t n = length/sizeof(size_t);
704 size_t* input_word = reinterpret_cast<size_t*>(input);
705 size_t* output_word = reinterpret_cast<size_t*>(output);
706
707 for (size_t i = 0; i < n; i++) {
708 output_word[i] = input_word[i] ^ prepared_key;
709 }
710
711 for (size_t i = n*sizeof(size_t); i < length; i++) {
712 output[i] = input[i] ^ key.c[i%4];
713 }
714}
size_t prepare_masking_key(masking_key_type const &key)
Extract a masking key into a value the size of a machine word.
Definition frame.hpp:595
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_header()

std::string websocketpp::frame::write_header ( basic_header const & ,
extended_header const &  )