Last active
December 26, 2015 12:19
-
-
Save deepakkumarnd/7150181 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Getip: This program takes a hostname as argument and outputs its ip address | |
Usage: ./getip <hostname> | |
Program Name: getip.c | |
Author Deepak Kumar | |
Email: deepakkumarnd@gmail.com | |
*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<netdb.h> | |
#include<errno.h> | |
#include<sys/types.h> | |
#include<sys/socket.h> | |
#include<netinet/in.h> | |
#include<arpa/inet.h> | |
struct in_addr getip(char *); //returns 32 bit ip address | |
int main(int argc, char *argv[]) { | |
if(argc! = 2) { | |
printf("Usage! ./getip <hostname>\n"); | |
exit(-1); | |
} | |
getip(argv[1]); | |
return(0); | |
} | |
// function reurning address | |
struct in_addr getip(char *domain) { | |
struct hostent *host = NULL; | |
if(!(host = gethostbyname(domain))) { | |
herror("Error! "); | |
exit(-2); | |
} | |
if(host -> h_name! = NULL){ | |
printf("Hostname:\t%s", host -> h_name); | |
} | |
printf("Aliases:\n"); | |
int i = 0; | |
while(1) { | |
if(!(host -> h_aliases[i])) { break; } | |
printf("\t%d)\t%s\n", (i+1), host -> h_aliases[i]); | |
i++; | |
} | |
printf("Address type\t%d\n", host -> h_addrtype); | |
printf("Address length\t%d\n", host -> h_length); | |
printf("Network addresses:\n"); | |
i = 0; | |
while(1){ | |
if(!(host -> h_addr_list[i])) { break; } | |
printf("\t%d)\t%s\n", (i+1), inet_ntoa(*((struct in_addr *)host -> h_addr_list[i]))); | |
i++; | |
} | |
return (*((struct in_addr*)host -> h_addr)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment