Revision: 7068 http://playerstage.svn.sourceforge.net/playerstage/?rev=7068&view=rev Author: gbiggs Date: 2008-10-07 00:49:03 +0000 (Tue, 07 Oct 2008)
Log Message: ----------- Applied and cleaned up most of patch 2145081. Modified Paths: -------------- code/player/trunk/client_libs/libplayerc/client.c code/player/trunk/libplayertcp/playerudp.cc code/player/trunk/libplayertcp/remote_driver.cc code/player/trunk/libplayertcp/socket_util.c code/player/trunk/server/drivers/blobfinder/acts/acts.cc code/player/trunk/server/drivers/laser/lms400_cola.cc code/player/trunk/server/drivers/laser/lms400_cola.h code/player/trunk/server/drivers/mixed/p2os/p2os.cc code/player/trunk/server/drivers/speech/festival.cc code/player/trunk/utils/playerv/playerv.c Modified: code/player/trunk/client_libs/libplayerc/client.c =================================================================== --- code/player/trunk/client_libs/libplayerc/client.c 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/client_libs/libplayerc/client.c 2008-10-07 00:49:03 UTC (rev 7068) @@ -46,6 +46,7 @@ #include <assert.h> #include <math.h> +#include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -211,7 +212,11 @@ // Connect to the server int playerc_client_connect(playerc_client_t *client) { - struct hostent* entp; +#if defined(HAVE_GETADDRINFO) + struct addrinfo* addr_ptr = NULL; +#else + struct hostent* entp = NULL; +#endif char banner[PLAYER_IDENT_STRLEN]; int old_flags; int ret; @@ -239,7 +244,7 @@ * * Specifying sin_port = 0 allows the system to choose the port. */ - clientaddr.sin_family = PF_INET; + clientaddr.sin_family = AF_INET; clientaddr.sin_addr.s_addr = INADDR_ANY; clientaddr.sin_port = 0; @@ -247,7 +252,7 @@ (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < -1) { PLAYERC_ERR1("bind call failed with error [%s]", strerror(errno)); - return(-1); + return -1; } } else @@ -273,17 +278,38 @@ #endif // Construct server address + memset(&client->server, 0, sizeof(client->server)); + client->server.sin_family = AF_INET; + client->server.sin_port = htons(client->port); +#if defined(HAVE_GETADDRINFO) + if (getaddrinfo(client->host, NULL, NULL, &addr_ptr) != 0) + { + playerc_client_disconnect(client); + PLAYERC_ERR("getaddrinfo() failed with error"); + return -1; + } + assert(addr_ptr); + assert(addr_ptr->ai_addr); + if ((addr_ptr->ai_addr->sa_family) != AF_INET) + { + playerc_client_disconnect(client); + PLAYERC_ERR("unsupported internet address family"); + return -1; + } + client->server.sin_addr.s_addr = ((struct sockaddr_in *)(addr_ptr->ai_addr))->sin_addr.s_addr; + freeaddrinfo(addr_ptr); + addr_ptr = NULL; +#else entp = gethostbyname(client->host); if (entp == NULL) { playerc_client_disconnect(client); - PLAYERC_ERR1("gethostbyname failed with error [%s]", strerror(errno)); + PLAYERC_ERR1("gethostbyname() failed with error [%s]", strerror(errno)); return -1; } - client->server.sin_family = PF_INET; - memcpy(&client->server.sin_addr, entp->h_addr_list[0], entp->h_length); - client->server.sin_port = htons(client->port); - + assert(entp->h_length <= sizeof (client->server.sin_addr)); + memcpy(&(client->server.sin_addr), entp->h_addr_list[0], entp->h_length); +#endif // Connect the socket /* t = client->request_timeout; @@ -363,7 +389,7 @@ { if(send(client->sock, NULL, 0, 0) < 0) { - PLAYERC_ERR1("gethostbyname failed with error [%s]", strerror(errno)); + PLAYERC_ERR1("send() failed with error [%s]", strerror(errno)); return -1; } } Modified: code/player/trunk/libplayertcp/playerudp.cc =================================================================== --- code/player/trunk/libplayertcp/playerudp.cc 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/libplayertcp/playerudp.cc 2008-10-07 00:49:03 UTC (rev 7068) @@ -1262,7 +1262,7 @@ * * Specifying sin_port = 0 would allow the system to choose the port. */ - serverp.sin_family = PF_INET; + serverp.sin_family = AF_INET; serverp.sin_addr.s_addr = INADDR_ANY; if(bind(sock, (struct sockaddr*)&serverp, sizeof(serverp)) == -1) Modified: code/player/trunk/libplayertcp/remote_driver.cc =================================================================== --- code/player/trunk/libplayertcp/remote_driver.cc 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/libplayertcp/remote_driver.cc 2008-10-07 00:49:03 UTC (rev 7068) @@ -95,7 +95,7 @@ return(-1); } - server.sin_family = PF_INET; + server.sin_family = AF_INET; server.sin_addr.s_addr = this->device_addr.host; server.sin_port = htons(this->device_addr.robot); Modified: code/player/trunk/libplayertcp/socket_util.c =================================================================== --- code/player/trunk/libplayertcp/socket_util.c 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/libplayertcp/socket_util.c 2008-10-07 00:49:03 UTC (rev 7068) @@ -1,9 +1,9 @@ /* * Player - One Hell of a Robot Server - * Copyright (C) 2000 + * Copyright (C) 2000 * Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard - * * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -59,10 +59,10 @@ #include "socket_util.h" /* - * this function creates a socket of the indicated type and binds it to + * this function creates a socket of the indicated type and binds it to * the indicated port. * - * NOTE: we pick the IP address (and thus network interface) for binding + * NOTE: we pick the IP address (and thus network interface) for binding * by calling gethostname() and then stripping it down to the first * component (i.e. no domain name). if this process won't * result in the IP address that you want, tough luck. @@ -75,14 +75,14 @@ * PLAYER_TRANSPORT_TCP (for TCP) * backlog: number of waiting connections to be allowed (TCP only) * - * RETURN: - * On success, the fd of the new socket is returned. Otherwise, -1 + * RETURN: + * On success, the fd of the new socket is returned. Otherwise, -1 * is returned and an explanatory note is dumped to stderr. */ int -create_and_bind_socket(char blocking, unsigned int host, int* portnum, +create_and_bind_socket(char blocking, unsigned int host, int* portnum, int playersocktype, int backlog) { int sock; /* socket we're creating */ @@ -108,11 +108,11 @@ serverp.sin_addr.s_addr = host; serverp.sin_port = htons(*portnum); - /* - * Create the INET socket. - * + /* + * Create the INET socket. + * */ - if((sock = socket(PF_INET, socktype, 0)) == -1) + if((sock = socket(PF_INET, socktype, 0)) == -1) { perror("create_and_bind_socket:socket() failed; socket not created."); return(-1); @@ -135,14 +135,14 @@ * get the current access flags */ if((flags = fcntl(sock, F_GETFL)) == -1) - { + { perror("create_and_bind_socket():fcntl() while getting socket " "access flags; socket not created."); close(sock); return(-1); } /* - * OR the current flags with O_NONBLOCK (so we won't block), + * OR the current flags with O_NONBLOCK (so we won't block), * and write them back */ if(fcntl(sock, F_SETFL, flags | O_NONBLOCK ) == -1) @@ -157,7 +157,7 @@ if(socktype == SOCK_STREAM) { /* make sure we can reuse the port soon after */ - if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, + if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, sizeof(one))) { perror("create_and_bind_socket(): setsockopt(2) failed"); @@ -166,18 +166,18 @@ } - /* + /* * Bind it to the port indicated * INADDR_ANY indicates that any network interface (IP address) - * for the local host may be used (presumably the OS will choose the + * for the local host may be used (presumably the OS will choose the * right one). * * Specifying sin_port = 0 would allow the system to choose the port. */ - serverp.sin_family = PF_INET; + serverp.sin_family = AF_INET; serverp.sin_addr.s_addr = INADDR_ANY; - if(bind(sock, (struct sockaddr*)&serverp, sizeof(serverp)) == -1) + if(bind(sock, (struct sockaddr*)&serverp, sizeof(serverp)) == -1) { perror ("create_and_bind_socket():bind() failed; socket not created."); close(sock); @@ -209,7 +209,7 @@ /* - * return the fd for the newly bound socket + * return the fd for the newly bound socket */ return(sock); } Modified: code/player/trunk/server/drivers/blobfinder/acts/acts.cc =================================================================== --- code/player/trunk/server/drivers/blobfinder/acts/acts.cc 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/server/drivers/blobfinder/acts/acts.cc 2008-10-07 00:49:03 UTC (rev 7068) @@ -139,7 +139,10 @@ /** @} */ +#include "config.h" + #include <assert.h> +#include <stddef.h> #include <stdio.h> #include <unistd.h> /* close(2),fcntl(2),getpid(2),usleep(3),execvp(3),fork(2)*/ #include <netdb.h> /* for gethostbyname(3) */ @@ -479,7 +482,6 @@ static struct sockaddr_in server; char host[] = "localhost"; - struct hostent* entp; printf("ACTS vision server connection initializing..."); fflush(stdout); @@ -671,13 +673,33 @@ } else { - /* in parent */ - /* fill in addr structure */ - server.sin_family = PF_INET; + memset(&server, 0, sizeof server); + server.sin_port = htons(portnum); + server.sin_family = AF_INET; +#if HAVE_GETADDRINFO + struct addrinfo* addr_ptr = NULL; + if (getaddrinfo(host, NULL, NULL, &addr_ptr)) + { + PLAYER_ERROR("getaddrinfo() failed with error"); + KillACTS(); + return(1); + } + assert(addr_ptr); + assert(addr_ptr->ai_addr); + if ((addr_ptr->ai_addr->sa_family) != AF_INET) + { + PLAYER_ERROR("unsupported internet address family"); + KillACTS(); + return(1); + } + server.sin_addr.s_addr = (reinterpret_cast<struct sockaddr_in *>(addr_ptr->ai_addr))->sin_addr.s_addr; + freeaddrinfo(addr_ptr); addr_ptr = NULL; +#else /* * this is okay to do, because gethostbyname(3) does no lookup if the * 'host' * arg is already an IP addr */ + struct hostent* entp; if((entp = gethostbyname(host)) == NULL) { fprintf(stderr, "Acts::Setup(): \"%s\" is unknown host; " @@ -688,9 +710,7 @@ } memcpy(&server.sin_addr, entp->h_addr_list[0], entp->h_length); - - server.sin_port = htons(portnum); - +#endif /* ok, we'll make this a bit smarter. first, we wait a baseline amount * of time, then try to connect periodically for some predefined number * of times @@ -710,7 +730,7 @@ KillACTS(); return(1); } - if(connect(sock,(struct sockaddr*)&server, sizeof(server)) == 0) + if(connect(sock,reinterpret_cast<struct sockaddr*> (&server), sizeof(server)) == 0) break; usleep(ACTS_STARTUP_INTERVAL_USEC); } Modified: code/player/trunk/server/drivers/laser/lms400_cola.cc =================================================================== --- code/player/trunk/server/drivers/laser/lms400_cola.cc 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/server/drivers/laser/lms400_cola.cc 2008-10-07 00:49:03 UTC (rev 7068) @@ -4,13 +4,17 @@ Date: 7 Feb 2007 CVS: $Id$ */ + +#include "config.h" + +#include <stddef.h> #include <sys/socket.h> #include <netdb.h> #include <libplayercore/playercore.h> #include "lms400_cola.h" #include <unistd.h> -#include <strings.h> +#include <string.h> //////////////////////////////////////////////////////////////////////////////// // Constructor. @@ -35,20 +39,35 @@ return (-1); // Get the network host entry - server = gethostbyname ((const char *)hostname); - if (server == NULL) + memset (&serv_addr, 0, sizeof (serv_addr)); + serv_addr.sin_port = htons(portno); + serv_addr.sin_family = AF_INET; +#if defined (HAVE_GETADDRINFO) + addr_ptr = NULL; + if (getaddrinfo (hostname, NULL, NULL, &(addr_ptr))) + { + PLAYER_ERROR ("getaddrinfo() failed with error"); return (-1); + } + assert (addr_ptr); + assert (addr_ptr->ai_addr); + if ((addr_ptr->ai_addr->sa_family) != AF_INET) + { + PLAYER_ERROR ("unsupported internet address family"); + return (-1); + } + serv_addr.sin_addr.s_addr = (reinterpret_cast<struct sockaddr_in*> (addr_ptr->ai_addr))->sin_addr.s_addr; + freeaddrinfo (addr_ptr); + addr_ptr = NULL; +#else + server = gethostbyname (hostname); + if ((server) == NULL) + return (-1); + memcpy (&(serv_addr.sin_addr.s_addr), server->h_addr, server->h_length); +#endif - // Fill in the sockaddr_in structure values - memset ((char *) &serv_addr, 0, sizeof (serv_addr)); - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons (portno); - memcpy ((char *)&serv_addr.sin_addr.s_addr, - (char *)server->h_addr, - server->h_length); - // Attempt to connect - if (connect (sockfd, (const sockaddr*)&serv_addr, sizeof (serv_addr)) < 0) + if (connect (sockfd, reinterpret_cast<struct sockaddr*> (&serv_addr), sizeof (serv_addr)) < 0) return (-1); return (0); Modified: code/player/trunk/server/drivers/laser/lms400_cola.h =================================================================== --- code/player/trunk/server/drivers/laser/lms400_cola.h 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/server/drivers/laser/lms400_cola.h 2008-10-07 00:49:03 UTC (rev 7068) @@ -4,6 +4,10 @@ Date: 7 Feb 2007 CVS: $Id$ */ + +#include "config.h" + +#include <netdb.h> #include <sys/types.h> #include <vector> #include <netinet/in.h> @@ -86,7 +90,11 @@ const char* hostname; int sockfd, portno, n; struct sockaddr_in serv_addr; +#if defined (HAVE_GETADDRINFO) + struct addrinfo *addr_ptr; +#else struct hostent *server; +#endif // Internal Parameters: int verbose; Modified: code/player/trunk/server/drivers/mixed/p2os/p2os.cc =================================================================== --- code/player/trunk/server/drivers/mixed/p2os/p2os.cc 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/server/drivers/mixed/p2os/p2os.cc 2008-10-07 00:49:03 UTC (rev 7068) @@ -717,22 +717,37 @@ return(1); } //printf("created socket %d.\nLooking up hostname...\n", this->psos_fd); + struct sockaddr_in addr; + memset(&addr, 0, sizeof addr); + addr.sin_family = AF_INET; + addr.sin_port = htons(this->psos_tcp_port); +#if HAVE_GETADDRINFO + struct addrinfo * addr_ptr = NULL; + if (getaddrinfo(this->psos_tcp_host, NULL, NULL, &addr_ptr)) + { + PLAYER_ERROR("Error looking up hostname or address"); + return 1; + } + assert(addr_ptr); + assert(addr_ptr->ai_addr); + assert((addr_ptr->ai_addr->sa_family) == AF_INET); + addr.sin_addr.s_addr = (reinterpret_cast<struct sockaddr_in *>(addr_ptr->ai_addr))->sin_addr.s_addr; + freeaddrinfo(addr_ptr); + addr_ptr = NULL; +#else struct hostent* h = gethostbyname(this->psos_tcp_host); if(!h) { perror("Error looking up hostname or address %s:"); return(1); } - struct sockaddr_in addr; - assert((size_t)h->h_length <= sizeof(addr.sin_addr)); + assert(static_cast<size_t> (h->h_length) <= sizeof(addr.sin_addr)); //printf("gethostbyname returned address %d length %d.\n", * h->h_addr, h->h_length); memcpy(&(addr.sin_addr), h->h_addr, h->h_length); //printf("copied address to addr.sin_addr.s_addr=%d\n", addr.sin_addr.s_addr); - addr.sin_family = AF_INET; - addr.sin_port = htons(this->psos_tcp_port); - printf("Found host address, connecting..."); - fflush(stdout); - if(connect(this->psos_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0) +#endif + PLAYER_WARN("Found host address, connecting..."); + if(connect(this->psos_fd, reinterpret_cast<struct sockaddr*> (&addr), sizeof(addr)) < 0) { perror("Error Connecting to remote host (P2OS::Setup()::connect()):"); return(1); @@ -746,7 +761,7 @@ return(1); } assert(flags & O_NONBLOCK); - printf("TCP socket connection is OK... "); + PLAYER_WARN("TCP socket connection is OK... "); fflush(stdout); } else Modified: code/player/trunk/server/drivers/speech/festival.cc =================================================================== --- code/player/trunk/server/drivers/speech/festival.cc 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/server/drivers/speech/festival.cc 2008-10-07 00:49:03 UTC (rev 7068) @@ -79,8 +79,12 @@ */ /** @} */ +#include "config.h" + +#include <stddef.h> #include <stdio.h> #include <errno.h> +#include <assert.h> #include <unistd.h> /* close(2),fcntl(2),getpid(2),usleep(3),execlp(3),fork(2)*/ #include <netdb.h> /* for gethostbyname(3) */ #include <netinet/in.h> /* for struct sockaddr_in, htons(3) */ @@ -229,9 +233,8 @@ char* festival_args[8]; - static struct sockaddr_in server; + struct sockaddr_in server; char host[] = "localhost"; - struct hostent* entp; // start out with a clean slate //queue->Flush(); @@ -286,29 +289,44 @@ } else { - /* in parent */ - /* fill in addr structure */ - server.sin_family = PF_INET; + memset(&server, 0, sizeof (server)); + server.sin_family = AF_INET; + server.sin_port = htons(portnum); +#if HAVE_GETADDRINFO + struct addrinfo* addr_ptr = NULL; + if (getaddrinfo(host, NULL, NULL, &addr_ptr)) + { + PLAYER_ERROR("getaddrinfo() failed with error"); + KillFestival(); + return(1); + } + assert(addr_ptr); + assert(addr_ptr->ai_addr); + if ((addr_ptr->ai_addr->sa_family) != AF_INET) + { + PLAYER_ERROR("unsupported internet address family"); + KillFestival(); + return(1); + } + server.sin_addr.s_addr = (reinterpret_cast<struct sockaddr_in *>(addr_ptr->ai_addr))->sin_addr.s_addr; + freeaddrinfo(addr_ptr); + addr_ptr = NULL; +#else /* * this is okay to do, because gethostbyname(3) does no lookup if the * 'host' * arg is already an IP addr */ + struct hostent* entp; if((entp = gethostbyname(host)) == NULL) { - fprintf(stderr, "Festival::Setup(): \"%s\" is unknown host; " + PLAYER_ERROR1("Festival::Setup(): \"%s\" is unknown host; " "can't connect to Festival\n", host); /* try to kill Festival just in case it's running */ KillFestival(); return(1); } +#endif - memcpy(&server.sin_addr, entp->h_addr_list[0], entp->h_length); - - - server.sin_port = htons(portnum); - - - /* ok, we'll make this a bit smarter. first, we wait a baseline amount * of time, then try to connect periodically for some predefined number * of times @@ -328,7 +346,7 @@ /* * hook it up */ - if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == 0) + if(connect(sock, reinterpret_cast<struct sockaddr*> (&server), sizeof(server)) == 0) break; usleep(FESTIVAL_STARTUP_INTERVAL_USEC); } Modified: code/player/trunk/utils/playerv/playerv.c =================================================================== --- code/player/trunk/utils/playerv/playerv.c 2008-10-06 20:33:32 UTC (rev 7067) +++ code/player/trunk/utils/playerv/playerv.c 2008-10-07 00:49:03 UTC (rev 7068) @@ -314,7 +314,7 @@ // start out timer if in pull mode if(rate > 0.0) gettimeofday(&tv, NULL); - + while (!quit) { // Let gui process messages This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Playerstage-commit mailing list Playerstage-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/playerstage-commit