mirror of
				https://github.com/taigrr/go-fastping
				synced 2025-01-18 05:03:15 -08:00 
			
		
		
		
	Added payload size
This commit is contained in:
		
							parent
							
								
									47b4079cf0
								
							
						
					
					
						commit
						c6c53385e0
					
				
							
								
								
									
										19
									
								
								fastping.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								fastping.go
									
									
									
									
									
								
							| @ -47,6 +47,15 @@ import ( | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| func byteSliceOfSize(n int) []byte { | ||||
| 	b := make([]byte, n) | ||||
| 	for i := 0; i < len(b); i++ { | ||||
| 		b[i] = 1 | ||||
| 	} | ||||
| 
 | ||||
| 	return b | ||||
| } | ||||
| 
 | ||||
| func timeToBytes(t time.Time) []byte { | ||||
| 	nsec := t.UnixNano() | ||||
| 	b := make([]byte, 8) | ||||
| @ -100,6 +109,9 @@ type Pinger struct { | ||||
| 	hasIPv6 bool | ||||
| 	ctx     *context | ||||
| 	mu      sync.Mutex | ||||
| 
 | ||||
| 	// Size in bytes of the payload to send | ||||
| 	Size int | ||||
| 	// Number of (nano,milli)seconds of an idle timeout. Once it passed, | ||||
| 	// the library calls an idle callback function. It is also used for an | ||||
| 	// interval time of RunLoop() method | ||||
| @ -122,6 +134,7 @@ func NewPinger() *Pinger { | ||||
| 		addrs:   make(map[string]*net.IPAddr), | ||||
| 		hasIPv4: false, | ||||
| 		hasIPv6: false, | ||||
| 		Size:    64, | ||||
| 		MaxRTT:  time.Second, | ||||
| 		OnRecv:  nil, | ||||
| 		OnIdle:  nil, | ||||
| @ -389,12 +402,14 @@ func (p *Pinger) sendICMP(conn, conn6 *net.IPConn) (map[string]*net.IPAddr, erro | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		t := timeToBytes(time.Now()) | ||||
| 
 | ||||
| 		p.mu.Lock() | ||||
| 		bytes, err := (&icmpMessage{ | ||||
| 			Type: typ, Code: 0, | ||||
| 			Body: &icmpEcho{ | ||||
| 				ID: p.id, Seq: p.seq, | ||||
| 				Data: timeToBytes(time.Now()), | ||||
| 				Data: append(t, byteSliceOfSize(p.Size-len(t))...), | ||||
| 			}, | ||||
| 		}).Marshal() | ||||
| 		p.mu.Unlock() | ||||
| @ -500,7 +515,7 @@ func (p *Pinger) procRecv(recv *packet, queue map[string]*net.IPAddr) { | ||||
| 	case *icmpEcho: | ||||
| 		p.mu.Lock() | ||||
| 		if pkt.ID == p.id && pkt.Seq == p.seq { | ||||
| 			rtt = time.Since(bytesToTime(pkt.Data)) | ||||
| 			rtt = time.Since(bytesToTime(pkt.Data[:8])) | ||||
| 		} | ||||
| 		p.mu.Unlock() | ||||
| 	default: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user