func (pn *peernet) Adapter(p testutil.Identity) bsnet.BitSwapNetwork { client, err := pn.Mocknet.AddPeer(p.PrivateKey(), p.Address()) if err != nil { panic(err.Error()) } routing := pn.routingserver.ClientWithDatastore(context.TODO(), p, ds.NewMapDatastore()) return bsnet.NewFromIpfsHost(client, routing) }
// Recursive call for verifying the structure of a trickledag func verifyTDagRec(nd *dag.Node, depth, direct, layerRepeat int, ds dag.DAGService) error { if depth == 0 { // zero depth dag is raw data block if len(nd.Links) > 0 { return errors.New("expected direct block") } pbn, err := ft.FromBytes(nd.Data) if err != nil { return err } if pbn.GetType() != ft.TRaw { return errors.New("Expected raw block") } return nil } // Verify this is a branch node pbn, err := ft.FromBytes(nd.Data) if err != nil { return err } if pbn.GetType() != ft.TFile { return errors.New("expected file as branch node") } if len(pbn.Data) > 0 { return errors.New("branch node should not have data") } for i := 0; i < len(nd.Links); i++ { child, err := nd.Links[i].GetNode(context.TODO(), ds) if err != nil { return err } if i < direct { // Direct blocks err := verifyTDagRec(child, 0, direct, layerRepeat, ds) if err != nil { return err } } else { // Recursive trickle dags rdepth := ((i - direct) / layerRepeat) + 1 if rdepth >= depth && depth > 0 { return errors.New("Child dag was too deep!") } err := verifyTDagRec(child, rdepth, direct, layerRepeat, ds) if err != nil { return err } } } return nil }
func logProtocolMismatchDisconnect(c inet.Conn, protocol, agent string) { lm := make(lgbl.DeferredMap) lm["remotePeer"] = func() interface{} { return c.RemotePeer().Pretty() } lm["remoteAddr"] = func() interface{} { return c.RemoteMultiaddr().String() } lm["protocolVersion"] = protocol lm["agentVersion"] = agent log.Event(context.TODO(), "IdentifyProtocolMismatch", lm) log.Debug("IdentifyProtocolMismatch %s %s %s (disconnected)", c.RemotePeer(), protocol, agent) }
func (ids *IDService) IdentifyConn(c inet.Conn) { ids.currmu.Lock() if wait, found := ids.currid[c]; found { ids.currmu.Unlock() log.Debugf("IdentifyConn called twice on: %s", c) <-wait // already identifying it. wait for it. return } ids.currid[c] = make(chan struct{}) ids.currmu.Unlock() s, err := c.NewStream() if err != nil { log.Debugf("error opening initial stream for %s", ID) log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer()) c.Close() return } else { bwc := ids.Host.GetBandwidthReporter() s = mstream.WrapStream(s, ID, bwc) // ok give the response to our handler. if err := protocol.WriteHeader(s, ID); err != nil { log.Debugf("error writing stream header for %s", ID) log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer()) s.Close() c.Close() return } ids.ResponseHandler(s) } ids.currmu.Lock() ch, found := ids.currid[c] delete(ids.currid, c) ids.currmu.Unlock() if !found { log.Debugf("IdentifyConn failed to find channel (programmer error) for %s", c) return } close(ch) // release everyone waiting. }
func (n *network) deliver( r bsnet.Receiver, from peer.ID, message bsmsg.BitSwapMessage) error { if message == nil || from == "" { return errors.New("Invalid input") } n.delay.Wait() r.ReceiveMessage(context.TODO(), from, message) return nil }
func addPortMapping(nmgr *natManager, intaddr ma.Multiaddr) { nat := nmgr.NAT() if nat == nil { panic("natManager addPortMapping called without a nat.") } // first, check if the port mapping already exists. for _, mapping := range nat.Mappings() { if mapping.InternalAddr().Equal(intaddr) { return // it exists! return. } } ctx := context.TODO() lm := make(lgbl.DeferredMap) lm["internalAddr"] = func() interface{} { return intaddr.String() } defer log.EventBegin(ctx, "natMgrAddPortMappingWait", lm).Done() select { case <-nmgr.proc.Closing(): lm["outcome"] = "cancelled" return // no use. case <-nmgr.ready: // wait until it's ready. } // actually start the port map (sub-event because waiting may take a while) defer log.EventBegin(ctx, "natMgrAddPortMapping", lm).Done() // get the nat m, err := nat.NewMapping(intaddr) if err != nil { lm["outcome"] = "failure" lm["error"] = err return } extaddr, err := m.ExternalAddr() if err != nil { lm["outcome"] = "failure" lm["error"] = err return } lm["outcome"] = "success" lm["externalAddr"] = func() interface{} { return extaddr.String() } log.Infof("established nat port mapping: %s <--> %s", intaddr, extaddr) }
// getNode returns the node for link. If it return an error, // stop processing. if it returns a nil node, just skip it. // // the error handling is a little complicated. func (t *traversal) getNode(link *mdag.Link) (*mdag.Node, error) { getNode := func(l *mdag.Link) (*mdag.Node, error) { next, err := l.GetNode(context.TODO(), t.opts.DAG) if err != nil { return nil, err } skip, err := t.shouldSkip(next) if skip { next = nil } return next, err } next, err := getNode(link) if err != nil && t.opts.ErrFunc != nil { // attempt recovery. err = t.opts.ErrFunc(err) next = nil // skip regardless } return next, err }
// ResolveUnspecifiedAddresses expands unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to // use the known local interfaces. func ResolveUnspecifiedAddresses(unspecAddrs, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) { // todo optimize: only fetch these if we have a "any" addr. if len(ifaceAddrs) < 1 { var err error ifaceAddrs, err = InterfaceAddresses() if err != nil { return nil, err } // log.Debug("InterfaceAddresses:", ifaceAddrs) } var outputAddrs []ma.Multiaddr for _, a := range unspecAddrs { // unspecified? resolved, err := ResolveUnspecifiedAddress(a, ifaceAddrs) if err != nil { continue // optimistic. if we cant resolve anything, we'll know at the bottom. } // log.Debug("resolved:", a, resolved) outputAddrs = append(outputAddrs, resolved...) } if len(outputAddrs) < 1 { return nil, fmt.Errorf("failed to specify addrs: %s", unspecAddrs) } log.Event(context.TODO(), "interfaceListenAddresses", func() logging.Loggable { var addrs []string for _, addr := range outputAddrs { addrs = append(addrs, addr.String()) } return logging.Metadata{"addresses": addrs} }()) log.Debug("ResolveUnspecifiedAddresses:", unspecAddrs, ifaceAddrs, outputAddrs) return outputAddrs, nil }
// Context returns the IpfsNode context func (n *IpfsNode) Context() context.Context { if n.ctx == nil { n.ctx = context.TODO() } return n.ctx }
func (rs *mocknetserver) Client(p testutil.Identity) Client { return rs.ClientWithDatastore(context.TODO(), p, ds.NewMapDatastore()) }