ping: Fix read ipv4 header on darwin

This commit is contained in:
世界
2025-08-25 01:19:52 +08:00
parent fe4e54bb0d
commit c089ffbd6c
2 changed files with 25 additions and 0 deletions

View File

@@ -315,6 +315,10 @@ func (b IPv4) Flags() uint8 {
return uint8(binary.BigEndian.Uint16(b[flagsFO:]) >> 13) return uint8(binary.BigEndian.Uint16(b[flagsFO:]) >> 13)
} }
func (b IPv4) FlagsDarwinRaw() uint8 {
return uint8(binary.BigEndian.Uint16(b[flagsFO:]) >> 13)
}
// More returns whether the more fragments flag is set. // More returns whether the more fragments flag is set.
func (b IPv4) More() bool { func (b IPv4) More() bool {
return b.Flags()&IPv4FlagMoreFragments != 0 return b.Flags()&IPv4FlagMoreFragments != 0
@@ -330,11 +334,19 @@ func (b IPv4) FragmentOffset() uint16 {
return binary.BigEndian.Uint16(b[flagsFO:]) << 3 return binary.BigEndian.Uint16(b[flagsFO:]) << 3
} }
func (b IPv4) FragmentOffsetDarwinRaw() uint16 {
return binary.NativeEndian.Uint16(b[flagsFO:]) << 3
}
// TotalLength returns the "total length" field of the IPv4 header. // TotalLength returns the "total length" field of the IPv4 header.
func (b IPv4) TotalLength() uint16 { func (b IPv4) TotalLength() uint16 {
return binary.BigEndian.Uint16(b[IPv4TotalLenOffset:]) return binary.BigEndian.Uint16(b[IPv4TotalLenOffset:])
} }
func (b IPv4) TotalLengthDarwinRaw() uint16 {
return binary.NativeEndian.Uint16(b[IPv4TotalLenOffset:]) + uint16(b.HeaderLength())
}
// Checksum returns the checksum field of the IPv4 header. // Checksum returns the checksum field of the IPv4 header.
func (b IPv4) Checksum() uint16 { func (b IPv4) Checksum() uint16 {
return binary.BigEndian.Uint16(b[xsum:]) return binary.BigEndian.Uint16(b[xsum:])
@@ -428,6 +440,10 @@ func (b IPv4) SetTotalLength(totalLength uint16) {
binary.BigEndian.PutUint16(b[IPv4TotalLenOffset:], totalLength) binary.BigEndian.PutUint16(b[IPv4TotalLenOffset:], totalLength)
} }
func (b IPv4) SetTotalLengthDarwinRaw(totalLength uint16) {
binary.NativeEndian.PutUint16(b[IPv4TotalLenOffset:], totalLength)
}
// SetChecksum sets the checksum field of the IPv4 header. // SetChecksum sets the checksum field of the IPv4 header.
func (b IPv4) SetChecksum(v uint16) { func (b IPv4) SetChecksum(v uint16) {
checksum.Put(b[xsum:], v) checksum.Put(b[xsum:], v)
@@ -440,6 +456,11 @@ func (b IPv4) SetFlagsFragmentOffset(flags uint8, offset uint16) {
binary.BigEndian.PutUint16(b[flagsFO:], v) binary.BigEndian.PutUint16(b[flagsFO:], v)
} }
func (b IPv4) SetFlagsFragmentOffsetDarwinRaw(flags uint8, offset uint16) {
v := (uint16(flags) << 13) | (offset >> 3)
binary.NativeEndian.PutUint16(b[flagsFO:], v)
}
// SetID sets the identification field. // SetID sets the identification field.
func (b IPv4) SetID(v uint16) { func (b IPv4) SetID(v uint16) {
binary.BigEndian.PutUint16(b[id:], v) binary.BigEndian.PutUint16(b[id:], v)

View File

@@ -159,6 +159,10 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
} }
if !c.destination.Is6() { if !c.destination.Is6() {
ipHdr := header.IPv4(buffer.Bytes()) ipHdr := header.IPv4(buffer.Bytes())
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
ipHdr.SetTotalLength(ipHdr.TotalLengthDarwinRaw())
ipHdr.SetFlagsFragmentOffset(ipHdr.FlagsDarwinRaw(), ipHdr.FragmentOffsetDarwinRaw())
}
if !ipHdr.IsValid(buffer.Len()) { if !ipHdr.IsValid(buffer.Len()) {
return E.New("invalid IPv4 header received") return E.New("invalid IPv4 header received")
} }