aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.7/libgo/go/net/ipraw_test.go
blob: 6136202727cfc70a4e5e4d1eea79564f1b670c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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.

package net

import (
	"bytes"
	"os"
	"syscall"
	"testing"
	"time"
)

var icmpTests = []struct {
	net   string
	laddr string
	raddr string
	ipv6  bool // test with underlying AF_INET6 socket
}{
	{"ip4:icmp", "", "127.0.0.1", false},
	{"ip6:icmp", "", "::1", true},
}

func TestICMP(t *testing.T) {
	if os.Getuid() != 0 {
		t.Logf("test disabled; must be root")
		return
	}

	seqnum := 61455
	for _, tt := range icmpTests {
		if tt.ipv6 && !supportsIPv6 {
			continue
		}
		id := os.Getpid() & 0xffff
		seqnum++
		echo := newICMPEchoRequest(tt.net, id, seqnum, 128, []byte("Go Go Gadget Ping!!!"))
		exchangeICMPEcho(t, tt.net, tt.laddr, tt.raddr, echo)
	}
}

func exchangeICMPEcho(t *testing.T, net, laddr, raddr string, echo []byte) {
	c, err := ListenPacket(net, laddr)
	if err != nil {
		t.Errorf("ListenPacket(%q, %q) failed: %v", net, laddr, err)
		return
	}
	c.SetDeadline(time.Now().Add(100 * time.Millisecond))
	defer c.Close()

	ra, err := ResolveIPAddr(net, raddr)
	if err != nil {
		t.Errorf("ResolveIPAddr(%q, %q) failed: %v", net, raddr, err)
		return
	}

	waitForReady := make(chan bool)
	go icmpEchoTransponder(t, net, raddr, waitForReady)
	<-waitForReady

	_, err = c.WriteTo(echo, ra)
	if err != nil {
		t.Errorf("WriteTo failed: %v", err)
		return
	}

	reply := make([]byte, 256)
	for {
		_, _, err := c.ReadFrom(reply)
		if err != nil {
			t.Errorf("ReadFrom failed: %v", err)
			return
		}
		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
			}
		}
		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
	}
}

func icmpEchoTransponder(t *testing.T, net, raddr string, waitForReady chan bool) {
	c, err := Dial(net, raddr)
	if err != nil {
		waitForReady <- true
		t.Errorf("Dial(%q, %q) failed: %v", net, raddr, err)
		return
	}
	c.SetDeadline(time.Now().Add(100 * time.Millisecond))
	defer c.Close()
	waitForReady <- true

	echo := make([]byte, 256)
	var nr int
	for {
		nr, err = c.Read(echo)
		if err != nil {
			t.Errorf("Read failed: %v", err)
			return
		}
		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
			}
		}
		break
	}

	switch c.(*IPConn).fd.family {
	case syscall.AF_INET:
		echo[0] = ICMP4_ECHO_REPLY
	case syscall.AF_INET6:
		echo[0] = ICMP6_ECHO_REPLY
	}

	_, err = c.Write(echo[:nr])
	if err != nil {
		t.Errorf("Write failed: %v", err)
		return
	}
}

const (
	ICMP4_ECHO_REQUEST = 8
	ICMP4_ECHO_REPLY   = 0
	ICMP6_ECHO_REQUEST = 128
	ICMP6_ECHO_REPLY   = 129
)

func newICMPEchoRequest(net string, id, seqnum, msglen int, filler []byte) []byte {
	afnet, _, _ := parseDialNetwork(net)
	switch afnet {
	case "ip4":
		return newICMPv4EchoRequest(id, seqnum, msglen, filler)
	case "ip6":
		return newICMPv6EchoRequest(id, seqnum, msglen, filler)
	}
	return nil
}

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
}