aboutsummaryrefslogtreecommitdiff
path: root/spdu
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-09-29 10:09:58 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-09-29 10:09:58 +0200
commite867d90e8e78a3b3e92debe2583e5583a2d8769a (patch)
tree8649491de232baa28916f5baeceac2e2a5ecb37f /spdu
parent7f505b9a170f76e6e5b985646c545e50c5884558 (diff)
Rename tde into spdu
Diffstat (limited to 'spdu')
-rw-r--r--spdu/spdu.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/spdu/spdu.go b/spdu/spdu.go
new file mode 100644
index 0000000..688d5b4
--- /dev/null
+++ b/spdu/spdu.go
@@ -0,0 +1,50 @@
+// SPDU (Session Protocol Data Unit)
+package spdu
+
+import (
+ "encoding/binary"
+ "errors"
+ "io"
+ "net"
+)
+
+// |<-- 2 octets -->|<-- maximum 240 octets -->|
+// | header | acse/rose pdu |
+// | big endian | |
+
+const maxLen = 240 // max PDU size
+
+type Conn struct{ net.Conn }
+
+func (c Conn) Write(p []byte) (n int, err error) {
+ size := uint16(len(p))
+ if size > maxLen {
+ return 0, errors.New("PDU too big")
+ }
+ if err := binary.Write(c.Conn, binary.BigEndian, size); err != nil {
+ return 0, err
+ }
+ return c.Conn.Write(p)
+}
+
+func (c Conn) Read(p []byte) (n int, err error) {
+ var size uint16
+ if err := binary.Read(c.Conn, binary.BigEndian, &size); err != nil {
+ return 0, err
+ }
+ return c.Conn.Read(p[:size])
+}
+
+func ReadAll(r io.Reader) ([]byte, error) {
+ p := make([]byte, maxLen)
+ n, err := r.Read(p)
+ return p[:n], err
+}
+
+func Dial(service string) (Conn, error) {
+ conn, err := net.Dial("tcp", service)
+ if err != nil {
+ return Conn{}, err
+ }
+ return Conn{conn}, nil
+}