hostname(1) change

Kent Ibbetson bsd at kibbet.com
Sun Jan 4 18:06:07 PST 2004


Hi Emiel, All,

Emiel Kollof probed the keyboard and on Mon, Jan 05, 2004 at 02:05:53AM +0100 produced:
> On Monday 05 January 2004 01:47, Kent Ibbetson wrote:

[snip]

> 
> One suggestion: Add support for ipv6 adresses. Resolving is almost the same 
> except for some differently named things (like AF_INET6, sockaddr6_in, etc.) 
> that have different sizes.

Thanks for the suggestion Emiel, attached is a new patchset (based on clean
source), I haven't done much IPV6 work, but it seems to work ok, you can now
do the following and get the same result;

hostname www.6bone.net
hostname -r 3ffe:b00:c18:1::10
hostname -r 206.123.31.124



> Cheers,
> Emiel
> -- 

Regards,

-- 
Kent Ibbetson
bsd at xxxxxxxxxx
*** bin/hostname/hostname.1.orig	Tue Jun 17 14:22:50 2003
--- bin/hostname/hostname.1	Mon Jan  5 12:01:46 2004
***************
*** 42,48 ****
  .Sh SYNOPSIS
  .Nm
  .Op Fl s
! .Op Ar name-of-host
  .Sh DESCRIPTION
  The
  .Nm
--- 42,48 ----
  .Sh SYNOPSIS
  .Nm
  .Op Fl s
! .Op Ar name-of-host | -r ip-address
  .Sh DESCRIPTION
  The
  .Nm
***************
*** 62,67 ****
--- 62,71 ----
  .It Fl s
  Trim off any domain information from the printed
  name.
+ .It Fl r
+ Retrive hostname via ip lookup.  The hostname is set to the first (official)
+ name returned for the supplied ip address.  Can be an IPv6 or IPv4 address.
+ 
  .El
  .Sh SEE ALSO
  .Xr gethostname 3 ,
*** bin/hostname/hostname.c.orig	Mon Sep 29 00:39:14 2003
--- bin/hostname/hostname.c	Mon Jan  5 12:01:46 2004
***************
*** 44,61 ****
  #include <string.h>
  #include <unistd.h>
  
  int main (int, char *[]);
  void usage (void);
  
  int
  main(int argc, char **argv)
  {
! 	int ch, sflag;
  	char *p, hostname[MAXHOSTNAMELEN];
  
! 	sflag = 0;
! 	while ((ch = getopt(argc, argv, "s")) != -1)
  		switch (ch) {
  		case 's':
  			sflag = 1;
  			break;
--- 44,84 ----
  #include <string.h>
  #include <unistd.h>
  
+ #include <netdb.h>
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ 
+ 
  int main (int, char *[]);
  void usage (void);
  
  int
  main(int argc, char **argv)
  {
! 	int ch,sflag,rflag,ret,flag6;
  	char *p, hostname[MAXHOSTNAMELEN];
+ 	char *srflag;
+ 	struct hostent *hst = NULL;
+ 	struct in_addr ia;
+ 	struct in6_addr ia6;
+ 
+ 	srflag = NULL;
+ 	sflag = rflag = 0;
+ 	flag6 = 0;
  
! 	while ((ch = getopt(argc, argv, "r:s")) != -1)
  		switch (ch) {
+ 		case 'r':
+ 			srflag = (char*)calloc(1,sizeof(char) * (strlen((char*)optarg)+1));
+ 			if(srflag) {
+ 				rflag = 1;
+ 				strlcpy(srflag, (char*)optarg, strlen((char*)optarg)+1);
+ 			} else {
+ 				errx(1, "malloc");
+ 			}
+ 			break;
  		case 's':
  			sflag = 1;
  			break;
***************
*** 69,75 ****
  	if (argc > 1)
  		usage();
  
! 	if (*argv) {
  		if (sethostname(*argv, (int)strlen(*argv)))
  			err(1, "sethostname");
  	} else {
--- 92,130 ----
  	if (argc > 1)
  		usage();
  
! 	if (rflag && *argv) {
! 		free(srflag);
! 		usage();
! 	}
! 
! 
! 	if(rflag) {
! 		ret = inet_pton(AF_INET, srflag, &ia);
! 		if(ret != 1) {
! 			// check IPV6
! 			ret = inet_pton(AF_INET6, srflag, &ia6);
! 
! 			if(ret != 1) {
! 				free(srflag);
! 				errx(1, "invalid ip address");
! 			}
! 
! 			flag6 = 1;
! 		}
! 		
! 		if(flag6 == 1) 
! 			hst = gethostbyaddr((const char*)&ia6, sizeof(ia6), AF_INET6);
! 		else
! 			hst = gethostbyaddr((const char*)&ia, sizeof(ia), AF_INET);
! 		if(!hst) {
! 			free(srflag);
! 			if(h_errno == HOST_NOT_FOUND) 
! 				errx(1,"host not found\n");
! 		}
! 
! 		if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
! 			err(1, "sethostname");
! 	} else if (*argv) {
  		if (sethostname(*argv, (int)strlen(*argv)))
  			err(1, "sethostname");
  	} else {
***************
*** 86,91 ****
  usage(void)
  {
  
! 	(void)fprintf(stderr, "usage: hostname [-s] [name-of-host]\n");
  	exit(1);
  }
--- 141,146 ----
  usage(void)
  {
  
! 	(void)fprintf(stderr, "usage: hostname [-s] [name-of-host | -r ip-address]\n");
  	exit(1);
  }




More information about the Submit mailing list