Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:03:54+00:00 2026-06-04T19:03:54+00:00

I have some code I’m writing under cygwin (using GCC) that successfully uses gethostbyname();

  • 0

I have some code I’m writing under cygwin (using GCC) that successfully uses gethostbyname(); however when I try to use the resolver directly to retrieve the IP address of the DNS server it fails (all entries in nsaddr_list[] are null and nscount is -1).
If gethostbyname() is working, then obviously it is able to connect to the DNS server.
This code…

    if (res_init() == -1) {
        fprintf(stderr,"res_init() failed\n");
        exit(1);
    }

    if (_res.nscount <= 0) {
        fprintf(stderr,"nscount = %d\n",_res.nscount);
    }
    else {
        for(i=0;i<_res.nscount;i++) {
            fprintf(stderr, "dnssrvr: %d.%d.%d.%d\n",
                (_res.nsaddr_list[i].sin_addr.s_addr & 0xff) >> 0,
                (_res.nsaddr_list[i].sin_addr.s_addr & 0xff00) >> 8,
                (_res.nsaddr_list[i].sin_addr.s_addr & 0xff0000) >> 16,
                (_res.nsaddr_list[i].sin_addr.s_addr & 0xff000000) >> 24);
        }
    }

works on unix/linux, but returns nscount=-1 on cygwin.
Is there some trick to getting the DNS server when using cygwin/gcc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:03:55+00:00Added an answer on June 4, 2026 at 7:03 pm

    As n.m. says, on Cygwin res_init() does not populate _res.nsaddr_list if it is using the Windows resolver. It uses the Windows resolver if either /etc/resolv.conf does not exist, or /etc/resolv.conf contains options osquery.

    In my opinion this is a Cygwin bug – returning a negative nscount is bogus – but nonetheless we are stuck with working around it.

    The solution is to call GetNetworkParams() just as Cygwin does itself – here’s what I’m doing as a fallback:

    #include <windows.h>
    #include <iphlpapi.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    if (_res.nscount < 0)
    {
        ULONG buflen = 0;
        FIXED_INFO *buf = NULL;
    
        if (GetNetworkParams(NULL, &buflen) == ERROR_BUFFER_OVERFLOW)
            buf = malloc(buflen);
    
        if (buf && GetNetworkParams(buf, &buflen) == NO_ERROR)
        {
            _res.nscount = 1;
            _res.nsaddr_list[0].sin_family = AF_INET;
            _res.nsaddr_list[0].sin_addr.s_addr = inet_addr(buf->DnsServerList.IpAddress.String);
            _res.nsaddr_list[0].sin_port = htons(53);
        }
    
        free(buf);
    }
    

    You need to link against -liphlpapi for the GetNetworkParams() function.

    This only takes the first Windows DNS address, but if you want the rest of them you can follow the linked list that GetNetworkParams() returns. GetNetworkParams() only returns IPv4 addresses, I’m not sure what you’re supposed to do if the machine has an IPv6 DNS server address configured.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses ctypes to try to determine if the file
I have some code that uses subprocess to look at the logs from a
I have some code in jQuery that iterate through children in div using each().
I have some code that creates a batch using the pyglet library. The whole
I have some code that does Bitmap manipulation using the LockBits method and accessing
I have some code i'd like to refactor that uses a C# iterator (ie
I have some code here that I have helped modified, however there is a
I have some code that uses a form.submit() to open up another window when
I have some code that parses an xml file using XDocument . It retrieves
I have some code that uses a native mozilla browser and it does a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.