struct hub_probe* probe_create(struct hub_info* hub, int sd, struct ip_addr_encap* addr) { struct hub_probe* probe = (struct hub_probe*) hub_malloc_zero(sizeof(struct hub_probe)); if (probe == NULL) return NULL; /* OOM */ LOG_TRACE("probe_create(): %p", probe); probe->hub = hub; probe->connection = net_con_create(); net_con_initialize(probe->connection, sd, probe_net_event, probe, NET_EVENT_READ); net_con_set_timeout(probe->connection, TIMEOUT_CONNECTED); memcpy(&probe->addr, addr, sizeof(struct ip_addr_encap)); return probe; }
static struct net_connection* start_listening_socket(const char* bind_addr, uint16_t port, int backlog, struct hub_info* hub) { struct net_connection* server; struct sockaddr_storage addr; socklen_t sockaddr_size; int sd, ret; if (ip_convert_address(bind_addr, port, (struct sockaddr*) &addr, &sockaddr_size) == -1) { return 0; } sd = net_socket_create(addr.ss_family, SOCK_STREAM, IPPROTO_TCP); if (sd == -1) { return 0; } if ((net_set_reuseaddress(sd, 1) == -1) || (net_set_nonblocking(sd, 1) == -1)) { net_close(sd); return 0; } ret = net_bind(sd, (struct sockaddr*) &addr, sockaddr_size); if (ret == -1) { LOG_ERROR("hub_start_service(): Unable to bind to TCP local address. errno=%d, str=%s", net_error(), net_error_string(net_error())); net_close(sd); return 0; } ret = net_listen(sd, backlog); if (ret == -1) { LOG_ERROR("hub_start_service(): Unable to listen to socket"); net_close(sd); return 0; } server = net_con_create(); net_con_initialize(server, sd, net_on_accept, hub, NET_EVENT_READ); return server; }
struct uhub_notify_handle* net_notify_create(net_notify_callback cb, void* ptr) { LOG_TRACE("net_notify_create()"); struct uhub_notify_handle* handle = (struct uhub_notify_handle*) hub_malloc(sizeof(struct uhub_notify_handle)); handle->callback = cb; handle->ptr = ptr; #ifndef WIN32 int ret = pipe(handle->pipe_fd); if (ret == -1) { LOG_ERROR("Unable to setup notification pipes."); hub_free(handle); return 0; } handle->con = net_con_create(); net_con_initialize(handle->con, handle->pipe_fd[0], notify_callback, handle, NET_EVENT_READ); #endif return handle; }
static int net_connect_job_process(struct net_connect_job* job) { int sd; if (!job->con) { sd = net_socket_create(job->addr.ss_family, SOCK_STREAM, IPPROTO_TCP); if (sd == -1) { LOG_DEBUG("net_connect_job_process: Unable to create socket!"); net_connect_callback(job->handle, net_connect_status_socket_error, NULL); return -1; // FIXME } job->con = net_con_create(); net_con_initialize(job->con, sd, net_connect_job_internal_cb, job, NET_EVENT_WRITE); net_con_set_timeout(job->con, TIMEOUT_CONNECTED); // FIXME: Use a proper timeout value! } return net_connect_job_check(job); }