1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2010, Mathieu Parent <math.parent@gmail.com>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Mathieu Parent <math.parent@gmail.com>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Mathieu Parent <math.parent@gmail.com>
27  *
28  *
29  * skinny_protocol.c -- Skinny Call Control Protocol (SCCP) Endpoint Module
30  *
31  */
32 #include <switch.h>
33 #include "mod_skinny.h"
34 #include "skinny_protocol.h"
35 #include "skinny_tables.h"
36 
37 /*****************************************************************************/
38 /* SKINNY FUNCTIONS */
39 /*****************************************************************************/
skinny_codec2string(skinny_codecs skinnycodec)40 char* skinny_codec2string(skinny_codecs skinnycodec)
41 {
42 	switch (skinnycodec) {
43 		case SKINNY_CODEC_ALAW_64K:
44 		case SKINNY_CODEC_ALAW_56K:
45 			return "PCMA";
46 		case SKINNY_CODEC_ULAW_64K:
47 		case SKINNY_CODEC_ULAW_56K:
48 			return "PCMU";
49 		case SKINNY_CODEC_G722_64K:
50 		case SKINNY_CODEC_G722_56K:
51 		case SKINNY_CODEC_G722_48K:
52 			return "G722";
53 		case SKINNY_CODEC_G723_1:
54 			return "G723";
55 		case SKINNY_CODEC_G728:
56 			return "G728";
57 		case SKINNY_CODEC_G729:
58 		case SKINNY_CODEC_G729A:
59 			return "G729";
60 		case SKINNY_CODEC_IS11172:
61 			return "IS11172";
62 		case SKINNY_CODEC_IS13818:
63 			return "IS13818";
64 		case SKINNY_CODEC_G729B:
65 		case SKINNY_CODEC_G729AB:
66 			return "G729";
67 		case SKINNY_CODEC_GSM_FULL:
68 		case SKINNY_CODEC_GSM_HALF:
69 		case SKINNY_CODEC_GSM_EFULL:
70 			return "GSM";
71 		case SKINNY_CODEC_WIDEBAND_256K:
72 			return "WIDEBAND";
73 		case SKINNY_CODEC_DATA_64K:
74 		case SKINNY_CODEC_DATA_56K:
75 			return "DATA";
76 		case SKINNY_CODEC_GSM:
77 			return "GSM";
78 		case SKINNY_CODEC_ACTIVEVOICE:
79 			return "ACTIVEVOICE";
80 		case SKINNY_CODEC_G726_32K:
81 		case SKINNY_CODEC_G726_24K:
82 		case SKINNY_CODEC_G726_16K:
83 			return "G726";
84 		case SKINNY_CODEC_G729B_BIS:
85 		case SKINNY_CODEC_G729B_LOW:
86 			return "G729";
87 		case SKINNY_CODEC_H261:
88 			return "H261";
89 		case SKINNY_CODEC_H263:
90 			return "H263";
91 		case SKINNY_CODEC_VIDEO:
92 			return "VIDEO";
93 		case SKINNY_CODEC_T120:
94 			return "T120";
95 		case SKINNY_CODEC_H224:
96 			return "H224";
97 		case SKINNY_CODEC_RFC2833_DYNPAYLOAD:
98 			return "RFC2833_DYNPAYLOAD";
99 		default:
100 			return "";
101 	}
102 }
103 
104 /*****************************************************************************/
skinny_read_packet(listener_t * listener,skinny_message_t ** req)105 switch_status_t skinny_read_packet(listener_t *listener, skinny_message_t **req)
106 {
107 	skinny_message_t *request;
108 	switch_size_t mlen, bytes = 0;
109 	char mbuf[SKINNY_MESSAGE_MAXSIZE] = "";
110 	char *ptr;
111 	switch_status_t status = SWITCH_STATUS_SUCCESS;
112 
113 	request = calloc(1,SKINNY_MESSAGE_MAXSIZE);
114 
115 	if (!request) {
116 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to allocate memory.\n");
117 		return SWITCH_STATUS_MEMERR;
118 	}
119 
120 	ptr = mbuf;
121 
122 	while (listener_is_ready(listener)) {
123 		uint8_t do_sleep = 1;
124 		if (listener->expire_time && listener->expire_time < switch_epoch_time_now(NULL)) {
125 			switch_safe_free(request);
126 			return SWITCH_STATUS_TIMEOUT;
127 		}
128 		if(bytes < SKINNY_MESSAGE_FIELD_SIZE) {
129 			/* We have nothing yet, get length header field */
130 			mlen = SKINNY_MESSAGE_FIELD_SIZE - bytes;
131 		} else {
132 			/* We now know the message size */
133 			mlen = request->length + 2*SKINNY_MESSAGE_FIELD_SIZE - bytes;
134 		}
135 
136 		status = switch_socket_recv(listener->sock, ptr, &mlen);
137 
138 		if (listener->expire_time && listener->expire_time < switch_epoch_time_now(NULL)) {
139 			switch_safe_free(request);
140 			return SWITCH_STATUS_TIMEOUT;
141 		}
142 
143 		if (!listener_is_ready(listener)) {
144 			switch_safe_free(request);
145 			break;
146 		}
147 		if (!switch_status_is_timeup(status) && !SWITCH_STATUS_IS_BREAK(status) && (status != SWITCH_STATUS_SUCCESS)) {
148 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Socket break with status=%d.\n", status);
149 			switch_safe_free(request);
150 			return SWITCH_STATUS_FALSE;
151 		}
152 
153 		if(mlen) {
154 			bytes += mlen;
155 
156 			if(bytes >= SKINNY_MESSAGE_FIELD_SIZE) {
157 				do_sleep = 0;
158 				ptr += mlen;
159 				memcpy(request, mbuf, bytes);
160 #ifdef SKINNY_MEGA_DEBUG
161 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
162 						"Got request: length=%d,version=%x,type=%x\n",
163 						request->length,request->version,request->type);
164 #endif
165 				if(request->length < SKINNY_MESSAGE_FIELD_SIZE) {
166 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
167 							"Skinny client sent invalid data. Length should be greater than 4 but got %d.\n",
168 							request->length);
169 					switch_safe_free(request);
170 					return SWITCH_STATUS_FALSE;
171 				}
172 				if(request->length + 2*SKINNY_MESSAGE_FIELD_SIZE > SKINNY_MESSAGE_MAXSIZE) {
173 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
174 							"Skinny client sent too huge data. Got %d which is above threshold %d.\n",
175 							request->length, SKINNY_MESSAGE_MAXSIZE - 2*SKINNY_MESSAGE_FIELD_SIZE);
176 					switch_safe_free(request);
177 					return SWITCH_STATUS_FALSE;
178 				}
179 				if(bytes >= request->length + 2*SKINNY_MESSAGE_FIELD_SIZE) {
180 					/* Message body */
181 					*req = request;
182 					/* Do not free here, caller needs to do it */
183 					return  SWITCH_STATUS_SUCCESS;
184 				}
185 			}
186 		}
187 
188 		if (listener->digit_timeout_time && listener->digit_timeout_time < switch_mono_micro_time_now()) {
189 			switch_safe_free(request);
190 			return SWITCH_STATUS_TIMEOUT;
191 		}
192 
193 		if (do_sleep) {
194 			switch_cond_next();
195 		}
196 	}
197 
198 	switch_safe_free(request);
199 	return SWITCH_STATUS_SUCCESS;
200 }
201 
202 /*****************************************************************************/
skinny_device_event_callback(void * pArg,int argc,char ** argv,char ** columnNames)203 int skinny_device_event_callback(void *pArg, int argc, char **argv, char **columnNames)
204 {
205 	switch_event_t *event = (switch_event_t *) pArg;
206 
207 	char *profile_name = argv[0];
208 	char *device_name = argv[1];
209 	char *user_id = argv[2];
210 	char *device_instance = argv[3];
211 	char *ip = argv[4];
212 	char *device_type = argv[5];
213 	char *max_streams = argv[6];
214 	char *port = argv[7];
215 	char *codec_string = argv[8];
216 
217 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Profile-Name", "%s", profile_name);
218 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Device-Name", "%s", device_name);
219 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Station-User-Id", "%s", user_id);
220 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Station-Instance", "%s", device_instance);
221 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-IP-Address", "%s", ip);
222 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Device-Type", "%s", device_type);
223 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Max-Streams", "%s", max_streams);
224 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Port", "%s", port);
225 	switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Skinny-Codecs", "%s", codec_string);
226 
227 	return 0;
228 }
229 
skinny_device_event(listener_t * listener,switch_event_t ** ev,switch_event_types_t event_id,const char * subclass_name)230 switch_status_t skinny_device_event(listener_t *listener, switch_event_t **ev, switch_event_types_t event_id, const char *subclass_name)
231 {
232 	switch_event_t *event = NULL;
233 	char *sql;
234 	skinny_profile_t *profile;
235 	assert(listener->profile);
236 	profile = listener->profile;
237 
238 	switch_event_create_subclass(&event, event_id, subclass_name);
239 	switch_assert(event);
240 	if ((sql = switch_mprintf("SELECT '%q', name, user_id, instance, ip, type, max_streams, port, codec_string "
241 					"FROM skinny_devices "
242 					"WHERE name='%q' AND instance=%d",
243 					listener->profile->name,
244 					listener->device_name, listener->device_instance))) {
245 		skinny_execute_sql_callback(profile, profile->sql_mutex, sql, skinny_device_event_callback, event);
246 		switch_safe_free(sql);
247 	}
248 
249 	*ev = event;
250 	return SWITCH_STATUS_SUCCESS;
251 }
252 
253 
254 /*****************************************************************************/
255 /*****************************************************************************/
skinny_session_walk_lines(skinny_profile_t * profile,char * channel_uuid,switch_core_db_callback_func_t callback,void * data)256 switch_status_t skinny_session_walk_lines(skinny_profile_t *profile, char *channel_uuid, switch_core_db_callback_func_t callback, void *data)
257 {
258 	char *sql;
259 	if ((sql = switch_mprintf(
260 					"SELECT skinny_lines.*, channel_uuid, call_id, call_state "
261 					"FROM skinny_active_lines "
262 					"INNER JOIN skinny_lines "
263 					"ON skinny_active_lines.device_name = skinny_lines.device_name "
264 					"AND skinny_active_lines.device_instance = skinny_lines.device_instance "
265 					"AND skinny_active_lines.line_instance = skinny_lines.line_instance "
266 					"WHERE channel_uuid='%q'",
267 					channel_uuid))) {
268 		skinny_execute_sql_callback(profile, profile->sql_mutex, sql, callback, data);
269 		switch_safe_free(sql);
270 	}
271 	return SWITCH_STATUS_SUCCESS;
272 }
273 
skinny_session_walk_lines_by_call_id(skinny_profile_t * profile,uint32_t call_id,switch_core_db_callback_func_t callback,void * data)274 switch_status_t skinny_session_walk_lines_by_call_id(skinny_profile_t *profile, uint32_t call_id, switch_core_db_callback_func_t callback, void *data)
275 {
276 	char *sql;
277 	if ((sql = switch_mprintf(
278 					"SELECT skinny_lines.*, channel_uuid, call_id, call_state "
279 					"FROM skinny_active_lines "
280 					"INNER JOIN skinny_lines "
281 					"ON skinny_active_lines.device_name = skinny_lines.device_name "
282 					"AND skinny_active_lines.device_instance = skinny_lines.device_instance "
283 					"AND skinny_active_lines.line_instance = skinny_lines.line_instance "
284 					"WHERE call_id='%d'",
285 					call_id))) {
286 		skinny_execute_sql_callback(profile, profile->sql_mutex, sql, callback, data);
287 		switch_safe_free(sql);
288 	}
289 	return SWITCH_STATUS_SUCCESS;
290 }
291 
292 /*****************************************************************************/
293 /* SKINNY BUTTONS */
294 /*****************************************************************************/
295 struct line_get_helper {
296 	uint32_t pos;
297 	struct line_stat_res_message *button;
298 };
299 
skinny_line_get_callback(void * pArg,int argc,char ** argv,char ** columnNames)300 int skinny_line_get_callback(void *pArg, int argc, char **argv, char **columnNames)
301 {
302 	struct line_get_helper *helper = pArg;
303 
304 	helper->pos++;
305 	if (helper->pos == atoi(argv[0])) { /* wanted_position */
306 		helper->button->number = helper->pos;
307 		switch_copy_string(helper->button->name,  argv[2], 24); /* label */
308 		switch_copy_string(helper->button->shortname,  argv[3], 40); /* value */
309 		switch_copy_string(helper->button->displayname,  argv[4], 44); /* caller_name */
310 	}
311 	return 0;
312 }
313 
skinny_line_get(listener_t * listener,uint32_t instance,struct line_stat_res_message ** button)314 void skinny_line_get(listener_t *listener, uint32_t instance, struct line_stat_res_message **button)
315 {
316 	struct line_get_helper helper = {0};
317 	char *sql;
318 
319 	switch_assert(listener);
320 	switch_assert(listener->profile);
321 	switch_assert(listener->device_name);
322 
323 	helper.button = calloc(sizeof(struct line_stat_res_message),1);
324 
325 	if ((sql = switch_mprintf(
326 					"SELECT '%d' AS wanted_position, position, label, value, caller_name "
327 					"FROM skinny_lines "
328 					"WHERE device_name='%q' AND device_instance=%d "
329 					"ORDER BY position",
330 					instance,
331 					listener->device_name, listener->device_instance
332 				 ))) {
333 		skinny_execute_sql_callback(listener->profile, listener->profile->sql_mutex, sql, skinny_line_get_callback, &helper);
334 		switch_safe_free(sql);
335 	}
336 	*button = helper.button;
337 }
338 
339 struct speed_dial_get_helper {
340 	uint32_t pos;
341 	struct speed_dial_stat_res_message *button;
342 };
343 
skinny_speed_dial_get_callback(void * pArg,int argc,char ** argv,char ** columnNames)344 int skinny_speed_dial_get_callback(void *pArg, int argc, char **argv, char **columnNames)
345 {
346 	struct speed_dial_get_helper *helper = pArg;
347 
348 	helper->pos++;
349 	if (helper->pos == atoi(argv[0])) { /* wanted_position */
350 		helper->button->number = helper->pos; /* value */
351 		switch_copy_string(helper->button->line,  argv[3], 24); /* value */
352 		switch_copy_string(helper->button->label,  argv[2], 40); /* label */
353 	}
354 	return 0;
355 }
356 
skinny_speed_dial_get(listener_t * listener,uint32_t instance,struct speed_dial_stat_res_message ** button)357 void skinny_speed_dial_get(listener_t *listener, uint32_t instance, struct speed_dial_stat_res_message **button)
358 {
359 	struct speed_dial_get_helper helper = {0};
360 	char *sql;
361 
362 	switch_assert(listener);
363 	switch_assert(listener->profile);
364 	switch_assert(listener->device_name);
365 
366 	helper.button = calloc(sizeof(struct speed_dial_stat_res_message),1);
367 
368 	if ((sql = switch_mprintf(
369 					"SELECT '%d' AS wanted_position, position, label, value, settings "
370 					"FROM skinny_buttons "
371 					"WHERE device_name='%q' AND device_instance=%d AND type=%d "
372 					"ORDER BY position",
373 					instance,
374 					listener->device_name, listener->device_instance,
375 					SKINNY_BUTTON_SPEED_DIAL
376 				 ))) {
377 		skinny_execute_sql_callback(listener->profile, listener->profile->sql_mutex, sql, skinny_speed_dial_get_callback, &helper);
378 		switch_safe_free(sql);
379 	}
380 	*button = helper.button;
381 }
382 
383 struct service_url_get_helper {
384 	uint32_t pos;
385 	struct service_url_stat_res_message *button;
386 };
387 
skinny_service_url_get_callback(void * pArg,int argc,char ** argv,char ** columnNames)388 int skinny_service_url_get_callback(void *pArg, int argc, char **argv, char **columnNames)
389 {
390 	struct service_url_get_helper *helper = pArg;
391 
392 	helper->pos++;
393 	if (helper->pos == atoi(argv[0])) { /* wanted_position */
394 		helper->button->index = helper->pos;
395 		switch_copy_string(helper->button->url, argv[3], 256); /* value */
396 		switch_copy_string(helper->button->display_name,  argv[2], 40); /* label */
397 	}
398 	return 0;
399 }
400 
skinny_service_url_get(listener_t * listener,uint32_t instance,struct service_url_stat_res_message ** button)401 void skinny_service_url_get(listener_t *listener, uint32_t instance, struct service_url_stat_res_message **button)
402 {
403 	struct service_url_get_helper helper = {0};
404 	char *sql;
405 
406 	switch_assert(listener);
407 	switch_assert(listener->profile);
408 	switch_assert(listener->device_name);
409 
410 	helper.button = calloc(sizeof(struct service_url_stat_res_message), 1);
411 
412 	if ((sql = switch_mprintf(
413 					"SELECT '%d' AS wanted_position, position, label, value, settings "
414 					"FROM skinny_buttons "
415 					"WHERE device_name='%q' AND device_instance=%d AND type=%d "
416 					"ORDER BY position",
417 					instance,
418 					listener->device_name,
419 					listener->device_instance,
420 					SKINNY_BUTTON_SERVICE_URL
421 				 ))) {
422 		skinny_execute_sql_callback(listener->profile, listener->profile->sql_mutex, sql, skinny_service_url_get_callback, &helper);
423 		switch_safe_free(sql);
424 	}
425 	*button = helper.button;
426 }
427 
428 struct feature_get_helper {
429 	uint32_t pos;
430 	struct feature_stat_res_message *button;
431 };
432 
skinny_feature_get_callback(void * pArg,int argc,char ** argv,char ** columnNames)433 int skinny_feature_get_callback(void *pArg, int argc, char **argv, char **columnNames)
434 {
435 	struct feature_get_helper *helper = pArg;
436 
437 	helper->pos++;
438 	if (helper->pos == atoi(argv[0])) { /* wanted_position */
439 		helper->button->index = helper->pos;
440 		helper->button->id = helper->pos;
441 		switch_copy_string(helper->button->text_label,  argv[2], 40); /* label */
442 		helper->button->status = atoi(argv[3]); /* value */
443 	}
444 	return 0;
445 }
446 
skinny_feature_get(listener_t * listener,uint32_t instance,struct feature_stat_res_message ** button)447 void skinny_feature_get(listener_t *listener, uint32_t instance, struct feature_stat_res_message **button)
448 {
449 	struct feature_get_helper helper = {0};
450 	char *sql;
451 
452 	switch_assert(listener);
453 	switch_assert(listener->profile);
454 	switch_assert(listener->device_name);
455 
456 	helper.button = calloc(sizeof(struct feature_stat_res_message), 1);
457 
458 	if ((sql = switch_mprintf(
459 					"SELECT '%d' AS wanted_position, position, label, value, settings "
460 					"FROM skinny_buttons "
461 					"WHERE device_name='%q' AND device_instance=%d AND NOT (type=%d OR type=%d) "
462 					"ORDER BY position",
463 					instance,
464 					listener->device_name,
465 					listener->device_instance,
466 					SKINNY_BUTTON_SPEED_DIAL, SKINNY_BUTTON_SERVICE_URL
467 				 ))) {
468 		skinny_execute_sql_callback(listener->profile, listener->profile->sql_mutex, sql, skinny_feature_get_callback, &helper);
469 		switch_safe_free(sql);
470 	}
471 	*button = helper.button;
472 }
473 
474 /*****************************************************************************/
475 /* SKINNY MESSAGE SENDER */
476 /*****************************************************************************/
perform_send_keep_alive_ack(listener_t * listener,const char * file,const char * func,int line)477 switch_status_t perform_send_keep_alive_ack(listener_t *listener,
478 		const char *file, const char *func, int line)
479 {
480 	skinny_message_t *message;
481 
482 	skinny_create_empty_message(message, KEEP_ALIVE_ACK_MESSAGE);
483 
484 	if ( listener->profile->debug >= 10 ) {
485 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
486 			"Sending Keep Alive Ack%s\n", "");
487 	}
488 
489 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
490 }
491 
perform_send_register_ack(listener_t * listener,const char * file,const char * func,int line,uint32_t keep_alive,char * date_format,char * reserved,uint32_t secondary_keep_alive,char * reserved2)492 switch_status_t perform_send_register_ack(listener_t *listener,
493 		const char *file, const char *func, int line,
494 		uint32_t keep_alive,
495 		char *date_format,
496 		char *reserved,
497 		uint32_t secondary_keep_alive,
498 		char *reserved2)
499 {
500 	skinny_message_t *message;
501 
502 	skinny_create_message(message, REGISTER_ACK_MESSAGE, reg_ack);
503 
504 	message->data.reg_ack.keep_alive = keep_alive;
505 	memcpy(message->data.reg_ack.date_format, date_format, 6);
506 	switch_copy_string(message->data.reg_ack.reserved, reserved, 2);
507 	message->data.reg_ack.secondary_keep_alive = keep_alive;
508 	switch_copy_string(message->data.reg_ack.reserved2, reserved2, 4);
509 
510 	if ( listener->profile->debug >= 9 ) {
511 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
512 			"Sending Register Ack with Keep Alive (%d), Date Format (%s), Secondary Keep Alive (%d)\n",
513 			keep_alive, date_format, secondary_keep_alive);
514 	}
515 
516 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
517 }
518 
perform_send_speed_dial_stat_res(listener_t * listener,const char * file,const char * func,int line,uint32_t number,char * speed_line,char * speed_label)519 switch_status_t perform_send_speed_dial_stat_res(listener_t *listener,
520 		const char *file, const char *func, int line,
521 		uint32_t number,
522 		char *speed_line,
523 		char *speed_label)
524 {
525 	skinny_message_t *message;
526 
527 	skinny_create_message(message, SPEED_DIAL_STAT_RES_MESSAGE, speed_dial_res);
528 
529 	message->data.speed_dial_res.number = number;
530     switch_copy_string(message->data.speed_dial_res.line, speed_line, 24);
531     switch_copy_string(message->data.speed_dial_res.label, speed_label, 40);
532 
533 	if ( listener->profile->debug >= 9 ) {
534 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
535 			"Sending Speed Dial Stat Res with Number (%d), Line (%s), Label (%s)\n",
536 			number, speed_line, speed_label);
537 	}
538 
539 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
540 }
541 
perform_send_start_tone(listener_t * listener,const char * file,const char * func,int line,uint32_t tone,uint32_t reserved,uint32_t line_instance,uint32_t call_id)542 switch_status_t perform_send_start_tone(listener_t *listener,
543 		const char *file, const char *func, int line,
544 		uint32_t tone,
545 		uint32_t reserved,
546 		uint32_t line_instance,
547 		uint32_t call_id)
548 {
549 	skinny_message_t *message;
550 
551 	skinny_create_message(message, START_TONE_MESSAGE, start_tone);
552 
553 	message->data.start_tone.tone = tone;
554 	message->data.start_tone.reserved = reserved;
555 	message->data.start_tone.line_instance = line_instance;
556 	message->data.start_tone.call_id = call_id;
557 
558 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
559 		"Sending Start Tone with Tone (%s), Line Instance (%d), Call ID (%d)\n",
560 		skinny_tone2str(tone), line_instance, call_id);
561 
562 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
563 }
564 
perform_send_stop_tone(listener_t * listener,const char * file,const char * func,int line,uint32_t line_instance,uint32_t call_id)565 switch_status_t perform_send_stop_tone(listener_t *listener,
566 		const char *file, const char *func, int line,
567 		uint32_t line_instance,
568 		uint32_t call_id)
569 {
570 	skinny_message_t *message;
571 
572 	skinny_create_message(message, STOP_TONE_MESSAGE, stop_tone);
573 
574 	message->data.stop_tone.line_instance = line_instance;
575 	message->data.stop_tone.call_id = call_id;
576 
577 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
578 		"Sending Stop Tone with Line Instance (%d), Call ID (%d)\n", line_instance, call_id);
579 
580 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
581 }
582 
perform_send_set_ringer(listener_t * listener,const char * file,const char * func,int line,uint32_t ring_type,uint32_t ring_mode,uint32_t line_instance,uint32_t call_id)583 switch_status_t perform_send_set_ringer(listener_t *listener,
584 		const char *file, const char *func, int line,
585 		uint32_t ring_type,
586 		uint32_t ring_mode,
587 		uint32_t line_instance,
588 		uint32_t call_id)
589 {
590 	skinny_message_t *message;
591 
592 	skinny_create_message(message, SET_RINGER_MESSAGE, ringer);
593 
594 	message->data.ringer.ring_type = ring_type;
595 	message->data.ringer.ring_mode = ring_mode;
596 	message->data.ringer.line_instance = line_instance;
597 	message->data.ringer.call_id = call_id;
598 
599 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
600 		"Sending SetRinger with Ring Type (%s), Mode (%s), Line Instance (%d), Call ID (%d)\n",
601 		skinny_ring_type2str(ring_type), skinny_ring_mode2str(ring_mode), line_instance, call_id);
602 
603 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
604 }
605 
perform_send_forward_stat(listener_t * listener,const char * file,const char * func,int line,const char * number)606 switch_status_t perform_send_forward_stat(listener_t *listener,
607 		const char *file, const char *func, int line,
608 		const char *number)
609 {
610 	skinny_message_t *message;
611 
612 	skinny_create_message(message, FORWARD_STAT_MESSAGE, forward_stat);
613 
614 	if ( number && number[0] )
615 	{
616 		message->data.forward_stat.active_forward = 1;
617 		message->data.forward_stat.line_instance = 1;
618 		message->data.forward_stat.forward_all_active = 1;
619 		message->data.forward_stat.forward_busy_active = 1;
620 		message->data.forward_stat.forward_noanswer_active = 1;
621 
622 		switch_copy_string(message->data.forward_stat.forward_all_number, number, sizeof(message->data.forward_stat.forward_all_number));
623 		switch_copy_string(message->data.forward_stat.forward_busy_number, number, sizeof(message->data.forward_stat.forward_all_number));
624 		switch_copy_string(message->data.forward_stat.forward_noanswer_number, number, sizeof(message->data.forward_stat.forward_all_number));
625 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
626 			"Sending ForwardStat with Number (%s)\n", number);
627 	}
628 	else
629 	{
630 		skinny_log_l_ffl_msg(listener, file, func, line, SWITCH_LOG_DEBUG,
631 			"Sending ForwardStat with No Number (Inactive)\n");
632 	}
633 
634 
635 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
636 }
637 
perform_send_set_lamp(listener_t * listener,const char * file,const char * func,int line,uint32_t stimulus,uint32_t stimulus_instance,uint32_t mode)638 switch_status_t perform_send_set_lamp(listener_t *listener,
639 		const char *file, const char *func, int line,
640 		uint32_t stimulus,
641 		uint32_t stimulus_instance,
642 		uint32_t mode)
643 {
644 	skinny_message_t *message;
645 
646 	skinny_create_message(message, SET_LAMP_MESSAGE, lamp);
647 
648 	message->data.lamp.stimulus = stimulus;
649 	message->data.lamp.stimulus_instance = stimulus_instance;
650 	message->data.lamp.mode = mode;
651 
652 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
653 		"Sending Set Lamp with Stimulus (%s), Stimulus Instance (%d), Mode (%s)\n",
654 		skinny_button2str(stimulus), stimulus_instance, skinny_lamp_mode2str(mode));
655 
656 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
657 }
658 
perform_send_set_speaker_mode(listener_t * listener,const char * file,const char * func,int line,uint32_t mode)659 switch_status_t perform_send_set_speaker_mode(listener_t *listener,
660 		const char *file, const char *func, int line,
661 		uint32_t mode)
662 {
663 	skinny_message_t *message;
664 
665 	skinny_create_message(message, SET_SPEAKER_MODE_MESSAGE, speaker_mode);
666 
667 	message->data.speaker_mode.mode = mode;
668 
669 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
670 		"Sending Set Speaker Mode with Mode (%s)\n", skinny_speaker_mode2str(mode));
671 
672 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
673 }
674 
perform_send_srvreq_response(listener_t * listener,const char * file,const char * func,int line,char * ip,uint32_t port)675 switch_status_t perform_send_srvreq_response(listener_t *listener,
676 		const char *file, const char *func, int line,
677 		char *ip, uint32_t port)
678 {
679 	skinny_message_t *message;
680 
681 	skinny_create_message(message, SERVER_RESPONSE_MESSAGE, serv_res_mess);
682 
683 	message->data.serv_res_mess.serverListenPort[0] = port;
684 	switch_inet_pton(AF_INET,ip, &message->data.serv_res_mess.serverIpAddr[0]);
685 	switch_copy_string(message->data.serv_res_mess.server[0].serverName,ip,sizeof(message->data.serv_res_mess.server[0].serverName));
686 
687 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
688 		"Sending Server Request Response with IP (%s) and Port (%d)\n", ip, port);
689 
690 	return skinny_send_reply(listener, message, SWITCH_TRUE);
691 }
692 
perform_send_start_media_transmission(listener_t * listener,const char * file,const char * func,int line,uint32_t conference_id,uint32_t pass_thru_party_id,uint32_t remote_ip,uint32_t remote_port,uint32_t ms_per_packet,uint32_t payload_capacity,uint32_t precedence,uint32_t silence_suppression,uint16_t max_frames_per_packet,uint32_t g723_bitrate)693 switch_status_t perform_send_start_media_transmission(listener_t *listener,
694 		const char *file, const char *func, int line,
695 		uint32_t conference_id,
696 		uint32_t pass_thru_party_id,
697 		uint32_t remote_ip,
698 		uint32_t remote_port,
699 		uint32_t ms_per_packet,
700 		uint32_t payload_capacity,
701 		uint32_t precedence,
702 		uint32_t silence_suppression,
703 		uint16_t max_frames_per_packet,
704 		uint32_t g723_bitrate)
705 {
706 	skinny_message_t *message;
707 
708 	skinny_create_message(message, START_MEDIA_TRANSMISSION_MESSAGE, start_media);
709 
710 	message->data.start_media.conference_id = conference_id;
711 	message->data.start_media.pass_thru_party_id = pass_thru_party_id;
712 	message->data.start_media.remote_ip = remote_ip;
713 	message->data.start_media.remote_port = remote_port;
714 	message->data.start_media.ms_per_packet = ms_per_packet;
715 	message->data.start_media.payload_capacity = payload_capacity;
716 	message->data.start_media.precedence = precedence;
717 	message->data.start_media.silence_suppression = silence_suppression;
718 	message->data.start_media.max_frames_per_packet = max_frames_per_packet;
719 	message->data.start_media.g723_bitrate = g723_bitrate;
720 	/* ... */
721 
722 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
723 		"Send Start Media Transmission with Conf ID(%d), Passthrough Party ID (%d), ...\n",
724 		conference_id, pass_thru_party_id);
725 
726 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
727 }
728 
perform_send_stop_media_transmission(listener_t * listener,const char * file,const char * func,int line,uint32_t conference_id,uint32_t pass_thru_party_id,uint32_t conference_id2)729 switch_status_t perform_send_stop_media_transmission(listener_t *listener,
730 		const char *file, const char *func, int line,
731 		uint32_t conference_id,
732 		uint32_t pass_thru_party_id,
733 		uint32_t conference_id2)
734 {
735 	skinny_message_t *message;
736 
737 	skinny_create_message(message, STOP_MEDIA_TRANSMISSION_MESSAGE, stop_media);
738 
739 	message->data.stop_media.conference_id = conference_id;
740 	message->data.stop_media.pass_thru_party_id = pass_thru_party_id;
741 	message->data.stop_media.conference_id2 = conference_id2;
742 	/* ... */
743 
744 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
745 		"Send Stop Media Transmission with Conf ID (%d), Passthrough Party ID (%d), Conf ID2 (%d)\n",
746 		conference_id, pass_thru_party_id, conference_id2);
747 
748 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
749 }
750 
perform_send_call_info(listener_t * listener,const char * file,const char * func,int line,const char * calling_party_name,const char * calling_party,const char * called_party_name,const char * called_party,uint32_t line_instance,uint32_t call_id,uint32_t call_type,const char * original_called_party_name,const char * original_called_party,const char * last_redirecting_party_name,const char * last_redirecting_party,uint32_t original_called_party_redirect_reason,uint32_t last_redirecting_reason,const char * calling_party_voice_mailbox,const char * called_party_voice_mailbox,const char * original_called_party_voice_mailbox,const char * last_redirecting_voice_mailbox,uint32_t call_instance,uint32_t call_security_status,uint32_t party_pi_restriction_bits)751 switch_status_t perform_send_call_info(listener_t *listener,
752 		const char *file, const char *func, int line,
753 		const char *calling_party_name,
754 		const char *calling_party,
755 		const char *called_party_name,
756 		const char *called_party,
757 		uint32_t line_instance,
758 		uint32_t call_id,
759 		uint32_t call_type,
760 		const char *original_called_party_name,
761 		const char *original_called_party,
762 		const char *last_redirecting_party_name,
763 		const char *last_redirecting_party,
764 		uint32_t original_called_party_redirect_reason,
765 		uint32_t last_redirecting_reason,
766 		const char *calling_party_voice_mailbox,
767 		const char *called_party_voice_mailbox,
768 		const char *original_called_party_voice_mailbox,
769 		const char *last_redirecting_voice_mailbox,
770 		uint32_t call_instance,
771 		uint32_t call_security_status,
772 		uint32_t party_pi_restriction_bits)
773 {
774 	skinny_message_t *message;
775 
776 	skinny_create_message(message, CALL_INFO_MESSAGE, call_info);
777 
778 	switch_copy_string(message->data.call_info.calling_party_name, calling_party_name, 40);
779 	switch_copy_string(message->data.call_info.calling_party, calling_party, 24);
780 	switch_copy_string(message->data.call_info.called_party_name, called_party_name, 40);
781 	switch_copy_string(message->data.call_info.called_party, called_party, 24);
782 	message->data.call_info.line_instance = line_instance;
783 	message->data.call_info.call_id = call_id;
784 	message->data.call_info.call_type = call_type;
785 	switch_copy_string(message->data.call_info.original_called_party_name, original_called_party_name, 40);
786 	switch_copy_string(message->data.call_info.original_called_party, original_called_party, 24);
787 	switch_copy_string(message->data.call_info.last_redirecting_party_name, last_redirecting_party_name, 40);
788 	switch_copy_string(message->data.call_info.last_redirecting_party, last_redirecting_party, 24);
789 	message->data.call_info.original_called_party_redirect_reason = original_called_party_redirect_reason;
790 	message->data.call_info.last_redirecting_reason = last_redirecting_reason;
791 	switch_copy_string(message->data.call_info.calling_party_voice_mailbox, calling_party_voice_mailbox, 24);
792 	switch_copy_string(message->data.call_info.called_party_voice_mailbox, called_party_voice_mailbox, 24);
793 	switch_copy_string(message->data.call_info.original_called_party_voice_mailbox, original_called_party_voice_mailbox, 24);
794 	switch_copy_string(message->data.call_info.last_redirecting_voice_mailbox, last_redirecting_voice_mailbox, 24);
795 	message->data.call_info.call_instance = call_instance;
796 	message->data.call_info.call_security_status = call_security_status;
797 	message->data.call_info.party_pi_restriction_bits = party_pi_restriction_bits;
798 
799 	if ( listener->profile->debug >= 9 ) {
800 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
801 			"Send Call Info with Line Instance (%d)...\n", line_instance);
802 	}
803 
804 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
805 }
806 
perform_send_define_time_date(listener_t * listener,const char * file,const char * func,int line,uint32_t year,uint32_t month,uint32_t day_of_week,uint32_t day,uint32_t hour,uint32_t minute,uint32_t seconds,uint32_t milliseconds,uint32_t timestamp)807 switch_status_t perform_send_define_time_date(listener_t *listener,
808 		const char *file, const char *func, int line,
809 		uint32_t year,
810 		uint32_t month,
811 		uint32_t day_of_week, /* monday = 1 */
812 		uint32_t day,
813 		uint32_t hour,
814 		uint32_t minute,
815 		uint32_t seconds,
816 		uint32_t milliseconds,
817 		uint32_t timestamp)
818 {
819 	skinny_message_t *message;
820 
821 	skinny_create_message(message, DEFINE_TIME_DATE_MESSAGE, define_time_date);
822 
823 	message->data.define_time_date.year = year;
824 	message->data.define_time_date.month = month;
825 	message->data.define_time_date.day_of_week = day_of_week;
826 	message->data.define_time_date.day = day;
827 	message->data.define_time_date.hour = hour;
828 	message->data.define_time_date.minute = minute;
829 	message->data.define_time_date.seconds = seconds;
830 	message->data.define_time_date.milliseconds = milliseconds;
831 	message->data.define_time_date.timestamp = timestamp;
832 
833 	if ( listener->profile->debug >= 9 ) {
834 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
835 			"Send Define Time Date with %.4d-%.2d-%.2d %.2d:%.2d:%.2d.%d, Timestamp (%d), DOW (%d)\n",
836 			year, month, day, hour, minute, seconds, milliseconds, timestamp, day_of_week);
837 	}
838 
839 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
840 }
841 
perform_send_define_current_time_date(listener_t * listener,const char * file,const char * func,int line)842 switch_status_t perform_send_define_current_time_date(listener_t *listener,
843 		const char *file, const char *func, int line)
844 {
845 	switch_time_t ts;
846 	switch_time_exp_t tm;
847 	ts = switch_micro_time_now();
848 	switch_time_exp_lt(&tm, ts);
849 
850 	return send_define_time_date(listener,
851 			tm.tm_year + 1900,
852 			tm.tm_mon + 1,
853 			tm.tm_wday,
854 			tm.tm_mday,
855 			tm.tm_hour,
856 			tm.tm_min,
857 			tm.tm_sec,
858 			tm.tm_usec / 1000,
859 			ts / 1000000);
860 }
861 
perform_send_capabilities_req(listener_t * listener,const char * file,const char * func,int line)862 switch_status_t perform_send_capabilities_req(listener_t *listener,
863 		const char *file, const char *func, int line)
864 {
865 	skinny_message_t *message;
866 
867 	skinny_create_empty_message(message, CAPABILITIES_REQ_MESSAGE);
868 
869 	if ( listener->profile->debug >= 9 ) {
870 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
871 			"Send Capabilities Req%s\n", "");
872 	}
873 
874 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
875 }
876 
perform_send_version(listener_t * listener,const char * file,const char * func,int line,char * version)877 switch_status_t perform_send_version(listener_t *listener,
878 		const char *file, const char *func, int line,
879 		char *version)
880 {
881 	skinny_message_t *message;
882 
883 	skinny_create_message(message, VERSION_MESSAGE, version);
884 
885 	memcpy(message->data.version.version, version, 16);
886 
887 	if ( listener->profile->debug >= 9 ) {
888 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
889 			"Send Version with Version(%s)\n", version);
890 	}
891 
892 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
893 }
894 
perform_send_register_reject(listener_t * listener,const char * file,const char * func,int line,char * error)895 switch_status_t perform_send_register_reject(listener_t *listener,
896 		const char *file, const char *func, int line,
897 		char *error)
898 {
899 	skinny_message_t *message;
900 
901 	skinny_create_message(message, REGISTER_REJECT_MESSAGE, reg_rej);
902 
903 	switch_copy_string(message->data.reg_rej.error, error, 33);
904 
905 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
906 		"Send Register Reject with Error (%s)\n", error);
907 
908 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
909 }
910 
perform_send_open_receive_channel(listener_t * listener,const char * file,const char * func,int line,uint32_t conference_id,uint32_t pass_thru_party_id,uint32_t ms_per_packet,uint32_t payload_capacity,uint32_t echo_cancel_type,uint32_t g723_bitrate,uint32_t conference_id2,uint32_t reserved[10])911 switch_status_t perform_send_open_receive_channel(listener_t *listener,
912 		const char *file, const char *func, int line,
913 		uint32_t conference_id,
914 		uint32_t pass_thru_party_id,
915 		uint32_t ms_per_packet,
916 		uint32_t payload_capacity,
917 		uint32_t echo_cancel_type,
918 		uint32_t g723_bitrate,
919 		uint32_t conference_id2,
920 		uint32_t reserved[10])
921 {
922 	skinny_message_t *message;
923 
924 	skinny_create_message(message, OPEN_RECEIVE_CHANNEL_MESSAGE, open_receive_channel);
925 
926 	message->data.open_receive_channel.conference_id = conference_id;
927 	message->data.open_receive_channel.pass_thru_party_id = pass_thru_party_id;
928 	message->data.open_receive_channel.ms_per_packet = ms_per_packet;
929 	message->data.open_receive_channel.payload_capacity = payload_capacity;
930 	message->data.open_receive_channel.echo_cancel_type = echo_cancel_type;
931 	message->data.open_receive_channel.g723_bitrate = g723_bitrate;
932 	message->data.open_receive_channel.conference_id2 = conference_id2;
933 	message->data.open_receive_channel.rtptimeout = htonl(0x0a);
934 	/*
935 	   message->data.open_receive_channel.reserved[0] = reserved[0];
936 	   message->data.open_receive_channel.reserved[1] = reserved[1];
937 	   message->data.open_receive_channel.reserved[2] = reserved[2];
938 	   message->data.open_receive_channel.reserved[3] = reserved[3];
939 	   message->data.open_receive_channel.reserved[4] = reserved[4];
940 	   message->data.open_receive_channel.reserved[5] = reserved[5];
941 	   message->data.open_receive_channel.reserved[6] = reserved[6];
942 	   message->data.open_receive_channel.reserved[7] = reserved[7];
943 	   message->data.open_receive_channel.reserved[8] = reserved[8];
944 	   message->data.open_receive_channel.reserved[9] = reserved[9];
945 	 */
946 
947 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
948 		"Send Open Receive Channel with Conf ID (%d), ...\n", conference_id);
949 
950 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
951 }
952 
perform_send_close_receive_channel(listener_t * listener,const char * file,const char * func,int line,uint32_t conference_id,uint32_t pass_thru_party_id,uint32_t conference_id2)953 switch_status_t perform_send_close_receive_channel(listener_t *listener,
954 		const char *file, const char *func, int line,
955 		uint32_t conference_id,
956 		uint32_t pass_thru_party_id,
957 		uint32_t conference_id2)
958 {
959 	skinny_message_t *message;
960 
961 	skinny_create_message(message, CLOSE_RECEIVE_CHANNEL_MESSAGE, close_receive_channel);
962 
963 	message->data.close_receive_channel.conference_id = conference_id;
964 	message->data.close_receive_channel.pass_thru_party_id = pass_thru_party_id;
965 	message->data.close_receive_channel.conference_id2 = conference_id2;
966 
967 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
968 		"Send Close Receive Channel with Conf ID (%d), ...\n", conference_id);
969 
970 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
971 }
972 
perform_send_select_soft_keys(listener_t * listener,const char * file,const char * func,int line,uint32_t line_instance,uint32_t call_id,uint32_t soft_key_set,uint32_t valid_key_mask)973 switch_status_t perform_send_select_soft_keys(listener_t *listener,
974 		const char *file, const char *func, int line,
975 		uint32_t line_instance,
976 		uint32_t call_id,
977 		uint32_t soft_key_set,
978 		uint32_t valid_key_mask)
979 {
980 	skinny_message_t *message;
981 
982 	skinny_create_message(message, SELECT_SOFT_KEYS_MESSAGE, select_soft_keys);
983 
984 	message->data.select_soft_keys.line_instance = line_instance;
985 	message->data.select_soft_keys.call_id = call_id;
986 	message->data.select_soft_keys.soft_key_set = soft_key_set;
987 	message->data.select_soft_keys.valid_key_mask = valid_key_mask;
988 
989 	if ( listener->profile->debug >= 9 ) {
990 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
991 			"Send Select Soft Keys with Line Instance (%d), Call ID (%d), Soft Key Set (%d), Valid Key Mask (%x)\n",
992 			line_instance, call_id, soft_key_set, valid_key_mask);
993 	}
994 
995 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
996 }
997 
perform_send_call_state(listener_t * listener,const char * file,const char * func,int line,uint32_t call_state,uint32_t line_instance,uint32_t call_id)998 switch_status_t perform_send_call_state(listener_t *listener,
999 		const char *file, const char *func, int line,
1000 		uint32_t call_state,
1001 		uint32_t line_instance,
1002 		uint32_t call_id)
1003 {
1004 	skinny_message_t *message;
1005 
1006 	skinny_create_message(message, CALL_STATE_MESSAGE, call_state);
1007 
1008 	message->data.call_state.call_state = call_state;
1009 	message->data.call_state.line_instance = line_instance;
1010 	message->data.call_state.call_id = call_id;
1011 
1012 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1013 		"Send Call State with State (%s), Line Instance (%d), Call ID (%d)\n",
1014 		skinny_call_state2str(call_state), line_instance, call_id);
1015 
1016 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1017 }
1018 
perform_send_display_prompt_status(listener_t * listener,const char * file,const char * func,int line,uint32_t timeout,const char * display,uint32_t line_instance,uint32_t call_id)1019 switch_status_t perform_send_display_prompt_status(listener_t *listener,
1020 		const char *file, const char *func, int line,
1021 		uint32_t timeout,
1022 		const char *display,
1023 		uint32_t line_instance,
1024 		uint32_t call_id)
1025 {
1026 	skinny_message_t *message;
1027 	char *tmp;
1028 
1029 	skinny_create_message(message, DISPLAY_PROMPT_STATUS_MESSAGE, display_prompt_status);
1030 
1031 	message->data.display_prompt_status.timeout = timeout;
1032 	switch_copy_string(message->data.display_prompt_status.display, display, 32);
1033 	message->data.display_prompt_status.line_instance = line_instance;
1034 	message->data.display_prompt_status.call_id = call_id;
1035 
1036 	tmp = skinny_format_message(display);
1037 
1038 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1039 		"Send Display Prompt Status with Timeout (%d), Display (%s), Line Instance (%d), Call ID (%d)\n",
1040 		timeout, tmp, line_instance, call_id);
1041 
1042 	switch_safe_free(tmp);
1043 
1044 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1045 }
1046 
perform_send_display_prompt_status_textid(listener_t * listener,const char * file,const char * func,int line,uint32_t timeout,uint32_t display_textid,uint32_t line_instance,uint32_t call_id)1047 switch_status_t perform_send_display_prompt_status_textid(listener_t *listener,
1048 		const char *file, const char *func, int line,
1049 		uint32_t timeout,
1050 		uint32_t display_textid,
1051 		uint32_t line_instance,
1052 		uint32_t call_id)
1053 {
1054 	skinny_message_t *message;
1055 	char *label;
1056 
1057 	skinny_create_message(message, DISPLAY_PROMPT_STATUS_MESSAGE, display_prompt_status);
1058 
1059 	message->data.display_prompt_status.timeout = timeout;
1060 
1061 	label = skinny_textid2raw(display_textid);
1062 	switch_copy_string(message->data.display_prompt_status.display, label, 32);
1063 	switch_safe_free(label);
1064 
1065 	message->data.display_prompt_status.line_instance = line_instance;
1066 	message->data.display_prompt_status.call_id = call_id;
1067 
1068 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1069 		"Send Display Prompt Status with Timeout (%d), Display (%s), Line Instance (%d), Call ID (%d)\n",
1070 		timeout, skinny_textid2str(display_textid), line_instance, call_id);
1071 
1072 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1073 }
1074 
perform_send_clear_prompt_status(listener_t * listener,const char * file,const char * func,int line,uint32_t line_instance,uint32_t call_id)1075 switch_status_t perform_send_clear_prompt_status(listener_t *listener,
1076 		const char *file, const char *func, int line,
1077 		uint32_t line_instance,
1078 		uint32_t call_id)
1079 {
1080 	skinny_message_t *message;
1081 
1082 	skinny_create_message(message, CLEAR_PROMPT_STATUS_MESSAGE, clear_prompt_status);
1083 
1084 	message->data.clear_prompt_status.line_instance = line_instance;
1085 	message->data.clear_prompt_status.call_id = call_id;
1086 
1087 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1088 		"Send Clear Prompt Status with Line Instance (%d), Call ID (%d)\n",
1089 		line_instance, call_id);
1090 
1091 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1092 }
1093 
perform_send_activate_call_plane(listener_t * listener,const char * file,const char * func,int line,uint32_t line_instance)1094 switch_status_t perform_send_activate_call_plane(listener_t *listener,
1095 		const char *file, const char *func, int line,
1096 		uint32_t line_instance)
1097 {
1098 	skinny_message_t *message;
1099 
1100 	skinny_create_message(message, ACTIVATE_CALL_PLANE_MESSAGE, activate_call_plane);
1101 
1102 	message->data.activate_call_plane.line_instance = line_instance;
1103 
1104 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1105 		"Send Activate Call Plane with Line Instance (%d)\n", line_instance);
1106 
1107 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1108 }
1109 
perform_send_back_space_request(listener_t * listener,const char * file,const char * func,int line,uint32_t line_instance,uint32_t call_id)1110 switch_status_t perform_send_back_space_request(listener_t *listener,
1111 		const char *file, const char *func, int line,
1112 		uint32_t line_instance,
1113 		uint32_t call_id)
1114 {
1115 	skinny_message_t *message;
1116 
1117 	skinny_create_message(message, BACK_SPACE_REQ_MESSAGE, back_space_req);
1118 
1119 	message->data.back_space_req.line_instance = line_instance;
1120 	message->data.back_space_req.call_id = call_id;
1121 
1122 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1123 		"Send Back Space Request with Line Instance (%d), Call ID (%d)\n",
1124 		line_instance, call_id);
1125 
1126 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1127 
1128 }
1129 
perform_send_dialed_number(listener_t * listener,const char * file,const char * func,int line,char called_party[24],uint32_t line_instance,uint32_t call_id)1130 switch_status_t perform_send_dialed_number(listener_t *listener,
1131 		const char *file, const char *func, int line,
1132 		char called_party[24],
1133 		uint32_t line_instance,
1134 		uint32_t call_id)
1135 {
1136 	skinny_message_t *message;
1137 
1138 	skinny_create_message(message, DIALED_NUMBER_MESSAGE, dialed_number);
1139 
1140 	switch_copy_string(message->data.dialed_number.called_party, called_party, 24);
1141 	message->data.dialed_number.line_instance = line_instance;
1142 	message->data.dialed_number.call_id = call_id;
1143 
1144 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1145 		"Send Dialed Number with Number (%s), Line Instance (%d), Call ID (%d)\n",
1146 		called_party, line_instance, call_id);
1147 
1148 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1149 }
1150 
perform_send_display_pri_notify(listener_t * listener,const char * file,const char * func,int line,uint32_t message_timeout,uint32_t priority,char * notify)1151 switch_status_t perform_send_display_pri_notify(listener_t *listener,
1152 		const char *file, const char *func, int line,
1153 		uint32_t message_timeout,
1154 		uint32_t priority,
1155 		char *notify)
1156 {
1157 	skinny_message_t *message;
1158 	char *tmp;
1159 
1160 	skinny_create_message(message, DISPLAY_PRI_NOTIFY_MESSAGE, display_pri_notify);
1161 
1162 	message->data.display_pri_notify.message_timeout = message_timeout;
1163 	message->data.display_pri_notify.priority = priority;
1164 	switch_copy_string(message->data.display_pri_notify.notify, notify, 32);
1165 
1166 	tmp = skinny_format_message(notify);
1167 
1168 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1169 		"Send Display Pri Notify with Timeout (%d), Priority (%d), Message (%s)\n",
1170 		message_timeout, priority, tmp);
1171 
1172 	switch_safe_free(tmp);
1173 
1174 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1175 }
1176 
1177 
perform_send_reset(listener_t * listener,const char * file,const char * func,int line,uint32_t reset_type)1178 switch_status_t perform_send_reset(listener_t *listener,
1179 		const char *file, const char *func, int line,
1180 		uint32_t reset_type)
1181 {
1182 	skinny_message_t *message;
1183 
1184 	skinny_create_message(message, RESET_MESSAGE, reset);
1185 
1186 	message->data.reset.reset_type = reset_type;
1187 
1188 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1189 		"Send Reset with Type (%s)\n", skinny_device_reset_type2str(reset_type));
1190 
1191 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1192 }
1193 
perform_send_data(listener_t * listener,const char * file,const char * func,int line,uint32_t message_type,uint32_t application_id,uint32_t line_instance,uint32_t call_id,uint32_t transaction_id,uint32_t data_length,const char * data)1194 switch_status_t perform_send_data(listener_t *listener,
1195 		const char *file, const char *func, int line,
1196 		uint32_t message_type,
1197 		uint32_t application_id,
1198 		uint32_t line_instance,
1199 		uint32_t call_id,
1200 		uint32_t transaction_id,
1201 		uint32_t data_length,
1202 		const char *data)
1203 {
1204 	skinny_message_t *message;
1205 	switch_assert(data_length == strlen(data));
1206 
1207 	/* data_length should be a multiple of 4 */
1208 	if ((data_length % 4) != 0) {
1209 		data_length = (data_length / 4 + 1) * 4;
1210 	}
1211 
1212 	/* This one needs explicit allocation */
1213 	message = calloc(12+sizeof(message->data.data)+data_length-1, 1);
1214 	message->type = message_type;
1215 	message->length = 4 + sizeof(message->data.data)+data_length-1;
1216 
1217 	message->data.data.application_id = application_id;
1218 	message->data.data.line_instance = line_instance;
1219 	message->data.data.call_id = call_id;
1220 	message->data.data.transaction_id = transaction_id;
1221 	message->data.data.data_length = data_length;
1222 	switch_copy_string(message->data.data.data, data, data_length);
1223 
1224 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1225 		"Send Data with Data Length (%d)\n", data_length);
1226 
1227 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1228 }
1229 
perform_send_extended_data(listener_t * listener,const char * file,const char * func,int line,uint32_t message_type,uint32_t application_id,uint32_t line_instance,uint32_t call_id,uint32_t transaction_id,uint32_t data_length,uint32_t sequence_flag,uint32_t display_priority,uint32_t conference_id,uint32_t app_instance_id,uint32_t routing_id,const char * data)1230 switch_status_t perform_send_extended_data(listener_t *listener,
1231 		const char *file, const char *func, int line,
1232 		uint32_t message_type,
1233 		uint32_t application_id,
1234 		uint32_t line_instance,
1235 		uint32_t call_id,
1236 		uint32_t transaction_id,
1237 		uint32_t data_length,
1238 		uint32_t sequence_flag,
1239 		uint32_t display_priority,
1240 		uint32_t conference_id,
1241 		uint32_t app_instance_id,
1242 		uint32_t routing_id,
1243 		const char *data)
1244 {
1245 	skinny_message_t *message;
1246 	switch_assert(data_length == strlen(data));
1247 
1248 	/* data_length should be a multiple of 4 */
1249 	if ((data_length % 4) != 0) {
1250 		data_length = (data_length / 4 + 1) * 4;
1251 	}
1252 
1253 	/* This one needs explicit allocation */
1254 	message = calloc(12+sizeof(message->data.extended_data)+data_length-1, 1);
1255 	message->type = message_type;
1256 	message->length = 4 + sizeof(message->data.extended_data)+data_length-1;
1257 
1258 	message->data.extended_data.application_id = application_id;
1259 	message->data.extended_data.line_instance = line_instance;
1260 	message->data.extended_data.call_id = call_id;
1261 	message->data.extended_data.transaction_id = transaction_id;
1262 	message->data.extended_data.data_length = data_length;
1263 	message->data.extended_data.sequence_flag = sequence_flag;
1264 	message->data.extended_data.display_priority = display_priority;
1265 	message->data.extended_data.conference_id = conference_id;
1266 	message->data.extended_data.app_instance_id = app_instance_id;
1267 	message->data.extended_data.routing_id = routing_id;
1268 	switch_copy_string(message->data.extended_data.data, data, data_length);
1269 
1270 	skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1271 		"Send Extended Data with Application ID (%d), Line Instance (%d), Call ID (%d), ...\n",
1272 		application_id, line_instance, call_id );
1273 
1274 	return skinny_send_reply_quiet(listener, message, SWITCH_TRUE);
1275 }
1276 
skinny_perform_send_reply_quiet(listener_t * listener,const char * file,const char * func,int line,skinny_message_t * reply,switch_bool_t discard)1277 switch_status_t skinny_perform_send_reply_quiet(listener_t *listener, const char *file, const char *func, int line, skinny_message_t *reply,
1278 	switch_bool_t discard)
1279 {
1280 	char *ptr;
1281 	switch_size_t len;
1282 	switch_status_t res;
1283 
1284 	switch_assert(reply != NULL);
1285 
1286 	len = reply->length+8;
1287 	ptr = (char *) reply;
1288 
1289 	if (listener_is_ready(listener)) {
1290 		res = switch_socket_send(listener->sock, ptr, &len);
1291 
1292 		if ( discard ) { switch_safe_free(reply); }
1293 		return res;
1294 	} else {
1295 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_WARNING,
1296 				"Not sending %s (type=%x,length=%d) while not ready.\n",
1297 				skinny_message_type2str(reply->type), reply->type, reply->length);
1298 
1299 		if ( discard ) { switch_safe_free(reply); }
1300 		return SWITCH_STATUS_FALSE;
1301 	}
1302 }
1303 
skinny_perform_send_reply(listener_t * listener,const char * file,const char * func,int line,skinny_message_t * reply,switch_bool_t discard)1304 switch_status_t skinny_perform_send_reply(listener_t *listener, const char *file, const char *func, int line, skinny_message_t *reply,
1305 	switch_bool_t discard)
1306 {
1307 	char *ptr;
1308 	switch_size_t len;
1309 	switch_status_t res;
1310 
1311 	switch_assert(reply != NULL);
1312 
1313 	len = reply->length+8;
1314 	ptr = (char *) reply;
1315 
1316 	if (listener_is_ready(listener)) {
1317 		if (listener->profile->debug >= 10 ||
1318 			(listener->profile->debug >= 9 && reply->type != KEEP_ALIVE_ACK_MESSAGE)) {
1319 			skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_DEBUG,
1320 					"Sending %s (type=%x,length=%d).\n",
1321 					skinny_message_type2str(reply->type), reply->type, reply->length);
1322 		}
1323 		res = switch_socket_send(listener->sock, ptr, &len);
1324 
1325 		if ( discard ) { switch_safe_free(reply); }
1326 		return res;
1327 	} else {
1328 		skinny_log_l_ffl(listener, file, func, line, SWITCH_LOG_WARNING,
1329 				"Not sending %s (type=%x,length=%d) while not ready.\n",
1330 				skinny_message_type2str(reply->type), reply->type, reply->length);
1331 
1332 		if ( discard ) { switch_safe_free(reply); }
1333 		return SWITCH_STATUS_FALSE;
1334 	}
1335 }
1336 
1337 /* For Emacs:
1338  * Local Variables:
1339  * mode:c
1340  * indent-tabs-mode:t
1341  * tab-width:4
1342  * c-basic-offset:4
1343  * End:
1344  * For VIM:
1345  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
1346  */
1347 
1348