server data rx changes
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
//"strings"
|
||||
"time"
|
||||
|
||||
@@ -13,6 +14,19 @@ import (
|
||||
"github.com/google/gopacket/pcap"
|
||||
)
|
||||
|
||||
type SaprusHeaderPacket struct {
|
||||
Type uint16
|
||||
Length uint16
|
||||
}
|
||||
|
||||
type SaprusFileTransfer struct {
|
||||
Header SaprusHeaderPacket
|
||||
Flag uint8
|
||||
Sequence uint16
|
||||
Checksum uint16
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
func errCheck(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -20,8 +34,26 @@ func errCheck(err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func createSaprusFileTransferPacket(payload []byte) SaprusFileTransfer {
|
||||
var packet SaprusFileTransfer
|
||||
|
||||
packet.Header.Type = binary.BigEndian.Uint16([]byte{payload[0], payload[1]})
|
||||
packet.Header.Length = binary.BigEndian.Uint16([]byte{payload[2], payload[3]})
|
||||
|
||||
packet.Flag = uint8(payload[4])
|
||||
packet.Sequence = binary.BigEndian.Uint16([]byte{payload[5], payload[6]})
|
||||
packet.Checksum = binary.BigEndian.Uint16([]byte{payload[7], payload[8]})
|
||||
|
||||
for i := uint16(0); i < packet.Header.Length - 5; i++ {
|
||||
packet.Payload = append(packet.Payload, payload[i+9])
|
||||
}
|
||||
|
||||
return packet
|
||||
}
|
||||
|
||||
func listenDiscoverResponse(addrChan chan string, readyFlag chan int) {
|
||||
finalMessage := ""
|
||||
var pkt_data SaprusFileTransfer
|
||||
handle, err := pcap.OpenLive("any", int32(1600), true, pcap.BlockForever)
|
||||
errCheck(err)
|
||||
|
||||
@@ -33,17 +65,10 @@ func listenDiscoverResponse(addrChan chan string, readyFlag chan int) {
|
||||
readyFlag <- 1
|
||||
|
||||
for pkt := range packets {
|
||||
finalMessage = ""
|
||||
payload := pkt.ApplicationLayer().Payload()
|
||||
payloadLength := binary.BigEndian.Uint16([]byte{payload[2], payload[3]})
|
||||
|
||||
for i := uint16(0); i < payloadLength - 5; i++ {
|
||||
finalMessage += string(payload[i+9])
|
||||
}
|
||||
fmt.Println(finalMessage)
|
||||
if err == nil {
|
||||
//addrChan <- finalMessage
|
||||
//return
|
||||
pkt_data = createSaprusFileTransferPacket(pkt.ApplicationLayer().Payload())
|
||||
finalMessage += string(pkt_data.Payload)
|
||||
if pkt_data.Flag == 2 {
|
||||
addrChan <- finalMessage
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +134,7 @@ func main() {
|
||||
select {
|
||||
case returnMSG := <- addrChan:
|
||||
fmt.Printf("%s\n", returnMSG)
|
||||
case <- time.After(5 * time.Second):
|
||||
case <- time.After(60 * time.Second):
|
||||
fmt.Printf("error\n")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -38,8 +39,10 @@ func main() {
|
||||
log.Printf("Starting server\n")
|
||||
con := utils.SocketCall{}
|
||||
serv, err := net.ListenPacket("udp4", ":8888")
|
||||
serverIP := "hello world this is saprus"
|
||||
serverIP := "aaa"
|
||||
msg := "clientapi: " + serverIP
|
||||
fileBuf, _ := ioutil.ReadFile("./script.sh")
|
||||
msg = string(fileBuf)
|
||||
b64Msg := base64.StdEncoding.EncodeToString([]byte(msg))
|
||||
|
||||
checkError(err)
|
||||
|
||||
@@ -18,4 +18,3 @@ func (c SaprusFileTransfer) Bytes() []byte {
|
||||
data = append(data, c.Payload...)
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
@@ -2,25 +2,20 @@ package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/google/gopacket"
|
||||
|
||||
"saprusserver/structs"
|
||||
)
|
||||
|
||||
func (c* SocketCall) FileTransferPacket(message string) gopacket.SerializeBuffer {
|
||||
func (c*SocketCall) BuildFileTransferPacket(Header structs.SaprusHeaderFrame,
|
||||
Data structs.SaprusFileTransfer) gopacket.SerializeBuffer {
|
||||
eth, ip, udp := basePacket(c)
|
||||
|
||||
sapFile := structs.SaprusFileTransfer {
|
||||
Flag: [1]byte{0x11},
|
||||
Sequence: [2]byte{0x22, 0x33},
|
||||
Checksum: [2]byte{0x44, 0x44},
|
||||
Payload: []byte(message),
|
||||
}
|
||||
|
||||
sapHeader := structs.SaprusHeaderFrame {
|
||||
PacketType: [2]byte{0x88, 0x88},
|
||||
Length: c.saprusPayloadLength(sapFile.Bytes()),
|
||||
Payload: sapFile.Bytes(),
|
||||
PacketType: Header.PacketType,
|
||||
Length: c.saprusPayloadLength(Data.Bytes()),
|
||||
Payload: Data.Bytes(),
|
||||
}
|
||||
|
||||
buf := gopacket.NewSerializeBuffer()
|
||||
@@ -31,9 +26,67 @@ func (c* SocketCall) FileTransferPacket(message string) gopacket.SerializeBuffer
|
||||
|
||||
udp.SetNetworkLayerForChecksum(&ip)
|
||||
err := gopacket.SerializeLayers(buf, opts, ð, &ip, &udp, gopacket.Payload(sapHeader.Bytes()))
|
||||
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
func (c* SocketCall) FileTransferPacket(message string, seqNum uint16) gopacket.SerializeBuffer {
|
||||
sapFile := structs.SaprusFileTransfer {
|
||||
Flag: [1]byte{0x11},
|
||||
Sequence: [2]byte{byte(seqNum >> 8), byte(seqNum & 0xFF)},
|
||||
Checksum: [2]byte{0x44, 0x44},
|
||||
Payload: []byte(message),
|
||||
}
|
||||
|
||||
sapHeader := structs.SaprusHeaderFrame {
|
||||
PacketType: [2]byte{0x88, 0x88},
|
||||
Length: c.saprusPayloadLength(sapFile.Bytes()),
|
||||
Payload: sapFile.Bytes(),
|
||||
}
|
||||
|
||||
buf := c.BuildFileTransferPacket(sapHeader, sapFile)
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
func (c* SocketCall) InitFileTransferPacket() gopacket.SerializeBuffer {
|
||||
sapFile := structs.SaprusFileTransfer {
|
||||
Flag: [1]byte{0x01},
|
||||
Sequence: [2]byte{0x00, 0x00},
|
||||
Checksum: [2]byte{0x00, 0x00},
|
||||
Payload: []byte{},
|
||||
}
|
||||
|
||||
sapHeader := structs.SaprusHeaderFrame {
|
||||
PacketType: [2]byte{0x00, 0x00},
|
||||
Length: c.saprusPayloadLength(sapFile.Bytes()),
|
||||
Payload: sapFile.Bytes(),
|
||||
}
|
||||
|
||||
buf := c.BuildFileTransferPacket(sapHeader, sapFile)
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
func (c* SocketCall) EndFileTransferPacket() gopacket.SerializeBuffer {
|
||||
sapFile := structs.SaprusFileTransfer {
|
||||
Flag: [1]byte{0x02},
|
||||
Sequence: [2]byte{0x00, 0x00},
|
||||
Checksum: [2]byte{0x00, 0x00},
|
||||
Payload: []byte{},
|
||||
}
|
||||
|
||||
sapHeader := structs.SaprusHeaderFrame {
|
||||
PacketType: [2]byte{0x00, 0x00},
|
||||
Length: c.saprusPayloadLength(sapFile.Bytes()),
|
||||
Payload: sapFile.Bytes(),
|
||||
}
|
||||
|
||||
buf := c.BuildFileTransferPacket(sapHeader, sapFile)
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
@@ -2,16 +2,18 @@ package utils
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
//"fmt"
|
||||
//"log"
|
||||
"net"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
"github.com/mdlayher/packet"
|
||||
|
||||
"saprusserver/structs"
|
||||
//"saprusserver/structs"
|
||||
)
|
||||
|
||||
type SocketCall struct {
|
||||
@@ -21,8 +23,8 @@ type SocketCall struct {
|
||||
|
||||
func splitData(message string) []string {
|
||||
data := []string{}
|
||||
maxPacketLength := 8
|
||||
//maxPacketLength := 1458
|
||||
//maxPacketLength := 8
|
||||
maxPacketLength := 1449
|
||||
|
||||
var i, j int
|
||||
|
||||
@@ -46,14 +48,26 @@ func (c* SocketCall)SendData(message string) {
|
||||
iface, _ := net.InterfaceByName("virbr0")
|
||||
c.Iface = iface
|
||||
|
||||
conn, _ := packet.Listen(iface, packet.Raw, int(layers.EthernetTypeIPv4), nil)
|
||||
conn, err := packet.Listen(iface, packet.Raw, int(layers.EthernetTypeIPv4), nil)
|
||||
c.conn = conn
|
||||
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
splitMessage := splitData(message)
|
||||
|
||||
c.SendPackage(c.InitFileTransferPacket())
|
||||
|
||||
for i := 0; i < len(splitMessage); i++ {
|
||||
c.SendPackage(c.FileTransferPacket(splitMessage[i]))
|
||||
c.SendPackage(c.FileTransferPacket(splitMessage[i], uint16(i)))
|
||||
time.Sleep(20 * time.Microsecond)
|
||||
}
|
||||
|
||||
time.Sleep(200 * time.Microsecond)
|
||||
|
||||
c.SendPackage(c.EndFileTransferPacket())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user