Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions ruby/lib/tailscale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ module Libtailscale
attach_function :TsnetSetLogFD, [:int, :int], :int
attach_function :TsnetDial, [:int, :string, :string, :pointer], :int, blocking: true
attach_function :TsnetListen, [:int, :string, :string, :pointer], :int
attach_function :close, [:int], :int
attach_function :tailscale_accept, [:int, :pointer], :int, blocking: true
attach_function :TsnetAccept, [:int, :pointer], :int, blocking: true
attach_function :TsnetGetIps, [:int, :pointer, :size_t], :int
attach_function :TsnetGetRemoteAddr, [:int, :int, :pointer, :size_t], :int
attach_function :TsnetErrmsg, [:int, :pointer, :size_t], :int
attach_function :TsnetLoopback, [:int, :pointer, :size_t, :pointer, :pointer], :int
end
Expand Down Expand Up @@ -81,20 +82,33 @@ def initialize(ts, listener)
@listener = listener
end

# Get the remote address of an accepted connection. +conn+ is the +IO+
# object returned by +accept+. Returns the remote IP address as a string.
def get_remote_addr(conn)
@ts.assert_open
buf = FFI::MemoryPointer.new(:char, 1024)
Error.check(@ts, Libtailscale::TsnetGetRemoteAddr(@listener, conn.fileno, buf, buf.size))
buf.read_string
end

# Accept a new connection. This method blocks until a new connection is
# received. An +IO+ object is returned which can be used to read and
# write.
def accept
@ts.assert_open
lio = IO.for_fd(@listener)
until IO.select([lio]).first.any?
@ts.assert_open
end
conn = FFI::MemoryPointer.new(:int)
Error.check(@ts, Libtailscale::tailscale_accept(@listener, conn))
Error.check(@ts, Libtailscale::TsnetAccept(@listener, conn))
IO::new(conn.read_int)
end

# Close the listener.
def close
@ts.assert_open
Error.check(@ts, Libtailscale::close(@listener))
IO.for_fd(@listener).close
end
end

Expand Down Expand Up @@ -228,9 +242,17 @@ def set_log_fd(log_fd)
Error.check(self, Libtailscale::TsnetSetLogFD(@t, log_fd))
end

# Get the IP addresses of this Tailscale node as an array of strings.
# The node must be started before calling this method.
def get_ips
assert_open
buf = FFI::MemoryPointer.new(:char, 1024)
Error.check(self, Libtailscale::TsnetGetIps(@t, buf, buf.size))
buf.read_string.split(",")
end

# Dial a network address. +network+ is one of "tcp" or "udp". +addr+ is the
# remote address to connect to. This method blocks until the connection is
# established.
# remote address to connect to. This method blocks until the connection is established.
def dial(network, addr)
assert_open
conn = FFI::MemoryPointer.new(:int)
Expand Down
Loading