Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Winsock2.h>
- #include <stdio.h>
- using namespace std;
- DWORD printError(int numError) {
- char errorMessage[1000];
- DWORD fm = FormatMessage(
- FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- (DWORD) numError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPSTR) errorMessage,
- sizeof(errorMessage),
- NULL);
- printf("error: %s\n", errorMessage);
- return fm;
- }
- int main(int argc, char** argv) {
- setlocale(LC_ALL, "Russian");
- if (argc < 2) {
- cout << "error: too few args" << endl;
- return EXIT_FAILURE;
- }
- //init socket
- WSADATA ws;
- if (WSAStartup(MAKEWORD(2, 0), &ws)) {
- printError(WSAGetLastError());
- return EXIT_FAILURE;
- }
- struct hostent* hent;
- unsigned long ip_address;
- //trying to parse IP
- if ((ip_address = inet_addr(argv[1])) != INADDR_NONE) {
- struct in_addr ips;
- ips.s_addr = ip_address;
- //on success try to get host by address
- if (!(hent = gethostbyaddr((char*)&ips, 4, AF_INET))) {
- printError(WSAGetLastError());
- return EXIT_FAILURE;
- }
- }
- else {
- //on fail try to get host by name
- if (!(hent = gethostbyname(argv[1]))) {
- printError(WSAGetLastError());
- return EXIT_FAILURE;
- }
- }
- if (WSACleanup()) {
- printError(WSAGetLastError());
- return EXIT_FAILURE;
- }
- cout << "name:\t" << hent->h_name << endl;
- cout << "h_alias:\n";
- for (char** pAlias = hent->h_aliases; *pAlias != NULL; ++pAlias) {
- cout << '\t' << *pAlias << '\n';
- }
- cout << "h_addresses:\n";
- for (char** pAddress = hent->h_addr_list; *pAddress != NULL; ++pAddress) {
- cout << '\t' << inet_ntoa(*(in_addr*)*pAddress) << '\n';
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement