Project

General

Profile

Bug #678

Socket.getservbyport should convert the port param to network byte order

Added by pragdave (Dave Thomas) almost 11 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
Backport:
[ruby-core:19454]

Description

=begin
getservbyname returns the port number in native byte order (so "telnet" returns 23).

But if you pass 23 back to getserverbyport, it fails, because it doesn't covert the param to network byte order before calling getservbyport(2).

Here's a patch that fixes it:

Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c (revision 19883)
+++ ext/socket/socket.c (working copy)
@@ -3254,7 +3254,7 @@
if (NIL_P(proto)) proto = rb_str_new2("tcp");
StringValue(proto);

  • sp = getservbyport(NUM2INT(port), StringValueCStr(proto));
  • sp = getservbyport(htons(NUM2INT(port)), StringValueCStr(proto)); if (!sp) { rb_raise(rb_eSocket, "no such service for port %d/%s", NUM2INT(port), RSTRING_PTR(proto)); } =end

History

#1

Updated by matz (Yukihiro Matsumoto) almost 11 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r19893.
=end

#2

Updated by nobu (Nobuyoshi Nakada) almost 11 years ago

=begin
Hi,

At Thu, 23 Oct 2008 07:54:55 +0900,
Dave Thomas wrote in [ruby-core:19454]:

getservbyname returns the port number in native byte order
(so "telnet" returns 23).

But if you pass 23 back to getserverbyport, it fails, because
it doesn't covert the param to network byte order before
calling getservbyport(2).

I think it should check if the port number fits with int16_t.

$ ./ruby -v -rsocket -e 'p Socket.getservbyport(2**16+23)'
ruby 1.9.0 (2008-10-23 revision 19896) [i686-linux]
"telnet"

Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c (revision 19896)
+++ ext/socket/socket.c (working copy)
@@ -3255,4 +3255,8 @@ sock_s_getservbyport(int argc, VALUE *ar
rb_scan_args(argc, argv, "11", &port, &proto);
portnum = NUM2LONG(port);

  • if (portnum != (uint16_t)portnum) {
  • const char *s = portnum > 0 ? "big" : "small";
  • rb_raise(rb_eRangeError, "integer %ld too %s to convert into `int16_t'", portnum, s);
  • } if (!NIL_P(proto)) protoname = StringValueCStr(proto);

--
Nobu Nakada

=end

#3

Updated by matz (Yukihiro Matsumoto) almost 11 years ago

=begin
Hi,

In message "Re: [ruby-core:19460] Re: [Bug #678] Socket.getservbyport should convert the port param to network byte order"
on Thu, 23 Oct 2008 14:33:39 +0900, Nobuyoshi Nakada nobu@ruby-lang.org writes:

|I think it should check if the port number fits with int16_t.

I think it's a good idea. Please check in.

                        matz.

=end

Also available in: Atom PDF