forked from jart/gosip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg.go
executable file
·474 lines (410 loc) · 11 KB
/
msg.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright 2020 Justine Alexandra Roberts Tunney
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// SIP Message Library
package sip
import (
"bytes"
"net"
"strconv"
)
// Msg represents a SIP message. This can either be a request or a response.
// These fields are never nil unless otherwise specified.
type Msg struct {
// Fields that aren't headers.
VersionMajor uint8
VersionMinor uint8
Method string // Indicates type of request (if request)
Request *URI // dest URI (nil if response)
Status int // Indicates happiness of response (if response)
Phrase string // Explains happiness of response (if response)
Payload Payload // Stuff that comes after two line breaks
// Special non-SIP fields.
SourceAddr *net.UDPAddr // Set by transport layer as received address.
// Important headers should be further up in the struct.
From *Addr // Logical sender of message
To *Addr // Logical destination of message
Via *Via // Linked list of agents traversed (must have one)
Route *Addr // Used for goose routing and loose routing
RecordRoute *Addr // Used for loose routing
Contact *Addr // Where we send response packets or nil
CallID string // Identifies call from invite to bye
CSeq int // Counter for network packet ordering
CSeqMethod string // Helps with matching to orig message
MaxForwards int // 0 has context specific meaning
UserAgent string
// All the other RFC 3261 headers in plus some extras.
Accept string
AcceptContact string
AcceptEncoding string
AcceptLanguage string
AlertInfo string
Allow string
AllowEvents string
AuthenticationInfo string
Authorization string
CallInfo string
ContentDisposition string
ContentEncoding string
ContentLanguage string
Date string
ErrorInfo string
Event string
Expires int // Seconds registration should expire.
InReplyTo string
MIMEVersion string
MinExpires int // Registrars need this when responding
Organization string
PAssertedIdentity *Addr // P-Asserted-Identity or nil (used for PSTN ANI)
Priority string
ProxyAuthenticate string
ProxyAuthorization string
ProxyRequire string
ReferTo string
ReferredBy string
RemotePartyID *Addr // Evil twin of P-Asserted-Identity.
ReplyTo string
Require string
RetryAfter string
Server string
Subject string
Supported string
Timestamp string
Unsupported string
WWWAuthenticate string
Warning string
// Extension headers.
XHeader *XHeader
}
//go:generate ragel -Z -G2 -o msg_parse.go msg_parse.rl
func (msg *Msg) IsResponse() bool {
return msg.Status > 0
}
func (msg *Msg) String() string {
var b bytes.Buffer
msg.Append(&b)
return b.String()
}
func (msg *Msg) Copy() *Msg {
if msg == nil {
return nil
}
res := new(Msg)
*res = *msg
res.Request = msg.Request.Copy()
res.To = msg.To.Copy()
res.From = msg.From.Copy()
res.Via = msg.Via.Copy()
res.PAssertedIdentity = msg.PAssertedIdentity.Copy()
res.RemotePartyID = msg.RemotePartyID.Copy()
res.Route = msg.Route.Copy()
res.Contact = msg.Contact.Copy()
res.RecordRoute = msg.RecordRoute.Copy()
res.XHeader = msg.XHeader
return res
}
// I turn a SIP message back into a packet.
func (msg *Msg) Append(b *bytes.Buffer) {
if msg == nil {
return
}
if !msg.IsResponse() {
if msg.Method == "" {
b.WriteString("INVITE ")
} else {
b.WriteString(msg.Method)
b.WriteString(" ")
}
if msg.Request == nil {
// In case of bugs, keep calm and DDOS NASA.
b.WriteString("sip:www.nasa.gov:80")
} else {
msg.Request.Append(b)
}
b.WriteString(" ")
msg.appendVersion(b)
b.WriteString("\r\n")
} else {
if msg.Phrase == "" {
msg.Phrase = Phrase(msg.Status)
}
msg.appendVersion(b)
b.WriteString(" ")
b.WriteString(strconv.Itoa(msg.Status))
b.WriteString(" ")
b.WriteString(msg.Phrase)
b.WriteString("\r\n")
}
b.WriteString("From: ")
msg.From.Append(b)
b.WriteString("\r\n")
b.WriteString("To: ")
msg.To.Append(b)
b.WriteString("\r\n")
for viap := msg.Via; viap != nil; viap = viap.Next {
b.WriteString("Via: ")
viap.Append(b)
b.WriteString("\r\n")
}
if msg.Route != nil {
b.WriteString("Route: ")
msg.Route.Append(b)
b.WriteString("\r\n")
}
if msg.RecordRoute != nil {
b.WriteString("Record-Route: ")
msg.RecordRoute.Append(b)
b.WriteString("\r\n")
}
if msg.Contact != nil {
b.WriteString("Contact: ")
msg.Contact.Append(b)
b.WriteString("\r\n")
}
b.WriteString("Call-ID: ")
b.WriteString(msg.CallID)
b.WriteString("\r\n")
b.WriteString("CSeq: ")
b.WriteString(strconv.Itoa(msg.CSeq))
b.WriteString(" ")
b.WriteString(msg.CSeqMethod)
b.WriteString("\r\n")
if msg.UserAgent != "" {
b.WriteString("User-Agent: ")
b.WriteString(msg.UserAgent)
b.WriteString("\r\n")
}
if !msg.IsResponse() {
if msg.MaxForwards == 0 {
b.WriteString("Max-Forwards: 70\r\n")
} else {
b.WriteString("Max-Forwards: ")
b.WriteString(strconv.Itoa(msg.MaxForwards))
b.WriteString("\r\n")
}
}
if msg.Accept != "" {
b.WriteString("Accept: ")
b.WriteString(msg.Accept)
b.WriteString("\r\n")
}
if msg.AcceptEncoding != "" {
b.WriteString("Accept-Encoding: ")
b.WriteString(msg.AcceptEncoding)
b.WriteString("\r\n")
}
if msg.AcceptLanguage != "" {
b.WriteString("Accept-Language: ")
b.WriteString(msg.AcceptLanguage)
b.WriteString("\r\n")
}
if msg.AlertInfo != "" {
b.WriteString("Alert-Info: ")
b.WriteString(msg.AlertInfo)
b.WriteString("\r\n")
}
if msg.Allow != "" {
b.WriteString("Allow: ")
b.WriteString(msg.Allow)
b.WriteString("\r\n")
}
if msg.AllowEvents != "" {
b.WriteString("Allow-Events: ")
b.WriteString(msg.AllowEvents)
b.WriteString("\r\n")
}
if msg.AuthenticationInfo != "" {
b.WriteString("Authentication-Info: ")
b.WriteString(msg.AuthenticationInfo)
b.WriteString("\r\n")
}
if msg.Authorization != "" {
b.WriteString("Authorization: ")
b.WriteString(msg.Authorization)
b.WriteString("\r\n")
}
if msg.CallInfo != "" {
b.WriteString("Call-Info: ")
b.WriteString(msg.CallInfo)
b.WriteString("\r\n")
}
if msg.ContentDisposition != "" {
b.WriteString("Content-Disposition: ")
b.WriteString(msg.ContentDisposition)
b.WriteString("\r\n")
}
if msg.ContentEncoding != "" {
b.WriteString("Content-Encoding: ")
b.WriteString(msg.ContentEncoding)
b.WriteString("\r\n")
}
if msg.ContentLanguage != "" {
b.WriteString("Content-Language: ")
b.WriteString(msg.ContentLanguage)
b.WriteString("\r\n")
}
if msg.Date != "" {
b.WriteString("Date: ")
b.WriteString(msg.Date)
b.WriteString("\r\n")
}
if msg.ErrorInfo != "" {
b.WriteString("Error-Info: ")
b.WriteString(msg.ErrorInfo)
b.WriteString("\r\n")
}
if msg.Event != "" {
b.WriteString("Event: ")
b.WriteString(msg.Event)
b.WriteString("\r\n")
}
// Expires is allowed to be 0 for for REGISTER stuff.
if msg.Expires > 0 || msg.Method == "REGISTER" || msg.CSeqMethod == "REGISTER" {
b.WriteString("Expires: ")
b.WriteString(strconv.Itoa(msg.Expires))
b.WriteString("\r\n")
}
if msg.InReplyTo != "" {
b.WriteString("In-Reply-To: ")
b.WriteString(msg.InReplyTo)
b.WriteString("\r\n")
}
if msg.MIMEVersion != "" {
b.WriteString("MIME-Version: ")
b.WriteString(msg.MIMEVersion)
b.WriteString("\r\n")
}
if msg.MinExpires > 0 {
b.WriteString("Min-Expires: ")
b.WriteString(strconv.Itoa(msg.MinExpires))
b.WriteString("\r\n")
}
if msg.Organization != "" {
b.WriteString("Organization: ")
b.WriteString(msg.Organization)
b.WriteString("\r\n")
}
if msg.PAssertedIdentity != nil {
b.WriteString("P-Asserted-Identity: ")
msg.PAssertedIdentity.Append(b)
b.WriteString("\r\n")
}
if msg.Priority != "" {
b.WriteString("Priority: ")
b.WriteString(msg.Priority)
b.WriteString("\r\n")
}
if msg.ProxyAuthenticate != "" {
b.WriteString("Proxy-Authenticate: ")
b.WriteString(msg.ProxyAuthenticate)
b.WriteString("\r\n")
}
if msg.ProxyAuthorization != "" {
b.WriteString("Proxy-Authorization: ")
b.WriteString(msg.ProxyAuthorization)
b.WriteString("\r\n")
}
if msg.ProxyRequire != "" {
b.WriteString("Proxy-Require: ")
b.WriteString(msg.ProxyRequire)
b.WriteString("\r\n")
}
if msg.ReferTo != "" {
b.WriteString("Refer-To: ")
b.WriteString(msg.ReferTo)
b.WriteString("\r\n")
}
if msg.ReferredBy != "" {
b.WriteString("Referred-By: ")
b.WriteString(msg.ReferredBy)
b.WriteString("\r\n")
}
if msg.RemotePartyID != nil {
b.WriteString("Remote-Party-ID: ")
msg.RemotePartyID.Append(b)
b.WriteString("\r\n")
}
if msg.ReplyTo != "" {
b.WriteString("Reply-To: ")
b.WriteString(msg.ReplyTo)
b.WriteString("\r\n")
}
if msg.Require != "" {
b.WriteString("Require: ")
b.WriteString(msg.Require)
b.WriteString("\r\n")
}
if msg.RetryAfter != "" {
b.WriteString("RetryAfter: ")
b.WriteString(msg.RetryAfter)
b.WriteString("\r\n")
}
if msg.Server != "" {
b.WriteString("Server: ")
b.WriteString(msg.Server)
b.WriteString("\r\n")
}
if msg.Subject != "" {
b.WriteString("Subject: ")
b.WriteString(msg.Subject)
b.WriteString("\r\n")
}
if msg.Supported != "" {
b.WriteString("Supported: ")
b.WriteString(msg.Supported)
b.WriteString("\r\n")
}
if msg.Timestamp != "" {
b.WriteString("Timestamp: ")
b.WriteString(msg.Timestamp)
b.WriteString("\r\n")
}
if msg.Unsupported != "" {
b.WriteString("Unsupported: ")
b.WriteString(msg.Unsupported)
b.WriteString("\r\n")
}
if msg.Warning != "" {
b.WriteString("Warning: ")
b.WriteString(msg.Warning)
b.WriteString("\r\n")
}
if msg.WWWAuthenticate != "" {
b.WriteString("WWW-Authenticate: ")
b.WriteString(msg.WWWAuthenticate)
b.WriteString("\r\n")
}
msg.XHeader.Append(b)
if msg.Payload != nil {
b.WriteString("Content-Type: ")
b.WriteString(msg.Payload.ContentType())
b.WriteString("\r\n")
payload := msg.Payload.Data()
b.WriteString("Content-Length: ")
b.WriteString(strconv.Itoa(len(payload)))
b.WriteString("\r\n\r\n")
b.Write(payload)
} else {
b.WriteString("Content-Length: 0\r\n\r\n")
}
}
func (msg *Msg) appendVersion(b *bytes.Buffer) {
b.WriteString("SIP/")
if msg.VersionMajor == 0 {
b.WriteString("2.0")
} else {
b.WriteString(strconv.FormatUint(uint64(msg.VersionMajor), 10))
b.WriteString(".")
b.WriteString(strconv.FormatUint(uint64(msg.VersionMinor), 10))
}
}