Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_scramble.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2021, Naveen Albert
5 *
6 * Naveen Albert <asterisk@phreaknet.org>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Frequency inverter
22 *
23 * \author Naveen Albert <asterisk@phreaknet.org>
24 *
25 * \ingroup functions
26 *
27 */
28
29/*** MODULEINFO
30 <support_level>extended</support_level>
31 ***/
32
33/*** DOCUMENTATION
34 <function name="SCRAMBLE" language="en_US">
35 <since>
36 <version>16.21.0</version>
37 <version>18.7.0</version>
38 </since>
39 <synopsis>
40 Scrambles audio on a channel.
41 </synopsis>
42 <syntax>
43 <parameter name="direction" required="false">
44 <para>Must be <literal>TX</literal> or <literal>RX</literal>
45 to limit to a specific direction, or <literal>both</literal>
46 for both directions. <literal>remove</literal>
47 will remove an existing scrambler.</para>
48 </parameter>
49 </syntax>
50 <description>
51 <para>Scrambles audio on a channel using whole spectrum inversion.
52 This is not intended to be used for securely scrambling
53 audio. It merely renders obfuscates audio on a channel
54 to render it unintelligible, as a privacy enhancement.</para>
55 </description>
56 <see-also>
57 <ref type="application">ChanSpy</ref>
58 </see-also>
59 </function>
60 ***/
61
62#include "asterisk.h"
63
64#include "asterisk/module.h"
65#include "asterisk/channel.h"
66#include "asterisk/pbx.h"
67#include "asterisk/utils.h"
68#include "asterisk/audiohook.h"
69#include "asterisk/app.h"
70
71#include <stdio.h>
72#include <string.h>
73
76 unsigned short int tx;
77 unsigned short int rx;
78 unsigned short int state;
79};
80
81static void destroy_callback(void *data)
82{
83 struct scramble_information *ni = data;
84
85 /* Destroy the audiohook, and destroy ourselves */
90 ast_free(ni);
91
92 return;
93}
94
95/*! \brief Static structure for datastore information */
97 .type = "scramble",
98 .destroy = destroy_callback
99};
100
101/* modifies buffer pointed to by 'amp' with inverted values */
102static inline void freq_invert(short *amp, int samples)
103{
104 int i;
105 /* invert every other sample by 1 */
106 for (i = 0; i < samples; i += 2) {
107 amp[i] = -amp[i];
108 }
109}
110
111static int scramble_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
112{
113 struct ast_datastore *datastore = NULL;
114 struct scramble_information *ni = NULL;
115
116 /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
118 return 0;
119 }
120
121 /* Grab datastore which contains our gain information */
122 if (!(datastore = ast_channel_datastore_find(chan, &scramble_datastore, NULL))) {
123 return 0;
124 }
125
126 if (frame->frametype == AST_FRAME_VOICE) { /* only invert voice frequencies */
127 ni = datastore->data;
128 /* Based on direction of frame, and confirm it is applicable */
129 if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? ni->rx : ni->tx)) {
130 return 0;
131 }
132 /* Scramble the sample now */
133 freq_invert(frame->data.ptr, frame->samples);
134 }
135 return 0;
136}
137
138/*! \internal \brief Disable scrambling on the channel */
139static int remove_scrambler(struct ast_channel *chan)
140{
141 struct ast_datastore *datastore = NULL;
142 struct scramble_information *data;
143 SCOPED_CHANNELLOCK(chan_lock, chan);
144
146 if (!datastore) {
147 ast_log(AST_LOG_WARNING, "Cannot remove SCRAMBLE from %s: SCRAMBLE not currently enabled\n",
148 ast_channel_name(chan));
149 return -1;
150 }
151 data = datastore->data;
152
153 if (ast_audiohook_remove(chan, &data->audiohook)) {
154 ast_log(AST_LOG_WARNING, "Failed to remove SCRAMBLE audiohook from channel %s\n", ast_channel_name(chan));
155 return -1;
156 }
157
158 if (ast_channel_datastore_remove(chan, datastore)) {
159 ast_log(AST_LOG_WARNING, "Failed to remove SCRAMBLE datastore from channel %s\n",
160 ast_channel_name(chan));
161 return -1;
162 }
163 ast_datastore_free(datastore);
164
165 return 0;
166}
167
168static int scramble_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
169{
170 char *parse;
171 struct ast_datastore *datastore = NULL;
172 struct scramble_information *ni = NULL;
173 int tx = 1, rx = 1;
174
177 );
178
179 if (!chan) {
180 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
181 return -1;
182 }
183
184 parse = ast_strdupa(value);
186
187 if (!strcasecmp(args.direction, "remove")) {
188 return remove_scrambler(chan);
189 }
190 if (!strcasecmp(args.direction, "tx")) {
191 tx = 1;
192 rx = 0;
193 } else if (!strcasecmp(args.direction, "rx")) {
194 rx = 0;
195 tx = 1;
196 } else if (strcasecmp(args.direction, "both")) {
197 ast_log(LOG_ERROR, "Direction must be either RX, TX, both, or remove\n");
198 return -1;
199 }
200 ast_channel_lock(chan);
201 if (!(datastore = ast_channel_datastore_find(chan, &scramble_datastore, NULL))) {
202 /* Allocate a new datastore to hold the reference to this audiohook information */
203 if (!(datastore = ast_datastore_alloc(&scramble_datastore, NULL))) {
204 return 0;
205 }
206 if (!(ni = ast_calloc(1, sizeof(*ni)))) {
207 ast_datastore_free(datastore);
208 return 0;
209 }
212 datastore->data = ni;
213 ast_channel_datastore_add(chan, datastore);
215 } else {
216 ni = datastore->data;
217 }
218 ni->tx = tx;
219 ni->rx = rx;
220 ast_channel_unlock(chan);
221
222 return 0;
223}
224
226 .name = "SCRAMBLE",
227 .write = scramble_write,
228};
229
230static int unload_module(void)
231{
233}
234
235static int load_module(void)
236{
238}
239
240AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Frequency inverting voice scrambler");
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
Audiohooks Architecture.
@ AST_AUDIOHOOK_MANIPULATE_ALL_RATES
Definition: audiohook.h:75
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
Definition: audiohook.c:100
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Definition: audiohook.c:749
ast_audiohook_direction
Definition: audiohook.h:48
@ AST_AUDIOHOOK_DIRECTION_READ
Definition: audiohook.h:49
#define ast_audiohook_lock(ah)
Lock an audiohook.
Definition: audiohook.h:313
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition: audiohook.c:578
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:512
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:124
#define ast_audiohook_unlock(ah)
Unlock an audiohook.
Definition: audiohook.h:318
@ AST_AUDIOHOOK_TYPE_MANIPULATE
Definition: audiohook.h:38
@ AST_AUDIOHOOK_STATUS_DONE
Definition: audiohook.h:45
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2414
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2423
#define ast_channel_lock(chan)
Definition: channel.h:2970
#define ast_channel_unlock(chan)
Definition: channel.h:2971
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2428
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
direction
static int scramble_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
static void freq_invert(short *amp, int samples)
static int remove_scrambler(struct ast_channel *chan)
static const struct ast_datastore_info scramble_datastore
Static structure for datastore information.
Definition: func_scramble.c:96
static void destroy_callback(void *data)
Definition: func_scramble.c:81
static int load_module(void)
static struct ast_custom_function scramble_function
static int scramble_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int unload_module(void)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
@ AST_FRAME_VOICE
#define AST_LOG_WARNING
#define LOG_ERROR
#define LOG_WARNING
#define SCOPED_CHANNELLOCK(varname, chan)
scoped lock specialization for channels.
Definition: lock.h:623
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define NULL
Definition: resample.c:96
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:118
enum ast_audiohook_status status
Definition: audiohook.h:108
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Structure for a data store type.
Definition: datastore.h:31
const char * type
Definition: datastore.h:32
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
Data structure associated with a single frame of data.
union ast_frame::@228 data
enum ast_frame_type frametype
unsigned short int tx
Definition: func_scramble.c:76
unsigned short int state
Definition: func_scramble.c:78
struct ast_audiohook audiohook
Definition: func_scramble.c:75
unsigned short int rx
Definition: func_scramble.c:77
int value
Definition: syslog.c:37
const char * args
Utility functions.