Asterisk - The Open Source Telephony Project  21.4.1
max_forwards.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
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 
36 static struct max_forwards *max_forwards_alloc(int starting_count, int current_count)
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 
55  return max_forwards_alloc(mf->starting_count, mf->current_count);
56 }
57 
58 static void max_forwards_destroy(void *data)
59 {
60  ast_free(data);
61 }
62 
63 const struct ast_datastore_info max_forwards_info = {
64  .type = "mfaled-interface",
65  .duplicate = max_forwards_duplicate,
66  .destroy = max_forwards_destroy,
67 };
68 
69 static struct ast_datastore *max_forwards_datastore_alloc(struct ast_channel *chan,
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 
93 static struct ast_datastore *max_forwards_datastore_find_or_alloc(struct ast_channel *chan)
94 {
95  struct ast_datastore *mf_datastore;
96 
97  mf_datastore = ast_channel_datastore_find(chan, &max_forwards_info, NULL);
98  if (!mf_datastore) {
99  mf_datastore = max_forwards_datastore_alloc(chan, DEFAULT_MAX_FORWARDS);
100  }
101 
102  return mf_datastore;
103 }
104 
105 int ast_max_forwards_set(struct ast_channel *chan, int starting_count)
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 
121 int ast_max_forwards_get(struct ast_channel *chan)
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 
135 int ast_max_forwards_decrement(struct ast_channel *chan)
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 
151 int ast_max_forwards_reset(struct ast_channel *chan)
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
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
int starting_count
Definition: max_forwards.c:31
Structure for a data store type.
Definition: datastore.h:31
Structure for a data store object.
Definition: datastore.h:64
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:2399
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
General Asterisk PBX channel definitions.
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
Channel datastore data for max forwards.
Definition: max_forwards.c:29
unsigned int inheritance
Definition: datastore.h:69
void * data
Definition: datastore.h:66
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2385