[683a60]: sipwitch / driver.cpp Maximize Restore History

Download this file

driver.cpp    210 lines (173 with data), 4.7 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
 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
// Copyright (C) 2008-2009 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "driver.h"
using namespace UCOMMON_NAMESPACE;
using namespace BAYONNE_NAMESPACE;
#ifdef EXOSIP_API4
eXosip_t *driver::context = NULL;
#endif
driver::driver() : Driver("sip", "registry")
{
autotimer = 500;
#ifdef EXOSIP_API4
context = eXosip_malloc();
#endif
}
int driver::start(void)
{
linked_pointer<keydata> kp = keyserver.get("registry");
const char *cp = NULL, *iface = NULL, *transport = NULL, *agent = NULL;
unsigned port = 5060, expires = 300, threads = 0, timeslots = 16;
unsigned rtp = 0;
int protocol = IPPROTO_UDP;
int family = AF_INET;
int tlsmode = 0;
size_t stack = 0;
unsigned priority = 0;
if(keys)
cp = keys->get("port");
if(cp)
port = atoi(cp);
if(keys)
cp = keys->get("expires");
if(cp)
expires = atoi(cp);
if(keys)
cp = keys->get("stack");
if(cp)
stack = atoi(cp);
if(keys)
cp = keys->get("priority");
if(cp)
priority = atoi(cp);
if(keys)
iface = keys->get("interface");
if(iface) {
#ifdef AF_INET6
if(strchr(iface, ':'))
family = AF_INET6;
#endif
if(eq(iface, ":::") || eq(iface, "::0") || eq(iface, "*") || iface[0] == 0)
iface = NULL;
}
if(keys)
transport = keys->get("transport");
if(transport && eq(transport, "tcp")) {
protocol = IPPROTO_TCP;
}
else if(transport && eq(transport, "tls")) {
protocol = IPPROTO_TCP;
tlsmode = 1;
}
if(keys)
agent = keys->get("agent");
if(!agent)
agent = "bayonne-" VERSION "/exosip2";
const char *addr = iface;
eXosip_init(EXOSIP_CONTEXT);
#ifdef AF_INET6
if(family == AF_INET6) {
eXosip_enable_ipv6(1);
if(!iface)
addr = "::0";
}
#endif
if(!addr)
addr = "*";
#if UCOMMON_ABI > 5
Socket::query(family);
#else
Socket::family(family);
#endif
if(eXosip_listen_addr(OPTION_CONTEXT protocol, iface, port, family, tlsmode))
shell::log(shell::FAIL, "driver cannot bind %s, port=%d", addr, port);
else
shell::log(shell::NOTIFY, "driver binding %s, port=%d", addr, port);
osip_trace_initialize_syslog(TRACE_LEVEL0, (char *)"bayonne");
eXosip_set_user_agent(OPTION_CONTEXT agent);
if(keys)
cp = keys->get("threads");
if(cp)
threads = atoi(cp);
if(!threads)
++threads;
while(threads--) {
thread *t = new thread(stack * 1024l);
t->start(priority);
}
if(is(kp)) {
new registry(*kp, port, expires);
return 0;
}
kp = keygroup.begin();
while(is(kp)) {
new registry(*kp, port, expires);
kp.next();
}
if(keys)
cp = keys->get("timeslots");
if(cp)
timeslots = atoi(cp);
if(keys)
cp = keys->get("rtp");
if(cp)
rtp = atoi(cp);
if(!rtp)
rtp = ((port / 2) * 2) + 2;
tsIndex = new Timeslot *[timeslots];
while(timeslots--) {
timeslot *ts = new timeslot(iface, rtp, family);
tsIndex[tsCount++] = (Timeslot *)ts;
rtp += 2;
}
return tsCount;
}
void driver::stop(void)
{
eXosip_quit(EXOSIP_CONTEXT);
thread::shutdown();
unsigned index = 0;
while(index < tsCount) {
Timeslot *ts = tsIndex[index++];
ts->shutdown();
}
}
void driver::automatic(void)
{
EXOSIP_LOCK
eXosip_automatic_action(EXOSIP_CONTEXT);
EXOSIP_UNLOCK
}
registry *driver::locate(int rid)
{
linked_pointer<registry> rp = Group::begin();
while(is(rp)) {
if(rp->rid == rid)
return *rp;
rp.next();
}
return NULL;
}
static SERVICE_MAIN(main, argc, argv)
{
signals::service("bayonne");
server::start(argc, argv);
}
PROGRAM_MAIN(argc, argv)
{
new driver;
server::start(argc, argv, &service_main);
PROGRAM_EXIT(0);
}