@@ -10,6 +10,7 @@ import (
1010
1111 "github.com/ethereum/go-ethereum/crypto"
1212 "github.com/ethereum/go-ethereum/logger"
13+ "github.com/ethereum/go-ethereum/p2p/nat"
1314 "github.com/ethereum/go-ethereum/rlp"
1415)
1516
@@ -82,6 +83,7 @@ type udp struct {
8283 addpending chan * pending
8384 replies chan reply
8485 closing chan struct {}
86+ nat nat.Interface
8587
8688 * Table
8789}
@@ -121,17 +123,26 @@ type reply struct {
121123}
122124
123125// ListenUDP returns a new table that listens for UDP packets on laddr.
124- func ListenUDP (priv * ecdsa.PrivateKey , laddr string ) (* Table , error ) {
125- net , realaddr , err := listen (priv , laddr )
126+ func ListenUDP (priv * ecdsa.PrivateKey , laddr string , natm nat. Interface ) (* Table , error ) {
127+ t , realaddr , err := listen (priv , laddr , natm )
126128 if err != nil {
127129 return nil , err
128130 }
129- net .Table = newTable (net , PubkeyID (& priv .PublicKey ), realaddr )
130- log .Debugf ("Listening, %v\n " , net .self )
131- return net .Table , nil
131+ if natm != nil {
132+ if ! realaddr .IP .IsLoopback () {
133+ go nat .Map (natm , t .closing , "udp" , realaddr .Port , realaddr .Port , "ethereum discovery" )
134+ }
135+ // TODO: react to external IP changes over time.
136+ if ext , err := natm .ExternalIP (); err == nil {
137+ realaddr = & net.UDPAddr {IP : ext , Port : realaddr .Port }
138+ }
139+ }
140+ t .Table = newTable (t , PubkeyID (& priv .PublicKey ), realaddr )
141+ log .Infoln ("Listening, " , t .self )
142+ return t .Table , nil
132143}
133144
134- func listen (priv * ecdsa.PrivateKey , laddr string ) (* udp , * net.UDPAddr , error ) {
145+ func listen (priv * ecdsa.PrivateKey , laddr string , nat nat. Interface ) (* udp , * net.UDPAddr , error ) {
135146 addr , err := net .ResolveUDPAddr ("udp" , laddr )
136147 if err != nil {
137148 return nil , nil , err
0 commit comments