This commit is contained in:
esrrhs committed 2018-12-19 14:38:44 +08:00
1 parent 743b82dda4
commit 2beaecc533
4 files changed
+78 -35

No files matched your search

+59 -17
View File
@@ -21,10 +21,12 @@ const (
// An Echo represents an ICMP echo request or reply message body.
type MyMsg struct {
TYPE uint32
ID string
TARGET string
Data []byte
TYPE uint32
ID string
TARGET string
Data []byte
RPROTO uint16
ENDTYPE uint32
}
// Len implements the Len method of MessageBody interface.
@@ -32,13 +34,17 @@ func (p *MyMsg) Len(proto int) int {
if p == nil {
return 0
}
return 4 + p.LenString(p.ID) + p.LenString(p.TARGET) + len(p.Data)
return 4 + p.LenString(p.ID) + p.LenString(p.TARGET) + p.LenData(p.Data) + 2 + 4
}
func (p *MyMsg) LenString(s string) int {
return 2 + len(s)
}
func (p *MyMsg) LenData(data []byte) int {
return 2 + len(data)
}
// Marshal implements the Marshal method of MessageBody interface.
func (p *MyMsg) Marshal(proto int) ([]byte, error) {
@@ -52,7 +58,12 @@ func (p *MyMsg) Marshal(proto int) ([]byte, error) {
target := p.MarshalString(p.TARGET)
copy(b[4+p.LenString(p.ID):], target)
copy(b[4+p.LenString(p.ID)+p.LenString(p.TARGET):], p.Data)
data := p.MarshalData(p.Data)
copy(b[4+p.LenString(p.ID)+p.LenString(p.TARGET):], data)
binary.BigEndian.PutUint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data):], uint16(p.RPROTO))
binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+2:], uint32(p.ENDTYPE))
return b, nil
}
@@ -64,6 +75,13 @@ func (p *MyMsg) MarshalString(s string) []byte {
return b
}
func (p *MyMsg) MarshalData(data []byte) []byte {
b := make([]byte, p.LenData(data))
binary.BigEndian.PutUint16(b[:2], uint16(len(data)))
copy(b[2:], []byte(data))
return b
}
// Marshal implements the Marshal method of MessageBody interface.
func (p *MyMsg) Unmarshal(b []byte) error {
defer func() {
@@ -76,30 +94,48 @@ func (p *MyMsg) Unmarshal(b []byte) error {
p.TARGET = p.UnmarshalString(b[4+p.LenString(p.ID):])
p.Data = make([]byte, len(b[4+p.LenString(p.ID)+p.LenString(p.TARGET):]))
copy(p.Data, b[4+p.LenString(p.ID)+p.LenString(p.TARGET):])
p.Data = p.UnmarshalData(b[4+p.LenString(p.ID)+p.LenString(p.TARGET):])
p.RPROTO = binary.BigEndian.Uint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data):])
p.ENDTYPE = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+2:])
return nil
}
func (p *MyMsg) UnmarshalString(b []byte) string {
len := binary.BigEndian.Uint16(b[:2])
if len > 32 || len < 0 {
panic(nil)
}
data := make([]byte, len)
copy(data, b[2:])
return string(data)
}
func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte, proto int) {
func (p *MyMsg) UnmarshalData(b []byte) []byte {
len := binary.BigEndian.Uint16(b[:2])
if len > 2048 || len < 0 {
panic(nil)
}
data := make([]byte, len)
copy(data, b[2:])
return data
}
func sendICMP(conn icmp.PacketConn, server *net.IPAddr, target string, connId string, msgType uint32, data []byte, sproto int, rproto int) {
m := &MyMsg{
ID: connId,
TYPE: msgType,
TARGET: target,
Data: data,
ID: connId,
TYPE: msgType,
TARGET: target,
Data: data,
RPROTO: (uint16)(rproto),
ENDTYPE: msgType,
}
msg := &icmp.Message{
Type: (ipv4.ICMPType)(proto),
Type: (ipv4.ICMPType)(sproto),
Code: 0,
Body: m,
}
@@ -148,12 +184,17 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) {
}
my.Unmarshal(bytes[4:n])
if my.TYPE != (uint32)(DATA) {
fmt.Printf("processPacket diff type %d \n", my.TYPE)
if my.TYPE != (uint32)(DATA) || my.ENDTYPE != (uint32)(DATA) {
fmt.Printf("processPacket diff type %s %d %d \n", my.ID, my.TYPE, my.ENDTYPE)
continue
}
recv <- &Packet{data: my.Data, id: my.ID, target: my.TARGET, src: srcaddr.(*net.IPAddr)}
if my.Data == nil {
fmt.Printf("processPacket data nil %s\n", my.ID)
return
}
recv <- &Packet{data: my.Data, id: my.ID, target: my.TARGET, src: srcaddr.(*net.IPAddr), rproto: (int)(my.RPROTO)}
}
}
@@ -162,6 +203,7 @@ type Packet struct {
id string
target string
src *net.IPAddr
rproto int
}
func UniqueId() string {