-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathendpointapp.cpp
371 lines (330 loc) · 10.6 KB
/
endpointapp.cpp
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
/*****************************************************************************\
** **
** Linux Call Router **
** **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg **
** **
** The EndpointApp represents the application for the Endpoint. **
** **
\*****************************************************************************/
#include "main.h"
/*
* EndpointApp constructor
*/
EndpointApp::EndpointApp(class Endpoint *epoint, int origin, int type)
{
ea_endpoint = epoint;
ea_type = type;
classuse++;
}
/*
* endpoint destructor
*/
EndpointApp::~EndpointApp(void)
{
classuse--;
}
/* mini application for test purpose only */
void EndpointApp::ea_message_port(unsigned int port_id, int message_type, union parameter *param)
{
PDEBUG(DEBUG_EPOINT, "%s: Spare function.\n", __FUNCTION__);
}
void EndpointApp::ea_message_join(unsigned int join_id, int message_type, union parameter *param)
{
PDEBUG(DEBUG_EPOINT, "%s: Spare function.\n", __FUNCTION__);
}
/* create endpoint app */
class EndpointApp *new_endpointapp(class Endpoint *epoint, int origin, int type)
{
class EndpointApp *app = NULL;
switch (type) {
case EAPP_TYPE_PBX:
app = new EndpointAppPBX(epoint, origin);
break;
case EAPP_TYPE_BRIDGE:
app = new EndpointAppBridge(epoint, origin);
break;
}
if (!app)
FATAL("Failed to create endpoint APP (type %d)\n", type);
epoint->ep_app_type = type;
epoint->ep_app = app;
return app;
}
#ifdef WITH_MISDN
/*
* hunts an mISDNport that is available for an outgoing call
* if no ifname was given, any interface that is not an extension
* will be searched.
*/
struct mISDNport *EndpointApp::hunt_port(char *ifname, int *channel)
{
struct interface *interface;
struct interface_port *ifport, *ifport_start;
struct select_channel *selchannel;
struct mISDNport *mISDNport;
int index, i;
int there_is_an_external = 0;
interface = interface_first;
/* first find the given interface or, if not given, one with no extension */
checknext:
if (!interface) {
if (!there_is_an_external && !(ifname && ifname[0])) {
trace_header("CHANNEL SELECTION (no external interface specified)", DIRECTION_NONE);
add_trace("info", NULL, "Add 'extern' parameter to interface.conf.");
end_trace();
}
return(NULL);
}
/* check for given interface */
if (ifname && ifname[0]) {
if (!strcasecmp(interface->name, ifname)) {
/* found explicit interface */
trace_header("CHANNEL SELECTION (found given interface)", DIRECTION_NONE);
add_trace("interface", NULL, "%s", ifname);
end_trace();
goto foundif;
}
} else {
if (interface->external) {
there_is_an_external = 1;
/* found non extension */
trace_header("CHANNEL SELECTION (found external interface)", DIRECTION_NONE);
add_trace("interface", NULL, "%s", interface->name);
end_trace();
goto foundif;
}
}
interface = interface->next;
goto checknext;
foundif:
/* see if interface has ports */
if (!interface->ifport) {
/* no ports */
trace_header("CHANNEL SELECTION (active ports, skipping)", DIRECTION_NONE);
add_trace("interface", NULL, "%s", interface->name);
end_trace();
interface = interface->next;
goto checknext;
}
/* select port by algorithm */
ifport_start = interface->ifport;
index = 0;
if (interface->hunt == HUNT_ROUNDROBIN) {
while(ifport_start->next && index<interface->hunt_next) {
ifport_start = ifport_start->next;
index++;
}
trace_header("CHANNEL SELECTION (starting round-robin)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport_start->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
}
/* loop ports */
ifport = ifport_start;
nextport:
/* see if port is available */
if (!ifport->mISDNport) {
trace_header("CHANNEL SELECTION (port not available, skipping)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
goto portbusy;
}
mISDNport = ifport->mISDNport;
/* see if port is administratively blocked */
if (ifport->block) {
trace_header("CHANNEL SELECTION (port blocked by admin, skipping)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
goto portbusy;
}
/* see if link is up on PTP*/
if (mISDNport->l2hold && mISDNport->l2link<1) {
trace_header("CHANNEL SELECTION (port's layer 2 is down, skipping)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
goto portbusy;
}
/* check for channel form selection list */
*channel = 0;
#ifdef WITH_SS5
if (mISDNport->ss5) {
class Pss5 *port;
port = ss5_hunt_line(mISDNport);
if (port) {
*channel = port->p_m_b_channel;
trace_header("CHANNEL SELECTION (selecting SS5 channel)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
add_trace("channel", NULL, "%d", *channel);
end_trace();
}
} else
#endif
{
selchannel = ifport->out_channel;
while(selchannel) {
switch(selchannel->channel) {
case CHANNEL_FREE: /* free channel */
if (mISDNport->b_reserved >= mISDNport->b_num)
break; /* all channel in use or reserverd */
/* find channel */
i = 0;
while(i < mISDNport->b_num) {
if (mISDNport->b_port[i] == NULL) {
*channel = i+1+(i>=15);
trace_header("CHANNEL SELECTION (selecting free channel)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
add_trace("channel", NULL, "%d", *channel);
end_trace();
break;
}
i++;
}
if (*channel)
break;
trace_header("CHANNEL SELECTION (no channel is 'free')", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
break;
case CHANNEL_ANY: /* don't ask for channel */
if (mISDNport->b_reserved >= mISDNport->b_num) {
trace_header("CHANNEL SELECTION (cannot ask for 'any' channel, all reserved)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
add_trace("total", NULL, "%d", mISDNport->b_num);
add_trace("reserved", NULL, "%d", mISDNport->b_reserved);
end_trace();
break; /* all channel in use or reserverd */
}
trace_header("CHANNEL SELECTION (using 'any' channel)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
*channel = CHANNEL_ANY;
break;
case CHANNEL_NO: /* call waiting */
trace_header("CHANNEL SELECTION (using 'no' channel, call-waiting)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
*channel = CHANNEL_NO;
break;
default:
if (selchannel->channel<1 || selchannel->channel==16) {
trace_header("CHANNEL SELECTION (channel out of range)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
add_trace("channel", NULL, "%d", selchannel->channel);
end_trace();
break; /* invalid channels */
}
i = selchannel->channel-1-(selchannel->channel>=17);
if (i >= mISDNport->b_num) {
trace_header("CHANNEL SELECTION (channel out of range)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
add_trace("channel", NULL, "%d", selchannel->channel);
add_trace("channels", NULL, "%d", mISDNport->b_num);
end_trace();
break; /* channel not in port */
}
if (mISDNport->b_port[i] == NULL) {
*channel = selchannel->channel;
trace_header("CHANNEL SELECTION (selecting given channel)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
add_trace("channel", NULL, "%d", *channel);
end_trace();
break;
}
break;
}
if (*channel)
break; /* found channel */
selchannel = selchannel->next;
}
}
/* if channel was found, return mISDNport and channel */
if (*channel) {
/* setting next port to start next time */
if (interface->hunt == HUNT_ROUNDROBIN) {
index++;
if (!ifport->next)
index = 0;
interface->hunt_next = index;
}
return(mISDNport);
}
trace_header("CHANNEL SELECTION (skipping, no channel found)", DIRECTION_NONE);
add_trace("port", NULL, "%d", ifport->portnum);
add_trace("position", NULL, "%d", index);
end_trace();
portbusy:
/* go next port, until all ports are checked */
index++;
ifport = ifport->next;
if (!ifport) {
index = 0;
ifport = interface->ifport;
}
if (ifport != ifport_start)
goto nextport;
if (!ifname) {
interface = interface->next;
goto checknext;
}
return(NULL); /* no port found */
}
#endif
/* hunts for the given interface
* it does not need to have an mISDNport instance */
struct interface *EndpointApp::hunt_interface(char *ifname)
{
struct interface *interface;
int there_is_an_external = 0;
interface = interface_first;
/* first find the given interface or, if not given, one with no extension */
checknext:
if (!interface) {
if (!there_is_an_external && !(ifname && ifname[0])) {
trace_header("CHANNEL SELECTION (no external interface specified)", DIRECTION_NONE);
add_trace("info", NULL, "Add 'extern' parameter to interface.conf.");
end_trace();
}
return(NULL);
}
/* check for given interface */
if (ifname && ifname[0]) {
if (!strcasecmp(interface->name, ifname)) {
/* found explicit interface */
trace_header("CHANNEL SELECTION (found given interface)", DIRECTION_NONE);
add_trace("interface", NULL, "%s", ifname);
end_trace();
goto foundif;
}
} else {
if (interface->external) {
there_is_an_external = 1;
/* found non extension */
trace_header("CHANNEL SELECTION (found external interface)", DIRECTION_NONE);
add_trace("interface", NULL, "%s", interface->name);
end_trace();
goto foundif;
}
}
interface = interface->next;
goto checknext;
foundif:
return interface;
}
/* must be overloaded by specific app */
void EndpointApp::trace_header(const char *name, int direction)
{
}