/
cpg_utils.c
488 lines (407 loc) · 14.2 KB
/
cpg_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Closed Process Group (Corosync CPG) failover module for Freeswitch
*
* Copyright (C) 2010 Voismart SRL
*
* Authors: Stefano Bossi <sbossi@voismart.it>
*
* Further Contributors:
* Matteo Brancaleoni <mbrancaleoni@voismart.it> - Original idea
*
* This program cannot be modified, distributed or used without
* specific written permission of the copyright holder.
*
* The source code is provided only for evaluation purposes.
* Any usage or license must be negotiated directly with Voismart Srl.
*
* Voismart Srl
* Via Benigno Crespi 12
* 20159 Milano - MI
* ITALY
*
* Phone : +39.02.70633354
*
*/
#include "cpg_utils.h"
#include <net/if.h>
#include <arpa/inet.h>
#include <netlink/route/addr.h>
#include <netlink/route/link.h>
#include "arpator.h"
#include "mod_cpg.h"
typedef enum {
ADD_IP,
DEL_IP
} cmd_t;
/* libnl bug */
extern inline void *nl_object_priv(struct nl_object *obj) {return obj;}
/*helpers*/
int init_handle(struct nl_handle **handle);
int ip_addr(char *ip4addr, char *interface, cmd_t cmd);
int init_handle(struct nl_handle **handle)
{
*handle = nl_handle_alloc();
if (!*handle)
return -1;
if (nl_connect(*handle, NETLINK_ROUTE)) {
nl_handle_destroy(*handle);
return -1;
}
return 0;
}
int ip_addr(char *ip4addr, char *interface, cmd_t cmd)
{
struct nl_handle *nlh = NULL;
struct rtnl_addr *addr = NULL;
struct nl_addr *nl_addr = NULL;
uint32_t binaddr = 0;
int iface_idx = -1;
int err,ret = 0;
if (init_handle(&nlh) != 0) {
return -1;
}
iface_idx = if_nametoindex(interface);
if (iface_idx < 0) {
return -1;
}
addr = rtnl_addr_alloc ();
if (!addr) {
return -1;
}
if (inet_pton(AF_INET, ip4addr, &binaddr) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
"not valid ip address\n");
ret = -1;
goto out;
}
nl_addr = nl_addr_build (AF_INET, &binaddr, sizeof(binaddr));
if (!nl_addr) {
ret = -1;
goto out;
}
rtnl_addr_set_local (addr, nl_addr);
nl_addr_put (nl_addr);
rtnl_addr_set_ifindex (addr, iface_idx);
switch (cmd) {
case ADD_IP:
err = rtnl_addr_add (nlh, addr, 0);
if ( err == -17 ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
"%s is already on %s interface\n", ip4addr, interface);
ret = 0;
} else if ( err < 0 ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
"error %d returned from rtnl_addr_add():\n%s\n",
err, nl_geterror());
ret = -1;
} else {
ret = 0;
}
break;
case DEL_IP:
err = rtnl_addr_delete (nlh, addr, 0);
if (err == -99) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
"%s is not present on %s interface\n", ip4addr, interface);
ret = 0;
} else if (err < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
"error %d returned from rtnl_addr_delete():\n%s\n",
err, nl_geterror());
ret = -1;
} else {
ret = 0;
}
break;
}
out:
if (addr) {
rtnl_addr_put (addr);
}
if (nlh) {
nl_close(nlh);
nl_handle_destroy(nlh);
}
return ret;
}
/*helpers end*/
switch_status_t utils_add_vip(char *ip,char *dev)
{
if ((!zstr(ip)) && (!zstr(dev))) {
if (ip_addr(ip,dev, ADD_IP) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"cannot add IP %s\n", ip);
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,"Ip added\n");
return SWITCH_STATUS_SUCCESS;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid ip or device\n");
return SWITCH_STATUS_FALSE;
}
}
switch_status_t utils_remove_vip(char *ip,char *dev)
{
if ((!zstr(ip)) && (!zstr(dev))) {
if (ip_addr(ip,dev, DEL_IP) < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Cannot remove Ip %s\n", ip);
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,"Ip removed\n");
return SWITCH_STATUS_SUCCESS;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid ip or device\n");
return SWITCH_STATUS_FALSE;
}
}
char *utils_get_mac_addr(char *interface)
{
int buflen = 20;
char *buf = NULL;
struct nl_handle *nlh = NULL;
struct nl_cache *cache = NULL;
struct rtnl_link *link = NULL;
struct nl_addr *addr = NULL;
if (zstr(interface)) {
return NULL;
}
if (init_handle(&nlh) != 0) {
return NULL;
}
if ((cache = rtnl_link_alloc_cache(nlh)) == NULL) {
return NULL;
}
if ((link = rtnl_link_get_by_name(cache, interface)) == NULL) {
goto mac2str_error2;
}
if ((addr = rtnl_link_get_addr(link)) == NULL) {
goto mac2str_error3;
}
if ((buf = calloc(sizeof(char *), buflen)) == NULL) {
goto mac2str_error4;
}
buf = nl_addr2str(addr, buf, buflen);
mac2str_error4:
nl_addr_destroy(addr);
mac2str_error3:
rtnl_link_put(link);
mac2str_error2:
nl_close(nlh);
nl_handle_destroy(nlh);
return buf;
}
void utils_reloadxml()
{
const char *err;
switch_xml_t xml_root;
if ((xml_root = switch_xml_open_root(1, &err))) {
switch_xml_free(xml_root);
} else switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Reload XML [%s]\n", err);
return;
}
switch_status_t utils_start_sofia_profile(char *profile_name)
{
char cmd[128];
char arg[128];
switch_stream_handle_t mystream = { 0 };
if (!zstr(profile_name)) {
if (utils_profile_control(profile_name) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
"profile %s doesn't exist in sip_profiles directory\n",profile_name);
return SWITCH_STATUS_FALSE;
}
switch_snprintf(cmd, sizeof(cmd),"sofia");
switch_snprintf(arg, sizeof(arg), "profile %s start",profile_name);
SWITCH_STANDARD_STREAM(mystream);
if (switch_api_execute(cmd, arg, NULL, &mystream) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"cannot execute sofia api %s\n", profile_name);
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,"profile %s started\n", profile_name);
switch_safe_free(mystream.data);
return SWITCH_STATUS_SUCCESS;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid profile name\n");
return SWITCH_STATUS_FALSE;
}
}
switch_status_t utils_stop_sofia_profile(char *profile_name)
{
char arg[128];
switch_stream_handle_t mystream = { 0 };
if (!zstr(profile_name)) {
switch_snprintf(arg, sizeof(arg),"profile %s stop",profile_name);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"sofia %s\n", arg);
SWITCH_STANDARD_STREAM(mystream);
if (switch_api_execute("sofia", arg, NULL, &mystream) != SWITCH_STATUS_SUCCESS){
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"cannot stop profile %s\n", profile_name);
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,"profile %s stopped\n", profile_name);
switch_safe_free(mystream.data);
return SWITCH_STATUS_SUCCESS;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid profile name\n");
return SWITCH_STATUS_FALSE;
}
return SWITCH_STATUS_FALSE;
}
void utils_hupall(char *profile_name)
{
char cmd[128];
char arg[128];
switch_stream_handle_t mystream = { 0 };
switch_snprintf(cmd, sizeof(cmd),"hupall");
switch_snprintf(arg, sizeof(arg),"normal_clearing sofia_profile_name %s",profile_name);
SWITCH_STANDARD_STREAM(mystream);
if (switch_api_execute(cmd, arg, NULL, &mystream) != SWITCH_STATUS_SUCCESS){
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"cannot hupall for %s\n",profile_name);
return;
}
switch_safe_free(mystream.data);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,"hupall for %s done\n",profile_name);
return;
}
switch_status_t utils_recover(char *profile_name)
{
char arg[128];
switch_stream_handle_t mystream = { 0 };
if (!zstr(profile_name)) {
switch_snprintf(arg, sizeof(arg),"profile %s recover",profile_name);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,"sofia %s\n", arg);
SWITCH_STANDARD_STREAM(mystream);
if (switch_api_execute("sofia", arg, NULL, &mystream) != SWITCH_STATUS_SUCCESS){
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"cannot recover profile %s\n", profile_name);
return SWITCH_STATUS_FALSE;
}
switch_safe_free(mystream.data);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,"profile %s recovered\n", profile_name);
return SWITCH_STATUS_SUCCESS;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,"Invalid profile name\n");
return SWITCH_STATUS_FALSE;
}
return SWITCH_STATUS_FALSE;
}
switch_status_t utils_profile_control(char *profile_name)
{
/*TODO trovare un altro modo per controllare che il profilo sofia sia definito. Probabilmente il controllo sul file è una idiozia.*/
char pathname[128];
switch_snprintf(pathname,sizeof(pathname),"%s/sip_profiles/%s.xml",SWITCH_GLOBAL_dirs.conf_dir,profile_name);
return switch_file_exists(pathname,globals.pool);
}
/*
* invio a sofia(vedere patch in freeswitch) le query che mi arrivano dagli altri nodi. Mando il profile name perchè
* serve a sofia per capire in che profilo eseguire la query SQL
*/
void utils_send_track_event(char *sql, char *profile_name)
{
switch_event_t *event = NULL;
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, "sofia::recovery_recv") == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "profile_name", profile_name);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "sql",sql);
switch_event_fire(&event);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "recovery message sent to sofia\n");
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "recovery message not sent to sofia\n");
}
}
/*
* chiedo alla patch tutte le chiamate. Probabilmente ritorna una una insert con tutte le chiamate da inserire.
* usata all'avvio di un nuovo nodo che deve ricostrurie lo stato delle chiamate in corso alla sua accensione.
*/
void utils_send_request_all(char *profile_name)
{
switch_event_t *event = NULL;
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, "sofia::recovery_recv") == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "profile_name", profile_name);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "request_all","true");
switch_event_fire(&event);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "request_all sent to sofia\n");
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "request_all not sent to sofia\n");
}
}
/*
* callback che conta i risultati di una query sql.
*/
static int show_callback(void *pArg, int argc, char **argv, char **columnNames)
{
int *count;
count = pArg;
(*count)++;
return 0;
}
int utils_count_profile_channels(char *profile_name)
{
char *sql;
char *errmsg;
switch_cache_db_handle_t *db;
int count = 0;
switch_status_t status;
char hostname[256] = "";
gethostname(hostname, sizeof(hostname));
if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
return -1;
}
/*non mi ricordo per quale motivo non usai una count(*)*/
sql = switch_mprintf("select * from channels where hostname='%s' AND (name LIKE 'sofia/%s/%')", hostname, profile_name);
if (!sql)
return -1;
status = switch_cache_db_execute_sql_callback(db, sql, show_callback, &count, &errmsg);
switch_safe_free(sql);
if (errmsg) {
free(errmsg);
errmsg = NULL;
}
if (db) {
switch_cache_db_release_db_handle(&db);
}
if (status == SWITCH_STATUS_FALSE)
return -1;
return count;
}
switch_status_t
utils_clean_up_table(char *runtime_uuid, char *sofia_profile_name)
{
// clean up the table
char *sql = NULL;
sql = switch_mprintf("delete from sip_recovery where "
"runtime_uuid='%q' and profile_name='%q'",
runtime_uuid, sofia_profile_name);
utils_send_track_event(sql, sofia_profile_name);
switch_safe_free(sql);
return SWITCH_STATUS_SUCCESS;
}
char * utils_node_pid_format(unsigned int nodeid) {
static char buffer[100];
struct in_addr saddr;
#if __BYTE_ORDER == __BIG_ENDIAN
saddr.s_addr = swab32(nodeid);
#else
saddr.s_addr = nodeid;
#endif
sprintf(buffer, "%s", inet_ntoa(saddr));
return buffer;
}
switch_bool_t utils_ip_is_valid(char *address) {
unsigned char buf[sizeof(struct in6_addr)];
int s;
if (!address)
return SWITCH_FALSE;
//TODO controlla anche ipv6
s = inet_pton(AF_INET, address, buf);
if (s != 1)
return SWITCH_FALSE;
return SWITCH_TRUE;
}
int utils_get_netmask(char *netmask) {
int nm = atoi(netmask);
return (nm <= 0 || nm > 32)? 32: nm;
}
switch_status_t utils_send_gARP(char *mac, char *address, char *device) {
int ret = net_send_arp_string(mac, "ff:ff:ff:ff:ff:ff", 1,
mac,address,mac, address, device);
if (ret == 0)
return SWITCH_STATUS_SUCCESS;
return SWITCH_STATUS_FALSE;
}