GVfsFtpConnection * g_vfs_ftp_connection_new (GSocketConnectable *addr, GCancellable * cancellable, GError ** error) { GVfsFtpConnection *conn; g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (addr), NULL); conn = g_slice_new0 (GVfsFtpConnection); conn->client = g_socket_client_new (); conn->debug_id = g_atomic_int_add (&debug_id, 1); conn->commands = G_IO_STREAM (g_socket_client_connect (conn->client, addr, cancellable, error)); if (conn->commands == NULL) { g_object_unref (conn->client); g_slice_free (GVfsFtpConnection, conn); return NULL; } conn->connection = G_SOCKET_CONNECTION (conn->commands); enable_nodelay (conn->connection); enable_keepalive (conn->connection); create_input_stream (conn); /* The first thing that needs to happen is receiving the welcome message */ conn->waiting_for_reply = TRUE; return conn; }
int local_connect_arbitrary_ports(int console_port, int adb_port) { char buf[64]; int fd = -1; #if ADB_HOST const char *host = getenv("ADBHOST"); if (host) { fd = socket_network_client(host, adb_port, SOCK_STREAM); } #endif if (fd < 0) { fd = socket_loopback_client(adb_port, SOCK_STREAM); } if (fd >= 0) { D("client: connected on remote on fd %d\n", fd); close_on_exec(fd); disable_tcp_nagle(fd); enable_keepalive(fd); snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port); register_socket_transport(fd, buf, adb_port, 1); return 0; } return -1; }
GVfsFtpConnection * g_vfs_ftp_connection_new (GSocketConnectable *addr, GCancellable * cancellable, GError ** error) { GVfsFtpConnection *conn; g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (addr), NULL); conn = g_slice_new0 (GVfsFtpConnection); conn->client = g_socket_client_new (); conn->debug_id = g_atomic_int_exchange_and_add (&debug_id, 1); conn->commands = G_IO_STREAM (g_socket_client_connect (conn->client, addr, cancellable, error)); if (conn->commands == NULL) { g_object_unref (conn->client); g_slice_free (GVfsFtpConnection, conn); return NULL; } enable_keepalive (G_SOCKET_CONNECTION (conn->commands)); conn->commands_in = G_DATA_INPUT_STREAM (g_data_input_stream_new (g_io_stream_get_input_stream (conn->commands))); g_data_input_stream_set_newline_type (conn->commands_in, G_DATA_STREAM_NEWLINE_TYPE_CR_LF); /* The first thing that needs to happen is receiving the welcome message */ conn->waiting_for_reply = TRUE; return conn; }
/* * Returns a connected socket() fd, or else die()s. */ static int git_tcp_connect_sock(char *host, int flags) { struct strbuf error_message = STRBUF_INIT; int sockfd = -1; const char *port = STR(DEFAULT_GIT_PORT); struct addrinfo hints, *ai0, *ai; int gai; int cnt = 0; get_host_and_port(&host, &port); if (!*port) port = "<none>"; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; if (flags & CONNECT_VERBOSE) fprintf(stderr, "Looking up %s ... ", host); gai = getaddrinfo(host, port, &hints, &ai); if (gai) die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai)); if (flags & CONNECT_VERBOSE) fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if ((sockfd < 0) || (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", host, cnt, ai_name(ai), strerror(errno)); if (0 <= sockfd) close(sockfd); sockfd = -1; continue; } if (flags & CONNECT_VERBOSE) fprintf(stderr, "%s ", ai_name(ai)); break; } freeaddrinfo(ai0); if (sockfd < 0) die("unable to connect to %s:\n%s", host, error_message.buf); enable_keepalive(sockfd); if (flags & CONNECT_VERBOSE) fprintf(stderr, "done.\n"); strbuf_release(&error_message); return sockfd; }
/* * Accept a new Telnet connection. */ static void telnet_accept(void *vp) { ndesc_t *nd = vp; int addrlen, s; struct sockaddr_in addr; telnet_t *tp; void *ip; nd_enable(nd, ND_R); addrlen = sizeof (addr); s = accept(nd_fd(nd), (struct sockaddr *)&addr, &addrlen); if (s == -1) { switch (errno) { default: warning("telnet_accept: accept() errno = %d. ip: %s\n", errno, inet_ntoa(addr.sin_addr)); case EWOULDBLOCK: case EINTR: case EPROTO: return; } } enable_nbio(s); enable_oobinline(s); enable_nodelay(s); enable_lowdelay(s); enable_keepalive(s); set_rcvsize(s, TELNET_RCVBUF_SIZE); set_sndsize(s, TELNET_SNDBUF_SIZE); tp = telnet_alloc(); tp->t_nd = nd_attach(s, telnet_read, telnet_write, telnet_exception, NULL, telnet_shutdown, tp); ip = (void *)new_player(tp, &addr, addrlen); if (ip == NULL) { telnet_shutdown(tp->t_nd, tp); } else { telnet_attach(tp, ip); nd_enable(tp->t_nd, ND_R | ND_X); } }
static int connect_device(char* host, char* buffer, int buffer_size) { int port, fd; char* portstr = strchr(host, ':'); char hostbuf[100]; char serial[100]; int ret; strncpy(hostbuf, host, sizeof(hostbuf) - 1); if (portstr) { if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) { snprintf(buffer, buffer_size, "bad host name %s", host); return 0; } // zero terminate the host at the point we found the colon hostbuf[portstr - host] = 0; if (sscanf(portstr + 1, "%d", &port) == 0) { snprintf(buffer, buffer_size, "bad port number %s", portstr); return 0; } } else { port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; } snprintf(serial, sizeof(serial), "%s:%d", hostbuf, port); fd = socket_network_client_timeout(hostbuf, port, SOCK_STREAM, 10); if (fd < 0) { snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port); return 0; } D("client: connected on remote on fd %d\n", fd); close_on_exec(fd); disable_tcp_nagle(fd); enable_keepalive(fd); ret = register_socket_transport(fd, serial, port, 0); if (ret < 0) { adb_close(fd); snprintf(buffer, buffer_size, "already connected to %s", serial); return 0; } else { snprintf(buffer, buffer_size, "connected to %s", serial); } return 1; }
static void *server_socket_thread(void * arg) { int serverfd, fd; struct sockaddr addr; socklen_t alen; int port = (int)arg; D("transport: server_socket_thread() starting\n"); serverfd = -1; for(;;) { if(serverfd == -1) { serverfd = socket_inaddr_any_server(port, SOCK_STREAM); if(serverfd < 0) { D("server: cannot bind socket yet\n"); adb_sleep_ms(1000); continue; } close_on_exec(serverfd); } alen = sizeof(addr); D("server: trying to get new connection from %d\n", port); fd = adb_socket_accept(serverfd, &addr, &alen); if(fd >= 0) { D("server: new connection on fd %d\n", fd); #if !ADB_HOST get_wakelock(); #endif close_on_exec(fd); disable_tcp_nagle(fd); enable_keepalive(fd); register_socket_transport(fd, "host", port, 1); } } D("transport: server_socket_thread() exiting\n"); return 0; }
/* * Returns a connected socket() fd, or else die()s. */ static int git_tcp_connect_sock(char *host, int flags) { struct strbuf error_message = STRBUF_INIT; int sockfd = -1; const char *port = STR(DEFAULT_GIT_PORT); char *ep; struct hostent *he; struct sockaddr_in sa; char **ap; unsigned int nport; int cnt; get_host_and_port(&host, &port); if (flags & CONNECT_VERBOSE) fprintf(stderr, "Looking up %s ... ", host); he = gethostbyname(host); if (!he) die("Unable to look up %s (%s)", host, hstrerror(h_errno)); nport = strtoul(port, &ep, 10); if ( ep == port || *ep ) { /* Not numeric */ struct servent *se = getservbyname(port,"tcp"); if ( !se ) die("Unknown port %s", port); nport = se->s_port; } if (flags & CONNECT_VERBOSE) fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { memset(&sa, 0, sizeof sa); sa.sin_family = he->h_addrtype; sa.sin_port = htons(nport); memcpy(&sa.sin_addr, *ap, he->h_length); sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); if ((sockfd < 0) || connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", host, cnt, inet_ntoa(*(struct in_addr *)&sa.sin_addr), strerror(errno)); if (0 <= sockfd) close(sockfd); sockfd = -1; continue; } if (flags & CONNECT_VERBOSE) fprintf(stderr, "%s ", inet_ntoa(*(struct in_addr *)&sa.sin_addr)); break; } if (sockfd < 0) die("unable to connect to %s:\n%s", host, error_message.buf); enable_keepalive(sockfd); if (flags & CONNECT_VERBOSE) fprintf(stderr, "done.\n"); return sockfd; }
void M2MConnectionHandlerPimpl::dns_handler() { palStatus_t status; palSocketLength_t _socket_address_len; tr_debug("M2MConnectionHandlerPimpl::dns_handler - _socket_state = %d", _socket_state); switch (_socket_state) { case ESocketStateConnectBeingCalled: case ESocketStateCloseBeingCalled: // Ignore these events break; case ESocketStateDisconnected: // Initialize the socket to stable state close_socket(); status = pal_getAddressInfo(_server_address.c_str(), &_socket_address, &_socket_address_len); if (PAL_SUCCESS != status) { tr_error("addrInfo, err: %d", (int)status); _observer.socket_error(M2MConnectionHandler::DNS_RESOLVING_ERROR); return; } status = pal_setSockAddrPort(&_socket_address, _server_port); if (PAL_SUCCESS != status) { tr_error("setSockAddrPort err: %d", (int)status); } else { tr_debug("address family: %d", (int)_socket_address.addressType); } if (_socket_address.addressType == PAL_AF_INET) { status = pal_getSockAddrIPV4Addr(&_socket_address,_ipV4Addr); if (PAL_SUCCESS != status) { tr_error("sockAddr4, err: %d", (int)status); _observer.socket_error(M2MConnectionHandler::DNS_RESOLVING_ERROR); return; } tr_debug("IPv4 Address %s", tr_array(_ipV4Addr, 4)); _address._address = (void*)_ipV4Addr; _address._length = PAL_IPV4_ADDRESS_SIZE; _address._port = _server_port; } else if (_socket_address.addressType == PAL_AF_INET6) { status = pal_getSockAddrIPV6Addr(&_socket_address,_ipV6Addr); if (PAL_SUCCESS != status) { tr_error("sockAddr6, err: %d", (int)status); _observer.socket_error(M2MConnectionHandler::DNS_RESOLVING_ERROR); return; } tr_debug("IPv6 Address %s", tr_array(_ipV6Addr,sizeof(_ipV6Addr))); _address._address = (void*)_ipV6Addr; _address._length = PAL_IPV6_ADDRESS_SIZE; _address._port = _server_port; } else { tr_error("socket config error, stack: %d", (int)_socket_address.addressType); _observer.socket_error(M2MConnectionHandler::SOCKET_ABORT); return; } if(!init_socket()) { tr_error("socket init error"); // The init_socket() calls the socket_error() -callback directly, so it must not be // done here too. return; } if(is_tcp_connection()) { #ifdef PAL_NET_TCP_AND_TLS_SUPPORT tr_debug("resolve_server_address - Using TCP"); // At least on mbed-os the pal_connect() will perform callbacks even during it // is called, which we will ignore when this state is set. _socket_state = ESocketStateConnectBeingCalled; status = pal_connect(_socket, &_socket_address, sizeof(_socket_address)); if (status == PAL_ERR_SOCKET_IN_PROGRES) { // In this case the connect is done asynchronously, and the pal_socketMiniSelect() // will be used to detect the end of connect. // XXX: the mbed-os version of PAL has a bug (IOTPAL-228) open that the select // does not necessarily work correctly. So, should we actually handle // the PAL_ERR_SOCKET_IN_PROGRESS as a error here if code is compiled for mbed-os? tr_debug("pal_connect(): %d, async connect started", (int)status); // we need to wait for the event _socket_state = ESocketStateConnecting; break; } else if (status == PAL_SUCCESS) { tr_info("pal_connect(): success"); _running = true; _socket_state = ESocketStateConnected; } else { tr_error("pal_connect(): failed: %d", (int)status); close_socket(); _observer.socket_error(M2MConnectionHandler::SOCKET_ABORT); return; } #else tr_error("dns_handler() - TCP not configured" #endif //PAL_NET_TCP_AND_TLS_SUPPORT } else { tr_debug("resolve_server_address - Using UDP"); _socket_state = ESocketStateConnected; _running = true; } // fall through is a normal flow in case the UDP was used or pal_connect() happened to return immediately with PAL_SUCCESS case ESocketStateConnected: if (_security) { if (_security->resource_value_int(M2MSecurity::SecurityMode) == M2MSecurity::Certificate || _security->resource_value_int(M2MSecurity::SecurityMode) == M2MSecurity::Psk) { if( _security_impl != NULL ){ _security_impl->reset(); if (_security_impl->init(_security) == 0) { _is_handshaking = true; tr_debug("resolve_server_address - connect DTLS"); if(_security_impl->start_connecting_non_blocking(_base) < 0 ){ tr_debug("dns_handler - handshake failed"); _is_handshaking = false; close_socket(); _observer.socket_error(M2MConnectionHandler::SSL_CONNECTION_ERROR); return; } } else { tr_error("resolve_server_address - init failed"); close_socket(); _observer.socket_error(M2MConnectionHandler::SSL_CONNECTION_ERROR, false); return; } } else { tr_error("dns_handler - sec is null"); close_socket(); _observer.socket_error(M2MConnectionHandler::SSL_CONNECTION_ERROR, false); return; } } } if(!_is_handshaking) { enable_keepalive(); _observer.address_ready(_address, _server_type, _address._port); } break; // This case is a continuation of a nonblocking connect() and is skipped // completely on UDP. case ESocketStateConnecting: // there is only one socket which we are interested uint8_t socketStatus[1]; pal_timeVal_t zeroTime = {0, 0}; uint32_t socketsSet = 0; status = pal_socketMiniSelect(&_socket, 1, &zeroTime, socketStatus, &socketsSet); if (status != PAL_SUCCESS) { // XXX: how could this fail? What to do? tr_error("dns_handler() - read select fail, err: %d", (int)status); close_socket(); // this will also set the socket state to disconnect // XXX: should we inform the observer here too? return; } if (socketsSet > 0) { if (PAL_NET_SELECT_IS_TX(socketStatus, 0)) { // Socket is connected, signal the dns_handler() again to run rest of the steps tr_debug("dns_handler() - connect+select succeeded"); _socket_state = ESocketStateConnected; send_dns_event(); } else if (PAL_NET_SELECT_IS_ERR(socketStatus, 0)) { tr_error("dns_handler() - connect+select failed"); close_socket(); // this will also set the socket state to disconnect // XXX: should we inform the observer here too? } else { tr_debug("dns_handler() - connect+select not ready yet, continue waiting"); } } break; }
int service_to_fd(const char *name) { int ret = -1; if(!strncmp(name, "tcp:", 4)) { int port = atoi(name + 4); name = strchr(name + 4, ':'); if(name == 0) { ret = socket_loopback_client(port, SOCK_STREAM); if (ret >= 0) { disable_tcp_nagle(ret); enable_keepalive(ret); } } else { #if ADB_HOST ret = socket_network_client(name + 1, port, SOCK_STREAM); #else return -1; #endif } #ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */ } else if(!strncmp(name, "local:", 6)) { ret = socket_local_client(name + 6, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); } else if(!strncmp(name, "localreserved:", 14)) { ret = socket_local_client(name + 14, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); } else if(!strncmp(name, "localabstract:", 14)) { ret = socket_local_client(name + 14, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); } else if(!strncmp(name, "localfilesystem:", 16)) { ret = socket_local_client(name + 16, ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); #endif #if !ADB_HOST } else if(!strncmp("dev:", name, 4)) { ret = unix_open(name + 4, O_RDWR); } else if(!strncmp(name, "framebuffer:", 12)) { ret = create_service_thread(framebuffer_service, 0); } else if (!strncmp(name, "jdwp:", 5)) { ret = create_jdwp_connection_fd(atoi(name+5)); } else if (!strncmp(name, "log:", 4)) { ret = create_service_thread(log_service, get_log_file_path(name + 4)); } else if(!HOST && !strncmp(name, "shell:", 6)) { if(name[6]) { ret = create_subproc_thread(name + 6); } else { ret = create_subproc_thread(0); } } else if(!strncmp(name, "sync:", 5)) { ret = create_service_thread(file_sync_service, NULL); } else if(!strncmp(name, "remount:", 8)) { ret = create_service_thread(remount_service, NULL); } else if(!strncmp(name, "reboot:", 7)) { void* arg = strdup(name + 7); if(arg == 0) return -1; ret = create_service_thread(reboot_service, arg); } else if(!strncmp(name, "root:", 5)) { ret = create_service_thread(restart_root_service, NULL); } else if(!strncmp(name, "backup:", 7)) { char* arg = strdup(name+7); if (arg == NULL) return -1; ret = backup_service(BACKUP, arg); } else if(!strncmp(name, "restore:", 8)) { ret = backup_service(RESTORE, NULL); } else if(!strncmp(name, "tcpip:", 6)) { int port; if (sscanf(name + 6, "%d", &port) == 0) { port = 0; } ret = create_service_thread(restart_tcp_service, (void *)port); } else if(!strncmp(name, "usb:", 4)) { ret = create_service_thread(restart_usb_service, NULL); #endif } if (ret >= 0) { close_on_exec(ret); } return ret; }