summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTomoyuki Sahara <[email protected]>2014-12-26 16:00:50 +0900
committerTomoyuki Sahara <[email protected]>2014-12-26 16:00:50 +0900
commit0d05a22602a49bdaba3f304ec6eb0808f1e6c286 (patch)
tree3763fdc3b25dd6fc73cc241bcf58832f96344736
parent7b17158f88a64c97c34cd4b617bb9a0185e5fb43 (diff)
downloadmruby-0d05a22602a49bdaba3f304ec6eb0808f1e6c286.tar.gz
mruby-0d05a22602a49bdaba3f304ec6eb0808f1e6c286.zip
don't call undefined method. fixes #15.
-rw-r--r--mrblib/socket.rb7
-rw-r--r--test/socket.rb17
2 files changed, 21 insertions, 3 deletions
diff --git a/mrblib/socket.rb b/mrblib/socket.rb
index e3a1487d4..2ff144080 100644
--- a/mrblib/socket.rb
+++ b/mrblib/socket.rb
@@ -63,9 +63,9 @@ class Addrinfo
def inspect
if ipv4? or ipv6?
- if @protocol == Socket::IPPROTO_TCP
+ if @protocol == Socket::IPPROTO_TCP or (@socktype == Socket::SOCK_STREAM and @protocol == 0)
proto = 'TCP'
- elsif @protocol == Socket::IPPROTO_UDP
+ elsif @protocol == Socket::IPPROTO_UDP or (@socktype == Socket::SOCK_DGRAM and @protocol == 0)
proto = 'UDP'
else
proto = '???'
@@ -439,7 +439,8 @@ class Socket
def recvfrom(maxlen, flags=0)
msg, sa = _recvfrom(maxlen, flags)
- [ msg, _ai_to_array(Addrinfo.new(sa)) ]
+ socktype = self.getsockopt(Socket::SOL_SOCKET, Socket::SO_TYPE).int
+ [ msg, Addrinfo.new(sa, Socket::PF_UNSPEC, socktype) ]
end
def recvfrom_nonblock(*args)
diff --git a/test/socket.rb b/test/socket.rb
index b602cc15d..517f5a00c 100644
--- a/test/socket.rb
+++ b/test/socket.rb
@@ -15,3 +15,20 @@ assert('Socket::getaddrinfo') do
assert_equal Socket::SOCK_DGRAM, a[5]
assert_equal Socket::IPPROTO_UDP, a[6]
end
+
+assert('Socket#recvfrom') do
+ begin
+ sstr = "abcdefg"
+ s = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
+ c = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
+ s.bind(Socket.sockaddr_in(0, "127.0.0.1"))
+ c.send sstr, 0, s.getsockname
+ rstr, ai = s.recvfrom sstr.size
+
+ assert_equal sstr, rstr
+ assert_true "127.0.0.1", ai.ip_address
+ ensure
+ s.close rescue nil
+ c.close rescue nil
+ end
+end