mockicmp_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. package ipv4_test
  5. import (
  6. "bytes"
  7. "flag"
  8. )
  9. var testExternal = flag.Bool("external", true, "allow use of external networks during long test")
  10. func newICMPEchoRequest(id, seqnum, msglen int, filler []byte) []byte {
  11. b := newICMPInfoMessage(id, seqnum, msglen, filler)
  12. b[0] = 8
  13. // calculate ICMP checksum
  14. cklen := len(b)
  15. s := uint32(0)
  16. for i := 0; i < cklen-1; i += 2 {
  17. s += uint32(b[i+1])<<8 | uint32(b[i])
  18. }
  19. if cklen&1 == 1 {
  20. s += uint32(b[cklen-1])
  21. }
  22. s = (s >> 16) + (s & 0xffff)
  23. s = s + (s >> 16)
  24. // place checksum back in header; using ^= avoids the
  25. // assumption the checksum bytes are zero
  26. b[2] ^= byte(^s & 0xff)
  27. b[3] ^= byte(^s >> 8)
  28. return b
  29. }
  30. func newICMPInfoMessage(id, seqnum, msglen int, filler []byte) []byte {
  31. b := make([]byte, msglen)
  32. copy(b[8:], bytes.Repeat(filler, (msglen-8)/len(filler)+1))
  33. b[0] = 0 // type
  34. b[1] = 0 // code
  35. b[2] = 0 // checksum
  36. b[3] = 0 // checksum
  37. b[4] = byte(id >> 8) // identifier
  38. b[5] = byte(id & 0xff) // identifier
  39. b[6] = byte(seqnum >> 8) // sequence number
  40. b[7] = byte(seqnum & 0xff) // sequence number
  41. return b
  42. }