pastebin - collaborative debugging tool
rovema.kpaste.net RSS


getaddrinfoex1.c - GetAddrInfoExA() test
Posted by Anonymous on Mon 6th Nov 2023 00:48
raw | new post

  1.  
  2. /*
  3.  * getaddrinfoex1.c - GetAddrInfoExA() test
  4.  *
  5.  * Compile on cygwin 3.5.0 $ clang getaddrinfoex1.c -lws2_32 && ./a #
  6.  *
  7.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  8.  */
  9. #define UNICODE 1
  10.  
  11. #include <ws2tcpip.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. int main() {
  17.   struct addrinfoexA hints;
  18.   struct addrinfoexA *result;
  19.   int error;
  20.  
  21.   WORD wVersionRequested;
  22.   WSADATA wsaData;
  23.   int err;
  24.  
  25.   wVersionRequested = MAKEWORD(2, 2);
  26.  
  27.   err = WSAStartup(wVersionRequested, &wsaData);
  28.    
  29.   memset(&hints, 0, sizeof(hints));
  30.   hints.ai_family = AF_INET6/*AF_UNSPEC*/;
  31.   hints.ai_socktype = SOCK_STREAM;
  32.   hints.ai_protocol = IPPROTO_TCP;
  33.  
  34.   error = GetAddrInfoExA("derfwpc5131_ipv6", "2049", 0, NULL, &hints, &result, NULL, NULL, NULL, NULL);
  35.   if (error != 0) {
  36.     fprintf(stderr, "getaddrinfo: error='%ls'\n", gai_strerror(error));
  37.     exit(1);
  38.   }
  39.  
  40.   for (struct addrinfoexA *p = result; p != NULL; p = p->ai_next) {
  41.     char host[NI_MAXHOST];
  42.     char service[NI_MAXSERV];
  43.  
  44.     error = getnameinfo(p->ai_addr, p->ai_addrlen, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
  45.     if (error != 0) {
  46.       fprintf(stderr, "getnameinfo: error='%ls'\n", gai_strerror(error));
  47.       continue;
  48.     }
  49.  
  50.     printf("res='%s':'%s'\n", host, service);
  51.   }
  52.  
  53. //  FreeAddrInfoEx(result);
  54.  
  55.   return 0;
  56. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at