summaryrefslogtreecommitdiff
path: root/test/dialgoogle.go
blob: ca2c35cce4587017d0b913ce9f114a36e27981cf (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
// 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.

// $G $F.go && $L $F.$A && GOMAXPROCS=2 ./$A.out

package main

import (
	"net";
	"flag";
	"io";
	"os";
	"syscall"
)

// If an IPv6 tunnel is running (see go/stubl), we can try dialing a real IPv6 address.
var ipv6 = false
var ipv6_flag = flag.Bool("ipv6", false, &ipv6, "assume ipv6 tunnel is present")

func StringToBuf(s string) *[]byte
{
	l := len(s);
	b := new([]byte, l);
	for i := 0; i < l; i++ {
		b[i] = s[i];
	}
	return b;
}

func Readn(fd io.Read, buf *[]byte) (n int, err *os.Error) {
	n = 0;
	for n < len(buf) {
		nn, e := fd.Read(buf[n:len(buf)]);
		if nn > 0 {
			n += nn
		}
		if e != nil {
			return n, e
		}
	}
	return n, nil
}


// fd is already connected to www.google.com port 80.
// Run an HTTP request to fetch the main page.
func FetchGoogle(fd net.Conn) {
	req := StringToBuf("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
	n, errno := fd.Write(req);

	buf := new([1000]byte);
	n, errno = Readn(fd, buf);

	fd.Close();
	if n < 1000 {
		panic("short http read");
	}
}

func TestDial(network, addr string) {
	fd, err := net.Dial(network, "", addr);
	if err != nil {
		panic("net.Dial ", network, " ", addr, ": ", err.String())
	}
	FetchGoogle(fd)
}

func TestDialTCP(network, addr string) {
	fd, err := net.DialTCP(network, "", addr);
	if err != nil {
		panic("net.DialTCP ", network, " ", addr, ": ", err.String())
	}
	FetchGoogle(fd)
}

var addrs = []string {
	"74.125.19.99:80",
	"074.125.019.099:0080",
	"[::ffff:74.125.19.99]:80",
	"[::ffff:4a7d:1363]:80",
	"[0:0:0:0:0000:ffff:74.125.19.99]:80",
	"[0:0:0:0:000000:ffff:74.125.19.99]:80",
	"[0:0:0:0:0:ffff::74.125.19.99]:80",
	"[2001:4860:0:2001::68]:80"	// ipv6.google.com; removed if ipv6 flag not set
}

func main()
{
	flag.Parse();
	// If no ipv6 tunnel, don't try the last address.
	if !ipv6 {
		addrs[len(addrs)-1] = ""
	}

	for i := 0; i < len(addrs); i++ {
		addr := addrs[i];
		if addr == "" {
			continue
		}
	//	print(addr, "\n");
		TestDial("tcp", addr);
		TestDialTCP("tcp", addr);
		if addr[0] != '[' {
			TestDial("tcp4", addr);
			TestDialTCP("tcp4", addr)
		}
		TestDial("tcp6", addr);
		TestDialTCP("tcp6", addr)
	}
}