static bool run(const options_t *options, transaction_t *transaction) { bool ret = false; connection_t *connection = NULL; transaction_t reply; connection = connection_new(options->host, options->port); if (!connection) { return EXIT_FAILURE; } ret = connection_connect(connection); if (!ret) { return ret; } switch (transaction->command) { case STATUS: { ret = status_command(connection, transaction); break; } default: { ret = default_send_receive(connection, transaction); break; } } return ret; }
struct connection *connection_create(int socket_descriptor, pid_t dest) { struct connection *conn = connection_new(); conn->peer_pid = dest; return conn; }
static void handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout) { struct connection *conn; log_set_peer_addr(request->idr_conf.isc_target_addr); if (request->idr_conf.isc_target[0] != '\0') { log_set_peer_name(request->idr_conf.isc_target); setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target); } else { setproctitle("%s", request->idr_conf.isc_target_addr); } conn = connection_new(request->idr_session_id, &request->idr_conf, iscsi_fd); set_timeout(timeout); capsicate(conn); login(conn); if (conn->conn_conf.isc_discovery != 0) discovery(conn); else handoff(conn); log_debugx("nothing more to do; exiting"); exit (0); }
connection_t *backtracking_algorithm(const network_t *network, node_t *source, node_t *destination) { //Used as a buffer connection_t *currentSolution = connection_new(source, destination); //The solution will be found here connection_t *globalSolution = connection_new(source, destination); //Apply algorithm backtracking(source, currentSolution, globalSolution); //Free the buffer free(currentSolution); //Return solution, which can be either VALID(if a solution was found) or INVALID(otherwise) return globalSolution; }
Connection* connect_to_host(const char *hostname, int port) { /* if(NULL == hostname){ */ /* return NULL; */ /* } */ /* int sockfd = -1, err; */ /* struct addrinfo *ailist = NULL, *aip = NULL; */ /* struct addrinfo hint; */ /* memset(&hint, 0, sizeof(hint)); */ /* hint.ai_socktype = SOCK_STREAM; */ /* if((err = getaddrinfo(hostname, NULL, &hint, &ailist)) != 0){ */ /* g_warning("Can't get the address information of %s (%s, %d)" */ /* ,hostname , __FILE__, __LINE__); */ /* return NULL; */ /* } */ /* struct sockaddr_in *sinp; */ /* for(aip = ailist; aip != NULL; aip = aip -> ai_next){ */ /* if((sockfd =socket(aip -> ai_family, SOCK_STREAM, 0)) < 0){ */ /* g_warning("Can't create a socket.(%s, %d)" */ /* , __FILE__, __LINE__); */ /* return NULL; */ /* } */ /* sinp = (struct sockaddr_in *)aip -> ai_addr; */ /* /\* */ /* * the http protocol uses port 80 */ /* *\/ */ /* sinp -> sin_port = htons((gint16)port); */ /* if(connect(sockfd,aip -> ai_addr, aip -> ai_addrlen) < 0){ */ /* close(sockfd); */ /* sockfd = -1; */ /* g_warning("Can't connect to the server.(%s, %d)" */ /* , __FILE__, __LINE__); */ /* continue; */ /* } */ /* //connect to the host success. */ /* break; */ /* } */ /* freeaddrinfo(ailist); */ int sockfd = get_authenticated_socket(hostname, port); Connection *con = connection_new(); con -> fd = sockfd; GIOChannel *channel = g_io_channel_unix_new(sockfd); //read and write binary data. g_io_channel_set_encoding(channel, NULL, NULL); con -> channel = channel; return con; }
static Connection * connections_get_connection (Connections * connections, const gchar * path) { Connection *connection = g_hash_table_lookup (connections->map, path); if (connection == NULL) { connection = connection_new (); g_hash_table_insert (connections->map, g_strdup (path), connection); } return connection; }
/** Launch a new cpuworker. Return 0 if we're happy, -1 if we failed. */ static int spawn_cpuworker(void) { tor_socket_t *fdarray; tor_socket_t fd; connection_t *conn; int err; fdarray = tor_malloc(sizeof(tor_socket_t)*2); if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) { log_warn(LD_NET, "Couldn't construct socketpair for cpuworker: %s", tor_socket_strerror(-err)); tor_free(fdarray); return -1; } tor_assert(SOCKET_OK(fdarray[0])); tor_assert(SOCKET_OK(fdarray[1])); fd = fdarray[0]; if (spawn_func(cpuworker_main, (void*)fdarray) < 0) { tor_close_socket(fdarray[0]); tor_close_socket(fdarray[1]); tor_free(fdarray); return -1; } log_debug(LD_OR,"just spawned a cpu worker."); #ifndef TOR_IS_MULTITHREADED tor_close_socket(fdarray[1]); /* don't need the worker's side of the pipe */ tor_free(fdarray); #endif conn = connection_new(CONN_TYPE_CPUWORKER, AF_UNIX); /* set up conn so it's got all the data we need to remember */ conn->s = fd; conn->address = tor_strdup("localhost"); tor_addr_make_unspec(&conn->addr); if (set_socket_nonblocking(fd) == -1) { connection_free(conn); /* this closes fd */ return -1; } if (connection_add(conn) < 0) { /* no space, forget it */ log_warn(LD_NET,"connection_add for cpuworker failed. Giving up."); connection_free(conn); /* this closes fd */ return -1; } conn->state = CPUWORKER_STATE_IDLE; connection_start_reading(conn); return 0; /* success */ }
JNIEXPORT jint JNICALL Java_com_iiordanov_aSPICE_SpiceCommunicator_SpiceClientConnect (JNIEnv *env, jobject obj, jstring h, jstring p, jstring tp, jstring pw, jstring cf, jstring cs, jboolean sound) { int result = 0; maintainConnection = TRUE; soundEnabled = sound; // Get a reference to the JVM to get JNIEnv from in (other) threads. jint rs = (*env)->GetJavaVM(env, &jvm); if (rs != JNI_OK) { __android_log_write(6, "spicy", "ERROR: Could not obtain jvm reference."); return 255; } // Find the jclass reference and get a Global reference for it for use in other threads. jclass local_class = (*env)->FindClass (env, "com/iiordanov/aSPICE/SpiceCommunicator"); jni_connector_class = (jclass)((*env)->NewGlobalRef(env, local_class)); // Get global method IDs for callback methods. jni_settings_changed = (*env)->GetStaticMethodID (env, jni_connector_class, "OnSettingsChanged", "(IIII)V"); jni_graphics_update = (*env)->GetStaticMethodID (env, jni_connector_class, "OnGraphicsUpdate", "(IIIII)V"); g_thread_init(NULL); bindtextdomain(GETTEXT_PACKAGE, SPICE_GTK_LOCALEDIR); bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); textdomain(GETTEXT_PACKAGE); g_type_init(); mainloop = g_main_loop_new(NULL, false); spice_connection *conn; conn = connection_new(); spice_session_setup(env, conn->session, h, p, tp, pw, cf, cs); //watch_stdin(); connection_connect(conn); if (connections > 0) { g_main_loop_run(mainloop); connection_disconnect(conn); g_object_unref(mainloop); __android_log_write(6, "spicy", "Exiting main loop."); } else { __android_log_write(6, "spicy", "Wrong hostname, port, or password."); result = 2; } jvm = NULL; jni_connector_class = NULL; jni_settings_changed = NULL; jni_graphics_update = NULL; return result; }
static void hotplug_poll(void) { /* Scan the input path for event devices */ DIR *dir; struct dirent *dent; int fd; char full_path[PATH_MAX]; dir = opendir(config_input_path); if (!dir) { /* No point to continuing if we can't scan the directory */ perror("Opening input directory"); exit(1); } while ((dent = readdir(dir))) { /* We only care about event devices */ if (strncmp(dent->d_name, "event", 5)) continue; /* Construct a full path, we'll need it for the rest */ strncpy(full_path, config_input_path, sizeof(full_path)-1); full_path[sizeof(full_path)-1] = '\0'; strncat(full_path, "/", sizeof(full_path)-1); full_path[sizeof(full_path)-1] = '\0'; strncat(full_path, dent->d_name, sizeof(full_path)-1); full_path[sizeof(full_path)-1] = '\0'; /* Make sure it isn't a device we already have connected */ if (connection_list_find_path(full_path)) continue; /* Make sure we can open it- if we can, we'll need to see * whether it's a device we're interested in. */ fd = open(full_path, O_RDWR); if (fd >= 0) { if (hotplug_detect(fd)) { close(fd); connection_new(full_path, config_host_and_port); } else { close(fd); } } } closedir(dir); }
static void test_routerlist_router_is_already_dir_fetching(void *arg) { (void)arg; tor_addr_port_t test_ap, null_addr_ap, zero_port_ap; /* Setup */ tor_addr_parse(&test_ap.addr, TEST_ADDR_STR); test_ap.port = TEST_DIR_PORT; tor_addr_make_null(&null_addr_ap.addr, AF_INET6); null_addr_ap.port = TEST_DIR_PORT; tor_addr_parse(&zero_port_ap.addr, TEST_ADDR_STR); zero_port_ap.port = 0; MOCK(connection_get_by_type_addr_port_purpose, mock_connection_get_by_type_addr_port_purpose); /* Test that we never get 1 from a NULL connection */ mocked_connection = NULL; tt_assert(router_is_already_dir_fetching(&test_ap, 1, 1) == 0); tt_assert(router_is_already_dir_fetching(&test_ap, 1, 0) == 0); tt_assert(router_is_already_dir_fetching(&test_ap, 0, 1) == 0); /* We always expect 0 in these cases */ tt_assert(router_is_already_dir_fetching(&test_ap, 0, 0) == 0); tt_assert(router_is_already_dir_fetching(NULL, 1, 1) == 0); tt_assert(router_is_already_dir_fetching(&null_addr_ap, 1, 1) == 0); tt_assert(router_is_already_dir_fetching(&zero_port_ap, 1, 1) == 0); /* Test that we get 1 with a connection in the appropriate circumstances */ mocked_connection = connection_new(CONN_TYPE_DIR, AF_INET); tt_assert(router_is_already_dir_fetching(&test_ap, 1, 1) == 1); tt_assert(router_is_already_dir_fetching(&test_ap, 1, 0) == 1); tt_assert(router_is_already_dir_fetching(&test_ap, 0, 1) == 1); /* Test that we get 0 even with a connection in the appropriate * circumstances */ tt_assert(router_is_already_dir_fetching(&test_ap, 0, 0) == 0); tt_assert(router_is_already_dir_fetching(NULL, 1, 1) == 0); tt_assert(router_is_already_dir_fetching(&null_addr_ap, 1, 1) == 0); tt_assert(router_is_already_dir_fetching(&zero_port_ap, 1, 1) == 0); done: /* If a connection is never set up, connection_free chokes on it. */ if (mocked_connection) { buf_free(mocked_connection->inbuf); buf_free(mocked_connection->outbuf); } tor_free(mocked_connection); UNMOCK(connection_get_by_type_addr_port_purpose); }
static void on_connected(int32_t fd,int32_t err,void *ud){ if(fd >= 0 && err == 0){ engine *e = (engine*)ud; connection *c = connection_new(fd,65535,rpacket_decoder_new(1024)); engine_associate(e,(handle*)c,on_packet); timer *t = engine_regtimer(e,20,timer_callback,c); c->ud_ptr = t; //packet *p = (packet*)wpacket_new(64); //wpacket_write_uint64((wpacket*)p,(uint64_t)c); //wpacket_write_string((wpacket*)p,"hello world\n"); //connection_send(c,p,NULL); }else{ printf("connect error\n"); } }
int main (void) { loop = EV_DEFAULT; // initialise an io watcher, then start it // this one will watch for stdin to become readable setnonblock(0); ev_io_init(&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); connection_new(EV_A_ "/tmp/libev-echo.sock"); // now wait for events to arrive ev_loop(EV_A_ 0); // unloop was called, so exit return 0; }
/* * This function does all of the setup required to run a test *except* * for instantiating the Tpm2Response object. */ static void tpm2_response_setup_base (void **state) { test_data_t *data = NULL; gint fds[2] = { 0, }; HandleMap *handle_map; data = calloc (1, sizeof (test_data_t)); /* allocate a buffer large enough to hold a TPM2 header and a handle */ data->buffer = calloc (1, TPM_RESPONSE_HEADER_SIZE + sizeof (TPM_HANDLE)); handle_map = handle_map_new (TPM_HT_TRANSIENT, MAX_ENTRIES_DEFAULT); data->connection = connection_new (&fds[0], &fds[1], 0, handle_map); g_object_unref (handle_map); *state = data; }
/** * This is a setup function for testing the "short-cut" constructor for the * Tpm2Response object that creates the response buffer with the provided RC. */ static void tpm2_response_new_rc_setup (void **state) { test_data_t *data = NULL; gint fds[2] = { 0, }; HandleMap *handle_map; data = calloc (1, sizeof (test_data_t)); /* allocate a buffer large enough to hold a TPM2 header */ handle_map = handle_map_new (TPM_HT_TRANSIENT, MAX_ENTRIES_DEFAULT); data->connection = connection_new (&fds[0], &fds[1], 0, handle_map); g_object_unref (handle_map); data->response = tpm2_response_new_rc (data->connection, TPM_RC_BINDING); *state = data; }
/* * This setup function chains up to the 'setup_with_init' function. * Additionally it creates a Connection and Tpm2Command object for use in * the unit test. */ static void access_broker_setup_with_command (void **state) { test_data_t *data; gint fds [2] = { 0 }; guint8 *buffer; HandleMap *handle_map; access_broker_setup_with_init (state); data = (test_data_t*)*state; buffer = calloc (1, TPM_HEADER_SIZE); handle_map = handle_map_new (TPM_HT_TRANSIENT, MAX_ENTRIES_DEFAULT); data->connection = connection_new (&fds[0], &fds[1], 0, handle_map); g_object_unref (handle_map); data->command = tpm2_command_new (data->connection, buffer, (TPMA_CC){ 0, }); }
static void * test_conn_get_basic_setup(const struct testcase_t *tc) { connection_t *conn = NULL; tor_addr_t addr; int socket_err = 0; int in_progress = 0; (void)tc; MOCK(connection_connect_sockaddr, mock_connection_connect_sockaddr); init_connection_lists(); conn = connection_new(TEST_CONN_TYPE, TEST_CONN_FAMILY); tt_assert(conn); test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr); tt_assert(!tor_addr_is_null(&addr)); /* XXXX - connection_connect doesn't set these, should it? */ tor_addr_copy_tight(&conn->addr, &addr); conn->port = TEST_CONN_PORT; mock_connection_connect_sockaddr_called = 0; in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr, TEST_CONN_PORT, &socket_err); tt_assert(mock_connection_connect_sockaddr_called == 1); tt_assert(!socket_err); tt_assert(in_progress == 0 || in_progress == 1); /* fake some of the attributes so the connection looks OK */ conn->state = TEST_CONN_STATE; conn->purpose = TEST_CONN_BASIC_PURPOSE; assert_connection_ok(conn, time(NULL)); UNMOCK(connection_connect_sockaddr); return conn; /* On failure */ done: UNMOCK(connection_connect_sockaddr); test_conn_get_basic_teardown(tc, conn); /* Returning NULL causes the unit test to fail */ return NULL; }
static connection_t * test_conn_get_connection(uint8_t state, uint8_t type, uint8_t purpose) { connection_t *conn = NULL; tor_addr_t addr; int socket_err = 0; int in_progress = 0; MOCK(connection_connect_sockaddr, mock_connection_connect_sockaddr); MOCK(tor_close_socket, fake_close_socket); init_connection_lists(); conn = connection_new(type, TEST_CONN_FAMILY); tt_assert(conn); test_conn_lookup_addr_helper(TEST_CONN_ADDRESS, TEST_CONN_FAMILY, &addr); tt_assert(!tor_addr_is_null(&addr)); tor_addr_copy_tight(&conn->addr, &addr); conn->port = TEST_CONN_PORT; mock_connection_connect_sockaddr_called = 0; in_progress = connection_connect(conn, TEST_CONN_ADDRESS_PORT, &addr, TEST_CONN_PORT, &socket_err); tt_assert(mock_connection_connect_sockaddr_called == 1); tt_assert(!socket_err); tt_assert(in_progress == 0 || in_progress == 1); /* fake some of the attributes so the connection looks OK */ conn->state = state; conn->purpose = purpose; assert_connection_ok(conn, time(NULL)); UNMOCK(connection_connect_sockaddr); UNMOCK(tor_close_socket); return conn; /* On failure */ done: UNMOCK(connection_connect_sockaddr); UNMOCK(tor_close_socket); return NULL; }
/* -------------------------------------------------------------------------- */ static bool mpd_init_connection(struct lcd_stuff_mpd *mpd) { char *server = NULL, *password = NULL; int port; /* get config items */ server = key_file_get_string_default(MODULE_NAME, "server", "localhost"); password = key_file_get_string_default(MODULE_NAME, "password", ""); port = key_file_get_integer_default(MODULE_NAME, "port", 6600); mpd->timeout = key_file_get_integer_default(MODULE_NAME, "timeout", 10); /* set the global connection */ mpd->connection = connection_new(server, password, port); g_free(server); g_free(password); if (!mpd->connection) return false; return true; }
static void server_listener_ready(void *arg) { log_debug("Server %p got a connection", arg); ServerPtr server = (ServerPtr) arg; if (server) { struct sockaddr_in incoming; socklen_t incoming_size = sizeof(incoming); int socket = accept(server->listener, (struct sockaddr *) &incoming, &incoming_size); if (socket == -1) { log_error("Error accepting connection: %s", strerror(errno)); } else { if (incoming.sin_family != AF_INET) { log_error("Invalid address family from incoming connection %d", incoming.sin_family); } else { char addr[16]; log_info("Got connection from %s:%u", inet_ntop(incoming.sin_family, &incoming.sin_addr, addr, sizeof(addr)), incoming.sin_port); ConnectionPtr conn = connection_new(server, socket); list_enqueue(server->connections, conn); } } } }
/** * dispatch a thread for a new connection * @param sfd: the fd of the connection * @param event_flags: event's flag * @param server_ev_base: base event * @param addr: the ip address of connection * @param srv: the struct of ast_server ************************************************************/ void server_dispatch_conn(int sfd, int event_flags, struct event_base *server_ev_base, struct sockaddr_in addr, emp_server_t *srv){ connection_t *connection = (connection_t*)connection_new(sfd, server_message_got, event_flags, tcp_transport, addr, server_ev_base); last_server_thread++; //LM_DBG ("last_server_thread is %d\n", last_server_thread); task_thread_t *thread = server_threads_manager.threads + last_server_thread; // conn *c = (struct conn*)conn_new(sfd, server_message_got, event_flags, tcp_transport, // addr, thread->base); connection->server = srv; connection->thread = thread; if (thread == NULL) LM_ERR("server_dispatch_conn thread is null\n"); }
/* process_packet: * Callback which processes a packet captured by libpcap. */ void process_packet(u_char *user, const struct pcap_pkthdr *hdr, const u_char *pkt) { struct tcphdr tcp; int off, len, delta; connection *C, c; struct sockaddr_storage src, dst; struct sockaddr *s, *d; uint8_t proto; s = (struct sockaddr *)&src; d = (struct sockaddr *)&dst; if (handle_link_layer(&datalink_info, pkt, hdr->caplen, &proto, &off)) return; if (layer3_find_tcp(pkt, proto, &off, s, d, &tcp)) return; len = hdr->caplen - off; /* XXX fragmented packets and other nasties. */ /* try to find the connection slot associated with this. */ C = find_connection(s, d); /* no connection at all, so we need to allocate one. */ if (!C) { log_msg(LOG_INFO, "new connection: %s", connection_string(s,d)); C = alloc_connection(); *C = connection_new(s, d); /* This might or might not be an entirely new connection (SYN flag * set). Either way we need a sequence number to start at. */ (*C)->isn = ntohl(tcp.th_seq); } /* Now we need to process this segment. */ c = *C; delta = 0;/*tcp.syn ? 1 : 0;*/ /* NB (STD0007): * SEG.LEN = the number of octets occupied by the data in the * segment (counting SYN and FIN) */ #if 0 if (tcp.syn) /* getting a new isn. */ c->isn = htonl(tcp.seq); #endif if (tcp.th_flags & TH_RST) { /* Looks like this connection is bogus, and so might be a * connection going the other way. */ log_msg(LOG_INFO, "connection reset: %s", connection_string(s, d)); connection_delete(c); *C = NULL; if ((C = find_connection(d, s))) { connection_delete(*C); *C = NULL; } return; } if (len > 0) { /* We have some data in the packet. If this data occurred after * the first data we collected for this connection, then save it * so that we can look for images. Otherwise, discard it. */ unsigned int offset; offset = ntohl(tcp.th_seq); /* Modulo 2**32 arithmetic; offset = seq - isn + delta. */ if (offset < (c->isn + delta)) offset = 0xffffffff - (c->isn + delta - offset); else offset -= c->isn + delta; if (offset > c->len + WRAPLEN) { /* Out-of-order packet. */ log_msg(LOG_INFO, "out of order packet: %s", connection_string(s, d)); } else { connection_push(c, pkt + off, offset, len); extract_media(c); } } if (tcp.th_flags & TH_FIN) { /* Connection closing; mark it as closed, but let sweep_connections * free it if appropriate. */ log_msg(LOG_INFO, "connection closing: %s, %d bytes transferred", connection_string(s, d), c->len); c->fin = 1; } /* sweep out old connections */ sweep_connections(); }
int main(int argc, char **argv) { int c; char *key = getenv("MAGIC_KEY"); if(key) { int k = atoi(key); if((k>KEY_RESERVED)&&(k<255)) { magic_key = k; printf("use key 0x%x (%d)\n",k,k); } } while (1) { static struct option long_options[] = { {"help", 0, 0, 'h'}, {"quiet", 0, 0, 'q'}, {"input-path", 1, 0, 'p'}, {"hotplug-js", 0, 0, 'j'}, {"hotplug-mice", 0, 0, 'm'}, {"hotplug-kb", 0, 0, 'k'}, {"hotplug-all", 0, 0, 'a'}, {"connect-led", 1, 0, 'l'}, {0}, }; c = getopt_long(argc, argv, "hqp:jmkal:", long_options, NULL); if (c == -1) break; switch (c) { case 'q': config_verbose = 0; break; case 'p': config_input_path = optarg; break; case 'j': config_detect_joystick = 1; config_hotplug_polling = 1; break; case 'm': config_detect_mouse = 1; config_hotplug_polling = 1; break; case 'k': config_detect_keyboard = 1; config_hotplug_polling = 1; break; case 'a': config_detect_all = 1; config_hotplug_polling = 1; break; case 'l': if (optarg[0] == '~') { config_connect_led_number = led_name_to_number(optarg+1); config_connect_led_polarity = 0; } else { config_connect_led_number = led_name_to_number(optarg); config_connect_led_polarity = 1; } break; case 'h': default: usage(argv[0]); return 1; } } /* We require at least a server name */ if (!argv[optind]) { usage(argv[0]); return 1; } config_host_and_port = argv[optind++]; /* Let the user know they're doing something silly if we aren't in hotplug * polling mode and there aren't any explicitly specified devices. */ if ((!config_hotplug_polling) && !argv[optind]) { fprintf(stderr, "Nothing to do; give at least one hotplug option or device name\n\n"); usage(argv[0]); return 1; } /* Open all the explicitly mentioned devices */ while (argv[optind]) { if (!connection_new(argv[optind], config_host_and_port)) return 1; optind++; } return event_loop(); }