Reputation: 251
I am trying to teach myself Go. I have written a simple client / server app that has some encryption and very simple packet structure.
I have a go routine for listening and then sending data to each client connected. In my function that sends data to each client i'm appending a message to a header but its doing some strange behavior.
func ClientSender(client *Client) {
for {
input := <-client.Outgoing //TODO add cleanup quit bool
for _, clientz := range RoomList[client.Room].Clients { //TODO rename to client connections = ClientList
temp := input
dbgMsg.Printf("RAW SENDER: % x", input)
dbgMsg.Printf("INPUT END1: % x", input)
dbgMsg.Printf("AES KEY % x\n", clientz.AES_Key)
dbgMsg.Printf("INPUT END2: % x", input)
dbgMsg.Printf("pre ecnryp: % x\n", input[30:])
dbgMsg.Printf("INPUT END3: % x", input)
encPayload := input[30:]
dbgMsg.Printf("INPUT END4: % x", input)
header := input[:30]
dbgMsg.Printf("INPUT END5: % x", input)
e,_ := barrenoid.Encrypt(clientz.AES_Key, encPayload)
dbgMsg.Printf("INPUT END6: % x", input)
dbgMsg.Printf("header: % x\n", input[:30])
dbgMsg.Printf("payload: % x\n", input[30:])
dbgMsg.Printf("encrypt: % x\n", e)
dbgMsg.Printf("TEMP: % x\n", temp)
asdf := append(header, e...)
dbgMsg.Printf("SENDING: % x", asdf)
//_, err := clientz.Conn.Write(payload)
//chkError(err)
input = temp
dbgMsg.Printf("INPUT END7: % x", input)
}
}
}
The value of "input" get changed and i cant figure out why. here is output from the above code:
INFO: 2016/02/22 10:47:38 RAW SENDER: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END1: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 AES KEY 06 89 c9 d7 ad ec 4a d0 33 bf fa ab 6e 05 cd 51 87 8b f0 ad 60 a8 36 47 ca 8f 7a f8 b8 6f 1c ce
INFO: 2016/02/22 10:47:38 INPUT END2: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 pre ecnryp: 0a
INFO: 2016/02/22 10:47:38 INPUT END3: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END4: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END5: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 INPUT END6: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 header: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00
INFO: 2016/02/22 10:47:38 payload: 0a
INFO: 2016/02/22 10:47:38 encrypt: ***c8*** 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO: 2016/02/22 10:47:38 TEMP: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 0a
INFO: 2016/02/22 10:47:38 SENDING: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 c8 7e ff f9 f5 c3 ce 1e 1d 44 91 b7 fb 09 5d e0 7e
INFO: 2016/02/22 10:47:38 INPUT END7: 0d ae 00 00 00 00 56 cb 57 ca 41 6e 6f 6e 79 6d 6f 75 73 00 00 00 00 00 00 00 00 00 00 00 ***c8***
I cant figure out why the line containing "INPUT END7" is not equal the the "input" value.
The last byte is ALWAYS equal to the first byte in "encrypted"output...
Here is code that sends slice to channel:
func ClientReader(client *Client) {
//Main Read loop
for {
bytesRead, buffer := client.Read()
if bytesRead < HEADERSIZE {
//client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(""), []byte("Minimum header not recieved."))
client.Close()
break // Connection to host is broken
}
//dbgMsg.Printf("RAW RECIEVED % x", buffer)
cmdBit, encryptionByte, ts, payload := protocolParser(buffer)
dbgMsg.Printf("CMDBIT: % x, ENCBIT: % x, TS: % d, PAYLOAD: % x", cmdBit, encryptionByte, ts, payload)
if encryptionByte == 0xAE {
payload, _ = barrenoid.Decrypt(client.AES_Key, payload)
dbgMsg.Printf("Decrypted payload % x\n", payload)
} else if encryptionByte == 0x00 {
// no need to decrypt
} else {
//bad packet reject
}
if cmdBit == 0x0D{
//payload, _ = barrenoid.Encrypt(client.AES_Key, payload)
client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), payload)
} else if cmdBit == 0x1C {
client.Name = string(payload)
} else {
//bad packet reject
//client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte("Unknown command bit."))
}
}
Upvotes: 0
Views: 292
Reputation: 120960
The slices temp
and input
share the same backing array. Modifications through one slice are visible through the other. The line containing "INPUT END7" is not the same as the line with "INPUT END1" because the backing array of the slices is modified on this line:
asdf := append(header, e...)
You can copy the backing array using this line of code:
temp := append([]byte(nil), input...)
Upvotes: 2