OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 package ipv4_test |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "flag" |
| 10 ) |
| 11 |
| 12 var testExternal = flag.Bool("external", true, "allow use of external networks d
uring long test") |
| 13 |
| 14 func newICMPEchoRequest(id, seqnum, msglen int, filler []byte) []byte { |
| 15 b := newICMPInfoMessage(id, seqnum, msglen, filler) |
| 16 b[0] = 8 |
| 17 // calculate ICMP checksum |
| 18 cklen := len(b) |
| 19 s := uint32(0) |
| 20 for i := 0; i < cklen-1; i += 2 { |
| 21 s += uint32(b[i+1])<<8 | uint32(b[i]) |
| 22 } |
| 23 if cklen&1 == 1 { |
| 24 s += uint32(b[cklen-1]) |
| 25 } |
| 26 s = (s >> 16) + (s & 0xffff) |
| 27 s = s + (s >> 16) |
| 28 // place checksum back in header; using ^= avoids the |
| 29 // assumption the checksum bytes are zero |
| 30 b[2] ^= byte(^s & 0xff) |
| 31 b[3] ^= byte(^s >> 8) |
| 32 return b |
| 33 } |
| 34 |
| 35 func newICMPInfoMessage(id, seqnum, msglen int, filler []byte) []byte { |
| 36 b := make([]byte, msglen) |
| 37 copy(b[8:], bytes.Repeat(filler, (msglen-8)/len(filler)+1)) |
| 38 b[0] = 0 // type |
| 39 b[1] = 0 // code |
| 40 b[2] = 0 // checksum |
| 41 b[3] = 0 // checksum |
| 42 b[4] = byte(id >> 8) // identifier |
| 43 b[5] = byte(id & 0xff) // identifier |
| 44 b[6] = byte(seqnum >> 8) // sequence number |
| 45 b[7] = byte(seqnum & 0xff) // sequence number |
| 46 return b |
| 47 } |
OLD | NEW |