environment/hostadress.c
///////////////////////////////////////////////////////////////////////////////
// Filename: hostadress.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: get some informations about host where program is running
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date Time Name Description
// -------- -------- -------- ------------------------------------------------
// 96/02/06 02:04:08 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////
// Feature test switches ///////////////////////////// Feature test switches //
/* NONE */
// System headers /////////////////////////////////////////// System headers //
#include <stdlib.h>
#include <sys/param.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// Local headers ///////////////////////////////////////////// Local headers //
#include "../common.h"
// Macros /////////////////////////////////////////////////////////// Macros //
/* NONE */
// File scope objects /////////////////////////////////// File scope objects //
const int maxhostnamelen = MAXHOSTNAMELEN+1;
// External variables, functions, and classes ///////////// External objects //
/* NONE */
// Signal catching functions ///////////////////// Signal catching functions //
/* NONE */
// Structures, unions, and class definitions /////////////////// Definitions //
/* NONE */
// Functions and class implementation /// Functions and class implementation //
/* NONE */
// Main /////////////////////////////////////////////////////////////// Main //
int
main(int argc, char *argv[])
{
error.set_program_name(argv[0]);
char name[maxhostnamelen];
struct hostent *entry;
int i=0;
// get name of host
if (gethostname(name, maxhostnamelen) != 0)
error.system("unknown name!");
cout << "host name: \"" << name << "\"" << endl;
// get more info with the help of our name
if ((entry = gethostbyname(name)) == NULL)
error.system("no host name info");
cout << "official name: \"" << entry->h_name << "\"" << endl;
// print aliases as well
char **aliases=entry->h_aliases;
for(i=1; aliases[i]; i++)
cout << "also known as: \"" << aliases[i] << "\""<< endl;
// and numerical...
switch(entry->h_addrtype)
{
case AF_INET: {
in_addr ia;
ia.s_addr=*((unsigned long *)(entry->h_addr_list[0]));
cout << "address: " << inet_ntoa(ia) << endl;
for(unsigned long **aliases=
(unsigned long **)entry->h_addr_list, i=1;
aliases[i]; i++)
{
ia.s_addr=*aliases[i];
cout << "or: " << inet_ntoa(ia) << endl;
}
}
break;
default:
error.warning("unknown address type");
break;
}
return(EXIT_SUCCESS);
}