Project

General

Profile

Actions

Bug #678

closed

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

Added by pragdave (Dave Thomas) over 15 years ago. Updated almost 13 years ago.

Status:
Closed
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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0