-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecvUtil.go
More file actions
96 lines (89 loc) · 2.06 KB
/
recvUtil.go
File metadata and controls
96 lines (89 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
Copyright 2014 JARST, LLC
This file is part of Quibit.
Quibit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
LICENSE file for details.
**/
package quibit
import (
"bytes"
"crypto/sha512"
"encoding/binary"
"errors"
"net"
"time"
"io"
)
const (
MAGIC = 6667787
)
func recvAll(conn net.Conn, log chan string) (Frame, error) {
// ret val
var h Header
var t time.Time
var frame Frame
// a buffer for decoing
var headerBuffer bytes.Buffer
for {
headerSize := int(binary.Size(h))
// Byte slice for moving to buffer
buffer := make([]byte, headerSize)
if conn == nil {
return frame, errors.New("Nil connection")
}
conn.SetReadDeadline(t)
n, err := io.ReadFull(conn, buffer)
if err != nil {
log <- err.Error()
return frame, err
}
if n > 0 {
// Add to header buffer
headerBuffer.Write(buffer)
// Check to see if we have the whole header
if len(headerBuffer.Bytes()) != headerSize {
return frame, errors.New("Incorrect header size...")
}
h.FromBytes(headerBuffer.Bytes())
if h.Magic != MAGIC {
return frame, errors.New("Incorrect Magic Number!")
}
frame.Header = h
break
}
}
payload := make([]byte, h.Length)
var payloadBuffer bytes.Buffer
if h.Length < 1 {
frame.Payload = nil
frame.Header = h
return frame, nil
}
for {
// store in byte array
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
n, err := io.ReadFull(conn, payload)
if err != nil {
return frame, err
}
if n > 1 {
// write to buffer
payloadBuffer.Write(payload)
// Check to see if we have whole payload
if len(payloadBuffer.Bytes()) == int(h.Length) {
// Verify checksum
frame.Payload = payloadBuffer.Bytes()
frame.Header = h
if h.Checksum != sha512.Sum384(payloadBuffer.Bytes()) {
return frame, errors.New("Incorrect Checksum")
}
return frame, nil
}
}
}
//Should never end here
panic("RECV PAYLOAD")
return frame, nil
}