问题描述
我有以下代码,该代码应使用RTNETLINK套接字监视网络更改.但是,当我为接口"New Addr"或"Del Addr"设置新的IP地址时,不会显示.可能是什么问题.
I have following code, which should monitor network changes using RTNETLINK socket. However when I am setting new IP address for interface "New Addr" or "Del Addr" does not showing. What can be possible problem.
package main
import (
"fmt"
"syscall"
)
func main() {
l, _ := ListenNetlink()
for {
msgs, err := l.ReadMsgs()
if err != nil {
fmt.Println("Could not read netlink: %s", err)
}
for _, m := range msgs {
if IsNewAddr(&m) {
fmt.Println("New Addr")
}
if IsDelAddr(&m) {
fmt.Println("Del Addr")
}
}
}
}
type NetlinkListener struct {
fd int
sa *syscall.SockaddrNetlink
}
func ListenNetlink() (*NetlinkListener, error) {
groups := syscall.RTNLGRP_LINK |
syscall.RTNLGRP_IPV4_IFADDR |
syscall.RTNLGRP_IPV6_IFADDR
s, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_DGRAM,
syscall.NETLINK_ROUTE)
if err != nil {
return nil, fmt.Errorf("socket: %s", err)
}
saddr := &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Pid: uint32(0),
Groups: uint32(groups),
}
err = syscall.Bind(s, saddr)
if err != nil {
return nil, fmt.Errorf("bind: %s", err)
}
return &NetlinkListener{fd: s, sa: saddr}, nil
}
func (l *NetlinkListener) ReadMsgs() ([]syscall.NetlinkMessage, error) {
defer func() {
recover()
}()
pkt := make([]byte, 2048)
n, err := syscall.Read(l.fd, pkt)
if err != nil {
return nil, fmt.Errorf("read: %s", err)
}
msgs, err := syscall.ParseNetlinkMessage(pkt[:n])
if err != nil {
return nil, fmt.Errorf("parse: %s", err)
}
return msgs, nil
}
func IsNewAddr(msg *syscall.NetlinkMessage) bool {
if msg.Header.Type == syscall.RTM_NEWADDR {
return true
}
return false
}
func IsDelAddr(msg *syscall.NetlinkMessage) bool {
if msg.Header.Type == syscall.RTM_DELADDR {
return true
}
return false
}
func IsRelevant(msg *syscall.IfAddrmsg) bool {
if msg.Scope == syscall.RT_SCOPE_UNIVERSE ||
msg.Scope == syscall.RT_SCOPE_SITE {
return true
}
return false
}
推荐答案
For now you can use 0x10
in your code insted of 0x5
. It will work perfectly.
结果证明这根本不是bug.他们没有从 rtnetlink.h
源,并且由于冻结了syscall.go
,因此也不想在功能中添加此源.但是,他们建议使用RTNLGRP_*
,它也在 rtnetlink.h
源.但是,这两组常量变量在以下方面有所不同. RTMGRP_*
组表示位值(即:RTMGRP_IPV4_IFADDR = 0x10
),并声明为具有用户空间后退功能. RTLNGRP_*
组代表位位置,而不是位值(即:RTNLGRP_IPV4_IFADDR=0x5
),可以通过以下方式将其转换为位值:1 << (RTNLGRP_* - 1)
Turns out that it is not bug at all. They did not re declare RTMGRP_*
constant variables group from rtnetlink.h
source and do not want to add this in feature as well since syscall.go
is frozen. However they suggest using RTNLGRP_*
which is also declared in rtnetlink.h
source. However this two groups of constant variables is different in following way. RTMGRP_*
group represents bit value (i.e.: RTMGRP_IPV4_IFADDR = 0x10
) and declared for userspace backward capabilities. RTLNGRP_*
group represents bit position rather than bit value (i.e.: RTNLGRP_IPV4_IFADDR=0x5
) which can be translated to bit value by following way 1 << (RTNLGRP_* - 1)
这篇关于如何使用Go语言中的RTNETLINK套接字监视IP地址更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!