util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 16/19] logger: replace gethostbyname() with getaddrinfo()
Date: Sun, 14 Oct 2012 21:21:07 +0100	[thread overview]
Message-ID: <1350246070-10544-17-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1350246070-10544-1-git-send-email-kerolasa@iki.fi>

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 | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 6f9edc2..ecdbfe3 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -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,
-- 
1.7.12.2


  parent reply	other threads:[~2012-10-14 20:21 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-14 20:20 [PATCH 00/19] compliancy fixes Sami Kerola
2012-10-14 20:20 ` [PATCH 01/19] last: stop using MAXHOSTNAMELEN Sami Kerola
2012-10-15  1:46   ` Mike Frysinger
2012-10-14 20:20 ` [PATCH 02/19] login: " Sami Kerola
2012-10-14 20:20 ` [PATCH 03/19] write: " Sami Kerola
2012-10-15  2:12   ` Mike Frysinger
     [not found]     ` <20121015152558.GK18377@x2.net.home>
2012-10-18 20:06       ` Sami Kerola
2012-10-22  6:01         ` Mike Frysinger
2012-10-22  8:06           ` Karel Zak
2012-10-22  8:09             ` Karel Zak
2012-10-22 20:03             ` Mike Frysinger
2012-10-22 20:22               ` Karel Zak
2012-10-14 20:20 ` [PATCH 04/19] agetty: " Sami Kerola
2012-10-14 20:20 ` [PATCH 05/19] c.h: remove unnecessary MAXHOSTNAMELEN fallback definition Sami Kerola
2012-10-14 20:20 ` [PATCH 06/19] docs: add line breaks to whereis.1 Sami Kerola
2012-10-14 20:20 ` [PATCH 07/19] sd-daemon: fix cppcheck warnings Sami Kerola
2012-10-14 22:10   ` Dave Reisner
2012-10-15  8:32     ` Sami Kerola
2012-10-14 20:20 ` [PATCH 08/19] libmount: replace usleep with nanosleep Sami Kerola
2012-10-15  2:14   ` Mike Frysinger
2012-10-14 20:21 ` [PATCH 09/19] include/all-io: " Sami Kerola
2012-10-14 20:21 ` [PATCH 10/19] hwclock: " Sami Kerola
2012-10-14 20:21 ` [PATCH 11/19] rtcwake: " Sami Kerola
2012-10-14 20:21 ` [PATCH 12/19] agetty: " Sami Kerola
2012-10-14 20:21 ` [PATCH 13/19] tailf: " Sami Kerola
2012-10-14 20:21 ` [PATCH 14/19] include/usleep: remove remaining references to usleep Sami Kerola
2012-10-14 20:21 ` [PATCH 15/19] libmount, eject: replace index() and rindex() with strrch() or strrchr() Sami Kerola
2012-10-15  2:14   ` Mike Frysinger
2012-10-14 20:21 ` Sami Kerola [this message]
2012-10-14 20:21 ` [PATCH 17/19] agetty: replace gethostbyname() with getaddrinfo() Sami Kerola
2012-10-14 20:21 ` [PATCH 18/19] build-sys: remove gethostbyname() check Sami Kerola
2012-10-14 20:21 ` [PATCH 19/19] fsck.cramfs: replace utime() with utimensat() Sami Kerola
2012-10-15  2:17   ` Mike Frysinger
2012-10-15  8:36     ` Sami Kerola
2012-10-15 17:39       ` Mike Frysinger
2012-10-22  9:07 ` [PATCH 00/19] compliancy fixes Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350246070-10544-17-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).