static int test_server(struct harness_t *harness_p) { struct ssl_context_t context; struct ssl_socket_t ssl_socket; struct socket_t listener, socket; struct inet_addr_t addr; char buf[8]; /* Create a context with default settings. */ BTASSERT(ssl_context_init(&context) == 0); /* Create a socket and connect to the server. */ BTASSERT(socket_open_tcp(&listener) == 0); BTASSERT(socket_listen(&listener, 5) == 0); BTASSERT(socket_accept(&listener, &socket, &addr) == 0); /* Wrap the socket in SSL. */ BTASSERT(ssl_socket_init(&ssl_socket, &context, &socket, ssl_socket_mode_server_t) == 0); BTASSERT(ssl_socket_handshake(&ssl_socket) == 0); /* Transfer data to and from the server. */ BTASSERT(ssl_socket_read(&ssl_socket, &buf[0], 6) == 6); BTASSERT(strcmp("hello", buf) == 0); BTASSERT(ssl_socket_write(&ssl_socket, "goodbye", 8) == 8); /* Close the connection. */ BTASSERT(socket_close(&socket) == 0); return (0); }
static int test_client(struct harness_t *harness_p) { struct ssl_context_t context; struct ssl_socket_t ssl_socket; struct socket_t socket; struct inet_addr_t addr; char buf[8]; /* Create a context with default settings. */ BTASSERT(ssl_context_init(&context) == 0); /* Create a socket and connect to the server. */ BTASSERT(socket_open_tcp(&socket) == 0); inet_aton("1.2.3.4", &addr.ip); addr.port = 1234; BTASSERT(socket_connect(&socket, &addr) == 0); /* Wrap the socket in SSL. */ BTASSERT(ssl_socket_init(&ssl_socket, &context, &socket, ssl_socket_mode_client_t) == 0); BTASSERT(ssl_socket_handshake(&ssl_socket) == 0); /* Transfer data to and from the server. */ BTASSERT(ssl_socket_write(&ssl_socket, "hello", 6) == 6); BTASSERT(ssl_socket_read(&ssl_socket, &buf[0], 8) == 8); BTASSERT(strcmp("goodbye", buf) == 0); /* Close the connection. */ BTASSERT(socket_close(&socket) == 0); return (0); }
static int test_init(struct harness_t *harness_p) { struct thrd_t *thrd_p; int port = REMOTE_HOST_PORT; char remote_host_ip[] = STRINGIFY(REMOTE_HOST_IP); struct inet_addr_t remote_host_address; self_p = thrd_self(); std_printf(FSTR("Connecting to '%s:%d'.\r\n"), remote_host_ip, port); BTASSERT(inet_aton(remote_host_ip, &remote_host_address.ip) == 0); remote_host_address.port = port; BTASSERT(socket_open_tcp(&server_sock) == 0); BTASSERT(socket_connect(&server_sock, &remote_host_address) == 0); BTASSERT(mqtt_client_init(&client, "mqtt_client", NULL, &server_sock, &server_sock, on_publish, NULL) == 0); thrd_p = thrd_spawn(mqtt_client_main, &client, 0, stack, sizeof(stack)); thrd_set_log_mask(thrd_p, LOG_UPTO(DEBUG)); return (0); }
/** * The listener thread main function. The listener listens for * connections from clients. */ static void *listener_main(void *arg_p) { struct http_server_t *self_p = arg_p; struct http_server_listener_t *listener_p; struct http_server_connection_t *connection_p; struct inet_addr_t addr; thrd_set_name(self_p->listener_p->thrd.name_p); listener_p = self_p->listener_p; if (socket_open_tcp(&listener_p->socket) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to open socket.")); return (NULL); } if (inet_aton(listener_p->address_p, &addr.ip) != 0) { return (NULL); } addr.port = listener_p->port; if (socket_bind(&listener_p->socket, &addr) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to bind socket.")); return (NULL); } if (socket_listen(&listener_p->socket, 3) != 0) { log_object_print(NULL, LOG_ERROR, FSTR("Failed to listen on socket.")); return (NULL); } /* Wait for clients to connect. */ while (1) { /* Allocate a connection. */ connection_p = allocate_connection(self_p); /* Wait for a client to connect. */ socket_accept(&listener_p->socket, &connection_p->socket, &addr); handle_accept(self_p, connection_p); } return (NULL); }