-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
113 lines (102 loc) · 2.53 KB
/
main.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
package main
import (
crand "crypto/rand"
"flag"
"fmt"
mrand "math/rand"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/voiceip/sbc-b2bua/sbc"
"sippy"
"sippy/conf"
"sippy/log"
"sippy/net"
)
func init() {
buf := make([]byte, 8)
crand.Read(buf)
var salt int64
for _, c := range buf {
salt = (salt << 8) | int64(c)
}
mrand.Seed(salt)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
var laddr, nh_addr, logfile string
var lport int
var err error
flag.StringVar(&laddr, "l", "", "Local addr")
flag.IntVar(&lport, "p", -1, "Local port")
flag.StringVar(&nh_addr, "n", "", "Next hop address")
flag.StringVar(&logfile, "L", "/var/log/sip.log", "Log file")
flag.Parse()
if nh_addr == "" {
flag.PrintDefaults()
os.Exit(1)
}
error_logger := sippy_log.NewErrorLogger()
sip_logger, err := sippy_log.NewSipLogger("b2bua", logfile)
if err != nil {
error_logger.Error(err)
return
}
config := &sbc.Config{
Config: sippy_conf.NewConfig(error_logger, sip_logger),
}
//config.SetIPV6Enabled(false)
if nh_addr != "" {
var parts []string
var addr string
if strings.HasPrefix(nh_addr, "[") {
parts = strings.SplitN(nh_addr, "]", 2)
addr = parts[0] + "]"
if len(parts) == 2 {
parts = strings.SplitN(parts[1], ":", 2)
}
} else {
parts = strings.SplitN(nh_addr, ":", 2)
addr = parts[0]
}
port := "5060"
if len(parts) == 2 {
port = parts[1]
}
config.NH_addr = sippy_net.NewHostPort(addr, port)
}
config.SetMyUAName("Strowger SBC B2BUA")
config.SetAllowFormats([]int{0, 8, 18, 100, 101})
if laddr != "" {
config.SetMyAddress(sippy_net.NewMyAddress(laddr))
}
config.SetSipAddress(config.GetMyAddress())
if lport > 0 {
config.SetMyPort(sippy_net.NewMyPort(strconv.Itoa(lport)))
}
config.SetSipPort(config.GetMyPort())
cmap := sbc.NewCallManager(config, error_logger)
sipTransactionManager, err := sippy.NewSipTransactionManager(config, cmap)
if err != nil {
error_logger.Error(err)
return
}
cmap.Sip_TM = sipTransactionManager
cmap.Proxy = sippy.NewStatefulProxy(sipTransactionManager, config.NH_addr, config)
go sipTransactionManager.Run()
fmt.Println(fmt.Sprintf("Started Listen on %s:%s", config.GetMyAddress(), config.GetMyPort().String()))
signal_chan := make(chan os.Signal, 1)
signal.Notify(signal_chan, syscall.SIGTERM, syscall.SIGINT)
signal.Ignore(syscall.SIGHUP, syscall.SIGPIPE, syscall.SIGUSR1, syscall.SIGUSR2)
select {
case <-signal_chan:
cmap.Shutdown()
sipTransactionManager.Shutdown()
time.Sleep(time.Second)
break
}
}