Asterisk - The Open Source Telephony Project  18.5.0
max_forwards.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Digium, Inc.
5  *
6  * Mark Michelson <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not mfrectly 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, mfstributed 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 #include "asterisk.h"
20 
21 #include "asterisk/max_forwards.h"
22 #include "asterisk/channel.h"
23 
24 #define DEFAULT_MAX_FORWARDS 20
25 
26 /*!
27  * \brief Channel datastore data for max forwards
28  */
29 struct max_forwards {
30  /*! The starting count. Used to allow resetting to the original value */
32  /*! The current count. When this reaches 0, you're outta luck */
34 };
35 
37 {
38  struct max_forwards *mf;
39 
40  mf = ast_malloc(sizeof(*mf));
41  if (!mf) {
42  return NULL;
43  }
44 
47 
48  return mf;
49 }
50 
51 static void *max_forwards_duplicate(void *data)
52 {
53  struct max_forwards *mf = data;
54 
56 }
57 
58 static void max_forwards_destroy(void *data)
59 {
60  ast_free(data);
61 }
62 
64  .type = "mfaled-interface",
65  .duplicate = max_forwards_duplicate,
66  .destroy = max_forwards_destroy,
67 };
68 
70  int starting_count)
71 {
72  struct ast_datastore *mf_datastore;
73  struct max_forwards *mf;
74 
75  mf_datastore = ast_datastore_alloc(&max_forwards_info, NULL);
76  if (!mf_datastore) {
77  return NULL;
78  }
79  mf_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
80 
81  mf = max_forwards_alloc(starting_count, starting_count);
82  if (!mf) {
83  ast_datastore_free(mf_datastore);
84  return NULL;
85  }
86  mf_datastore->data = mf;
87 
88  ast_channel_datastore_add(chan, mf_datastore);
89 
90  return mf_datastore;
91 }
92 
94 {
95  struct ast_datastore *mf_datastore;
96 
97  mf_datastore = ast_channel_datastore_find(chan, &max_forwards_info, NULL);
98  if (!mf_datastore) {
100  }
101 
102  return mf_datastore;
103 }
104 
106 {
107  struct ast_datastore *mf_datastore;
108  struct max_forwards *mf;
109 
110  mf_datastore = max_forwards_datastore_find_or_alloc(chan);
111  if (!mf_datastore) {
112  return -1;
113  }
114 
115  mf = mf_datastore->data;
117 
118  return 0;
119 }
120 
122 {
123  struct ast_datastore *mf_datastore;
124  struct max_forwards *mf;
125 
126  mf_datastore = max_forwards_datastore_find_or_alloc(chan);
127  if (!mf_datastore) {
128  return -1;
129  }
130 
131  mf = mf_datastore->data;
132  return mf->current_count;
133 }
134 
136 {
137  struct ast_datastore *mf_datastore;
138  struct max_forwards *mf;
139 
140  mf_datastore = max_forwards_datastore_find_or_alloc(chan);
141  if (!mf_datastore) {
142  return -1;
143  }
144 
145  mf = mf_datastore->data;
146  --mf->current_count;
147 
148  return 0;
149 }
150 
152 {
153  struct ast_datastore *mf_datastore;
154  struct max_forwards *mf;
155 
156  mf_datastore = max_forwards_datastore_find_or_alloc(chan);
157  if (!mf_datastore) {
158  return -1;
159  }
160 
161  mf = mf_datastore->data;
162  mf->current_count = mf->starting_count;
163 
164  return 0;
165 }
const char * type
Definition: datastore.h:32
int ast_max_forwards_decrement(struct ast_channel *chan)
Decrement the max forwards count for a particular channel.
Definition: max_forwards.c:135
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
#define DEFAULT_MAX_FORWARDS
Definition: max_forwards.c:24
int starting_count
Definition: max_forwards.c:31
int ast_max_forwards_set(struct ast_channel *chan, int starting_count)
Set the starting max forwards for a particular channel.
Definition: max_forwards.c:105
int ast_max_forwards_get(struct ast_channel *chan)
Get the current max forwards for a particular channel.
Definition: max_forwards.c:121
Structure for a data store type.
Definition: datastore.h:31
static void max_forwards_destroy(void *data)
Definition: max_forwards.c:58
Structure for a data store object.
Definition: datastore.h:68
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:2404
#define NULL
Definition: resample.c:96
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
static struct max_forwards * max_forwards_alloc(int starting_count, int current_count)
Definition: max_forwards.c:36
General Asterisk PBX channel definitions.
static void * max_forwards_duplicate(void *data)
Definition: max_forwards.c:51
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
int ast_max_forwards_reset(struct ast_channel *chan)
Reset the max forwards on a channel to its starting value.
Definition: max_forwards.c:151
static struct ast_datastore * max_forwards_datastore_alloc(struct ast_channel *chan, int starting_count)
Definition: max_forwards.c:69
Channel datastore data for max forwards.
Definition: max_forwards.c:29
#define ast_free(a)
Definition: astmm.h:182
#define DATASTORE_INHERIT_FOREVER
Definition: channel.h:193
unsigned int inheritance
Definition: datastore.h:73
void * data
Definition: datastore.h:70
const struct ast_datastore_info max_forwards_info
Definition: max_forwards.c:63
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
static struct ast_datastore * max_forwards_datastore_find_or_alloc(struct ast_channel *chan)
Definition: max_forwards.c:93
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390