コード例 #1
0
ファイル: ucat.go プロジェクト: carriercomm/interplanetary
// Dial connects to a remote address and pipes all os.Stdin to the remote end.
// If localAddr is set, uses it to Dial from.
func Dial(localAddr, remoteAddr string) error {

	var laddr net.Addr
	var err error
	if localAddr != "" {
		laddr, err = utp.ResolveUTPAddr("utp", localAddr)
		if err != nil {
			return fmt.Errorf("failed to resolve address %s", localAddr)
		}
	}

	if laddr != nil {
		log("dialing %s from %s", remoteAddr, laddr)
	} else {
		log("dialing %s", remoteAddr)
	}

	d := utp.Dialer{LocalAddr: laddr}
	c, err := d.Dial("utp", remoteAddr)
	if err != nil {
		return err
	}
	log("connected to %s", c.RemoteAddr())

	netcat(c)
	return c.Close()
}
コード例 #2
0
ファイル: net.go プロジェクト: carriercomm/interplanetary
// Dial connects to a remote address, using the options of the
// Dialer. Dialer uses an underlying net.Dialer to Dial a
// net.Conn, then wraps that in a Conn object (with local and
// remote Multiaddrs).
func (d *Dialer) Dial(remote ma.Multiaddr) (Conn, error) {

	// if a LocalAddr is specified, use it on the embedded dialer.
	if d.LocalAddr != nil {
		// convert our multiaddr to net.Addr friendly
		naddr, err := ToNetAddr(d.LocalAddr)
		if err != nil {
			return nil, err
		}

		// set the dialer's LocalAddr as naddr
		d.Dialer.LocalAddr = naddr
	}

	// get the net.Dial friendly arguments from the remote addr
	rnet, rnaddr, err := DialArgs(remote)
	if err != nil {
		return nil, err
	}

	// ok, Dial!
	var nconn net.Conn
	switch rnet {
	case "tcp":
		nconn, err = d.Dialer.Dial(rnet, rnaddr)
		if err != nil {
			return nil, err
		}
	case "utp":
		// construct utp dialer, with options on our net.Dialer
		utpd := utp.Dialer{
			Timeout:   d.Dialer.Timeout,
			LocalAddr: d.Dialer.LocalAddr,
		}

		nconn, err = utpd.Dial(rnet, rnaddr)
		if err != nil {
			return nil, err
		}
	}

	// get local address (pre-specified or assigned within net.Conn)
	local := d.LocalAddr
	if local == nil {
		local, err = FromNetAddr(nconn.LocalAddr())
		if err != nil {
			return nil, err
		}
	}

	return &maConn{
		Conn:  nconn,
		laddr: local,
		raddr: remote,
	}, nil
}