Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions rtrlib/rtr/rtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ int rtr_init(struct rtr_socket *rtr_socket, struct tr_socket *tr, struct pfx_tab
RTR_DBG("Interval value not in range.");
return RTR_INVALID_PARAM;
}

if (pthread_mutex_init(&rtr_socket->mutex, NULL) != 0) {
RTR_DBG("mutex init failed");
return RTR_ERROR;
}

rtr_socket->refresh_interval = refresh_interval;
rtr_socket->expire_interval = expire_interval;
rtr_socket->retry_interval = retry_interval;
Expand All @@ -76,14 +82,25 @@ int rtr_init(struct rtr_socket *rtr_socket, struct tr_socket *tr, struct pfx_tab

int rtr_start(struct rtr_socket *rtr_socket)
{
if (rtr_socket->thread_id)
return RTR_ERROR;
pthread_mutex_lock(&rtr_socket->mutex);

int ret;

if (rtr_socket->thread_id) {
ret = RTR_ERROR;
goto unlock;
}

int rtval = pthread_create(&(rtr_socket->thread_id), NULL, (void *(*)(void *)) &rtr_fsm_start, rtr_socket);

if (rtval == 0)
return RTR_SUCCESS;
return RTR_ERROR;
ret = RTR_SUCCESS;
else
ret = RTR_ERROR;

unlock:
pthread_mutex_unlock(&rtr_socket->mutex);
return ret;
}

void rtr_purge_outdated_records(struct rtr_socket *rtr_socket)
Expand Down Expand Up @@ -228,6 +245,7 @@ void *rtr_fsm_start(struct rtr_socket *rtr_socket)
void rtr_stop(struct rtr_socket *rtr_socket)
{
RTR_DBG("%s()", __func__);
pthread_mutex_lock(&rtr_socket->mutex);
rtr_change_socket_state(rtr_socket, RTR_SHUTDOWN);
if (rtr_socket->thread_id != 0) {
RTR_DBG1("pthread_cancel()");
Expand All @@ -244,9 +262,20 @@ void rtr_stop(struct rtr_socket *rtr_socket)
rtr_socket->thread_id = 0;
rtr_socket->state = RTR_CLOSED;
}
pthread_mutex_unlock(&rtr_socket->mutex);
RTR_DBG1("Socket shut down");
}

void rtr_free(struct rtr_socket *rtr_socket)
{
RTR_DBG("%s()", __func__);

pthread_mutex_destroy(&rtr_socket->mutex);
tr_free(rtr_socket->tr_socket);

RTR_DBG1("Socket freed");
}

RTRLIB_EXPORT const char *rtr_state_to_str(enum rtr_socket_state state)
{
return socket_str_states[state];
Expand Down
2 changes: 2 additions & 0 deletions rtrlib/rtr/rtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ typedef void (*rtr_connection_state_fp)(const struct rtr_socket *rtr_socket, con
* @param version Protocol version used by this socket
* @param has_received_pdus True, if this socket has already received PDUs
* @param spki_table spki_table that stores the router keys obtained from the connected rtr server
* @param mutex mutex to protect members of this struct that are accesed from multiple threads
*/
struct rtr_socket {
struct tr_socket *tr_socket;
Expand All @@ -133,6 +134,7 @@ struct rtr_socket {
bool has_received_pdus;
struct spki_table *spki_table;
bool is_resetting;
pthread_mutex_t mutex;
};

/**
Expand Down
6 changes: 6 additions & 0 deletions rtrlib/rtr/rtr_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ int rtr_start(struct rtr_socket *rtr_socket);
*/
void rtr_stop(struct rtr_socket *rtr_socket);

/**
* @brief Frees resources held by this rtr socket
* @paran[in] rtr_socket rtr_socket that will be freed
*/
void rtr_free(struct rtr_socket *rtr_socket);

#endif
2 changes: 1 addition & 1 deletion rtrlib/rtr_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ RTRLIB_EXPORT void rtr_mgr_free(struct rtr_mgr_config *config)

head = head->next;
for (unsigned int j = 0; j < group_node->group->sockets_len; j++)
tr_free(group_node->group->sockets[j]->tr_socket);
rtr_free(group_node->group->sockets[j]);

lrtr_free(group_node->group);
lrtr_free(group_node);
Expand Down