Skip to content

Commit

Permalink
Merge pull request #104 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
crioto committed Mar 27, 2016
2 parents 339c2aa + e9e3735 commit 16336d8
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 311 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ARCH=$(shell uname -m)

all: pack

$(APP): p2p.go packet.go help.go instance.go main.go
$(APP): help.go instance.go main.go
$(CC) build -ldflags="-w -s -X main.VERSION=$(VERSION)" -o $@ -v $^

$(CP_APP): p2p-cp/cp.go p2p-cp/proxy.go
Expand Down
14 changes: 10 additions & 4 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
ptp "github.com/subutai-io/p2p/lib"
"os"
Expand All @@ -23,7 +24,7 @@ type RunArgs struct {
}

type Instance struct {
PTP *PTPCloud
PTP *ptp.PTPCloud
ID string
Args RunArgs
}
Expand Down Expand Up @@ -197,12 +198,17 @@ func (p *Procedures) Run(args *RunArgs, resp *Response) error {
args.Key = string(key)
}

ptpInstance := p2pmain(args.IP, args.Mac, args.Dev, "", args.Hash, args.Dht, args.Keyfile, args.Key, args.TTL, "", args.Fwd, args.Port)
var newInst Instance
newInst.ID = args.Hash
newInst.PTP = ptpInstance
newInst.Args = *args
Instances[args.Hash] = newInst
ptpInstance := ptp.StartP2PInstance(args.IP, args.Mac, args.Dev, "", args.Hash, args.Dht, args.Keyfile, args.Key, args.TTL, "", args.Fwd, args.Port)
if ptpInstance == nil {
resp.Output = resp.Output + "Failed to create P2P Instance"
return errors.New("Failed to create P2P Instance")
}
newInst.PTP = ptpInstance
Instances[args.Hash] = newInst
go ptpInstance.Run()
if SaveFile != "" {
SaveInstances(SaveFile)
Expand Down Expand Up @@ -262,7 +268,7 @@ func (p *Procedures) Debug(args *Args, resp *Response) error {
resp.Output += fmt.Sprintf("Instances information:\n")
for _, ins := range Instances {
resp.Output += fmt.Sprintf("Hash: %s\n", ins.ID)
resp.Output += fmt.Sprintf("ID: %s\n", ins.PTP.dht.ID)
resp.Output += fmt.Sprintf("ID: %s\n", ins.PTP.Dht.ID)
resp.Output += fmt.Sprintf("Interface %s, HW Addr: %s, IP: %s\n", ins.PTP.DeviceName, ins.PTP.Mac, ins.PTP.IP)
resp.Output += fmt.Sprintf("Peers:\n")
// TODO: Rewrite this part
Expand Down
72 changes: 36 additions & 36 deletions lib/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type PeerIP struct {
Ips []string
}

type DHTResponseCallback func(data DHTResponse, conn *net.UDPConn)
type DHTResponseCallback func(data DHTMessage, conn *net.UDPConn)

func (dht *DHTClient) DHTClientConfig() *DHTClient {
return &DHTClient{
Expand All @@ -82,7 +82,7 @@ func (dht *DHTClient) AddConnection(connections []*net.UDPConn, conn *net.UDPCon

func (dht *DHTClient) Handshake(conn *net.UDPConn) error {
// Handshake
var req DHTRequest
var req DHTMessage
req.Id = "0"
req.Query = PACKET_VERSION
req.Command = CMD_CONN
Expand Down Expand Up @@ -134,8 +134,8 @@ func (dht *DHTClient) ConnectAndHandshake(router string, ips []net.IP) (*net.UDP
return conn, err
}

// Extracts DHTRequest from received packet
func (dht *DHTClient) Extract(b []byte) (response DHTResponse, err error) {
// Extracts DHTMessage from received packet
func (dht *DHTClient) Extract(b []byte) (response DHTMessage, err error) {
defer func() {
if x := recover(); x != nil {
Log(ERROR, "Bencode Unmarshal failed %q, %v", string(b), x)
Expand All @@ -150,9 +150,9 @@ func (dht *DHTClient) Extract(b []byte) (response DHTResponse, err error) {
}
}

// Returns a bencoded representation of a DHTRequest
// Returns a bencoded representation of a DHTMessage
func (dht *DHTClient) Compose(command, id, query, arguments string) string {
var req DHTRequest
var req DHTMessage
// Command is mandatory
req.Command = command
// Defaults
Expand All @@ -168,7 +168,7 @@ func (dht *DHTClient) Compose(command, id, query, arguments string) string {
return dht.EncodeRequest(req)
}

func (dht *DHTClient) EncodeRequest(req DHTRequest) string {
func (dht *DHTClient) EncodeRequest(req DHTMessage) string {
if req.Command == "" {
return ""
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func (dht *DHTClient) ListenDHT(conn *net.UDPConn) string {
return ""
}

func (dht *DHTClient) HandleConn(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleConn(data DHTMessage, conn *net.UDPConn) {
if dht.State != D_CONNECTING && dht.State != D_RECONNECTING {
return
}
Expand Down Expand Up @@ -301,18 +301,18 @@ func (dht *DHTClient) HandleConn(data DHTResponse, conn *net.UDPConn) {
}
}

func (dht *DHTClient) HandlePing(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandlePing(data DHTMessage, conn *net.UDPConn) {
msg := dht.Compose(CMD_PING, dht.ID, "", "")
_, err := conn.Write([]byte(msg))
if err != nil {
Log(ERROR, "Failed to send 'ping' packet: %v", err)
}
}

func (dht *DHTClient) HandleFind(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleFind(data DHTMessage, conn *net.UDPConn) {
// This means we've received a list of nodes we can connect to
if data.Dest != "" {
ids := strings.Split(data.Dest, ",")
if data.Arguments != "" {
ids := strings.Split(data.Arguments, ",")
if len(ids) == 0 {
Log(ERROR, "Malformed list of peers received")
} else {
Expand Down Expand Up @@ -343,46 +343,46 @@ func (dht *DHTClient) HandleFind(data DHTResponse, conn *net.UDPConn) {
dht.Peers = append(dht.Peers[:i], dht.Peers[i+1:]...)
}
}
Log(DEBUG, "Received peers from %s: %s", conn.RemoteAddr().String(), data.Dest)
dht.UpdateLastCatch(data.Dest)
Log(DEBUG, "Received peers from %s: %s", conn.RemoteAddr().String(), data.Arguments)
dht.UpdateLastCatch(data.Arguments)
}
} else {
dht.Peers = dht.Peers[:0]
}
}

func (dht *DHTClient) HandleRegCp(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleRegCp(data DHTMessage, conn *net.UDPConn) {
Log(INFO, "Control peer has been registered in Service Discovery Peer")
// We've received a registration confirmation message from DHT bootstrap node
}

func (dht *DHTClient) HandleNode(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleNode(data DHTMessage, conn *net.UDPConn) {
// We've received an IPs associated with target node
for i, peer := range dht.Peers {
if peer.ID == data.Id {
ips := strings.Split(data.Dest, "|")
ips := strings.Split(data.Arguments, "|")
dht.Peers[i].Ips = ips
}
}
}

func (dht *DHTClient) HandleCp(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleCp(data DHTMessage, conn *net.UDPConn) {
// We've received information about proxy
if data.Dest == "0" {
if data.Arguments == "0" || data.Arguments == "" {
return
}
Log(INFO, "Received control peer %s. Saving", data.Dest)
Log(INFO, "Received control peer %s. Saving", data.Arguments)
var found bool = false
for _, fwd := range dht.Forwarders {
if fwd.Addr.String() == data.Dest && fwd.DestinationID == data.Id {
if fwd.Addr.String() == data.Arguments && fwd.DestinationID == data.Id {
found = true
}
}
if !found {
var fwd Forwarder
a, err := net.ResolveUDPAddr("udp", data.Dest)
a, err := net.ResolveUDPAddr("udp", data.Arguments)
if err != nil {
Log(ERROR, "Failed to resolve UDP Address for proxy %s", data.Dest)
Log(ERROR, "Failed to resolve UDP Address for proxy %s", data.Arguments)
} else {
fwd.Addr = a
fwd.DestinationID = data.Id
Expand All @@ -403,24 +403,24 @@ func (dht *DHTClient) HandleCp(data DHTResponse, conn *net.UDPConn) {
}
}

func (dht *DHTClient) HandleNotify(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleNotify(data DHTMessage, conn *net.UDPConn) {
// Notify means we should ask DHT bootstrap node for a control peer
// in order to connect to a node that can't reach us
dht.RequestControlPeer(data.Id)
}

func (dht *DHTClient) HandleStop(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleStop(data DHTMessage, conn *net.UDPConn) {
conn.Close()
}

func (dht *DHTClient) HandleDHCP(data DHTResponse, conn *net.UDPConn) {
if data.Dest == "ok" {
func (dht *DHTClient) HandleDHCP(data DHTMessage, conn *net.UDPConn) {
if data.Arguments == "ok" {
Log(INFO, "DHCP Registration confirmed")
return
} else {
Log(INFO, "Received DHCP Information")
}
ip, ipnet, err := net.ParseCIDR(data.Dest)
ip, ipnet, err := net.ParseCIDR(data.Arguments)
if err != nil {
Log(ERROR, "Failed to parse received DHCP packet: %v", err)
return
Expand All @@ -429,7 +429,7 @@ func (dht *DHTClient) HandleDHCP(data DHTResponse, conn *net.UDPConn) {
dht.Network = ipnet
}

func (dht *DHTClient) HandleUnknown(data DHTResponse, conn *net.UDPConn) {
func (dht *DHTClient) HandleUnknown(data DHTMessage, conn *net.UDPConn) {
Log(WARNING, "DHT server refuses our identity")
if dht.State == D_CONNECTING || dht.State == D_RECONNECTING {
time.Sleep(3 * time.Second)
Expand All @@ -442,10 +442,10 @@ func (dht *DHTClient) HandleUnknown(data DHTResponse, conn *net.UDPConn) {
}
}

func (dht *DHTClient) HandleError(data DHTResponse, conn *net.UDPConn) {
e, exists := ErrorList[ErrorType(data.Dest)]
func (dht *DHTClient) HandleError(data DHTMessage, conn *net.UDPConn) {
e, exists := ErrorList[ErrorType(data.Arguments)]
if !exists {
Log(ERROR, "Unknown error were received from DHT: %s", data.Dest)
Log(ERROR, "Unknown error were received from DHT: %s", data.Arguments)
} else {
Log(ERROR, "DHT returned error: %s", e.Error())
}
Expand Down Expand Up @@ -504,7 +504,7 @@ func (dht *DHTClient) RegisterControlPeer() {
for len(dht.ID) != 36 {
time.Sleep(1 * time.Second)
}
var req DHTRequest
var req DHTMessage
var err error
req.Id = dht.ID
req.Query = "0"
Expand Down Expand Up @@ -532,7 +532,7 @@ func (dht *DHTClient) RegisterControlPeer() {

// This method request a new control peer for particular host
func (dht *DHTClient) RequestControlPeer(id string) {
var req DHTRequest
var req DHTMessage
var err error
req.Id = dht.ID
req.Query = ""
Expand Down Expand Up @@ -563,7 +563,7 @@ func (dht *DHTClient) RequestControlPeer(id string) {
}

func (dht *DHTClient) ReportControlPeerLoad(amount int) {
var req DHTRequest
var req DHTMessage
req.Id = dht.ID
req.Command = CMD_LOAD
req.Arguments = fmt.Sprintf("%d", amount)
Expand Down Expand Up @@ -606,7 +606,7 @@ func (dht *DHTClient) SendIP(ip string, mask string) {

func (dht *DHTClient) Stop() {
dht.Shutdown = true
var req DHTRequest
var req DHTMessage
req.Id = dht.ID
req.Command = CMD_STOP
req.Arguments = "0"
Expand Down
Loading

0 comments on commit 16336d8

Please sign in to comment.