open-hpi / openhpi

Other
7 stars 10 forks source link

libipmidirect still uses gethostbyname, which impact IPv6 support #2739

Closed ngothan closed 5 years ago

ngothan commented 6 years ago

libipmidirect still uses the function gethostbyname, which may impact IPv6 support. It should be use the getaddrinfo.

grep -r gethostbyname plugins/ipmi/ipmi.c: struct hostent ent = gethostbyname(tok); plugins/ipmidirect/ipmi.cpp: ent = gethostbyname( addr ); plugins/ipmidirect/t/con_000.cpp: struct hostent *ent = gethostbyname( host );

ngothan commented 5 years ago

proposed fix for this issue:

diff -up openhpi-3.8.0/plugins/ipmidirect/ipmi.cpp.me openhpi-3.8.0/plugins/ipmidirect/ipmi.cpp
--- openhpi-3.8.0/plugins/ipmidirect/ipmi.cpp.me        2018-11-19 17:55:14.447840556 +0100
+++ openhpi-3.8.0/plugins/ipmidirect/ipmi.cpp   2018-11-19 19:38:19.040952595 +0100
@@ -1929,7 +1929,12 @@ cIpmi::AllocConnection( GHashTable *hand
        char            user[32]   = "";
        char            passwd[32] = "";
        char           *value;
-       struct hostent *ent;
+       struct addrinfo hints, *servinfo = NULL, *ai = NULL;
+       char portnumber_string[4];
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = SOCK_STREAM;

        // Address
        addr = (const char *)g_hash_table_lookup(handler_config, "addr");
@@ -1941,16 +1946,16 @@ cIpmi::AllocConnection( GHashTable *hand
           }

        stdlog << "AllocConnection: addr = '" << addr << "'.\n";
-       ent = gethostbyname( addr );
+       (void)sprintf(portnumber_string, "%d", lan_port);

-       if ( !ent )
+       if (getaddrinfo(addr, portnumber_string, &hints, &servinfo) != 0) 
           {
             stdlog << "Unable to resolve IPMI LAN address: " << addr << " !\n";
             return 0;
           }

-       memcpy( &lan_addr, ent->h_addr_list[0], ent->h_length );
-       unsigned int a = *(unsigned int *)(void *)ent->h_addr_list[0];
+       memcpy(&lan_addr, servinfo->ai_addr, servinfo->ai_addrlen);
+       unsigned int a = *(unsigned int *)(void *)servinfo->ai_addr;

        stdlog << "Using host at "
               << (int)(a & 0xff) << "."
@@ -1958,6 +1963,8 @@ cIpmi::AllocConnection( GHashTable *hand
               << (int)((a >> 16) & 0xff) << "."
               << (int)((a >> 24) & 0xff) << ".\n";

+       freeaddrinfo(servinfo);
+
        // Port
        lan_port = GetIntNotNull( handler_config, "port", 623 );
mohandev2 commented 5 years ago

Than, Thank you very much for the bug and the patch. I have created a pull request with that.