-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.go
658 lines (604 loc) · 16.2 KB
/
parser.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// Copyright 2011, Shelby Ramsey. All rights reserved.
// Copyright 2018, Eugen Biegler. All rights reserved.
// Use of this code is governed by a BSD license that can be
// found in the LICENSE.txt file.
package sipparser
import (
"errors"
"fmt"
"strings"
)
const (
sipParseStateStartLine = "SipParseStateStartLine"
sipParseStateCrlf = "SipMsgStateCrlf"
sipParseStateBody = "SipMsgStateBody"
sipParseStateHeaders = "SipMsgStateHeaders"
sipParseStateParseHeaders = "SipMsgStateParseHeaders"
uintCR = '\r'
uintLF = '\n'
CR = "\r"
LF = "\n"
CALLING_PARTY_DEFAULT = "default"
CALLING_PARTY_RPID = "rpid"
CALLING_PARTY_PAID = "paid"
)
type CallingPartyInfo struct {
Name string
Number string
Anonymous bool
}
type Header struct {
Header string
Val string
}
func (h *Header) String() string {
return fmt.Sprintf("%s: %s", h.Header, h.Val)
}
type sipParserStateFn func(s *SipMsg) sipParserStateFn
type SipMsg struct {
State string
Error error
Msg string
CallingParty *CallingPartyInfo
Body string
Authorization *Authorization
AuthVal string
AuthUser string
ContentLength string
ContentType string
From *From
FromUser string
FromHost string
FromTag string
MaxForwards string
Organization string
To *From
ToUser string
ToHost string
ToTag string
Contact *From
ContactVal string
ContactUser string
ContactHost string
ContactPort int
CallID string
XCallID string
XHeader []string
Cseq *Cseq
CseqMethod string
CseqVal string
Reason *Reason
ReasonVal string
RTPStatVal string
ViaOne string
ViaOneBranch string
Privacy string
RemotePartyIdVal string
DiversionVal string
RemotePartyId *RemotePartyId
PAssertedIdVal string
PaiUser string
PaiHost string
PAssertedId *PAssertedId
UserAgent string
Server string
URIHost string
URIRaw string
URIUser string
FirstMethod string
FirstResp string
FirstRespText string
eof int
hdr string
hdrv string
//Via []*Via
//StartLine *StartLine
//Headers []*Header
//Accept *Accept
//AlertInfo string
//Allow []string
//AllowEvents []string
//ContentDisposition *ContentDisposition
//ContentLengthInt int
//MaxForwardsInt int
//ProxyAuthenticate *Authorization
//ProxyRequire []string
//Rack *Rack
//Rseq string
//RseqInt int
//RecordRoute []*URI
//RTPStat *RTPStat
//Route []*URI
//Require []string
//Unsupported []string
//Subject string
//Supported []string
//Warning *Warning
//WWWAuthenticate *Authorization
}
func (s *SipMsg) run() {
for state := parseSip; state != nil; {
state = state(s)
}
}
func (s *SipMsg) addError(err string) sipParserStateFn {
s.Error = errors.New(err)
return nil
}
func (s *SipMsg) addErrorNoReturn(err string) {
s.Error = errors.New(err)
}
func (s *SipMsg) addHdr(str string) {
if str == "" || str == " " {
return
}
sp := strings.IndexRune(str, ':')
if sp == -1 {
return
}
//s.hdr = strings.ToLower(strings.TrimSpace(str[0:sp]))
s.hdr = cleanWs(str[0:sp])
if len(str)-1 >= sp+1 {
s.hdrv = cleanWs(str[sp+1:])
} else {
// No header value
s.hdrv = ""
}
switch {
case s.hdr == "Call-ID" || s.hdr == "CALL-ID" || s.hdr == "Call-Id" || s.hdr == "Call-id" || s.hdr == "call-id" || s.hdr == "I" || s.hdr == "i":
s.CallID = s.hdrv
case s.hdr == "CSeq" || s.hdr == "CSEQ" || s.hdr == "Cseq" || s.hdr == "cseq":
s.CseqVal = s.hdrv
s.parseCseq(s.hdrv)
case s.hdr == "From" || s.hdr == "FROM" || s.hdr == "from" || s.hdr == "F" || s.hdr == "f":
s.parseFrom(s.hdrv)
case s.hdr == "To" || s.hdr == "TO" || s.hdr == "to" || s.hdr == "T" || s.hdr == "t":
s.parseTo(s.hdrv)
case s.hdr == "Server" || s.hdr == "server":
s.Server = s.hdrv
case s.hdr == "Contact" || s.hdr == "CONTACT" || s.hdr == "contact" || s.hdr == "M" || s.hdr == "m":
s.ContactVal = s.hdrv
s.parseContact(str)
case s.hdr == "User-Agent" || s.hdr == "USER-AGENT" || s.hdr == "user-agent":
s.UserAgent = s.hdrv
case s.hdr == "Via" || s.hdr == "VIA" || s.hdr == "via" || s.hdr == "V" || s.hdr == "v":
s.parseVia(s.hdrv)
case s.hdr == "Content-Type" || s.hdr == "CONTENT-TYPE" || s.hdr == "content-type" || s.hdr == "C" || s.hdr == "c":
s.ContentType = s.hdrv
case s.hdr == "Content-Length" || s.hdr == "CONTENT-LENGTH" || s.hdr == "content-length" || s.hdr == "L" || s.hdr == "l":
s.ContentLength = s.hdrv
case s.hdr == "Accept" || s.hdr == "Accept-Encoding" || s.hdr == "Accept-Language":
//s.parseAccept(s.hdrv)
case s.hdr == "Allow":
//s.parseAllow(s.hdrv)
case s.hdr == "Allow‑Events" || s.hdr == "u":
//s.parseAllowEvents(s.hdrv)
case s.hdr == "Authorization" || s.hdr == "authorization" || s.hdr == "Proxy-Authorization" || s.hdr == "proxy-authorization":
s.parseAuthorization(s.hdrv)
case s.hdr == "Content-Disposition":
//s.parseContentDisposition(s.hdrv)
case s.hdr == "Route":
//s.parseRoute(s.hdrv)
case s.hdr == "Record-Route":
//s.parseRecordRoute(s.hdrv)
case s.hdr == "Max-Forwards" || s.hdr == "MAX-FORWARDS" || s.hdr == "max-forwards":
s.MaxForwards = s.hdrv
case s.hdr == "Organization" || s.hdr == "organization":
s.Organization = s.hdrv
case s.hdr == "P-Asserted-Identity" || s.hdr == "p-asserted-identity":
s.PAssertedIdVal = s.hdrv
s.parsePAssertedId(s.hdrv)
case s.hdr == "Proxy-Authenticate" || s.hdr == "proxy-authenticate":
//s.parseProxyAuthenticate(s.hdrv)
case s.hdr == "RAck":
//s.parseRack(s.hdrv)
case s.hdr == "Reason" || s.hdr == "reason":
s.ReasonVal = s.hdrv
//s.parseReason(s.hdrv)
case s.hdr == "Remote-Party-Id" || s.hdr == "remote-party-id":
s.RemotePartyIdVal = s.hdrv
case s.hdr == "Diversion" || s.hdr == "diversion":
s.DiversionVal = s.hdrv
case s.hdr == "Supported":
//s.parseSupported(s.hdrv)
case s.hdr == "Unsupported":
//s.parseUnsupported(s.hdrv)
case s.hdr == "Warning":
//s.parseWarning(s.hdrv)
case s.hdr == "WWW-Authenticate":
//s.parseWWWAuthenticate(s.hdrv)
case s.hdr == "Privacy" || s.hdr == "privacy":
s.Privacy = s.hdrv
case s.hdr == "X-RTP-Stat":
s.parseRTPStat(s.hdrv)
case s.hdr == "Expires":
default:
if len(s.XHeader) > 0 {
for i := range s.XHeader {
if s.hdr == s.XHeader[i] {
s.XCallID = s.hdrv
}
}
}
}
}
func GetSIPHeaderVal(header string, data string) (val string) {
l := len(header)
if startPos := strings.Index(data, header); startPos > -1 {
restData := data[startPos:]
if endPos := strings.Index(restData, "\r\n"); endPos > l {
val = restData[l:endPos]
i := 0
for i < len(val) && (val[i] == ' ' || val[i] == '\t') {
i++
}
return val[i:]
}
}
return ""
}
/*
func (s *SipMsg) GetRURIParamBool(str string) bool {
if s.StartLine == nil || s.StartLine.URI == nil {
return false
}
for i := range s.StartLine.URI.UriParams {
if s.StartLine.URI.UriParams[i].Param == str {
return true
}
}
return false
}
func (s *SipMsg) GetRURIParamVal(str string) string {
if s.StartLine == nil || s.StartLine.URI == nil {
return ""
}
for i := range s.StartLine.URI.UriParams {
if s.StartLine.URI.UriParams[i].Param == str {
return s.StartLine.URI.UriParams[i].Val
}
}
return ""
}
*/
func (s *SipMsg) GetCallingParty(str string) error {
switch {
case str == CALLING_PARTY_RPID:
return s.getCallingPartyRpid()
case str == CALLING_PARTY_PAID:
return s.getCallingPartyPaid()
}
return s.getCallingPartyDefault()
}
func (s *SipMsg) getCallingPartyDefault() error {
if s.From == nil {
return errors.New("getCallingPartyDefault err: no from header found")
}
if s.From.URI == nil {
return errors.New("getCallingPartyDefault err: no uri found in from header")
}
s.CallingParty = &CallingPartyInfo{Name: s.From.Name, Number: s.From.URI.User}
return nil
}
func (s *SipMsg) getCallingPartyPaid() error {
if s.PAssertedId == nil {
if s.PAssertedIdVal == "" {
return s.getCallingPartyDefault()
}
s.parsePAssertedId(s.PAssertedIdVal)
if s.Error != nil {
return s.Error
}
if s.PAssertedId.URI == nil {
return errors.New("getCallingPartyPaid err: p-asserted-id uri is nil")
}
s.CallingParty = &CallingPartyInfo{Name: s.PAssertedId.Name, Number: s.PAssertedId.URI.User}
return nil
}
if s.PAssertedId.URI == nil {
return errors.New("getCallingPartyPaid err: p-asserted-id uri is nil")
}
s.CallingParty = &CallingPartyInfo{Name: s.PAssertedId.Name, Number: s.PAssertedId.URI.User}
return nil
}
func (s *SipMsg) getCallingPartyRpid() error {
if s.RemotePartyId == nil {
if s.RemotePartyIdVal == "" {
return s.getCallingPartyDefault()
}
s.parseRemotePartyId(s.RemotePartyIdVal)
if s.Error != nil {
return s.Error
}
if s.RemotePartyId.URI == nil {
return errors.New("getCallingPartyRpid err: remote party id uri is nil")
}
s.CallingParty = &CallingPartyInfo{Name: s.RemotePartyId.Name, Number: s.RemotePartyId.URI.User}
return nil
}
if s.RemotePartyId.URI == nil {
return errors.New("getCallingPartyRpid err: remote party id uri is nil")
}
s.CallingParty = &CallingPartyInfo{Name: s.RemotePartyId.Name, Number: s.RemotePartyId.URI.User}
return nil
}
/* func (s *SipMsg) parseAccept(str string) {
s.Accept = &Accept{Val: str}
s.Accept.parse()
} */
/* func (s *SipMsg) parseAllow(str string) {
s.Allow = getCommaSeperated(str)
if s.Allow == nil {
s.Allow = []string{str}
}
} */
/* func (s *SipMsg) parseAllowEvents(str string) {
s.AllowEvents = getCommaSeperated(str)
if s.AllowEvents == nil {
s.Allow = []string{str}
}
} */
func (s *SipMsg) parseAuthorization(str string) {
s.Authorization = &Authorization{Val: str}
if s.Error = s.Authorization.parse(); s.Error == nil {
s.AuthUser = s.Authorization.Username
s.AuthVal = s.Authorization.Val
}
}
func (s *SipMsg) parseContact(str string) {
s.Contact = getFrom(str)
if s.Contact.Error == nil {
s.ContactUser = s.Contact.URI.User
s.ContactHost = s.Contact.URI.Host
s.ContactPort = s.Contact.URI.PortInt
} else {
s.Error = s.Contact.Error
}
}
func (s *SipMsg) ParseContact(str string) {
s.parseContact(str)
}
/* func (s *SipMsg) parseContentDisposition(str string) {
s.ContentDisposition = &ContentDisposition{Val: str}
s.ContentDisposition.parse()
} */
func (s *SipMsg) parseCseq(str string) {
s.Cseq = &Cseq{Val: str}
if s.Error = s.Cseq.parse(); s.Error == nil {
s.CseqMethod = s.Cseq.Method
}
}
func (s *SipMsg) parseFrom(str string) {
s.From = getFrom(str)
if s.From.Error == nil {
s.FromUser = s.From.URI.User
s.FromHost = s.From.URI.Host
s.FromTag = s.From.Tag
} else {
s.Error = s.From.Error
}
}
func (s *SipMsg) parsePAssertedId(str string) {
s.PAssertedId = &PAssertedId{Val: str}
s.PAssertedId.parse()
if s.PAssertedId.Error == nil {
if s.PaiUser == "" {
s.PaiUser = s.PAssertedId.URI.User
}
if s.PaiHost == "" {
s.PaiHost = s.PAssertedId.URI.Host
}
} else {
s.PaiUser = s.PAssertedIdVal
}
}
func (s *SipMsg) ParsePAssertedId(str string) {
s.parsePAssertedId(str)
}
/* func (s *SipMsg) parseProxyAuthenticate(str string) {
s.ProxyAuthenticate = &Authorization{Val: str}
s.Error = s.ProxyAuthenticate.parse()
} */
/* func (s *SipMsg) parseRack(str string) {
s.Rack = &Rack{Val: str}
s.Error = s.Rack.parse()
} */
func (s *SipMsg) parseReason(str string) {
s.Reason = &Reason{Val: str}
s.Reason.parse()
}
func (s *SipMsg) parseRTPStat(str string) {
//s.RTPStat = &RTPStat{Val: str}
//s.RTPStat.parse()
s.RTPStatVal = str
}
/* func (s *SipMsg) parseRecordRoute(str string) {
cs := []string{str}
for rt := range cs {
left := 0
right := 0
for i := range cs[rt] {
if cs[rt][i] == '<' && left == 0 {
left = i
}
if cs[rt][i] == '>' && right == 0 {
right = i
}
}
if left < right {
u := ParseURI(cs[rt][left+1 : right])
if u.Error != nil {
s.Error = fmt.Errorf("parseRecordRoute err: received err parsing uri: %v", u.Error)
return
}
if s.RecordRoute == nil {
s.RecordRoute = []*URI{u}
}
s.RecordRoute = append(s.RecordRoute, u)
}
}
return
} */
func (s *SipMsg) parseRemotePartyId(str string) {
s.RemotePartyId = &RemotePartyId{Val: str}
s.RemotePartyId.parse()
if s.RemotePartyId.Error != nil {
s.Error = s.RemotePartyId.Error
}
}
func (s *SipMsg) ParseRemotePartyId(str string) {
s.parseRemotePartyId(str)
}
/* func (s *SipMsg) parseRequire(str string) {
s.Require = getCommaSeperated(str)
if s.Require == nil {
s.Require = []string{str}
}
} */
/* func (s *SipMsg) parseRoute(str string) {
cs := getCommaSeperated(str)
for rt := range cs {
left := 0
right := 0
for i := range cs[rt] {
if cs[rt][i] == '<' && left == 0 {
left = i
}
if cs[rt][i] == '>' && right == 0 {
right = i
}
}
if left < right {
u := ParseURI(cs[rt][left+1 : right])
if u.Error != nil {
s.Error = fmt.Errorf("parseRoute err: received err parsing uri: %v", u.Error)
return
}
if s.Route == nil {
s.Route = []*URI{u}
}
s.Route = append(s.Route, u)
}
}
} */
func (s *SipMsg) parseStartLine(str string) {
s.State = sipParseStateStartLine
sLine := ParseStartLine(str)
s.FirstMethod = sLine.Method
s.FirstResp = sLine.Resp
s.FirstRespText = sLine.RespText
if sLine.URI != nil {
s.URIHost = sLine.URI.Host
s.URIRaw = sLine.URI.Raw
s.URIUser = sLine.URI.User
}
if sLine.Error != nil {
s.Error = fmt.Errorf("parseStartLine err: received err while parsing start line: %v", sLine.Error)
}
}
/* func (s *SipMsg) parseSupported(str string) {
s.Supported = getCommaSeperated(str)
if s.Supported == nil {
s.Supported = []string{str}
}
} */
func (s *SipMsg) parseTo(str string) {
s.To = getFrom(str)
if s.To.Error == nil {
s.ToUser = s.To.URI.User
s.ToHost = s.To.URI.Host
s.ToTag = s.To.Tag
} else {
s.Error = s.To.Error
}
}
/* func (s *SipMsg) parseUnsupported(str string) {
s.Unsupported = getCommaSeperated(str)
if s.Unsupported == nil {
s.Unsupported = []string{str}
}
} */
func (s *SipMsg) parseVia(str string) {
/* vs := &vias{via: str}
vs.parse()
if vs.err != nil {
s.Error = vs.err
return
}
for _, v := range vs.vias {
s.Via = append(s.Via, v)
}
*/
s.ViaOne = str
if a := strings.Index(str, "branch="); a > -1 && a < len(str) {
b := str[a:]
l := len(b)
if c := strings.Index(b, ";"); c > -1 && c < l && l > 7 {
s.ViaOneBranch = b[7:c]
} else if l > 7 {
s.ViaOneBranch = b[7:]
}
}
}
/* func (s *SipMsg) parseWarning(str string) {
s.Warning = &Warning{Val: str}
s.Error = s.Warning.parse()
} */
/* func (s *SipMsg) parseWWWAuthenticate(str string) {
s.WWWAuthenticate = &Authorization{Val: str}
s.Error = s.WWWAuthenticate.parse()
} */
func getHeaders(s *SipMsg) sipParserStateFn {
s.State = sipParseStateHeaders
var hdr string
msgLen := len(s.Msg)
for curPos, crlfPos := 0, 0; curPos < s.eof+2 && s.eof+2 <= msgLen; curPos += 2 {
crlfPos = strings.Index(s.Msg[curPos:s.eof+2], "\n")
if crlfPos > 0 && curPos+crlfPos < msgLen {
if strings.HasSuffix(s.Msg[curPos:curPos+crlfPos], "\r") {
crlfPos--
hdr = s.Msg[curPos : curPos+crlfPos]
} else {
hdr = s.Msg[curPos : curPos+crlfPos]
crlfPos--
}
}
hdr = cleanWs(hdr)
if curPos == 0 {
s.parseStartLine(hdr)
} else {
s.addHdr(hdr)
}
if s.Error != nil {
return nil
}
curPos += crlfPos
}
return nil
}
func getBody(s *SipMsg) sipParserStateFn {
s.State = sipParseStateBody
if len(s.Msg)-1 > s.eof+4 {
s.Body = s.Msg[s.eof+4:]
}
return getHeaders
}
func ParseMsg(str string, xcid ...string) (s *SipMsg) {
headersEnd := strings.Index(str, "\r\n\r\n")
if headersEnd == -1 {
headersEnd = strings.LastIndex(str, "\r\n")
}
s = &SipMsg{Msg: str, XHeader: xcid, eof: headersEnd}
if s.eof == -1 {
s.Error = errors.New("ParseMsg: err parsing msg no SIP eof found")
return s
}
s.run()
return s
}
func parseSip(s *SipMsg) sipParserStateFn {
if s.Error != nil {
return nil
}
return getBody
}