SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr, const char *remote_ip_addr, int remote_port, SilcSchedule schedule, SilcNetCallback callback, void *context) { SilcNetConnect conn; if (!remote_ip_addr || remote_port < 1 || !schedule || !callback) return NULL; SILC_LOG_DEBUG(("Creating connection to host %s port %d", remote_ip_addr, remote_port)); conn = silc_calloc(1, sizeof(*conn)); if (!conn) { callback(SILC_NET_NO_MEMORY, NULL, context); return NULL; } /* Start async operation */ conn->op = silc_async_alloc(silc_net_connect_abort, NULL, conn); if (!conn->op) { silc_free(conn); callback(SILC_NET_NO_MEMORY, NULL, context); return NULL; } if (local_ip_addr) conn->local_ip = strdup(local_ip_addr); conn->remote = strdup(remote_ip_addr); if (!conn->remote) { silc_async_free(conn->op); silc_free(conn->local_ip); silc_free(conn); callback(SILC_NET_NO_MEMORY, NULL, context); return NULL; } conn->port = remote_port; conn->callback = callback; conn->context = context; conn->retry = 1; conn->status = SILC_NET_ERROR; silc_fsm_init(&conn->fsm, conn, silc_net_connect_destructor, NULL, schedule); silc_fsm_start(&conn->fsm, silc_net_connect_st_start); return conn->op; }
int main(int argc, char **argv) { SilcBool success = FALSE; SilcSchedule schedule; SilcFSM fsm; Foo f; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_quick(TRUE); silc_log_set_debug_string("*fsm*,*async*"); } SILC_LOG_DEBUG(("Allocating scheduler")); schedule = silc_schedule_init(0, NULL); f = silc_calloc(1, sizeof(*f)); if (!f) goto err; f->schedule = schedule; SILC_LOG_DEBUG(("Allocating FSM context")); f->fsm = fsm = silc_fsm_alloc(f, destructor, f, schedule); if (!fsm) goto err; silc_fsm_start(fsm, test_st_start); SILC_LOG_DEBUG(("Running scheduler")); silc_schedule(schedule); if (f->error) goto err; silc_schedule_uninit(schedule); silc_free(f); success = TRUE; err: SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); return success; }
SilcAsyncOperation silc_connauth_initiator(SilcConnAuth connauth, SilcConnectionType conn_type, SilcAuthMethod auth_method, void *auth_data, SilcUInt32 auth_data_len, SilcConnAuthCompletion completion, void *context) { SILC_LOG_DEBUG(("Connection authentication as initiator")); if (auth_method == SILC_AUTH_PASSWORD && !auth_data) { completion(connauth, FALSE, context); return NULL; } if (auth_method == SILC_AUTH_PUBLIC_KEY && !auth_data) { completion(connauth, FALSE, context); return NULL; } connauth->conn_type = conn_type; connauth->auth_method = auth_method; connauth->auth_data = auth_data; connauth->auth_data_len = auth_data_len; connauth->completion = completion; connauth->context = context; /* Link to packet stream to get packets */ silc_packet_stream_link(connauth->ske->stream, &silc_connauth_stream_cbs, connauth, 1000000, SILC_PACKET_SUCCESS, SILC_PACKET_FAILURE, -1); /* Start the protocol */ silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth); silc_fsm_start(connauth->fsm, silc_connauth_st_initiator_start); return &connauth->op; }
int main(int argc, char **argv) { SilcFSM fsm; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*fdstream*"); } SILC_LOG_DEBUG(("Allocating scheduler")); schedule = silc_schedule_init(0, NULL); if (!schedule) goto err; SILC_LOG_DEBUG(("Allocating FSM")); fsm = silc_fsm_alloc(NULL, fsm_dest, NULL, schedule); if (!fsm) goto err; silc_fsm_start(fsm, st_write); SILC_LOG_DEBUG(("Running scheduler")); silc_schedule(schedule); if (!success) goto err; silc_schedule_uninit(schedule); success = TRUE; err: SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); return success; }
SilcAsyncOperation silc_connauth_responder(SilcConnAuth connauth, SilcConnAuthGetAuthData get_auth_data, SilcConnAuthCompletion completion, void *context) { SILC_LOG_DEBUG(("Connection authentication as responder")); connauth->get_auth_data = get_auth_data; connauth->completion = completion; connauth->context = context; /* Link to packet stream to get packets */ silc_packet_stream_link(connauth->ske->stream, &silc_connauth_stream_cbs, connauth, 1000000, SILC_PACKET_CONNECTION_AUTH, SILC_PACKET_FAILURE, -1); /* Start the protocol */ silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth); silc_fsm_start(connauth->fsm, silc_connauth_st_responder_start); return &connauth->op; }
int main(int argc, char **argv) { SilcFSM fsm; silc_runtime_init(); if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*thread*"); } SILC_LOG_DEBUG(("Allocating scheduler")); schedule = silc_schedule_init(0, NULL, NULL, NULL); if (!schedule) goto err; SILC_LOG_DEBUG(("Allocating FSM context")); fsm = silc_fsm_alloc(NULL, destructor, NULL, schedule); if (!fsm) goto err; silc_fsm_start(fsm, test_st_start); SILC_LOG_DEBUG(("Running scheduler")); silc_schedule(schedule); silc_schedule_uninit(schedule); err: SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); silc_runtime_uninit(); return !success; }
SilcClientConnection silc_client_add_connection(SilcClient client, SilcConnectionType conn_type, SilcBool connect, SilcClientConnectionParams *params, SilcPublicKey public_key, SilcPrivateKey private_key, char *remote_host, int port, SilcClientConnectCallback callback, void *context) { SilcClientConnection conn; SilcFSMThread thread; if (!callback) return NULL; SILC_LOG_DEBUG(("Adding new connection to %s:%d", remote_host, port)); conn = silc_calloc(1, sizeof(*conn)); if (!conn) return NULL; conn->client = client; conn->public_key = public_key; conn->private_key = private_key; conn->remote_host = strdup(remote_host); conn->remote_port = port ? port : 706; conn->type = conn_type; conn->callback = callback; conn->callback_context = context; conn->internal = silc_calloc(1, sizeof(*conn->internal)); if (!conn->internal) { silc_free(conn); return NULL; } conn->internal->retry_timer = SILC_CLIENT_RETRY_MIN; silc_mutex_alloc(&conn->internal->lock); silc_atomic_init16(&conn->internal->cmd_ident, 0); if (!silc_hash_alloc("sha1", &conn->internal->sha1hash)) { silc_free(conn); silc_free(conn->internal); return NULL; } /* Set parameters */ if (params) { conn->internal->params = *params; conn->context = params->context; } if (!conn->internal->params.rekey_secs) conn->internal->params.rekey_secs = 3600; if (conn->internal->params.rekey_secs < 300) conn->internal->params.rekey_secs = 300; conn->internal->verbose = TRUE; silc_list_init(conn->internal->pending_commands, struct SilcClientCommandContextStruct, next); silc_list_init(conn->internal->thread_pool, SilcFSMThreadStruct, next); /* Allocate client, channel and serve caches */ if (conn_type != SILC_CONN_CLIENT) { conn->internal->client_cache = silc_idcache_alloc(0, SILC_ID_CLIENT, NULL, NULL); conn->internal->channel_cache = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL, NULL); conn->internal->server_cache = silc_idcache_alloc(0, SILC_ID_SERVER, NULL, NULL); if (!conn->internal->client_cache || !conn->internal->channel_cache || !conn->internal->server_cache) { silc_client_del_connection(client, conn); return NULL; } } if (connect) { /* Initialize our async operation so that application may abort us while we're connecting. */ conn->internal->cop = silc_async_alloc(silc_client_connect_abort, NULL, conn); if (!conn->internal->cop) { silc_client_del_connection(client, conn); return NULL; } } /* Run the connection state machine. If threads are in use the connection machine is always run in a real thread. */ thread = silc_fsm_thread_alloc(&client->internal->fsm, conn, silc_client_connection_finished, NULL, client->internal->params->threads); if (!thread) { silc_client_del_connection(client, conn); return NULL; } silc_fsm_set_state_context(thread, client); silc_fsm_start(thread, silc_client_connection_st_start); SILC_LOG_DEBUG(("New connection %p", conn)); silc_atomic_add_int32(&client->internal->conns, 1); return conn; }