Skip to content

Commit b2cc746

Browse files
authored
Adding random adapter (#654)
* Adding random * Disabling MACOSX random * prototype fix * Flag comment
1 parent 1129147 commit b2cc746

12 files changed

Lines changed: 92 additions & 59 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ set(source_c_files
232232
./src/gbnetwork.c
233233
./src/gb_stdio.c
234234
./src/gb_time.c
235-
./src/gb_rand.c
236235
./src/hmac.c
237236
./src/hmacsha256.c
238237
./src/xio.c
@@ -257,6 +256,7 @@ set(source_c_files
257256
${LOCK_C_FILE}
258257
${SRW_LOCK_C_FILE}
259258
${TIMER_C_FILE}
259+
${RANDOM_C_FILE}
260260
${STRING_UTILS_C_FILE}
261261
${PLATFORM_C_FILE}
262262
${SOCKETIO_C_FILE}
@@ -356,7 +356,6 @@ set(source_h_files
356356
./inc/azure_c_shared_utility/gbnetwork.h
357357
./inc/azure_c_shared_utility/gb_stdio.h
358358
./inc/azure_c_shared_utility/gb_time.h
359-
./inc/azure_c_shared_utility/gb_rand.h
360359
./inc/azure_c_shared_utility/hmac.h
361360
./inc/azure_c_shared_utility/hmacsha256.h
362361
./inc/azure_c_shared_utility/http_proxy_io.h

‎adapters/random_posix.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
#ifdef __cplusplus
5+
#include <cstdlib>
6+
#else
7+
#include <stdlib.h>
8+
#endif
9+
10+
#include "azure_c_shared_utility/random.h"
11+
12+
void RANDOM_seed(unsigned int seed)
13+
{
14+
srandom(seed);
15+
}
16+
17+
int RANDOM_generate(void)
18+
{
19+
return random();
20+
}

src/gb_rand.c renamed to adapters/random_stub.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
#include <stdlib.h>
88
#endif
99

10-
#include "azure_c_shared_utility/gb_rand.h"
10+
#include "azure_c_shared_utility/random.h"
1111

12-
/*this is rand*/
13-
int gb_rand(void)
12+
void RANDOM_seed(unsigned int seed)
1413
{
15-
return rand();
14+
srand(seed);
15+
}
16+
17+
int RANDOM_generate(void)
18+
{
19+
return rand();
1620
}

‎configs/azure_c_shared_utilityFunctions.cmake‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ function(set_platform_files c_shared_dir)
700700
set(SRW_LOCK_C_FILE ${c_shared_dir}/adapters/srw_lock.c PARENT_SCOPE)
701701
set(TIMER_C_FILE ${c_shared_dir}/adapters/timer.c PARENT_SCOPE)
702702
set(STRING_UTILS_C_FILE ${c_shared_dir}/adapters/string_utils.c PARENT_SCOPE)
703+
set(RANDOM_C_FILE ${c_shared_dir}/adapters/random_stub.c PARENT_SCOPE)
703704

704705
if (${use_builtin_httpapi})
705706
set(HTTP_C_FILE ${c_shared_dir}/adapters/httpapi_compact.c PARENT_SCOPE)
@@ -754,6 +755,12 @@ function(set_platform_files c_shared_dir)
754755
else()
755756
set(DNS_C_FILE ${c_shared_dir}/src/dns_resolver_sync.c PARENT_SCOPE)
756757
endif()
758+
759+
if(UNIX AND NOT MACOSX)
760+
set(RANDOM_C_FILE ${c_shared_dir}/adapters/random_posix.c PARENT_SCOPE)
761+
else()
762+
set(RANDOM_C_FILE ${c_shared_dir}/adapters/random_stub.c PARENT_SCOPE)
763+
endif()
757764
endif()
758765

759766
if(WIN32 OR MACOSX OR LINUX)

‎configs/azure_iot_build_rules.cmake‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ elseif(UNIX) #LINUX OR APPLE
7474
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
7575
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
7676
if(NOT (IN_OPENWRT OR APPLE))
77-
set (CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L ${CMAKE_C_FLAGS}")
77+
# _XOPEN_SOURCE=500 is required for glibc to expose random and srandom.
78+
set (CMAKE_C_FLAGS "-D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=500 ${CMAKE_C_FLAGS}")
7879
endif()
7980
endif()
8081

‎devdoc/uws_frame_encoder_requirements.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extern int uws_frame_encoder_encode(BUFFER_HANDLE encode_buffer, WS_FRAME_TYPE o
6464

6565
**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. **]**
6666

67-
**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). **]**
67+
**SRS_UWS_FRAME_ENCODER_01_053: [** In order to obtain a 32 bit value for masking, `RANDOM_generate` shall be used 4 times (for each byte). **]**
6868

6969
### RFC6455 relevant parts
7070

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
#ifndef GB_RAND_H
5-
#define GB_RAND_H
4+
#ifndef RANDOM_H
5+
#define RANDOM_H
66

77
#include "umock_c/umock_c_prod.h"
88

99
#ifdef __cplusplus
1010
extern "C" {
1111
#endif
1212

13-
MOCKABLE_FUNCTION(, int, gb_rand);
13+
MOCKABLE_FUNCTION(, void, RANDOM_seed, unsigned int, seed);
14+
MOCKABLE_FUNCTION(, int, RANDOM_generate);
1415

1516
#ifdef __cplusplus
1617
}
1718
#endif
1819

19-
#endif /* GB_RAND_H */
20+
#endif /* RANDOM_H */

‎src/aziotsharedutil.def‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ EXPORTS
129129
STRING_quote
130130
STRING_sprintf
131131
STRING_replace
132+
RANDOM_seed
133+
RANDOM_generate
132134
THREADAPI_RESULTStringStorage
133135
THREADAPI_RESULTStrings
134136
THREADAPI_RESULT_FromString
@@ -174,7 +176,6 @@ EXPORTS
174176
connectionstringparser_splitHostName_from_char
175177
consolelogger_log
176178
consolelogger_log_with_GetLastError
177-
gb_rand
178179
gballoc_calloc
179180
gballoc_deinit
180181
gballoc_free

‎src/uws_client.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "azure_c_shared_utility/uws_frame_encoder.h"
2121
#include "azure_c_shared_utility/crt_abstractions.h"
2222
#include "azure_c_shared_utility/utf8_checker.h"
23-
#include "azure_c_shared_utility/gb_rand.h"
23+
#include "azure_c_shared_utility/random.h"
2424
#include "azure_c_shared_utility/azure_base64.h"
2525
#include "azure_c_shared_utility/optionhandler.h"
2626
#include "azure_c_shared_utility/map.h"
@@ -780,7 +780,7 @@ static void on_underlying_io_open_complete(void* context, IO_OPEN_RESULT open_re
780780
/* Codes_SRS_UWS_CLIENT_01_090: [ The nonce MUST be selected randomly for each connection. ]*/
781781
for (i = 0; i < sizeof(nonce); i++)
782782
{
783-
nonce[i] = (unsigned char)gb_rand();
783+
nonce[i] = (unsigned char)RANDOM_generate();
784784
}
785785

786786
/* Codes_SRS_UWS_CLIENT_01_497: [ The nonce needed for the upgrade request shall be Base64 encoded with Azure_Base64_Encode_Bytes. ]*/

‎src/uws_frame_encoder.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <stdint.h>
66
#include <stdbool.h>
77
#include "azure_c_shared_utility/gballoc.h"
8-
#include "azure_c_shared_utility/gb_rand.h"
8+
#include "azure_c_shared_utility/random.h"
99
#include "azure_c_shared_utility/uws_frame_encoder.h"
1010
#include "azure_c_shared_utility/xlogging.h"
1111
#include "azure_c_shared_utility/buffer_.h"
@@ -148,17 +148,17 @@ BUFFER_HANDLE uws_frame_encoder_encode(WS_FRAME_TYPE opcode, const unsigned char
148148
/* 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. ]*/
149149
buffer[1] |= 0x80;
150150

151-
/* 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). ]*/
151+
/* Codes_SRS_UWS_FRAME_ENCODER_01_053: [ In order to obtain a 32 bit value for masking, RANDOM_generate shall be used 4 times (for each byte). ]*/
152152
/* 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. ]*/
153153
/* 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. ]*/
154154
/* 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. ]*/
155155
/* Codes_SRS_UWS_FRAME_ENCODER_01_036: [ The masking key is a 32-bit value chosen at random by the client. ]*/
156156
/* 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. ]*/
157157
/* 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. ]*/
158-
buffer[header_bytes - 4] = (unsigned char)gb_rand();
159-
buffer[header_bytes - 3] = (unsigned char)gb_rand();
160-
buffer[header_bytes - 2] = (unsigned char)gb_rand();
161-
buffer[header_bytes - 1] = (unsigned char)gb_rand();
158+
buffer[header_bytes - 4] = (unsigned char)RANDOM_generate();
159+
buffer[header_bytes - 3] = (unsigned char)RANDOM_generate();
160+
buffer[header_bytes - 2] = (unsigned char)RANDOM_generate();
161+
buffer[header_bytes - 1] = (unsigned char)RANDOM_generate();
162162
}
163163

164164
if (length > 0)

0 commit comments

Comments
 (0)