Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/archive/cyclonessl/tlsio_cyclonessl_socket.c

raw
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 #error CycloneSSL is NOT supported for the Azure C shared utilities.
5 #error The code here is provided for reference purposes.
6 #error
7 #error A security audit is required if you attempt to bring this code back.
8
9 #include <stdlib.h>
10 #include "azure_c_shared_utility/optimize_size.h"
11 #include "azure_c_shared_utility/xlogging.h"
12 #include "tls.h"
13 #include "azure_c_shared_utility/tlsio_cyclonessl_socket.h"
14
15 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_001: [ tlsio_cyclonessl_socket_create shall create a new socket to be used by CycloneSSL. ]*/
16 int tlsio_cyclonessl_socket_create(const char* hostname, unsigned int port, TlsSocket* new_socket)
17 {
18 int result;
19
20 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_003: [ If hostname or socket is NULL, then tlsio_cyclonessl_socket_create shall fail and it shall return a non-zero value. ]*/
21 if ((hostname == NULL) ||
22 (new_socket == NULL))
23 {
24 LogError("Invalid arguments: hostname = %p, new_socket = %p", hostname, new_socket);
25 result = MU_FAILURE;
26 }
27 else
28 {
29 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_004: [ tlsio_cyclonessl_socket_create shall call socketOpen to create a TCP socket. ]*/
30 Socket* socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP);
31 if (socket == NULL)
32 {
33 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_007: [ If any of the socket calls fails, then tlsio_cyclonessl_socket_create shall fail and return a non-zero value. ]*/
34 LogError("socketOpen failed, cannot create socket");
35 result = MU_FAILURE;
36 }
37 else
38 {
39 IpAddr ipAddr;
40
41 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_005: [ tlsio_cyclonessl_socket_create shall call getHostByName to obtain an IpAddr structure filled with the address of the hostname. ]*/
42 if (getHostByName(NULL, hostname, &ipAddr, 0) != 0)
43 {
44 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_007: [ If any of the socket calls fails, then tlsio_cyclonessl_socket_create shall fail and return a non-zero value. ]*/
45 socketClose(socket);
46 LogError("Cannot resolve host");
47 result = MU_FAILURE;
48 }
49 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_006: [ tlsio_cyclonessl_socket_create shall call socketConnect and pass the obtained address in order to connect the socket. ]*/
50 else if (socketConnect(socket, &ipAddr, port) != 0)
51 {
52 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_007: [ If any of the socket calls fails, then tlsio_cyclonessl_socket_create shall fail and return a non-zero value. ]*/
53 socketClose(socket);
54 LogError("Failed to connect");
55 result = MU_FAILURE;
56 }
57 else
58 {
59 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_002: [ On success tlsio_cyclonessl_socket_create shall return 0 and fill in the socket handle in the new_socket out argument. ]*/
60 *new_socket = (TlsSocket)socket;
61 result = 0;
62 }
63 }
64 }
65
66 return result;
67 }
68
69 void tlsio_cyclonessl_socket_destroy(TlsSocket socket)
70 {
71 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_009: [ If socket is NULL, tlsio_cyclonessl_socket_destroy shall do nothing. ]*/
72 if (socket == NULL)
73 {
74 LogError("tlsio_cyclonessl_socket_destroy: NULL socket");
75 }
76 else
77 {
78 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_01_008: [ tlsio_cyclonessl_socket_destroy shall close the socket passed as argument by calling the function socketClose. ]*/
79 socketClose(socket);
80 }
81 }