Make Tracker uint64 and check for both UDP and ICMP (#134)

This commit is contained in:
Charlie Jonas 2021-02-07 22:15:49 +00:00 committed by GitHub
parent 38783b05ce
commit 62f79f1f4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 22 deletions

27
ping.go
View File

@ -92,7 +92,7 @@ func New(addr string) *Pinger {
RecordRtts: true,
Size: timeSliceLength,
Timeout: time.Duration(math.MaxInt64),
Tracker: r.Int63n(math.MaxInt64),
Tracker: r.Uint64(),
addr: addr,
done: make(chan bool),
@ -159,8 +159,8 @@ type Pinger struct {
// Size of packet being sent
Size int
// Tracker: Used to uniquely identify packet when non-priviledged
Tracker int64
// Tracker: Used to uniquely identify packets
Tracker uint64
// Source is the source IP address
Source string
@ -542,12 +542,9 @@ func (p *Pinger) processPacket(recv *packet) error {
switch pkt := m.Body.(type) {
case *icmp.Echo:
// If we are priviledged, we can match icmp.ID
if p.protocol == "icmp" {
// Check if reply from same ID
if pkt.ID != p.id {
return nil
}
// Check if the reply has the ID we expect.
if pkt.ID != p.id {
return nil
}
if len(pkt.Data) < timeSliceLength+trackerLength {
@ -555,7 +552,7 @@ func (p *Pinger) processPacket(recv *packet) error {
len(pkt.Data), pkt.Data)
}
tracker := bytesToInt(pkt.Data[timeSliceLength:])
tracker := bytesToUint(pkt.Data[timeSliceLength:])
timestamp := bytesToTime(pkt.Data[:timeSliceLength])
if tracker != p.Tracker {
@ -604,7 +601,7 @@ func (p *Pinger) sendICMP(conn *icmp.PacketConn) error {
dst = &net.UDPAddr{IP: p.ipaddr.IP, Zone: p.ipaddr.Zone}
}
t := append(timeToBytes(time.Now()), intToBytes(p.Tracker)...)
t := append(timeToBytes(time.Now()), uintToBytes(p.Tracker)...)
if remainSize := p.Size - timeSliceLength - trackerLength; remainSize > 0 {
t = append(t, bytes.Repeat([]byte{1}, remainSize)...)
}
@ -684,13 +681,13 @@ func timeToBytes(t time.Time) []byte {
return b
}
func bytesToInt(b []byte) int64 {
return int64(binary.BigEndian.Uint64(b))
func bytesToUint(b []byte) uint64 {
return uint64(binary.BigEndian.Uint64(b))
}
func intToBytes(tracker int64) []byte {
func uintToBytes(tracker uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(tracker))
binary.BigEndian.PutUint64(b, tracker)
return b
}

View File

@ -19,7 +19,7 @@ func TestProcessPacket(t *testing.T) {
shouldBe1++
}
data := append(timeToBytes(time.Now()), intToBytes(pinger.Tracker)...)
data := append(timeToBytes(time.Now()), uintToBytes(pinger.Tracker)...)
if remainSize := pinger.Size - timeSliceLength - trackerLength; remainSize > 0 {
data = append(data, bytes.Repeat([]byte{1}, remainSize)...)
}
@ -58,7 +58,7 @@ func TestProcessPacket_IgnoreNonEchoReplies(t *testing.T) {
shouldBe0++
}
data := append(timeToBytes(time.Now()), intToBytes(pinger.Tracker)...)
data := append(timeToBytes(time.Now()), uintToBytes(pinger.Tracker)...)
if remainSize := pinger.Size - timeSliceLength - trackerLength; remainSize > 0 {
data = append(data, bytes.Repeat([]byte{1}, remainSize)...)
}
@ -97,7 +97,7 @@ func TestProcessPacket_IDMismatch(t *testing.T) {
shouldBe0++
}
data := append(timeToBytes(time.Now()), intToBytes(pinger.Tracker)...)
data := append(timeToBytes(time.Now()), uintToBytes(pinger.Tracker)...)
if remainSize := pinger.Size - timeSliceLength - trackerLength; remainSize > 0 {
data = append(data, bytes.Repeat([]byte{1}, remainSize)...)
}
@ -135,7 +135,7 @@ func TestProcessPacket_TrackerMismatch(t *testing.T) {
shouldBe0++
}
data := append(timeToBytes(time.Now()), intToBytes(999)...)
data := append(timeToBytes(time.Now()), uintToBytes(999)...)
if remainSize := pinger.Size - timeSliceLength - trackerLength; remainSize > 0 {
data = append(data, bytes.Repeat([]byte{1}, remainSize)...)
}
@ -169,7 +169,7 @@ func TestProcessPacket_LargePacket(t *testing.T) {
pinger := makeTestPinger()
pinger.Size = 4096
data := append(timeToBytes(time.Now()), intToBytes(pinger.Tracker)...)
data := append(timeToBytes(time.Now()), uintToBytes(pinger.Tracker)...)
if remainSize := pinger.Size - timeSliceLength - trackerLength; remainSize > 0 {
data = append(data, bytes.Repeat([]byte{1}, remainSize)...)
}
@ -520,7 +520,7 @@ func BenchmarkProcessPacket(b *testing.B) {
pinger.id = 123
pinger.Tracker = 456
t := append(timeToBytes(time.Now()), intToBytes(pinger.Tracker)...)
t := append(timeToBytes(time.Now()), uintToBytes(pinger.Tracker)...)
if remainSize := pinger.Size - timeSliceLength - trackerLength; remainSize > 0 {
t = append(t, bytes.Repeat([]byte{1}, remainSize)...)
}
@ -565,7 +565,7 @@ func TestProcessPacket_IgnoresDuplicateSequence(t *testing.T) {
dups++
}
data := append(timeToBytes(time.Now()), intToBytes(pinger.Tracker)...)
data := append(timeToBytes(time.Now()), uintToBytes(pinger.Tracker)...)
if remainSize := pinger.Size - timeSliceLength - trackerLength; remainSize > 0 {
data = append(data, bytes.Repeat([]byte{1}, remainSize)...)
}