Created
October 24, 2022 09:09
-
-
Save JackyYin/d4c9ec4da7514db809f3fa666970d188 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
#include <arpa/inet.h> | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char **argv) { | |
if (argc < 2) { | |
fprintf(stderr, "Please provide a hostname or IP...\n"); | |
exit(-1); | |
} | |
char *name = argv[1]; | |
struct hostent *hostinfo = gethostbyname(name); | |
if (hostinfo) { | |
printf("name: %s, length: %d, type: %d\n", hostinfo->h_name, hostinfo->h_length, hostinfo->h_addrtype); | |
char *alias; | |
for (int i = 0; alias; i++) { | |
alias = hostinfo->h_aliases[i]; | |
if (alias) | |
printf("alias number %d: %s\n", i, alias); | |
} | |
void *addr; | |
for (int i = 0; addr; i++) { | |
addr = hostinfo->h_addr_list[i]; | |
if (addr) | |
if (hostinfo->h_addrtype == AF_INET) { | |
struct in_addr *inaddr = addr; | |
uint32_t u32addr = ntohl(inaddr->s_addr); | |
printf("address num %d: %u.%u.%u.%u\n", i, ((u32addr >> 24) & 0xFF), ((u32addr >> 16) & 0xFF), ((u32addr >> 8) & 0xFF), (u32addr & 0xFF)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment