Skip to content

Commit

Permalink
keepalive: allow variables in cfg function parameters
Browse files Browse the repository at this point in the history
- use str* instead of str for api functions
  • Loading branch information
miconda committed May 29, 2017
1 parent 857800a commit 211dc1b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/modules/keepalive/api.h
Expand Up @@ -36,10 +36,10 @@ typedef int ka_state;
#define KA_STATE_UP 1
#define KA_STATE_DOWN 2

typedef void (*ka_statechanged_f)(str uri, int state, void *user_attr);
typedef int (*ka_add_dest_f)(str uri, str owner, int flags,
typedef void (*ka_statechanged_f)(str *uri, int state, void *user_attr);
typedef int (*ka_add_dest_f)(str *uri, str *owner, int flags,
ka_statechanged_f callback, void *user_attr);
typedef ka_state (*ka_dest_state_f)(str uri);
typedef ka_state (*ka_dest_state_f)(str *uri);

typedef struct keepalive_api
{
Expand Down Expand Up @@ -70,4 +70,4 @@ static inline int keepalive_load_api(keepalive_api_t *api)
return 0;
}

#endif /* __KEEPALIVE_API_H__ */
#endif /* __KEEPALIVE_API_H__ */
8 changes: 4 additions & 4 deletions src/modules/keepalive/keepalive.h
Expand Up @@ -45,7 +45,7 @@
#define KA_PROBE_INACTIVE 2
#define KA_PROBE_ONLYFLAGGED 3

typedef void (*ka_statechanged_f)(str uri, int state, void *user_attr);
typedef void (*ka_statechanged_f)(str *uri, int state, void *user_attr);

typedef struct _ka_dest
{
Expand Down Expand Up @@ -73,9 +73,9 @@ typedef struct _ka_destinations_list

extern ka_destinations_list_t *ka_destinations_list;

int ka_add_dest(str uri, str owner, int flags, ka_statechanged_f callback,
int ka_add_dest(str *uri, str *owner, int flags, ka_statechanged_f callback,
void *user_attr);
int ka_destination_state(str uri);
int ka_str_copy(str src, str *dest, char *prefix);
int ka_destination_state(str *uri);
int ka_str_copy(str *src, str *dest, char *prefix);

#endif
15 changes: 8 additions & 7 deletions src/modules/keepalive/keepalive_api.c
Expand Up @@ -59,13 +59,13 @@ int bind_keepalive(keepalive_api_t *api)
/*
* Add a new destination in keepalive pool
*/
int ka_add_dest(str uri, str owner, int flags, ka_statechanged_f callback,
int ka_add_dest(str *uri, str *owner, int flags, ka_statechanged_f callback,
void *user_attr)
{
struct sip_uri _uri;
ka_dest_t *dest;

LM_INFO("adding destination: %.*s\n", uri.len, uri.s);
LM_INFO("adding destination: %.*s\n", uri->len, uri->s);

dest = (ka_dest_t *)shm_malloc(sizeof(ka_dest_t));
if(dest == NULL) {
Expand All @@ -74,8 +74,8 @@ int ka_add_dest(str uri, str owner, int flags, ka_statechanged_f callback,
}
memset(dest, 0, sizeof(ka_dest_t));

if(uri.len >= 4 && (!strncasecmp("sip:", uri.s, 4)
|| !strncasecmp("sips:", uri.s, 5))) {
if(uri->len >= 4 && (!strncasecmp("sip:", uri->s, 4)
|| !strncasecmp("sips:", uri->s, 5))) {
// protocol found
if(ka_str_copy(uri, &(dest->uri), NULL) < 0)
goto err;
Expand Down Expand Up @@ -123,14 +123,15 @@ int ka_rm_dest()
/*
*
*/
ka_state ka_destination_state(str destination)
ka_state ka_destination_state(str *destination)
{
ka_dest_t *ka_dest = NULL;

for(ka_dest = ka_destinations_list->first; ka_dest != NULL;
ka_dest = ka_dest->next) {
if(strncmp(ka_dest->uri.s + 4, destination.s, ka_dest->uri.len - 4)
== 0) {
if((destination->len == ka_dest->uri.len - 4)
&& (strncmp(ka_dest->uri.s + 4, destination->s, ka_dest->uri.len - 4)
== 0)) {
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/modules/keepalive/keepalive_core.c
Expand Up @@ -122,7 +122,7 @@ static void ka_options_callback(
ka_run_route(msg, &uri, state_routes[state]);

if(ka_dest->statechanged_clb != NULL) {
ka_dest->statechanged_clb(ka_dest->uri, state, ka_dest->user_attr);
ka_dest->statechanged_clb(&ka_dest->uri, state, ka_dest->user_attr);
}

ka_dest->state = state;
Expand Down Expand Up @@ -174,21 +174,21 @@ static void ka_run_route(sip_msg_t *msg, str *uri, char *route)
/*
* copy str into dynamically allocated shm memory
*/
int ka_str_copy(str src, str *dest, char *prefix)
int ka_str_copy(str *src, str *dest, char *prefix)
{
int lp = prefix ? strlen(prefix) : 0;

dest->s = (char *)shm_malloc((src.len + 1 + lp) * sizeof(char));
dest->s = (char *)shm_malloc((src->len + 1 + lp) * sizeof(char));
if(dest->s == NULL) {
LM_ERR("no more memory!\n");
return -1;
}

if(prefix)
strncpy(dest->s, prefix, lp);
strncpy(dest->s + lp, src.s, src.len);
dest->s[src.len + lp] = '\0';
dest->len = src.len + lp;
strncpy(dest->s + lp, src->s, src->len);
dest->s[src->len + lp] = '\0';
dest->len = src->len + lp;

return 0;
}
25 changes: 17 additions & 8 deletions src/modules/keepalive/keepalive_mod.c
Expand Up @@ -34,6 +34,7 @@
#include <sys/types.h>
#include <unistd.h>

#include "../../core/mod_fix.h"
#include "../tm/tm_load.h"
#include "../dispatcher/api.h"

Expand All @@ -50,7 +51,7 @@ int ka_init_rpc(void);
int ka_alloc_destinations_list();
extern void ka_check_timer(unsigned int ticks, void *param);

static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);
static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2);

extern struct tm_binds tmb;

Expand All @@ -59,8 +60,8 @@ ka_destinations_list_t *ka_destinations_list = NULL;


static cmd_export_t cmds[] = {
{"is_alive", (cmd_function)cmd_is_alive, 1, 0, 0,
REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE},
{"is_alive", (cmd_function)w_cmd_is_alive, 1,
fixup_spve_null, 0, ANY_ROUTE},
// internal API
{"bind_keepalive", (cmd_function)bind_keepalive, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
Expand Down Expand Up @@ -151,7 +152,7 @@ static int ka_mod_add_destination(modparam_t type, void *val)
str owner = str_init("_params");
LM_DBG("adding destination %.*s\n", dest.len, dest.s);

return ka_add_dest(dest, owner, 0, 0, 0);
return ka_add_dest(&dest, &owner, 0, 0, 0);
}

/*
Expand All @@ -176,11 +177,8 @@ int ka_alloc_destinations_list()
return 0;
}


static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)
static int ki_is_alive(sip_msg_t *msg, str *dest)
{
str dest = {str1, strlen(str1)};

ka_state state = ka_destination_state(dest);
// must not return 0, as it stops dialplan execution
if(state == KA_STATE_UNKNOWN) {
Expand All @@ -189,3 +187,14 @@ static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)

return state;
}

static int w_cmd_is_alive(struct sip_msg *msg, char *str1, char *str2)
{
str dest = STR_NULL;

if(fixup_get_svalue(msg, (gparam_t*)str1, &dest)!=0) {
LM_ERR("failed to get dest parameter\n");
return -1;
}
return ki_is_alive(msg, &dest);
}

0 comments on commit 211dc1b

Please sign in to comment.