Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <netdb.h>
- #include <regex.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- int main(int argc, char *argv[]) {
- int i;
- struct hostent *host;
- struct in_addr **addr_list;
- struct sockaddr_in sin;
- char domain[512];
- regex_t er;
- if (argc != 2) {
- fprintf(stderr,"usage: %s {hostname|ip_address}\n",argv[0]);
- return 1;
- }
- regcomp(&er, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}", REG_EXTENDED|REG_NOSUB);
- if ((regexec(&er, argv[1],0,NULL,0))==0) { // is a IP Address
- sin.sin_addr.s_addr = inet_addr(argv[1]);
- host = gethostbyaddr((char *)&sin.sin_addr.s_addr,sizeof(struct in_addr), AF_INET);
- if (host!=(struct hostent *)0) {
- strcpy(domain,host->h_name);
- printf("%s domain name pointer %s\n", argv[1], domain);
- } else {
- printf("gethostbyaddr failed\n");
- }
- } else { // is a hostname
- if ((host = gethostbyname(argv[1])) != NULL) {
- printf("IP Addresses:\n");
- addr_list = (struct in_addr **)host->h_addr_list;
- for(i = 0; addr_list[i] != NULL; i++)
- printf("%s has address %s\n", argv[1], inet_ntoa(*addr_list[i]));
- } else {
- printf("gethostbyname failed\n");
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment