source: azure_iot_hub/trunk/azure_iohub/c-utility/src/uws_frame_encoder.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 11.2 KB
Line 
1 // Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#include <stdlib.h>
5#include <stdint.h>
6#include <stddef.h>
7#include <stdbool.h>
8#include "azure_c_shared_utility/gballoc.h"
9#include "azure_c_shared_utility/gb_rand.h"
10#include "azure_c_shared_utility/uws_frame_encoder.h"
11#include "azure_c_shared_utility/xlogging.h"
12#include "azure_c_shared_utility/buffer_.h"
13#include "azure_c_shared_utility/uniqueid.h"
14
15BUFFER_HANDLE uws_frame_encoder_encode(WS_FRAME_TYPE opcode, const unsigned char* payload, size_t length, bool is_masked, bool is_final, unsigned char reserved)
16{
17 BUFFER_HANDLE result;
18
19 if (reserved > 7)
20 {
21 /* Codes_SRS_UWS_FRAME_ENCODER_01_052: [ If reserved has any bits set except the lowest 3 then uws_frame_encoder_encode shall fail and return NULL. ]*/
22 LogError("Bad reserved value: 0x%02x", reserved);
23 result = NULL;
24 }
25 else if (opcode > 0x0F)
26 {
27 /* Codes_SRS_UWS_FRAME_ENCODER_01_006: [ If an unknown opcode is received, the receiving endpoint MUST _Fail the WebSocket Connection_. ]*/
28 LogError("Invalid opcode: 0x%02x", opcode);
29 result = NULL;
30 }
31 else if ((length > 0) &&
32 (payload == NULL))
33 {
34 /* Codes_SRS_UWS_FRAME_ENCODER_01_054: [ If length is greater than 0 and payload is NULL, then uws_frame_encoder_encode shall fail and return NULL. ]*/
35 LogError("Invalid arguments: NULL payload and length=%u", (unsigned int)length);
36 result = NULL;
37 }
38 else
39 {
40 size_t needed_bytes = 2;
41 size_t header_bytes;
42
43 /* Codes_SRS_UWS_FRAME_ENCODER_01_044: [ On success uws_frame_encoder_encode shall return a non-NULL handle to the result buffer. ]*/
44 /* Codes_SRS_UWS_FRAME_ENCODER_01_048: [ The newly created buffer shall be created by calling BUFFER_new. ]*/
45 result = BUFFER_new();
46 if (result == NULL)
47 {
48 /* Codes_SRS_UWS_FRAME_ENCODER_01_049: [ If BUFFER_new fails then uws_frame_encoder_encode shall fail and return NULL. ]*/
49 LogError("Cannot create new buffer");
50 }
51 else
52 {
53 /* Codes_SRS_UWS_FRAME_ENCODER_01_001: [ uws_frame_encoder_encode shall encode the information given in opcode, payload, length, is_masked, is_final and reserved according to the RFC6455 into a new buffer.]*/
54 if (length > 65535)
55 {
56 needed_bytes += 8;
57 }
58 else if (length > 125)
59 {
60 needed_bytes += 2;
61 }
62
63 if (is_masked)
64 {
65 needed_bytes += 4;
66 }
67
68 header_bytes = needed_bytes;
69 needed_bytes += length;
70
71 /* Codes_SRS_UWS_FRAME_ENCODER_01_046: [ The result buffer shall be resized accordingly using BUFFER_enlarge. ]*/
72 if (BUFFER_enlarge(result, needed_bytes) != 0)
73 {
74 /* Codes_SRS_UWS_FRAME_ENCODER_01_047: [ If BUFFER_enlarge fails then uws_frame_encoder_encode shall fail and return NULL. ]*/
75 LogError("Cannot allocate memory for encoded frame");
76 BUFFER_delete(result);
77 result = NULL;
78 }
79 else
80 {
81 /* Codes_SRS_UWS_FRAME_ENCODER_01_050: [ The allocated memory shall be accessed by calling BUFFER_u_char. ]*/
82 unsigned char* buffer = BUFFER_u_char(result);
83 if (buffer == NULL)
84 {
85 /* Codes_SRS_UWS_FRAME_ENCODER_01_051: [ If BUFFER_u_char fails then uws_frame_encoder_encode shall fail and return a NULL. ]*/
86 LogError("Cannot get encoded buffer pointer");
87 BUFFER_delete(result);
88 result = NULL;
89 }
90 else
91 {
92 /* Codes_SRS_UWS_FRAME_ENCODER_01_007: [ * %x0 denotes a continuation frame ]*/
93 /* Codes_SRS_UWS_FRAME_ENCODER_01_008: [ * %x1 denotes a text frame ]*/
94 /* Codes_SRS_UWS_FRAME_ENCODER_01_009: [ * %x2 denotes a binary frame ]*/
95 /* Codes_SRS_UWS_FRAME_ENCODER_01_010: [ * %x3-7 are reserved for further non-control frames ]*/
96 /* Codes_SRS_UWS_FRAME_ENCODER_01_011: [ * %x8 denotes a connection close ]*/
97 /* Codes_SRS_UWS_FRAME_ENCODER_01_012: [ * %x9 denotes a ping ]*/
98 /* Codes_SRS_UWS_FRAME_ENCODER_01_013: [ * %xA denotes a pong ]*/
99 /* Codes_SRS_UWS_FRAME_ENCODER_01_014: [ * %xB-F are reserved for further control frames ]*/
100 buffer[0] = (unsigned char)opcode;
101
102 /* Codes_SRS_UWS_FRAME_ENCODER_01_002: [ Indicates that this is the final fragment in a message. ]*/
103 /* Codes_SRS_UWS_FRAME_ENCODER_01_003: [ The first fragment MAY also be the final fragment. ]*/
104 if (is_final)
105 {
106 buffer[0] |= 0x80;
107 }
108
109 /* Codes_SRS_UWS_FRAME_ENCODER_01_004: [ MUST be 0 unless an extension is negotiated that defines meanings for non-zero values. ]*/
110 buffer[0] |= reserved << 4;
111
112 /* Codes_SRS_UWS_FRAME_ENCODER_01_022: [ Note that in all cases, the minimal number of bytes MUST be used to encode the length, for example, the length of a 124-byte-long string can't be encoded as the sequence 126, 0, 124. ]*/
113 /* Codes_SRS_UWS_FRAME_ENCODER_01_018: [ The length of the "Payload data", in bytes: ]*/
114 /* Codes_SRS_UWS_FRAME_ENCODER_01_023: [ The payload length is the length of the "Extension data" + the length of the "Application data". ]*/
115 /* Codes_SRS_UWS_FRAME_ENCODER_01_042: [ The payload length, indicated in the framing as frame-payload-length, does NOT include the length of the masking key. ]*/
116 if (length > 65535)
117 {
118 /* Codes_SRS_UWS_FRAME_ENCODER_01_020: [ If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0) are the payload length. ]*/
119 buffer[1] = 127;
120
121 /* Codes_SRS_UWS_FRAME_ENCODER_01_021: [ Multibyte length quantities are expressed in network byte order. ]*/
122 buffer[2] = (unsigned char)((uint64_t)length >> 56) & 0xFF;
123 buffer[3] = (unsigned char)((uint64_t)length >> 48) & 0xFF;
124 buffer[4] = (unsigned char)((uint64_t)length >> 40) & 0xFF;
125 buffer[5] = (unsigned char)((uint64_t)length >> 32) & 0xFF;
126 buffer[6] = (unsigned char)((uint64_t)length >> 24) & 0xFF;
127 buffer[7] = (unsigned char)((uint64_t)length >> 16) & 0xFF;
128 buffer[8] = (unsigned char)((uint64_t)length >> 8) & 0xFF;
129 buffer[9] = (unsigned char)(length & 0xFF);
130 }
131 else if (length > 125)
132 {
133 /* Codes_SRS_UWS_FRAME_ENCODER_01_019: [ If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length. ]*/
134 buffer[1] = 126;
135
136 /* Codes_SRS_UWS_FRAME_ENCODER_01_021: [ Multibyte length quantities are expressed in network byte order. ]*/
137 buffer[2] = (unsigned char)(length >> 8);
138 buffer[3] = (unsigned char)(length & 0xFF);
139 }
140 else
141 {
142 /* Codes_SRS_UWS_FRAME_ENCODER_01_043: [ if 0-125, that is the payload length. ]*/
143 buffer[1] = (unsigned char)length;
144 }
145
146 if (is_masked)
147 {
148 /* Codes_SRS_UWS_FRAME_ENCODER_01_015: [ Defines whether the "Payload data" is masked. ]*/
149 /* Codes_SRS_UWS_FRAME_ENCODER_01_033: [ A masked frame MUST have the field frame-masked set to 1, as defined in Section 5.2. ]*/
150 buffer[1] |= 0x80;
151
152 /* Codes_SRS_UWS_FRAME_ENCODER_01_053: [ In order to obtain a 32 bit value for masking, gb_rand shall be used 4 times (for each byte). ]*/
153 /* Codes_SRS_UWS_FRAME_ENCODER_01_016: [ If set to 1, a masking key is present in masking-key, and this is used to unmask the "Payload data" as per Section 5.3. ]*/
154 /* Codes_SRS_UWS_FRAME_ENCODER_01_026: [ This field is present if the mask bit is set to 1 and is absent if the mask bit is set to 0. ]*/
155 /* Codes_SRS_UWS_FRAME_ENCODER_01_034: [ The masking key is contained completely within the frame, as defined in Section 5.2 as frame-masking-key. ]*/
156 /* Codes_SRS_UWS_FRAME_ENCODER_01_036: [ The masking key is a 32-bit value chosen at random by the client. ]*/
157 /* Codes_SRS_UWS_FRAME_ENCODER_01_037: [ When preparing a masked frame, the client MUST pick a fresh masking key from the set of allowed 32-bit values. ]*/
158 /* Codes_SRS_UWS_FRAME_ENCODER_01_038: [ The masking key needs to be unpredictable; thus, the masking key MUST be derived from a strong source of entropy, and the masking key for a given frame MUST NOT make it simple for a server/proxy to predict the masking key for a subsequent frame. ]*/
159 buffer[header_bytes - 4] = (unsigned char)gb_rand();
160 buffer[header_bytes - 3] = (unsigned char)gb_rand();
161 buffer[header_bytes - 2] = (unsigned char)gb_rand();
162 buffer[header_bytes - 1] = (unsigned char)gb_rand();
163 }
164
165 if (length > 0)
166 {
167 if (is_masked)
168 {
169 size_t i;
170
171 /* Codes_SRS_UWS_FRAME_ENCODER_01_035: [ It is used to mask the "Payload data" defined in the same section as frame-payload-data, which includes "Extension data" and "Application data". ]*/
172 /* Codes_SRS_UWS_FRAME_ENCODER_01_039: [ To convert masked data into unmasked data, or vice versa, the following algorithm is applied. ]*/
173 /* Codes_SRS_UWS_FRAME_ENCODER_01_040: [ The same algorithm applies regardless of the direction of the translation, e.g., the same steps are applied to mask the data as to unmask the data. ]*/
174 for (i = 0; i < length; i++)
175 {
176 /* Codes_SRS_UWS_FRAME_ENCODER_01_041: [ Octet i of the transformed data ("transformed-octet-i") is the XOR of octet i of the original data ("original-octet-i") with octet at index i modulo 4 of the masking key ("masking-key-octet-j"): ]*/
177 buffer[header_bytes + i] = ((unsigned char*)payload)[i] ^ buffer[header_bytes - 4 + (i % 4)];
178 }
179 }
180 else
181 {
182 (void)memcpy(buffer + header_bytes, payload, length);
183 }
184 }
185 }
186 }
187 }
188 }
189
190 return result;
191}
Note: See TracBrowser for help on using the repository browser.