Skip to content

Commit

Permalink
Merge pull request #180 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
crioto authored Jul 19, 2016
2 parents 62d0885 + eb993c1 commit e509d3f
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 129 deletions.
43 changes: 43 additions & 0 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func (p *Procedures) Run(args *RunArgs, resp *Response) error {
if ptpInstance == nil {
delete(Instances, args.Hash)
resp.Output = resp.Output + "Failed to create P2P Instance"
resp.ExitCode = 1
Unlock()
return errors.New("Failed to create P2P Instance")
}
Expand Down Expand Up @@ -353,3 +354,45 @@ func (p *Procedures) Debug(args *Args, resp *Response) error {
}
return nil
}

func (p *Procedures) Status(args *RunArgs, resp *Response) error {
for _, ins := range Instances {
resp.Output += ins.ID + " | " + ins.PTP.IP + "\n"
for _, peer := range ins.PTP.NetworkPeers {
resp.Output += peer.ID + "|"
resp.Output += peer.PeerLocalIP.String() + "|"
resp.Output += "State:" + StringifyState(peer.State) + "|"
if peer.LastError != "" {
resp.Output += "LastError:" + peer.LastError
}
resp.Output += "\n"
}
}
return nil
}

func StringifyState(state ptp.PeerState) string {
switch state {
case ptp.P_INIT:
return "Initializing"
case ptp.P_REQUESTED_IP:
return "Waiting for IP"
case ptp.P_CONNECTING_DIRECTLY:
return "Trying direct connection"
case ptp.P_CONNECTED:
return "Connected"
case ptp.P_HANDSHAKING:
return "Handshaking"
case ptp.P_HANDSHAKING_FAILED:
return "Handshaking failed"
case ptp.P_WAITING_FORWARDER:
return "Waiting forwarder IP"
case ptp.P_HANDSHAKING_FORWARDER:
return "Handshaking forwarder"
case ptp.P_DISCONNECT:
return "Disconnected"
case ptp.P_STOP:
return "Stopped"
}
return "Unknown"
}
11 changes: 9 additions & 2 deletions lib/dht_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// +build ignore
package ptp

import (
"testing"
)

func TestExtract(t *testing.T) {

var m string = "d1:a0:1:c4:ping1:i36:00000000-1111-2222-3333-4444444444441:p0:1:q1:0e"
var dht DHTClient
result, err := dht.Extract([]byte(m))
if result.Id != "00000000-1111-2222-3333-444444444444" {
t.Errorf("Failed to extract DHT message")
}
if err != nil {
t.Errorf("Error during DHT message extraction")
}
}
44 changes: 14 additions & 30 deletions lib/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ func CreateStringP2PMessage(c Crypto, data string, netProto uint16) *P2PMessage
msg.Header.NetProto = netProto
msg.Header.Length = uint16(len(data))
msg.Header.Complete = 1
msg.Header.Id = 1
msg.Header.Seq = 1
if c.Active {
var err error
msg.Data, err = c.Encrypt(c.ActiveKey.Key, []byte(data))
Expand All @@ -118,7 +116,20 @@ func CreatePingP2PMessage() *P2PMessage {
msg.Header.Length = uint16(len("1"))
msg.Header.Complete = 1
msg.Header.Id = 0
msg.Header.Seq = 0
msg.Data = []byte("1")
return msg
}

func CreateConfP2PMessage(id, seq uint16) *P2PMessage {
msg := new(P2PMessage)
msg.Header = new(P2PMessageHeader)
msg.Header.Magic = MAGIC_COOKIE
msg.Header.Type = uint16(MT_CONF)
msg.Header.NetProto = 0
msg.Header.Length = uint16(len("1"))
msg.Header.Complete = 1
msg.Header.Id = id
msg.Header.Seq = seq
msg.Data = []byte("1")
return msg
}
Expand All @@ -132,7 +143,6 @@ func CreateXpeerPingMessage(pt PingType, hw string) *P2PMessage {
msg.Header.Length = uint16(len(hw))
msg.Header.Complete = 1
msg.Header.Id = 0
msg.Header.Seq = 0
msg.Data = []byte(hw)
return msg
}
Expand All @@ -146,7 +156,6 @@ func CreateIntroP2PMessage(c Crypto, data string, netProto uint16) *P2PMessage {
msg.Header.Length = uint16(len(data))
msg.Header.Complete = 1
msg.Header.Id = 0
msg.Header.Seq = 0
if c.Active {
var err error
msg.Data, err = c.Encrypt(c.ActiveKey.Key, []byte(data))
Expand All @@ -168,7 +177,6 @@ func CreateIntroRequest(c Crypto, id string) *P2PMessage {
msg.Header.Length = uint16(len(id))
msg.Header.Complete = 1
msg.Header.Id = 0
msg.Header.Seq = 0
if c.Active {
var err error
msg.Data, err = c.Encrypt(c.ActiveKey.Key, []byte(id))
Expand Down Expand Up @@ -212,7 +220,6 @@ func CreateTestP2PMessage(c Crypto, data string, netProto uint16) *P2PMessage {
msg.Header.Length = uint16(len(data))
msg.Header.Complete = 1
msg.Header.Id = 0
msg.Header.Seq = 0
if c.Active {
var err error
msg.Data, err = c.Encrypt(c.ActiveKey.Key, []byte(data))
Expand All @@ -236,7 +243,6 @@ func CreateProxyP2PMessage(id int, data string, netProto uint16) *P2PMessage {
msg.Header.Complete = 1
msg.Header.ProxyId = uint16(id)
msg.Header.Id = 0
msg.Header.Seq = 0
msg.Data = []byte(data)
return msg
}
Expand All @@ -252,7 +258,6 @@ func CreateBadTunnelP2PMessage(id int, netProto uint16) *P2PMessage {
msg.Header.ProxyId = uint16(id)
msg.Header.Complete = 1
msg.Header.Id = 0
msg.Header.Seq = 0
msg.Data = []byte(data)
return msg
}
Expand Down Expand Up @@ -326,24 +331,3 @@ func (uc *PTPNet) SendMessage(msg *P2PMessage, dst_addr *net.UDPAddr) (int, erro
}
return n, nil
}

func Process_p2p_msg(count int, src_addr *net.UDPAddr, err error, rcv_bytes []byte) {
if err != nil {
fmt.Printf("process_p2p_msg error : %v\n", err)
return
}

buf := make([]byte, count)
copy(buf[:], rcv_bytes[:])

msg, des_err := P2PMessageFromBytes(buf)
if des_err != nil {
fmt.Printf("P2PMessageFromBytes err : %v\n", des_err)
return
}

fmt.Printf("processed message from %s, msg_count %d, msg_data : %s\n",
src_addr.String(),
count,
msg.Data)
}
Loading

0 comments on commit e509d3f

Please sign in to comment.