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 301681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:01:41+00:00 2026-05-12T07:01:41+00:00

Background: I’m writing a daemon that makes outgoing TCP/IP connections. It will be running

  • 0

Background:

I’m writing a daemon that makes outgoing TCP/IP connections. It will be running on machines with multiple (non-loopback) IP addresses. I’d like the users to be able to specify, in the daemon’s config file, which IP address(es) to use for outgoing connections, or * to use all.

The addresses will be used in a rotation, each connection going out from the IP address used least recently. This behavior is important, as is * being a replacement for “all” so daemons running on multiple machines can point to the same config file on a fileshare, and have each use its own set of IP addresses.

Problem:

How do I get a list of all the IP addresses a machine can make outgoing (i.e. to any other computer) connections on? Given a list of all IP addresses, how would I filter out loopback addresses?

I’m in C, and if possible I’d like to use POSIX only, but the daemon will probably only ever run on Linux boxes, so I’d accept a Linux-centric answer.

Each IP address will be available on exactly one (possibly virtual) network device and vice versa, so a way to enumerate network devices and get associated IP addresses would also suffice, though I wouldn’t really be happy about it. (Side questions: Is it even possible to associate multiple IP addresses with a single device? How ’bout the same IP under multiple devices? Not important.)

Insufficient Solutions:

  • gethostname()/gethostbyname() (as this question). Using that method, I only ever get 127.0.0.1 back (or .1.1 in Debian). I suspect this is because the hostname of the machine is in the hosts file, and that’s as far as gethostbyname() checks. (I believe that’s why in Debian I always get 127.0.1.1: Debian defaults to adding localhost as 127.0.0.1 and the machine’s hostname as 127.0.1.1 to the hosts file, right?) I’d like a solution that ignores hosts and gives me everything actually there.
  • I’ve had no more luck with getaddrinfo() than gethostname()/gethostbyname(). It seems to be bound by the same problem. I tested this passing the machine’s hostname and a NULL service (port) into it; the docs say passing a NULL hostname AND a NULL service is illegal, and this is backed up by testing. Not sure how else to ask it for everything on the machine, but I’m open to suggestions in this vein.
  • EDIT: this answer shows how to get the IP address from a device name, but doesn’t show how to enumerate the device names. Any ideas?

FINAL EDIT: I’ve accepted caskey’s answer to give him the credit for pointing me in the direction of how this needs to be done. I’ve posted my own answer listing the source code of how exactly to do it in case anyone else needs it.

  • 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-05-12T07:01:42+00:00Added an answer on May 12, 2026 at 7:01 am

    This can only be done in an operating system dependent fashion. You could try parsing the output of ‘iptables’, but the right answer for linux is to use ioctl.

    SIOCGIFCONF  takes  a  struct  ifconf *.  The ifc_buf field points to a
          buffer of length ifc_len bytes, into which the kernel writes a list of
          type struct ifreq [].
    

    The struct ifreq is documented in linux/if.h:

    struct ifreq 
    {
    #define IFHWADDRLEN     6
            union
            {
                    char    ifrn_name[IFNAMSIZ];            /* if name, e.g. "en0" */
            } ifr_ifrn;
    
            union {
                    struct  sockaddr ifru_addr;
                    struct  sockaddr ifru_dstaddr;
                    struct  sockaddr ifru_broadaddr;
                    struct  sockaddr ifru_netmask;
                    struct  sockaddr ifru_hwaddr;
                    short   ifru_flags;
                    int     ifru_ivalue;
                    int     ifru_mtu;
                    struct  ifmap ifru_map;
                    char    ifru_slave[IFNAMSIZ];   /* Just fits the size */
                    char    ifru_newname[IFNAMSIZ];
                    void *  ifru_data;
                    struct  if_settings ifru_settings;
            } ifr_ifru;
    };
    

    As you can see, it contains the address information you desire.

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

Sidebar

Related Questions

Background: My employeer at my non-programming job knows that I am an undergraduate CS
Background: I've inherited a web application that is intended to create on-the-fly connections between
Background : I'm currently writing a greasemonkey script that embeds/modifies a specific page's html.
Background: At my company we are developing a bunch applications that are using the
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background I am writing code in VS 2010, .NET 4, C#. Also, in case
Background: I have a css and a js that is used only by the
Background: I'm running a Minecraft server for friends on a VPS, and I'm attempting
Background This is only my second PyQt4 project. Developing a Windows app that has
Background I have a basic HTML page with an iframe that points to 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.