Skip to content

JRuby 1.7.20.1 Socket.tcp, Addrinfo.tcp, etc. mistake port for protocol #3067

@aetherknight

Description

@aetherknight

Observed Behavior

$ jruby --version
jruby 1.7.20.1 (1.9.3p551) 2015-06-10 d7c8c27 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_55-b13 +jit [darwin-x86_64]

$ jruby -rsocket -e "sock = Socket.tcp('www.google.com', 443); p sock"
ArgumentError: unsupported protocol family `__UNKNOWN_CONSTANT__'
        initialize at org/jruby/ext/socket/RubySocket.java:191
               new at org/jruby/RubyIO.java:853
  connect_internal at /Users/bjorvis/.rbenv/versions/jruby-1.7.20.1/lib/ruby/1.9/socket.rb:39
           connect at /Users/bjorvis/.rbenv/versions/jruby-1.7.20.1/lib/ruby/1.9/socket.rb:97
               tcp at /Users/bjorvis/.rbenv/versions/jruby-1.7.20.1/lib/ruby/1.9/socket.rb:268
              each at org/jruby/RubyArray.java:1613
           foreach at /Users/bjorvis/.rbenv/versions/jruby-1.7.20.1/lib/ruby/1.9/socket.rb:178
               tcp at /Users/bjorvis/.rbenv/versions/jruby-1.7.20.1/lib/ruby/1.9/socket.rb:260
            (root) at -e:1

$ jruby -rsocket -e "ai = Addrinfo.tcp('www.google.com', 443); ai.connect; p ai"
ArgumentError: unsupported protocol family `__UNKNOWN_CONSTANT__'
        initialize at org/jruby/ext/socket/RubySocket.java:191
               new at org/jruby/RubyIO.java:853
  connect_internal at /Users/bjorvis/.rbenv/versions/jruby-1.7.20.1/lib/ruby/1.9/socket.rb:39
           connect at /Users/bjorvis/.rbenv/versions/jruby-1.7.20.1/lib/ruby/1.9/socket.rb:97
            (root) at -e:1

Root Cause

JRuby's Addrinfo implementation incorrectly returns the port number as the protocol:

$ jruby -rsocket -e "ai = Addrinfo.tcp('www.google.com', 443); puts ai.afamily; puts ai.socktype; puts ai.protocol"
2
1
443

Source 1.7.20.1:
https://github.com/jruby/jruby/blob/1.7.20.1/core/src/main/java/org/jruby/ext/socket/Addrinfo.java#L225-L228

    @JRubyMethod(notImplemented = true)
    public IRubyObject protocol(ThreadContext context) {
        return context.runtime.newFixnum(port);
    }

It still is a problem in 9.0.0.0.rc1: https://github.com/jruby/jruby/blob/9.0.0.0.rc1/core/src/main/java/org/jruby/ext/socket/Addrinfo.java#L263-L266

MRI Behavior

$ ruby --version
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.3.0]

$ ruby -rsocket -e "sock = Socket.tcp('www.google.com', 443); p sock"
#<Socket:fd 9>

$ ruby -rsocket -e "ai = Addrinfo.tcp('www.google.com', 443); ai.connect; p ai"
#<Addrinfo: 216.58.192.4:443 TCP (www.google.com)>

$ ruby -rsocket -e "ai = Addrinfo.tcp('www.google.com', 443); puts ai.afamily; puts ai.socktype; puts ai.protocol"
2
1
6

afamily 2 is PF_INET (ipv4), socktype of 1 is SOCK_STREAM, and protocol 6 is TCP (according to /etc/protocols on Mac OS X)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions