From 338e5df6f96ccf1f3df313593d3fc9bb228f5d85 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Wed, 5 Apr 2017 08:52:01 +0200 Subject: [PATCH] keepalive: format to match default conding style and 80 chars lines --- src/modules/keepalive/api.h | 25 ++++----- src/modules/keepalive/keepalive.h | 40 +++++++------- src/modules/keepalive/keepalive_api.c | 49 ++++++++++------- src/modules/keepalive/keepalive_core.c | 76 ++++++++++++-------------- src/modules/keepalive/keepalive_mod.c | 63 ++++++++++----------- src/modules/keepalive/keepalive_rpc.c | 16 +++--- 6 files changed, 134 insertions(+), 135 deletions(-) diff --git a/src/modules/keepalive/api.h b/src/modules/keepalive/api.h index 17166c16e4a..15c7636e27b 100644 --- a/src/modules/keepalive/api.h +++ b/src/modules/keepalive/api.h @@ -32,22 +32,23 @@ typedef int ka_state; -#define KA_STATE_UNKNOWN 0 -#define KA_STATE_UP 1 -#define KA_STATE_DOWN 2 +#define KA_STATE_UNKNOWN 0 +#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, ka_statechanged_f callback, - 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 struct keepalive_api { - ka_add_dest_f add_destination; - ka_dest_state_f destination_state; +typedef struct keepalive_api +{ + ka_add_dest_f add_destination; + ka_dest_state_f destination_state; } keepalive_api_t; -typedef int (*bind_keepalive_f)(keepalive_api_t* api); -int bind_keepalive(keepalive_api_t* api); +typedef int (*bind_keepalive_f)(keepalive_api_t *api); +int bind_keepalive(keepalive_api_t *api); /** * @brief Load the dispatcher API @@ -62,8 +63,7 @@ static inline int keepalive_load_api(keepalive_api_t *api) return -1; } - if(bindkeepalive(api) < 0) - { + if(bindkeepalive(api) < 0) { LM_ERR("cannot bind keepalive api\n"); return -1; } @@ -71,4 +71,3 @@ static inline int keepalive_load_api(keepalive_api_t *api) } #endif /* __KEEPALIVE_API_H__ */ - diff --git a/src/modules/keepalive/keepalive.h b/src/modules/keepalive/keepalive.h index 11c06aeaf35..504eb7fc4bb 100644 --- a/src/modules/keepalive/keepalive.h +++ b/src/modules/keepalive/keepalive.h @@ -32,39 +32,37 @@ #include "../../core/sr_module.h" -#define KA_INACTIVE_DST 1 /*!< inactive destination */ -#define KA_TRYING_DST 2 /*!< temporary trying destination */ -#define KA_DISABLED_DST 4 /*!< admin disabled destination */ -#define KA_PROBING_DST 8 /*!< checking destination */ -#define KA_STATES_ALL 15 /*!< all bits for the states of destination */ +#define KA_INACTIVE_DST 1 /*!< inactive destination */ +#define KA_TRYING_DST 2 /*!< temporary trying destination */ +#define KA_DISABLED_DST 4 /*!< admin disabled destination */ +#define KA_PROBING_DST 8 /*!< checking destination */ +#define KA_STATES_ALL 15 /*!< all bits for the states of destination */ -#define ds_skip_dst(flags) ((flags) & (KA_INACTIVE_DST|KA_DISABLED_DST)) +#define ds_skip_dst(flags) ((flags) & (KA_INACTIVE_DST | KA_DISABLED_DST)) -#define KA_PROBE_NONE 0 -#define KA_PROBE_ALL 1 -#define KA_PROBE_INACTIVE 2 -#define KA_PROBE_ONLYFLAGGED 3 +#define KA_PROBE_NONE 0 +#define KA_PROBE_ALL 1 +#define KA_PROBE_INACTIVE 2 +#define KA_PROBE_ONLYFLAGGED 3 typedef void (*ka_statechanged_f)(str uri, int state, void *user_attr); typedef struct _ka_dest { str uri; - str owner; // name of destination "owner" - // (module asking to monitor this destination + str owner; // name of destination "owner" + // (module asking to monitor this destination int flags; int state; - time_t last_checked, - last_up, - last_down; + time_t last_checked, last_up, last_down; void *user_attr; ka_statechanged_f statechanged_clb; - struct socket_info * sock; - struct ip_addr ip_address; /*!< IP-Address of the entry */ - unsigned short int port; /*!< Port of the URI */ - unsigned short int proto; /*!< Protocol of the URI */ + struct socket_info *sock; + struct ip_addr ip_address; /*!< IP-Address of the entry */ + unsigned short int port; /*!< Port of the URI */ + unsigned short int proto; /*!< Protocol of the URI */ struct _ka_dest *next; } ka_dest_t; @@ -75,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, void *user_attr); +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); #endif - diff --git a/src/modules/keepalive/keepalive_api.c b/src/modules/keepalive/keepalive_api.c index 29de0f6238f..e3bb3f55a3a 100644 --- a/src/modules/keepalive/keepalive_api.c +++ b/src/modules/keepalive/keepalive_api.c @@ -44,9 +44,9 @@ * Regroup all exported functions in keepalive_api_t structure * */ -int bind_keepalive(keepalive_api_t* api) +int bind_keepalive(keepalive_api_t *api) { - if (!api) { + if(!api) { ERR("Invalid parameter value\n"); return -1; } @@ -59,49 +59,52 @@ 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, void *user_attr) { +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); - dest = (ka_dest_t *) shm_malloc(sizeof(ka_dest_t)); + dest = (ka_dest_t *)shm_malloc(sizeof(ka_dest_t)); if(dest == NULL) { LM_ERR("no more memory.\n"); goto err; } 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) + if(ka_str_copy(uri, &(dest->uri), NULL) < 0) goto err; } else { - if (ka_str_copy(uri , &(dest->uri), "sip:") < 0) + if(ka_str_copy(uri, &(dest->uri), "sip:") < 0) goto err; } // checking uri is valid - if (parse_uri(dest->uri.s, dest->uri.len, &_uri) != 0) { + if(parse_uri(dest->uri.s, dest->uri.len, &_uri) != 0) { LM_ERR("invalid uri <%.*s>\n", dest->uri.len, dest->uri.s); goto err; } - if (ka_str_copy(owner, &(dest->owner), NULL) < 0) + if(ka_str_copy(owner, &(dest->owner), NULL) < 0) goto err; - dest->flags = flags; - dest->statechanged_clb = callback; - dest->user_attr = user_attr; + dest->flags = flags; + dest->statechanged_clb = callback; + dest->user_attr = user_attr; - dest->next = ka_destinations_list->first; + dest->next = ka_destinations_list->first; ka_destinations_list->first = dest; return 0; err: - if (dest) { - if (dest->uri.s) + if(dest) { + if(dest->uri.s) shm_free(dest->uri.s); shm_free(dest); @@ -112,24 +115,28 @@ int ka_add_dest(str uri, str owner, int flags, ka_statechanged_f callback, void /* * TODO */ -int ka_rm_dest() { +int ka_rm_dest() +{ return -1; } /* * */ -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) { + 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) { break; } } - if (ka_dest == NULL) { - return(-1); + if(ka_dest == NULL) { + return (-1); } return ka_dest->state; diff --git a/src/modules/keepalive/keepalive_core.c b/src/modules/keepalive/keepalive_core.c index 19e02b4afd3..a19b2078e75 100644 --- a/src/modules/keepalive/keepalive_core.c +++ b/src/modules/keepalive/keepalive_core.c @@ -43,7 +43,8 @@ struct tm_binds tmb; static void ka_run_route(sip_msg_t *msg, str *uri, char *route); -static void ka_options_callback( struct cell *t, int type, struct tmcb_params *ps ); +static void ka_options_callback(struct cell *t, int type, + struct tmcb_params *ps); /*! \brief @@ -51,32 +52,30 @@ static void ka_options_callback( struct cell *t, int type, struct tmcb_params *p * * This timer is regularly fired. */ -void ka_check_timer(unsigned int ticks, void* param) +void ka_check_timer(unsigned int ticks, void *param) { ka_dest_t *ka_dest; str ka_ping_method = str_init("OPTIONS"); - str ka_ping_from = str_init("sip:dispatcher@localhost"); + str ka_ping_from = str_init("sip:dispatcher@localhost"); str ka_outbound_proxy = {0, 0}; uac_req_t uac_r; LM_DBG("ka check timer\n"); - for(ka_dest = ka_destinations_list->first; ka_dest != NULL; ka_dest = ka_dest->next) { + for(ka_dest = ka_destinations_list->first; ka_dest != NULL; + ka_dest = ka_dest->next) { LM_DBG("ka_check_timer dest:%.*s\n", ka_dest->uri.len, ka_dest->uri.s); /* Send ping using TM-Module. * int request(str* m, str* ruri, str* to, str* from, str* h, * str* b, str *oburi, * transaction_cb cb, void* cbp); */ - set_uac_req(&uac_r, &ka_ping_method, 0, 0, 0, - TMCB_LOCAL_COMPLETED, ka_options_callback, - (void *) ka_dest); - - if (tmb.t_request(&uac_r, - &ka_dest->uri, - &ka_dest->uri, - &ka_ping_from, - &ka_outbound_proxy) < 0) { + set_uac_req(&uac_r, &ka_ping_method, 0, 0, 0, TMCB_LOCAL_COMPLETED, + ka_options_callback, (void *)ka_dest); + + if(tmb.t_request(&uac_r, &ka_dest->uri, &ka_dest->uri, &ka_ping_from, + &ka_outbound_proxy) + < 0) { LM_ERR("unable to ping [%.*s]\n", ka_dest->uri.len, ka_dest->uri.s); } @@ -91,8 +90,8 @@ void ka_check_timer(unsigned int ticks, void* param) * This Function is called, as soon as the Transaction is finished * (e. g. a Response came in, the timeout was hit, ...) */ -static void ka_options_callback( struct cell *t, int type, - struct tmcb_params *ps ) +static void ka_options_callback( + struct cell *t, int type, struct tmcb_params *ps) { str uri = {0, 0}; sip_msg_t *msg = NULL; @@ -101,25 +100,25 @@ static void ka_options_callback( struct cell *t, int type, char *state_routes[] = {"", "keepalive:dst-up", "keepalive:dst-down"}; //NOTE: how to be sure destination is still allocated ? - ka_dest_t *ka_dest = (ka_dest_t *) (*ps->param); + ka_dest_t *ka_dest = (ka_dest_t *)(*ps->param); - uri.s = t->to.s + 5; + uri.s = t->to.s + 5; uri.len = t->to.len - 8; - LM_DBG("OPTIONS-Request was finished with code %d (to %.*s)\n", - ps->code, ka_dest->uri.len, ka_dest->uri.s); //uri.len, uri.s); + LM_DBG("OPTIONS-Request was finished with code %d (to %.*s)\n", ps->code, + ka_dest->uri.len, ka_dest->uri.s); //uri.len, uri.s); // accepting 2XX return codes - if (ps->code >= 200 && ps->code <= 299) { - state = KA_STATE_UP; + if(ps->code >= 200 && ps->code <= 299) { + state = KA_STATE_UP; ka_dest->last_down = time(NULL); } else { - state = KA_STATE_DOWN; - ka_dest->last_up = time(NULL); + state = KA_STATE_DOWN; + ka_dest->last_up = time(NULL); } LM_DBG("new state is: %d\n", state); - if (state != ka_dest->state) { + if(state != ka_dest->state) { ka_run_route(msg, &uri, state_routes[state]); if(ka_dest->statechanged_clb != NULL) { @@ -140,8 +139,7 @@ static void ka_run_route(sip_msg_t *msg, str *uri, char *route) struct run_act_ctx ctx; sip_msg_t *fmsg; - if (route == NULL) - { + if(route == NULL) { LM_ERR("bad route\n"); return; } @@ -149,17 +147,14 @@ static void ka_run_route(sip_msg_t *msg, str *uri, char *route) LM_DBG("ka_run_route event_route[%s]\n", route); rt = route_get(&event_rt, route); - if (rt < 0 || event_rt.rlist[rt] == NULL) - { + if(rt < 0 || event_rt.rlist[rt] == NULL) { LM_DBG("route *%s* does not exist", route); return; } fmsg = msg; - if (fmsg == NULL) - { - if (faked_msg_init() < 0) - { + if(fmsg == NULL) { + if(faked_msg_init() < 0) { LM_ERR("faked_msg_init() failed\n"); return; } @@ -179,22 +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 lp = prefix?strlen(prefix):0; +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)); - if(dest->s == NULL) - { + 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; } - diff --git a/src/modules/keepalive/keepalive_mod.c b/src/modules/keepalive/keepalive_mod.c index 2db3f1e8901..fe277421be7 100644 --- a/src/modules/keepalive/keepalive_mod.c +++ b/src/modules/keepalive/keepalive_mod.c @@ -43,14 +43,14 @@ MODULE_VERSION -static int mod_init(void); +static int mod_init(void); static void mod_destroy(void); -static int ka_mod_add_destination(modparam_t type, void *val); -int ka_init_rpc(void); -int ka_alloc_destinations_list(); -extern void ka_check_timer(unsigned int ticks, void* param); +static int ka_mod_add_destination(modparam_t type, void *val); +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 cmd_is_alive(struct sip_msg *msg, char *str1, char *str2); extern struct tm_binds tmb; @@ -58,36 +58,37 @@ int ka_ping_interval = 30; 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}, +static cmd_export_t cmds[] = { + {"is_alive", (cmd_function)cmd_is_alive, 1, 0, 0, + REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE}, // internal API {"bind_keepalive", (cmd_function)bind_keepalive, 0, 0, 0, 0}, - {0,0,0,0,0,0} + {0, 0, 0, 0, 0, 0} }; -static param_export_t params[]={ - {"ping_interval", PARAM_INT , &ka_ping_interval}, - {"destination" , PARAM_STRING|USE_FUNC_PARAM, (void *)ka_mod_add_destination}, - {0,0,0} +static param_export_t params[] = { + {"ping_interval", PARAM_INT, &ka_ping_interval}, + {"destination", PARAM_STRING | USE_FUNC_PARAM, + (void *)ka_mod_add_destination}, + {0, 0, 0} }; /** module exports */ -struct module_exports exports= { +struct module_exports exports = { "keepalive", DEFAULT_DLFLAGS, /* dlopen flags */ cmds, params, - 0, /* exported statistics */ - 0, /* exported MI functions - no available anymore since 5.0 */ - 0, /* exported pseudo-variables */ - 0, /* extra processes */ - mod_init, /* module initialization function */ + 0, /* exported statistics */ + 0, /* exported MI functions - no available anymore since 5.0 */ + 0, /* exported pseudo-variables */ + 0, /* extra processes */ + mod_init, /* module initialization function */ 0, - (destroy_function) mod_destroy, - 0 /* per-child init function */ + (destroy_function)mod_destroy, + 0 /* per-child init function */ }; @@ -98,8 +99,7 @@ static int mod_init(void) { LM_INFO("Initializing keepalive module\n"); - if (load_tm_api( &tmb ) == -1) - { + if(load_tm_api(&tmb) == -1) { LM_ERR("could not load the TM-functions - please load tm module\n"); return -1; } @@ -109,7 +109,7 @@ static int mod_init(void) return -1; } - if (ka_alloc_destinations_list() < 0) + if(ka_alloc_destinations_list() < 0) return -1; if(register_timer(ka_check_timer, NULL, ka_ping_interval) < 0) { @@ -132,7 +132,7 @@ static void mod_destroy(void) * parses string to dispatcher dst flags set * returns <0 on failure or int with flag on success. */ -int ka_parse_flags( char* flag_str, int flag_len ) +int ka_parse_flags(char *flag_str, int flag_len) { return 0; } @@ -144,7 +144,7 @@ int ka_parse_flags( char* flag_str, int flag_len ) */ static int ka_mod_add_destination(modparam_t type, void *val) { - if (ka_alloc_destinations_list() < 0) + if(ka_alloc_destinations_list() < 0) return -1; str dest = {val, strlen(val)}; @@ -161,12 +161,13 @@ static int ka_mod_add_destination(modparam_t type, void *val) */ int ka_alloc_destinations_list() { - if (ka_destinations_list != NULL) { + if(ka_destinations_list != NULL) { LM_DBG("ka_destinations_list already allocated\n"); return 1; } - ka_destinations_list = (ka_destinations_list_t *) shm_malloc(sizeof(ka_destinations_list_t)); + ka_destinations_list = (ka_destinations_list_t *)shm_malloc( + sizeof(ka_destinations_list_t)); if(ka_destinations_list == NULL) { LM_ERR("no more memory.\n"); return -1; @@ -176,13 +177,13 @@ int ka_alloc_destinations_list() } -static int cmd_is_alive(struct sip_msg* msg, char *str1, char *str2) +static int cmd_is_alive(struct sip_msg *msg, char *str1, char *str2) { 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) { + if(state == KA_STATE_UNKNOWN) { return KA_STATE_UP; } diff --git a/src/modules/keepalive/keepalive_rpc.c b/src/modules/keepalive/keepalive_rpc.c index cb080ca1ee7..3444cf05c87 100644 --- a/src/modules/keepalive/keepalive_rpc.c +++ b/src/modules/keepalive/keepalive_rpc.c @@ -49,8 +49,9 @@ rpc_export_t keepalive_rpc_cmds[] = { {0, 0, 0, 0} }; -int ka_init_rpc(void) { - if (rpc_register_array(keepalive_rpc_cmds) != 0) { +int ka_init_rpc(void) +{ + if(rpc_register_array(keepalive_rpc_cmds) != 0) { LM_ERR("failed to register RPC commands\n"); } @@ -60,7 +61,8 @@ int ka_init_rpc(void) { static const char *keepalive_rpc_list_doc[2] = { "Return the content of dispatcher sets", 0}; -static void keepalive_rpc_list(rpc_t *rpc, void *ctx) { +static void keepalive_rpc_list(rpc_t *rpc, void *ctx) +{ void *foo, *bar, *baz; void *sub; ka_dest_t *dest; @@ -69,9 +71,9 @@ static void keepalive_rpc_list(rpc_t *rpc, void *ctx) { char *_dtime; str text = str_init("foobar"); - if (rpc->add(ctx, "Sd", &text, 42) < 0) + if(rpc->add(ctx, "Sd", &text, 42) < 0) LM_ERR("failed creating RPC struct\n"); - if (rpc->add(ctx, "Sd", &text, 42) < 0) + if(rpc->add(ctx, "Sd", &text, 42) < 0) LM_ERR("failed creating RPC struct\n"); rpc->add(ctx, "{", &foo); @@ -85,9 +87,7 @@ static void keepalive_rpc_list(rpc_t *rpc, void *ctx) { for(dest = ka_destinations_list->first; dest != NULL; dest = dest->next) { rpc->add(ctx, "{", &sub); - rpc->struct_add(sub, "SS", - "uri" , &dest->uri, - "owner", &dest->owner); + rpc->struct_add(sub, "SS", "uri", &dest->uri, "owner", &dest->owner); _ctime = ctime(&dest->last_checked); _ctime[strlen(_ctime) - 1] = '\0';