? .deps ? Makefile ? Makefile.in ? config.h ? patch.diff ? stamp-h1 ? gtk-gui/.deps ? gtk-gui/Makefile ? gtk-gui/Makefile.in Index: Communicator.cxx =================================================================== RCS file: /cvsroot/crack-attack/crack-attack/src/Communicator.cxx,v retrieving revision 1.5 diff -u -r1.5 Communicator.cxx --- Communicator.cxx 16 Apr 2005 08:13:38 -0000 1.5 +++ Communicator.cxx 6 May 2005 13:39:54 -0000 @@ -125,21 +125,68 @@ switch (mode & (CM_SERVER | CM_CLIENT)) { case CM_SERVER: { - int connection_socket = socket(AF_INET, SOCK_STREAM, 0); + struct addrinfo hints, *res, *ressave; + struct sockaddr_storage address; - sockaddr_in address; + int error, ReUseAddr = 1; + int connection_socket = -1; + char port_str[NI_MAXSERV]; + + snprintf(port_str, NI_MAXSERV, "%d", port); + + /* Clear the hints variable */ + memset(&hints, 0, sizeof(hints)); + + /* + * AI_PASSIVE flag: the resulting address is used to bind + * to a socket for accepting incoming connections. + * So, when the hostname==NULL, getaddrinfo function will + * return one entry per allowed protocol family containing + * the unspecified address for that family. + */ + hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + error = getaddrinfo(NULL, port_str, &hints, &res); + if (error != 0) + { + /* handle getaddrinfo error */ + cerr << "Error in getaddrinfo()." << endl; + exit(1); + } + + ressave=res; + + /* + * Try open socket with each address getaddrinfo returned, + * until getting a valid listening socket. + */ + while (res) + { + /* create socket */ + connection_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (connection_socket >= 0) + { #ifndef _WIN32 - int val = 1; - setsockopt (connection_socket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (int)); + /* allow it to always reuse the same port */ + ReUseAddr = 1; + setsockopt(connection_socket, SOL_SOCKET, + SO_REUSEADDR, &ReUseAddr, sizeof(ReUseAddr)); #endif - address.sin_family = AF_INET; - address.sin_addr.s_addr = htonl(INADDR_ANY); - address.sin_port = htons(port); - if (bind(connection_socket, (sockaddr *) &address, sizeof(address)) < 0) { - cerr << "Port " << port << " is busy." << endl; - exit(1); + if (bind(connection_socket, res->ai_addr, res->ai_addrlen) == 0) + break; + + cerr << "Port " << port << " is busy." << endl; + exit(1); + + close(connection_socket); + connection_socket=-1; + } + res = res->ai_next; } + freeaddrinfo(ressave); cout << "Waiting for connection at port " << port << "..." << endl; listen(connection_socket, 1); @@ -162,7 +209,15 @@ #else int length = sizeof(address); #endif - comm_link = accept(connection_socket, (sockaddr *) &address, &length); + /* take care: accept is a "slow" system call */ +accept_again: + if ((comm_link = accept(connection_socket, (struct sockaddr *) &address, &length)) < 0) + { + if (errno == EINTR) + goto accept_again; + else + cerr << "Error in accept()" << endl; + } comm_link_active = true; // check version id @@ -186,32 +241,74 @@ // available symmetry breaking term win_ties = true; - cout << "Connection made by " << inet_ntoa(address.sin_addr) << '.' << endl; + char clientname[NI_MAXHOST]; + memset(clientname, 0, sizeof(clientname)); + + getnameinfo((struct sockaddr *)&address, length, + clientname, sizeof(clientname), + port_str, sizeof(port_str), + NI_NUMERICHOST); + + cout << "Connection made by " << clientname << '.' << endl; break; } case CM_CLIENT: { - comm_link = socket(AF_INET, SOCK_STREAM, 0); - comm_link_active = true; + struct addrinfo hints, *res, *ressave; + struct sockaddr_storage address; -#ifdef DEVELOPMENT - cout << "Hostname: " << host_name << endl; -#endif - hostent *host = gethostbyname(host_name); - if (!host) { - cerr << "Host '" << host_name << "' not found." << endl; - exit(1); - } + int error, ReUseAddr = 1; + char port_str[NI_MAXSERV]; + + cerr << "Port: " << port << endl; + snprintf(port_str, NI_MAXSERV, "%d", port); + + /* Clear the hints variable */ + memset(&hints, 0, sizeof(hints)); - sockaddr_in address; - address.sin_family = AF_INET; - address.sin_addr = *(struct in_addr *) host->h_addr; - address.sin_port = htons((short) port); - if (connect(comm_link, (sockaddr *) &address, sizeof(address)) < 0) { - cerr << "Connection failed. Unable to connect to address." << endl; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + cerr << host_name << endl; + + error = getaddrinfo(host_name, port_str, &hints, &res); + if (error != 0) + { + /* handle getaddrinfo error */ + cerr << "Error in getaddrinfo()." << endl; + perror(gai_strerror(error)); exit(1); } + cerr << "Connecting to " << host_name << endl; + + /* + * Try open socket with each address getaddrinfo returned, + * until getting a valid connection on the socket. + */ + ressave=res; + while (res) + { + /* create socket */ + comm_link = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (comm_link >= 0) + { + /* allow it to always reuse the same port */ + ReUseAddr = 1; + setsockopt(comm_link, SOL_SOCKET, + SO_REUSEADDR, &ReUseAddr, sizeof(ReUseAddr)); + + if (connect(comm_link, res->ai_addr, res->ai_addrlen) == 0) + break; + + cerr << "Connection failed. Unable to connect to address." << endl; + close(comm_link); + comm_link=-1; + } + res = res->ai_next; + } + freeaddrinfo(ressave); + // check version id uint32 version_id = CO_TEST_INT; for (char *c = CO_VERSION; *c; c++) @@ -238,7 +335,7 @@ // for simplicity, client loses ties - but don't tell anyone win_ties = false; - cout << "Connection made to " << inet_ntoa(address.sin_addr) << ':' + cout << "Connection made to " << host_name << ':' << (short) port << '.' << endl; break; } } Index: gtk-gui/interface.cxx =================================================================== RCS file: /cvsroot/crack-attack/crack-attack/src/gtk-gui/interface.cxx,v retrieving revision 1.13 diff -u -r1.13 interface.cxx --- gtk-gui/interface.cxx 2 May 2005 21:03:47 -0000 1.13 +++ gtk-gui/interface.cxx 6 May 2005 13:39:55 -0000 @@ -225,7 +225,7 @@ gtk_widget_show (lblTmpServerAddress); gtk_box_pack_start (GTK_BOX (vbox7), lblTmpServerAddress, FALSE, FALSE, 0); - lblServerAddress = gtk_label_new ("127.0.0.1"); + lblServerAddress = gtk_label_new ("::1"); gtk_widget_set_name (lblServerAddress, "lblServerAddress"); gtk_widget_show (lblServerAddress); gtk_box_pack_start (GTK_BOX (vbox7), lblServerAddress, FALSE, FALSE, 0);