Skip to content

Commit

Permalink
macOS下编译时,无论uid==0,icmpPktListen均使用fakeboboliu的实现方式
Browse files Browse the repository at this point in the history
  • Loading branch information
tsosunchia committed Oct 6, 2024
1 parent 8774e8c commit b0c0f8d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion trace/icmp_ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (t *ICMPTracer) Execute() (*Result, error) {

var err error

t.icmpListen, err = internal.ListenICMP("ip4:1", t.SrcAddr)
t.icmpListen, err = internal.ListenICMP("ip4:icmp", t.SrcAddr)
if err != nil {
return &t.res, err
}
Expand Down
110 changes: 55 additions & 55 deletions trace/internal/icmp_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"errors"
"net"
"os"
"syscall"
"unsafe"
)
Expand All @@ -30,68 +29,69 @@ var (
)

func ListenICMP(network string, laddr string) (net.PacketConn, error) {
if os.Getuid() == 0 { // root
return net.ListenPacket(network, laddr)
} else {
if nw, ok := networkMap[network]; ok {
proto := syscall.IPPROTO_ICMP
if nw == "udp6" {
proto = syscall.IPPROTO_ICMPV6
}
// 为兼容NE,需要注释掉
//if os.Getuid() == 0 { // root
// return net.ListenPacket(network, laddr)
//} else {
if nw, ok := networkMap[network]; ok {
proto := syscall.IPPROTO_ICMP
if nw == "udp6" {
proto = syscall.IPPROTO_ICMPV6
}

var ifIndex = -1
if laddr != "" {
la := net.ParseIP(laddr)
if ifaces, err := net.Interfaces(); err == nil {
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
if ipnet.IP.Equal(la) {
ifIndex = iface.Index
break
}
var ifIndex = -1
if laddr != "" {
la := net.ParseIP(laddr)
if ifaces, err := net.Interfaces(); err == nil {
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok {
if ipnet.IP.Equal(la) {
ifIndex = iface.Index
break
}
}
}
if ifIndex == -1 {
return nil, errUnknownIface
}
} else {
return nil, err
}
if ifIndex == -1 {
return nil, errUnknownIface
}
} else {
return nil, err
}
}

isock, err := internetSocket(context.Background(), nw, nil, nil, syscall.SOCK_DGRAM, proto, "listen",
func(ctx context.Context, network, address string, c syscall.RawConn) error {
if ifIndex != -1 {
if proto == syscall.IPPROTO_ICMP {
return c.Control(func(fd uintptr) {
err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, ifIndex)
if err != nil {
return
}
})
} else {
return c.Control(func(fd uintptr) {
err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, ifIndex)
if err != nil {
return
}
})
}
isock, err := internetSocket(context.Background(), nw, nil, nil, syscall.SOCK_DGRAM, proto, "listen",
func(ctx context.Context, network, address string, c syscall.RawConn) error {
if ifIndex != -1 {
if proto == syscall.IPPROTO_ICMP {
return c.Control(func(fd uintptr) {
err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, ifIndex)
if err != nil {
return
}
})
} else {
return c.Control(func(fd uintptr) {
err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, ifIndex)
if err != nil {
return
}
})
}
return nil
})
if err != nil {
panic(err)
}
return newIPConn(isock), nil
} else {
return nil, errUnknownNetwork
}
return nil
})
if err != nil {
panic(err)
}
return newIPConn(isock), nil
} else {
return nil, errUnknownNetwork
}
//}
}
2 changes: 1 addition & 1 deletion trace/tcp_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (t *TCPTracerv6) Execute() (*Result, error) {
if err != nil {
return nil, err
}
t.icmp, err = icmp.ListenPacket("ip6:58", "::")
t.icmp, err = icmp.ListenPacket("ip6:ipv6-icmp", "::")
if err != nil {
return &t.res, err
}
Expand Down

0 comments on commit b0c0f8d

Please sign in to comment.