-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
246 lines (215 loc) · 5.65 KB
/
message.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package sip
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
)
// ---------------
type HeaderLine struct {
Name string
Value string
}
// ---------------
type Headers struct {
Lines []HeaderLine
}
func (h *Headers) AddHeader(name string, value string) {
h.Lines = append(h.Lines,
HeaderLine{
name,
value,
})
}
func (h *Headers) ReplaceAddHeader(name string, value string) {
found := false
for _, crt := range h.Lines {
if crt.Name == name {
crt.Value = value
found = true
}
}
if !found {
h.AddHeader(name, value)
}
}
func (h *Headers) FindHeaderByName(name string) (header HeaderLine, err error) {
for _, crt := range h.Lines {
if crt.Name == name {
return crt, nil
}
}
return HeaderLine{}, errors.New("Not found")
}
// ---------------
type Headline interface {
ToString() string
}
// ---------------
type MessageType int
const (
REQUEST MessageType = iota
RESPONSE
)
type Message struct {
Headline Headline
Headers Headers
Body []byte
RequestID string
MessageType MessageType
}
func (m *Message) SetRequestId(requestID string) {
m.RequestID = requestID
}
func (m *Message) GetFrom() string {
header, err := m.Headers.FindHeaderByName("From")
if err != nil {
log.Println("Error finding header From", err)
}
return header.Value
}
func (m *Message) SetFrom(proto string, user string, host string, tag string) *Message {
var value string
if tag == "" {
value = fmt.Sprintf("<%s:%s@%s>", proto, user, host)
} else {
value = fmt.Sprintf("<%s:%s@%s>;tag=%s", proto, user, host, tag)
}
m.Headers.ReplaceAddHeader("From", value)
return m
}
func (m *Message) SetFromValue(value string) *Message {
m.Headers.ReplaceAddHeader("From", value)
return m
}
func (m *Message) SetContact(proto string, user string, host string, port int) *Message {
var value string
value = fmt.Sprintf("<%s:%s@%s:%d>;transport=tcp", proto, user, host, port)
m.Headers.ReplaceAddHeader("Contact", value)
return m
}
func (m *Message) SetContactValue(value string) *Message {
m.Headers.ReplaceAddHeader("Contact", value)
return m
}
func (m *Message) SetUserAgent(value string) *Message {
m.Headers.ReplaceAddHeader("User-Agent", value)
return m
}
func (m *Message) GetTo() string {
header, err := m.Headers.FindHeaderByName("To")
if err != nil {
log.Println("Error finding header To", err)
}
return header.Value
}
func (m *Message) SetTo(proto string, user string, host string, tag string) *Message {
var value string
if tag == "" {
value = fmt.Sprintf("<%s:%s@%s>", proto, user, host)
} else {
value = fmt.Sprintf("<%s:%s@%s>;tag=%s", proto, user, host, tag)
}
m.Headers.ReplaceAddHeader("To", value)
return m
}
func (m *Message) SetToValue(value string) *Message {
m.Headers.ReplaceAddHeader("To", value)
return m
}
func (m *Message) GetVia() string {
header, err := m.Headers.FindHeaderByName("Via")
if err != nil {
log.Println("Error finding header Via", err)
}
return header.Value
}
func (m *Message) SetVia(transport string, host string, port int, branch string) *Message {
value := fmt.Sprintf("SIP/2.0/%s %s:%d;rport;branch=z9hG4bK%s", transport, host, port, branch)
m.Headers.ReplaceAddHeader("Via", value)
return m
}
func (m *Message) SetViaValue(value string) *Message {
m.Headers.ReplaceAddHeader("Via", value)
return m
}
func (m *Message) GetCallId() string {
header, err := m.Headers.FindHeaderByName("Call-ID")
if err != nil {
log.Println("Error finding header Call-ID", err)
}
return header.Value
}
func (m *Message) SetCallId(value string) *Message {
m.Headers.ReplaceAddHeader("Call-ID", value)
return m
}
func (m *Message) SetContentLength(length int) *Message {
m.Headers.AddHeader("Content-Length", strconv.Itoa(length))
return m
}
func (m *Message) GetCSeq() (number uint32, verb string) {
header, err := m.Headers.FindHeaderByName("CSeq")
if err != nil {
log.Println("Error finding header CSeq", err)
}
headerLine := strings.Split(header.Value, " ")
numberString := headerLine[0]
number64, parseErr := strconv.ParseUint(numberString, 10, 0)
number = uint32(number64)
verb = headerLine[1]
if parseErr != nil {
log.Println("Cannot parse numberString to int" + numberString)
}
return number, verb
}
func (m *Message) SetCSeq(number uint32, verb string) *Message {
m.Headers.AddHeader("CSeq", strconv.FormatUint(uint64(number), 10)+" "+verb)
return m
}
func (m *Message) GetExpires() int {
header, err := m.Headers.FindHeaderByName("Expires")
if err != nil {
log.Println("Field 'Expires' could not be found. Assume 120")
return 120
}
expirationValue, err := strconv.Atoi(header.Value)
if err != nil {
log.Println("Field 'Expires' could not be parsed. Assume 120")
return 120
}
return expirationValue
}
func (m *Message) SetExpires(value int) *Message {
m.Headers.AddHeader("Expires", strconv.Itoa(300))
return m
}
func (m *Message) SetDigestAuthorizationHeader(authInfo AuthInformation) *Message {
value := fmt.Sprintf(`Digest username="%s" realm="%s" nonce="%s" response="%s"`, authInfo.Username, authInfo.Wwwauth.Realm, authInfo.Wwwauth.Nonce, authInfo.FinalHash())
m.Headers.AddHeader("Authorization", value)
log.Println(value)
return m
}
func (m *Message) SetAllow(actions []string) *Message {
m.Headers.AddHeader("Allow", strings.Join(actions, ", "))
return m
}
func (m *Message) AddHeader(name string, value string) *Message {
m.Headers.AddHeader(name, value)
return m
}
func (m *Message) GetType() MessageType {
return m.MessageType
}
func (m *Message) String() string {
s := ""
s += m.Headline.ToString() + "\n"
for _, crtHeader := range m.Headers.Lines {
crtLine := crtHeader.Name + ": " + crtHeader.Value + "\n"
s += crtLine
}
s += "\n"
s += string(m.Body)
return s
}