Class: Socket
Defined Under Namespace
Modules: Constants, Foreign, ListenAndAccept
Classes: Option, Servent, SockAddr_In, SockAddr_Un
Constant Summary
collapse
- FFI =
Rubinius::FFI
Class Method Summary
collapse
-
.get_protocol_family(family) ⇒ Object
-
.get_socket_type(type) ⇒ Object
-
.getaddrinfo(host, service, family = 0, socktype = 0, protocol = 0, flags = 0) ⇒ Object
-
.gethostbyname(hostname) ⇒ Object
-
.gethostname ⇒ Object
-
.getnameinfo(sockaddr, flags = 0) ⇒ Object
-
.getservbyname(service, proto = 'tcp') ⇒ Object
-
.pack_sockaddr_in(port, host, type = Socket::SOCK_DGRAM, flags = 0) ⇒ Object
(also: sockaddr_in)
-
.pack_sockaddr_un(file) ⇒ Object
(also: sockaddr_un)
-
.socketpair(domain, type, protocol, klass = self) ⇒ Object
(also: pair)
-
.unpack_sockaddr_in(sockaddr) ⇒ Object
-
.unpack_sockaddr_un(addr) ⇒ Object
Instance Method Summary
collapse
#accept, #accept_nonblock, #listen
Methods inherited from BasicSocket
#close_read, #close_write, do_not_reverse_lookup, #do_not_reverse_lookup, do_not_reverse_lookup=, #do_not_reverse_lookup=, for_fd, from_descriptor, #from_descriptor, #getpeername, #getsockname, #getsockopt, #recv, #recv_nonblock, #recvfrom, #send, #setsockopt, #shutdown
Constructor Details
#initialize(family, socket_type, protocol = 0) ⇒ Socket
Returns a new instance of Socket.
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
|
# File 'lib/rubysl/socket.rb', line 1023
def initialize(family, socket_type, protocol=0)
@no_reverse_lookup = self.class.do_not_reverse_lookup
family = self.class.get_protocol_family(family)
socket_type = self.class.get_socket_type(socket_type)
descriptor = Socket::Foreign.socket family, socket_type, protocol
Errno.handle 'socket(2)' if descriptor < 0
IO.setup self, descriptor, nil, true
end
|
Class Method Details
.get_protocol_family(family) ⇒ Object
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
|
# File 'lib/rubysl/socket.rb', line 1072
def self.get_protocol_family(family)
case family
when Fixnum
return family
when String
when Symbol
family = family.to_s
else
family = StringValue(family)
end
family = "PF_#{family}" unless family[0, 3] == "PF_"
Socket::Constants.const_get family
end
|
.get_socket_type(type) ⇒ Object
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
|
# File 'lib/rubysl/socket.rb', line 1088
def self.get_socket_type(type)
if type.kind_of? String
if type.prefix? "SOCK_"
begin
type = Socket::Constants.const_get(type)
rescue NameError
raise SocketError, "unknown socket type #{type}"
end
else
raise SocketError, "unknown socket type #{type}"
end
end
if type.kind_of? Symbol
begin
type = Socket::Constants.const_get("SOCK_#{type}")
rescue NameError
raise SocketError, "unknown socket type #{type}"
end
end
type
end
|
.getaddrinfo(host, service, family = 0, socktype = 0, protocol = 0, flags = 0) ⇒ Object
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
|
# File 'lib/rubysl/socket.rb', line 830
def self.getaddrinfo(host, service, family = 0, socktype = 0,
protocol = 0, flags = 0)
if service
if service.kind_of? Fixnum
service = service.to_s
else
service = StringValue(service)
end
end
addrinfos = Socket::Foreign.getaddrinfo(host, service, family, socktype,
protocol, flags)
addrinfos.map do |ai|
addrinfo = []
addrinfo << Socket::Constants::AF_TO_FAMILY[ai[1]]
sockaddr = Foreign.unpack_sockaddr_in ai[4], !BasicSocket.do_not_reverse_lookup
addrinfo << sockaddr.pop
addrinfo.concat sockaddr
addrinfo << ai[1]
addrinfo << ai[2]
addrinfo << ai[3]
addrinfo
end
end
|
.gethostbyname(hostname) ⇒ Object
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
|
# File 'lib/rubysl/socket.rb', line 894
def self.gethostbyname(hostname)
addrinfos = Socket.getaddrinfo(hostname, nil)
hostname = addrinfos.first[2]
family = addrinfos.first[4]
addresses = []
alternatives = []
addrinfos.each do |a|
alternatives << a[2] unless a[2] == hostname
if a[4] == family
sockaddr = Socket.sockaddr_in(1, a[3])
if family == AF_INET
offset = FFI.config("sockaddr_in.sin_addr.offset")
size = FFI.config("sockaddr_in.sin_addr.size")
addresses << sockaddr.byteslice(offset, size)
elsif family == AF_INET6
offset = FFI.config("sockaddr_in6.sin6_addr.offset")
size = FFI.config("sockaddr_in6.sin6_addr.size")
addresses << sockaddr.byteslice(offset, size)
else
addresses << a[3]
end
end
end
[hostname, alternatives.uniq, family] + addresses.uniq
end
|
.gethostname ⇒ Object
887
888
889
890
891
892
|
# File 'lib/rubysl/socket.rb', line 887
def self.gethostname
FFI::MemoryPointer.new :char, 1024 do |mp|
Socket::Foreign.gethostname(mp, 1024)
return mp.read_string
end
end
|
.getnameinfo(sockaddr, flags = 0) ⇒ Object
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
|
# File 'lib/rubysl/socket.rb', line 858
def self.getnameinfo(sockaddr, flags = 0)
port = nil
host = nil
family = Socket::AF_UNSPEC
if sockaddr.is_a?(Array)
if sockaddr.size == 3
af = sockaddr[0]
port = sockaddr[1]
host = sockaddr[2]
elsif sockaddr.size == 4
af = sockaddr[0]
port = sockaddr[1]
host = sockaddr[3] || sockaddr[2]
else
raise ArgumentError, "array size should be 3 or 4, #{sockaddr.size} given"
end
if family == "AF_INET"
family = Socket::AF_INET
elsif family == "AF_INET6"
family = Socket::AF_INET6
end
sockaddr = Socket::Foreign.pack_sockaddr_in(host, port, family, Socket::SOCK_DGRAM, 0)
end
family, port, host, ip = Socket::Foreign.getnameinfo(sockaddr, flags)
[host, port]
end
|
.getservbyname(service, proto = 'tcp') ⇒ Object
941
942
943
944
945
946
947
948
949
950
951
952
953
954
|
# File 'lib/rubysl/socket.rb', line 941
def self.getservbyname(service, proto='tcp')
FFI::MemoryPointer.new :char, service.length + 1 do |svc|
FFI::MemoryPointer.new :char, proto.length + 1 do |prot|
svc.write_string(service + "\0")
prot.write_string(proto + "\0")
fn = Socket::Foreign.getservbyname(svc, prot)
raise SocketError, "no such service #{service}/#{proto}" if fn.nil?
s = Servent.new(fn.read_string(Servent.size))
return Socket::Foreign.ntohs(s[:s_port])
end
end
end
|
.pack_sockaddr_in(port, host, type = Socket::SOCK_DGRAM, flags = 0) ⇒ Object
Also known as:
sockaddr_in
956
957
958
|
# File 'lib/rubysl/socket.rb', line 956
def self.pack_sockaddr_in(port, host, type = Socket::SOCK_DGRAM, flags = 0)
Socket::Foreign.pack_sockaddr_in host, port, Socket::AF_UNSPEC, type, flags
end
|
.pack_sockaddr_un(file) ⇒ Object
Also known as:
sockaddr_un
1002
1003
1004
|
# File 'lib/rubysl/socket.rb', line 1002
def self.pack_sockaddr_un(file)
SockAddr_Un.new(file).to_s
end
|
.socketpair(domain, type, protocol, klass = self) ⇒ Object
Also known as:
pair
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
|
# File 'lib/rubysl/socket.rb', line 972
def self.socketpair(domain, type, protocol, klass=self)
if domain.kind_of? String
if domain.prefix? "AF_" or domain.prefix? "PF_"
begin
domain = Socket::Constants.const_get(domain)
rescue NameError
raise SocketError, "unknown socket domain #{domani}"
end
else
raise SocketError, "unknown socket domain #{domani}"
end
end
type = get_socket_type(type)
FFI::MemoryPointer.new :int, 2 do |mp|
Socket::Foreign.socketpair(domain, type, protocol, mp)
fd0, fd1 = mp.read_array_of_int(2)
[ klass.from_descriptor(fd0), klass.from_descriptor(fd1) ]
end
end
|
.unpack_sockaddr_in(sockaddr) ⇒ Object
960
961
962
963
964
965
966
967
968
969
970
|
# File 'lib/rubysl/socket.rb', line 960
def self.unpack_sockaddr_in(sockaddr)
host, address, port = Socket::Foreign.unpack_sockaddr_in sockaddr, false
return [port, address]
rescue SocketError => e
if e.message =~ /ai_family not supported/ then
raise ArgumentError, 'not an AF_INET/AF_INET6 sockaddr'
else
raise e
end
end
|
.unpack_sockaddr_un(addr) ⇒ Object
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
|
# File 'lib/rubysl/socket.rb', line 1006
def self.unpack_sockaddr_un(addr)
if addr.bytesize > FFI.config("sockaddr_un.sizeof")
raise TypeError, "too long sockaddr_un - #{addr.bytesize} longer than #{FFI.config("sockaddr_un.sizeof")}"
end
struct = SockAddr_Un.new
struct.pointer.write_string(addr, addr.bytesize)
struct[:sun_path]
end
|
Instance Method Details
#bind(server_sockaddr) ⇒ Object
1034
1035
1036
1037
1038
|
# File 'lib/rubysl/socket.rb', line 1034
def bind(server_sockaddr)
err = Socket::Foreign.bind(descriptor, server_sockaddr)
Errno.handle 'bind(2)' unless err == 0
err
end
|
#connect(sockaddr, extra = nil) ⇒ Object
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
|
# File 'lib/rubysl/socket.rb', line 1041
def connect(sockaddr, =nil)
if
sockaddr = Socket.pack_sockaddr_in sockaddr,
else
sockaddr = StringValue(sockaddr)
end
status = Socket::Foreign.connect descriptor, sockaddr
if status < 0
begin
Errno.handle "connect(2)"
rescue Errno::EISCONN
return 0
end
end
return 0
end
|
#connect_nonblock(sockaddr) ⇒ Object
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
|
# File 'lib/rubysl/socket.rb', line 1061
def connect_nonblock(sockaddr)
fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
status = Socket::Foreign.connect descriptor, StringValue(sockaddr)
if status < 0
Errno.handle "connect(2)"
end
return status
end
|