-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1424 from libp2p/merge-quic
move go-libp2p-quic-transport here
- Loading branch information
Showing
21 changed files
with
2,792 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
ic "github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 3 { | ||
fmt.Printf("Usage: %s <multiaddr> <peer id>", os.Args[0]) | ||
return | ||
} | ||
if err := run(os.Args[1], os.Args[2]); err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
} | ||
|
||
func run(raddr string, p string) error { | ||
peerID, err := peer.Decode(p) | ||
if err != nil { | ||
return err | ||
} | ||
addr, err := ma.NewMultiaddr(raddr) | ||
if err != nil { | ||
return err | ||
} | ||
priv, _, err := ic.GenerateECDSAKeyPair(rand.Reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t, err := libp2pquic.NewTransport(priv, nil, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("Dialing %s\n", addr.String()) | ||
conn, err := t.Dial(context.Background(), addr, peerID) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
str, err := conn.OpenStream(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
defer str.Close() | ||
const msg = "Hello world!" | ||
log.Printf("Sending: %s\n", msg) | ||
if _, err := str.Write([]byte(msg)); err != nil { | ||
return err | ||
} | ||
if err := str.CloseWrite(); err != nil { | ||
return err | ||
} | ||
data, err := ioutil.ReadAll(str) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Received: %s\n", data) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
ic "github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
tpt "github.com/libp2p/go-libp2p-core/transport" | ||
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Printf("Usage: %s <port>", os.Args[0]) | ||
return | ||
} | ||
if err := run(os.Args[1]); err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
} | ||
|
||
func run(port string) error { | ||
addr, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/udp/%s/quic", port)) | ||
if err != nil { | ||
return err | ||
} | ||
priv, _, err := ic.GenerateECDSAKeyPair(rand.Reader) | ||
if err != nil { | ||
return err | ||
} | ||
peerID, err := peer.IDFromPrivateKey(priv) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t, err := libp2pquic.NewTransport(priv, nil, nil, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ln, err := t.Listen(addr) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Listening. Now run: go run cmd/client/main.go %s %s\n", ln.Multiaddr(), peerID) | ||
for { | ||
conn, err := ln.Accept() | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Accepted new connection from %s (%s)\n", conn.RemotePeer(), conn.RemoteMultiaddr()) | ||
go func() { | ||
if err := handleConn(conn); err != nil { | ||
log.Printf("handling conn failed: %s", err.Error()) | ||
} | ||
}() | ||
} | ||
} | ||
|
||
func handleConn(conn tpt.CapableConn) error { | ||
str, err := conn.AcceptStream() | ||
if err != nil { | ||
return err | ||
} | ||
data, err := ioutil.ReadAll(str) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Received: %s\n", data) | ||
if _, err := str.Write(data); err != nil { | ||
return err | ||
} | ||
return str.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package libp2pquic | ||
|
||
import ( | ||
"context" | ||
|
||
ic "github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/network" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
tpt "github.com/libp2p/go-libp2p-core/transport" | ||
|
||
"github.com/lucas-clemente/quic-go" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
type conn struct { | ||
quicConn quic.Connection | ||
pconn *reuseConn | ||
transport *transport | ||
scope network.ConnManagementScope | ||
|
||
localPeer peer.ID | ||
privKey ic.PrivKey | ||
localMultiaddr ma.Multiaddr | ||
|
||
remotePeerID peer.ID | ||
remotePubKey ic.PubKey | ||
remoteMultiaddr ma.Multiaddr | ||
} | ||
|
||
var _ tpt.CapableConn = &conn{} | ||
|
||
// Close closes the connection. | ||
// It must be called even if the peer closed the connection in order for | ||
// garbage collection to properly work in this package. | ||
func (c *conn) Close() error { | ||
c.transport.removeConn(c.quicConn) | ||
err := c.quicConn.CloseWithError(0, "") | ||
c.pconn.DecreaseCount() | ||
c.scope.Done() | ||
return err | ||
} | ||
|
||
// IsClosed returns whether a connection is fully closed. | ||
func (c *conn) IsClosed() bool { | ||
return c.quicConn.Context().Err() != nil | ||
} | ||
|
||
func (c *conn) allowWindowIncrease(size uint64) bool { | ||
return c.scope.ReserveMemory(int(size), network.ReservationPriorityMedium) == nil | ||
} | ||
|
||
// OpenStream creates a new stream. | ||
func (c *conn) OpenStream(ctx context.Context) (network.MuxedStream, error) { | ||
qstr, err := c.quicConn.OpenStreamSync(ctx) | ||
return &stream{Stream: qstr}, err | ||
} | ||
|
||
// AcceptStream accepts a stream opened by the other side. | ||
func (c *conn) AcceptStream() (network.MuxedStream, error) { | ||
qstr, err := c.quicConn.AcceptStream(context.Background()) | ||
return &stream{Stream: qstr}, err | ||
} | ||
|
||
// LocalPeer returns our peer ID | ||
func (c *conn) LocalPeer() peer.ID { | ||
return c.localPeer | ||
} | ||
|
||
// LocalPrivateKey returns our private key | ||
func (c *conn) LocalPrivateKey() ic.PrivKey { | ||
return c.privKey | ||
} | ||
|
||
// RemotePeer returns the peer ID of the remote peer. | ||
func (c *conn) RemotePeer() peer.ID { | ||
return c.remotePeerID | ||
} | ||
|
||
// RemotePublicKey returns the public key of the remote peer. | ||
func (c *conn) RemotePublicKey() ic.PubKey { | ||
return c.remotePubKey | ||
} | ||
|
||
// LocalMultiaddr returns the local Multiaddr associated | ||
func (c *conn) LocalMultiaddr() ma.Multiaddr { | ||
return c.localMultiaddr | ||
} | ||
|
||
// RemoteMultiaddr returns the remote Multiaddr associated | ||
func (c *conn) RemoteMultiaddr() ma.Multiaddr { | ||
return c.remoteMultiaddr | ||
} | ||
|
||
func (c *conn) Transport() tpt.Transport { | ||
return c.transport | ||
} | ||
|
||
func (c *conn) Scope() network.ConnScope { | ||
return c.scope | ||
} |
Oops, something went wrong.