-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclient.go
116 lines (97 loc) · 2.72 KB
/
client.go
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
package twamp
import (
"bytes"
"errors"
"fmt"
"net"
"time"
)
/*
Security modes for TWAMP session.
*/
const (
ModeUnspecified = 0
ModeUnauthenticated = 1
)
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
}
if greeting.Mode == ModeUnspecified {
return nil, fmt.Errorf("The TWAMP server is not interested in communicating with you.")
}
if greeting.Mode&ModeUnauthenticated == 0 {
return nil, errors.New("only unauthenticated mode is 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(conn net.Conn, size int, timeoutSeconds int) (bytes.Buffer, error) {
timeout := time.Now().Add(time.Duration(timeoutSeconds) * time.Second)
buf := make([]byte, size)
buffer := *bytes.NewBuffer(buf)
conn.SetReadDeadline(timeout)
bytesRead, err := conn.Read(buf)
if err != nil {
return buffer, err
}
if bytesRead < size {
return buffer, fmt.Errorf(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 fmt.Errorf("ERROR: The %s failed.", context)
case InternalError:
return fmt.Errorf("ERROR: The %s failed: internal error.", context)
case NotSupported:
return fmt.Errorf("ERROR: The %s failed: not supported.", context)
case PermanentResourceLimitation:
return fmt.Errorf("ERROR: The %s failed: permanent resource limitation.", context)
case TemporaryResourceLimitation:
return fmt.Errorf("ERROR: The %s failed: temporary resource limitation.", context)
}
return nil
}