routes.get
This commit is contained in:
@@ -22,3 +22,6 @@ pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).to_a
|
|||||||
puts "\nDefault route is probably:"
|
puts "\nDefault route is probably:"
|
||||||
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).
|
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).
|
||||||
min_by { |route| route.dst_len }
|
min_by { |route| route.dst_len }
|
||||||
|
|
||||||
|
puts "\nTraffic to 192.168.1.1 goes out via:"
|
||||||
|
puts rt.ifname(rt.routes.get(:dst=>"192.168.1.1").oif)
|
||||||
|
@@ -128,10 +128,10 @@ module Netlink
|
|||||||
# an exception if any error message is returned, or on timeout.
|
# an exception if any error message is returned, or on timeout.
|
||||||
#
|
#
|
||||||
# (Compare: rtnl_talk in lib/libnetlink.c, with answer=NULL)
|
# (Compare: rtnl_talk in lib/libnetlink.c, with answer=NULL)
|
||||||
def cmd(type, msg, flags=NLM_F_REQUEST, timeout=@timeout, sockaddr=SOCKADDR_DEFAULT)
|
def cmd(type, msg, flags=NLM_F_REQUEST, resp_type=NLMSG_ERROR, timeout=@timeout, sockaddr=SOCKADDR_DEFAULT)
|
||||||
send_request(type, msg, flags|NLM_F_ACK, sockaddr)
|
send_request(type, msg, flags|NLM_F_ACK, sockaddr)
|
||||||
receive_responses(true, timeout) do |type,msg|
|
receive_responses(true, timeout) do |type,msg|
|
||||||
return if type == NLMSG_ERROR
|
return msg if type == resp_type
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -138,6 +138,15 @@ module Netlink
|
|||||||
def delete(opt)
|
def delete(opt)
|
||||||
iproute_modify(RTM_DELROUTE, 0, opt)
|
iproute_modify(RTM_DELROUTE, 0, opt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Get route matching given criteria
|
||||||
|
def get(msg)
|
||||||
|
msg = RT.new(msg)
|
||||||
|
raise "Missing :dst" unless msg.dst
|
||||||
|
msg.iif = index(msg.iif) if msg.iif.is_a?(String)
|
||||||
|
msg.oif = index(msg.oif) if msg.oif.is_a?(String)
|
||||||
|
@rtsocket.cmd RTM_GETROUTE, msg, NLM_F_REQUEST, RTM_NEWROUTE
|
||||||
|
end
|
||||||
|
|
||||||
def iproute_modify(code, flags, msg) #:nodoc:
|
def iproute_modify(code, flags, msg) #:nodoc:
|
||||||
msg = RT.new(msg)
|
msg = RT.new(msg)
|
||||||
|
Reference in New Issue
Block a user