Skip to content

Commit 5d6baac

Browse files
committed
icmp: 修复 windows 系统中 ipv6 地址显示错误
1 parent f428607 commit 5d6baac

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

pkg/ping/icmp_windows.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ type icmp_echo_reply struct {
3939
}
4040

4141
type ipv6_address_ex struct {
42-
sin6_port uint16
43-
sin6_flowinfo uint32
44-
sin6_addr [16]byte
45-
sin6_scope_id uint32
42+
// go 语言不支持手动指定内存对齐方式,sin6_flowinfo 字段错位
43+
data [26]byte
44+
// sin6_port uint16
45+
// sin6_flowinfo uint32
46+
// sin6_addr [16]byte
47+
// sin6_scope_id uint32
4648
}
4749

4850
var (
@@ -81,7 +83,7 @@ func (this *IcmpPing) ping_rootless(ctx context.Context) IPingResult {
8183
return this.errorResult(errors.New("IcmpSendEcho failed"))
8284
}
8385
recvmsg := (*icmpv6_echo_reply)(unsafe.Pointer(&recv[0]))
84-
var ip net.IP = recvmsg.address.sin6_addr[:]
86+
var ip net.IP = recvmsg.address.data[6:22]
8587
if recvmsg.status != 0 {
8688
return this.errorResult(fmt.Errorf("%s: %s", ip.String(), icmpStatusToString(recvmsg.status)))
8789
}
@@ -152,7 +154,7 @@ func IcmpSendEcho(handle syscall.Handle, ip net.IP, data []byte, timeout time.Du
152154
if n == 0 {
153155
return nil
154156
}
155-
return buf[:n]
157+
return buf
156158
}
157159

158160
func Icmp6CreateFile() syscall.Handle {
@@ -192,7 +194,7 @@ func Icmp6SendEcho(handle syscall.Handle, ip net.IP, data []byte, timeout time.D
192194
if n == 0 {
193195
return nil
194196
}
195-
return buf[:n]
197+
return buf
196198
}
197199

198200
func icmpStatusToString(status uint32) string {

0 commit comments

Comments
 (0)