2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// +build !plan9
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
2012-12-13 00:13:29 +01:00
|
|
|
"reflect"
|
2012-03-02 21:01:37 +01:00
|
|
|
"syscall"
|
2010-12-03 05:34:57 +01:00
|
|
|
"testing"
|
2012-01-25 22:54:22 +01:00
|
|
|
"time"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2012-12-13 00:13:29 +01:00
|
|
|
var resolveIPAddrTests = []struct {
|
|
|
|
net string
|
|
|
|
litAddr string
|
|
|
|
addr *IPAddr
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
|
|
{"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
|
|
{"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
|
|
|
|
|
|
{"ip", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
|
|
|
|
{"ip6", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
|
|
|
|
{"ip6:icmp", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
|
|
|
|
|
|
|
|
{"", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, // Go 1.0 behavior
|
|
|
|
{"", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, // Go 1.0 behavior
|
|
|
|
|
|
|
|
{"l2tp", "127.0.0.1", nil, UnknownNetworkError("l2tp")},
|
|
|
|
{"l2tp:gre", "127.0.0.1", nil, UnknownNetworkError("l2tp:gre")},
|
|
|
|
{"tcp", "1.2.3.4:123", nil, UnknownNetworkError("tcp")},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveIPAddr(t *testing.T) {
|
|
|
|
for _, tt := range resolveIPAddrTests {
|
|
|
|
addr, err := ResolveIPAddr(tt.net, tt.litAddr)
|
|
|
|
if err != tt.err {
|
|
|
|
t.Fatalf("ResolveIPAddr(%v, %v) failed: %v", tt.net, tt.litAddr, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(addr, tt.addr) {
|
|
|
|
t.Fatalf("got %#v; expected %#v", addr, tt.addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
var icmpTests = []struct {
|
|
|
|
net string
|
|
|
|
laddr string
|
|
|
|
raddr string
|
2012-03-02 21:01:37 +01:00
|
|
|
ipv6 bool // test with underlying AF_INET6 socket
|
2012-02-01 20:26:59 +01:00
|
|
|
}{
|
|
|
|
{"ip4:icmp", "", "127.0.0.1", false},
|
2012-11-21 08:03:38 +01:00
|
|
|
{"ip6:ipv6-icmp", "", "::1", true},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestICMP(t *testing.T) {
|
|
|
|
if os.Getuid() != 0 {
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skip("skipping test; must be root")
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
seqnum := 61455
|
|
|
|
for _, tt := range icmpTests {
|
|
|
|
if tt.ipv6 && !supportsIPv6 {
|
|
|
|
continue
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
id := os.Getpid() & 0xffff
|
|
|
|
seqnum++
|
2012-03-02 21:01:37 +01:00
|
|
|
echo := newICMPEchoRequest(tt.net, id, seqnum, 128, []byte("Go Go Gadget Ping!!!"))
|
|
|
|
exchangeICMPEcho(t, tt.net, tt.laddr, tt.raddr, echo)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
func exchangeICMPEcho(t *testing.T, net, laddr, raddr string, echo []byte) {
|
2012-02-01 20:26:59 +01:00
|
|
|
c, err := ListenPacket(net, laddr)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
t.Errorf("ListenPacket(%q, %q) failed: %v", net, laddr, err)
|
2012-02-01 20:26:59 +01:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
c.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
defer c.Close()
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
ra, err := ResolveIPAddr(net, raddr)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
t.Errorf("ResolveIPAddr(%q, %q) failed: %v", net, raddr, err)
|
2012-02-01 20:26:59 +01:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
waitForReady := make(chan bool)
|
2012-03-02 21:01:37 +01:00
|
|
|
go icmpEchoTransponder(t, net, raddr, waitForReady)
|
2012-02-01 20:26:59 +01:00
|
|
|
<-waitForReady
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
_, err = c.WriteTo(echo, ra)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("WriteTo failed: %v", err)
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
reply := make([]byte, 256)
|
|
|
|
for {
|
|
|
|
_, _, err := c.ReadFrom(reply)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ReadFrom failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
switch c.(*IPConn).fd.family {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
if reply[0] != ICMP4_ECHO_REPLY {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case syscall.AF_INET6:
|
|
|
|
if reply[0] != ICMP6_ECHO_REPLY {
|
|
|
|
continue
|
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
xid, xseqnum := parseICMPEchoReply(echo)
|
|
|
|
rid, rseqnum := parseICMPEchoReply(reply)
|
|
|
|
if rid != xid || rseqnum != xseqnum {
|
|
|
|
t.Errorf("ID = %v, Seqnum = %v, want ID = %v, Seqnum = %v", rid, rseqnum, xid, xseqnum)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
func icmpEchoTransponder(t *testing.T, net, raddr string, waitForReady chan bool) {
|
2012-02-01 20:26:59 +01:00
|
|
|
c, err := Dial(net, raddr)
|
|
|
|
if err != nil {
|
|
|
|
waitForReady <- true
|
2012-03-02 21:01:37 +01:00
|
|
|
t.Errorf("Dial(%q, %q) failed: %v", net, raddr, err)
|
2012-02-01 20:26:59 +01:00
|
|
|
return
|
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
c.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
2012-02-01 20:26:59 +01:00
|
|
|
defer c.Close()
|
|
|
|
waitForReady <- true
|
|
|
|
|
|
|
|
echo := make([]byte, 256)
|
|
|
|
var nr int
|
2010-12-03 05:34:57 +01:00
|
|
|
for {
|
2012-02-01 20:26:59 +01:00
|
|
|
nr, err = c.Read(echo)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
t.Errorf("Read failed: %v", err)
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
switch c.(*IPConn).fd.family {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
if echo[0] != ICMP4_ECHO_REQUEST {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case syscall.AF_INET6:
|
|
|
|
if echo[0] != ICMP6_ECHO_REQUEST {
|
|
|
|
continue
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
switch c.(*IPConn).fd.family {
|
|
|
|
case syscall.AF_INET:
|
2012-02-01 20:26:59 +01:00
|
|
|
echo[0] = ICMP4_ECHO_REPLY
|
2012-03-02 21:01:37 +01:00
|
|
|
case syscall.AF_INET6:
|
2012-02-01 20:26:59 +01:00
|
|
|
echo[0] = ICMP6_ECHO_REPLY
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Write(echo[:nr])
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Write failed: %v", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ICMP4_ECHO_REQUEST = 8
|
|
|
|
ICMP4_ECHO_REPLY = 0
|
|
|
|
ICMP6_ECHO_REQUEST = 128
|
|
|
|
ICMP6_ECHO_REPLY = 129
|
|
|
|
)
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
func newICMPEchoRequest(net string, id, seqnum, msglen int, filler []byte) []byte {
|
|
|
|
afnet, _, _ := parseDialNetwork(net)
|
|
|
|
switch afnet {
|
|
|
|
case "ip4":
|
2012-02-01 20:26:59 +01:00
|
|
|
return newICMPv4EchoRequest(id, seqnum, msglen, filler)
|
2012-03-02 21:01:37 +01:00
|
|
|
case "ip6":
|
|
|
|
return newICMPv6EchoRequest(id, seqnum, msglen, filler)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newICMPv4EchoRequest(id, seqnum, msglen int, filler []byte) []byte {
|
|
|
|
b := newICMPInfoMessage(id, seqnum, msglen, filler)
|
|
|
|
b[0] = ICMP4_ECHO_REQUEST
|
|
|
|
|
|
|
|
// calculate ICMP checksum
|
|
|
|
cklen := len(b)
|
|
|
|
s := uint32(0)
|
|
|
|
for i := 0; i < cklen-1; i += 2 {
|
|
|
|
s += uint32(b[i+1])<<8 | uint32(b[i])
|
|
|
|
}
|
|
|
|
if cklen&1 == 1 {
|
|
|
|
s += uint32(b[cklen-1])
|
|
|
|
}
|
|
|
|
s = (s >> 16) + (s & 0xffff)
|
|
|
|
s = s + (s >> 16)
|
|
|
|
// place checksum back in header; using ^= avoids the
|
|
|
|
// assumption the checksum bytes are zero
|
|
|
|
b[2] ^= uint8(^s & 0xff)
|
|
|
|
b[3] ^= uint8(^s >> 8)
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func newICMPv6EchoRequest(id, seqnum, msglen int, filler []byte) []byte {
|
|
|
|
b := newICMPInfoMessage(id, seqnum, msglen, filler)
|
|
|
|
b[0] = ICMP6_ECHO_REQUEST
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func newICMPInfoMessage(id, seqnum, msglen int, filler []byte) []byte {
|
|
|
|
b := make([]byte, msglen)
|
|
|
|
copy(b[8:], bytes.Repeat(filler, (msglen-8)/len(filler)+1))
|
|
|
|
b[0] = 0 // type
|
|
|
|
b[1] = 0 // code
|
|
|
|
b[2] = 0 // checksum
|
|
|
|
b[3] = 0 // checksum
|
|
|
|
b[4] = uint8(id >> 8) // identifier
|
|
|
|
b[5] = uint8(id & 0xff) // identifier
|
|
|
|
b[6] = uint8(seqnum >> 8) // sequence number
|
|
|
|
b[7] = uint8(seqnum & 0xff) // sequence number
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseICMPEchoReply(b []byte) (id, seqnum int) {
|
|
|
|
id = int(b[4])<<8 | int(b[5])
|
|
|
|
seqnum = int(b[6])<<8 | int(b[7])
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
|
|
|
|
var ipConnLocalNameTests = []struct {
|
|
|
|
net string
|
|
|
|
laddr *IPAddr
|
|
|
|
}{
|
|
|
|
{"ip4:icmp", &IPAddr{IP: IPv4(127, 0, 0, 1)}},
|
|
|
|
{"ip4:icmp", &IPAddr{}},
|
|
|
|
{"ip4:icmp", nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPConnLocalName(t *testing.T) {
|
|
|
|
if os.Getuid() != 0 {
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skip("skipping test; must be root")
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range ipConnLocalNameTests {
|
|
|
|
c, err := ListenIP(tt.net, tt.laddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ListenIP failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
la := c.LocalAddr()
|
|
|
|
if la == nil {
|
|
|
|
t.Error("IPConn.LocalAddr failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|