Skip to content

Instantly share code, notes, and snippets.

@filipnavara
Last active May 25, 2021 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipnavara/d5fb55bdb5edcceb1981f73078b855c4 to your computer and use it in GitHub Desktop.
Save filipnavara/d5fb55bdb5edcceb1981f73078b855c4 to your computer and use it in GitHub Desktop.
#include <Network/Network.h>
#include <assert.h>
#include <stdio.h>
static void dump_hex(const void* data, size_t size)
{
char ascii[17];
size_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i) {
printf("%02X ", ((unsigned char*)data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i];
} else {
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size) {
printf(" ");
if ((i+1) % 16 == 0) {
printf("| %s \n", ascii);
} else if (i+1 == size) {
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8) {
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
}
}
int main()
{
nw_framer_output_handler_t framer_output_handler = ^(nw_framer_t framer, nw_framer_message_t message, size_t message_length, bool is_complete) {
printf("framer_output_handler got message with length %zu\n", message_length);
uint8_t *buffer = alloca(message_length);
nw_framer_parse_output(framer, 1, message_length, buffer, ^size_t(uint8_t *buffer, size_t buffer_length, bool is_complete) {
dump_hex(buffer, buffer_length);
return buffer_length;
});
};
nw_framer_input_handler_t framer_input_handler = ^size_t(nw_framer_t framer) {
printf("framer_input_handler\n");
return 0;
};
nw_framer_start_handler_t framer_start = ^nw_framer_start_result_t(nw_framer_t framer) {
printf("framer start\n");
nw_protocol_options_t tlsOptions = nw_tls_create_options();
sec_protocol_options_t sec_options = nw_tls_copy_sec_protocol_options(tlsOptions);
sec_protocol_options_set_tls_server_name(sec_options, "SERVER");
nw_release(sec_options);
nw_framer_prepend_application_protocol(framer, tlsOptions);
nw_framer_set_output_handler(framer, framer_output_handler);
nw_framer_set_input_handler(framer, framer_input_handler);
return nw_framer_start_result_ready;
};
nw_protocol_definition_t framer = nw_framer_create_definition("tlsFramer", NW_FRAMER_CREATE_FLAGS_DEFAULT, framer_start);
nw_protocol_options_t framer_options = nw_framer_create_options(framer);
nw_parameters_t parameters; // = nw_parameters_create();
parameters = nw_parameters_create_secure_udp(NW_PARAMETERS_DISABLE_PROTOCOL, NW_PARAMETERS_DEFAULT_CONFIGURATION);
nw_protocol_stack_t protocol_stack = nw_parameters_copy_default_protocol_stack(parameters);
nw_protocol_stack_prepend_application_protocol(protocol_stack, framer_options);
//nw_protocol_stack_set_transport_protocol(protocol_stack, framer_options);
nw_release(protocol_stack);
nw_endpoint_t endpoint = nw_endpoint_create_host("127.0.0.1", "42");
nw_connection_t connection = nw_connection_create(endpoint, parameters);
assert(connection != NULL);
//nw_retain(connection);
/*nw_connection_set_state_changed_handler(connection, ^(nw_connection_state_t state, nw_error_t error) {
nw_endpoint_t remote = nw_connection_copy_endpoint(connection);
errno = error ? nw_error_get_error_code(error) : 0;
if (state == nw_connection_state_waiting) {
printf("connect to %s port %u failed, is waiting\n",
nw_endpoint_get_hostname(remote),
nw_endpoint_get_port(remote));
} else if (state == nw_connection_state_failed) {
printf("connect to %s port %u failed\n",
nw_endpoint_get_hostname(remote),
nw_endpoint_get_port(remote));
} else if (state == nw_connection_state_ready) {
//if (g_verbose) {
fprintf(stderr, "Connection to %s port %u succeeded!\n",
nw_endpoint_get_hostname(remote),
nw_endpoint_get_port(remote));
//}
} else if (state == nw_connection_state_cancelled) {
// Release the primary reference on the connection
// that was taken at creation time
nw_release(connection);
}
nw_release(remote);
});*/
nw_connection_set_queue(connection, dispatch_get_main_queue());
nw_connection_start(connection);
dispatch_data_t data = dispatch_data_create("DATA", 4, dispatch_get_main_queue(), ^{ });
nw_connection_send(connection, data, NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT, true, ^(nw_error_t _Nullable error) {
if (error != NULL) {
errno = nw_error_get_error_code(error);
assert(false);
}
});
dispatch_main();
}
@filipnavara
Copy link
Author

Output:

framer start
framer_output_handler got message with length 517
16 03 01 02 00 01 00 01  FC 03 03 F7 1F C7 F2 9B  |  ................ 
BC 47 41 FF A6 31 20 46  AC 63 4B F5 B2 10 98 6C  |  .GA..1 F.cK....l 
3B C0 34 05 90 BE 61 2D  BF 10 5F 20 11 FE 05 3F  |  ;.4...a-.._ ...? 
9B F1 D3 7A 02 E3 C2 7A  B4 DC 3B 37 37 1C CD B9  |  ...z...z..;77... 
11 D4 AE 64 2B 22 5F F9  36 49 0F E2 00 36 9A 9A  |  ...d+"_.6I...6.. 
13 01 13 02 13 03 C0 2C  C0 2B CC A9 C0 30 C0 2F  |  .......,.+...0./ 
CC A8 C0 24 C0 23 C0 0A  C0 09 C0 28 C0 27 C0 14  |  ...$.#.....(.'.. 
C0 13 00 9D 00 9C 00 3D  00 3C 00 35 00 2F C0 08  |  .......=.<.5./.. 
C0 12 00 0A 01 00 01 7D  1A 1A 00 00 00 00 00 0B  |  .......}........ 
00 09 00 00 06 53 45 52  56 45 52 00 17 00 00 FF  |  .....SERVER..... 
01 00 01 00 00 0A 00 0C  00 0A DA DA 00 1D 00 17  |  ................ 
00 18 00 19 00 0B 00 02  01 00 00 05 00 05 01 00  |  ................ 
00 00 00 00 0D 00 18 00  16 04 03 08 04 04 01 05  |  ................ 
03 02 03 08 05 08 05 05  01 08 06 06 01 02 01 00  |  ................ 
12 00 00 00 33 00 2B 00  29 DA DA 00 01 00 00 1D  |  ....3.+.)....... 
00 20 A5 F5 AD 6A 91 41  63 0F 63 F4 88 E1 EE 43  |  . ...j.Ac.c....C 
43 A4 CA 3C 9D 41 B1 D9  42 CE 78 14 D0 AA 2F BA  |  C..<.A..B.x.../. 
E0 2E 00 2D 00 02 01 01  00 2B 00 0B 0A 5A 5A 03  |  ...-.....+...ZZ. 
04 03 03 03 02 03 01 BA  BA 00 01 00 00 15 00 D5  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |  ................ 
00 00 00 00 00                                    |  ..... 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment