From owner-svn-src-projects@FreeBSD.ORG Sat Aug 21 17:15:37 2010 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CF98E10656A5; Sat, 21 Aug 2010 17:15:37 +0000 (UTC) (envelope-from attilio@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id B4A258FC1F; Sat, 21 Aug 2010 17:15:37 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o7LHFbJK075896; Sat, 21 Aug 2010 17:15:37 GMT (envelope-from attilio@svn.freebsd.org) Received: (from attilio@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o7LHFb8b075894; Sat, 21 Aug 2010 17:15:37 GMT (envelope-from attilio@svn.freebsd.org) Message-Id: <201008211715.o7LHFb8b075894@svn.freebsd.org> From: Attilio Rao Date: Sat, 21 Aug 2010 17:15:37 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r211576 - projects/sv/usr.sbin/netdumpsrv X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 Aug 2010 17:15:37 -0000 Author: attilio Date: Sat Aug 21 17:15:37 2010 New Revision: 211576 URL: http://svn.freebsd.org/changeset/base/211576 Log: Replace the use of the old gethostbyname() with getnameinfo(). Modified: projects/sv/usr.sbin/netdumpsrv/netdump_server.c Modified: projects/sv/usr.sbin/netdumpsrv/netdump_server.c ============================================================================== --- projects/sv/usr.sbin/netdumpsrv/netdump_server.c Sat Aug 21 16:47:41 2010 (r211575) +++ projects/sv/usr.sbin/netdumpsrv/netdump_server.c Sat Aug 21 17:15:37 2010 (r211576) @@ -74,7 +74,7 @@ struct netdump_client SLIST_ENTRY(netdump_client) iter; char infofilename[MAXPATHLEN]; char corefilename[MAXPATHLEN]; - char hostname[MAXHOSTNAMELEN]; + char hostname[NI_MAXHOST]; struct in_addr ip; FILE *infofile; int corefd; @@ -93,7 +93,7 @@ struct in_addr bindip; struct pidfh *pfh; int sock; -static struct netdump_client *alloc_client(struct in_addr *ip); +static struct netdump_client *alloc_client(struct sockaddr_in *sip); static void eventloop(void); static void exec_handler(struct netdump_client *client, const char *reason); @@ -119,14 +119,15 @@ static void send_ack(struct netdump_cl static void signal_shutdown(int sig); static void timeout_clients(void); -static struct netdump_client *alloc_client(struct in_addr *ip) +static struct netdump_client *alloc_client(struct sockaddr_in *sip) { struct sockaddr_in saddr; struct netdump_client *client; - struct hostent *hp; - int i, fd, bufsz; + struct in_addr *ip; + char *firstdot; + int i, ecode, fd, bufsz; - assert(ip != NULL); + assert(sip != NULL); client = calloc(1, sizeof(*client)); if (!client) @@ -134,27 +135,25 @@ static struct netdump_client *alloc_clie LOGERR_PERROR("calloc()"); return NULL; } + ip = &sip->sin_addr; bcopy(ip, &client->ip, sizeof(*ip)); client->corefd = -1; client->sock = -1; client->last_msg = now; - /* XXX: To be replaced by getnameinfo(). Get the hostname */ - if ((hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET)) == NULL || - !hp->h_name || strlen(hp->h_name) == 0) - { -#if 0 - /* Can't resolve; use IP */ - addr2ascii(AF_INET, ip, sizeof(*ip), client->hostname); -#endif - } - else - { - char *firstdot; - - /* Grab the hostname */ - strncpy(client->hostname, hp->h_name, MAXHOSTNAMELEN); - hp->h_name[MAXHOSTNAMELEN-1]='\0'; + ecode = getnameinfo((struct sockaddr *)sip, sip->sin_len, client->hostname, + sizeof(client->hostname), NULL, 0, NI_NAMEREQD); + if (ecode != 0) { + + /* Can't resolve, try with a numeric IP. */ + ecode = getnameinfo((struct sockaddr *)sip, sip->sin_len, + client->hostname, sizeof(client->hostname), NULL, 0, 0); + if (ecode != 0) { + LOGERR("getnameinfo(): %s\n", gai_strerror(ecode)); + free(client); + return NULL; + } + } else { /* Strip off the domain name */ firstdot = strchr(client->hostname, '.'); @@ -408,7 +407,7 @@ static void handle_herald(struct sockadd handle_timeout(client); } - client = alloc_client(&from->sin_addr); + client = alloc_client(from); if (!client) {