Beispiel #1
0
func (d *dht) see(key string, x *e3x.Exchange) ([]hashname.H, error) {
	c, err := x.Open(d.prefix+"see", false)
	if err != nil {
		return nil, err
	}
	defer c.Close()

	pkt := &lob.Packet{}
	pkt.Header().SetString("key", key)
	pkt.Header().SetBool("end", true)
	err = c.WritePacket(pkt)
	if err != nil {
		return nil, err
	}

	pkt, err = c.ReadPacket()
	if err != nil {
		return nil, err
	}

	v, _ := pkt.Header().Get("see")
	l, ok := v.([]string)
	if !ok {
		return nil, nil
	}

	h := make([]hashname.H, len(l))
	for i, s := range l {
		h[i] = hashname.H(s)
	}

	return h, nil
}
Beispiel #2
0
func (mod *module) negotiatePaths(x *e3x.Exchange) {
	addrs := e3x.TransportsFromEndpoint(mod.endpoint).LocalAddresses()

	c, err := x.Open("path", false)
	if err != nil {
		return
	}
	defer c.Kill()

	c.SetDeadline(time.Now().Add(1 * time.Minute))

	pkt := &lob.Packet{}
	pkt.Header().Set("paths", addrs)
	if err := c.WritePacket(pkt); err != nil {
		return // ignore
	}

	for {
		_, err := c.ReadPacket()
		if err == io.EOF || err == e3x.ErrTimeout {
			return
		}
		if err != nil {
			return
		}
	}
}
Beispiel #3
0
func (mod *module) peerVia(router *e3x.Exchange, to hashname.H, body *bufpool.Buffer) error {
	ch, err := router.Open("peer", false)
	if err != nil {
		return err
	}
	defer ch.Kill()

	pkt := lob.New(body.RawBytes())
	pkt.Header().SetString("peer", string(to))
	ch.WritePacket(pkt)

	return nil
}
Beispiel #4
0
func (mod *module) on_dropped_packet(e *e3x.Endpoint, x *e3x.Exchange, msg []byte, pipe *e3x.Pipe, reason error) error {
	if !x.State().IsOpen() {
		return nil
	}

	err := mod.forwardMessage(e, x, msg, pipe, reason)
	if err != nil {
		return err
	}

	err = mod.receivedForwardedMessage(e, x, msg, pipe, reason)
	return err
}
func (mod *module) connect(ex *e3x.Exchange, inner *bufpool.Buffer) error {
	ch, err := ex.Open("connect", false)
	if err != nil {
		return err
	}

	defer ch.Kill()

	err = ch.WritePacket(lob.New(inner.RawBytes()))
	if err != nil {
		return err
	}

	return nil
}
Beispiel #6
0
func (a *peerAddr) Dial(e *e3x.Endpoint, x *e3x.Exchange) (net.Conn, error) {
	mod, _ := FromEndpoint(e).(*module)
	if mod == nil {
		return nil, net.UnknownNetworkError("unable to bridge")
	}

	router := e.GetExchange(a.router)
	if router == nil {
		return nil, net.UnknownNetworkError("unable to bridge")
	}

	conn := newConnection(x.RemoteHashname(), a, router, func() {
		mod.unregisterConnection(router, x.LocalToken())
	})

	mod.registerConnection(router, x.LocalToken(), conn)

	return conn, nil
}