Advertisement
sanpai

IP address

Jun 29th, 2013
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include<WinSock2.h>
  2. #include<iphlpapi.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<Windows.h>
  6. #pragma comment(lib, "IPHLPAPI.lib")
  7. #define BUFF_SIZE 15000
  8.  
  9.  
  10. int main(void )
  11. {
  12. PIP_ADAPTER_ADDRESSES pAddresses = NULL;
  13. //DWORD len;
  14. DWORD retval=0;
  15. ULONG iterations=0;
  16. ULONG outBufLen=BUFF_SIZE;
  17. ULONG size=0;
  18. DWORD size2=0;
  19. WSADATA wsaData;
  20. char op[256];
  21. LPSOCKADDR lp=NULL;
  22. do
  23. {
  24. pAddresses = (IP_ADAPTER_ADDRESSES *) malloc(outBufLen);
  25. if(pAddresses==NULL)
  26. {
  27. printf("\n The buffer memory could not be allocated ");
  28. return 1;
  29. }
  30.  
  31.  
  32. retval=GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen);
  33. if(retval==ERROR_BUFFER_OVERFLOW)
  34. {
  35. free(pAddresses);
  36. pAddresses=NULL;
  37. }
  38.  
  39. else
  40. break;
  41.  
  42. iterations++;
  43.  
  44.  
  45.  
  46.  
  47. }while((retval==ERROR_BUFFER_OVERFLOW)&&(iterations<3));
  48.  
  49.  
  50. printf(" Return value = %d ",retval);
  51.  
  52. if(retval==NO_ERROR)
  53. {
  54. while (pAddresses) {
  55.  
  56. printf("\tAdapter name: %s\n", pAddresses->AdapterName);
  57.  
  58. printf("Description: %wS\n", pAddresses->Description);
  59.  
  60. retval = WSAStartup(MAKEWORD(2, 2), &wsaData);
  61. if (retval != 0)
  62. {
  63. printf("WSAStartup() failed with error code %d\n", WSAGetLastError());
  64. return 1;
  65. }
  66. else
  67. printf("WSAStartup() is OK...\n");
  68.  
  69.  
  70. size=256;
  71.  
  72. lp=pAddresses->FirstUnicastAddress->Address.lpSockaddr;
  73. size2=pAddresses->FirstUnicastAddress->Address.iSockaddrLength;
  74. if(WSAAddressToStringA(lp,size2,NULL,op,&size)!= 0)
  75. {
  76. printf("This thing has failed \n");
  77. printf("errordetail: %i\n", WSAGetLastError());
  78.  
  79. return 1;
  80. }
  81. else
  82. printf("\t The ip address is = %s\n", op);
  83.  
  84.  
  85. pAddresses = pAddresses->Next;
  86. }
  87. }
  88. else
  89. {
  90. printf("\n We have attempted thrice and there has been no output :( ");
  91. free(pAddresses);
  92. }
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement