//connect to a remote host as specified by host and port //host may be wither an ip address or a host name _SOCKET connect_to(const char *host, short port) { _SOCKET sock; sockaddr_in server; memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(host); server.sin_port = qhtons(port); //If a domain name was specified, we may not have an IP. if (server.sin_addr.s_addr == INADDR_NONE) { hostent *he = gethostbyname(host); if (he == NULL) { msg(PLUGIN_NAME": Unable to resolve name: %s\n", host); return INVALID_SOCKET; } server.sin_addr = *(in_addr*) he->h_addr; } //create a socket. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) { if (connect(sock, (sockaddr *) &server, sizeof(server)) == SOCKET_ERROR) { msg(PLUGIN_NAME": Failed to connect to server.\n"); closesocket(sock); sock = INVALID_SOCKET; } else { #ifdef _WIN32 DWORD tv = 2000; #else timeval tv; tv.tv_sec = 2; tv.tv_usec = 0; #endif //we force a periodic timeout to force a recv error after //the socket has been closed. On windows, simply closing //the socket causes a blocking recv to fail and the recvHandler //thread to terminate. On Linux, closing the socket was not //causing the blocking recv to terminate, hence the timeout //following a timeout, if the socket has been closed, the next //receive will fail. Not elegant but it works setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)); } } else { msg(PLUGIN_NAME": Failed to create socket.\n"); } return sock; }
//connect to a remote host as specified by host and port //host may be either an ip address or a host name bool connect_to(const char *host, short port, _SOCKET *sock) { bool result = false; addrinfo hints; addrinfo *ai; sockaddr_in *server; memset(&hints, 0, sizeof(addrinfo)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(host, NULL, &hints, &ai) == 0) { server = (sockaddr_in*)ai->ai_addr; server->sin_port = qhtons(port); //create a socket. if ((*sock = (_SOCKET)socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) { if (connect(*sock, ai->ai_addr, (int)ai->ai_addrlen) == SOCKET_ERROR) { msg(PLUGIN_NAME": Failed to connect to server.\n"); closesocket(*sock); } else { #ifdef _WIN32 DWORD tv = 2000; #else timeval tv; tv.tv_sec = 2; tv.tv_usec = 0; #endif //we force a periodic timeout to force a recv error after //the socket has been closed. On windows, simply closing //the socket causes a blocking recv to fail and the recvHandler //thread to terminate. On Linux, closing the socket was not //causing the blocking recv to terminate, hence the timeout //following a timeout, if the socket has been closed, the next //receive will fail. Not elegant but it works setsockopt(*sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv)); result = true; } } else { msg(PLUGIN_NAME": Failed to create socket.\n"); } freeaddrinfo(ai); } return result; }
bool Buffer::writeShort(int val) { short s = (short)val; s = qhtons(s); return write(&s, sizeof(s)); }
//-------------------------------------------------------------------------- // debugger remote server - TCP/IP mode int NT_CDECL main(int argc, char *argv[]) { int port_number = DEBUGGER_PORT_NUMBER; lprintf("IDA " SYSTEM SYSBITS " remote debug server(" __SERVER_TYPE__ ") v1.%d. Copyright HexRays 2004-2010\n", IDD_INTERFACE_VERSION); while ( argc > 1 && (argv[1][0] == '-' || argv[1][0] == '/')) { switch ( argv[1][1] ) { case 'p': port_number = atoi(&argv[1][2]); break; case 'P': server_password = argv[1] + 2; break; case 'v': verbose = true; break; default: error("usage: ida_remote [switches]\n" " -p... port number\n" " -P... password\n" " -v verbose\n"); } argv++; argc--; } // call the debugger module to initialize its subsystem once if ( !init_subsystem() #ifndef __SINGLE_THREADED_SERVER__ || ((g_lock = qmutex_create())== NULL) #endif ) { lprintf("Could not initialize subsystem!"); return -1; } #ifndef __NT__ signal(SIGHUP, shutdown_gracefully); #endif signal(SIGINT, shutdown_gracefully); signal(SIGTERM, shutdown_gracefully); signal(SIGSEGV, shutdown_gracefully); // signal(SIGPIPE, SIG_IGN); if ( !init_irs_layer() ) { neterr(NULL, "init_sockets"); } listen_socket = socket(AF_INET, SOCK_STREAM, 0); if ( listen_socket == -1 ) neterr(NULL, "socket"); setup_irs((idarpc_stream_t*)listen_socket); struct sockaddr_in sa; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = qhtons(short(port_number)); if ( bind(listen_socket, (sockaddr *)&sa, sizeof(sa)) == SOCKET_ERROR ) neterr((idarpc_stream_t *)listen_socket, "bind"); if ( listen(listen_socket, SOMAXCONN) == SOCKET_ERROR ) neterr((idarpc_stream_t *)listen_socket, "listen"); hostent *local_host = gethostbyname(""); if ( local_host != NULL ) { const char *local_ip = inet_ntoa (*(struct in_addr *)*local_host->h_addr_list); if ( local_host->h_name != NULL && local_ip != NULL ) lprintf("Host %s (%s): ", local_host->h_name, local_ip); else if ( local_ip != NULL ) lprintf("Host %s: ", local_ip); } lprintf("Listening on port #%u...\n", port_number); while ( true ) { sockaddr_in sa; socklen_t salen = sizeof(sa); SOCKET rpc_socket = accept(listen_socket, (sockaddr *)&sa, &salen); if ( rpc_socket == -1 ) neterr((idarpc_stream_t *)listen_socket, "accept"); #if defined(__LINUX__) && defined(LIBWRAP) const char *p; if ( (p=check_connection(rpc_socket)) != NULL ) { fprintf(stderr, "ida-server CONNECTION REFUSED from %s (tcp_wrappers)\n", p); shutdown(rpc_socket, 2); close(rpc_socket); continue; } #endif // defined(__LINUX__) && defined(LIBWRAP) rpc_server_t *server = new rpc_server_t(rpc_socket); server->verbose = verbose; server->set_debugger_instance(create_debug_session()); handle_session(server); } /* NOTREACHED term_subsystem(); #ifndef __SINGLE_THREADED_SERVER__ qmutex_free(g_lock); #endif */ }
//-------------------------------------------------------------------------- // debugger remote server - TCP/IP mode int NT_CDECL main(int argc, char *argv[]) { #ifdef ENABLE_LOWCNDS init_idc(); #endif // call the debugger module to initialize its subsystem once if ( !init_lock() || !init_subsystem() #ifndef __SINGLE_THREADED_SERVER__ || !srv_lock_init() #endif ) { lprintf("Could not initialize subsystem!"); return -1; } bool reuse_conns = are_broken_connections_supported(); int port_number = DEBUGGER_PORT_NUMBER; lprintf("IDA " SYSTEM SYSBITS " remote debug server(" __SERVER_TYPE__ ") v1.%d. Hex-Rays (c) 2004-2014\n", IDD_INTERFACE_VERSION); while ( argc > 1 && (argv[1][0] == '-' || argv[1][0] == '/')) { switch ( argv[1][1] ) { case 'p': port_number = atoi(&argv[1][2]); break; case 'P': server_password = argv[1] + 2; break; case 'i': ipv4_address = argv[1] + 2; break; case 'v': verbose = true; break; case 'k': if ( !reuse_conns ) error("Sorry, debugger doesn't support reusing broken connections\n"); keep_broken_connections = true; break; default: error("usage: ida_remote [switches]\n" " -i... IP address to bind to (default to any)\n" " -v verbose\n" " -p... port number\n" " -P... password\n" "%s", reuse_conns ? " -k keep broken connections\n" : ""); break; } argv++; argc--; } #ifndef UNDER_CE #ifndef __NT__ signal(SIGHUP, shutdown_gracefully); #endif signal(SIGINT, shutdown_gracefully); signal(SIGTERM, shutdown_gracefully); signal(SIGSEGV, shutdown_gracefully); // signal(SIGPIPE, SIG_IGN); #endif if ( !init_irs_layer() ) { neterr(NULL, "init_sockets"); } listen_socket = socket(AF_INET, SOCK_STREAM, 0); if ( listen_socket == INVALID_SOCKET ) neterr(NULL, "socket"); idarpc_stream_t *irs = (idarpc_stream_t *)listen_socket; setup_irs(irs); struct sockaddr_in sa; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = qhtons(short(port_number)); if ( ipv4_address != NULL ) sa.sin_addr.s_addr = inet_addr(ipv4_address); if( sa.sin_addr.s_addr == INADDR_NONE ) { lprintf("Cannot parse IP v4 address %s, falling back to INADDR_ANY\n", ipv4_address); sa.sin_addr.s_addr = INADDR_ANY; ipv4_address = NULL; } if ( bind(listen_socket, (sockaddr *)&sa, sizeof(sa)) == SOCKET_ERROR ) neterr(irs, "bind"); if ( listen(listen_socket, SOMAXCONN) == SOCKET_ERROR ) neterr(irs, "listen"); hostent *local_host = gethostbyname(""); if ( local_host != NULL ) { const char *local_ip; if ( ipv4_address != NULL ) local_ip = ipv4_address; else local_ip = inet_ntoa(*(struct in_addr *)*local_host->h_addr_list); if ( local_host->h_name != NULL && local_ip != NULL ) lprintf("Host %s (%s): ", local_host->h_name, local_ip); else if ( local_ip != NULL ) lprintf("Host %s: ", local_ip); } lprintf("Listening on port #%u...\n", port_number); while ( true ) { socklen_t salen = sizeof(sa); SOCKET rpc_socket = accept(listen_socket, (sockaddr *)&sa, &salen); if ( rpc_socket == INVALID_SOCKET ) { #ifdef UNDER_CE if ( WSAGetLastError() != WSAEINTR ) #else if ( errno != EINTR ) #endif neterr(irs, "accept"); continue; } #if defined(__LINUX__) && defined(LIBWRAP) const char *p = check_connection(rpc_socket); if ( p != NULL ) { fprintf(stderr, "ida-server CONNECTION REFUSED from %s (tcp_wrappers)\n", p); shutdown(rpc_socket, 2); close(rpc_socket); continue; } #endif // defined(__LINUX__) && defined(LIBWRAP) rpc_server_t *server = new rpc_server_t((idarpc_stream_t *)rpc_socket); server->verbose = verbose; server->set_debugger_instance(create_debug_session()); { #ifdef UNDER_CE get_permissions_t all_permissions; #endif handle_session(server); } } /* NOTREACHED term_lock(); term_subsystem(); #ifndef __SINGLE_THREADED_SERVER__ qmutex_free(g_lock); #endif */ }