Commit dfa778fe authored by obscuren's avatar obscuren

UPNP wip

parent 8c4746a3
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"github.com/ethereum/ethwire-go" "github.com/ethereum/ethwire-go"
"log" "log"
"net" "net"
"strconv"
"sync/atomic" "sync/atomic"
"time" "time"
) )
...@@ -40,6 +41,10 @@ type Ethereum struct { ...@@ -40,6 +41,10 @@ type Ethereum struct {
peers *list.List peers *list.List
// Nonce // Nonce
Nonce uint64 Nonce uint64
Addr net.Addr
nat NAT
} }
func New() (*Ethereum, error) { func New() (*Ethereum, error) {
...@@ -51,12 +56,20 @@ func New() (*Ethereum, error) { ...@@ -51,12 +56,20 @@ func New() (*Ethereum, error) {
ethutil.Config.Db = db ethutil.Config.Db = db
/*
nat, err := Discover()
if err != nil {
log.Printf("Can'them discover upnp: %v", err)
}
*/
nonce, _ := ethutil.RandomUint64() nonce, _ := ethutil.RandomUint64()
ethereum := &Ethereum{ ethereum := &Ethereum{
shutdownChan: make(chan bool), shutdownChan: make(chan bool),
db: db, db: db,
peers: list.New(), peers: list.New(),
Nonce: nonce, Nonce: nonce,
//nat: nat,
} }
ethereum.TxPool = ethchain.NewTxPool() ethereum.TxPool = ethchain.NewTxPool()
ethereum.TxPool.Speaker = ethereum ethereum.TxPool.Speaker = ethereum
...@@ -179,18 +192,59 @@ func (s *Ethereum) ReapDeadPeers() { ...@@ -179,18 +192,59 @@ func (s *Ethereum) ReapDeadPeers() {
} }
} }
// FIXME
func (s *Ethereum) upnpUpdateThread() {
// Go off immediately to prevent code duplication, thereafter we renew
// lease every 15 minutes.
timer := time.NewTimer(0 * time.Second)
lport, _ := strconv.ParseInt("30303", 10, 16)
first := true
out:
for {
select {
case <-timer.C:
listenPort, err := s.nat.AddPortMapping("TCP", int(lport), int(lport), "eth listen port", 20*60)
if err != nil {
log.Printf("can't add UPnP port mapping: %v\n", err)
}
if first && err == nil {
externalip, err := s.nat.GetExternalAddress()
if err != nil {
log.Printf("UPnP can't get external address: %v\n", err)
continue out
}
// externalip, listenport
log.Println("Successfully bound via UPnP to", externalip, listenPort)
first = false
}
timer.Reset(time.Minute * 15)
case <-s.shutdownChan:
break out
}
}
timer.Stop()
if err := s.nat.DeletePortMapping("tcp", int(lport), int(lport)); err != nil {
log.Printf("unable to remove UPnP port mapping: %v\n", err)
} else {
log.Printf("succesfully disestablished UPnP port mapping\n")
}
}
// Start the ethereum // Start the ethereum
func (s *Ethereum) Start() { func (s *Ethereum) Start() {
// Bind to addr and port // Bind to addr and port
ln, err := net.Listen("tcp", ":30303") ln, err := net.Listen("tcp", ":30303")
if err != nil { if err != nil {
// This is mainly for testing to create a "network" // This is mainly for testing to create a "network"
if ethutil.Config.Debug { //if ethutil.Config.Debug {
log.Println("Connection listening disabled. Acting as client") //log.Println("Connection listening disabled. Acting as client")
} else { //} else {
log.Fatal(err) log.Fatal(err)
} //}
} else { } else {
s.Addr = ln.Addr()
// Starting accepting connections // Starting accepting connections
go func() { go func() {
log.Println("Ready and accepting connections") log.Println("Ready and accepting connections")
......
...@@ -253,22 +253,21 @@ out: ...@@ -253,22 +253,21 @@ out:
case ethwire.MsgPeersTy: case ethwire.MsgPeersTy:
// Received a list of peers (probably because MsgGetPeersTy was send) // Received a list of peers (probably because MsgGetPeersTy was send)
// Only act on message if we actually requested for a peers list // Only act on message if we actually requested for a peers list
if p.requestedPeerList { //if p.requestedPeerList {
data := msg.Data data := msg.Data
// Create new list of possible peers for the ethereum to process // Create new list of possible peers for the ethereum to process
peers := make([]string, data.Length()) peers := make([]string, data.Length())
// Parse each possible peer // Parse each possible peer
for i := 0; i < data.Length(); i++ { for i := 0; i < data.Length(); i++ {
peers[i] = unpackAddr(data.Get(i).Get(0).AsBytes(), data.Get(i).Get(1).AsUint()) peers[i] = unpackAddr(data.Get(i).Get(0), data.Get(i).Get(1).AsUint())
log.Println(peers[i]) }
}
// Connect to the list of peers // Connect to the list of peers
p.ethereum.ProcessPeerList(peers) p.ethereum.ProcessPeerList(peers)
// Mark unrequested again // Mark unrequested again
p.requestedPeerList = false p.requestedPeerList = false
} //}
case ethwire.MsgGetChainTy: case ethwire.MsgGetChainTy:
var parent *ethchain.Block var parent *ethchain.Block
// Length minus one since the very last element in the array is a count // Length minus one since the very last element in the array is a count
...@@ -326,15 +325,11 @@ func packAddr(address, port string) ([]byte, uint16) { ...@@ -326,15 +325,11 @@ func packAddr(address, port string) ([]byte, uint16) {
return host, uint16(prt) return host, uint16(prt)
} }
func unpackAddr(h []byte, p uint64) string { func unpackAddr(value *ethutil.RlpValue, p uint64) string {
if len(h) != 4 { a := strconv.Itoa(int(value.Get(0).AsUint()))
return "" b := strconv.Itoa(int(value.Get(1).AsUint()))
} c := strconv.Itoa(int(value.Get(2).AsUint()))
d := strconv.Itoa(int(value.Get(3).AsUint()))
a := strconv.Itoa(int(h[0]))
b := strconv.Itoa(int(h[1]))
c := strconv.Itoa(int(h[2]))
d := strconv.Itoa(int(h[3]))
host := strings.Join([]string{a, b, c, d}, ".") host := strings.Join([]string{a, b, c, d}, ".")
port := strconv.Itoa(int(p)) port := strconv.Itoa(int(p))
...@@ -349,9 +344,9 @@ func (p *Peer) Start(seed bool) { ...@@ -349,9 +344,9 @@ func (p *Peer) Start(seed bool) {
if peerHost == servHost { if peerHost == servHost {
log.Println("Connected to self") log.Println("Connected to self")
//p.Stop() p.Stop()
//return return
} }
if p.inbound { if p.inbound {
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment