Identify all relevant ip addresses from ruby Socket.ip_address_list

Tom Freudenberg

When using rubies Socket.ip_address_list, it will return an array of addr_info (https://ruby-doc.org/stdlib-2.0.0/libdoc/socket/rdoc/Addrinfo.html)

Example:

require 'socket'

addr_infos = Socket.ip_address_list

This array could be iterated and listed by all defined ip_addresses and attributes like

addr_infos.each do |addr_info|
  puts "#{addr_info.ip_address} 
         #{addr_info.ipv4? ? 'ipv4? ' : ''}" + 
        "#{addr_info.ipv4_loopback? ? 'ipv4_loopback? ' : ''}" +
        "#{addr_info.ipv4_private? ? 'ipv4_private? ' : ''}" +
        "#{addr_info.ipv4_multicast? ? 'ipv4_multicast? ' : ''}" +
        "#{addr_info.ipv6? ? 'ipv6? ' : '' }" +
        "#{addr_info.ipv6_loopback? ? 'ipv6_loopback? ' : ''}" +
        "#{addr_info.ipv6_linklocal? ? 'ipv6_linklocal? ' : ''}" +
        "#{addr_info.ipv6_multicast? ? 'ipv6_multicast? ' : ''}" +
        "#{addr_info.ipv6_sitelocal? ? 'ipv6_sitelocal? ' : ''}" +
        "#{addr_info.ipv6_unique_local? ? 'ipv6_unique_local? ' : ''}" +
        "#{addr_info.ipv6_mc_global? ? 'ipv6_mc_global? ' : ''}" +
        "#{addr_info.ipv6_unspecified? ? 'ipv6_unspecified? ' : ''}"
end

The result will look like

127.0.0.1
       ipv4? ipv4_loopback? 
192.168.178.33
       ipv4? ipv4_private? 
1.2.4.5
       ipv4?
::1
       ipv6? ipv6_loopback?
fe80::1%lo0
       ipv6? ipv6_linklocal? 
fe80::aede:48ff:fe00:1122%en5
       ipv6? ipv6_linklocal? 
fe80::68:e785:4cfb:41e6%en0
       ipv6? ipv6_linklocal? 
fe80::50fc:46ff:fe4c:c2b4%awdl0
       ipv6? ipv6_linklocal? 
fe80::3203:d609:ff08:151d%utun0
       ipv6? ipv6_linklocal?
fd00::ffff:aaaa:bbbb:7005
       ipv6? ipv6_unique_local?
2003:ffff:4723:aaaa:bbbb:8888:269a:42a4
       ipv6?

Q: How to identify "correct" ip-addresses to bind listening services to?

IMHO it would be easy to identify the IPv4 addresses like:

IPv4 = ipv4? && (ipv4_loopback? || ipv4_private? || !(ipv4_loopback? || ipv4_private? || ipv4_multicast?))

But in case of those many ipv6_? attributes I wondering what to to check to identify the IPv6 addresses.

Is this the correct suggestion?

It is an IPv6 address when:

a. ipv6? is true and no other ipv6_...? attribute is true
b. ipv6? and ipv6_loopback? are true
c. ipv6? and ipv6_unique_local are true

Do I miss something from IPv6 addresses?

Tom Freudenberg

This is my current solution in addition to this question. Maybe some may add a comment or vote to make sure that this goes the right direction

ip_addresses_for_host = []
Socket.ip_address_list.each do |a|
# test for all local valid ipv4 and ipv6 ip_addresses
# check question on stackoverflow for details
# https://stackoverflow.com/questions/59770803/identify-all-relevant-ip-addresses-from-ruby-socket-ip-address-list
ip_addresses_for_host << a.ip_address if \
  (a.ipv4? &&
    (a.ipv4_loopback? || a.ipv4_private? ||
     !(a.ipv4_loopback? || a.ipv4_private? || a.ipv4_multicast?)
    )
  ) ||
  (a.ipv6? &&
    (a.ipv6_loopback? || a.ipv6_unique_local? ||
     !(a.ipv6_loopback? || a.ipv6_unique_local? || a.ipv6_linklocal? || a.ipv6_multicast? || a.ipv6_sitelocal? ||
       a.ipv6_mc_global? || a.ipv6_mc_linklocal? || a.ipv6_mc_nodelocal? || a.ipv6_mc_orglocal? || a.ipv6_mc_sitelocal? ||
       a.ipv6_v4compat? || a.ipv6_v4mapped? || a.ipv6_unspecified?)
    )
  )
end

This will resolve for IPv4:

a. 127.0.0.1
b. private addresses like 192.168./16, 10./8, ...
c. other ipv4 addresses like 1.2.3.4

and for IPv6:

a. ::1
b. fc00::
c. abcd::

Appreciate if someone would add more knowledge.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Getting all IP addresses of a hostname

分類Dev

Setting address of whois service for ruby whois gem for IP addresses

分類Dev

Parsing a Text File of IP addresses into an Array with Ruby?

分類Dev

Node.js - Get list of all connected clients IP Addresses?

分類Dev

Tool to group a list of IP addresses into IP ranges

分類Dev

How to obtain all available Elastic IP addresses in boto3

分類Dev

How to refer to all IPv6 IP addresses using mask?

分類Dev

What are these weird IP addresses?

分類Dev

IP addresses in distributed systems

分類Dev

How to whilelist specific IP addresses from AWS WAF

分類Dev

How are local ip addresses separated from public ones?

分類Dev

Reject all connections except from a specific IP

分類Dev

Match multiple ip addresses to one endpoint

分類Dev

Bind to multiple ip addresses in a single docker container

分類Dev

open ip addresses for redshift and data factory

分類Dev

How to find valid IP addresses of a network /16

分類Dev

How to grep specific IP Addresses using regex?

分類Dev

Reactive Form Validity based on IP Addresses RegEx

分類Dev

Local network device changing other IP addresses

分類Dev

Unable to allow specific ip addresses in ufw

分類Dev

busybox regular expression for group of IP addresses

分類Dev

2 ip addresses for 2 Server on 1 computer?

分類Dev

List of Active Directory DNS servers IP addresses in an SSM document

分類Dev

extracting ip address from a file

分類Dev

How to monitor ip address change using RTNETLINK socket in go language

分類Dev

Python socket gethostbyname() returns only one IP address

分類Dev

Python - Using socket to send data to every IP address on a network

分類Dev

DHCPD logs show PC's requesting IP addresses from router when they are turned off. Are our log files incorrect?

分類Dev

Identify and replace minimum value from a numeric column present in all dataframes in a list of dataframes

Related 関連記事

  1. 1

    Getting all IP addresses of a hostname

  2. 2

    Setting address of whois service for ruby whois gem for IP addresses

  3. 3

    Parsing a Text File of IP addresses into an Array with Ruby?

  4. 4

    Node.js - Get list of all connected clients IP Addresses?

  5. 5

    Tool to group a list of IP addresses into IP ranges

  6. 6

    How to obtain all available Elastic IP addresses in boto3

  7. 7

    How to refer to all IPv6 IP addresses using mask?

  8. 8

    What are these weird IP addresses?

  9. 9

    IP addresses in distributed systems

  10. 10

    How to whilelist specific IP addresses from AWS WAF

  11. 11

    How are local ip addresses separated from public ones?

  12. 12

    Reject all connections except from a specific IP

  13. 13

    Match multiple ip addresses to one endpoint

  14. 14

    Bind to multiple ip addresses in a single docker container

  15. 15

    open ip addresses for redshift and data factory

  16. 16

    How to find valid IP addresses of a network /16

  17. 17

    How to grep specific IP Addresses using regex?

  18. 18

    Reactive Form Validity based on IP Addresses RegEx

  19. 19

    Local network device changing other IP addresses

  20. 20

    Unable to allow specific ip addresses in ufw

  21. 21

    busybox regular expression for group of IP addresses

  22. 22

    2 ip addresses for 2 Server on 1 computer?

  23. 23

    List of Active Directory DNS servers IP addresses in an SSM document

  24. 24

    extracting ip address from a file

  25. 25

    How to monitor ip address change using RTNETLINK socket in go language

  26. 26

    Python socket gethostbyname() returns only one IP address

  27. 27

    Python - Using socket to send data to every IP address on a network

  28. 28

    DHCPD logs show PC's requesting IP addresses from router when they are turned off. Are our log files incorrect?

  29. 29

    Identify and replace minimum value from a numeric column present in all dataframes in a list of dataframes

ホットタグ

アーカイブ