Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/samples/tlsio_connect/main.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 #include "stdio.h"
5 #include "azure_c_shared_utility/optimize_size.h"
6 #include "azure_c_shared_utility/xio.h"
7 #include "azure_c_shared_utility/tlsio.h"
8 #include "azure_c_shared_utility/platform.h"
9
10 // A simple sample callback.
11 static void on_send_complete(void* context, IO_SEND_RESULT send_result)
12 {
13 (void)context;
14 (void)send_result;
15 }
16
17 static void on_io_open_complete(void* context, IO_OPEN_RESULT open_result)
18 {
19 (void)context, (void)open_result;
20 (void)printf("Open complete called\r\n");
21
22 if (open_result == IO_OPEN_OK)
23 {
24 XIO_HANDLE tlsio = (XIO_HANDLE)context;
25 const char to_send[] = "GET / HTTP/1.1\r\n"
26 "Host: www.google.com\r\n"
27 "\r\n";
28 (void)printf("Sending bytes ...\r\n");
29 if (xio_send(tlsio, to_send, sizeof(to_send), on_send_complete, NULL) != 0)
30 {
31 (void)printf("Send failed\r\n");
32 }
33 }
34 else
35 {
36 (void)printf("Open error\r\n");
37 }
38 }
39
40 static void on_io_bytes_received(void* context, const unsigned char* buffer, size_t size)
41 {
42 (void)context, (void)buffer;
43 (void)printf("Received %lu bytes\r\n", (unsigned long)size);
44 }
45
46 static void on_io_error(void* context)
47 {
48 (void)context;
49 (void)printf("IO reported an error\r\n");
50 }
51
52 int main(int argc, char** argv)
53 {
54 int result;
55
56 (void)argc, (void)argv;
57
58 if (platform_init() != 0)
59 {
60 (void)printf("Cannot initialize platform.");
61 result = MU_FAILURE;
62 }
63 else
64 {
65 const IO_INTERFACE_DESCRIPTION* tlsio_interface = platform_get_default_tlsio();
66 if (tlsio_interface == NULL)
67 {
68 (void)printf("Error getting tlsio interface description.");
69 result = MU_FAILURE;
70 }
71 else
72 {
73 TLSIO_CONFIG tlsio_config = { "www.google.com", 443, NULL, NULL};
74 XIO_HANDLE tlsio;
75
76 tlsio = xio_create(tlsio_interface, &tlsio_config);
77 if (tlsio == NULL)
78 {
79 (void)printf("Error creating TLS IO.");
80 result = MU_FAILURE;
81 }
82 else
83 {
84 if (xio_open(tlsio, on_io_open_complete, tlsio, on_io_bytes_received, tlsio, on_io_error, tlsio) != 0)
85 {
86 (void)printf("Error opening TLS IO.");
87 result = MU_FAILURE;
88 }
89 else
90 {
91 unsigned char done = 0;
92 while (!done)
93 {
94 xio_dowork(tlsio);
95 }
96
97 result = 0;
98 }
99
100 xio_destroy(tlsio);
101 }
102 }
103
104 platform_deinit();
105 }
106
107 return result;
108 }