package main import ( "fmt" "time" "net" "strings" ) const host0 string = "192.168.1.4"; const host1 string = "192.168.1.8"; func main() { go monitor(host0) go monitor(host1) for { time.Sleep(time.Second) } } func monitor(host string) { fmt.Printf("Pinging %s...\n", host) for { ping(host) time.Sleep(time.Millisecond * 100) } } func ping(host string) { var message_data [512]byte // var bytes_read int c, e := net.Dial("ip4:1", host) if e != nil { fmt.Printf("%s %s\n", host, e); return } defer c.Close() message_data[0] = 8 // ECHO message_data[1] = 0 message_data[2] = 0 // Checksum (incorrect) message_data[3] = 0 // Checksum (also incorrect) message_data[4] = 0 message_data[5] = 15 message_data[6] = 0 // Seq (L-byte) message_data[7] = 0 // Seq (H-byte) checksum := compute_checksum(message_data[0:8]) message_data[2] = byte(checksum >> 8) message_data[3] = byte(checksum & 0xFF) _, e = c.Write(message_data[0:8]) if e != nil { return } c.SetReadDeadline(time.Now().Add(time.Second * 5)) _, e = c.Read(message_data[0:]) if e != nil { return } source_IP := fmt.Sprintf("%d.%d.%d.%d", message_data[12], message_data[13], message_data[14], message_data[15]); if (!strings.Contains(host, source_IP)) { // BUG! We received a response from a different IP address then we sent // // the ICMP Echo request to. // fmt.Printf("X") } else { fmt.Printf(".") } } func compute_checksum(message []byte) uint16 { var word_index uint16 = 0 var sum uint16 = 0 var answer uint16 = 0 bytes_remaining := len(message) for bytes_remaining > 0 { sum += uint16(message[word_index]) * 0x100 + uint16(message[word_index + 1]) word_index += 2 bytes_remaining -= 2 if bytes_remaining == 1 { sum += uint16(message[len(message) - 1]) } } answer = ^((sum & 0xFFFF) + (sum >> 16)) return answer }