1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-10 22:49:13 +08:00

updated multiaddr use across codebase

This commit is contained in:
Juan Batiz-Benet
2014-10-06 04:13:39 -07:00
parent 20a20c9e1a
commit 910a76e220
12 changed files with 25 additions and 39 deletions

View File

@ -177,14 +177,14 @@ func initIdentity(cfg *config.Config, online bool) (*peer.Peer, error) {
}
// address is optional
var addresses []*ma.Multiaddr
var addresses []ma.Multiaddr
if len(cfg.Addresses.Swarm) > 0 {
maddr, err := ma.NewMultiaddr(cfg.Addresses.Swarm)
if err != nil {
return nil, err
}
addresses = []*ma.Multiaddr{maddr}
addresses = []ma.Multiaddr{maddr}
}
var (

View File

@ -39,7 +39,7 @@ type Command struct {
Opts map[string]interface{}
}
func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir string) (*DaemonListener, error) {
func NewDaemonListener(ipfsnode *core.IpfsNode, addr ma.Multiaddr, confdir string) (*DaemonListener, error) {
var err error
confdir, err = u.TildeExpansion(confdir)
if err != nil {
@ -51,7 +51,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir stri
return nil, err
}
network, host, err := addr.DialArgs()
network, host, err := ma.DialArgs(addr)
if err != nil {
return nil, err
}
@ -62,12 +62,7 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr *ma.Multiaddr, confdir stri
return nil, err
}
mstr, err := addr.String()
if err != nil {
return nil, err
}
_, err = ofi.Write([]byte(mstr))
_, err = ofi.Write([]byte(addr.String()))
if err != nil {
log.Warning("Could not write to rpcaddress file: %s", err)
return nil, err

View File

@ -73,7 +73,7 @@ func SendCommand(command *Command, confdir string) error {
return err
}
network, host, err := maddr.DialArgs()
network, host, err := ma.DialArgs(maddr)
conn, err := net.Dial(network, host)
if err != nil {

View File

@ -21,7 +21,7 @@ const MaxMessageSize = 1 << 20
// Conn represents a connection to another Peer (IPFS Node).
type Conn struct {
Peer *peer.Peer
Addr *ma.Multiaddr
Addr ma.Multiaddr
Conn net.Conn
Closed chan bool
@ -34,7 +34,7 @@ type Conn struct {
type Map map[u.Key]*Conn
// NewConn constructs a new connection
func NewConn(peer *peer.Peer, addr *ma.Multiaddr, nconn net.Conn) (*Conn, error) {
func NewConn(peer *peer.Peer, addr ma.Multiaddr, nconn net.Conn) (*Conn, error) {
conn := &Conn{
Peer: peer,
Addr: addr,
@ -56,7 +56,7 @@ func Dial(network string, peer *peer.Peer) (*Conn, error) {
return nil, fmt.Errorf("No address for network %s", network)
}
network, host, err := addr.DialArgs()
network, host, err := ma.DialArgs(addr)
if err != nil {
return nil, err
}
@ -104,6 +104,6 @@ func (c *Conn) Close() error {
// NetConnMultiaddr returns the net.Conn's address, recast as a multiaddr.
// (consider moving this directly into the multiaddr package)
func NetConnMultiaddr(nconn net.Conn) (*ma.Multiaddr, error) {
func NetConnMultiaddr(nconn net.Conn) (ma.Multiaddr, error) {
return ma.FromNetAddr(nconn.RemoteAddr())
}

View File

@ -37,8 +37,8 @@ func (s *Swarm) listen() error {
}
// Listen for new connections on the given multiaddr
func (s *Swarm) connListen(maddr *ma.Multiaddr) error {
netstr, addr, err := maddr.DialArgs()
func (s *Swarm) connListen(maddr ma.Multiaddr) error {
netstr, addr, err := ma.DialArgs(maddr)
if err != nil {
return err
}

View File

@ -143,7 +143,7 @@ func (s *Swarm) Dial(peer *peer.Peer) (*conn.Conn, error) {
// DialAddr is for connecting to a peer when you know their addr but not their ID.
// Should only be used when sure that not connected to peer in question
// TODO(jbenet) merge with Dial? need way to patch back.
func (s *Swarm) DialAddr(addr *ma.Multiaddr) (*conn.Conn, error) {
func (s *Swarm) DialAddr(addr ma.Multiaddr) (*conn.Conn, error) {
if addr == nil {
return nil, errors.New("addr must be a non-nil Multiaddr")
}

View File

@ -95,7 +95,7 @@ func TestSwarm(t *testing.T) {
if a == nil {
t.Fatal("error setting up peer (addr is nil)", peer)
}
n, h, err := a.DialArgs()
n, h, err := ma.DialArgs(a)
if err != nil {
t.Fatal("error getting dial args from addr")
}

View File

@ -38,7 +38,7 @@ type Map map[u.Key]*Peer
// ID, and relevant Addresses.
type Peer struct {
ID ID
Addresses []*ma.Multiaddr
Addresses []ma.Multiaddr
PrivKey ic.PrivKey
PubKey ic.PubKey
@ -54,7 +54,7 @@ func (p *Peer) Key() u.Key {
}
// AddAddress adds the given Multiaddr address to Peer's addresses.
func (p *Peer) AddAddress(a *ma.Multiaddr) {
func (p *Peer) AddAddress(a ma.Multiaddr) {
p.Lock()
defer p.Unlock()
@ -67,17 +67,12 @@ func (p *Peer) AddAddress(a *ma.Multiaddr) {
}
// NetAddress returns the first Multiaddr found for a given network.
func (p *Peer) NetAddress(n string) *ma.Multiaddr {
func (p *Peer) NetAddress(n string) ma.Multiaddr {
p.RLock()
defer p.RUnlock()
for _, a := range p.Addresses {
ps, err := a.Protocols()
if err != nil {
continue // invalid addr
}
for _, p := range ps {
for _, p := range a.Protocols() {
if p.Name == n {
return a
}

View File

@ -20,11 +20,7 @@ func peerToPBPeer(p *peer.Peer) *Message_Peer {
if len(p.Addresses) == 0 || p.Addresses[0] == nil {
pbp.Addr = proto.String("")
} else {
addr, err := p.Addresses[0].String()
if err != nil {
//Temp: what situations could cause this?
panic(err)
}
addr := p.Addresses[0].String()
pbp.Addr = &addr
}
pid := string(p.ID)

View File

@ -43,8 +43,8 @@ func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT {
return d
}
func setupDHTS(n int, t *testing.T) ([]*ma.Multiaddr, []*peer.Peer, []*IpfsDHT) {
var addrs []*ma.Multiaddr
func setupDHTS(n int, t *testing.T) ([]ma.Multiaddr, []*peer.Peer, []*IpfsDHT) {
var addrs []ma.Multiaddr
for i := 0; i < n; i++ {
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i))
if err != nil {
@ -67,7 +67,7 @@ func setupDHTS(n int, t *testing.T) ([]*ma.Multiaddr, []*peer.Peer, []*IpfsDHT)
return addrs, peers, dhts
}
func makePeer(addr *ma.Multiaddr) *peer.Peer {
func makePeer(addr ma.Multiaddr) *peer.Peer {
p := new(peer.Peer)
p.AddAddress(addr)
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)

View File

@ -184,7 +184,7 @@ func TestGetFailures(t *testing.T) {
func _randPeer() *peer.Peer {
p := new(peer.Peer)
p.ID = make(peer.ID, 16)
p.Addresses = []*ma.Multiaddr{nil}
p.Addresses = []ma.Multiaddr{nil}
crand.Read(p.ID)
return p
}

View File

@ -17,14 +17,14 @@ type handler struct {
}
// Serve starts the http server
func Serve(address *ma.Multiaddr, node *core.IpfsNode) error {
func Serve(address ma.Multiaddr, node *core.IpfsNode) error {
r := mux.NewRouter()
handler := &handler{&ipfsHandler{node}}
r.HandleFunc("/ipfs/", handler.postHandler).Methods("POST")
r.PathPrefix("/ipfs/").Handler(handler).Methods("GET")
http.Handle("/", r)
_, host, err := address.DialArgs()
_, host, err := ma.DialArgs(address)
if err != nil {
return err
}