Commit dfa778fe authored by obscuren's avatar obscuren

UPNP wip

parent 8c4746a3
......@@ -8,6 +8,7 @@ import (
"github.com/ethereum/ethwire-go"
"log"
"net"
"strconv"
"sync/atomic"
"time"
)
......@@ -40,6 +41,10 @@ type Ethereum struct {
peers *list.List
// Nonce
Nonce uint64
Addr net.Addr
nat NAT
}
func New() (*Ethereum, error) {
......@@ -51,12 +56,20 @@ func New() (*Ethereum, error) {
ethutil.Config.Db = db
/*
nat, err := Discover()
if err != nil {
log.Printf("Can'them discover upnp: %v", err)
}
*/
nonce, _ := ethutil.RandomUint64()
ethereum := &Ethereum{
shutdownChan: make(chan bool),
db: db,
peers: list.New(),
Nonce: nonce,
//nat: nat,
}
ethereum.TxPool = ethchain.NewTxPool()
ethereum.TxPool.Speaker = ethereum
......@@ -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
func (s *Ethereum) Start() {
// Bind to addr and port
ln, err := net.Listen("tcp", ":30303")
if err != nil {
// This is mainly for testing to create a "network"
if ethutil.Config.Debug {
log.Println("Connection listening disabled. Acting as client")
} else {
//if ethutil.Config.Debug {
//log.Println("Connection listening disabled. Acting as client")
//} else {
log.Fatal(err)
}
//}
} else {
s.Addr = ln.Addr()
// Starting accepting connections
go func() {
log.Println("Ready and accepting connections")
......
......@@ -253,14 +253,13 @@ out:
case ethwire.MsgPeersTy:
// Received a list of peers (probably because MsgGetPeersTy was send)
// Only act on message if we actually requested for a peers list
if p.requestedPeerList {
//if p.requestedPeerList {
data := msg.Data
// Create new list of possible peers for the ethereum to process
peers := make([]string, data.Length())
// Parse each possible peer
for i := 0; i < data.Length(); i++ {
peers[i] = unpackAddr(data.Get(i).Get(0).AsBytes(), data.Get(i).Get(1).AsUint())
log.Println(peers[i])
peers[i] = unpackAddr(data.Get(i).Get(0), data.Get(i).Get(1).AsUint())
}
// Connect to the list of peers
......@@ -268,7 +267,7 @@ out:
// Mark unrequested again
p.requestedPeerList = false
}
//}
case ethwire.MsgGetChainTy:
var parent *ethchain.Block
// 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) {
return host, uint16(prt)
}
func unpackAddr(h []byte, p uint64) string {
if len(h) != 4 {
return ""
}
a := strconv.Itoa(int(h[0]))
b := strconv.Itoa(int(h[1]))
c := strconv.Itoa(int(h[2]))
d := strconv.Itoa(int(h[3]))
func unpackAddr(value *ethutil.RlpValue, p uint64) string {
a := strconv.Itoa(int(value.Get(0).AsUint()))
b := strconv.Itoa(int(value.Get(1).AsUint()))
c := strconv.Itoa(int(value.Get(2).AsUint()))
d := strconv.Itoa(int(value.Get(3).AsUint()))
host := strings.Join([]string{a, b, c, d}, ".")
port := strconv.Itoa(int(p))
......@@ -349,9 +344,9 @@ func (p *Peer) Start(seed bool) {
if peerHost == servHost {
log.Println("Connected to self")
//p.Stop()
p.Stop()
//return
return
}
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