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 ¶
const ( UDP = Protocol("udp") TCP = Protocol("tcp") )
Variables ¶
This section is empty.
Functions ¶
func IsFiveTuple ¶
Types ¶
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 ¶
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 ¶
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 ¶
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 ¶
FiveTuple gets the FiveTuple from the Traversal, blocking until such is available or the configured timeout is hit.
func (*Traversal) MsgIn ¶
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 ¶
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.