status_t socket_socketpair(int family, int type, int protocol, net_socket* sockets[2]) { sockets[0] = NULL; sockets[1] = NULL; // create sockets status_t error = socket_open(family, type, protocol, &sockets[0]); if (error != B_OK) return error; if (error == B_OK) error = socket_open(family, type, protocol, &sockets[1]); // bind one if (error == B_OK) error = socket_bind(sockets[0], NULL, 0); // start listening if (error == B_OK) error = socket_listen(sockets[0], 1); // connect them if (error == B_OK) { error = socket_connect(sockets[1], (sockaddr*)&sockets[0]->address, sockets[0]->address.ss_len); } // accept a socket net_socket* acceptedSocket = NULL; if (error == B_OK) error = socket_accept(sockets[0], NULL, NULL, &acceptedSocket); if (error == B_OK) { // everything worked: close the listener socket socket_close(sockets[0]); socket_free(sockets[0]); sockets[0] = acceptedSocket; } else { // close sockets on error for (int i = 0; i < 2; i++) { if (sockets[i] != NULL) { socket_close(sockets[i]); socket_free(sockets[i]); sockets[i] = NULL; } } } return error; }
/* ** Open a connection to the server. The server is defined by the following ** variables: ** ** pUrlData->name Name of the server. Ex: www.fossil-scm.org ** pUrlData->port TCP/IP port. Ex: 80 ** pUrlData->isHttps Use TLS for the connection ** ** Return the number of errors. */ int transport_open(UrlData *pUrlData){ int rc = 0; if( transport.isOpen==0 ){ if( pUrlData->isSsh ){ rc = transport_ssh_open(pUrlData); if( rc==0 ) transport.isOpen = 1; }else if( pUrlData->isHttps ){ #ifdef FOSSIL_ENABLE_SSL rc = ssl_open(pUrlData); if( rc==0 ) transport.isOpen = 1; #else socket_set_errmsg("HTTPS: Fossil has been compiled without SSL support"); rc = 1; #endif }else if( pUrlData->isFile ){ sqlite3_uint64 iRandId; sqlite3_randomness(sizeof(iRandId), &iRandId); transport.zOutFile = mprintf("%s-%llu-out.http", g.zRepositoryName, iRandId); transport.zInFile = mprintf("%s-%llu-in.http", g.zRepositoryName, iRandId); transport.pFile = fossil_fopen(transport.zOutFile, "wb"); if( transport.pFile==0 ){ fossil_fatal("cannot output temporary file: %s", transport.zOutFile); } transport.isOpen = 1; }else{ rc = socket_open(pUrlData); if( rc==0 ) transport.isOpen = 1; } } return rc; }
void server_run(server_context_t *c, void *(*popper_thread)(void *)) { fifo_init(&c->fifo, c->fifo_size_in_bits, c->fifo_packet_size_in_bits); c->server_thread_arg.fifo = &c->fifo; c->popper_thread_arg.fifo = &c->fifo; c->server_thread_arg.max_packet_size = 1 << c->fifo_packet_size_in_bits; c->popper_thread_arg.max_packet_size = 1 << c->fifo_packet_size_in_bits; c->server_thread_arg.sleep_usec = c->sleep_usec; c->popper_thread_arg.sleep_usec = c->sleep_usec; c->server_thread_arg.max_mbps = c->max_mbps; c->popper_thread_arg.max_mbps = c->max_mbps; socket_open(c); pthread_create(&c->server_th, NULL, (void* (*)(void*))server_thread, &c->server_thread_arg); pthread_create(&c->popper_th, NULL, popper_thread, &c->popper_thread_arg); pthread_join(c->popper_th, NULL); #ifdef __ANDROID__ // this is wrong, but android has no pthread_cancel // see stack overflow for a better solution that uses a SIGUSR1 handler // that I don't have time to implement right now // http://stackoverflow.com/questions/4610086/pthread-cancel-alternatives-in-android-ndk // pthread_kill(c->server_th, SIGUSR1); #else pthread_cancel(c->server_th); #endif socket_close(c); fifo_delete(&c->fifo); }
int qif_init (void) { int ret; struct rt_msghdr rtm; if (initialized++) return (0); if (qos_rtsock < 0) { int sockfd; sockfd = socket_open (PF_ROUTE, SOCK_RAW, 0); if (sockfd < 0) { trace (TR_ERROR, MRT->trace, "QIF qif_init (%m)\n"); return (sockfd); } qos_rtsock = sockfd; } memset (&rtm, 0, sizeof (rtm)); rtm.rtm_type = RTM_FLOW_INIT; rtm.rtm_msglen = sizeof (rtm); if ((ret = qif_sendmsg (&rtm)) < 0) { trace (TR_ERROR, MRT->trace, "QIF qif_init (%m)\n"); } trace (TR_INFO, MRT->trace, "QIF initialized\n"); select_add_fd_event ("qif_rtmsg_rcv", qos_rtsock, SELECT_READ, TRUE, NULL, qif_rtmsg_rcv, 1, qos_rtsock); return (ret); }
void init_socket_example(int port, uint8_t *ip) { int i = 0; int socket = socket_open(TCP_STREAM); struct sock_addr addr; addr.port = port; addr.ip = 0; for(i=0; i<4; i++) { addr.ip |= ip[i] << i*8; } char buffer[11]; // printf("ip is %x\n", addr.ip); socket_bind(socket, &addr); socket_listen(socket, 5); struct sock_addr client; // printf("Waiting for accept\n"); logger(SOCKET, NORMAL, "waiting on accept\n"); int new_socket = socket_accept(socket, &client); logger(SOCKET, NORMAL, "coming off accept\n"); socket_send(new_socket, "Hello World", 12); printf("waiting on socket read\n"); socket_read(new_socket, buffer, 10); printf("received from socket %s\n", buffer); socket_close(new_socket); // printf("accepted the connection\n"); }
LSEC_API int luaopen_ssl_core(lua_State *L) { /* Initialize SSL */ if (!SSL_library_init()) { lua_pushstring(L, "unable to initialize SSL library"); lua_error(L); } OpenSSL_add_all_algorithms(); SSL_load_error_strings(); #if defined(WITH_LUASOCKET) /* Initialize internal library */ socket_open(); #endif /* Register the functions and tables */ luaL_newmetatable(L, "SSL:Connection"); luaL_setfuncs(L, meta, 0); lua_newtable(L); luaL_setfuncs(L, methods, 0); lua_setfield(L, -2, "__index"); lua_newtable(L); luaL_setfuncs(L, funcs, 0); lua_pushnumber(L, SOCKET_INVALID); lua_setfield(L, -2, "invalidfd"); return 1; }
void init_socket_example(int port, uint8_t *ip) { int i = 0; int socket = socket_open(TCP_STREAM); struct sock_addr addr; addr.port = port; addr.ip = 0; for(i=0; i<4; i++) { addr.ip |= ip[i] << i*8; } // printf("ip is %x\n", addr.ip); #if 1 socket_bind(socket, &addr); socket_listen(socket, 5); struct sock_addr client; // printf("Waiting for accept\n"); logger(LOG_SOCKET, NORMAL, "waiting on accept\n"); pthread_t thread_id = 0; while(1) { int *socket_child = malloc(sizeof(int)); *socket_child = socket_accept(socket, &client); pthread_create(&thread_id, NULL, DoWork, socket_child); } #endif // printf("accepted the connection\n"); }
int UdmHTTPConnect(UDM_ENV * Conf, UDM_CONN *connp, char *hostname, int port, int timeout) { size_t len; if (!connp || !hostname || !port) return -1; connp->port= port; connp->timeout= timeout; len= strlen(hostname); connp->hostname= UdmXrealloc(connp->hostname, len+1); udm_snprintf(connp->hostname, len+1, "%s", hostname); if (UdmHostLookup(&Conf->Hosts,connp)) /* FIXME: not reentrant */ return -1; if (socket_open(connp)) return -1; if (socket_connect(connp)) return -1; return 0; }
/** * Initialize modules */ LUASEC_API int luaopen_ssl_core(lua_State *L) { /* Initialize SSL */ if (!SSL_library_init()) { lua_pushstring(L, "unable to initialize SSL library"); lua_error(L); } SSL_load_error_strings(); /* Initialize internal library */ socket_open(); /* Registre the functions and tables */ luaL_newmetatable(L, "SSL:Connection"); lua_newtable(L); luaL_register(L, NULL, meta); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, meth_destroy); lua_setfield(L, -2, "__gc"); luaL_register(L, "ssl.core", funcs); lua_pushnumber(L, SOCKET_INVALID); lua_setfield(L, -2, "invalidfd"); return 1; }
/*-------------------------------------------------------------------------*\ * Setup basic stuff. \*-------------------------------------------------------------------------*/ static int base_open(lua_State *L) { if (socket_open()) { /* export functions (and leave namespace table on top of stack) */ #if LUA_VERSION_NUM > 501 && !defined(LUA_COMPAT_MODULE) lua_newtable(L); luaL_setfuncs(L, func, 0); #else luaL_openlib(L, "socket", func, 0); #endif #ifdef LUASOCKET_DEBUG lua_pushstring(L, "_DEBUG"); lua_pushboolean(L, 1); lua_rawset(L, -3); #endif /* make version string available to scripts */ lua_pushstring(L, "_VERSION"); lua_pushstring(L, LUASOCKET_VERSION); lua_rawset(L, -3); return 1; } else { lua_pushstring(L, "unable to initialize library"); lua_error(L); return 0; } }
bool NanostackSocket::open(void) { nanostack_assert_locked(); MBED_ASSERT(SOCKET_MODE_UNOPENED == mode); int temp_socket = socket_open(proto, 0, socket_callback); if (temp_socket < 0) { tr_error("NanostackSocket::open() failed"); return false; } if (temp_socket >= NS_INTERFACE_SOCKETS_MAX) { MBED_ASSERT(false); return false; } if (socket_tbl[temp_socket] != NULL) { MBED_ASSERT(false); return false; } socket_id = temp_socket; socket_tbl[socket_id] = this; mode = SOCKET_MODE_OPENED; return true; }
/*-------------------------------------------------------------------------*\ * Setup basic stuff. \*-------------------------------------------------------------------------*/ static int base_open(lua_State *L) { if (socket_open()) { /* export functions (and leave namespace table on top of stack) */ lua_newtable(L); luaL_setfuncs(L,func,0); #ifdef LUASOCKET_DEBUG lua_pushstring(L, "_DEBUG"); lua_pushboolean(L, 1); lua_rawset(L, -3); #endif #ifdef INET_PTON lua_pushstring(L, "_BROKEN_XP"); lua_pushboolean(L, 1); lua_rawset(L, -3); #endif /* make version string available to scripts */ lua_pushstring(L, "_VERSION"); lua_pushstring(L, LUASOCKET_VERSION); lua_rawset(L, -3); return 1; } else { lua_pushstring(L, "unable to initialize library"); lua_error(L); return 0; } }
static void socket_Precheck(void) { if (sockethandler < 0) { /* Socket is not open yet, fix it */ socket_open(); } }
DEFINE_SYSCALL(socket, int, domain, int, type, int, protocol) { log_info("socket(domain=%d, type=0%o, protocol=%d)", domain, type, protocol); int fd = socket_open(domain, type, protocol); if (fd >= 0) log_info("socket fd: %d", fd); return fd; }
int connect_unix(const void *handle, size_t len, struct motmot_connect_cb *cb) { GIOChannel *chan; chan = socket_open((char *)handle, len, false); return cb->func(chan, cb->data); }
scm_socket_t make_socket(object_heap_t* heap, const char* node, const char* service, int family, int type, int protocol, int m_flags) { scm_socket_t obj = (scm_socket_t)heap->allocate_collectible(sizeof(scm_socket_rec_t)); memset(obj, 0 , sizeof(scm_socket_rec_t)); obj->hdr = scm_hdr_socket; obj->fd = INVALID_SOCKET; socket_open(obj, node, service, family, type, protocol, m_flags); return obj; }
int main (int argc, char** argv) { int server; server = socket_open(); socket_bind( server, PORT ); socket_listen( server, LISTEN ); socket_server( server ); return 0; }
int test_init(struct harness_t *harness_p) { struct socket_t socket; BTASSERT(socket_open(&socket, SOCKET_DOMAIN_AF_INET, SOCKET_TYPE_DGRAM, 0) == 0); return (0); }
static const char *obtain_cert(const char *hostname, const char *proto, unsigned port, const char *app_proto, unsigned quiet) { socket_st hd; char txt_port[16]; unsigned udp = 0; static char tmpfile[32]; int fd, ret; const char *str = "Obtaining certificate from"; const char *service; if (strcmp(proto, "udp") == 0) udp = 1; else if (strcmp(proto, "tcp") != 0) { /* we cannot handle this protocol */ return NULL; } strcpy(tmpfile, "danetool-certXXXXXX"); sockets_init(); snprintf(txt_port, sizeof(txt_port), "%u", port); if (quiet) str = NULL; service = port_to_service(txt_port, proto); socket_open(&hd, hostname, service, udp, str); if (app_proto == NULL) app_proto = service; socket_starttls(&hd, app_proto); umask(066); fd = mkstemp(tmpfile); if (fd == -1) { int e = errno; fprintf(stderr, "error[%d]: %s\n", __LINE__, strerror(e)); exit(1); } ret = get_cert(&hd, hostname, udp, fd); close(fd); socket_bye(&hd); if (ret == -1) return NULL; else return tmpfile; }
int Dps_ftp_open_control_port(DPS_AGENT *Agent, DPS_CONN *connp) { int code; if (DpsHostLookup(Agent, connp)) return -1; if (socket_open(connp)) return -1; if (socket_connect(connp)) return -1; /* Read server response */ Dps_ftp_read_line(connp); code = Dps_ftp_get_reply(connp); if (code != 2) return -1; return 0; }
dnscache_t * dnscache_create(char *server_addr, uint16_t port, uint32_t thread_num) { dnscache_t *dnscache = malloc(sizeof(dnscache_t)); if (!dnscache) return NULL; dnscache->thread_count_ = thread_num; dnscache->base_event_ = event_base_new(); dnscache_init_sig(dnscache); dnscache->tp_ = thread_pool_create(thread_num); ASSERT(dnscache->tp_, "thread pool create failed"); addr_t addr; addr_init(&addr, server_addr, port); dnscache->listen_socket = (socket_t *)malloc(sizeof(socket_t)); int socket_type = addr_get_type(&addr) == ADDR_IPV4 ? AF_INET : AF_INET6; socket_open(dnscache->listen_socket, socket_type, SOCK_DGRAM, 0); socket_set_unblock(dnscache->listen_socket, true); socket_set_addr_reusable(dnscache->listen_socket); int ret = socket_bind(dnscache->listen_socket, &addr); ASSERT(ret == 0, "udp server bind failed\n"); int threads_len = thread_num * sizeof(thread_local_param_t *); dnscache->thread_params_ = (thread_local_param_t **)malloc(threads_len); ASSERT(dnscache->thread_params_ != NULL, "udp server bind failed\n"); int i = 0; for (; i < thread_num; ++i) { dnscache->thread_params_[i] = (thread_local_param_t *)malloc(sizeof(thread_local_param_t)); thread_local_param_t *tlp = dnscache->thread_params_[i]; tlp->thread_id_ = i; tlp->base_ = event_base_new(); tlp->dns_server_ = dns_server_create(tlp->base_, UDP_SERVER, dnscache->listen_socket, queue_size); thread_pool_set_thread_data(dnscache->tp_, i, tlp); } return dnscache; }
int iocp_server::open() { if(!socket_open()) { socket_close(); return 0; } SYSTEM_INFO sys; GetSystemInfo(&sys); if(!boss_open(sys.dwNumberOfProcessors * 2)) { socket_close(); boss_close(); return 0; } if(!thread_listen()) return 0; return 1; }
/* ** Open a connection to the server. The server is defined by the following ** global variables: ** ** g.urlName Name of the server. Ex: www.fossil-scm.org ** g.urlPort TCP/IP port. Ex: 80 ** g.urlIsHttps Use TLS for the connection ** ** Return the number of errors. */ int transport_open(void){ int rc = 0; if( transport.isOpen==0 ){ if( g.urlIsSsh ){ Blob cmd; blob_zero(&cmd); shell_escape(&cmd, g.urlFossil); blob_append(&cmd, " test-http ", -1); shell_escape(&cmd, g.urlPath); /* fprintf(stdout, "%s\n", blob_str(&cmd)); */ fprintf(sshOut, "%s\n", blob_str(&cmd)); fflush(sshOut); blob_reset(&cmd); }else if( g.urlIsHttps ){ #ifdef FOSSIL_ENABLE_SSL rc = ssl_open(); if( rc==0 ) transport.isOpen = 1; #else socket_set_errmsg("HTTPS: Fossil has been compiled without SSL support"); rc = 1; #endif }else if( g.urlIsFile ){ sqlite3_uint64 iRandId; sqlite3_randomness(sizeof(iRandId), &iRandId); transport.zOutFile = mprintf("%s-%llu-out.http", g.zRepositoryName, iRandId); transport.zInFile = mprintf("%s-%llu-in.http", g.zRepositoryName, iRandId); transport.pFile = fopen(transport.zOutFile, "wb"); if( transport.pFile==0 ){ fossil_fatal("cannot output temporary file: %s", transport.zOutFile); } transport.isOpen = 1; }else{ rc = socket_open(); if( rc==0 ) transport.isOpen = 1; } } return rc; }
int nntp_connect( int *sockfd, const char *hostname, int port, const char *username, const char *password ) { *sockfd = socket_open( hostname, port ); // Check if the socket opened correctly if ( *sockfd == SOCKET_UNKNOWN_HOST ) { printf( "Error opening socket: Unknown host.\n" ); return CONNECT_FAILURE; } else if ( *sockfd <= 0 ) return CONNECT_TRY_AGAIN_LATER; // Now let's try to log onto the server int ret = nntp_logon( *sockfd, username, password ); if ( ret == SOCKET_FAILED || ret == SOCKET_EMPTY || ret == SOCKET_TRY_LATER ) { // TODO: If we get this error, we should avoid making any new connections // e.g. if at the 21st connection the server tells us we reached our connection // limit, that 21st connection will fail. However, 22 and beyond will connect fine // but might result in problems while posting. close( *sockfd ); printf( "Failed to log onto server, trying again in 120 seconds.\n" ); *sockfd = ret; return CONNECT_TRY_AGAIN_LATER; } else if( ret < 0 ) { close( *sockfd ); *sockfd = ret; printf( "Failed to log onto server, see you~.\n" ); return CONNECT_FAILURE; } return 0; }
int Dps_ftp_open_data_port( DPS_CONN *c, DPS_CONN *d){ char buf[64]; unsigned char *a, *p; int code; if (!d) return -1; if (socket_getname(c, &d->sin) == -1) return -1; if (d->port) { d->sin.sin_port = htons(d->port); } if (socket_open(d)) return -1; if (socket_listen(d)){ return -1; } if (socket_getname(d, &d->sin) == -1){ return -1; } a = (unsigned char *)&d->sin.sin_addr; p = (unsigned char *)&d->sin.sin_port; dps_snprintf(buf, 64, "PORT %d,%d,%d,%d,%d,%d", a[0], a[1], a[2], a[3], p[0], p[1]); code = Dps_ftp_send_cmd(c, buf); if ((code < 0) || strncasecmp(c->buf, "200 ", 4)){ return -1; } d->user = c->user; d->pass = c->pass; return 0; }
bool NanostackSocket::open(void) { nanostack_assert_locked(); MBED_ASSERT(SOCKET_MODE_UNOPENED == mode); int temp_socket = socket_open(proto, 0, socket_callback); if (temp_socket < 0) { tr_error("NanostackSocket::open() failed"); return false; } if (proto == SOCKET_TCP) { /* Receive and send buffers enabled by default */ mode = SOCKET_MODE_OPENED; } else { static const int32_t rcvbuf_size = 2048; socket_setsockopt(temp_socket, SOCKET_SOL_SOCKET, SOCKET_SO_RCVBUF, &rcvbuf_size, sizeof rcvbuf_size); mode = SOCKET_MODE_DATAGRAM; } return attach(temp_socket); }
void init_socket_bridge_example(int port, uint8_t *ip) { int i = 0; int socket = socket_open(TCP_BRIDGE); struct sock_bridge_addr bridge_addr; bridge_addr.m_PortA = 0; bridge_addr.m_PortB = 1; bridge_addr.m_FuncA = TcpDataA; bridge_addr.m_FuncB = TcpDataB; // printf("ip is %x\n", addr.ip); sock_bridge_bind(bridge_addr); struct sock_addr client; // printf("Waiting for accept\n"); logger(SOCKET, NORMAL, "waiting on accept\n"); pthread_attr_t attr; int thread_id = 0; while(1) { int *socket_child = malloc(sizeof(int)); *socket_child = socket_accept(socket, &client); pthread_create(&thread_id, NULL, DoWork2, socket_child); } // printf("accepted the connection\n"); }
int qif_close (void) { int ret; struct rt_msghdr rtm; if (qos_rtsock < 0) { int sockfd; sockfd = socket_open (PF_ROUTE, SOCK_RAW, 0); if (sockfd < 0) { trace (TR_ERROR, MRT->trace, "QIF qif_close (%m)\n"); return (sockfd); } qos_rtsock = sockfd; } memset (&rtm, 0, sizeof (rtm)); rtm.rtm_type = RTM_FLOW_CLOSE; rtm.rtm_msglen = sizeof (rtm); if ((ret = qif_sendmsg (&rtm)) < 0) { /* trace (TR_ERROR, MRT->trace, "QIF qif_close (%m)\n"); */ } return (ret); }
static void reply_to_end_user(dns_client_error_code_t error_code, const char *dns_raw_data, uint16_t raw_data_len, const addr_t *name_server_addr, void *arg) { query_session_t *session = (query_session_t *)arg; if (error_code != QUERY_NO_ERROR) { log_warning(QUERY_LOG, "has error during query backend named server\n"); memorypool_free_node(session->zipper_->query_session_pool_, session); return; } socket_write_to(&session->client_socket_, (uint8_t *)dns_raw_data, raw_data_len, &session->client_addr_); /* -----------------test for dns cache ---------------*/ socket_t client_socket; socket_open(&client_socket, AF_INET, SOCK_DGRAM, 0); socket_set_unblock(&client_socket, true); addr_t server_addr; addr_init(&server_addr, dnscache_ip, dnscache_port); if (socket_connect(&client_socket, &server_addr) == 0) { socket_write(&client_socket, (uint8_t *)dns_raw_data, raw_data_len); } socket_close(&client_socket); /*---------------------------------------------------*/ memorypool_free_node(session->zipper_->query_session_pool_, session); }
int mesh_start_listening(Socket *mesh_listen_socket, uint16_t mesh_listen_socket_port, SocketCreateAllocatedFunction create_allocated) { int phase = 0; struct addrinfo *resolved_address = NULL; const char *address = config_get_option_value("listen.address")->string; log_info("Opening mesh listen socket (P: %u)", mesh_listen_socket_port); resolved_address = socket_hostname_to_address(address, mesh_listen_socket_port); // Currently no IPv6 support for mesh. if(resolved_address->ai_family == AF_INET6) { log_error("Mesh gateway does not support IPv6"); goto CLEANUP; } if (resolved_address == NULL) { log_error("Could not resolve mesh listen address '%s' (P: %u): %s (%d)", address, mesh_listen_socket_port, get_errno_name(errno), errno); goto CLEANUP; } phase = 1; // Create socket. if (socket_create(mesh_listen_socket) < 0) { log_error("Could not create mesh listen socket: %s (%d)", get_errno_name(errno), errno); goto CLEANUP; } phase = 2; if (socket_open(mesh_listen_socket, resolved_address->ai_family, resolved_address->ai_socktype, resolved_address->ai_protocol) < 0) { log_error("Could not open %s mesh listen socket: %s (%d)", network_get_address_family_name(resolved_address->ai_family, false), get_errno_name(errno), errno); goto CLEANUP; } #ifndef _WIN32 /* * On Unix the SO_REUSEADDR socket option allows to rebind sockets in * CLOSE-WAIT state. this is a desired effect. On Windows SO_REUSEADDR * allows to rebind sockets in any state. This is dangerous. Therefore, * don't set SO_REUSEADDR on Windows. Sockets can be rebound in CLOSE-WAIT * state on Windows by default. */ if (socket_set_address_reuse(mesh_listen_socket, true) < 0) { log_error("Could not enable address-reuse mode for mesh listen socket: %s (%d)", get_errno_name(errno), errno); goto CLEANUP; } #endif // Bind socket and start to listen. if (socket_bind(mesh_listen_socket, resolved_address->ai_addr, resolved_address->ai_addrlen) < 0) { log_error("Could not bind %s mesh listen socket to (A: %s, P: %u): %s (%d)", network_get_address_family_name(resolved_address->ai_family, true), address, mesh_listen_socket_port, get_errno_name(errno), errno); goto CLEANUP; } if (socket_listen(mesh_listen_socket, 10, create_allocated) < 0) { log_error("Could not listen to %s mesh socket (A: %s, P: %u): %s (%d)", network_get_address_family_name(resolved_address->ai_family, true), address, mesh_listen_socket_port, get_errno_name(errno), errno); goto CLEANUP; } log_info("Mesh gateway started listening on (A: %s, P: %u, F: %s)", address, mesh_listen_socket_port, network_get_address_family_name(resolved_address->ai_family, true)); if(event_add_source(mesh_listen_socket->base.handle, EVENT_SOURCE_TYPE_GENERIC, EVENT_READ, mesh_handle_accept, mesh_listen_socket) < 0) { goto CLEANUP; } phase = 3; freeaddrinfo(resolved_address); CLEANUP: switch(phase) { // No breaks, all cases fall through intentionally. case 2: socket_destroy(mesh_listen_socket); case 1: freeaddrinfo(resolved_address); default: break; } return phase == 3 ? 0 : -1; }