~ubuntu-branches/debian/sid/kamailio/sid

« back to all changes in this revision

Viewing changes to modules/cnxcc/cnxcc_rpc.c

  • Committer: Package Import Robot
  • Author(s): Victor Seva
  • Date: 2014-01-06 11:47:13 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140106114713-t8xidp4arzrnyeya
Tags: 4.1.1-1
* New upstream release
* debian/patches:
  - add upstream fixes
* Added tls outbound websocket autheph dnssec modules
  - openssl exception added to their license
* removing sparc and ia64 from supported archs
  for mono module (Closes: #728915)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id$
 
3
 *
 
4
 * Copyright (C) 2012 Carlos Ruiz Díaz (caruizdiaz.com),
 
5
 *                    ConexionGroup (www.conexiongroup.com)
 
6
 *
 
7
 * This file is part of Kamailio, a free SIP server.
 
8
 *
 
9
 * Kamailio is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version
 
13
 *
 
14
 * Kamailio is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
 *
 
23
 */
 
24
 
 
25
#include <stdio.h>
 
26
 
 
27
#include "../../locking.h"
 
28
#include "../../lock_ops.h"
 
29
#include "../../rpc.h"
 
30
#include "../../rpc_lookup.h"
 
31
 
 
32
#include "cnxcc_mod.h"
 
33
 
 
34
extern data_t _data;
 
35
 
 
36
void rpc_kill_call(rpc_t* rpc, void* ctx)
 
37
{
 
38
        call_t *call;
 
39
        hash_tables_t *hts;
 
40
        str callid;
 
41
 
 
42
        if (!rpc->scan(ctx, "S", &callid))
 
43
        {
 
44
                LM_ERR("%s: error reading RPC param\n", __FUNCTION__);
 
45
                return;
 
46
        }
 
47
 
 
48
        if (try_get_call_entry(&callid, &call, &hts) != 0)
 
49
        {
 
50
                LM_ERR("%s: call [%.*s] not found\n", __FUNCTION__, callid.len, callid.s);
 
51
                rpc->fault(ctx, 404, "CallID Not Found");
 
52
                return;
 
53
        }
 
54
 
 
55
        if (call == NULL)
 
56
        {
 
57
                LM_ERR("%s: call [%.*s] is in null state\n", __FUNCTION__, callid.len, callid.s);
 
58
                rpc->fault(ctx, 500, "Call is NULL");
 
59
                return;
 
60
        }
 
61
 
 
62
        LM_ALERT("Killing call [%.*s] via XMLRPC request\n", callid.len, callid.s);
 
63
 
 
64
        lock_get(&call->lock);
 
65
 
 
66
        terminate_call(call);
 
67
 
 
68
        lock_release(&call->lock);
 
69
}
 
70
 
 
71
void rpc_check_client_stats(rpc_t* rpc, void* ctx)
 
72
{
 
73
        call_t *call, *tmp;
 
74
        int index       = 0;
 
75
        str client_id, rows;
 
76
        char row_buffer[512];
 
77
        credit_data_t *credit_data;
 
78
 
 
79
        if (!rpc->scan(ctx, "S", &client_id))
 
80
        {
 
81
                LM_ERR("%s: error reading RPC param\n", __FUNCTION__);
 
82
                return;
 
83
        }
 
84
 
 
85
        if (try_get_credit_data_entry(&client_id, &credit_data) != 0)
 
86
        {
 
87
                LM_ERR("%s: client [%.*s] not found\n", __FUNCTION__, client_id.len, client_id.s);
 
88
                rpc->fault(ctx, 404, "Not Found");
 
89
                return;
 
90
        }
 
91
 
 
92
        if (credit_data == NULL)
 
93
        {
 
94
                LM_ERR("%s: credit data for client [%.*s] is NULL\n", __FUNCTION__, client_id.len, client_id.s);
 
95
                rpc->fault(ctx, 500, "Internal Server Error");
 
96
                return;
 
97
        }
 
98
 
 
99
        lock_get(&credit_data->lock);
 
100
 
 
101
        if (credit_data->number_of_calls <= 0)
 
102
        {
 
103
                lock_release(&credit_data->lock);
 
104
                LM_INFO("No calls for current client\n");
 
105
                return;
 
106
        }
 
107
 
 
108
        rows.len = 0;
 
109
        rows.s   = pkg_malloc(10);
 
110
 
 
111
        if (rows.s == NULL)
 
112
                goto nomem;
 
113
 
 
114
        clist_foreach_safe(credit_data->call_list, call, tmp, next)
 
115
        {
 
116
                int row_len = 0;
 
117
 
 
118
                memset(row_buffer, 0, sizeof(row_buffer));
 
119
 
 
120
                if (credit_data->type == CREDIT_MONEY)
 
121
                        snprintf(row_buffer, sizeof(row_buffer), "id:%d,confirmed:%s,local_consumed_amount:%f,global_consumed_amount:%f,local_max_amount:%f,global_max_amount:%f,call_id:%.*s,start_timestamp:%d"
 
122
                                                                                                                                ",inip:%d,finp:%d,cps:%f;",
 
123
                                                                                                                                 index,
 
124
                                                                                                                                 call->confirmed ? "yes" : "no",
 
125
                                                                                                                                 call->consumed_amount,
 
126
                                                                                                                                 credit_data->consumed_amount,
 
127
                                                                                                                                 call->max_amount,
 
128
                                                                                                                                 credit_data->max_amount,
 
129
                                                                                                                                 call->sip_data.callid.len, call->sip_data.callid.s,
 
130
                                                                                                                                 call->start_timestamp,
 
131
                                                                                                                                 call->money_based.initial_pulse,
 
132
                                                                                                                                 call->money_based.final_pulse,
 
133
                                                                                                                                 call->money_based.cost_per_second
 
134
                                                                                                                                 );
 
135
                else
 
136
                        snprintf(row_buffer, sizeof(row_buffer), "id:%d,confirmed:%s,local_consumed_amount:%d,global_consumed_amount:%d,local_max_amount:%d,global_max_amount:%d,call_id:%.*s,start_timestamp:%d;",
 
137
                                                                                                                                 index,
 
138
                                                                                                                                 call->confirmed ? "yes" : "no",
 
139
                                                                                                                                 (int) call->consumed_amount,
 
140
                                                                                                                                 (int) credit_data->consumed_amount,
 
141
                                                                                                                                 (int) call->max_amount,
 
142
                                                                                                                                 (int) credit_data->max_amount,
 
143
                                                                                                                                 call->sip_data.callid.len, call->sip_data.callid.s,
 
144
                                                                                                                                 call->start_timestamp);
 
145
 
 
146
                row_len         = strlen(row_buffer);
 
147
                rows.s          = pkg_realloc(rows.s, rows.len + row_len);
 
148
 
 
149
                if (rows.s == NULL)
 
150
                {
 
151
                        lock_release(&credit_data->lock);
 
152
                        goto nomem;
 
153
                }
 
154
 
 
155
                memcpy(rows.s + rows.len, row_buffer, row_len);
 
156
                rows.len += row_len;
 
157
 
 
158
                index++;
 
159
        }
 
160
 
 
161
        lock_release(&credit_data->lock);
 
162
 
 
163
        if (rpc->add(ctx, "S", &rows) < 0)
 
164
        {
 
165
                LM_ERR("%s: error creating RPC struct\n", __FUNCTION__);
 
166
        }
 
167
 
 
168
        if (rows.s != NULL)
 
169
                pkg_free(rows.s);
 
170
 
 
171
        return;
 
172
 
 
173
nomem:
 
174
        LM_ERR("No more pkg memory");
 
175
        rpc->fault(ctx, 500, "No more memory\n");
 
176
}
 
177
 
 
178
static int iterate_over_table(hash_tables_t *hts, str *result, credit_type_t type)
 
179
{
 
180
        struct str_hash_entry *h_entry, *tmp;
 
181
        char row_buffer[512];
 
182
        int index = 0;
 
183
 
 
184
        lock_get(&hts->lock);
 
185
 
 
186
        if (hts->credit_data_by_client->table)
 
187
                for(index = 0; index < hts->credit_data_by_client->size; index++)
 
188
                        clist_foreach_safe(&hts->credit_data_by_client->table[index], h_entry, tmp, next)
 
189
                        {
 
190
                                credit_data_t *credit_data      = (credit_data_t *) h_entry->u.p;
 
191
                                lock_get(&credit_data->lock);
 
192
 
 
193
                                int row_len = 0;
 
194
 
 
195
                                memset(row_buffer, 0, sizeof(row_buffer));
 
196
 
 
197
                                if (type == CREDIT_TIME)
 
198
                                {
 
199
                                        snprintf(row_buffer, sizeof(row_buffer), "client_id:%.*s,"
 
200
                                                                                                                         "number_of_calls:%d,"
 
201
                                                                                                                         "concurrent_calls:%d,"
 
202
                                                                                                                         "type:%d,"
 
203
                                                                                                                         "max_amount:%d,"
 
204
                                                                                                                         "consumed_amount:%d;",
 
205
                                                                                                                         credit_data->call_list->client_id.len, credit_data->call_list->client_id.s,
 
206
                                                                                                                         credit_data->number_of_calls,
 
207
                                                                                                                         credit_data->concurrent_calls,
 
208
                                                                                                                         type,
 
209
                                                                                                                         (int) credit_data->max_amount,
 
210
                                                                                                                         (int) credit_data->consumed_amount);
 
211
                                }
 
212
                                else if (type == CREDIT_MONEY)
 
213
                                {
 
214
                                        snprintf(row_buffer, sizeof(row_buffer), "client_id:%.*s,"
 
215
                                                                                                                         "number_of_calls:%d,"
 
216
                                                                                                                         "concurrent_calls:%d,"
 
217
                                                                                                                         "type:%d,"
 
218
                                                                                                                         "max_amount:%f,"
 
219
                                                                                                                         "consumed_amount:%f;",
 
220
                                                                                                                         credit_data->call_list->client_id.len, credit_data->call_list->client_id.s,
 
221
                                                                                                                         credit_data->number_of_calls,
 
222
                                                                                                                         credit_data->concurrent_calls,
 
223
                                                                                                                         type,
 
224
                                                                                                                         credit_data->max_amount,
 
225
                                                                                                                         credit_data->consumed_amount);
 
226
                                }
 
227
                                else
 
228
                                {
 
229
                                        LM_ERR("Unknown credit type: %d", type);
 
230
                                        return -1;
 
231
                                }
 
232
 
 
233
                                lock_release(&credit_data->lock);
 
234
 
 
235
                                row_len         = strlen(row_buffer);
 
236
                                result->s       = pkg_realloc(result->s, result->len + row_len);
 
237
 
 
238
                                if (result->s == NULL)
 
239
                                {
 
240
                                        lock_release(&hts->lock);
 
241
                                        goto nomem;
 
242
                                }
 
243
 
 
244
                                memcpy(result->s + result->len, row_buffer, row_len);
 
245
                                result->len += row_len;
 
246
 
 
247
                        }
 
248
 
 
249
        lock_release(&hts->lock);
 
250
 
 
251
        return 0;
 
252
 
 
253
nomem:
 
254
        LM_ERR("No more pkg memory");
 
255
        return -1;
 
256
}
 
257
 
 
258
void rpc_active_clients(rpc_t* rpc, void* ctx)
 
259
{
 
260
        str rows;
 
261
 
 
262
        rows.s   = pkg_malloc(10);
 
263
 
 
264
        if (rows.s == NULL)
 
265
                goto nomem;
 
266
 
 
267
        rows.len = 0;
 
268
 
 
269
        iterate_over_table(&_data.time, &rows, CREDIT_TIME);
 
270
        iterate_over_table(&_data.money, &rows, CREDIT_MONEY);
 
271
 
 
272
        if (!rpc->add(ctx, "S", &rows) < 0)
 
273
        {
 
274
                LM_ERR("%s: error creating RPC struct\n", __FUNCTION__);
 
275
        }
 
276
 
 
277
        if (rows.s != NULL)
 
278
                pkg_free(rows.s);
 
279
 
 
280
        return;
 
281
 
 
282
nomem:
 
283
        LM_ERR("No more pkg memory");
 
284
        rpc->fault(ctx, 500, "No more memory\n");
 
285
}
 
286