From: <av...@us...> - 2011-02-08 23:13:44
|
Revision: 7269 http://openhpi.svn.sourceforge.net/openhpi/?rev=7269&view=rev Author: avpak Date: 2011-02-08 23:13:37 +0000 (Tue, 08 Feb 2011) Log Message: ----------- Feature request #3129977 Command line options -4 and -6 for IPv4 and IPv6 implemented in openhpid. Modified Paths: -------------- openhpi/trunk/docs/man/openhpid.pod.8 openhpi/trunk/openhpid/openhpid-posix.cpp openhpi/trunk/openhpid/openhpid-win32.cpp openhpi/trunk/openhpid/server.cpp openhpi/trunk/openhpid/server.h openhpi/trunk/transport/strmsock.cpp openhpi/trunk/transport/strmsock.h Modified: openhpi/trunk/docs/man/openhpid.pod.8 =================================================================== --- openhpi/trunk/docs/man/openhpid.pod.8 2011-02-03 14:27:46 UTC (rev 7268) +++ openhpi/trunk/docs/man/openhpid.pod.8 2011-02-08 23:13:37 UTC (rev 7269) @@ -62,6 +62,18 @@ Display verbose messages +=item -4 + +The daemon will try to bind IPv4 socket. + +=item -6 + +The daemon will try to bind IPv6 socket. + +=item -4 -6 + +The daemon will try to bind IPv4 or IPv6 socket. + =back =head1 ENVIRONMENTAL VARIABLES Modified: openhpi/trunk/openhpid/openhpid-posix.cpp =================================================================== --- openhpi/trunk/openhpid/openhpid-posix.cpp 2011-02-03 14:27:46 UTC (rev 7268) +++ openhpi/trunk/openhpid/openhpid-posix.cpp 2011-02-08 23:13:37 UTC (rev 7269) @@ -36,6 +36,7 @@ #include <oHpi.h> #include <oh_error.h> +#include <strmsock.h> #include "init.h" #include "server.h" @@ -71,6 +72,9 @@ printf(" -n Forces the code to run as a foreground process\n"); printf(" and NOT as a daemon. The default is to run as\n"); printf(" a daemon. The option is optional.\n\n"); + printf(" -4 The daemon will try to bind IPv4 socket.\n"); + printf(" -6 The daemon will try to bind IPv6 socket.\n"); + printf(" -4 -6 The daemon will try to bind IPv4 or IPv6 socket.\n"); printf("A typical invocation might be\n\n"); printf(" ./openhpid -c /etc/openhpi/openhpi.conf\n\n"); } @@ -247,6 +251,8 @@ struct option options[] = { {"verbose", no_argument, 0, 'v'}, {"nondaemon", no_argument, 0, 'n'}, + {"ipv4", no_argument, 0, '4'}, + {"ipv6", no_argument, 0, '6'}, {"cfg", required_argument, 0, 'c'}, {"port", required_argument, 0, 'p'}, {"pidfile", required_argument, 0, 'f'}, @@ -261,11 +267,12 @@ unsigned int sock_timeout = 0; // unlimited int max_threads = -1; // unlimited bool runasdaemon = true; + int ipvflags = 0; /* get the command line options */ int c; while (1) { - c = getopt_long(argc, argv, "nvc:p:f:s:t:", options, 0); + c = getopt_long(argc, argv, "nv46c:p:f:s:t:", options, 0); /* detect when done scanning options */ if (c == -1) { break; @@ -300,6 +307,12 @@ case 'n': runasdaemon = false; break; + case '4': + ipvflags |= FlagIPv4; + break; + case '6': + ipvflags |= FlagIPv6; + break; case '?': default: /* they obviously need it */ @@ -313,6 +326,10 @@ exit(-1); } + if (ipvflags == 0) { + ipvflags = FlagIPv4; + } + // see if we have a valid configuration file if ((!cfgfile) || (!g_file_test(cfgfile, G_FILE_TEST_EXISTS))) { CRIT("Cannot find configuration file. Exiting."); @@ -347,8 +364,11 @@ INFO("%s version %s started.", argv[0], VERSION); INFO("OPENHPI_CONF = %s.", cfgfile); INFO("OPENHPI_DAEMON_PORT = %u", port); + INFO("Enabled IP versions:%s%s\n", + (ipvflags & FlagIPv4) ? " IPv4" : "", + (ipvflags & FlagIPv6) ? " IPv6" : ""); - bool rc = oh_server_run(port, sock_timeout, max_threads); + bool rc = oh_server_run(ipvflags, port, sock_timeout, max_threads); if (!rc) { return 9; } Modified: openhpi/trunk/openhpid/openhpid-win32.cpp =================================================================== --- openhpi/trunk/openhpid/openhpid-win32.cpp 2011-02-03 14:27:46 UTC (rev 7268) +++ openhpi/trunk/openhpid/openhpid-win32.cpp 2011-02-08 23:13:37 UTC (rev 7269) @@ -24,6 +24,7 @@ #include <oHpi.h> #include <oh_error.h> +#include <strmsock.h> #include "init.h" #include "server.h" @@ -57,6 +58,9 @@ printf(" -n Forces the code to run as a foreground process\n"); printf(" and NOT as a daemon. The default is to run as\n"); printf(" a daemon. The option is optional.\n\n"); + printf(" -4 The daemon will try to bind IPv4 socket.\n"); + printf(" -6 The daemon will try to bind IPv6 socket.\n"); + printf(" -4 -6 The daemon will try to bind IPv4 or IPv6 socket.\n"); printf("A typical invocation might be\n\n"); printf(" openhpid.exe -c C:\\openhpi.conf\n\n"); } @@ -128,6 +132,8 @@ struct option options[] = { {"verbose", no_argument, 0, 'v'}, {"nondaemon", no_argument, 0, 'n'}, + {"ipv4", no_argument, 0, '4'}, + {"ipv6", no_argument, 0, '6'}, {"cfg", required_argument, 0, 'c'}, {"port", required_argument, 0, 'p'}, {"timeout", required_argument, 0, 's'}, @@ -140,11 +146,12 @@ unsigned int sock_timeout = 0; // unlimited int max_threads = -1; // unlimited //bool runasdaemon = true; + int ipvflags = 0; /* get the command line options */ int c; while (1) { - c = getopt_long(argc, argv, "nvc:p:f:s:t:", options, 0); + c = getopt_long(argc, argv, "nv46c:p:f:s:t:", options, 0); /* detect when done scanning options */ if (c == -1) { break; @@ -157,7 +164,7 @@ setenv("OPENHPI_CONF", optarg); cfgfile = g_strdup(optarg); break; - case 'p': + case 'P': setenv("OPENHPI_DAEMON_PORT", optarg); port = atoi(optarg); break; @@ -176,6 +183,12 @@ case 'n': //runasdaemon = false; break; + case '4': + ipvflags |= FlagIPv4; + break; + case '6': + ipvflags |= FlagIPv6; + break; case '?': default: /* they obviously need it */ @@ -189,6 +202,10 @@ exit(-1); } + if (ipvflags == 0) { + ipvflags = FlagIPv4; + } + // see if we have a valid configuration file if ((!cfgfile) || (!g_file_test(cfgfile, G_FILE_TEST_EXISTS))) { CRIT("Cannot find configuration file. Exiting."); @@ -205,8 +222,12 @@ INFO("%s version %s started.", argv[0], VERSION); INFO("OPENHPI_CONF = %s.", cfgfile); INFO("OPENHPI_DAEMON_PORT = %u.", port); + INFO("Enabled IP versions:%s%s\n", + (ipvflags & FlagIPv4) ? " IPv4" : "", + (ipvflags & FlagIPv6) ? " IPv6" : ""); - bool rc = oh_server_run(port, sock_timeout, max_threads); + + bool rc = oh_server_run(ipvflags, port, sock_timeout, max_threads); if (!rc) { return 9; } Modified: openhpi/trunk/openhpid/server.cpp =================================================================== --- openhpi/trunk/openhpid/server.cpp 2011-02-03 14:27:46 UTC (rev 7268) +++ openhpi/trunk/openhpid/server.cpp 2011-02-08 23:13:37 UTC (rev 7269) @@ -75,13 +75,14 @@ /* HPI Server Interface */ /*--------------------------------------------------------------------*/ -bool oh_server_run( uint16_t port, +bool oh_server_run( int ipvflags, + uint16_t port, unsigned int sock_timeout, int max_threads ) { // create the server socket cServerStreamSock ssock; - if (!ssock.Create(port)) { + if (!ssock.Create(ipvflags, port)) { CRIT("Error creating server socket. Exiting."); return false; } Modified: openhpi/trunk/openhpid/server.h =================================================================== --- openhpi/trunk/openhpid/server.h 2011-02-03 14:27:46 UTC (rev 7268) +++ openhpi/trunk/openhpid/server.h 2011-02-08 23:13:37 UTC (rev 7269) @@ -19,8 +19,11 @@ #include <stdint.h> +#include <strmsock.h> -bool oh_server_run( uint16_t port, + +bool oh_server_run( int ipvflags, + uint16_t port, unsigned int sock_timeout, int max_threads ); Modified: openhpi/trunk/transport/strmsock.cpp =================================================================== --- openhpi/trunk/transport/strmsock.cpp 2011-02-03 14:27:46 UTC (rev 7268) +++ openhpi/trunk/transport/strmsock.cpp 2011-02-08 23:13:37 UTC (rev 7269) @@ -19,6 +19,8 @@ #include <stdio.h> #include <string.h> +#include <list> + #ifdef _WIN32 #include <winsock2.h> #include <ws2tcpip.h> @@ -64,13 +66,12 @@ /*************************************************************** * Helper functions **************************************************************/ -static void SelectAddresses( int hintflags, +static void SelectAddresses( int ipvflags, + int hintflags, const char * node, uint16_t port, - struct addrinfo * & selected ) + std::list<struct addrinfo *>& selected ) { - selected = 0; - struct addrinfo hints; memset( &hints, 0, sizeof(hints) ); hints.ai_flags = hintflags; @@ -88,36 +89,19 @@ return; } - struct addrinfo * in = 0; - struct addrinfo * in_tail = 0; - struct addrinfo * out = 0; - struct addrinfo * out_tail = 0; - for ( struct addrinfo * item = items; item != 0; item = item->ai_next ) { - if ( ( item->ai_family == AF_INET ) || ( item->ai_family == AF_INET6 ) ) { - if ( in_tail ) { - in_tail->ai_next = item; - in_tail = item; - } else { - in = in_tail = item; - } + for ( struct addrinfo * item = items; item != 0; ) { + struct addrinfo * next = item->ai_next; + item->ai_next = 0; + if ( ( ipvflags & FlagIPv4 ) && ( item->ai_family == AF_INET ) ) { + selected.push_back( item ); + } else if ( ( ipvflags & FlagIPv6 ) && ( item->ai_family == AF_INET6 ) ) { + selected.push_back( item ); } else { - if ( out_tail ) { - out_tail->ai_next = item; - out_tail = item; - } else { - out = out_tail = item; - } + freeaddrinfo( item ); } - } - if ( in_tail ) { - in_tail->ai_next = 0; - } - if ( out_tail ) { - out_tail->ai_next = 0; - freeaddrinfo( out ); - } - selected = in; + item = next; + } } @@ -325,24 +309,21 @@ bool cClientStreamSock::Create( const char * host, uint16_t port ) { - struct addrinfo * infos; - SelectAddresses( 0, host, port, infos ); - if ( infos == 0 ) { - CRIT( "failed to find sockaddr." ); - return false; - } - bool connected = false; struct addrinfo * info; - for ( info = infos; info != 0; info = info->ai_next ) { - connected = Create( info ); - if ( connected ) { - break; + std::list<struct addrinfo *> infos; + + SelectAddresses( FlagIPv4 | FlagIPv6, 0, host, port, infos ); + + while ( !infos.empty() ) { + info = *infos.begin(); + if ( !connected ) { + connected = Create( info ); } + freeaddrinfo( info ); + infos.pop_front(); } - freeaddrinfo( infos ); - return connected; } @@ -422,26 +403,23 @@ // empty } -bool cServerStreamSock::Create( uint16_t port ) +bool cServerStreamSock::Create( int ipvflags, uint16_t port ) { - struct addrinfo * infos; - SelectAddresses( AI_PASSIVE, 0, port, infos ); - if ( infos == 0 ) { - CRIT( "failed to find sockaddr." ); - return false; - } - bool bound = false; struct addrinfo * info; - for ( info = infos; info != 0; info = info->ai_next ) { - bound = Create( info ); - if ( bound ) { - break; + std::list<struct addrinfo *> infos; + + SelectAddresses( ipvflags, AI_PASSIVE, 0, port, infos ); + + while ( !infos.empty() ) { + info = *infos.begin(); + if ( !bound ) { + bound = Create( info ); } + freeaddrinfo( info ); + infos.pop_front(); } - freeaddrinfo( infos ); - return bound; } Modified: openhpi/trunk/transport/strmsock.h =================================================================== --- openhpi/trunk/transport/strmsock.h 2011-02-03 14:27:46 UTC (rev 7268) +++ openhpi/trunk/transport/strmsock.h 2011-02-08 23:13:37 UTC (rev 7269) @@ -33,6 +33,16 @@ /*************************************************************** + * IPv4 / IPv6 + **************************************************************/ +typedef enum +{ + FlagIPv4 = 1, + FlagIPv6 = 2 +} IPvFlags; + + +/*************************************************************** * Message Header **************************************************************/ typedef struct @@ -152,7 +162,7 @@ explicit cServerStreamSock(); ~cServerStreamSock(); - bool Create( uint16_t port ); + bool Create( int ipvflags, uint16_t port ); cStreamSock * Accept(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |