Skip to content

Commit c089ffb

Browse files
committed
ping: Fix read ipv4 header on darwin
1 parent fe4e54b commit c089ffb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

internal/gtcpip/header/ipv4.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ func (b IPv4) Flags() uint8 {
315315
return uint8(binary.BigEndian.Uint16(b[flagsFO:]) >> 13)
316316
}
317317

318+
func (b IPv4) FlagsDarwinRaw() uint8 {
319+
return uint8(binary.BigEndian.Uint16(b[flagsFO:]) >> 13)
320+
}
321+
318322
// More returns whether the more fragments flag is set.
319323
func (b IPv4) More() bool {
320324
return b.Flags()&IPv4FlagMoreFragments != 0
@@ -330,11 +334,19 @@ func (b IPv4) FragmentOffset() uint16 {
330334
return binary.BigEndian.Uint16(b[flagsFO:]) << 3
331335
}
332336

337+
func (b IPv4) FragmentOffsetDarwinRaw() uint16 {
338+
return binary.NativeEndian.Uint16(b[flagsFO:]) << 3
339+
}
340+
333341
// TotalLength returns the "total length" field of the IPv4 header.
334342
func (b IPv4) TotalLength() uint16 {
335343
return binary.BigEndian.Uint16(b[IPv4TotalLenOffset:])
336344
}
337345

346+
func (b IPv4) TotalLengthDarwinRaw() uint16 {
347+
return binary.NativeEndian.Uint16(b[IPv4TotalLenOffset:]) + uint16(b.HeaderLength())
348+
}
349+
338350
// Checksum returns the checksum field of the IPv4 header.
339351
func (b IPv4) Checksum() uint16 {
340352
return binary.BigEndian.Uint16(b[xsum:])
@@ -428,6 +440,10 @@ func (b IPv4) SetTotalLength(totalLength uint16) {
428440
binary.BigEndian.PutUint16(b[IPv4TotalLenOffset:], totalLength)
429441
}
430442

443+
func (b IPv4) SetTotalLengthDarwinRaw(totalLength uint16) {
444+
binary.NativeEndian.PutUint16(b[IPv4TotalLenOffset:], totalLength)
445+
}
446+
431447
// SetChecksum sets the checksum field of the IPv4 header.
432448
func (b IPv4) SetChecksum(v uint16) {
433449
checksum.Put(b[xsum:], v)
@@ -440,6 +456,11 @@ func (b IPv4) SetFlagsFragmentOffset(flags uint8, offset uint16) {
440456
binary.BigEndian.PutUint16(b[flagsFO:], v)
441457
}
442458

459+
func (b IPv4) SetFlagsFragmentOffsetDarwinRaw(flags uint8, offset uint16) {
460+
v := (uint16(flags) << 13) | (offset >> 3)
461+
binary.NativeEndian.PutUint16(b[flagsFO:], v)
462+
}
463+
443464
// SetID sets the identification field.
444465
func (b IPv4) SetID(v uint16) {
445466
binary.BigEndian.PutUint16(b[id:], v)

ping/ping.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ func (c *Conn) ReadIP(buffer *buf.Buffer) error {
159159
}
160160
if !c.destination.Is6() {
161161
ipHdr := header.IPv4(buffer.Bytes())
162+
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
163+
ipHdr.SetTotalLength(ipHdr.TotalLengthDarwinRaw())
164+
ipHdr.SetFlagsFragmentOffset(ipHdr.FlagsDarwinRaw(), ipHdr.FragmentOffsetDarwinRaw())
165+
}
162166
if !ipHdr.IsValid(buffer.Len()) {
163167
return E.New("invalid IPv4 header received")
164168
}

0 commit comments

Comments
 (0)