#define IAX2_TRUNK_PREFACE (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr))
-static struct iax2_trunk_peer {
+struct iax2_trunk_peer {
ast_mutex_t lock;
int sockfd;
struct sockaddr_in addr;
unsigned char *trunkdata;
unsigned int trunkdatalen;
unsigned int trunkdataalloc;
- struct iax2_trunk_peer *next;
int trunkerror;
int calls;
-} *tpeers = NULL;
+ AST_LIST_ENTRY(iax2_trunk_peer) list;
+};
-AST_MUTEX_DEFINE_STATIC(tpeerlock);
+static AST_LIST_HEAD_STATIC(tpeers, iax2_trunk_peer);
struct iax_firmware {
AST_LIST_ENTRY(iax_firmware) list;
static struct iax2_trunk_peer *find_tpeer(struct sockaddr_in *sin, int fd)
{
- struct iax2_trunk_peer *tpeer;
+ struct iax2_trunk_peer *tpeer = NULL;
/* Finds and locks trunk peer */
- ast_mutex_lock(&tpeerlock);
- for (tpeer = tpeers; tpeer; tpeer = tpeer->next) {
- /* We don't lock here because tpeer->addr *never* changes */
+ AST_LIST_LOCK(&tpeers);
+
+ AST_LIST_TRAVERSE(&tpeers, tpeer, list) {
if (!inaddrcmp(&tpeer->addr, sin)) {
ast_mutex_lock(&tpeer->lock);
break;
}
}
+
if (!tpeer) {
if ((tpeer = ast_calloc(1, sizeof(*tpeer)))) {
ast_mutex_init(&tpeer->lock);
memcpy(&tpeer->addr, sin, sizeof(tpeer->addr));
tpeer->trunkact = ast_tvnow();
ast_mutex_lock(&tpeer->lock);
- tpeer->next = tpeers;
tpeer->sockfd = fd;
- tpeers = tpeer;
#ifdef SO_NO_CHECK
setsockopt(tpeer->sockfd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
#endif
ast_log(LOG_DEBUG, "Created trunk peer for '%s:%d'\n", ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port));
+ AST_LIST_INSERT_TAIL(&tpeers, tpeer, list);
}
}
- ast_mutex_unlock(&tpeerlock);
+
+ AST_LIST_UNLOCK(&tpeers);
+
return tpeer;
}
static int timing_read(int *id, int fd, short events, void *cbdata)
{
char buf[1024];
- int res;
- struct iax2_trunk_peer *tpeer, *prev = NULL, *drop=NULL;
- int processed = 0;
- int totalcalls = 0;
+ int res, processed = 0, totalcalls = 0;
+ struct iax2_trunk_peer *tpeer = NULL, *drop = NULL;
#ifdef ZT_TIMERACK
int x = 1;
#endif
}
}
/* For each peer that supports trunking... */
- ast_mutex_lock(&tpeerlock);
- tpeer = tpeers;
- while(tpeer) {
+ AST_LIST_LOCK(&tpeers);
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&tpeers, tpeer, list) {
processed++;
res = 0;
ast_mutex_lock(&tpeer->lock);
if (!drop && iax2_trunk_expired(tpeer, &now)) {
/* Take it out of the list, but don't free it yet, because it
could be in use */
- if (prev)
- prev->next = tpeer->next;
- else
- tpeers = tpeer->next;
+ AST_LIST_REMOVE_CURRENT(&tpeers, list);
drop = tpeer;
} else {
res = send_trunk(tpeer, &now);
totalcalls += res;
res = 0;
ast_mutex_unlock(&tpeer->lock);
- prev = tpeer;
- tpeer = tpeer->next;
}
- ast_mutex_unlock(&tpeerlock);
+ AST_LIST_TRAVERSE_SAFE_END
+ AST_LIST_UNLOCK(&tpeers);
+
if (drop) {
ast_mutex_lock(&drop->lock);
/* Once we have this lock, we're sure nobody else is using it or could use it once we release it,
free(drop);
}
+
if (iaxtrunkdebug)
ast_verbose("Ending trunk processing with %d peers and %d call chunks processed\n", processed, totalcalls);
- iaxtrunkdebug =0;
+ iaxtrunkdebug = 0;
+
return 1;
}