]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
logger: replace gethostbyname() with getaddrinfo()
authorSami Kerola <kerolasa@iki.fi>
Fri, 12 Oct 2012 20:28:41 +0000 (21:28 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 22 Oct 2012 08:14:24 +0000 (10:14 +0200)
The gethostbyname() is legacy function which may be withdrawn in a
future.

Reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyname.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
misc-utils/logger.c

index 6f9edc22550db1715e1864e26d2430a23192c007..ecdbfe372ad2353ba72f1e966f5013644ac09f0e 100644 (file)
@@ -59,7 +59,6 @@
 #include <syslog.h>
 
 static int optd = 0;
-static uint16_t udpport = 514;
 
 static int decode(char *name, CODE *codetab)
 {
@@ -119,24 +118,25 @@ myopenlog(const char *sock) {
 }
 
 static int
-udpopenlog(const char *servername, uint16_t port) {
-       int fd;
-       struct sockaddr_in s_addr;
-       struct hostent *serverhost;
-
-       if ((serverhost = gethostbyname(servername)) == NULL )
-               errx(EXIT_FAILURE, _("unable to resolve '%s'"), servername);
-
-       if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1)
+udpopenlog(const char *servername, const char *port) {
+       int fd, errcode;
+       struct addrinfo hints, *res;
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_socktype = SOCK_DGRAM;
+       hints.ai_family = AF_UNSPEC;
+
+       errcode = getaddrinfo(servername, port, &hints, &res);
+       if (errcode != 0)
+               errx(EXIT_FAILURE, _("getaddrinfo %s:%s: %s"), servername, port,
+                    gai_strerror(errcode));
+       if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1)
                err(EXIT_FAILURE, _("socket"));
 
-       memcpy(&s_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
-        s_addr.sin_family=AF_INET;
-        s_addr.sin_port=htons(port);
-
-        if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
+       if (connect(fd, res->ai_addr, res->ai_addrlen) == -1)
                err(EXIT_FAILURE, _("connect"));
 
+       freeaddrinfo(res);
        return fd;
 }
 static void
@@ -201,6 +201,7 @@ main(int argc, char **argv) {
        char *tag, buf[1024];
        char *usock = NULL;
        char *udpserver = NULL;
+       char *udpport = NULL;
        int LogSock = -1;
 
        static const struct option longopts[] = {
@@ -257,8 +258,7 @@ main(int argc, char **argv) {
                        udpserver = optarg;
                        break;
                case 'P':               /* change udp port */
-                       udpport = strtou16_or_err(optarg,
-                                               _("invalid port number argument"));
+                       udpport = optarg;
                        break;
                case 'V':
                        printf(_("%s from %s\n"), program_invocation_short_name,