要获取某些RTF_*标志的值,可以使用什么系统调用?

huangapple go评论110阅读模式
英文:

what syscall to use in order to obtain the value of some RTF_* flags

问题

使用go,我想从netstat(1)的手册页面中获取一些RTF_*标志的值(UGHS):

  1. G RTF_GATEWAY 目标需要通过中间人进行转发
  2. H RTF_HOST 主机条目(否则为网络条目)
  3. S RTF_STATIC 手动添加
  4. U RTF_UP 可用路由

你知道我可以使用哪些系统调用/方法来获取这些值吗?我看到它们在https://golang.org/pkg/syscall/中被声明,但我想知道如何使用它们?

我需要找到添加到路由表中的网关的IP,主要是在连接到VPN时(在macOS、FreeBSD中使用netstat):

  1. netstat -rna -f inet | grep UGHS | awk '{print $1}'

有什么建议吗?

英文:

Using go, I would like to obtain the value of some RTF_* flags, (UGHS) from netstat(1) man page:

  1. G RTF_GATEWAY Destination requires forwarding by intermediary
  2. H RTF_HOST Host entry (net otherwise)
  3. S RTF_STATIC Manually added
  4. U RTF_UP Route usable

Any idea of what syscall/methods could I use to retrieve does values? I see they are declared https://golang.org/pkg/syscall/ but would like to know to use them?

I need this to find the IP of gateways added to the route table, mainly when connecting to VPN's, currently using netstat for this (withing macOS, FreeBSD):

  1. netstat -rna -f inet | grep UGHS | awk '{print $1}'

Any ideas?

答案1

得分: 3

根据@JimB的建议,使用route包,我能够查询当前路由并获取与特定标志匹配的IP地址,例如UGSHUGSc

基本示例代码:

  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "syscall"
  6. "golang.org/x/net/route"
  7. )
  8. const (
  9. UGSH = syscall.RTF_UP | syscall.RTF_GATEWAY | syscall.RTF_STATIC | syscall.RTF_HOST
  10. UGSc = syscall.RTF_UP | syscall.RTF_GATEWAY | syscall.RTF_STATIC | syscall.RTF_PRCLONING
  11. )
  12. func main() {
  13. if rib, err := route.FetchRIB(syscall.AF_UNSPEC, route.RIBTypeRoute, 0); err == nil {
  14. if msgs, err := route.ParseRIB(route.RIBTypeRoute, rib); err == nil {
  15. for _, msg := range msgs {
  16. m := msg.(*route.RouteMessage)
  17. if m.Flags == UGSH || m.Flags == UGSc {
  18. var ip net.IP
  19. switch a := m.Addrs[syscall.AF_UNSPEC].(type) {
  20. case *route.Inet4Addr:
  21. ip = net.IPv4(a.IP[0], a.IP[1], a.IP[2], a.IP[3])
  22. case *route.Inet6Addr:
  23. ip = make(net.IP, net.IPv6len)
  24. copy(ip, a.IP[:])
  25. }
  26. fmt.Printf("ip = %s\n", ip)
  27. }
  28. }
  29. }
  30. }
  31. }
英文:

As @JimB suggested by using the route package I was able to query the current routes and get only IP's matching certain flags, in this case" UGSH, UGSc.

Basic example code:

  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "syscall"
  6. "golang.org/x/net/route"
  7. )
  8. const (
  9. UGSH = syscall.RTF_UP | syscall.RTF_GATEWAY | syscall.RTF_STATIC | syscall.RTF_HOST
  10. UGSc = syscall.RTF_UP | syscall.RTF_GATEWAY | syscall.RTF_STATIC | syscall.RTF_PRCLONING
  11. )
  12. func main() {
  13. if rib, err := route.FetchRIB(syscall.AF_UNSPEC, route.RIBTypeRoute, 0); err == nil {
  14. if msgs, err := route.ParseRIB(route.RIBTypeRoute, rib); err == nil {
  15. for _, msg := range msgs {
  16. m := msg.(*route.RouteMessage)
  17. if m.Flags == UGSH || m.Flags == UGSc {
  18. var ip net.IP
  19. switch a := m.Addrs[syscall.AF_UNSPEC].(type) {
  20. case *route.Inet4Addr:
  21. ip = net.IPv4(a.IP[0], a.IP[1], a.IP[2], a.IP[3])
  22. case *route.Inet6Addr:
  23. ip = make(net.IP, net.IPv6len)
  24. copy(ip, a.IP[:])
  25. }
  26. fmt.Printf("ip = %s\n", ip)
  27. }
  28. }
  29. }
  30. }
  31. }

答案2

得分: 0

strace netstat的等效命令(在MacOS上是dtruss,请参考https://opensourcehacker.com/2011/12/02/osx-strace-equivalent-dtruss-seeing-inside-applications-what-they-do-and-why-they-hang/)将给出netstat所进行的系统调用列表,您可以根据需要确定您需要进行的系统调用。

英文:

The equivalent of strace netstat (dtruss on MacOS, see https://opensourcehacker.com/2011/12/02/osx-strace-equivalent-dtruss-seeing-inside-applications-what-they-do-and-why-they-hang/) should give you a list of system calls it makes and you can decide what system calls you need to make for your problem.

huangapple
  • 本文由 发表于 2017年3月21日 22:46:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/42930401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定