Skip to content

Commit

Permalink
freeswitch: Add socket flags; Avoid re-learning user/pass for DB sockets
Browse files Browse the repository at this point in the history
While re-learning the user/pass for sockets learned through MI makes
sense (same host/port), the same is not valid for DB-provisioned
sockets.  In that case, just update the table & reload.
  • Loading branch information
liviuchircu committed Feb 12, 2024
1 parent 6e6c60b commit 01b5fdd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
28 changes: 21 additions & 7 deletions modules/freeswitch/fs_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ static fs_evs* get_evs(const str *host, unsigned short port,
list_add(&sock->reconnect_list, fs_sockets_down);
lock_stop_write(sockets_down_lock);
} else {
evs_update(sock, user, pass);
/* avoid interfering with the auth of DB-provisioned sockets */
if (!(sock->flags & FS_EVS_FL_DB))
evs_update(sock, user, pass);

LM_DBG("found & updated FS sock: host=%s, port=%d, user=%s, pass=%s\n",
sock->host.s, sock->port, sock->user.s, sock->pass.s);
Expand Down Expand Up @@ -368,12 +370,8 @@ int dup_common_tag(const str *tag, str *out)
memcpy(t->s.s, tag->s, tag->len);
t->s.s[t->s.len] = '\0';

if (!all_tags) {
all_tags = t;
} else {
t->next = all_tags;
all_tags = t;
}
t->next = all_tags;
all_tags = t;

*out = t->s;
return 0;
Expand Down Expand Up @@ -577,6 +575,20 @@ void evs_unsub(fs_evs *sock, const str *tag, const str_list *name)
LM_ERR("oom! some events may have been skipped\n");
}

void evs_set_flags(fs_evs *sock, unsigned int flags)
{
lock_start_write(sock->stats_lk);
sock->flags |= flags;
lock_stop_write(sock->stats_lk);
}

void evs_reset_flags(fs_evs *sock, unsigned int flags)
{
lock_start_write(sock->stats_lk);
sock->flags &= ~flags;
lock_stop_write(sock->stats_lk);
}

void put_evs(fs_evs *sock)
{
/* prevents deadlocks on shutdown.
Expand Down Expand Up @@ -721,6 +733,8 @@ int fs_bind(struct fs_binds *fapi)
fapi->get_evs_by_url = get_evs_by_url;
fapi->evs_sub = evs_sub;
fapi->evs_unsub = evs_unsub;
fapi->evs_set_flags = evs_set_flags;
fapi->evs_reset_flags = evs_reset_flags;
fapi->put_evs = put_evs;
fapi->get_stats_evs = get_stats_evs;
fapi->put_stats_evs = put_stats_evs;
Expand Down
19 changes: 16 additions & 3 deletions modules/freeswitch/fs_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ struct fs_esl_reply {
struct list_head list;
};

#define FS_EVS_FL_CONNECTED (1<<0)
#define FS_EVS_FL_DB (1<<1) /* this socket is at least reff'ed by the DB */

struct _fs_evs {
str user;
str pass;
Expand All @@ -98,15 +101,15 @@ struct _fs_evs {
fs_stats stats;

int ref;

rw_lock_t *lists_lk; /* protects all three internal lists */
unsigned int flags;

unsigned long esl_reply_id; /* positive ID/counter for each FS esl cmd */
struct list_head esl_replies;

struct list_head events; /* events we're successfully subscribed to */
rw_lock_t *lists_lk; /* protects the flags + above lists */

/* a socket may concurrently be part of up to three lists! */
/* a socket may concurrently be part of up to three global lists */
struct list_head list; /* "fs_sockets" - all FS sockets */
struct list_head reconnect_list; /* "fs_sockets_down" - new/failed conns */
struct list_head esl_cmd_list; /* "fs_sockets_esl" - pending ESL cmds */
Expand All @@ -121,6 +124,8 @@ typedef int (*evs_sub_f) (fs_evs *sock, const str *tag,
const str_list *events, ipc_handler_type ipc_type);
typedef void (*evs_unsub_f) (fs_evs *sock, const str *tag,
const str_list *events);
typedef void (*evs_set_flags_f) (fs_evs *sock, unsigned int flags);
typedef void (*evs_reset_flags_f) (fs_evs *sock, unsigned int flags);

typedef void (*put_evs_f) (fs_evs *sock);
typedef void (*put_stats_evs_f) (fs_evs *sock, str *tag);
Expand Down Expand Up @@ -185,6 +190,12 @@ struct fs_binds {
*/
evs_unsub_f evs_unsub;

/*
* Set/unset flags
*/
evs_set_flags_f evs_set_flags;
evs_reset_flags_f evs_reset_flags;

/*
* Return a FreeSWITCH event socket. If its reference count reaches zero,
* it will get destroyed, along with any subscriptions attached to it.
Expand Down Expand Up @@ -236,6 +247,8 @@ static inline int is_fs_url(str *in)

typedef int (*bind_fs_t)(struct fs_binds *fapi);
int fs_bind(struct fs_binds *fapi);
void evs_set_flags(fs_evs *sock, unsigned int flags);
void evs_reset_flags(fs_evs *sock, unsigned int flags);

static inline int load_fs_api(struct fs_binds *fapi)
{
Expand Down
1 change: 0 additions & 1 deletion modules/freeswitch/fs_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,6 @@ static void apply_socket_commands(void)
handle_reconnects();
lock_stop_write(sockets_down_lock);
lock_stop_write(sockets_lock);

}

void fs_conn_mgr_loop(int proc_no)
Expand Down
2 changes: 2 additions & 0 deletions modules/freeswitch_scripting/fss_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ int fss_db_reload(void)
continue;
}

fs_api.evs_set_flags(sock, FS_EVS_FL_DB);

evlist = _parse_csv_record(&events, CSV_SHM|CSV_DUP_FIELDS);
if (!evlist) {
LM_ERR("failed to parse events: %.*s\n", events.len, events.s);
Expand Down

0 comments on commit 01b5fdd

Please sign in to comment.