natty

package
v0.0.2-0...-b7deada Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2015 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Overview

Package natty provides a Go language wrapper to the natty NAT traversal utility. See https://github.com/getlantern/natty.

See natty_test for an example of Natty in use, including debug logging showing the messages that are sent across the signaling channel.

Index

Examples

Constants

View Source
const (
	UDP = Protocol("udp")
	TCP = Protocol("tcp")
)

Variables

This section is empty.

Functions

func IsError

func IsError(msg string) bool

func IsFiveTuple

func IsFiveTuple(msg string) bool

Types

type FiveTuple

type FiveTuple struct {
	Proto  Protocol
	Local  string
	Remote string
}

A FiveTuple is the result of a successful NAT traversal.

func (*FiveTuple) UDPAddrs

func (ft *FiveTuple) UDPAddrs() (local *net.UDPAddr, remote *net.UDPAddr, err error)

UDPAddrs returns a pair of UDPAddrs representing the Local and Remote addresses of this FiveTuple. If the FiveTuple's Proto is not UDP, this method returns an error.

type Protocol

type Protocol string

type Traversal

type Traversal struct {
	// contains filtered or unexported fields
}

Traversal represents a single NAT traversal using natty, whose result is available via the methods FiveTuple() and FiveTupleTimeout().

Consumers should make sure to call Close() after finishing with this Natty in order to make sure the underlying natty process and associated resources are closed.

func Answer

func Answer(timeout time.Duration) *Traversal

Answer starts a Traversal as an Answerer, meaning that it will accept offers to initiate an ICE session. Call FiveTuple() to get the FiveTuple resulting from Traversal. If timeout is hit, the traversal will stop and FiveTuple() will return an error. A timeout of 0 means that the Traversal will never time out.

Example
t := natty.Answer(15 * time.Second)
defer t.Close()

// Process outbound messages
go func() {
	for {
		msg, done := t.NextMsgOut()
		if done {
			return
		}
		// TODO: Send message to peer via signaling channel
		elog.Printf("Sent: %s", msg)
	}
}()

// Process inbound messages
go func() {
	for {
		// TODO: Get message from signaling channel
		var msg string
		t.MsgIn(msg)
	}
}()

fiveTuple, err := t.FiveTuple()
if err != nil {
	elog.Fatal(err)
}

local, _, err := fiveTuple.UDPAddrs()
if err != nil {
	elog.Fatal(err)
}

conn, err := net.ListenUDP("udp", local)
if err != nil {
	elog.Fatal(err)
}

// TODO: signal to peer that we're ready to receive traffic

b := make([]byte, 1024)
for {
	n, addr, err := conn.ReadFrom(b)
	if err != nil {
		elog.Fatal(err)
	}

	elog.Printf("Received message '%s' from %s", string(b[:n]), addr)
}
Output:

func Offer

func Offer(timeout time.Duration) *Traversal

Offer starts a Traversal as an Offerer, meaning that it will make an offer to initiate an ICE session. Call FiveTuple() to get the FiveTuple resulting from Traversal. If timeout is hit, the traversal will stop and FiveTuple() will return an error. A timeout of 0 means that the Traversal will never time out.

Example
t := natty.Offer(15 * time.Second)
defer t.Close()

// Process outbound messages
go func() {
	for {
		msg, done := t.NextMsgOut()
		if done {
			return
		}
		// TODO: Send message to peer via signaling channel
		elog.Printf("Sent: %s", msg)
	}
}()

// Process inbound messages
go func() {
	for {
		// TODO: Get message from signaling channel
		var msg string
		t.MsgIn(msg)
	}
}()

// Try it with a really short timeout (should error)
fiveTuple, err := t.FiveTuple()
if err != nil {
	elog.Fatal(err)
}

local, remote, err := fiveTuple.UDPAddrs()
if err != nil {
	elog.Fatal(err)
}

// TODO: Wait for peer to signal that it's ready to receive traffic
conn, err := net.DialUDP("udp", local, remote)
if err != nil {
	elog.Fatal(err)
}
for {
	_, err := conn.Write([]byte("My data"))
	if err != nil {
		elog.Fatal(err)
	}
}
Output:

func (*Traversal) Close

func (t *Traversal) Close() error

Close closes this Traversal, terminating any outstanding natty process by sending SIGKILL. Close blocks until the natty process has terminated, at which point any ports that it bound should be available for use.

func (*Traversal) FiveTuple

func (t *Traversal) FiveTuple() (*FiveTuple, error)

FiveTuple gets the FiveTuple from the Traversal, blocking until such is available or the configured timeout is hit.

func (*Traversal) MsgIn

func (t *Traversal) MsgIn(msg string)

MsgIn is used to pass this Traversal a message from the peer t. This method is buffered and will typically not block.

func (*Traversal) NextMsgOut

func (t *Traversal) NextMsgOut() (msg string, done bool)

NextMsgOut gets the next message to pass to the peer. If done is true, there are no more messages to be read, and the currently returned message should be ignored.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL