forked from tcaine/twamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
121 lines (103 loc) · 2.9 KB
/
client.go
File metadata and controls
121 lines (103 loc) · 2.9 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package twamp
import (
"bytes"
"errors"
"fmt"
"io"
"net"
"time"
)
/*
Default TCP port for remote TWAMP server.
*/
const TwampControlPort int = 862
/*
Security modes for TWAMP session.
*/
const (
ModeUnspecified = 0
ModeUnauthenticated = 1
ModeAuthenticated = 2
ModeEncypted = 4
)
type TwampClient struct{}
func NewClient() *TwampClient {
return &TwampClient{}
}
func (c *TwampClient) Connect(hostname string) (*TwampConnection, error) {
// connect to remote host
conn, err := net.DialTimeout("tcp", hostname, time.Second*5)
if err != nil {
return nil, err
}
// create a new TwampConnection
twampConnection := NewTwampConnection(conn)
// check for greeting message from TWAMP server
greeting, err := twampConnection.getTwampServerGreetingMessage()
if err != nil {
return nil, err
}
// check greeting mode for errors
switch greeting.Mode {
case ModeUnspecified:
return nil, errors.New("The TWAMP server is not interested in communicating with you.")
case ModeUnauthenticated:
case ModeAuthenticated:
return nil, errors.New("Authentication is not currently supported.")
case ModeEncypted:
return nil, errors.New("Encyption is not currently supported.")
}
// negotiate TWAMP session configuration
twampConnection.sendTwampClientSetupResponse()
// check the start message from TWAMP server
serverStartMessage, err := twampConnection.getTwampServerStartMessage()
if err != nil {
return nil, err
}
err = checkAcceptStatus(int(serverStartMessage.Accept), "connection")
if err != nil {
return nil, err
}
return twampConnection, nil
}
func readFromSocket(reader io.Reader, size int) (bytes.Buffer, error) {
buf := make([]byte, size)
buffer := *bytes.NewBuffer(buf)
bytesRead, err := reader.Read(buf)
if err != nil && bytesRead < size {
return buffer, errors.New(fmt.Sprintf("readFromSocket: expected %d bytes, got %d", size, bytesRead))
}
return buffer, err
}
/*
TWAMP Accept Field Status Code
*/
const (
OK = 0
Failed = 1
InternalError = 2
NotSupported = 3
PermanentResourceLimitation = 4
TemporaryResourceLimitation = 5
)
/*
Convenience function for checking the accept code contained in various TWAMP server
response messages.
*/
func checkAcceptStatus(accept int, context string) error {
switch accept {
case OK:
return nil
case Failed:
return errors.New(fmt.Sprintf("ERROR: The ", context, " failed."))
case InternalError:
return errors.New(fmt.Sprintf("ERROR: The ", context, " failed: internal error."))
case NotSupported:
return errors.New(fmt.Sprintf("ERROR: The ", context, " failed: not supported."))
case PermanentResourceLimitation:
return errors.New(fmt.Sprintf("ERROR: The ", context, " failed: permanent resource limitation."))
case TemporaryResourceLimitation:
return errors.New(fmt.Sprintf("ERROR: The ", context, " failed: temporary resource limitation."))
}
return nil
}