Skip to content

Commit

Permalink
keepalive: clang-format for coherent indentation and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxmaniac committed May 18, 2023
1 parent b33f7a2 commit 567384c
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 172 deletions.
9 changes: 5 additions & 4 deletions src/modules/keepalive/api.h
Expand Up @@ -37,12 +37,13 @@ typedef int ka_state;
#define KA_STATE_UP 1
#define KA_STATE_DOWN 2

typedef int (*ka_add_dest_f)(str *uri, str *owner, int flags,
int ping_interval, ka_statechanged_f statechanged_clb, ka_response_f response_clb,
void *user_attr);
typedef int (*ka_add_dest_f)(str *uri, str *owner, int flags, int ping_interval,
ka_statechanged_f statechanged_clb, ka_response_f response_clb,
void *user_attr);
typedef ka_state (*ka_dest_state_f)(str *uri);
typedef int (*ka_del_destination_f)(str *uri, str *owner);
typedef int (*ka_find_destination_f)(str *uri, str *owner,ka_dest_t **target,ka_dest_t **head);
typedef int (*ka_find_destination_f)(
str *uri, str *owner, ka_dest_t **target, ka_dest_t **head);
typedef int (*ka_lock_destination_list_f)();
typedef int (*ka_unlock_destination_list_f)();

Expand Down
29 changes: 16 additions & 13 deletions src/modules/keepalive/keepalive.h
Expand Up @@ -36,7 +36,7 @@
#include "../tm/tm_load.h"

#define KA_INACTIVE_DST 1 /*!< inactive destination */
#define KA_TRYING_DST 2 /*!< temporary trying 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 */
Expand All @@ -50,7 +50,8 @@ extern int ka_ping_interval;
#define KA_PROBE_INACTIVE 2
#define KA_PROBE_ONLYFLAGGED 3

#define KA_FIRST_TRY_DELAY 500 /* First OPTIONS send is done 500 millis after adding the destination */
#define KA_FIRST_TRY_DELAY \
500 /* First OPTIONS send is done 500 millis after adding the destination */

typedef void (*ka_statechanged_f)(str *uri, int state, void *user_attr);
typedef void (*ka_response_f)(
Expand All @@ -70,12 +71,12 @@ typedef struct _ka_dest
str uri;
str owner; // name of destination "owner"
// (module asking to monitor this destination
str uuid; // Universal id for this record
str uuid; // Universal id for this record
int flags;
int state;
time_t last_checked, last_up, last_down;
int counter; // counts unreachable attempts
ticks_t ping_interval; /*!< Actual interval between OPTIONS */
int counter; // counts unreachable attempts
ticks_t ping_interval; /*!< Actual interval between OPTIONS */

void *user_attr;
ka_statechanged_f statechanged_clb;
Expand All @@ -84,8 +85,9 @@ typedef struct _ka_dest
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 timer_ln *timer; /*!< Timer firing the OPTIONS test */
gen_lock_t lock; /*!< Lock of this record to prevent being removed while running */
struct timer_ln *timer; /*!< Timer firing the OPTIONS test */
gen_lock_t
lock; /*!< Lock of this record to prevent being removed while running */
struct _ka_dest *next;
} ka_dest_t;

Expand All @@ -99,16 +101,17 @@ extern ka_destinations_list_t *ka_destinations_list;
extern int ka_counter_del;
extern sruid_t ka_sruid;

ticks_t ka_check_timer(ticks_t ticks, struct timer_ln* tl, void* param);
ticks_t ka_check_timer(ticks_t ticks, struct timer_ln *tl, void *param);

int ka_add_dest(str *uri, str *owner, int flags, int ping_interval,
ka_statechanged_f statechanged_clb, ka_response_f response_clb,
void *user_attr);
ka_statechanged_f statechanged_clb, ka_response_f response_clb,
void *user_attr);
int ka_destination_state(str *uri);
int ka_str_copy(str *src, str *dest, char *prefix);
int free_destination(ka_dest_t *dest) ;
int ka_del_destination(str *uri, str *owner) ;
int ka_find_destination(str *uri, str *owner, ka_dest_t **target ,ka_dest_t **head);
int free_destination(ka_dest_t *dest);
int ka_del_destination(str *uri, str *owner);
int ka_find_destination(
str *uri, str *owner, ka_dest_t **target, ka_dest_t **head);
int ka_find_destination_by_uuid(str uuid, ka_dest_t **target, ka_dest_t **head);
int ka_lock_destination_list();
int ka_unlock_destination_list();
Expand Down
105 changes: 60 additions & 45 deletions src/modules/keepalive/keepalive_api.c
Expand Up @@ -63,16 +63,18 @@ int bind_keepalive(keepalive_api_t *api)
* Add a new destination in keepalive pool
*/
int ka_add_dest(str *uri, str *owner, int flags, int ping_interval,
ka_statechanged_f statechanged_clb, ka_response_f response_clb, void *user_attr)
ka_statechanged_f statechanged_clb, ka_response_f response_clb,
void *user_attr)
{
struct sip_uri _uri;
ka_dest_t *dest=0,*hollow=0;
ka_dest_t *dest = 0, *hollow = 0;

LM_DBG("adding destination: %.*s\n", uri->len, uri->s);
ka_lock_destination_list();
if(ka_find_destination(uri , owner , &dest , &hollow)){
LM_INFO("uri [%.*s] already in stack --ignoring \r\n",uri->len, uri->s);
dest->counter=0;
if(ka_find_destination(uri, owner, &dest, &hollow)) {
LM_INFO("uri [%.*s] already in stack --ignoring \r\n", uri->len,
uri->s);
dest->counter = 0;
ka_unlock_destination_list();
return -2;
}
Expand All @@ -84,8 +86,9 @@ int ka_add_dest(str *uri, str *owner, int flags, int ping_interval,
}
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 All @@ -111,20 +114,21 @@ int ka_add_dest(str *uri, str *owner, int flags, int ping_interval,
dest->statechanged_clb = statechanged_clb;
dest->response_clb = response_clb;
dest->user_attr = user_attr;
dest->ping_interval = MS_TO_TICKS((ping_interval == 0 ? ka_ping_interval : ping_interval) * 1000) ;
if (lock_init(&dest->lock)==0){
dest->ping_interval = MS_TO_TICKS(
(ping_interval == 0 ? ka_ping_interval : ping_interval) * 1000);
if(lock_init(&dest->lock) == 0) {
LM_ERR("failed initializing Lock \n");
}

dest->timer = timer_alloc();
if (dest->timer == NULL) {
dest->timer = timer_alloc();
if(dest->timer == NULL) {
LM_ERR("failed allocating timer\n");
goto err;
}
}

timer_init(dest->timer, ka_check_timer, dest, 0);

if(timer_add(dest->timer, MS_TO_TICKS(KA_FIRST_TRY_DELAY)) < 0){
if(timer_add(dest->timer, MS_TO_TICKS(KA_FIRST_TRY_DELAY)) < 0) {
LM_ERR("failed to start timer\n");
goto err;
}
Expand Down Expand Up @@ -157,7 +161,8 @@ ka_state ka_destination_state(str *destination)
for(ka_dest = ka_destinations_list->first; ka_dest != NULL;
ka_dest = ka_dest->next) {
if((destination->len == ka_dest->uri.len - 4)
&& (strncmp(ka_dest->uri.s + 4, destination->s, ka_dest->uri.len - 4)
&& (strncmp(ka_dest->uri.s + 4, destination->s,
ka_dest->uri.len - 4)
== 0)) {
break;
}
Expand All @@ -179,22 +184,23 @@ ka_state ka_destination_state(str *destination)
* *
* @result 1 successful , -1 fail
*/
int ka_del_destination(str *uri, str *owner){
int ka_del_destination(str *uri, str *owner)
{
LM_DBG("removing destination: %.*s\n", uri->len, uri->s);
ka_dest_t *target=0,*head=0;
ka_dest_t *target = 0, *head = 0;
ka_lock_destination_list();

if(!ka_find_destination(uri,owner,&target,&head)){
if(!ka_find_destination(uri, owner, &target, &head)) {
LM_ERR("Couldn't find destination \r\n");
goto err;
}

if(!target){
if(!target) {
LM_ERR("Couldn't find destination \r\n");
goto err;
}
lock_get(&target->lock);
if(!head){
if(!head) {
LM_DBG("There isn't any head so maybe it is first \r\n");
ka_destinations_list->first = target->next;
} else {
Expand All @@ -219,22 +225,25 @@ int ka_del_destination(str *uri, str *owner){
* *
* @result 1 successful , -1 fail
*/
int ka_find_destination(str *uri, str *owner, ka_dest_t **target, ka_dest_t **head){
int ka_find_destination(
str *uri, str *owner, ka_dest_t **target, ka_dest_t **head)
{

ka_dest_t *dest=0 ,*temp=0;
ka_dest_t *dest = 0, *temp = 0;
LM_DBG("finding destination: %.*s\n", uri->len, uri->s);

for(dest = ka_destinations_list->first; dest; temp=dest, dest=dest->next) {
if (STR_EQ(*uri, dest->uri) && STR_EQ(*owner, dest->owner)){
for(dest = ka_destinations_list->first; dest;
temp = dest, dest = dest->next) {
if(STR_EQ(*uri, dest->uri) && STR_EQ(*owner, dest->owner)) {
*head = temp;
*target = dest;
LM_DBG("destination is found [target : %p] [head : %p] \r\n",target,temp);
LM_DBG("destination is found [target : %p] [head : %p] \r\n",
target, temp);
return 1;
}
}

return 0;

}

/*!
Expand All @@ -247,22 +256,24 @@ int ka_find_destination(str *uri, str *owner, ka_dest_t **target, ka_dest_t **he
* *
* @result 1 successful , 0 fail
*/
int ka_find_destination_by_uuid(str uuid, ka_dest_t **target, ka_dest_t **head){
ka_dest_t *dest=0 ,*temp=0;
int ka_find_destination_by_uuid(str uuid, ka_dest_t **target, ka_dest_t **head)
{
ka_dest_t *dest = 0, *temp = 0;

LM_DBG("finding destination with uuid:%.*s\n", uuid.len, uuid.s);

for(dest = ka_destinations_list->first; dest; temp = dest, dest = dest->next) {
if (STR_EQ(uuid, dest->uuid)){
for(dest = ka_destinations_list->first; dest;
temp = dest, dest = dest->next) {
if(STR_EQ(uuid, dest->uuid)) {
*head = temp;
*target = dest;
LM_DBG("destination is found [target : %p] [head : %p] \r\n",target,temp);
LM_DBG("destination is found [target : %p] [head : %p] \r\n",
target, temp);
return 1;
}
}

return 0;

}


Expand All @@ -274,15 +285,17 @@ int ka_find_destination_by_uuid(str uuid, ka_dest_t **target, ka_dest_t **head){
* @result 1 successful , -1 fail
*/
int free_destination(ka_dest_t *dest){

if(dest){
if(timer_del(dest->timer) < 0){
LM_ERR("failed to remove timer for destination <%.*s>\n", dest->uri.len, dest->uri.s);
return -1;
}

timer_free(dest->timer);
int free_destination(ka_dest_t *dest)
{

if(dest) {
if(timer_del(dest->timer) < 0) {
LM_ERR("failed to remove timer for destination <%.*s>\n",
dest->uri.len, dest->uri.s);
return -1;
}

timer_free(dest->timer);
lock_destroy(dest->lock);
if(dest->uri.s)
shm_free(dest->uri.s);
Expand All @@ -292,23 +305,25 @@ int free_destination(ka_dest_t *dest){

if(dest->uuid.s)
shm_free(dest->uuid.s);

shm_free(dest);
}

return 1;
}

int ka_lock_destination_list(){
if(ka_destinations_list){
int ka_lock_destination_list()
{
if(ka_destinations_list) {
lock_get(ka_destinations_list->lock);
return 1;
}
return 0;
}

int ka_unlock_destination_list(){
if(ka_destinations_list){
int ka_unlock_destination_list()
{
if(ka_destinations_list) {
lock_release(ka_destinations_list->lock);
return 1;
}
Expand Down

0 comments on commit 567384c

Please sign in to comment.