1: Code
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include < errno.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <
netdb.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <net/if.h>//extern int h_errno;
Char interfaceipaddr[50];
int get_interface_ipaddr (char *ethname) {int inet_sock;
struct Ifreq IFR;
Inet_sock = socket (af_inet, SOCK_DGRAM, 0);
strcpy (Ifr.ifr_name, ethname);
if (IOCTL (Inet_sock, SIOCGIFADDR, &IFR) < 0) {perror ("bind");
Exit (Exit_failure);
sprintf (interfaceipaddr, "%s", Inet_ntoa ((struct sockaddr_in*) & (IFR.IFR_ADDR)));
} void Getinterfaceinfo (char* ethinterface) {int i,sock_fd;
struct Ifreq ifreq;
Char mac[32];
Char ipbuf[16];
int rcvbuf_size,sndbuf_size,type=0;
socklen_t size; if (Sock_fd=socket (af_inet,sock_stream,0) <0) {perror ("error");
Exit (Exit_failure);
} size=sizeof (Sndbuf_size);
GetSockOpt (sock_fd,sol_socket,so_sndbuf,&sndbuf_size,&size);
printf ("sndbuf_size=%d size=%d\n", sndbuf_size,size);
Size=sizeof (rcvbuf_size);
GetSockOpt (sock_fd,sol_socket,so_rcvbuf,&rcvbuf_size,&size);
printf ("rcvbuf_size=%d size=%d\n", rcvbuf_size,size);
Size=sizeof (type);
GetSockOpt (sock_fd,sol_socket,so_type,&type,&size);
printf ("Socket type=%d\n", type);
strcpy (Ifreq.ifr_name,ethinterface);
if (IOCTL (sock_fd,siocgifhwaddr,&ifreq) <0) {perror ("error:");
Exit (Exit_failure);
For (i=0 i<6; i++) {sprintf (Mac+3*i, "%02x:", (unsigned char) ifreq.ifr_hwaddr.sa_data[i]);
} mac[17]= ';
printf ("mac:%s\n", MAC);
if (IOCTL (sock_fd,siocgifaddr,&ifreq) <0) {perror ("error:");
Exit (Exit_failure); printf ("IP:%s\n", Inet_ntoa ((struct sockaddr_in*) &ifrEQ.IFR_ADDR)) (->SIN_ADDR));
return 0;
} void Gethostbyipadrr (char* ipaddr) {U_int addr;
struct Hostent *hp;
Char **p;
struct Ifreq ifreq;
if (ipaddr = = NULL) {printf ("usage:ip-address\n");
Exit (Exit_failure);
} if ((int) (addr = inet_addr (ipaddr)) = = 1) {printf ("ip-address must be of the form a.b.c.d\n");
Exit (Exit_failure);
} hp=gethostbyaddr ((char *) &addr, sizeof (addr), af_inet);
if (HP = NULL) {printf ("Host information for%s no found \ n", ipaddr);
Exit (Exit_failure);
for (P = hp->h_addr_list; *p!=0;p++) {struct in_addr in;
Char **q;
memcpy (&in.s_addr, *p, sizeof (IN.S_ADDR));
printf ("%s\t%s", Inet_ntoa (in), hp->h_name);
for (q=hp->h_aliases;*q!= 0; q++) printf ("%s\t", *q);
printf ("\ n");
} void Ipaddrconvert () {in_addr_t netaddr;
struct IN_ADDR net_addr,ret;
Char buf[128];
memset (buf, ' n ', 128);
NETADDR=INET_ADDR ("192.168.68.128");
NET_ADDR.S_ADDR=NETADDR; printf ("inet_addr (192.168.68.128) =0x%x\ n ", inet_addr (" 192.168.68.128 "));
printf ("Inet_network (192.168.68.128) =0x%x\n", Inet_network ("192.168.68.128"));
printf ("Inet_ntoa (net)%s\n", Inet_ntoa (NET_ADDR));
Inet_aton ("192.168.68.128", &ret);
printf ("Test inet_aton,then Inet_ntoa (ret)%s\n", Inet_ntoa (ret));
Inet_pton (Af_inet, "192.168.68.128", &ret);
Inet_ntop (af_inet,&ret,buf,128);
printf ("buf=%s\n", buf);
} void gethostbyname (char *hostname) {char *ptr,**pptr;
Char Str[inet_addrstrlen];
struct Hostent *hptr;
{ptr = hostname; if ((hptr = gethostbyname (ptr)) = = NULL) {printf ("GetHostByName error for Host:%s:%s", PTR, Hstrerror (H_errno)
);
Exit (Exit_failure);
printf ("Official hostname:%s\n", hptr->h_name);
For (pptr=hptr->h_aliases; *pptr!= NULL; pptr++) {printf ("\talias:%s\n", *pptr);
Switch (hptr->h_addrtype) {Case af_inet:pptr = hptr->h_addr_list; for (; *pptr!= NULL; pptr++) printf ("\taddress:%s\n", Inet_ntop (Hptr->h_addRtype, *pptr, str, sizeof (str));
Break
Default:printf ("Unknown address type");
Break
}} void Showinterfaceipinfo (char *ethname) {/* initializes the network to obtain information for the current network device */int i;
int sock;
struct sockaddr_in sin;
struct Ifreq IFR;
unsigned char g_eth_name[16];
unsigned char g_macaddr[6];
unsigned int g_subnetmask;
unsigned int g_ipaddr;
unsigned int g_broadcast_ipaddr;
Sock = socket (af_inet, SOCK_DGRAM, 0);
if (sock = = 1) {perror ("socket");
} strcpy (G_eth_name,ethname);
strcpy (Ifr.ifr_name, g_eth_name);
printf ("Eth name:\t%s\n", g_eth_name);
Gets and prints the network card address if (IOCTL (sock, SIOCGIFHWADDR, &IFR) < 0) {perror ("ioctl");
} memcpy (G_macaddr, Ifr.ifr_hwaddr.sa_data, 6);
printf ("local mac:\t");
for (i=0;i<5;i++) {printf ("%.2x:", G_macaddr[i]);
printf ("%.2x\n", G_macaddr[i]);
Gets and prints the IP address if (IOCTL (sock, SIOCGIFADDR, &IFR) < 0) {perror ("ioctl"); memcpy (&sin, &ifr.ifr_addr, sizeof (SIN));
G_IPADDR = sin.sin_addr.s_addr;
printf ("Local%s:\t%s\n", G_eth_name, Inet_ntoa (SIN.SIN_ADDR));
Gets and prints the broadcast address if (IOCTL (sock, SIOCGIFBRDADDR, &IFR) < 0) {perror ("ioctl");
} memcpy (&sin, &ifr.ifr_addr, sizeof (sin));
G_BROADCAST_IPADDR = sin.sin_addr.s_addr;
printf ("broadcast:\t%s\n", Inet_ntoa (SIN.SIN_ADDR));
Gets and prints the subnet mask if (IOCTL (sock, Siocgifnetmask, &IFR) < 0) {perror ("ioctl");
} memcpy (&sin, &ifr.ifr_addr, sizeof (sin));
G_subnetmask = sin.sin_addr.s_addr;
printf ("subnetmask:\t%s\n", Inet_ntoa (SIN.SIN_ADDR));
Close (sock);
} usageerror (const char *progname, const char *msg) {if (msg!= NULL) {fprintf (stderr, '%s ', msg);
} fprintf (stderr, "Usage:%s [options] parameter\n", progname);
fprintf (stderr, "Usage:%s [N:I:P:FH] parameter\n", progname); fprintf (stderr, "Usage:%s-n [hostName];"
to Execute:gethostbyname () \ n ", progname); fprintf (stderr, "Usage:%s-i [ipaddr];"to EXECUTE:GETHOSTBYIPADRR () \ n ", progname); fprintf (stderr, "Usage:%s-p [interface;eth0/eth1];"
to Execute:getinterfaceinfo () \ n ", progname); fprintf (stderr, "Usage:%s-f [addr convert function];
to Execute:ipaddrconvert () \ n ", progname);
fprintf (stderr, "Usage:%s-h; to Execute:help\n", progname);
Exit (Exit_failure);
int main (int argc, char **argv) {int opt;
if (argc==1) {usageerror (argv[0], NULL); #if 0 printf ("\n\n*********************************************************************************************
\ n ");
System ("cat/etc/hosts"); printf ("****************************************************************************************************\n
\ n "); #endif while (opt = getopt (argc, argv, "N:I:P:FH"))!=-1} {switch (opt) {case ' n ': if (optarg!=null) {p
rintf ("optopt:%c:%s\n", Optopt,optarg);
gethostbyname (OPTARG);
else {usageerror (argv[0], NULL);
} break;
Case ' I ': if (optarg!=null) { printf ("Optopt:%c:%s\n", Optopt,optarg);
GETHOSTBYIPADRR (OPTARG);
else {usageerror (argv[0], NULL);
} break;
Case ' P ': if (optarg!=null) {printf ("optopt:%c:%s\n", Optopt,optarg);
Getinterfaceinfo (OPTARG);
Showinterfaceipinfo (OPTARG);
else {usageerror (argv[0], NULL);
} break;
Case ' F ': Ipaddrconvert ();
Break
Default:usageerror (Argv[0], NULL);
} exit (0);
}
2: Execution Results