Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
a90d8b8a
Commit
a90d8b8a
authored
Feb 24, 2014
by
Tomoyuki Sahara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Socket.getaddrinfo.
parent
b3c6075d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
21 deletions
+82
-21
mrblib/socket.rb
mrblib/socket.rb
+29
-21
test/ipsocket.rb
test/ipsocket.rb
+40
-0
test/socket.rb
test/socket.rb
+13
-0
No files found.
mrblib/socket.rb
View file @
a90d8b8a
...
...
@@ -139,6 +139,21 @@ class Addrinfo
attr_reader
:protocol
attr_reader
:socktype
def
_to_array
case
@family
when
Socket
::
AF_INET
s
=
"AF_INET"
when
Socket
::
AF_INET6
s
=
"AF_INET6"
when
Socket
::
AF_UNIX
s
=
"AF_UNIX"
else
s
=
"(unknown AF)"
end
addr
,
port
=
self
.
getnameinfo
(
Socket
::
NI_NUMERICHOST
|
Socket
::
NI_NUMERICSERV
)
[
s
,
port
.
to_i
,
addr
,
addr
]
end
def
to_sockaddr
@sockaddr
end
...
...
@@ -189,35 +204,21 @@ class BasicSocket
end
class
IPSocket
def
self
.
getddress
(
host
)
def
self
.
get
a
ddress
(
host
)
Addrinfo
.
ip
(
host
).
ip_address
end
def
_ai_to_array
(
ai
)
case
ai
.
afamily
when
Socket
::
AF_INET
s
=
"AF_INET"
when
Socket
::
AF_INET6
s
=
"AF_INET6"
when
Socket
::
AF_UNIX
s
=
"AF_UNIX"
else
s
=
"(unknown AF)"
end
[
s
,
ai
.
ip_port
,
ai
.
ip_address
,
ai
.
ip_address
]
end
def
addr
_ai_to_array
(
Addrinfo
.
new
(
self
.
getsockname
))
Addrinfo
.
new
(
self
.
getsockname
).
_to_array
end
def
peeraddr
_ai_to_array
(
Addrinfo
.
new
(
self
.
getpeername
))
Addrinfo
.
new
(
self
.
getpeername
).
_to_array
end
def
recvfrom
(
maxlen
,
flags
=
0
)
msg
,
sa
=
_recvfrom
(
maxlen
,
flags
)
[
msg
,
_ai_to_array
(
Addrinfo
.
new
(
sa
))
]
[
msg
,
Addrinfo
.
new
(
sa
).
_to_array
]
end
end
...
...
@@ -328,9 +329,16 @@ class Socket
#def self.accept_loop
# def self.getaddrinfo
# by Addrinfo.getaddrinfo
#end
def
self
.
getaddrinfo
(
nodename
,
servname
,
family
=
nil
,
socktype
=
nil
,
protocol
=
nil
,
flags
=
0
)
Addrinfo
.
getaddrinfo
(
nodename
,
servname
,
family
,
socktype
,
protocol
,
flags
).
map
{
|
ai
|
ary
=
ai
.
_to_array
ary
[
2
]
=
nodename
ary
[
4
]
=
ai
.
afamily
ary
[
5
]
=
ai
.
socktype
ary
[
6
]
=
ai
.
protocol
ary
}
end
#def self.getnameinfo
#def self.ip_address_list
...
...
test/ipsocket.rb
0 → 100644
View file @
a90d8b8a
# Note: most of tests below will fail if UDPSocket is broken.
assert
(
'IPSocket.getaddress'
)
do
l
=
IPSocket
.
getaddress
(
"localhost"
)
assert_true
(
l
==
"127.0.0.1"
or
l
==
"::1"
)
end
assert
(
'IPSocket.addr'
)
do
localhost
=
"127.0.0.1"
s
=
UDPSocket
.
new
s
.
bind
(
localhost
,
0
)
port
=
Addrinfo
.
new
(
s
.
getsockname
).
ip_port
a
=
s
.
addr
assert_equal
"AF_INET"
,
a
[
0
]
assert_equal
port
,
a
[
1
]
assert_equal
localhost
,
a
[
2
]
assert_equal
localhost
,
a
[
3
]
s
.
close
true
end
assert
(
'IPSocket.peeraddr'
)
do
localhost
=
"127.0.0.1"
server
=
UDPSocket
.
new
server
.
bind
(
localhost
,
0
)
port
=
server
.
local_address
.
ip_port
client
=
UDPSocket
.
new
client
.
connect
(
localhost
,
port
)
a
=
client
.
peeraddr
assert_equal
"AF_INET"
,
a
[
0
]
assert_equal
port
,
a
[
1
]
assert_equal
localhost
,
a
[
2
]
assert_equal
localhost
,
a
[
3
]
client
.
close
server
.
close
true
end
test/socket.rb
View file @
a90d8b8a
...
...
@@ -133,3 +133,16 @@ assert('Socket.gethostname') do
assert_true
(
Socket
.
gethostname
.
is_a?
String
)
end
assert
(
'Socket::getaddrinfo'
)
do
ret
=
Socket
.
getaddrinfo
(
"localhost"
,
"domain"
,
Socket
::
AF_INET
,
Socket
::
SOCK_DGRAM
)
assert_true
ret
.
size
>=
1
a
=
ret
[
0
]
assert_equal
"AF_INET"
,
a
[
0
]
assert_equal
53
,
a
[
1
]
# documents says it's a hostname but CRuby returns an address
#assert_equal "127.0.0.1", a[2]
assert_equal
"127.0.0.1"
,
a
[
3
]
assert_equal
Socket
::
AF_INET
,
a
[
4
]
assert_equal
Socket
::
SOCK_DGRAM
,
a
[
5
]
assert_equal
Socket
::
IPPROTO_UDP
,
a
[
6
]
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment