Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <Winsock2.h>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. DWORD printError(int numError) {
  8. char errorMessage[1000];
  9. DWORD fm = FormatMessage(
  10. FORMAT_MESSAGE_FROM_SYSTEM,
  11. NULL,
  12. (DWORD) numError,
  13. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  14. (LPSTR) errorMessage,
  15. sizeof(errorMessage),
  16. NULL);
  17. printf("error: %s\n", errorMessage);
  18. return fm;
  19. }
  20.  
  21. int main(int argc, char** argv) {
  22.  
  23. setlocale(LC_ALL, "Russian");
  24.  
  25. if (argc < 2) {
  26. cout << "error: too few args" << endl;
  27. return EXIT_FAILURE;
  28. }
  29.  
  30. //init socket
  31. WSADATA ws;
  32. if (WSAStartup(MAKEWORD(2, 0), &ws)) {
  33. printError(WSAGetLastError());
  34. return EXIT_FAILURE;
  35. }
  36.  
  37. struct hostent* hent;
  38. unsigned long ip_address;
  39.  
  40. //trying to parse IP
  41. if ((ip_address = inet_addr(argv[1])) != INADDR_NONE) {
  42. struct in_addr ips;
  43. ips.s_addr = ip_address;
  44.  
  45. //on success try to get host by address
  46. if (!(hent = gethostbyaddr((char*)&ips, 4, AF_INET))) {
  47. printError(WSAGetLastError());
  48. return EXIT_FAILURE;
  49. }
  50. }
  51. else {
  52. //on fail try to get host by name
  53. if (!(hent = gethostbyname(argv[1]))) {
  54. printError(WSAGetLastError());
  55. return EXIT_FAILURE;
  56. }
  57. }
  58.  
  59. if (WSACleanup()) {
  60. printError(WSAGetLastError());
  61. return EXIT_FAILURE;
  62. }
  63.  
  64. cout << "name:\t" << hent->h_name << endl;
  65. cout << "h_alias:\n";
  66. for (char** pAlias = hent->h_aliases; *pAlias != NULL; ++pAlias) {
  67. cout << '\t' << *pAlias << '\n';
  68. }
  69.  
  70. cout << "h_addresses:\n";
  71. for (char** pAddress = hent->h_addr_list; *pAddress != NULL; ++pAddress) {
  72. cout << '\t' << inet_ntoa(*(in_addr*)*pAddress) << '\n';
  73. }
  74.  
  75. return EXIT_SUCCESS;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement