Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
chan_iax2.c File Reference

Implementation of Inter-Asterisk eXchange Version 2 as specified in RFC 5456. More...

#include "asterisk.h"
#include <sys/mman.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <sys/time.h>
#include <signal.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <regex.h>
#include "asterisk/paths.h"
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/config.h"
#include "asterisk/cli.h"
#include "asterisk/translate.h"
#include "asterisk/md5.h"
#include "asterisk/crypto.h"
#include "asterisk/acl.h"
#include "asterisk/manager.h"
#include "asterisk/callerid.h"
#include "asterisk/app.h"
#include "asterisk/mwi.h"
#include "asterisk/astdb.h"
#include "asterisk/musiconhold.h"
#include "asterisk/features.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/localtime.h"
#include "asterisk/dnsmgr.h"
#include "asterisk/devicestate.h"
#include "asterisk/stringfields.h"
#include "asterisk/linkedlists.h"
#include "asterisk/astobj2.h"
#include "asterisk/timing.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/test.h"
#include "asterisk/security_events.h"
#include "asterisk/stasis_endpoints.h"
#include "asterisk/bridge.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_system.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/format_cache.h"
#include "asterisk/format_compatibility.h"
#include "asterisk/format_cap.h"
#include "iax2/include/iax2.h"
#include "iax2/include/firmware.h"
#include "iax2/include/parser.h"
#include "iax2/include/provision.h"
#include "iax2/include/codec_pref.h"
#include "iax2/include/format_compatibility.h"
#include "iax2/include/netsock.h"
#include "jitterbuf.h"

Go to the source code of this file.

Data Structures

struct  active_list
 
struct  addr_range
 
struct  call_number_pool
 
struct  chan_iax2_pvt
 
struct  create_addr_info
 
struct  dpcache
 
struct  dpreq_data
 
struct  dynamic_list
 
struct  iax2_context
 
struct  iax2_dpcache
 
struct  iax2_peer
 
struct  iax2_pkt_buf
 
struct  iax2_registry
 
struct  iax2_thread
 
struct  iax2_trunk_peer
 
struct  iax2_user
 
struct  iax_rr
 
struct  idle_list
 
struct  parsed_dial_string
 
struct  peercnt
 
struct  registrations
 
struct  show_peers_context
 
struct  chan_iax2_pvt::signaling_queue
 
struct  signaling_queue_entry
 
struct  tpeers
 

Macros

#define ACN_FORMAT1   "%-24.25s %4u %4d %4d %5d %3d %5d %4d %6d %4d %4d %5d %3d %5d %4d %6d %s%s %4s%s\n"
 
#define ACN_FORMAT2   "%s %u %d %d %d %d %d %d %d %d %d %d %d %d %d %d %s%s %s%s\n"
 
#define AUTH_METHOD_NAMES_BUFSIZE   19
 
#define CALLNO_ENTRY_GET_CALLNO(a)    ((a) & 0x7FFF)
 
#define CALLNO_ENTRY_IS_VALIDATED(a)   ((a) & 0x8000)
 
#define CALLNO_ENTRY_SET_VALIDATED(a)   ((a) |= 0x8000)
 
#define CALLNO_ENTRY_TO_PTR(a)   ((void *)(unsigned long)(a))
 
#define CALLNO_TO_PTR(a)   ((void *)(unsigned long)(a))
 
#define CALLTOKEN_HASH_FORMAT   "%s%u%d" /* address + port + ts + randomcalldata */
 
#define CALLTOKEN_IE_FORMAT   "%u?%s" /* time + ? + (40 char hash) */
 
#define DEBUG_SCHED_MULTITHREAD
 
#define DEBUG_SUPPORT
 
#define DEFAULT_CONTEXT   "default"
 
#define DEFAULT_DROP   3
 
#define DEFAULT_FREQ_NOTOK   10 * 1000 /* How often to check, if the host is down... */
 
#define DEFAULT_FREQ_OK   60 * 1000 /* How often to check for the host to be up */
 
#define DEFAULT_MAX_THREAD_COUNT   100
 
#define DEFAULT_MAXMS   2000 /* Must be faster than 2 seconds by default */
 
#define DEFAULT_RETRY_TIME   1000
 
#define DEFAULT_THREAD_COUNT   10
 
#define DEFAULT_TRUNKDATA   640 * 10
 
#define FORMAT   "%-15.15s %-20.20s %-15.15s %-15.15s %-5.5s %-5.10s\n"
 
#define FORMAT   "%-45.45s %-6.6s %-10.10s %-45.45s %8d %s\n"
 
#define FORMAT   "%-20.20s %-40.40s %-10.10s %5.5d/%5.5d %5.5d/%5.5d %-5.5dms %-4.4dms %-4.4dms %-6.6s %s%s %3s%s\n"
 
#define FORMAT2   "%-15.15s %-20.20s %-15.15d %-15.15s %-5.5s %-5.10s\n"
 
#define FORMAT2   "%-45.45s %-6.6s %-10.10s %-45.45s %8.8s %s\n"
 
#define FORMAT2   "%-20.20s %-40.40s %-10.10s %-11.11s %-11.11s %-7.7s %-6.6s %-6.6s %s %s %9s\n"
 
#define FORMATB   "%-20.20s %-40.40s %-10.10s %5.5d/%5.5d %5.5d/%5.5d [Native Bridged to ID=%5.5d]\n"
 
#define GAMMA   (0.01)
 
#define IAX2_TRUNK_PREFACE   (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr))
 
#define IAX_ALLOWFWDOWNLOAD   (uint64_t)(1LLU << 26)
 
#define IAX_ALREADYGONE   (uint64_t)(1LLU << 9)
 
#define IAX_CALLENCRYPTED(pvt)   (ast_test_flag64(pvt, IAX_ENCRYPTED) && ast_test_flag64(pvt, IAX_KEYPOPULATED))
 
#define IAX_CAPABILITY_FULLBANDWIDTH   0xFFFF
 
#define IAX_CAPABILITY_LOWBANDWIDTH
 
#define IAX_CAPABILITY_LOWFREE
 
#define IAX_CAPABILITY_MEDBANDWIDTH
 
#define IAX_CODEC_NOCAP   (uint64_t)(1LLU << 16)
 
#define IAX_CODEC_NOPREFS   (uint64_t)(1LLU << 15)
 
#define IAX_CODEC_USER_FIRST   (uint64_t)(1LLU << 14)
 
#define IAX_DEBUGDIGEST(msg, key)
 
#define IAX_DELAYPBXSTART   (uint64_t)(1LLU << 25)
 
#define IAX_DELME   (uint64_t)(1LLU << 1)
 
#define IAX_DYNAMIC   (uint64_t)(1LLU << 6)
 
#define IAX_ENCRYPTED   (uint64_t)(1LLU << 12)
 
#define IAX_FORCE_ENCRYPT   (uint64_t)(1LLU << 30)
 
#define IAX_HASCALLERID   (uint64_t)(1LLU << 0)
 
#define IAX_IMMEDIATE   (uint64_t)(1LLU << 27)
 
#define IAX_KEYPOPULATED   (uint64_t)(1LLU << 13)
 
#define IAX_MAXAUTHREQ   (uint64_t)(1LLU << 24)
 
#define IAX_NOTRANSFER   (uint64_t)(1LLU << 4)
 
#define IAX_PROVISION   (uint64_t)(1LLU << 10)
 
#define IAX_QUELCH   (uint64_t)(1LLU << 11)
 
#define IAX_RECVCONNECTEDLINE   (uint64_t)(1LLU << 29)
 
#define IAX_RTAUTOCLEAR   (uint64_t)(1LLU << 19)
 
#define IAX_RTCACHEFRIENDS   (uint64_t)(1LLU << 17)
 
#define IAX_RTIGNOREREGEXPIRE   (uint64_t)(1LLU << 21)
 
#define IAX_RTSAVE_SYSNAME   (uint64_t)(1LLU << 8)
 
#define IAX_RTUPDATE   (uint64_t)(1LLU << 18)
 
#define IAX_SENDANI   (uint64_t)(1LLU << 7)
 
#define IAX_SENDCONNECTEDLINE   (uint64_t)(1LLU << 28)
 
#define IAX_SHRINKCALLERID   (uint64_t)(1LLU << 31)
 
#define IAX_TEMPONLY   (uint64_t)(1LLU << 2)
 
#define IAX_TRANSFERMEDIA   (uint64_t)(1LLU << 23)
 
#define IAX_TRUNK   (uint64_t)(1LLU << 3)
 
#define IAX_TRUNKTIMESTAMPS   (uint64_t)(1LLU << 22)
 
#define IAX_USEJITTERBUF   (uint64_t)(1LLU << 5)
 
#define MARK_IAX_SUBCLASS_TX   0x8000
 
#define MAX_JITTER_BUFFER   50
 
#define MAX_PEER_BUCKETS   563
 
#define MAX_RETRY_TIME   10000
 
#define MAX_TIMESTAMP_SKEW   160
 
#define MAX_TRUNK_MTU   1240
 Maximum transmission unit for the UDP packet in the trunk not to be fragmented. This is based on 1516 - ethernet - ip - udp - iax minus one g711 frame = 1240.
 
#define MAX_TRUNKDATA   640 * 200
 
#define MAX_USER_BUCKETS   MAX_PEER_BUCKETS
 
#define MEMORY_SIZE   100
 
#define MIN_JITTER_BUFFER   10
 
#define MIN_RETRY_TIME   100
 
#define MIN_REUSE_TIME   60 /* Don't reuse a call number within 60 seconds */
 
#define PEERS_FORMAT   "%-15.15s %-40.40s %s %-40.40s %-6s%s %s %-11s %-32.32s\n"
 
#define PEERS_FORMAT2   "%-15.15s %-40.40s %s %-40.40s %-9s %s %-11s %-32.32s\n"
 
#define PTR_TO_CALLNO(a)   ((unsigned short)(unsigned long)(a))
 
#define PTR_TO_CALLNO_ENTRY(a)   ((uint16_t)(unsigned long)(a))
 
#define SCHED_MULTITHREADED
 
#define schedule_action(func, data)   __schedule_action(func, data, __PRETTY_FUNCTION__)
 
#define TRUNK_CALL_START   (IAX_MAX_CALLS / 2)
 
#define TS_GAP_FOR_JB_RESYNC   5000
 

Typedefs

typedef uint16_t callno_entry
 

Enumerations

enum  {
  CACHE_FLAG_EXISTS = (1 << 0), CACHE_FLAG_NONEXISTENT = (1 << 1), CACHE_FLAG_CANEXIST = (1 << 2), CACHE_FLAG_PENDING = (1 << 3),
  CACHE_FLAG_TIMEOUT = (1 << 4), CACHE_FLAG_TRANSMITTED = (1 << 5), CACHE_FLAG_UNKNOWN = (1 << 6), CACHE_FLAG_MATCHMORE = (1 << 7)
}
 
enum  { NEW_PREVENT = 0, NEW_ALLOW = 1, NEW_FORCE = 2, NEW_ALLOW_CALLTOKEN_VALIDATED = 3 }
 
enum  callno_type { CALLNO_TYPE_NORMAL, CALLNO_TYPE_TRUNK }
 
enum  calltoken_peer_enum { CALLTOKEN_DEFAULT = 0, CALLTOKEN_YES = 1, CALLTOKEN_AUTO = 2, CALLTOKEN_NO = 3 }
 Call token validation settings. More...
 
enum  iax2_state { IAX_STATE_STARTED = (1 << 0), IAX_STATE_AUTHENTICATED = (1 << 1), IAX_STATE_TBD = (1 << 2) }
 
enum  iax2_thread_iostate { IAX_IOSTATE_IDLE, IAX_IOSTATE_READY, IAX_IOSTATE_PROCESSING, IAX_IOSTATE_SCHEDREADY }
 
enum  iax2_thread_type { IAX_THREAD_TYPE_POOL, IAX_THREAD_TYPE_DYNAMIC }
 
enum  iax_reg_state {
  REG_STATE_UNREGISTERED = 0, REG_STATE_REGSENT, REG_STATE_AUTHSENT, REG_STATE_REGISTERED,
  REG_STATE_REJECTED, REG_STATE_TIMEOUT, REG_STATE_NOAUTH
}
 
enum  iax_transfer_state {
  TRANSFER_NONE = 0, TRANSFER_BEGIN, TRANSFER_READY, TRANSFER_RELEASED,
  TRANSFER_PASSTHROUGH, TRANSFER_MBEGIN, TRANSFER_MREADY, TRANSFER_MRELEASED,
  TRANSFER_MPASSTHROUGH, TRANSFER_MEDIA, TRANSFER_MEDIAPASS
}
 

Functions

static void __attempt_transmit (const void *data)
 
static void __auth_reject (const void *nothing)
 
static void __auto_congest (const void *nothing)
 
static void __auto_hangup (const void *nothing)
 
static int __do_deliver (void *data)
 
static void __expire_registry (const void *data)
 
static int __find_callno (unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int return_locked, int check_dcallno)
 
static void __get_from_jb (const void *p)
 
static void __iax2_do_register_s (const void *data)
 
static void __iax2_poke_noanswer (const void *data)
 
static void __iax2_poke_peer_s (const void *data)
 
static int __iax2_show_peers (int fd, int *total, struct mansession *s, const int argc, const char *const argv[])
 
static void __reg_module (void)
 
static int __schedule_action (void(*func)(const void *data), const void *data, const char *funcname)
 
static int __send_command (struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno, int now, int transfer, int final)
 
static void __send_lagrq (const void *data)
 
static void __send_ping (const void *data)
 
static int __unload_module (void)
 
static void __unreg_module (void)
 
static void _iax2_show_peers_one (int fd, struct mansession *s, struct show_peers_context *cont, struct iax2_peer *peer)
 
static int acf_channel_read (struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
 
static int acf_iaxvar_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int acf_iaxvar_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static void acl_change_stasis_cb (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 
static void acl_change_stasis_subscribe (void)
 
static void acl_change_stasis_unsubscribe (void)
 
static int add_calltoken_ignore (const char *addr)
 
static void add_empty_calltoken_ie (struct chan_iax2_pvt *pvt, struct iax_ie_data *ied)
 
static int addr_range_cmp_cb (void *obj, void *arg, int flags)
 
static int addr_range_delme_cb (void *obj, void *arg, int flags)
 
static int addr_range_hash_cb (const void *obj, const int flags)
 
static int addr_range_match_address_cb (void *obj, void *arg, int flags)
 
static int apply_context (struct iax2_context *con, const char *context)
 
static int ast_cli_netstats (struct mansession *s, int fd, int limit_fmt)
 
static struct ast_channelast_iax2_new (int callno, int state, iax2_format capability, struct iax2_codec_pref *prefs, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, unsigned int cachable)
 Create new call, interface with the PBX core.
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int attempt_transmit (const void *data)
 
static int auth_fail (int callno, int failcode)
 
static char * auth_method_names (int authmethods, char *restrict buf)
 Get names of all auth methods. More...
 
static int auth_reject (const void *data)
 
static int authenticate (const char *challenge, const char *secret, const char *keyn, int authmethods, struct iax_ie_data *ied, struct ast_sockaddr *addr, struct chan_iax2_pvt *pvt)
 
static int authenticate_reply (struct chan_iax2_pvt *p, struct ast_sockaddr *addr, struct iax_ies *ies, const char *override, const char *okey)
 
static int authenticate_request (int call_num)
 
static int authenticate_verify (struct chan_iax2_pvt *p, struct iax_ies *ies)
 
static int auto_congest (const void *data)
 
static int auto_hangup (const void *data)
 
static void build_callno_limits (struct ast_variable *v)
 
static struct iax2_contextbuild_context (const char *context)
 
static void build_ecx_key (const unsigned char *digest, struct chan_iax2_pvt *pvt)
 
static void build_encryption_keys (const unsigned char *digest, struct chan_iax2_pvt *pvt)
 
static struct iax2_peerbuild_peer (const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
 Create peer structure based on configuration.
 
static void build_rand_pad (unsigned char *buf, ssize_t len)
 
static struct iax2_userbuild_user (const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
 Create in-memory user structure from configuration.
 
static int cache_get_callno_locked (const char *data)
 
static unsigned int calc_rxstamp (struct chan_iax2_pvt *p, unsigned int offset)
 
static unsigned int calc_timestamp (struct chan_iax2_pvt *p, unsigned int ts, struct ast_frame *f)
 
static unsigned int calc_txpeerstamp (struct iax2_trunk_peer *tpeer, int sampms, struct timeval *now)
 
static int calltoken_required (struct ast_sockaddr *addr, const char *name, int subclass)
 
static int check_access (int callno, struct ast_sockaddr *addr, struct iax_ies *ies)
 
static int check_provisioning (struct ast_sockaddr *addr, int sockfd, char *si, unsigned int ver)
 
static int check_srcaddr (struct ast_sockaddr *addr)
 Check if address can be used as packet source. More...
 
static void cleanup_thread_list (void *head)
 
static struct ast_formatcodec_choose_from_prefs (struct iax2_codec_pref *pref, struct ast_format_cap *cap)
 
static int complete_dpreply (struct chan_iax2_pvt *pvt, struct iax_ies *ies)
 
static char * complete_iax2_peers (const char *line, const char *word, int pos, int state, uint64_t flags)
 
static char * complete_iax2_unregister (const char *line, const char *word, int pos, int state)
 
static int complete_transfer (int callno, struct iax_ies *ies)
 
static unsigned char compress_subclass (iax2_format subclass)
 
static void construct_rr (struct chan_iax2_pvt *pvt, struct iax_ie_data *iep)
 
static int create_addr (const char *peername, struct ast_channel *c, struct ast_sockaddr *addr, struct create_addr_info *cai)
 
static int create_callno_pools (void)
 
static int decode_frame (ast_aes_decrypt_key *dcx, struct ast_iax2_full_hdr *fh, struct ast_frame *f, int *datalen)
 
static int decrypt_frame (int callno, struct ast_iax2_full_hdr *fh, struct ast_frame *f, int *datalen)
 
static void defer_full_frame (struct iax2_thread *from_here, struct iax2_thread *to_here)
 Queue the last read full frame for processing by a certain thread. More...
 
static void delete_users (void)
 
static void dp_lookup (int callno, const char *context, const char *callednum, const char *callerid, int skiplock)
 
static void * dp_lookup_thread (void *data)
 
static void encmethods_to_str (int e, struct ast_str **buf)
 
static int encrypt_frame (ast_aes_encrypt_key *ecx, struct ast_iax2_full_hdr *fh, unsigned char *poo, int *datalen)
 
static int expire_registry (const void *data)
 
static struct iax2_dpcachefind_cache (struct ast_channel *chan, const char *data, const char *context, const char *exten, int priority)
 
static int find_callno (unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int full_frame)
 
static int find_callno_locked (unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int full_frame)
 
static struct iax2_threadfind_idle_thread (void)
 
static struct iax2_peerfind_peer (const char *name, int realtime)
 
static struct iax2_trunk_peerfind_tpeer (struct ast_sockaddr *addr, int fd)
 
static struct iax2_userfind_user (const char *name)
 
static int firmware_show_callback (struct ast_iax2_firmware_header *header, void *user_data)
 
static unsigned int fix_peerts (struct timeval *rxtrunktime, int callno, unsigned int ts)
 
static void free_context (struct iax2_context *con)
 
static void free_signaling_queue_entry (struct signaling_queue_entry *s)
 
static int function_iaxpeer (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int get_auth_methods (const char *value)
 
static int get_encrypt_methods (const char *s)
 
static int get_from_jb (const void *p)
 
static int get_unused_callno (enum callno_type type, int validated, callno_entry *entry)
 
static int handle_call_token (struct ast_iax2_full_hdr *fh, struct iax_ies *ies, struct ast_sockaddr *addr, int fd)
 
static char * handle_cli_iax2_provision (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_prune_realtime (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_set_debug (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_set_debug_jb (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_set_debug_trunk (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_set_mtu (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 Set trunk MTU from CLI.
 
static char * handle_cli_iax2_show_cache (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_callno_limits (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_channels (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_firmware (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_netstats (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_peer (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 Show one peer in detail.
 
static char * handle_cli_iax2_show_peers (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_registry (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_stats (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_threads (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_show_users (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_test_losspct (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_iax2_unregister (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static void handle_deferred_full_frames (struct iax2_thread *thread)
 Handle any deferred full frames for this thread.
 
static int handle_error (void)
 
static int iax2_ack_registry (struct iax_ies *ies, struct ast_sockaddr *addr, int callno)
 Acknowledgment received for OUR registration.
 
static int attribute_pure iax2_allow_new (int frametype, int subclass, int inbound)
 
static int iax2_answer (struct ast_channel *c)
 
static int iax2_append_register (const char *hostname, const char *username, const char *secret, const char *porta)
 
static int iax2_call (struct ast_channel *c, const char *dest, int timeout)
 
static int iax2_canmatch (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 part of the IAX2 dial plan switch interface
 
static iax2_format iax2_codec_choose (struct iax2_codec_pref *pref, iax2_format formats)
 
static unsigned int iax2_datetime (const char *tz)
 
static int iax2_delete_from_sched (const void *data)
 
static void iax2_destroy (int callno)
 
static void iax2_destroy_helper (struct chan_iax2_pvt *pvt)
 
static int iax2_devicestate (const char *data)
 Part of the device state notification system —.
 
static int iax2_digit_begin (struct ast_channel *c, char digit)
 
static int iax2_digit_end (struct ast_channel *c, char digit, unsigned int duration)
 
static int iax2_do_register (struct iax2_registry *reg)
 
static int iax2_do_register_s (const void *data)
 
static void iax2_dprequest (struct iax2_dpcache *dp, int callno)
 
static void * iax2_dup_variable_datastore (void *)
 
static int iax2_exec (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 Execute IAX2 dialplan switch.
 
static int iax2_exists (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 Part of the IAX2 switch interface.
 
static int iax2_fixup (struct ast_channel *oldchannel, struct ast_channel *newchan)
 
static void iax2_frame_free (struct iax_frame *fr)
 
static void iax2_free_variable_datastore (void *)
 
const char * iax2_getformatname (iax2_format format)
 iax2 wrapper function for ast_getformatname
 
static const char * iax2_getformatname_multiple (iax2_format format, struct ast_str **codec_buf)
 
static int iax2_getpeername (struct ast_sockaddr addr, char *host, int len)
 
static int iax2_getpeertrunk (struct ast_sockaddr addr)
 
static int iax2_hangup (struct ast_channel *c)
 
static int iax2_indicate (struct ast_channel *c, int condition, const void *data, size_t datalen)
 
static int iax2_is_control_frame_allowed (int subtype)
 
static int iax2_key_rotate (const void *vpvt)
 
static int iax2_lock_callno_unless_destroyed (int callno)
 Acquire the iaxsl[callno] if call exists and not having ongoing hangup. More...
 
static void iax2_lock_owner (int callno)
 
static int iax2_matchmore (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
 Part of the IAX2 Switch interface.
 
static int iax2_parse_allow_disallow (struct iax2_codec_pref *pref, iax2_format *formats, const char *list, int allowing)
 
static int iax2_poke_noanswer (const void *data)
 
static int iax2_poke_peer (struct iax2_peer *peer, int heldcall)
 
static int iax2_poke_peer_cb (void *obj, void *arg, int flags)
 
static int iax2_poke_peer_s (const void *data)
 
static int iax2_predestroy (int callno)
 
static void * iax2_process_thread (void *data)
 
static void iax2_process_thread_cleanup (void *data)
 
static int iax2_prov_app (struct ast_channel *chan, const char *data)
 
static int iax2_provision (struct ast_sockaddr *end, int sockfd, const char *dest, const char *template, int force)
 
static void iax2_publish_registry (const char *username, const char *domain, const char *status, const char *cause)
 
static int iax2_queryoption (struct ast_channel *c, int option, void *data, int *datalen)
 
static int iax2_queue_frame (int callno, struct ast_frame *f)
 Queue a frame to a call's owning asterisk channel. More...
 
static int iax2_queue_hangup (int callno)
 Queue a hangup frame on the ast_channel owner. More...
 
static int iax2_queue_hold (int callno, const char *musicclass)
 Queue a hold frame on the ast_channel owner. More...
 
static int iax2_queue_unhold (int callno)
 Queue an unhold frame on the ast_channel owner. More...
 
static struct ast_frameiax2_read (struct ast_channel *c)
 
static int iax2_register (const char *value, int lineno)
 
static struct ast_channeliax2_request (const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
 
static int iax2_sched_add (struct ast_sched_context *sched, int when, ast_sched_cb callback, const void *data)
 
static int iax2_sched_replace (int id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data)
 
static int iax2_send (struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
 
static int iax2_sendhtml (struct ast_channel *c, int subclass, const char *data, int datalen)
 
static int iax2_sendimage (struct ast_channel *c, struct ast_frame *img)
 
static int iax2_sendtext (struct ast_channel *c, const char *text)
 
static int iax2_setoption (struct ast_channel *c, int option, void *data, int datalen)
 
static int iax2_transfer (struct ast_channel *c, const char *dest)
 
static int iax2_transmit (struct iax_frame *fr)
 
static int iax2_trunk_expired (struct iax2_trunk_peer *tpeer, struct timeval *now)
 
static int iax2_trunk_queue (struct chan_iax2_pvt *pvt, struct iax_frame *fr)
 
static int iax2_vnak (int callno)
 
static int iax2_write (struct ast_channel *c, struct ast_frame *f)
 
static void iax_debug_output (const char *data)
 
static void iax_error_output (const char *data)
 
static void iax_outputframe (struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct ast_sockaddr *addr, int datalen)
 
static ast_callid iax_pvt_callid_get (int callno)
 
static void iax_pvt_callid_new (int callno)
 
static void iax_pvt_callid_set (int callno, ast_callid callid)
 
static struct iax_frameiaxfrdup2 (struct iax_frame *fr)
 
static void insert_idle_thread (struct iax2_thread *thread)
 
static int invalid_key (ast_aes_decrypt_key *ecx)
 
static void jb_debug_output (const char *fmt,...)
 
static void jb_error_output (const char *fmt,...)
 
static void jb_warning_output (const char *fmt,...)
 
static int load_module (void)
 Load the module. More...
 
static int load_objects (void)
 
static void log_jitterstats (unsigned short callno)
 
static int make_trunk (unsigned short callno, int locked)
 
static int manager_iax2_show_netstats (struct mansession *s, const struct message *m)
 
static int manager_iax2_show_peer_list (struct mansession *s, const struct message *m)
 callback to display iax peers in manager format
 
static int manager_iax2_show_peers (struct mansession *s, const struct message *m)
 callback to display iax peers in manager
 
static int manager_iax2_show_registry (struct mansession *s, const struct message *m)
 
static int match (struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
 
static void memcpy_decrypt (unsigned char *dst, const unsigned char *src, int len, ast_aes_decrypt_key *dcx)
 
static void memcpy_encrypt (unsigned char *dst, const unsigned char *src, int len, ast_aes_encrypt_key *ecx)
 
static void merge_encryption (struct chan_iax2_pvt *p, unsigned int enc)
 
static int network_change_sched_cb (const void *data)
 
static void network_change_stasis_cb (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 
static void network_change_stasis_subscribe (void)
 
static void network_change_stasis_unsubscribe (void)
 
static void * network_thread (void *ignore)
 
static struct chan_iax2_pvtnew_iax (struct ast_sockaddr *addr, const char *host)
 
static void parse_dial_string (char *data, struct parsed_dial_string *pds)
 Parses an IAX dial string into its component parts. More...
 
static int peer_cmp_cb (void *obj, void *arg, int flags)
 
static int peer_delme_cb (void *obj, void *arg, int flags)
 
static void peer_destructor (void *obj)
 
static int peer_hash_cb (const void *obj, const int flags)
 
static struct iax2_peerpeer_ref (struct iax2_peer *peer)
 
static int peer_set_sock_cb (void *obj, void *arg, int flags)
 
static int peer_set_srcaddr (struct iax2_peer *peer, const char *srcaddr)
 Parse the "sourceaddress" value, lookup in netsock list and set peer's sockfd. Defaults to defaultsockfd if not found.
 
static int peer_status (struct iax2_peer *peer, char *status, int statuslen)
 peer_status: Report Peer status in character string
 
static struct iax2_peerpeer_unref (struct iax2_peer *peer)
 
static int peercnt_add (struct ast_sockaddr *addr)
 
static int peercnt_cmp_cb (void *obj, void *arg, int flags)
 
static int peercnt_hash_cb (const void *obj, const int flags)
 
static void peercnt_modify (unsigned char reg, uint16_t limit, struct ast_sockaddr *sockaddr)
 
static void peercnt_remove (struct peercnt *peercnt)
 
static int peercnt_remove_by_addr (struct ast_sockaddr *addr)
 
static int peercnt_remove_cb (const void *obj)
 
static void poke_all_peers (void)
 
static int prune_addr_range_cb (void *obj, void *arg, int flags)
 
static void prune_peers (void)
 
static void prune_users (void)
 
static int pvt_cmp_cb (void *obj, void *arg, int flags)
 
static void pvt_destructor (void *obj)
 
static int pvt_hash_cb (const void *obj, const int flags)
 
static int queue_signalling (struct chan_iax2_pvt *pvt, struct ast_frame *f)
 All frames other than that of type AST_FRAME_IAX must be held until we have received a destination call number.
 
static int raw_hangup (struct ast_sockaddr *addr, unsigned short src, unsigned short dst, int sockfd)
 
static struct iax2_peerrealtime_peer (const char *peername, struct ast_sockaddr *addr)
 
static void realtime_update_peer (const char *peername, struct ast_sockaddr *sockaddr, time_t regtime)
 
static struct iax2_userrealtime_user (const char *username, struct ast_sockaddr *addr)
 
static void reg_source_db (struct iax2_peer *p)
 
static void register_peer_exten (struct iax2_peer *peer, int onoff)
 
static int register_verify (int callno, struct ast_sockaddr *addr, struct iax_ies *ies)
 Verify inbound registration.
 
static int registry_authrequest (int callno)
 
static int registry_rerequest (struct iax_ies *ies, int callno, struct ast_sockaddr *addr)
 
static char * regstate2str (int regstate)
 
static int reload (void)
 
static int reload_config (int forced_reload)
 
static void remove_by_peercallno (struct chan_iax2_pvt *pvt)
 
static void remove_by_transfercallno (struct chan_iax2_pvt *pvt)
 
static int replace_callno (const void *obj)
 
static void requirecalltoken_mark_auto (const char *name, int subclass)
 
static void resend_with_token (int callno, struct iax_frame *f, const char *newtoken)
 
static void save_osptoken (struct iax_frame *fr, struct iax_ies *ies)
 
static void save_rr (struct iax_frame *fr, struct iax_ies *ies)
 
static void sched_delay_remove (struct ast_sockaddr *addr, callno_entry entry)
 
static int schedule_delivery (struct iax_frame *fr, int updatehistory, int fromtrunk, unsigned int *tsout)
 
static int scheduled_destroy (const void *vid)
 
static int send_apathetic_reply (unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int command, int ts, unsigned char seqno, int sockfd, struct iax_ie_data *ied)
 
static int send_command (struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int)
 
static int send_command_final (struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int)
 
static int send_command_immediate (struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int)
 
static int send_command_locked (unsigned short callno, char, int, unsigned int, const unsigned char *, int, int)
 
static int send_command_transfer (struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int)
 
static int send_lagrq (const void *data)
 
static int send_packet (struct iax_frame *f)
 
static int send_ping (const void *data)
 
static void send_signaling (struct chan_iax2_pvt *pvt)
 This function must be called once we are sure the other side has given us a call number. All signaling is held here until that point.
 
static int send_trunk (struct iax2_trunk_peer *tpeer, struct timeval *now)
 
static int set_config (const char *config_file, int reload, int forced)
 Load configuration.
 
static void set_config_destroy (void)
 
static void set_hangup_source_and_cause (int callno, unsigned char causecode)
 
static void set_peercnt_limit (struct peercnt *peercnt)
 
static int set_peercnt_limit_all_cb (void *obj, void *arg, int flags)
 
static void signal_condition (ast_mutex_t *lock, ast_cond_t *cond)
 
static int socket_process (struct iax2_thread *thread)
 
static int socket_process_helper (struct iax2_thread *thread)
 
static int socket_process_meta (int packet_len, struct ast_iax2_meta_hdr *meta, struct ast_sockaddr *addr, int sockfd, struct iax_frame *fr)
 
static int socket_read (int *id, int fd, short events, void *cbdata)
 
static void spawn_dp_lookup (int callno, const char *context, const char *callednum, const char *callerid)
 
static int start_network_thread (void)
 
static void stop_stuff (int callno)
 
static void store_by_peercallno (struct chan_iax2_pvt *pvt)
 
static void store_by_transfercallno (struct chan_iax2_pvt *pvt)
 
static int timing_read (int *id, int fd, short events, void *cbdata)
 
static int transfercallno_pvt_cmp_cb (void *obj, void *arg, int flags)
 
static int transfercallno_pvt_hash_cb (const void *obj, const int flags)
 
static int transmit_frame (void *data)
 
static int transmit_trunk (struct iax_frame *f, struct ast_sockaddr *addr, int sockfd)
 
static int try_transfer (struct chan_iax2_pvt *pvt, struct iax_ies *ies)
 
static iax2_format uncompress_subclass (unsigned char csub)
 
static void unlink_peer (struct iax2_peer *peer)
 
static int unload_module (void)
 
static void unwrap_timestamp (struct iax_frame *fr)
 
static void update_jbsched (struct chan_iax2_pvt *pvt)
 
static int update_packet (struct iax_frame *f)
 
static int update_registry (struct ast_sockaddr *addr, int callno, char *devtype, int fd, unsigned short refresh)
 
static int user_cmp_cb (void *obj, void *arg, int flags)
 
static int user_delme_cb (void *obj, void *arg, int flags)
 
static void user_destructor (void *obj)
 
static int user_hash_cb (const void *obj, const int flags)
 
static struct iax2_useruser_unref (struct iax2_user *user)
 
static void vnak_retransmit (int callno, int last)
 
static int wait_for_peercallno (struct chan_iax2_pvt *pvt)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Inter Asterisk eXchange (Ver 2)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CHANNEL_DRIVER, .requires = "dnsmgr", .optional_modules = "res_crypto", }
 
static char accountcode [AST_MAX_ACCOUNT_CODE]
 
static struct stasis_subscriptionacl_change_sub
 
static struct active_list active_list = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 
static int adsi = 0
 
static int amaflags = 0
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const char * auth_method_labels []
 Name of effective auth method.
 
static int authdebug = 0
 
static int autokill = 0
 
static struct ao2_containercallno_limits
 
static struct call_number_pool callno_pool
 
static ast_mutex_t callno_pool_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 
static struct call_number_pool callno_pool_trunk
 
static struct ao2_containercalltoken_ignores
 
static struct ast_cli_entry cli_iax2 []
 
static struct ast_sockaddr debugaddr
 
static uint16_t DEFAULT_MAXCALLNO_LIMIT = 2048
 
static uint16_t DEFAULT_MAXCALLNO_LIMIT_NONVAL = 8192
 
static char default_parkinglot [AST_MAX_CONTEXT]
 
static int defaultsockfd = -1
 
static int delayreject = 0
 
static struct dpcache dpcache = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 
static struct dynamic_list dynamic_list = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 
struct {
   struct iax_frame *   first
 
   struct iax_frame *   last
 
frame_queue [IAX_MAX_CALLS]
 a list of frames that may need to be retransmitted More...
 
static int global_max_trunk_mtu
 
static uint16_t global_maxcallno
 
static uint16_t global_maxcallno_nonval
 
static int global_rtautoclear = 120
 
static struct ast_flags64 globalflags = { 0 }
 
static int iax2_authmethods = 0
 
static iax2_format iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH
 
static int iax2_encryption = 0
 
static int(* iax2_regfunk )(const char *username, int onoff) = NULL
 
static struct ast_switch iax2_switch
 
static struct ast_channel_tech iax2_tech
 
static const struct ast_datastore_info iax2_variable_datastore_info
 
static struct ao2_containeriax_peercallno_pvts
 Another container of iax2_pvt structures. More...
 
static struct ao2_containeriax_transfercallno_pvts
 Another container of iax2_pvt structures. More...
 
static int iaxactivethreadcount = 0
 
static int iaxcompat = 0
 
static int iaxdebug = 0
 
static int iaxdefaultdpcache =10 * 60
 
static int iaxdefaulttimeout = 5
 
static int iaxdynamicthreadcount = 0
 
static int iaxdynamicthreadnum = 0
 
static int iaxmaxthreadcount = DEFAULT_MAX_THREAD_COUNT
 
static struct ast_custom_function iaxpeer_function
 
static struct chan_iax2_pvtiaxs [IAX_MAX_CALLS]
 an array of iax2 pvt structures More...
 
static ast_mutex_t iaxsl [ARRAY_LEN(iaxs)]
 chan_iax2_pvt structure locks More...
 
static int iaxthreadcount = DEFAULT_THREAD_COUNT
 
static int iaxtrunkdebug = 0
 
static struct ast_custom_function iaxvar_function
 
static struct idle_list idle_list = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 
static struct io_contextio
 
static int jittertargetextra = 40
 
static int lagrq_time = 10
 
static char language [MAX_LANGUAGE] = ""
 
static int last_authmethod = 0
 
static time_t max_calltoken_delay = 10
 
static int max_reg_expire
 
static int max_retries = 4
 
static int maxauthreq = 3
 
static int maxjitterbuffer =1000
 
static int maxjitterinterps =10
 
static int min_reg_expire
 
static char mohinterpret [MAX_MUSICCLASS]
 
static char mohsuggest [MAX_MUSICCLASS]
 
static struct ast_netsock_listnetsock
 
static pthread_t netthreadid = AST_PTHREADT_NULL
 
static int network_change_sched_id = -1
 
static struct stasis_subscriptionnetwork_change_sub
 
static struct ast_netsock_listoutsock
 
static char * papp = "IAX2Provision"
 
static struct ao2_containerpeercnts
 
static struct ao2_containerpeers
 
static int ping_time = 21
 
static struct iax2_codec_pref prefs_global
 
struct {
   unsigned int   cos
 
   unsigned int   tos
 
qos = { 0, 0 }
 
static int randomcalltokendata
 
static char regcontext [AST_MAX_CONTEXT] = ""
 
static struct registrations registrations = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 
static int resyncthreshold =1000
 
static struct ast_sched_contextsched
 
static int srvlookup = 0
 
static const char tdesc [] = "Inter Asterisk eXchange Driver (Ver 2)"
 
static int test_losspct = 0
 
static struct ast_timertimer
 
static uint16_t total_nonval_callno_used = 0
 
static struct tpeers tpeers = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 
static struct ast_taskprocessortransmit_processor
 
static int trunk_maxmtu
 
static int trunk_nmaxmtu
 
static int trunk_timed
 
static int trunk_untimed
 
static int trunkfreq = 20
 
static int trunkmaxsize = MAX_TRUNKDATA
 
static struct ao2_containerusers
 

Detailed Description

Implementation of Inter-Asterisk eXchange Version 2 as specified in RFC 5456.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
Todo:
Implement musicclass settings for IAX2 devices

Definition in file chan_iax2.c.

Macro Definition Documentation

#define DEFAULT_TRUNKDATA   640 * 10

40ms, uncompressed linear * 10 channels

Definition at line 710 of file chan_iax2.c.

#define IAX_ALLOWFWDOWNLOAD   (uint64_t)(1LLU << 26)

Allow the FWDOWNL command?

Definition at line 523 of file chan_iax2.c.

Referenced by set_config().

#define IAX_ALREADYGONE   (uint64_t)(1LLU << 9)

Already disconnected

Definition at line 507 of file chan_iax2.c.

Referenced by __do_deliver(), and iax2_predestroy().

#define IAX_CAPABILITY_LOWBANDWIDTH
Value:
(IAX_CAPABILITY_MEDBANDWIDTH & \
~AST_FORMAT_G726 & \
~AST_FORMAT_G726_AAL2 & \
~AST_FORMAT_ADPCM)

Definition at line 385 of file chan_iax2.c.

#define IAX_CAPABILITY_LOWFREE
Value:
(IAX_CAPABILITY_LOWBANDWIDTH & \
~AST_FORMAT_G723)

Definition at line 390 of file chan_iax2.c.

#define IAX_CODEC_NOCAP   (uint64_t)(1LLU << 16)

only consider requested format and ignore capabilities

Definition at line 514 of file chan_iax2.c.

Referenced by build_user(), and set_config().

#define IAX_CODEC_NOPREFS   (uint64_t)(1LLU << 15)

Force old behaviour by turning off prefs

Definition at line 513 of file chan_iax2.c.

Referenced by build_user(), and set_config().

#define IAX_CODEC_USER_FIRST   (uint64_t)(1LLU << 14)

are we willing to let the other guy choose the codec?

Definition at line 512 of file chan_iax2.c.

Referenced by build_user(), and set_config().

#define IAX_DELAYPBXSTART   (uint64_t)(1LLU << 25)

Don't start a PBX on the channel until the peer sends us a response, so that we've achieved a three-way handshake with them before sending voice or anything else

Definition at line 522 of file chan_iax2.c.

#define IAX_DELME   (uint64_t)(1LLU << 1)

Needs to be deleted

Definition at line 499 of file chan_iax2.c.

Referenced by build_peer(), and build_user().

#define IAX_DYNAMIC   (uint64_t)(1LLU << 6)

dynamic peer

Definition at line 504 of file chan_iax2.c.

Referenced by build_peer(), handle_cli_iax2_show_peer(), realtime_peer(), register_verify(), and set_config().

#define IAX_ENCRYPTED   (uint64_t)(1LLU << 12)

Whether we should assume encrypted tx/rx

Definition at line 510 of file chan_iax2.c.

Referenced by authenticate_reply(), and authenticate_request().

#define IAX_FORCE_ENCRYPT   (uint64_t)(1LLU << 30)

Forces call encryption, if encryption not possible hangup

Definition at line 527 of file chan_iax2.c.

Referenced by __find_callno(), authenticate_reply(), build_peer(), build_user(), and set_config().

#define IAX_HASCALLERID   (uint64_t)(1LLU << 0)

CallerID has been specified

Definition at line 498 of file chan_iax2.c.

Referenced by build_peer(), build_user(), and update_registry().

#define IAX_IMMEDIATE   (uint64_t)(1LLU << 27)

Allow immediate off-hook to extension s

Definition at line 524 of file chan_iax2.c.

Referenced by build_user().

#define IAX_KEYPOPULATED   (uint64_t)(1LLU << 13)

Whether we have a key populated

Definition at line 511 of file chan_iax2.c.

Referenced by authenticate_reply().

#define IAX_MAXAUTHREQ   (uint64_t)(1LLU << 24)

Maximum outstanding AUTHREQ restriction is in place

Definition at line 521 of file chan_iax2.c.

Referenced by authenticate_request(), and iax2_destroy_helper().

#define IAX_NOTRANSFER   (uint64_t)(1LLU << 4)

Don't native bridge

Definition at line 502 of file chan_iax2.c.

Referenced by __find_callno(), build_peer(), build_user(), and set_config().

#define IAX_PROVISION   (uint64_t)(1LLU << 10)

This is a provisioning request

Definition at line 508 of file chan_iax2.c.

#define IAX_QUELCH   (uint64_t)(1LLU << 11)

Whether or not we quelch audio

Definition at line 509 of file chan_iax2.c.

#define IAX_RECVCONNECTEDLINE   (uint64_t)(1LLU << 29)

Allow receiving of connected line updates

Definition at line 526 of file chan_iax2.c.

Referenced by __find_callno(), build_peer(), build_user(), and set_config().

#define IAX_RTAUTOCLEAR   (uint64_t)(1LLU << 19)

erase me on expire

Definition at line 517 of file chan_iax2.c.

Referenced by realtime_peer(), and set_config().

#define IAX_RTCACHEFRIENDS   (uint64_t)(1LLU << 17)

let realtime stay till your reload

Definition at line 515 of file chan_iax2.c.

Referenced by realtime_peer(), realtime_user(), set_config(), and update_registry().

#define IAX_RTIGNOREREGEXPIRE   (uint64_t)(1LLU << 21)

When using realtime, ignore registration expiration

Definition at line 518 of file chan_iax2.c.

Referenced by realtime_peer(), and set_config().

#define IAX_RTSAVE_SYSNAME   (uint64_t)(1LLU << 8)

Save Systname on Realtime Updates

Definition at line 506 of file chan_iax2.c.

Referenced by set_config().

#define IAX_RTUPDATE   (uint64_t)(1LLU << 18)

Send a realtime update

Definition at line 516 of file chan_iax2.c.

Referenced by set_config(), and update_registry().

#define IAX_SENDANI   (uint64_t)(1LLU << 7)

Send ANI along with CallerID

Definition at line 505 of file chan_iax2.c.

Referenced by build_peer().

#define IAX_SENDCONNECTEDLINE   (uint64_t)(1LLU << 28)

Allow sending of connected line updates

Definition at line 525 of file chan_iax2.c.

Referenced by __find_callno(), build_peer(), build_user(), and set_config().

#define IAX_SHRINKCALLERID   (uint64_t)(1LLU << 31)

Turn on and off caller id shrinking

Definition at line 528 of file chan_iax2.c.

Referenced by set_config().

#define IAX_TEMPONLY   (uint64_t)(1LLU << 2)

Temporary (realtime)

Definition at line 500 of file chan_iax2.c.

Referenced by realtime_peer(), realtime_user(), and update_registry().

#define IAX_TRANSFERMEDIA   (uint64_t)(1LLU << 23)

When doing IAX2 transfers, transfer media only

Definition at line 520 of file chan_iax2.c.

Referenced by __find_callno(), build_peer(), build_user(), and set_config().

#define IAX_TRUNK   (uint64_t)(1LLU << 3)

Treat as a trunk

Definition at line 501 of file chan_iax2.c.

Referenced by build_peer(), build_user(), and handle_cli_iax2_show_peer().

#define IAX_TRUNKTIMESTAMPS   (uint64_t)(1LLU << 22)

Send trunk timestamps

Definition at line 519 of file chan_iax2.c.

Referenced by set_config().

#define IAX_USEJITTERBUF   (uint64_t)(1LLU << 5)

Use jitter buffer

Definition at line 503 of file chan_iax2.c.

Referenced by __find_callno(), build_peer(), build_user(), schedule_delivery(), and set_config().

#define MAX_PEER_BUCKETS   563

This module will get much higher performance when doing a lot of user and peer lookups if the number of buckets is increased from 1. However, to maintain old behavior for Asterisk 1.4, these are set to 1 by default. When using multiple buckets, search order through these containers is considered random, so you will not be able to depend on the order the entires are specified in iax.conf for matching order.

Definition at line 998 of file chan_iax2.c.

#define MAX_TIMESTAMP_SKEW   160

maximum difference between actual and predicted ts for sending

Definition at line 712 of file chan_iax2.c.

Referenced by rtp_raw_write().

#define MAX_TRUNKDATA   640 * 200

40ms, uncompressed linear * 200 channels

Definition at line 340 of file chan_iax2.c.

Referenced by set_config().

#define SCHED_MULTITHREADED
Todo:
XXX The IAX2 channel driver needs its native bridge code converted to the new bridge technology scheme.
Note
The chan_dahdi native bridge code can be used as an example. It also appears that chan_iax2 also has a native transfer check like chan_dahdi to eliminate tromboned calls.
The existing native bridge code is marked with the IAX2_NATIVE_BRIDGING conditional.

Definition at line 281 of file chan_iax2.c.

Enumeration Type Documentation

anonymous enum
Enumerator
CACHE_FLAG_EXISTS 

Extension exists

CACHE_FLAG_NONEXISTENT 

Extension is nonexistent

CACHE_FLAG_CANEXIST 

Extension can exist

CACHE_FLAG_PENDING 

Waiting to hear back response

CACHE_FLAG_TIMEOUT 

Timed out

CACHE_FLAG_TRANSMITTED 

Request transmitted

CACHE_FLAG_UNKNOWN 

Timeout

CACHE_FLAG_MATCHMORE 

Matchmore

Definition at line 1049 of file chan_iax2.c.

1049  {
1050  /*! Extension exists */
1051  CACHE_FLAG_EXISTS = (1 << 0),
1052  /*! Extension is nonexistent */
1053  CACHE_FLAG_NONEXISTENT = (1 << 1),
1054  /*! Extension can exist */
1055  CACHE_FLAG_CANEXIST = (1 << 2),
1056  /*! Waiting to hear back response */
1057  CACHE_FLAG_PENDING = (1 << 3),
1058  /*! Timed out */
1059  CACHE_FLAG_TIMEOUT = (1 << 4),
1060  /*! Request transmitted */
1061  CACHE_FLAG_TRANSMITTED = (1 << 5),
1062  /*! Timeout */
1063  CACHE_FLAG_UNKNOWN = (1 << 6),
1064  /*! Matchmore */
1065  CACHE_FLAG_MATCHMORE = (1 << 7),
1066 };

Call token validation settings.

Enumerator
CALLTOKEN_DEFAULT 

Default calltoken required unless the ip is in the ignorelist.

CALLTOKEN_YES 

Require call token validation.

CALLTOKEN_AUTO 

Require call token validation after a successful registration using call token validation occurs.

CALLTOKEN_NO 

Do not require call token validation.

Definition at line 536 of file chan_iax2.c.

536  {
537  /*! \brief Default calltoken required unless the ip is in the ignorelist */
538  CALLTOKEN_DEFAULT = 0,
539  /*! \brief Require call token validation. */
540  CALLTOKEN_YES = 1,
541  /*! \brief Require call token validation after a successful registration
542  * using call token validation occurs. */
543  CALLTOKEN_AUTO = 2,
544  /*! \brief Do not require call token validation. */
545  CALLTOKEN_NO = 3,
546 };
Require call token validation after a successful registration using call token validation occurs...
Definition: chan_iax2.c:543
Require call token validation.
Definition: chan_iax2.c:540
Default calltoken required unless the ip is in the ignorelist.
Definition: chan_iax2.c:538
Do not require call token validation.
Definition: chan_iax2.c:545

Function Documentation

static int __do_deliver ( void *  data)
static
Note
This function assumes that iaxsl[callno] is locked when called.
IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno] was valid before calling it, it may no longer be valid after calling it. This function calls iax2_queue_frame(), which may unlock and lock the mutex associated with this callno, meaning that another thread may grab it and destroy the call.

Definition at line 3387 of file chan_iax2.c.

References iax_frame::af, AST_FRFLAG_HAS_TIMING_INFO, iax_frame::callno, iax_frame::data, iax2_queue_frame(), IAX_ALREADYGONE, and iax_frame::retrans.

Referenced by schedule_delivery().

3388 {
3389  /* Just deliver the packet by using queueing. This is called by
3390  the IAX thread with the iaxsl lock held. */
3391  struct iax_frame *fr = data;
3392  fr->retrans = -1;
3393  ast_clear_flag(&fr->af, AST_FRFLAG_HAS_TIMING_INFO);
3394  if (iaxs[fr->callno] && !ast_test_flag64(iaxs[fr->callno], IAX_ALREADYGONE))
3395  iax2_queue_frame(fr->callno, &fr->af);
3396  /* Free our iax frame */
3397  iax2_frame_free(fr);
3398  /* And don't run again */
3399  return 0;
3400 }
struct ast_frame af
Definition: parser.h:142
static int iax2_queue_frame(int callno, struct ast_frame *f)
Queue a frame to a call's owning asterisk channel.
Definition: chan_iax2.c:3300
void * data
Definition: parser.h:104
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
unsigned short callno
Definition: parser.h:100
int retrans
Definition: parser.h:130
#define IAX_ALREADYGONE
Definition: chan_iax2.c:507
static int __find_callno ( unsigned short  callno,
unsigned short  dcallno,
struct ast_sockaddr addr,
int  new,
int  sockfd,
int  return_locked,
int  check_dcallno 
)
static
Note
Calling this function while holding another pvt lock can cause a deadlock.

Definition at line 3155 of file chan_iax2.c.

References chan_iax2_pvt::addr, ao2_ref, ast_debug, ast_sockaddr_copy(), ast_sockaddr_stringify(), ast_string_field_set, chan_iax2_pvt::callno, chan_iax2_pvt::callno_entry, chan_iax2_pvt::expiry, IAX_FORCE_ENCRYPT, IAX_NOTRANSFER, IAX_RECVCONNECTEDLINE, IAX_SENDCONNECTEDLINE, IAX_TRANSFERMEDIA, IAX_USEJITTERBUF, chan_iax2_pvt::lagid, OBJ_POINTER, chan_iax2_pvt::parkinglot, chan_iax2_pvt::peercallno, chan_iax2_pvt::pingid, chan_iax2_pvt::pingtime, chan_iax2_pvt::sockfd, and chan_iax2_pvt::transfer.

3156 {
3157  int res = 0;
3158  int x;
3159  /* this call is calltoken validated as long as it is either NEW_FORCE
3160  * or NEW_ALLOW_CALLTOKEN_VALIDATED */
3161  int validated = (new > NEW_ALLOW) ? 1 : 0;
3162  char host[80];
3163 
3164  if (new <= NEW_ALLOW) {
3165  if (callno) {
3166  struct chan_iax2_pvt *pvt;
3167  struct chan_iax2_pvt tmp_pvt = {
3168  .callno = dcallno,
3169  .peercallno = callno,
3170  .transfercallno = callno,
3171  /* hack!! */
3172  .frames_received = check_dcallno,
3173  };
3174 
3175  ast_sockaddr_copy(&tmp_pvt.addr, addr);
3176  /* this works for finding normal call numbers not involving transfering */
3177  if ((pvt = ao2_find(iax_peercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
3178  if (return_locked) {
3179  ast_mutex_lock(&iaxsl[pvt->callno]);
3180  }
3181  res = pvt->callno;
3182  ao2_ref(pvt, -1);
3183  pvt = NULL;
3184  return res;
3185  }
3186  /* this searches for transfer call numbers that might not get caught otherwise */
3187  memset(&tmp_pvt.addr, 0, sizeof(tmp_pvt.addr));
3188  ast_sockaddr_copy(&tmp_pvt.transfer, addr);
3189  if ((pvt = ao2_find(iax_transfercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
3190  if (return_locked) {
3191  ast_mutex_lock(&iaxsl[pvt->callno]);
3192  }
3193  res = pvt->callno;
3194  ao2_ref(pvt, -1);
3195  pvt = NULL;
3196  return res;
3197  }
3198  }
3199  /* This will occur on the first response to a message that we initiated,
3200  * such as a PING. */
3201  if (dcallno) {
3202  ast_mutex_lock(&iaxsl[dcallno]);
3203  }
3204  if (callno && dcallno && iaxs[dcallno] && !iaxs[dcallno]->peercallno && match(addr, callno, dcallno, iaxs[dcallno], check_dcallno)) {
3205  iaxs[dcallno]->peercallno = callno;
3206  res = dcallno;
3207  store_by_peercallno(iaxs[dcallno]);
3208  if (!res || !return_locked) {
3209  ast_mutex_unlock(&iaxsl[dcallno]);
3210  }
3211  return res;
3212  }
3213  if (dcallno) {
3214  ast_mutex_unlock(&iaxsl[dcallno]);
3215  }
3216  }
3217  if (!res && (new >= NEW_ALLOW)) {
3218  callno_entry entry;
3219 
3220  /* It may seem odd that we look through the peer list for a name for
3221  * this *incoming* call. Well, it is weird. However, users don't
3222  * have an IP address/port number that we can match against. So,
3223  * this is just checking for a peer that has that IP/port and
3224  * assuming that we have a user of the same name. This isn't always
3225  * correct, but it will be changed if needed after authentication. */
3226  if (!iax2_getpeername(*addr, host, sizeof(host)))
3227  snprintf(host, sizeof(host), "%s", ast_sockaddr_stringify(addr));
3228 
3229  if (peercnt_add(addr)) {
3230  /* This address has hit its callnumber limit. When the limit
3231  * is reached, the connection is not added to the peercnts table.*/
3232  return 0;
3233  }
3234 
3235  if (get_unused_callno(CALLNO_TYPE_NORMAL, validated, &entry)) {
3236  /* since we ran out of space, remove the peercnt
3237  * entry we added earlier */
3238  peercnt_remove_by_addr(addr);
3239  ast_log(LOG_WARNING, "No more space\n");
3240  return 0;
3241  }
3242  x = CALLNO_ENTRY_GET_CALLNO(entry);
3243  ast_mutex_lock(&iaxsl[x]);
3244 
3245  iaxs[x] = new_iax(addr, host);
3246  if (iaxs[x]) {
3247  if (iaxdebug)
3248  ast_debug(1, "Creating new call structure %d\n", x);
3249  iaxs[x]->callno_entry = entry;
3250  iaxs[x]->sockfd = sockfd;
3251  ast_sockaddr_copy(&iaxs[x]->addr, addr);
3252  iaxs[x]->peercallno = callno;
3253  iaxs[x]->callno = x;
3254  iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
3255  iaxs[x]->expiry = min_reg_expire;
3256  iaxs[x]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
3257  iaxs[x]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
3258  iaxs[x]->amaflags = amaflags;
3260  ast_string_field_set(iaxs[x], accountcode, accountcode);
3261  ast_string_field_set(iaxs[x], mohinterpret, mohinterpret);
3262  ast_string_field_set(iaxs[x], mohsuggest, mohsuggest);
3263  ast_string_field_set(iaxs[x], parkinglot, default_parkinglot);
3264 
3265  if (iaxs[x]->peercallno) {
3266  store_by_peercallno(iaxs[x]);
3267  }
3268  } else {
3269  ast_log(LOG_WARNING, "Out of resources\n");
3270  ast_mutex_unlock(&iaxsl[x]);
3271  replace_callno(CALLNO_ENTRY_TO_PTR(entry));
3272  return 0;
3273  }
3274  if (!return_locked)
3275  ast_mutex_unlock(&iaxsl[x]);
3276  res = x;
3277  }
3278  return res;
3279 }
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
static struct ao2_container * iax_peercallno_pvts
Another container of iax2_pvt structures.
Definition: chan_iax2.c:1202
#define IAX_USEJITTERBUF
Definition: chan_iax2.c:503
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:167
#define OBJ_POINTER
Definition: astobj2.h:1150
#define IAX_FORCE_ENCRYPT
Definition: chan_iax2.c:527
Definition: sched.c:76
callno_entry callno_entry
Definition: chan_iax2.c:784
const ast_string_field parkinglot
Definition: chan_iax2.c:861
static struct ao2_container * iax_transfercallno_pvts
Another container of iax2_pvt structures.
Definition: chan_iax2.c:1218
unsigned int pingtime
Definition: chan_iax2.c:772
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
unsigned short peercallno
Definition: chan_iax2.c:786
#define ast_debug(level,...)
Log a DEBUG message.
#define IAX_RECVCONNECTEDLINE
Definition: chan_iax2.c:526
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
unsigned short callno
Definition: parser.h:100
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
#define IAX_TRANSFERMEDIA
Definition: chan_iax2.c:520
#define IAX_NOTRANSFER
Definition: chan_iax2.c:502
struct ast_sockaddr addr
Definition: chan_iax2.c:776
Definition: search.h:40
unsigned short callno
Definition: chan_iax2.c:782
#define IAX_SENDCONNECTEDLINE
Definition: chan_iax2.c:525
static struct ast_flags globalflags
Definition: app_minivm.c:699
struct ast_sockaddr transfer
Definition: chan_iax2.c:894
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
static char* auth_method_names ( int  authmethods,
char *restrict  buf 
)
static

Get names of all auth methods.

Parameters
Bitfield of auth methods
[out]bufBuffer into which to write the names. Must be of size AUTH_METHOD_NAMES_BUFSIZE.
Returns
Auth methods name

Definition at line 415 of file chan_iax2.c.

416 {
417  char *pos = buf;
418 
419  *pos = '\0';
420 
421  if (authmethods & IAX_AUTH_RSA) {
422  pos += sprintf(pos, "|RSA");
423  }
424  if (authmethods & IAX_AUTH_MD5) {
425  pos += sprintf(pos, "|MD5");
426  }
427  if (authmethods & IAX_AUTH_PLAINTEXT) {
428  pos += sprintf(pos, "|plaintext");
429  }
430 
431  if (pos == buf) { /* No auth methods */
432  strcpy(buf, "none");
433  return buf;
434  }
435 
436  return buf + 1; /* Skip leading | */
437 }
static int authenticate_reply ( struct chan_iax2_pvt p,
struct ast_sockaddr addr,
struct iax_ies ies,
const char *  override,
const char *  okey 
)
static
Note
This function calls realtime_peer -> reg_source_db -> iax2_poke_peer -> find_callno, so do not call this function with a pvt lock held.

Definition at line 8543 of file chan_iax2.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ast_calloc, ast_channel_datastore_add(), ast_datastore_free(), AST_FRAME_IAX, AST_LIST_HEAD, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, ast_sockaddr_apply_netmask(), ast_sockaddr_cmp_addr(), ast_sockaddr_copy(), ast_sockaddr_isnull(), ast_strdupa, ast_string_field_set, chan_iax2_pvt::callno, chan_iax2_pvt::challenge, ast_datastore::data, chan_iax2_pvt::dcx, chan_iax2_pvt::ecx, chan_iax2_pvt::encmethods, IAX_ENCRYPTED, IAX_FORCE_ENCRYPT, IAX_KEYPOPULATED, ast_datastore::inheritance, ast_variable::name, ast_variable::next, iax2_peer::outkey, chan_iax2_pvt::owner, chan_iax2_pvt::peer, realtime_peer(), chan_iax2_pvt::secret, chan_iax2_pvt::username, and ast_variable::value.

8544 {
8545  struct iax2_peer *peer = NULL;
8546  /* Start pessimistic */
8547  int res = -1;
8548  int authmethods = 0;
8549  struct iax_ie_data ied;
8550  uint16_t callno = p->callno;
8551 
8552  memset(&ied, 0, sizeof(ied));
8553 
8554  if (ies->username)
8555  ast_string_field_set(p, username, ies->username);
8556  if (ies->challenge)
8557  ast_string_field_set(p, challenge, ies->challenge);
8558  if (ies->authmethods)
8559  authmethods = ies->authmethods;
8560  if (authmethods & IAX_AUTH_MD5)
8561  merge_encryption(p, ies->encmethods);
8562  else
8563  p->encmethods = 0;
8564 
8565  /* Check for override RSA authentication first */
8566  if (!ast_strlen_zero(override) || !ast_strlen_zero(okey)) {
8567  /* Normal password authentication */
8568  res = authenticate(p->challenge, override, okey, authmethods, &ied, addr, p);
8569  } else {
8570  struct ao2_iterator i = ao2_iterator_init(peers, 0);
8571  while ((peer = ao2_iterator_next(&i))) {
8572  struct ast_sockaddr peer_addr;
8573  struct ast_sockaddr tmp_sockaddr1;
8574  struct ast_sockaddr tmp_sockaddr2;
8575 
8576  ast_sockaddr_copy(&peer_addr, &peer->addr);
8577 
8578  ast_sockaddr_apply_netmask(addr, &peer->mask, &tmp_sockaddr1);
8579  ast_sockaddr_apply_netmask(&peer_addr, &peer->mask, &tmp_sockaddr2);
8580 
8581  if ((ast_strlen_zero(p->peer) || !strcmp(p->peer, peer->name))
8582  /* No peer specified at our end, or this is the peer */
8583  && (ast_strlen_zero(peer->username) || (!strcmp(peer->username, p->username)))
8584  /* No username specified in peer rule, or this is the right username */
8585  && (ast_sockaddr_isnull(&peer_addr) || !(ast_sockaddr_cmp_addr(&tmp_sockaddr1, &tmp_sockaddr2)))
8586  /* No specified host, or this is our host */
8587  ) {
8588  res = authenticate(p->challenge, peer->secret, peer->outkey, authmethods, &ied, addr, p);
8589  if (!res) {
8590  peer_unref(peer);
8591  break;
8592  }
8593  }
8594  peer_unref(peer);
8595  }
8597  if (!peer) {
8598  /* We checked our list and didn't find one. It's unlikely, but possible,
8599  that we're trying to authenticate *to* a realtime peer */
8600  const char *peer_name = ast_strdupa(p->peer);
8601  ast_mutex_unlock(&iaxsl[callno]);
8602  if ((peer = realtime_peer(peer_name, NULL))) {
8603  ast_mutex_lock(&iaxsl[callno]);
8604  if (!(p = iaxs[callno])) {
8605  peer_unref(peer);
8606  return -1;
8607  }
8608  res = authenticate(p->challenge, peer->secret,peer->outkey, authmethods, &ied, addr, p);
8609  peer_unref(peer);
8610  }
8611  if (!peer) {
8612  ast_mutex_lock(&iaxsl[callno]);
8613  if (!(p = iaxs[callno]))
8614  return -1;
8615  }
8616  }
8617  }
8618 
8619  if (!(ies->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT)) && (ies->authmethods & IAX_AUTH_RSA) && ast_strlen_zero(okey)) {
8620  /* If the only thing available is RSA, and we don't have an outkey, we can't do it... */
8621  ast_log(LOG_WARNING, "Call terminated. RSA authentication requires an outkey\n");
8622  return -1;
8623  }
8624 
8625  if (ies->encmethods) {
8626  if (ast_strlen_zero(p->secret) &&
8627  ((ies->authmethods & IAX_AUTH_RSA) || (ies->authmethods & IAX_AUTH_MD5) || (ies->authmethods & IAX_AUTH_PLAINTEXT))) {
8628  ast_log(LOG_WARNING, "Call terminated. Encryption requested by peer but no secret available locally\n");
8629  return -1;
8630  }
8631  /* Don't even THINK about trying to encrypt or decrypt anything if we don't have valid keys, for some reason... */
8632  /* If either of these happens, it's our fault, not the user's. But we should abort rather than crash. */
8633  ast_assert_return(!invalid_key(&p->ecx), -1);
8634  ast_assert_return(!invalid_key(&p->dcx), -1);
8635  ast_set_flag64(p, IAX_ENCRYPTED | IAX_KEYPOPULATED);
8636  } else if (ast_test_flag64(iaxs[callno], IAX_FORCE_ENCRYPT)) {
8637  ast_log(LOG_NOTICE, "Call initiated without encryption while forceencryption=yes option is set\n");
8638  return -1; /* if force encryption is yes, and no encryption methods, then return -1 to hangup */
8639  }
8640  if (!res) {
8641  struct ast_datastore *variablestore;
8642  struct ast_variable *var, *prev = NULL;
8643  AST_LIST_HEAD(, ast_var_t) *varlist;
8644  varlist = ast_calloc(1, sizeof(*varlist));
8645  variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
8646  if (variablestore && varlist && p->owner) {
8647  variablestore->data = varlist;
8648  variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
8649  AST_LIST_HEAD_INIT(varlist);
8650  for (var = ies->vars; var; var = var->next) {
8651  struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
8652  if (prev)
8653  ast_free(prev);
8654  prev = var;
8655  if (!newvar) {
8656  /* Don't abort list traversal, as this would leave ies->vars in an inconsistent state. */
8657  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
8658  } else {
8659  AST_LIST_INSERT_TAIL(varlist, newvar, entries);
8660  }
8661  }
8662  if (prev)
8663  ast_free(prev);
8664  ies->vars = NULL;
8665  ast_channel_datastore_add(p->owner, variablestore);
8666  } else {
8667  if (p->owner)
8668  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
8669  if (variablestore)
8670  ast_datastore_free(variablestore);
8671  if (varlist)
8672  ast_free(varlist);
8673  }
8674  }
8675 
8676  if (!res)
8677  res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREP, 0, ied.buf, ied.pos, -1);
8678  return res;
8679 }
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
struct ast_variable * next
const ast_string_field secret
Definition: chan_iax2.c:861
#define AST_LIST_HEAD(name, type)
Defines a structure to be used to hold a list of specified type.
Definition: linkedlists.h:173
ast_aes_decrypt_key dcx
Definition: chan_iax2.c:875
static struct iax2_peer * realtime_peer(const char *peername, struct ast_sockaddr *addr)
Definition: chan_iax2.c:4417
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:167
ast_aes_encrypt_key ecx
Definition: chan_iax2.c:871
#define IAX_FORCE_ENCRYPT
Definition: chan_iax2.c:527
Structure for variables, used for configurations and for channel variables.
const ast_string_field challenge
Definition: chan_iax2.c:861
#define IAX_ENCRYPTED
Definition: chan_iax2.c:510
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
Structure for a data store object.
Definition: datastore.h:64
Socket address structure.
Definition: netsock2.h:97
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
Definition: netsock2.c:413
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
int authmethods
Definition: chan_iax2.c:611
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
const ast_string_field username
Definition: chan_iax2.c:861
int ast_sockaddr_apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask, struct ast_sockaddr *result)
Apply a netmask to an address and store the result in a separate structure.
Definition: netsock2.c:357
const ast_string_field outkey
Definition: chan_iax2.c:599
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
const ast_string_field peer
Definition: chan_iax2.c:861
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
#define AST_LIST_HEAD_INIT(head)
Initializes a list head structure.
Definition: linkedlists.h:626
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
unsigned int inheritance
Definition: datastore.h:69
void * data
Definition: datastore.h:66
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
#define IAX_KEYPOPULATED
Definition: chan_iax2.c:511
struct ast_channel * owner
Definition: chan_iax2.c:808
unsigned short callno
Definition: chan_iax2.c:782
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2385
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
static int authenticate_request ( int  call_num)
static
Precondition
iaxsl[call_num] is locked
Note
Since this function calls send_command_final(), the pvt struct for the given call number may disappear while executing this function.

Definition at line 8157 of file chan_iax2.c.

References AST_FRAME_IAX, ast_string_field_set, chan_iax2_pvt::authmethods, chan_iax2_pvt::challenge, iax2_user::curauthreq, chan_iax2_pvt::encmethods, IAX_ENCRYPTED, IAX_IE_AUTHMETHODS, IAX_IE_CAUSE, IAX_IE_CAUSECODE, IAX_IE_CHALLENGE, IAX_IE_ENCRYPTION, IAX_IE_USERNAME, IAX_MAXAUTHREQ, iax2_user::maxauthreq, OBJ_KEY, send_command_final(), and chan_iax2_pvt::username.

8158 {
8159  struct iax_ie_data ied;
8160  int res = -1, authreq_restrict = 0;
8161  char challenge[10];
8162  struct chan_iax2_pvt *p = iaxs[call_num];
8163 
8164  memset(&ied, 0, sizeof(ied));
8165 
8166  /* If an AUTHREQ restriction is in place, make sure we can send an AUTHREQ back */
8167  if (ast_test_flag64(p, IAX_MAXAUTHREQ)) {
8168  struct iax2_user *user;
8169 
8170  user = ao2_find(users, p->username, OBJ_KEY);
8171  if (user) {
8172  if (user->curauthreq == user->maxauthreq)
8173  authreq_restrict = 1;
8174  else
8175  user->curauthreq++;
8176  user = user_unref(user);
8177  }
8178  }
8179 
8180  /* If the AUTHREQ limit test failed, send back an error */
8181  if (authreq_restrict) {
8182  iax_ie_append_str(&ied, IAX_IE_CAUSE, "Unauthenticated call limit reached");
8183  iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_CALL_REJECTED);
8184  send_command_final(p, AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied.buf, ied.pos, -1);
8185  return 0;
8186  }
8187 
8188  iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, p->authmethods);
8189  if (p->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_RSA)) {
8190  snprintf(challenge, sizeof(challenge), "%d", (int)ast_random());
8191  ast_string_field_set(p, challenge, challenge);
8192  /* snprintf(p->challenge, sizeof(p->challenge), "%d", (int)ast_random()); */
8193  iax_ie_append_str(&ied, IAX_IE_CHALLENGE, p->challenge);
8194  }
8195  if (p->encmethods)
8196  iax_ie_append_short(&ied, IAX_IE_ENCRYPTION, p->encmethods);
8197 
8198  iax_ie_append_str(&ied,IAX_IE_USERNAME, p->username);
8199 
8200  res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREQ, 0, ied.buf, ied.pos, -1);
8201 
8202  if (p->encmethods)
8203  ast_set_flag64(p, IAX_ENCRYPTED);
8204 
8205  return res;
8206 }
#define OBJ_KEY
Definition: astobj2.h:1151
const ast_string_field challenge
Definition: chan_iax2.c:861
#define IAX_ENCRYPTED
Definition: chan_iax2.c:510
#define IAX_IE_CHALLENGE
Definition: iax2.h:145
list of users found in the config file
#define IAX_IE_ENCRYPTION
Definition: iax2.h:173
#define IAX_IE_CAUSECODE
Definition: iax2.h:172
const ast_string_field username
Definition: chan_iax2.c:861
int curauthreq
Definition: chan_iax2.c:570
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
#define IAX_IE_AUTHMETHODS
Definition: iax2.h:144
static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int)
Definition: chan_iax2.c:7856
structure to hold users read from users.conf
int maxauthreq
Definition: chan_iax2.c:569
#define IAX_IE_USERNAME
Definition: iax2.h:136
#define IAX_IE_CAUSE
Definition: iax2.h:152
#define IAX_MAXAUTHREQ
Definition: chan_iax2.c:521
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
static int check_srcaddr ( struct ast_sockaddr addr)
static

Check if address can be used as packet source.

Return values
0address available
1address unavailable
-1error

Definition at line 12843 of file chan_iax2.c.

References ast_bind(), and ast_debug.

Referenced by peer_set_srcaddr().

12844 {
12845  int sd;
12846 
12847  sd = socket(addr->ss.ss_family, SOCK_DGRAM, 0);
12848  if (sd < 0) {
12849  ast_log(LOG_ERROR, "Socket: %s\n", strerror(errno));
12850  return -1;
12851  }
12852 
12853  if (ast_bind(sd, addr) < 0) {
12854  ast_debug(1, "Can't bind: %s\n", strerror(errno));
12855  close(sd);
12856  return 1;
12857  }
12858 
12859  close(sd);
12860  return 0;
12861 }
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
Definition: netsock2.c:590
#define ast_debug(level,...)
Log a DEBUG message.
static void defer_full_frame ( struct iax2_thread from_here,
struct iax2_thread to_here 
)
static

Queue the last read full frame for processing by a certain thread.

If there are already any full frames queued, they are sorted by sequence number.

Definition at line 9884 of file chan_iax2.c.

References ast_calloc, AST_LIST_INSERT_BEFORE_CURRENT, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, iax2_thread::full_frames, and ast_iax2_full_hdr::oseqno.

9885 {
9886  struct iax2_pkt_buf *pkt_buf, *cur_pkt_buf;
9887  struct ast_iax2_full_hdr *fh, *cur_fh;
9888 
9889  if (!(pkt_buf = ast_calloc(1, sizeof(*pkt_buf) + from_here->buf_len)))
9890  return;
9891 
9892  pkt_buf->len = from_here->buf_len;
9893  memcpy(pkt_buf->buf, from_here->buf, pkt_buf->len);
9894 
9895  fh = (struct ast_iax2_full_hdr *) pkt_buf->buf;
9896  ast_mutex_lock(&to_here->lock);
9897  AST_LIST_TRAVERSE_SAFE_BEGIN(&to_here->full_frames, cur_pkt_buf, entry) {
9898  cur_fh = (struct ast_iax2_full_hdr *) cur_pkt_buf->buf;
9899  if (fh->oseqno < cur_fh->oseqno) {
9901  break;
9902  }
9903  }
9905 
9906  if (!cur_pkt_buf)
9907  AST_LIST_INSERT_TAIL(&to_here->full_frames, pkt_buf, entry);
9908 
9909  to_here->iostate = IAX_IOSTATE_READY;
9910  ast_cond_signal(&to_here->cond);
9911 
9912  ast_mutex_unlock(&to_here->lock);
9913 }
unsigned char oseqno
Definition: iax2.h:234
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
struct iax2_thread::@126 full_frames
Definition: search.h:40
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_INSERT_BEFORE_CURRENT(elm, field)
Inserts a list entry before the current entry during a traversal.
Definition: linkedlists.h:599
static struct iax2_peer* find_peer ( const char *  name,
int  realtime 
)
static
Note
This funtion calls realtime_peer -> reg_source_db -> iax2_poke_peer -> find_callno, so do not call it with a pvt lock held.

Definition at line 2056 of file chan_iax2.c.

References OBJ_KEY, and realtime_peer().

Referenced by ast_iax2_new(), handle_cli_iax2_show_peer(), iax2_devicestate(), register_verify(), and update_registry().

2057 {
2058  struct iax2_peer *peer = NULL;
2059 
2060  peer = ao2_find(peers, name, OBJ_KEY);
2061 
2062  /* Now go for realtime if applicable */
2063  if (!peer && realtime) {
2064  peer = realtime_peer(name, NULL);
2065  }
2066  return peer;
2067 }
static struct iax2_peer * realtime_peer(const char *peername, struct ast_sockaddr *addr)
Definition: chan_iax2.c:4417
#define OBJ_KEY
Definition: astobj2.h:1151
static void iax2_destroy_helper ( struct chan_iax2_pvt pvt)
static
Note
Assumes the lock on the pvt is already held, when iax2_destroy_helper() is called.

Definition at line 2135 of file chan_iax2.c.

References ast_atomic_fetchadd_int(), AST_SCHED_DEL, chan_iax2_pvt::authid, chan_iax2_pvt::autoid, iax2_user::curauthreq, chan_iax2_pvt::destroy_initiated, IAX_MAXAUTHREQ, chan_iax2_pvt::initid, chan_iax2_pvt::jbid, chan_iax2_pvt::keyrotateid, chan_iax2_pvt::lagid, OBJ_KEY, chan_iax2_pvt::pingid, and chan_iax2_pvt::username.

Referenced by iax2_predestroy().

2136 {
2137  /* Decrement AUTHREQ count if needed */
2138  if (ast_test_flag64(pvt, IAX_MAXAUTHREQ)) {
2139  struct iax2_user *user;
2140 
2141  user = ao2_find(users, pvt->username, OBJ_KEY);
2142  if (user) {
2143  ast_atomic_fetchadd_int(&user->curauthreq, -1);
2144  user_unref(user);
2145  }
2146 
2147  ast_clear_flag64(pvt, IAX_MAXAUTHREQ);
2148  }
2149 
2150 
2151  /* Mark call destroy initiated flag. */
2152  pvt->destroy_initiated = 1;
2153 
2154  /*
2155  * Schedule deleting the scheduled (but didn't run yet) PINGs or LAGRQs.
2156  * Already running tasks will be terminated because of destroy_initiated.
2157  *
2158  * Don't call AST_SCHED_DEL from this thread for pingid and lagid because
2159  * it leads to a deadlock between the scheduler thread callback locking
2160  * the callno mutex and this thread which holds the callno mutex one or
2161  * more times. It is better to have another thread delete the scheduled
2162  * callbacks which doesn't lock the callno mutex.
2163  */
2164  iax2_sched_add(sched, 0, iax2_delete_from_sched, (void*)(long)pvt->pingid);
2165  iax2_sched_add(sched, 0, iax2_delete_from_sched, (void*)(long)pvt->lagid);
2166 
2167  pvt->pingid = -1;
2168  pvt->lagid = -1;
2169 
2170  AST_SCHED_DEL(sched, pvt->autoid);
2171  AST_SCHED_DEL(sched, pvt->authid);
2172  AST_SCHED_DEL(sched, pvt->initid);
2173  AST_SCHED_DEL(sched, pvt->jbid);
2175 }
#define OBJ_KEY
Definition: astobj2.h:1151
Definition: sched.c:76
list of users found in the config file
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
const ast_string_field username
Definition: chan_iax2.c:861
int destroy_initiated
Definition: chan_iax2.c:931
int curauthreq
Definition: chan_iax2.c:570
structure to hold users read from users.conf
#define IAX_MAXAUTHREQ
Definition: chan_iax2.c:521
static int iax2_lock_callno_unless_destroyed ( int  callno)
static

Acquire the iaxsl[callno] if call exists and not having ongoing hangup.

Parameters
callnoCall number to lock.
Return values
0If call disappeared or has ongoing hangup procedure.
1If call found and mutex is locked.

Definition at line 1745 of file chan_iax2.c.

References ast_debug.

1746 {
1747  ast_mutex_lock(&iaxsl[callno]);
1748 
1749  /* We acquired the lock; but the call was already destroyed (we came after full hang up procedures)
1750  * or destroy initiated (in middle of hang up procedure. */
1751  if (!iaxs[callno] || iaxs[callno]->destroy_initiated) {
1752  ast_debug(3, "I wanted to lock callno %d, but it is dead or going to die.\n", callno);
1753  ast_mutex_unlock(&iaxsl[callno]);
1754  return 0;
1755  }
1756 
1757  /* Lock acquired, and callno is alive and kicking. */
1758  return 1;
1759 }
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
#define ast_debug(level,...)
Log a DEBUG message.
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
static int iax2_predestroy ( int  callno)
static
Note
Since this function calls iax2_queue_hangup(), the pvt struct for the given call number may disappear during its execution.

Definition at line 3483 of file chan_iax2.c.

References ast_module_unref, chan_iax2_pvt::callno, iax2_destroy_helper(), iax2_queue_hangup(), IAX_ALREADYGONE, chan_iax2_pvt::owner, and ast_module_info::self.

Referenced by send_command_final().

3484 {
3485  struct ast_channel *c = NULL;
3486  struct chan_iax2_pvt *pvt = iaxs[callno];
3487 
3488  if (!pvt)
3489  return -1;
3490 
3491  if (!ast_test_flag64(pvt, IAX_ALREADYGONE)) {
3492  iax2_destroy_helper(pvt);
3493  ast_set_flag64(pvt, IAX_ALREADYGONE);
3494  }
3495 
3496  if ((c = pvt->owner)) {
3497  ast_channel_tech_pvt_set(c, NULL);
3499  pvt->owner = NULL;
3501  }
3502 
3503  return 0;
3504 }
Main Channel structure associated with a channel.
static void iax2_destroy_helper(struct chan_iax2_pvt *pvt)
Definition: chan_iax2.c:2135
struct ast_module * self
Definition: module.h:356
static int iax2_queue_hangup(int callno)
Queue a hangup frame on the ast_channel owner.
Definition: chan_iax2.c:3369
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
#define IAX_ALREADYGONE
Definition: chan_iax2.c:507
struct ast_channel * owner
Definition: chan_iax2.c:808
unsigned short callno
Definition: chan_iax2.c:782
static void * iax2_process_thread ( void *  data)
static
Note
For some reason, idle threads are exiting without being removed from an idle list, which is causing memory corruption. Forcibly remove it from the list, if it's there.

Definition at line 12158 of file chan_iax2.c.

References ast_atomic_fetchadd_int(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_samp2tv(), ast_tvadd(), ast_tvnow(), and handle_deferred_full_frames().

12159 {
12160  struct iax2_thread *thread = data;
12161  struct timeval wait;
12162  struct timespec ts;
12163  int put_into_idle = 0;
12164  int first_time = 1;
12165  int old_state;
12166 
12167  ast_atomic_fetchadd_int(&iaxactivethreadcount, 1);
12168 
12169  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
12170  pthread_cleanup_push(iax2_process_thread_cleanup, data);
12171 
12172  for (;;) {
12173  /* Wait for something to signal us to be awake */
12174  ast_mutex_lock(&thread->lock);
12175 
12176  if (thread->stop) {
12177  ast_mutex_unlock(&thread->lock);
12178  break;
12179  }
12180 
12181  /* Flag that we're ready to accept signals */
12182  if (first_time) {
12183  signal_condition(&thread->init_lock, &thread->init_cond);
12184  first_time = 0;
12185  }
12186 
12187  /* Put into idle list if applicable */
12188  if (put_into_idle) {
12189  insert_idle_thread(thread);
12190  }
12191 
12192  if (thread->type == IAX_THREAD_TYPE_DYNAMIC) {
12193  struct iax2_thread *t = NULL;
12194  /* Wait to be signalled or time out */
12195  wait = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
12196  ts.tv_sec = wait.tv_sec;
12197  ts.tv_nsec = wait.tv_usec * 1000;
12198  if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT) {
12199  /* This thread was never put back into the available dynamic
12200  * thread list, so just go away. */
12201  if (!put_into_idle || thread->stop) {
12202  ast_mutex_unlock(&thread->lock);
12203  break;
12204  }
12206  /* Account for the case where this thread is acquired *right* after a timeout */
12207  if ((t = AST_LIST_REMOVE(&dynamic_list, thread, list)))
12208  ast_atomic_fetchadd_int(&iaxdynamicthreadcount, -1);
12210  if (t) {
12211  /* This dynamic thread timed out waiting for a task and was
12212  * not acquired immediately after the timeout,
12213  * so it's time to go away. */
12214  ast_mutex_unlock(&thread->lock);
12215  break;
12216  }
12217  /* Someone grabbed our thread *right* after we timed out.
12218  * Wait for them to set us up with something to do and signal
12219  * us to continue. */
12220  wait = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
12221  ts.tv_sec = wait.tv_sec;
12222  ts.tv_nsec = wait.tv_usec * 1000;
12223  if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT) {
12224  ast_mutex_unlock(&thread->lock);
12225  break;
12226  }
12227  }
12228  } else {
12229  ast_cond_wait(&thread->cond, &thread->lock);
12230  }
12231 
12232  /* Go back into our respective list */
12233  put_into_idle = 1;
12234 
12235  ast_mutex_unlock(&thread->lock);
12236 
12237  if (thread->stop) {
12238  break;
12239  }
12240 
12241  /* See what we need to do */
12242  switch (thread->iostate) {
12243  case IAX_IOSTATE_IDLE:
12244  continue;
12245  case IAX_IOSTATE_READY:
12246  thread->actions++;
12247  thread->iostate = IAX_IOSTATE_PROCESSING;
12248  socket_process(thread);
12250  break;
12251  case IAX_IOSTATE_SCHEDREADY:
12252  thread->actions++;
12253  thread->iostate = IAX_IOSTATE_PROCESSING;
12254 #ifdef SCHED_MULTITHREADED
12255  thread->schedfunc(thread->scheddata);
12256 #endif
12257  break;
12258  default:
12259  break;
12260  }
12261 
12262  /* The network thread added us to the active_thread list when we were given
12263  * frames to process, Now that we are done, we must remove ourselves from
12264  * the active list, and return to the idle list */
12266  AST_LIST_REMOVE(&active_list, thread, list);
12268 
12269  /* Make sure another frame didn't sneak in there after we thought we were done. */
12271 
12272  time(&thread->checktime);
12273  thread->iostate = IAX_IOSTATE_IDLE;
12274 #ifdef DEBUG_SCHED_MULTITHREAD
12275  thread->curfunc[0]='\0';
12276 #endif
12277  }
12278 
12279  /*!
12280  * \note For some reason, idle threads are exiting without being
12281  * removed from an idle list, which is causing memory
12282  * corruption. Forcibly remove it from the list, if it's there.
12283  */
12285  AST_LIST_REMOVE(&idle_list, thread, list);
12287 
12289  AST_LIST_REMOVE(&dynamic_list, thread, list);
12291 
12292  if (!thread->stop) {
12293  /* Nobody asked me to stop so nobody is waiting to join me. */
12294  pthread_detach(pthread_self());
12295  }
12296 
12297  /* I am exiting here on my own volition, I need to clean up my own data structures
12298  * Assume that I am no longer in any of the lists (idle, active, or dynamic)
12299  */
12300  pthread_cleanup_pop(1);
12301  return NULL;
12302 }
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
static void handle_deferred_full_frames(struct iax2_thread *thread)
Handle any deferred full frames for this thread.
Definition: chan_iax2.c:9854
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
pthread_t thread
Definition: app_sla.c:329
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:856
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:282
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
static int iax2_queue_frame ( int  callno,
struct ast_frame f 
)
static

Queue a frame to a call's owning asterisk channel.

Precondition
This function assumes that iaxsl[callno] is locked when called.
Note
IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno] was valid before calling it, it may no longer be valid after calling it. This function may unlock and lock the mutex associated with this callno, meaning that another thread may grab it and destroy the call.

Definition at line 3300 of file chan_iax2.c.

References ast_queue_frame().

Referenced by __do_deliver().

3301 {
3302  iax2_lock_owner(callno);
3303  if (iaxs[callno] && iaxs[callno]->owner) {
3304  ast_queue_frame(iaxs[callno]->owner, f);
3305  ast_channel_unlock(iaxs[callno]->owner);
3306  }
3307  return 0;
3308 }
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel's frame queue.
Definition: channel.c:1139
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
static int iax2_queue_hangup ( int  callno)
static

Queue a hangup frame on the ast_channel owner.

This function queues a hangup frame on the owner of the IAX2 pvt struct that is active for the given call number.

Precondition
Assumes lock for callno is already held.
Note
IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno] was valid before calling it, it may no longer be valid after calling it. This function may unlock and lock the mutex associated with this callno, meaning that another thread may grab it and destroy the call.

Definition at line 3369 of file chan_iax2.c.

References ast_queue_hangup().

Referenced by iax2_predestroy().

3370 {
3371  iax2_lock_owner(callno);
3372  if (iaxs[callno] && iaxs[callno]->owner) {
3373  ast_queue_hangup(iaxs[callno]->owner);
3374  ast_channel_unlock(iaxs[callno]->owner);
3375  }
3376  return 0;
3377 }
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1150
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
static int iax2_queue_hold ( int  callno,
const char *  musicclass 
)
static

Queue a hold frame on the ast_channel owner.

This function queues a hold frame on the owner of the IAX2 pvt struct that is active for the given call number.

Precondition
Assumes lock for callno is already held.
Note
IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno] was valid before calling it, it may no longer be valid after calling it. This function may unlock and lock the mutex associated with this callno, meaning that another thread may grab it and destroy the call.

Definition at line 3323 of file chan_iax2.c.

References ast_queue_hold().

3324 {
3325  iax2_lock_owner(callno);
3326  if (iaxs[callno] && iaxs[callno]->owner) {
3327  ast_queue_hold(iaxs[callno]->owner, musicclass);
3328  ast_channel_unlock(iaxs[callno]->owner);
3329  }
3330  return 0;
3331 }
int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
Queue a hold frame.
Definition: channel.c:1191
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
static int iax2_queue_unhold ( int  callno)
static

Queue an unhold frame on the ast_channel owner.

This function queues an unhold frame on the owner of the IAX2 pvt struct that is active for the given call number.

Precondition
Assumes lock for callno is already held.
Note
IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno] was valid before calling it, it may no longer be valid after calling it. This function may unlock and lock the mutex associated with this callno, meaning that another thread may grab it and destroy the call.

Definition at line 3346 of file chan_iax2.c.

References ast_queue_unhold().

3347 {
3348  iax2_lock_owner(callno);
3349  if (iaxs[callno] && iaxs[callno]->owner) {
3350  ast_queue_unhold(iaxs[callno]->owner);
3351  ast_channel_unlock(iaxs[callno]->owner);
3352  }
3353  return 0;
3354 }
int ast_queue_unhold(struct ast_channel *chan)
Queue an unhold frame.
Definition: channel.c:1216
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
static int load_module ( void  )
static

Load the module.

Module loading including tests for configuration or dependencies. This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE, or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails tests return AST_MODULE_LOAD_FAILURE. If the module can not load the configuration file or other non-critical problem return AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.

Definition at line 14990 of file chan_iax2.c.

References ao2_callback, ao2_ref, ast_channel_register(), ast_cli_register_multiple, ast_custom_function_register, ast_format_cap_alloc, ast_format_cap_append_by_type(), AST_FORMAT_CAP_FLAG_DEFAULT, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_manager_register_xml, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_realtime_require_field(), ast_register_application_xml, ast_register_switch(), ast_sched_context_create(), ast_sched_context_destroy(), ast_sched_start_thread(), ast_timer_close(), ast_timer_open(), ast_timer_set_rate(), ast_channel_tech::capabilities, iax2_prov_app(), io_context_create(), io_context_destroy(), manager_iax2_show_peer_list(), manager_iax2_show_peers(), and set_config().

14991 {
14992  static const char config[] = "iax.conf";
14993  int x = 0;
14994  struct iax2_registry *reg = NULL;
14995 
14997  return AST_MODULE_LOAD_DECLINE;
14998  }
14999  ast_format_cap_append_by_type(iax2_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
15000 
15001  if (load_objects()) {
15002  ao2_ref(iax2_tech.capabilities, -1);
15003  iax2_tech.capabilities = NULL;
15004  return AST_MODULE_LOAD_DECLINE;
15005  }
15006 
15007  memset(iaxs, 0, sizeof(iaxs));
15008 
15009  for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
15010  ast_mutex_init(&iaxsl[x]);
15011  }
15012 
15013  if (!(sched = ast_sched_context_create())) {
15014  ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
15015  ao2_ref(iax2_tech.capabilities, -1);
15016  iax2_tech.capabilities = NULL;
15017  return AST_MODULE_LOAD_DECLINE;
15018  }
15019 
15022  ao2_ref(iax2_tech.capabilities, -1);
15023  iax2_tech.capabilities = NULL;
15024  sched = NULL;
15025  return AST_MODULE_LOAD_DECLINE;
15026  }
15027 
15028  if (!(io = io_context_create())) {
15029  ast_log(LOG_ERROR, "Failed to create I/O context\n");
15031  ao2_ref(iax2_tech.capabilities, -1);
15032  iax2_tech.capabilities = NULL;
15033  sched = NULL;
15034  return AST_MODULE_LOAD_DECLINE;
15035  }
15036 
15037  if (!(netsock = ast_netsock_list_alloc())) {
15038  ast_log(LOG_ERROR, "Failed to create netsock list\n");
15039  io_context_destroy(io);
15041  ao2_ref(iax2_tech.capabilities, -1);
15042  iax2_tech.capabilities = NULL;
15043  sched = NULL;
15044  return AST_MODULE_LOAD_DECLINE;
15045  }
15046  ast_netsock_init(netsock);
15047 
15048  outsock = ast_netsock_list_alloc();
15049  if (!outsock) {
15050  ast_log(LOG_ERROR, "Could not allocate outsock list.\n");
15051  io_context_destroy(io);
15053  ao2_ref(iax2_tech.capabilities, -1);
15054  iax2_tech.capabilities = NULL;
15055  sched = NULL;
15056  return AST_MODULE_LOAD_DECLINE;
15057  }
15058  ast_netsock_init(outsock);
15059 
15060  randomcalltokendata = ast_random();
15061 
15062  iax_set_output(iax_debug_output);
15063  iax_set_error(iax_error_output);
15064  jb_setoutput(jb_error_output, jb_warning_output, NULL);
15065 
15066  if ((timer = ast_timer_open())) {
15067  ast_timer_set_rate(timer, 1000 / trunkfreq);
15068  }
15069 
15070  if (set_config(config, 0, 0) == -1) {
15071  if (timer) {
15072  ast_timer_close(timer);
15073  timer = NULL;
15074  }
15075  __unload_module();
15076  return AST_MODULE_LOAD_DECLINE;
15077  }
15078 
15079  ast_cli_register_multiple(cli_iax2, ARRAY_LEN(cli_iax2));
15080 
15082 
15083  ast_custom_function_register(&iaxpeer_function);
15084  ast_custom_function_register(&iaxvar_function);
15085 
15086  ast_manager_register_xml("IAXpeers", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_peers);
15087  ast_manager_register_xml("IAXpeerlist", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_peer_list);
15088  ast_manager_register_xml("IAXnetstats", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_netstats);
15089  ast_manager_register_xml("IAXregistry", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_registry);
15090 
15091  if (ast_channel_register(&iax2_tech)) {
15092  ast_log(LOG_ERROR, "Unable to register channel class %s\n", "IAX2");
15093  __unload_module();
15094  return AST_MODULE_LOAD_DECLINE;
15095  }
15096 
15097  if (ast_register_switch(&iax2_switch)) {
15098  ast_log(LOG_ERROR, "Unable to register IAX switch\n");
15099  }
15100 
15101  if (start_network_thread()) {
15102  ast_log(LOG_ERROR, "Unable to start network thread\n");
15103  __unload_module();
15104  return AST_MODULE_LOAD_DECLINE;
15105  } else {
15106  ast_verb(2, "IAX Ready and Listening\n");
15107  }
15108 
15111  iax2_do_register(reg);
15113 
15114  ao2_callback(peers, 0, peer_set_sock_cb, NULL);
15115  ao2_callback(peers, 0, iax2_poke_peer_cb, NULL);
15116 
15117 
15118  iax_firmware_reload();
15119  iax_provision_reload(0);
15120 
15121  ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL);
15122 
15123  network_change_stasis_subscribe();
15124 
15125  return AST_MODULE_LOAD_SUCCESS;
15126 }
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
int ast_sched_start_thread(struct ast_sched_context *con)
Start a thread for processing scheduler entries.
Definition: sched.c:197
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
Definition: sched.c:76
void ast_timer_close(struct ast_timer *handle)
Close an opened timing handle.
Definition: timing.c:154
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition: timing.c:122
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Add all codecs Asterisk knows about for a specific type to the capabilities structure.
Definition: format_cap.c:216
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:539
static int manager_iax2_show_peer_list(struct mansession *s, const struct message *m)
callback to display iax peers in manager format
Definition: chan_iax2.c:7367
int ast_realtime_require_field(const char *family,...) attribute_sentinel
Inform realtime what fields that may be stored.
Definition: main/config.c:3549
void io_context_destroy(struct io_context *ioc)
Destroys a context.
Definition: io.c:107
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
Definition: sched.c:238
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
static struct ast_netsock_list * outsock
Definition: chan_iax2.c:367
struct ast_format_cap * capabilities
Definition: channel.h:632
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
static int manager_iax2_show_peers(struct mansession *s, const struct message *m)
callback to display iax peers in manager
Definition: chan_iax2.c:7345
int ast_register_switch(struct ast_switch *sw)
Register an alternative dialplan switch.
Definition: pbx_switch.c:58
static int set_config(const char *config_file, int reload, int forced)
Load configuration.
Definition: chan_iax2.c:13646
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition: timing.c:166
Definition: search.h:40
static int iax2_prov_app(struct ast_channel *chan, const char *data)
Definition: chan_iax2.c:12417
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:191
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
Definition: sched.c:271
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
struct io_context * io_context_create(void)
Creates a context Create a context for I/O operations Basically mallocs an IO structure and sets up s...
Definition: io.c:81
static int make_trunk ( unsigned short  callno,
int  locked 
)
static
Note
We delete these before switching the slot, because if they fire in the meantime, they will generate a warning.

Definition at line 2380 of file chan_iax2.c.

References ast_debug, AST_SCHED_DEL, iax_frame::callno, chan_iax2_pvt::callno, chan_iax2_pvt::callno_entry, chan_iax2_pvt::lagid, and chan_iax2_pvt::pingid.

2381 {
2382  int x;
2383  int res= 0;
2384  callno_entry entry;
2385  if (iaxs[callno]->oseqno) {
2386  ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
2387  return -1;
2388  }
2389  if (callno >= TRUNK_CALL_START) {
2390  ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
2391  return -1;
2392  }
2393 
2394  if (get_unused_callno(
2395  CALLNO_TYPE_TRUNK,
2396  CALLNO_ENTRY_IS_VALIDATED(iaxs[callno]->callno_entry),
2397  &entry)) {
2398  ast_log(LOG_WARNING, "Unable to trunk call: Insufficient space\n");
2399  return -1;
2400  }
2401 
2402  x = CALLNO_ENTRY_GET_CALLNO(entry);
2403  ast_mutex_lock(&iaxsl[x]);
2404 
2405  /*!
2406  * \note We delete these before switching the slot, because if
2407  * they fire in the meantime, they will generate a warning.
2408  */
2409  AST_SCHED_DEL(sched, iaxs[callno]->pingid);
2410  AST_SCHED_DEL(sched, iaxs[callno]->lagid);
2411  iaxs[callno]->lagid = iaxs[callno]->pingid = -1;
2412  iaxs[x] = iaxs[callno];
2413  iaxs[x]->callno = x;
2414 
2415  /* since we copied over the pvt from a different callno, make sure the old entry is replaced
2416  * before assigning the new one */
2417  if (iaxs[x]->callno_entry) {
2418  iax2_sched_add(
2419  sched,
2420  MIN_REUSE_TIME * 1000,
2421  replace_callno,
2422  CALLNO_ENTRY_TO_PTR(iaxs[x]->callno_entry));
2423 
2424  }
2425  iaxs[x]->callno_entry = entry;
2426 
2427  iaxs[callno] = NULL;
2428  /* Update the two timers that should have been started */
2429  iaxs[x]->pingid = iax2_sched_add(sched,
2430  ping_time * 1000, send_ping, (void *)(long)x);
2431  iaxs[x]->lagid = iax2_sched_add(sched,
2432  lagrq_time * 1000, send_lagrq, (void *)(long)x);
2433 
2434  if (locked)
2435  ast_mutex_unlock(&iaxsl[callno]);
2436  res = x;
2437  if (!locked)
2438  ast_mutex_unlock(&iaxsl[x]);
2439 
2440  /* We moved this call from a non-trunked to a trunked call */
2441  ast_debug(1, "Made call %d into trunk call %d\n", callno, x);
2442 
2443  return res;
2444 }
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
Definition: sched.c:76
callno_entry callno_entry
Definition: chan_iax2.c:784
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
#define ast_debug(level,...)
Log a DEBUG message.
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
Definition: search.h:40
unsigned short callno
Definition: chan_iax2.c:782
static void parse_dial_string ( char *  data,
struct parsed_dial_string pds 
)
static

Parses an IAX dial string into its component parts.

Parameters
datathe string to be parsed
pdspointer to a struct parsed_dial_string to be filled in

This function parses the string and fills the structure with pointers to its component parts. The input string will be modified.

Note
This function supports both plaintext passwords and RSA key names; if the password string is formatted as '[keyname]', then the keyname will be placed into the key field, and the password field will be set to NULL.
The dial string format is:
[username[:password]@]peer[:port][/exten[@context]][/options] 

Definition at line 5092 of file chan_iax2.c.

References ast_debug, and ast_strip_quoted().

Referenced by iax2_devicestate(), and set_config().

5093 {
5094  char *outkey = NULL;
5095 
5096  if (ast_strlen_zero(data))
5097  return;
5098 
5099  pds->peer = strsep(&data, "/");
5100  pds->exten = strsep(&data, "/");
5101  pds->options = data;
5102 
5103  if (pds->exten) {
5104  data = pds->exten;
5105  pds->exten = strsep(&data, "@");
5106  pds->context = data;
5107  }
5108 
5109  if (strchr(pds->peer, '@')) {
5110  data = pds->peer;
5111  pds->username = strsep(&data, "@");
5112  pds->peer = data;
5113  }
5114 
5115  if (pds->username) {
5116  data = pds->username;
5117  pds->username = strsep(&data, ":");
5118  pds->password = strsep(&data, ":");
5119  outkey = data;
5120  }
5121 
5122  data = pds->peer;
5123  pds->peer = strsep(&data, ":");
5124  pds->port = data;
5125 
5126  /*
5127  * Check for a key name wrapped in [] in the password position.
5128  * If found, move it to the key field instead.
5129  * Also allow for both key and secret to be specified, now that
5130  * encryption is possible with RSA authentication.
5131  */
5132 
5133  if (pds->password && (pds->password[0] == '[')) { /* key (then maybe secret) */
5134  pds->key = ast_strip_quoted(pds->password, "[", "]");
5135  if (ast_strlen_zero(outkey)) {
5136  pds->password = NULL;
5137  ast_debug(1, "Outkey (%s), no secret\n", pds->key);
5138  } else {
5139  pds->password = outkey;
5140  ast_debug(1, "Outkey (%s) and secret (%s)\n", pds->key, pds->password);
5141  }
5142  } else if (outkey && (outkey[0] == '[')) { /* secret, then key */
5143  pds->key = ast_strip_quoted(outkey, "[", "]");
5144  if (ast_strlen_zero(pds->password)) {
5145  ast_debug(1, "Outkey (%s), no secret\n", pds->key);
5146  } else {
5147  ast_debug(1, "Outkey (%s) and secret (%s)\n", pds->key, pds->password);
5148  }
5149  }
5150 }
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: utils.c:1818
#define ast_debug(level,...)
Log a DEBUG message.
static int peer_cmp_cb ( void *  obj,
void *  arg,
int  flags 
)
static
Note
The only member of the peer passed here guaranteed to be set is the name field

Definition at line 2020 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, and OBJ_KEY.

2021 {
2022  struct iax2_peer *peer = obj, *peer2 = arg;
2023  const char *name = arg;
2024 
2025  return !strcmp(peer->name, flags & OBJ_KEY ? name : peer2->name) ?
2026  CMP_MATCH | CMP_STOP : 0;
2027 }
#define OBJ_KEY
Definition: astobj2.h:1151
static int peer_hash_cb ( const void *  obj,
const int  flags 
)
static
Note
The only member of the peer passed here guaranteed to be set is the name field

Definition at line 2009 of file chan_iax2.c.

References ast_str_hash(), and OBJ_KEY.

2010 {
2011  const struct iax2_peer *peer = obj;
2012  const char *name = obj;
2013 
2014  return ast_str_hash(flags & OBJ_KEY ? name : peer->name);
2015 }
#define OBJ_KEY
Definition: astobj2.h:1151
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259
static struct iax2_peer * realtime_peer ( const char *  peername,
struct ast_sockaddr addr 
)
static
Note
This function calls reg_source_db -> iax2_poke_peer -> find_callno, so do not call this with a pvt lock held.
Note
If this one loaded something, then we need to ensure that the host field matched. The only reason why we can't have this as a criteria is because we only have the IP address and the host field might be set as a name (and the reverse PTR might not match).

Definition at line 4417 of file chan_iax2.c.

References ao2_link, ast_debug, ast_get_time_t(), ast_parse_arg(), AST_SCHED_DEL, ast_sockaddr_cmp_addr(), ast_sockaddr_isnull(), ast_sockaddr_parse(), ast_sockaddr_port, ast_sockaddr_resolve(), ast_sockaddr_set_port, ast_sockaddr_stringify_addr(), ast_sockaddr_stringify_port(), ast_strdupa, ast_variables_destroy(), build_peer(), iax2_peer::expire, IAX_DEFAULT_REG_EXPIRE, IAX_DYNAMIC, IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, IAX_RTIGNOREREGEXPIRE, IAX_TEMPONLY, ast_variable::name, ast_variable::next, and ast_variable::value.

Referenced by authenticate_reply(), and find_peer().

4418 {
4419  struct ast_variable *var = NULL;
4420  struct ast_variable *tmp;
4421  struct iax2_peer *peer=NULL;
4422  time_t regseconds = 0, nowtime;
4423  int dynamic=0;
4424  char *str_addr, *str_port;
4425 
4426  str_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
4427  str_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
4428 
4429  if (peername) {
4430  var = ast_load_realtime("iaxpeers", "name", peername, "host", "dynamic", SENTINEL);
4431  if (!var && !ast_sockaddr_isnull(addr)) {
4432  var = ast_load_realtime("iaxpeers", "name", peername, "host", str_addr, SENTINEL);
4433  }
4434  } else if (!ast_sockaddr_isnull(addr)) {
4435  var = ast_load_realtime("iaxpeers", "ipaddr", str_addr, "port", str_port, SENTINEL);
4436  if (var) {
4437  /* We'll need the peer name in order to build the structure! */
4438  for (tmp = var; tmp; tmp = tmp->next) {
4439  if (!strcasecmp(tmp->name, "name"))
4440  peername = tmp->value;
4441  }
4442  }
4443  }
4444  if (!var && peername) { /* Last ditch effort */
4445  var = ast_load_realtime("iaxpeers", "name", peername, SENTINEL);
4446  /*!\note
4447  * If this one loaded something, then we need to ensure that the host
4448  * field matched. The only reason why we can't have this as a criteria
4449  * is because we only have the IP address and the host field might be
4450  * set as a name (and the reverse PTR might not match).
4451  */
4452  if (var && !ast_sockaddr_isnull(addr)) {
4453  for (tmp = var; tmp; tmp = tmp->next) {
4454  if (!strcasecmp(tmp->name, "host")) {
4455  struct ast_sockaddr *hostaddr = NULL;
4456 
4457  if (!ast_sockaddr_resolve(&hostaddr, tmp->value, PARSE_PORT_FORBID, AST_AF_UNSPEC)
4458  || ast_sockaddr_cmp_addr(hostaddr, addr)) {
4459  /* No match */
4460  ast_variables_destroy(var);
4461  var = NULL;
4462  }
4463  ast_free(hostaddr);
4464  break;
4465  }
4466  }
4467  }
4468  }
4469  if (!var)
4470  return NULL;
4471 
4472  peer = build_peer(peername, var, NULL, ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS) ? 0 : 1);
4473 
4474  if (!peer) {
4475  ast_variables_destroy(var);
4476  return NULL;
4477  }
4478 
4479  for (tmp = var; tmp; tmp = tmp->next) {
4480  /* Make sure it's not a user only... */
4481  if (!strcasecmp(tmp->name, "type")) {
4482  if (strcasecmp(tmp->value, "friend") &&
4483  strcasecmp(tmp->value, "peer")) {
4484  /* Whoops, we weren't supposed to exist! */
4485  peer = peer_unref(peer);
4486  break;
4487  }
4488  } else if (!strcasecmp(tmp->name, "regseconds")) {
4489  ast_get_time_t(tmp->value, &regseconds, 0, NULL);
4490  } else if (!strcasecmp(tmp->name, "ipaddr")) {
4491  int setport = ast_sockaddr_port(&peer->addr);
4492  if (ast_parse_arg(tmp->value, PARSE_ADDR | PARSE_PORT_FORBID, NULL)) {
4493  ast_log(LOG_WARNING, "Failed to parse sockaddr '%s' for ipaddr of realtime peer '%s'\n", tmp->value, tmp->name);
4494  } else {
4495  ast_sockaddr_parse(&peer->addr, tmp->value, 0);
4496  }
4497  ast_sockaddr_set_port(&peer->addr, setport);
4498  } else if (!strcasecmp(tmp->name, "port")) {
4499  int bindport;
4500  if (ast_parse_arg(tmp->value, PARSE_UINT32 | PARSE_IN_RANGE, &bindport, 0, 65535)) {
4501  bindport = IAX_DEFAULT_PORTNO;
4502  }
4503  ast_sockaddr_set_port(&peer->addr, bindport);
4504  } else if (!strcasecmp(tmp->name, "host")) {
4505  if (!strcasecmp(tmp->value, "dynamic"))
4506  dynamic = 1;
4507  }
4508  }
4509 
4510  ast_variables_destroy(var);
4511 
4512  if (ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS)) {
4513  ast_copy_flags64(peer, &globalflags, IAX_RTAUTOCLEAR|IAX_RTCACHEFRIENDS);
4514  if (ast_test_flag64(peer, IAX_RTAUTOCLEAR)) {
4515  if (peer->expire > -1) {
4516  if (!AST_SCHED_DEL(sched, peer->expire)) {
4517  peer->expire = -1;
4518  peer_unref(peer);
4519  }
4520  }
4521  peer->expire = iax2_sched_add(sched, (global_rtautoclear) * 1000, expire_registry, peer_ref(peer));
4522  if (peer->expire == -1)
4523  peer_unref(peer);
4524  }
4525  ao2_link(peers, peer);
4526  if (ast_test_flag64(peer, IAX_DYNAMIC))
4527  reg_source_db(peer);
4528  } else {
4529  ast_set_flag64(peer, IAX_TEMPONLY);
4530  }
4531 
4532  if (!ast_test_flag64(&globalflags, IAX_RTIGNOREREGEXPIRE) && dynamic) {
4533  time(&nowtime);
4534  if ((nowtime - regseconds) > IAX_DEFAULT_REG_EXPIRE) {
4535  memset(&peer->addr, 0, sizeof(peer->addr));
4536  realtime_update_peer(peer->name, &peer->addr, 0);
4537  ast_debug(1, "realtime_peer: Bah, '%s' is expired (%d/%d/%d)!\n",
4538  peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
4539  }
4540  else {
4541  ast_debug(1, "realtime_peer: Registration for '%s' still active (%d/%d/%d)!\n",
4542  peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
4543  }
4544  }
4545 
4546  return peer;
4547 }
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:286
struct ast_variable * next
int expire
Definition: chan_iax2.c:614
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
#define IAX_DEFAULT_REG_EXPIRE
Definition: iax2.h:123
Structure for variables, used for configurations and for channel variables.
Definition: sched.c:76
Socket address structure.
Definition: netsock2.h:97
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
Definition: netsock2.c:413
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:517
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Definition: netsock2.h:358
#define IAX_DYNAMIC
Definition: chan_iax2.c:504
int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
Parse a time (integer) string.
Definition: utils.c:2446
static struct iax2_peer * build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
Create peer structure based on configuration.
Definition: chan_iax2.c:12964
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define IAX_RTCACHEFRIENDS
Definition: chan_iax2.c:515
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:532
#define IAX_RTIGNOREREGEXPIRE
Definition: chan_iax2.c:518
#define IAX_RTAUTOCLEAR
Definition: chan_iax2.c:517
static struct ast_flags globalflags
Definition: app_minivm.c:699
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
Definition: netsock2.c:280
#define IAX_TEMPONLY
Definition: chan_iax2.c:500
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
static struct iax2_user * realtime_user ( const char *  username,
struct ast_sockaddr addr 
)
static
Note
If this one loaded something, then we need to ensure that the host field matched. The only reason why we can't have this as a criteria is because we only have the IP address and the host field might be set as a name (and the reverse PTR might not match).

Definition at line 4549 of file chan_iax2.c.

References ao2_link, ast_sockaddr_cmp_addr(), ast_sockaddr_isnull(), ast_sockaddr_resolve(), ast_sockaddr_stringify_addr(), ast_sockaddr_stringify_port(), ast_strdupa, ast_variables_destroy(), build_user(), IAX_RTCACHEFRIENDS, IAX_TEMPONLY, ast_variable::name, ast_variable::next, and ast_variable::value.

4550 {
4551  struct ast_variable *var;
4552  struct ast_variable *tmp;
4553  struct iax2_user *user=NULL;
4554  char *str_addr, *str_port;
4555 
4556  str_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
4557  str_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
4558 
4559  var = ast_load_realtime("iaxusers", "name", username, "host", "dynamic", SENTINEL);
4560  if (!var)
4561  var = ast_load_realtime("iaxusers", "name", username, "host", str_addr, SENTINEL);
4562  if (!var && !ast_sockaddr_isnull(addr)) {
4563  var = ast_load_realtime("iaxusers", "name", username, "ipaddr", str_addr, "port", str_port, SENTINEL);
4564  if (!var)
4565  var = ast_load_realtime("iaxusers", "ipaddr", str_addr, "port", str_port, SENTINEL);
4566  }
4567  if (!var) { /* Last ditch effort */
4568  var = ast_load_realtime("iaxusers", "name", username, SENTINEL);
4569  /*!\note
4570  * If this one loaded something, then we need to ensure that the host
4571  * field matched. The only reason why we can't have this as a criteria
4572  * is because we only have the IP address and the host field might be
4573  * set as a name (and the reverse PTR might not match).
4574  */
4575  if (var) {
4576  for (tmp = var; tmp; tmp = tmp->next) {
4577  if (!strcasecmp(tmp->name, "host")) {
4578  struct ast_sockaddr *hostaddr = NULL;
4579 
4580  if (!ast_sockaddr_resolve(&hostaddr, tmp->value, PARSE_PORT_FORBID, AST_AF_UNSPEC)
4581  || ast_sockaddr_cmp_addr(hostaddr, addr)) {
4582  /* No match */
4583  ast_variables_destroy(var);
4584  var = NULL;
4585  }
4586  ast_free(hostaddr);
4587  break;
4588  }
4589  }
4590  }
4591  }
4592  if (!var)
4593  return NULL;
4594 
4595  tmp = var;
4596  while(tmp) {
4597  /* Make sure it's not a peer only... */
4598  if (!strcasecmp(tmp->name, "type")) {
4599  if (strcasecmp(tmp->value, "friend") &&
4600  strcasecmp(tmp->value, "user")) {
4601  return NULL;
4602  }
4603  }
4604  tmp = tmp->next;
4605  }
4606 
4607  user = build_user(username, var, NULL, !ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS));
4608 
4609  ast_variables_destroy(var);
4610 
4611  if (!user)
4612  return NULL;
4613 
4614  if (ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS)) {
4615  ast_set_flag64(user, IAX_RTCACHEFRIENDS);
4616  ao2_link(users, user);
4617  } else {
4618  ast_set_flag64(user, IAX_TEMPONLY);
4619  }
4620 
4621  return user;
4622 }
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:286
struct ast_variable * next
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
Structure for variables, used for configurations and for channel variables.
static struct iax2_user * build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
Create in-memory user structure from configuration.
Definition: chan_iax2.c:13289
list of users found in the config file
Socket address structure.
Definition: netsock2.h:97
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
Definition: netsock2.c:413
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Definition: netsock2.h:358
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define IAX_RTCACHEFRIENDS
Definition: chan_iax2.c:515
structure to hold users read from users.conf
static struct ast_flags globalflags
Definition: app_minivm.c:699
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
Definition: netsock2.c:280
#define IAX_TEMPONLY
Definition: chan_iax2.c:500
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
static int schedule_delivery ( struct iax_frame fr,
int  updatehistory,
int  fromtrunk,
unsigned int *  tsout 
)
static
Note
This function assumes fr->callno is locked
IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno] was valid before calling it, it may no longer be valid after calling it.

Definition at line 4276 of file chan_iax2.c.

References __do_deliver(), iax_frame::af, ast_codec_samples_count(), ast_debug, ast_format_get_sample_rate(), AST_FRAME_CNG, AST_FRAME_VOICE, ast_samp2tv(), ast_tv(), ast_tvadd(), ast_tvzero(), iax_frame::callno, ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_frame_subclass::format, ast_frame::frametype, IAX_USEJITTERBUF, chan_iax2_pvt::jb, jb_put(), JB_TYPE_CONTROL, JB_TYPE_SILENCE, JB_TYPE_VOICE, ast_frame::len, chan_iax2_pvt::rxcore, ast_frame::subclass, and iax_frame::ts.

4277 {
4278  int type, len;
4279  int ret;
4280  int needfree = 0;
4281 
4282  /*
4283  * Clear fr->af.data if there is no data in the buffer. Things
4284  * like AST_CONTROL_HOLD without a suggested music class must
4285  * have a NULL pointer.
4286  */
4287  if (!fr->af.datalen) {
4288  memset(&fr->af.data, 0, sizeof(fr->af.data));
4289  }
4290 
4291  /* Attempt to recover wrapped timestamps */
4292  unwrap_timestamp(fr);
4293 
4294  /* delivery time is sender's sent timestamp converted back into absolute time according to our clock */
4295  if ( !fromtrunk && !ast_tvzero(iaxs[fr->callno]->rxcore))
4296  fr->af.delivery = ast_tvadd(iaxs[fr->callno]->rxcore, ast_samp2tv(fr->ts, 1000));
4297  else {
4298 #if 0
4299  ast_debug(1, "schedule_delivery: set delivery to 0 as we don't have an rxcore yet, or frame is from trunk.\n");
4300 #endif
4301  fr->af.delivery = ast_tv(0,0);
4302  }
4303 
4304  type = JB_TYPE_CONTROL;
4305  len = 0;
4306 
4307  if(fr->af.frametype == AST_FRAME_VOICE) {
4308  type = JB_TYPE_VOICE;
4310  } else if(fr->af.frametype == AST_FRAME_CNG) {
4311  type = JB_TYPE_SILENCE;
4312  }
4313 
4314  if ( (!ast_test_flag64(iaxs[fr->callno], IAX_USEJITTERBUF)) ) {
4315  if (tsout)
4316  *tsout = fr->ts;
4317  __do_deliver(fr);
4318  return -1;
4319  }
4320 
4321  /* insert into jitterbuffer */
4322  /* TODO: Perhaps we could act immediately if it's not droppable and late */
4323  ret = jb_put(iaxs[fr->callno]->jb, fr, type, len, fr->ts,
4324  calc_rxstamp(iaxs[fr->callno],fr->ts));
4325  if (ret == JB_DROP) {
4326  needfree++;
4327  } else if (ret == JB_SCHED) {
4328  update_jbsched(iaxs[fr->callno]);
4329  }
4330  if (tsout)
4331  *tsout = fr->ts;
4332  if (needfree) {
4333  /* Free our iax frame */
4334  iax2_frame_free(fr);
4335  return -1;
4336  }
4337  return 0;
4338 }
struct ast_frame af
Definition: parser.h:142
struct timeval rxcore
Definition: chan_iax2.c:798
#define IAX_USEJITTERBUF
Definition: chan_iax2.c:503
enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now)
queue a frame
Definition: jitterbuf.c:525
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:117
jitterbuf * jb
Definition: chan_iax2.c:800
struct ast_frame_subclass subclass
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:282
unsigned int ast_codec_samples_count(struct ast_frame *frame)
Get the number of samples contained within a frame.
Definition: codec.c:379
#define ast_debug(level,...)
Log a DEBUG message.
static int __do_deliver(void *data)
Definition: chan_iax2.c:3387
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
unsigned short callno
Definition: parser.h:100
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
unsigned int ts
Definition: parser.h:110
union ast_frame::@224 data
struct timeval delivery
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
enum ast_frame_type frametype
struct ast_format * format
static int send_command_final ( struct chan_iax2_pvt i,
char  type,
int  command,
unsigned int  ts,
const unsigned char *  data,
int  datalen,
int  seqno 
)
static
Note
Since this function calls iax2_predestroy() -> iax2_queue_hangup(), the pvt struct for the given call number may disappear during its execution.

Definition at line 7856 of file chan_iax2.c.

References chan_iax2_pvt::callno, and iax2_predestroy().

Referenced by authenticate_request(), and update_registry().

7857 {
7858  int call_num = i->callno;
7859  /* It is assumed that the callno has already been locked */
7860  iax2_predestroy(i->callno);
7861  if (!iaxs[call_num])
7862  return -1;
7863  return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 1);
7864 }
static int iax2_predestroy(int callno)
Definition: chan_iax2.c:3483
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
unsigned short callno
Definition: chan_iax2.c:782
static int update_registry ( struct ast_sockaddr addr,
int  callno,
char *  devtype,
int  fd,
unsigned short  refresh 
)
static
Precondition
iaxsl[callno] is locked
Note
Since this function calls send_command_final(), the pvt struct for the given call number may disappear while executing this function.

Definition at line 9144 of file chan_iax2.c.

References ast_app_inboxcount(), ast_db_del(), ast_db_put(), AST_DEVICE_UNAVAILABLE, AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_endpoint_blob_publish(), AST_ENDPOINT_OFFLINE, AST_ENDPOINT_ONLINE, ast_endpoint_set_state(), ast_endpoint_state_type(), AST_FRAME_IAX, ast_json_pack(), ast_json_unref(), ast_mwi_state_cache(), ast_mwi_state_type(), AST_SCHED_DEL, ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_isnull(), ast_sockaddr_port, ast_sockaddr_stringify(), ast_sockaddr_stringify_addr(), ast_strdupa, iax2_peer::cid_name, iax2_peer::cid_num, iax2_peer::endpoint, iax2_peer::expire, iax2_peer::expiry, find_peer(), IAX_COMMAND_REGACK, IAX_HASCALLERID, IAX_IE_APPARENT_ADDR, IAX_IE_CALLING_NAME, IAX_IE_CALLING_NUMBER, IAX_IE_DATETIME, IAX_IE_FIRMWAREVER, IAX_IE_MSGCOUNT, IAX_IE_REFRESH, IAX_IE_USERNAME, IAX_RTCACHEFRIENDS, IAX_RTUPDATE, IAX_TEMPONLY, iax2_peer::mailbox, iax2_peer::maxcallno, ast_mwi_state::new_msgs, ast_mwi_state::old_msgs, RAII_VAR, refresh, send_command_final(), iax2_peer::sockfd, stasis_cache_get(), stasis_message_data(), and iax2_peer::zonetag.

9145 {
9146 
9147  /* Called from IAX thread only, with proper iaxsl lock */
9148  struct iax_ie_data ied = {
9149  .pos = 0,
9150  };
9151  struct iax2_peer *p;
9152  int msgcount;
9153  char data[80];
9154  uint16_t version;
9155  const char *peer_name;
9156  int res = -1;
9157  char *str_addr;
9158 
9159  peer_name = ast_strdupa(iaxs[callno]->peer);
9160 
9161  /* SLD: Another find_peer call during registration - this time when we are really updating our registration */
9162  ast_mutex_unlock(&iaxsl[callno]);
9163  if (!(p = find_peer(peer_name, 1))) {
9164  ast_mutex_lock(&iaxsl[callno]);
9165  ast_log(LOG_WARNING, "No such peer '%s'\n", peer_name);
9166  return -1;
9167  }
9168  ast_mutex_lock(&iaxsl[callno]);
9169  if (!iaxs[callno])
9170  goto return_unref;
9171 
9172  if (ast_test_flag64((&globalflags), IAX_RTUPDATE) && (ast_test_flag64(p, IAX_TEMPONLY|IAX_RTCACHEFRIENDS))) {
9173  if (!ast_sockaddr_isnull(addr)) {
9174  time_t nowtime;
9175  time(&nowtime);
9176  realtime_update_peer(peer_name, addr, nowtime);
9177  } else {
9178  realtime_update_peer(peer_name, addr, 0);
9179  }
9180  }
9181 
9182  /* treat an unspecified refresh interval as the minimum */
9183  if (!refresh) {
9184  refresh = min_reg_expire;
9185  }
9186  if (refresh > max_reg_expire) {
9187  ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
9188  p->name, max_reg_expire, refresh);
9189  p->expiry = max_reg_expire;
9190  } else if (refresh < min_reg_expire) {
9191  ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
9192  p->name, min_reg_expire, refresh);
9193  p->expiry = min_reg_expire;
9194  } else {
9195  p->expiry = refresh;
9196  }
9197 
9198  if (ast_sockaddr_cmp(&p->addr, addr)) {
9199  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
9200 
9201  if (iax2_regfunk) {
9202  iax2_regfunk(p->name, 1);
9203  }
9204 
9205  /* modify entry in peercnts table as _not_ registered */
9206  peercnt_modify((unsigned char) 0, 0, &p->addr);
9207 
9208  /* Stash the IP address from which they registered */
9209  ast_sockaddr_copy(&p->addr, addr);
9210 
9211  str_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
9212 
9213  snprintf(data, sizeof(data), "%s:%d", ast_sockaddr_stringify(addr), p->expiry);
9214 
9215  if (!ast_test_flag64(p, IAX_TEMPONLY) && !ast_sockaddr_isnull(addr)) {
9216  ast_db_put("IAX/Registry", p->name, data);
9217  ast_verb(3, "Registered IAX2 '%s' (%s) at %s\n",
9218  p->name,
9219  ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED",
9220  ast_sockaddr_stringify(addr));
9222  blob = ast_json_pack("{s: s, s: s, s: i}",
9223  "peer_status", "Registered",
9224  "address", str_addr,
9225  "port", ast_sockaddr_port(addr));
9226  register_peer_exten(p, 1);
9227  ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "IAX2/%s", p->name); /* Activate notification */
9228  } else if (!ast_test_flag64(p, IAX_TEMPONLY)) {
9229  ast_verb(3, "Unregistered IAX2 '%s' (%s)\n",
9230  p->name,
9231  ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED");
9233  blob = ast_json_pack("{s: s}",
9234  "peer_status", "Unregistered");
9235  register_peer_exten(p, 0);
9236  ast_db_del("IAX/Registry", p->name);
9237  ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "IAX2/%s", p->name); /* Activate notification */
9238  }
9239 
9241 
9242  /* Update the host */
9243  /* Verify that the host is really there */
9244  iax2_poke_peer(p, callno);
9245  }
9246 
9247  /* modify entry in peercnts table as registered */
9248  if (p->maxcallno) {
9249  peercnt_modify((unsigned char) 1, p->maxcallno, &p->addr);
9250  }
9251 
9252  /* Make sure our call still exists, an INVAL at the right point may make it go away */
9253  if (!iaxs[callno]) {
9254  res = -1;
9255  goto return_unref;
9256  }
9257 
9258  /* Store socket fd */
9259  p->sockfd = fd;
9260  /* Setup the expiry */
9261  if (p->expire > -1) {
9262  if (!AST_SCHED_DEL(sched, p->expire)) {
9263  p->expire = -1;
9264  peer_unref(p);
9265  }
9266  }
9267 
9268  if (p->expiry && !ast_sockaddr_isnull(addr)) {
9269  p->expire = iax2_sched_add(sched, (p->expiry + 10) * 1000, expire_registry, peer_ref(p));
9270  if (p->expire == -1)
9271  peer_unref(p);
9272  }
9273  iax_ie_append_str(&ied, IAX_IE_USERNAME, p->name);
9274  iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime(p->zonetag));
9275  if (!ast_sockaddr_isnull(addr)) {
9276  struct ast_sockaddr peer_addr;
9277 
9278  ast_sockaddr_copy(&peer_addr, &p->addr);
9279 
9280  iax_ie_append_short(&ied, IAX_IE_REFRESH, p->expiry);
9281  iax_ie_append_addr(&ied, IAX_IE_APPARENT_ADDR, &peer_addr);
9282  if (!ast_strlen_zero(p->mailbox)) {
9283  int new, old;
9284  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
9285 
9287  if (msg) {
9288  struct ast_mwi_state *mwi_state = stasis_message_data(msg);
9289  new = mwi_state->new_msgs;
9290  old = mwi_state->old_msgs;
9291  } else { /* Fall back on checking the mailbox directly */
9292  ast_app_inboxcount(p->mailbox, &new, &old);
9293  }
9294 
9295  if (new > 255) {
9296  new = 255;
9297  }
9298  if (old > 255) {
9299  old = 255;
9300  }
9301  msgcount = (old << 8) | new;
9302 
9303  iax_ie_append_short(&ied, IAX_IE_MSGCOUNT, msgcount);
9304  }
9305  if (ast_test_flag64(p, IAX_HASCALLERID)) {
9306  iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, p->cid_num);
9307  iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, p->cid_name);
9308  }
9309  }
9310  if (iax_firmware_get_version(devtype, &version)) {
9311  iax_ie_append_short(&ied, IAX_IE_FIRMWAREVER, version);
9312  }
9313 
9314  res = 0;
9315 
9316 return_unref:
9317  peer_unref(p);
9318 
9319  return res ? res : send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGACK, 0, ied.buf, ied.pos, -1);
9320 }
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:286
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
static struct iax2_peer * find_peer(const char *name, int realtime)
Definition: chan_iax2.c:2056
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
int expire
Definition: chan_iax2.c:614
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:167
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int sockfd
Definition: chan_iax2.c:604
#define IAX_IE_DATETIME
Definition: iax2.h:161
#define IAX_IE_FIRMWAREVER
Definition: iax2.h:164
Definition: sched.c:76
int callno
Definition: chan_iax2.c:619
void ast_endpoint_set_state(struct ast_endpoint *endpoint, enum ast_endpoint_state state)
Updates the state of the given endpoint.
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
Socket address structure.
Definition: netsock2.h:97
#define IAX_IE_CALLING_NUMBER
Definition: iax2.h:132
uint16_t maxcallno
Definition: chan_iax2.c:628
int expiry
Definition: chan_iax2.c:615
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:517
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:510
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define IAX_HASCALLERID
Definition: chan_iax2.c:498
#define IAX_RTCACHEFRIENDS
Definition: chan_iax2.c:515
const ast_string_field zonetag
Definition: chan_iax2.c:599
struct stasis_cache * ast_mwi_state_cache(void)
Backend cache for ast_mwi_topic_cached().
Definition: mwi.c:94
#define IAX_IE_CALLING_NAME
Definition: iax2.h:134
int new_msgs
Definition: mwi.h:459
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_message_type * ast_endpoint_state_type(void)
Message type for endpoint state changes.
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
#define IAX_IE_MSGCOUNT
Definition: iax2.h:154
struct ast_endpoint * endpoint
Definition: chan_iax2.c:635
static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int)
Definition: chan_iax2.c:7856
const ast_string_field cid_num
Definition: chan_iax2.c:599
void ast_endpoint_blob_publish(struct ast_endpoint *endpoint, struct stasis_message_type *type, struct ast_json *blob)
Creates and publishes a ast_endpoint_blob message.
#define IAX_IE_REFRESH
Definition: iax2.h:149
int old_msgs
Definition: mwi.h:460
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:476
#define IAX_IE_USERNAME
Definition: iax2.h:136
const ast_string_field mailbox
Definition: chan_iax2.c:599
struct stasis_message * stasis_cache_get(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
Retrieve an item from the cache for the ast_eid_default entity.
Definition: stasis_cache.c:686
Abstract JSON element (object, array, string, int, ...).
const ast_string_field cid_name
Definition: chan_iax2.c:599
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:342
#define IAX_IE_APPARENT_ADDR
Definition: iax2.h:148
The structure that contains MWI state.
Definition: mwi.h:455
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
static struct ast_flags globalflags
Definition: app_minivm.c:699
#define IAX_RTUPDATE
Definition: chan_iax2.c:516
int ast_app_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs)
Determine number of new/old messages in a mailbox.
Definition: main/app.c:604
unsigned int refresh
#define IAX_TEMPONLY
Definition: chan_iax2.c:500
static int user_cmp_cb ( void *  obj,
void *  arg,
int  flags 
)
static
Note
The only member of the user passed here guaranteed to be set is the name field

Definition at line 2043 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, and OBJ_KEY.

2044 {
2045  struct iax2_user *user = obj, *user2 = arg;
2046  const char *name = arg;
2047 
2048  return !strcmp(user->name, flags & OBJ_KEY ? name : user2->name) ?
2049  CMP_MATCH | CMP_STOP : 0;
2050 }
#define OBJ_KEY
Definition: astobj2.h:1151
structure to hold users read from users.conf
static int user_hash_cb ( const void *  obj,
const int  flags 
)
static
Note
The only member of the user passed here guaranteed to be set is the name field

Definition at line 2032 of file chan_iax2.c.

References ast_str_hash(), and OBJ_KEY.

2033 {
2034  const struct iax2_user *user = obj;
2035  const char *name = obj;
2036 
2037  return ast_str_hash(flags & OBJ_KEY ? name : user->name);
2038 }
#define OBJ_KEY
Definition: astobj2.h:1151
structure to hold users read from users.conf
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259
static int wait_for_peercallno ( struct chan_iax2_pvt pvt)
static
Note
expects the pvt to be locked

Definition at line 5425 of file chan_iax2.c.

References chan_iax2_pvt::callno, and chan_iax2_pvt::peercallno.

5426 {
5427  unsigned short callno = pvt->callno;
5428 
5429  if (!pvt->peercallno) {
5430  /* We don't know the remote side's call number, yet. :( */
5431  int count = 10;
5432  while (count-- && pvt && !pvt->peercallno) {
5433  DEADLOCK_AVOIDANCE(&iaxsl[callno]);
5434  pvt = iaxs[callno];
5435  }
5436  if (!pvt || !pvt->peercallno) {
5437  return -1;
5438  }
5439  }
5440 
5441  return 0;
5442 }
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
unsigned short peercallno
Definition: chan_iax2.c:786
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
unsigned short callno
Definition: chan_iax2.c:782

Variable Documentation

struct stasis_subscription* acl_change_sub
static

subscription id for ACL change events

Definition at line 328 of file chan_iax2.c.

struct ao2_container* callno_limits
static

Table containing custom callno limit rules for a range of ip addresses.

Definition at line 1009 of file chan_iax2.c.

struct call_number_pool callno_pool
static

table of available call numbers

Definition at line 966 of file chan_iax2.c.

struct call_number_pool callno_pool_trunk
static

table of available trunk call numbers

Definition at line 969 of file chan_iax2.c.

struct ao2_container* calltoken_ignores
static

Table containing ip addresses not requiring calltoken validation

Definition at line 1012 of file chan_iax2.c.

struct { ... } frame_queue[IAX_MAX_CALLS]

a list of frames that may need to be retransmitted

Note
The contents of this list do not need to be explicitly destroyed on module unload. This is because all active calls are destroyed, and all frames in this queue will get destroyed as a part of that process.
Contents protected by the iaxsl[] locks
int global_max_trunk_mtu
static

Maximum MTU, 0 if not used

Definition at line 317 of file chan_iax2.c.

Referenced by handle_cli_iax2_set_mtu(), and set_config().

uint16_t global_maxcallno_nonval
static

Total num of call numbers allowed to be allocated without calltoken validation

Definition at line 1021 of file chan_iax2.c.

const struct ast_datastore_info iax2_variable_datastore_info
static
Initial value:
= {
.type = "IAX2_VARIABLE",
.duplicate = iax2_dup_variable_datastore,
.destroy = iax2_free_variable_datastore,
}

Definition at line 1569 of file chan_iax2.c.

struct ao2_container* iax_peercallno_pvts
static

Another container of iax2_pvt structures.

Active IAX2 pvt structs are also stored in this container, if they are a part of an active call where we know the remote side's call number. The reason for this is that incoming media frames do not contain our call number. So, instead of having to iterate the entire iaxs array, we use this container to look up calls where the remote side is using a given call number.

Definition at line 1202 of file chan_iax2.c.

struct ao2_container* iax_transfercallno_pvts
static

Another container of iax2_pvt structures.

Active IAX2 pvt structs used during transfering a call are stored here.

Definition at line 1218 of file chan_iax2.c.

struct ast_custom_function iaxpeer_function
static
Initial value:
= {
.name = "IAXPEER",
.read = function_iaxpeer,
}

Definition at line 14624 of file chan_iax2.c.

struct chan_iax2_pvt* iaxs[IAX_MAX_CALLS]
static

an array of iax2 pvt structures

The container for active chan_iax2_pvt structures is implemented as an array for extremely quick direct access to the correct pvt structure based on the local call number. The local call number is used as the index into the array where the associated pvt structure is stored.

Definition at line 1173 of file chan_iax2.c.

ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
static

chan_iax2_pvt structure locks

These locks are used when accessing a pvt structure in the iaxs array. The index used here is the same as used in the iaxs array. It is the local call number for the associated pvt struct.

Definition at line 1211 of file chan_iax2.c.

struct ast_custom_function iaxvar_function
static
Initial value:
= {
.name = "IAXVAR",
.read = acf_iaxvar_read,
.write = acf_iaxvar_write,
}

Definition at line 10198 of file chan_iax2.c.

struct stasis_subscription* network_change_sub
static

subscription id for network change events

Definition at line 327 of file chan_iax2.c.

struct ast_netsock_list* outsock
static

used if sourceaddress specified and bindaddr == INADDR_ANY

Definition at line 367 of file chan_iax2.c.

struct ao2_container* peercnts
static

Table containing peercnt objects for every ip address consuming a callno

Definition at line 1006 of file chan_iax2.c.

int trunk_nmaxmtu
static

Trunk MTU statistics

Definition at line 318 of file chan_iax2.c.

Referenced by set_config().