2 * Asterisk -- A telephony toolkit for Linux.
4 * Implementation of the Skinny protocol
6 * Asterisk is Copyright (C) 1999-2003 Mark Spencer
8 * chan_skinny was developed by Jeremy McNamara & Florian Overkamp
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
20 #include <asterisk/lock.h>
21 #include <asterisk/channel.h>
22 #include <asterisk/channel_pvt.h>
23 #include <asterisk/config.h>
24 #include <asterisk/logger.h>
25 #include <asterisk/module.h>
26 #include <asterisk/pbx.h>
27 #include <asterisk/options.h>
28 #include <asterisk/lock.h>
29 #include <asterisk/sched.h>
30 #include <asterisk/io.h>
31 #include <asterisk/rtp.h>
32 #include <asterisk/acl.h>
33 #include <asterisk/callerid.h>
34 #include <asterisk/cli.h>
35 #include <asterisk/say.h>
36 #include <asterisk/cdr.h>
37 #include <asterisk/astdb.h>
38 #include <asterisk/parking.h>
39 #include <asterisk/app.h>
40 #include <asterisk/musiconhold.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <sys/ioctl.h>
50 #include <arpa/inet.h>
51 #include <sys/signal.h>
52 #include <asterisk/dsp.h>
55 /************************************************************************************/
56 /* Skinny/Asterisk Protocol Settings */
57 /************************************************************************************/
58 static char *desc = "Skinny Client Control Protocol (Skinny)";
59 static char *tdesc = "Skinny Client Control Protocol (Skinny)";
60 static char *type = "Skinny";
61 static char *config = "skinny.conf";
63 /* Just about everybody seems to support ulaw, so make it a nice default */
64 static int capability = AST_FORMAT_ULAW;
66 #define DEFAULT_SKINNY_PORT 2000
67 #define DEFAULT_SKINNY_BACKLOG 2 // was 200
68 #define SKINNY_MAX_PACKET 1000
70 static int keep_alive = 120;
71 static char date_format[6] = "D-M-Y";
72 static char version_id[16] = "P002F202";
74 // these should be in an include file, but dunno what to include
75 typedef unsigned char UINT8;
76 typedef unsigned short UINT16;
77 typedef unsigned int UINT32;
79 /************************************************************************************/
80 /* Protocol Messages */
81 /************************************************************************************/
83 #define KEEP_ALIVE_MESSAGE 0x0000
84 /* no additional struct */
86 #define REGISTER_MESSAGE 0x0001
87 typedef struct register_message {
96 #define IP_PORT_MESSAGE 0x0002
98 #define KEYPAD_BUTTON_MESSAGE 0x0003
99 typedef struct keypad_button_message {
101 } keypad_button_message;
103 #define STIMULUS_MESSAGE 0x0005
104 typedef struct stimulus_message {
106 int stimulusInstance;
109 #define OFFHOOK_MESSAGE 0x0006
110 #define ONHOOK_MESSAGE 0x0007
112 #define CAPABILITIES_RES_MESSAGE 0x0010
113 typedef struct station_capabilities {
120 } station_capabilities;
122 typedef struct capabilities_res_message {
124 struct station_capabilities caps[18];
125 } capabilities_res_message;
127 #define SPEED_DIAL_STAT_REQ_MESSAGE 0x000A
128 typedef struct speed_dial_stat_req_message {
130 } speed_dial_stat_req_message;
132 #define LINE_STATE_REQ_MESSAGE 0x000B
133 typedef struct line_state_req_message {
135 } line_state_req_message;
137 #define TIME_DATE_REQ_MESSAGE 0x000D
138 #define VERSION_REQ_MESSAGE 0x000F
139 #define BUTTON_TEMPLATE_REQ_MESSAGE 0x000E
140 #define SERVER_REQUEST_MESSAGE 0x0012
141 #define ALARM_MESSAGE 0x0020
143 #define OPEN_RECIEVE_CHANNEL_ACK_MESSAGE 0x0022
144 typedef struct open_recieve_channel_ack_message {
149 } open_recieve_channel_ack_message;
151 #define SOFT_KEY_SET_REQ_MESSAGE 0x0025
152 #define UNREGISTER_MESSAGE 0x0027
153 #define SOFT_KEY_TEMPLATE_REQ_MESSAGE 0x0028
155 #define REGISTER_ACK_MESSAGE 0x0081
156 typedef struct register_ack_message {
158 char dateTemplate[6];
160 int secondaryKeepAlive;
162 } register_ack_message;
164 #define START_TONE_MESSAGE 0x0082
165 typedef struct start_tone_message {
167 } start_tone_message;
169 #define STOP_TONE_MESSAGE 0x0083
171 #define SET_RINGER_MESSAGE 0x0085
172 typedef struct set_ringer_message {
174 } set_ringer_message;
176 #define SET_LAMP_MESSAGE 0x0086
177 typedef struct set_lamp_message {
179 int stimulusInstance;
183 #define SET_SPEAKER_MESSAGE 0x0088
184 typedef struct set_speaker_message {
186 } set_speaker_message;
188 #define START_MEDIA_TRANSMISSION_MESSAGE 0x008A
189 typedef struct media_qualifier {
196 typedef struct start_media_transmission_message {
203 media_qualifier qualifier;
204 } start_media_transmission_message;
206 #define CALL_INFO_MESSAGE 0x008F
207 typedef struct call_info_message {
208 char callingPartyName[40];
209 char callingParty[24];
210 char calledPartyName[40];
211 char calledParty[24];
215 char originalCalledPartyName[40];
216 char originalCalledParty[24];
219 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
220 typedef struct speed_dial_stat_res_message {
222 char speedDialDirNumber[24];
223 char speedDialDisplayName[40];
224 } speed_dial_stat_res_message;
226 #define LINE_STAT_RES_MESSAGE 0x0092
227 typedef struct line_stat_res_message {
229 char lineDirNumber[24];
230 char lineDisplayName[42];
232 } line_stat_res_message;
234 #define DEFINETIMEDATE_MESSAGE 0x0094
235 typedef struct definetimedate_message {
236 int year; /* since 1900 */
238 int dayofweek; /* monday = 1 */
245 } definetimedate_message;
247 #define DISPLAYTEXT_MESSAGE 0x0099
248 typedef struct displaytext_message {
250 } displaytext_message;
252 #define REGISTER_REJ_MESSAGE 0x009D
253 typedef struct register_rej_message {
255 } register_rej_message;
257 #define CAPABILITIES_REQ_MESSAGE 0x009B
259 #define SERVER_RES_MESSAGE 0x009E
260 typedef struct server_identifier {
264 typedef struct server_res_message {
265 server_identifier server[5];
266 int serverListenPort[5];
268 } server_res_message;
270 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
271 static const char *button_definition_hack = {
272 "\x01\x09\x01\x02\x02\x02\x03\x02\x04\x02\x05\x02\x06\x02\x07\x02"
273 "\x08\x02\x09\x02\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
274 "\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
275 "\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
276 "\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff\x00\xff"
280 typedef struct buttondefinition {
281 UINT8 instanceNumber;
282 UINT8 buttonDefinition;
285 typedef struct button_template_res_message {
288 UINT32 totalButtonCount;
289 button_definition definition[42];
290 } button_template_res_message;
292 #define VERSION_RES_MESSAGE 0x0098
293 typedef struct version_res_message {
295 } version_res_message;
297 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
299 #define OPEN_RECIEVE_CHANNEL_MESSAGE 0x0105
300 typedef struct open_recieve_channel_message {
307 } open_recieve_channel_message;
309 #define CLOSE_RECIEVE_CHANNEL_MESSAGE 0x0106
310 typedef struct close_recieve_channel_message {
313 } close_recieve_channel_message;
316 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
317 static const char *soft_key_template_hack = {
318 "\x52\x65\x64\x69\x61\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
319 "\x01\x00\x00\x00\x4e\x65\x77\x43\x61\x6c\6c\\x00\x00\x00\x00\x00"
320 "\x00\x00\x00\x00\x02\x00\x00\x00\x48\x6f\x6c\x64\x00\x00\x00\x00"
321 "\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x54\x72\x6e\x73"
322 "\x66\x65\x72\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00"
323 "\x43\x46\x77\x64\x41\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05"
324 "\x00\x00\x00\x43\x46\x77\x64\x20\x42\x75\x73\x79\x00\x00\x00\x00"
325 "\x00\x00\x00\x06\x00\x00\x00\x43\x46\x77\x64\x4e\x6f\x41\x6e\x73"
326 "\x77\x65\x72\x00\x00\x00\x00\x07\x00\x00\x00\x3c\x3c\x00\x00\x00"
327 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x45"
328 "\x6e\x64\x43\x61\x6c\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x09"
329 "\x00\x00\x00\x52\x65\x73\x75\x6d\x65\x00\x00\x00\x00\x00\x00\x00"
330 "\x00\x00\x0a\x00\x00\x00\x41\x6e\x73\x77\x65\x72\x00\x00\x00\x00"
331 "\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x49\x6e\x66\x6f\x00\x00"
332 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x43\x6f"
333 "\x6e\x66\x72\x6e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0d\x00\x00"
334 "\x00\x50\x61\x72\x6b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
335 "\x00\x0e\x00\x00\x00\x4a\x6f\x69\x6e\x00\x00\x00\x00\x00\x00\x00"
336 "\x00\x00\x00\x00\x0f\x00\x00\x00\x4d\x65\x65\x74\x4d\x65\x00\x00"
337 "\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x50\x69\x63\x6b"
338 "\x55\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00"
339 "\x47\x50\x69\x63\x6b\x55\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00"
340 "\x12\x00\x00\x00\x52\x6d\x4c\x73\x43\x00\x00\x00\x00\x00\x00\x00"
341 "\x00\x00\x00\x13\x00\x00\x00\x42\x61\x72\x67\x65\x00\x00\x00\x00"
342 "\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x42\x61\x72\x67\x65"
343 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00"
346 typedef struct soft_key_template_definition {
347 char softKeyLabel[16];
349 } soft_key_template_definition;
351 typedef struct soft_key_template {
354 int totalSoftKeyCount;
355 soft_key_template_definition softKeyTemplateDefinition[32];
358 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
359 static const char *soft_key_set_hack = {
360 "\x01\x02\x05\x03\x09\x0a\x0b\x10\x11\x12\x04\x0e\x0d\x00\x00\x00"
361 "\x2d\x01\x2e\x01\x31\x01\x2f\x01\x35\x01\x36\x01\x37\x01\x3c\x01"
362 "\x3d\x01\x3e\x01\x30\x01\x3a\x01\x39\x01\x00\x00\x00\x00\x00\x00"
363 "\x03\x09\x04\x0e\x0d\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
364 "\x2f\x01\x35\x01\x30\x01\x3a\x01\x39\x01\x3f\x01\x00\x00\x00\x00"
365 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
366 "\x0a\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
367 "\x36\x01\x2e\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
368 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
369 "\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
370 "\x37\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
371 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
372 "\x01\x09\x05\x10\x11\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
373 "\x2d\x01\x35\x01\x31\x01\x3c\x01\x3d\x01\x3e\x01\x00\x00\x00\x00"
374 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
375 "\x00\x09\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
376 "\x00\x00\x35\x01\x30\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
377 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
378 "\x08\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
379 "\x34\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
380 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
381 "\x00\x09\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
382 "\x00\x00\x35\x01\x39\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
383 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
384 "\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
385 "\x00\x00\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
386 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
387 "\x01\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
388 "\x2d\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
389 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
390 "\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
391 "\x41\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
392 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
395 typedef struct soft_key_set_definition {
396 UINT8 softKeyTemplateIndex[16];
397 UINT16 softKeyInfoIndex[16];
398 } soft_key_set_definition;
400 typedef struct soft_key_sets {
401 UINT32 softKeySetOffset;
402 UINT32 softKeySetCount;
403 UINT32 totalSoftKeySetCount;
404 soft_key_set_definition softKeySetDefinition[16];
408 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
409 typedef struct select_soft_keys_message {
414 } select_soft_keys_message;
416 #define CALL_STATE_MESSAGE 0x0111
417 typedef struct call_state_message {
421 } call_state_message;
423 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
424 typedef struct activate_call_plane_message {
426 } activate_call_plane_message;
428 /* packet composition */
434 speed_dial_stat_req_message speeddialreq;
435 register_message reg;
436 register_ack_message regack;
437 register_rej_message regrej;
438 capabilities_res_message caps;
439 version_res_message version;
440 button_template_res_message buttontemplate;
441 displaytext_message displaytext;
442 definetimedate_message definetimedate;
443 start_tone_message starttone;
444 speed_dial_stat_res_message speeddial;
445 line_state_req_message line;
446 line_stat_res_message linestat;
447 soft_key_sets softkeysets;
448 soft_key_template softkeytemplate;
449 server_res_message serverres;
450 set_lamp_message setlamp;
451 set_ringer_message setringer;
452 call_state_message callstate;
453 keypad_button_message keypad;
454 select_soft_keys_message selectsoftkey;
455 activate_call_plane_message activatecallplane;
456 stimulus_message stimulus;
457 set_speaker_message setspeaker;
458 call_info_message callinfo;
459 start_media_transmission_message startmedia;
460 open_recieve_channel_message openrecievechannel;
461 open_recieve_channel_ack_message openrecievechannelack;
462 close_recieve_channel_message closerecievechannel;
466 /************************************************************************************/
467 /* Asterisk specific globals */
468 /************************************************************************************/
470 static int skinnydebug = 1; /* XXX for now, enable debugging default */
472 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
473 static struct sockaddr_in bindaddr;
474 static char ourhost[256];
476 static struct in_addr __ourip;
478 static int skinnysock = -1;
479 static pthread_t tcp_thread;
480 static pthread_t accept_t;
481 static char context[AST_MAX_EXTENSION] = "default";
482 static char language[MAX_LANGUAGE] = "";
483 static char musicclass[MAX_LANGUAGE] = "";
484 static char callerid[AST_MAX_EXTENSION] = "";
485 static char linelabel[AST_MAX_EXTENSION] ="";
487 static unsigned int cur_callergroup = 0;
488 static unsigned int cur_pickupgroup = 0;
489 static int immediate = 0;
490 static int callwaiting = 0;
491 static int callreturn = 0;
492 static int threewaycalling = 0;
493 /* This is for flashhook transfers */
494 static int transfer = 0;
495 static int cancallforward = 0;
496 /*static int busycount = 3;*/
497 static char accountcode[20] = "";
498 static char mailbox[AST_MAX_EXTENSION];
499 static int amaflags = 0;
500 static int callnums = 1;
506 #define SKINNY_SPEAKERON 1
507 #define SKINNY_SPEAKEROFF 2
509 #define SKINNY_OFFHOOK 1
510 #define SKINNY_ONHOOK 2
511 #define SKINNY_RINGOUT 3
512 #define SKINNY_RINGIN 4
513 #define SKINNY_CONNECTED 5
514 #define SKINNY_BUSY 6
515 #define SKINNY_CONGESTION 7
516 #define SKINNY_HOLD 8
517 #define SKINNY_CALLWAIT 9
518 #define SKINNY_TRANSFER 10
519 #define SKINNY_PARK 11
520 #define SKINNY_PROGRESS 12
521 #define SKINNY_INVALID 14
523 #define SKINNY_SILENCE 0
524 #define SKINNY_DIALTONE 33
525 #define SKINNY_BUSYTONE 35
526 #define SKINNY_ALERT 36
527 #define SKINNY_REORDER 37
528 #define SKINNY_CALLWAITTONE 45
530 #define SKINNY_LAMP_OFF 1
531 #define SKINNY_LAMP_ON 2
532 #define SKINNY_LAMP_WINK 3
533 #define SKINNY_LAMP_FLASH 4
534 #define SKINNY_LAMP_BLINK 5
536 #define SKINNY_RING_OFF 1
537 #define SKINNY_RING_INSIDE 2
538 #define SKINNY_RING_OUTSIDE 3
539 #define SKINNY_RING_FEATURE 4
544 #define STIMULUS_REDIAL 1
545 #define STIMULUS_SPEEDDIAL 2
546 #define STIMULUS_HOLD 3
547 #define STIMULUS_TRANSFER 4
548 #define STIMULUS_FORWARDALL 5
549 #define STIMULUS_FORWARDBUSY 6
550 #define STIMULUS_FORWARDNOANSWER 7
551 #define STIMULUS_DISPLAY 8
552 #define STIMULUS_LINE 9
554 /* Skinny rtp stream modes */
555 #define SKINNY_CX_SENDONLY 0
556 #define SKINNY_CX_RECVONLY 1
557 #define SKINNY_CX_SENDRECV 2
558 #define SKINNY_CX_CONF 3
559 #define SKINNY_CX_CONFERENCE 3
560 #define SKINNY_CX_MUTE 4
561 #define SKINNY_CX_INACTIVE 4
564 static char *skinny_cxmodes[] = {
573 /* driver scheduler */
574 static struct sched_context *sched;
575 static struct io_context *io;
577 /* usage count and locking */
578 static int usecnt = 0;
579 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
581 /* Protect the monitoring thread, so only one process can kill or start it, and not
582 when it's doing something critical. */
583 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
584 /* Protect the network socket */
585 static ast_mutex_t netlock = AST_MUTEX_INITIALIZER;
586 /* Protect the session list */
587 static ast_mutex_t sessionlock = AST_MUTEX_INITIALIZER;
588 /* Protect the device list */
589 static ast_mutex_t devicelock = AST_MUTEX_INITIALIZER;
591 /* This is the thread for the monitor which checks for input on the channels
592 which are not currently in use. */
593 static pthread_t monitor_thread = 0;
595 /* Wait up to 16 seconds for first digit */
596 static int firstdigittimeout = 16000;
598 /* How long to wait for following digits */
599 static int gendigittimeout = 8000;
601 /* How long to wait for an extra digit, if there is an ambiguous match */
602 static int matchdigittimeout = 3000;
604 struct skinny_subchannel {
607 struct ast_channel *owner;
608 struct skinny_line *parent;
616 struct skinny_subchannel *next; /* for our circular linked list */
622 char label[42]; /* Label that shows next to the line buttons */
623 struct skinny_subchannel *sub; /* pointer to our current connection, channel and stuff */
624 char accountcode[80];
625 char exten[AST_MAX_EXTENSION]; /* Extention where to start */
626 char context[AST_MAX_EXTENSION];
627 char language[MAX_LANGUAGE];
628 char callerid[AST_MAX_EXTENSION]; /* Caller*ID */
629 char lastcallerid[AST_MAX_EXTENSION]; /* Last Caller*ID */
630 char call_forward[AST_MAX_EXTENSION];
631 char mailbox[AST_MAX_EXTENSION];
632 char musicclass[MAX_LANGUAGE];
633 int curtone; /* Current tone */
634 unsigned int callgroup;
635 unsigned int pickupgroup;
641 int dnd; /* How does this affect callwait? Do we just deny a skinny_request if we're dnd? */
650 int nonCodecCapability;
652 int msgstate; /* voicemail message state */
656 struct skinny_line *next;
657 struct skinny_device *parent;
660 static struct skinny_device {
661 /* A device containing one or more lines */
666 struct sockaddr_in addr;
667 struct in_addr ourip;
668 struct skinny_line *lines;
670 struct skinnysession *session;
671 struct skinny_device *next;
674 struct skinnysession {
677 struct sockaddr_in sin;
679 char inbuf[SKINNY_MAX_PACKET];
680 struct skinny_device *device;
681 struct skinnysession *next;
684 static skinny_req *req_alloc(size_t size)
687 req = malloc(size+12);
691 memset(req, 0, size+12);
695 static struct skinny_subchannel *find_subchannel_by_line(struct skinny_line *l)
697 /* Need to figure out how to determine which sub we want */
699 struct skinny_subchannel *sub = l->sub;
703 static struct skinny_subchannel *find_subchannel_by_name(char *dest)
705 struct skinny_line *l;
706 struct skinny_device *d;
711 strncpy(line, dest, sizeof(line) - 1);
712 at = strchr(line, '@');
714 ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
720 ast_mutex_lock(&devicelock);
723 if (!strcasecmp(d->name, device)) {
725 printf("Found device: %s\n", d->name);
727 /* Found the device */
730 /* Search for the right line */
731 if (!strcasecmp(l->name, line)) {
732 ast_mutex_unlock(&devicelock);
740 /* Device not found*/
741 ast_mutex_unlock(&devicelock);
745 static int transmit_response(struct skinnysession *s, skinny_req *req)
748 ast_mutex_lock(&s->lock);
750 printf("writing packet type %d (%d bytes) to socket %d\n", req->e, req->len+8, s->fd);
752 res = write(s->fd, req, req->len+8);
753 if (res != req->len+8) {
754 ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, req->len+8, strerror(errno));
756 ast_mutex_unlock(&s->lock);
760 /* XXX Do this right*/
761 static int convert_cap(int capability)
763 return 4; // ulaw (this is not the same as asterisk's '4' :)
767 static void transmit_speaker_mode(struct skinnysession *s, int mode)
771 req = req_alloc(sizeof(struct set_speaker_message));
773 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
776 req->len = sizeof(set_speaker_message)+4;
777 req->e = SET_SPEAKER_MESSAGE;
778 req->data.setspeaker.mode = mode;
779 transmit_response(s, req);
782 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
785 int memsize = sizeof(struct call_state_message);
787 req = req_alloc(memsize);
789 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
792 if (state == SKINNY_ONHOOK) {
793 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
795 req->len = sizeof(call_state_message)+4;
796 req->e = CALL_STATE_MESSAGE;
797 req->data.callstate.callState = state;
798 req->data.callstate.lineInstance = instance;
799 req->data.callstate.callReference = callid;
800 transmit_response(s, req);
801 if (state == SKINNY_OFFHOOK) {
802 memset(req, 0, memsize);
803 req->len = sizeof(activate_call_plane_message)+4;
804 req->e = ACTIVATE_CALL_PLANE_MESSAGE;
805 req->data.activatecallplane.lineInstance = instance;
806 transmit_response(s, req);
807 } else if (state == SKINNY_ONHOOK) {
808 memset(req, 0, memsize);
809 req->len = sizeof(activate_call_plane_message)+4;
810 req->e = ACTIVATE_CALL_PLANE_MESSAGE;
811 req->data.activatecallplane.lineInstance = 0;
812 transmit_response(s, req);
813 memset(req, 0, memsize);
814 req->len = sizeof(close_recieve_channel_message)+4;
815 req->e = CLOSE_RECIEVE_CHANNEL_MESSAGE;
816 req->data.closerecievechannel.conferenceId = 0;
817 req->data.closerecievechannel.partyId = 0;
818 transmit_response(s, req);
822 static void transmit_connect(struct skinnysession *s)
825 struct skinny_line *l = s->device->lines;
827 req = req_alloc(sizeof(struct open_recieve_channel_message));
829 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
832 req->len = sizeof(struct call_info_message);
833 req->e = OPEN_RECIEVE_CHANNEL_MESSAGE;
834 req->data.openrecievechannel.conferenceId = 0;
835 req->data.openrecievechannel.partyId = 0;
836 req->data.openrecievechannel.packets = 20;
837 req->data.openrecievechannel.capability = convert_cap(l->capability);
838 req->data.openrecievechannel.echo = 0;
839 req->data.openrecievechannel.bitrate = 0;
840 transmit_response(s, req);
843 static void transmit_tone(struct skinnysession *s, int tone)
848 req = req_alloc(sizeof(struct start_tone_message));
852 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
856 req->len = sizeof(start_tone_message)+4;
857 req->e = START_TONE_MESSAGE;
858 req->data.starttone.tone = tone; // da tone
861 req->e = STOP_TONE_MESSAGE;
863 transmit_response(s, req);
866 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
869 int memsize = sizeof(struct select_soft_keys_message);
871 req = req_alloc(memsize);
873 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
876 memset(req, 0, memsize);
877 req->len = sizeof(select_soft_keys_message)+4;
878 req->e = SELECT_SOFT_KEYS_MESSAGE;
879 req->data.selectsoftkey.instance = instance;
880 req->data.selectsoftkey.reference = callid;
881 req->data.selectsoftkey.softKeySetIndex = softkey;
882 transmit_response(s, req);
885 static void transmit_lamp_indication(struct skinnysession *s, int instance, int indication)
889 req = req_alloc(sizeof(struct set_lamp_message));
891 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
894 req->len = sizeof(set_lamp_message)+4;
895 req->e = SET_LAMP_MESSAGE;
896 req->data.setlamp.stimulus = 0x9; // magic number
897 req->data.setlamp.stimulusInstance = instance;
898 req->data.setlamp.deviceStimulus = indication;
899 transmit_response(s, req);
902 static void transmit_ringer_mode(struct skinnysession *s, int mode)
906 req = req_alloc(sizeof(struct set_ringer_message));
908 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
911 req->len = sizeof(set_ringer_message)+4;
912 req->e = SET_RINGER_MESSAGE;
913 req->data.setringer.ringerMode = mode;
914 transmit_response(s, req);
917 /* I do not believe skinny can deal with video.
918 Anyone know differently? */
919 static struct ast_rtp *skinny_get_vrtp_peer(struct ast_channel *chan)
924 static struct ast_rtp *skinny_get_rtp_peer(struct ast_channel *chan)
926 struct skinny_subchannel *sub;
927 sub = chan->pvt->pvt;
933 static int skinny_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp)
935 struct skinny_subchannel *sub;
936 sub = chan->pvt->pvt;
938 /* transmit_modify_with_sdp(sub, rtp); @@FIXME@@ if needed */
944 static struct ast_rtp_protocol skinny_rtp = {
945 get_rtp_info: skinny_get_rtp_peer,
946 get_vrtp_info: skinny_get_vrtp_peer,
947 set_rtp_peer: skinny_set_rtp_peer,
950 static int skinny_do_debug(int fd, int argc, char *argv[])
953 return RESULT_SHOWUSAGE;
955 ast_cli(fd, "Skinny Debugging Enabled\n");
956 return RESULT_SUCCESS;
959 static int skinny_no_debug(int fd, int argc, char *argv[])
962 return RESULT_SHOWUSAGE;
964 ast_cli(fd, "Skinny Debugging Disabled\n");
965 return RESULT_SUCCESS;
968 static int skinny_show_lines(int fd, int argc, char *argv[])
970 struct skinny_device *d;
971 struct skinny_line *l;
974 return RESULT_SHOWUSAGE;
975 ast_mutex_lock(&devicelock);
979 ast_cli(fd, "Device '%s' at %s\n", d->name, inet_ntoa(d->addr.sin_addr));
981 ast_cli(fd, " -- '%s@%s in '%s' is %s\n", l->name, d->name, l->context, l->sub->owner ? "active" : "idle");
986 ast_cli(fd, " << No Lines Defined >> ");
990 ast_mutex_unlock(&devicelock);
991 return RESULT_SUCCESS;
994 static char show_lines_usage[] =
995 "Usage: skinny show lines\n"
996 " Lists all lines known to the Skinny subsystem.\n";
998 static char debug_usage[] =
999 "Usage: skinny debug\n"
1000 " Enables dumping of Skinny packets for debugging purposes\n";
1002 static char no_debug_usage[] =
1003 "Usage: skinny no debug\n"
1004 " Disables dumping of Skinny packets for debugging purposes\n";
1006 static struct ast_cli_entry cli_show_lines =
1007 { { "skinny", "show", "lines", NULL }, skinny_show_lines, "Show defined Skinny lines per device", show_lines_usage };
1008 static struct ast_cli_entry cli_debug =
1009 { { "skinny", "debug", NULL }, skinny_do_debug, "Enable Skinny debugging", debug_usage };
1010 static struct ast_cli_entry cli_no_debug =
1011 { { "skinny", "no", "debug", NULL }, skinny_no_debug, "Disable Skinny debugging", no_debug_usage };
1013 static struct skinny_device *build_device(char *cat, struct ast_variable *v)
1015 struct skinny_device *d;
1016 struct skinny_line *l;
1017 struct skinny_subchannel *sub;
1020 d = malloc(sizeof(struct skinny_device));
1022 memset(d, 0, sizeof(struct skinny_device));
1023 strncpy(d->name, cat, sizeof(d->name) - 1);
1025 if (!strcasecmp(v->name, "host")) {
1026 if (ast_get_ip(&d->addr, v->value)) {
1030 } else if (!strcasecmp(v->name, "port")) {
1031 d->addr.sin_port = htons(atoi(v->value));
1032 } else if (!strcasecmp(v->name, "device")) {
1033 strncpy(d->id, v->value, sizeof(d->id)-1);
1034 } else if (!strcasecmp(v->name, "permit") ||
1035 !strcasecmp(v->name, "deny")) {
1036 d->ha = ast_append_ha(v->name, v->value, d->ha);
1037 } else if (!strcasecmp(v->name, "context")) {
1038 strncpy(context, v->value, sizeof(context) - 1);
1039 } else if (!strcasecmp(v->name, "nat")) {
1040 nat = ast_true(v->value);
1041 } else if (!strcasecmp(v->name, "callerid")) {
1042 if (!strcasecmp(v->value, "asreceived"))
1043 strcpy(callerid, "");
1045 strncpy(callerid, v->value, sizeof(callerid) - 1);
1046 } else if (!strcasecmp(v->name, "language")) {
1047 strncpy(language, v->value, sizeof(language)-1);
1048 } else if (!strcasecmp(v->name, "accountcode")) {
1049 strncpy(accountcode, v->value, sizeof(accountcode)-1);
1050 } else if (!strcasecmp(v->name, "amaflags")) {
1051 y = ast_cdr_amaflags2int(v->value);
1053 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
1057 } else if (!strcasecmp(v->name, "musiconhold")) {
1058 strncpy(musicclass, v->value, sizeof(musicclass)-1);
1059 } else if (!strcasecmp(v->name, "callgroup")) {
1060 cur_callergroup = ast_get_group(v->value);
1061 } else if (!strcasecmp(v->name, "pickupgroup")) {
1062 cur_pickupgroup = ast_get_group(v->value);
1063 } else if (!strcasecmp(v->name, "immediate")) {
1064 immediate = ast_true(v->value);
1065 } else if (!strcasecmp(v->name, "cancallforward")) {
1066 cancallforward = ast_true(v->value);
1067 } else if (!strcasecmp(v->name, "mailbox")) {
1068 strncpy(mailbox, v->value, sizeof(mailbox) -1);
1069 } else if (!strcasecmp(v->name, "callreturn")) {
1070 callreturn = ast_true(v->value);
1071 } else if (!strcasecmp(v->name, "immediate")) {
1072 immediate = ast_true(v->value);
1073 } else if (!strcasecmp(v->name, "callwaiting")) {
1074 callwaiting = ast_true(v->value);
1075 } else if (!strcasecmp(v->name, "transfer")) {
1076 transfer = ast_true(v->value);
1077 } else if (!strcasecmp(v->name, "threewaycalling")) {
1078 threewaycalling = ast_true(v->value);
1079 } else if (!strcasecmp(v->name, "linelabel")) {
1080 strncpy(linelabel, v->value, sizeof(linelabel)-1);
1081 } else if (!strcasecmp(v->name, "trunk") ||
1082 !strcasecmp(v->name, "line")) {
1083 l = malloc(sizeof(struct skinny_line));;
1085 memset(l, 0, sizeof(struct skinny_line));
1086 strncpy(l->name, v->value, sizeof(l->name) - 1);
1088 /* XXX Should we check for uniqueness?? XXX */
1090 strncpy(l->context, context, sizeof(l->context) - 1);
1091 strncpy(l->callerid, callerid, sizeof(l->callerid) - 1);
1092 strncpy(l->label, linelabel, sizeof(l->label) - 1);
1093 strncpy(l->language, language, sizeof(l->language) - 1);
1094 strncpy(l->musicclass, musicclass, sizeof(l->musicclass)-1);
1095 strncpy(l->mailbox, mailbox, sizeof(l->mailbox)-1);
1096 if (strlen(mailbox)) {
1097 ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
1100 l->capability = capability;
1102 if (!strcasecmp(v->name, "trunk"))
1103 l->type = TYPE_TRUNK;
1105 l->type = TYPE_LINE;
1107 l->immediate = immediate;
1108 l->callgroup = cur_callergroup;
1109 l->pickupgroup = cur_pickupgroup;
1110 l->callreturn = callreturn;
1111 l->cancallforward = cancallforward;
1112 l->callwaiting = callwaiting;
1113 l->transfer = transfer;
1114 l->threewaycalling = threewaycalling;
1116 l->onhooktime = time(NULL);
1118 /* ASSUME we're onhook at this point*/
1119 l->hookstate = SKINNY_ONHOOK;
1121 for (i = 0; i < MAX_SUBS; i++) {
1122 sub = malloc(sizeof(struct skinny_subchannel));
1124 ast_verbose(VERBOSE_PREFIX_3 "Allocating Skinny subchannel '%d' on %s@%s\n", i, l->name, d->name);
1125 memset(sub, 0, sizeof(struct skinny_subchannel));
1127 /* Make a call*ID */
1128 sub->callid = callnums;
1130 sub->cxmode = SKINNY_CX_INACTIVE;
1135 /* XXX Should find a way to clean up our memory */
1136 ast_log(LOG_WARNING, "Out of memory allocating subchannel");
1143 /* XXX Should find a way to clean up our memory */
1144 ast_log(LOG_WARNING, "Out of memory allocating line");
1148 ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
1153 ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
1157 if (d->addr.sin_addr.s_addr && !ntohs(d->addr.sin_port))
1158 d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
1159 if (d->addr.sin_addr.s_addr) {
1160 if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
1161 memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
1164 memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
1170 static int has_voicemail(struct skinny_line *l)
1172 return ast_app_has_voicemail(l->mailbox);
1175 static int skinny_register(skinny_req *req, struct skinnysession *s)
1177 struct skinny_device *d;
1179 ast_mutex_lock(&devicelock);
1182 if (!strcasecmp(req->data.reg.name, d->id)) {
1183 /* XXX Deal with IP authentication */
1185 d->type = req->data.reg.type;
1192 ast_mutex_unlock(&devicelock);
1200 static void start_rtp(struct skinny_subchannel *sub)
1202 ast_mutex_lock(&sub->lock);
1203 /* Allocate the RTP */
1204 sub->rtp = ast_rtp_new(sched, io, 1, 0);
1205 if (sub->rtp && sub->owner)
1206 sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
1208 ast_rtp_setnat(sub->rtp, sub->nat);
1210 /* Create the RTP connections */
1211 transmit_connect(sub->parent->parent->session);
1212 ast_mutex_unlock(&sub->lock);
1216 static void *skinny_ss(void *data)
1218 struct ast_channel *chan = data;
1219 struct skinny_subchannel *sub = chan->pvt->pvt;
1220 struct skinny_line *l = sub->parent;
1221 struct skinnysession *s = l->parent->session;
1222 char exten[AST_MAX_EXTENSION] = "";
1224 int timeout = firstdigittimeout;
1228 if (option_verbose > 2)
1229 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, l->parent->name);
1231 while(len < AST_MAX_EXTENSION-1) {
1232 res = ast_waitfordigit(chan, timeout);
1236 printf("Skinny(%s@%s): waitfordigit returned < 0\n", l->name, l->parent->name);
1237 ast_indicate(chan, -1);
1244 if (!ast_ignore_pattern(chan->context, exten)) {
1245 transmit_tone(s, SKINNY_SILENCE);
1247 if (ast_exists_extension(chan, chan->context, exten, 1, l->callerid)) {
1248 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, l->callerid)) {
1250 /* Record this as the forwarding extension */
1251 strncpy(l->call_forward, exten, sizeof(l->call_forward));
1252 if (option_verbose > 2) {
1253 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %s\n",
1254 l->call_forward, chan->name);
1256 transmit_tone(s, SKINNY_DIALTONE);
1260 ast_indicate(chan, -1);
1262 memset(exten, 0, sizeof(exten));
1263 transmit_tone(s, SKINNY_DIALTONE);
1267 strncpy(chan->exten, exten, sizeof(chan->exten)-1);
1268 if (strlen(l->callerid)) {
1269 if (!l->hidecallerid)
1270 chan->callerid = strdup(l->callerid);
1271 chan->ani = strdup(l->callerid);
1273 ast_setstate(chan, AST_STATE_RING);
1274 res = ast_pbx_run(chan);
1276 ast_log(LOG_WARNING, "PBX exited non-zero\n");
1277 transmit_tone(s, SKINNY_REORDER);
1282 /* It's a match, but they just typed a digit, and there is an ambiguous match,
1283 so just set the timeout to matchdigittimeout and wait some more */
1284 timeout = matchdigittimeout;
1286 } else if (res == 0) {
1287 ast_log(LOG_DEBUG, "Not enough digits (and no ambiguous match)...\n");
1288 transmit_tone(s, SKINNY_REORDER);
1291 } else if (l->callwaiting && !strcmp(exten, "*70")) {
1292 if (option_verbose > 2) {
1293 ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
1295 /* Disable call waiting if enabled */
1297 transmit_tone(s, SKINNY_DIALTONE);
1299 memset(exten, 0, sizeof(exten));
1300 timeout = firstdigittimeout;
1302 } else if (!strcmp(exten,ast_pickup_ext())) {
1303 /* Scan all channels and see if any there
1304 * ringing channqels with that have call groups
1305 * that equal this channels pickup group
1307 if (ast_pickup_call(chan)) {
1308 ast_log(LOG_WARNING, "No call pickup possible...\n");
1309 transmit_tone(s, SKINNY_REORDER);
1314 } else if (!l->hidecallerid && !strcmp(exten, "*67")) {
1315 if (option_verbose > 2) {
1316 ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
1318 /* Disable Caller*ID if enabled */
1319 l->hidecallerid = 1;
1321 free(chan->callerid);
1322 chan->callerid = NULL;
1323 transmit_tone(s, SKINNY_DIALTONE);
1325 memset(exten, 0, sizeof(exten));
1326 timeout = firstdigittimeout;
1327 } else if (l->callreturn && !strcmp(exten, "*69")) {
1329 if (strlen(l->lastcallerid)) {
1330 res = ast_say_digit_str(chan, l->lastcallerid, "", chan->language);
1333 transmit_tone(s, SKINNY_DIALTONE);
1336 } else if (!strcmp(exten, "*78")) {
1337 /* Do not disturb */
1338 if (option_verbose > 2) {
1339 ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %s\n", chan->name);
1341 transmit_tone(s, SKINNY_DIALTONE);
1344 memset(exten, 0, sizeof(exten));
1346 } else if (!strcmp(exten, "*79")) {
1347 /* Do not disturb */
1348 if (option_verbose > 2) {
1349 ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %s\n", chan->name);
1351 transmit_tone(s, SKINNY_DIALTONE);
1354 memset(exten, 0, sizeof(exten));
1356 } else if (l->cancallforward && !strcmp(exten, "*72")) {
1357 transmit_tone(s, SKINNY_DIALTONE);
1359 memset(exten, 0, sizeof(exten));
1361 } else if (l->cancallforward && !strcmp(exten, "*73")) {
1362 if (option_verbose > 2) {
1363 ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %s\n", chan->name);
1365 transmit_tone(s, SKINNY_DIALTONE);
1366 memset(l->call_forward, 0, sizeof(l->call_forward));
1368 memset(exten, 0, sizeof(exten));
1370 } else if (!strcmp(exten, ast_parking_ext()) &&
1372 sub->next->owner->bridge) {
1373 /* This is a three way call, the main call being a real channel,
1374 and we're parking the first call. */
1375 ast_masq_park_call(sub->next->owner->bridge, chan, 0, NULL);
1376 if (option_verbose > 2) {
1377 ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
1380 } else if (strlen(l->lastcallerid) && !strcmp(exten, "*80")) {
1381 if (option_verbose > 2) {
1382 ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", l->lastcallerid);
1384 res = ast_db_put("blacklist", l->lastcallerid, "1");
1386 transmit_tone(s, SKINNY_DIALTONE);
1387 memset(exten, 0, sizeof(exten));
1390 } else if (l->hidecallerid && !strcmp(exten, "*82")) {
1391 if (option_verbose > 2) {
1392 ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
1394 /* Enable Caller*ID if enabled */
1395 l->hidecallerid = 0;
1397 free(chan->callerid);
1398 if (strlen(l->callerid))
1399 chan->callerid = strdup(l->callerid);
1400 transmit_tone(s, SKINNY_DIALTONE);
1402 memset(exten, 0, sizeof(exten));
1403 timeout = firstdigittimeout;
1404 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->callerid) &&
1405 ((exten[0] != '*') || (strlen(exten) > 2))) {
1406 ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", exten, chan->callerid ? chan->callerid : "<Unknown Caller>", chan->context);
1407 transmit_tone(s, SKINNY_REORDER);
1408 sleep(3); // hang out for 3 seconds to let congestion play
1412 timeout = gendigittimeout;
1413 if (len && !ast_ignore_pattern(chan->context, exten))
1414 ast_indicate(chan, -1);
1422 static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
1426 struct skinny_line *l;
1427 struct skinny_subchannel *sub;
1428 struct skinnysession *session;
1430 sub = ast->pvt->pvt;
1432 session = l->parent->session;
1434 if (!l->parent->registered) {
1435 ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
1439 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1440 ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast->name);
1445 ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
1449 ast_queue_control(ast, AST_CONTROL_BUSY, 0);
1453 switch (l->hookstate) {
1454 case SKINNY_OFFHOOK:
1455 tone = SKINNY_CALLWAITTONE;
1458 tone = SKINNY_ALERT;
1461 ast_log(LOG_ERROR, "Don't know how to deal with hookstate %d\n", l->hookstate);
1465 transmit_lamp_indication(session, l->instance, SKINNY_LAMP_BLINK);
1466 transmit_ringer_mode(session, SKINNY_RING_INSIDE);
1467 transmit_tone(session, tone);
1468 transmit_callstate(session, l->instance, SKINNY_RINGIN, sub->callid);
1471 // Select the active softkeys
1473 ast_setstate(ast, AST_STATE_RINGING);
1474 ast_queue_control(ast, AST_CONTROL_RINGING, 0);
1477 // sub->cxmode = SKINNY_CX_RECVONLY;
1478 if (l->type == TYPE_LINE) {
1482 /* do we need to anything if there already is an RTP allocated? */
1483 // transmit_modify_request(sub);
1487 if (sub->next->owner && sub->next->callid) {
1488 /* try to prevent a callwait from disturbing the other connection */
1489 sub->next->cxmode = SKINNY_CX_RECVONLY;
1490 // transmit_modify_request(sub->next);
1493 /* not sure what this doing */
1494 if (sub->next->owner && sub->next->callid) {
1495 /* Put the connection back in sendrecv */
1496 sub->next->cxmode = SKINNY_CX_SENDRECV;
1497 // transmit_modify_request(sub->next);
1502 ast_log(LOG_NOTICE, "Don't know how to dial on trunks yet\n");
1509 static int skinny_hangup(struct ast_channel *ast)
1511 struct skinny_subchannel *sub = ast->pvt->pvt;
1512 struct skinny_line *l = sub->parent;
1513 struct skinnysession *s = l->parent->session;
1516 ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
1518 if (!ast->pvt->pvt) {
1519 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
1523 if (l->parent->registered) {
1524 if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_OFFHOOK)) {
1525 sub->parent->hookstate = SKINNY_ONHOOK;
1526 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
1527 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
1528 } else if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_ONHOOK)) {
1529 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
1530 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
1531 transmit_ringer_mode(s, SKINNY_RING_OFF);
1532 transmit_tone(s, SKINNY_SILENCE);
1535 ast_mutex_lock(&sub->lock);
1537 ast->pvt->pvt = NULL;
1538 sub->alreadygone = 0;
1540 sub->cxmode = SKINNY_CX_INACTIVE;
1542 ast_rtp_destroy(sub->rtp);
1545 ast_mutex_unlock(&sub->lock);
1549 static int skinny_answer(struct ast_channel *ast)
1552 struct skinny_subchannel *sub = ast->pvt->pvt;
1553 struct skinny_line *l = sub->parent;
1554 sub->cxmode = SKINNY_CX_SENDRECV;
1558 // transmit_modify_request(sub);
1560 ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, l->parent->name, sub->callid);
1561 if (ast->_state != AST_STATE_UP) {
1562 ast_setstate(ast, AST_STATE_UP);
1564 // transmit_notify_request(sub, "");
1565 // transmit_modify_request(sub);
1570 static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
1572 /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
1573 struct ast_frame *f;
1574 f = ast_rtp_read(sub->rtp);
1576 /* We already hold the channel lock */
1577 if (f->frametype == AST_FRAME_VOICE) {
1578 if (f->subclass != sub->owner->nativeformats) {
1579 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
1580 sub->owner->nativeformats = f->subclass;
1581 ast_set_read_format(sub->owner, sub->owner->readformat);
1582 ast_set_write_format(sub->owner, sub->owner->writeformat);
1590 static struct ast_frame *skinny_read(struct ast_channel *ast)
1592 struct ast_frame *fr;
1593 struct skinny_subchannel *sub = ast->pvt->pvt;
1594 ast_mutex_lock(&sub->lock);
1595 fr = skinny_rtp_read(sub);
1596 ast_mutex_unlock(&sub->lock);
1600 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
1602 struct skinny_subchannel *sub = ast->pvt->pvt;
1604 if (frame->frametype != AST_FRAME_VOICE) {
1605 if (frame->frametype == AST_FRAME_IMAGE)
1608 ast_log(LOG_WARNING, "Can't send %d type frames with SKINNY write\n", frame->frametype);
1612 if (!(frame->subclass & ast->nativeformats)) {
1613 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
1614 frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
1619 ast_mutex_lock(&sub->lock);
1621 res = ast_rtp_write(sub->rtp, frame);
1623 ast_mutex_unlock(&sub->lock);
1628 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1630 struct skinny_subchannel *sub = newchan->pvt->pvt;
1631 ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", oldchan->name, newchan->name);
1632 if (sub->owner != oldchan) {
1633 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
1636 sub->owner = newchan;
1640 static int skinny_senddigit(struct ast_channel *ast, char digit)
1642 // struct skinny_subchannel *sub = ast->pvt->pvt;
1646 // transmit_notify_request(sub, tmp);
1650 static char *control2str(int ind) {
1652 case AST_CONTROL_HANGUP:
1653 return "Other end has hungup";
1654 case AST_CONTROL_RING:
1655 return "Local ring";
1656 case AST_CONTROL_RINGING:
1657 return "Remote end is ringing";
1658 case AST_CONTROL_ANSWER:
1659 return "Remote end has answered";
1660 case AST_CONTROL_BUSY:
1661 return "Remote end is busy";
1662 case AST_CONTROL_TAKEOFFHOOK:
1663 return "Make it go off hook";
1664 case AST_CONTROL_OFFHOOK:
1665 return "Line is off hook";
1666 case AST_CONTROL_CONGESTION:
1667 return "Congestion (circuits busy)";
1668 case AST_CONTROL_FLASH:
1669 return "Flash hook";
1670 case AST_CONTROL_WINK:
1672 case AST_CONTROL_OPTION:
1673 return "Set a low-level option";
1674 case AST_CONTROL_RADIO_KEY:
1676 case AST_CONTROL_RADIO_UNKEY:
1677 return "Un-Key Radio";
1685 static int skinny_indicate(struct ast_channel *ast, int ind)
1687 struct skinny_subchannel *sub = ast->pvt->pvt;
1688 struct skinny_line *l = sub->parent;
1689 struct skinnysession *s = l->parent->session;
1692 ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n", control2str(ind), ast->name);
1695 case AST_CONTROL_RINGING:
1696 transmit_tone(s, SKINNY_ALERT);
1697 transmit_callstate(s, l->instance, SKINNY_PROGRESS, sub->callid);
1699 case AST_CONTROL_BUSY:
1700 transmit_tone(s, SKINNY_BUSYTONE);
1701 transmit_callstate(s, l->instance, SKINNY_BUSY, sub->callid);
1703 case AST_CONTROL_CONGESTION:
1704 transmit_tone(s, SKINNY_REORDER);
1705 transmit_callstate(s, l->instance, SKINNY_CONGESTION, sub->callid);
1708 transmit_tone(s, SKINNY_SILENCE);
1711 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
1717 static struct ast_channel *skinny_new(struct skinny_subchannel *sub, int state)
1719 struct ast_channel *tmp;
1720 struct skinny_line *l = sub->parent;
1723 tmp = ast_channel_alloc(1);
1725 tmp->nativeformats = l->capability;
1726 if (!tmp->nativeformats)
1727 tmp->nativeformats = capability;
1728 fmt = ast_best_codec(tmp->nativeformats);
1729 snprintf(tmp->name, sizeof(tmp->name), "Skinny/%s@%s-%d", l->name, l->parent->name, sub->callid);
1731 tmp->fds[0] = ast_rtp_fd(sub->rtp);
1733 ast_setstate(tmp, state);
1734 if (state == AST_STATE_RING)
1736 tmp->writeformat = fmt;
1737 tmp->pvt->rawwriteformat = fmt;
1738 tmp->readformat = fmt;
1739 tmp->pvt->rawreadformat = fmt;
1740 tmp->pvt->pvt = sub;
1741 tmp->pvt->call = skinny_call;
1742 tmp->pvt->hangup = skinny_hangup;
1743 tmp->pvt->answer = skinny_answer;
1744 tmp->pvt->read = skinny_read;
1745 tmp->pvt->write = skinny_write;
1746 tmp->pvt->indicate = skinny_indicate;
1747 tmp->pvt->fixup = skinny_fixup;
1748 tmp->pvt->send_digit = skinny_senddigit;
1749 // tmp->pvt->bridge = ast_rtp_bridge;
1750 if (strlen(l->language))
1751 strncpy(tmp->language, l->language, sizeof(tmp->language)-1);
1752 if (strlen(l->accountcode))
1753 strncpy(tmp->accountcode, l->accountcode, sizeof(tmp->accountcode)-1);
1755 tmp->amaflags = l->amaflags;
1757 ast_mutex_lock(&usecnt_lock);
1759 ast_mutex_unlock(&usecnt_lock);
1760 ast_update_use_count();
1761 tmp->callgroup = l->callgroup;
1762 tmp->pickupgroup = l->pickupgroup;
1763 strncpy(tmp->call_forward, l->call_forward, sizeof(tmp->call_forward));
1764 strncpy(tmp->context, l->context, sizeof(tmp->context)-1);
1765 strncpy(tmp->exten,l->exten, sizeof(tmp->exten)-1);
1766 if (strlen(l->callerid))
1767 tmp->callerid = strdup(l->callerid);
1769 if (state != AST_STATE_DOWN) {
1770 if (ast_pbx_start(tmp)) {
1771 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
1777 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1783 static int handle_message(skinny_req *req, struct skinnysession *s)
1785 struct skinny_subchannel *sub;
1786 struct ast_channel *c;
1787 struct ast_frame f = { 0, };
1788 struct sockaddr_in sin;
1789 struct sockaddr_in us;
1790 struct skinny_line *lines;
1799 int stimulusInstance;
1807 if ( (!s->device) && (req->e != REGISTER_MESSAGE && req->e != ALARM_MESSAGE)) {
1808 ast_log(LOG_WARNING, "Client sent message #%d without first registering.\n", req->e);
1817 printf("Recieved AlarmMessage\n");
1819 case REGISTER_MESSAGE:
1821 printf("Device %s is attempting to register\n", req->data.reg.name);
1822 res = skinny_register(req, s);
1824 ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", req->data.reg.name);
1825 memcpy(&name, req->data.reg.name, sizeof(req->data.reg.name));
1826 memset(req, 0, sizeof(skinny_req));
1827 req->len = sizeof(register_rej_message)+4;
1828 req->e = REGISTER_REJ_MESSAGE;
1829 sprintf(req->data.regrej.errMsg, "No Authority: %s", name);
1830 transmit_response(s, req);
1833 if (option_verbose > 2)
1834 ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfuly registered\n", s->device->name);
1835 memset(req, 0, SKINNY_MAX_PACKET);
1836 req->len = sizeof(register_ack_message)+4;
1837 req->e = REGISTER_ACK_MESSAGE;
1838 strcpy(req->data.regack.res, "0");
1839 req->data.regack.keepAlive = keep_alive;
1840 strcpy(req->data.regack.dateTemplate, date_format);
1841 strcpy(req->data.regack.res2, "0");
1842 req->data.regack.secondaryKeepAlive = keep_alive;
1843 transmit_response(s, req);
1845 printf ("Requesting capabilities\n");
1846 memset(req, 0, SKINNY_MAX_PACKET);
1848 req->e = CAPABILITIES_REQ_MESSAGE;
1849 transmit_response(s, req);
1851 case UNREGISTER_MESSAGE:
1852 /* XXX Acutally unregister the device */
1854 case IP_PORT_MESSAGE:
1856 case STIMULUS_MESSAGE:
1857 stimulus = req->data.stimulus.stimulus;
1858 stimulusInstance = req->data.stimulus.stimulusInstance;
1861 case STIMULUS_REDIAL:
1862 // XXX how we gonna deal with redial ?!?!
1864 printf("Recieved Stimulus: Redial\n");
1867 case STIMULUS_SPEEDDIAL:
1870 printf("Recieved Stimulus: SpeedDial\n");
1875 printf("Recieved Stimulus: Hold\n");
1877 case STIMULUS_TRANSFER:
1879 printf("Recieved Stimulus: Transfer\n");
1881 case STIMULUS_FORWARDALL:
1882 case STIMULUS_FORWARDBUSY:
1883 case STIMULUS_FORWARDNOANSWER:
1886 printf("Recieved Stimulus: Forward (%d)\n", stimulus);
1888 case STIMULUS_DISPLAY:
1891 printf("Recieved Stimulus: Display\n");
1895 printf("Recieved Stimulus: Line\n");
1897 sub = find_subchannel_by_line(s->device->lines);
1898 transmit_speaker_mode(s, 1); // Turn on
1901 printf("RECEIVED UNKNOWN STIMULUS: %d\n", stimulus);
1907 case VERSION_REQ_MESSAGE:
1909 printf ("Version Request\n");
1910 memset(req, 0, SKINNY_MAX_PACKET);
1911 req->len = sizeof(version_res_message)+4;
1912 req->e = VERSION_RES_MESSAGE;
1913 sprintf(req->data.version.version, version_id);
1914 transmit_response(s, req);
1916 case SERVER_REQUEST_MESSAGE:
1918 printf ("Recieved Server Request\n");
1919 memset(req, 0, SKINNY_MAX_PACKET);
1920 req->len = sizeof(server_res_message)+4;
1921 req->e = SERVER_RES_MESSAGE;
1922 memcpy(req->data.serverres.server[0].serverName, ourhost,
1923 sizeof(req->data.serverres.server[0].serverName));
1924 req->data.serverres.serverListenPort[0] = ourport;
1925 req->data.serverres.serverIpAddr[0] = __ourip.s_addr;
1926 transmit_response(s, req);
1928 case BUTTON_TEMPLATE_REQ_MESSAGE:
1930 printf ("Buttontemplate requested\n");
1931 memset(req, 0, SKINNY_MAX_PACKET);
1932 req->len = sizeof(button_template_res_message)+4;
1933 req->e = BUTTON_TEMPLATE_RES_MESSAGE;
1934 req->data.buttontemplate.buttonOffset = 0;
1935 req->data.buttontemplate.buttonCount = 10;
1936 req->data.buttontemplate.totalButtonCount = 10;
1937 /* XXX Figure out how to do this correctly */
1938 memcpy(req->data.buttontemplate.definition,
1939 button_definition_hack,
1940 sizeof(req->data.buttontemplate.definition));
1941 transmit_response(s, req);
1943 case SOFT_KEY_SET_REQ_MESSAGE:
1945 printf ("Received SoftKeySetReq\n");
1946 memset(req, 0, SKINNY_MAX_PACKET);
1947 req->len = sizeof(soft_key_sets)+4;
1948 req->e = SOFT_KEY_SET_RES_MESSAGE;
1949 req->data.softkeysets.softKeySetOffset = 0;
1950 req->data.softkeysets.softKeySetCount = 11;
1951 req->data.softkeysets.totalSoftKeySetCount = 11;
1952 /* XXX Wicked hack XXX */
1953 memcpy(req->data.softkeysets.softKeySetDefinition,
1955 sizeof(req->data.softkeysets.softKeySetDefinition));
1956 transmit_response(s,req);
1958 case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
1960 printf ("Recieved SoftKey Template Request\n");
1961 memset(req, 0, SKINNY_MAX_PACKET);
1962 req->len = sizeof(soft_key_template)+4;
1963 req->e = SOFT_KEY_TEMPLATE_RES_MESSAGE;
1964 req->data.softkeytemplate.softKeyOffset = 0;
1965 req->data.softkeytemplate.softKeyCount = 21;
1966 req->data.softkeytemplate.totalSoftKeyCount = 21;
1967 /* XXX Another wicked hack XXX */
1968 memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
1969 soft_key_template_hack,
1970 sizeof(req->data.softkeytemplate.softKeyTemplateDefinition));
1971 transmit_response(s,req);
1973 case TIME_DATE_REQ_MESSAGE:
1975 printf ("Received Time/Date Request\n");
1976 memset(req, 0, SKINNY_MAX_PACKET);
1977 req->len = sizeof(definetimedate_message)+4;
1978 req->e = DEFINETIMEDATE_MESSAGE;
1980 cmtime = localtime(&timer);
1981 req->data.definetimedate.year = cmtime->tm_year+1900;
1982 req->data.definetimedate.month = cmtime->tm_mon+1;
1983 req->data.definetimedate.dayofweek = cmtime->tm_wday;
1984 req->data.definetimedate.day = cmtime->tm_mday;
1985 req->data.definetimedate.hour = cmtime->tm_hour;
1986 req->data.definetimedate.minute = cmtime->tm_min;
1987 req->data.definetimedate.seconds = cmtime->tm_sec;
1988 transmit_response(s, req);
1990 case SPEED_DIAL_STAT_REQ_MESSAGE:
1991 /* Not really sure how Speed Dial's are different than the
1992 Softkey templates */
1993 speedDialNum = req->data.speeddialreq.speedDialNumber;
1995 // printf ("Recieved SpeedDialStatReq: %d\n", speedDialNum);
1996 memset(req, 0, SKINNY_MAX_PACKET);
1997 req->len = sizeof(speed_dial_stat_res_message)+4;
1998 req->e = SPEED_DIAL_STAT_RES_MESSAGE;
2000 /* XXX Do this right XXX */
2001 req->data.speeddialreq.speedDialNumber = speedDialNum;
2002 sprintf(req->data.speeddial.speedDialDirNumber, "31337");
2003 sprintf(req->data.speeddial.speedDialDisplayName, "Asterisk Rules!");
2005 transmit_response(s, req);
2007 case LINE_STATE_REQ_MESSAGE:
2008 lineNumber = req->data.line.lineNumber;
2010 printf ("Received LineStateReq\n");
2011 memset(req, 0, SKINNY_MAX_PACKET);
2012 req->len = sizeof(line_stat_res_message)+4;
2013 req->e = LINE_STAT_RES_MESSAGE;
2014 sub = find_subchannel_by_line(s->device->lines);
2016 ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
2019 lines = sub->parent;
2020 ast_mutex_lock(&devicelock);
2021 for (i=1; i < lineNumber; i++) {
2022 lines = lines->next;
2024 ast_mutex_unlock(&devicelock);
2025 req->data.linestat.linenumber = lineNumber;
2026 memcpy(req->data.linestat.lineDirNumber, lines->name,
2027 sizeof(req->data.linestat.lineDirNumber));
2028 memcpy(req->data.linestat.lineDisplayName, lines->label,
2029 sizeof(req->data.linestat.lineDisplayName));
2030 transmit_response(s,req);
2032 case CAPABILITIES_RES_MESSAGE:
2034 printf ("Received CapabilitiesRes\n");
2035 // zien wat ie kan en verwerken (handshake)
2037 case KEEP_ALIVE_MESSAGE:
2038 memset(req, 0, SKINNY_MAX_PACKET);
2040 req->e = KEEP_ALIVE_ACK_MESSAGE;
2041 transmit_response(s, req);
2043 case OFFHOOK_MESSAGE:
2044 transmit_ringer_mode(s,SKINNY_RING_OFF);
2045 transmit_lamp_indication(s, s->device->lines->instance, SKINNY_LAMP_ON);
2047 sub = find_subchannel_by_line(s->device->lines);
2049 ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
2052 sub->parent->hookstate = SKINNY_OFFHOOK;
2054 if (sub->outgoing) {
2055 transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
2056 transmit_tone(s, SKINNY_SILENCE);
2057 ast_setstate(sub->owner, AST_STATE_UP);
2062 transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
2063 transmit_tone(s, SKINNY_DIALTONE);
2064 c = skinny_new(sub, AST_STATE_DOWN);
2067 if (pthread_create(&t, NULL, skinny_ss, c)) {
2068 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
2072 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", sub->parent->name, s->device->name);
2076 ast_log(LOG_DEBUG, "Current sub [%s] already has owner\n", sub->owner->name);
2080 case ONHOOK_MESSAGE:
2081 sub = find_subchannel_by_line(s->device->lines);
2082 if (sub->parent->hookstate == SKINNY_ONHOOK) {
2083 /* Somthing else already put us back on hook */
2086 sub->cxmode = SKINNY_CX_RECVONLY;
2087 sub->parent->hookstate = SKINNY_ONHOOK;
2088 transmit_callstate(s, s->device->lines->instance, sub->parent->hookstate,sub->callid);
2090 printf("Skinny %s@%s went on hook\n",sub->parent->name, sub->parent->parent->name);
2092 // transmit_modify_request(sub);
2094 if (sub->parent->transfer && (sub->owner && sub->next->owner) && ((!sub->outgoing) || (!sub->next->outgoing))) {
2095 /* We're allowed to transfer, we have two active calls and */
2096 /* we made at least one of the calls. Let's try and transfer */
2098 if ((res = attempt_transfer(p)) < 0) {
2099 if (p->sub->next->owner) {
2100 sub->next->alreadygone = 1;
2101 ast_queue_hangup(sub->next->owner,1);
2104 ast_log(LOG_WARNING, "Transfer attempt failed\n");
2109 /* Hangup the current call */
2110 /* If there is another active call, skinny_hangup will ring the phone with the other call */
2112 sub->alreadygone = 1;
2113 ast_queue_hangup(sub->owner, 1);
2115 ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
2116 sub->parent->name, sub->parent->parent->name, sub->callid);
2119 if ((sub->parent->hookstate == SKINNY_ONHOOK) && (!sub->rtp) && (!sub->next->rtp)) {
2120 if (has_voicemail(sub->parent)) {
2121 transmit_lamp_indication(s, s->device->lines->instance, SKINNY_LAMP_FLASH);
2123 transmit_lamp_indication(s, s->device->lines->instance, SKINNY_LAMP_OFF);
2127 case KEYPAD_BUTTON_MESSAGE:
2128 digit = req->data.keypad.button;
2130 printf("Collected digit: [%d]\n", digit);
2132 f.frametype = AST_FRAME_DTMF;
2135 } else if (digit == 15) {
2138 sprintf(&d, "%d", digit);
2142 sub = find_subchannel_by_line(s->device->lines);
2145 /* XXX MUST queue this frame to all subs in threeway call if threeway call is active */
2146 ast_queue_frame(sub->owner, &f, 1);
2147 if (sub->next->owner) {
2148 ast_queue_frame(sub->next->owner, &f, 1);
2151 printf("No owner: %s\n", s->device->lines->name);
2154 case OPEN_RECIEVE_CHANNEL_ACK_MESSAGE:
2155 printf("Recieved Open Recieve Channel Ack\n");
2156 status = req->data.openrecievechannelack.status;
2158 ast_log(LOG_ERROR, "Open Recieve Channel Failure\n");
2161 memcpy(addr, req->data.openrecievechannelack.ipAddr, sizeof(addr));
2162 port = req->data.openrecievechannelack.port;
2164 sin.sin_family = AF_INET;
2165 memcpy(&sin.sin_addr, addr, sizeof(sin.sin_addr)); // Endian?
2166 sin.sin_port = htons(port);
2168 sub = find_subchannel_by_line(s->device->lines);
2169 ast_rtp_set_peer(sub->rtp, &sin);
2170 ast_rtp_get_us(sub->rtp, &us);
2172 printf("us port: %d\n", ntohs(us.sin_port));
2173 printf("sin port: %d\n", ntohs(sin.sin_port));
2176 memset(req, 0, SKINNY_MAX_PACKET);
2177 req->len = sizeof(start_media_transmission_message)+4;
2178 req->e = START_MEDIA_TRANSMISSION_MESSAGE;
2179 req->data.startmedia.conferenceId = 0;
2180 req->data.startmedia.passThruPartyId = 0;
2181 memcpy(req->data.startmedia.remoteIp, &s->device->ourip, 4); // Endian?
2182 req->data.startmedia.remotePort = ntohs(us.sin_port);
2183 req->data.startmedia.packetSize = 20;
2184 req->data.startmedia.payloadType = convert_cap(s->device->lines->capability);
2185 req->data.startmedia.qualifier.precedence = 127;
2186 req->data.startmedia.qualifier.vad = 0;
2187 req->data.startmedia.qualifier.packets = 0;
2188 req->data.startmedia.qualifier.bitRate = 0;
2189 transmit_response(s, req);
2192 printf("RECEIVED UNKNOWN MESSAGE TYPE: %x\n", req->e);
2201 static void destroy_session(struct skinnysession *s)
2203 struct skinnysession *cur, *prev = NULL;
2204 ast_mutex_lock(&sessionlock);
2214 prev->next = cur->next;
2216 sessions = cur->next;
2221 ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
2222 ast_mutex_unlock(&sessionlock);
2225 static int get_input(struct skinnysession *s)
2233 FD_SET(s->fd, &fds);
2235 res = ast_select(s->fd + 1, &fds, NULL, NULL, NULL);
2238 ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
2239 } else if (res > 0) {
2240 memset(s->inbuf,0,sizeof(s->inbuf));
2241 res = read(s->fd, s->inbuf, 4);
2243 ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
2246 dlen = *(int *)s->inbuf;
2247 res = read(s->fd, s->inbuf+4, dlen+4);
2248 ast_mutex_unlock(&s->lock);
2249 if (res != (dlen+4)) {
2250 ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
2259 static skinny_req *skinny_req_parse(struct skinnysession *s)
2263 req = malloc(SKINNY_MAX_PACKET);
2265 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
2268 memset(req, 0, sizeof(skinny_req));
2269 memcpy(req, s->inbuf, *(int*)(s->inbuf)+8); // +8
2271 ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
2278 static void *skinny_session(void *data)
2282 struct skinnysession *s = data;
2284 ast_verbose(VERBOSE_PREFIX_3 "Starting Skinny session from %s\n", inet_ntoa(s->sin.sin_addr));
2291 req = skinny_req_parse(s);
2295 res = handle_message(req, s);
2301 ast_log(LOG_NOTICE, "Skinny Session returned: %s\n", strerror(errno));
2306 static void *accept_thread(void *ignore)
2309 struct sockaddr_in sin;
2311 struct skinnysession *s;
2314 pthread_attr_t attr;
2316 pthread_attr_init(&attr);
2317 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2320 sinlen = sizeof(sin);
2321 as = accept(skinnysock, &sin, &sinlen);
2323 ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
2326 p = getprotobyname("tcp");
2328 if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
2329 ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
2332 s = malloc(sizeof(struct skinnysession));
2334 ast_log(LOG_WARNING, "Failed to allocate Skinny session: %s\n", strerror(errno));
2337 memset(s, 0, sizeof(struct skinnysession));
2338 memcpy(&s->sin, &sin, sizeof(sin));
2339 ast_mutex_init(&s->lock);
2341 ast_mutex_lock(&sessionlock);
2344 ast_mutex_unlock(&sessionlock);
2346 if (pthread_create(&tcp_thread, NULL, skinny_session, s))
2352 printf("killing accept thread\n");
2358 static void *do_monitor(void *data)
2363 /* Add an I/O event to our TCP socket */
2364 if (skinnysock > -1) {
2365 ast_io_add(io, skinnysock, accept_thread, AST_IO_IN, NULL);
2367 ast_log(LOG_WARNING, "Unable to create I/O socket event\n");
2372 /* This thread monitors all the interfaces which are not yet in use
2373 (and thus do not have a separate thread) indefinitely */
2374 /* From here on out, we die whenever asked */
2376 pthread_testcancel();
2377 /* Wait for sched or io */
2378 res = ast_sched_wait(sched);
2379 if ((res < 0) || (res > 1000))
2381 res = ast_io_wait(io, res);
2382 ast_mutex_lock(&monlock);
2384 ast_sched_runq(sched);
2385 ast_mutex_unlock(&monlock);
2392 static int restart_monitor(void)
2395 /* If we're supposed to be stopped -- stay stopped */
2396 if (monitor_thread == -2)
2398 if (ast_mutex_lock(&monlock)) {
2399 ast_log(LOG_WARNING, "Unable to lock monitor\n");
2402 if (monitor_thread == pthread_self()) {
2403 ast_mutex_unlock(&monlock);
2404 ast_log(LOG_WARNING, "Cannot kill myself\n");
2407 if (monitor_thread) {
2408 /* Wake up the thread */
2409 pthread_kill(monitor_thread, SIGURG);
2411 /* Start a new monitor */
2412 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
2413 ast_mutex_unlock(&monlock);
2414 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2418 ast_mutex_unlock(&monlock);
2422 static struct ast_channel *skinny_request(char *type, int format, void *data)
2425 struct skinny_subchannel *sub;
2426 struct ast_channel *tmpc = NULL;
2431 format &= capability;
2433 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
2437 strncpy(tmp, dest, sizeof(tmp) - 1);
2439 ast_log(LOG_NOTICE, "Skinny channels require a device\n");
2443 sub = find_subchannel_by_name(tmp);
2445 ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
2449 if (option_verbose > 2) {
2450 ast_verbose(VERBOSE_PREFIX_3 "skinny_request(%s)\n", tmp);
2451 ast_verbose(VERBOSE_PREFIX_3 "Skinny cw: %d, dnd: %d, so: %d, sno: %d\n",
2452 sub->parent->callwaiting, sub->parent->dnd, sub->owner ? 1 : 0, sub->next->owner ? 1: 0);
2455 tmpc = skinny_new(sub->owner ? sub->next : sub, AST_STATE_DOWN);
2457 ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
2464 static int reload_config(void)
2467 struct ast_config *cfg;
2468 struct ast_variable *v;
2471 struct skinny_device *d;
2472 int oldport = ntohs(bindaddr.sin_port);
2474 if (gethostname(ourhost, sizeof(ourhost))) {
2475 ast_log(LOG_WARNING, "Unable to get hostname, Skinny disabled\n");
2478 cfg = ast_load(config);
2480 /* We *must* have a config file otherwise stop immediately */
2482 ast_log(LOG_NOTICE, "Unable to load config %s, Skinny disabled\n", config);
2486 /* load the general section */
2487 memset(&bindaddr, 0, sizeof(bindaddr));
2488 v = ast_variable_browse(cfg, "general");
2490 /* Create the interface list */
2491 if (!strcasecmp(v->name, "bindaddr")) {
2492 if (!(hp = gethostbyname(v->value))) {
2493 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2495 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2497 } else if (!strcasecmp(v->name, "keepAlive")) {
2498 keep_alive = atoi(v->value);
2499 } else if (!strcasecmp(v->name, "dateFormat")) {
2500 strncpy(date_format, v->value, sizeof(date_format) - 1);
2501 } else if (!strcasecmp(v->name, "versionId")) {
2502 strncpy(version_id, v->value, sizeof(version_id) - 1);
2503 } else if (!strcasecmp(v->name, "allow")) {
2504 format = ast_getformatbyname(v->value);
2506 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
2508 capability |= format;
2509 } else if (!strcasecmp(v->name, "disallow")) {
2510 format = ast_getformatbyname(v->value);
2512 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
2514 capability &= ~format;
2515 } else if (!strcasecmp(v->name, "port")) {
2516 if (sscanf(v->value, "%i", &ourport) == 1) {
2517 bindaddr.sin_port = htons(ourport);
2519 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
2525 if (ntohl(bindaddr.sin_addr.s_addr)) {
2526 memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
2528 hp = gethostbyname(ourhost);
2530 ast_log(LOG_WARNING, "Unable to get our IP address, Skinny disabled\n");
2533 memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
2535 if (!ntohs(bindaddr.sin_port))
2536 bindaddr.sin_port = ntohs(DEFAULT_SKINNY_PORT);
2537 bindaddr.sin_family = AF_INET;
2539 /* load the device sections */
2540 cat = ast_category_browse(cfg, NULL);
2542 if (strcasecmp(cat, "general")) {
2543 d = build_device(cat, ast_variable_browse(cfg, cat));
2545 if (option_verbose > 2) {
2546 ast_verbose(VERBOSE_PREFIX_3 "Added device '%s'\n", d->name);
2548 ast_mutex_lock(&devicelock);
2551 ast_mutex_unlock(&devicelock);
2554 cat = ast_category_browse(cfg, cat);
2556 ast_mutex_lock(&netlock);
2557 if ((skinnysock > -1) && (ntohs(bindaddr.sin_port) != oldport)) {
2561 if (skinnysock < 0) {
2562 skinnysock = socket(AF_INET, SOCK_STREAM, 0);
2563 if(setsockopt(skinnysock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
2564 ast_log(LOG_ERROR, "Set Socket Options failed: errno %d, %s", errno, strerror(errno));
2568 if (skinnysock < 0) {
2569 ast_log(LOG_WARNING, "Unable to create Skinny socket: %s\n", strerror(errno));
2571 if (bind(skinnysock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
2572 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
2573 inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
2580 if (listen(skinnysock,DEFAULT_SKINNY_BACKLOG)) {
2581 ast_log(LOG_WARNING, "Failed to start listening to %s:%d: %s\n",
2582 inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port),
2589 if (option_verbose > 1)
2590 ast_verbose(VERBOSE_PREFIX_2 "Skinny listening on %s:%d\n",
2591 inet_ntoa(bindaddr.sin_addr), ntohs(bindaddr.sin_port));
2593 pthread_create(&accept_t,NULL, accept_thread, NULL);
2597 ast_mutex_unlock(&netlock);
2600 /* and unload the configuration when were done */
2606 void delete_devices(void)
2608 struct skinny_device *d, *dlast;
2609 struct skinny_line *l, *llast;
2610 struct skinny_subchannel *sub, *slast;
2612 ast_mutex_lock(&devicelock);
2614 /* Delete all devices */
2615 for (d=devices;d;) {
2617 /* Delete all lines for this device */
2618 for (l=d->lines;l;) {
2619 /* Delete all subchannels for this line */
2620 for (sub=l->sub;sub;) {
2634 ast_mutex_unlock(&devicelock);
2654 /* load and parse config */
2655 res = reload_config();
2657 /* Announce our presence to Asterisk */
2659 /* Make sure we can register our skinny channel type */
2660 if (ast_channel_register(type, tdesc, capability, skinny_request)) {
2661 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
2665 skinny_rtp.type = type;
2666 ast_rtp_proto_register(&skinny_rtp);
2667 ast_cli_register(&cli_show_lines);
2668 ast_cli_register(&cli_debug);
2669 ast_cli_register(&cli_no_debug);
2670 sched = sched_context_create();
2672 ast_log(LOG_WARNING, "Unable to create schedule context\n");
2674 io = io_context_create();
2676 ast_log(LOG_WARNING, "Unable to create I/O context\n");
2678 /* And start the monitor for the first time */
2687 struct skinny_subchannel *p, *pl;
2689 ast_channel_unregister(type);
2690 ast_verbose("Skinny module unloading\n");
2692 /* First, take us out of the channel loop */
2693 ast_channel_unregister(type);
2695 /* close all IP connections */
2696 if (!ast_mutex_lock(&devicelock)) {
2697 /* Hangup all interfaces if they have an owner */
2701 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
2705 ast_mutex_unlock(&iflock);
2707 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
2710 if (!ast_mutex_lock(&monlock)) {
2711 if (monitor_thread) {
2712 pthread_cancel(monitor_thread);
2713 pthread_kill(monitor_thread, SIGURG);
2714 pthread_join(monitor_thread, NULL);
2716 monitor_thread = -2;
2717 ast_mutex_unlock(&monlock);
2719 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
2723 if (!ast_mutex_lock(&iflock)) {
2724 /* Destroy all the interfaces and free their memory */
2729 /* Free associated memory */
2733 ast_mutex_unlock(&iflock);
2735 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
2739 /* if we're still here something was fscked up */
2746 ast_mutex_lock(&usecnt_lock);
2748 ast_mutex_unlock(&usecnt_lock);
2754 return ASTERISK_GPL_KEY;