Ejemplo n.º 1
0
// Open opens the transport.
func (c Config) Open() (transports.Transport, error) {
	mtx.Lock()
	id := netxID
	t := &transport{&inprocAddr{id}, make(chan packet, 10)}
	netxID++
	pipes[id] = t
	mtx.Unlock()

	return dgram.Wrap(t)
}
Ejemplo n.º 2
0
// Open opens the transport.
func (c Config) Open() (transports.Transport, error) {
	var (
		addr *net.UDPAddr
		err  error
	)

	if c.Network == "" {
		c.Network = UDPv4
	}
	if c.Addr == "" {
		c.Addr = ":0"
	}

	if c.Network != UDPv4 && c.Network != UDPv6 {
		return nil, errors.New("udp: Network must be either `udp4` or `udp6`")
	}

	{ // parse and verify source address
		addr, err = net.ResolveUDPAddr(c.Network, c.Addr)
		if err != nil {
			return nil, err
		}

		if c.Network == UDPv4 && addr.IP != nil && !ipIs4(addr.IP) {
			return nil, errors.New("udp: expected a IPv4 address")
		}

		if c.Network == UDPv6 && addr.IP != nil && ipIs4(addr.IP) {
			return nil, errors.New("udp: expected a IPv6 address")
		}
	}

	conn, err := net.ListenUDP(c.Network, addr)
	if err != nil {
		return nil, err
	}

	addr = conn.LocalAddr().(*net.UDPAddr)

	t := &transport{net: c.Network, laddr: wrapAddr(addr), c: conn}
	return dgram.Wrap(t)
}