• Antelope Release 5.9 Mac OS X 10.13.2 2019-05-01

 

NAME

my_hardware, my_hostname, my_ip, my_os, my_username, my_ipv6s - return information about current machine

SYNOPSIS

#include "stock.h"

int my_hardware(char *hdw)
int my_hostname(char *name)
int my_ip(char *hostname, char *ipc, int *ip)
int my_os(char *name)
int my_username(char *name)
int my_ipv6s(struct sockaddr_storage *sa, int max_sa_cnt)

DESCRIPTION

These routines return information about the current machine: the processor, the hostname, the ip address, the operating system, and the current user id.

my_ipv6s returns a list of the addresses of the current machine, both ipv6 and ipv4; it returns a count.

EXAMPLE

on an x86 Linux machine named ouray at brtt.com:

my_hardware returns "i686"
my_hostname returns "ouray"
my_ip returns "ouray.brtt.com", "206.168.219.188",
    and the integer 0xbcdba8ce
my_os returns "Linux"
my_username returns "danq"

# Here's a fragmentary example of ipv6s

    struct sockaddr_storage sa[MAXSA] ;
    n = my_ipv6s(sa, MAXSA) ;

    for (int i=0 ; i<n ; i++ ) {
	char ip[64] ;
	bt_inet_ntop((struct sockaddr *) (sa+i), ip, sizeof(ip)) ;
	switch ( (sa+i)->ss_family ) {
	    case AF_INET:
		err = getnameinfo((struct sockaddr *) (sa+i), sizeof(struct sockaddr_in),
			hostname, sizeof(hostname), servname, sizeof(servname), flags ) ;
		break;

	    case AF_INET6:
		err = getnameinfo((struct sockaddr *) (sa+i), sizeof(struct sockaddr_in6),
			hostname, sizeof(hostname), 0, 0, flags ) ;
		break;
	}
	printf("hostname='%s' servname='%s'\n", okstr(hostname), okstr(servname) ) ;
	if ( err != 0 ) {
	    fprintf(stderr, "getnameinfo failed: %s\n", strerror(errno)) ;
	}
	printf("%i %s\n", i, ip ) ;
    }

RETURN VALUES

These routines return 0 for success, -1 if a problem occurred. my_ip may return a hostname of localhost if gethostbyname(3) does not return any entries; in this situation, if ifconfig(8) shows some assigned ip addresses, the first such address is returned. If ifconfig shows no assigned ip addresses, then my_ip returns the error code -1.

my_ipv6s returns a count of the number of ip addresses returned.

LIBRARY

$(STOCKLIBS)

SEE ALSO

uname(2)
ifconfig(8)

BUGS AND CAVEATS

The routine my_ip is predicated on some false assumptions: there is only ip address associated with a machine, and it is an ipv4 address. Consequently, my_ip is deprecated. It should not be used in any new location, and should be phased out in older code. For the present, when it finds an ipv6 address for a machine, it returns the lowest 4 bytes.

Instead of my_ip, use my_ips and/or my_ipv6s.

AUTHOR

Daniel Quinlan
Printer icon