-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
59 lines (48 loc) · 1.26 KB
/
utils.go
File metadata and controls
59 lines (48 loc) · 1.26 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
// Copyright 2015 sip authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package sip
import (
"bytes"
"fmt"
"math/rand"
"os"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const branchMagicCookie = "z9hG4bK"
// generateRandom generates random strings for tags/call-ids/branches
func generateRandom(charsToDouble int) string {
var buf bytes.Buffer
for i := 0; i < charsToDouble; i++ {
buf.WriteByte(byte(randInt(65, 90)))
buf.WriteByte(byte(randInt(97, 122)))
}
return string(buf.Bytes())
}
// Helper for generating random number between two given numbers
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
// GenerateBranch generates branch for via header
func GenerateBranch() string {
randomPart := generateRandom(4)
return branchMagicCookie + randomPart
}
// GenerateTag generates tags for To/From headers
func GenerateTag() string {
return generateRandom(4)
}
// GenerateCallID generates call-id
func GenerateCallID() string {
return generateRandom(7)
}
// CheckConnError is used to check for errors encountered during connection establishment
func CheckConnError(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}