Coder Social home page Coder Social logo

getaddrinfo-test's Introduction

getaddrinfo-test

This repository is to test a behavior of the C library getaddrinfo [1] is same with the Ruby's Addrinfo.getaddrinfo [2].

Ruby Addrinfo.getaddrinfo method.

On the Ruby 3.0.0 that I tested, if the /etc/hosts has setting of both IPv4 (127.0.0.1) and IPv6 (::1), the method returns the 2 Addrinfos of both IPv4 and IPv6.

$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain
::1       localhost localhost.localdomain localhost6 localhost6.localdomain6
$ irb
irb(main):001:0> require 'socket'
=> true

irb(main):002:0> Addrinfo.getaddrinfo('localhost', 0, nil, :STREAM, nil, Socket::AI_PASSIVE)
=> [#<Addrinfo: ::1 TCP (localhost)>, #<Addrinfo: 127.0.0.1 TCP (localhost)>]

As a notable behavior, Addrinfo.getaddrinfo method still returns the 2 Addrinfos when only disalbing the settings by sysctl.

$ sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
$ sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
$ sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1
irb(main):003:0> Addrinfo.getaddrinfo('localhost', 0, nil, :STREAM, nil, Socket::AI_PASSIVE)
=> [#<Addrinfo: ::1 TCP (localhost)>, #<Addrinfo: 127.0.0.1 TCP (localhost)>]

However if the /etc/hosts has the setting of the only IPv4 like this, the method returns the Addrinfo of IPv4.

# vi /etc/hosts

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
# ::1         localhost localhost.localdomain localhost6 localhost6.localdomai
$ irb
irb(main):001:0> require 'socket'
=> true
irb(main):002:0> Addrinfo.getaddrinfo('localhost', 0, nil, :STREAM, nil, Socket::AI_PASSIVE)
=> [#<Addrinfo: 127.0.0.1 TCP (localhost)>]

C getaddrinfo function.

The behavior of the C getaddrinfo function is same with the Ruby Addrinfo.getaddrinfo method.

$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain
::1       localhost localhost.localdomain localhost6 localhost6.localdomain6
$ sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0
$ sudo sysctl -w net.ipv6.conf.default.disable_ipv6=0
$ sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=0
$ gcc -o main main.c
$ ./main
addrinfo result AF_INET6
addrinfo result AF_INET
$ sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
$ sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
$ sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1
$ ./main
addrinfo result AF_INET6
addrinfo result AF_INET
# vi /etc/hosts

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
# ::1         localhost localhost.localdomain localhost6 localhost6.localdomai
$ ./main
addrinfo result AF_INET

References

  • [1] See man 3 getaddrinfo.
  • [2] See ri Addrinfo.getaddrinfo.

getaddrinfo-test's People

Contributors

junaruga avatar

Watchers

 avatar James Cloos avatar  avatar

getaddrinfo-test's Issues

Creating a simple RPM spec file to test

Creating a simple RPM spec file to check Koji and Internal Copr.

https://docs.fedoraproject.org/en-US/quick-docs/creating-rpm-packages/#creating_rpm_package

Here is the checking items.

BuildRequires: /usr/sbin/ip
 
%prep
# Debug
cat /etc/hosts
ip a
cat /proc/sys/net/ipv6/conf/all/disable_ipv6
sysctl net.ipv6.conf.all.disable_ipv6
sysctl net.ipv6.conf.default.disable_ipv6
sysctl net.ipv6.conf.lo.disable_ipv6
route -6

Here is the checking items 2.

Source0: main.c

BuildRequires: gcc
BuildRequires: ruby

%check
gcc -o main "%{SOURCE0}"
./main

ruby <<EOR
require 'socket'
p Addrinfo.getaddrinfo('localhost', 0, nil, :STREAM, nil, Socket::AI_PASSIVE)
EOR

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.