mirror of
				https://github.com/go-ping/ping.git
				synced 2025-10-30 08:39:44 +00:00 
			
		
		
		
	Added ICMP Packet ID to distinguish ICMP replies in multithreading environment (#15)
This commit is contained in:
		
							
								
								
									
										20
									
								
								ping.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								ping.go
									
									
									
									
									
								
							| @@ -90,7 +90,10 @@ func NewPinger(addr string) (*Pinger, error) { | |||||||
| 		Interval: time.Second, | 		Interval: time.Second, | ||||||
| 		Timeout:  time.Second * 100000, | 		Timeout:  time.Second * 100000, | ||||||
| 		Count:    -1, | 		Count:    -1, | ||||||
|  | 		id:       rand.Intn(0xffff), | ||||||
|  | 		network:  "udp", | ||||||
|  | 		ipv4:     ipv4, | ||||||
|  | 		size:     timeSliceLength, | ||||||
| 		network: "udp", | 		network: "udp", | ||||||
| 		ipv4:    ipv4, | 		ipv4:    ipv4, | ||||||
| 		Size:    timeSliceLength, | 		Size:    timeSliceLength, | ||||||
| @@ -140,9 +143,10 @@ type Pinger struct { | |||||||
| 	ipaddr *net.IPAddr | 	ipaddr *net.IPAddr | ||||||
| 	addr   string | 	addr   string | ||||||
|  |  | ||||||
| 	ipv4   bool | 	ipv4     bool | ||||||
| 	source string | 	source   string | ||||||
|  | 	size     int | ||||||
|  | 	id       int | ||||||
| 	sequence int | 	sequence int | ||||||
| 	network  string | 	network  string | ||||||
| } | } | ||||||
| @@ -430,6 +434,12 @@ func (p *Pinger) processPacket(recv *packet) error { | |||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// Check if reply from same ID | ||||||
|  | 	body := m.Body.(*icmp.Echo) | ||||||
|  | 	if body.ID != p.id { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	outPkt := &Packet{ | 	outPkt := &Packet{ | ||||||
| 		Nbytes: recv.nbytes, | 		Nbytes: recv.nbytes, | ||||||
| 		IPAddr: p.ipaddr, | 		IPAddr: p.ipaddr, | ||||||
| @@ -475,7 +485,7 @@ func (p *Pinger) sendICMP(conn *icmp.PacketConn) error { | |||||||
| 	bytes, err := (&icmp.Message{ | 	bytes, err := (&icmp.Message{ | ||||||
| 		Type: typ, Code: 0, | 		Type: typ, Code: 0, | ||||||
| 		Body: &icmp.Echo{ | 		Body: &icmp.Echo{ | ||||||
| 			ID:   rand.Intn(65535), | 			ID:   p.id, | ||||||
| 			Seq:  p.sequence, | 			Seq:  p.sequence, | ||||||
| 			Data: t, | 			Data: t, | ||||||
| 		}, | 		}, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user