Index: ext/socket/raddrinfo.c =================================================================== --- ext/socket/raddrinfo.c (revision 28350) +++ ext/socket/raddrinfo.c (working copy) @@ -270,6 +270,13 @@ return 0; } else if (FIXNUM_P(port)) { +#if defined(__APPLE__) +/* [ruby-core:29427] it seems Mac OS X cannot accept port "0" */ + if (FIX2LONG(port) == 0) { + return 0; + } +#endif + snprintf(pbuf, len, "%ld", FIX2LONG(port)); #ifdef AI_NUMERICSERV if (flags_ptr) *flags_ptr |= AI_NUMERICSERV; @@ -299,9 +306,26 @@ char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; int additional_flags = 0; +#ifdef __APPLE__ + if (NIL_P(host) && NIL_P(port)) { + rb_raise(rb_eSocket, "At least one of hostname and servname must be non-null."); + } +#endif + hostp = host_str(host, hbuf, sizeof(hbuf), &additional_flags); portp = port_str(port, pbuf, sizeof(pbuf), &additional_flags); +#if defined(__APPLE__) +/* [ruby-core:29427] it seems Mac OS X cannot accept port "0" */ + if (hostp == NULL && portp == NULL) { + strcpy(pbuf, "0"); + portp = pbuf; +#ifdef AI_NUMERICSERV + additional_flags |= AI_NUMERICSERV; +#endif + } +#endif + if (socktype_hack && hints->ai_socktype == 0 && str_isnumber(portp)) { hints->ai_socktype = SOCK_DGRAM; } Index: test/socket/test_socket.rb =================================================================== --- test/socket/test_socket.rb (revision 28350) +++ test/socket/test_socket.rb (working copy) @@ -60,6 +60,12 @@ assert_raise(SocketError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") } end + def test_getaddrinfo_raises_no_errors_on_port_argument_of_0 + assert_nothing_raised("[ruby-core:29427]"){ Socket.getaddrinfo(Socket.gethostname, 0, Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) } + assert_nothing_raised("[ruby-core:29427]"){ TCPServer.open('localhost', 0) } + end + + def test_getnameinfo assert_raise(SocketError) { Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) } end