2 * Asterisk -- A telephony toolkit for Linux.
4 * Implementation of the Skinny protocol
6 * Copyright (C) 1999 - 2005 Digium, inc
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
19 #include <asterisk/lock.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/config.h>
22 #include <asterisk/logger.h>
23 #include <asterisk/module.h>
24 #include <asterisk/pbx.h>
25 #include <asterisk/options.h>
26 #include <asterisk/lock.h>
27 #include <asterisk/sched.h>
28 #include <asterisk/io.h>
29 #include <asterisk/rtp.h>
30 #include <asterisk/acl.h>
31 #include <asterisk/callerid.h>
32 #include <asterisk/cli.h>
33 #include <asterisk/say.h>
34 #include <asterisk/cdr.h>
35 #include <asterisk/astdb.h>
36 #include <asterisk/features.h>
37 #include <asterisk/app.h>
38 #include <asterisk/musiconhold.h>
39 #include <asterisk/utils.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
43 #include <sys/ioctl.h>
49 #include <arpa/inet.h>
50 #include <sys/signal.h>
52 #include <asterisk/dsp.h>
55 /************************************************************************************/
56 /* Skinny/Asterisk Protocol Settings */
57 /************************************************************************************/
58 static const char desc[] = "Skinny Client Control Protocol (Skinny)";
59 static const char tdesc[] = "Skinny Client Control Protocol (Skinny)";
60 static const char type[] = "Skinny";
61 static const 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
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 STOP_MEDIA_TRANSMISSION_MESSAGE 0x008B
207 typedef struct stop_media_transmission_message {
210 } stop_media_transmission_message;
212 #define CALL_INFO_MESSAGE 0x008F
213 typedef struct call_info_message {
214 char callingPartyName[40];
215 char callingParty[24];
216 char calledPartyName[40];
217 char calledParty[24];
221 char originalCalledPartyName[40];
222 char originalCalledParty[24];
225 #define SPEED_DIAL_STAT_RES_MESSAGE 0x0091
226 typedef struct speed_dial_stat_res_message {
228 char speedDialDirNumber[24];
229 char speedDialDisplayName[40];
230 } speed_dial_stat_res_message;
232 #define LINE_STAT_RES_MESSAGE 0x0092
233 typedef struct line_stat_res_message {
235 char lineDirNumber[24];
236 char lineDisplayName[42];
238 } line_stat_res_message;
240 #define DEFINETIMEDATE_MESSAGE 0x0094
241 typedef struct definetimedate_message {
242 int year; /* since 1900 */
244 int dayofweek; /* monday = 1 */
251 } definetimedate_message;
253 #define DISPLAYTEXT_MESSAGE 0x0099
254 typedef struct displaytext_message {
256 } displaytext_message;
258 #define CLEAR_DISPLAY_MESSAGE 0x009A
260 #define REGISTER_REJ_MESSAGE 0x009D
261 typedef struct register_rej_message {
263 } register_rej_message;
265 #define CAPABILITIES_REQ_MESSAGE 0x009B
267 #define SERVER_RES_MESSAGE 0x009E
268 typedef struct server_identifier {
272 typedef struct server_res_message {
273 server_identifier server[5];
274 int serverListenPort[5];
276 } server_res_message;
278 #define BUTTON_TEMPLATE_RES_MESSAGE 0x0097
280 Define the template for a 30VIP phone, this is the older Selsius systems
281 model that has 30 buttons for your button-pushing enjoyment. Each
282 softbutton defition goes by a 'index' of a button followed by that buttons
283 assignment for example "\x01\x09" means Line 1, while "\x02\x09" means Line 2
284 comments on what each assignment means is below
286 static const char *thirtyvip_button_definition_hack = {
287 "\x01\x09" /* Line 1 */
288 "\x02\x09" /* Line 2 */
289 "\x03\x09" /* Line 3 */
290 "\x04\x09" /* Line 4 */
292 "\x01\x01" /* Redial */
294 "\x01\x02" /* Speeddial 1 */
295 "\x02\x02" /* Speeddial 2 */
296 "\x03\x02" /* Speeddial 3 */
297 "\x04\x02" /* Speeddial 4 */
298 "\x05\x02" /* Speeddial 5 */
299 "\x06\x02" /* Speeddial 6 */
300 "\x01\x0f" /* Voicemail */
301 "\x01\x05" /* Forward all */
302 "\x01\x7d" /* Conference */
308 "\x07\x02" /* Speeddial 7 */
309 "\x08\x02" /* Speeddial 8 */
310 "\x09\x02" /* Speeddial 9 */
311 "\x0A\x02" /* Speeddial 10 */
332 Now, define the buttons on the 12SP, you 79XX series folks, you get this
333 too, as I don't know how the buttons are laid out, and I don't care as I
334 don't have one. Code your own damn buttons or send me a 7960
337 static const char *twelvesp_button_definition_hack = {
338 "\x01\x09" /* Line 1 */
339 "\x01\x09" /* Line 2 */
340 "\x01\x02" /* Speeddial 1 */
341 "\x02\x02" /* Speeddial 2 */
342 "\x03\x02" /* Speeddial 3 */
343 "\x04\x02" /* Speeddial 4 */
344 "\x01\x0f" /* Voicemail */
345 "\x05\x02" /* Speeddial 5 */
346 "\x06\x02" /* Speeddial 6 */
347 "\x07\x02" /* Speeddial 7 */
348 "\x08\x02" /* Speeddial 8 */
349 "\x09\x02" /* Speeddial 9 */
382 typedef struct buttondefinition {
383 UINT8 instanceNumber;
384 UINT8 buttonDefinition;
387 typedef struct button_template_res_message {
390 UINT32 totalButtonCount;
391 button_definition definition[42];
392 } button_template_res_message;
394 #define VERSION_RES_MESSAGE 0x0098
395 typedef struct version_res_message {
397 } version_res_message;
399 #define KEEP_ALIVE_ACK_MESSAGE 0x0100
401 #define OPEN_RECIEVE_CHANNEL_MESSAGE 0x0105
402 typedef struct open_recieve_channel_message {
409 } open_recieve_channel_message;
411 #define CLOSE_RECIEVE_CHANNEL_MESSAGE 0x0106
412 typedef struct close_recieve_channel_message {
415 } close_recieve_channel_message;
417 #define SOFT_KEY_TEMPLATE_RES_MESSAGE 0x0108
418 static const char *soft_key_template_hack = {
419 "\x52\x65\x64\x69\x61\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
420 "\x01\x00\x00\x00\x4e\x65\x77\x43\x61\x6c\6c\\x00\x00\x00\x00\x00"
421 "\x00\x00\x00\x00\x02\x00\x00\x00\x48\x6f\x6c\x64\x00\x00\x00\x00"
422 "\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x54\x72\x6e\x73"
423 "\x66\x65\x72\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00"
424 "\x43\x46\x77\x64\x41\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05"
425 "\x00\x00\x00\x43\x46\x77\x64\x20\x42\x75\x73\x79\x00\x00\x00\x00"
426 "\x00\x00\x00\x06\x00\x00\x00\x43\x46\x77\x64\x4e\x6f\x41\x6e\x73"
427 "\x77\x65\x72\x00\x00\x00\x00\x07\x00\x00\x00\x3c\x3c\x00\x00\x00"
428 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x45"
429 "\x6e\x64\x43\x61\x6c\x6c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x09"
430 "\x00\x00\x00\x52\x65\x73\x75\x6d\x65\x00\x00\x00\x00\x00\x00\x00"
431 "\x00\x00\x0a\x00\x00\x00\x41\x6e\x73\x77\x65\x72\x00\x00\x00\x00"
432 "\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x49\x6e\x66\x6f\x00\x00"
433 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x43\x6f"
434 "\x6e\x66\x72\x6e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0d\x00\x00"
435 "\x00\x50\x61\x72\x6b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
436 "\x00\x0e\x00\x00\x00\x4a\x6f\x69\x6e\x00\x00\x00\x00\x00\x00\x00"
437 "\x00\x00\x00\x00\x0f\x00\x00\x00\x4d\x65\x65\x74\x4d\x65\x00\x00"
438 "\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x50\x69\x63\x6b"
439 "\x55\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00"
440 "\x47\x50\x69\x63\x6b\x55\x70\x00\x00\x00\x00\x00\x00\x00\x00\x00"
441 "\x12\x00\x00\x00\x52\x6d\x4c\x73\x43\x00\x00\x00\x00\x00\x00\x00"
442 "\x00\x00\x00\x13\x00\x00\x00\x42\x61\x72\x67\x65\x00\x00\x00\x00"
443 "\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x42\x61\x72\x67\x65"
444 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00"
447 typedef struct soft_key_template_definition {
448 char softKeyLabel[16];
450 } soft_key_template_definition;
452 typedef struct soft_key_template {
455 int totalSoftKeyCount;
456 soft_key_template_definition softKeyTemplateDefinition[32];
459 #define SOFT_KEY_SET_RES_MESSAGE 0x0109
460 static const char *soft_key_set_hack = {
461 "\x01\x02\x05\x03\x09\x0a\x0b\x10\x11\x12\x04\x0e\x0d\x00\x00\x00"
462 "\x2d\x01\x2e\x01\x31\x01\x2f\x01\x35\x01\x36\x01\x37\x01\x3c\x01"
463 "\x3d\x01\x3e\x01\x30\x01\x3a\x01\x39\x01\x00\x00\x00\x00\x00\x00"
464 "\x03\x09\x04\x0e\x0d\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
465 "\x2f\x01\x35\x01\x30\x01\x3a\x01\x39\x01\x3f\x01\x00\x00\x00\x00"
466 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
467 "\x0a\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
468 "\x36\x01\x2e\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
469 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
470 "\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
471 "\x37\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
472 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
473 "\x01\x09\x05\x10\x11\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
474 "\x2d\x01\x35\x01\x31\x01\x3c\x01\x3d\x01\x3e\x01\x00\x00\x00\x00"
475 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
476 "\x00\x09\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
477 "\x00\x00\x35\x01\x30\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
478 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
479 "\x08\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
480 "\x34\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
481 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
482 "\x00\x09\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
483 "\x00\x00\x35\x01\x39\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
484 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
485 "\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
486 "\x00\x00\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
487 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
488 "\x01\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
489 "\x2d\x01\x35\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
490 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
491 "\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
492 "\x41\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
493 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
496 typedef struct soft_key_set_definition {
497 UINT8 softKeyTemplateIndex[16];
498 UINT16 softKeyInfoIndex[16];
499 } soft_key_set_definition;
501 typedef struct soft_key_sets {
502 UINT32 softKeySetOffset;
503 UINT32 softKeySetCount;
504 UINT32 totalSoftKeySetCount;
505 soft_key_set_definition softKeySetDefinition[16];
509 #define SELECT_SOFT_KEYS_MESSAGE 0x0110
510 typedef struct select_soft_keys_message {
515 } select_soft_keys_message;
517 #define CALL_STATE_MESSAGE 0x0111
518 typedef struct call_state_message {
522 } call_state_message;
524 #define ACTIVATE_CALL_PLANE_MESSAGE 0x0116
525 typedef struct activate_call_plane_message {
527 } activate_call_plane_message;
529 /* packet composition */
535 speed_dial_stat_req_message speeddialreq;
536 register_message reg;
537 register_ack_message regack;
538 register_rej_message regrej;
539 capabilities_res_message caps;
540 version_res_message version;
541 button_template_res_message buttontemplate;
542 displaytext_message displaytext;
543 definetimedate_message definetimedate;
544 start_tone_message starttone;
545 speed_dial_stat_res_message speeddial;
546 line_state_req_message line;
547 line_stat_res_message linestat;
548 soft_key_sets softkeysets;
549 soft_key_template softkeytemplate;
550 server_res_message serverres;
551 set_lamp_message setlamp;
552 set_ringer_message setringer;
553 call_state_message callstate;
554 keypad_button_message keypad;
555 select_soft_keys_message selectsoftkey;
556 activate_call_plane_message activatecallplane;
557 stimulus_message stimulus;
558 set_speaker_message setspeaker;
559 call_info_message callinfo;
560 start_media_transmission_message startmedia;
561 stop_media_transmission_message stopmedia;
562 open_recieve_channel_message openrecievechannel;
563 open_recieve_channel_ack_message openrecievechannelack;
564 close_recieve_channel_message closerecievechannel;
568 /************************************************************************************/
569 /* Asterisk specific globals */
570 /************************************************************************************/
572 static int skinnydebug = 1; /* XXX for now, enable debugging default */
574 /* a hostname, portnumber, socket and such is usefull for VoIP protocols */
575 static struct sockaddr_in bindaddr;
576 static char ourhost[256];
578 static struct in_addr __ourip;
579 struct ast_hostent ahp; struct hostent *hp;
580 static int skinnysock = -1;
581 static pthread_t tcp_thread;
582 static pthread_t accept_t;
583 static char context[AST_MAX_EXTENSION] = "default";
584 static char language[MAX_LANGUAGE] = "";
585 static char musicclass[MAX_LANGUAGE] = "";
586 static char cid_num[AST_MAX_EXTENSION] = "";
587 static char cid_name[AST_MAX_EXTENSION] = "";
588 static char linelabel[AST_MAX_EXTENSION] ="";
590 static ast_group_t cur_callergroup = 0;
591 static ast_group_t cur_pickupgroup = 0;
592 static int immediate = 0;
593 static int callwaiting = 0;
594 static int callreturn = 0;
595 static int threewaycalling = 0;
596 /* This is for flashhook transfers */
597 static int transfer = 0;
598 static int cancallforward = 0;
599 /* static int busycount = 3;*/
600 static char accountcode[20] = "";
601 static char mailbox[AST_MAX_EXTENSION];
602 static int amaflags = 0;
603 static int callnums = 1;
609 #define SKINNY_SPEAKERON 1
610 #define SKINNY_SPEAKEROFF 2
612 #define SKINNY_OFFHOOK 1
613 #define SKINNY_ONHOOK 2
614 #define SKINNY_RINGOUT 3
615 #define SKINNY_RINGIN 4
616 #define SKINNY_CONNECTED 5
617 #define SKINNY_BUSY 6
618 #define SKINNY_CONGESTION 7
619 #define SKINNY_HOLD 8
620 #define SKINNY_CALLWAIT 9
621 #define SKINNY_TRANSFER 10
622 #define SKINNY_PARK 11
623 #define SKINNY_PROGRESS 12
624 #define SKINNY_INVALID 14
626 #define SKINNY_SILENCE 0
627 #define SKINNY_DIALTONE 33
628 #define SKINNY_BUSYTONE 35
629 #define SKINNY_ALERT 36
630 #define SKINNY_REORDER 37
631 #define SKINNY_CALLWAITTONE 45
633 #define SKINNY_LAMP_OFF 1
634 #define SKINNY_LAMP_ON 2
635 #define SKINNY_LAMP_WINK 3
636 #define SKINNY_LAMP_FLASH 4
637 #define SKINNY_LAMP_BLINK 5
639 #define SKINNY_RING_OFF 1
640 #define SKINNY_RING_INSIDE 2
641 #define SKINNY_RING_OUTSIDE 3
642 #define SKINNY_RING_FEATURE 4
647 #define STIMULUS_REDIAL 1
648 #define STIMULUS_SPEEDDIAL 2
649 #define STIMULUS_HOLD 3
650 #define STIMULUS_TRANSFER 4
651 #define STIMULUS_FORWARDALL 5
652 #define STIMULUS_FORWARDBUSY 6
653 #define STIMULUS_FORWARDNOANSWER 7
654 #define STIMULUS_DISPLAY 8
655 #define STIMULUS_LINE 9
656 #define STIMULUS_VOICEMAIL 15
657 #define STIMULUS_AUTOANSWER 17
658 #define STIMULUS_CONFERENCE 125
659 #define STIMULUS_CALLPARK 126
660 #define STIMULUS_CALLPICKUP 127
663 /* Skinny rtp stream modes. Do we really need this? */
664 #define SKINNY_CX_SENDONLY 0
665 #define SKINNY_CX_RECVONLY 1
666 #define SKINNY_CX_SENDRECV 2
667 #define SKINNY_CX_CONF 3
668 #define SKINNY_CX_CONFERENCE 3
669 #define SKINNY_CX_MUTE 4
670 #define SKINNY_CX_INACTIVE 4
673 static char *skinny_cxmodes[] = {
682 /* driver scheduler */
683 static struct sched_context *sched;
684 static struct io_context *io;
686 /* usage count and locking */
687 static int usecnt = 0;
688 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
690 /* Protect the monitoring thread, so only one process can kill or start it, and not
691 when it's doing something critical. */
692 AST_MUTEX_DEFINE_STATIC(monlock);
693 /* Protect the network socket */
694 AST_MUTEX_DEFINE_STATIC(netlock);
695 /* Protect the session list */
696 AST_MUTEX_DEFINE_STATIC(sessionlock);
697 /* Protect the device list */
698 AST_MUTEX_DEFINE_STATIC(devicelock);
700 /* This is the thread for the monitor which checks for input on the channels
701 which are not currently in use. */
702 static pthread_t monitor_thread = AST_PTHREADT_NULL;
704 /* Wait up to 16 seconds for first digit */
705 static int firstdigittimeout = 16000;
707 /* How long to wait for following digits */
708 static int gendigittimeout = 8000;
710 /* How long to wait for an extra digit, if there is an ambiguous match */
711 static int matchdigittimeout = 3000;
713 struct skinny_subchannel {
716 struct ast_channel *owner;
717 struct skinny_line *parent;
727 struct skinny_subchannel *next;
733 char label[42]; /* Label that shows next to the line buttons */
734 struct skinny_subchannel *sub; /* pointer to our current connection, channel and stuff */
735 char accountcode[80];
736 char exten[AST_MAX_EXTENSION]; /* Extention where to start */
737 char context[AST_MAX_EXTENSION];
738 char language[MAX_LANGUAGE];
739 char cid_num[AST_MAX_EXTENSION]; /* Caller*ID */
740 char cid_name[AST_MAX_EXTENSION]; /* Caller*ID */
741 char lastcallerid[AST_MAX_EXTENSION]; /* Last Caller*ID */
742 char call_forward[AST_MAX_EXTENSION];
743 char mailbox[AST_MAX_EXTENSION];
744 char musicclass[MAX_LANGUAGE];
745 int curtone; /* Current tone being played */
746 ast_group_t callgroup;
747 ast_group_t pickupgroup;
753 int dnd; /* How does this affect callwait? Do we just deny a skinny_request if we're dnd? */
762 int nonCodecCapability;
764 int msgstate; /* voicemail message state */
768 struct skinny_line *next;
769 struct skinny_device *parent;
772 static struct skinny_device {
773 /* A device containing one or more lines */
780 struct sockaddr_in addr;
781 struct in_addr ourip;
782 struct skinny_line *lines;
784 struct skinnysession *session;
785 struct skinny_device *next;
788 static struct skinnysession {
791 struct sockaddr_in sin;
793 char inbuf[SKINNY_MAX_PACKET];
794 struct skinny_device *device;
795 struct skinnysession *next;
798 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause);
799 static int skinny_call(struct ast_channel *ast, char *dest, int timeout);
800 static int skinny_hangup(struct ast_channel *ast);
801 static int skinny_answer(struct ast_channel *ast);
802 static struct ast_frame *skinny_read(struct ast_channel *ast);
803 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame);
804 static int skinny_indicate(struct ast_channel *ast, int ind);
805 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
806 static int skinny_senddigit(struct ast_channel *ast, char digit);
808 static const struct ast_channel_tech skinny_tech = {
810 .description = tdesc,
811 .capabilities = AST_FORMAT_ULAW,
812 .requester = skinny_request,
814 .hangup = skinny_hangup,
815 .answer = skinny_answer,
817 .write = skinny_write,
818 .indicate = skinny_indicate,
819 .fixup = skinny_fixup,
820 .send_digit = skinny_senddigit,
821 /* .bridge = ast_rtp_bridge, */
824 static skinny_req *req_alloc(size_t size)
827 req = malloc(size+12);
831 memset(req, 0, size+12);
835 static struct skinny_subchannel *find_subchannel_by_line(struct skinny_line *l)
837 /* XXX Need to figure out how to determine which sub we want */
838 struct skinny_subchannel *sub = l->sub;
842 static struct skinny_subchannel *find_subchannel_by_name(char *dest)
844 struct skinny_line *l;
845 struct skinny_device *d;
850 strncpy(line, dest, sizeof(line) - 1);
851 at = strchr(line, '@');
853 ast_log(LOG_NOTICE, "Device '%s' has no @ (at) sign!\n", dest);
859 ast_mutex_lock(&devicelock);
862 if (!strcasecmp(d->name, device)) {
864 ast_verbose("Found device: %s\n", d->name);
866 /* Found the device */
869 /* Search for the right line */
870 if (!strcasecmp(l->name, line)) {
871 ast_mutex_unlock(&devicelock);
879 /* Device not found*/
880 ast_mutex_unlock(&devicelock);
884 static int transmit_response(struct skinnysession *s, skinny_req *req)
887 ast_mutex_lock(&s->lock);
889 ast_verbose("writing packet type %d (%d bytes) to socket %d\n", req->e, req->len+8, s->fd);
891 res = write(s->fd, req, req->len+8);
892 if (res != req->len+8) {
893 ast_log(LOG_WARNING, "Transmit: write only sent %d out of %d bytes: %s\n", res, req->len+8, strerror(errno));
895 ast_mutex_unlock(&s->lock);
899 /* XXX Do this right */
900 static int convert_cap(int capability)
902 return 4; /* ulaw (this is not the same as asterisk's '4' */
906 static void transmit_speaker_mode(struct skinnysession *s, int mode)
910 req = req_alloc(sizeof(struct set_speaker_message));
912 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
915 req->len = sizeof(set_speaker_message)+4;
916 req->e = SET_SPEAKER_MESSAGE;
917 req->data.setspeaker.mode = mode;
918 transmit_response(s, req);
921 static void transmit_callstate(struct skinnysession *s, int instance, int state, unsigned callid)
924 int memsize = sizeof(struct call_state_message);
926 req = req_alloc(memsize);
928 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
931 if (state == SKINNY_ONHOOK) {
932 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
934 req->len = sizeof(call_state_message)+4;
935 req->e = CALL_STATE_MESSAGE;
936 req->data.callstate.callState = state;
937 req->data.callstate.lineInstance = instance;
938 req->data.callstate.callReference = callid;
939 transmit_response(s, req);
940 if (state == SKINNY_OFFHOOK) {
941 memset(req, 0, memsize);
942 req->len = sizeof(activate_call_plane_message)+4;
943 req->e = ACTIVATE_CALL_PLANE_MESSAGE;
944 req->data.activatecallplane.lineInstance = instance;
945 transmit_response(s, req);
946 } else if (state == SKINNY_ONHOOK) {
947 memset(req, 0, memsize);
948 req->len = sizeof(activate_call_plane_message)+4;
949 req->e = ACTIVATE_CALL_PLANE_MESSAGE;
950 req->data.activatecallplane.lineInstance = 0;
951 transmit_response(s, req);
952 memset(req, 0, memsize);
953 req->len = sizeof(close_recieve_channel_message)+4;
954 req->e = CLOSE_RECIEVE_CHANNEL_MESSAGE;
955 req->data.closerecievechannel.conferenceId = 0;
956 req->data.closerecievechannel.partyId = 0;
957 transmit_response(s, req);
958 memset(req, 0, memsize);
959 req->len = sizeof(stop_media_transmission_message)+4;
960 req->e = STOP_MEDIA_TRANSMISSION_MESSAGE;
961 req->data.stopmedia.conferenceId = 0;
962 req->data.stopmedia.passThruPartyId = 0;
963 transmit_response(s, req);
967 static void transmit_connect(struct skinnysession *s)
970 struct skinny_line *l = s->device->lines;
972 req = req_alloc(sizeof(struct open_recieve_channel_message));
974 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
977 req->len = sizeof(struct call_info_message);
978 req->e = OPEN_RECIEVE_CHANNEL_MESSAGE;
979 req->data.openrecievechannel.conferenceId = 0;
980 req->data.openrecievechannel.partyId = 0;
981 req->data.openrecievechannel.packets = 20;
982 req->data.openrecievechannel.capability = convert_cap(l->capability);
983 req->data.openrecievechannel.echo = 0;
984 req->data.openrecievechannel.bitrate = 0;
985 transmit_response(s, req);
988 static void transmit_tone(struct skinnysession *s, int tone)
993 req = req_alloc(sizeof(struct start_tone_message));
997 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
1001 req->len = sizeof(start_tone_message)+4;
1002 req->e = START_TONE_MESSAGE;
1003 req->data.starttone.tone = tone;
1006 req->e = STOP_TONE_MESSAGE;
1008 transmit_response(s, req);
1012 /* XXX need to properly deal with softkeys */
1013 static void transmit_selectsoftkeys(struct skinnysession *s, int instance, int callid, int softkey)
1016 int memsize = sizeof(struct select_soft_keys_message);
1018 req = req_alloc(memsize);
1020 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
1023 memset(req, 0, memsize);
1024 req->len = sizeof(select_soft_keys_message)+4;
1025 req->e = SELECT_SOFT_KEYS_MESSAGE;
1026 req->data.selectsoftkey.instance = instance;
1027 req->data.selectsoftkey.reference = callid;
1028 req->data.selectsoftkey.softKeySetIndex = softkey;
1029 transmit_response(s, req);
1033 static void transmit_lamp_indication(struct skinnysession *s, int stimulus, int instance, int indication)
1037 req = req_alloc(sizeof(struct set_lamp_message));
1039 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
1042 req->len = sizeof(set_lamp_message)+4;
1043 req->e = SET_LAMP_MESSAGE;
1044 req->data.setlamp.stimulus = stimulus;
1045 req->data.setlamp.stimulusInstance = instance;
1046 req->data.setlamp.deviceStimulus = indication;
1047 transmit_response(s, req);
1050 static void transmit_ringer_mode(struct skinnysession *s, int mode)
1054 req = req_alloc(sizeof(struct set_ringer_message));
1056 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
1059 req->len = sizeof(set_ringer_message)+4;
1060 req->e = SET_RINGER_MESSAGE;
1061 req->data.setringer.ringerMode = mode;
1062 transmit_response(s, req);
1065 static void transmit_displaymessage(struct skinnysession *s, char *text)
1072 req->e = CLEAR_DISPLAY_MESSAGE;
1074 req = req_alloc(sizeof(struct displaytext_message));
1076 strncpy(req->data.displaytext.text, text, sizeof(req->data.displaytext.text)-1);
1077 req->len = sizeof(displaytext_message) + 4;
1078 req->e = DISPLAYTEXT_MESSAGE;
1080 ast_verbose("Displaying message '%s'\n", req->data.displaytext.text);
1085 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
1088 transmit_response(s, req);
1091 static int has_voicemail(struct skinny_line *l)
1093 return ast_app_has_voicemail(l->mailbox, NULL);
1097 static void do_housekeeping(struct skinnysession *s)
1099 struct skinny_subchannel *sub;
1101 sub = find_subchannel_by_line(s->device->lines);
1102 transmit_displaymessage(s, 0);
1105 ast_verbose("Checking for voicemail Skinny %s@%s\n", sub->parent->name, sub->parent->parent->name);
1107 if (has_voicemail(sub->parent)) {
1110 ast_app_messagecount(sub->parent->mailbox, &new, &old);
1112 ast_verbose("Skinny %s@%s has voicemail! Yay!\n", sub->parent->name, sub->parent->parent->name);
1114 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, s->device->lines->instance, SKINNY_LAMP_BLINK);
1116 transmit_lamp_indication(s, STIMULUS_VOICEMAIL, s->device->lines->instance, SKINNY_LAMP_OFF);
1121 /* I do not believe skinny can deal with video.
1122 Anyone know differently? */
1123 static struct ast_rtp *skinny_get_vrtp_peer(struct ast_channel *chan)
1128 static struct ast_rtp *skinny_get_rtp_peer(struct ast_channel *chan)
1130 struct skinny_subchannel *sub;
1131 sub = chan->tech_pvt;
1132 if (sub && sub->rtp)
1137 static int skinny_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
1139 struct skinny_subchannel *sub;
1140 sub = chan->tech_pvt;
1142 /* transmit_modify_with_sdp(sub, rtp); @@FIXME@@ if needed */
1148 static struct ast_rtp_protocol skinny_rtp = {
1150 .get_rtp_info = skinny_get_rtp_peer,
1151 .get_vrtp_info = skinny_get_vrtp_peer,
1152 .set_rtp_peer = skinny_set_rtp_peer,
1155 static int skinny_do_debug(int fd, int argc, char *argv[])
1158 return RESULT_SHOWUSAGE;
1160 ast_cli(fd, "Skinny Debugging Enabled\n");
1161 return RESULT_SUCCESS;
1164 static int skinny_no_debug(int fd, int argc, char *argv[])
1167 return RESULT_SHOWUSAGE;
1169 ast_cli(fd, "Skinny Debugging Disabled\n");
1170 return RESULT_SUCCESS;
1173 static int skinny_show_lines(int fd, int argc, char *argv[])
1175 struct skinny_device *d;
1176 struct skinny_line *l;
1178 char iabuf[INET_ADDRSTRLEN];
1180 return RESULT_SHOWUSAGE;
1181 ast_mutex_lock(&devicelock);
1185 ast_cli(fd, "Device '%s' at %s\n", d->name, ast_inet_ntoa(iabuf, sizeof(iabuf), d->addr.sin_addr));
1187 ast_cli(fd, " -- '%s@%s in '%s' is %s\n", l->name, d->name, l->context, l->sub->owner ? "active" : "idle");
1192 ast_cli(fd, " << No Lines Defined >> ");
1196 ast_mutex_unlock(&devicelock);
1197 return RESULT_SUCCESS;
1200 static char show_lines_usage[] =
1201 "Usage: skinny show lines\n"
1202 " Lists all lines known to the Skinny subsystem.\n";
1204 static char debug_usage[] =
1205 "Usage: skinny debug\n"
1206 " Enables dumping of Skinny packets for debugging purposes\n";
1208 static char no_debug_usage[] =
1209 "Usage: skinny no debug\n"
1210 " Disables dumping of Skinny packets for debugging purposes\n";
1212 static struct ast_cli_entry cli_show_lines =
1213 { { "skinny", "show", "lines", NULL }, skinny_show_lines, "Show defined Skinny lines per device", show_lines_usage };
1214 static struct ast_cli_entry cli_debug =
1215 { { "skinny", "debug", NULL }, skinny_do_debug, "Enable Skinny debugging", debug_usage };
1216 static struct ast_cli_entry cli_no_debug =
1217 { { "skinny", "no", "debug", NULL }, skinny_no_debug, "Disable Skinny debugging", no_debug_usage };
1219 static struct skinny_device *build_device(char *cat, struct ast_variable *v)
1221 struct skinny_device *d;
1222 struct skinny_line *l;
1223 struct skinny_subchannel *sub;
1226 d = malloc(sizeof(struct skinny_device));
1228 memset(d, 0, sizeof(struct skinny_device));
1229 strncpy(d->name, cat, sizeof(d->name) - 1);
1231 if (!strcasecmp(v->name, "host")) {
1232 if (ast_get_ip(&d->addr, v->value)) {
1236 } else if (!strcasecmp(v->name, "port")) {
1237 d->addr.sin_port = htons(atoi(v->value));
1238 } else if (!strcasecmp(v->name, "device")) {
1239 strncpy(d->id, v->value, sizeof(d->id)-1);
1240 } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
1241 d->ha = ast_append_ha(v->name, v->value, d->ha);
1242 } else if (!strcasecmp(v->name, "context")) {
1243 strncpy(context, v->value, sizeof(context) - 1);
1244 } else if (!strcasecmp(v->name, "version")) {
1245 strncpy(d->version_id, v->value, sizeof(d->version_id) -1);
1246 } else if (!strcasecmp(v->name, "nat")) {
1247 nat = ast_true(v->value);
1248 } else if (!strcasecmp(v->name, "model")) {
1249 strncpy(d->model, v->value, sizeof(d->model) - 1);
1250 } else if (!strcasecmp(v->name, "callerid")) {
1251 if (!strcasecmp(v->value, "asreceived")) {
1255 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
1257 } else if (!strcasecmp(v->name, "language")) {
1258 strncpy(language, v->value, sizeof(language)-1);
1259 } else if (!strcasecmp(v->name, "accountcode")) {
1260 strncpy(accountcode, v->value, sizeof(accountcode)-1);
1261 } else if (!strcasecmp(v->name, "amaflags")) {
1262 y = ast_cdr_amaflags2int(v->value);
1264 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
1268 } else if (!strcasecmp(v->name, "musiconhold")) {
1269 strncpy(musicclass, v->value, sizeof(musicclass)-1);
1270 } else if (!strcasecmp(v->name, "callgroup")) {
1271 cur_callergroup = ast_get_group(v->value);
1272 } else if (!strcasecmp(v->name, "pickupgroup")) {
1273 cur_pickupgroup = ast_get_group(v->value);
1274 } else if (!strcasecmp(v->name, "immediate")) {
1275 immediate = ast_true(v->value);
1276 } else if (!strcasecmp(v->name, "cancallforward")) {
1277 cancallforward = ast_true(v->value);
1278 } else if (!strcasecmp(v->name, "mailbox")) {
1279 strncpy(mailbox, v->value, sizeof(mailbox) -1);
1280 } else if (!strcasecmp(v->name, "callreturn")) {
1281 callreturn = ast_true(v->value);
1282 } else if (!strcasecmp(v->name, "immediate")) {
1283 immediate = ast_true(v->value);
1284 } else if (!strcasecmp(v->name, "callwaiting")) {
1285 callwaiting = ast_true(v->value);
1286 } else if (!strcasecmp(v->name, "transfer")) {
1287 transfer = ast_true(v->value);
1288 } else if (!strcasecmp(v->name, "threewaycalling")) {
1289 threewaycalling = ast_true(v->value);
1290 } else if (!strcasecmp(v->name, "linelabel")) {
1291 strncpy(linelabel, v->value, sizeof(linelabel)-1);
1292 } else if (!strcasecmp(v->name, "trunk") || !strcasecmp(v->name, "line")) {
1293 l = malloc(sizeof(struct skinny_line));;
1295 memset(l, 0, sizeof(struct skinny_line));
1296 ast_mutex_init(&l->lock);
1297 strncpy(l->name, v->value, sizeof(l->name) - 1);
1299 /* XXX Should we check for uniqueness?? XXX */
1301 strncpy(l->context, context, sizeof(l->context) - 1);
1302 strncpy(l->cid_num, cid_num, sizeof(l->cid_num) - 1);
1303 strncpy(l->cid_name, cid_name, sizeof(l->cid_name) - 1);
1304 strncpy(l->label, linelabel, sizeof(l->label) - 1);
1305 strncpy(l->language, language, sizeof(l->language) - 1);
1306 strncpy(l->musicclass, musicclass, sizeof(l->musicclass)-1);
1307 strncpy(l->mailbox, mailbox, sizeof(l->mailbox)-1);
1308 strncpy(l->mailbox, mailbox, sizeof(l->mailbox)-1);
1309 if (!ast_strlen_zero(mailbox)) {
1310 ast_verbose(VERBOSE_PREFIX_3 "Setting mailbox '%s' on %s@%s\n", mailbox, d->name, l->name);
1314 l->capability = capability;
1316 if (!strcasecmp(v->name, "trunk")) {
1317 l->type = TYPE_TRUNK;
1319 l->type = TYPE_LINE;
1321 l->immediate = immediate;
1322 l->callgroup = cur_callergroup;
1323 l->pickupgroup = cur_pickupgroup;
1324 l->callreturn = callreturn;
1325 l->cancallforward = cancallforward;
1326 l->callwaiting = callwaiting;
1327 l->transfer = transfer;
1328 l->threewaycalling = threewaycalling;
1329 l->onhooktime = time(NULL);
1331 /* ASSUME we're onhook at this point*/
1332 l->hookstate = SKINNY_ONHOOK;
1334 for (i = 0; i < MAX_SUBS; i++) {
1335 sub = malloc(sizeof(struct skinny_subchannel));
1337 ast_verbose(VERBOSE_PREFIX_3 "Allocating Skinny subchannel '%d' on %s@%s\n", i, l->name, d->name);
1338 memset(sub, 0, sizeof(struct skinny_subchannel));
1339 ast_mutex_init(&sub->lock);
1341 /* Make a call*ID */
1342 sub->callid = callnums;
1344 sub->cxmode = SKINNY_CX_INACTIVE;
1349 /* XXX Should find a way to clean up our memory */
1350 ast_log(LOG_WARNING, "Out of memory allocating subchannel");
1357 /* XXX Should find a way to clean up our memory */
1358 ast_log(LOG_WARNING, "Out of memory allocating line");
1362 ast_log(LOG_WARNING, "Don't know keyword '%s' at line %d\n", v->name, v->lineno);
1368 ast_log(LOG_ERROR, "A Skinny device must have at least one line!\n");
1372 if (d->addr.sin_addr.s_addr && !ntohs(d->addr.sin_port))
1373 d->addr.sin_port = htons(DEFAULT_SKINNY_PORT);
1374 if (d->addr.sin_addr.s_addr) {
1375 if (ast_ouraddrfor(&d->addr.sin_addr, &d->ourip)) {
1376 memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
1379 memcpy(&d->ourip, &__ourip, sizeof(d->ourip));
1385 static int skinny_register(skinny_req *req, struct skinnysession *s)
1387 struct skinny_device *d;
1389 ast_mutex_lock(&devicelock);
1392 if (!strcasecmp(req->data.reg.name, d->id)) {
1393 /* XXX Deal with IP authentication */
1395 d->type = req->data.reg.type;
1396 if (ast_strlen_zero(d->version_id)) {
1397 strncpy(d->version_id, version_id, sizeof(d->version_id) - 1);
1405 ast_mutex_unlock(&devicelock);
1413 static void start_rtp(struct skinny_subchannel *sub)
1415 ast_mutex_lock(&sub->lock);
1416 /* Allocate the RTP */
1417 sub->rtp = ast_rtp_new(sched, io, 1, 0);
1418 if (sub->rtp && sub->owner)
1419 sub->owner->fds[0] = ast_rtp_fd(sub->rtp);
1421 ast_rtp_setnat(sub->rtp, sub->nat);
1423 /* Create the RTP connection */
1424 transmit_connect(sub->parent->parent->session);
1425 ast_mutex_unlock(&sub->lock);
1429 static void *skinny_ss(void *data)
1431 struct ast_channel *chan = data;
1432 struct skinny_subchannel *sub = chan->tech_pvt;
1433 struct skinny_line *l = sub->parent;
1434 struct skinnysession *s = l->parent->session;
1435 char exten[AST_MAX_EXTENSION] = "";
1437 int timeout = firstdigittimeout;
1441 if (option_verbose > 2) {
1442 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s@%s'\n", l->name, l->parent->name);
1444 while(len < AST_MAX_EXTENSION-1) {
1445 res = ast_waitfordigit(chan, timeout);
1449 ast_verbose("Skinny(%s@%s): waitfordigit returned < 0\n", l->name, l->parent->name);
1451 ast_indicate(chan, -1);
1458 if (!ast_ignore_pattern(chan->context, exten)) {
1459 transmit_tone(s, SKINNY_SILENCE);
1461 if (ast_exists_extension(chan, chan->context, exten, 1, l->cid_num)) {
1462 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, l->cid_num)) {
1464 /* Record this as the forwarding extension */
1465 strncpy(l->call_forward, exten, sizeof(l->call_forward) - 1);
1466 if (option_verbose > 2) {
1467 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %s\n",
1468 l->call_forward, chan->name);
1470 transmit_tone(s, SKINNY_DIALTONE);
1475 ast_indicate(chan, -1);
1477 memset(exten, 0, sizeof(exten));
1478 transmit_tone(s, SKINNY_DIALTONE);
1482 strncpy(chan->exten, exten, sizeof(chan->exten)-1);
1483 if (!ast_strlen_zero(l->cid_num)) {
1484 if (!l->hidecallerid)
1485 chan->cid.cid_num = strdup(l->cid_num);
1486 chan->cid.cid_ani = strdup(l->cid_num);
1488 ast_setstate(chan, AST_STATE_RING);
1489 res = ast_pbx_run(chan);
1491 ast_log(LOG_WARNING, "PBX exited non-zero\n");
1492 transmit_tone(s, SKINNY_REORDER);
1497 /* It's a match, but they just typed a digit, and there is an ambiguous match,
1498 so just set the timeout to matchdigittimeout and wait some more */
1499 timeout = matchdigittimeout;
1501 } else if (res == 0) {
1502 ast_log(LOG_DEBUG, "Not enough digits (and no ambiguous match)...\n");
1503 transmit_tone(s, SKINNY_REORDER);
1506 } else if (l->callwaiting && !strcmp(exten, "*70")) {
1507 if (option_verbose > 2) {
1508 ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
1510 /* Disable call waiting if enabled */
1512 transmit_tone(s, SKINNY_DIALTONE);
1514 memset(exten, 0, sizeof(exten));
1515 timeout = firstdigittimeout;
1517 } else if (!strcmp(exten,ast_pickup_ext())) {
1518 /* Scan all channels and see if any there
1519 * ringing channqels with that have call groups
1520 * that equal this channels pickup group
1522 if (ast_pickup_call(chan)) {
1523 ast_log(LOG_WARNING, "No call pickup possible...\n");
1524 transmit_tone(s, SKINNY_REORDER);
1529 } else if (!l->hidecallerid && !strcmp(exten, "*67")) {
1530 if (option_verbose > 2) {
1531 ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
1533 /* Disable Caller*ID if enabled */
1534 l->hidecallerid = 1;
1535 if (chan->cid.cid_num)
1536 free(chan->cid.cid_num);
1537 chan->cid.cid_num = NULL;
1539 if (chan->cid.cid_name)
1540 free(chan->cid.cid_name);
1541 chan->cid.cid_name = NULL;
1543 transmit_tone(s, SKINNY_DIALTONE);
1545 memset(exten, 0, sizeof(exten));
1546 timeout = firstdigittimeout;
1547 } else if (l->callreturn && !strcmp(exten, "*69")) {
1549 if (!ast_strlen_zero(l->lastcallerid)) {
1550 res = ast_say_digit_str(chan, l->lastcallerid, "", chan->language);
1553 transmit_tone(s, SKINNY_DIALTONE);
1556 } else if (!strcmp(exten, "*78")) {
1557 /* Do not disturb */
1558 if (option_verbose > 2) {
1559 ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %s\n", chan->name);
1561 transmit_tone(s, SKINNY_DIALTONE);
1564 memset(exten, 0, sizeof(exten));
1566 } else if (!strcmp(exten, "*79")) {
1567 /* Do not disturb */
1568 if (option_verbose > 2) {
1569 ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %s\n", chan->name);
1571 transmit_tone(s, SKINNY_DIALTONE);
1574 memset(exten, 0, sizeof(exten));
1576 } else if (l->cancallforward && !strcmp(exten, "*72")) {
1577 transmit_tone(s, SKINNY_DIALTONE);
1579 memset(exten, 0, sizeof(exten));
1581 } else if (l->cancallforward && !strcmp(exten, "*73")) {
1582 if (option_verbose > 2) {
1583 ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %s\n", chan->name);
1585 transmit_tone(s, SKINNY_DIALTONE);
1586 memset(l->call_forward, 0, sizeof(l->call_forward));
1588 memset(exten, 0, sizeof(exten));
1590 } else if (!strcmp(exten, ast_parking_ext()) &&
1592 ast_bridged_channel(sub->next->owner)) {
1593 /* This is a three way call, the main call being a real channel,
1594 and we're parking the first call. */
1595 ast_masq_park_call(ast_bridged_channel(sub->next->owner), chan, 0, NULL);
1596 if (option_verbose > 2) {
1597 ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
1600 } else if (!ast_strlen_zero(l->lastcallerid) && !strcmp(exten, "*60")) {
1601 if (option_verbose > 2) {
1602 ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", l->lastcallerid);
1604 res = ast_db_put("blacklist", l->lastcallerid, "1");
1606 transmit_tone(s, SKINNY_DIALTONE);
1607 memset(exten, 0, sizeof(exten));
1610 } else if (l->hidecallerid && !strcmp(exten, "*82")) {
1611 if (option_verbose > 2) {
1612 ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
1614 /* Enable Caller*ID if enabled */
1615 l->hidecallerid = 0;
1616 if (chan->cid.cid_num)
1617 free(chan->cid.cid_num);
1618 if (!ast_strlen_zero(l->cid_num))
1619 chan->cid.cid_num = strdup(l->cid_num);
1621 if (chan->cid.cid_name)
1622 free(chan->cid.cid_name);
1623 if (!ast_strlen_zero(l->cid_name))
1624 chan->cid.cid_name = strdup(l->cid_name);
1626 transmit_tone(s, SKINNY_DIALTONE);
1628 memset(exten, 0, sizeof(exten));
1629 timeout = firstdigittimeout;
1630 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
1631 ((exten[0] != '*') || (!ast_strlen_zero(exten) > 2))) {
1632 ast_log(LOG_WARNING, "Can't match [%s] from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
1633 transmit_tone(s, SKINNY_REORDER);
1634 sleep(3); /* hang out for 3 seconds to let congestion play */
1638 timeout = gendigittimeout;
1639 if (len && !ast_ignore_pattern(chan->context, exten))
1640 ast_indicate(chan, -1);
1648 static int skinny_call(struct ast_channel *ast, char *dest, int timeout)
1652 struct skinny_line *l;
1653 struct skinny_subchannel *sub;
1654 struct skinnysession *session;
1656 sub = ast->tech_pvt;
1658 session = l->parent->session;
1660 if (!l->parent->registered) {
1661 ast_log(LOG_ERROR, "Device not registered, cannot call %s\n", dest);
1665 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1666 ast_log(LOG_WARNING, "skinny_call called on %s, neither down nor reserved\n", ast->name);
1671 ast_verbose(VERBOSE_PREFIX_3 "skinny_call(%s)\n", ast->name);
1675 ast_queue_control(ast, AST_CONTROL_BUSY);
1679 switch (l->hookstate) {
1680 case SKINNY_OFFHOOK:
1681 tone = SKINNY_CALLWAITTONE;
1684 tone = SKINNY_ALERT;
1687 ast_log(LOG_ERROR, "Don't know how to deal with hookstate %d\n", l->hookstate);
1691 transmit_lamp_indication(session, STIMULUS_LINE, l->instance, SKINNY_LAMP_BLINK);
1692 transmit_ringer_mode(session, SKINNY_RING_INSIDE);
1694 if (ast->cid.cid_num){
1696 char ciddisplay[41] = "";
1698 /* We'll assume that if it is 10 numbers, it is a standard NANPA number
1699 Why? Because I am bloody American, and I'm bigoted that way. */
1701 if (strlen(ast->cid.cid_num) == 10) {
1703 strcat (ciddisplay, "(");
1704 strncat (ciddisplay, ast->cid.cid_num,3);
1705 strcat (ciddisplay, ") ");
1707 strncat (ciddisplay, &ast->cid.cid_num[3],3);
1708 strcat (ciddisplay,"-");
1709 strncat (ciddisplay, &ast->cid.cid_num[6],4);
1711 strncat (ciddisplay, " ", 6);
1713 strncat (ciddisplay, ast->cid.cid_name,17);
1715 if (strlen(ast->cid.cid_num) < 40) {
1716 strncpy(ciddisplay,ast->cid.cid_num,strlen(ast->cid.cid_num));
1717 strcat (ciddisplay," -- ");
1719 if (sizeof(ast->cid.cid_name) > (40 - (strlen(ast->cid.cid_num) + 4))) {
1720 strncat (ciddisplay, ast->cid.cid_name, (40 - (strlen(ast->cid.cid_num) + 4)));
1722 strncat (ciddisplay, ast->cid.cid_name, strlen(ast->cid.cid_name));
1725 strncpy(ciddisplay, "Number too long!", 15);
1730 ast_verbose("Trying to send: '%s'\n",ciddisplay);
1733 transmit_displaymessage(session, ciddisplay);
1735 transmit_displaymessage(session, "Unknown Name");
1738 transmit_tone(session, tone);
1739 transmit_callstate(session, l->instance, SKINNY_RINGIN, sub->callid);
1741 /* XXX need to set the prompt */
1742 /* XXX need to deal with softkeys */
1744 ast_setstate(ast, AST_STATE_RINGING);
1745 ast_queue_control(ast, AST_CONTROL_RINGING);
1748 if (l->type == TYPE_LINE) {
1752 /* do/should we need to anything if there already is an RTP allocated? */
1756 ast_log(LOG_ERROR, "I don't know how to dial on trunks, yet\n");
1763 static int skinny_hangup(struct ast_channel *ast)
1765 struct skinny_subchannel *sub = ast->tech_pvt;
1766 struct skinny_line *l = sub->parent;
1767 struct skinnysession *s = l->parent->session;
1770 ast_verbose("skinny_hangup(%s) on %s@%s\n", ast->name, l->name, l->parent->name);
1772 if (!ast->tech_pvt) {
1773 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
1777 if (l->parent->registered) {
1778 if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_OFFHOOK)) {
1779 sub->parent->hookstate = SKINNY_ONHOOK;
1780 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
1781 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
1782 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
1783 } else if ((sub->parent->type = TYPE_LINE) && (sub->parent->hookstate == SKINNY_ONHOOK)) {
1784 transmit_callstate(s, l->instance, SKINNY_ONHOOK, sub->callid);
1785 transmit_speaker_mode(s, SKINNY_SPEAKEROFF);
1786 transmit_ringer_mode(s, SKINNY_RING_OFF);
1787 transmit_tone(s, SKINNY_SILENCE);
1788 transmit_lamp_indication(s, STIMULUS_LINE, l->instance, SKINNY_LAMP_OFF);
1792 ast_mutex_lock(&sub->lock);
1794 ast->tech_pvt = NULL;
1795 sub->alreadygone = 0;
1798 ast_rtp_destroy(sub->rtp);
1801 ast_mutex_unlock(&sub->lock);
1805 static int skinny_answer(struct ast_channel *ast)
1808 struct skinny_subchannel *sub = ast->tech_pvt;
1809 struct skinny_line *l = sub->parent;
1810 sub->cxmode = SKINNY_CX_SENDRECV;
1814 ast_verbose("skinny_answer(%s) on %s@%s-%d\n", ast->name, l->name, l->parent->name, sub->callid);
1815 if (ast->_state != AST_STATE_UP) {
1816 ast_setstate(ast, AST_STATE_UP);
1821 static struct ast_frame *skinny_rtp_read(struct skinny_subchannel *sub)
1823 /* Retrieve audio/etc from channel. Assumes sub->lock is already held. */
1824 struct ast_frame *f;
1825 f = ast_rtp_read(sub->rtp);
1827 /* We already hold the channel lock */
1828 if (f->frametype == AST_FRAME_VOICE) {
1829 if (f->subclass != sub->owner->nativeformats) {
1830 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
1831 sub->owner->nativeformats = f->subclass;
1832 ast_set_read_format(sub->owner, sub->owner->readformat);
1833 ast_set_write_format(sub->owner, sub->owner->writeformat);
1840 static struct ast_frame *skinny_read(struct ast_channel *ast)
1842 struct ast_frame *fr;
1843 struct skinny_subchannel *sub = ast->tech_pvt;
1844 ast_mutex_lock(&sub->lock);
1845 fr = skinny_rtp_read(sub);
1846 ast_mutex_unlock(&sub->lock);
1850 static int skinny_write(struct ast_channel *ast, struct ast_frame *frame)
1852 struct skinny_subchannel *sub = ast->tech_pvt;
1854 if (frame->frametype != AST_FRAME_VOICE) {
1855 if (frame->frametype == AST_FRAME_IMAGE)
1858 ast_log(LOG_WARNING, "Can't send %d type frames with skinny_write\n", frame->frametype);
1862 if (!(frame->subclass & ast->nativeformats)) {
1863 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
1864 frame->subclass, ast->nativeformats, ast->readformat, ast->writeformat);
1869 ast_mutex_lock(&sub->lock);
1871 res = ast_rtp_write(sub->rtp, frame);
1873 ast_mutex_unlock(&sub->lock);
1878 static int skinny_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1880 struct skinny_subchannel *sub = newchan->tech_pvt;
1881 ast_log(LOG_NOTICE, "skinny_fixup(%s, %s)\n", oldchan->name, newchan->name);
1882 if (sub->owner != oldchan) {
1883 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, sub->owner);
1886 sub->owner = newchan;
1890 static int skinny_senddigit(struct ast_channel *ast, char digit)
1893 struct skinny_subchannel *sub = ast->tech_pvt;
1896 sprintf(tmp, "%d", digit);
1897 transmit_tone(sub->parent->parent->session, digit);
1902 static char *control2str(int ind) {
1904 case AST_CONTROL_HANGUP:
1905 return "Other end has hungup";
1906 case AST_CONTROL_RING:
1907 return "Local ring";
1908 case AST_CONTROL_RINGING:
1909 return "Remote end is ringing";
1910 case AST_CONTROL_ANSWER:
1911 return "Remote end has answered";
1912 case AST_CONTROL_BUSY:
1913 return "Remote end is busy";
1914 case AST_CONTROL_TAKEOFFHOOK:
1915 return "Make it go off hook";
1916 case AST_CONTROL_OFFHOOK:
1917 return "Line is off hook";
1918 case AST_CONTROL_CONGESTION:
1919 return "Congestion (circuits busy)";
1920 case AST_CONTROL_FLASH:
1921 return "Flash hook";
1922 case AST_CONTROL_WINK:
1924 case AST_CONTROL_OPTION:
1925 return "Set a low-level option";
1926 case AST_CONTROL_RADIO_KEY:
1928 case AST_CONTROL_RADIO_UNKEY:
1929 return "Un-Key Radio";
1937 static int skinny_indicate(struct ast_channel *ast, int ind)
1939 struct skinny_subchannel *sub = ast->tech_pvt;
1940 struct skinny_line *l = sub->parent;
1941 struct skinnysession *s = l->parent->session;
1944 ast_verbose(VERBOSE_PREFIX_3 "Asked to indicate '%s' condition on channel %s\n", control2str(ind), ast->name);
1947 case AST_CONTROL_RINGING:
1948 if (ast->_state != AST_STATE_UP) {
1949 if (!sub->progress) {
1950 transmit_tone(s, SKINNY_ALERT);
1951 transmit_callstate(s, l->instance, SKINNY_RINGOUT, sub->callid);
1957 case AST_CONTROL_BUSY:
1958 if (ast->_state != AST_STATE_UP) {
1959 transmit_tone(s, SKINNY_BUSYTONE);
1960 transmit_callstate(s, l->instance, SKINNY_BUSY, sub->callid);
1961 sub->alreadygone = 1;
1962 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
1966 case AST_CONTROL_CONGESTION:
1967 if (ast->_state != AST_STATE_UP) {
1968 transmit_tone(s, SKINNY_REORDER);
1969 transmit_callstate(s, l->instance, SKINNY_CONGESTION, sub->callid);
1970 sub->alreadygone = 1;
1971 ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
1975 case AST_CONTROL_PROGRESS:
1976 if ((ast->_state != AST_STATE_UP) && !sub->progress && !sub->outgoing) {
1977 transmit_callstate(s, l->instance, SKINNY_PROGRESS, sub->callid);
1983 transmit_tone(s, SKINNY_SILENCE);
1985 case AST_CONTROL_PROCEEDING:
1988 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", ind);
1994 static struct ast_channel *skinny_new(struct skinny_subchannel *sub, int state)
1996 struct ast_channel *tmp;
1997 struct skinny_line *l = sub->parent;
2000 tmp = ast_channel_alloc(1);
2002 tmp->tech = &skinny_tech;
2003 tmp->nativeformats = l->capability;
2004 if (!tmp->nativeformats)
2005 tmp->nativeformats = capability;
2006 fmt = ast_best_codec(tmp->nativeformats);
2007 snprintf(tmp->name, sizeof(tmp->name), "Skinny/%s@%s-%d", l->name, l->parent->name, sub->callid);
2009 tmp->fds[0] = ast_rtp_fd(sub->rtp);
2011 ast_setstate(tmp, state);
2012 if (state == AST_STATE_RING)
2014 tmp->writeformat = fmt;
2015 tmp->rawwriteformat = fmt;
2016 tmp->readformat = fmt;
2017 tmp->rawreadformat = fmt;
2018 tmp->tech_pvt = sub;
2019 if (!ast_strlen_zero(l->language))
2020 strncpy(tmp->language, l->language, sizeof(tmp->language)-1);
2021 if (!ast_strlen_zero(l->accountcode))
2022 strncpy(tmp->accountcode, l->accountcode, sizeof(tmp->accountcode)-1);
2024 tmp->amaflags = l->amaflags;
2026 ast_mutex_lock(&usecnt_lock);
2028 ast_mutex_unlock(&usecnt_lock);
2029 ast_update_use_count();
2030 tmp->callgroup = l->callgroup;
2031 tmp->pickupgroup = l->pickupgroup;
2032 strncpy(tmp->call_forward, l->call_forward, sizeof(tmp->call_forward) - 1);
2033 strncpy(tmp->context, l->context, sizeof(tmp->context)-1);
2034 strncpy(tmp->exten,l->exten, sizeof(tmp->exten)-1);
2035 if (!ast_strlen_zero(l->cid_num)) {
2036 tmp->cid.cid_num = strdup(l->cid_num);
2038 if (!ast_strlen_zero(l->cid_name)) {
2039 tmp->cid.cid_name = strdup(l->cid_name);
2042 if (state != AST_STATE_DOWN) {
2043 if (ast_pbx_start(tmp)) {
2044 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
2050 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
2056 static int handle_message(skinny_req *req, struct skinnysession *s)
2058 struct skinny_subchannel *sub;
2059 struct ast_channel *c;
2060 struct ast_frame f = { 0, };
2061 struct sockaddr_in sin;
2062 struct sockaddr_in us;
2063 struct skinny_line *lines;
2072 int stimulusInstance;
2080 if ( (!s->device) && (req->e != REGISTER_MESSAGE && req->e != ALARM_MESSAGE)) {
2081 ast_log(LOG_WARNING, "Client sent message #%d without first registering.\n", req->e);
2088 /* no response necessary */
2090 case REGISTER_MESSAGE:
2092 ast_verbose("Device %s is attempting to register\n", req->data.reg.name);
2094 res = skinny_register(req, s);
2096 ast_log(LOG_ERROR, "Rejecting Device %s: Device not found\n", req->data.reg.name);
2097 memcpy(&name, req->data.reg.name, sizeof(req->data.reg.name));
2098 memset(req, 0, sizeof(skinny_req));
2099 req->len = sizeof(register_rej_message)+4;
2100 req->e = REGISTER_REJ_MESSAGE;
2101 snprintf(req->data.regrej.errMsg, sizeof(req->data.regrej.errMsg), "No Authority: %s", name);
2102 transmit_response(s, req);
2105 if (option_verbose > 2) {
2106 ast_verbose(VERBOSE_PREFIX_3 "Device '%s' successfuly registered\n", s->device->name);
2108 memset(req, 0, SKINNY_MAX_PACKET);
2109 req->len = sizeof(register_ack_message)+4;
2110 req->e = REGISTER_ACK_MESSAGE;
2111 req->data.regack.res[0] = '0';
2112 req->data.regack.res[1] = '\0';
2113 req->data.regack.keepAlive = keep_alive;
2114 strncpy(req->data.regack.dateTemplate, date_format, sizeof(req->data.regack.dateTemplate) - 1);
2115 req->data.regack.res2[0] = '0';
2116 req->data.regack.res2[1] = '\0';
2117 req->data.regack.secondaryKeepAlive = keep_alive;
2118 transmit_response(s, req);
2120 ast_verbose("Requesting capabilities\n");
2122 memset(req, 0, SKINNY_MAX_PACKET);
2124 req->e = CAPABILITIES_REQ_MESSAGE;
2125 transmit_response(s, req);
2127 case UNREGISTER_MESSAGE:
2128 /* XXX Acutally unregister the device */
2130 case IP_PORT_MESSAGE:
2131 /* no response necessary */
2133 case STIMULUS_MESSAGE:
2134 stimulus = req->data.stimulus.stimulus;
2135 stimulusInstance = req->data.stimulus.stimulusInstance;
2138 case STIMULUS_REDIAL:
2139 /* If we can keep an array of dialed frames we can implement a quick
2140 and dirty redial, feeding the frames we last got into the queue
2144 ast_verbose("Recieved Stimulus: Redial(%d)\n", stimulusInstance);
2149 case STIMULUS_SPEEDDIAL:
2151 ast_verbose("Recieved Stimulus: SpeedDial(%d)\n", stimulusInstance);
2155 /* start moh? set RTP to 0.0.0.0? */
2157 ast_verbose("Recieved Stimulus: Hold(%d)\n", stimulusInstance);
2160 case STIMULUS_TRANSFER:
2162 ast_verbose("Recieved Stimulus: Transfer(%d)\n", stimulusInstance);
2164 transmit_tone(s, SKINNY_DIALTONE);
2166 /* figure out how to transfer */
2169 case STIMULUS_CONFERENCE:
2171 ast_verbose("Recieved Stimulus: Transfer(%d)\n", stimulusInstance);
2173 transmit_tone(s, SKINNY_DIALTONE);
2175 /* figure out how to bridge n' stuff */
2179 case STIMULUS_VOICEMAIL:
2181 ast_verbose("Recieved Stimulus: Voicemail(%d)\n", stimulusInstance);
2184 /* Dial Voicemail */
2187 case STIMULUS_CALLPARK:
2189 ast_verbose("Recieved Stimulus: Park Call(%d)\n", stimulusInstance);
2193 case STIMULUS_FORWARDALL:
2194 /* Do not disturb */
2195 transmit_tone(s, SKINNY_DIALTONE);
2196 if(s->device->lines->dnd != 0){
2197 if (option_verbose > 2) {
2198 ast_verbose(VERBOSE_PREFIX_3 "Disabling DND on %s@%s\n",find_subchannel_by_line(s->device->lines)->parent->name,find_subchannel_by_line(s->device->lines)->parent->name);
2200 s->device->lines->dnd = 0;
2201 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_ON);
2203 if (option_verbose > 2) {
2204 ast_verbose(VERBOSE_PREFIX_3 "Enabling DND on %s@%s\n",find_subchannel_by_line(s->device->lines)->parent->name,find_subchannel_by_line(s->device->lines)->parent->name);
2206 s->device->lines->dnd = 1;
2207 transmit_lamp_indication(s, STIMULUS_FORWARDALL, 1, SKINNY_LAMP_OFF);
2210 case STIMULUS_FORWARDBUSY:
2211 case STIMULUS_FORWARDNOANSWER:
2212 /* Gonna be fun, not */
2214 ast_verbose("Recieved Stimulus: Forward (%d)\n", stimulusInstance);
2217 case STIMULUS_DISPLAY:
2218 /* Not sure what this is */
2220 ast_verbose("Recieved Stimulus: Display(%d)\n", stimulusInstance);
2225 ast_verbose("Recieved Stimulus: Line(%d)\n", stimulusInstance);
2227 sub = find_subchannel_by_line(s->device->lines);
2228 /* turn the speaker on */
2229 transmit_speaker_mode(s, 1);
2232 ast_verbose("RECEIVED UNKNOWN STIMULUS: %d(%d)\n", stimulus, stimulusInstance);
2236 case VERSION_REQ_MESSAGE:
2238 ast_verbose("Version Request\n");
2240 memset(req, 0, SKINNY_MAX_PACKET);
2241 req->len = sizeof(version_res_message)+4;
2242 req->e = VERSION_RES_MESSAGE;
2243 snprintf(req->data.version.version, sizeof(req->data.version.version), s->device->version_id);
2244 transmit_response(s, req);
2246 case SERVER_REQUEST_MESSAGE:
2248 ast_verbose("Recieved Server Request\n");
2250 memset(req, 0, SKINNY_MAX_PACKET);
2251 req->len = sizeof(server_res_message)+4;
2252 req->e = SERVER_RES_MESSAGE;
2253 memcpy(req->data.serverres.server[0].serverName, ourhost,
2254 sizeof(req->data.serverres.server[0].serverName));
2255 req->data.serverres.serverListenPort[0] = ourport;
2256 req->data.serverres.serverIpAddr[0] = __ourip.s_addr;
2257 transmit_response(s, req);
2259 case BUTTON_TEMPLATE_REQ_MESSAGE:
2261 ast_verbose("Buttontemplate requested\n");
2263 memset(req, 0, SKINNY_MAX_PACKET);
2264 req->e = BUTTON_TEMPLATE_RES_MESSAGE;
2265 /* XXX Less of a hack, more of a kludge now */
2266 sub = find_subchannel_by_line(s->device->lines);
2267 req->len = sizeof(button_template_res_message)+4;
2268 if (!strcmp(s->device->model,"30VIP")){
2269 req->data.buttontemplate.buttonOffset = 0;
2270 req->data.buttontemplate.buttonCount = 30;
2271 req->data.buttontemplate.totalButtonCount = 30;
2272 memcpy(req->data.buttontemplate.definition,
2273 thirtyvip_button_definition_hack,
2274 sizeof(req->data.buttontemplate.definition));
2276 ast_verbose("Sending 30VIP template to %s@%s (%s)\n",sub->parent->name, sub->parent->parent->name, s->device->model);
2278 }else if(!strcmp(s->device->model,"12SP")){
2279 req->data.buttontemplate.buttonOffset = 0;
2280 req->data.buttontemplate.buttonCount = 12;
2281 req->data.buttontemplate.totalButtonCount = 12;
2282 memcpy(req->data.buttontemplate.definition,
2283 twelvesp_button_definition_hack,
2284 sizeof(req->data.buttontemplate.definition));
2286 ast_verbose("Sending 12SP template to %s@%s (%s)\n",sub->parent->name, sub->parent->parent->name, s->device->model);
2289 req->data.buttontemplate.buttonOffset = 0;
2290 req->data.buttontemplate.buttonCount = 12;
2291 req->data.buttontemplate.totalButtonCount = 12;
2292 memcpy(req->data.buttontemplate.definition,
2293 twelvesp_button_definition_hack,
2294 sizeof(req->data.buttontemplate.definition));
2296 ast_verbose("Sending default template to %s@%s (%s)\n",sub->parent->name, sub->parent->parent->name, s->device->model);
2301 transmit_response(s, req);
2303 case SOFT_KEY_SET_REQ_MESSAGE:
2305 ast_verbose("Received SoftKeySetReq\n");
2307 memset(req, 0, SKINNY_MAX_PACKET);
2308 req->len = sizeof(soft_key_sets)+4;
2309 req->e = SOFT_KEY_SET_RES_MESSAGE;
2310 req->data.softkeysets.softKeySetOffset = 0;
2311 req->data.softkeysets.softKeySetCount = 11;
2312 req->data.softkeysets.totalSoftKeySetCount = 11;
2313 /* XXX Wicked hack XXX */
2314 memcpy(req->data.softkeysets.softKeySetDefinition,
2316 sizeof(req->data.softkeysets.softKeySetDefinition));
2317 transmit_response(s,req);
2319 case SOFT_KEY_TEMPLATE_REQ_MESSAGE:
2321 ast_verbose("Recieved SoftKey Template Request\n");
2323 memset(req, 0, SKINNY_MAX_PACKET);
2324 req->len = sizeof(soft_key_template)+4;
2325 req->e = SOFT_KEY_TEMPLATE_RES_MESSAGE;
2326 req->data.softkeytemplate.softKeyOffset = 0;
2327 req->data.softkeytemplate.softKeyCount = 21;
2328 req->data.softkeytemplate.totalSoftKeyCount = 21;
2329 /* XXX Another wicked hack XXX */
2330 memcpy(req->data.softkeytemplate.softKeyTemplateDefinition,
2331 soft_key_template_hack,
2332 sizeof(req->data.softkeytemplate.softKeyTemplateDefinition));
2333 transmit_response(s,req);
2335 case TIME_DATE_REQ_MESSAGE:
2337 ast_verbose("Received Time/Date Request\n");
2339 memset(req, 0, SKINNY_MAX_PACKET);
2340 req->len = sizeof(definetimedate_message)+4;
2341 req->e = DEFINETIMEDATE_MESSAGE;
2343 cmtime = localtime(&timer);
2344 req->data.definetimedate.year = cmtime->tm_year+1900;
2345 req->data.definetimedate.month = cmtime->tm_mon+1;
2346 req->data.definetimedate.dayofweek = cmtime->tm_wday;
2347 req->data.definetimedate.day = cmtime->tm_mday;
2348 req->data.definetimedate.hour = cmtime->tm_hour;
2349 req->data.definetimedate.minute = cmtime->tm_min;
2350 req->data.definetimedate.seconds = cmtime->tm_sec;
2351 transmit_response(s, req);
2353 case SPEED_DIAL_STAT_REQ_MESSAGE:
2354 /* Not really sure how Speed Dial's are different than the
2355 Softkey templates */
2356 speedDialNum = req->data.speeddialreq.speedDialNumber;
2357 memset(req, 0, SKINNY_MAX_PACKET);
2358 req->len = sizeof(speed_dial_stat_res_message)+4;
2359 req->e = SPEED_DIAL_STAT_RES_MESSAGE;
2361 /* XXX Do this right XXX */
2362 /* If the redial function works the way I think it will, a modification of it
2363 can work here was well. Yikes. */
2364 req->data.speeddialreq.speedDialNumber = speedDialNum;
2365 snprintf(req->data.speeddial.speedDialDirNumber, sizeof(req->data.speeddial.speedDialDirNumber), "31337");
2366 snprintf(req->data.speeddial.speedDialDisplayName, sizeof(req->data.speeddial.speedDialDisplayName),"Asterisk Rules!");
2368 transmit_response(s, req);
2370 case LINE_STATE_REQ_MESSAGE:
2371 lineNumber = req->data.line.lineNumber;
2373 ast_verbose("Received LineStateReq\n");
2375 memset(req, 0, SKINNY_MAX_PACKET);
2376 req->len = sizeof(line_stat_res_message)+4;
2377 req->e = LINE_STAT_RES_MESSAGE;
2378 sub = find_subchannel_by_line(s->device->lines);
2380 ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
2383 lines = sub->parent;
2384 ast_mutex_lock(&devicelock);
2385 for (i=1; i < lineNumber; i++) {
2386 lines = lines->next;
2388 ast_mutex_unlock(&devicelock);
2389 req->data.linestat.linenumber = lineNumber;
2390 memcpy(req->data.linestat.lineDirNumber, lines->name,
2391 sizeof(req->data.linestat.lineDirNumber));
2392 memcpy(req->data.linestat.lineDisplayName, lines->label,
2393 sizeof(req->data.linestat.lineDisplayName));
2394 transmit_response(s,req);
2396 case CAPABILITIES_RES_MESSAGE:
2398 ast_verbose("Received CapabilitiesRes\n");
2400 /* XXX process the capabilites */
2402 case KEEP_ALIVE_MESSAGE:
2403 memset(req, 0, SKINNY_MAX_PACKET);
2405 req->e = KEEP_ALIVE_ACK_MESSAGE;
2406 transmit_response(s, req);
2410 case OFFHOOK_MESSAGE:
2411 transmit_ringer_mode(s,SKINNY_RING_OFF);
2412 transmit_lamp_indication(s, STIMULUS_LINE, s->device->lines->instance, SKINNY_LAMP_ON);
2414 sub = find_subchannel_by_line(s->device->lines);
2416 ast_log(LOG_NOTICE, "No available lines on: %s\n", s->device->name);
2419 sub->parent->hookstate = SKINNY_OFFHOOK;
2421 if (sub->outgoing) {
2422 transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
2423 transmit_tone(s, SKINNY_SILENCE);
2424 ast_setstate(sub->owner, AST_STATE_UP);
2425 /* XXX select the appropriate soft key here */
2428 transmit_callstate(s, s->device->lines->instance, SKINNY_OFFHOOK, sub->callid);
2430 ast_verbose("Attempting to Clear display on Skinny %s@%s\n",sub->parent->name, sub->parent->parent->name);
2432 transmit_displaymessage(s, 0); /* clear display */
2433 transmit_tone(s, SKINNY_DIALTONE);
2434 c = skinny_new(sub, AST_STATE_DOWN);
2437 if (ast_pthread_create(&t, NULL, skinny_ss, c)) {
2438 ast_log(LOG_WARNING, "Unable to create switch thread: %s\n", strerror(errno));
2442 ast_log(LOG_WARNING, "Unable to create channel for %s@%s\n", sub->parent->name, s->device->name);
2446 ast_log(LOG_DEBUG, "Current sub [%s] already has owner\n", sub->owner->name);
2450 case ONHOOK_MESSAGE:
2451 sub = find_subchannel_by_line(s->device->lines);
2452 if (sub->parent->hookstate == SKINNY_ONHOOK) {
2453 /* Somthing else already put us back on hook */
2456 sub->cxmode = SKINNY_CX_RECVONLY;
2457 sub->parent->hookstate = SKINNY_ONHOOK;
2458 transmit_callstate(s, s->device->lines->instance, sub->parent->hookstate,sub->callid);
2460 ast_verbose("Skinny %s@%s went on hook\n",sub->parent->name, sub->parent->parent->name);
2462 if (sub->parent->transfer && (sub->owner && sub->next->owner) && ((!sub->outgoing) || (!sub->next->outgoing))) {
2463 /* We're allowed to transfer, we have two active calls and */
2464 /* we made at least one of the calls. Let's try and transfer */
2466 if ((res = attempt_transfer(p)) < 0) {
2467 if (p->sub->next->owner) {
2468 sub->next->alreadygone = 1;
2469 ast_queue_hangup(sub->next->owner,1);
2472 ast_log(LOG_WARNING, "Transfer attempt failed\n");
2477 /* Hangup the current call */
2478 /* If there is another active call, skinny_hangup will ring the phone with the other call */
2480 sub->alreadygone = 1;
2481 ast_queue_hangup(sub->owner);
2483 ast_log(LOG_WARNING, "Skinny(%s@%s-%d) channel already destroyed\n",
2484 sub->parent->name, sub->parent->parent->name, sub->callid);
2488 if ((sub->parent->hookstate == SKINNY_ONHOOK) && (!sub->next->rtp)) {
2493 case KEYPAD_BUTTON_MESSAGE:
2494 digit = req->data.keypad.button;
2496 ast_verbose("Collected digit: [%d]\n", digit);
2498 f.frametype = AST_FRAME_DTMF;
2501 } else if (digit == 15) {
2503 } else if (digit >=0 && digit <= 9) {
2506 /* digit=10-13 (A,B,C,D ?), or
2507 * digit is bad value
2509 * probably should not end up here, but set
2510 * value for backward compatibility, and log
2514 ast_log(LOG_WARNING, "Unsupported digit %d\n", digit);
2519 sub = find_subchannel_by_line(s->device->lines);
2522 /* XXX MUST queue this frame to all subs in threeway call if threeway call is active */
2523 ast_queue_frame(sub->owner, &f);
2524 if (sub->next->owner) {
2525 ast_queue_frame(sub->next->owner, &f);
2528 ast_verbose("No owner: %s\n", s->device->lines->name);
2531 case OPEN_RECIEVE_CHANNEL_ACK_MESSAGE:
2532 ast_verbose("Recieved Open Recieve Channel Ack\n");
2533 status = req->data.openrecievechannelack.status;
2535 ast_log(LOG_ERROR, "Open Recieve Channel Failure\n");
2538 memcpy(addr, req->data.openrecievechannelack.ipAddr, sizeof(addr));
2539 port = req->data.openrecievechannelack.port;
2541 sin.sin_family = AF_INET;
2542 /* I smell endian problems */
2543 memcpy(&sin.sin_addr, addr, sizeof(sin.sin_addr));
2544 sin.sin_port = htons(port);
2546 sub = find_subchannel_by_line(s->device->lines);
2548 ast_rtp_set_peer(sub->rtp, &sin);
2549 ast_rtp_get_us(sub->rtp, &us);
2551 ast_log(LOG_ERROR, "No RTP structure, this is very bad\n");
2554 memset(req, 0, SKINNY_MAX_PACKET);
2555 req->len = sizeof(start_media_transmission_message)+4;
2556 req->e = START_MEDIA_TRANSMISSION_MESSAGE;
2557 req->data.startmedia.conferenceId = 0;
2558 req->data.startmedia.passThruPartyId = 0;
2559 memcpy(req->data.startmedia.remoteIp, &s->device->ourip, 4); /* Endian? */
2560 req->data.startmedia.remotePort = ntohs(us.sin_port);
2561 req->data.startmedia.packetSize = 20;
2562 req->data.startmedia.payloadType = convert_cap(s->device->lines->capability);
2563 req->data.startmedia.qualifier.precedence = 127;
2564 req->data.startmedia.qualifier.vad = 0;
2565 req->data.startmedia.qualifier.packets = 0;
2566 req->data.startmedia.qualifier.bitRate = 0;
2567 transmit_response(s, req);
2570 ast_verbose("RECEIVED UNKNOWN MESSAGE TYPE: %x\n", req->e);
2579 static void destroy_session(struct skinnysession *s)
2581 struct skinnysession *cur, *prev = NULL;
2582 ast_mutex_lock(&sessionlock);
2592 prev->next = cur->next;
2594 sessions = cur->next;
2597 ast_mutex_destroy(&s->lock);
2600 ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
2601 ast_mutex_unlock(&sessionlock);
2604 static int get_input(struct skinnysession *s)
2608 struct pollfd fds[1];
2611 fds[0].events = POLLIN;
2612 res = poll(fds, 1, -1);
2615 ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
2616 } else if (res > 0) {
2617 memset(s->inbuf,0,sizeof(s->inbuf));
2618 res = read(s->fd, s->inbuf, 4);
2620 ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
2623 dlen = *(int *)s->inbuf;
2624 if (dlen+8 > sizeof(s->inbuf))
2625 dlen = sizeof(s->inbuf) - 8;
2626 *(int *)s->inbuf = dlen;
2627 res = read(s->fd, s->inbuf+4, dlen+4);
2628 ast_mutex_unlock(&s->lock);
2629 if (res != (dlen+4)) {
2630 ast_log(LOG_WARNING, "Skinny Client sent less data than expected.\n");
2637 static skinny_req *skinny_req_parse(struct skinnysession *s)
2641 req = malloc(SKINNY_MAX_PACKET);
2643 ast_log(LOG_ERROR, "Unable to allocate skinny_request, this is bad\n");
2646 memset(req, 0, sizeof(skinny_req));
2647 /* +8 to account for reserved and length fields */
2648 memcpy(req, s->inbuf, *(int*)(s->inbuf)+8);
2650 ast_log(LOG_ERROR, "Event Message is NULL from socket %d, This is bad\n", s->fd);
2657 static void *skinny_session(void *data)
2661 struct skinnysession *s = data;
2662 char iabuf[INET_ADDRSTRLEN];
2664 ast_verbose(VERBOSE_PREFIX_3 "Starting Skinny session from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
2671 req = skinny_req_parse(s);
2675 res = handle_message(req, s);
2681 ast_log(LOG_NOTICE, "Skinny Session returned: %s\n", strerror(errno));
2686 static void *accept_thread(void *ignore)
2689 struct sockaddr_in sin;
2691 struct skinnysession *s;
2694 pthread_attr_t attr;
2696 pthread_attr_init(&attr);
2697 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2700 sinlen = sizeof(sin);
2701 as = accept(skinnysock, (struct sockaddr *)&sin, &sinlen);
2703 ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
2706 p = getprotobyname("tcp");
2708 if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
2709 ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
2712 s = malloc(sizeof(struct skinnysession));
2714 ast_log(LOG_WARNING, "Failed to allocate Skinny session: %s\n", strerror(errno));
2717 memset(s, 0, sizeof(struct skinnysession));
2718 memcpy(&s->sin, &sin, sizeof(sin));
2719 ast_mutex_init(&s->lock);
2721 ast_mutex_lock(&sessionlock);
2724 ast_mutex_unlock(&sessionlock);
2726 if (ast_pthread_create(&tcp_thread, NULL, skinny_session, s)) {
2733 ast_verbose("killing accept thread\n");
2739 static void *do_monitor(void *data)
2743 /* This thread monitors all the interfaces which are not yet in use
2744 (and thus do not have a separate thread) indefinitely */
2745 /* From here on out, we die whenever asked */
2747 pthread_testcancel();
2748 /* Wait for sched or io */
2749 res = ast_sched_wait(sched);
2750 if ((res < 0) || (res > 1000)) {
2753 res = ast_io_wait(io, res);
2754 ast_mutex_lock(&monlock);
2756 ast_sched_runq(sched);
2758 ast_mutex_unlock(&monlock);
2765 static int restart_monitor(void)
2767 /* If we're supposed to be stopped -- stay stopped */
2768 if (monitor_thread == AST_PTHREADT_STOP)
2770 if (ast_mutex_lock(&monlock)) {
2771 ast_log(LOG_WARNING, "Unable to lock monitor\n");
2774 if (monitor_thread == pthread_self()) {
2775 ast_mutex_unlock(&monlock);
2776 ast_log(LOG_WARNING, "Cannot kill myself\n");
2779 if (monitor_thread != AST_PTHREADT_NULL) {
2780 /* Wake up the thread */
2781 pthread_kill(monitor_thread, SIGURG);
2783 /* Start a new monitor */
2784 if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
2785 ast_mutex_unlock(&monlock);
2786 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2790 ast_mutex_unlock(&monlock);
2794 static struct ast_channel *skinny_request(const char *type, int format, void *data, int *cause)
2797 struct skinny_subchannel *sub;
2798 struct ast_channel *tmpc = NULL;
2803 format &= capability;
2805 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
2809 strncpy(tmp, dest, sizeof(tmp) - 1);
2810 if (ast_strlen_zero(tmp)) {
2811 ast_log(LOG_NOTICE, "Skinny channels require a device\n");
2815 sub = find_subchannel_by_name(tmp);
2817 ast_log(LOG_NOTICE, "No available lines on: %s\n", dest);
2821 if (option_verbose > 2) {
2822 ast_verbose(VERBOSE_PREFIX_3 "skinny_request(%s)\n", tmp);
2823 ast_verbose(VERBOSE_PREFIX_3 "Skinny cw: %d, dnd: %d, so: %d, sno: %d\n",
2824 sub->parent->callwaiting, sub->parent->dnd, sub->owner ? 1 : 0, sub->next->owner ? 1: 0);
2826 tmpc = skinny_new(sub->owner ? sub->next : sub, AST_STATE_DOWN);
2828 ast_log(LOG_WARNING, "Unable to make channel for '%s'\n", tmp);
2836 static int reload_config(void)
2839 struct ast_config *cfg;
2840 struct ast_variable *v;
2843 char iabuf[INET_ADDRSTRLEN];
2844 struct skinny_device *d;
2845 int oldport = ntohs(bindaddr.sin_port);
2848 hp = ast_gethostbyname(ourhost, &ahp);
2850 ast_log(LOG_WARNING, "Unable to get hostname, Skinny disabled\n");
2854 cfg = ast_config_load(config);
2856 /* We *must* have a config file otherwise stop immediately */
2858 ast_log(LOG_NOTICE, "Unable to load config %s, Skinny disabled\n", config);
2862 /* load the general section */
2863 memset(&bindaddr, 0, sizeof(bindaddr));
2864 v = ast_variable_browse(cfg, "general");
2866 /* Create the interface list */
2867 if (!strcasecmp(v->name, "bindaddr")) {
2868 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
2869 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2871 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2873 } else if (!strcasecmp(v->name, "keepAlive")) {
2874 keep_alive = atoi(v->value);
2875 } else if (!strcasecmp(v->name, "dateFormat")) {
2876 strncpy(date_format, v->value, sizeof(date_format) - 1);
2877 } else if (!strcasecmp(v->name, "allow")) {
2878 format = ast_getformatbyname(v->value);
2880 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
2882 capability |= format;
2883 } else if (!strcasecmp(v->name, "disallow")) {
2884 format = ast_getformatbyname(v->value);
2886 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
2888 capability &= ~format;
2889 } else if (!strcasecmp(v->name, "port")) {
2890 if (sscanf(v->value, "%i", &ourport) == 1) {
2891 bindaddr.sin_port = htons(ourport);
2893 ast_log(LOG_WARNING, "Invalid port number '%s' at line %d of %s\n", v->value, v->lineno, config);
2899 if (ntohl(bindaddr.sin_addr.s_addr)) {
2900 memcpy(&__ourip, &bindaddr.sin_addr, sizeof(__ourip));
2902 hp = ast_gethostbyname(ourhost, &ahp);
2904 ast_log(LOG_WARNING, "Unable to get our IP address, Skinny disabled\n");
2905 ast_config_destroy(cfg);
2908 memcpy(&__ourip, hp->h_addr, sizeof(__ourip));
2910 if (!ntohs(bindaddr.sin_port)) {
2911 bindaddr.sin_port = ntohs(DEFAULT_SKINNY_PORT);
2913 bindaddr.sin_family = AF_INET;
2915 /* load the device sections */
2916 cat = ast_category_browse(cfg, NULL);
2918 if (strcasecmp(cat, "general")) {
2919 d = build_device(cat, ast_variable_browse(cfg, cat));
2921 if (option_verbose > 2) {
2922 ast_verbose(VERBOSE_PREFIX_3 "Added device '%s'\n", d->name);
2924 ast_mutex_lock(&devicelock);
2927 ast_mutex_unlock(&devicelock);
2930 cat = ast_category_browse(cfg, cat);
2932 ast_mutex_lock(&netlock);
2933 if ((skinnysock > -1) && (ntohs(bindaddr.sin_port) != oldport)) {
2937 if (skinnysock < 0) {
2938 skinnysock = socket(AF_INET, SOCK_STREAM, 0);
2939 if(setsockopt(skinnysock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
2940 ast_log(LOG_ERROR, "Set Socket Options failed: errno %d, %s", errno, strerror(errno));
2941 ast_config_destroy(cfg);
2945 if (skinnysock < 0) {
2946 ast_log(LOG_WARNING, "Unable to create Skinny socket: %s\n", strerror(errno));
2948 if (bind(skinnysock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
2949 ast_log(LOG_WARNING, "Failed to bind to %s:%d: %s\n",
2950 ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port),
2954 ast_config_destroy(cfg);
2958 if (listen(skinnysock,DEFAULT_SKINNY_BACKLOG)) {
2959 ast_log(LOG_WARNING, "Failed to start listening to %s:%d: %s\n",
2960 ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port),
2964 ast_config_destroy(cfg);
2968 if (option_verbose > 1)
2969 ast_verbose(VERBOSE_PREFIX_2 "Skinny listening on %s:%d\n",
2970 ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port));
2972 ast_pthread_create(&accept_t,NULL, accept_thread, NULL);
2975 ast_mutex_unlock(&netlock);
2977 /* and unload the configuration when were done */
2978 ast_config_destroy(cfg);
2984 void delete_devices(void)
2986 struct skinny_device *d, *dlast;
2987 struct skinny_line *l, *llast;
2988 struct skinny_subchannel *sub, *slast;
2990 ast_mutex_lock(&devicelock);
2992 /* Delete all devices */
2993 for (d=devices;d;) {
2995 /* Delete all lines for this device */
2996 for (l=d->lines;l;) {
2997 /* Delete all subchannels for this line */
2998 for (sub=l->sub;sub;) {
3001 ast_mutex_destroy(&slast->lock);
3006 ast_mutex_destroy(&llast->lock);
3014 ast_mutex_unlock(&devicelock);
3021 /* XXX Causes Seg - needs to be fixed, or? */
3035 /* load and parse config */
3036 res = reload_config();
3038 /* Announce our presence to Asterisk */
3040 /* Make sure we can register our skinny channel type */
3041 if (ast_channel_register(&skinny_tech)) {
3042 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
3046 ast_rtp_proto_register(&skinny_rtp);
3047 ast_cli_register(&cli_show_lines);
3048 ast_cli_register(&cli_debug);
3049 ast_cli_register(&cli_no_debug);
3050 sched = sched_context_create();
3052 ast_log(LOG_WARNING, "Unable to create schedule context\n");
3054 io = io_context_create();
3056 ast_log(LOG_WARNING, "Unable to create I/O context\n");
3058 /* And start the monitor for the first time */
3066 struct skinny_session *session, s;
3067 struct skinny_subchannel *sub;
3068 struct skinny_line *line = session;
3070 /* close all IP connections */
3071 if (!ast_mutex_lock(&devicelock)) {
3072 /* Terminate tcp listener thread */
3076 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
3079 if (!ast_mutex_lock(&monlock)) {
3080 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
3081 pthread_cancel(monitor_thread);
3082 pthread_kill(monitor_thread, SIGURG);
3083 pthread_join(monitor_thread, NULL);
3085 monitor_thread = AST_PTHREADT_STOP;