// DialAddr connects to a peer over a particular address // Ensures raddr is part of peer.Addresses() // Example: d.DialAddr(ctx, peer.Addresses()[0], peer) func (d *Dialer) DialAddr(ctx context.Context, raddr ma.Multiaddr, remote peer.Peer) (Conn, error) { found := false for _, addr := range remote.Addresses() { if addr.Equal(raddr) { found = true } } if !found { return nil, debugerror.Errorf("address %s is not in peer %s", raddr, remote) } network, _, err := manet.DialArgs(raddr) if err != nil { return nil, err } laddr := d.LocalPeer.NetAddress(network) if laddr == nil { return nil, debugerror.Errorf("No local address for network %s", network) } if strings.HasPrefix(raddr.String(), "/ip4/0.0.0.0") { return nil, debugerror.Errorf("Attempted to connect to zero address: %s", raddr) } remote.SetType(peer.Remote) remote, err = d.Peerstore.Add(remote) if err != nil { log.Errorf("Error putting peer into peerstore: %s", remote) } // TODO: try to get reusing addr/ports to work. // madialer := manet.Dialer{LocalAddr: laddr} madialer := manet.Dialer{} log.Infof("%s dialing %s %s", d.LocalPeer, remote, raddr) maconn, err := madialer.Dial(raddr) if err != nil { return nil, err } c, err := newSingleConn(ctx, d.LocalPeer, remote, maconn) if err != nil { return nil, err } return newSecureConn(ctx, c, d.Peerstore) }
// Dial connects to a particular peer, over a given network // Example: d.Dial(ctx, "udp", peer) func (d *Dialer) Dial(ctx context.Context, network string, remote peer.Peer) (Conn, error) { raddr := remote.NetAddress(network) if raddr == nil { return nil, debugerror.Errorf("No remote address for network %s", network) } return d.DialAddr(ctx, raddr, remote) }
func makeLevelDBDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) { if len(cfg.Path) == 0 { return nil, debugerror.Errorf("config datastore.path required for leveldb") } ds, err := lds.NewDatastore(cfg.Path, &lds.Options{ // TODO don't import ldbopts. Get from go-datastore.leveldb Compression: ldbopts.NoCompression, }) return ds, debugerror.Wrap(err) }
func makeDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) { if len(cfg.Type) == 0 { return nil, debugerror.Errorf("config datastore.type required") } switch cfg.Type { case "leveldb": return makeLevelDBDatastore(cfg) case "memory": return u.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil case "fs": log.Warning("using fs.Datastore at .datastore for testing.") d, err := fsds.NewDatastore(".datastore") // for testing!! if err != nil { return nil, err } ktd := ktds.Wrap(d, u.B58KeyConverter) return u.CloserWrap(syncds.MutexWrap(ktd)), nil } return nil, debugerror.Errorf("Unknown datastore type: %s", cfg.Type) }
// NewIpfsNode constructs a new IpfsNode based on the given config. func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) { success := false // flip to true after all sub-system inits succeed defer func() { if !success && n != nil { n.Close() } }() if cfg == nil { return nil, debugerror.Errorf("configuration required") } // derive this from a higher context. ctx := context.TODO() n = &IpfsNode{ onlineMode: online, Config: cfg, } n.ContextCloser = ctxc.NewContextCloser(ctx, n.teardown) // setup datastore. if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil { return nil, debugerror.Wrap(err) } // setup peerstore + local peer identity n.Peerstore = peer.NewPeerstore() n.Identity, err = initIdentity(&n.Config.Identity, n.Peerstore, online) if err != nil { return nil, debugerror.Wrap(err) } // setup online services if online { dhtService := netservice.NewService(ctx, nil) // nil handler for now, need to patch it exchangeService := netservice.NewService(ctx, nil) // nil handler for now, need to patch it diagService := netservice.NewService(ctx, nil) // nil handler for now, need to patch it muxMap := &mux.ProtocolMap{ mux.ProtocolID_Routing: dhtService, mux.ProtocolID_Exchange: exchangeService, mux.ProtocolID_Diagnostic: diagService, // add protocol services here. } // setup the network listenAddrs, err := listenAddresses(cfg) if err != nil { return nil, debugerror.Wrap(err) } n.Network, err = inet.NewIpfsNetwork(ctx, listenAddrs, n.Identity, n.Peerstore, muxMap) if err != nil { return nil, debugerror.Wrap(err) } n.AddCloserChild(n.Network) // setup diagnostics service n.Diagnostics = diag.NewDiagnostics(n.Identity, n.Network, diagService) diagService.SetHandler(n.Diagnostics) // setup routing service dhtRouting := dht.NewDHT(ctx, n.Identity, n.Peerstore, n.Network, dhtService, n.Datastore) dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord // TODO(brian): perform this inside NewDHT factory method dhtService.SetHandler(dhtRouting) // wire the handler to the service. n.Routing = dhtRouting n.AddCloserChild(dhtRouting) // setup exchange service const alwaysSendToPeer = true // use YesManStrategy bitswapNetwork := bsnet.NewFromIpfsNetwork(exchangeService, n.Network) n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Routing, n.Datastore, alwaysSendToPeer) go initConnections(ctx, n.Config, n.Peerstore, dhtRouting) } // TODO(brian): when offline instantiate the BlockService with a bitswap // session that simply doesn't return blocks n.Blocks, err = bserv.NewBlockService(n.Datastore, n.Exchange) if err != nil { return nil, debugerror.Wrap(err) } n.DAG = merkledag.NewDAGService(n.Blocks) n.Namesys = namesys.NewNameSystem(n.Routing) n.Pinning, err = pin.LoadPinner(n.Datastore, n.DAG) if err != nil { n.Pinning = pin.NewPinner(n.Datastore, n.DAG) } n.Resolver = &path.Resolver{DAG: n.DAG} success = true return n, nil }