Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/archive/cyclonessl/tlsio_cyclonessl_socket_bsd.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
11 #if _WIN32
12 #define _WINERROR_
13 #include <winsock2.h>
14 #include <ws2tcpip.h>
15 #endif
16
17 #include <stdio.h>
18 #include "tls.h"
19 #include "azure_c_shared_utility/optimize_size.h"
20 #include "azure_c_shared_utility/xlogging.h"
21 #include "azure_c_shared_utility/tlsio_cyclonessl_socket.h"
22
23 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_001: [ tlsio_cyclonessl_socket_create shall create a new socket to be used by CycloneSSL. ]*/
24 int tlsio_cyclonessl_socket_create(const char* hostname, unsigned int port, TlsSocket* new_socket)
25 {
26 int result;
27
28 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_002: [ If hostname or new_socket is NULL, then tlsio_cyclonessl_socket_create shall fail and it shall return a non-zero value. ]*/
29 if ((hostname == NULL) ||
30 (new_socket == NULL))
31 {
32 LogError("Invalid arguments: hostname = %p, new_socket = %p", hostname, new_socket);
33 result = MU_FAILURE;
34 }
35 else
36 {
37 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_003: [ tlsio_cyclonessl_socket_create shall call socket to create a TCP socket. ]*/
38 SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
39 if (sock == (SOCKET)-1)
40 {
41 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_007: [ If any of the socket calls fails, then tlsio_cyclonessl_socket_create shall fail and return a non-zero value. ]*/
42 LogError("Error: Cannot create socket (%d)\r\n", WSAGetLastError());
43 result = MU_FAILURE;
44 }
45 else
46 {
47 char portString[16];
48 ADDRINFO addrHint = { 0 };
49 ADDRINFO* addrInfo = NULL;
50
51 addrHint.ai_family = AF_INET;
52 addrHint.ai_socktype = SOCK_STREAM;
53 addrHint.ai_protocol = 0;
54
55 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_004: [ tlsio_cyclonessl_socket_create shall call getaddrinfo to obtain a hint ADDRINFO. ]*/
56 if ((sprintf(portString, "%u", port) < 0) ||
57 (getaddrinfo(hostname, portString, &addrHint, &addrInfo) != 0))
58 {
59 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_007: [ If any of the socket calls fails, then tlsio_cyclonessl_socket_create shall fail and return a non-zero value. ]*/
60 LogError("Failure: getaddrinfo failure %d.", WSAGetLastError());
61 (void)closesocket(sock);
62 result = MU_FAILURE;
63 }
64 else
65 {
66 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_006: [ tlsio_cyclonessl_socket_create shall call connect and pass the constructed address in order to connect the socket. ]*/
67 if (connect(sock, addrInfo->ai_addr, (int)addrInfo->ai_addrlen) < 0)
68 {
69 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_007: [ If any of the socket calls fails, then tlsio_cyclonessl_socket_create shall fail and return a non-zero value. ]*/
70 LogError("Error: Failed to connect (%d)\r\n", WSAGetLastError());
71 closesocket(sock);
72 result = MU_FAILURE;
73 }
74 else
75 {
76 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_008: [ On success tlsio_cyclonessl_socket_create shall return 0 and fill in the socket handle in the new_socket out argument. ]*/
77 *new_socket = (TlsSocket)sock;
78 result = 0;
79 }
80 }
81 }
82 }
83
84 return result;
85 }
86
87 void tlsio_cyclonessl_socket_destroy(TlsSocket socket)
88 {
89 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_010: [ If socket is INVALID_SOCKET (-1), tlsio_cyclonessl_socket_destroy shall do nothing. ]*/
90 if (socket == (SOCKET)-1)
91 {
92 LogError("Invalid socket\r\n");
93 }
94 else
95 {
96 /* Codes_SRS_TLSIO_CYCLONESSL_SOCKET_BSD_01_009: [ tlsio_cyclonessl_socket_destroy shall close the socket passed as argument by calling the function close. ]*/
97 (void)closesocket((SOCKET)socket);
98 }
99 }