static void error_cb(InfTcpConnection* connection, GError* error, gpointer user_data) { fprintf(stderr, "Connection Error occurred: %s\n", error->message); if(inf_standalone_io_loop_running(INF_STANDALONE_IO(user_data))) inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data)); }
static void error_cb(InfdTcpServer* server, GError* error, gpointer user_data) { fprintf(stderr, "Error occured: %s\n", error->message); if(inf_standalone_io_loop_running(INF_STANDALONE_IO(user_data))) inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data)); }
static void notify_status_cb(InfTcpConnection* connection, const gchar* property, gpointer user_data) { InfTcpConnectionStatus status; InfIpAddress* addr; guint port; gchar* addr_str; g_object_get( G_OBJECT(connection), "status", &status, "remote-address", &addr, "remote-port", &port, NULL ); addr_str = inf_ip_address_to_string(addr); inf_ip_address_free(addr); switch(status) { case INF_TCP_CONNECTION_CONNECTING: printf("Connecting to %s:%u\n", addr_str, port); break; case INF_TCP_CONNECTION_CONNECTED: printf("Connected to %s:%u\n", addr_str, port); g_object_get( G_OBJECT(connection), "local-address", &addr, "local-port", &port, NULL ); g_free(addr_str); addr_str = inf_ip_address_to_string(addr); inf_ip_address_free(addr); printf("Connected from %s:%u\n", addr_str, port); inf_tcp_connection_send(connection, "Hello, World!\n", 14); break; case INF_TCP_CONNECTION_CLOSED: printf("Connection to %s:%u closed\n", addr_str, port); if(inf_standalone_io_loop_running(INF_STANDALONE_IO(user_data))) inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data)); break; default: g_assert_not_reached(); break; } g_free(addr_str); }
static void notify_status_cb(InfdXmlServer* server, GParamSpec* pspec, gpointer user_data) { InfdXmlServerStatus status; g_object_get( G_OBJECT(server), "status", &status, NULL ); switch(status) { case INFD_XML_SERVER_CLOSED: printf("Server closed\n"); inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data)); break; case INFD_XML_SERVER_CLOSING: printf("Server closing\n"); break; case INFD_XML_SERVER_OPENING: printf("Server opening\n"); break; case INFD_XML_SERVER_OPEN: printf("Server open\n"); break; default: g_assert_not_reached(); break; } }
static void inf_test_mass_join_browser_notify_status_cb(GObject* object, const GParamSpec* pspec, gpointer user_data) { InfBrowser* browser; InfBrowserStatus status; InfBrowserIter iter; InfTestMassJoin* massjoin; InfTestMassJoiner* joiner; GSList* item; browser = INF_BROWSER(object); massjoin = (InfTestMassJoin*)user_data; joiner = NULL; for(item = massjoin->joiners; item != NULL; item = item->next) { joiner = (InfTestMassJoiner*)item->data; if(INF_BROWSER(joiner->browser) == browser) break; } g_assert(joiner != NULL); g_object_get(G_OBJECT(browser), "status", &status, NULL); switch(status) { case INF_BROWSER_OPENING: /* nothing to do */ break; case INF_BROWSER_OPEN: fprintf(stdout, "Joiner %s: Connected\n", joiner->username); inf_browser_get_root(browser, &iter); inf_browser_explore( browser, &iter, inf_test_mass_join_explore_finished_cb, joiner ); break; case INF_BROWSER_CLOSED: fprintf(stdout, "Joiner %s: Disconnected\n", joiner->username); massjoin->joiners = g_slist_remove(massjoin->joiners, joiner); if(massjoin->joiners == NULL) inf_standalone_io_loop_quit(INF_STANDALONE_IO(massjoin->io)); break; default: g_assert_not_reached(); break; } }
static void inf_test_validate_certificate_notify_status_cb(GObject* object, GParamSpec* pspec, gpointer user_data) { InfStandaloneIo* io; InfXmlConnectionStatus status; io = INF_STANDALONE_IO(user_data); g_object_get(G_OBJECT(object), "status", &status, NULL); if(status == INF_XML_CONNECTION_OPEN || status == INF_XML_CONNECTION_CLOSING || status == INF_XML_CONNECTION_CLOSED) { if(inf_standalone_io_loop_running(io)) inf_standalone_io_loop_quit(io); } }
int main(int argc, char* argv[]) { InfTestMassJoin massjoin; GError* error; int i; gchar* name; error = NULL; if(!inf_init(&error)) { fprintf(stderr, "%s\n", error->message); return -1; } massjoin.io = INF_IO(inf_standalone_io_new()); massjoin.joiners = NULL; for(i = 0; i < 128; ++i) { name = g_strdup_printf("MassJoin%03d", i); inf_test_mass_join_connect( &massjoin, "127.0.0.1", inf_protocol_get_default_port(), "Test", name ); g_free(name); //g_usleep(100000); } inf_standalone_io_loop(INF_STANDALONE_IO(massjoin.io)); return 0; }
static void notify_status_cb(InfTcpConnection* connection, GParamSpec* pspec, gpointer user_data) { InfTcpConnectionStatus status; InfIpAddress* addr; guint port; InfNameResolver* resolver; gchar* addr_str_tmp; gchar* addr_str; g_object_get( G_OBJECT(connection), "status", &status, "remote-address", &addr, "remote-port", &port, "resolver", &resolver, NULL ); if(addr != NULL) { addr_str_tmp = inf_ip_address_to_string(addr); addr_str = g_strdup_printf("%s:%u", addr_str_tmp, port); g_free(addr_str_tmp); } else { g_object_get(G_OBJECT(resolver), "hostname", &addr_str, NULL); } if(addr != NULL) inf_ip_address_free(addr); if(resolver != NULL) g_object_unref(resolver); switch(status) { case INF_TCP_CONNECTION_CONNECTING: printf("Connecting to %s\n", addr_str); break; case INF_TCP_CONNECTION_CONNECTED: printf("Connected to %s\n", addr_str); g_object_get( G_OBJECT(connection), "local-address", &addr, "local-port", &port, NULL ); g_free(addr_str); addr_str = inf_ip_address_to_string(addr); inf_ip_address_free(addr); printf("Connected from %s:%u\n", addr_str, port); inf_tcp_connection_send(connection, "Hello, World!\n", 14); break; case INF_TCP_CONNECTION_CLOSED: printf("Connection to %s closed\n", addr_str); if(inf_standalone_io_loop_running(INF_STANDALONE_IO(user_data))) inf_standalone_io_loop_quit(INF_STANDALONE_IO(user_data)); break; default: g_assert_not_reached(); break; } g_free(addr_str); }
static gboolean inf_test_certificate_validate_run(const InfTestCertificateValidateDesc* desc, GError** error) { InfIo* io; InfdXmppServer* server; InfXmppManager* xmpp_manager; InfCertificateVerify* verify; InfXmppConnection* client; gchar* pinned_file; InfXmlConnectionStatus status; InfTestCertificateValidateCheckCertificateData check_certificate_data; gboolean result; GError* conn_error; GHashTable* pinned; gnutls_x509_crt_t pinned_cert; InfCertificateChain* current_cert; gboolean cert_equal; /* Setup server */ io = INF_IO(inf_standalone_io_new()); server = inf_test_certificate_setup_server( io, desc->key_file, desc->cert_file, error ); if(server == NULL) { g_object_unref(io); return FALSE; } /* Create client */ pinned_file = inf_test_validate_setup_pin( desc->hostname, desc->pinned_certificate, error ); if(pinned_file == NULL) { g_object_unref(server); g_object_unref(io); return FALSE; } xmpp_manager = inf_xmpp_manager_new(); verify = inf_certificate_verify_new(xmpp_manager, pinned_file); check_certificate_data.did_query = FALSE; check_certificate_data.accept_query = desc->accept_query; g_signal_connect( G_OBJECT(verify), "check-certificate", G_CALLBACK(inf_test_certificate_validate_check_certificate), &check_certificate_data ); client = inf_test_certificate_validate_setup_client( io, desc->ca_file, desc->hostname, error ); if(client == NULL) { g_unlink(pinned_file); g_free(pinned_file); g_object_unref(io); g_object_unref(xmpp_manager); g_object_unref(verify); g_object_unref(server); return FALSE; } inf_xmpp_manager_add_connection(xmpp_manager, client); /* Okay, now watch for status changes on the client or whether a dialog * appears. */ g_signal_connect( G_OBJECT(client), "notify::status", G_CALLBACK(inf_test_validate_certificate_notify_status_cb), io ); conn_error = NULL; g_signal_connect( G_OBJECT(client), "error", G_CALLBACK(inf_test_validate_certificate_error_cb), &conn_error ); inf_standalone_io_loop(INF_STANDALONE_IO(io)); g_object_unref(io); /* Evaluate result */ result = TRUE; g_object_get(G_OBJECT(client), "status", &status, NULL); if(status == INF_XML_CONNECTION_OPEN) { g_assert(conn_error == NULL); if(check_certificate_data.did_query == TRUE && desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_QUERY_ACCEPT) { g_set_error( error, inf_test_certificate_validate_error(), 3, "Certificate queried and accepted but not expected to" ); result = FALSE; } else if(check_certificate_data.did_query == FALSE && desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_ACCEPT) { g_set_error( error, inf_test_certificate_validate_error(), 0, "Certificate accepted but not expected to" ); result = FALSE; } } else { g_assert(check_certificate_data.did_query || conn_error != NULL); /* TODO: The certificate verification result is not preserved at * the moment. We could change this in * inf_xmpp_connection_certificate_verify_cancel such that the existing * error is used if any, or otherwise our own is created. */ if(conn_error != NULL && conn_error->domain != inf_xmpp_connection_error_quark() && conn_error->code != INF_XMPP_CONNECTION_ERROR_CERTIFICATE_NOT_TRUSTED) { g_propagate_error(error, conn_error); conn_error = NULL; result = FALSE; } else if(check_certificate_data.did_query == TRUE && desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_QUERY_REJECT) { g_set_error( error, inf_test_certificate_validate_error(), 2, "Certificate queried and rejected but not expected to" ); result = FALSE; } else if(check_certificate_data.did_query == FALSE && desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_REJECT) { g_set_error( error, inf_test_certificate_validate_error(), 1, "Certificate rejected but not expected to" ); result = FALSE; } if(conn_error != NULL) { g_error_free(conn_error); conn_error = NULL; } } /* If we got the expected result, check whether the host was correctly * pinned or not. */ if(result == TRUE) { pinned = inf_cert_util_read_certificate_map(pinned_file, error); if(pinned == NULL) { result = FALSE; } else { pinned_cert = g_hash_table_lookup(pinned, desc->hostname); cert_equal = FALSE; if(pinned_cert != NULL) { g_object_get( G_OBJECT(client), "remote-certificate", ¤t_cert, NULL ); cert_equal = inf_cert_util_compare_fingerprint( pinned_cert, inf_certificate_chain_get_own_certificate(current_cert), &conn_error ); inf_certificate_chain_unref(current_cert); } if(conn_error != NULL) { g_propagate_error(error, conn_error); conn_error = NULL; } else if(cert_equal == TRUE && desc->expect_pinned == FALSE) { g_set_error( error, inf_test_certificate_validate_error(), 4, "Certificate was pinned but not expected to" ); result = FALSE; } else if(pinned_cert == NULL && desc->expect_pinned == TRUE) { g_set_error( error, inf_test_certificate_validate_error(), 5, "Certificate was not pinned but expected to" ); result = FALSE; } g_hash_table_destroy(pinned); } } g_unlink(pinned_file); g_free(pinned_file); g_object_unref(xmpp_manager); g_object_unref(verify); g_object_unref(server); g_object_unref(client); return result; }
static void inf_test_mass_join_connect(InfTestMassJoin* massjoin, const char* hostname, guint port, const char* document, const char* username) { InfIpAddress* addr; InfTcpConnection* tcp; InfXmppConnection* xmpp; InfTestMassJoiner* joiner; InfXmlConnection* xml; GError* error; addr = inf_ip_address_new_from_string(hostname); tcp = inf_tcp_connection_new(massjoin->io, addr, port); xmpp = inf_xmpp_connection_new( tcp, INF_XMPP_CONNECTION_CLIENT, g_get_host_name(), hostname, INF_XMPP_CONNECTION_SECURITY_BOTH_PREFER_TLS, NULL, NULL, NULL ); joiner = g_slice_new(InfTestMassJoiner); joiner->communication_manager = inf_communication_manager_new(); joiner->browser = infc_browser_new( massjoin->io, joiner->communication_manager, INF_XML_CONNECTION(xmpp) ); joiner->session = NULL; joiner->document = g_strdup(document); joiner->username = g_strdup(username); g_object_unref(xmpp); g_object_unref(tcp); inf_ip_address_free(addr); massjoin->joiners = g_slist_prepend(massjoin->joiners, joiner); infc_browser_add_plugin(joiner->browser, &INF_TEST_MASS_JOIN_TEXT_PLUGIN); g_signal_connect( G_OBJECT(joiner->browser), "notify::status", G_CALLBACK(inf_test_mass_join_browser_notify_status_cb), massjoin ); error = NULL; xml = infc_browser_get_connection(joiner->browser); if(inf_xml_connection_open(xml, &error) == FALSE) { fprintf( stderr, "Joiner %s: Failed to connect to %s: %s\n", joiner->username, hostname, error->message ); g_error_free(error); massjoin->joiners = g_slist_remove(massjoin->joiners, joiner); if(massjoin->joiners == NULL) inf_standalone_io_loop_quit(INF_STANDALONE_IO(massjoin->io)); } }