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"); sdb_sleep_ms(1000); continue; } close_on_exec(serverfd); } alen = sizeof(addr); D("server: trying to get new connection from %d\n", port); fd = sdb_socket_accept(serverfd, &addr, &alen); if(fd >= 0) { D("server: new connection on fd %d\n", fd); close_on_exec(fd); disable_tcp_nagle(fd); register_socket_transport(fd, "host", port, 1, NULL); } } D("transport: server_socket_thread() exiting\n"); return 0; }
static void server_socket_thread(void* arg) { int serverfd, fd; sockaddr_storage ss; sockaddr *addrp = reinterpret_cast<sockaddr*>(&ss); socklen_t alen; int port = (int) (uintptr_t) arg; adb_thread_setname("server socket"); D("transport: server_socket_thread() starting"); serverfd = -1; for(;;) { if(serverfd == -1) { std::string error; serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error); if(serverfd < 0) { D("server: cannot bind socket yet: %s", error.c_str()); adb_sleep_ms(1000); continue; } close_on_exec(serverfd); } alen = sizeof(ss); D("server: trying to get new connection from %d", port); fd = adb_socket_accept(serverfd, addrp, &alen); if(fd >= 0) { D("server: new connection on fd %d", fd); close_on_exec(fd); disable_tcp_nagle(fd); register_socket_transport(fd, "host", port, 1); } } D("transport: server_socket_thread() exiting"); }
int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name) { char buf[64]; int fd = -1; #if SDB_HOST const char *host = getenv("SDBHOST"); if (host) { fd = socket_network_client(host, sdb_port, SOCK_STREAM); } #endif if (fd < 0) { fd = socket_loopback_client(sdb_port, SOCK_STREAM); } if (fd >= 0) { D("client: connected on remote on fd %d\n", fd); close_on_exec(fd); disable_tcp_nagle(fd); snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port); register_socket_transport(fd, buf, sdb_port, 1, device_name); return 0; } return -1; }
static void connect_device(const std::string& address, std::string* response) { if (address.empty()) { *response = "empty address"; return; } std::string serial; std::string host; int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; if (!parse_host_and_port(address, &serial, &host, &port, response)) { return; } std::string error; int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error); if (fd == -1) { *response = android::base::StringPrintf("unable to connect to %s: %s", serial.c_str(), error.c_str()); return; } D("client: connected %s remote on fd %d", serial.c_str(), fd); close_on_exec(fd); disable_tcp_nagle(fd); int ret = register_socket_transport(fd, serial.c_str(), port, 0); if (ret < 0) { adb_close(fd); *response = android::base::StringPrintf("already connected to %s", serial.c_str()); } else { *response = android::base::StringPrintf("connected to %s", serial.c_str()); } }
int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) { int fd = -1; #if ADB_HOST if (find_emulator_transport_by_adb_port(adb_port) != nullptr) { return -1; } const char *host = getenv("ADBHOST"); if (host) { fd = network_connect(host, adb_port, SOCK_STREAM, 0, error); } #endif if (fd < 0) { fd = network_loopback_client(adb_port, SOCK_STREAM, error); } if (fd >= 0) { D("client: connected on remote on fd %d", fd); close_on_exec(fd); disable_tcp_nagle(fd); std::string serial = android::base::StringPrintf("emulator-%d", console_port); if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) { return 0; } adb_close(fd); } return -1; }
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); snprintf(buf, sizeof buf, "emulator-%d", console_port); register_socket_transport(fd, buf, adb_port, 1); return 0; } return -1; }
static void server_socket_thread(void* arg) { int serverfd, fd; int port = (int) (uintptr_t) arg; adb_thread_setname("server socket"); D("transport: server_socket_thread() starting"); serverfd = -1; for(;;) { if(serverfd == -1) { std::string error; serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error); if(serverfd < 0) { D("server: cannot bind socket yet: %s", error.c_str()); std::this_thread::sleep_for(1s); continue; } close_on_exec(serverfd); } D("server: trying to get new connection from %d", port); fd = adb_socket_accept(serverfd, nullptr, nullptr); if(fd >= 0) { D("server: new connection on fd %d", fd); close_on_exec(fd); disable_tcp_nagle(fd); std::string serial = android::base::StringPrintf("host-%d", fd); if (register_socket_transport(fd, serial.c_str(), port, 1) != 0) { adb_close(fd); } } } D("transport: server_socket_thread() exiting"); }
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; }
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); } else { adb_mutex_lock(&dns_lock); ret = socket_network_client(name + 1, port, SOCK_STREAM); adb_mutex_unlock(&dns_lock); } #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 } else if(!strncmp("dns:", name, 4)){ char *n = strdup(name + 4); if(n == 0) return -1; ret = create_service_thread(dns_service, n); #if 0 } else if(!strncmp(name, "echo:", 5)){ ret = create_service_thread(echo_service, 0); #endif } if (ret >= 0) { close_on_exec(ret); } return ret; }
static void connect_device(const std::string& host, std::string* response) { if (host.empty()) { *response = "empty host name"; return; } std::vector<std::string> pieces = android::base::Split(host, ":"); const std::string& hostname = pieces[0]; int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; if (pieces.size() > 1) { if (sscanf(pieces[1].c_str(), "%d", &port) != 1) { *response = android::base::StringPrintf("bad port number %s", pieces[1].c_str()); return; } } // This may look like we're putting 'host' back together, // but we're actually inserting the default port if necessary. std::string serial = android::base::StringPrintf("%s:%d", hostname.c_str(), port); int fd = socket_network_client_timeout(hostname.c_str(), port, SOCK_STREAM, 10); if (fd < 0) { *response = android::base::StringPrintf("unable to connect to %s:%d", hostname.c_str(), port); return; } D("client: connected on remote on fd %d\n", fd); close_on_exec(fd); disable_tcp_nagle(fd); int ret = register_socket_transport(fd, serial.c_str(), port, 0); if (ret < 0) { adb_close(fd); *response = android::base::StringPrintf("already connected to %s", serial.c_str()); } else { *response = android::base::StringPrintf("connected to %s", serial.c_str()); } }
int service_to_fd(const char* name, const atransport* transport) { int ret = -1; if(!strncmp(name, "tcp:", 4)) { int port = atoi(name + 4); name = strchr(name + 4, ':'); if(name == 0) { std::string error; ret = network_loopback_client(port, SOCK_STREAM, &error); if (ret >= 0) disable_tcp_nagle(ret); } else { #if ADB_HOST std::string error; ret = network_connect(name + 1, port, SOCK_STREAM, 0, &error); #else return -1; #endif } #if !defined(_WIN32) /* 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 | O_CLOEXEC); } 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, "shell", 5)) { ret = ShellService(name + 5, transport); } else if(!strncmp(name, "exec:", 5)) { ret = StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); } 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 == NULL) 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, "unroot:", 7)) { ret = create_service_thread(restart_unroot_service, NULL); } else if(!strncmp(name, "backup:", 7)) { ret = StartSubprocess(android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(), nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); } else if(!strncmp(name, "restore:", 8)) { ret = StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone); } else if(!strncmp(name, "tcpip:", 6)) { int port; if (sscanf(name + 6, "%d", &port) != 1) { return -1; } ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port); } else if(!strncmp(name, "usb:", 4)) { ret = create_service_thread(restart_usb_service, NULL); } else if (!strncmp(name, "reverse:", 8)) { ret = reverse_service(name + 8); } else if(!strncmp(name, "disable-verity:", 15)) { ret = create_service_thread(set_verity_enabled_state_service, (void*)0); } else if(!strncmp(name, "enable-verity:", 15)) { ret = create_service_thread(set_verity_enabled_state_service, (void*)1); #endif } if (ret >= 0) { close_on_exec(ret); } return ret; }
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); } 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 | O_CLOEXEC); } 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(!HOST && !strncmp(name, "shell:", 6)) { ret = create_subproc_thread(name + 6, SUBPROC_PTY); } else if(!HOST && !strncmp(name, "exec:", 5)) { ret = create_subproc_thread(name + 5, SUBPROC_RAW); } 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 == NULL) 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; char* c = arg; for (; *c != '\0'; c++) { if (*c == ':') *c = ' '; } char* cmd; if (asprintf(&cmd, "%s backup %s", bu_path(), arg) != -1) { ret = create_subproc_thread(cmd, SUBPROC_RAW); free(cmd); } free(arg); } else if(!strncmp(name, "restore:", 8)) { char* cmd; if (asprintf(&cmd, "%s restore", bu_path()) != -1) { ret = create_subproc_thread(cmd, SUBPROC_RAW); free(cmd); } } 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 *) (uintptr_t) port); } else if(!strncmp(name, "usb:", 4)) { ret = create_service_thread(restart_usb_service, NULL); } else if (!strncmp(name, "reverse:", 8)) { char* cookie = strdup(name + 8); if (cookie == NULL) { ret = -1; } else { ret = create_service_thread(reverse_service, cookie); if (ret < 0) { free(cookie); } } #endif } if (ret >= 0) { close_on_exec(ret); } return ret; }
int service_to_fd(const char *name) { int ret = -1; #if !ADB_HOST XLOGV("service_to_fd() name=%s\n", name); #endif 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); } else { #if ADB_HOST adb_mutex_lock(&dns_lock); ret = socket_network_client(name + 1, port, SOCK_STREAM); adb_mutex_unlock(&dns_lock); #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("dns:", name, 4)){ char *n = strdup(name + 4); if(n == 0) return -1; ret = create_service_thread(dns_service, n); #else /* !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(recovery_mode && !strncmp(name, "recover:", 8)) { ret = create_service_thread(recover_service, (void*) atoi(name + 8)); } 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 0 } else if(!strncmp(name, "echo:", 5)){ ret = create_service_thread(echo_service, 0); #endif } if (ret >= 0) { close_on_exec(ret); } return ret; }