Wed Aug 11 01:07:09 2010

Asterisk developer's documentation


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 <sys/signal.h>
#include <signal.h>
#include <strings.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/cdr.h"
#include "asterisk/crypto.h"
#include "asterisk/acl.h"
#include "asterisk/manager.h"
#include "asterisk/callerid.h"
#include "asterisk/app.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/aes.h"
#include "asterisk/dnsmgr.h"
#include "asterisk/devicestate.h"
#include "asterisk/netsock.h"
#include "asterisk/stringfields.h"
#include "asterisk/linkedlists.h"
#include "asterisk/event.h"
#include "asterisk/astobj2.h"
#include "asterisk/timing.h"
#include "iax2.h"
#include "iax2-parser.h"
#include "iax2-provision.h"
#include "jitterbuf.h"

Go to the source code of this file.

Data Structures

struct  active_list
struct  addr_range
struct  callno_entry
struct  chan_iax2_pvt
struct  create_addr_info
struct  dpcache
struct  dpreq_data
struct  dynamic_list
struct  firmwares
struct  frame_queue
 a list of frames that may need to be retransmitted More...
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_dual
struct  iax_firmware
struct  iax_rr
struct  idle_list
struct  parsed_dial_string
struct  peercnt
struct  registrations
struct  signaling_queue
struct  signaling_queue_entry
struct  tpeers

Defines

#define ACN_FORMAT1   "%-20.25s %4d %4d %4d %5d %3d %5d %4d %6d %4d %4d %5d %3d %5d %4d %6d %s%s %4s%s\n"
#define ACN_FORMAT2   "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %s%s %s%s\n"
#define CALLNO_TO_PTR(a)   ((void *)(unsigned long)(a))
#define CALLTOKEN_HASH_FORMAT   "%s%d%u%d"
#define CALLTOKEN_IE_FORMAT   "%u?%s"
#define DEBUG_SCHED_MULTITHREAD
#define DEBUG_SUPPORT
#define DEFAULT_CONTEXT   "default"
#define DEFAULT_DROP   3
#define DEFAULT_FREQ_NOTOK   10 * 1000
#define DEFAULT_FREQ_OK   60 * 1000
#define DEFAULT_MAX_THREAD_COUNT   100
#define DEFAULT_MAXMS   2000
#define DEFAULT_RETRY_TIME   1000
#define DEFAULT_THREAD_COUNT   10
#define DEFAULT_TRUNKDATA   640 * 10
#define FORMAT   "%-20.20s %-15.15s %-10.10s %5.5d/%5.5d %5.5d/%5.5d %-5.5dms %-4.4dms %-4.4dms %-6.6s %s%s %3s%s\n"
#define FORMAT   "%-20.20s %-6.6s %-10.10s %-20.20s %8d %s\n"
#define FORMAT   "%-15.15s %-15.15s %s %-15.15s %-5d%s %s %-10s%s"
#define FORMAT   "%-15.15s %-20.20s %-15.15s %-15.15s %-5.5s %-5.10s\n"
#define FORMAT2   "%-20.20s %-15.15s %-10.10s %-11.11s %-11.11s %-7.7s %-6.6s %-6.6s %s %s %9s\n"
#define FORMAT2   "%-20.20s %-6.6s %-10.10s %-20.20s %8.8s %s\n"
#define FORMAT2   "%-15.15s %-15.15s %s %-15.15s %-8s %s %-10s%s"
#define FORMAT2   "%-15.15s %-20.20s %-15.15d %-15.15s %-5.5s %-5.10s\n"
#define FORMATB   "%-20.20s %-15.15s %-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_CALLENCRYPTED(pvt)   (ast_test_flag(pvt, IAX_ENCRYPTED) && ast_test_flag(pvt, IAX_KEYPOPULATED))
#define IAX_CAPABILITY_FULLBANDWIDTH   0xFFFF
#define IAX_CAPABILITY_LOWBANDWIDTH
#define IAX_CAPABILITY_LOWFREE
#define IAX_CAPABILITY_MEDBANDWIDTH
#define IAX_DEBUGDIGEST(msg, key)
#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
#define PTR_TO_CALLNO(a)   ((unsigned short)(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

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  calltoken_peer_enum { CALLTOKEN_DEFAULT = 0, CALLTOKEN_YES = 1, CALLTOKEN_AUTO = 2, CALLTOKEN_NO = 3 }
 Call token validation settings. More...
enum  iax2_flags {
  IAX_HASCALLERID = (1 << 0), IAX_DELME = (1 << 1), IAX_TEMPONLY = (1 << 2), IAX_TRUNK = (1 << 3),
  IAX_NOTRANSFER = (1 << 4), IAX_USEJITTERBUF = (1 << 5), IAX_DYNAMIC = (1 << 6), IAX_SENDANI = (1 << 7),
  IAX_ALREADYGONE = (1 << 9), IAX_PROVISION = (1 << 10), IAX_QUELCH = (1 << 11), IAX_ENCRYPTED = (1 << 12),
  IAX_KEYPOPULATED = (1 << 13), IAX_CODEC_USER_FIRST = (1 << 14), IAX_CODEC_NOPREFS = (1 << 15), IAX_CODEC_NOCAP = (1 << 16),
  IAX_RTCACHEFRIENDS = (1 << 17), IAX_RTUPDATE = (1 << 18), IAX_RTAUTOCLEAR = (1 << 19), IAX_FORCEJITTERBUF = (1 << 20),
  IAX_RTIGNOREREGEXPIRE = (1 << 21), IAX_TRUNKTIMESTAMPS = (1 << 22), IAX_TRANSFERMEDIA = (1 << 23), IAX_MAXAUTHREQ = (1 << 24),
  IAX_DELAYPBXSTART = (1 << 25), IAX_ALLOWFWDOWNLOAD = (1 << 26), IAX_IMMEDIATE = (1 << 27), IAX_FORCE_ENCRYPT = (1 << 28),
  IAX_SHRINKCALLERID = (1 << 29)
}
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 sockaddr_in *sin, 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 manager, int fd, struct mansession *s, int argc, char *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 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 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_channel * ast_iax2_new (int callno, int state, int capability)
 Create new call, interface with the PBX core.
static int attempt_transmit (const void *data)
static int auth_fail (int callno, int failcode)
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 sockaddr_in *sin, struct chan_iax2_pvt *pvt)
static int authenticate_reply (struct chan_iax2_pvt *p, struct sockaddr_in *sin, 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_context * build_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_peer * build_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_user * build_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 callno_hash (const void *obj, const int flags)
static int calltoken_required (struct sockaddr_in *sin, const char *name, int subclass)
static int check_access (int callno, struct sockaddr_in *sin, struct iax_ies *ies)
static int check_provisioning (struct sockaddr_in *sin, int sockfd, char *si, unsigned int ver)
static int check_srcaddr (struct sockaddr *sa, socklen_t salen)
 Check if address can be used as packet source.
static void cleanup_thread_list (void *head)
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, int 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 (int 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 sockaddr_in *sin, 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.
static void delete_users (void)
static void destroy_firmware (struct iax_firmware *cur)
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_dpcache * find_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 sockaddr_in *sin, int new, int sockfd, int full_frame)
static int find_callno_locked (unsigned short callno, unsigned short dcallno, struct sockaddr_in *sin, int new, int sockfd, int full_frame)
static struct iax2_thread * find_idle_thread (void)
static struct iax2_peer * find_peer (const char *name, int realtime)
static struct iax2_trunk_peer * find_tpeer (struct sockaddr_in *sin, int fd)
static struct iax2_user * find_user (const char *name)
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 struct callno_entry * get_unused_callno (int trunk, int validated)
static int handle_call_token (struct ast_iax2_full_hdr *fh, struct iax_ies *ies, struct sockaddr_in *sin, 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 sockaddr_in *sin, int callno)
 Acknowledgment received for OUR registration.
static int attribute_pure iax2_allow_new (int frametype, int subclass, int inbound)
static void iax2_ami_channelupdate (struct chan_iax2_pvt *pvt)
 Send manager event at call setup to link between Asterisk channel name and IAX2 call identifiers.
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 enum ast_bridge_result iax2_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
static int iax2_call (struct ast_channel *c, 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 unsigned int iax2_datetime (const char *tz)
static void iax2_destroy (int callno)
static void iax2_destroy_helper (struct chan_iax2_pvt *pvt)
static int iax2_devicestate (void *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 *)
static int iax2_getpeername (struct sockaddr_in sin, char *host, int len)
static int iax2_getpeertrunk (struct sockaddr_in sin)
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_key_rotate (const void *vpvt)
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_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, void *data)
static int iax2_provision (struct sockaddr_in *end, int sockfd, char *dest, const char *template, int force)
static int iax2_queue_control_data (int callno, enum ast_control_frame_type control, const void *data, size_t datalen)
 Queue a control frame on the ast_channel owner.
static int iax2_queue_frame (int callno, struct ast_frame *f)
 Queue a frame to a call's owning asterisk channel.
static int iax2_queue_hangup (int callno)
 Queue a hangup frame on the ast_channel owner.
static struct ast_frame * iax2_read (struct ast_channel *c)
static int iax2_register (const char *value, int lineno)
static struct ast_channel * iax2_request (const char *type, int format, void *data, int *cause)
static int iax2_sched_add (struct ast_sched_thread *st, int when, ast_sched_cb callback, const void *data)
static int iax2_sched_replace (int id, struct ast_sched_thread *st, 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_start_transfer (unsigned short callno0, unsigned short callno1, int mediaonly)
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 int iax_check_version (char *dev)
static void iax_debug_output (const char *data)
static void iax_error_output (const char *data)
static int iax_firmware_append (struct iax_ie_data *ied, const unsigned char *dev, unsigned int desc)
static void iax_outputframe (struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct sockaddr_in *sin, int datalen)
static int iax_park (struct ast_channel *chan1, struct ast_channel *chan2)
static void * iax_park_thread (void *stuff)
static struct iax_frame * iaxfrdup2 (struct iax_frame *fr)
static void insert_idle_thread (struct iax2_thread *thread)
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 IAX2 module, load configuraiton ---.
static int load_objects (void)
static void lock_both (unsigned short callno0, unsigned short callno1)
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 sockaddr_in *sin, 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 void mwi_event_cb (const struct ast_event *event, void *userdata)
static void * network_thread (void *ignore)
static struct chan_iax2_pvt * new_iax (struct sockaddr_in *sin, const char *host)
static void parse_dial_string (char *data, struct parsed_dial_string *pds)
 Parses an IAX dial string into its component parts.
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_peer * peer_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_peer * peer_unref (struct iax2_peer *peer)
static int peercnt_add (struct sockaddr_in *sin)
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 sockaddr_in *sin)
static void peercnt_remove (struct peercnt *peercnt)
static int peercnt_remove_by_addr (struct sockaddr_in *sin)
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 sockaddr_in *sin, unsigned short src, unsigned short dst, int sockfd)
static struct iax2_peer * realtime_peer (const char *peername, struct sockaddr_in *sin)
static void realtime_update_peer (const char *peername, struct sockaddr_in *sin, time_t regtime)
static struct iax2_user * realtime_user (const char *username, struct sockaddr_in *sin)
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 sockaddr_in *sin, struct iax_ies *ies)
 Verify inbound registration.
static int registry_authrequest (int callno)
static int registry_rerequest (struct iax_ies *ies, int callno, struct sockaddr_in *sin)
static char * regstate2str (int regstate)
static int reload (void)
static int reload_config (void)
static void reload_firmware (int unload)
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 sockaddr_in *sin, struct callno_entry *callno_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 sockaddr_in *sin, 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)
 Load configuration.
static void set_config_destroy (void)
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_meta (int packet_len, struct ast_iax2_meta_hdr *meta, struct sockaddr_in *sin, 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_trunk (struct iax_frame *f, struct sockaddr_in *sin, int sockfd)
static int try_firmware (char *s)
static int try_transfer (struct chan_iax2_pvt *pvt, struct iax_ies *ies)
static int uncompress_subclass (unsigned char csub)
static void unlink_peer (struct iax2_peer *peer)
static int unload_module (void)
static void unlock_both (unsigned short callno0, unsigned short callno1)
static void unwrap_timestamp (struct iax_frame *fr)
static void update_jbsched (struct chan_iax2_pvt *pvt)
static void update_max_nontrunk (void)
static void update_max_trunk (void)
static int update_packet (struct iax_frame *f)
static int update_registry (struct sockaddr_in *sin, 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_user * user_ref (struct iax2_user *user)
static struct iax2_user * user_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_DEFAULT , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, }
static char accountcode [AST_MAX_ACCOUNT_CODE]
static int adsi = 0
static int amaflags = 0
static struct ast_module_info * ast_module_info = &__mod_info
static int authdebug = 1
static int autokill = 0
static struct ao2_container * callno_limits
static struct ao2_container * callno_pool
static const unsigned int CALLNO_POOL_BUCKETS = 2699
static struct ao2_container * callno_pool_trunk
static struct ao2_container * calltoken_ignores
static struct ast_cli_entry cli_iax2 []
static struct sockaddr_in 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 int global_max_trunk_mtu
static uint16_t global_maxcallno
static uint16_t global_maxcallno_nonval
static int global_rtautoclear = 120
static struct ast_flags globalflags = { 0 }
static int iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH
static int iax2_encryption = 0
int(* iax2_regfunk )(const char *username, int onoff) = NULL
static struct ast_switch iax2_switch
static struct ast_channel_tech iax2_tech
static struct ast_datastore_info iax2_variable_datastore_info
static struct ao2_container * iax_peercallno_pvts
 Another container of iax2_pvt structures.
static struct ao2_container * iax_transfercallno_pvts
 Another container of iax2_pvt structures.
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
struct ast_custom_function iaxpeer_function
static struct chan_iax2_pvt * iaxs [IAX_MAX_CALLS+1]
 an array of iax2 pvt structures
static ast_mutex_t iaxsl [ARRAY_LEN(iaxs)]
 chan_iax2_pvt structure locks
static int iaxthreadcount = DEFAULT_THREAD_COUNT
static int iaxtrunkdebug = 0
static struct ast_custom_function iaxvar_function
static struct io_context * io
static int jittertargetextra = 40
static int lagrq_time = 10
static char language [MAX_LANGUAGE] = ""
static int last_authmethod = 0
static const 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 maxnontrunkcall = 1
static int maxtrunkcall = TRUNK_CALL_START
static int min_reg_expire
static char mohinterpret [MAX_MUSICCLASS]
static char mohsuggest [MAX_MUSICCLASS]
static struct ast_netsock_list * netsock
static pthread_t netthreadid = AST_PTHREADT_NULL
static struct ast_netsock_list * outsock
static char * papp = "IAX2Provision"
static struct ao2_container * peercnts
static struct ao2_container * peers
static int ping_time = 21
static struct ast_codec_pref prefs
struct {
   unsigned int   cos
   unsigned int   tos
} qos
static int randomcalltokendata
static char regcontext [AST_MAX_CONTEXT] = ""
static int resyncthreshold = 1000
static struct ast_sched_thread * sched
static int srvlookup = 0
static const char tdesc [] = "Inter Asterisk eXchange Driver (Ver 2)"
static int test_losspct = 0
static struct ast_timer * timer
static uint16_t total_nonval_callno_used = 0
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_container * users


Detailed Description

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

Author:
Mark Spencer <markster@digium.com>
See also
Todo:
Implement musicclass settings for IAX2 devices

Definition in file chan_iax2.c.


Define Documentation

#define ACN_FORMAT1   "%-20.25s %4d %4d %4d %5d %3d %5d %4d %6d %4d %4d %5d %3d %5d %4d %6d %s%s %4s%s\n"

Referenced by ast_cli_netstats().

#define ACN_FORMAT2   "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %s%s %s%s\n"

Referenced by ast_cli_netstats().

#define CALLNO_TO_PTR ( a   )     ((void *)(unsigned long)(a))

Definition at line 194 of file chan_iax2.c.

Referenced by ast_iax2_new(), iax2_call(), iax2_hangup(), and update_jbsched().

#define CALLTOKEN_HASH_FORMAT   "%s%d%u%d"

Referenced by handle_call_token().

#define CALLTOKEN_IE_FORMAT   "%u?%s"

Referenced by handle_call_token().

#define DEBUG_SCHED_MULTITHREAD

Definition at line 186 of file chan_iax2.c.

#define DEBUG_SUPPORT

Definition at line 202 of file chan_iax2.c.

#define DEFAULT_CONTEXT   "default"

Definition at line 221 of file chan_iax2.c.

Referenced by check_access(), handle_cli_iax2_show_users(), and reload_config().

#define DEFAULT_DROP   3

Definition at line 200 of file chan_iax2.c.

#define DEFAULT_FREQ_NOTOK   10 * 1000

Definition at line 292 of file chan_iax2.c.

Referenced by build_peer(), handle_response_peerpoke(), and sip_poke_noanswer().

#define DEFAULT_FREQ_OK   60 * 1000

Definition at line 291 of file chan_iax2.c.

Referenced by build_peer().

#define DEFAULT_MAX_THREAD_COUNT   100

Definition at line 197 of file chan_iax2.c.

#define DEFAULT_MAXMS   2000

Definition at line 290 of file chan_iax2.c.

Referenced by build_peer(), iax2_poke_peer(), reload_config(), and set_config().

#define DEFAULT_RETRY_TIME   1000

Definition at line 198 of file chan_iax2.c.

Referenced by __find_callno(), and complete_transfer().

#define DEFAULT_THREAD_COUNT   10

Definition at line 196 of file chan_iax2.c.

#define DEFAULT_TRUNKDATA   640 * 10

40ms, uncompressed linear * 10 channels

Definition at line 571 of file chan_iax2.c.

Referenced by iax2_trunk_queue().

#define FORMAT   "%-20.20s %-15.15s %-10.10s %5.5d/%5.5d %5.5d/%5.5d %-5.5dms %-4.4dms %-4.4dms %-6.6s %s%s %3s%s\n"

#define FORMAT   "%-20.20s %-6.6s %-10.10s %-20.20s %8d %s\n"

#define FORMAT   "%-15.15s %-15.15s %s %-15.15s %-5d%s %s %-10s%s"

#define FORMAT   "%-15.15s %-20.20s %-15.15s %-15.15s %-5.5s %-5.10s\n"

#define FORMAT2   "%-20.20s %-15.15s %-10.10s %-11.11s %-11.11s %-7.7s %-6.6s %-6.6s %s %s %9s\n"

#define FORMAT2   "%-20.20s %-6.6s %-10.10s %-20.20s %8.8s %s\n"

#define FORMAT2   "%-15.15s %-15.15s %s %-15.15s %-8s %s %-10s%s"

#define FORMAT2   "%-15.15s %-20.20s %-15.15d %-15.15s %-5.5s %-5.10s\n"

#define FORMATB   "%-20.20s %-15.15s %-10.10s %5.5d/%5.5d %5.5d/%5.5d [Native Bridged to ID=%5.5d]\n"

#define GAMMA   (0.01)

Definition at line 207 of file chan_iax2.c.

#define IAX2_TRUNK_PREFACE   (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr))

Definition at line 492 of file chan_iax2.c.

Referenced by iax2_trunk_queue().

#define IAX_CALLENCRYPTED ( pvt   )     (ast_test_flag(pvt, IAX_ENCRYPTED) && ast_test_flag(pvt, IAX_KEYPOPULATED))

Definition at line 295 of file chan_iax2.c.

Referenced by iax2_send(), iax2_start_transfer(), and socket_process().

#define IAX_CAPABILITY_FULLBANDWIDTH   0xFFFF

Definition at line 270 of file chan_iax2.c.

Referenced by cache_get_callno_locked(), and set_config().

#define IAX_CAPABILITY_LOWBANDWIDTH

Value:

Definition at line 281 of file chan_iax2.c.

Referenced by set_config().

#define IAX_CAPABILITY_LOWFREE

Value:

Definition at line 286 of file chan_iax2.c.

#define IAX_CAPABILITY_MEDBANDWIDTH

Definition at line 272 of file chan_iax2.c.

Referenced by set_config().

#define IAX_DEBUGDIGEST ( msg,
key   ) 

Definition at line 298 of file chan_iax2.c.

Referenced by iax2_key_rotate(), and socket_process().

#define MARK_IAX_SUBCLASS_TX   0x8000

Definition at line 579 of file chan_iax2.c.

Referenced by ast_cli_netstats(), handle_cli_iax2_show_channels(), and iax2_send().

#define MAX_JITTER_BUFFER   50

Definition at line 568 of file chan_iax2.c.

#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 826 of file chan_iax2.c.

Referenced by load_objects().

#define MAX_RETRY_TIME   10000

Definition at line 566 of file chan_iax2.c.

Referenced by __attempt_transmit(), and iax2_send().

#define MAX_TIMESTAMP_SKEW   160

maximum difference between actual and predicted ts for sending

Definition at line 573 of file chan_iax2.c.

Referenced by ast_rtp_raw_write(), calc_timestamp(), and calc_txpeerstamp().

#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.

Definition at line 216 of file chan_iax2.c.

Referenced by handle_cli_iax2_set_mtu(), and set_config().

#define MAX_TRUNKDATA   640 * 200

40ms, uncompressed linear * 200 channels

Definition at line 237 of file chan_iax2.c.

Referenced by set_config(), and set_config_destroy().

#define MAX_USER_BUCKETS   MAX_PEER_BUCKETS

Definition at line 830 of file chan_iax2.c.

Referenced by load_module(), and load_objects().

#define MEMORY_SIZE   100

Definition at line 199 of file chan_iax2.c.

#define MIN_JITTER_BUFFER   10

Definition at line 569 of file chan_iax2.c.

#define MIN_RETRY_TIME   100

Definition at line 565 of file chan_iax2.c.

Referenced by iax2_send().

#define MIN_REUSE_TIME   60

Definition at line 204 of file chan_iax2.c.

Referenced by make_trunk(), and sched_delay_remove().

#define PTR_TO_CALLNO ( a   )     ((unsigned short)(unsigned long)(a))

#define SCHED_MULTITHREADED

Definition at line 182 of file chan_iax2.c.

#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

Definition at line 576 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 886 of file chan_iax2.c.

00886      {
00887    /*! Extension exists */
00888    CACHE_FLAG_EXISTS      = (1 << 0),
00889    /*! Extension is nonexistent */
00890    CACHE_FLAG_NONEXISTENT = (1 << 1),
00891    /*! Extension can exist */
00892    CACHE_FLAG_CANEXIST    = (1 << 2),
00893    /*! Waiting to hear back response */
00894    CACHE_FLAG_PENDING     = (1 << 3),
00895    /*! Timed out */
00896    CACHE_FLAG_TIMEOUT     = (1 << 4),
00897    /*! Request transmitted */
00898    CACHE_FLAG_TRANSMITTED = (1 << 5),
00899    /*! Timeout */
00900    CACHE_FLAG_UNKNOWN     = (1 << 6),
00901    /*! Matchmore */
00902    CACHE_FLAG_MATCHMORE   = (1 << 7),
00903 };

anonymous enum

Enumerator:
NEW_PREVENT 
NEW_ALLOW 
NEW_FORCE 
NEW_ALLOW_CALLTOKEN_VALIDATED 

Definition at line 1823 of file chan_iax2.c.

01823      {
01824    /* do not allow a new call number, only search ones in use for match */
01825    NEW_PREVENT = 0,
01826    /* search for match first, then allow a new one to be allocated */
01827    NEW_ALLOW = 1,
01828    /* do not search for match, force a new call number */
01829    NEW_FORCE = 2,
01830    /* do not search for match, force a new call number.  Signifies call number
01831     * has been calltoken validated */
01832    NEW_ALLOW_CALLTOKEN_VALIDATED = 3,
01833 };

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 393 of file chan_iax2.c.

00393                          {
00394    /*! \brief Default calltoken required unless the ip is in the ignorelist */
00395    CALLTOKEN_DEFAULT = 0,
00396    /*! \brief Require call token validation. */
00397    CALLTOKEN_YES = 1,
00398    /*! \brief Require call token validation after a successful registration
00399     *         using call token validation occurs. */
00400    CALLTOKEN_AUTO = 2,
00401    /*! \brief Do not require call token validation. */
00402    CALLTOKEN_NO = 3,
00403 };

enum iax2_flags

Enumerator:
IAX_HASCALLERID  CallerID has been specified
IAX_DELME  Needs to be deleted
IAX_TEMPONLY  Temporary (realtime)
IAX_TRUNK  Treat as a trunk
IAX_NOTRANSFER  Don't native bridge
IAX_USEJITTERBUF  Use jitter buffer
IAX_DYNAMIC  dynamic peer
IAX_SENDANI  Send ANI along with CallerID
IAX_ALREADYGONE  Already disconnected
IAX_PROVISION  This is a provisioning request
IAX_QUELCH  Whether or not we quelch audio
IAX_ENCRYPTED  Whether we should assume encrypted tx/rx
IAX_KEYPOPULATED  Whether we have a key populated
IAX_CODEC_USER_FIRST  are we willing to let the other guy choose the codec?
IAX_CODEC_NOPREFS  Force old behaviour by turning off prefs
IAX_CODEC_NOCAP  only consider requested format and ignore capabilities
IAX_RTCACHEFRIENDS  let realtime stay till your reload
IAX_RTUPDATE  Send a realtime update
IAX_RTAUTOCLEAR  erase me on expire
IAX_FORCEJITTERBUF  Force jitterbuffer, even when bridged to a channel that can take jitter
IAX_RTIGNOREREGEXPIRE  When using realtime, ignore registration expiration
IAX_TRUNKTIMESTAMPS  Send trunk timestamps
IAX_TRANSFERMEDIA  When doing IAX2 transfers, transfer media only
IAX_MAXAUTHREQ  Maximum outstanding AUTHREQ restriction is in place
IAX_DELAYPBXSTART  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
IAX_ALLOWFWDOWNLOAD  Allow the FWDOWNL command?
IAX_IMMEDIATE  Allow immediate off-hook to extension s
IAX_FORCE_ENCRYPT  Forces call encryption, if encryption not possible hangup
IAX_SHRINKCALLERID  Turn on and off caller id shrinking

Definition at line 351 of file chan_iax2.c.

00351                 {
00352    IAX_HASCALLERID =    (1 << 0),   /*!< CallerID has been specified */
00353    IAX_DELME =    (1 << 1),   /*!< Needs to be deleted */
00354    IAX_TEMPONLY =    (1 << 2),   /*!< Temporary (realtime) */
00355    IAX_TRUNK =    (1 << 3),   /*!< Treat as a trunk */
00356    IAX_NOTRANSFER =  (1 << 4),   /*!< Don't native bridge */
00357    IAX_USEJITTERBUF =   (1 << 5),   /*!< Use jitter buffer */
00358    IAX_DYNAMIC =     (1 << 6),   /*!< dynamic peer */
00359    IAX_SENDANI =     (1 << 7),   /*!< Send ANI along with CallerID */
00360         /* (1 << 8) is currently unused due to the deprecation of an old option. Go ahead, take it! */
00361    IAX_ALREADYGONE = (1 << 9),   /*!< Already disconnected */
00362    IAX_PROVISION =      (1 << 10),  /*!< This is a provisioning request */
00363    IAX_QUELCH =      (1 << 11),  /*!< Whether or not we quelch audio */
00364    IAX_ENCRYPTED =      (1 << 12),  /*!< Whether we should assume encrypted tx/rx */
00365    IAX_KEYPOPULATED =   (1 << 13),  /*!< Whether we have a key populated */
00366    IAX_CODEC_USER_FIRST =  (1 << 14),  /*!< are we willing to let the other guy choose the codec? */
00367    IAX_CODEC_NOPREFS =     (1 << 15),  /*!< Force old behaviour by turning off prefs */
00368    IAX_CODEC_NOCAP =    (1 << 16),  /*!< only consider requested format and ignore capabilities*/
00369    IAX_RTCACHEFRIENDS =    (1 << 17),  /*!< let realtime stay till your reload */
00370    IAX_RTUPDATE =       (1 << 18),  /*!< Send a realtime update */
00371    IAX_RTAUTOCLEAR =    (1 << 19),  /*!< erase me on expire */ 
00372    IAX_FORCEJITTERBUF = (1 << 20),  /*!< Force jitterbuffer, even when bridged to a channel that can take jitter */ 
00373    IAX_RTIGNOREREGEXPIRE = (1 << 21),  /*!< When using realtime, ignore registration expiration */
00374    IAX_TRUNKTIMESTAMPS =   (1 << 22),  /*!< Send trunk timestamps */
00375    IAX_TRANSFERMEDIA =     (1 << 23),      /*!< When doing IAX2 transfers, transfer media only */
00376    IAX_MAXAUTHREQ =        (1 << 24),      /*!< Maximum outstanding AUTHREQ restriction is in place */
00377    IAX_DELAYPBXSTART =  (1 << 25),  /*!< Don't start a PBX on the channel until the peer sends us a
00378                        response, so that we've achieved a three-way handshake with
00379                        them before sending voice or anything else*/
00380    IAX_ALLOWFWDOWNLOAD =   (1 << 26),  /*!< Allow the FWDOWNL command? */
00381    IAX_IMMEDIATE =      (1 << 27),      /*!< Allow immediate off-hook to extension s */
00382    IAX_FORCE_ENCRYPT =  (1 << 28),      /*!< Forces call encryption, if encryption not possible hangup */
00383    IAX_SHRINKCALLERID = (1 << 29),   /*!< Turn on and off caller id shrinking */
00384 };

enum iax2_state

Enumerator:
IAX_STATE_STARTED 
IAX_STATE_AUTHENTICATED 
IAX_STATE_TBD 

Definition at line 340 of file chan_iax2.c.

00340                 {
00341    IAX_STATE_STARTED =        (1 << 0),
00342    IAX_STATE_AUTHENTICATED =  (1 << 1),
00343    IAX_STATE_TBD =            (1 << 2),
00344 };

Enumerator:
IAX_IOSTATE_IDLE 
IAX_IOSTATE_READY 
IAX_IOSTATE_PROCESSING 
IAX_IOSTATE_SCHEDREADY 

Definition at line 927 of file chan_iax2.c.

Enumerator:
IAX_THREAD_TYPE_POOL 
IAX_THREAD_TYPE_DYNAMIC 

Definition at line 934 of file chan_iax2.c.

00934                       {
00935    IAX_THREAD_TYPE_POOL,
00936    IAX_THREAD_TYPE_DYNAMIC,
00937 };

Enumerator:
REG_STATE_UNREGISTERED 
REG_STATE_REGSENT 
REG_STATE_AUTHSENT 
REG_STATE_REGISTERED 
REG_STATE_REJECTED 
REG_STATE_TIMEOUT 
REG_STATE_NOAUTH 

Definition at line 524 of file chan_iax2.c.

Enumerator:
TRANSFER_NONE 
TRANSFER_BEGIN 
TRANSFER_READY 
TRANSFER_RELEASED 
TRANSFER_PASSTHROUGH 
TRANSFER_MBEGIN 
TRANSFER_MREADY 
TRANSFER_MRELEASED 
TRANSFER_MPASSTHROUGH 
TRANSFER_MEDIA 
TRANSFER_MEDIAPASS 

Definition at line 534 of file chan_iax2.c.


Function Documentation

static void __attempt_transmit ( const void *  data  )  [static]

Definition at line 3317 of file chan_iax2.c.

References chan_iax2_pvt::addr, iax_frame::af, AST_CAUSE_DESTINATION_OUT_OF_ORDER, AST_CONTROL_HANGUP, AST_FRAME_CONTROL, AST_FRAME_IAX, ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), attempt_transmit(), iax_frame::callno, chan_iax2_pvt::error, f, iax_frame::final, ast_frame::frametype, ast_channel::hangupcause, iax2_destroy(), iax2_frame_free(), iax2_queue_frame(), iax2_sched_add(), IAX_COMMAND_TXREJ, IAX_DEFAULT_REG_EXPIRE, LOG_WARNING, MAX_RETRY_TIME, ast_channel::name, iax_frame::oseqno, chan_iax2_pvt::owner, iax2_registry::refresh, chan_iax2_pvt::reg, REG_STATE_TIMEOUT, iax2_registry::regstate, iax_frame::retrans, iax_frame::retries, iax_frame::retrytime, send_command(), send_packet(), ast_frame::subclass, iax_frame::transfer, iax_frame::ts, update_packet(), and iax2_registry::us.

Referenced by attempt_transmit().

03318 {
03319    /* Attempt to transmit the frame to the remote peer...
03320       Called without iaxsl held. */
03321    struct iax_frame *f = (struct iax_frame *)data;
03322    int freeme = 0;
03323    int callno = f->callno;
03324    /* Make sure this call is still active */
03325    if (callno) 
03326       ast_mutex_lock(&iaxsl[callno]);
03327    if (callno && iaxs[callno]) {
03328       if ((f->retries < 0) /* Already ACK'd */ ||
03329           (f->retries >= max_retries) /* Too many attempts */) {
03330             /* Record an error if we've transmitted too many times */
03331             if (f->retries >= max_retries) {
03332                if (f->transfer) {
03333                   /* Transfer timeout */
03334                   send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
03335                } else if (f->final) {
03336                   iax2_destroy(callno);
03337                } else {
03338                   if (iaxs[callno]->owner)
03339                      ast_log(LOG_WARNING, "Max retries exceeded to host %s on %s (type = %d, subclass = %d, ts=%d, seqno=%d)\n", ast_inet_ntoa(iaxs[f->callno]->addr.sin_addr),iaxs[f->callno]->owner->name , f->af.frametype, f->af.subclass, f->ts, f->oseqno);
03340                   iaxs[callno]->error = ETIMEDOUT;
03341                   if (iaxs[callno]->owner) {
03342                      struct ast_frame fr = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP, .data.uint32 = AST_CAUSE_DESTINATION_OUT_OF_ORDER };
03343                      /* Hangup the fd */
03344                      iax2_queue_frame(callno, &fr); /* XXX */
03345                      /* Remember, owner could disappear */
03346                      if (iaxs[callno] && iaxs[callno]->owner)
03347                         iaxs[callno]->owner->hangupcause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
03348                   } else {
03349                      if (iaxs[callno]->reg) {
03350                         memset(&iaxs[callno]->reg->us, 0, sizeof(iaxs[callno]->reg->us));
03351                         iaxs[callno]->reg->regstate = REG_STATE_TIMEOUT;
03352                         iaxs[callno]->reg->refresh = IAX_DEFAULT_REG_EXPIRE;
03353                      }
03354                      iax2_destroy(callno);
03355                   }
03356                }
03357 
03358             }
03359             freeme = 1;
03360       } else {
03361          /* Update it if it needs it */
03362          update_packet(f);
03363          /* Attempt transmission */
03364          send_packet(f);
03365          f->retries++;
03366          /* Try again later after 10 times as long */
03367          f->retrytime *= 10;
03368          if (f->retrytime > MAX_RETRY_TIME)
03369             f->retrytime = MAX_RETRY_TIME;
03370          /* Transfer messages max out at one second */
03371          if (f->transfer && (f->retrytime > 1000))
03372             f->retrytime = 1000;
03373          f->retrans = iax2_sched_add(sched, f->retrytime, attempt_transmit, f);
03374       }
03375    } else {
03376       /* Make sure it gets freed */
03377       f->retries = -1;
03378       freeme = 1;
03379    }
03380    if (callno)
03381       ast_mutex_unlock(&iaxsl[callno]);
03382    /* Do not try again */
03383    if (freeme) {
03384       /* Don't attempt delivery, just remove it from the queue */
03385       AST_LIST_LOCK(&frame_queue);
03386       AST_LIST_REMOVE(&frame_queue, f, list);
03387       AST_LIST_UNLOCK(&frame_queue);
03388       f->retrans = -1; /* this is safe because this is the scheduled function */
03389       /* Free the IAX frame */
03390       iax2_frame_free(f);
03391    }
03392 }

static void __auth_reject ( const void *  nothing  )  [static]

Definition at line 8616 of file chan_iax2.c.

References AST_CAUSE_FACILITY_NOT_SUBSCRIBED, AST_CAUSE_FACILITY_REJECTED, AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), iax_ie_data::buf, IAX_COMMAND_REGREJ, IAX_COMMAND_REJECT, iax_ie_append_byte(), iax_ie_append_str(), IAX_IE_CAUSE, IAX_IE_CAUSECODE, iax_ie_data::pos, and send_command_final().

Referenced by auth_reject().

08617 {
08618    /* Called from IAX thread only, without iaxs lock */
08619    int callno = (int)(long)(nothing);
08620    struct iax_ie_data ied;
08621    ast_mutex_lock(&iaxsl[callno]);
08622    if (iaxs[callno]) {
08623       memset(&ied, 0, sizeof(ied));
08624       if (iaxs[callno]->authfail == IAX_COMMAND_REGREJ) {
08625          iax_ie_append_str(&ied, IAX_IE_CAUSE, "Registration Refused");
08626          iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_REJECTED);
08627       } else if (iaxs[callno]->authfail == IAX_COMMAND_REJECT) {
08628          iax_ie_append_str(&ied, IAX_IE_CAUSE, "No authority found");
08629          iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_NOT_SUBSCRIBED);
08630       }
08631       send_command_final(iaxs[callno], AST_FRAME_IAX, iaxs[callno]->authfail, 0, ied.buf, ied.pos, -1);
08632    }
08633    ast_mutex_unlock(&iaxsl[callno]);
08634 }

static void __auto_congest ( const void *  nothing  )  [static]

Definition at line 4436 of file chan_iax2.c.

References AST_CONTROL_CONGESTION, AST_FRAME_CONTROL, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax2_queue_frame(), chan_iax2_pvt::initid, LOG_NOTICE, and PTR_TO_CALLNO.

Referenced by auto_congest().

04437 {
04438    int callno = PTR_TO_CALLNO(nothing);
04439    struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_CONGESTION };
04440    ast_mutex_lock(&iaxsl[callno]);
04441    if (iaxs[callno]) {
04442       iaxs[callno]->initid = -1;
04443       iax2_queue_frame(callno, &f);
04444       ast_log(LOG_NOTICE, "Auto-congesting call due to slow response\n");
04445    }
04446    ast_mutex_unlock(&iaxsl[callno]);
04447 }

static void __auto_hangup ( const void *  nothing  )  [static]

Definition at line 8665 of file chan_iax2.c.

References AST_CAUSE_NO_USER_RESPONSE, AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), iax_ie_data::buf, IAX_COMMAND_HANGUP, iax_ie_append_byte(), iax_ie_append_str(), IAX_IE_CAUSE, IAX_IE_CAUSECODE, iax_ie_data::pos, and send_command_final().

Referenced by auto_hangup().

08666 {
08667    /* Called from IAX thread only, without iaxs lock */
08668    int callno = (int)(long)(nothing);
08669    struct iax_ie_data ied;
08670    ast_mutex_lock(&iaxsl[callno]);
08671    if (iaxs[callno]) {
08672       memset(&ied, 0, sizeof(ied));
08673       iax_ie_append_str(&ied, IAX_IE_CAUSE, "Timeout");
08674       iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_NO_USER_RESPONSE);
08675       send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, ied.buf, ied.pos, -1);
08676    }
08677    ast_mutex_unlock(&iaxsl[callno]);
08678 }

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 3103 of file chan_iax2.c.

References iax_frame::af, ast_clear_flag, AST_FRFLAG_HAS_TIMING_INFO, ast_test_flag, iax_frame::callno, iax2_frame_free(), iax2_queue_frame(), IAX_ALREADYGONE, and iax_frame::retrans.

Referenced by __get_from_jb(), and schedule_delivery().

03104 {
03105    /* Just deliver the packet by using queueing.  This is called by
03106      the IAX thread with the iaxsl lock held. */
03107    struct iax_frame *fr = data;
03108    fr->retrans = -1;
03109    ast_clear_flag(&fr->af, AST_FRFLAG_HAS_TIMING_INFO);
03110    if (iaxs[fr->callno] && !ast_test_flag(iaxs[fr->callno], IAX_ALREADYGONE))
03111       iax2_queue_frame(fr->callno, &fr->af);
03112    /* Free our iax frame */
03113    iax2_frame_free(fr);
03114    /* And don't run again */
03115    return 0;
03116 }

static void __expire_registry ( const void *  data  )  [static]

Definition at line 8264 of file chan_iax2.c.

References iax2_peer::addr, ast_db_del(), ast_debug, AST_DEVICE_UNAVAILABLE, ast_devstate_changed(), ast_test_flag, EVENT_FLAG_SYSTEM, iax2_peer::expire, iax2_peer::expiry, iax2_regfunk, IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, IAX_RTUPDATE, IAX_TEMPONLY, manager_event, iax2_peer::name, peer_unref(), peercnt_modify(), realtime_update_peer(), register_peer_exten(), and unlink_peer().

Referenced by expire_registry().

08265 {
08266    struct iax2_peer *peer = (struct iax2_peer *) data;
08267 
08268    if (!peer)
08269       return;
08270 
08271    peer->expire = -1;
08272 
08273    ast_debug(1, "Expiring registration for peer '%s'\n", peer->name);
08274    if (ast_test_flag((&globalflags), IAX_RTUPDATE) && (ast_test_flag(peer, IAX_TEMPONLY|IAX_RTCACHEFRIENDS)))
08275       realtime_update_peer(peer->name, &peer->addr, 0);
08276    manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: IAX2\r\nPeer: IAX2/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
08277    /* modify entry in peercnts table as _not_ registered */
08278    peercnt_modify(0, 0, &peer->addr);
08279    /* Reset the address */
08280    memset(&peer->addr, 0, sizeof(peer->addr));
08281    /* Reset expiry value */
08282    peer->expiry = min_reg_expire;
08283    if (!ast_test_flag(peer, IAX_TEMPONLY))
08284       ast_db_del("IAX/Registry", peer->name);
08285    register_peer_exten(peer, 0);
08286    ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "IAX2/%s", peer->name); /* Activate notification */
08287    if (iax2_regfunk)
08288       iax2_regfunk(peer->name, 0);
08289 
08290    if (ast_test_flag(peer, IAX_RTAUTOCLEAR))
08291       unlink_peer(peer);
08292 
08293    peer_unref(peer);
08294 }

static int __find_callno ( unsigned short  callno,
unsigned short  dcallno,
struct sockaddr_in *  sin,
int  new,
int  sockfd,
int  return_locked,
int  check_dcallno 
) [static]

Definition at line 2581 of file chan_iax2.c.

References chan_iax2_pvt::addr, chan_iax2_pvt::amaflags, ao2_find, ao2_ref, ast_copy_flags, ast_debug, ast_inet_ntoa(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_string_field_set, callno_entry::callno, chan_iax2_pvt::callno, chan_iax2_pvt::callno_entry, DEFAULT_RETRY_TIME, chan_iax2_pvt::expiry, get_unused_callno(), iax2_getpeername(), iax2_sched_add(), IAX_FORCE_ENCRYPT, IAX_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_TRANSFERMEDIA, IAX_USEJITTERBUF, chan_iax2_pvt::lagid, LOG_WARNING, match(), NEW_ALLOW, new_iax(), OBJ_POINTER, parkinglot, chan_iax2_pvt::peercallno, peercnt_add(), peercnt_remove_by_addr(), chan_iax2_pvt::pingid, chan_iax2_pvt::pingtime, replace_callno(), send_lagrq(), send_ping(), chan_iax2_pvt::sockfd, store_by_peercallno(), chan_iax2_pvt::transfer, TRUNK_CALL_START, and update_max_nontrunk().

Referenced by find_callno(), and find_callno_locked().

02582 {
02583    int res = 0;
02584    int x;
02585    /* this call is calltoken validated as long as it is either NEW_FORCE
02586     * or NEW_ALLOW_CALLTOKEN_VALIDATED */
02587    int validated = (new > NEW_ALLOW) ? 1 : 0;
02588    char host[80];
02589 
02590    if (new <= NEW_ALLOW) {
02591       if (callno) {
02592          struct chan_iax2_pvt *pvt;
02593          struct chan_iax2_pvt tmp_pvt = {
02594             .callno = dcallno,
02595             .peercallno = callno,
02596             .transfercallno = callno,
02597             /* hack!! */
02598             .frames_received = check_dcallno,
02599          };
02600 
02601          memcpy(&tmp_pvt.addr, sin, sizeof(tmp_pvt.addr));
02602          /* this works for finding normal call numbers not involving transfering */ 
02603          if ((pvt = ao2_find(iax_peercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
02604             if (return_locked) {
02605                ast_mutex_lock(&iaxsl[pvt->callno]);
02606             }
02607             res = pvt->callno;
02608             ao2_ref(pvt, -1);
02609             pvt = NULL;
02610             return res;
02611          }
02612          /* this searches for transfer call numbers that might not get caught otherwise */
02613          memset(&tmp_pvt.addr, 0, sizeof(tmp_pvt.addr));
02614          memcpy(&tmp_pvt.transfer, sin, sizeof(tmp_pvt.transfer));
02615          if ((pvt = ao2_find(iax_transfercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
02616             if (return_locked) {
02617                ast_mutex_lock(&iaxsl[pvt->callno]);
02618             }
02619             res = pvt->callno;
02620             ao2_ref(pvt, -1);
02621             pvt = NULL;
02622             return res;
02623          }
02624       }
02625          /* This will occur on the first response to a message that we initiated,
02626        * such as a PING. */
02627       if (dcallno) {
02628          ast_mutex_lock(&iaxsl[dcallno]);
02629       }
02630       if (callno && dcallno && iaxs[dcallno] && !iaxs[dcallno]->peercallno && match(sin, callno, dcallno, iaxs[dcallno], check_dcallno)) {
02631          iaxs[dcallno]->peercallno = callno;
02632          res = dcallno;
02633          store_by_peercallno(iaxs[dcallno]);
02634          if (!res || !return_locked) {
02635             ast_mutex_unlock(&iaxsl[dcallno]);
02636          }
02637          return res;
02638       }
02639       if (dcallno) {
02640          ast_mutex_unlock(&iaxsl[dcallno]);
02641       }
02642 #ifdef IAX_OLD_FIND
02643       /* If we get here, we SHOULD NOT find a call structure for this
02644          callno; if we do, it means that there is a call structure that
02645          has a peer callno but did NOT get entered into the hash table,
02646          which is bad.
02647 
02648          If we find a call structure using this old, slow method, output a log
02649          message so we'll know about it. After a few months of leaving this in
02650          place, if we don't hear about people seeing these messages, we can
02651          remove this code for good.
02652       */
02653 
02654       for (x = 1; !res && x < maxnontrunkcall; x++) {
02655          ast_mutex_lock(&iaxsl[x]);
02656          if (iaxs[x]) {
02657             /* Look for an exact match */
02658             if (match(sin, callno, dcallno, iaxs[x], check_dcallno)) {
02659                res = x;
02660             }
02661          }
02662          if (!res || !return_locked)
02663             ast_mutex_unlock(&iaxsl[x]);
02664       }
02665       for (x = TRUNK_CALL_START; !res && x < maxtrunkcall; x++) {
02666          ast_mutex_lock(&iaxsl[x]);
02667          if (iaxs[x]) {
02668             /* Look for an exact match */
02669             if (match(sin, callno, dcallno, iaxs[x], check_dcallno)) {
02670                res = x;
02671             }
02672          }
02673          if (!res || !return_locked)
02674             ast_mutex_unlock(&iaxsl[x]);
02675       }
02676 #endif
02677    }
02678    if (!res && (new >= NEW_ALLOW)) {
02679       struct callno_entry *callno_entry;
02680       /* It may seem odd that we look through the peer list for a name for
02681        * this *incoming* call.  Well, it is weird.  However, users don't
02682        * have an IP address/port number that we can match against.  So,
02683        * this is just checking for a peer that has that IP/port and
02684        * assuming that we have a user of the same name.  This isn't always
02685        * correct, but it will be changed if needed after authentication. */
02686       if (!iax2_getpeername(*sin, host, sizeof(host)))
02687          snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
02688 
02689       if (peercnt_add(sin)) {
02690          /* This address has hit its callnumber limit.  When the limit
02691           * is reached, the connection is not added to the peercnts table.*/
02692          return 0;
02693       }
02694 
02695       if (!(callno_entry = get_unused_callno(0, validated))) {
02696          /* since we ran out of space, remove the peercnt
02697           * entry we added earlier */
02698          peercnt_remove_by_addr(sin);
02699          ast_log(LOG_WARNING, "No more space\n");
02700          return 0;
02701       }
02702       x = callno_entry->callno;
02703       ast_mutex_lock(&iaxsl[x]);
02704 
02705       iaxs[x] = new_iax(sin, host);
02706       update_max_nontrunk();
02707       if (iaxs[x]) {
02708          if (iaxdebug)
02709             ast_debug(1, "Creating new call structure %d\n", x);
02710          iaxs[x]->callno_entry = callno_entry;
02711          iaxs[x]->sockfd = sockfd;
02712          iaxs[x]->addr.sin_port = sin->sin_port;
02713          iaxs[x]->addr.sin_family = sin->sin_family;
02714          iaxs[x]->addr.sin_addr.s_addr = sin->sin_addr.s_addr;
02715          iaxs[x]->peercallno = callno;
02716          iaxs[x]->callno = x;
02717          iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
02718          iaxs[x]->expiry = min_reg_expire;
02719          iaxs[x]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
02720          iaxs[x]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
02721          iaxs[x]->amaflags = amaflags;
02722          ast_copy_flags(iaxs[x], &globalflags, IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF | IAX_FORCE_ENCRYPT); 
02723          ast_string_field_set(iaxs[x], accountcode, accountcode);
02724          ast_string_field_set(iaxs[x], mohinterpret, mohinterpret);
02725          ast_string_field_set(iaxs[x], mohsuggest, mohsuggest);
02726          ast_string_field_set(iaxs[x], parkinglot, default_parkinglot);
02727 
02728          if (iaxs[x]->peercallno) {
02729             store_by_peercallno(iaxs[x]);
02730          }
02731       } else {
02732          ast_log(LOG_WARNING, "Out of resources\n");
02733          ast_mutex_unlock(&iaxsl[x]);
02734          replace_callno(callno_entry);
02735          return 0;
02736       }
02737       if (!return_locked)
02738          ast_mutex_unlock(&iaxsl[x]);
02739       res = x;
02740    }
02741    return res;
02742 }

static void __get_from_jb ( const void *  p  )  [static]

Definition at line 3889 of file chan_iax2.c.

References __do_deliver(), ast_codec_interp_len(), ast_format_rate(), AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_mutex_lock(), ast_mutex_unlock(), ast_samp2tv(), ast_test_flag, ast_tvadd(), ast_tvdiff_ms(), ast_tvnow(), jb_frame::data, ast_frame::delivery, ast_frame::frametype, iax2_frame_free(), iax2_queue_frame(), IAX_ALREADYGONE, chan_iax2_pvt::jb, JB_DROP, JB_EMPTY, jb_get(), JB_INTERP, jb_next(), JB_NOFRAME, JB_OK, chan_iax2_pvt::jbid, jb_frame::ms, ast_frame::offset, PTR_TO_CALLNO, chan_iax2_pvt::rxcore, ast_frame::samples, ast_frame::src, ast_frame::subclass, update_jbsched(), and chan_iax2_pvt::voiceformat.

Referenced by get_from_jb().

03890 {
03891    int callno = PTR_TO_CALLNO(p);
03892    struct chan_iax2_pvt *pvt = NULL;
03893    struct iax_frame *fr;
03894    jb_frame frame;
03895    int ret;
03896    long ms;
03897    long next;
03898    struct timeval now = ast_tvnow();
03899    
03900    /* Make sure we have a valid private structure before going on */
03901    ast_mutex_lock(&iaxsl[callno]);
03902    pvt = iaxs[callno];
03903    if (!pvt) {
03904       /* No go! */
03905       ast_mutex_unlock(&iaxsl[callno]);
03906       return;
03907    }
03908 
03909    pvt->jbid = -1;
03910    
03911    /* round up a millisecond since ast_sched_runq does; */
03912    /* prevents us from spinning while waiting for our now */
03913    /* to catch up with runq's now */
03914    now.tv_usec += 1000;
03915    
03916    ms = ast_tvdiff_ms(now, pvt->rxcore);
03917    
03918    if(ms >= (next = jb_next(pvt->jb))) {
03919       ret = jb_get(pvt->jb,&frame,ms,ast_codec_interp_len(pvt->voiceformat));
03920       switch(ret) {
03921       case JB_OK:
03922          fr = frame.data;
03923          __do_deliver(fr);
03924          /* __do_deliver() can cause the call to disappear */
03925          pvt = iaxs[callno];
03926          break;
03927       case JB_INTERP:
03928       {
03929          struct ast_frame af = { 0, };
03930          
03931          /* create an interpolation frame */
03932          af.frametype = AST_FRAME_VOICE;
03933          af.subclass = pvt->voiceformat;
03934          af.samples  = frame.ms * (ast_format_rate(pvt->voiceformat) / 1000);
03935          af.src  = "IAX2 JB interpolation";
03936          af.delivery = ast_tvadd(pvt->rxcore, ast_samp2tv(next, 1000));
03937          af.offset = AST_FRIENDLY_OFFSET;
03938          
03939          /* queue the frame:  For consistency, we would call __do_deliver here, but __do_deliver wants an iax_frame,
03940           * which we'd need to malloc, and then it would free it.  That seems like a drag */
03941          if (!ast_test_flag(iaxs[callno], IAX_ALREADYGONE)) {
03942             iax2_queue_frame(callno, &af);
03943             /* iax2_queue_frame() could cause the call to disappear */
03944             pvt = iaxs[callno];
03945          }
03946       }
03947          break;
03948       case JB_DROP:
03949          iax2_frame_free(frame.data);
03950          break;
03951       case JB_NOFRAME:
03952       case JB_EMPTY:
03953          /* do nothing */
03954          break;
03955       default:
03956          /* shouldn't happen */
03957          break;
03958       }
03959    }
03960    if (pvt)
03961       update_jbsched(pvt);
03962    ast_mutex_unlock(&iaxsl[callno]);
03963 }

static void __iax2_do_register_s ( const void *  data  )  [static]

Definition at line 7938 of file chan_iax2.c.

References iax2_registry::expire, and iax2_do_register().

Referenced by iax2_do_register_s().

07939 {
07940    struct iax2_registry *reg = (struct iax2_registry *)data;
07941    reg->expire = -1;
07942    iax2_do_register(reg);
07943 }

static void __iax2_poke_noanswer ( const void *  data  )  [static]

Definition at line 11462 of file chan_iax2.c.

References AST_DEVICE_UNAVAILABLE, ast_devstate_changed(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax2_peer::callno, EVENT_FLAG_SYSTEM, iax2_destroy(), iax2_poke_peer_s(), iax2_sched_add(), iax2_peer::lastms, LOG_NOTICE, manager_event, iax2_peer::name, peer_ref(), peer_unref(), iax2_peer::pokeexpire, and iax2_peer::pokefreqnotok.

Referenced by iax2_poke_noanswer().

11463 {
11464    struct iax2_peer *peer = (struct iax2_peer *)data;
11465    int callno;
11466 
11467    if (peer->lastms > -1) {
11468       ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Time: %d\n", peer->name, peer->lastms);
11469       manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: IAX2\r\nPeer: IAX2/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, peer->lastms);
11470       ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "IAX2/%s", peer->name); /* Activate notification */
11471    }
11472    if ((callno = peer->callno) > 0) {
11473       ast_mutex_lock(&iaxsl[callno]);
11474       iax2_destroy(callno);
11475       ast_mutex_unlock(&iaxsl[callno]);
11476    }
11477    peer->callno = 0;
11478    peer->lastms = -1;
11479    /* Try again quickly */
11480    peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_peer_s, peer_ref(peer));
11481    if (peer->pokeexpire == -1)
11482       peer_unref(peer);
11483 }

static void __iax2_poke_peer_s ( const void *  data  )  [static]

Definition at line 8728 of file chan_iax2.c.

References iax2_poke_peer(), and peer_unref().

Referenced by iax2_poke_peer_s().

08729 {
08730    struct iax2_peer *peer = (struct iax2_peer *)data;
08731    iax2_poke_peer(peer, 0);
08732    peer_unref(peer);
08733 }

static int __iax2_show_peers ( int  manager,
int  fd,
struct mansession *  s,
int  argc,
char *  argv[] 
) [static]

Definition at line 6334 of file chan_iax2.c.

References iax2_peer::addr, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_cli(), ast_copy_string(), ast_inet_ntoa(), ast_str_alloca, ast_str_buffer(), ast_strlen_zero(), ast_test_flag, astman_append(), iax2_peer::encmethods, encmethods_to_str(), FORMAT, FORMAT2, IAX_DYNAMIC, IAX_TRUNK, iax2_peer::mask, iax2_peer::name, name, peer_status(), peer_unref(), RESULT_SHOWUSAGE, RESULT_SUCCESS, status, and iax2_peer::username.

Referenced by handle_cli_iax2_show_peers(), and manager_iax2_show_peers().

06335 {
06336    regex_t regexbuf;
06337    int havepattern = 0;
06338    int total_peers = 0;
06339    int online_peers = 0;
06340    int offline_peers = 0;
06341    int unmonitored_peers = 0;
06342    struct ao2_iterator i;
06343 
06344 #define FORMAT2 "%-15.15s  %-15.15s %s  %-15.15s  %-8s  %s %-10s%s"
06345 #define FORMAT "%-15.15s  %-15.15s %s  %-15.15s  %-5d%s  %s %-10s%s"
06346 
06347    struct iax2_peer *peer = NULL;
06348    char name[256];
06349    struct ast_str *encmethods = ast_str_alloca(256);
06350    int registeredonly=0;
06351    char *term = manager ? "\r\n" : "\n";
06352    char idtext[256] = "";
06353    switch (argc) {
06354    case 6:
06355       if (!strcasecmp(argv[3], "registered"))
06356          registeredonly = 1;
06357       else
06358          return RESULT_SHOWUSAGE;
06359       if (!strcasecmp(argv[4], "like")) {
06360          if (regcomp(&regexbuf, argv[5], REG_EXTENDED | REG_NOSUB))
06361             return RESULT_SHOWUSAGE;
06362          havepattern = 1;
06363       } else
06364          return RESULT_SHOWUSAGE;
06365       break;
06366    case 5:
06367       if (!strcasecmp(argv[3], "like")) {
06368          if (regcomp(&regexbuf, argv[4], REG_EXTENDED | REG_NOSUB))
06369             return RESULT_SHOWUSAGE;
06370          havepattern = 1;
06371       } else
06372          return RESULT_SHOWUSAGE;
06373       break;
06374    case 4:
06375       if (!strcasecmp(argv[3], "registered"))
06376          registeredonly = 1;
06377       else
06378          return RESULT_SHOWUSAGE;
06379       break;
06380    case 3:
06381       break;
06382    default:
06383       return RESULT_SHOWUSAGE;
06384    }
06385 
06386 
06387    if (!s)
06388       ast_cli(fd, FORMAT2, "Name/Username", "Host", "   ", "Mask", "Port", "   ", "Status", term);
06389 
06390    i = ao2_iterator_init(peers, 0);
06391    for (peer = ao2_iterator_next(&i); peer; 
06392       peer_unref(peer), peer = ao2_iterator_next(&i)) {
06393       char nm[20];
06394       char status[20];
06395       int retstatus;
06396 
06397       if (registeredonly && !peer->addr.sin_addr.s_addr)
06398          continue;
06399       if (havepattern && regexec(&regexbuf, peer->name, 0, NULL, 0))
06400          continue;
06401 
06402       if (!ast_strlen_zero(peer->username))
06403          snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
06404       else
06405          ast_copy_string(name, peer->name, sizeof(name));
06406 
06407       encmethods_to_str(peer->encmethods, encmethods);
06408       retstatus = peer_status(peer, status, sizeof(status));
06409       if (retstatus > 0)
06410          online_peers++;
06411       else if (!retstatus)
06412          offline_peers++;
06413       else
06414          unmonitored_peers++;
06415 
06416       ast_copy_string(nm, ast_inet_ntoa(peer->mask), sizeof(nm));
06417 
06418       if (s) {
06419          astman_append(s,
06420             "Event: PeerEntry\r\n%s"
06421             "Channeltype: IAX2\r\n"
06422             "ChanObjectType: peer\r\n"
06423             "ObjectName: %s\r\n"
06424             "IPaddress: %s\r\n"
06425             "IPport: %d\r\n"
06426             "Dynamic: %s\r\n"
06427             "Trunk: %s\r\n"
06428             "Encryption: %s\r\n"
06429             "Status: %s\r\n\r\n",
06430             idtext,
06431             name,
06432             peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "-none-",
06433             ntohs(peer->addr.sin_port),
06434             ast_test_flag(peer, IAX_DYNAMIC) ? "yes" : "no",
06435             ast_test_flag(peer, IAX_TRUNK) ? "yes" : "no",
06436             peer->encmethods ? ast_str_buffer(encmethods) : "no",
06437             status);
06438       } else {
06439          ast_cli(fd, FORMAT, name,
06440             peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
06441             ast_test_flag(peer, IAX_DYNAMIC) ? "(D)" : "(S)",
06442             nm,
06443             ntohs(peer->addr.sin_port),
06444             ast_test_flag(peer, IAX_TRUNK) ? "(T)" : "   ",
06445             peer->encmethods ? "(E)" : "   ",
06446             status,
06447             term);
06448       }
06449       total_peers++;
06450    }
06451    ao2_iterator_destroy(&i);
06452 
06453    if (!s)
06454       ast_cli(fd,"%d iax2 peers [%d online, %d offline, %d unmonitored]%s", total_peers, online_peers, offline_peers, unmonitored_peers, term);
06455 
06456    if (havepattern)
06457       regfree(&regexbuf);
06458 
06459    return RESULT_SUCCESS;
06460 #undef FORMAT
06461 #undef FORMAT2
06462 }

static void __reg_module ( void   )  [static]

Definition at line 13939 of file chan_iax2.c.

static int __schedule_action ( void(*)(const void *data)  func,
const void *  data,
const char *  funcname 
) [static]

Definition at line 1329 of file chan_iax2.c.

References ast_copy_string(), ast_debug, iax2_thread::cond, iax2_thread::curfunc, find_idle_thread(), IAX_IOSTATE_SCHEDREADY, iax2_thread::iostate, iax2_thread::lock, iax2_thread::scheddata, iax2_thread::schedfunc, signal_condition(), and thread.

01330 {
01331    struct iax2_thread *thread = NULL;
01332    static time_t lasterror;
01333    static time_t t;
01334 
01335    thread = find_idle_thread();
01336 
01337    if (thread != NULL) {
01338       thread->schedfunc = func;
01339       thread->scheddata = data;
01340       thread->iostate = IAX_IOSTATE_SCHEDREADY;
01341 #ifdef DEBUG_SCHED_MULTITHREAD
01342       ast_copy_string(thread->curfunc, funcname, sizeof(thread->curfunc));
01343 #endif
01344       signal_condition(&thread->lock, &thread->cond);
01345       return 0;
01346    }
01347    time(&t);
01348    if (t != lasterror) 
01349       ast_debug(1, "Out of idle IAX2 threads for scheduling!\n");
01350    lasterror = t;
01351 
01352    return -1;
01353 }

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]

Definition at line 7138 of file chan_iax2.c.

References ast_frame::data, ast_frame::datalen, ast_frame::frametype, iax2_send(), ast_frame::ptr, queue_signalling(), ast_frame::src, and ast_frame::subclass.

Referenced by send_command(), send_command_final(), send_command_immediate(), and send_command_transfer().

07140 {
07141    struct ast_frame f = { 0, };
07142    int res = 0;
07143 
07144    f.frametype = type;
07145    f.subclass = command;
07146    f.datalen = datalen;
07147    f.src = __FUNCTION__;
07148    f.data.ptr = (void *) data;
07149 
07150    if ((res = queue_signalling(i, &f)) <= 0) {
07151       return res;
07152    }
07153 
07154    return iax2_send(i, &f, ts, seqno, now, transfer, final);
07155 }

static void __send_lagrq ( const void *  data  )  [static]

Definition at line 1434 of file chan_iax2.c.

References ast_debug, AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), iax2_thread::callno, iax2_sched_add(), IAX_COMMAND_LAGRQ, chan_iax2_pvt::lagid, send_command(), and send_lagrq().

Referenced by send_lagrq().

01435 {
01436    int callno = (long) data;
01437 
01438    ast_mutex_lock(&iaxsl[callno]);
01439 
01440    if (iaxs[callno]) {
01441       if (iaxs[callno]->peercallno) {
01442          send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
01443          iaxs[callno]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, data);
01444       } else {
01445          /* I am the schedule, so I'm allowed to do this */
01446          iaxs[callno]->lagid = -1;
01447       }
01448    } else {
01449       ast_debug(1, "I was supposed to send a LAGRQ with callno %d, but no such call exists.\n", callno);
01450    }
01451 
01452    ast_mutex_unlock(&iaxsl[callno]);
01453 }

static void __send_ping ( const void *  data  )  [static]

Definition at line 1373 of file chan_iax2.c.

References ast_debug, AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), iax2_thread::callno, iax2_sched_add(), IAX_COMMAND_PING, chan_iax2_pvt::pingid, send_command(), and send_ping().

Referenced by send_ping().

01374 {
01375    int callno = (long) data;
01376 
01377    ast_mutex_lock(&iaxsl[callno]);
01378 
01379    if (iaxs[callno]) {
01380       if (iaxs[callno]->peercallno) {
01381          send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PING, 0, NULL, 0, -1);
01382          iaxs[callno]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, data);
01383       } else {
01384          /* I am the schedule, so I'm allowed to do this */
01385          iaxs[callno]->pingid = -1;
01386       }
01387    } else {
01388       ast_debug(1, "I was supposed to send a PING with callno %d, but no such call exists.\n", callno);
01389    }
01390 
01391    ast_mutex_unlock(&iaxsl[callno]);
01392 }

static int __unload_module ( void   )  [static]

Definition at line 13652 of file chan_iax2.c.

References ao2_ref, ARRAY_LEN, ast_channel_unregister(), ast_cli_unregister_multiple(), ast_context_destroy(), ast_context_find(), AST_LIST_LOCK, AST_LIST_UNLOCK, ast_manager_unregister(), ast_mutex_destroy(), ast_netsock_release(), AST_PTHREADT_NULL, ast_sched_thread_destroy(), ast_timer_close(), ast_unload_realtime(), ast_unregister_application(), ast_unregister_switch(), cleanup_thread_list(), delete_users(), iax2_destroy(), iax_provision_unload(), and reload_firmware().

Referenced by load_module(), and unload_module().

13653 {
13654    struct ast_context *con;
13655    int x;
13656 
13657    ast_manager_unregister("IAXpeers");
13658    ast_manager_unregister("IAXpeerlist");
13659    ast_manager_unregister("IAXnetstats");
13660    ast_manager_unregister("IAXregistry");
13661    ast_unregister_application(papp);
13662    ast_cli_unregister_multiple(cli_iax2, ARRAY_LEN(cli_iax2));
13663    ast_unregister_switch(&iax2_switch);
13664    ast_channel_unregister(&iax2_tech);
13665 
13666    if (netthreadid != AST_PTHREADT_NULL) {
13667       AST_LIST_LOCK(&frame_queue);
13668       pthread_cancel(netthreadid);
13669       AST_LIST_UNLOCK(&frame_queue);
13670       pthread_join(netthreadid, NULL);
13671    }
13672 
13673    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
13674       if (iaxs[x]) {
13675          iax2_destroy(x);
13676       }
13677    }
13678    
13679    /* Call for all threads to halt */
13680    cleanup_thread_list(&idle_list);
13681    cleanup_thread_list(&active_list);
13682    cleanup_thread_list(&dynamic_list);
13683 
13684    sched = ast_sched_thread_destroy(sched);
13685 
13686    ast_netsock_release(netsock);
13687    ast_netsock_release(outsock);
13688 
13689    delete_users();
13690    iax_provision_unload();
13691    reload_firmware(1);
13692 
13693    for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
13694       ast_mutex_destroy(&iaxsl[x]);
13695    }
13696 
13697    ao2_ref(peers, -1);
13698    ao2_ref(users, -1);
13699    ao2_ref(iax_peercallno_pvts, -1);
13700    ao2_ref(iax_transfercallno_pvts, -1);
13701    ao2_ref(peercnts, -1);
13702    ao2_ref(callno_limits, -1);
13703    ao2_ref(calltoken_ignores, -1);
13704    ao2_ref(callno_pool, -1);
13705    ao2_ref(callno_pool_trunk, -1);
13706    if (timer) {
13707       ast_timer_close(timer);
13708    }
13709 
13710    con = ast_context_find(regcontext);
13711    if (con)
13712       ast_context_destroy(con, "IAX2");
13713    ast_unload_realtime("iaxpeers");
13714    return 0;
13715 }

static void __unreg_module ( void   )  [static]

Definition at line 13939 of file chan_iax2.c.

static int acf_channel_read ( struct ast_channel *  chan,
const char *  funcname,
char *  preparse,
char *  buf,
size_t  buflen 
) [static]

Definition at line 13417 of file chan_iax2.c.

References chan_iax2_pvt::addr, ast_copy_string(), ast_inet_ntoa(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), chan_iax2_pvt::callno, LOG_ERROR, chan_iax2_pvt::osptoken, PTR_TO_CALLNO, ast_channel::tech, ast_channel::tech_pvt, and chan_iax2_pvt::username.

13418 {
13419    struct chan_iax2_pvt *pvt;
13420    unsigned int callno;
13421    int res = 0;
13422 
13423    if (!chan || chan->tech != &iax2_tech) {
13424       ast_log(LOG_ERROR, "This function requires a valid IAX2 channel\n");
13425       return -1;
13426    }
13427 
13428    callno = PTR_TO_CALLNO(chan->tech_pvt);
13429    ast_mutex_lock(&iaxsl[callno]);
13430    if (!(pvt = iaxs[callno])) {
13431       ast_mutex_unlock(&iaxsl[callno]);
13432       return -1;
13433    }
13434 
13435    if (!strcasecmp(args, "osptoken")) {
13436       ast_copy_string(buf, pvt->osptoken, buflen);
13437    } else if (!strcasecmp(args, "peerip")) {
13438       ast_copy_string(buf, pvt->addr.sin_addr.s_addr ? ast_inet_ntoa(pvt->addr.sin_addr) : "", buflen);
13439    } else if (!strcasecmp(args, "peername")) {
13440       ast_copy_string(buf, pvt->username, buflen);
13441    } else {
13442       res = -1;
13443    }
13444 
13445    ast_mutex_unlock(&iaxsl[callno]);
13446 
13447    return res;
13448 }

static int acf_iaxvar_read ( struct ast_channel *  chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 9397 of file chan_iax2.c.

References ast_channel_datastore_find(), ast_copy_string(), AST_LIST_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_datastore::data, ast_var_t::entries, ast_var_t::name, ast_var_t::value, and var.

09398 {
09399    struct ast_datastore *variablestore = ast_channel_datastore_find(chan, &iax2_variable_datastore_info, NULL);
09400    AST_LIST_HEAD(, ast_var_t) *varlist;
09401    struct ast_var_t *var;
09402 
09403    if (!variablestore) {
09404       *buf = '\0';
09405       return 0;
09406    }
09407    varlist = variablestore->data;
09408 
09409    AST_LIST_LOCK(varlist);
09410    AST_LIST_TRAVERSE(varlist, var, entries) {
09411       if (strcmp(var->name, data) == 0) {
09412          ast_copy_string(buf, var->value, len);
09413          break;
09414       }
09415    }
09416    AST_LIST_UNLOCK(varlist);
09417    return 0;
09418 }

static int acf_iaxvar_write ( struct ast_channel *  chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

Definition at line 9420 of file chan_iax2.c.

References ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_datastore_alloc, AST_LIST_HEAD, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), ast_var_assign(), ast_var_delete(), ast_datastore::data, DATASTORE_INHERIT_FOREVER, ast_var_t::entries, ast_datastore::inheritance, LOG_ERROR, ast_var_t::name, and var.

09421 {
09422    struct ast_datastore *variablestore = ast_channel_datastore_find(chan, &iax2_variable_datastore_info, NULL);
09423    AST_LIST_HEAD(, ast_var_t) *varlist;
09424    struct ast_var_t *var;
09425 
09426    if (!variablestore) {
09427       variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
09428       if (!variablestore) {
09429          ast_log(LOG_ERROR, "Memory allocation error\n");
09430          return -1;
09431       }
09432       varlist = ast_calloc(1, sizeof(*varlist));
09433       if (!varlist) {
09434          ast_log(LOG_ERROR, "Unable to assign new variable '%s'\n", data);
09435          return -1;
09436       }
09437 
09438       AST_LIST_HEAD_INIT(varlist);
09439       variablestore->data = varlist;
09440       variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
09441       ast_channel_datastore_add(chan, variablestore);
09442    } else
09443       varlist = variablestore->data;
09444 
09445    AST_LIST_LOCK(varlist);
09446    AST_LIST_TRAVERSE_SAFE_BEGIN(varlist, var, entries) {
09447       if (strcmp(var->name, data) == 0) {
09448          AST_LIST_REMOVE_CURRENT(entries);
09449          ast_var_delete(var);
09450          break;
09451       }
09452    }
09453    AST_LIST_TRAVERSE_SAFE_END;
09454    var = ast_var_assign(data, value);
09455    if (var)
09456       AST_LIST_INSERT_TAIL(varlist, var, entries);
09457    else
09458       ast_log(LOG_ERROR, "Unable to assign new variable '%s'\n", data);
09459    AST_LIST_UNLOCK(varlist);
09460    return 0;
09461 }

static int add_calltoken_ignore ( const char *  addr  )  [static]

Definition at line 2334 of file chan_iax2.c.

References ao2_alloc, ao2_find, ao2_link, ao2_lock(), ao2_ref, ao2_unlock(), ast_append_ha(), ast_copy_ha(), ast_free_ha(), ast_log(), ast_strlen_zero(), addr_range::delme, addr_range::ha, LOG_WARNING, and OBJ_POINTER.

Referenced by set_config().

02335 {
02336    struct addr_range tmp;
02337    struct addr_range *addr_range = NULL;
02338    struct ast_ha *ha = NULL;
02339    int error = 0;
02340 
02341    if (ast_strlen_zero(addr)) {
02342       ast_log(LOG_WARNING, "invalid calltokenoptional %s\n", addr);
02343       return -1;
02344    }
02345 
02346    ha = ast_append_ha("permit", addr, NULL, &error);
02347 
02348    /* check for valid config information */
02349    if (error) {
02350       ast_log(LOG_WARNING, "Error %d creating calltokenoptional entry %s\n", error, addr);
02351       return -1;
02352    }
02353 
02354    ast_copy_ha(ha, &tmp.ha);
02355    /* find or create the addr_range */
02356    if ((addr_range = ao2_find(calltoken_ignores, &tmp, OBJ_POINTER))) {
02357       ao2_lock(addr_range);
02358       addr_range->delme = 0;
02359       ao2_unlock(addr_range);
02360    } else if ((addr_range = ao2_alloc(sizeof(*addr_range), NULL))) {
02361       /* copy over config data into addr_range object */
02362       ast_copy_ha(ha, &addr_range->ha); /* this is safe because only one ha is possible */
02363       ao2_link(calltoken_ignores, addr_range);
02364    } else {
02365       ast_free_ha(ha);
02366       return -1;
02367    }
02368 
02369    ast_free_ha(ha);
02370    ao2_ref(addr_range, -1); /* decrement ref from ao2_find and ao2_alloc, only container ref remains */
02371 
02372    return 0;
02373 }

static void add_empty_calltoken_ie ( struct chan_iax2_pvt *  pvt,
struct iax_ie_data *  ied 
) [static]

Definition at line 4510 of file chan_iax2.c.

References iax_ie_data::buf, chan_iax2_pvt::calltoken_ie_len, IAX_IE_CALLTOKEN, and iax_ie_data::pos.

Referenced by cache_get_callno_locked(), iax2_call(), iax2_do_register(), iax2_poke_peer(), and registry_rerequest().

04511 {
04512    /* first make sure their are two empty bytes left in ied->buf */
04513    if (pvt && ied && (2 < ((int) sizeof(ied->buf) - ied->pos))) {
04514       ied->buf[ied->pos++] = IAX_IE_CALLTOKEN;  /* type */
04515       ied->buf[ied->pos++] = 0;   /* data size,  ZERO in this case */
04516       pvt->calltoken_ie_len = 2;
04517    }
04518 }

static int addr_range_cmp_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 1996 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, addr_range::ha, ast_ha::netaddr, and ast_ha::netmask.

Referenced by load_objects().

01997 {
01998    struct addr_range *lim1 = obj, *lim2 = arg;
01999    return ((lim1->ha.netaddr.s_addr == lim2->ha.netaddr.s_addr) &&
02000       (lim1->ha.netmask.s_addr == lim2->ha.netmask.s_addr)) ?
02001       CMP_MATCH | CMP_STOP : 0;
02002 }

static int addr_range_delme_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 1983 of file chan_iax2.c.

References addr_range::delme.

Referenced by set_config_destroy().

01984 {
01985    struct addr_range *lim = obj;
01986    lim->delme = 1;
01987    return 0;
01988 }

static int addr_range_hash_cb ( const void *  obj,
const int  flags 
) [static]

Definition at line 1990 of file chan_iax2.c.

References addr_range::ha, and ast_ha::netaddr.

Referenced by load_objects().

01991 {
01992    const struct addr_range *lim = obj;
01993    return abs((int) lim->ha.netaddr.s_addr);
01994 }

static int addr_range_match_address_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 2016 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, addr_range::ha, ast_ha::netaddr, and ast_ha::netmask.

Referenced by calltoken_required(), and set_peercnt_limit().

02017 {
02018    struct addr_range *addr_range = obj;
02019    struct sockaddr_in *sin = arg;
02020 
02021    if ((sin->sin_addr.s_addr & addr_range->ha.netmask.s_addr) == addr_range->ha.netaddr.s_addr) {
02022       return CMP_MATCH | CMP_STOP;
02023    }
02024    return 0;
02025 }

static int apply_context ( struct iax2_context *  con,
const char *  context 
) [static]

Definition at line 7196 of file chan_iax2.c.

References iax2_context::context, and iax2_context::next.

Referenced by check_access().

07197 {
07198    while(con) {
07199       if (!strcmp(con->context, context) || !strcmp(con->context, "*"))
07200          return -1;
07201       con = con->next;
07202    }
07203    return 0;
07204 }

static int ast_cli_netstats ( struct mansession *  s,
int  fd,
int  limit_fmt 
) [static]

Definition at line 6901 of file chan_iax2.c.

References ACN_FORMAT1, ACN_FORMAT2, ARRAY_LEN, ast_cli(), ast_mutex_lock(), ast_mutex_unlock(), ast_test_flag, astman_append(), jb_info::current, iax_rr::delay, iax_rr::dropped, chan_iax2_pvt::first_iax_message, jb_info::frames_dropped, jb_info::frames_lost, jb_info::frames_ooo, chan_iax2_pvt::frames_received, iax_frame_subclass2str(), IAX_USEJITTERBUF, jb_getinfo(), iax_rr::jitter, jb_info::jitter, chan_iax2_pvt::last_iax_message, iax_rr::losscnt, iax_rr::losspct, jb_info::losspct, MARK_IAX_SUBCLASS_TX, jb_info::min, iax_rr::ooo, iax_rr::packets, chan_iax2_pvt::pingtime, and chan_iax2_pvt::remote_rr.

Referenced by handle_cli_iax2_show_netstats(), and manager_iax2_show_netstats().

06902 {
06903    int x;
06904    int numchans = 0;
06905    char first_message[10] = { 0, };
06906    char last_message[10] = { 0, };
06907 #define ACN_FORMAT1 "%-20.25s %4d %4d %4d %5d %3d %5d %4d %6d %4d %4d %5d %3d %5d %4d %6d %s%s %4s%s\n"
06908 #define ACN_FORMAT2 "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %s%s %s%s\n"
06909    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
06910       ast_mutex_lock(&iaxsl[x]);
06911       if (iaxs[x]) {
06912          int localjitter, localdelay, locallost, locallosspct, localdropped, localooo;
06913          jb_info jbinfo;
06914          iax_frame_subclass2str(iaxs[x]->first_iax_message & ~MARK_IAX_SUBCLASS_TX, first_message, sizeof(first_message));
06915          iax_frame_subclass2str(iaxs[x]->last_iax_message & ~MARK_IAX_SUBCLASS_TX, last_message, sizeof(last_message));
06916 
06917          if(ast_test_flag(iaxs[x], IAX_USEJITTERBUF)) {
06918             jb_getinfo(iaxs[x]->jb, &jbinfo);
06919             localjitter = jbinfo.jitter;
06920             localdelay = jbinfo.current - jbinfo.min;
06921             locallost = jbinfo.frames_lost;
06922             locallosspct = jbinfo.losspct/1000;
06923             localdropped = jbinfo.frames_dropped;
06924             localooo = jbinfo.frames_ooo;
06925          } else {
06926             localjitter = -1;
06927             localdelay = 0;
06928             locallost = -1;
06929             locallosspct = -1;
06930             localdropped = 0;
06931             localooo = -1;
06932          }
06933          if (s)
06934             astman_append(s, limit_fmt ? ACN_FORMAT1 : ACN_FORMAT2,
06935                iaxs[x]->owner ? iaxs[x]->owner->name : "(None)",
06936                iaxs[x]->pingtime,
06937                localjitter,
06938                localdelay,
06939                locallost,
06940                locallosspct,
06941                localdropped,
06942                localooo,
06943                iaxs[x]->frames_received/1000,
06944                iaxs[x]->remote_rr.jitter,
06945                iaxs[x]->remote_rr.delay,
06946                iaxs[x]->remote_rr.losscnt,
06947                iaxs[x]->remote_rr.losspct,
06948                iaxs[x]->remote_rr.dropped,
06949                iaxs[x]->remote_rr.ooo,
06950                iaxs[x]->remote_rr.packets/1000,
06951                (iaxs[x]->first_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
06952                first_message,
06953                (iaxs[x]->last_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
06954                last_message);
06955          else
06956             ast_cli(fd, limit_fmt ? ACN_FORMAT1 : ACN_FORMAT2,
06957                iaxs[x]->owner ? iaxs[x]->owner->name : "(None)",
06958                iaxs[x]->pingtime,
06959                localjitter,
06960                localdelay,
06961                locallost,
06962                locallosspct,
06963                localdropped,
06964                localooo,
06965                iaxs[x]->frames_received/1000,
06966                iaxs[x]->remote_rr.jitter,
06967                iaxs[x]->remote_rr.delay,
06968                iaxs[x]->remote_rr.losscnt,
06969                iaxs[x]->remote_rr.losspct,
06970                iaxs[x]->remote_rr.dropped,
06971                iaxs[x]->remote_rr.ooo,
06972                iaxs[x]->remote_rr.packets/1000,
06973                (iaxs[x]->first_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
06974                first_message,
06975                (iaxs[x]->last_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
06976                last_message);
06977          numchans++;
06978       }
06979       ast_mutex_unlock(&iaxsl[x]);
06980    }
06981 
06982    return numchans;
06983 }

static struct ast_channel* ast_iax2_new ( int  callno,
int  state,
int  capability 
) [static, read]

Create new call, interface with the PBX core.

Definition at line 5425 of file chan_iax2.c.

References chan_iax2_pvt::accountcode, chan_iax2_pvt::adsi, ast_channel::adsicpe, ast_channel::amaflags, chan_iax2_pvt::amaflags, chan_iax2_pvt::ani, AST_ADSI_UNAVAILABLE, ast_best_codec(), ast_calloc, ast_channel_alloc, ast_channel_datastore_add(), ast_channel_free(), ast_copy_string(), ast_datastore_alloc, ast_datastore_free(), ast_debug, ast_free, ast_hangup(), AST_LIST_HEAD, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, ast_log(), ast_module_ref(), ast_mutex_lock(), ast_mutex_unlock(), ast_pbx_start(), AST_STATE_DOWN, ast_strdup, ast_string_field_set, ast_strlen_zero(), ast_var_assign(), chan_iax2_pvt::calling_pres, chan_iax2_pvt::calling_tns, chan_iax2_pvt::calling_ton, chan_iax2_pvt::callno, CALLNO_TO_PTR, chan_iax2_pvt::capability, ast_channel::cid, ast_callerid::cid_ani, ast_callerid::cid_dnid, chan_iax2_pvt::cid_name, chan_iax2_pvt::cid_num, ast_callerid::cid_pres, ast_callerid::cid_rdnis, ast_callerid::cid_tns, ast_callerid::cid_ton, ast_channel::context, chan_iax2_pvt::context, ast_datastore::data, DATASTORE_INHERIT_FOREVER, chan_iax2_pvt::dnid, ast_var_t::entries, ast_channel::exten, chan_iax2_pvt::exten, chan_iax2_pvt::host, iax2_ami_channelupdate(), chan_iax2_pvt::iaxvars, ast_datastore::inheritance, chan_iax2_pvt::language, LOG_ERROR, LOG_WARNING, ast_channel::name, ast_variable::name, ast_channel::nativeformats, ast_variable::next, chan_iax2_pvt::owner, parkinglot, chan_iax2_pvt::parkinglot, pbx_builtin_setvar_helper(), chan_iax2_pvt::peeradsicpe, ast_channel::rawreadformat, ast_channel::rawwriteformat, chan_iax2_pvt::rdnis, ast_channel::readformat, ast_channel::tech, ast_channel::tech_pvt, ast_variable::value, var, chan_iax2_pvt::vars, and ast_channel::writeformat.

Referenced by iax2_request(), and socket_process().

05426 {
05427    struct ast_channel *tmp;
05428    struct chan_iax2_pvt *i;
05429    struct ast_variable *v = NULL;
05430 
05431    if (!(i = iaxs[callno])) {
05432       ast_log(LOG_WARNING, "No IAX2 pvt found for callno '%d' !\n", callno);
05433       return NULL;
05434    }
05435 
05436    /* Don't hold call lock */
05437    ast_mutex_unlock(&iaxsl[callno]);
05438    tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, "IAX2/%s-%d", i->host, i->callno);
05439    ast_mutex_lock(&iaxsl[callno]);
05440    if (i != iaxs[callno]) {
05441       if (tmp) {
05442          /* unlock and relock iaxsl[callno] to preserve locking order */
05443          ast_mutex_unlock(&iaxsl[callno]);
05444          ast_channel_free(tmp);
05445          ast_mutex_lock(&iaxsl[callno]);
05446       }
05447       return NULL;
05448    }
05449    iax2_ami_channelupdate(i);
05450    if (!tmp)
05451       return NULL;
05452    tmp->tech = &iax2_tech;
05453    /* We can support any format by default, until we get restricted */
05454    tmp->nativeformats = capability;
05455    tmp->readformat = tmp->rawreadformat = ast_best_codec(capability);
05456    tmp->writeformat = tmp->rawwriteformat = ast_best_codec(capability);
05457    tmp->tech_pvt = CALLNO_TO_PTR(i->callno);
05458 
05459    if (!ast_strlen_zero(i->parkinglot))
05460       ast_string_field_set(tmp, parkinglot, i->parkinglot);
05461    /* Don't use ast_set_callerid() here because it will
05462     * generate a NewCallerID event before the NewChannel event */
05463    if (!ast_strlen_zero(i->ani))
05464       tmp->cid.cid_ani = ast_strdup(i->ani);
05465    else
05466       tmp->cid.cid_ani = ast_strdup(i->cid_num);
05467    tmp->cid.cid_dnid = ast_strdup(i->dnid);
05468    tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
05469    tmp->cid.cid_pres = i->calling_pres;
05470    tmp->cid.cid_ton = i->calling_ton;
05471    tmp->cid.cid_tns = i->calling_tns;
05472    if (!ast_strlen_zero(i->language))
05473       ast_string_field_set(tmp, language, i->language);
05474    if (!ast_strlen_zero(i->accountcode))
05475       ast_string_field_set(tmp, accountcode, i->accountcode);
05476    if (i->amaflags)
05477       tmp->amaflags = i->amaflags;
05478    ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
05479    ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
05480    if (i->adsi)
05481       tmp->adsicpe = i->peeradsicpe;
05482    else
05483       tmp->adsicpe = AST_ADSI_UNAVAILABLE;
05484    i->owner = tmp;
05485    i->capability = capability;
05486 
05487    /* Set inherited variables */
05488    if (i->vars) {
05489       for (v = i->vars ; v ; v = v->next)
05490          pbx_builtin_setvar_helper(tmp, v->name, v->value);
05491    }
05492    if (i->iaxvars) {
05493       struct ast_datastore *variablestore;
05494       struct ast_variable *var, *prev = NULL;
05495       AST_LIST_HEAD(, ast_var_t) *varlist;
05496       ast_debug(1, "Loading up the channel with IAXVARs\n");
05497       varlist = ast_calloc(1, sizeof(*varlist));
05498       variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
05499       if (variablestore && varlist) {
05500          variablestore->data = varlist;
05501          variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
05502          AST_LIST_HEAD_INIT(varlist);
05503          for (var = i->iaxvars; var; var = var->next) {
05504             struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
05505             if (prev)
05506                ast_free(prev);
05507             prev = var;
05508             if (!newvar) {
05509                /* Don't abort list traversal, as this would leave i->iaxvars in an inconsistent state. */
05510                ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
05511             } else {
05512                AST_LIST_INSERT_TAIL(varlist, newvar, entries);
05513             }
05514          }
05515          if (prev)
05516             ast_free(prev);
05517          i->iaxvars = NULL;
05518          ast_channel_datastore_add(i->owner, variablestore);
05519       } else {
05520          if (variablestore) {
05521             ast_datastore_free(variablestore);
05522          }
05523          if (varlist) {
05524             ast_free(varlist);
05525          }
05526       }
05527    }
05528 
05529    if (state != AST_STATE_DOWN) {
05530       if (ast_pbx_start(tmp)) {
05531          ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05532          ast_hangup(tmp);
05533          i->owner = NULL;
05534          return NULL;
05535       }
05536    }
05537 
05538    ast_module_ref(ast_module_info->self);
05539    return tmp;
05540 }

static int attempt_transmit ( const void *  data  )  [static]

Definition at line 3394 of file chan_iax2.c.

References __attempt_transmit(), and schedule_action.

Referenced by __attempt_transmit(), and network_thread().

03395 {
03396 #ifdef SCHED_MULTITHREADED
03397    if (schedule_action(__attempt_transmit, data))
03398 #endif      
03399       __attempt_transmit(data);
03400    return 0;
03401 }

static int auth_fail ( int  callno,
int  failcode 
) [static]

Definition at line 8650 of file chan_iax2.c.

References auth_reject(), chan_iax2_pvt::authfail, chan_iax2_pvt::authid, and iax2_sched_replace().

Referenced by socket_process().

08651 {
08652    /* Schedule sending the authentication failure in one second, to prevent
08653       guessing */
08654    if (iaxs[callno]) {
08655       iaxs[callno]->authfail = failcode;
08656       if (delayreject) {
08657          iaxs[callno]->authid = iax2_sched_replace(iaxs[callno]->authid, 
08658             sched, 1000, auth_reject, (void *)(long)callno);
08659       } else
08660          auth_reject((void *)(long)callno);
08661    }
08662    return 0;
08663 }

static int auth_reject ( const void *  data  )  [static]

Definition at line 8636 of file chan_iax2.c.

References __auth_reject(), ast_mutex_lock(), ast_mutex_unlock(), chan_iax2_pvt::authid, and schedule_action.

Referenced by auth_fail().

08637 {
08638    int callno = (int)(long)(data);
08639    ast_mutex_lock(&iaxsl[callno]);
08640    if (iaxs[callno])
08641       iaxs[callno]->authid = -1;
08642    ast_mutex_unlock(&iaxsl[callno]);
08643 #ifdef SCHED_MULTITHREADED
08644    if (schedule_action(__auth_reject, data))
08645 #endif      
08646       __auth_reject(data);
08647    return 0;
08648 }

static int authenticate ( const char *  challenge,
const char *  secret,
const char *  keyn,
int  authmethods,
struct iax_ie_data *  ied,
struct sockaddr_in *  sin,
struct chan_iax2_pvt *  pvt 
) [static]

Definition at line 7764 of file chan_iax2.c.

References ast_inet_ntoa(), ast_key_get, AST_KEY_PRIVATE, ast_log(), ast_sign, ast_strlen_zero(), build_encryption_keys(), IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_AUTH_RSA, iax_ie_append_str(), IAX_IE_MD5_RESULT, IAX_IE_PASSWORD, IAX_IE_RSA_RESULT, LOG_NOTICE, MD5Final(), MD5Init(), and MD5Update().

Referenced by authenticate_reply(), and registry_rerequest().

07765 {
07766    int res = -1;
07767    int x;
07768    if (!ast_strlen_zero(keyn)) {
07769       if (!(authmethods & IAX_AUTH_RSA)) {
07770          if (ast_strlen_zero(secret)) 
07771             ast_log(LOG_NOTICE, "Asked to authenticate to %s with an RSA key, but they don't allow RSA authentication\n", ast_inet_ntoa(sin->sin_addr));
07772       } else if (ast_strlen_zero(challenge)) {
07773          ast_log(LOG_NOTICE, "No challenge provided for RSA authentication to %s\n", ast_inet_ntoa(sin->sin_addr));
07774       } else {
07775          char sig[256];
07776          struct ast_key *key;
07777          key = ast_key_get(keyn, AST_KEY_PRIVATE);
07778          if (!key) {
07779             ast_log(LOG_NOTICE, "Unable to find private key '%s'\n", keyn);
07780          } else {
07781             if (ast_sign(key, (char*)challenge, sig)) {
07782                ast_log(LOG_NOTICE, "Unable to sign challenge with key\n");
07783                res = -1;
07784             } else {
07785                iax_ie_append_str(ied, IAX_IE_RSA_RESULT, sig);
07786                res = 0;
07787             }
07788          }
07789       }
07790    } 
07791    /* Fall back */
07792    if (res && !ast_strlen_zero(secret)) {
07793       if ((authmethods & IAX_AUTH_MD5) && !ast_strlen_zero(challenge)) {
07794          struct MD5Context md5;
07795          unsigned char digest[16];
07796          char digres[128];
07797          MD5Init(&md5);
07798          MD5Update(&md5, (unsigned char *)challenge, strlen(challenge));
07799          MD5Update(&md5, (unsigned char *)secret, strlen(secret));
07800          MD5Final(digest, &md5);
07801          /* If they support md5, authenticate with it.  */
07802          for (x=0;x<16;x++)
07803             sprintf(digres + (x << 1),  "%2.2x", digest[x]); /* safe */
07804          if (pvt) {
07805             build_encryption_keys(digest, pvt);
07806          }
07807          iax_ie_append_str(ied, IAX_IE_MD5_RESULT, digres);
07808          res = 0;
07809       } else if (authmethods & IAX_AUTH_PLAINTEXT) {
07810          iax_ie_append_str(ied, IAX_IE_PASSWORD, secret);
07811          res = 0;
07812       } else
07813          ast_log(LOG_NOTICE, "No way to send secret to peer '%s' (their methods: %d)\n", ast_inet_ntoa(sin->sin_addr), authmethods);
07814    }
07815    return res;
07816 }

static int authenticate_reply ( struct chan_iax2_pvt *  p,
struct sockaddr_in *  sin,
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 7822 of file chan_iax2.c.

References iax2_peer::addr, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_calloc, ast_channel_datastore_add(), ast_datastore_alloc, ast_datastore_free(), AST_FRAME_IAX, ast_free, AST_LIST_HEAD, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_set_flag, ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_var_assign(), authenticate(), iax_ies::authmethods, iax2_peer::authmethods, iax_ie_data::buf, chan_iax2_pvt::callno, chan_iax2_pvt::challenge, iax_ies::challenge, ast_datastore::data, DATASTORE_INHERIT_FOREVER, chan_iax2_pvt::encmethods, iax_ies::encmethods, ast_var_t::entries, IAX_AUTH_MD5, IAX_COMMAND_AUTHREP, IAX_ENCRYPTED, IAX_FORCE_ENCRYPT, IAX_KEYPOPULATED, ast_datastore::inheritance, LOG_ERROR, LOG_NOTICE, iax2_peer::mask, merge_encryption(), ast_variable::name, iax2_peer::name, ast_variable::next, iax2_peer::outkey, chan_iax2_pvt::owner, chan_iax2_pvt::peer, peer_unref(), iax_ie_data::pos, realtime_peer(), iax2_peer::secret, send_command(), chan_iax2_pvt::username, iax2_peer::username, iax_ies::username, ast_variable::value, var, and iax_ies::vars.

Referenced by socket_process().

07823 {
07824    struct iax2_peer *peer = NULL;
07825    /* Start pessimistic */
07826    int res = -1;
07827    int authmethods = 0;
07828    struct iax_ie_data ied;
07829    uint16_t callno = p->callno;
07830 
07831    memset(&ied, 0, sizeof(ied));
07832    
07833    if (ies->username)
07834       ast_string_field_set(p, username, ies->username);
07835    if (ies->challenge)
07836       ast_string_field_set(p, challenge, ies->challenge);
07837    if (ies->authmethods)
07838       authmethods = ies->authmethods;
07839    if (authmethods & IAX_AUTH_MD5)
07840       merge_encryption(p, ies->encmethods);
07841    else
07842       p->encmethods = 0;
07843 
07844    /* Check for override RSA authentication first */
07845    if (!ast_strlen_zero(override) || !ast_strlen_zero(okey)) {
07846       /* Normal password authentication */
07847       res = authenticate(p->challenge, override, okey, authmethods, &ied, sin, p);
07848    } else {
07849       struct ao2_iterator i = ao2_iterator_init(peers, 0);
07850       while ((peer = ao2_iterator_next(&i))) {
07851          if ((ast_strlen_zero(p->peer) || !strcmp(p->peer, peer->name)) 
07852              /* No peer specified at our end, or this is the peer */
07853              && (ast_strlen_zero(peer->username) || (!strcmp(peer->username, p->username)))
07854              /* No username specified in peer rule, or this is the right username */
07855              && (!peer->addr.sin_addr.s_addr || ((sin->sin_addr.s_addr & peer->mask.s_addr) == (peer->addr.sin_addr.s_addr & peer->mask.s_addr)))
07856              /* No specified host, or this is our host */
07857             ) {
07858             res = authenticate(p->challenge, peer->secret, peer->outkey, authmethods, &ied, sin, p);
07859             if (!res) {
07860                peer_unref(peer);
07861                break;
07862             }
07863          }
07864          peer_unref(peer);
07865       }
07866       ao2_iterator_destroy(&i);
07867       if (!peer) {
07868          /* We checked our list and didn't find one.  It's unlikely, but possible, 
07869             that we're trying to authenticate *to* a realtime peer */
07870          const char *peer_name = ast_strdupa(p->peer);
07871          ast_mutex_unlock(&iaxsl[callno]);
07872          if ((peer = realtime_peer(peer_name, NULL))) {
07873             ast_mutex_lock(&iaxsl[callno]);
07874             if (!(p = iaxs[callno])) {
07875                peer_unref(peer);
07876                return -1;
07877             }
07878             res = authenticate(p->challenge, peer->secret,peer->outkey, authmethods, &ied, sin, p);
07879             peer_unref(peer);
07880          }
07881          if (!peer) {
07882             ast_mutex_lock(&iaxsl[callno]);
07883             if (!(p = iaxs[callno]))
07884                return -1;
07885          }
07886       }
07887    }
07888 
07889    if (ies->encmethods) {
07890       ast_set_flag(p, IAX_ENCRYPTED | IAX_KEYPOPULATED);
07891    } else if (ast_test_flag(iaxs[callno], IAX_FORCE_ENCRYPT)) {
07892       ast_log(LOG_NOTICE, "Call initiated without encryption while forceencryption=yes option is set");
07893       return -1;             /* if force encryption is yes, and no encryption methods, then return -1 to hangup */
07894    }
07895    if (!res) {
07896       struct ast_datastore *variablestore;
07897       struct ast_variable *var, *prev = NULL;
07898       AST_LIST_HEAD(, ast_var_t) *varlist;
07899       varlist = ast_calloc(1, sizeof(*varlist));
07900       variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
07901       if (variablestore && varlist && p->owner) {
07902          variablestore->data = varlist;
07903          variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
07904          AST_LIST_HEAD_INIT(varlist);
07905          for (var = ies->vars; var; var = var->next) {
07906             struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
07907             if (prev)
07908                ast_free(prev);
07909             prev = var;
07910             if (!newvar) {
07911                /* Don't abort list traversal, as this would leave ies->vars in an inconsistent state. */
07912                ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
07913             } else {
07914                AST_LIST_INSERT_TAIL(varlist, newvar, entries);
07915             }
07916          }
07917          if (prev)
07918             ast_free(prev);
07919          ies->vars = NULL;
07920          ast_channel_datastore_add(p->owner, variablestore);
07921       } else {
07922          if (p->owner)
07923             ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
07924          if (variablestore)
07925             ast_datastore_free(variablestore);
07926          if (varlist)
07927             ast_free(varlist);
07928       }
07929    }
07930 
07931    if (!res)
07932       res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREP, 0, ied.buf, ied.pos, -1);
07933    return res;
07934 }

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 7472 of file chan_iax2.c.

References ao2_find, AST_CAUSE_CALL_REJECTED, AST_FRAME_IAX, ast_random(), ast_set_flag, ast_string_field_set, ast_test_flag, chan_iax2_pvt::authmethods, iax_ie_data::buf, chan_iax2_pvt::challenge, iax2_user::curauthreq, chan_iax2_pvt::encmethods, IAX_AUTH_MD5, IAX_AUTH_RSA, IAX_COMMAND_AUTHREQ, IAX_COMMAND_REJECT, IAX_ENCRYPTED, iax_ie_append_byte(), iax_ie_append_short(), iax_ie_append_str(), IAX_IE_AUTHMETHODS, IAX_IE_CAUSE, IAX_IE_CAUSECODE, IAX_IE_CHALLENGE, IAX_IE_ENCRYPTION, IAX_IE_USERNAME, IAX_MAXAUTHREQ, iax2_user::maxauthreq, OBJ_POINTER, iax_ie_data::pos, send_command(), send_command_final(), user_unref(), and chan_iax2_pvt::username.

Referenced by socket_process().

07473 {
07474    struct iax_ie_data ied;
07475    int res = -1, authreq_restrict = 0;
07476    char challenge[10];
07477    struct chan_iax2_pvt *p = iaxs[call_num];
07478 
07479    memset(&ied, 0, sizeof(ied));
07480 
07481    /* If an AUTHREQ restriction is in place, make sure we can send an AUTHREQ back */
07482    if (ast_test_flag(p, IAX_MAXAUTHREQ)) {
07483       struct iax2_user *user, tmp_user = {
07484          .name = p->username, 
07485       };
07486 
07487       user = ao2_find(users, &tmp_user, OBJ_POINTER);
07488       if (user) {
07489          if (user->curauthreq == user->maxauthreq)
07490             authreq_restrict = 1;
07491          else
07492             user->curauthreq++;
07493          user = user_unref(user);
07494       }
07495    }
07496 
07497    /* If the AUTHREQ limit test failed, send back an error */
07498    if (authreq_restrict) {
07499       iax_ie_append_str(&ied, IAX_IE_CAUSE, "Unauthenticated call limit reached");
07500       iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_CALL_REJECTED);
07501       send_command_final(p, AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied.buf, ied.pos, -1);
07502       return 0;
07503    }
07504 
07505    iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, p->authmethods);
07506    if (p->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_RSA)) {
07507       snprintf(challenge, sizeof(challenge), "%d", (int)ast_random());
07508       ast_string_field_set(p, challenge, challenge);
07509       /* snprintf(p->challenge, sizeof(p->challenge), "%d", (int)ast_random()); */
07510       iax_ie_append_str(&ied, IAX_IE_CHALLENGE, p->challenge);
07511    }
07512    if (p->encmethods)
07513       iax_ie_append_short(&ied, IAX_IE_ENCRYPTION, p->encmethods);
07514 
07515    iax_ie_append_str(&ied,IAX_IE_USERNAME, p->username);
07516 
07517    res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREQ, 0, ied.buf, ied.pos, -1);
07518 
07519    if (p->encmethods)
07520       ast_set_flag(p, IAX_ENCRYPTED);
07521 
07522    return res;
07523 }

static int authenticate_verify ( struct chan_iax2_pvt *  p,
struct iax_ies *  ies 
) [static]

Definition at line 7525 of file chan_iax2.c.

References ao2_find, ast_atomic_fetchadd_int(), ast_check_signature, ast_clear_flag, ast_copy_string(), ast_key_get, AST_KEY_PUBLIC, ast_log(), ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_test_flag, chan_iax2_pvt::authmethods, chan_iax2_pvt::authrej, chan_iax2_pvt::challenge, iax2_user::curauthreq, chan_iax2_pvt::encmethods, IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_AUTH_RSA, IAX_FORCE_ENCRYPT, IAX_MAXAUTHREQ, IAX_STATE_AUTHENTICATED, chan_iax2_pvt::inkeys, LOG_NOTICE, LOG_WARNING, iax_ies::md5_result, MD5Final(), MD5Init(), MD5Update(), iax2_user::name, OBJ_POINTER, iax_ies::password, iax_ies::rsa_result, chan_iax2_pvt::secret, secret, chan_iax2_pvt::state, strsep(), user_unref(), and chan_iax2_pvt::username.

Referenced by socket_process().

07526 {
07527    char requeststr[256];
07528    char md5secret[256] = "";
07529    char secret[256] = "";
07530    char rsasecret[256] = "";
07531    int res = -1; 
07532    int x;
07533    struct iax2_user *user, tmp_user = {
07534       .name = p->username, 
07535    };
07536 
07537    if (p->authrej) {
07538       return res;
07539    }
07540    user = ao2_find(users, &tmp_user, OBJ_POINTER);
07541    if (user) {
07542       if (ast_test_flag(p, IAX_MAXAUTHREQ)) {
07543          ast_atomic_fetchadd_int(&user->curauthreq, -1);
07544          ast_clear_flag(p, IAX_MAXAUTHREQ);
07545       }
07546       ast_string_field_set(p, host, user->name);
07547       user = user_unref(user);
07548    }
07549    if (ast_test_flag(p, IAX_FORCE_ENCRYPT) && !p->encmethods) { 
07550       ast_log(LOG_NOTICE, "Call Terminated, Incoming call is unencrypted while force encrypt is enabled.");
07551       return res;
07552    }
07553    if (!ast_test_flag(&p->state, IAX_STATE_AUTHENTICATED))
07554       return res;
07555    if (ies->password)
07556       ast_copy_string(secret, ies->password, sizeof(secret));
07557    if (ies->md5_result)
07558       ast_copy_string(md5secret, ies->md5_result, sizeof(md5secret));
07559    if (ies->rsa_result)
07560       ast_copy_string(rsasecret, ies->rsa_result, sizeof(rsasecret));
07561    if ((p->authmethods & IAX_AUTH_RSA) && !ast_strlen_zero(rsasecret) && !ast_strlen_zero(p->inkeys)) {
07562       struct ast_key *key;
07563       char *keyn;
07564       char tmpkey[256];
07565       char *stringp=NULL;
07566       ast_copy_string(tmpkey, p->inkeys, sizeof(tmpkey));
07567       stringp=tmpkey;
07568       keyn = strsep(&stringp, ":");
07569       while(keyn) {
07570          key = ast_key_get(keyn, AST_KEY_PUBLIC);
07571          if (key && !ast_check_signature(key, p->challenge, rsasecret)) {
07572             res = 0;
07573             break;
07574          } else if (!key)
07575             ast_log(LOG_WARNING, "requested inkey '%s' for RSA authentication does not exist\n", keyn);
07576          keyn = strsep(&stringp, ":");
07577       }
07578    } else if (p->authmethods & IAX_AUTH_MD5) {
07579       struct MD5Context md5;
07580       unsigned char digest[16];
07581       char *tmppw, *stringp;
07582       
07583       tmppw = ast_strdupa(p->secret);
07584       stringp = tmppw;
07585       while((tmppw = strsep(&stringp, ";"))) {
07586          MD5Init(&md5);
07587          MD5Update(&md5, (unsigned char *)p->challenge, strlen(p->challenge));
07588          MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
07589          MD5Final(digest, &md5);
07590          /* If they support md5, authenticate with it.  */
07591          for (x=0;x<16;x++)
07592             sprintf(requeststr + (x << 1), "%2.2x", digest[x]); /* safe */
07593          if (!strcasecmp(requeststr, md5secret)) {
07594             res = 0;
07595             break;
07596          }
07597       }
07598    } else if (p->authmethods & IAX_AUTH_PLAINTEXT) {
07599       if (!strcmp(secret, p->secret))
07600          res = 0;
07601    }
07602    return res;
07603 }

static int auto_congest ( const void *  data  )  [static]

Definition at line 4449 of file chan_iax2.c.

References __auto_congest(), and schedule_action.

Referenced by iax2_call().

04450 {
04451 #ifdef SCHED_MULTITHREADED
04452    if (schedule_action(__auto_congest, data))
04453 #endif      
04454       __auto_congest(data);
04455    return 0;
04456 }

static int auto_hangup ( const void *  data  )  [static]

Definition at line 8680 of file chan_iax2.c.

References __auto_hangup(), ast_mutex_lock(), ast_mutex_unlock(), chan_iax2_pvt::autoid, and schedule_action.

Referenced by iax2_dprequest(), and iax2_provision().

08681 {
08682    int callno = (int)(long)(data);
08683    ast_mutex_lock(&iaxsl[callno]);
08684    if (iaxs[callno]) {
08685       iaxs[callno]->autoid = -1;
08686    }
08687    ast_mutex_unlock(&iaxsl[callno]);
08688 #ifdef SCHED_MULTITHREADED
08689    if (schedule_action(__auto_hangup, data))
08690 #endif      
08691       __auto_hangup(data);
08692    return 0;
08693 }

static void build_callno_limits ( struct ast_variable *  v  )  [static]

Definition at line 2279 of file chan_iax2.c.

References ao2_alloc, ao2_find, ao2_link, ao2_lock(), ao2_ref, ao2_unlock(), ast_append_ha(), ast_copy_ha(), ast_free_ha(), ast_log(), addr_range::delme, addr_range::ha, addr_range::limit, LOG_ERROR, ast_variable::name, ast_variable::next, OBJ_POINTER, and ast_variable::value.

Referenced by set_config().

02280 {
02281    struct addr_range *addr_range = NULL;
02282    struct addr_range tmp;
02283    struct ast_ha *ha;
02284    int limit;
02285    int error;
02286    int found;
02287 
02288    for (; v; v = v->next) {
02289       limit = -1;
02290       error = 0;
02291       found = 0;
02292       ha = ast_append_ha("permit", v->name, NULL, &error);
02293 
02294       /* check for valid config information */
02295       if (error) {
02296          ast_log(LOG_ERROR, "Call number limit for %s could not be added, Invalid address range\n.", v->name);
02297          continue;
02298       } else if ((sscanf(v->value, "%d", &limit) != 1) || (limit < 0)) {
02299          ast_log(LOG_ERROR, "Call number limit for %s could not be added. Invalid limit %s\n.", v->name, v->value);
02300          ast_free_ha(ha);
02301          continue;
02302       }
02303 
02304       ast_copy_ha(ha, &tmp.ha);
02305       /* find or create the addr_range */
02306       if ((addr_range = ao2_find(callno_limits, &tmp, OBJ_POINTER))) {
02307          ao2_lock(addr_range);
02308          found = 1;
02309       } else if (!(addr_range = ao2_alloc(sizeof(*addr_range), NULL))) {
02310          ast_free_ha(ha);
02311          return; /* out of memory */
02312       }
02313 
02314       /* copy over config data into addr_range object */
02315       ast_copy_ha(ha, &addr_range->ha); /* this is safe because only one ha is possible for each limit */
02316       ast_free_ha(ha); /* cleanup the tmp ha */
02317       addr_range->limit = limit;
02318       addr_range->delme = 0;
02319 
02320       /* cleanup */
02321       if (found) {
02322          ao2_unlock(addr_range);
02323       } else {
02324          ao2_link(callno_limits, addr_range);
02325       }
02326       ao2_ref(addr_range, -1); /* decrement ref from ao2_find and ao2_alloc, only container ref remains */
02327    }
02328 }

static struct iax2_context* build_context ( const char *  context  )  [static, read]

Definition at line 11753 of file chan_iax2.c.

References ast_calloc, ast_copy_string(), and iax2_context::context.

Referenced by build_user().

11754 {
11755    struct iax2_context *con;
11756 
11757    if ((con = ast_calloc(1, sizeof(*con))))
11758       ast_copy_string(con->context, context, sizeof(con->context));
11759    
11760    return con;
11761 }

static void build_ecx_key ( const unsigned char *  digest,
struct chan_iax2_pvt *  pvt 
) [static]

Definition at line 5883 of file chan_iax2.c.

References ast_aes_decrypt_key, ast_aes_encrypt_key, build_rand_pad(), chan_iax2_pvt::ecx, chan_iax2_pvt::mydcx, and chan_iax2_pvt::semirand.

Referenced by build_encryption_keys(), and iax2_key_rotate().

05884 {
05885    /* it is required to hold the corresponding decrypt key to our encrypt key
05886     * in the pvt struct because queued frames occasionally need to be decrypted and
05887     * re-encrypted when updated for a retransmission */
05888    build_rand_pad(pvt->semirand, sizeof(pvt->semirand));
05889    ast_aes_encrypt_key(digest, &pvt->ecx);
05890    ast_aes_decrypt_key(digest, &pvt->mydcx);
05891 }

static void build_encryption_keys ( const unsigned char *  digest,
struct chan_iax2_pvt *  pvt 
) [static]

Definition at line 5877 of file chan_iax2.c.

References ast_aes_decrypt_key, build_ecx_key(), and chan_iax2_pvt::dcx.

Referenced by authenticate(), and decrypt_frame().

05878 {
05879    build_ecx_key(digest, pvt);
05880    ast_aes_decrypt_key(digest, &pvt->dcx);
05881 }

static struct iax2_peer * build_peer ( const char *  name,
struct ast_variable *  v,
struct ast_variable *  alt,
int  temponly 
) [static, read]

Create peer structure based on configuration.

Definition at line 11901 of file chan_iax2.c.

References iax2_peer::addr, iax2_peer::adsi, ao2_alloc, ao2_find, ast_append_ha(), ast_callerid_split(), ast_clear_flag, ast_copy_flags, ast_dnsmgr_lookup(), AST_EVENT_IE_CONTEXT, AST_EVENT_IE_END, AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_MWI, ast_event_subscribe(), ast_false(), ast_free_ha(), ast_get_ip(), ast_log(), ast_parse_allow_disallow(), ast_sched_thread_del, ast_set2_flag, ast_set_flag, ast_set_flags_to, ast_strdupa, ast_string_field_init, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_true(), iax2_peer::authmethods, CALLTOKEN_AUTO, CALLTOKEN_DEFAULT, CALLTOKEN_NO, iax2_peer::calltoken_required, CALLTOKEN_YES, iax2_peer::capability, cid_name, cid_num, context, iax2_peer::dbsecret, iax2_peer::defaddr, DEFAULT_FREQ_NOTOK, DEFAULT_FREQ_OK, DEFAULT_MAXMS, iax2_peer::dnsmgr, iax2_peer::encmethods, iax2_peer::expire, iax2_peer::expiry, get_auth_methods(), get_encrypt_methods(), iax2_peer::ha, IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_DEFAULT_PORTNO, IAX_DELME, IAX_DYNAMIC, IAX_FORCE_ENCRYPT, IAX_FORCEJITTERBUF, IAX_HASCALLERID, IAX_NOTRANSFER, IAX_SENDANI, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, inet_aton(), iax2_peer::inkeys, ast_variable::lineno, LOG_WARNING, iax2_peer::mailbox, mailbox, iax2_peer::mask, iax2_peer::maxcallno, iax2_peer::maxms, mwi_event_cb(), iax2_peer::mwi_event_sub, ast_variable::name, iax2_peer::name, ast_variable::next, OBJ_POINTER, iax2_peer::outkey, peer_destructor(), peer_set_srcaddr(), peer_unref(), peercnt_modify(), iax2_peer::peercontext, iax2_peer::pokeexpire, iax2_peer::pokefreqnotok, iax2_peer::pokefreqok, prefs, iax2_peer::prefs, iax2_peer::regexten, S_OR, secret, iax2_peer::smoothing, iax2_peer::sockfd, strsep(), unlink_peer(), ast_variable::value, and zonetag.

Referenced by realtime_peer(), and set_config().

11902 {
11903    struct iax2_peer *peer = NULL;
11904    struct ast_ha *oldha = NULL;
11905    int maskfound = 0;
11906    int found = 0;
11907    int firstpass = 1;
11908    struct iax2_peer tmp_peer = {
11909       .name = name,
11910    };
11911 
11912    if (!temponly) {
11913       peer = ao2_find(peers, &tmp_peer, OBJ_POINTER);
11914       if (peer && !ast_test_flag(peer, IAX_DELME))
11915          firstpass = 0;
11916    }
11917 
11918    if (peer) {
11919       found++;
11920       if (firstpass) {
11921          oldha = peer->ha;
11922          peer->ha = NULL;
11923       }
11924       unlink_peer(peer);
11925    } else if ((peer = ao2_alloc(sizeof(*peer), peer_destructor))) {
11926       peer->expire = -1;
11927       peer->pokeexpire = -1;
11928       peer->sockfd = defaultsockfd;
11929       if (ast_string_field_init(peer, 32))
11930          peer = peer_unref(peer);
11931    }
11932 
11933    if (peer) {
11934       if (firstpass) {
11935          ast_copy_flags(peer, &globalflags, IAX_USEJITTERBUF | IAX_FORCEJITTERBUF | IAX_FORCE_ENCRYPT);
11936          peer->encmethods = iax2_encryption;
11937          peer->adsi = adsi;
11938          ast_string_field_set(peer,secret,"");
11939          if (!found) {
11940             ast_string_field_set(peer, name, name);
11941             peer->addr.sin_port = htons(IAX_DEFAULT_PORTNO);
11942             peer->expiry = min_reg_expire;
11943          }
11944          peer->prefs = prefs;
11945          peer->capability = iax2_capability;
11946          peer->smoothing = 0;
11947          peer->pokefreqok = DEFAULT_FREQ_OK;
11948          peer->pokefreqnotok = DEFAULT_FREQ_NOTOK;
11949          peer->maxcallno = 0;
11950          peercnt_modify(0, 0, &peer->addr);
11951          peer->calltoken_required = CALLTOKEN_DEFAULT;
11952          ast_string_field_set(peer,context,"");
11953          ast_string_field_set(peer,peercontext,"");
11954          ast_clear_flag(peer, IAX_HASCALLERID);
11955          ast_string_field_set(peer, cid_name, "");
11956          ast_string_field_set(peer, cid_num, "");
11957          ast_string_field_set(peer, mohinterpret, mohinterpret);
11958          ast_string_field_set(peer, mohsuggest, mohsuggest);
11959       }
11960 
11961       if (!v) {
11962          v = alt;
11963          alt = NULL;
11964       }
11965       while(v) {
11966          if (!strcasecmp(v->name, "secret")) {
11967             ast_string_field_set(peer, secret, v->value);
11968          } else if (!strcasecmp(v->name, "mailbox")) {
11969             ast_string_field_set(peer, mailbox, v->value);
11970          } else if (!strcasecmp(v->name, "hasvoicemail")) {
11971             if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
11972                ast_string_field_set(peer, mailbox, name);
11973             }
11974          } else if (!strcasecmp(v->name, "mohinterpret")) {
11975             ast_string_field_set(peer, mohinterpret, v->value);
11976          } else if (!strcasecmp(v->name, "mohsuggest")) {
11977             ast_string_field_set(peer, mohsuggest, v->value);
11978          } else if (!strcasecmp(v->name, "dbsecret")) {
11979             ast_string_field_set(peer, dbsecret, v->value);
11980          } else if (!strcasecmp(v->name, "trunk")) {
11981             ast_set2_flag(peer, ast_true(v->value), IAX_TRUNK);   
11982             if (ast_test_flag(peer, IAX_TRUNK) && !timer) {
11983                ast_log(LOG_WARNING, "Unable to support trunking on peer '%s' without a timing interface\n", peer->name);
11984                ast_clear_flag(peer, IAX_TRUNK);
11985             }
11986          } else if (!strcasecmp(v->name, "auth")) {
11987             peer->authmethods = get_auth_methods(v->value);
11988          } else if (!strcasecmp(v->name, "encryption")) {
11989             peer->encmethods |= get_encrypt_methods(v->value);
11990             if (!peer->encmethods) {
11991                ast_clear_flag(peer, IAX_FORCE_ENCRYPT);
11992             }
11993          } else if (!strcasecmp(v->name, "forceencryption")) {
11994             if (ast_false(v->value)) {
11995                ast_clear_flag(peer, IAX_FORCE_ENCRYPT);
11996             } else {
11997                peer->encmethods |= get_encrypt_methods(v->value);
11998                if (peer->encmethods) {
11999                   ast_set_flag(peer, IAX_FORCE_ENCRYPT);
12000                }
12001             }
12002          } else if (!strcasecmp(v->name, "transfer")) {
12003             if (!strcasecmp(v->value, "mediaonly")) {
12004                ast_set_flags_to(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA);  
12005             } else if (ast_true(v->value)) {
12006                ast_set_flags_to(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
12007             } else 
12008                ast_set_flags_to(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
12009          } else if (!strcasecmp(v->name, "jitterbuffer")) {
12010             ast_set2_flag(peer, ast_true(v->value), IAX_USEJITTERBUF);  
12011          } else if (!strcasecmp(v->name, "forcejitterbuffer")) {
12012             ast_set2_flag(peer, ast_true(v->value), IAX_FORCEJITTERBUF);   
12013          } else if (!strcasecmp(v->name, "host")) {
12014             if (!strcasecmp(v->value, "dynamic")) {
12015                /* They'll register with us */
12016                ast_set_flag(peer, IAX_DYNAMIC); 
12017                if (!found) {
12018                   /* Initialize stuff iff we're not found, otherwise
12019                      we keep going with what we had */
12020                   memset(&peer->addr.sin_addr, 0, 4);
12021                   if (peer->addr.sin_port) {
12022                      /* If we've already got a port, make it the default rather than absolute */
12023                      peer->defaddr.sin_port = peer->addr.sin_port;
12024                      peer->addr.sin_port = 0;
12025                   }
12026                }
12027             } else {
12028                /* Non-dynamic.  Make sure we become that way if we're not */
12029                ast_sched_thread_del(sched, peer->expire);
12030                ast_clear_flag(peer, IAX_DYNAMIC);
12031                if (ast_dnsmgr_lookup(v->value, &peer->addr, &peer->dnsmgr, srvlookup ? "_iax._udp" : NULL))
12032                   return peer_unref(peer);
12033                if (!peer->addr.sin_port)
12034                   peer->addr.sin_port = htons(IAX_DEFAULT_PORTNO);
12035             }
12036             if (!maskfound)
12037                inet_aton("255.255.255.255", &peer->mask);
12038          } else if (!strcasecmp(v->name, "defaultip")) {
12039             if (ast_get_ip(&peer->defaddr, v->value))
12040                return peer_unref(peer);
12041          } else if (!strcasecmp(v->name, "sourceaddress")) {
12042             peer_set_srcaddr(peer, v->value);
12043          } else if (!strcasecmp(v->name, "permit") ||
12044                   !strcasecmp(v->name, "deny")) {
12045             peer->ha = ast_append_ha(v->name, v->value, peer->ha, NULL);
12046          } else if (!strcasecmp(v->name, "mask")) {
12047             maskfound++;
12048             inet_aton(v->value, &peer->mask);
12049          } else if (!strcasecmp(v->name, "context")) {
12050             ast_string_field_set(peer, context, v->value);
12051          } else if (!strcasecmp(v->name, "regexten")) {
12052             ast_string_field_set(peer, regexten, v->value);
12053          } else if (!strcasecmp(v->name, "peercontext")) {
12054             ast_string_field_set(peer, peercontext, v->value);
12055          } else if (!strcasecmp(v->name, "port")) {
12056             if (ast_test_flag(peer, IAX_DYNAMIC))
12057                peer->defaddr.sin_port = htons(atoi(v->value));
12058             else
12059                peer->addr.sin_port = htons(atoi(v->value));
12060          } else if (!strcasecmp(v->name, "username")) {
12061             ast_string_field_set(peer, username, v->value);
12062          } else if (!strcasecmp(v->name, "allow")) {
12063             ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
12064          } else if (!strcasecmp(v->name, "disallow")) {
12065             ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
12066          } else if (!strcasecmp(v->name, "callerid")) {
12067             if (!ast_strlen_zero(v->value)) {
12068                char name2[80];
12069                char num2[80];
12070                ast_callerid_split(v->value, name2, sizeof(name2), num2, sizeof(num2));
12071                ast_string_field_set(peer, cid_name, name2);
12072                ast_string_field_set(peer, cid_num, num2);
12073             } else {
12074                ast_string_field_set(peer, cid_name, "");
12075                ast_string_field_set(peer, cid_num, "");
12076             }
12077             ast_set_flag(peer, IAX_HASCALLERID);
12078          } else if (!strcasecmp(v->name, "fullname")) {
12079             ast_string_field_set(peer, cid_name, S_OR(v->value, ""));
12080             ast_set_flag(peer, IAX_HASCALLERID);
12081          } else if (!strcasecmp(v->name, "cid_number")) {
12082             ast_string_field_set(peer, cid_num, S_OR(v->value, ""));
12083             ast_set_flag(peer, IAX_HASCALLERID);
12084          } else if (!strcasecmp(v->name, "sendani")) {
12085             ast_set2_flag(peer, ast_true(v->value), IAX_SENDANI); 
12086          } else if (!strcasecmp(v->name, "inkeys")) {
12087             ast_string_field_set(peer, inkeys, v->value);
12088          } else if (!strcasecmp(v->name, "outkey")) {
12089             ast_string_field_set(peer, outkey, v->value);
12090          } else if (!strcasecmp(v->name, "qualify")) {
12091             if (!strcasecmp(v->value, "no")) {
12092                peer->maxms = 0;
12093             } else if (!strcasecmp(v->value, "yes")) {
12094                peer->maxms = DEFAULT_MAXMS;
12095             } else if (sscanf(v->value, "%30d", &peer->maxms) != 1) {
12096                ast_log(LOG_WARNING, "Qualification of peer '%s' should be 'yes', 'no', or a number of milliseconds at line %d of iax.conf\n", peer->name, v->lineno);
12097                peer->maxms = 0;
12098             }
12099          } else if (!strcasecmp(v->name, "qualifysmoothing")) {
12100             peer->smoothing = ast_true(v->value);
12101          } else if (!strcasecmp(v->name, "qualifyfreqok")) {
12102             if (sscanf(v->value, "%30d", &peer->pokefreqok) != 1) {
12103                ast_log(LOG_WARNING, "Qualification testing frequency of peer '%s' when OK should a number of milliseconds at line %d of iax.conf\n", peer->name, v->lineno);
12104             }
12105          } else if (!strcasecmp(v->name, "qualifyfreqnotok")) {
12106             if (sscanf(v->value, "%30d", &peer->pokefreqnotok) != 1) {
12107                ast_log(LOG_WARNING, "Qualification testing frequency of peer '%s' when NOT OK should be a number of milliseconds at line %d of iax.conf\n", peer->name, v->lineno);
12108             } else ast_log(LOG_WARNING, "Set peer->pokefreqnotok to %d\n", peer->pokefreqnotok);
12109          } else if (!strcasecmp(v->name, "timezone")) {
12110             ast_string_field_set(peer, zonetag, v->value);
12111          } else if (!strcasecmp(v->name, "adsi")) {
12112             peer->adsi = ast_true(v->value);
12113          } else if (!strcasecmp(v->name, "maxcallnumbers")) {
12114             if (sscanf(v->value, "%10hu", &peer->maxcallno) != 1) {
12115                ast_log(LOG_WARNING, "maxcallnumbers must be set to a valid number. %s is not valid at line %d.\n", v->value, v->lineno);
12116             } else {
12117                peercnt_modify(1, peer->maxcallno, &peer->addr);
12118             }
12119          } else if (!strcasecmp(v->name, "requirecalltoken")) {
12120             /* default is required unless in optional ip list */
12121             if (ast_false(v->value)) {
12122                peer->calltoken_required = CALLTOKEN_NO;
12123             } else if (!strcasecmp(v->value, "auto")) {
12124                peer->calltoken_required = CALLTOKEN_AUTO;
12125             } else if (ast_true(v->value)) {
12126                peer->calltoken_required = CALLTOKEN_YES;
12127             } else {
12128                ast_log(LOG_WARNING, "requirecalltoken must be set to a valid value. at line %d\n", v->lineno);
12129             }
12130          } /* else if (strcasecmp(v->name,"type")) */
12131          /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
12132          v = v->next;
12133          if (!v) {
12134             v = alt;
12135             alt = NULL;
12136          }
12137       }
12138       if (!peer->authmethods)
12139          peer->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
12140       ast_clear_flag(peer, IAX_DELME); 
12141       /* Make sure these are IPv4 addresses */
12142       peer->addr.sin_family = AF_INET;
12143    }
12144 
12145    if (oldha)
12146       ast_free_ha(oldha);
12147 
12148    if (!ast_strlen_zero(peer->mailbox)) {
12149       char *mailbox, *context;
12150       context = mailbox = ast_strdupa(peer->mailbox);
12151       strsep(&context, "@");
12152       if (ast_strlen_zero(context))
12153          context = "default";
12154       peer->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, NULL,
12155          AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
12156          AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
12157          AST_EVENT_IE_END);
12158    }
12159 
12160    return peer;
12161 }

static void build_rand_pad ( unsigned char *  buf,
ssize_t  len 
) [static]

Definition at line 5867 of file chan_iax2.c.

References ast_random().

Referenced by build_ecx_key(), and update_packet().

05868 {
05869    long tmp;
05870    for (tmp = ast_random(); len > 0; tmp = ast_random()) {
05871       memcpy(buf, (unsigned char *) &tmp, (len > sizeof(tmp)) ? sizeof(tmp) : len);
05872       buf += sizeof(tmp);
05873       len -= sizeof(tmp);
05874    }
05875 }

static struct iax2_user * build_user ( const char *  name,
struct ast_variable *  v,
struct ast_variable *  alt,
int  temponly 
) [static, read]

Create in-memory user structure from configuration.

Definition at line 12177 of file chan_iax2.c.

References iax2_user::adsi, iax2_user::amaflags, ao2_alloc, ao2_find, ao2_unlink, ast_append_ha(), ast_callerid_split(), ast_cdr_amaflags2int(), ast_clear_flag, ast_copy_flags, ast_false(), ast_free_ha(), ast_log(), ast_parse_allow_disallow(), ast_set2_flag, ast_set_flag, ast_set_flags_to, ast_strdupa, ast_string_field_build, ast_string_field_free_memory, ast_string_field_init, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_true(), ast_variable_new(), iax2_user::authmethods, build_context(), CALLTOKEN_AUTO, CALLTOKEN_DEFAULT, CALLTOKEN_NO, iax2_user::calltoken_required, CALLTOKEN_YES, iax2_user::capability, iax2_user::cid_name, cid_name, iax2_user::cid_num, cid_num, cleanup(), iax2_user::contexts, iax2_user::curauthreq, iax2_user::dbsecret, iax2_user::encmethods, format, free_context(), get_auth_methods(), get_encrypt_methods(), iax2_user::ha, IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_AUTH_RSA, IAX_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, IAX_DELME, IAX_FORCE_ENCRYPT, IAX_FORCEJITTERBUF, IAX_HASCALLERID, IAX_IMMEDIATE, IAX_NOTRANSFER, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, iax2_user::inkeys, ast_variable::lineno, LOG_WARNING, iax2_user::maxauthreq, ast_variable::name, iax2_user::name, ast_variable::next, iax2_context::next, OBJ_POINTER, parkinglot, prefs, iax2_user::prefs, secret, iax2_user::secret, user_destructor(), user_unref(), ast_variable::value, and iax2_user::vars.

Referenced by realtime_user(), and set_config().

12178 {
12179    struct iax2_user *user = NULL;
12180    struct iax2_context *con, *conl = NULL;
12181    struct ast_ha *oldha = NULL;
12182    struct iax2_context *oldcon = NULL;
12183    int format;
12184    int firstpass=1;
12185    int oldcurauthreq = 0;
12186    char *varname = NULL, *varval = NULL;
12187    struct ast_variable *tmpvar = NULL;
12188    struct iax2_user tmp_user = {
12189       .name = name,
12190    };
12191 
12192    if (!temponly) {
12193       user = ao2_find(users, &tmp_user, OBJ_POINTER);
12194       if (user && !ast_test_flag(user, IAX_DELME))
12195          firstpass = 0;
12196    }
12197 
12198    if (user) {
12199       if (firstpass) {
12200          oldcurauthreq = user->curauthreq;
12201          oldha = user->ha;
12202          oldcon = user->contexts;
12203          user->ha = NULL;
12204          user->contexts = NULL;
12205       }
12206       /* Already in the list, remove it and it will be added back (or FREE'd) */
12207       ao2_unlink(users, user);
12208    } else {
12209       user = ao2_alloc(sizeof(*user), user_destructor);
12210    }
12211    
12212    if (user) {
12213       if (firstpass) {
12214          ast_string_field_free_memory(user);
12215          memset(user, 0, sizeof(struct iax2_user));
12216          if (ast_string_field_init(user, 32)) {
12217             user = user_unref(user);
12218             goto cleanup;
12219          }
12220          user->maxauthreq = maxauthreq;
12221          user->curauthreq = oldcurauthreq;
12222          user->prefs = prefs;
12223          user->capability = iax2_capability;
12224          user->encmethods = iax2_encryption;
12225          user->adsi = adsi;
12226          user->calltoken_required = CALLTOKEN_DEFAULT;
12227          ast_string_field_set(user, name, name);
12228          ast_string_field_set(user, language, language);
12229          ast_copy_flags(user, &globalflags, IAX_USEJITTERBUF | IAX_FORCEJITTERBUF | IAX_CODEC_USER_FIRST | IAX_CODEC_NOPREFS | IAX_CODEC_NOCAP | IAX_FORCE_ENCRYPT); 
12230          ast_clear_flag(user, IAX_HASCALLERID);
12231          ast_string_field_set(user, cid_name, "");
12232          ast_string_field_set(user, cid_num, "");
12233          ast_string_field_set(user, accountcode, accountcode);
12234          ast_string_field_set(user, mohinterpret, mohinterpret);
12235          ast_string_field_set(user, mohsuggest, mohsuggest);
12236       }
12237       if (!v) {
12238          v = alt;
12239          alt = NULL;
12240       }
12241       while(v) {
12242          if (!strcasecmp(v->name, "context")) {
12243             con = build_context(v->value);
12244             if (con) {
12245                if (conl)
12246                   conl->next = con;
12247                else
12248                   user->contexts = con;
12249                conl = con;
12250             }
12251          } else if (!strcasecmp(v->name, "permit") ||
12252                   !strcasecmp(v->name, "deny")) {
12253             user->ha = ast_append_ha(v->name, v->value, user->ha, NULL);
12254          } else if (!strcasecmp(v->name, "setvar")) {
12255             varname = ast_strdupa(v->value);
12256             if (varname && (varval = strchr(varname,'='))) {
12257                *varval = '\0';
12258                varval++;
12259                if((tmpvar = ast_variable_new(varname, varval, ""))) {
12260                   tmpvar->next = user->vars; 
12261                   user->vars = tmpvar;
12262                }
12263             }
12264          } else if (!strcasecmp(v->name, "allow")) {
12265             ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
12266          } else if (!strcasecmp(v->name, "disallow")) {
12267             ast_parse_allow_disallow(&user->prefs, &user->capability,v->value, 0);
12268          } else if (!strcasecmp(v->name, "trunk")) {
12269             ast_set2_flag(user, ast_true(v->value), IAX_TRUNK);   
12270             if (ast_test_flag(user, IAX_TRUNK) && !timer) {
12271                ast_log(LOG_WARNING, "Unable to support trunking on user '%s' without a timing interface\n", user->name);
12272                ast_clear_flag(user, IAX_TRUNK);
12273             }
12274          } else if (!strcasecmp(v->name, "auth")) {
12275             user->authmethods = get_auth_methods(v->value);
12276          } else if (!strcasecmp(v->name, "encryption")) {
12277             user->encmethods |= get_encrypt_methods(v->value);
12278             if (!user->encmethods) {
12279                ast_clear_flag(user, IAX_FORCE_ENCRYPT);
12280             }
12281          } else if (!strcasecmp(v->name, "forceencryption")) {
12282             if (ast_false(v->value)) {
12283                ast_clear_flag(user, IAX_FORCE_ENCRYPT);
12284             } else {
12285                user->encmethods |= get_encrypt_methods(v->value);
12286                if (user->encmethods) {
12287                   ast_set_flag(user, IAX_FORCE_ENCRYPT);
12288                }
12289             }
12290          } else if (!strcasecmp(v->name, "transfer")) {
12291             if (!strcasecmp(v->value, "mediaonly")) {
12292                ast_set_flags_to(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA);  
12293             } else if (ast_true(v->value)) {
12294                ast_set_flags_to(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
12295             } else 
12296                ast_set_flags_to(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
12297          } else if (!strcasecmp(v->name, "codecpriority")) {
12298             if(!strcasecmp(v->value, "caller"))
12299                ast_set_flag(user, IAX_CODEC_USER_FIRST);
12300             else if(!strcasecmp(v->value, "disabled"))
12301                ast_set_flag(user, IAX_CODEC_NOPREFS);
12302             else if(!strcasecmp(v->value, "reqonly")) {
12303                ast_set_flag(user, IAX_CODEC_NOCAP);
12304                ast_set_flag(user, IAX_CODEC_NOPREFS);
12305             }
12306          } else if (!strcasecmp(v->name, "immediate")) {
12307             ast_set2_flag(user, ast_true(v->value), IAX_IMMEDIATE);
12308          } else if (!strcasecmp(v->name, "jitterbuffer")) {
12309             ast_set2_flag(user, ast_true(v->value), IAX_USEJITTERBUF);
12310          } else if (!strcasecmp(v->name, "forcejitterbuffer")) {
12311             ast_set2_flag(user, ast_true(v->value), IAX_FORCEJITTERBUF);
12312          } else if (!strcasecmp(v->name, "dbsecret")) {
12313             ast_string_field_set(user, dbsecret, v->value);
12314          } else if (!strcasecmp(v->name, "secret")) {
12315             if (!ast_strlen_zero(user->secret)) {
12316                char *old = ast_strdupa(user->secret);
12317 
12318                ast_string_field_build(user, secret, "%s;%s", old, v->value);
12319             } else
12320                ast_string_field_set(user, secret, v->value);
12321          } else if (!strcasecmp(v->name, "callerid")) {
12322             if (!ast_strlen_zero(v->value) && strcasecmp(v->value, "asreceived")) {
12323                char name2[80];
12324                char num2[80];
12325                ast_callerid_split(v->value, name2, sizeof(name2), num2, sizeof(num2));
12326                ast_string_field_set(user, cid_name, name2);
12327                ast_string_field_set(user, cid_num, num2);
12328                ast_set_flag(user, IAX_HASCALLERID);
12329             } else {
12330                ast_clear_flag(user, IAX_HASCALLERID);
12331                ast_string_field_set(user, cid_name, "");
12332                ast_string_field_set(user, cid_num, "");
12333             }
12334          } else if (!strcasecmp(v->name, "fullname")) {
12335             if (!ast_strlen_zero(v->value)) {
12336                ast_string_field_set(user, cid_name, v->value);
12337                ast_set_flag(user, IAX_HASCALLERID);
12338             } else {
12339                ast_string_field_set(user, cid_name, "");
12340                if (ast_strlen_zero(user->cid_num))
12341                   ast_clear_flag(user, IAX_HASCALLERID);
12342             }
12343          } else if (!strcasecmp(v->name, "cid_number")) {
12344             if (!ast_strlen_zero(v->value)) {
12345                ast_string_field_set(user, cid_num, v->value);
12346                ast_set_flag(user, IAX_HASCALLERID);
12347             } else {
12348                ast_string_field_set(user, cid_num, "");
12349                if (ast_strlen_zero(user->cid_name))
12350                   ast_clear_flag(user, IAX_HASCALLERID);
12351             }
12352          } else if (!strcasecmp(v->name, "accountcode")) {
12353             ast_string_field_set(user, accountcode, v->value);
12354          } else if (!strcasecmp(v->name, "mohinterpret")) {
12355             ast_string_field_set(user, mohinterpret, v->value);
12356          } else if (!strcasecmp(v->name, "mohsuggest")) {
12357             ast_string_field_set(user, mohsuggest, v->value);
12358          } else if (!strcasecmp(v->name, "parkinglot")) {
12359             ast_string_field_set(user, parkinglot, v->value);
12360          } else if (!strcasecmp(v->name, "language")) {
12361             ast_string_field_set(user, language, v->value);
12362          } else if (!strcasecmp(v->name, "amaflags")) {
12363             format = ast_cdr_amaflags2int(v->value);
12364             if (format < 0) {
12365                ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
12366             } else {
12367                user->amaflags = format;
12368             }
12369          } else if (!strcasecmp(v->name, "inkeys")) {
12370             ast_string_field_set(user, inkeys, v->value);
12371          } else if (!strcasecmp(v->name, "maxauthreq")) {
12372             user->maxauthreq = atoi(v->value);
12373             if (user->maxauthreq < 0)
12374                user->maxauthreq = 0;
12375          } else if (!strcasecmp(v->name, "adsi")) {
12376             user->adsi = ast_true(v->value);
12377          } else if (!strcasecmp(v->name, "requirecalltoken")) {
12378             /* default is required unless in optional ip list */
12379             if (ast_false(v->value)) {
12380                user->calltoken_required = CALLTOKEN_NO;
12381             } else if (!strcasecmp(v->value, "auto")) {
12382                user->calltoken_required = CALLTOKEN_AUTO;
12383             } else if (ast_true(v->value)) {
12384                user->calltoken_required = CALLTOKEN_YES;
12385             } else {
12386                ast_log(LOG_WARNING, "requirecalltoken must be set to a valid value. at line %d\n", v->lineno);
12387             }
12388          } /* else if (strcasecmp(v->name,"type")) */
12389          /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
12390          v = v->next;
12391          if (!v) {
12392             v = alt;
12393             alt = NULL;
12394          }
12395       }
12396       if (!user->authmethods) {
12397          if (!ast_strlen_zero(user->secret)) {
12398             user->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
12399             if (!ast_strlen_zero(user->inkeys))
12400                user->authmethods |= IAX_AUTH_RSA;
12401          } else if (!ast_strlen_zero(user->inkeys)) {
12402             user->authmethods = IAX_AUTH_RSA;
12403          } else {
12404             user->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
12405          }
12406       }
12407       ast_clear_flag(user, IAX_DELME);
12408    }
12409 cleanup:
12410    if (oldha)
12411       ast_free_ha(oldha);
12412    if (oldcon)
12413       free_context(oldcon);
12414    return user;
12415 }

static int cache_get_callno_locked ( const char *  data  )  [static]

Definition at line 13022 of file chan_iax2.c.

References add_empty_calltoken_ie(), ARRAY_LEN, ast_debug, AST_FRAME_IAX, ast_log(), ast_mutex_trylock(), ast_mutex_unlock(), ast_strdupa, ast_string_field_set, ast_strlen_zero(), iax_ie_data::buf, chan_iax2_pvt::capability, parsed_dial_string::context, create_addr(), parsed_dial_string::exten, find_callno_locked(), IAX_CAPABILITY_FULLBANDWIDTH, IAX_COMMAND_NEW, iax_ie_append_int(), iax_ie_append_short(), iax_ie_append_str(), IAX_IE_CALLED_CONTEXT, IAX_IE_CALLED_NUMBER, IAX_IE_CAPABILITY, IAX_IE_FORMAT, IAX_IE_USERNAME, IAX_IE_VERSION, IAX_PROTO_VERSION, parsed_dial_string::key, LOG_WARNING, NEW_FORCE, parse_dial_string(), parsed_dial_string::password, parsed_dial_string::peer, iax_ie_data::pos, secret, send_command(), create_addr_info::sockfd, and parsed_dial_string::username.

Referenced by find_cache().

13023 {
13024    struct sockaddr_in sin;
13025    int x;
13026    int callno;
13027    struct iax_ie_data ied;
13028    struct create_addr_info cai;
13029    struct parsed_dial_string pds;
13030    char *tmpstr;
13031 
13032    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
13033       /* Look for an *exact match* call.  Once a call is negotiated, it can only
13034          look up entries for a single context */
13035       if (!ast_mutex_trylock(&iaxsl[x])) {
13036          if (iaxs[x] && !strcasecmp(data, iaxs[x]->dproot))
13037             return x;
13038          ast_mutex_unlock(&iaxsl[x]);
13039       }
13040    }
13041 
13042    /* No match found, we need to create a new one */
13043 
13044    memset(&cai, 0, sizeof(cai));
13045    memset(&ied, 0, sizeof(ied));
13046    memset(&pds, 0, sizeof(pds));
13047 
13048    tmpstr = ast_strdupa(data);
13049    parse_dial_string(tmpstr, &pds);
13050 
13051    if (ast_strlen_zero(pds.peer)) {
13052       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", data);
13053       return -1;
13054    }
13055 
13056    /* Populate our address from the given */
13057    if (create_addr(pds.peer, NULL, &sin, &cai))
13058       return -1;
13059 
13060    ast_debug(1, "peer: %s, username: %s, password: %s, context: %s\n",
13061       pds.peer, pds.username, pds.password, pds.context);
13062 
13063    callno = find_callno_locked(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
13064    if (callno < 1) {
13065       ast_log(LOG_WARNING, "Unable to create call\n");
13066       return -1;
13067    }
13068 
13069    ast_string_field_set(iaxs[callno], dproot, data);
13070    iaxs[callno]->capability = IAX_CAPABILITY_FULLBANDWIDTH;
13071 
13072    iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
13073    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, "TBD");
13074    /* the string format is slightly different from a standard dial string,
13075       because the context appears in the 'exten' position
13076    */
13077    if (pds.exten)
13078       iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, pds.exten);
13079    if (pds.username)
13080       iax_ie_append_str(&ied, IAX_IE_USERNAME, pds.username);
13081    iax_ie_append_int(&ied, IAX_IE_FORMAT, IAX_CAPABILITY_FULLBANDWIDTH);
13082    iax_ie_append_int(&ied, IAX_IE_CAPABILITY, IAX_CAPABILITY_FULLBANDWIDTH);
13083    /* Keep password handy */
13084    if (pds.password)
13085       ast_string_field_set(iaxs[callno], secret, pds.password);
13086    if (pds.key)
13087       ast_string_field_set(iaxs[callno], outkey, pds.key);
13088    /* Start the call going */
13089    add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
13090    send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
13091 
13092    return callno;
13093 }

static unsigned int calc_rxstamp ( struct chan_iax2_pvt *  p,
unsigned int  offset 
) [static]

Definition at line 5719 of file chan_iax2.c.

References ast_debug, ast_random(), ast_samp2tv(), ast_tvdiff_ms(), ast_tvnow(), ast_tvsub(), ast_tvzero(), chan_iax2_pvt::callno, and chan_iax2_pvt::rxcore.

Referenced by schedule_delivery().

05720 {
05721    /* Returns where in "receive time" we are.  That is, how many ms
05722       since we received (or would have received) the frame with timestamp 0 */
05723    int ms;
05724 #ifdef IAXTESTS
05725    int jit;
05726 #endif /* IAXTESTS */
05727    /* Setup rxcore if necessary */
05728    if (ast_tvzero(p->rxcore)) {
05729       p->rxcore = ast_tvnow();
05730       if (iaxdebug)
05731          ast_debug(1, "calc_rxstamp: call=%d: rxcore set to %d.%6.6d - %dms\n",
05732                p->callno, (int)(p->rxcore.tv_sec), (int)(p->rxcore.tv_usec), offset);
05733       p->rxcore = ast_tvsub(p->rxcore, ast_samp2tv(offset, 1000));
05734 #if 1
05735       if (iaxdebug)
05736          ast_debug(1, "calc_rxstamp: call=%d: works out as %d.%6.6d\n",
05737                p->callno, (int)(p->rxcore.tv_sec),(int)( p->rxcore.tv_usec));
05738 #endif
05739    }
05740 
05741    ms = ast_tvdiff_ms(ast_tvnow(), p->rxcore);
05742 #ifdef IAXTESTS
05743    if (test_jit) {
05744       if (!test_jitpct || ((100.0 * ast_random() / (RAND_MAX + 1.0)) < test_jitpct)) {
05745          jit = (int)((float)test_jit * ast_random() / (RAND_MAX + 1.0));
05746          if ((int)(2.0 * ast_random() / (RAND_MAX + 1.0)))
05747             jit = -jit;
05748          ms += jit;
05749       }
05750    }
05751    if (test_late) {
05752       ms += test_late;
05753       test_late = 0;
05754    }
05755 #endif /* IAXTESTS */
05756    return ms;
05757 }

static unsigned int calc_timestamp ( struct chan_iax2_pvt *  p,
unsigned int  ts,
struct ast_frame *  f 
) [static]

Definition at line 5586 of file chan_iax2.c.

References ast_debug, ast_format_rate(), AST_FRAME_CNG, AST_FRAME_IAX, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_samp2tv(), ast_tvadd(), ast_tvdiff_ms(), ast_tvnow(), ast_tvsub(), ast_tvzero(), chan_iax2_pvt::callno, ast_frame::delivery, ast_frame::frametype, iax2_trunk_peer::lastsent, chan_iax2_pvt::lastsent, MAX_TIMESTAMP_SKEW, chan_iax2_pvt::nextpred, chan_iax2_pvt::notsilenttx, chan_iax2_pvt::offset, chan_iax2_pvt::peercallno, ast_frame::samples, and ast_frame::subclass.

Referenced by iax2_send(), and socket_process().

05587 {
05588    int ms;
05589    int voice = 0;
05590    int genuine = 0;
05591    int adjust;
05592    int rate = ast_format_rate(f->subclass) / 1000;
05593    struct timeval *delivery = NULL;
05594 
05595 
05596    /* What sort of frame do we have?: voice is self-explanatory
05597       "genuine" means an IAX frame - things like LAGRQ/RP, PING/PONG, ACK
05598       non-genuine frames are CONTROL frames [ringing etc], DTMF
05599       The "genuine" distinction is needed because genuine frames must get a clock-based timestamp,
05600       the others need a timestamp slaved to the voice frames so that they go in sequence
05601    */
05602    if (f) {
05603       if (f->frametype == AST_FRAME_VOICE) {
05604          voice = 1;
05605          delivery = &f->delivery;
05606       } else if (f->frametype == AST_FRAME_IAX) {
05607          genuine = 1;
05608       } else if (f->frametype == AST_FRAME_CNG) {
05609          p->notsilenttx = 0;  
05610       }
05611    }
05612    if (ast_tvzero(p->offset)) {
05613       p->offset = ast_tvnow();
05614       /* Round to nearest 20ms for nice looking traces */
05615       p->offset.tv_usec -= p->offset.tv_usec % 20000;
05616    }
05617    /* If the timestamp is specified, just send it as is */
05618    if (ts)
05619       return ts;
05620    /* If we have a time that the frame arrived, always use it to make our timestamp */
05621    if (delivery && !ast_tvzero(*delivery)) {
05622       ms = ast_tvdiff_ms(*delivery, p->offset);
05623       if (ms < 0) {
05624          ms = 0;
05625       }
05626       if (iaxdebug)
05627          ast_debug(3, "calc_timestamp: call %d/%d: Timestamp slaved to delivery time\n", p->callno, iaxs[p->callno]->peercallno);
05628    } else {
05629       ms = ast_tvdiff_ms(ast_tvnow(), p->offset);
05630       if (ms < 0)
05631          ms = 0;
05632       if (voice) {
05633          /* On a voice frame, use predicted values if appropriate */
05634          if (p->notsilenttx && abs(ms - p->nextpred) <= MAX_TIMESTAMP_SKEW) {
05635             /* Adjust our txcore, keeping voice and non-voice synchronized */
05636             /* AN EXPLANATION:
05637                When we send voice, we usually send "calculated" timestamps worked out
05638                on the basis of the number of samples sent. When we send other frames,
05639                we usually send timestamps worked out from the real clock.
05640                The problem is that they can tend to drift out of step because the 
05641                   source channel's clock and our clock may not be exactly at the same rate.
05642                We fix this by continuously "tweaking" p->offset.  p->offset is "time zero"
05643                for this call.  Moving it adjusts timestamps for non-voice frames.
05644                We make the adjustment in the style of a moving average.  Each time we
05645                adjust p->offset by 10% of the difference between our clock-derived
05646                timestamp and the predicted timestamp.  That's why you see "10000"
05647                below even though IAX2 timestamps are in milliseconds.
05648                The use of a moving average avoids offset moving too radically.
05649                Generally, "adjust" roams back and forth around 0, with offset hardly
05650                changing at all.  But if a consistent different starts to develop it
05651                will be eliminated over the course of 10 frames (200-300msecs) 
05652             */
05653             adjust = (ms - p->nextpred);
05654             if (adjust < 0)
05655                p->offset = ast_tvsub(p->offset, ast_samp2tv(abs(adjust), 10000));
05656             else if (adjust > 0)
05657                p->offset = ast_tvadd(p->offset, ast_samp2tv(adjust, 10000));
05658 
05659             if (!p->nextpred) {
05660                p->nextpred = ms; /*f->samples / rate;*/
05661                if (p->nextpred <= p->lastsent)
05662                   p->nextpred = p->lastsent + 3;
05663             }
05664             ms = p->nextpred;
05665          } else {
05666                 /* in this case, just use the actual
05667             * time, since we're either way off
05668             * (shouldn't happen), or we're  ending a
05669             * silent period -- and seed the next
05670             * predicted time.  Also, round ms to the
05671             * next multiple of frame size (so our
05672             * silent periods are multiples of
05673             * frame size too) */
05674 
05675             if (iaxdebug && abs(ms - p->nextpred) > MAX_TIMESTAMP_SKEW )
05676                ast_debug(1, "predicted timestamp skew (%u) > max (%u), using real ts instead.\n",
05677                   abs(ms - p->nextpred), MAX_TIMESTAMP_SKEW);
05678 
05679             if (f->samples >= rate) /* check to make sure we dont core dump */
05680             {
05681                int diff = ms % (f->samples / rate);
05682                if (diff)
05683                    ms += f->samples/rate - diff;
05684             }
05685 
05686             p->nextpred = ms;
05687             p->notsilenttx = 1;
05688          }
05689       } else if ( f->frametype == AST_FRAME_VIDEO ) {
05690          /*
05691          * IAX2 draft 03 says that timestamps MUST be in order.
05692          * It does not say anything about several frames having the same timestamp
05693          * When transporting video, we can have a frame that spans multiple iax packets
05694          * (so called slices), so it would make sense to use the same timestamp for all of
05695          * them
05696          * We do want to make sure that frames don't go backwards though
05697          */
05698          if ( (unsigned int)ms < p->lastsent )
05699             ms = p->lastsent;
05700       } else {
05701          /* On a dataframe, use last value + 3 (to accomodate jitter buffer shrinking) if appropriate unless
05702             it's a genuine frame */
05703          if (genuine) {
05704             /* genuine (IAX LAGRQ etc) must keep their clock-based stamps */
05705             if (ms <= p->lastsent)
05706                ms = p->lastsent + 3;
05707          } else if (abs(ms - p->lastsent) <= MAX_TIMESTAMP_SKEW) {
05708             /* non-genuine frames (!?) (DTMF, CONTROL) should be pulled into the predicted stream stamps */
05709             ms = p->lastsent + 3;
05710          }
05711       }
05712    }
05713    p->lastsent = ms;
05714    if (voice)
05715       p->nextpred = p->nextpred + f->samples / rate;
05716    return ms;
05717 }

static unsigned int calc_txpeerstamp ( struct iax2_trunk_peer *  tpeer,
int  sampms,
struct timeval *  now 
) [static]

Definition at line 5542 of file chan_iax2.c.

References ast_tvdiff_ms(), ast_tvzero(), iax2_trunk_peer::lastsent, iax2_trunk_peer::lasttxtime, MAX_TIMESTAMP_SKEW, iax2_trunk_peer::trunkact, and iax2_trunk_peer::txtrunktime.

Referenced by send_trunk().

05543 {
05544    unsigned long int mssincetx; /* unsigned to handle overflows */
05545    long int ms, pred;
05546 
05547    tpeer->trunkact = *now;
05548    mssincetx = ast_tvdiff_ms(*now, tpeer->lasttxtime);
05549    if (mssincetx > 5000 || ast_tvzero(tpeer->txtrunktime)) {
05550       /* If it's been at least 5 seconds since the last time we transmitted on this trunk, reset our timers */
05551       tpeer->txtrunktime = *now;
05552       tpeer->lastsent = 999999;
05553    }
05554    /* Update last transmit time now */
05555    tpeer->lasttxtime = *now;
05556    
05557    /* Calculate ms offset */
05558    ms = ast_tvdiff_ms(*now, tpeer->txtrunktime);
05559    /* Predict from last value */
05560    pred = tpeer->lastsent + sampms;
05561    if (abs(ms - pred) < MAX_TIMESTAMP_SKEW)
05562       ms = pred;
05563    
05564    /* We never send the same timestamp twice, so fudge a little if we must */
05565    if (ms == tpeer->lastsent)
05566       ms = tpeer->lastsent + 1;
05567    tpeer->lastsent = ms;
05568    return ms;
05569 }

static int callno_hash ( const void *  obj,
const int  flags 
) [static]

Definition at line 2484 of file chan_iax2.c.

References ast_random().

Referenced by create_callno_pools().

02485 {
02486    return abs(ast_random());
02487 }

static int calltoken_required ( struct sockaddr_in *  sin,
const char *  name,
int  subclass 
) [static]

Definition at line 2032 of file chan_iax2.c.

References addr_range_match_address_cb(), ao2_callback, ao2_ref, ast_debug, ast_inet_ntoa(), CALLTOKEN_AUTO, CALLTOKEN_DEFAULT, CALLTOKEN_NO, iax2_peer::calltoken_required, iax2_user::calltoken_required, find_peer(), find_user(), IAX_COMMAND_NEW, peer_unref(), realtime_peer(), realtime_user(), S_OR, and user_unref().

Referenced by handle_call_token().

02033 {
02034    struct addr_range *addr_range;
02035    struct iax2_peer *peer = NULL;
02036    struct iax2_user *user = NULL;
02037    /* if no username is given, check for guest accounts */
02038    const char *find = S_OR(name, "guest");
02039    int res = 1;  /* required by default */
02040    int optional = 0;
02041    enum calltoken_peer_enum calltoken_required = CALLTOKEN_DEFAULT;
02042    /* There are only two cases in which calltoken validation is not required.
02043     * Case 1. sin falls within the list of address ranges specified in the calltoken optional table and
02044     *         the peer definition has not set the requirecalltoken option.
02045     * Case 2. Username is a valid peer/user, and that peer has requirecalltoken set either auto or no.
02046     */
02047 
02048    /* ----- Case 1 ----- */
02049    if ((addr_range = ao2_callback(calltoken_ignores, 0, addr_range_match_address_cb, sin))) {
02050       ao2_ref(addr_range, -1);
02051       optional = 1;
02052    }
02053 
02054    /* ----- Case 2 ----- */
02055    if ((subclass == IAX_COMMAND_NEW) && (user = find_user(find))) {
02056       calltoken_required = user->calltoken_required;
02057    } else if ((subclass == IAX_COMMAND_NEW) && (user = realtime_user(find, sin))) {
02058       calltoken_required = user->calltoken_required;
02059    } else if ((subclass != IAX_COMMAND_NEW) && (peer = find_peer(find, 0))) {
02060       calltoken_required = peer->calltoken_required;
02061    } else if ((subclass != IAX_COMMAND_NEW) && (peer = realtime_peer(find, sin))) {
02062       calltoken_required = peer->calltoken_required;
02063    }
02064 
02065    if (peer) {
02066       peer_unref(peer);
02067    }
02068    if (user) {
02069       user_unref(user);
02070    }
02071 
02072    ast_debug(1, "Determining if address %s with username %s requires calltoken validation.  Optional = %d  calltoken_required = %d \n", ast_inet_ntoa(sin->sin_addr), name, optional, calltoken_required);
02073    if (((calltoken_required == CALLTOKEN_NO) || (calltoken_required == CALLTOKEN_AUTO)) ||
02074       (optional && (calltoken_required == CALLTOKEN_DEFAULT))) {
02075       res = 0;
02076    }
02077 
02078    return res;
02079 }

static int check_access ( int  callno,
struct sockaddr_in *  sin,
struct iax_ies *  ies 
) [static]

Definition at line 7207 of file chan_iax2.c.

References iax2_user::accountcode, iax2_user::adsi, chan_iax2_pvt::adsi, iax_ies::adsicpe, chan_iax2_pvt::amaflags, iax2_user::amaflags, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, apply_context(), ast_apply_ha(), ast_codec_pref_convert(), ast_copy_flags, ast_db_get(), ast_inet_ntoa(), ast_log(), AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, AST_PRES_NUMBER_NOT_AVAILABLE, ast_set2_flag, ast_set_flag, ast_shrink_phone_number(), ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_variable_new(), iax2_user::authmethods, chan_iax2_pvt::authmethods, chan_iax2_pvt::authrej, iax_ies::called_context, iax_ies::called_number, iax_ies::calling_ani, iax_ies::calling_name, iax_ies::calling_number, chan_iax2_pvt::calling_pres, iax_ies::calling_pres, chan_iax2_pvt::calling_tns, iax_ies::calling_tns, chan_iax2_pvt::calling_ton, iax_ies::calling_ton, iax2_user::capability, chan_iax2_pvt::capability, iax_ies::capability, iax2_user::cid_name, cid_name, iax2_user::cid_num, cid_num, iax_ies::codec_prefs, iax2_context::context, chan_iax2_pvt::context, context, iax2_user::contexts, iax2_user::dbsecret, DEFAULT_CONTEXT, iax_ies::dnid, iax2_user::encmethods, chan_iax2_pvt::encmethods, exten, ast_variable::file, iax_ies::format, iax2_user::ha, iax2_getpeertrunk(), IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, IAX_FORCE_ENCRYPT, IAX_FORCEJITTERBUF, IAX_HASCALLERID, IAX_IMMEDIATE, IAX_MAXAUTHREQ, IAX_NOTRANSFER, IAX_PROTO_VERSION, IAX_SHRINKCALLERID, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, iax2_user::inkeys, iax2_user::language, iax_ies::language, LOG_WARNING, iax2_user::maxauthreq, iax2_user::mohinterpret, iax2_user::mohsuggest, ast_variable::name, iax2_user::name, ast_variable::next, parkinglot, iax2_user::parkinglot, chan_iax2_pvt::peeradsicpe, chan_iax2_pvt::peercapability, chan_iax2_pvt::peerformat, iax2_user::prefs, chan_iax2_pvt::prefs, prefs, iax_ies::rdnis, realtime_user(), secret, iax2_user::secret, user_unref(), iax_ies::username, ast_variable::value, chan_iax2_pvt::vars, iax2_user::vars, iax_ies::version, and version.

Referenced by socket_process().

07208 {
07209    /* Start pessimistic */
07210    int res = -1;
07211    int version = 2;
07212    struct iax2_user *user = NULL, *best = NULL;
07213    int bestscore = 0;
07214    int gotcapability = 0;
07215    struct ast_variable *v = NULL, *tmpvar = NULL;
07216    struct ao2_iterator i;
07217 
07218    if (!iaxs[callno])
07219       return res;
07220    if (ies->called_number)
07221       ast_string_field_set(iaxs[callno], exten, ies->called_number);
07222    if (ies->calling_number) {
07223       if (ast_test_flag(&globalflags, IAX_SHRINKCALLERID)) { 
07224          ast_shrink_phone_number(ies->calling_number);
07225       }
07226       ast_string_field_set(iaxs[callno], cid_num, ies->calling_number);
07227    }
07228    if (ies->calling_name)
07229       ast_string_field_set(iaxs[callno], cid_name, ies->calling_name);
07230    if (ies->calling_ani)
07231       ast_string_field_set(iaxs[callno], ani, ies->calling_ani);
07232    if (ies->dnid)
07233       ast_string_field_set(iaxs[callno], dnid, ies->dnid);
07234    if (ies->rdnis)
07235       ast_string_field_set(iaxs[callno], rdnis, ies->rdnis);
07236    if (ies->called_context)
07237       ast_string_field_set(iaxs[callno], context, ies->called_context);
07238    if (ies->language)
07239       ast_string_field_set(iaxs[callno], language, ies->language);
07240    if (ies->username)
07241       ast_string_field_set(iaxs[callno], username, ies->username);
07242    if (ies->calling_ton > -1)
07243       iaxs[callno]->calling_ton = ies->calling_ton;
07244    if (ies->calling_tns > -1)
07245       iaxs[callno]->calling_tns = ies->calling_tns;
07246    if (ies->calling_pres > -1)
07247       iaxs[callno]->calling_pres = ies->calling_pres;
07248    if (ies->format)
07249       iaxs[callno]->peerformat = ies->format;
07250    if (ies->adsicpe)
07251       iaxs[callno]->peeradsicpe = ies->adsicpe;
07252    if (ies->capability) {
07253       gotcapability = 1;
07254       iaxs[callno]->peercapability = ies->capability;
07255    } 
07256    if (ies->version)
07257       version = ies->version;
07258 
07259    /* Use provided preferences until told otherwise for actual preferences */
07260    if(ies->codec_prefs) {
07261       ast_codec_pref_convert(&iaxs[callno]->rprefs, ies->codec_prefs, 32, 0);
07262       ast_codec_pref_convert(&iaxs[callno]->prefs, ies->codec_prefs, 32, 0);
07263    }
07264 
07265    if (!gotcapability) 
07266       iaxs[callno]->peercapability = iaxs[callno]->peerformat;
07267    if (version > IAX_PROTO_VERSION) {
07268       ast_log(LOG_WARNING, "Peer '%s' has too new a protocol version (%d) for me\n", 
07269          ast_inet_ntoa(sin->sin_addr), version);
07270       return res;
07271    }
07272    /* Search the userlist for a compatible entry, and fill in the rest */
07273    i = ao2_iterator_init(users, 0);
07274    while ((user = ao2_iterator_next(&i))) {
07275       if ((ast_strlen_zero(iaxs[callno]->username) ||          /* No username specified */
07276          !strcmp(iaxs[callno]->username, user->name)) /* Or this username specified */
07277          && ast_apply_ha(user->ha, sin)   /* Access is permitted from this IP */
07278          && (ast_strlen_zero(iaxs[callno]->context) ||         /* No context specified */
07279               apply_context(user->contexts, iaxs[callno]->context))) {        /* Context is permitted */
07280          if (!ast_strlen_zero(iaxs[callno]->username)) {
07281             /* Exact match, stop right now. */
07282             if (best)
07283                user_unref(best);
07284             best = user;
07285             break;
07286          } else if (ast_strlen_zero(user->secret) && ast_strlen_zero(user->dbsecret) && ast_strlen_zero(user->inkeys)) {
07287             /* No required authentication */
07288             if (user->ha) {
07289                /* There was host authentication and we passed, bonus! */
07290                if (bestscore < 4) {
07291                   bestscore = 4;
07292                   if (best)
07293                      user_unref(best);
07294                   best = user;
07295                   continue;
07296                }
07297             } else {
07298                /* No host access, but no secret, either, not bad */
07299                if (bestscore < 3) {
07300                   bestscore = 3;
07301                   if (best)
07302                      user_unref(best);
07303                   best = user;
07304                   continue;
07305                }
07306             }
07307          } else {
07308             if (user->ha) {
07309                /* Authentication, but host access too, eh, it's something.. */
07310                if (bestscore < 2) {
07311                   bestscore = 2;
07312                   if (best)
07313                      user_unref(best);
07314                   best = user;
07315                   continue;
07316                }
07317             } else {
07318                /* Authentication and no host access...  This is our baseline */
07319                if (bestscore < 1) {
07320                   bestscore = 1;
07321                   if (best)
07322                      user_unref(best);
07323                   best = user;
07324                   continue;
07325                }
07326             }
07327          }
07328       }
07329       user_unref(user);
07330    }
07331    ao2_iterator_destroy(&i);
07332    user = best;
07333    if (!user && !ast_strlen_zero(iaxs[callno]->username)) {
07334       user = realtime_user(iaxs[callno]->username, sin);
07335       if (user && !ast_strlen_zero(iaxs[callno]->context) &&         /* No context specified */
07336           !apply_context(user->contexts, iaxs[callno]->context)) {      /* Context is permitted */
07337          user = user_unref(user);
07338       }
07339    }
07340    if (user) {
07341       /* We found our match (use the first) */
07342       /* copy vars */
07343       for (v = user->vars ; v ; v = v->next) {
07344          if((tmpvar = ast_variable_new(v->name, v->value, v->file))) {
07345             tmpvar->next = iaxs[callno]->vars; 
07346             iaxs[callno]->vars = tmpvar;
07347          }
07348       }
07349       /* If a max AUTHREQ restriction is in place, activate it */
07350       if (user->maxauthreq > 0)
07351          ast_set_flag(iaxs[callno], IAX_MAXAUTHREQ);
07352       iaxs[callno]->prefs = user->prefs;
07353       ast_copy_flags(iaxs[callno], user, IAX_CODEC_USER_FIRST | IAX_IMMEDIATE | IAX_CODEC_NOPREFS | IAX_CODEC_NOCAP | IAX_FORCE_ENCRYPT);
07354       iaxs[callno]->encmethods = user->encmethods;
07355       /* Store the requested username if not specified */
07356       if (ast_strlen_zero(iaxs[callno]->username))
07357          ast_string_field_set(iaxs[callno], username, user->name);
07358       /* Store whether this is a trunked call, too, of course, and move if appropriate */
07359       ast_copy_flags(iaxs[callno], user, IAX_TRUNK);
07360       iaxs[callno]->capability = user->capability;
07361       /* And use the default context */
07362       if (ast_strlen_zero(iaxs[callno]->context)) {
07363          if (user->contexts)
07364             ast_string_field_set(iaxs[callno], context, user->contexts->context);
07365          else
07366             ast_string_field_set(iaxs[callno], context, DEFAULT_CONTEXT);
07367       }
07368       /* And any input keys */
07369       ast_string_field_set(iaxs[callno], inkeys, user->inkeys);
07370       /* And the permitted authentication methods */
07371       iaxs[callno]->authmethods = user->authmethods;
07372       iaxs[callno]->adsi = user->adsi;
07373       /* If the user has callerid, override the remote caller id. */
07374       if (ast_test_flag(user, IAX_HASCALLERID)) {
07375          iaxs[callno]->calling_tns = 0;
07376          iaxs[callno]->calling_ton = 0;
07377          ast_string_field_set(iaxs[callno], cid_num, user->cid_num);
07378          ast_string_field_set(iaxs[callno], cid_name, user->cid_name);
07379          ast_string_field_set(iaxs[callno], ani, user->cid_num);
07380          iaxs[callno]->calling_pres = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
07381       } else if (ast_strlen_zero(iaxs[callno]->cid_num) && ast_strlen_zero(iaxs[callno]->cid_name)) {
07382          iaxs[callno]->calling_pres = AST_PRES_NUMBER_NOT_AVAILABLE;
07383       } /* else user is allowed to set their own CID settings */
07384       if (!ast_strlen_zero(user->accountcode))
07385          ast_string_field_set(iaxs[callno], accountcode, user->accountcode);
07386       if (!ast_strlen_zero(user->mohinterpret))
07387          ast_string_field_set(iaxs[callno], mohinterpret, user->mohinterpret);
07388       if (!ast_strlen_zero(user->mohsuggest))
07389          ast_string_field_set(iaxs[callno], mohsuggest, user->mohsuggest);
07390       if (!ast_strlen_zero(user->parkinglot))
07391          ast_string_field_set(iaxs[callno], parkinglot, user->parkinglot);
07392       if (user->amaflags)
07393          iaxs[callno]->amaflags = user->amaflags;
07394       if (!ast_strlen_zero(user->language))
07395          ast_string_field_set(iaxs[callno], language, user->language);
07396       ast_copy_flags(iaxs[callno], user, IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);   
07397       /* Keep this check last */
07398       if (!ast_strlen_zero(user->dbsecret)) {
07399          char *family, *key=NULL;
07400          char buf[80];
07401          family = ast_strdupa(user->dbsecret);
07402          key = strchr(family, '/');
07403          if (key) {
07404             *key = '\0';
07405             key++;
07406          }
07407          if (!key || ast_db_get(family, key, buf, sizeof(buf)))
07408             ast_log(LOG_WARNING, "Unable to retrieve database password for family/key '%s'!\n", user->dbsecret);
07409          else
07410             ast_string_field_set(iaxs[callno], secret, buf);
07411       } else
07412          ast_string_field_set(iaxs[callno], secret, user->secret);
07413       res = 0;
07414       user = user_unref(user);
07415    } else {
07416        /* user was not found, but we should still fake an AUTHREQ.
07417         * Set authmethods to the last known authmethod used by the system
07418         * Set a fake secret, it's not looked at, just required to attempt authentication.
07419         * Set authrej so the AUTHREP is rejected without even looking at its contents */
07420       iaxs[callno]->authmethods = last_authmethod ? last_authmethod : (IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT);
07421       ast_string_field_set(iaxs[callno], secret, "badsecret");
07422       iaxs[callno]->authrej = 1;
07423       if (!ast_strlen_zero(iaxs[callno]->username)) {
07424          /* only send the AUTHREQ if a username was specified. */
07425          res = 0;
07426       }
07427    }
07428    ast_set2_flag(iaxs[callno], iax2_getpeertrunk(*sin), IAX_TRUNK);  
07429    return res;
07430 }

static int check_provisioning ( struct sockaddr_in *  sin,
int  sockfd,
char *  si,
unsigned int  ver 
) [static]

Definition at line 9004 of file chan_iax2.c.

References ast_debug, iax2_provision(), and iax_provision_version().

Referenced by socket_process().

09005 {
09006    unsigned int ourver;
09007    char rsi[80];
09008    snprintf(rsi, sizeof(rsi), "si-%s", si);
09009    if (iax_provision_version(&ourver, rsi, 1))
09010       return 0;
09011    ast_debug(1, "Service identifier '%s', we think '%08x', they think '%08x'\n", si, ourver, ver);
09012    if (ourver != ver) 
09013       iax2_provision(sin, sockfd, NULL, rsi, 1);
09014    return 0;
09015 }

static int check_srcaddr ( struct sockaddr *  sa,
socklen_t  salen 
) [static]

Check if address can be used as packet source.

Returns:
0 address available, 1 address unavailable, -1 error

Definition at line 11779 of file chan_iax2.c.

References ast_debug, ast_log(), errno, and LOG_ERROR.

Referenced by peer_set_srcaddr().

11780 {
11781    int sd;
11782    int res;
11783    
11784    sd = socket(AF_INET, SOCK_DGRAM, 0);
11785    if (sd < 0) {
11786       ast_log(LOG_ERROR, "Socket: %s\n", strerror(errno));
11787       return -1;
11788    }
11789 
11790    res = bind(sd, sa, salen);
11791    if (res < 0) {
11792       ast_debug(1, "Can't bind: %s\n", strerror(errno));
11793       close(sd);
11794       return 1;
11795    }
11796 
11797    close(sd);
11798    return 0;
11799 }

static void cleanup_thread_list ( void *  head  )  [static]

Definition at line 13632 of file chan_iax2.c.

References AST_LIST_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, iax2_thread::cond, iax2_thread::lock, signal_condition(), iax2_thread::stop, thread, and iax2_thread::threadid.

Referenced by __unload_module().

13633 {
13634    AST_LIST_HEAD(iax2_thread_list, iax2_thread);
13635    struct iax2_thread_list *list_head = head;
13636    struct iax2_thread *thread;
13637 
13638    AST_LIST_LOCK(list_head);
13639    while ((thread = AST_LIST_REMOVE_HEAD(&idle_list, list))) {
13640       pthread_t thread_id = thread->threadid;
13641 
13642       thread->stop = 1;
13643       signal_condition(&thread->lock, &thread->cond);
13644 
13645       AST_LIST_UNLOCK(list_head);
13646       pthread_join(thread_id, NULL);
13647       AST_LIST_LOCK(list_head);
13648    }
13649    AST_LIST_UNLOCK(list_head);
13650 }

static int complete_dpreply ( struct chan_iax2_pvt *  pvt,
struct iax_ies *  ies 
) [static]

Definition at line 7989 of file chan_iax2.c.

References ARRAY_LEN, ast_copy_string(), AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, CACHE_FLAG_CANEXIST, CACHE_FLAG_EXISTS, CACHE_FLAG_MATCHMORE, CACHE_FLAG_NONEXISTENT, CACHE_FLAG_PENDING, CACHE_FLAG_UNKNOWN, iax_ies::called_number, iax2_dpcache::callno, iax_ies::dpstatus, iax2_dpcache::expiry, iax2_dpcache::exten, exten, iax2_dpcache::flags, IAX_DPSTATUS_CANEXIST, IAX_DPSTATUS_EXISTS, IAX_DPSTATUS_MATCHMORE, IAX_DPSTATUS_NONEXISTENT, matchmore(), iax2_dpcache::orig, iax2_dpcache::peer_list, iax_ies::refresh, status, and iax2_dpcache::waiters.

Referenced by socket_process().

07990 {
07991    char exten[256] = "";
07992    int status = CACHE_FLAG_UNKNOWN, expiry = iaxdefaultdpcache, x, matchmore = 0;
07993    struct iax2_dpcache *dp = NULL;
07994    
07995    if (ies->called_number)
07996       ast_copy_string(exten, ies->called_number, sizeof(exten));
07997    
07998    if (ies->dpstatus & IAX_DPSTATUS_EXISTS)
07999       status = CACHE_FLAG_EXISTS;
08000    else if (ies->dpstatus & IAX_DPSTATUS_CANEXIST)
08001       status = CACHE_FLAG_CANEXIST;
08002    else if (ies->dpstatus & IAX_DPSTATUS_NONEXISTENT)
08003       status = CACHE_FLAG_NONEXISTENT;
08004 
08005    if (ies->refresh)
08006       expiry = ies->refresh;
08007    if (ies->dpstatus & IAX_DPSTATUS_MATCHMORE)
08008       matchmore = CACHE_FLAG_MATCHMORE;
08009    
08010    AST_LIST_LOCK(&dpcache);
08011    AST_LIST_TRAVERSE_SAFE_BEGIN(&dpcache, dp, peer_list) {
08012       if (strcmp(dp->exten, exten))
08013          continue;
08014       AST_LIST_REMOVE_CURRENT(peer_list);
08015       dp->callno = 0;
08016       dp->expiry.tv_sec = dp->orig.tv_sec + expiry;
08017       if (dp->flags & CACHE_FLAG_PENDING) {
08018          dp->flags &= ~CACHE_FLAG_PENDING;
08019          dp->flags |= status;
08020          dp->flags |= matchmore;
08021       }
08022       /* Wake up waiters */
08023       for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
08024          if (dp->waiters[x] > -1) {
08025             if (write(dp->waiters[x], "asdf", 4) < 0) {
08026             }
08027          }
08028       }
08029    }
08030    AST_LIST_TRAVERSE_SAFE_END;
08031    AST_LIST_UNLOCK(&dpcache);
08032 
08033    return 0;
08034 }

static char * complete_iax2_peers ( const char *  line,
const char *  word,
int  pos,
int  state,
int  flags 
) [static]

Definition at line 3657 of file chan_iax2.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_strdup, ast_test_flag, iax2_peer::name, and peer_unref().

Referenced by handle_cli_iax2_prune_realtime(), handle_cli_iax2_set_debug(), and handle_cli_iax2_show_peer().

03658 {
03659    int which = 0;
03660    struct iax2_peer *peer;
03661    char *res = NULL;
03662    int wordlen = strlen(word);
03663    struct ao2_iterator i;
03664 
03665    i = ao2_iterator_init(peers, 0);
03666    while ((peer = ao2_iterator_next(&i))) {
03667       if (!strncasecmp(peer->name, word, wordlen) && ++which > state
03668          && (!flags || ast_test_flag(peer, flags))) {
03669          res = ast_strdup(peer->name);
03670          peer_unref(peer);
03671          break;
03672       }
03673       peer_unref(peer);
03674    }
03675    ao2_iterator_destroy(&i);
03676 
03677    return res;
03678 }

static char * complete_iax2_unregister ( const char *  line,
const char *  word,
int  pos,
int  state 
) [static]

Definition at line 6576 of file chan_iax2.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_strdup, iax2_peer::expire, iax2_peer::name, and peer_unref().

Referenced by handle_cli_iax2_unregister().

06577 {
06578    int which = 0;
06579    struct iax2_peer *p = NULL;
06580    char *res = NULL;
06581    int wordlen = strlen(word);
06582 
06583    /* 0 - iax2; 1 - unregister; 2 - <peername> */
06584    if (pos == 2) {
06585       struct ao2_iterator i = ao2_iterator_init(peers, 0);
06586       while ((p = ao2_iterator_next(&i))) {
06587          if (!strncasecmp(p->name, word, wordlen) && 
06588             ++which > state && p->expire > 0) {
06589             res = ast_strdup(p->name);
06590             peer_unref(p);
06591             break;
06592          }
06593          peer_unref(p);
06594       }
06595       ao2_iterator_destroy(&i);
06596    }
06597 
06598    return res;
06599 }

static int complete_transfer ( int  callno,
struct iax_ies *  ies 
) [static]

Definition at line 8036 of file chan_iax2.c.

References chan_iax2_pvt::addr, chan_iax2_pvt::aseqno, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), iax_frame::callno, iax_ies::callno, jb_frame::data, DEFAULT_RETRY_TIME, iax2_frame_free(), chan_iax2_pvt::iseqno, chan_iax2_pvt::jb, jb_getall(), JB_OK, jb_reset(), chan_iax2_pvt::lag, chan_iax2_pvt::last, chan_iax2_pvt::lastsent, LOG_WARNING, chan_iax2_pvt::nextpred, chan_iax2_pvt::offset, chan_iax2_pvt::oseqno, chan_iax2_pvt::peercallno, peercnt_add(), peercnt_remove_by_addr(), chan_iax2_pvt::pingtime, remove_by_peercallno(), remove_by_transfercallno(), iax_frame::retries, chan_iax2_pvt::rseqno, chan_iax2_pvt::rxcore, store_by_peercallno(), chan_iax2_pvt::svideoformat, chan_iax2_pvt::svoiceformat, chan_iax2_pvt::transfer, TRANSFER_NONE, chan_iax2_pvt::transfercallno, chan_iax2_pvt::transferring, chan_iax2_pvt::videoformat, and chan_iax2_pvt::voiceformat.

Referenced by socket_process().

08037 {
08038    int peercallno = 0;
08039    struct chan_iax2_pvt *pvt = iaxs[callno];
08040    struct iax_frame *cur;
08041    jb_frame frame;
08042 
08043    if (ies->callno)
08044       peercallno = ies->callno;
08045 
08046    if (peercallno < 1) {
08047       ast_log(LOG_WARNING, "Invalid transfer request\n");
08048       return -1;
08049    }
08050    remove_by_transfercallno(pvt);
08051    /* since a transfer has taken place, the address will change.
08052     * This must be accounted for in the peercnts table.  Remove
08053     * the old address and add the new one */
08054    peercnt_remove_by_addr(&pvt->addr);
08055    peercnt_add(&pvt->transfer);
08056    /* now copy over the new address */
08057    memcpy(&pvt->addr, &pvt->transfer, sizeof(pvt->addr));
08058    memset(&pvt->transfer, 0, sizeof(pvt->transfer));
08059    /* Reset sequence numbers */
08060    pvt->oseqno = 0;
08061    pvt->rseqno = 0;
08062    pvt->iseqno = 0;
08063    pvt->aseqno = 0;
08064 
08065    if (pvt->peercallno) {
08066       remove_by_peercallno(pvt);
08067    }
08068    pvt->peercallno = peercallno;
08069    /*this is where the transfering call swiches hash tables */
08070    store_by_peercallno(pvt);
08071    pvt->transferring = TRANSFER_NONE;
08072    pvt->svoiceformat = -1;
08073    pvt->voiceformat = 0;
08074    pvt->svideoformat = -1;
08075    pvt->videoformat = 0;
08076    pvt->transfercallno = 0;
08077    memset(&pvt->rxcore, 0, sizeof(pvt->rxcore));
08078    memset(&pvt->offset, 0, sizeof(pvt->offset));
08079    /* reset jitterbuffer */
08080    while(jb_getall(pvt->jb,&frame) == JB_OK)
08081       iax2_frame_free(frame.data);
08082    jb_reset(pvt->jb);
08083    pvt->lag = 0;
08084    pvt->last = 0;
08085    pvt->lastsent = 0;
08086    pvt->nextpred = 0;
08087    pvt->pingtime = DEFAULT_RETRY_TIME;
08088    AST_LIST_LOCK(&frame_queue);
08089    AST_LIST_TRAVERSE(&frame_queue, cur, list) {
08090       /* We must cancel any packets that would have been transmitted
08091          because now we're talking to someone new.  It's okay, they
08092          were transmitted to someone that didn't care anyway. */
08093       if (callno == cur->callno) 
08094          cur->retries = -1;
08095    }
08096    AST_LIST_UNLOCK(&frame_queue);
08097    return 0; 
08098 }

static unsigned char compress_subclass ( int  subclass  )  [static]

Definition at line 1465 of file chan_iax2.c.

References ast_log(), IAX_FLAG_SC_LOG, IAX_MAX_SHIFT, and LOG_WARNING.

Referenced by iax2_send(), raw_hangup(), and send_apathetic_reply().

01466 {
01467    int x;
01468    int power=-1;
01469    /* If it's 128 or smaller, just return it */
01470    if (subclass < IAX_FLAG_SC_LOG)
01471       return subclass;
01472    /* Otherwise find its power */
01473    for (x = 0; x < IAX_MAX_SHIFT; x++) {
01474       if (subclass & (1 << x)) {
01475          if (power > -1) {
01476             ast_log(LOG_WARNING, "Can't compress subclass %d\n", subclass);
01477             return 0;
01478          } else
01479             power = x;
01480       }
01481    }
01482    return power | IAX_FLAG_SC_LOG;
01483 }

static void construct_rr ( struct chan_iax2_pvt *  pvt,
struct iax_ie_data *  iep 
) [static]

Definition at line 9017 of file chan_iax2.c.

References jb_info::current, jb_info::frames_dropped, jb_info::frames_in, jb_info::frames_lost, jb_info::frames_ooo, iax_ie_append_int(), iax_ie_append_short(), IAX_IE_RR_DELAY, IAX_IE_RR_DROPPED, IAX_IE_RR_JITTER, IAX_IE_RR_LOSS, IAX_IE_RR_OOO, IAX_IE_RR_PKTS, chan_iax2_pvt::jb, jb_getinfo(), jb_info::jitter, jb_info::losspct, and jb_info::min.

Referenced by socket_process().

09018 {
09019    jb_info stats;
09020    jb_getinfo(pvt->jb, &stats);
09021    
09022    memset(iep, 0, sizeof(*iep));
09023 
09024    iax_ie_append_int(iep,IAX_IE_RR_JITTER, stats.jitter);
09025    if(stats.frames_in == 0) stats.frames_in = 1;
09026    iax_ie_append_int(iep,IAX_IE_RR_LOSS, ((0xff & (stats.losspct/1000)) << 24 | (stats.frames_lost & 0x00ffffff)));
09027    iax_ie_append_int(iep,IAX_IE_RR_PKTS, stats.frames_in);
09028    iax_ie_append_short(iep,IAX_IE_RR_DELAY, stats.current - stats.min);
09029    iax_ie_append_int(iep,IAX_IE_RR_DROPPED, stats.frames_dropped);
09030    iax_ie_append_int(iep,IAX_IE_RR_OOO, stats.frames_ooo);
09031 }

static int create_addr ( const char *  peername,
struct ast_channel *  c,
struct sockaddr_in *  sin,
struct create_addr_info *  cai 
) [static]

Definition at line 4347 of file chan_iax2.c.

References iax2_peer::addr, iax2_peer::adsi, create_addr_info::adsi, ast_clear_flag, ast_codec_pref_convert(), ast_codec_pref_prepend(), ast_copy_flags, ast_copy_string(), ast_db_get(), ast_debug, ast_get_ip_or_srv(), ast_log(), ast_strdupa, ast_strlen_zero(), iax2_peer::capability, create_addr_info::capability, iax2_peer::context, create_addr_info::context, iax2_peer::dbsecret, iax2_peer::defaddr, iax2_peer::encmethods, create_addr_info::encmethods, find_peer(), create_addr_info::found, IAX_DEFAULT_PORTNO, IAX_FORCE_ENCRYPT, IAX_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_SENDANI, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, iax2_peer::lastms, LOG_WARNING, iax2_peer::maxms, create_addr_info::maxtime, iax2_peer::mohinterpret, create_addr_info::mohinterpret, iax2_peer::mohsuggest, create_addr_info::mohsuggest, ast_channel::nativeformats, iax2_peer::outkey, create_addr_info::outkey, peer_unref(), iax2_peer::peercontext, create_addr_info::peercontext, iax2_peer::prefs, create_addr_info::prefs, prefs, iax2_peer::secret, create_addr_info::secret, iax2_peer::sockfd, create_addr_info::sockfd, create_addr_info::timezone, iax2_peer::username, create_addr_info::username, and iax2_peer::zonetag.

Referenced by cache_get_callno_locked(), iax2_call(), iax2_provision(), and iax2_request().

04348 {
04349    struct iax2_peer *peer;
04350    int res = -1;
04351    struct ast_codec_pref ourprefs;
04352 
04353    ast_clear_flag(cai, IAX_SENDANI | IAX_TRUNK);
04354    cai->sockfd = defaultsockfd;
04355    cai->maxtime = 0;
04356    sin->sin_family = AF_INET;
04357 
04358    if (!(peer = find_peer(peername, 1))) {
04359       cai->found = 0;
04360       if (ast_get_ip_or_srv(sin, peername, srvlookup ? "_iax._udp" : NULL)) {
04361          ast_log(LOG_WARNING, "No such host: %s\n", peername);
04362          return -1;
04363       }
04364       sin->sin_port = htons(IAX_DEFAULT_PORTNO);
04365       /* use global iax prefs for unknown peer/user */
04366       /* But move the calling channel's native codec to the top of the preference list */
04367       memcpy(&ourprefs, &prefs, sizeof(ourprefs));
04368       if (c)
04369          ast_codec_pref_prepend(&ourprefs, c->nativeformats, 1);
04370       ast_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
04371       return 0;
04372    }
04373 
04374    cai->found = 1;
04375    
04376    /* if the peer has no address (current or default), return failure */
04377    if (!(peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr))
04378       goto return_unref;
04379 
04380    /* if the peer is being monitored and is currently unreachable, return failure */
04381    if (peer->maxms && ((peer->lastms > peer->maxms) || (peer->lastms < 0)))
04382       goto return_unref;
04383 
04384    ast_copy_flags(cai, peer, IAX_SENDANI | IAX_TRUNK | IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF | IAX_FORCE_ENCRYPT);
04385    cai->maxtime = peer->maxms;
04386    cai->capability = peer->capability;
04387    cai->encmethods = peer->encmethods;
04388    cai->sockfd = peer->sockfd;
04389    cai->adsi = peer->adsi;
04390    memcpy(&ourprefs, &peer->prefs, sizeof(ourprefs));
04391    /* Move the calling channel's native codec to the top of the preference list */
04392    if (c) {
04393       ast_debug(1, "prepending %x to prefs\n", c->nativeformats);
04394       ast_codec_pref_prepend(&ourprefs, c->nativeformats, 1);
04395    }
04396    ast_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
04397    ast_copy_string(cai->context, peer->context, sizeof(cai->context));
04398    ast_copy_string(cai->peercontext, peer->peercontext, sizeof(cai->peercontext));
04399    ast_copy_string(cai->username, peer->username, sizeof(cai->username));
04400    ast_copy_string(cai->timezone, peer->zonetag, sizeof(cai->timezone));
04401    ast_copy_string(cai->outkey, peer->outkey, sizeof(cai->outkey));
04402    ast_copy_string(cai->mohinterpret, peer->mohinterpret, sizeof(cai->mohinterpret));
04403    ast_copy_string(cai->mohsuggest, peer->mohsuggest, sizeof(cai->mohsuggest));
04404    if (ast_strlen_zero(peer->dbsecret)) {
04405       ast_copy_string(cai->secret, peer->secret, sizeof(cai->secret));
04406    } else {
04407       char *family;
04408       char *key = NULL;
04409 
04410       family = ast_strdupa(peer->dbsecret);
04411       key = strchr(family, '/');
04412       if (key)
04413          *key++ = '\0';
04414       if (!key || ast_db_get(family, key, cai->secret, sizeof(cai->secret))) {
04415          ast_log(LOG_WARNING, "Unable to retrieve database password for family/key '%s'!\n", peer->dbsecret);
04416          goto return_unref;
04417       }
04418    }
04419 
04420    if (peer->addr.sin_addr.s_addr) {
04421       sin->sin_addr = peer->addr.sin_addr;
04422       sin->sin_port = peer->addr.sin_port;
04423    } else {
04424       sin->sin_addr = peer->defaddr.sin_addr;
04425       sin->sin_port = peer->defaddr.sin_port;
04426    }
04427 
04428    res = 0;
04429 
04430 return_unref:
04431    peer_unref(peer);
04432 
04433    return res;
04434 }

static int create_callno_pools ( void   )  [static]

Definition at line 2489 of file chan_iax2.c.

References ao2_alloc, ao2_container_alloc, ao2_link, ao2_ref, callno_entry::callno, callno_hash(), IAX_MAX_CALLS, and TRUNK_CALL_START.

Referenced by load_objects().

02490 {
02491    uint16_t i;
02492 
02493    if (!(callno_pool = ao2_container_alloc(CALLNO_POOL_BUCKETS, callno_hash, NULL))) {
02494       return -1;
02495    }
02496 
02497    if (!(callno_pool_trunk = ao2_container_alloc(CALLNO_POOL_BUCKETS, callno_hash, NULL))) {
02498       return -1;
02499    }
02500 
02501    /* start at 2, 0 and 1 are reserved */
02502    for (i = 2; i <= IAX_MAX_CALLS; i++) {
02503       struct callno_entry *callno_entry;
02504 
02505       if (!(callno_entry = ao2_alloc(sizeof(*callno_entry), NULL))) {
02506          return -1;
02507       }
02508 
02509       callno_entry->callno = i;
02510 
02511       if (i < TRUNK_CALL_START) {
02512          ao2_link(callno_pool, callno_entry);
02513       } else {
02514          ao2_link(callno_pool_trunk, callno_entry);
02515       }
02516 
02517       ao2_ref(callno_entry, -1);
02518    }
02519 
02520    return 0;
02521 }

static int decode_frame ( ast_aes_decrypt_key *  dcx,
struct ast_iax2_full_hdr *  fh,
struct ast_frame *  f,
int *  datalen 
) [static]

Definition at line 5941 of file chan_iax2.c.

References ast_debug, AST_FRAME_VIDEO, ast_iax2_full_hdr::csub, ast_iax2_mini_enc_hdr::encdata, ast_iax2_full_enc_hdr::encdata, ast_frame::frametype, IAX_FLAG_FULL, memcpy_decrypt(), ast_iax2_full_hdr::scallno, ast_frame::subclass, ast_iax2_full_hdr::type, and uncompress_subclass().

Referenced by decrypt_frame(), and update_packet().

05942 {
05943    int padding;
05944    unsigned char *workspace;
05945 
05946    workspace = alloca(*datalen);
05947    memset(f, 0, sizeof(*f));
05948    if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
05949       struct ast_iax2_full_enc_hdr *efh = (struct ast_iax2_full_enc_hdr *)fh;
05950       if (*datalen < 16 + sizeof(struct ast_iax2_full_hdr))
05951          return -1;
05952       /* Decrypt */
05953       memcpy_decrypt(workspace, efh->encdata, *datalen - sizeof(struct ast_iax2_full_enc_hdr), dcx);
05954 
05955       padding = 16 + (workspace[15] & 0x0f);
05956       if (iaxdebug)
05957          ast_debug(1, "Decoding full frame with length %d (padding = %d) (15=%02x)\n", *datalen, padding, workspace[15]);
05958       if (*datalen < padding + sizeof(struct ast_iax2_full_hdr))
05959          return -1;
05960 
05961       *datalen -= padding;
05962       memcpy(efh->encdata, workspace + padding, *datalen - sizeof(struct ast_iax2_full_enc_hdr));
05963       f->frametype = fh->type;
05964       if (f->frametype == AST_FRAME_VIDEO) {
05965          f->subclass = uncompress_subclass(fh->csub & ~0x40) | ((fh->csub >> 6) & 0x1);
05966       } else {
05967          f->subclass = uncompress_subclass(fh->csub);
05968       }
05969    } else {
05970       struct ast_iax2_mini_enc_hdr *efh = (struct ast_iax2_mini_enc_hdr *)fh;
05971       if (iaxdebug)
05972          ast_debug(1, "Decoding mini with length %d\n", *datalen);
05973       if (*datalen < 16 + sizeof(struct ast_iax2_mini_hdr))
05974          return -1;
05975       /* Decrypt */
05976       memcpy_decrypt(workspace, efh->encdata, *datalen - sizeof(struct ast_iax2_mini_enc_hdr), dcx);
05977       padding = 16 + (workspace[15] & 0x0f);
05978       if (*datalen < padding + sizeof(struct ast_iax2_mini_hdr))
05979          return -1;
05980       *datalen -= padding;
05981       memcpy(efh->encdata, workspace + padding, *datalen - sizeof(struct ast_iax2_mini_enc_hdr));
05982    }
05983    return 0;
05984 }

static int decrypt_frame ( int  callno,
struct ast_iax2_full_hdr *  fh,
struct ast_frame *  f,
int *  datalen 
) [static]

Definition at line 6027 of file chan_iax2.c.

References ast_set_flag, ast_strdupa, ast_test_flag, build_encryption_keys(), decode_frame(), IAX_KEYPOPULATED, MD5Final(), MD5Init(), MD5Update(), secret, and strsep().

Referenced by socket_process().

06028 {
06029    int res=-1;
06030    if (!ast_test_flag(iaxs[callno], IAX_KEYPOPULATED)) {
06031       /* Search for possible keys, given secrets */
06032       struct MD5Context md5;
06033       unsigned char digest[16];
06034       char *tmppw, *stringp;
06035       
06036       tmppw = ast_strdupa(iaxs[callno]->secret);
06037       stringp = tmppw;
06038       while ((tmppw = strsep(&stringp, ";"))) {
06039          MD5Init(&md5);
06040          MD5Update(&md5, (unsigned char *)iaxs[callno]->challenge, strlen(iaxs[callno]->challenge));
06041          MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
06042          MD5Final(digest, &md5);
06043          build_encryption_keys(digest, iaxs[callno]);
06044          res = decode_frame(&iaxs[callno]->dcx, fh, f, datalen);
06045          if (!res) {
06046             ast_set_flag(iaxs[callno], IAX_KEYPOPULATED);
06047             break;
06048          }
06049       }
06050    } else 
06051       res = decode_frame(&iaxs[callno]->dcx, fh, f, datalen);
06052    return res;
06053 }

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 9164 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, ast_mutex_lock(), ast_mutex_unlock(), iax2_thread::buf, iax2_pkt_buf::buf, iax2_thread::buf_len, iax2_thread::full_frames, iax2_pkt_buf::len, iax2_thread::lock, and ast_iax2_full_hdr::oseqno.

Referenced by socket_read().

09165 {
09166    struct iax2_pkt_buf *pkt_buf, *cur_pkt_buf;
09167    struct ast_iax2_full_hdr *fh, *cur_fh;
09168 
09169    if (!(pkt_buf = ast_calloc(1, sizeof(*pkt_buf) + from_here->buf_len)))
09170       return;
09171 
09172    pkt_buf->len = from_here->buf_len;
09173    memcpy(pkt_buf->buf, from_here->buf, pkt_buf->len);
09174 
09175    fh = (struct ast_iax2_full_hdr *) pkt_buf->buf;
09176    ast_mutex_lock(&to_here->lock);
09177    AST_LIST_TRAVERSE_SAFE_BEGIN(&to_here->full_frames, cur_pkt_buf, entry) {
09178       cur_fh = (struct ast_iax2_full_hdr *) cur_pkt_buf->buf;
09179       if (fh->oseqno < cur_fh->oseqno) {
09180          AST_LIST_INSERT_BEFORE_CURRENT(pkt_buf, entry);
09181          break;
09182       }
09183    }
09184    AST_LIST_TRAVERSE_SAFE_END
09185 
09186    if (!cur_pkt_buf)
09187       AST_LIST_INSERT_TAIL(&to_here->full_frames, pkt_buf, entry);
09188    
09189    ast_mutex_unlock(&to_here->lock);
09190 }

static void delete_users ( void   )  [static]

Definition at line 12435 of file chan_iax2.c.

References ao2_callback, ast_dnsmgr_release(), ast_free, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), ast_sched_thread_del, iax2_registry::callno, iax2_registry::dnsmgr, iax2_registry::expire, iax2_destroy(), peer_delme_cb(), chan_iax2_pvt::reg, and user_delme_cb().

Referenced by __unload_module(), and set_config_destroy().

12436 {
12437    struct iax2_registry *reg;
12438 
12439    ao2_callback(users, 0, user_delme_cb, NULL);
12440 
12441    AST_LIST_LOCK(&registrations);
12442    while ((reg = AST_LIST_REMOVE_HEAD(&registrations, entry))) {
12443       if (sched) {
12444          ast_sched_thread_del(sched, reg->expire);
12445       }
12446       if (reg->callno) {
12447          int callno = reg->callno;
12448          ast_mutex_lock(&iaxsl[callno]);
12449          if (iaxs[callno]) {
12450             iaxs[callno]->reg = NULL;
12451             iax2_destroy(callno);
12452          }
12453          ast_mutex_unlock(&iaxsl[callno]);
12454       }
12455       if (reg->dnsmgr)
12456          ast_dnsmgr_release(reg->dnsmgr);
12457       ast_free(reg);
12458    }
12459    AST_LIST_UNLOCK(&registrations);
12460 
12461    ao2_callback(peers, 0, peer_delme_cb, NULL);
12462 }

static void destroy_firmware ( struct iax_firmware *  cur  )  [static]

Definition at line 2844 of file chan_iax2.c.

References ast_free, ast_iax2_firmware_header::datalen, iax_firmware::fd, and iax_firmware::fwh.

Referenced by reload_firmware().

02845 {
02846    /* Close firmware */
02847    if (cur->fwh) {
02848       munmap((void*)cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
02849    }
02850    close(cur->fd);
02851    ast_free(cur);
02852 }

static void dp_lookup ( int  callno,
const char *  context,
const char *  callednum,
const char *  callerid,
int  skiplock 
) [static]

Definition at line 8865 of file chan_iax2.c.

References ast_canmatch_extension(), ast_exists_extension(), AST_FRAME_IAX, ast_ignore_pattern(), ast_matchmore_extension(), ast_mutex_lock(), ast_mutex_unlock(), ast_parking_ext(), iax_ie_data::buf, IAX_COMMAND_DPREP, IAX_DPSTATUS_CANEXIST, IAX_DPSTATUS_EXISTS, IAX_DPSTATUS_IGNOREPAT, IAX_DPSTATUS_MATCHMORE, IAX_DPSTATUS_NONEXISTENT, iax_ie_append_short(), iax_ie_append_str(), IAX_IE_CALLED_NUMBER, IAX_IE_DPSTATUS, IAX_IE_REFRESH, iax_ie_data::pos, and send_command().

Referenced by dp_lookup_thread(), and socket_process().

08866 {
08867    unsigned short dpstatus = 0;
08868    struct iax_ie_data ied1;
08869    int mm;
08870 
08871    memset(&ied1, 0, sizeof(ied1));
08872    mm = ast_matchmore_extension(NULL, context, callednum, 1, callerid);
08873    /* Must be started */
08874    if (!strcmp(callednum, ast_parking_ext()) || ast_exists_extension(NULL, context, callednum, 1, callerid)) {
08875       dpstatus = IAX_DPSTATUS_EXISTS;
08876    } else if (ast_canmatch_extension(NULL, context, callednum, 1, callerid)) {
08877       dpstatus = IAX_DPSTATUS_CANEXIST;
08878    } else {
08879       dpstatus = IAX_DPSTATUS_NONEXISTENT;
08880    }
08881    if (ast_ignore_pattern(context, callednum))
08882       dpstatus |= IAX_DPSTATUS_IGNOREPAT;
08883    if (mm)
08884       dpstatus |= IAX_DPSTATUS_MATCHMORE;
08885    if (!skiplock)
08886       ast_mutex_lock(&iaxsl[callno]);
08887    if (iaxs[callno]) {
08888       iax_ie_append_str(&ied1, IAX_IE_CALLED_NUMBER, callednum);
08889       iax_ie_append_short(&ied1, IAX_IE_DPSTATUS, dpstatus);
08890       iax_ie_append_short(&ied1, IAX_IE_REFRESH, iaxdefaultdpcache);
08891       send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_DPREP, 0, ied1.buf, ied1.pos, -1);
08892    }
08893    if (!skiplock)
08894       ast_mutex_unlock(&iaxsl[callno]);
08895 }

static void* dp_lookup_thread ( void *  data  )  [static]

Definition at line 8897 of file chan_iax2.c.

References ast_free, dpreq_data::callednum, dpreq_data::callerid, dpreq_data::callno, dpreq_data::context, and dp_lookup().

Referenced by spawn_dp_lookup().

08898 {
08899    /* Look up for dpreq */
08900    struct dpreq_data *dpr = data;
08901    dp_lookup(dpr->callno, dpr->context, dpr->callednum, dpr->callerid, 0);
08902    if (dpr->callerid)
08903       ast_free(dpr->callerid);
08904    ast_free(dpr);
08905    return NULL;
08906 }

static void encmethods_to_str ( int  e,
struct ast_str *  buf 
) [static]

Definition at line 1404 of file chan_iax2.c.

References ast_str_append(), ast_str_set(), ast_str_strlen(), IAX_ENCRYPT_AES128, and IAX_ENCRYPT_KEYROTATE.

Referenced by __iax2_show_peers(), handle_cli_iax2_show_peer(), and manager_iax2_show_peer_list().

01405 {
01406    ast_str_set(&buf, 0, "(");
01407    if (e & IAX_ENCRYPT_AES128) {
01408       ast_str_append(&buf, 0, "aes128");
01409    }
01410    if (e & IAX_ENCRYPT_KEYROTATE) {
01411       ast_str_append(&buf, 0, ",keyrotate");
01412    }
01413    if (ast_str_strlen(buf) > 1) {
01414       ast_str_append(&buf, 0, ")");
01415    } else {
01416       ast_str_set(&buf, 0, "No");
01417    }
01418 }

static int encrypt_frame ( ast_aes_encrypt_key *  ecx,
struct ast_iax2_full_hdr *  fh,
unsigned char *  poo,
int *  datalen 
) [static]

Definition at line 5986 of file chan_iax2.c.

References ast_debug, ast_iax2_full_hdr::csub, ast_iax2_mini_enc_hdr::encdata, ast_iax2_full_enc_hdr::encdata, IAX_FLAG_FULL, memcpy_encrypt(), ast_iax2_full_hdr::scallno, and ast_iax2_full_hdr::type.

Referenced by iax2_send(), and update_packet().

05987 {
05988    int padding;
05989    unsigned char *workspace;
05990    workspace = alloca(*datalen + 32);
05991    if (!workspace)
05992       return -1;
05993    if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
05994       struct ast_iax2_full_enc_hdr *efh = (struct ast_iax2_full_enc_hdr *)fh;
05995       if (iaxdebug)
05996          ast_debug(1, "Encoding full frame %d/%d with length %d\n", fh->type, fh->csub, *datalen);
05997       padding = 16 - ((*datalen - sizeof(struct ast_iax2_full_enc_hdr)) % 16);
05998       padding = 16 + (padding & 0xf);
05999       memcpy(workspace, poo, padding);
06000       memcpy(workspace + padding, efh->encdata, *datalen - sizeof(struct ast_iax2_full_enc_hdr));
06001       workspace[15] &= 0xf0;
06002       workspace[15] |= (padding & 0xf);
06003       if (iaxdebug)
06004          ast_debug(1, "Encoding full frame %d/%d with length %d + %d padding (15=%02x)\n", fh->type, fh->csub, *datalen, padding, workspace[15]);
06005       *datalen += padding;
06006       memcpy_encrypt(efh->encdata, workspace, *datalen - sizeof(struct ast_iax2_full_enc_hdr), ecx);
06007       if (*datalen >= 32 + sizeof(struct ast_iax2_full_enc_hdr))
06008          memcpy(poo, workspace + *datalen - 32, 32);
06009    } else {
06010       struct ast_iax2_mini_enc_hdr *efh = (struct ast_iax2_mini_enc_hdr *)fh;
06011       if (iaxdebug)
06012          ast_debug(1, "Encoding mini frame with length %d\n", *datalen);
06013       padding = 16 - ((*datalen - sizeof(struct ast_iax2_mini_enc_hdr)) % 16);
06014       padding = 16 + (padding & 0xf);
06015       memcpy(workspace, poo, padding);
06016       memcpy(workspace + padding, efh->encdata, *datalen - sizeof(struct ast_iax2_mini_enc_hdr));
06017       workspace[15] &= 0xf0;
06018       workspace[15] |= (padding & 0x0f);
06019       *datalen += padding;
06020       memcpy_encrypt(efh->encdata, workspace, *datalen - sizeof(struct ast_iax2_mini_enc_hdr), ecx);
06021       if (*datalen >= 32 + sizeof(struct ast_iax2_mini_enc_hdr))
06022          memcpy(poo, workspace + *datalen - 32, 32);
06023    }
06024    return 0;
06025 }

static int expire_registry ( const void *  data  )  [static]

Definition at line 8296 of file chan_iax2.c.

References __expire_registry(), and schedule_action.

Referenced by handle_cli_iax2_prune_realtime(), handle_cli_iax2_unregister(), realtime_peer(), reg_source_db(), and update_registry().

08297 {
08298 #ifdef SCHED_MULTITHREADED
08299    if (schedule_action(__expire_registry, data))
08300 #endif      
08301       __expire_registry(data);
08302    return 0;
08303 }

static struct iax2_dpcache* find_cache ( struct ast_channel *  chan,
const char *  data,
const char *  context,
const char *  exten,
int  priority 
) [static, read]

Definition at line 13095 of file chan_iax2.c.

References ARRAY_LEN, ast_calloc, ast_channel_defer_dtmf(), ast_channel_undefer_dtmf(), ast_copy_string(), ast_free, ast_frfree, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), ast_mutex_unlock(), ast_read(), ast_test_flag, ast_tvcmp(), ast_tvnow(), ast_waitfor_nandfds(), CACHE_FLAG_PENDING, CACHE_FLAG_TIMEOUT, cache_get_callno_locked(), iax2_dpcache::callno, errno, iax2_dpcache::expiry, iax2_dpcache::exten, f, iax2_dpcache::flags, iax2_dprequest(), IAX_STATE_STARTED, LOG_WARNING, iax2_dpcache::orig, iax2_dpcache::peercontext, and iax2_dpcache::waiters.

Referenced by iax2_canmatch(), iax2_exec(), iax2_exists(), and iax2_matchmore().

13096 {
13097    struct iax2_dpcache *dp = NULL;
13098    struct timeval now = ast_tvnow();
13099    int x, com[2], timeout, old = 0, outfd, doabort, callno;
13100    struct ast_channel *c = NULL;
13101    struct ast_frame *f = NULL;
13102 
13103    AST_LIST_TRAVERSE_SAFE_BEGIN(&dpcache, dp, cache_list) {
13104       if (ast_tvcmp(now, dp->expiry) > 0) {
13105          AST_LIST_REMOVE_CURRENT(cache_list);
13106          if ((dp->flags & CACHE_FLAG_PENDING) || dp->callno)
13107             ast_log(LOG_WARNING, "DP still has peer field or pending or callno (flags = %d, peer = blah, callno = %d)\n", dp->flags, dp->callno);
13108          else
13109             ast_free(dp);
13110          continue;
13111       }
13112       if (!strcmp(dp->peercontext, data) && !strcmp(dp->exten, exten))
13113          break;
13114    }
13115    AST_LIST_TRAVERSE_SAFE_END;
13116 
13117    if (!dp) {
13118       /* No matching entry.  Create a new one. */
13119       /* First, can we make a callno? */
13120       if ((callno = cache_get_callno_locked(data)) < 0) {
13121          ast_log(LOG_WARNING, "Unable to generate call for '%s'\n", data);
13122          return NULL;
13123       }
13124       if (!(dp = ast_calloc(1, sizeof(*dp)))) {
13125          ast_mutex_unlock(&iaxsl[callno]);
13126          return NULL;
13127       }
13128       ast_copy_string(dp->peercontext, data, sizeof(dp->peercontext));
13129       ast_copy_string(dp->exten, exten, sizeof(dp->exten));
13130       dp->expiry = ast_tvnow();
13131       dp->orig = dp->expiry;
13132       /* Expires in 30 mins by default */
13133       dp->expiry.tv_sec += iaxdefaultdpcache;
13134       dp->flags = CACHE_FLAG_PENDING;
13135       for (x = 0; x < ARRAY_LEN(dp->waiters); x++)
13136          dp->waiters[x] = -1;
13137       /* Insert into the lists */
13138       AST_LIST_INSERT_TAIL(&dpcache, dp, cache_list);
13139       AST_LIST_INSERT_TAIL(&iaxs[callno]->dpentries, dp, peer_list);
13140       /* Send the request if we're already up */
13141       if (ast_test_flag(&iaxs[callno]->state, IAX_STATE_STARTED))
13142          iax2_dprequest(dp, callno);
13143       ast_mutex_unlock(&iaxsl[callno]);
13144    }
13145 
13146    /* By here we must have a dp */
13147    if (dp->flags & CACHE_FLAG_PENDING) {
13148       /* Okay, here it starts to get nasty.  We need a pipe now to wait
13149          for a reply to come back so long as it's pending */
13150       for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
13151          /* Find an empty slot */
13152          if (dp->waiters[x] < 0)
13153             break;
13154       }
13155       if (x >= ARRAY_LEN(dp->waiters)) {
13156          ast_log(LOG_WARNING, "No more waiter positions available\n");
13157          return NULL;
13158       }
13159       if (pipe(com)) {
13160          ast_log(LOG_WARNING, "Unable to create pipe for comm\n");
13161          return NULL;
13162       }
13163       dp->waiters[x] = com[1];
13164       /* Okay, now we wait */
13165       timeout = iaxdefaulttimeout * 1000;
13166       /* Temporarily unlock */
13167       AST_LIST_UNLOCK(&dpcache);
13168       /* Defer any dtmf */
13169       if (chan)
13170          old = ast_channel_defer_dtmf(chan);
13171       doabort = 0;
13172       while(timeout) {
13173          c = ast_waitfor_nandfds(&chan, chan ? 1 : 0, &com[0], 1, NULL, &outfd, &timeout);
13174          if (outfd > -1)
13175             break;
13176          if (!c)
13177             continue;
13178          if (!(f = ast_read(c))) {
13179             doabort = 1;
13180             break;
13181          }
13182          ast_frfree(f);
13183       }
13184       if (!timeout) {
13185          ast_log(LOG_WARNING, "Timeout waiting for %s exten %s\n", data, exten);
13186       }
13187       AST_LIST_LOCK(&dpcache);
13188       dp->waiters[x] = -1;
13189       close(com[1]);
13190       close(com[0]);
13191       if (doabort) {
13192          /* Don't interpret anything, just abort.  Not sure what th epoint
13193            of undeferring dtmf on a hung up channel is but hey whatever */
13194          if (!old && chan)
13195             ast_channel_undefer_dtmf(chan);
13196          return NULL;
13197       }
13198       if (!(dp->flags & CACHE_FLAG_TIMEOUT)) {
13199          /* Now to do non-independent analysis the results of our wait */
13200          if (dp->flags & CACHE_FLAG_PENDING) {
13201             /* Still pending... It's a timeout.  Wake everybody up.  Consider it no longer
13202                pending.  Don't let it take as long to timeout. */
13203             dp->flags &= ~CACHE_FLAG_PENDING;
13204             dp->flags |= CACHE_FLAG_TIMEOUT;
13205             /* Expire after only 60 seconds now.  This is designed to help reduce backlog in heavily loaded
13206                systems without leaving it unavailable once the server comes back online */
13207             dp->expiry.tv_sec = dp->orig.tv_sec + 60;
13208             for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
13209                if (dp->waiters[x] > -1) {
13210                   if (write(dp->waiters[x], "asdf", 4) < 0) {
13211                      ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
13212                   }
13213                }
13214             }
13215          }
13216       }
13217       /* Our caller will obtain the rest */
13218       if (!old && chan)
13219          ast_channel_undefer_dtmf(chan);
13220    }
13221    return dp;  
13222 }

static int find_callno ( unsigned short  callno,
unsigned short  dcallno,
struct sockaddr_in *  sin,
int  new,
int  sockfd,
int  full_frame 
) [static]

Definition at line 2744 of file chan_iax2.c.

References __find_callno().

Referenced by iax2_poke_peer(), and socket_process().

02744                                                                                                                                     {
02745 
02746    return __find_callno(callno, dcallno, sin, new, sockfd, 0, full_frame);
02747 }

static int find_callno_locked ( unsigned short  callno,
unsigned short  dcallno,
struct sockaddr_in *  sin,
int  new,
int  sockfd,
int  full_frame 
) [static]

Definition at line 2749 of file chan_iax2.c.

References __find_callno().

Referenced by cache_get_callno_locked(), iax2_do_register(), iax2_provision(), iax2_request(), and socket_process_meta().

02749                                                                                                                                            {
02750 
02751    return __find_callno(callno, dcallno, sin, new, sockfd, 1, full_frame);
02752 }

static struct iax2_thread* find_idle_thread ( void   )  [static, read]

Definition at line 1265 of file chan_iax2.c.

References ast_atomic_fetchadd_int(), ast_calloc, ast_cond_destroy(), ast_cond_init(), ast_cond_wait(), ast_free, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_mutex_destroy(), ast_mutex_init(), ast_mutex_lock(), ast_mutex_unlock(), ast_pthread_create_background, iax2_thread::cond, iax2_thread::ffinfo, iax2_process_thread(), IAX_THREAD_TYPE_DYNAMIC, iaxdynamicthreadcount, iaxdynamicthreadnum, iaxmaxthreadcount, iax2_thread::init_cond, iax2_thread::init_lock, iax2_thread::lock, thread, iax2_thread::threadid, iax2_thread::threadnum, and iax2_thread::type.

Referenced by __schedule_action(), and socket_read().

01266 {
01267    struct iax2_thread *thread = NULL;
01268 
01269    /* Pop the head of the idle list off */
01270    AST_LIST_LOCK(&idle_list);
01271    thread = AST_LIST_REMOVE_HEAD(&idle_list, list);
01272    AST_LIST_UNLOCK(&idle_list);
01273 
01274    /* If we popped a thread off the idle list, just return it */
01275    if (thread) {
01276       memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
01277       return thread;
01278    }
01279 
01280    /* Pop the head of the dynamic list off */
01281    AST_LIST_LOCK(&dynamic_list);
01282    thread = AST_LIST_REMOVE_HEAD(&dynamic_list, list);
01283    AST_LIST_UNLOCK(&dynamic_list);
01284 
01285    /* If we popped a thread off the dynamic list, just return it */
01286    if (thread) {
01287       memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
01288       return thread;
01289    }
01290 
01291    /* If we can't create a new dynamic thread for any reason, return no thread at all */
01292    if (iaxdynamicthreadcount >= iaxmaxthreadcount || !(thread = ast_calloc(1, sizeof(*thread))))
01293       return NULL;
01294 
01295    /* Set default values */
01296    ast_atomic_fetchadd_int(&iaxdynamicthreadcount, 1);
01297    thread->threadnum = ast_atomic_fetchadd_int(&iaxdynamicthreadnum, 1);
01298    thread->type = IAX_THREAD_TYPE_DYNAMIC;
01299 
01300    /* Initialize lock and condition */
01301    ast_mutex_init(&thread->lock);
01302    ast_cond_init(&thread->cond, NULL);
01303    ast_mutex_init(&thread->init_lock);
01304    ast_cond_init(&thread->init_cond, NULL);
01305    ast_mutex_lock(&thread->init_lock);
01306 
01307    /* Create thread and send it on it's way */
01308    if (ast_pthread_create_background(&thread->threadid, NULL, iax2_process_thread, thread)) {
01309       ast_cond_destroy(&thread->cond);
01310       ast_mutex_destroy(&thread->lock);
01311       ast_free(thread);
01312       return NULL;
01313    }
01314 
01315    /* this thread is not processing a full frame (since it is idle),
01316       so ensure that the field for the full frame call number is empty */
01317    memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
01318 
01319    /* Wait for the thread to be ready before returning it to the caller */
01320    ast_cond_wait(&thread->init_cond, &thread->init_lock);
01321 
01322    /* Done with init_lock */
01323    ast_mutex_unlock(&thread->init_lock);
01324 
01325    return thread;
01326 }

static struct iax2_peer* find_peer ( const char *  name,
int  realtime 
) [static, read]

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 1543 of file chan_iax2.c.

References ao2_find, iax2_peer::name, OBJ_POINTER, and realtime_peer().

Referenced by calltoken_required(), create_addr(), function_iaxpeer(), handle_cli_iax2_prune_realtime(), handle_cli_iax2_set_debug(), handle_cli_iax2_show_peer(), handle_cli_iax2_unregister(), iax2_devicestate(), register_verify(), registry_authrequest(), requirecalltoken_mark_auto(), and update_registry().

01544 {
01545    struct iax2_peer *peer = NULL;
01546    struct iax2_peer tmp_peer = {
01547       .name = name,
01548    };
01549 
01550    peer = ao2_find(peers, &tmp_peer, OBJ_POINTER);
01551 
01552    /* Now go for realtime if applicable */
01553    if(!peer && realtime)
01554       peer = realtime_peer(name, NULL);
01555 
01556    return peer;
01557 }

static struct iax2_trunk_peer* find_tpeer ( struct sockaddr_in *  sin,
int  fd 
) [static, read]

Definition at line 5759 of file chan_iax2.c.

References iax2_trunk_peer::addr, ast_calloc, ast_debug, ast_inet_ntoa(), AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_init(), ast_mutex_lock(), ast_tvnow(), inaddrcmp(), iax2_trunk_peer::lastsent, iax2_trunk_peer::lock, iax2_trunk_peer::sockfd, and iax2_trunk_peer::trunkact.

Referenced by iax2_trunk_queue(), and socket_process_meta().

05760 {
05761    struct iax2_trunk_peer *tpeer = NULL;
05762    
05763    /* Finds and locks trunk peer */
05764    AST_LIST_LOCK(&tpeers);
05765 
05766    AST_LIST_TRAVERSE(&tpeers, tpeer, list) {
05767       if (!inaddrcmp(&tpeer->addr, sin)) {
05768          ast_mutex_lock(&tpeer->lock);
05769          break;
05770       }
05771    }
05772 
05773    if (!tpeer) {
05774       if ((tpeer = ast_calloc(1, sizeof(*tpeer)))) {
05775          ast_mutex_init(&tpeer->lock);
05776          tpeer->lastsent = 9999;
05777          memcpy(&tpeer->addr, sin, sizeof(tpeer->addr));
05778          tpeer->trunkact = ast_tvnow();
05779          ast_mutex_lock(&tpeer->lock);
05780          tpeer->sockfd = fd;
05781 #ifdef SO_NO_CHECK
05782          setsockopt(tpeer->sockfd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
05783 #endif
05784          ast_debug(1, "Created trunk peer for '%s:%d'\n", ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port));
05785          AST_LIST_INSERT_TAIL(&tpeers, tpeer, list);
05786       }
05787    }
05788 
05789    AST_LIST_UNLOCK(&tpeers);
05790 
05791    return tpeer;
05792 }

static struct iax2_user* find_user ( const char *  name  )  [static, read]

Definition at line 1571 of file chan_iax2.c.

References ao2_find, iax2_user::name, and OBJ_POINTER.

Referenced by calltoken_required(), handle_cli_iax2_prune_realtime(), and requirecalltoken_mark_auto().

01572 {
01573    struct iax2_user tmp_user = {
01574       .name = name,
01575    };
01576 
01577    return ao2_find(users, &tmp_user, OBJ_POINTER);
01578 }

static unsigned int fix_peerts ( struct timeval *  rxtrunktime,
int  callno,
unsigned int  ts 
) [static]

Definition at line 5571 of file chan_iax2.c.

References ast_tvdiff_ms(), ast_tvnow(), ast_tvzero(), and chan_iax2_pvt::rxcore.

Referenced by socket_process_meta().

05572 {
05573    long ms; /* NOT unsigned */
05574    if (ast_tvzero(iaxs[callno]->rxcore)) {
05575       /* Initialize rxcore time if appropriate */
05576       iaxs[callno]->rxcore = ast_tvnow();
05577       /* Round to nearest 20ms so traces look pretty */
05578       iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
05579    }
05580    /* Calculate difference between trunk and channel */
05581    ms = ast_tvdiff_ms(*rxtrunktime, iaxs[callno]->rxcore);
05582    /* Return as the sum of trunk time and the difference between trunk and real time */
05583    return ms + ts;
05584 }

static void free_context ( struct iax2_context *  con  )  [static]

Definition at line 11572 of file chan_iax2.c.

References ast_free, and iax2_context::next.

Referenced by build_user(), and user_destructor().

11573 {
11574    struct iax2_context *conl;
11575    while(con) {
11576       conl = con;
11577       con = con->next;
11578       ast_free(conl);
11579    }
11580 }

static void free_signaling_queue_entry ( struct signaling_queue_entry *  s  )  [static]

Definition at line 1671 of file chan_iax2.c.

References ast_free, ast_frame::data, signaling_queue_entry::f, and ast_frame::ptr.

Referenced by pvt_destructor(), queue_signalling(), and send_signaling().

01672 {
01673    ast_free(s->f.data.ptr);
01674    ast_free(s);
01675 }

static int function_iaxpeer ( struct ast_channel *  chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 13345 of file chan_iax2.c.

References iax2_peer::addr, iax2_trunk_peer::addr, ast_codec_pref_index(), ast_copy_string(), ast_getformatname(), ast_getformatname_multiple(), ast_inet_ntoa(), ast_strdupa, ast_test_flag, iax2_peer::callno, iax2_peer::capability, iax2_peer::cid_name, iax2_peer::cid_num, iax2_peer::context, iax2_peer::expire, find_peer(), IAX_DYNAMIC, iax2_peer::mailbox, peer_status(), peer_unref(), iax2_peer::prefs, PTR_TO_CALLNO, ast_channel::tech, and ast_channel::tech_pvt.

13346 {
13347    struct iax2_peer *peer;
13348    char *peername, *colname;
13349 
13350    peername = ast_strdupa(data);
13351 
13352    /* if our channel, return the IP address of the endpoint of current channel */
13353    if (!strcmp(peername,"CURRENTCHANNEL")) {
13354            unsigned short callno;
13355       if (chan->tech != &iax2_tech)
13356          return -1;
13357       callno = PTR_TO_CALLNO(chan->tech_pvt);   
13358       ast_copy_string(buf, iaxs[callno]->addr.sin_addr.s_addr ? ast_inet_ntoa(iaxs[callno]->addr.sin_addr) : "", len);
13359       return 0;
13360    }
13361 
13362    if ((colname = strchr(peername, ',')))
13363       *colname++ = '\0';
13364    else
13365       colname = "ip";
13366 
13367    if (!(peer = find_peer(peername, 1)))
13368       return -1;
13369 
13370    if (!strcasecmp(colname, "ip")) {
13371       ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
13372    } else  if (!strcasecmp(colname, "status")) {
13373       peer_status(peer, buf, len); 
13374    } else  if (!strcasecmp(colname, "mailbox")) {
13375       ast_copy_string(buf, peer->mailbox, len);
13376    } else  if (!strcasecmp(colname, "context")) {
13377       ast_copy_string(buf, peer->context, len);
13378    } else  if (!strcasecmp(colname, "expire")) {
13379       snprintf(buf, len, "%d", peer->expire);
13380    } else  if (!strcasecmp(colname, "dynamic")) {
13381       ast_copy_string(buf, (ast_test_flag(peer, IAX_DYNAMIC) ? "yes" : "no"), len);
13382    } else  if (!strcasecmp(colname, "callerid_name")) {
13383       ast_copy_string(buf, peer->cid_name, len);
13384    } else  if (!strcasecmp(colname, "callerid_num")) {
13385       ast_copy_string(buf, peer->cid_num, len);
13386    } else  if (!strcasecmp(colname, "codecs")) {
13387       ast_getformatname_multiple(buf, len -1, peer->capability);
13388    } else  if (!strncasecmp(colname, "codec[", 6)) {
13389       char *codecnum, *ptr;
13390       int codec = 0;
13391       
13392       codecnum = strchr(colname, '[');
13393       *codecnum = '\0';
13394       codecnum++;
13395       if ((ptr = strchr(codecnum, ']'))) {
13396          *ptr = '\0';
13397       }
13398       if((codec = ast_codec_pref_index(&peer->prefs, atoi(codecnum)))) {
13399          ast_copy_string(buf, ast_getformatname(codec), len);
13400       } else {
13401          buf[0] = '\0';
13402       }
13403    } else {
13404       buf[0] = '\0';
13405    }
13406 
13407    peer_unref(peer);
13408 
13409    return 0;
13410 }

static int get_auth_methods ( const char *  value  )  [static]

Definition at line 11763 of file chan_iax2.c.

References IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, and IAX_AUTH_RSA.

Referenced by build_peer(), and build_user().

11764 {
11765    int methods = 0;
11766    if (strstr(value, "rsa"))
11767       methods |= IAX_AUTH_RSA;
11768    if (strstr(value, "md5"))
11769       methods |= IAX_AUTH_MD5;
11770    if (strstr(value, "plaintext"))
11771       methods |= IAX_AUTH_PLAINTEXT;
11772    return methods;
11773 }

static int get_encrypt_methods ( const char *  s  )  [static]

Definition at line 1420 of file chan_iax2.c.

References ast_true(), IAX_ENCRYPT_AES128, and IAX_ENCRYPT_KEYROTATE.

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

01421 {
01422    int e;
01423    if (!strcasecmp(s, "aes128"))
01424       e = IAX_ENCRYPT_AES128 | IAX_ENCRYPT_KEYROTATE;
01425    else if (ast_true(s))
01426       e = IAX_ENCRYPT_AES128 | IAX_ENCRYPT_KEYROTATE;
01427    else
01428       e = 0;
01429    return e;
01430 }

static int get_from_jb ( const void *  p  )  [static]

Definition at line 3965 of file chan_iax2.c.

References __get_from_jb(), and schedule_action.

Referenced by update_jbsched().

03966 {
03967 #ifdef SCHED_MULTITHREADED
03968    if (schedule_action(__get_from_jb, data))
03969 #endif      
03970       __get_from_jb(data);
03971    return 0;
03972 }

static struct callno_entry * get_unused_callno ( int  trunk,
int  validated 
) [static, read]

Definition at line 2422 of file chan_iax2.c.

References ao2_container_count(), ao2_find, ao2_lock(), ao2_unlock(), ast_log(), LOG_WARNING, OBJ_CONTINUE, OBJ_POINTER, OBJ_UNLINK, and callno_entry::validated.

Referenced by __find_callno(), and make_trunk().

02423 {
02424    struct callno_entry *callno_entry = NULL;
02425    if ((!ao2_container_count(callno_pool) && !trunk) || (!ao2_container_count(callno_pool_trunk) && trunk)) {
02426       ast_log(LOG_WARNING, "Out of CallNumbers\n");
02427       /* Minor optimization for the extreme case. */
02428       return NULL;
02429    }
02430 
02431    /* the callno_pool container is locked here primarily to ensure thread
02432     * safety of the total_nonval_callno_used check and increment */
02433    ao2_lock(callno_pool);
02434 
02435    /* only a certain number of nonvalidated call numbers should be allocated.
02436     * If there ever is an attack, this separates the calltoken validating
02437     * users from the non calltoken validating users. */
02438    if (!validated && (total_nonval_callno_used >= global_maxcallno_nonval)) {
02439       ast_log(LOG_WARNING, "NON-CallToken callnumber limit is reached. Current:%d Max:%d\n", total_nonval_callno_used, global_maxcallno_nonval);
02440       ao2_unlock(callno_pool);
02441       return NULL;
02442    }
02443 
02444    /* unlink the object from the container, taking over ownership
02445     * of the reference the container had to the object */
02446    callno_entry = ao2_find((trunk ? callno_pool_trunk : callno_pool), NULL, OBJ_POINTER | OBJ_UNLINK | OBJ_CONTINUE);
02447 
02448    if (callno_entry) {
02449       callno_entry->validated = validated;
02450       if (!validated) {
02451          total_nonval_callno_used++;
02452       }
02453    }
02454 
02455    ao2_unlock(callno_pool);
02456    return callno_entry;
02457 }

static int handle_call_token ( struct ast_iax2_full_hdr *  fh,
struct iax_ies *  ies,
struct sockaddr_in *  sin,
int  fd 
) [static]

Definition at line 4644 of file chan_iax2.c.

References ast_inet_ntoa(), ast_log(), ast_sha1_hash(), ast_str_alloca, ast_str_buffer(), ast_str_set(), iax_ie_data::buf, iax_ies::calltoken, CALLTOKEN_HASH_FORMAT, CALLTOKEN_IE_FORMAT, calltoken_required(), iax_ies::calltokendata, ast_iax2_full_hdr::csub, IAX_COMMAND_CALLTOKEN, IAX_COMMAND_REGREJ, IAX_COMMAND_REGREL, IAX_COMMAND_REGREQ, IAX_COMMAND_REJECT, iax_ie_append_str(), IAX_IE_CALLTOKEN, ast_iax2_full_hdr::iseqno, LOG_ERROR, LOG_WARNING, requirecalltoken_mark_auto(), S_OR, ast_iax2_full_hdr::scallno, send_apathetic_reply(), ast_iax2_full_hdr::ts, uncompress_subclass(), and iax_ies::username.

Referenced by socket_process().

04646 {
04647 #define CALLTOKEN_HASH_FORMAT "%s%d%u%d"  /* address + port + ts + randomcalldata */
04648 #define CALLTOKEN_IE_FORMAT   "%u?%s"     /* time + ? + (40 char hash) */
04649    struct ast_str *buf = ast_str_alloca(256);
04650    time_t t = time(NULL);
04651    char hash[41]; /* 40 char sha1 hash */
04652    int subclass = uncompress_subclass(fh->csub);
04653 
04654    /* ----- Case 1 ----- */
04655    if (ies->calltoken && !ies->calltokendata) {  /* empty calltoken is provided, client supports calltokens */
04656       struct iax_ie_data ied = {
04657          .buf = { 0 },
04658          .pos = 0,
04659       };
04660 
04661       /* create the hash with their address data and our timestamp */
04662       ast_str_set(&buf, 0, CALLTOKEN_HASH_FORMAT, ast_inet_ntoa(sin->sin_addr), sin->sin_port, (unsigned int) t, randomcalltokendata);
04663       ast_sha1_hash(hash, ast_str_buffer(buf));
04664 
04665       ast_str_set(&buf, 0, CALLTOKEN_IE_FORMAT, (unsigned int) t, hash);
04666       iax_ie_append_str(&ied, IAX_IE_CALLTOKEN, ast_str_buffer(buf));
04667       send_apathetic_reply(1, ntohs(fh->scallno), sin, IAX_COMMAND_CALLTOKEN, ntohl(fh->ts), fh->iseqno + 1, fd, &ied);
04668 
04669       return 1;
04670 
04671    /* ----- Case 2 ----- */
04672    } else if (ies->calltoken && ies->calltokendata) { /* calltoken received, check to see if it is valid */
04673       char *rec_hash = NULL;    /* the received hash, make sure it matches with ours. */
04674       char *rec_ts = NULL;      /* received timestamp */
04675       unsigned int rec_time;  /* received time_t */
04676 
04677       /* split the timestamp from the hash data */
04678       rec_hash = strchr((char *) ies->calltokendata, '?');
04679       if (rec_hash) {
04680          *rec_hash++ = '\0';
04681          rec_ts = (char *) ies->calltokendata;
04682       }
04683 
04684       /* check that we have valid data before we do any comparisons */
04685       if (!rec_hash || !rec_ts) {
04686          goto reject;
04687       } else if (sscanf(rec_ts, "%u", &rec_time) != 1) {
04688          goto reject;
04689       }
04690 
04691       /* create a hash with their address and the _TOKEN'S_ timestamp */
04692       ast_str_set(&buf, 0, CALLTOKEN_HASH_FORMAT, ast_inet_ntoa(sin->sin_addr), sin->sin_port, (unsigned int) rec_time, randomcalltokendata);
04693       ast_sha1_hash(hash, ast_str_buffer(buf));
04694 
04695       /* compare hashes and then check timestamp delay */
04696       if (strcmp(hash, rec_hash)) {
04697          ast_log(LOG_WARNING, "Address %s failed CallToken hash inspection\n", ast_inet_ntoa(sin->sin_addr));
04698          goto reject; /* received hash does not match ours, reject */
04699       } else if ((t < rec_time) || ((t - rec_time) >= MAX_CALLTOKEN_DELAY)) {
04700          ast_log(LOG_WARNING, "Too much delay in IAX2 calltoken timestamp from address %s\n", ast_inet_ntoa(sin->sin_addr));
04701          goto reject; /* too much delay, reject */
04702       }
04703 
04704       /* at this point the call token is valid, returning 0 
04705        * will allow socket_process to continue as usual */
04706       requirecalltoken_mark_auto(ies->username, subclass);
04707       return 0;
04708 
04709    /* ----- Case 3 ----- */
04710    } else { /* calltokens are not supported for this client, how do we respond? */
04711       if (calltoken_required(sin, ies->username, subclass)) {
04712          ast_log(LOG_ERROR, "Call rejected, CallToken Support required. If unexpected, resolve by placing address %s in the calltokenoptional list or setting user %s requirecalltoken=no\n", ast_inet_ntoa(sin->sin_addr), S_OR(ies->username, "guest"));
04713          goto reject;
04714       }
04715       return 0; /* calltoken is not required for this addr, so permit it. */
04716    }
04717 
04718 reject:
04719    /* received frame has failed calltoken inspection, send apathetic reject messages */
04720    if (subclass == IAX_COMMAND_REGREQ || subclass == IAX_COMMAND_REGREL) {
04721       send_apathetic_reply(1, ntohs(fh->scallno), sin, IAX_COMMAND_REGREJ, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
04722    } else {
04723       send_apathetic_reply(1, ntohs(fh->scallno), sin, IAX_COMMAND_REJECT, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
04724    }
04725 
04726    return 1;
04727 }

static char* handle_cli_iax2_provision ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 11423 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, iax2_provision(), iax_prov_complete_template(), ast_cli_args::line, ast_cli_args::n, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

11424 {
11425    int force = 0;
11426    int res;
11427 
11428    switch (cmd) {
11429    case CLI_INIT:
11430       e->command = "iax2 provision";
11431       e->usage = 
11432          "Usage: iax2 provision <host> <template> [forced]\n"
11433          "       Provisions the given peer or IP address using a template\n"
11434          "       matching either 'template' or '*' if the template is not\n"
11435          "       found.  If 'forced' is specified, even empty provisioning\n"
11436          "       fields will be provisioned as empty fields.\n";
11437       return NULL;
11438    case CLI_GENERATE:
11439       if (a->pos == 3)
11440          return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
11441       return NULL;
11442    }
11443 
11444    if (a->argc < 4)
11445       return CLI_SHOWUSAGE;
11446    if (a->argc > 4) {
11447       if (!strcasecmp(a->argv[4], "forced"))
11448          force = 1;
11449       else
11450          return CLI_SHOWUSAGE;
11451    }
11452    res = iax2_provision(NULL, -1, a->argv[2], a->argv[3], force);
11453    if (res < 0)
11454       ast_cli(a->fd, "Unable to find peer/address '%s'\n", a->argv[2]);
11455    else if (res < 1)
11456       ast_cli(a->fd, "No template (including wildcard) matching '%s'\n", a->argv[3]);
11457    else
11458       ast_cli(a->fd, "Provisioning '%s' with template '%s'%s\n", a->argv[2], a->argv[3], force ? ", forced" : "");
11459    return CLI_SUCCESS;
11460 }

static char* handle_cli_iax2_prune_realtime ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 3403 of file chan_iax2.c.

References ao2_unlink, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_cli_complete(), ast_set_flag, ast_test_flag, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_iax2_peers(), expire_registry(), ast_cli_args::fd, find_peer(), find_user(), IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, ast_cli_args::line, ast_cli_args::n, peer_ref(), peer_unref(), ast_cli_args::pos, prune_peers(), prune_users(), ast_cli_entry::usage, user_unref(), and ast_cli_args::word.

03404 {
03405    struct iax2_peer *peer = NULL;
03406    struct iax2_user *user = NULL;
03407    static char *choices[] = { "all", NULL };
03408    char *cmplt;
03409 
03410    switch (cmd) {
03411    case CLI_INIT:
03412       e->command = "iax2 prune realtime";
03413       e->usage =
03414          "Usage: iax2 prune realtime [<peername>|all]\n"
03415          "       Prunes object(s) from the cache\n";
03416       return NULL;
03417    case CLI_GENERATE:
03418       if (a->pos == 3) {
03419          cmplt = ast_cli_complete(a->word, choices, a->n);
03420          if (!cmplt)
03421             cmplt = complete_iax2_peers(a->line, a->word, a->pos, a->n - sizeof(choices), IAX_RTCACHEFRIENDS);
03422          return cmplt;
03423       }
03424       return NULL;
03425    }
03426    if (a->argc != 4)
03427       return CLI_SHOWUSAGE;
03428    if (!strcmp(a->argv[3], "all")) {
03429       prune_users();
03430       prune_peers();
03431       ast_cli(a->fd, "Cache flushed successfully.\n");
03432       return CLI_SUCCESS;
03433    }
03434    peer = find_peer(a->argv[3], 0);
03435    user = find_user(a->argv[3]);
03436    if (peer || user) {
03437       if (peer) {
03438          if (ast_test_flag(peer, IAX_RTCACHEFRIENDS)) {
03439             ast_set_flag(peer, IAX_RTAUTOCLEAR);
03440             expire_registry(peer_ref(peer));
03441             ast_cli(a->fd, "Peer %s was removed from the cache.\n", a->argv[3]);
03442          } else {
03443             ast_cli(a->fd, "Peer %s is not eligible for this operation.\n", a->argv[3]);
03444          }
03445          peer_unref(peer);
03446       }
03447       if (user) {
03448          if (ast_test_flag(user, IAX_RTCACHEFRIENDS)) {
03449             ast_set_flag(user, IAX_RTAUTOCLEAR);
03450             ast_cli(a->fd, "User %s was removed from the cache.\n", a->argv[3]);
03451          } else {
03452             ast_cli(a->fd, "User %s is not eligible for this operation.\n", a->argv[3]);
03453          }
03454          ao2_unlink(users,user);
03455          user_unref(user);
03456       }
03457    } else {
03458       ast_cli(a->fd, "%s was not found in the cache.\n", a->argv[3]);
03459    }
03460 
03461    return CLI_SUCCESS;
03462 }

static char* handle_cli_iax2_reload ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 12999 of file chan_iax2.c.

References CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, reload_config(), and ast_cli_entry::usage.

13000 {
13001    switch (cmd) {
13002    case CLI_INIT:
13003       e->command = "iax2 reload";
13004       e->usage =
13005          "Usage: iax2 reload\n"
13006          "       Reloads IAX configuration from iax.conf\n";
13007       return NULL;
13008    case CLI_GENERATE:
13009       return NULL;
13010    }
13011 
13012    reload_config();
13013 
13014    return CLI_SUCCESS;
13015 }

static char* handle_cli_iax2_set_debug ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 7008 of file chan_iax2.c.

References iax2_peer::addr, ao2_ref, ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_inet_ntoa(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_iax2_peers(), debugaddr, ast_cli_args::fd, find_peer(), ast_cli_args::line, ast_cli_args::n, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

07009 {
07010    switch (cmd) {
07011    case CLI_INIT:
07012       e->command = "iax2 set debug {on|off|peer}";
07013       e->usage =
07014          "Usage: iax2 set debug {on|off|peer peername}\n"
07015          "       Enables/Disables dumping of IAX packets for debugging purposes.\n";
07016       return NULL;
07017    case CLI_GENERATE:
07018       if (a->pos == 4 && !strcasecmp(a->argv[3], "peer"))
07019          return complete_iax2_peers(a->line, a->word, a->pos, a->n, 0);
07020       return NULL;
07021    }
07022 
07023    if (a->argc < e->args  || a->argc > e->args + 1)
07024       return CLI_SHOWUSAGE;
07025 
07026    if (!strcasecmp(a->argv[3], "peer")) {
07027       struct iax2_peer *peer;
07028 
07029       if (a->argc != e->args + 1)
07030          return CLI_SHOWUSAGE;
07031 
07032       peer = find_peer(a->argv[4], 1);
07033 
07034       if (!peer) {
07035          ast_cli(a->fd, "IAX2 peer '%s' does not exist\n", a->argv[e->args-1]);
07036          return CLI_FAILURE;
07037       }
07038 
07039       debugaddr.sin_addr = peer->addr.sin_addr;
07040       debugaddr.sin_port = peer->addr.sin_port;
07041 
07042       ast_cli(a->fd, "IAX2 Debugging Enabled for IP: %s:%d\n",
07043          ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
07044 
07045       ao2_ref(peer, -1);
07046    } else if (!strncasecmp(a->argv[3], "on", 2)) {
07047       iaxdebug = 1;
07048       ast_cli(a->fd, "IAX2 Debugging Enabled\n");
07049    } else {
07050       iaxdebug = 0;
07051       memset(&debugaddr, 0, sizeof(debugaddr));
07052       ast_cli(a->fd, "IAX2 Debugging Disabled\n");
07053    }
07054    return CLI_SUCCESS;
07055 }

static char* handle_cli_iax2_set_debug_jb ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 7083 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, jb_debug_output(), jb_error_output(), jb_setoutput(), jb_warning_output(), and ast_cli_entry::usage.

07084 {
07085    switch (cmd) {
07086    case CLI_INIT:
07087       e->command = "iax2 set debug jb {on|off}";
07088       e->usage =
07089          "Usage: iax2 set debug jb {on|off}\n"
07090          "       Enables/Disables jitterbuffer debugging information\n";
07091       return NULL;
07092    case CLI_GENERATE:
07093       return NULL;
07094    }
07095 
07096    if (a->argc != e->args)
07097       return CLI_SHOWUSAGE;
07098    
07099    if (!strncasecmp(a->argv[e->args -1], "on", 2)) {
07100       jb_setoutput(jb_error_output, jb_warning_output, jb_debug_output);
07101       ast_cli(a->fd, "IAX2 Jitterbuffer Debugging Enabled\n");
07102    } else {
07103       jb_setoutput(jb_error_output, jb_warning_output, NULL);
07104       ast_cli(a->fd, "IAX2 Jitterbuffer Debugging Disabled\n");
07105    }
07106    return CLI_SUCCESS;
07107 }

static char* handle_cli_iax2_set_debug_trunk ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 7057 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

07058 {
07059    switch (cmd) {
07060    case CLI_INIT:
07061       e->command = "iax2 set debug trunk {on|off}";
07062       e->usage =
07063          "Usage: iax2 set debug trunk {on|off}\n"
07064          "       Enables/Disables debugging of IAX trunking\n";
07065       return NULL;
07066    case CLI_GENERATE:
07067       return NULL;
07068    }
07069 
07070    if (a->argc != e->args)
07071       return CLI_SHOWUSAGE;
07072 
07073    if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
07074       iaxtrunkdebug = 1;
07075       ast_cli(a->fd, "IAX2 Trunk Debugging Enabled\n");
07076    } else {
07077       iaxtrunkdebug = 0;
07078       ast_cli(a->fd, "IAX2 Trunk Debugging Disabled\n");
07079    }
07080    return CLI_SUCCESS;
07081 }

static char* handle_cli_iax2_set_mtu ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Set trunk MTU from CLI.

Definition at line 3724 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, MAX_TRUNK_MTU, and ast_cli_entry::usage.

03725 {
03726    int mtuv;
03727 
03728    switch (cmd) {
03729    case CLI_INIT:
03730       e->command = "iax2 set mtu";
03731       e->usage =
03732          "Usage: iax2 set mtu <value>\n"
03733          "       Set the system-wide IAX IP mtu to <value> bytes net or\n"
03734          "       zero to disable. Disabling means that the operating system\n"
03735          "       must handle fragmentation of UDP packets when the IAX2 trunk\n"
03736          "       packet exceeds the UDP payload size. This is substantially\n"
03737          "       below the IP mtu. Try 1240 on ethernets. Must be 172 or\n"
03738          "       greater for G.711 samples.\n";
03739       return NULL;
03740    case CLI_GENERATE:
03741       return NULL;
03742    }
03743 
03744    if (a->argc != 4)
03745       return CLI_SHOWUSAGE; 
03746    if (strncasecmp(a->argv[3], "default", strlen(a->argv[3])) == 0)
03747       mtuv = MAX_TRUNK_MTU;
03748    else
03749       mtuv = atoi(a->argv[3]);
03750 
03751    if (mtuv == 0) {
03752       ast_cli(a->fd, "Trunk MTU control disabled (mtu was %d)\n", global_max_trunk_mtu); 
03753       global_max_trunk_mtu = 0; 
03754       return CLI_SUCCESS; 
03755    }
03756    if (mtuv < 172 || mtuv > 4000) {
03757       ast_cli(a->fd, "Trunk MTU must be between 172 and 4000\n"); 
03758       return CLI_SHOWUSAGE; 
03759    }
03760    ast_cli(a->fd, "Trunk MTU changed from %d to %d\n", global_max_trunk_mtu, mtuv); 
03761    global_max_trunk_mtu = mtuv; 
03762    return CLI_SUCCESS;
03763 }

static char* handle_cli_iax2_show_cache ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 3765 of file chan_iax2.c.

References ARRAY_LEN, ast_cli(), ast_copy_string(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), ast_tvnow(), CACHE_FLAG_CANEXIST, CACHE_FLAG_EXISTS, CACHE_FLAG_MATCHMORE, CACHE_FLAG_NONEXISTENT, CACHE_FLAG_PENDING, CACHE_FLAG_TIMEOUT, CACHE_FLAG_TRANSMITTED, CACHE_FLAG_UNKNOWN, CLI_GENERATE, CLI_INIT, CLI_SUCCESS, ast_cli_entry::command, iax2_dpcache::expiry, iax2_dpcache::exten, ast_cli_args::fd, iax2_dpcache::flags, iax2_dpcache::peercontext, s, ast_cli_entry::usage, and iax2_dpcache::waiters.

03766 {
03767    struct iax2_dpcache *dp = NULL;
03768    char tmp[1024], *pc = NULL;
03769    int s, x, y;
03770    struct timeval now = ast_tvnow();
03771 
03772    switch (cmd) {
03773    case CLI_INIT:
03774       e->command = "iax2 show cache";
03775       e->usage =
03776          "Usage: iax2 show cache\n"
03777          "       Display currently cached IAX Dialplan results.\n";
03778       return NULL;
03779    case CLI_GENERATE:
03780       return NULL;
03781    }
03782 
03783    AST_LIST_LOCK(&dpcache);
03784 
03785    ast_cli(a->fd, "%-20.20s %-12.12s %-9.9s %-8.8s %s\n", "Peer/Context", "Exten", "Exp.", "Wait.", "Flags");
03786 
03787    AST_LIST_TRAVERSE(&dpcache, dp, cache_list) {
03788       s = dp->expiry.tv_sec - now.tv_sec;
03789       tmp[0] = '\0';
03790       if (dp->flags & CACHE_FLAG_EXISTS)
03791          strncat(tmp, "EXISTS|", sizeof(tmp) - strlen(tmp) - 1);
03792       if (dp->flags & CACHE_FLAG_NONEXISTENT)
03793          strncat(tmp, "NONEXISTENT|", sizeof(tmp) - strlen(tmp) - 1);
03794       if (dp->flags & CACHE_FLAG_CANEXIST)
03795          strncat(tmp, "CANEXIST|", sizeof(tmp) - strlen(tmp) - 1);
03796       if (dp->flags & CACHE_FLAG_PENDING)
03797          strncat(tmp, "PENDING|", sizeof(tmp) - strlen(tmp) - 1);
03798       if (dp->flags & CACHE_FLAG_TIMEOUT)
03799          strncat(tmp, "TIMEOUT|", sizeof(tmp) - strlen(tmp) - 1);
03800       if (dp->flags & CACHE_FLAG_TRANSMITTED)
03801          strncat(tmp, "TRANSMITTED|", sizeof(tmp) - strlen(tmp) - 1);
03802       if (dp->flags & CACHE_FLAG_MATCHMORE)
03803          strncat(tmp, "MATCHMORE|", sizeof(tmp) - strlen(tmp) - 1);
03804       if (dp->flags & CACHE_FLAG_UNKNOWN)
03805          strncat(tmp, "UNKNOWN|", sizeof(tmp) - strlen(tmp) - 1);
03806       /* Trim trailing pipe */
03807       if (!ast_strlen_zero(tmp)) {
03808          tmp[strlen(tmp) - 1] = '\0';
03809       } else {
03810          ast_copy_string(tmp, "(none)", sizeof(tmp));
03811       }
03812       y = 0;
03813       pc = strchr(dp->peercontext, '@');
03814       if (!pc) {
03815          pc = dp->peercontext;
03816       } else {
03817          pc++;
03818       }
03819       for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
03820          if (dp->waiters[x] > -1)
03821             y++;
03822       }
03823       if (s > 0) {
03824          ast_cli(a->fd, "%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->exten, s, y, tmp);
03825       } else {
03826          ast_cli(a->fd, "%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->exten, "(expired)", y, tmp);
03827       }
03828    }
03829 
03830    AST_LIST_UNLOCK(&dpcache);
03831 
03832    return CLI_SUCCESS;
03833 }

static char* handle_cli_iax2_show_callno_limits ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 2375 of file chan_iax2.c.

References peercnt::addr, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_inet_ntoa(), CLI_GENERATE, CLI_HANDLER, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, peercnt::cur, ast_cli_args::fd, peercnt::limit, and ast_cli_entry::usage.

02376 {
02377    struct ao2_iterator i;
02378    struct peercnt *peercnt;
02379    struct sockaddr_in sin;
02380    int found = 0;
02381 
02382    switch (cmd) {
02383    case CLI_INIT:
02384       e->command = "iax2 show callnumber usage";
02385       e->usage =
02386          "Usage: iax2 show callnumber usage <ip address (optional)>\n"
02387          "       Shows current ip addresses which are consuming iax2 call numbers\n";
02388       return NULL;
02389    case CLI_GENERATE:
02390       return NULL;
02391    case CLI_HANDLER:
02392       if (a->argc < 4 || a->argc > 5)
02393          return CLI_SHOWUSAGE;
02394 
02395       ast_cli(a->fd, "%-15s %-12s %-12s\n", "Address", "Callno Usage", "Callno Limit");
02396       i = ao2_iterator_init(peercnts, 0);
02397       while ((peercnt = ao2_iterator_next(&i))) {
02398          sin.sin_addr.s_addr = peercnt->addr;
02399          if (a->argc == 5 && (!strcasecmp(a->argv[4], ast_inet_ntoa(sin.sin_addr)))) {
02400                ast_cli(a->fd, "%-15s %-12d %-12d\n", ast_inet_ntoa(sin.sin_addr), peercnt->cur, peercnt->limit);
02401                found = 1;
02402                break;
02403          } else {
02404             ast_cli(a->fd, "%-15s %-12d %-12d\n", ast_inet_ntoa(sin.sin_addr), peercnt->cur, peercnt->limit);
02405          }
02406          ao2_ref(peercnt, -1);
02407       }
02408       ao2_iterator_destroy(&i);
02409 
02410       if (a->argc == 4) {
02411          ast_cli(a->fd, "\nNon-CallToken Validation Limit: %d\nNon-CallToken Validated: %d\n", global_maxcallno_nonval, total_nonval_callno_used);
02412       } else if (a->argc == 5 && !found) {
02413          ast_cli(a->fd, "No callnumber table entries for %s found\n", a->argv[4] );
02414       }
02415 
02416       return CLI_SUCCESS;
02417    default:
02418       return NULL;
02419    }
02420 }

static char* handle_cli_iax2_show_channels ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6835 of file chan_iax2.c.

References iax2_trunk_peer::addr, ast_cli_args::argc, ARRAY_LEN, ast_cli(), ast_getformatname(), ast_inet_ntoa(), ast_mutex_lock(), ast_mutex_unlock(), ast_test_flag, iax2_registry::callno, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, jb_info::current, iax_rr::delay, ast_cli_args::fd, FORMAT, FORMAT2, iax_frame_subclass2str(), IAX_USEJITTERBUF, jb_getinfo(), jb_info::jitter, MARK_IAX_SUBCLASS_TX, jb_info::min, ast_channel::name, chan_iax2_pvt::owner, chan_iax2_pvt::remote_rr, S_OR, and ast_cli_entry::usage.

06836 {
06837 #define FORMAT2 "%-20.20s  %-15.15s  %-10.10s  %-11.11s  %-11.11s  %-7.7s  %-6.6s  %-6.6s  %s  %s  %9s\n"
06838 #define FORMAT  "%-20.20s  %-15.15s  %-10.10s  %5.5d/%5.5d  %5.5d/%5.5d  %-5.5dms  %-4.4dms  %-4.4dms  %-6.6s  %s%s  %3s%s\n"
06839 #define FORMATB "%-20.20s  %-15.15s  %-10.10s  %5.5d/%5.5d  %5.5d/%5.5d  [Native Bridged to ID=%5.5d]\n"
06840    int x;
06841    int numchans = 0;
06842    char first_message[10] = { 0, };
06843    char last_message[10] = { 0, };
06844 
06845    switch (cmd) {
06846    case CLI_INIT:
06847       e->command = "iax2 show channels";
06848       e->usage =
06849          "Usage: iax2 show channels\n"
06850          "       Lists all currently active IAX channels.\n";
06851       return NULL;
06852    case CLI_GENERATE:
06853       return NULL;
06854    }
06855 
06856    if (a->argc != 3)
06857       return CLI_SHOWUSAGE;
06858    ast_cli(a->fd, FORMAT2, "Channel", "Peer", "Username", "ID (Lo/Rem)", "Seq (Tx/Rx)", "Lag", "Jitter", "JitBuf", "Format", "FirstMsg", "LastMsg");
06859    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
06860       ast_mutex_lock(&iaxsl[x]);
06861       if (iaxs[x]) {
06862          int lag, jitter, localdelay;
06863          jb_info jbinfo;
06864          if (ast_test_flag(iaxs[x], IAX_USEJITTERBUF)) {
06865             jb_getinfo(iaxs[x]->jb, &jbinfo);
06866             jitter = jbinfo.jitter;
06867             localdelay = jbinfo.current - jbinfo.min;
06868          } else {
06869             jitter = -1;
06870             localdelay = 0;
06871          }
06872 
06873          iax_frame_subclass2str(iaxs[x]->first_iax_message & ~MARK_IAX_SUBCLASS_TX, first_message, sizeof(first_message));
06874          iax_frame_subclass2str(iaxs[x]->last_iax_message & ~MARK_IAX_SUBCLASS_TX, last_message, sizeof(last_message));
06875          lag = iaxs[x]->remote_rr.delay;
06876          ast_cli(a->fd, FORMAT,
06877             iaxs[x]->owner ? iaxs[x]->owner->name : "(None)",
06878             ast_inet_ntoa(iaxs[x]->addr.sin_addr),
06879             S_OR(iaxs[x]->username, "(None)"),
06880             iaxs[x]->callno, iaxs[x]->peercallno,
06881             iaxs[x]->oseqno, iaxs[x]->iseqno,
06882             lag,
06883             jitter,
06884             localdelay,
06885             ast_getformatname(iaxs[x]->voiceformat),
06886             (iaxs[x]->first_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
06887             first_message,
06888             (iaxs[x]->last_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
06889             last_message);
06890          numchans++;
06891       }
06892       ast_mutex_unlock(&iaxsl[x]);
06893    }
06894    ast_cli(a->fd, "%d active IAX channel%s\n", numchans, (numchans != 1) ? "s" : "");
06895    return CLI_SUCCESS;
06896 #undef FORMAT
06897 #undef FORMAT2
06898 #undef FORMATB
06899 }

static char* handle_cli_iax2_show_firmware ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6633 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli_args::argv, ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_iax2_firmware_header::datalen, ast_iax2_firmware_header::devname, ast_cli_args::fd, iax_firmware::fwh, ast_cli_entry::usage, and ast_iax2_firmware_header::version.

06634 {
06635    struct iax_firmware *cur = NULL;
06636 
06637    switch (cmd) {
06638    case CLI_INIT:
06639       e->command = "iax2 show firmware";
06640       e->usage =
06641          "Usage: iax2 show firmware\n"
06642          "       Lists all known IAX firmware images.\n";
06643       return NULL;
06644    case CLI_GENERATE:
06645       return NULL;
06646    }
06647 
06648    if (a->argc != 3 && a->argc != 4)
06649       return CLI_SHOWUSAGE;
06650 
06651    ast_cli(a->fd, "%-15.15s  %-15.15s %-15.15s\n", "Device", "Version", "Size");
06652    AST_LIST_LOCK(&firmwares);
06653    AST_LIST_TRAVERSE(&firmwares, cur, list) {
06654       if ((a->argc == 3) || (!strcasecmp(a->argv[3], (char *) cur->fwh->devname)))  {
06655          ast_cli(a->fd, "%-15.15s  %-15d %-15d\n", cur->fwh->devname, 
06656             ntohs(cur->fwh->version), (int)ntohl(cur->fwh->datalen));
06657       }
06658    }
06659    AST_LIST_UNLOCK(&firmwares);
06660 
06661    return CLI_SUCCESS;
06662 }

static char* handle_cli_iax2_show_netstats ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6985 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli(), ast_cli_netstats(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, and ast_cli_entry::usage.

06986 {
06987    int numchans = 0;
06988 
06989    switch (cmd) {
06990    case CLI_INIT:
06991       e->command = "iax2 show netstats";
06992       e->usage =
06993          "Usage: iax2 show netstats\n"
06994          "       Lists network status for all currently active IAX channels.\n";
06995       return NULL;
06996    case CLI_GENERATE:
06997       return NULL;
06998    }
06999    if (a->argc != 3)
07000       return CLI_SHOWUSAGE;
07001    ast_cli(a->fd, "                           -------- LOCAL ---------------------  -------- REMOTE --------------------\n");
07002    ast_cli(a->fd, "Channel               RTT  Jit  Del  Lost   %%  Drop  OOO  Kpkts  Jit  Del  Lost   %%  Drop  OOO  Kpkts FirstMsg    LastMsg\n");
07003    numchans = ast_cli_netstats(NULL, a->fd, 1);
07004    ast_cli(a->fd, "%d active IAX channel%s\n", numchans, (numchans != 1) ? "s" : "");
07005    return CLI_SUCCESS;
07006 }

static char* handle_cli_iax2_show_peer ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Show one peer in detail.

Definition at line 3578 of file chan_iax2.c.

References iax2_peer::addr, ast_cli_args::argc, ast_cli_args::argv, ast_callerid_merge(), ast_cli(), ast_codec_pref_index(), ast_getformatname(), ast_getformatname_multiple(), ast_inet_ntoa(), ast_str_alloca, ast_str_buffer(), ast_strlen_zero(), ast_test_flag, CALLTOKEN_AUTO, iax2_peer::calltoken_required, CALLTOKEN_YES, iax2_peer::capability, iax2_peer::cid_name, iax2_peer::cid_num, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_iax2_peers(), iax2_peer::context, iax2_peer::defaddr, iax2_peer::encmethods, encmethods_to_str(), iax2_peer::expire, ast_cli_args::fd, find_peer(), iax2_peer::ha, IAX_DYNAMIC, IAX_TRUNK, ast_cli_args::line, iax2_peer::mailbox, iax2_peer::maxcallno, ast_cli_args::n, iax2_peer::name, iax2_peer::parkinglot, peer_status(), peer_unref(), iax2_peer::pokefreqnotok, iax2_peer::pokefreqok, ast_cli_args::pos, iax2_peer::prefs, iax2_peer::secret, iax2_peer::smoothing, status, ast_cli_entry::usage, iax2_peer::username, and ast_cli_args::word.

03579 {
03580    char status[30];
03581    char cbuf[256];
03582    struct iax2_peer *peer;
03583    char codec_buf[512];
03584    struct ast_str *encmethods = ast_str_alloca(256);
03585    int x = 0, codec = 0, load_realtime = 0;
03586 
03587    switch (cmd) {
03588    case CLI_INIT:
03589       e->command = "iax2 show peer";
03590       e->usage =
03591          "Usage: iax2 show peer <name>\n"
03592          "       Display details on specific IAX peer\n";
03593       return NULL;
03594    case CLI_GENERATE:
03595       if (a->pos == 3)
03596          return complete_iax2_peers(a->line, a->word, a->pos, a->n, 0);
03597       return NULL;
03598    }
03599 
03600    if (a->argc < 4)
03601       return CLI_SHOWUSAGE;
03602 
03603    load_realtime = (a->argc == 5 && !strcmp(a->argv[4], "load")) ? 1 : 0;
03604 
03605    peer = find_peer(a->argv[3], load_realtime);
03606    if (peer) {
03607       encmethods_to_str(peer->encmethods, encmethods);
03608       ast_cli(a->fd, "\n\n");
03609       ast_cli(a->fd, "  * Name       : %s\n", peer->name);
03610       ast_cli(a->fd, "  Secret       : %s\n", ast_strlen_zero(peer->secret) ? "<Not set>" : "<Set>");
03611       ast_cli(a->fd, "  Context      : %s\n", peer->context);
03612       ast_cli(a->fd, "  Parking lot  : %s\n", peer->parkinglot);
03613       ast_cli(a->fd, "  Mailbox      : %s\n", peer->mailbox);
03614       ast_cli(a->fd, "  Dynamic      : %s\n", ast_test_flag(peer, IAX_DYNAMIC) ? "Yes" : "No");
03615       ast_cli(a->fd, "  Callnum limit: %d\n", peer->maxcallno);
03616       ast_cli(a->fd, "  Calltoken req: %s\n", (peer->calltoken_required == CALLTOKEN_YES) ? "Yes" : ((peer->calltoken_required == CALLTOKEN_AUTO) ? "Auto" : "No"));
03617       ast_cli(a->fd, "  Trunk        : %s\n", ast_test_flag(peer, IAX_TRUNK) ? "Yes" : "No");
03618       ast_cli(a->fd, "  Encryption   : %s\n", peer->encmethods ? ast_str_buffer(encmethods) : "No");
03619       ast_cli(a->fd, "  Callerid     : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
03620       ast_cli(a->fd, "  Expire       : %d\n", peer->expire);
03621       ast_cli(a->fd, "  ACL          : %s\n", (peer->ha ? "Yes" : "No"));
03622       ast_cli(a->fd, "  Addr->IP     : %s Port %d\n",  peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "(Unspecified)", ntohs(peer->addr.sin_port));
03623       ast_cli(a->fd, "  Defaddr->IP  : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
03624       ast_cli(a->fd, "  Username     : %s\n", peer->username);
03625       ast_cli(a->fd, "  Codecs       : ");
03626       ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
03627       ast_cli(a->fd, "%s\n", codec_buf);
03628 
03629       ast_cli(a->fd, "  Codec Order  : (");
03630       for(x = 0; x < 32 ; x++) {
03631          codec = ast_codec_pref_index(&peer->prefs,x);
03632          if(!codec)
03633             break;
03634          ast_cli(a->fd, "%s", ast_getformatname(codec));
03635          if(x < 31 && ast_codec_pref_index(&peer->prefs,x+1))
03636             ast_cli(a->fd, "|");
03637       }
03638 
03639       if (!x)
03640          ast_cli(a->fd, "none");
03641       ast_cli(a->fd, ")\n");
03642 
03643       ast_cli(a->fd, "  Status       : ");
03644       peer_status(peer, status, sizeof(status));   
03645       ast_cli(a->fd, "%s\n",status);
03646       ast_cli(a->fd, "  Qualify      : every %dms when OK, every %dms when UNREACHABLE (sample smoothing %s)\n", peer->pokefreqok, peer->pokefreqnotok, peer->smoothing ? "On" : "Off");
03647       ast_cli(a->fd, "\n");
03648       peer_unref(peer);
03649    } else {
03650       ast_cli(a->fd, "Peer %s not found.\n", a->argv[3]);
03651       ast_cli(a->fd, "\n");
03652    }
03653 
03654    return CLI_SUCCESS;
03655 }

static char* handle_cli_iax2_show_peers ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6601 of file chan_iax2.c.

References __iax2_show_peers(), ast_cli_args::argc, ast_cli_args::argv, CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, RESULT_FAILURE, RESULT_SHOWUSAGE, and ast_cli_entry::usage.

06602 {
06603    switch (cmd) {
06604    case CLI_INIT:
06605       e->command = "iax2 show peers";
06606       e->usage =
06607          "Usage: iax2 show peers [registered] [like <pattern>]\n"
06608          "       Lists all known IAX2 peers.\n"
06609          "       Optional 'registered' argument lists only peers with known addresses.\n"
06610          "       Optional regular expression pattern is used to filter the peer list.\n";
06611       return NULL;
06612    case CLI_GENERATE:
06613       return NULL;
06614    }
06615 
06616    switch (__iax2_show_peers(0, a->fd, NULL, a->argc, a->argv)) {
06617    case RESULT_SHOWUSAGE:
06618       return CLI_SHOWUSAGE;
06619    case RESULT_FAILURE:
06620       return CLI_FAILURE;
06621    default:
06622       return CLI_SUCCESS;
06623    }
06624 }

static char* handle_cli_iax2_show_registry ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6745 of file chan_iax2.c.

References iax2_registry::addr, ast_cli_args::argc, ast_cli(), ast_copy_string(), ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, iax2_registry::dnsmgr, ast_cli_args::fd, FORMAT, FORMAT2, iax2_registry::refresh, iax2_registry::regstate, regstate2str(), iax2_registry::us, ast_cli_entry::usage, and iax2_registry::username.

06746 {
06747 #define FORMAT2 "%-20.20s  %-6.6s  %-10.10s  %-20.20s %8.8s  %s\n"
06748 #define FORMAT  "%-20.20s  %-6.6s  %-10.10s  %-20.20s %8d  %s\n"
06749    struct iax2_registry *reg = NULL;
06750    char host[80];
06751    char perceived[80];
06752    int counter = 0;
06753 
06754    switch (cmd) {
06755    case CLI_INIT:
06756       e->command = "iax2 show registry";
06757       e->usage =
06758          "Usage: iax2 show registry\n"
06759          "       Lists all registration requests and status.\n";
06760       return NULL;
06761    case CLI_GENERATE:
06762       return NULL;
06763    }
06764    if (a->argc != 3)
06765       return CLI_SHOWUSAGE;
06766    ast_cli(a->fd, FORMAT2, "Host", "dnsmgr", "Username", "Perceived", "Refresh", "State");
06767    AST_LIST_LOCK(&registrations);
06768    AST_LIST_TRAVERSE(&registrations, reg, entry) {
06769       snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(reg->addr.sin_addr), ntohs(reg->addr.sin_port));
06770       if (reg->us.sin_addr.s_addr) 
06771          snprintf(perceived, sizeof(perceived), "%s:%d", ast_inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port));
06772       else
06773          ast_copy_string(perceived, "<Unregistered>", sizeof(perceived));
06774       ast_cli(a->fd, FORMAT, host, 
06775                (reg->dnsmgr) ? "Y" : "N", 
06776                reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
06777       counter++;
06778    }
06779    AST_LIST_UNLOCK(&registrations);
06780    ast_cli(a->fd, "%d IAX2 registrations.\n", counter);
06781    return CLI_SUCCESS;
06782 #undef FORMAT
06783 #undef FORMAT2
06784 }

static char* handle_cli_iax2_show_stats ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 3680 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, iax_frame::final, iax_get_frames(), iax_get_iframes(), iax_get_oframes(), iax_frame::retries, and ast_cli_entry::usage.

03681 {
03682    struct iax_frame *cur;
03683    int cnt = 0, dead = 0, final = 0;
03684 
03685    switch (cmd) {
03686    case CLI_INIT:
03687       e->command = "iax2 show stats";
03688       e->usage =
03689          "Usage: iax2 show stats\n"
03690          "       Display statistics on IAX channel driver.\n";
03691       return NULL;
03692    case CLI_GENERATE:
03693       return NULL;
03694    }
03695 
03696    if (a->argc != 3)
03697       return CLI_SHOWUSAGE;
03698 
03699    AST_LIST_LOCK(&frame_queue);
03700    AST_LIST_TRAVERSE(&frame_queue, cur, list) {
03701       if (cur->retries < 0)
03702          dead++;
03703       if (cur->final)
03704          final++;
03705       cnt++;
03706    }
03707    AST_LIST_UNLOCK(&frame_queue);
03708 
03709    ast_cli(a->fd, "    IAX Statistics\n");
03710    ast_cli(a->fd, "---------------------\n");
03711    ast_cli(a->fd, "Outstanding frames: %d (%d ingress, %d egress)\n", iax_get_frames(), iax_get_iframes(), iax_get_oframes());
03712    ast_cli(a->fd, "%d timed and %d untimed transmits; MTU %d/%d/%d\n", trunk_timed, trunk_untimed,
03713       trunk_maxmtu, trunk_nmaxmtu, global_max_trunk_mtu);
03714    ast_cli(a->fd, "Packets in transmit queue: %d dead, %d final, %d total\n\n", dead, final, cnt);
03715 
03716    trunk_timed = trunk_untimed = 0;
03717    if (trunk_maxmtu > trunk_nmaxmtu)
03718       trunk_nmaxmtu = trunk_maxmtu;
03719 
03720    return CLI_SUCCESS;
03721 }

static char* handle_cli_iax2_show_threads ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6464 of file chan_iax2.c.

References iax2_thread::actions, ast_cli_args::argc, ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, iax2_thread::checktime, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, iax2_thread::curfunc, ast_cli_args::fd, IAX_THREAD_TYPE_DYNAMIC, iaxthreadcount, iax2_thread::iostate, thread, iax2_thread::threadnum, iax2_thread::type, and ast_cli_entry::usage.

06465 {
06466    struct iax2_thread *thread = NULL;
06467    time_t t;
06468    int threadcount = 0, dynamiccount = 0;
06469    char type;
06470 
06471    switch (cmd) {
06472    case CLI_INIT:
06473       e->command = "iax2 show threads";
06474       e->usage =
06475          "Usage: iax2 show threads\n"
06476          "       Lists status of IAX helper threads\n";
06477       return NULL;
06478    case CLI_GENERATE:
06479       return NULL;
06480    }
06481    if (a->argc != 3)
06482       return CLI_SHOWUSAGE;
06483       
06484    ast_cli(a->fd, "IAX2 Thread Information\n");
06485    time(&t);
06486    ast_cli(a->fd, "Idle Threads:\n");
06487    AST_LIST_LOCK(&idle_list);
06488    AST_LIST_TRAVERSE(&idle_list, thread, list) {
06489 #ifdef DEBUG_SCHED_MULTITHREAD
06490       ast_cli(a->fd, "Thread %d: state=%d, update=%d, actions=%d, func='%s'\n", 
06491          thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
06492 #else
06493       ast_cli(a->fd, "Thread %d: state=%d, update=%d, actions=%d\n", 
06494          thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
06495 #endif
06496       threadcount++;
06497    }
06498    AST_LIST_UNLOCK(&idle_list);
06499    ast_cli(a->fd, "Active Threads:\n");
06500    AST_LIST_LOCK(&active_list);
06501    AST_LIST_TRAVERSE(&active_list, thread, list) {
06502       if (thread->type == IAX_THREAD_TYPE_DYNAMIC)
06503          type = 'D';
06504       else
06505          type = 'P';
06506 #ifdef DEBUG_SCHED_MULTITHREAD
06507       ast_cli(a->fd, "Thread %c%d: state=%d, update=%d, actions=%d, func='%s'\n", 
06508          type, thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
06509 #else
06510       ast_cli(a->fd, "Thread %c%d: state=%d, update=%d, actions=%d\n", 
06511          type, thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
06512 #endif
06513       threadcount++;
06514    }
06515    AST_LIST_UNLOCK(&active_list);
06516    ast_cli(a->fd, "Dynamic Threads:\n");
06517    AST_LIST_LOCK(&dynamic_list);
06518    AST_LIST_TRAVERSE(&dynamic_list, thread, list) {
06519 #ifdef DEBUG_SCHED_MULTITHREAD
06520       ast_cli(a->fd, "Thread %d: state=%d, update=%d, actions=%d, func='%s'\n",
06521          thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
06522 #else
06523       ast_cli(a->fd, "Thread %d: state=%d, update=%d, actions=%d\n",
06524          thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
06525 #endif
06526       dynamiccount++;
06527    }
06528    AST_LIST_UNLOCK(&dynamic_list);
06529    ast_cli(a->fd, "%d of %d threads accounted for with %d dynamic threads\n", threadcount, iaxthreadcount, dynamiccount);
06530    return CLI_SUCCESS;
06531 }

static char* handle_cli_iax2_show_users ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6260 of file chan_iax2.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), ast_copy_string(), ast_strlen_zero(), ast_test_flag, iax2_user::authmethods, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, iax2_context::context, iax2_user::contexts, DEFAULT_CONTEXT, ast_cli_args::fd, FORMAT, FORMAT2, iax2_user::ha, IAX_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, iax2_user::inkeys, iax2_user::name, iax2_user::secret, ast_cli_entry::usage, and user_unref().

06261 {
06262    regex_t regexbuf;
06263    int havepattern = 0;
06264 
06265 #define FORMAT "%-15.15s  %-20.20s  %-15.15s  %-15.15s  %-5.5s  %-5.10s\n"
06266 #define FORMAT2 "%-15.15s  %-20.20s  %-15.15d  %-15.15s  %-5.5s  %-5.10s\n"
06267 
06268    struct iax2_user *user = NULL;
06269    char auth[90];
06270    char *pstr = "";
06271    struct ao2_iterator i;
06272 
06273    switch (cmd) {
06274    case CLI_INIT:
06275       e->command = "iax2 show users [like]";
06276       e->usage =
06277          "Usage: iax2 show users [like <pattern>]\n"
06278          "       Lists all known IAX2 users.\n"
06279          "       Optional regular expression pattern is used to filter the user list.\n";
06280       return NULL;
06281    case CLI_GENERATE:
06282       return NULL;
06283    }
06284 
06285    switch (a->argc) {
06286    case 5:
06287       if (!strcasecmp(a->argv[3], "like")) {
06288          if (regcomp(&regexbuf, a->argv[4], REG_EXTENDED | REG_NOSUB))
06289             return CLI_SHOWUSAGE;
06290          havepattern = 1;
06291       } else
06292          return CLI_SHOWUSAGE;
06293    case 3:
06294       break;
06295    default:
06296       return CLI_SHOWUSAGE;
06297    }
06298 
06299    ast_cli(a->fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C","Codec Pref");
06300    i = ao2_iterator_init(users, 0);
06301    for (user = ao2_iterator_next(&i); user; 
06302       user_unref(user), user = ao2_iterator_next(&i)) {
06303       if (havepattern && regexec(&regexbuf, user->name, 0, NULL, 0))
06304          continue;
06305       
06306       if (!ast_strlen_zero(user->secret)) {
06307          ast_copy_string(auth,user->secret, sizeof(auth));
06308       } else if (!ast_strlen_zero(user->inkeys)) {
06309          snprintf(auth, sizeof(auth), "Key: %-15.15s ", user->inkeys);
06310       } else
06311          ast_copy_string(auth, "-no secret-", sizeof(auth));
06312       
06313       if(ast_test_flag(user,IAX_CODEC_NOCAP))
06314          pstr = "REQ Only";
06315       else if(ast_test_flag(user,IAX_CODEC_NOPREFS))
06316          pstr = "Disabled";
06317       else
06318          pstr = ast_test_flag(user,IAX_CODEC_USER_FIRST) ? "Caller" : "Host";
06319       
06320       ast_cli(a->fd, FORMAT2, user->name, auth, user->authmethods, 
06321          user->contexts ? user->contexts->context : DEFAULT_CONTEXT,
06322          user->ha ? "Yes" : "No", pstr);
06323    }
06324    ao2_iterator_destroy(&i);
06325 
06326    if (havepattern)
06327       regfree(&regexbuf);
06328 
06329    return CLI_SUCCESS;
06330 #undef FORMAT
06331 #undef FORMAT2
06332 }

static char* handle_cli_iax2_test_losspct ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 3464 of file chan_iax2.c.

References ast_cli_args::argc, ast_cli_args::argv, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, and ast_cli_entry::usage.

03465 {
03466    switch (cmd) {
03467    case CLI_INIT:
03468       e->command = "iax2 test losspct";
03469       e->usage =
03470          "Usage: iax2 test losspct <percentage>\n"
03471          "       For testing, throws away <percentage> percent of incoming packets\n";
03472       return NULL;
03473    case CLI_GENERATE:
03474       return NULL;
03475    }
03476    if (a->argc != 4)
03477       return CLI_SHOWUSAGE;
03478 
03479    test_losspct = atoi(a->argv[3]);
03480 
03481    return CLI_SUCCESS;
03482 }

static char* handle_cli_iax2_unregister ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 6533 of file chan_iax2.c.

References ao2_find, ast_cli_args::argc, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_iax2_unregister(), iax2_peer::expire, expire_registry(), ast_cli_args::fd, find_peer(), ast_cli_args::line, ast_cli_args::n, iax2_peer::name, OBJ_POINTER, peer_ref(), peer_unref(), ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

06534 {
06535    struct iax2_peer *p;
06536 
06537    switch (cmd) {
06538    case CLI_INIT:
06539       e->command = "iax2 unregister";
06540       e->usage =
06541          "Usage: iax2 unregister <peername>\n"
06542          "       Unregister (force expiration) an IAX2 peer from the registry.\n";
06543       return NULL;
06544    case CLI_GENERATE:
06545       return complete_iax2_unregister(a->line, a->word, a->pos, a->n);
06546    }
06547 
06548    if (a->argc != 3)
06549       return CLI_SHOWUSAGE;
06550 
06551    p = find_peer(a->argv[2], 1);
06552    if (p) {
06553       if (p->expire > 0) {
06554          struct iax2_peer tmp_peer = {
06555             .name = a->argv[2],
06556          };
06557          struct iax2_peer *peer;
06558 
06559          peer = ao2_find(peers, &tmp_peer, OBJ_POINTER);
06560          if (peer) {
06561             expire_registry(peer_ref(peer)); /* will release its own reference when done */
06562             peer_unref(peer); /* ref from ao2_find() */
06563             ast_cli(a->fd, "Peer %s unregistered\n", a->argv[2]);
06564          } else {
06565             ast_cli(a->fd, "Peer %s not found\n", a->argv[2]);
06566          }
06567       } else {
06568          ast_cli(a->fd, "Peer %s not registered\n", a->argv[2]);
06569       }
06570    } else {
06571       ast_cli(a->fd, "Peer unknown: %s. Not unregistered\n", a->argv[2]);
06572    }
06573    return CLI_SUCCESS;
06574 }

static void handle_deferred_full_frames ( struct iax2_thread *  thread  )  [static]

Handle any deferred full frames for this thread.

Definition at line 9134 of file chan_iax2.c.

References ast_free, AST_LIST_REMOVE_HEAD, ast_mutex_lock(), ast_mutex_unlock(), iax2_pkt_buf::buf, iax2_thread::buf, iax2_thread::buf_len, iax2_thread::buf_size, iax2_thread::full_frames, iax2_pkt_buf::len, iax2_thread::lock, and socket_process().

Referenced by iax2_process_thread().

09135 {
09136    struct iax2_pkt_buf *pkt_buf;
09137 
09138    ast_mutex_lock(&thread->lock);
09139 
09140    while ((pkt_buf = AST_LIST_REMOVE_HEAD(&thread->full_frames, entry))) {
09141       ast_mutex_unlock(&thread->lock);
09142 
09143       thread->buf = pkt_buf->buf;
09144       thread->buf_len = pkt_buf->len;
09145       thread->buf_size = pkt_buf->len + 1;
09146       
09147       socket_process(thread);
09148 
09149       thread->buf = NULL;
09150       ast_free(pkt_buf);
09151 
09152       ast_mutex_lock(&thread->lock);
09153    }
09154 
09155    ast_mutex_unlock(&thread->lock);
09156 }

static int handle_error ( void   )  [static]

Definition at line 3118 of file chan_iax2.c.

References ast_inet_ntoa(), ast_log(), errno, LOG_WARNING, and netsocket.

Referenced by send_packet(), socket_read(), and transmit_trunk().

03119 {
03120    /* XXX Ideally we should figure out why an error occurred and then abort those
03121       rather than continuing to try.  Unfortunately, the published interface does
03122       not seem to work XXX */
03123 #if 0
03124    struct sockaddr_in *sin;
03125    int res;
03126    struct msghdr m;
03127    struct sock_extended_err e;
03128    m.msg_name = NULL;
03129    m.msg_namelen = 0;
03130    m.msg_iov = NULL;
03131    m.msg_control = &e;
03132    m.msg_controllen = sizeof(e);
03133    m.msg_flags = 0;
03134    res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
03135    if (res < 0)
03136       ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
03137    else {
03138       if (m.msg_controllen) {
03139          sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
03140          if (sin) 
03141             ast_log(LOG_WARNING, "Receive error from %s\n", ast_inet_ntoa(sin->sin_addr));
03142          else
03143             ast_log(LOG_WARNING, "No address detected??\n");
03144       } else {
03145          ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
03146       }
03147    }
03148 #endif
03149    return 0;
03150 }

static int iax2_ack_registry ( struct iax_ies *  ies,
struct sockaddr_in *  sin,
int  callno 
) [static]

Acknowledgment received for OUR registration.

Definition at line 8101 of file chan_iax2.c.

References iax2_registry::addr, iax_ies::apparent_addr, ast_copy_string(), ast_inet_ntoa(), ast_log(), ast_verb, iax_ies::calling_number, EVENT_FLAG_SYSTEM, iax2_registry::expire, iax2_do_register_s(), iax2_sched_replace(), inaddrcmp(), LOG_WARNING, manager_event, iax2_registry::messages, iax_ies::msgcount, iax_ies::refresh, iax2_registry::refresh, chan_iax2_pvt::reg, REG_STATE_REGISTERED, iax2_registry::regstate, iax2_registry::us, and iax_ies::username.

Referenced by socket_process().

08102 {
08103    struct iax2_registry *reg;
08104    /* Start pessimistic */
08105    char peer[256] = "";
08106    char msgstatus[60];
08107    int refresh = 60;
08108    char ourip[256] = "<Unspecified>";
08109    struct sockaddr_in oldus;
08110    struct sockaddr_in us;
08111    int oldmsgs;
08112 
08113    memset(&us, 0, sizeof(us));
08114    if (ies->apparent_addr)
08115       memmove(&us, ies->apparent_addr, sizeof(us));
08116    if (ies->username)
08117       ast_copy_string(peer, ies->username, sizeof(peer));
08118    if (ies->refresh)
08119       refresh = ies->refresh;
08120    if (ies->calling_number) {
08121       /* We don't do anything with it really, but maybe we should */
08122    }
08123    reg = iaxs[callno]->reg;
08124    if (!reg) {
08125       ast_log(LOG_WARNING, "Registry acknowledge on unknown registry '%s'\n", peer);
08126       return -1;
08127    }
08128    memcpy(&oldus, &reg->us, sizeof(oldus));
08129    oldmsgs = reg->messages;
08130    if (inaddrcmp(&reg->addr, sin)) {
08131       ast_log(LOG_WARNING, "Received unsolicited registry ack from '%s'\n", ast_inet_ntoa(sin->sin_addr));
08132       return -1;
08133    }
08134    memcpy(&reg->us, &us, sizeof(reg->us));
08135    if (ies->msgcount >= 0)
08136       reg->messages = ies->msgcount & 0xffff;      /* only low 16 bits are used in the transmission of the IE */
08137    /* always refresh the registration at the interval requested by the server
08138       we are registering to
08139    */
08140    reg->refresh = refresh;
08141    reg->expire = iax2_sched_replace(reg->expire, sched, 
08142       (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
08143    if (inaddrcmp(&oldus, &reg->us) || (reg->messages != oldmsgs)) {
08144          if (reg->messages > 255)
08145             snprintf(msgstatus, sizeof(msgstatus), " with %d new and %d old messages waiting", reg->messages & 0xff, reg->messages >> 8);
08146          else if (reg->messages > 1)
08147             snprintf(msgstatus, sizeof(msgstatus), " with %d new messages waiting\n", reg->messages);
08148          else if (reg->messages > 0)
08149             ast_copy_string(msgstatus, " with 1 new message waiting\n", sizeof(msgstatus));
08150          else
08151             ast_copy_string(msgstatus, " with no messages waiting\n", sizeof(msgstatus));
08152          snprintf(ourip, sizeof(ourip), "%s:%d", ast_inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port));
08153       ast_verb(3, "Registered IAX2 to '%s', who sees us as %s%s\n", ast_inet_ntoa(sin->sin_addr), ourip, msgstatus);
08154       manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: IAX2\r\nDomain: %s\r\nStatus: Registered\r\n", ast_inet_ntoa(sin->sin_addr));
08155    }
08156    reg->regstate = REG_STATE_REGISTERED;
08157    return 0;
08158 }

static int attribute_pure iax2_allow_new ( int  frametype,
int  subclass,
int  inbound 
) [inline, static]

Definition at line 2558 of file chan_iax2.c.

References AST_FRAME_IAX, IAX_COMMAND_FWDOWNL, IAX_COMMAND_NEW, IAX_COMMAND_POKE, IAX_COMMAND_REGREL, and IAX_COMMAND_REGREQ.

Referenced by resend_with_token(), and socket_process().

02559 {
02560    if (frametype != AST_FRAME_IAX) {
02561       return 0;
02562    }
02563    switch (subclass) {
02564    case IAX_COMMAND_NEW:
02565    case IAX_COMMAND_REGREQ:
02566    case IAX_COMMAND_FWDOWNL:
02567    case IAX_COMMAND_REGREL:
02568       return 1;
02569    case IAX_COMMAND_POKE:
02570       if (!inbound) {
02571          return 1;
02572       }
02573       break;
02574    }
02575    return 0;
02576 }

static void iax2_ami_channelupdate ( struct chan_iax2_pvt *  pvt  )  [static]

Send manager event at call setup to link between Asterisk channel name and IAX2 call identifiers.

Definition at line 1193 of file chan_iax2.c.

References chan_iax2_pvt::callno, EVENT_FLAG_SYSTEM, manager_event, ast_channel::name, chan_iax2_pvt::owner, chan_iax2_pvt::peer, and chan_iax2_pvt::peercallno.

Referenced by ast_iax2_new(), and iax2_answer().

01194 {
01195    manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
01196       "Channel: %s\r\nChanneltype: IAX2\r\nIAX2-callno-local: %d\r\nIAX2-callno-remote: %d\r\nIAX2-peer: %s\r\n",
01197       pvt->owner ? pvt->owner->name : "",
01198       pvt->callno, pvt->peercallno, pvt->peer ? pvt->peer : "");
01199 }

static int iax2_answer ( struct ast_channel *  c  )  [static]

Definition at line 5335 of file chan_iax2.c.

References AST_CONTROL_ANSWER, ast_debug, AST_FRAME_CONTROL, ast_mutex_lock(), ast_mutex_unlock(), iax2_ami_channelupdate(), PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

05336 {
05337    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
05338    ast_debug(1, "Answering IAX2 call\n");
05339    ast_mutex_lock(&iaxsl[callno]);
05340    if (iaxs[callno])
05341       iax2_ami_channelupdate(iaxs[callno]);
05342    ast_mutex_unlock(&iaxsl[callno]);
05343    return send_command_locked(callno, AST_FRAME_CONTROL, AST_CONTROL_ANSWER, 0, NULL, 0, -1);
05344 }

static int iax2_append_register ( const char *  hostname,
const char *  username,
const char *  secret,
const char *  porta 
) [static]

Definition at line 8160 of file chan_iax2.c.

References iax2_registry::addr, ast_calloc, ast_copy_string(), ast_dnsmgr_lookup(), ast_free, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, iax2_registry::dnsmgr, iax2_registry::expire, IAX_DEFAULT_PORTNO, IAX_DEFAULT_REG_EXPIRE, iax2_registry::refresh, iax2_registry::secret, and iax2_registry::username.

Referenced by iax2_register().

08162 {
08163    struct iax2_registry *reg;
08164 
08165    if (!(reg = ast_calloc(1, sizeof(*reg))))
08166       return -1;
08167 
08168    if (ast_dnsmgr_lookup(hostname, &reg->addr, &reg->dnsmgr, srvlookup ? "_iax._udp" : NULL) < 0) {
08169       ast_free(reg);
08170       return -1;
08171    }
08172 
08173    ast_copy_string(reg->username, username, sizeof(reg->username));
08174 
08175    if (secret)
08176       ast_copy_string(reg->secret, secret, sizeof(reg->secret));
08177 
08178    reg->expire = -1;
08179    reg->refresh = IAX_DEFAULT_REG_EXPIRE;
08180    reg->addr.sin_family = AF_INET;
08181    reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO);
08182 
08183    AST_LIST_LOCK(&registrations);
08184    AST_LIST_INSERT_HEAD(&registrations, reg, entry);
08185    AST_LIST_UNLOCK(&registrations);
08186    
08187    return 0;
08188 }

static enum ast_bridge_result iax2_bridge ( struct ast_channel *  c0,
struct ast_channel *  c1,
int  flags,
struct ast_frame **  fo,
struct ast_channel **  rc,
int  timeoutms 
) [static]

Definition at line 5180 of file chan_iax2.c.

References ast_channel::_softhangup, AST_BRIDGE_COMPLETE, AST_BRIDGE_DTMF_CHANNEL_0, AST_BRIDGE_DTMF_CHANNEL_1, AST_BRIDGE_FAILED, AST_BRIDGE_FAILED_NOWARN, AST_BRIDGE_IGNORE_SIGS, AST_BRIDGE_RETRY, ast_check_hangup(), AST_CONTROL_SRCUPDATE, AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_IMAGE, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree, ast_getformatname_multiple(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_read(), AST_SOFTHANGUP_DEV, ast_test_flag, ast_tvnow(), ast_tvzero(), ast_verb, ast_waitfor_n(), ast_write(), chan_iax2_pvt::bridgecallno, buf1, f, ast_frame::frametype, iax2_start_transfer(), IAX_LINGER_TIMEOUT, IAX_NOTRANSFER, IAX_TRANSFERMEDIA, lock_both(), LOG_WARNING, ast_channel::nativeformats, PTR_TO_CALLNO, ast_frame::subclass, ast_channel::tech, ast_channel::tech_pvt, TRANSFER_RELEASED, and unlock_both().

05181 {
05182    struct ast_channel *cs[3];
05183    struct ast_channel *who, *other;
05184    int to = -1;
05185    int res = -1;
05186    int transferstarted=0;
05187    struct ast_frame *f;
05188    unsigned short callno0 = PTR_TO_CALLNO(c0->tech_pvt);
05189    unsigned short callno1 = PTR_TO_CALLNO(c1->tech_pvt);
05190    struct timeval waittimer = {0, 0};
05191 
05192    /* We currently do not support native bridging if a timeoutms value has been provided */
05193    if (timeoutms > 0) {
05194       return AST_BRIDGE_FAILED;
05195    }
05196 
05197    timeoutms = -1;
05198 
05199    lock_both(callno0, callno1);
05200    if (!iaxs[callno0] || !iaxs[callno1]) {
05201       unlock_both(callno0, callno1);
05202       return AST_BRIDGE_FAILED;
05203    }
05204    /* Put them in native bridge mode */
05205    if (!(flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))) {
05206       iaxs[callno0]->bridgecallno = callno1;
05207       iaxs[callno1]->bridgecallno = callno0;
05208    }
05209    unlock_both(callno0, callno1);
05210 
05211    /* If not, try to bridge until we can execute a transfer, if we can */
05212    cs[0] = c0;
05213    cs[1] = c1;
05214    for (/* ever */;;) {
05215       /* Check in case we got masqueraded into */
05216       if ((c0->tech != &iax2_tech) || (c1->tech != &iax2_tech)) {
05217          ast_verb(3, "Can't masquerade, we're different...\n");
05218          /* Remove from native mode */
05219          if (c0->tech == &iax2_tech) {
05220             ast_mutex_lock(&iaxsl[callno0]);
05221             iaxs[callno0]->bridgecallno = 0;
05222             ast_mutex_unlock(&iaxsl[callno0]);
05223          }
05224          if (c1->tech == &iax2_tech) {
05225             ast_mutex_lock(&iaxsl[callno1]);
05226             iaxs[callno1]->bridgecallno = 0;
05227             ast_mutex_unlock(&iaxsl[callno1]);
05228          }
05229          return AST_BRIDGE_FAILED_NOWARN;
05230       }
05231       if (c0->nativeformats != c1->nativeformats) {
05232             char buf0[255];
05233             char buf1[255];
05234             ast_getformatname_multiple(buf0, sizeof(buf0) -1, c0->nativeformats);
05235             ast_getformatname_multiple(buf1, sizeof(buf1) -1, c1->nativeformats);
05236          ast_verb(3, "Operating with different codecs %d[%s] %d[%s] , can't native bridge...\n", c0->nativeformats, buf0, c1->nativeformats, buf1);
05237          /* Remove from native mode */
05238          lock_both(callno0, callno1);
05239          if (iaxs[callno0])
05240             iaxs[callno0]->bridgecallno = 0;
05241          if (iaxs[callno1])
05242             iaxs[callno1]->bridgecallno = 0;
05243          unlock_both(callno0, callno1);
05244          return AST_BRIDGE_FAILED_NOWARN;
05245       }
05246       /* check if transfered and if we really want native bridging */
05247       if (!transferstarted && !ast_test_flag(iaxs[callno0], IAX_NOTRANSFER) && !ast_test_flag(iaxs[callno1], IAX_NOTRANSFER)) {
05248          /* Try the transfer */
05249          if (iax2_start_transfer(callno0, callno1, (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1)) ||
05250                      ast_test_flag(iaxs[callno0], IAX_TRANSFERMEDIA) | ast_test_flag(iaxs[callno1], IAX_TRANSFERMEDIA)))
05251             ast_log(LOG_WARNING, "Unable to start the transfer\n");
05252          transferstarted = 1;
05253       }
05254       if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
05255          /* Call has been transferred.  We're no longer involved */
05256          struct timeval now = ast_tvnow();
05257          if (ast_tvzero(waittimer)) {
05258             waittimer = now;
05259          } else if (now.tv_sec - waittimer.tv_sec > IAX_LINGER_TIMEOUT) {
05260             c0->_softhangup |= AST_SOFTHANGUP_DEV;
05261             c1->_softhangup |= AST_SOFTHANGUP_DEV;
05262             *fo = NULL;
05263             *rc = c0;
05264             res = AST_BRIDGE_COMPLETE;
05265             break;
05266          }
05267       }
05268       to = 1000;
05269       who = ast_waitfor_n(cs, 2, &to);
05270       if (timeoutms > -1) {
05271          timeoutms -= (1000 - to);
05272          if (timeoutms < 0)
05273             timeoutms = 0;
05274       }
05275       if (!who) {
05276          if (!timeoutms) {
05277             res = AST_BRIDGE_RETRY;
05278             break;
05279          }
05280          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
05281             res = AST_BRIDGE_FAILED;
05282             break;
05283          }
05284          continue;
05285       }
05286       f = ast_read(who);
05287       if (!f) {
05288          *fo = NULL;
05289          *rc = who;
05290          res = AST_BRIDGE_COMPLETE;
05291          break;
05292       }
05293       if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS) && (f->subclass != AST_CONTROL_SRCUPDATE)) {
05294          *fo = f;
05295          *rc = who;
05296          res =  AST_BRIDGE_COMPLETE;
05297          break;
05298       }
05299       other = (who == c0) ? c1 : c0;  /* the 'other' channel */
05300       if ((f->frametype == AST_FRAME_VOICE) ||
05301          (f->frametype == AST_FRAME_TEXT) ||
05302          (f->frametype == AST_FRAME_VIDEO) || 
05303          (f->frametype == AST_FRAME_IMAGE) ||
05304          (f->frametype == AST_FRAME_DTMF) ||
05305          (f->frametype == AST_FRAME_CONTROL)) {
05306          /* monitored dtmf take out of the bridge.
05307           * check if we monitor the specific source.
05308           */
05309          int monitored_source = (who == c0) ? AST_BRIDGE_DTMF_CHANNEL_0 : AST_BRIDGE_DTMF_CHANNEL_1;
05310          if (f->frametype == AST_FRAME_DTMF && (flags & monitored_source)) {
05311             *rc = who;
05312             *fo = f;
05313             res = AST_BRIDGE_COMPLETE;
05314             /* Remove from native mode */
05315             break;
05316          }
05317          /* everything else goes to the other side */
05318          ast_write(other, f);
05319       }
05320       ast_frfree(f);
05321       /* Swap who gets priority */
05322       cs[2] = cs[0];
05323       cs[0] = cs[1];
05324       cs[1] = cs[2];
05325    }
05326    lock_both(callno0, callno1);
05327    if(iaxs[callno0])
05328       iaxs[callno0]->bridgecallno = 0;
05329    if(iaxs[callno1])
05330       iaxs[callno1]->bridgecallno = 0;
05331    unlock_both(callno0, callno1);
05332    return res;
05333 }

static int iax2_call ( struct ast_channel *  c,
char *  dest,
int  timeout 
) [static]

Definition at line 4787 of file chan_iax2.c.

References ast_channel::_state, add_empty_calltoken_ie(), create_addr_info::adsi, chan_iax2_pvt::adsi, ast_channel::adsicpe, ast_channel_datastore_find(), ast_copy_string(), ast_debug, AST_FRAME_IAX, AST_LIST_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_PRES_NUMBER_NOT_AVAILABLE, ast_setstate(), AST_STATE_DOWN, AST_STATE_RESERVED, AST_STATE_RINGING, ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_var_name(), ast_var_value(), auto_congest(), iax_ie_data::buf, CALLNO_TO_PTR, capability, ast_channel::cid, ast_callerid::cid_ani, ast_callerid::cid_dnid, ast_callerid::cid_name, ast_callerid::cid_num, ast_callerid::cid_pres, ast_callerid::cid_rdnis, ast_callerid::cid_tns, ast_callerid::cid_ton, context, create_addr_info::context, ast_channel::context, parsed_dial_string::context, create_addr(), ast_datastore::data, chan_iax2_pvt::encmethods, create_addr_info::encmethods, parsed_dial_string::exten, iax2_datetime(), iax2_sched_add(), IAX_COMMAND_NEW, IAX_FORCE_ENCRYPT, IAX_IE_ADSICPE, iax_ie_append(), iax_ie_append_byte(), iax_ie_append_int(), iax_ie_append_raw(), iax_ie_append_short(), iax_ie_append_str(), IAX_IE_AUTOANSWER, IAX_IE_CALLED_CONTEXT, IAX_IE_CALLED_NUMBER, IAX_IE_CALLING_ANI, IAX_IE_CALLING_NAME, IAX_IE_CALLING_NUMBER, IAX_IE_CALLINGPRES, IAX_IE_CALLINGTNS, IAX_IE_CALLINGTON, IAX_IE_CAPABILITY, IAX_IE_CODEC_PREFS, IAX_IE_DATETIME, IAX_IE_DNID, IAX_IE_ENCRYPTION, IAX_IE_FORMAT, IAX_IE_LANGUAGE, IAX_IE_OSPTOKEN, IAX_IE_RDNIS, IAX_IE_USERNAME, IAX_IE_VARIABLE, IAX_IE_VERSION, IAX_MAX_OSPBLOCK_SIZE, IAX_MAX_OSPTOKEN_SIZE, IAX_PROTO_VERSION, IAX_SENDANI, chan_iax2_pvt::initid, parsed_dial_string::key, ast_channel::language, LOG_WARNING, chan_iax2_pvt::maxtime, create_addr_info::mohinterpret, create_addr_info::mohsuggest, ast_channel::name, ast_channel::nativeformats, parsed_dial_string::options, create_addr_info::outkey, parse_dial_string(), parsed_dial_string::password, pbx_builtin_getvar_helper(), parsed_dial_string::peer, create_addr_info::peercontext, chan_iax2_pvt::pingtime, parsed_dial_string::port, iax_ie_data::pos, create_addr_info::prefs, PTR_TO_CALLNO, secret, create_addr_info::secret, send_command(), create_addr_info::sockfd, chan_iax2_pvt::sockfd, ast_channel::tech_pvt, create_addr_info::timezone, create_addr_info::username, parsed_dial_string::username, and var.

04788 {
04789    struct sockaddr_in sin;
04790    char *l=NULL, *n=NULL, *tmpstr;
04791    struct iax_ie_data ied;
04792    char *defaultrdest = "s";
04793    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
04794    struct parsed_dial_string pds;
04795    struct create_addr_info cai;
04796    struct ast_var_t *var;
04797    struct ast_datastore *variablestore = ast_channel_datastore_find(c, &iax2_variable_datastore_info, NULL);
04798    const char* osp_token_ptr;
04799    unsigned int osp_token_length;
04800    unsigned char osp_block_index;
04801    unsigned int osp_block_length;
04802    unsigned char osp_buffer[256];
04803 
04804    if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
04805       ast_log(LOG_WARNING, "Channel is already in use (%s)?\n", c->name);
04806       return -1;
04807    }
04808 
04809    memset(&cai, 0, sizeof(cai));
04810    cai.encmethods = iax2_encryption;
04811 
04812    memset(&pds, 0, sizeof(pds));
04813    tmpstr = ast_strdupa(dest);
04814    parse_dial_string(tmpstr, &pds);
04815 
04816    if (ast_strlen_zero(pds.peer)) {
04817       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", dest);
04818       return -1;
04819    }
04820    if (!pds.exten) {
04821       pds.exten = defaultrdest;
04822    }
04823    if (create_addr(pds.peer, c, &sin, &cai)) {
04824       ast_log(LOG_WARNING, "No address associated with '%s'\n", pds.peer);
04825       return -1;
04826    }
04827    if (ast_strlen_zero(cai.secret) && ast_test_flag(iaxs[callno], IAX_FORCE_ENCRYPT)) {
04828       ast_log(LOG_WARNING, "Call terminated. No secret given and force encrypt enabled\n");
04829       return -1;
04830    }
04831    if (!pds.username && !ast_strlen_zero(cai.username))
04832       pds.username = cai.username;
04833    if (!pds.password && !ast_strlen_zero(cai.secret))
04834       pds.password = cai.secret;
04835    if (!pds.key && !ast_strlen_zero(cai.outkey))
04836       pds.key = cai.outkey;
04837    if (!pds.context && !ast_strlen_zero(cai.peercontext))
04838       pds.context = cai.peercontext;
04839 
04840    /* Keep track of the context for outgoing calls too */
04841    ast_copy_string(c->context, cai.context, sizeof(c->context));
04842 
04843    if (pds.port)
04844       sin.sin_port = htons(atoi(pds.port));
04845 
04846    l = c->cid.cid_num;
04847    n = c->cid.cid_name;
04848 
04849    /* Now build request */ 
04850    memset(&ied, 0, sizeof(ied));
04851 
04852    /* On new call, first IE MUST be IAX version of caller */
04853    iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
04854    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, pds.exten);
04855    if (pds.options && strchr(pds.options, 'a')) {
04856       /* Request auto answer */
04857       iax_ie_append(&ied, IAX_IE_AUTOANSWER);
04858    }
04859 
04860    iax_ie_append_str(&ied, IAX_IE_CODEC_PREFS, cai.prefs);
04861 
04862    if (l) {
04863       iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, l);
04864       iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES, c->cid.cid_pres);
04865    } else {
04866       if (n)
04867          iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES, c->cid.cid_pres);
04868       else
04869          iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES, AST_PRES_NUMBER_NOT_AVAILABLE);
04870    }
04871 
04872    iax_ie_append_byte(&ied, IAX_IE_CALLINGTON, c->cid.cid_ton);
04873    iax_ie_append_short(&ied, IAX_IE_CALLINGTNS, c->cid.cid_tns);
04874 
04875    if (n)
04876       iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, n);
04877    if (ast_test_flag(iaxs[callno], IAX_SENDANI) && c->cid.cid_ani)
04878       iax_ie_append_str(&ied, IAX_IE_CALLING_ANI, c->cid.cid_ani);
04879 
04880    if (!ast_strlen_zero(c->language))
04881       iax_ie_append_str(&ied, IAX_IE_LANGUAGE, c->language);
04882    if (!ast_strlen_zero(c->cid.cid_dnid))
04883       iax_ie_append_str(&ied, IAX_IE_DNID, c->cid.cid_dnid);
04884    if (!ast_strlen_zero(c->cid.cid_rdnis))
04885       iax_ie_append_str(&ied, IAX_IE_RDNIS, c->cid.cid_rdnis);
04886 
04887    if (pds.context)
04888       iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, pds.context);
04889 
04890    if (pds.username)
04891       iax_ie_append_str(&ied, IAX_IE_USERNAME, pds.username);
04892 
04893    if (cai.encmethods)
04894       iax_ie_append_short(&ied, IAX_IE_ENCRYPTION, cai.encmethods);
04895 
04896    ast_mutex_lock(&iaxsl[callno]);
04897 
04898    if (!ast_strlen_zero(c->context))
04899       ast_string_field_set(iaxs[callno], context, c->context);
04900 
04901    if (pds.username)
04902       ast_string_field_set(iaxs[callno], username, pds.username);
04903 
04904    iaxs[callno]->encmethods = cai.encmethods;
04905 
04906    iaxs[callno]->adsi = cai.adsi;
04907    
04908    ast_string_field_set(iaxs[callno], mohinterpret, cai.mohinterpret);
04909    ast_string_field_set(iaxs[callno], mohsuggest, cai.mohsuggest);
04910 
04911    if (pds.key)
04912       ast_string_field_set(iaxs[callno], outkey, pds.key);
04913    if (pds.password)
04914       ast_string_field_set(iaxs[callno], secret, pds.password);
04915 
04916    iax_ie_append_int(&ied, IAX_IE_FORMAT, c->nativeformats);
04917    iax_ie_append_int(&ied, IAX_IE_CAPABILITY, iaxs[callno]->capability);
04918    iax_ie_append_short(&ied, IAX_IE_ADSICPE, c->adsicpe);
04919    iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime(cai.timezone));
04920 
04921    if (iaxs[callno]->maxtime) {
04922       /* Initialize pingtime and auto-congest time */
04923       iaxs[callno]->pingtime = iaxs[callno]->maxtime / 2;
04924       iaxs[callno]->initid = iax2_sched_add(sched, iaxs[callno]->maxtime * 2, auto_congest, CALLNO_TO_PTR(callno));
04925    } else if (autokill) {
04926       iaxs[callno]->pingtime = autokill / 2;
04927       iaxs[callno]->initid = iax2_sched_add(sched, autokill * 2, auto_congest, CALLNO_TO_PTR(callno));
04928    }
04929 
04930    /* Check if there is an OSP token */
04931    osp_token_ptr = pbx_builtin_getvar_helper(c, "IAX2OSPTOKEN");
04932    if (!ast_strlen_zero(osp_token_ptr)) {
04933       if ((osp_token_length = strlen(osp_token_ptr)) <= IAX_MAX_OSPTOKEN_SIZE) {
04934          osp_block_index = 0;
04935          while (osp_token_length > 0) {
04936             osp_block_length = IAX_MAX_OSPBLOCK_SIZE < osp_token_length ? IAX_MAX_OSPBLOCK_SIZE : osp_token_length;
04937             osp_buffer[0] = osp_block_index;
04938             memcpy(osp_buffer + 1, osp_token_ptr, osp_block_length);
04939             iax_ie_append_raw(&ied, IAX_IE_OSPTOKEN, osp_buffer, osp_block_length + 1);
04940             osp_block_index++;
04941             osp_token_ptr += osp_block_length;
04942             osp_token_length -= osp_block_length;
04943          } 
04944       } else
04945          ast_log(LOG_WARNING, "OSP token is too long\n");
04946    } else if (iaxdebug)
04947       ast_debug(1, "OSP token is undefined\n");
04948 
04949    /* send the command using the appropriate socket for this peer */
04950    iaxs[callno]->sockfd = cai.sockfd;
04951 
04952    /* Add remote vars */
04953    if (variablestore) {
04954       AST_LIST_HEAD(, ast_var_t) *variablelist = variablestore->data;
04955       ast_debug(1, "Found an IAX variable store on this channel\n");
04956       AST_LIST_LOCK(variablelist);
04957       AST_LIST_TRAVERSE(variablelist, var, entries) {
04958          char tmp[256];
04959          int i;
04960          ast_debug(1, "Found IAXVAR '%s' with value '%s' (to transmit)\n", ast_var_name(var), ast_var_value(var));
04961          /* Automatically divide the value up into sized chunks */
04962          for (i = 0; i < strlen(ast_var_value(var)); i += 255 - (strlen(ast_var_name(var)) + 1)) {
04963             snprintf(tmp, sizeof(tmp), "%s=%s", ast_var_name(var), ast_var_value(var) + i);
04964             iax_ie_append_str(&ied, IAX_IE_VARIABLE, tmp);
04965          }
04966       }
04967       AST_LIST_UNLOCK(variablelist);
04968    }
04969 
04970    /* Transmit the string in a "NEW" request */
04971    add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
04972    send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
04973 
04974    ast_mutex_unlock(&iaxsl[callno]);
04975    ast_setstate(c, AST_STATE_RINGING);
04976 
04977    return 0;
04978 }

static int iax2_canmatch ( struct ast_channel *  chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

part of the IAX2 dial plan switch interface

Definition at line 13248 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), CACHE_FLAG_CANEXIST, find_cache(), iax2_dpcache::flags, LOG_NOTICE, and LOG_WARNING.

13249 {
13250    int res = 0;
13251    struct iax2_dpcache *dp = NULL;
13252 #if 0
13253    ast_log(LOG_NOTICE, "iax2_canmatch: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
13254 #endif
13255    if ((priority != 1) && (priority != 2))
13256       return 0;
13257 
13258    AST_LIST_LOCK(&dpcache);
13259    if ((dp = find_cache(chan, data, context, exten, priority))) {
13260       if (dp->flags & CACHE_FLAG_CANEXIST)
13261          res = 1;
13262    } else {
13263       ast_log(LOG_WARNING, "Unable to make DP cache\n");
13264    }
13265    AST_LIST_UNLOCK(&dpcache);
13266 
13267    return res;
13268 }

static unsigned int iax2_datetime ( const char *  tz  )  [static]

Definition at line 4458 of file chan_iax2.c.

References ast_localtime(), ast_strlen_zero(), ast_tvnow(), ast_tm::tm_hour, ast_tm::tm_mday, ast_tm::tm_min, ast_tm::tm_mon, ast_tm::tm_sec, and ast_tm::tm_year.

Referenced by iax2_call(), and update_registry().

04459 {
04460    struct timeval t = ast_tvnow();
04461    struct ast_tm tm;
04462    unsigned int tmp;
04463    ast_localtime(&t, &tm, ast_strlen_zero(tz) ? NULL : tz);
04464    tmp  = (tm.tm_sec >> 1) & 0x1f;        /* 5 bits of seconds */
04465    tmp |= (tm.tm_min & 0x3f) << 5;        /* 6 bits of minutes */
04466    tmp |= (tm.tm_hour & 0x1f) << 11;      /* 5 bits of hours */
04467    tmp |= (tm.tm_mday & 0x1f) << 16;      /* 5 bits of day of month */
04468    tmp |= ((tm.tm_mon + 1) & 0xf) << 21;     /* 4 bits of month */
04469    tmp |= ((tm.tm_year - 100) & 0x7f) << 25; /* 7 bits of year */
04470    return tmp;
04471 }

static void iax2_destroy ( int  callno  )  [static]

Definition at line 3224 of file chan_iax2.c.

References ao2_ref, ast_channel_trylock, ast_channel_unlock, ast_debug, ast_queue_hangup(), DEADLOCK_AVOIDANCE, iax2_destroy_helper(), chan_iax2_pvt::owner, chan_iax2_pvt::peercallno, remove_by_peercallno(), remove_by_transfercallno(), chan_iax2_pvt::transfercallno, and update_max_trunk().

Referenced by __attempt_transmit(), __iax2_poke_noanswer(), __unload_module(), delete_users(), iax2_do_register(), iax2_hangup(), iax2_poke_peer(), peer_destructor(), scheduled_destroy(), and socket_process().

03225 {
03226    struct chan_iax2_pvt *pvt = NULL;
03227    struct ast_channel *owner = NULL;
03228 
03229 retry:
03230    if ((pvt = iaxs[callno])) {
03231 #if 0
03232       /* iax2_destroy_helper gets called from this function later on.  When
03233        * called twice, we get the (previously) familiar FRACK! errors in
03234        * devmode, from the scheduler.  An alternative to this approach is to
03235        * reset the scheduler entries to -1 when they're deleted in
03236        * iax2_destroy_helper().  That approach was previously decided to be
03237        * "wrong" because "the memory is going to be deallocated anyway.  Why
03238        * should we be resetting those values?" */
03239       iax2_destroy_helper(pvt);
03240 #endif
03241    }
03242 
03243    owner = pvt ? pvt->owner : NULL;
03244 
03245    if (owner) {
03246       if (ast_channel_trylock(owner)) {
03247          ast_debug(3, "Avoiding IAX destroy deadlock\n");
03248          DEADLOCK_AVOIDANCE(&iaxsl[callno]);
03249          goto retry;
03250       }
03251    }
03252 
03253    if (!owner) {
03254       iaxs[callno] = NULL;
03255    }
03256 
03257    if (pvt) {
03258       if (!owner) {
03259          pvt->owner = NULL;
03260       } else {
03261          /* If there's an owner, prod it to give up */
03262          /* It is ok to use ast_queue_hangup() here instead of iax2_queue_hangup()
03263           * because we already hold the owner channel lock. */
03264          ast_queue_hangup(owner);
03265       }
03266 
03267       if (pvt->peercallno) {
03268          remove_by_peercallno(pvt);
03269       }
03270 
03271       if (pvt->transfercallno) {
03272          remove_by_transfercallno(pvt);
03273       }
03274 
03275       if (!owner) {
03276          ao2_ref(pvt, -1);
03277          pvt = NULL;
03278       }
03279    }
03280 
03281    if (owner) {
03282       ast_channel_unlock(owner);
03283    }
03284 
03285    if (callno & 0x4000) {
03286       update_max_trunk();
03287    }
03288 }

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 1624 of file chan_iax2.c.

References ao2_find, ast_atomic_fetchadd_int(), ast_clear_flag, AST_SCHED_DEL_SPINLOCK, ast_sched_thread_del, ast_sched_thread_get_context(), ast_test_flag, chan_iax2_pvt::authid, chan_iax2_pvt::autoid, chan_iax2_pvt::callno, iax2_user::curauthreq, IAX_MAXAUTHREQ, chan_iax2_pvt::initid, chan_iax2_pvt::jbid, chan_iax2_pvt::keyrotateid, chan_iax2_pvt::lagid, iax2_user::name, OBJ_POINTER, chan_iax2_pvt::pingid, user_unref(), and chan_iax2_pvt::username.

Referenced by iax2_destroy(), iax2_predestroy(), pvt_destructor(), and stop_stuff().

01625 {
01626    /* Decrement AUTHREQ count if needed */
01627    if (ast_test_flag(pvt, IAX_MAXAUTHREQ)) {
01628       struct iax2_user *user;
01629       struct iax2_user tmp_user = {
01630          .name = pvt->username,
01631       };
01632 
01633       user = ao2_find(users, &tmp_user, OBJ_POINTER);
01634       if (user) {
01635          ast_atomic_fetchadd_int(&user->curauthreq, -1);
01636          user_unref(user); 
01637       }
01638 
01639       ast_clear_flag(pvt, IAX_MAXAUTHREQ);
01640    }
01641    /* No more pings or lagrq's */
01642    AST_SCHED_DEL_SPINLOCK(ast_sched_thread_get_context(sched), pvt->pingid, &iaxsl[pvt->callno]);
01643    AST_SCHED_DEL_SPINLOCK(ast_sched_thread_get_context(sched), pvt->lagid, &iaxsl[pvt->callno]);
01644    ast_sched_thread_del(sched, pvt->autoid);
01645    ast_sched_thread_del(sched, pvt->authid);
01646    ast_sched_thread_del(sched, pvt->initid);
01647    ast_sched_thread_del(sched, pvt->jbid);
01648    ast_sched_thread_del(sched, pvt->keyrotateid);
01649 }

static int iax2_devicestate ( void *  data  )  [static]

Part of the device state notification system ---.

Definition at line 13451 of file chan_iax2.c.

References iax2_peer::addr, ast_debug, AST_DEVICE_INVALID, AST_DEVICE_UNAVAILABLE, AST_DEVICE_UNKNOWN, ast_log(), ast_strdupa, ast_strlen_zero(), iax2_peer::defaddr, find_peer(), iax2_peer::historicms, iax2_peer::lastms, LOG_WARNING, iax2_peer::maxms, parse_dial_string(), parsed_dial_string::peer, and peer_unref().

13452 {
13453    struct parsed_dial_string pds;
13454    char *tmp = ast_strdupa(data);
13455    struct iax2_peer *p;
13456    int res = AST_DEVICE_INVALID;
13457 
13458    memset(&pds, 0, sizeof(pds));
13459    parse_dial_string(tmp, &pds);
13460 
13461    if (ast_strlen_zero(pds.peer)) {
13462       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", (char *) data);
13463       return res;
13464    }
13465    
13466    ast_debug(3, "Checking device state for device %s\n", pds.peer);
13467 
13468    /* SLD: FIXME: second call to find_peer during registration */
13469    if (!(p = find_peer(pds.peer, 1)))
13470       return res;
13471 
13472    res = AST_DEVICE_UNAVAILABLE;
13473    ast_debug(3, "iax2_devicestate: Found peer. What's device state of %s? addr=%d, defaddr=%d maxms=%d, lastms=%d\n",
13474       pds.peer, p->addr.sin_addr.s_addr, p->defaddr.sin_addr.s_addr, p->maxms, p->lastms);
13475    
13476    if ((p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) &&
13477        (!p->maxms || ((p->lastms > -1) && (p->historicms <= p->maxms)))) {
13478       /* Peer is registered, or have default IP address
13479          and a valid registration */
13480       if (p->historicms == 0 || p->historicms <= p->maxms)
13481          /* let the core figure out whether it is in use or not */
13482          res = AST_DEVICE_UNKNOWN;  
13483    }
13484 
13485    peer_unref(p);
13486 
13487    return res;
13488 }

static int iax2_digit_begin ( struct ast_channel *  c,
char  digit 
) [static]

Definition at line 4082 of file chan_iax2.c.

References AST_FRAME_DTMF_BEGIN, PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

04083 {
04084    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_DTMF_BEGIN, digit, 0, NULL, 0, -1);
04085 }

static int iax2_digit_end ( struct ast_channel *  c,
char  digit,
unsigned int  duration 
) [static]

Definition at line 4087 of file chan_iax2.c.

References AST_FRAME_DTMF_END, PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

04088 {
04089    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_DTMF_END, digit, 0, NULL, 0, -1);
04090 }

static int iax2_do_register ( struct iax2_registry *  reg  )  [static]

Definition at line 11286 of file chan_iax2.c.

References add_empty_calltoken_ie(), iax2_registry::addr, ast_debug, ast_dnsmgr_changed(), ast_dnsmgr_refresh(), AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax_ie_data::buf, iax2_registry::callno, iax2_registry::dnsmgr, iax2_registry::expire, find_callno_locked(), iax2_destroy(), iax2_do_register_s(), iax2_sched_replace(), IAX_COMMAND_REGREQ, iax_ie_append_short(), iax_ie_append_str(), IAX_IE_REFRESH, IAX_IE_USERNAME, LOG_WARNING, NEW_FORCE, iax_ie_data::pos, iax2_registry::refresh, chan_iax2_pvt::reg, REG_STATE_REGSENT, REG_STATE_TIMEOUT, iax2_registry::regstate, send_command(), and iax2_registry::username.

Referenced by __iax2_do_register_s(), load_module(), and reload_config().

11287 {
11288    struct iax_ie_data ied;
11289    if (iaxdebug)
11290       ast_debug(1, "Sending registration request for '%s'\n", reg->username);
11291 
11292    if (reg->dnsmgr && 
11293        ((reg->regstate == REG_STATE_TIMEOUT) || !reg->addr.sin_addr.s_addr)) {
11294       /* Maybe the IP has changed, force DNS refresh */
11295       ast_dnsmgr_refresh(reg->dnsmgr);
11296    }
11297    
11298    /*
11299     * if IP has Changed, free allocated call to create a new one with new IP
11300     * call has the pointer to IP and must be updated to the new one
11301     */
11302    if (reg->dnsmgr && ast_dnsmgr_changed(reg->dnsmgr) && (reg->callno > 0)) {
11303       int callno = reg->callno;
11304       ast_mutex_lock(&iaxsl[callno]);
11305       iax2_destroy(callno);
11306       ast_mutex_unlock(&iaxsl[callno]);
11307       reg->callno = 0;
11308    }
11309    if (!reg->addr.sin_addr.s_addr) {
11310       if (iaxdebug)
11311          ast_debug(1, "Unable to send registration request for '%s' without IP address\n", reg->username);
11312       /* Setup the next registration attempt */
11313       reg->expire = iax2_sched_replace(reg->expire, sched, 
11314          (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
11315       return -1;
11316    }
11317 
11318    if (!reg->callno) {
11319       ast_debug(3, "Allocate call number\n");
11320       reg->callno = find_callno_locked(0, 0, &reg->addr, NEW_FORCE, defaultsockfd, 0);
11321       if (reg->callno < 1) {
11322          ast_log(LOG_WARNING, "Unable to create call for registration\n");
11323          return -1;
11324       } else
11325          ast_debug(3, "Registration created on call %d\n", reg->callno);
11326       iaxs[reg->callno]->reg = reg;
11327       ast_mutex_unlock(&iaxsl[reg->callno]);
11328    }
11329    /* Setup the next registration a little early */
11330    reg->expire = iax2_sched_replace(reg->expire, sched, 
11331       (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
11332    /* Send the request */
11333    memset(&ied, 0, sizeof(ied));
11334    iax_ie_append_str(&ied, IAX_IE_USERNAME, reg->username);
11335    iax_ie_append_short(&ied, IAX_IE_REFRESH, reg->refresh);
11336    add_empty_calltoken_ie(iaxs[reg->callno], &ied); /* this _MUST_ be the last ie added */
11337    send_command(iaxs[reg->callno],AST_FRAME_IAX, IAX_COMMAND_REGREQ, 0, ied.buf, ied.pos, -1);
11338    reg->regstate = REG_STATE_REGSENT;
11339    return 0;
11340 }

static int iax2_do_register_s ( const void *  data  )  [static]

Definition at line 7945 of file chan_iax2.c.

References __iax2_do_register_s(), and schedule_action.

Referenced by iax2_ack_registry(), and iax2_do_register().

07946 {
07947 #ifdef SCHED_MULTITHREADED
07948    if (schedule_action(__iax2_do_register_s, data))
07949 #endif      
07950       __iax2_do_register_s(data);
07951    return 0;
07952 }

static void iax2_dprequest ( struct iax2_dpcache *  dp,
int  callno 
) [static]

Definition at line 8695 of file chan_iax2.c.

References AST_FRAME_IAX, auto_hangup(), chan_iax2_pvt::autoid, iax_ie_data::buf, CACHE_FLAG_TRANSMITTED, iax2_dpcache::exten, iax2_dpcache::flags, iax2_sched_replace(), IAX_COMMAND_DPREQ, iax_ie_append_str(), IAX_IE_CALLED_NUMBER, iax_ie_data::pos, and send_command().

Referenced by find_cache(), and socket_process().

08696 {
08697    struct iax_ie_data ied;
08698    /* Auto-hangup with 30 seconds of inactivity */
08699    iaxs[callno]->autoid = iax2_sched_replace(iaxs[callno]->autoid, 
08700       sched, 30000, auto_hangup, (void *)(long)callno);
08701    memset(&ied, 0, sizeof(ied));
08702    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, dp->exten);
08703    send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_DPREQ, 0, ied.buf, ied.pos, -1);
08704    dp->flags |= CACHE_FLAG_TRANSMITTED;
08705 }

static void * iax2_dup_variable_datastore ( void *  old  )  [static]

Definition at line 1208 of file chan_iax2.c.

References ast_calloc, AST_LIST_HEAD, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_var_assign(), ast_var_name(), ast_var_value(), ast_var_t::entries, and LOG_ERROR.

01209 {
01210    AST_LIST_HEAD(, ast_var_t) *oldlist = old, *newlist;
01211    struct ast_var_t *oldvar, *newvar;
01212 
01213    newlist = ast_calloc(sizeof(*newlist), 1);
01214    if (!newlist) {
01215       ast_log(LOG_ERROR, "Unable to duplicate iax2 variables\n");
01216       return NULL;
01217    }
01218 
01219    AST_LIST_HEAD_INIT(newlist);
01220    AST_LIST_LOCK(oldlist);
01221    AST_LIST_TRAVERSE(oldlist, oldvar, entries) {
01222       newvar = ast_var_assign(ast_var_name(oldvar), ast_var_value(oldvar));
01223       if (newvar)
01224          AST_LIST_INSERT_TAIL(newlist, newvar, entries);
01225       else
01226          ast_log(LOG_ERROR, "Unable to duplicate iax2 variable '%s'\n", ast_var_name(oldvar));
01227    }
01228    AST_LIST_UNLOCK(oldlist);
01229    return newlist;
01230 }

static int iax2_exec ( struct ast_channel *  chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

Execute IAX2 dialplan switch.

Definition at line 13294 of file chan_iax2.c.

References ast_copy_string(), AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_verb, CACHE_FLAG_EXISTS, find_cache(), iax2_dpcache::flags, LOG_NOTICE, LOG_WARNING, pbx_builtin_getvar_helper(), pbx_exec(), and pbx_findapp().

13295 {
13296    char odata[256];
13297    char req[256];
13298    char *ncontext;
13299    struct iax2_dpcache *dp = NULL;
13300    struct ast_app *dial = NULL;
13301 #if 0
13302    ast_log(LOG_NOTICE, "iax2_exec: con: %s, exten: %s, pri: %d, cid: %s, data: %s, newstack: %d\n", context, exten, priority, callerid ? callerid : "<unknown>", data, newstack);
13303 #endif
13304    if (priority == 2) {
13305       /* Indicate status, can be overridden in dialplan */
13306       const char *dialstatus = pbx_builtin_getvar_helper(chan, "DIALSTATUS");
13307       if (dialstatus) {
13308          dial = pbx_findapp(dialstatus);
13309          if (dial) 
13310             pbx_exec(chan, dial, "");
13311       }
13312       return -1;
13313    } else if (priority != 1)
13314       return -1;
13315 
13316    AST_LIST_LOCK(&dpcache);
13317    if ((dp = find_cache(chan, data, context, exten, priority))) {
13318       if (dp->flags & CACHE_FLAG_EXISTS) {
13319          ast_copy_string(odata, data, sizeof(odata));
13320          ncontext = strchr(odata, '/');
13321          if (ncontext) {
13322             *ncontext = '\0';
13323             ncontext++;
13324             snprintf(req, sizeof(req), "IAX2/%s/%s@%s", odata, exten, ncontext);
13325          } else {
13326             snprintf(req, sizeof(req), "IAX2/%s/%s", odata, exten);
13327          }
13328          ast_verb(3, "Executing Dial('%s')\n", req);
13329       } else {
13330          AST_LIST_UNLOCK(&dpcache);
13331          ast_log(LOG_WARNING, "Can't execute nonexistent extension '%s[@%s]' in data '%s'\n", exten, context, data);
13332          return -1;
13333       }
13334    }
13335    AST_LIST_UNLOCK(&dpcache);
13336 
13337    if ((dial = pbx_findapp("Dial")))
13338       return pbx_exec(chan, dial, req);
13339    else
13340       ast_log(LOG_WARNING, "No dial application registered\n");
13341 
13342    return -1;
13343 }

static int iax2_exists ( struct ast_channel *  chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

Part of the IAX2 switch interface.

Definition at line 13225 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), CACHE_FLAG_EXISTS, find_cache(), iax2_dpcache::flags, LOG_NOTICE, and LOG_WARNING.

13226 {
13227    int res = 0;
13228    struct iax2_dpcache *dp = NULL;
13229 #if 0
13230    ast_log(LOG_NOTICE, "iax2_exists: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
13231 #endif
13232    if ((priority != 1) && (priority != 2))
13233       return 0;
13234 
13235    AST_LIST_LOCK(&dpcache);
13236    if ((dp = find_cache(chan, data, context, exten, priority))) {
13237       if (dp->flags & CACHE_FLAG_EXISTS)
13238          res = 1;
13239    } else {
13240       ast_log(LOG_WARNING, "Unable to make DP cache\n");
13241    }
13242    AST_LIST_UNLOCK(&dpcache);
13243 
13244    return res;
13245 }

static int iax2_fixup ( struct ast_channel *  oldchannel,
struct ast_channel *  newchan 
) [static]

Definition at line 4109 of file chan_iax2.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), LOG_WARNING, chan_iax2_pvt::owner, PTR_TO_CALLNO, and ast_channel::tech_pvt.

04110 {
04111    unsigned short callno = PTR_TO_CALLNO(newchan->tech_pvt);
04112    ast_mutex_lock(&iaxsl[callno]);
04113    if (iaxs[callno])
04114       iaxs[callno]->owner = newchan;
04115    else
04116       ast_log(LOG_WARNING, "Uh, this isn't a good sign...\n");
04117    ast_mutex_unlock(&iaxsl[callno]);
04118    return 0;
04119 }

static void iax2_frame_free ( struct iax_frame *  fr  )  [static]

static void iax2_free_variable_datastore ( void *  old  )  [static]

Definition at line 1232 of file chan_iax2.c.

References ast_free, AST_LIST_HEAD, AST_LIST_HEAD_DESTROY, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, and ast_var_t::entries.

01233 {
01234    AST_LIST_HEAD(, ast_var_t) *oldlist = old;
01235    struct ast_var_t *oldvar;
01236 
01237    AST_LIST_LOCK(oldlist);
01238    while ((oldvar = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
01239       ast_free(oldvar);
01240    }
01241    AST_LIST_UNLOCK(oldlist);
01242    AST_LIST_HEAD_DESTROY(oldlist);
01243    ast_free(oldlist);
01244 }

static int iax2_getpeername ( struct sockaddr_in  sin,
char *  host,
int  len 
) [static]

Definition at line 1591 of file chan_iax2.c.

References iax2_peer::addr, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_copy_string(), iax2_peer::name, peer_unref(), and realtime_peer().

Referenced by __find_callno().

01592 {
01593    struct iax2_peer *peer = NULL;
01594    int res = 0;
01595    struct ao2_iterator i;
01596 
01597    i = ao2_iterator_init(peers, 0);
01598    while ((peer = ao2_iterator_next(&i))) {
01599       if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
01600           (peer->addr.sin_port == sin.sin_port)) {
01601          ast_copy_string(host, peer->name, len);
01602          peer_unref(peer);
01603          res = 1;
01604          break;
01605       }
01606       peer_unref(peer);
01607    }
01608    ao2_iterator_destroy(&i);
01609 
01610    if (!peer) {
01611       peer = realtime_peer(NULL, &sin);
01612       if (peer) {
01613          ast_copy_string(host, peer->name, len);
01614          peer_unref(peer);
01615          res = 1;
01616       }
01617    }
01618 
01619    return res;
01620 }

static int iax2_getpeertrunk ( struct sockaddr_in  sin  )  [static]

Definition at line 5403 of file chan_iax2.c.

References iax2_peer::addr, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_test_flag, IAX_TRUNK, and peer_unref().

Referenced by check_access().

05404 {
05405    struct iax2_peer *peer;
05406    int res = 0;
05407    struct ao2_iterator i;
05408 
05409    i = ao2_iterator_init(peers, 0);
05410    while ((peer = ao2_iterator_next(&i))) {
05411       if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
05412           (peer->addr.sin_port == sin.sin_port)) {
05413          res = ast_test_flag(peer, IAX_TRUNK);
05414          peer_unref(peer);
05415          break;
05416       }
05417       peer_unref(peer);
05418    }
05419    ao2_iterator_destroy(&i);
05420 
05421    return res;
05422 }

static int iax2_hangup ( struct ast_channel *  c  )  [static]

Definition at line 4980 of file chan_iax2.c.

References ast_debug, AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_thread_add(), ast_test_flag, ast_verb, iax_ie_data::buf, CALLNO_TO_PTR, ast_channel::hangupcause, iax2_destroy(), iax2_predestroy(), IAX_ALREADYGONE, IAX_COMMAND_HANGUP, iax_ie_append_byte(), IAX_IE_CAUSECODE, LOG_ERROR, LOG_WARNING, ast_channel::name, iax_ie_data::pos, PTR_TO_CALLNO, scheduled_destroy(), send_command_final(), and ast_channel::tech_pvt.

04981 {
04982    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
04983    struct iax_ie_data ied;
04984    int alreadygone;
04985    memset(&ied, 0, sizeof(ied));
04986    ast_mutex_lock(&iaxsl[callno]);
04987    if (callno && iaxs[callno]) {
04988       ast_debug(1, "We're hanging up %s now...\n", c->name);
04989       alreadygone = ast_test_flag(iaxs[callno], IAX_ALREADYGONE);
04990       /* Send the hangup unless we have had a transmission error or are already gone */
04991       iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, (unsigned char)c->hangupcause);
04992       if (!iaxs[callno]->error && !alreadygone) {
04993          if (send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, ied.buf, ied.pos, -1)) {
04994             ast_log(LOG_WARNING, "No final packet could be sent for callno %d\n", callno);
04995          }
04996          if (!iaxs[callno]) {
04997             ast_mutex_unlock(&iaxsl[callno]);
04998             return 0;
04999          }
05000       }
05001       /* Explicitly predestroy it */
05002       iax2_predestroy(callno);
05003       /* If we were already gone to begin with, destroy us now */
05004       if (iaxs[callno] && alreadygone) {
05005          ast_debug(1, "Really destroying %s now...\n", c->name);
05006          iax2_destroy(callno);
05007       } else if (iaxs[callno]) {
05008          if (ast_sched_thread_add(sched, 10000, scheduled_destroy, CALLNO_TO_PTR(callno)) < 0) {
05009             ast_log(LOG_ERROR, "Unable to schedule iax2 callno %d destruction?!!  Destroying immediately.\n", callno);
05010             iax2_destroy(callno);
05011          }
05012       }
05013    } else if (c->tech_pvt) {
05014       /* If this call no longer exists, but the channel still
05015        * references it we need to set the channel's tech_pvt to null
05016        * to avoid ast_channel_free() trying to free it.
05017        */
05018       c->tech_pvt = NULL;
05019    }
05020    ast_mutex_unlock(&iaxsl[callno]);
05021    ast_verb(3, "Hungup '%s'\n", c->name);
05022    return 0;
05023 }

static int iax2_indicate ( struct ast_channel *  c,
int  condition,
const void *  data,
size_t  datalen 
) [static]

Definition at line 5346 of file chan_iax2.c.

References AST_CONTROL_HOLD, AST_CONTROL_UNHOLD, ast_debug, AST_FRAME_CONTROL, ast_moh_start(), ast_moh_stop(), ast_mutex_lock(), ast_mutex_unlock(), chan_iax2_pvt::mohinterpret, PTR_TO_CALLNO, send_command(), ast_channel::tech_pvt, and wait_for_peercallno().

05347 {
05348    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
05349    struct chan_iax2_pvt *pvt;
05350    int res = 0;
05351 
05352    if (iaxdebug)
05353       ast_debug(1, "Indicating condition %d\n", condition);
05354 
05355    ast_mutex_lock(&iaxsl[callno]);
05356    pvt = iaxs[callno];
05357 
05358    if (wait_for_peercallno(pvt)) {
05359       res = -1;
05360       goto done;
05361    }
05362 
05363    switch (condition) {
05364    case AST_CONTROL_HOLD:
05365       if (strcasecmp(pvt->mohinterpret, "passthrough")) {
05366          ast_moh_start(c, data, pvt->mohinterpret);
05367          goto done;
05368       }
05369       break;
05370    case AST_CONTROL_UNHOLD:
05371       if (strcasecmp(pvt->mohinterpret, "passthrough")) {
05372          ast_moh_stop(c);
05373          goto done;
05374       }
05375    }
05376 
05377    res = send_command(pvt, AST_FRAME_CONTROL, condition, 0, data, datalen, -1);
05378 
05379 done:
05380    ast_mutex_unlock(&iaxsl[callno]);
05381 
05382    return res;
05383 }

static int iax2_key_rotate ( const void *  vpvt  )  [static]

Definition at line 5098 of file chan_iax2.c.

References AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), ast_random(), ast_sched_thread_add(), iax_ie_data::buf, build_ecx_key(), chan_iax2_pvt::callno, IAX_COMMAND_RTKEY, IAX_DEBUGDIGEST, iax_ie_append_raw(), IAX_IE_CHALLENGE, chan_iax2_pvt::keyrotateid, MD5Final(), MD5Init(), MD5Update(), iax_ie_data::pos, and send_command().

Referenced by iax2_send().

05099 {
05100    int res = 0;
05101    struct chan_iax2_pvt *pvt = (void *) vpvt;
05102    struct MD5Context md5;
05103    char key[17] = "";
05104    struct iax_ie_data ied = {
05105       .pos = 0,   
05106    };
05107    
05108    ast_mutex_lock(&iaxsl[pvt->callno]);
05109    pvt->keyrotateid = 
05110       ast_sched_thread_add(sched, 120000 + (ast_random() % 180001), iax2_key_rotate, vpvt);
05111 
05112    snprintf(key, sizeof(key), "%lX", ast_random());
05113 
05114    MD5Init(&md5);
05115    MD5Update(&md5, (unsigned char *) key, strlen(key));
05116    MD5Final((unsigned char *) key, &md5);
05117 
05118    IAX_DEBUGDIGEST("Sending", key);
05119 
05120    iax_ie_append_raw(&ied, IAX_IE_CHALLENGE, key, 16);
05121 
05122    res = send_command(pvt, AST_FRAME_IAX, IAX_COMMAND_RTKEY, 0, ied.buf, ied.pos, -1);
05123 
05124    build_ecx_key((unsigned char *) key, pvt);
05125 
05126    ast_mutex_unlock(&iaxsl[pvt->callno]);
05127 
05128    return res;
05129 }

static int iax2_matchmore ( struct ast_channel *  chan,
const char *  context,
const char *  exten,
int  priority,
const char *  callerid,
const char *  data 
) [static]

Part of the IAX2 Switch interface.

Definition at line 13271 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), CACHE_FLAG_MATCHMORE, find_cache(), iax2_dpcache::flags, LOG_NOTICE, and LOG_WARNING.

13272 {
13273    int res = 0;
13274    struct iax2_dpcache *dp = NULL;
13275 #if 0
13276    ast_log(LOG_NOTICE, "iax2_matchmore: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
13277 #endif
13278    if ((priority != 1) && (priority != 2))
13279       return 0;
13280 
13281    AST_LIST_LOCK(&dpcache);
13282    if ((dp = find_cache(chan, data, context, exten, priority))) {
13283       if (dp->flags & CACHE_FLAG_MATCHMORE)
13284          res = 1;
13285    } else {
13286       ast_log(LOG_WARNING, "Unable to make DP cache\n");
13287    }
13288    AST_LIST_UNLOCK(&dpcache);
13289 
13290    return res;
13291 }

static int iax2_poke_noanswer ( const void *  data  )  [static]

Definition at line 11485 of file chan_iax2.c.

References __iax2_poke_noanswer(), peer_unref(), iax2_peer::pokeexpire, and schedule_action.

Referenced by iax2_poke_peer().

11486 {
11487    struct iax2_peer *peer = (struct iax2_peer *)data;
11488    peer->pokeexpire = -1;
11489 #ifdef SCHED_MULTITHREADED
11490    if (schedule_action(__iax2_poke_noanswer, data))
11491 #endif      
11492       __iax2_poke_noanswer(data);
11493    peer_unref(peer);
11494    return 0;
11495 }

static int iax2_poke_peer ( struct iax2_peer *  peer,
int  heldcall 
) [static]

Definition at line 11506 of file chan_iax2.c.

References add_empty_calltoken_ie(), iax2_peer::addr, AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_thread_del, iax_ie_data::buf, iax2_peer::callno, DEFAULT_MAXMS, iax2_peer::dnsmgr, find_callno(), iax2_peer::historicms, iax2_destroy(), iax2_poke_noanswer(), iax2_sched_add(), IAX_COMMAND_POKE, iax2_peer::lastms, LOG_NOTICE, LOG_WARNING, iax2_peer::maxms, iax2_peer::name, NEW_FORCE, peer_ref(), peer_unref(), chan_iax2_pvt::peerpoke, chan_iax2_pvt::pingtime, iax2_peer::pokeexpire, iax2_peer::pokefreqnotok, iax_ie_data::pos, send_command(), and iax2_peer::sockfd.

Referenced by __iax2_poke_peer_s(), iax2_poke_peer_cb(), poke_all_peers(), reg_source_db(), and update_registry().

11507 {
11508    int callno;
11509    if (!peer->maxms || (!peer->addr.sin_addr.s_addr && !peer->dnsmgr)) {
11510       /* IF we have no IP without dnsmgr, or this isn't to be monitored, return
11511         immediately after clearing things out */
11512       peer->lastms = 0;
11513       peer->historicms = 0;
11514       peer->pokeexpire = -1;
11515       peer->callno = 0;
11516       return 0;
11517    }
11518 
11519    /* The peer could change the callno inside iax2_destroy, since we do deadlock avoidance */
11520    if ((callno = peer->callno) > 0) {
11521       ast_log(LOG_NOTICE, "Still have a callno...\n");
11522       ast_mutex_lock(&iaxsl[callno]);
11523       iax2_destroy(callno);
11524       ast_mutex_unlock(&iaxsl[callno]);
11525    }
11526    if (heldcall)
11527       ast_mutex_unlock(&iaxsl[heldcall]);
11528    callno = peer->callno = find_callno(0, 0, &peer->addr, NEW_FORCE, peer->sockfd, 0);
11529    if (heldcall)
11530       ast_mutex_lock(&iaxsl[heldcall]);
11531    if (peer->callno < 1) {
11532       ast_log(LOG_WARNING, "Unable to allocate call for poking peer '%s'\n", peer->name);
11533       return -1;
11534    }
11535 
11536    /* Speed up retransmission times for this qualify call */
11537    iaxs[peer->callno]->pingtime = peer->maxms / 4 + 1;
11538    iaxs[peer->callno]->peerpoke = peer;
11539 
11540    if (peer->pokeexpire > -1) {
11541       if (!ast_sched_thread_del(sched, peer->pokeexpire)) {
11542          peer->pokeexpire = -1;
11543          peer_unref(peer);
11544       }
11545    }
11546  
11547    /* Queue up a new task to handle no reply */
11548    /* If the host is already unreachable then use the unreachable interval instead */
11549    if (peer->lastms < 0)
11550       peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_noanswer, peer_ref(peer));
11551    else
11552       peer->pokeexpire = iax2_sched_add(sched, DEFAULT_MAXMS * 2, iax2_poke_noanswer, peer_ref(peer));
11553 
11554    if (peer->pokeexpire == -1)
11555       peer_unref(peer);
11556 
11557    /* And send the poke */
11558    ast_mutex_lock(&iaxsl[callno]);
11559    if (iaxs[callno]) {
11560       struct iax_ie_data ied = {
11561          .buf = { 0 },
11562          .pos = 0,
11563       };
11564       add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
11565       send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_POKE, 0, ied.buf, ied.pos, -1);
11566    }
11567    ast_mutex_unlock(&iaxsl[callno]);
11568 
11569    return 0;
11570 }

static int iax2_poke_peer_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 11497 of file chan_iax2.c.

References iax2_poke_peer().

Referenced by load_module().

11498 {
11499    struct iax2_peer *peer = obj;
11500 
11501    iax2_poke_peer(peer, 0);
11502 
11503    return 0;
11504 }

static int iax2_poke_peer_s ( const void *  data  )  [static]

Definition at line 8735 of file chan_iax2.c.

References __iax2_poke_peer_s(), iax2_peer::pokeexpire, and schedule_action.

Referenced by __iax2_poke_noanswer(), and socket_process().

08736 {
08737    struct iax2_peer *peer = (struct iax2_peer *)data;
08738    peer->pokeexpire = -1;
08739 #ifdef SCHED_MULTITHREADED
08740    if (schedule_action(__iax2_poke_peer_s, data))
08741 #endif      
08742       __iax2_poke_peer_s(data);
08743    return 0;
08744 }

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 3201 of file chan_iax2.c.

References ast_module_unref(), ast_set_flag, ast_test_flag, iax2_destroy_helper(), iax2_queue_hangup(), IAX_ALREADYGONE, chan_iax2_pvt::owner, and ast_channel::tech_pvt.

Referenced by iax2_hangup(), and send_command_final().

03202 {
03203    struct ast_channel *c = NULL;
03204    struct chan_iax2_pvt *pvt = iaxs[callno];
03205 
03206    if (!pvt)
03207       return -1;
03208 
03209    if (!ast_test_flag(pvt, IAX_ALREADYGONE)) {
03210       iax2_destroy_helper(pvt);
03211       ast_set_flag(pvt, IAX_ALREADYGONE); 
03212    }
03213 
03214    if ((c = pvt->owner)) {
03215       c->tech_pvt = NULL;
03216       iax2_queue_hangup(callno);
03217       pvt->owner = NULL;
03218       ast_module_unref(ast_module_info->self);
03219    }
03220 
03221    return 0;
03222 }

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 11147 of file chan_iax2.c.

References iax2_thread::actions, ast_atomic_fetchadd_int(), ast_cond_timedwait(), ast_cond_wait(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), ast_samp2tv(), ast_tvadd(), ast_tvnow(), iax2_thread::checktime, iax2_thread::cond, iax2_thread::curfunc, handle_deferred_full_frames(), iax2_process_thread_cleanup(), IAX_IOSTATE_IDLE, IAX_IOSTATE_PROCESSING, IAX_IOSTATE_READY, IAX_IOSTATE_SCHEDREADY, IAX_THREAD_TYPE_DYNAMIC, iaxactivethreadcount, iaxdynamicthreadcount, iax2_thread::init_cond, iax2_thread::init_lock, insert_idle_thread(), iax2_thread::iostate, iax2_thread::lock, iax2_thread::scheddata, iax2_thread::schedfunc, signal_condition(), socket_process(), iax2_thread::stop, thread, and iax2_thread::type.

Referenced by find_idle_thread(), and start_network_thread().

11148 {
11149    struct iax2_thread *thread = data;
11150    struct timeval wait;
11151    struct timespec ts;
11152    int put_into_idle = 0;
11153    int first_time = 1;
11154    int old_state;
11155 
11156    ast_atomic_fetchadd_int(&iaxactivethreadcount, 1);
11157 
11158    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
11159    pthread_cleanup_push(iax2_process_thread_cleanup, data);
11160 
11161    for (;;) {
11162       /* Wait for something to signal us to be awake */
11163       ast_mutex_lock(&thread->lock);
11164 
11165       if (thread->stop) {
11166          ast_mutex_unlock(&thread->lock);
11167          break;
11168       }
11169 
11170       /* Flag that we're ready to accept signals */
11171       if (first_time) {
11172          signal_condition(&thread->init_lock, &thread->init_cond);
11173          first_time = 0;
11174       }
11175 
11176       /* Put into idle list if applicable */
11177       if (put_into_idle) {
11178          insert_idle_thread(thread);
11179       }
11180 
11181       if (thread->type == IAX_THREAD_TYPE_DYNAMIC) {
11182          struct iax2_thread *t = NULL;
11183          /* Wait to be signalled or time out */
11184          wait = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
11185          ts.tv_sec = wait.tv_sec;
11186          ts.tv_nsec = wait.tv_usec * 1000;
11187          if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT) {
11188             /* This thread was never put back into the available dynamic
11189              * thread list, so just go away. */
11190             if (!put_into_idle || thread->stop) {
11191                ast_mutex_unlock(&thread->lock);
11192                break;
11193             }
11194             AST_LIST_LOCK(&dynamic_list);
11195             /* Account for the case where this thread is acquired *right* after a timeout */
11196             if ((t = AST_LIST_REMOVE(&dynamic_list, thread, list)))
11197                ast_atomic_fetchadd_int(&iaxdynamicthreadcount, -1);
11198             AST_LIST_UNLOCK(&dynamic_list);
11199             if (t) {
11200                /* This dynamic thread timed out waiting for a task and was
11201                 * not acquired immediately after the timeout, 
11202                 * so it's time to go away. */
11203                ast_mutex_unlock(&thread->lock);
11204                break;
11205             }
11206             /* Someone grabbed our thread *right* after we timed out.
11207              * Wait for them to set us up with something to do and signal
11208              * us to continue. */
11209             wait = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
11210             ts.tv_sec = wait.tv_sec;
11211             ts.tv_nsec = wait.tv_usec * 1000;
11212             if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT) {
11213                ast_mutex_unlock(&thread->lock);
11214                break;
11215             }
11216          }
11217       } else {
11218          ast_cond_wait(&thread->cond, &thread->lock);
11219       }
11220 
11221       /* Go back into our respective list */
11222       put_into_idle = 1;
11223 
11224       ast_mutex_unlock(&thread->lock);
11225 
11226       if (thread->stop) {
11227          break;
11228       }
11229 
11230       if (thread->iostate == IAX_IOSTATE_IDLE)
11231          continue;
11232 
11233       /* See what we need to do */
11234       switch (thread->iostate) {
11235       case IAX_IOSTATE_READY:
11236          thread->actions++;
11237          thread->iostate = IAX_IOSTATE_PROCESSING;
11238          socket_process(thread);
11239          handle_deferred_full_frames(thread);
11240          break;
11241       case IAX_IOSTATE_SCHEDREADY:
11242          thread->actions++;
11243          thread->iostate = IAX_IOSTATE_PROCESSING;
11244 #ifdef SCHED_MULTITHREADED
11245          thread->schedfunc(thread->scheddata);
11246 #endif      
11247       default:
11248          break;
11249       }
11250       time(&thread->checktime);
11251       thread->iostate = IAX_IOSTATE_IDLE;
11252 #ifdef DEBUG_SCHED_MULTITHREAD
11253       thread->curfunc[0]='\0';
11254 #endif      
11255 
11256       /* The network thread added us to the active_thread list when we were given
11257        * frames to process, Now that we are done, we must remove ourselves from
11258        * the active list, and return to the idle list */
11259       AST_LIST_LOCK(&active_list);
11260       AST_LIST_REMOVE(&active_list, thread, list);
11261       AST_LIST_UNLOCK(&active_list);
11262 
11263       /* Make sure another frame didn't sneak in there after we thought we were done. */
11264       handle_deferred_full_frames(thread);
11265    }
11266 
11267    /*!\note For some reason, idle threads are exiting without being removed
11268     * from an idle list, which is causing memory corruption.  Forcibly remove
11269     * it from the list, if it's there.
11270     */
11271    AST_LIST_LOCK(&idle_list);
11272    AST_LIST_REMOVE(&idle_list, thread, list);
11273    AST_LIST_UNLOCK(&idle_list);
11274 
11275    AST_LIST_LOCK(&dynamic_list);
11276    AST_LIST_REMOVE(&dynamic_list, thread, list);
11277    AST_LIST_UNLOCK(&dynamic_list);
11278 
11279    /* I am exiting here on my own volition, I need to clean up my own data structures
11280    * Assume that I am no longer in any of the lists (idle, active, or dynamic)
11281    */
11282    pthread_cleanup_pop(1);
11283    return NULL;
11284 }

static void iax2_process_thread_cleanup ( void *  data  )  [static]

static int iax2_provision ( struct sockaddr_in *  end,
int  sockfd,
char *  dest,
const char *  template,
int  force 
) [static]

Definition at line 11342 of file chan_iax2.c.

References ast_debug, AST_FRAME_IAX, ast_mutex_unlock(), ast_set_flag, auto_hangup(), chan_iax2_pvt::autoid, iax_ie_data::buf, create_addr(), find_callno_locked(), iax2_sched_replace(), IAX_COMMAND_PROVISION, iax_ie_append_raw(), IAX_IE_PROVISIONING, IAX_PROVISION, iax_provision_build(), NEW_FORCE, iax_ie_data::pos, send_command(), and create_addr_info::sockfd.

Referenced by check_provisioning(), handle_cli_iax2_provision(), and iax2_prov_app().

11343 {
11344    /* Returns 1 if provisioned, -1 if not able to find destination, or 0 if no provisioning
11345       is found for template */
11346    struct iax_ie_data provdata;
11347    struct iax_ie_data ied;
11348    unsigned int sig;
11349    struct sockaddr_in sin;
11350    int callno;
11351    struct create_addr_info cai;
11352 
11353    memset(&cai, 0, sizeof(cai));
11354 
11355    ast_debug(1, "Provisioning '%s' from template '%s'\n", dest, template);
11356 
11357    if (iax_provision_build(&provdata, &sig, template, force)) {
11358       ast_debug(1, "No provisioning found for template '%s'\n", template);
11359       return 0;
11360    }
11361 
11362    if (end) {
11363       memcpy(&sin, end, sizeof(sin));
11364       cai.sockfd = sockfd;
11365    } else if (create_addr(dest, NULL, &sin, &cai))
11366       return -1;
11367 
11368    /* Build the rest of the message */
11369    memset(&ied, 0, sizeof(ied));
11370    iax_ie_append_raw(&ied, IAX_IE_PROVISIONING, provdata.buf, provdata.pos);
11371 
11372    callno = find_callno_locked(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
11373    if (!callno)
11374       return -1;
11375 
11376    if (iaxs[callno]) {
11377       /* Schedule autodestruct in case they don't ever give us anything back */
11378       iaxs[callno]->autoid = iax2_sched_replace(iaxs[callno]->autoid, 
11379          sched, 15000, auto_hangup, (void *)(long)callno);
11380       ast_set_flag(iaxs[callno], IAX_PROVISION);
11381       /* Got a call number now, so go ahead and send the provisioning information */
11382       send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PROVISION, 0, ied.buf, ied.pos, -1);
11383    }
11384    ast_mutex_unlock(&iaxsl[callno]);
11385 
11386    return 1;
11387 }

static int iax2_queue_control_data ( int  callno,
enum ast_control_frame_type  control,
const void *  data,
size_t  datalen 
) [static]

Queue a control frame on the ast_channel owner.

This function queues a control 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 2826 of file chan_iax2.c.

References ast_channel_trylock, ast_channel_unlock, ast_queue_control_data(), and DEADLOCK_AVOIDANCE.

Referenced by socket_process().

02828 {
02829    for (;;) {
02830       if (iaxs[callno] && iaxs[callno]->owner) {
02831          if (ast_channel_trylock(iaxs[callno]->owner)) {
02832             /* Avoid deadlock by pausing and trying again */
02833             DEADLOCK_AVOIDANCE(&iaxsl[callno]);
02834          } else {
02835             ast_queue_control_data(iaxs[callno]->owner, control, data, datalen);
02836             ast_channel_unlock(iaxs[callno]->owner);
02837             break;
02838          }
02839       } else
02840          break;
02841    }
02842    return 0;
02843 }

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 2764 of file chan_iax2.c.

References ast_channel_trylock, ast_channel_unlock, ast_queue_frame(), and DEADLOCK_AVOIDANCE.

Referenced by __attempt_transmit(), __auto_congest(), __do_deliver(), __get_from_jb(), and socket_process().

02765 {
02766    for (;;) {
02767       if (iaxs[callno] && iaxs[callno]->owner) {
02768          if (ast_channel_trylock(iaxs[callno]->owner)) {
02769             /* Avoid deadlock by pausing and trying again */
02770             DEADLOCK_AVOIDANCE(&iaxsl[callno]);
02771          } else {
02772             ast_queue_frame(iaxs[callno]->owner, f);
02773             ast_channel_unlock(iaxs[callno]->owner);
02774             break;
02775          }
02776       } else
02777          break;
02778    }
02779    return 0;
02780 }

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 2795 of file chan_iax2.c.

References ast_channel_trylock, ast_channel_unlock, ast_queue_hangup(), and DEADLOCK_AVOIDANCE.

Referenced by iax2_predestroy().

02796 {
02797    for (;;) {
02798       if (iaxs[callno] && iaxs[callno]->owner) {
02799          if (ast_channel_trylock(iaxs[callno]->owner)) {
02800             /* Avoid deadlock by pausing and trying again */
02801             DEADLOCK_AVOIDANCE(&iaxsl[callno]);
02802          } else {
02803             ast_queue_hangup(iaxs[callno]->owner);
02804             ast_channel_unlock(iaxs[callno]->owner);
02805             break;
02806          }
02807       } else
02808          break;
02809    }
02810    return 0;
02811 }

static struct ast_frame * iax2_read ( struct ast_channel *  c  )  [static, read]

Definition at line 5092 of file chan_iax2.c.

References ast_log(), ast_null_frame, and LOG_NOTICE.

05093 {
05094    ast_log(LOG_NOTICE, "I should never be called!\n");
05095    return &ast_null_frame;
05096 }

static int iax2_register ( const char *  value,
int  lineno 
) [static]

Definition at line 8190 of file chan_iax2.c.

References ast_copy_string(), ast_log(), copy(), hostname, iax2_append_register(), LOG_WARNING, secret, and strsep().

Referenced by set_config().

08191 {
08192    char copy[256];
08193    char *username, *hostname, *secret;
08194    char *porta;
08195    char *stringp=NULL;
08196    
08197    if (!value)
08198       return -1;
08199 
08200    ast_copy_string(copy, value, sizeof(copy));
08201    stringp = copy;
08202    username = strsep(&stringp, "@");
08203    hostname = strsep(&stringp, "@");
08204 
08205    if (!hostname) {
08206       ast_log(LOG_WARNING, "Format for registration is user[:secret]@host[:port] at line %d\n", lineno);
08207       return -1;
08208    }
08209 
08210    stringp = username;
08211    username = strsep(&stringp, ":");
08212    secret = strsep(&stringp, ":");
08213    stringp = hostname;
08214    hostname = strsep(&stringp, ":");
08215    porta = strsep(&stringp, ":");
08216    
08217    if (porta && !atoi(porta)) {
08218       ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
08219       return -1;
08220    }
08221 
08222    return iax2_append_register(hostname, username, secret, porta);
08223 }

static struct ast_channel * iax2_request ( const char *  type,
int  format,
void *  data,
int *  cause 
) [static, read]

Definition at line 11582 of file chan_iax2.c.

References ast_best_codec(), AST_CAUSE_CONGESTION, AST_CAUSE_UNREGISTERED, ast_copy_flags, ast_getformatname(), ast_hangup(), ast_iax2_new(), ast_log(), ast_mutex_unlock(), AST_STATE_DOWN, ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_translator_best_choice(), create_addr_info::capability, create_addr(), find_callno_locked(), create_addr_info::found, IAX_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_SENDANI, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, LOG_WARNING, make_trunk(), create_addr_info::maxtime, chan_iax2_pvt::maxtime, ast_channel::name, ast_channel::nativeformats, NEW_FORCE, parse_dial_string(), parsed_dial_string::peer, parsed_dial_string::port, ast_channel::readformat, create_addr_info::sockfd, and ast_channel::writeformat.

11583 {
11584    int callno;
11585    int res;
11586    int fmt, native;
11587    struct sockaddr_in sin;
11588    struct ast_channel *c;
11589    struct parsed_dial_string pds;
11590    struct create_addr_info cai;
11591    char *tmpstr;
11592 
11593    memset(&pds, 0, sizeof(pds));
11594    tmpstr = ast_strdupa(data);
11595    parse_dial_string(tmpstr, &pds);
11596 
11597    if (ast_strlen_zero(pds.peer)) {
11598       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", (char *) data);
11599       return NULL;
11600    }
11601           
11602    memset(&cai, 0, sizeof(cai));
11603    cai.capability = iax2_capability;
11604 
11605    ast_copy_flags(&cai, &globalflags, IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);
11606    
11607    /* Populate our address from the given */
11608    if (create_addr(pds.peer, NULL, &sin, &cai)) {
11609       *cause = AST_CAUSE_UNREGISTERED;
11610       return NULL;
11611    }
11612 
11613    if (pds.port)
11614       sin.sin_port = htons(atoi(pds.port));
11615 
11616    callno = find_callno_locked(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
11617    if (callno < 1) {
11618       ast_log(LOG_WARNING, "Unable to create call\n");
11619       *cause = AST_CAUSE_CONGESTION;
11620       return NULL;
11621    }
11622 
11623    /* If this is a trunk, update it now */
11624    ast_copy_flags(iaxs[callno], &cai, IAX_TRUNK | IAX_SENDANI | IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);
11625    if (ast_test_flag(&cai, IAX_TRUNK)) {
11626       int new_callno;
11627       if ((new_callno = make_trunk(callno, 1)) != -1)
11628          callno = new_callno;
11629    }
11630    iaxs[callno]->maxtime = cai.maxtime;
11631    if (cai.found)
11632       ast_string_field_set(iaxs[callno], host, pds.peer);
11633 
11634    c = ast_iax2_new(callno, AST_STATE_DOWN, cai.capability);
11635 
11636    ast_mutex_unlock(&iaxsl[callno]);
11637 
11638    if (c) {
11639       /* Choose a format we can live with */
11640       if (c->nativeformats & format) 
11641          c->nativeformats &= format;
11642       else {
11643          native = c->nativeformats;
11644          fmt = format;
11645          res = ast_translator_best_choice(&fmt, &native);
11646          if (res < 0) {
11647             ast_log(LOG_WARNING, "Unable to create translator path for %s to %s on %s\n",
11648                ast_getformatname(c->nativeformats), ast_getformatname(fmt), c->name);
11649             ast_hangup(c);
11650             return NULL;
11651          }
11652          c->nativeformats = native;
11653       }
11654       c->readformat = ast_best_codec(c->nativeformats);
11655       c->writeformat = c->readformat;
11656    }
11657 
11658    return c;
11659 }

static int iax2_sched_add ( struct ast_sched_thread *  st,
int  when,
ast_sched_cb  callback,
const void *  data 
) [static]

static int iax2_sched_replace ( int  id,
struct ast_sched_thread *  st,
int  when,
ast_sched_cb  callback,
const void *  data 
) [static]

Definition at line 1357 of file chan_iax2.c.

References ast_sched_thread_add(), and ast_sched_thread_del.

Referenced by auth_fail(), iax2_ack_registry(), iax2_do_register(), iax2_dprequest(), iax2_provision(), and update_jbsched().

01359 {
01360    ast_sched_thread_del(st, id);
01361 
01362    return ast_sched_thread_add(st, when, callback, data);
01363 }

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]

Definition at line 6055 of file chan_iax2.c.

References chan_iax2_pvt::addr, iax_frame::af, chan_iax2_pvt::aseqno, AST_FRAME_IAX, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_log(), ast_test_flag, calc_timestamp(), ast_iax2_mini_hdr::callno, ast_iax2_video_hdr::callno, chan_iax2_pvt::callno, iax_frame::callno, compress_subclass(), ast_iax2_full_hdr::csub, iax_frame::data, ast_frame::data, iax_frame::datalen, ast_frame::datalen, ast_iax2_full_hdr::dcallno, iax_frame::dcallno, DIRECTION_OUTGRESS, iax_frame::ecx, chan_iax2_pvt::ecx, chan_iax2_pvt::encmethods, iax_frame::encmethods, encrypt_frame(), iax_frame::final, chan_iax2_pvt::first_iax_message, ast_frame::frametype, iax2_key_rotate(), iax2_transmit(), iax2_trunk_queue(), IAX_CALLENCRYPTED, IAX_COMMAND_ACK, IAX_ENCRYPTED, IAX_FLAG_FULL, iax_frame_new(), iax_frame_wrap(), IAX_KEYPOPULATED, iax_outputframe(), IAX_TRUNK, ast_iax2_full_hdr::iseqno, chan_iax2_pvt::iseqno, iax_frame::iseqno, chan_iax2_pvt::keyrotateid, chan_iax2_pvt::last_iax_message, chan_iax2_pvt::lastsent, iax2_trunk_peer::lastsent, chan_iax2_pvt::lastvsent, LOG_NOTICE, LOG_WARNING, MARK_IAX_SUBCLASS_TX, MAX_RETRY_TIME, MIN_RETRY_TIME, chan_iax2_pvt::mydcx, iax_frame::mydcx, ast_iax2_full_hdr::oseqno, chan_iax2_pvt::oseqno, iax_frame::oseqno, chan_iax2_pvt::peercallno, chan_iax2_pvt::pingtime, ast_frame::ptr, iax_frame::retries, iax_frame::retrytime, ast_iax2_full_hdr::scallno, iax_frame::semirand, chan_iax2_pvt::semirand, send_packet(), ast_frame::subclass, chan_iax2_pvt::svideoformat, chan_iax2_pvt::svoiceformat, chan_iax2_pvt::transfer, iax_frame::transfer, TRANSFER_MEDIAPASS, chan_iax2_pvt::transfercallno, chan_iax2_pvt::transferring, ast_iax2_mini_hdr::ts, ast_iax2_video_hdr::ts, ast_iax2_full_hdr::ts, iax_frame::ts, ast_iax2_full_hdr::type, and ast_iax2_video_hdr::zeros.

Referenced by __send_command(), iax2_write(), send_signaling(), and socket_process().

06056 {
06057    /* Queue a packet for delivery on a given private structure.  Use "ts" for
06058       timestamp, or calculate if ts is 0.  Send immediately without retransmission
06059       or delayed, with retransmission */
06060    struct ast_iax2_full_hdr *fh;
06061    struct ast_iax2_mini_hdr *mh;
06062    struct ast_iax2_video_hdr *vh;
06063    struct {
06064       struct iax_frame fr2;
06065       unsigned char buffer[4096];
06066    } frb;
06067    struct iax_frame *fr;
06068    int res;
06069    int sendmini=0;
06070    unsigned int lastsent;
06071    unsigned int fts;
06072 
06073    frb.fr2.afdatalen = sizeof(frb.buffer);
06074 
06075    if (!pvt) {
06076       ast_log(LOG_WARNING, "No private structure for packet?\n");
06077       return -1;
06078    }
06079    
06080    lastsent = pvt->lastsent;
06081 
06082    /* Calculate actual timestamp */
06083    fts = calc_timestamp(pvt, ts, f);
06084 
06085    /* Bail here if this is an "interp" frame; we don't want or need to send these placeholders out
06086     * (the endpoint should detect the lost packet itself).  But, we want to do this here, so that we
06087     * increment the "predicted timestamps" for voice, if we're predicting */
06088    if(f->frametype == AST_FRAME_VOICE && f->datalen == 0)
06089       return 0;
06090 #if 0
06091    ast_log(LOG_NOTICE, 
06092       "f->frametype %c= AST_FRAME_VOICE, %sencrypted, %srotation scheduled...\n",
06093       *("=!" + (f->frametype == AST_FRAME_VOICE)),
06094       IAX_CALLENCRYPTED(pvt) ? "" : "not ",
06095       pvt->keyrotateid != -1 ? "" : "no "
06096    );
06097 #endif
06098    if (pvt->keyrotateid == -1 && f->frametype == AST_FRAME_VOICE && IAX_CALLENCRYPTED(pvt)) {
06099       iax2_key_rotate(pvt);
06100    }
06101 
06102    if ((ast_test_flag(pvt, IAX_TRUNK) || 
06103          (((fts & 0xFFFF0000L) == (lastsent & 0xFFFF0000L)) ||
06104          ((fts & 0xFFFF0000L) == ((lastsent + 0x10000) & 0xFFFF0000L))))
06105       /* High two bytes are the same on timestamp, or sending on a trunk */ &&
06106        (f->frametype == AST_FRAME_VOICE) 
06107       /* is a voice frame */ &&
06108       (f->subclass == pvt->svoiceformat) 
06109       /* is the same type */ ) {
06110          /* Force immediate rather than delayed transmission */
06111          now = 1;
06112          /* Mark that mini-style frame is appropriate */
06113          sendmini = 1;
06114    }
06115    if ( f->frametype == AST_FRAME_VIDEO ) {
06116       /*
06117        * If the lower 15 bits of the timestamp roll over, or if
06118        * the video format changed then send a full frame.
06119        * Otherwise send a mini video frame
06120        */
06121       if (((fts & 0xFFFF8000L) == (pvt->lastvsent & 0xFFFF8000L)) &&
06122           ((f->subclass & ~0x1) == pvt->svideoformat)
06123          ) {
06124          now = 1;
06125          sendmini = 1;
06126       } else {
06127          now = 0;
06128          sendmini = 0;
06129       }
06130       pvt->lastvsent = fts;
06131    }
06132    if (f->frametype == AST_FRAME_IAX) {
06133       /* 0x8000 marks this message as TX:, this bit will be stripped later */
06134       pvt->last_iax_message = f->subclass | MARK_IAX_SUBCLASS_TX;
06135       if (!pvt->first_iax_message) {
06136          pvt->first_iax_message = pvt->last_iax_message;
06137       }
06138    }
06139    /* Allocate an iax_frame */
06140    if (now) {
06141       fr = &frb.fr2;
06142    } else
06143       fr = iax_frame_new(DIRECTION_OUTGRESS, ast_test_flag(pvt, IAX_ENCRYPTED) ? f->datalen + 32 : f->datalen, (f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_VIDEO));
06144    if (!fr) {
06145       ast_log(LOG_WARNING, "Out of memory\n");
06146       return -1;
06147    }
06148    /* Copy our prospective frame into our immediate or retransmitted wrapper */
06149    iax_frame_wrap(fr, f);
06150 
06151    fr->ts = fts;
06152    fr->callno = pvt->callno;
06153    fr->transfer = transfer;
06154    fr->final = final;
06155    fr->encmethods = 0;
06156    if (!sendmini) {
06157       /* We need a full frame */
06158       if (seqno > -1)
06159          fr->oseqno = seqno;
06160       else
06161          fr->oseqno = pvt->oseqno++;
06162       fr->iseqno = pvt->iseqno;
06163       fh = (struct ast_iax2_full_hdr *)(fr->af.data.ptr - sizeof(struct ast_iax2_full_hdr));
06164       fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
06165       fh->ts = htonl(fr->ts);
06166       fh->oseqno = fr->oseqno;
06167       if (transfer) {
06168          fh->iseqno = 0;
06169       } else
06170          fh->iseqno = fr->iseqno;
06171       /* Keep track of the last thing we've acknowledged */
06172       if (!transfer)
06173          pvt->aseqno = fr->iseqno;
06174       fh->type = fr->af.frametype & 0xFF;
06175       if (fr->af.frametype == AST_FRAME_VIDEO)
06176          fh->csub = compress_subclass(fr->af.subclass & ~0x1) | ((fr->af.subclass & 0x1) << 6);
06177       else
06178          fh->csub = compress_subclass(fr->af.subclass);
06179       if (transfer) {
06180          fr->dcallno = pvt->transfercallno;
06181       } else
06182          fr->dcallno = pvt->peercallno;
06183       fh->dcallno = htons(fr->dcallno);
06184       fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_full_hdr);
06185       fr->data = fh;
06186       fr->retries = 0;
06187       /* Retry after 2x the ping time has passed */
06188       fr->retrytime = pvt->pingtime * 2;
06189       if (fr->retrytime < MIN_RETRY_TIME)
06190          fr->retrytime = MIN_RETRY_TIME;
06191       if (fr->retrytime > MAX_RETRY_TIME)
06192          fr->retrytime = MAX_RETRY_TIME;
06193       /* Acks' don't get retried */
06194       if ((f->frametype == AST_FRAME_IAX) && (f->subclass == IAX_COMMAND_ACK))
06195          fr->retries = -1;
06196       else if (f->frametype == AST_FRAME_VOICE)
06197          pvt->svoiceformat = f->subclass;
06198       else if (f->frametype == AST_FRAME_VIDEO)
06199          pvt->svideoformat = f->subclass & ~0x1;
06200       if (ast_test_flag(pvt, IAX_ENCRYPTED)) {
06201          if (ast_test_flag(pvt, IAX_KEYPOPULATED)) {
06202             if (fr->transfer)
06203                iax_outputframe(fr, NULL, 2, &pvt->transfer, fr->datalen - sizeof(struct ast_iax2_full_hdr));
06204             else
06205                iax_outputframe(fr, NULL, 2, &pvt->addr, fr->datalen - sizeof(struct ast_iax2_full_hdr));
06206             encrypt_frame(&pvt->ecx, fh, pvt->semirand, &fr->datalen);
06207             fr->encmethods = pvt->encmethods;
06208             fr->ecx = pvt->ecx;
06209             fr->mydcx = pvt->mydcx;
06210             memcpy(fr->semirand, pvt->semirand, sizeof(fr->semirand));
06211          } else
06212             ast_log(LOG_WARNING, "Supposed to send packet encrypted, but no key?\n");
06213       }
06214 
06215       if (now) {
06216          res = send_packet(fr);
06217       } else
06218          res = iax2_transmit(fr);
06219    } else {
06220       if (ast_test_flag(pvt, IAX_TRUNK)) {
06221          iax2_trunk_queue(pvt, fr);
06222          res = 0;
06223       } else if (fr->af.frametype == AST_FRAME_VIDEO) {
06224          /* Video frame have no sequence number */
06225          fr->oseqno = -1;
06226          fr->iseqno = -1;
06227          vh = (struct ast_iax2_video_hdr *)(fr->af.data.ptr - sizeof(struct ast_iax2_video_hdr));
06228          vh->zeros = 0;
06229          vh->callno = htons(0x8000 | fr->callno);
06230          vh->ts = htons((fr->ts & 0x7FFF) | (fr->af.subclass & 0x1 ? 0x8000 : 0));
06231          fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_video_hdr);
06232          fr->data = vh;
06233          fr->retries = -1;
06234          res = send_packet(fr);        
06235       } else {
06236          /* Mini-frames have no sequence number */
06237          fr->oseqno = -1;
06238          fr->iseqno = -1;
06239          /* Mini frame will do */
06240          mh = (struct ast_iax2_mini_hdr *)(fr->af.data.ptr - sizeof(struct ast_iax2_mini_hdr));
06241          mh->callno = htons(fr->callno);
06242          mh->ts = htons(fr->ts & 0xFFFF);
06243          fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
06244          fr->data = mh;
06245          fr->retries = -1;
06246          if (pvt->transferring == TRANSFER_MEDIAPASS)
06247             fr->transfer = 1;
06248          if (ast_test_flag(pvt, IAX_ENCRYPTED)) {
06249             if (ast_test_flag(pvt, IAX_KEYPOPULATED)) {
06250                encrypt_frame(&pvt->ecx, (struct ast_iax2_full_hdr *)mh, pvt->semirand, &fr->datalen);
06251             } else
06252                ast_log(LOG_WARNING, "Supposed to send packet encrypted, but no key?\n");
06253          }
06254          res = send_packet(fr);
06255       }
06256    }
06257    return res;
06258 }

static int iax2_sendhtml ( struct ast_channel *  c,
int  subclass,
const char *  data,
int  datalen 
) [static]

Definition at line 4104 of file chan_iax2.c.

References AST_FRAME_HTML, PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

04105 {
04106    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_HTML, subclass, 0, (unsigned char *)data, datalen, -1);
04107 }

static int iax2_sendimage ( struct ast_channel *  c,
struct ast_frame *  img 
) [static]

static int iax2_sendtext ( struct ast_channel *  c,
const char *  text 
) [static]

Definition at line 4092 of file chan_iax2.c.

References AST_FRAME_TEXT, PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

04093 {
04094    
04095    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_TEXT,
04096       0, 0, (unsigned char *)text, strlen(text) + 1, -1);
04097 }

static int iax2_setoption ( struct ast_channel *  c,
int  option,
void *  data,
int  datalen 
) [static]

Definition at line 5047 of file chan_iax2.c.

References AST_CONTROL_OPTION, AST_FRAME_CONTROL, ast_free, ast_malloc, ast_mutex_lock(), ast_mutex_unlock(), AST_OPTION_FLAG_REQUEST, AST_OPTION_OPRMODE, AST_OPTION_RXGAIN, AST_OPTION_TXGAIN, ast_option_header::data, errno, PTR_TO_CALLNO, send_command_locked(), ast_channel::tech_pvt, and wait_for_peercallno().

05048 {
05049    struct ast_option_header *h;
05050    int res;
05051 
05052    switch (option) {
05053    case AST_OPTION_TXGAIN:
05054    case AST_OPTION_RXGAIN:
05055       /* these two cannot be sent, because they require a result */
05056       errno = ENOSYS;
05057       return -1;
05058    case AST_OPTION_OPRMODE:
05059       errno = EINVAL;
05060       return -1;
05061    default:
05062    {
05063       unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
05064       struct chan_iax2_pvt *pvt;
05065 
05066       ast_mutex_lock(&iaxsl[callno]);
05067       pvt = iaxs[callno];
05068 
05069       if (wait_for_peercallno(pvt)) {
05070          ast_mutex_unlock(&iaxsl[callno]);
05071          return -1;
05072       }
05073 
05074       ast_mutex_unlock(&iaxsl[callno]);
05075 
05076       if (!(h = ast_malloc(datalen + sizeof(*h)))) {
05077          return -1;
05078       }
05079 
05080       h->flag = AST_OPTION_FLAG_REQUEST;
05081       h->option = htons(option);
05082       memcpy(h->data, data, datalen);
05083       res = send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_CONTROL,
05084                  AST_CONTROL_OPTION, 0, (unsigned char *) h,
05085                  datalen + sizeof(*h), -1);
05086       ast_free(h);
05087       return res;
05088    }
05089    }
05090 }

static int iax2_start_transfer ( unsigned short  callno0,
unsigned short  callno1,
int  mediaonly 
) [static]

Definition at line 5131 of file chan_iax2.c.

References iax2_trunk_peer::addr, ast_debug, AST_FRAME_IAX, ast_random(), ast_set_flag, iax_ie_data::buf, IAX_CALLENCRYPTED, IAX_COMMAND_TXREQ, IAX_IE_APPARENT_ADDR, iax_ie_append_addr(), iax_ie_append_int(), iax_ie_append_short(), IAX_IE_CALLNO, IAX_IE_TRANSFERID, IAX_NOTRANSFER, iax_ie_data::pos, send_command(), TRANSFER_BEGIN, TRANSFER_MBEGIN, and chan_iax2_pvt::transferring.

Referenced by iax2_bridge().

05132 {
05133    int res;
05134    struct iax_ie_data ied0;
05135    struct iax_ie_data ied1;
05136    unsigned int transferid = (unsigned int)ast_random();
05137 
05138    if (IAX_CALLENCRYPTED(iaxs[callno0]) || IAX_CALLENCRYPTED(iaxs[callno1])) {
05139       ast_debug(1, "transfers are not supported for encrypted calls at this time");
05140       ast_set_flag(iaxs[callno0], IAX_NOTRANSFER);
05141       ast_set_flag(iaxs[callno1], IAX_NOTRANSFER);
05142       return 0;
05143    }
05144 
05145    memset(&ied0, 0, sizeof(ied0));
05146    iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr);
05147    iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno);
05148    iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, transferid);
05149 
05150    memset(&ied1, 0, sizeof(ied1));
05151    iax_ie_append_addr(&ied1, IAX_IE_APPARENT_ADDR, &iaxs[callno0]->addr);
05152    iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[callno0]->peercallno);
05153    iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
05154    
05155    res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
05156    if (res)
05157       return -1;
05158    res = send_command(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos, -1);
05159    if (res)
05160       return -1;
05161    iaxs[callno0]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
05162    iaxs[callno1]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
05163    return 0;
05164 }

static int iax2_transfer ( struct ast_channel *  c,
const char *  dest 
) [static]

Definition at line 5385 of file chan_iax2.c.

References ast_copy_string(), ast_debug, AST_FRAME_IAX, iax_ie_data::buf, chan_iax2_pvt::callno, context, IAX_COMMAND_TRANSFER, iax_ie_append_str(), IAX_IE_CALLED_CONTEXT, IAX_IE_CALLED_NUMBER, ast_channel::name, iax_ie_data::pos, PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

05386 {
05387    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
05388    struct iax_ie_data ied = { "", };
05389    char tmp[256], *context;
05390    ast_copy_string(tmp, dest, sizeof(tmp));
05391    context = strchr(tmp, '@');
05392    if (context) {
05393       *context = '\0';
05394       context++;
05395    }
05396    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, tmp);
05397    if (context)
05398       iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, context);
05399    ast_debug(1, "Transferring '%s' to '%s'\n", c->name, dest);
05400    return send_command_locked(callno, AST_FRAME_IAX, IAX_COMMAND_TRANSFER, 0, ied.buf, ied.pos, -1);
05401 }

static int iax2_transmit ( struct iax_frame *  fr  )  [static]

Definition at line 4064 of file chan_iax2.c.

References AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, AST_PTHREADT_NULL, ast_sched_thread_poke(), and iax_frame::sentyet.

Referenced by iax2_send().

04065 {
04066    /* Lock the queue and place this packet at the end */
04067    /* By setting this to 0, the network thread will send it for us, and
04068       queue retransmission if necessary */
04069    fr->sentyet = 0;
04070    AST_LIST_LOCK(&frame_queue);
04071    AST_LIST_INSERT_TAIL(&frame_queue, fr, list);
04072    AST_LIST_UNLOCK(&frame_queue);
04073    /* Wake up the network and scheduler thread */
04074    if (netthreadid != AST_PTHREADT_NULL)
04075       pthread_kill(netthreadid, SIGURG);
04076    ast_sched_thread_poke(sched);
04077    return 0;
04078 }

static int iax2_trunk_expired ( struct iax2_trunk_peer *  tpeer,
struct timeval *  now 
) [inline, static]

Definition at line 8789 of file chan_iax2.c.

References iax2_trunk_peer::trunkact.

Referenced by timing_read().

08790 {
08791    /* Drop when trunk is about 5 seconds idle */
08792    if (now->tv_sec > tpeer->trunkact.tv_sec + 5) 
08793       return 1;
08794    return 0;
08795 }

static int iax2_trunk_queue ( struct chan_iax2_pvt *  pvt,
struct iax_frame *  fr 
) [static]

Definition at line 5794 of file chan_iax2.c.

References iax2_trunk_peer::addr, chan_iax2_pvt::addr, iax_frame::af, ast_debug, ast_inet_ntoa(), ast_log(), ast_mutex_unlock(), ast_realloc, ast_test_flag, ast_tvnow(), ast_iax2_meta_trunk_entry::callno, chan_iax2_pvt::callno, ast_iax2_mini_hdr::callno, iax2_trunk_peer::calls, ast_frame::data, ast_frame::datalen, DEFAULT_TRUNKDATA, f, find_tpeer(), IAX2_TRUNK_PREFACE, IAX_TRUNKTIMESTAMPS, ast_iax2_meta_trunk_entry::len, ast_iax2_meta_trunk_mini::len, iax2_trunk_peer::lock, LOG_WARNING, ast_iax2_meta_trunk_mini::mini, ast_frame::ptr, send_trunk(), chan_iax2_pvt::sockfd, iax2_trunk_peer::trunkdata, iax2_trunk_peer::trunkdataalloc, iax2_trunk_peer::trunkdatalen, iax_frame::ts, and ast_iax2_mini_hdr::ts.

Referenced by iax2_send().

05795 {
05796    struct ast_frame *f;
05797    struct iax2_trunk_peer *tpeer;
05798    void *tmp, *ptr;
05799    struct timeval now;
05800    int res; 
05801    struct ast_iax2_meta_trunk_entry *met;
05802    struct ast_iax2_meta_trunk_mini *mtm;
05803 
05804    f = &fr->af;
05805    tpeer = find_tpeer(&pvt->addr, pvt->sockfd);
05806    if (tpeer) {
05807       if (tpeer->trunkdatalen + f->datalen + 4 >= tpeer->trunkdataalloc) {
05808          /* Need to reallocate space */
05809          if (tpeer->trunkdataalloc < trunkmaxsize) {
05810             if (!(tmp = ast_realloc(tpeer->trunkdata, tpeer->trunkdataalloc + DEFAULT_TRUNKDATA + IAX2_TRUNK_PREFACE))) {
05811                ast_mutex_unlock(&tpeer->lock);
05812                return -1;
05813             }
05814             
05815             tpeer->trunkdataalloc += DEFAULT_TRUNKDATA;
05816             tpeer->trunkdata = tmp;
05817             ast_debug(1, "Expanded trunk '%s:%d' to %d bytes\n", ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), tpeer->trunkdataalloc);
05818          } else {
05819             ast_log(LOG_WARNING, "Maximum trunk data space exceeded to %s:%d\n", ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port));
05820             ast_mutex_unlock(&tpeer->lock);
05821             return -1;
05822          }
05823       }
05824 
05825       /* Append to meta frame */
05826       ptr = tpeer->trunkdata + IAX2_TRUNK_PREFACE + tpeer->trunkdatalen;
05827       if (ast_test_flag(&globalflags, IAX_TRUNKTIMESTAMPS)) {
05828          mtm = (struct ast_iax2_meta_trunk_mini *)ptr;
05829          mtm->len = htons(f->datalen);
05830          mtm->mini.callno = htons(pvt->callno);
05831          mtm->mini.ts = htons(0xffff & fr->ts);
05832          ptr += sizeof(struct ast_iax2_meta_trunk_mini);
05833          tpeer->trunkdatalen += sizeof(struct ast_iax2_meta_trunk_mini);
05834       } else {
05835          met = (struct ast_iax2_meta_trunk_entry *)ptr;
05836          /* Store call number and length in meta header */
05837          met->callno = htons(pvt->callno);
05838          met->len = htons(f->datalen);
05839          /* Advance pointers/decrease length past trunk entry header */
05840          ptr += sizeof(struct ast_iax2_meta_trunk_entry);
05841          tpeer->trunkdatalen += sizeof(struct ast_iax2_meta_trunk_entry);
05842       }
05843       /* Copy actual trunk data */
05844       memcpy(ptr, f->data.ptr, f->datalen);
05845       tpeer->trunkdatalen += f->datalen;
05846 
05847       tpeer->calls++;
05848 
05849       /* track the largest mtu we actually have sent */
05850       if (tpeer->trunkdatalen + f->datalen + 4 > trunk_maxmtu) 
05851          trunk_maxmtu = tpeer->trunkdatalen + f->datalen + 4 ; 
05852 
05853       /* if we have enough for a full MTU, ship it now without waiting */
05854       if (global_max_trunk_mtu > 0 && tpeer->trunkdatalen + f->datalen + 4 >= global_max_trunk_mtu) {
05855          now = ast_tvnow();
05856          res = send_trunk(tpeer, &now); 
05857          trunk_untimed ++; 
05858       }
05859 
05860       ast_mutex_unlock(&tpeer->lock);
05861    }
05862    return 0;
05863 }

static int iax2_vnak ( int  callno  )  [static]

Definition at line 8707 of file chan_iax2.c.

References AST_FRAME_IAX, IAX_COMMAND_VNAK, and send_command_immediate().

Referenced by socket_process(), and socket_process_meta().

08708 {
08709    return send_command_immediate(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_VNAK, 0, NULL, 0, iaxs[callno]->iseqno);
08710 }

static int iax2_write ( struct ast_channel *  c,
struct ast_frame *  f 
) [static]

Definition at line 7109 of file chan_iax2.c.

References ast_debug, AST_FRAME_NULL, AST_FRAME_VOICE, ast_mutex_lock(), ast_mutex_unlock(), ast_test_flag, iax2_peer::callno, errno, ast_frame::frametype, iax2_send(), IAX_ALREADYGONE, IAX_QUELCH, IAX_STATE_STARTED, PTR_TO_CALLNO, and ast_channel::tech_pvt.

07110 {
07111    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
07112    int res = -1;
07113    ast_mutex_lock(&iaxsl[callno]);
07114    if (iaxs[callno]) {
07115    /* If there's an outstanding error, return failure now */
07116       if (!iaxs[callno]->error) {
07117          if (ast_test_flag(iaxs[callno], IAX_ALREADYGONE))
07118             res = 0;
07119             /* Don't waste bandwidth sending null frames */
07120          else if (f->frametype == AST_FRAME_NULL)
07121             res = 0;
07122          else if ((f->frametype == AST_FRAME_VOICE) && ast_test_flag(iaxs[callno], IAX_QUELCH))
07123             res = 0;
07124          else if (!ast_test_flag(&iaxs[callno]->state, IAX_STATE_STARTED))
07125             res = 0;
07126          else
07127          /* Simple, just queue for transmission */
07128             res = iax2_send(iaxs[callno], f, 0, -1, 0, 0, 0);
07129       } else {
07130          ast_debug(1, "Write error: %s\n", strerror(errno));
07131       }
07132    }
07133    /* If it's already gone, just return */
07134    ast_mutex_unlock(&iaxsl[callno]);
07135    return res;
07136 }

static int iax_check_version ( char *  dev  )  [static]

Definition at line 2993 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), ast_iax2_firmware_header::devname, iax_firmware::fwh, and ast_iax2_firmware_header::version.

Referenced by update_registry().

02994 {
02995    int res = 0;
02996    struct iax_firmware *cur = NULL;
02997 
02998    if (ast_strlen_zero(dev))
02999       return 0;
03000 
03001    AST_LIST_LOCK(&firmwares);
03002    AST_LIST_TRAVERSE(&firmwares, cur, list) {
03003       if (!strcmp(dev, (char *)cur->fwh->devname)) {
03004          res = ntohs(cur->fwh->version);
03005          break;
03006       }
03007    }
03008    AST_LIST_UNLOCK(&firmwares);
03009 
03010    return res;
03011 }

static void iax_debug_output ( const char *  data  )  [static]

Definition at line 1063 of file chan_iax2.c.

References ast_verbose.

Referenced by load_module().

01064 {
01065    if (iaxdebug)
01066       ast_verbose("%s", data);
01067 }

static void iax_error_output ( const char *  data  )  [static]

Definition at line 1069 of file chan_iax2.c.

References ast_log(), and LOG_WARNING.

Referenced by load_module().

01070 {
01071    ast_log(LOG_WARNING, "%s", data);
01072 }

static int iax_firmware_append ( struct iax_ie_data *  ied,
const unsigned char *  dev,
unsigned int  desc 
) [static]

Definition at line 3013 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), ast_iax2_firmware_header::data, ast_iax2_firmware_header::datalen, ast_iax2_firmware_header::devname, iax_firmware::fwh, iax_ie_append(), iax_ie_append_int(), iax_ie_append_raw(), IAX_IE_FWBLOCKDATA, and IAX_IE_FWBLOCKDESC.

Referenced by socket_process().

03014 {
03015    int res = -1;
03016    unsigned int bs = desc & 0xff;
03017    unsigned int start = (desc >> 8) & 0xffffff;
03018    unsigned int bytes;
03019    struct iax_firmware *cur;
03020 
03021    if (ast_strlen_zero((char *)dev) || !bs)
03022       return -1;
03023 
03024    start *= bs;
03025    
03026    AST_LIST_LOCK(&firmwares);
03027    AST_LIST_TRAVERSE(&firmwares, cur, list) {
03028       if (strcmp((char *)dev, (char *)cur->fwh->devname))
03029          continue;
03030       iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
03031       if (start < ntohl(cur->fwh->datalen)) {
03032          bytes = ntohl(cur->fwh->datalen) - start;
03033          if (bytes > bs)
03034             bytes = bs;
03035          iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
03036       } else {
03037          bytes = 0;
03038          iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
03039       }
03040       if (bytes == bs)
03041          res = 0;
03042       else
03043          res = 1;
03044       break;
03045    }
03046    AST_LIST_UNLOCK(&firmwares);
03047 
03048    return res;
03049 }

static void iax_outputframe ( struct iax_frame *  f,
struct ast_iax2_full_hdr *  fhi,
int  rx,
struct sockaddr_in *  sin,
int  datalen 
) [static]

Definition at line 1046 of file chan_iax2.c.

References debugaddr, and iax_showframe().

Referenced by iax2_send(), raw_hangup(), and socket_process().

01047 {
01048    if (iaxdebug ||
01049        (sin && debugaddr.sin_addr.s_addr && 
01050         (!ntohs(debugaddr.sin_port) ||
01051          debugaddr.sin_port == sin->sin_port) &&
01052         debugaddr.sin_addr.s_addr == sin->sin_addr.s_addr)) {
01053       if (iaxdebug) {
01054          iax_showframe(f, fhi, rx, sin, datalen);
01055       } else {
01056          iaxdebug = 1;
01057          iax_showframe(f, fhi, rx, sin, datalen);
01058          iaxdebug = 0;
01059       }
01060    }
01061 }

static int iax_park ( struct ast_channel *  chan1,
struct ast_channel *  chan2 
) [static]

Definition at line 8951 of file chan_iax2.c.

References ast_channel::accountcode, ast_channel::amaflags, ast_calloc, ast_channel_alloc, ast_channel_masquerade(), ast_copy_string(), ast_do_masquerade(), ast_free, ast_hangup(), ast_log(), ast_pthread_create_detached_background, AST_STATE_DOWN, iax_dual::chan1, iax_dual::chan2, ast_channel::context, ast_channel::exten, iax_park_thread(), LOG_WARNING, ast_channel::name, ast_channel::priority, ast_channel::readformat, and ast_channel::writeformat.

Referenced by socket_process().

08952 {
08953    struct iax_dual *d;
08954    struct ast_channel *chan1m, *chan2m;
08955    pthread_t th;
08956    chan1m = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
08957    chan2m = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "IAXPeer/%s",chan2->name);
08958    if (chan2m && chan1m) {
08959       /* Make formats okay */
08960       chan1m->readformat = chan1->readformat;
08961       chan1m->writeformat = chan1->writeformat;
08962       ast_channel_masquerade(chan1m, chan1);
08963       /* Setup the extensions and such */
08964       ast_copy_string(chan1m->context, chan1->context, sizeof(chan1m->context));
08965       ast_copy_string(chan1m->exten, chan1->exten, sizeof(chan1m->exten));
08966       chan1m->priority = chan1->priority;
08967       
08968       /* We make a clone of the peer channel too, so we can play
08969          back the announcement */
08970       /* Make formats okay */
08971       chan2m->readformat = chan2->readformat;
08972       chan2m->writeformat = chan2->writeformat;
08973       ast_channel_masquerade(chan2m, chan2);
08974       /* Setup the extensions and such */
08975       ast_copy_string(chan2m->context, chan2->context, sizeof(chan2m->context));
08976       ast_copy_string(chan2m->exten, chan2->exten, sizeof(chan2m->exten));
08977       chan2m->priority = chan2->priority;
08978       if (ast_do_masquerade(chan2m)) {
08979          ast_log(LOG_WARNING, "Masquerade failed :(\n");
08980          ast_hangup(chan2m);
08981          return -1;
08982       }
08983    } else {
08984       if (chan1m)
08985          ast_hangup(chan1m);
08986       if (chan2m)
08987          ast_hangup(chan2m);
08988       return -1;
08989    }
08990    if ((d = ast_calloc(1, sizeof(*d)))) {
08991       d->chan1 = chan1m;
08992       d->chan2 = chan2m;
08993       if (!ast_pthread_create_detached_background(&th, NULL, iax_park_thread, d)) {
08994          return 0;
08995       }
08996       ast_free(d);
08997    }
08998    return -1;
08999 }

static void* iax_park_thread ( void *  stuff  )  [static]

Definition at line 8931 of file chan_iax2.c.

References ast_free, ast_frfree, ast_hangup(), ast_log(), ast_park_call(), ast_read(), iax_dual::chan1, iax_dual::chan2, ext, f, and LOG_NOTICE.

Referenced by iax_park().

08932 {
08933    struct ast_channel *chan1, *chan2;
08934    struct iax_dual *d;
08935    struct ast_frame *f;
08936    int ext;
08937    int res;
08938    d = stuff;
08939    chan1 = d->chan1;
08940    chan2 = d->chan2;
08941    ast_free(d);
08942    f = ast_read(chan1);
08943    if (f)
08944       ast_frfree(f);
08945    res = ast_park_call(chan1, chan2, 0, &ext);
08946    ast_hangup(chan2);
08947    ast_log(LOG_NOTICE, "Parked on extension '%d'\n", ext);
08948    return NULL;
08949 }

static struct iax_frame* iaxfrdup2 ( struct iax_frame *  fr  )  [static, read]

Definition at line 1806 of file chan_iax2.c.

References iax_frame::af, iax_frame::afdatalen, iax_frame::cacheable, ast_frame::datalen, DIRECTION_INGRESS, iax_frame_new(), and iax_frame_wrap().

Referenced by socket_process(), and socket_process_meta().

01807 {
01808    struct iax_frame *new = iax_frame_new(DIRECTION_INGRESS, fr->af.datalen, fr->cacheable);
01809    if (new) {
01810       size_t afdatalen = new->afdatalen;
01811       memcpy(new, fr, sizeof(*new));
01812       iax_frame_wrap(new, &fr->af);
01813       new->afdatalen = afdatalen;
01814       new->data = NULL;
01815       new->datalen = 0;
01816       new->direction = DIRECTION_INGRESS;
01817       new->retrans = -1;
01818    }
01819    return new;
01820 }

static void insert_idle_thread ( struct iax2_thread *  thread  )  [static]

Definition at line 1250 of file chan_iax2.c.

References AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, IAX_THREAD_TYPE_DYNAMIC, and iax2_thread::type.

Referenced by iax2_process_thread().

01251 {
01252    if (thread->type == IAX_THREAD_TYPE_DYNAMIC) {
01253       AST_LIST_LOCK(&dynamic_list);
01254       AST_LIST_INSERT_TAIL(&dynamic_list, thread, list);
01255       AST_LIST_UNLOCK(&dynamic_list);
01256    } else {
01257       AST_LIST_LOCK(&idle_list);
01258       AST_LIST_INSERT_TAIL(&idle_list, thread, list);
01259       AST_LIST_UNLOCK(&idle_list);
01260    }
01261 
01262    return;
01263 }

static void jb_debug_output ( const char *  fmt,
  ... 
) [static]

Definition at line 1098 of file chan_iax2.c.

References ast_verbose.

Referenced by handle_cli_iax2_set_debug_jb().

01099 {
01100    va_list args;
01101    char buf[1024];
01102 
01103    va_start(args, fmt);
01104    vsnprintf(buf, sizeof(buf), fmt, args);
01105    va_end(args);
01106 
01107    ast_verbose("%s", buf);
01108 }

static void jb_error_output ( const char *  fmt,
  ... 
) [static]

Definition at line 1074 of file chan_iax2.c.

References ast_log(), and LOG_ERROR.

Referenced by handle_cli_iax2_set_debug_jb(), and load_module().

01075 {
01076    va_list args;
01077    char buf[1024];
01078 
01079    va_start(args, fmt);
01080    vsnprintf(buf, sizeof(buf), fmt, args);
01081    va_end(args);
01082 
01083    ast_log(LOG_ERROR, "%s", buf);
01084 }

static void jb_warning_output ( const char *  fmt,
  ... 
) [static]

Definition at line 1086 of file chan_iax2.c.

References ast_log(), and LOG_WARNING.

Referenced by handle_cli_iax2_set_debug_jb(), and load_module().

01087 {
01088    va_list args;
01089    char buf[1024];
01090 
01091    va_start(args, fmt);
01092    vsnprintf(buf, sizeof(buf), fmt, args);
01093    va_end(args);
01094 
01095    ast_log(LOG_WARNING, "%s", buf);
01096 }

static int load_module ( void   )  [static]

Load IAX2 module, load configuraiton ---.

Definition at line 13829 of file chan_iax2.c.

References __unload_module(), ao2_callback, ARRAY_LEN, ast_channel_register(), ast_cli_register_multiple(), ast_custom_function_register, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_manager_register, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_mutex_init(), ast_netsock_init(), ast_netsock_list_alloc(), ast_random(), ast_realtime_require_field(), ast_register_application_xml, ast_register_switch(), ast_sched_thread_create(), ast_sched_thread_destroy(), ast_timer_close(), ast_timer_open(), ast_timer_set_rate(), ast_verb, config, EVENT_FLAG_REPORTING, EVENT_FLAG_SYSTEM, iax2_do_register(), iax2_poke_peer_cb(), iax2_prov_app(), iax_debug_output(), iax_error_output(), iax_provision_reload(), iax_set_error(), iax_set_output(), io_context_create(), io_context_destroy(), jb_error_output(), jb_setoutput(), jb_warning_output(), load_objects(), LOG_ERROR, manager_iax2_show_netstats(), manager_iax2_show_peer_list(), manager_iax2_show_peers(), manager_iax2_show_registry(), peer_set_sock_cb(), reload_firmware(), RQ_CHAR, RQ_UINTEGER2, SENTINEL, set_config(), and start_network_thread().

13830 {
13831 
13832    static const char config[] = "iax.conf";
13833    int x = 0;
13834    struct iax2_registry *reg = NULL;
13835 
13836    if (load_objects()) {
13837       return AST_MODULE_LOAD_FAILURE;
13838    }
13839 
13840    randomcalltokendata = ast_random();
13841    ast_custom_function_register(&iaxpeer_function);
13842    ast_custom_function_register(&iaxvar_function);
13843 
13844    iax_set_output(iax_debug_output);
13845    iax_set_error(iax_error_output);
13846    jb_setoutput(jb_error_output, jb_warning_output, NULL);
13847 
13848    memset(iaxs, 0, sizeof(iaxs));
13849 
13850    for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
13851       ast_mutex_init(&iaxsl[x]);
13852    }
13853 
13854    if (!(sched = ast_sched_thread_create())) {
13855       ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
13856       return AST_MODULE_LOAD_FAILURE;
13857    }
13858 
13859    if (!(io = io_context_create())) {
13860       ast_log(LOG_ERROR, "Failed to create I/O context\n");
13861       sched = ast_sched_thread_destroy(sched);
13862       return AST_MODULE_LOAD_FAILURE;
13863    }
13864 
13865    if (!(netsock = ast_netsock_list_alloc())) {
13866       ast_log(LOG_ERROR, "Failed to create netsock list\n");
13867       io_context_destroy(io);
13868       sched = ast_sched_thread_destroy(sched);
13869       return AST_MODULE_LOAD_FAILURE;
13870    }
13871    ast_netsock_init(netsock);
13872    
13873    outsock = ast_netsock_list_alloc();
13874    if (!outsock) {
13875       ast_log(LOG_ERROR, "Could not allocate outsock list.\n");
13876       io_context_destroy(io);
13877       sched = ast_sched_thread_destroy(sched);
13878       return AST_MODULE_LOAD_FAILURE;
13879    }
13880    ast_netsock_init(outsock);
13881 
13882    ast_cli_register_multiple(cli_iax2, ARRAY_LEN(cli_iax2));
13883 
13884    ast_register_application_xml(papp, iax2_prov_app);
13885    
13886    ast_manager_register( "IAXpeers", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_peers, "List IAX Peers" );
13887    ast_manager_register( "IAXpeerlist", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_peer_list, "List IAX Peers" );
13888    ast_manager_register( "IAXnetstats", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_netstats, "Show IAX Netstats" );
13889    ast_manager_register( "IAXregistry", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_registry, "Show IAX registrations");
13890 
13891    if ((timer = ast_timer_open())) {
13892       ast_timer_set_rate(timer, trunkfreq);
13893    }
13894 
13895    if (set_config(config, 0) == -1) {
13896       if (timer) {
13897          ast_timer_close(timer);
13898       }
13899       return AST_MODULE_LOAD_DECLINE;
13900    }
13901 
13902    if (ast_channel_register(&iax2_tech)) {
13903       ast_log(LOG_ERROR, "Unable to register channel class %s\n", "IAX2");
13904       __unload_module();
13905       return AST_MODULE_LOAD_FAILURE;
13906    }
13907 
13908    if (ast_register_switch(&iax2_switch)) 
13909       ast_log(LOG_ERROR, "Unable to register IAX switch\n");
13910 
13911    if (start_network_thread()) {
13912       ast_log(LOG_ERROR, "Unable to start network thread\n");
13913       __unload_module();
13914       return AST_MODULE_LOAD_FAILURE;
13915    } else
13916       ast_verb(2, "IAX Ready and Listening\n");
13917 
13918    AST_LIST_LOCK(&registrations);
13919    AST_LIST_TRAVERSE(&registrations, reg, entry)
13920       iax2_do_register(reg);
13921    AST_LIST_UNLOCK(&registrations); 
13922    
13923    ao2_callback(peers, 0, peer_set_sock_cb, NULL);
13924    ao2_callback(peers, 0, iax2_poke_peer_cb, NULL);
13925 
13926 
13927    reload_firmware(0);
13928    iax_provision_reload(0);
13929 
13930    ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL);
13931 
13932    return AST_MODULE_LOAD_SUCCESS;
13933 }

static int load_objects ( void   )  [static]

Definition at line 13770 of file chan_iax2.c.

References addr_range_cmp_cb(), addr_range_hash_cb(), ao2_container_alloc, ao2_ref, AST_MODULE_LOAD_FAILURE, create_callno_pools(), IAX_MAX_CALLS, MAX_PEER_BUCKETS, MAX_USER_BUCKETS, peer_cmp_cb(), peer_hash_cb(), peercnt_cmp_cb(), peercnt_hash_cb(), pvt_cmp_cb(), pvt_hash_cb(), transfercallno_pvt_cmp_cb(), transfercallno_pvt_hash_cb(), user_cmp_cb(), and user_hash_cb().

Referenced by load_module().

13771 {
13772    peers = users = iax_peercallno_pvts = iax_transfercallno_pvts = NULL;
13773    peercnts = callno_limits = calltoken_ignores = callno_pool = callno_pool_trunk = NULL;
13774 
13775    if (!(peers = ao2_container_alloc(MAX_PEER_BUCKETS, peer_hash_cb, peer_cmp_cb))) {
13776       goto container_fail;
13777    } else if (!(users = ao2_container_alloc(MAX_USER_BUCKETS, user_hash_cb, user_cmp_cb))) {
13778       goto container_fail;
13779    } else if (!(iax_peercallno_pvts = ao2_container_alloc(IAX_MAX_CALLS, pvt_hash_cb, pvt_cmp_cb))) {
13780       goto container_fail;
13781    } else if (!(iax_transfercallno_pvts = ao2_container_alloc(IAX_MAX_CALLS, transfercallno_pvt_hash_cb, transfercallno_pvt_cmp_cb))) {
13782       goto container_fail;
13783    } else if (!(peercnts = ao2_container_alloc(MAX_PEER_BUCKETS, peercnt_hash_cb, peercnt_cmp_cb))) {
13784       goto container_fail;
13785    } else if (!(callno_limits = ao2_container_alloc(MAX_PEER_BUCKETS, addr_range_hash_cb, addr_range_cmp_cb))) {
13786       goto container_fail;
13787    } else if (!(calltoken_ignores = ao2_container_alloc(MAX_PEER_BUCKETS, addr_range_hash_cb, addr_range_cmp_cb))) {
13788       goto container_fail;
13789    } else if (create_callno_pools()) {
13790       goto container_fail;
13791    }
13792 
13793    return 0;
13794 
13795 container_fail:
13796    if (peers) {
13797       ao2_ref(peers, -1);
13798    }
13799    if (users) {
13800       ao2_ref(users, -1);
13801    }
13802    if (iax_peercallno_pvts) {
13803       ao2_ref(iax_peercallno_pvts, -1);
13804    }
13805    if (iax_transfercallno_pvts) {
13806       ao2_ref(iax_transfercallno_pvts, -1);
13807    }
13808    if (peercnts) {
13809       ao2_ref(peercnts, -1);
13810    }
13811    if (callno_limits) {
13812       ao2_ref(callno_limits, -1);
13813    }
13814    if (calltoken_ignores) {
13815       ao2_ref(calltoken_ignores, -1);
13816    }
13817    if (callno_pool) {
13818       ao2_ref(callno_pool, -1);
13819    }
13820    if (callno_pool_trunk) {
13821       ao2_ref(callno_pool_trunk, -1);
13822    }
13823    return AST_MODULE_LOAD_FAILURE;
13824 }

static void lock_both ( unsigned short  callno0,
unsigned short  callno1 
) [static]

Definition at line 5166 of file chan_iax2.c.

References ast_mutex_lock(), ast_mutex_trylock(), and DEADLOCK_AVOIDANCE.

Referenced by iax2_bridge().

05167 {
05168    ast_mutex_lock(&iaxsl[callno0]);
05169    while (ast_mutex_trylock(&iaxsl[callno1])) {
05170       DEADLOCK_AVOIDANCE(&iaxsl[callno0]);
05171    }
05172 }

static void log_jitterstats ( unsigned short  callno  )  [static]

Definition at line 9074 of file chan_iax2.c.

References ast_debug, ast_mutex_lock(), ast_mutex_unlock(), ast_test_flag, jb_info::current, iax_rr::delay, iax_rr::dropped, EVENT_FLAG_REPORTING, jb_info::frames_dropped, jb_info::frames_in, jb_info::frames_lost, jb_info::frames_ooo, IAX_USEJITTERBUF, ast_channel::jb, jb_getinfo(), iax_rr::jitter, jb_info::jitter, iax_rr::losscnt, iax_rr::losspct, jb_info::losspct, manager_event, jb_info::min, iax_rr::ooo, iax_rr::packets, chan_iax2_pvt::pingtime, and chan_iax2_pvt::remote_rr.

Referenced by socket_process().

09075 {
09076    int localjitter = -1, localdelay = 0, locallost = -1, locallosspct = -1, localdropped = 0, localooo = -1, localpackets = -1;
09077    jb_info jbinfo;
09078 
09079    ast_mutex_lock(&iaxsl[callno]);
09080    if (iaxs[callno] && iaxs[callno]->owner && iaxs[callno]->owner->name) {
09081       if(ast_test_flag(iaxs[callno], IAX_USEJITTERBUF)) {
09082          jb_getinfo(iaxs[callno]->jb, &jbinfo);
09083          localjitter = jbinfo.jitter;
09084          localdelay = jbinfo.current - jbinfo.min;
09085          locallost = jbinfo.frames_lost;
09086          locallosspct = jbinfo.losspct/1000;
09087          localdropped = jbinfo.frames_dropped;
09088          localooo = jbinfo.frames_ooo;
09089          localpackets = jbinfo.frames_in;
09090       }
09091       ast_debug(3, "JB STATS:%s ping=%d ljitterms=%d ljbdelayms=%d ltotlost=%d lrecentlosspct=%d ldropped=%d looo=%d lrecvd=%d rjitterms=%d rjbdelayms=%d rtotlost=%d rrecentlosspct=%d rdropped=%d rooo=%d rrecvd=%d\n",
09092          iaxs[callno]->owner->name,
09093          iaxs[callno]->pingtime,
09094          localjitter,
09095          localdelay,
09096          locallost,
09097          locallosspct,
09098          localdropped,
09099          localooo,
09100          localpackets,
09101          iaxs[callno]->remote_rr.jitter,
09102          iaxs[callno]->remote_rr.delay,
09103          iaxs[callno]->remote_rr.losscnt,
09104          iaxs[callno]->remote_rr.losspct/1000,
09105          iaxs[callno]->remote_rr.dropped,
09106          iaxs[callno]->remote_rr.ooo,
09107          iaxs[callno]->remote_rr.packets);
09108       manager_event(EVENT_FLAG_REPORTING, "JitterBufStats", "Owner: %s\r\nPing: %d\r\nLocalJitter: %d\r\nLocalJBDelay: %d\r\nLocalTotalLost: %d\r\nLocalLossPercent: %d\r\nLocalDropped: %d\r\nLocalooo: %d\r\nLocalReceived: %d\r\nRemoteJitter: %d\r\nRemoteJBDelay: %d\r\nRemoteTotalLost: %d\r\nRemoteLossPercent: %d\r\nRemoteDropped: %d\r\nRemoteooo: %d\r\nRemoteReceived: %d\r\n",
09109          iaxs[callno]->owner->name,
09110          iaxs[callno]->pingtime,
09111          localjitter,
09112          localdelay,
09113          locallost,
09114          locallosspct,
09115          localdropped,
09116          localooo,
09117          localpackets,
09118          iaxs[callno]->remote_rr.jitter,
09119          iaxs[callno]->remote_rr.delay,
09120          iaxs[callno]->remote_rr.losscnt,
09121          iaxs[callno]->remote_rr.losspct/1000,
09122          iaxs[callno]->remote_rr.dropped,
09123          iaxs[callno]->remote_rr.ooo,
09124          iaxs[callno]->remote_rr.packets);
09125    }
09126    ast_mutex_unlock(&iaxsl[callno]);
09127 }

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 1886 of file chan_iax2.c.

References ast_debug, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_thread_del, chan_iax2_pvt::callno, callno_entry::callno, chan_iax2_pvt::callno_entry, get_unused_callno(), iax2_sched_add(), chan_iax2_pvt::lagid, LOG_WARNING, MIN_REUSE_TIME, chan_iax2_pvt::pingid, replace_callno(), send_lagrq(), send_ping(), TRUNK_CALL_START, update_max_nontrunk(), update_max_trunk(), and callno_entry::validated.

Referenced by iax2_request(), and socket_process().

01887 {
01888    int x;
01889    int res= 0;
01890    struct callno_entry *callno_entry;
01891    if (iaxs[callno]->oseqno) {
01892       ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
01893       return -1;
01894    }
01895    if (callno & TRUNK_CALL_START) {
01896       ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
01897       return -1;
01898    }
01899 
01900    if (!(callno_entry = get_unused_callno(1, iaxs[callno]->callno_entry->validated))) {
01901       ast_log(LOG_WARNING, "Unable to trunk call: Insufficient space\n");
01902       return -1;
01903    }
01904 
01905    x = callno_entry->callno;
01906    ast_mutex_lock(&iaxsl[x]);
01907 
01908    /*!
01909     * \note We delete these before switching the slot, because if
01910     * they fire in the meantime, they will generate a warning.
01911     */
01912    ast_sched_thread_del(sched, iaxs[callno]->pingid);
01913    ast_sched_thread_del(sched, iaxs[callno]->lagid);
01914    iaxs[x] = iaxs[callno];
01915    iaxs[x]->callno = x;
01916 
01917    /* since we copied over the pvt from a different callno, make sure the old entry is replaced
01918     * before assigning the new one */
01919    if (iaxs[x]->callno_entry) {
01920       iax2_sched_add(sched, MIN_REUSE_TIME * 1000, replace_callno, iaxs[x]->callno_entry);
01921    }
01922    iaxs[x]->callno_entry = callno_entry;
01923 
01924    iaxs[callno] = NULL;
01925    /* Update the two timers that should have been started */
01926    iaxs[x]->pingid = iax2_sched_add(sched, 
01927       ping_time * 1000, send_ping, (void *)(long)x);
01928    iaxs[x]->lagid = iax2_sched_add(sched, 
01929       lagrq_time * 1000, send_lagrq, (void *)(long)x);
01930 
01931    if (locked)
01932       ast_mutex_unlock(&iaxsl[callno]);
01933    res = x;
01934    if (!locked)
01935       ast_mutex_unlock(&iaxsl[x]);
01936 
01937    ast_debug(1, "Made call %d into trunk call %d\n", callno, x);
01938    /* We move this call from a non-trunked to a trunked call */
01939    update_max_trunk();
01940    update_max_nontrunk();
01941    return res;
01942 }

static int manager_iax2_show_netstats ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 6626 of file chan_iax2.c.

References ast_cli_netstats(), astman_append(), and RESULT_SUCCESS.

Referenced by load_module().

06627 {
06628    ast_cli_netstats(s, -1, 0);
06629    astman_append(s, "\r\n");
06630    return RESULT_SUCCESS;
06631 }

static int manager_iax2_show_peer_list ( struct mansession *  s,
const struct message *  m 
) [static]

callback to display iax peers in manager format

Definition at line 6678 of file chan_iax2.c.

References iax2_peer::addr, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_copy_string(), ast_inet_ntoa(), ast_str_alloca, ast_str_buffer(), ast_strlen_zero(), ast_test_flag, astman_append(), astman_get_header(), iax2_peer::encmethods, encmethods_to_str(), IAX_DYNAMIC, IAX_TRUNK, iax2_peer::mask, iax2_peer::name, peer_status(), peer_unref(), RESULT_SUCCESS, status, and iax2_peer::username.

Referenced by load_module().

06679 {
06680    struct iax2_peer *peer = NULL;
06681    int peer_count = 0;
06682    char nm[20];
06683    char status[20];
06684    const char *id = astman_get_header(m,"ActionID");
06685    char idtext[256] = "";
06686    struct ast_str *encmethods = ast_str_alloca(256);
06687    struct ao2_iterator i;
06688 
06689    if (!ast_strlen_zero(id))
06690       snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
06691 
06692    astman_append(s, "Response: Success\r\n%sMessage: IAX Peer status list will follow\r\n\r\n", idtext);
06693 
06694 
06695    i = ao2_iterator_init(peers, 0);
06696    for (peer = ao2_iterator_next(&i); peer; peer_unref(peer), peer = ao2_iterator_next(&i)) {
06697       encmethods_to_str(peer->encmethods, encmethods);
06698       astman_append(s, "Event: PeerEntry\r\n%sChanneltype: IAX\r\n", idtext);
06699       if (!ast_strlen_zero(peer->username)) {
06700          astman_append(s, "ObjectName: %s\r\nObjectUsername: %s\r\n", peer->name, peer->username);
06701       } else {
06702          astman_append(s, "ObjectName: %s\r\n", peer->name);
06703       }
06704       astman_append(s, "ChanObjectType: peer\r\n");
06705       astman_append(s, "IPaddress: %s\r\n", peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "-none-");
06706       ast_copy_string(nm, ast_inet_ntoa(peer->mask), sizeof(nm));
06707       astman_append(s, "Mask: %s\r\n", nm);
06708       astman_append(s, "Port: %d\r\n", ntohs(peer->addr.sin_port));
06709       astman_append(s, "Dynamic: %s\r\n", ast_test_flag(peer, IAX_DYNAMIC) ? "Yes" : "No");
06710       astman_append(s, "Trunk: %s\r\n", ast_test_flag(peer, IAX_TRUNK) ? "Yes" : "No");
06711       astman_append(s, "Encryption: %s\r\n", peer->encmethods ? ast_str_buffer(encmethods) : "No");
06712       peer_status(peer, status, sizeof(status));
06713       astman_append(s, "Status: %s\r\n\r\n", status);
06714       peer_count++;
06715    }
06716    ao2_iterator_destroy(&i);
06717 
06718    astman_append(s, "Event: PeerlistComplete\r\n%sListItems: %d\r\n\r\n", idtext, peer_count);
06719    return RESULT_SUCCESS;
06720 }

static int manager_iax2_show_peers ( struct mansession *  s,
const struct message *  m 
) [static]

callback to display iax peers in manager

Definition at line 6665 of file chan_iax2.c.

References __iax2_show_peers(), ast_strlen_zero(), astman_get_header(), and astman_send_ack().

Referenced by load_module().

06666 {
06667    char *a[] = { "iax2", "show", "users" };
06668    const char *id = astman_get_header(m,"ActionID");
06669    char idtext[256] = "";
06670 
06671    if (!ast_strlen_zero(id))
06672       snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
06673    astman_send_ack(s, m, "Peer status list will follow");
06674    return __iax2_show_peers(1, -1, s, 3, a );
06675 } 

static int manager_iax2_show_registry ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 6786 of file chan_iax2.c.

References iax2_registry::addr, ast_copy_string(), ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_listack(), iax2_registry::dnsmgr, iax2_registry::refresh, iax2_registry::regstate, regstate2str(), total, iax2_registry::us, and iax2_registry::username.

Referenced by load_module().

06787 {
06788    const char *id = astman_get_header(m, "ActionID");
06789    struct iax2_registry *reg = NULL;
06790    char idtext[256] = "";
06791    char host[80] = "";
06792    char perceived[80] = "";
06793    int total = 0;
06794 
06795    if (!ast_strlen_zero(id))
06796       snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
06797 
06798    astman_send_listack(s, m, "Registrations will follow", "start");
06799 
06800    AST_LIST_LOCK(&registrations);
06801    AST_LIST_TRAVERSE(&registrations, reg, entry) {
06802       snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(reg->addr.sin_addr), ntohs(reg->addr.sin_port));
06803       
06804       if (reg->us.sin_addr.s_addr) {
06805          snprintf(perceived, sizeof(perceived), "%s:%d", ast_inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port));
06806       } else {
06807          ast_copy_string(perceived, "<Unregistered>", sizeof(perceived));
06808       }
06809       
06810       astman_append(s,
06811          "Event: RegistryEntry\r\n"
06812          "Host: %s\r\n"
06813          "DNSmanager: %s\r\n"
06814          "Username: %s\r\n"
06815          "Perceived: %s\r\n"
06816          "Refresh: %d\r\n"
06817          "State: %s\r\n"
06818          "\r\n", host, (reg->dnsmgr) ? "Y" : "N", reg->username, perceived, 
06819          reg->refresh, regstate2str(reg->regstate));
06820 
06821       total++;
06822    }
06823    AST_LIST_UNLOCK(&registrations);
06824 
06825    astman_append(s,
06826       "Event: RegistrationsComplete\r\n"
06827       "EventList: Complete\r\n"
06828       "ListItems: %d\r\n"
06829       "%s"
06830       "\r\n", total, idtext);
06831    
06832    return 0;
06833 }

static int match ( struct sockaddr_in *  sin,
unsigned short  callno,
unsigned short  dcallno,
const struct chan_iax2_pvt *  cur,
int  check_dcallno 
) [static]

Definition at line 1835 of file chan_iax2.c.

References chan_iax2_pvt::addr, chan_iax2_pvt::callno, chan_iax2_pvt::peercallno, chan_iax2_pvt::transfer, TRANSFER_MEDIAPASS, chan_iax2_pvt::transfercallno, and chan_iax2_pvt::transferring.

Referenced by __ao2_callback(), __find_callno(), ast_parse_device_state(), check_blacklist(), find_by_name(), find_command(), handle_updates(), lua_find_extension(), pbx_find_extension(), pvt_cmp_cb(), realtime_switch_common(), and transfercallno_pvt_cmp_cb().

01836 {
01837    if ((cur->addr.sin_addr.s_addr == sin->sin_addr.s_addr) &&
01838       (cur->addr.sin_port == sin->sin_port)) {
01839       /* This is the main host */
01840       if ( (cur->peercallno == 0 || cur->peercallno == callno) &&
01841           (check_dcallno ? dcallno == cur->callno : 1) ) {
01842          /* That's us.  Be sure we keep track of the peer call number */
01843          return 1;
01844       }
01845    }
01846    if ((cur->transfer.sin_addr.s_addr == sin->sin_addr.s_addr) &&
01847        (cur->transfer.sin_port == sin->sin_port) && (cur->transferring)) {
01848       /* We're transferring */
01849       if ((dcallno == cur->callno) || (cur->transferring == TRANSFER_MEDIAPASS && cur->transfercallno == callno))
01850          return 1;
01851    }
01852    return 0;
01853 }

static void memcpy_decrypt ( unsigned char *  dst,
const unsigned char *  src,
int  len,
ast_aes_decrypt_key *  dcx 
) [static]

Definition at line 5893 of file chan_iax2.c.

References ast_aes_decrypt, ast_log(), and LOG_WARNING.

Referenced by decode_frame().

05894 {
05895 #if 0
05896    /* Debug with "fake encryption" */
05897    int x;
05898    if (len % 16)
05899       ast_log(LOG_WARNING, "len should be multiple of 16, not %d!\n", len);
05900    for (x=0;x<len;x++)
05901       dst[x] = src[x] ^ 0xff;
05902 #else 
05903    unsigned char lastblock[16] = { 0 };
05904    int x;
05905    while(len > 0) {
05906       ast_aes_decrypt(src, dst, dcx);
05907       for (x=0;x<16;x++)
05908          dst[x] ^= lastblock[x];
05909       memcpy(lastblock, src, sizeof(lastblock));
05910       dst += 16;
05911       src += 16;
05912       len -= 16;
05913    }
05914 #endif
05915 }

static void memcpy_encrypt ( unsigned char *  dst,
const unsigned char *  src,
int  len,
ast_aes_encrypt_key *  ecx 
) [static]

Definition at line 5917 of file chan_iax2.c.

References ast_aes_encrypt, ast_log(), and LOG_WARNING.

Referenced by encrypt_frame().

05918 {
05919 #if 0
05920    /* Debug with "fake encryption" */
05921    int x;
05922    if (len % 16)
05923       ast_log(LOG_WARNING, "len should be multiple of 16, not %d!\n", len);
05924    for (x=0;x<len;x++)
05925       dst[x] = src[x] ^ 0xff;
05926 #else
05927    unsigned char curblock[16] = { 0 };
05928    int x;
05929    while(len > 0) {
05930       for (x=0;x<16;x++)
05931          curblock[x] ^= src[x];
05932       ast_aes_encrypt(curblock, dst, ecx);
05933       memcpy(curblock, dst, sizeof(curblock)); 
05934       dst += 16;
05935       src += 16;
05936       len -= 16;
05937    }
05938 #endif
05939 }

static void merge_encryption ( struct chan_iax2_pvt *  p,
unsigned int  enc 
) [static]

Definition at line 7451 of file chan_iax2.c.

References chan_iax2_pvt::encmethods, IAX_ENCRYPT_AES128, IAX_ENCRYPT_KEYROTATE, and chan_iax2_pvt::keyrotateid.

Referenced by authenticate_reply(), and socket_process().

07452 {
07453    /* Select exactly one common encryption if there are any */
07454    p->encmethods &= enc;
07455    if (p->encmethods) {
07456       if (!(p->encmethods & IAX_ENCRYPT_KEYROTATE)){ /* if key rotation is not supported, turn off keyrotation. */
07457          p->keyrotateid = -2;
07458       }
07459       if (p->encmethods & IAX_ENCRYPT_AES128)
07460          p->encmethods = IAX_ENCRYPT_AES128;
07461       else
07462          p->encmethods = 0;
07463    }
07464 }

static void mwi_event_cb ( const struct ast_event *  event,
void *  userdata 
) [static]

Definition at line 1184 of file chan_iax2.c.

Referenced by build_peer().

01185 {
01186    /* The MWI subscriptions exist just so the core knows we care about those
01187     * mailboxes.  However, we just grab the events out of the cache when it
01188     * is time to send MWI, since it is only sent with a REGACK. */
01189 }

static void* network_thread ( void *  ignore  )  [static]

Definition at line 11661 of file chan_iax2.c.

References ast_debug, ast_io_add(), AST_IO_IN, AST_IO_PRI, ast_io_wait(), AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_mutex_trylock(), ast_mutex_unlock(), ast_timer_fd(), attempt_transmit(), iax_frame::callno, f, iax2_sched_add(), iax_frame_free(), iax_frame::retrans, iax_frame::retries, iax_frame::retrytime, send_packet(), iax_frame::sentyet, and timing_read().

Referenced by start_network_thread().

11662 {
11663    /* Our job is simple: Send queued messages, retrying if necessary.  Read frames 
11664       from the network, and queue them for delivery to the channels */
11665    int res, count, wakeup;
11666    struct iax_frame *f;
11667 
11668    if (timer)
11669       ast_io_add(io, ast_timer_fd(timer), timing_read, AST_IO_IN | AST_IO_PRI, NULL);
11670    
11671    for(;;) {
11672       pthread_testcancel();
11673 
11674       /* Go through the queue, sending messages which have not yet been
11675          sent, and scheduling retransmissions if appropriate */
11676       AST_LIST_LOCK(&frame_queue);
11677       count = 0;
11678       wakeup = -1;
11679       AST_LIST_TRAVERSE_SAFE_BEGIN(&frame_queue, f, list) {
11680          if (f->sentyet)
11681             continue;
11682          
11683          /* Try to lock the pvt, if we can't... don't fret - defer it till later */
11684          if (ast_mutex_trylock(&iaxsl[f->callno])) {
11685             wakeup = 1;
11686             continue;
11687          }
11688 
11689          f->sentyet = 1;
11690 
11691          if (iaxs[f->callno]) {
11692             send_packet(f);
11693             count++;
11694          } 
11695 
11696          ast_mutex_unlock(&iaxsl[f->callno]);
11697 
11698          if (f->retries < 0) {
11699             /* This is not supposed to be retransmitted */
11700             AST_LIST_REMOVE_CURRENT(list);
11701             /* Free the iax frame */
11702             iax_frame_free(f);
11703          } else {
11704             /* We need reliable delivery.  Schedule a retransmission */
11705             f->retries++;
11706             f->retrans = iax2_sched_add(sched, f->retrytime, attempt_transmit, f);
11707          }
11708       }
11709       AST_LIST_TRAVERSE_SAFE_END;
11710       AST_LIST_UNLOCK(&frame_queue);
11711 
11712       pthread_testcancel();
11713       if (count >= 20)
11714          ast_debug(1, "chan_iax2: Sent %d queued outbound frames all at once\n", count);
11715 
11716       /* Now do the IO, and run scheduled tasks */
11717       res = ast_io_wait(io, wakeup);
11718       if (res >= 0) {
11719          if (res >= 20)
11720             ast_debug(1, "chan_iax2: ast_io_wait ran %d I/Os all at once\n", res);
11721       }
11722    }
11723    return NULL;
11724 }

static struct chan_iax2_pvt* new_iax ( struct sockaddr_in *  sin,
const char *  host 
) [static, read]

Definition at line 1764 of file chan_iax2.c.

References ao2_alloc, ao2_ref, AST_LIST_HEAD_INIT_NOLOCK, ast_string_field_init, ast_string_field_set, chan_iax2_pvt::authid, chan_iax2_pvt::autoid, chan_iax2_pvt::dpentries, exten, chan_iax2_pvt::hold_signaling, chan_iax2_pvt::initid, chan_iax2_pvt::jb, jb_new(), jb_setconf(), chan_iax2_pvt::jbid, chan_iax2_pvt::keyrotateid, chan_iax2_pvt::lagid, jb_conf::max_contig_interp, jb_conf::max_jitterbuf, chan_iax2_pvt::pingid, prefs, chan_iax2_pvt::prefs, pvt_destructor(), jb_conf::resync_threshold, chan_iax2_pvt::signaling_queue, and jb_conf::target_extra.

Referenced by __find_callno().

01765 {
01766    struct chan_iax2_pvt *tmp;
01767    jb_conf jbconf;
01768 
01769    if (!(tmp = ao2_alloc(sizeof(*tmp), pvt_destructor))) {
01770       return NULL;
01771    }
01772 
01773    if (ast_string_field_init(tmp, 32)) {
01774       ao2_ref(tmp, -1);
01775       tmp = NULL;
01776       return NULL;
01777    }
01778       
01779    tmp->prefs = prefs;
01780    tmp->pingid = -1;
01781    tmp->lagid = -1;
01782    tmp->autoid = -1;
01783    tmp->authid = -1;
01784    tmp->initid = -1;
01785    tmp->keyrotateid = -1;
01786 
01787    ast_string_field_set(tmp,exten, "s");
01788    ast_string_field_set(tmp,host, host);
01789 
01790    tmp->jb = jb_new();
01791    tmp->jbid = -1;
01792    jbconf.max_jitterbuf = maxjitterbuffer;
01793    jbconf.resync_threshold = resyncthreshold;
01794    jbconf.max_contig_interp = maxjitterinterps;
01795    jbconf.target_extra = jittertargetextra;
01796    jb_setconf(tmp->jb,&jbconf);
01797 
01798    AST_LIST_HEAD_INIT_NOLOCK(&tmp->dpentries);
01799 
01800    tmp->hold_signaling = 1;
01801    AST_LIST_HEAD_INIT_NOLOCK(&tmp->signaling_queue);
01802 
01803    return tmp;
01804 }

static void parse_dial_string ( char *  data,
struct parsed_dial_string *  pds 
) [static]

Parses an IAX dial string into its component parts.

Parameters:
data the string to be parsed
pds pointer to a struct parsed_dial_string to be filled in
Returns:
nothing
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 4747 of file chan_iax2.c.

References ast_strip_quoted(), ast_strlen_zero(), parsed_dial_string::context, parsed_dial_string::exten, parsed_dial_string::key, parsed_dial_string::options, parsed_dial_string::password, parsed_dial_string::peer, parsed_dial_string::port, strsep(), and parsed_dial_string::username.

Referenced by cache_get_callno_locked(), iax2_call(), iax2_devicestate(), and iax2_request().

04748 {
04749    if (ast_strlen_zero(data))
04750       return;
04751 
04752    pds->peer = strsep(&data, "/");
04753    pds->exten = strsep(&data, "/");
04754    pds->options = data;
04755 
04756    if (pds->exten) {
04757       data = pds->exten;
04758       pds->exten = strsep(&data, "@");
04759       pds->context = data;
04760    }
04761 
04762    if (strchr(pds->peer, '@')) {
04763       data = pds->peer;
04764       pds->username = strsep(&data, "@");
04765       pds->peer = data;
04766    }
04767 
04768    if (pds->username) {
04769       data = pds->username;
04770       pds->username = strsep(&data, ":");
04771       pds->password = data;
04772    }
04773 
04774    data = pds->peer;
04775    pds->peer = strsep(&data, ":");
04776    pds->port = data;
04777 
04778    /* check for a key name wrapped in [] in the secret position, if found,
04779       move it to the key field instead
04780    */
04781    if (pds->password && (pds->password[0] == '[')) {
04782       pds->key = ast_strip_quoted(pds->password, "[", "]");
04783       pds->password = NULL;
04784    }
04785 }

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 1512 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, and iax2_peer::name.

Referenced by load_objects().

01513 {
01514    struct iax2_peer *peer = obj, *peer2 = arg;
01515 
01516    return !strcmp(peer->name, peer2->name) ? CMP_MATCH | CMP_STOP : 0;
01517 }

static int peer_delme_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 12417 of file chan_iax2.c.

References ast_set_flag, and IAX_DELME.

Referenced by delete_users().

12418 {
12419    struct iax2_peer *peer = obj;
12420 
12421    ast_set_flag(peer, IAX_DELME);
12422 
12423    return 0;
12424 }

static void peer_destructor ( void *  obj  )  [static]

Definition at line 11876 of file chan_iax2.c.

References ast_dnsmgr_release(), ast_event_unsubscribe(), ast_free_ha(), ast_mutex_lock(), ast_mutex_unlock(), ast_string_field_free_memory, iax2_peer::callno, iax2_peer::dnsmgr, iax2_peer::ha, iax2_destroy(), iax2_peer::mwi_event_sub, and register_peer_exten().

Referenced by build_peer().

11877 {
11878    struct iax2_peer *peer = obj;
11879    int callno = peer->callno;
11880 
11881    ast_free_ha(peer->ha);
11882 
11883    if (callno > 0) {
11884       ast_mutex_lock(&iaxsl[callno]);
11885       iax2_destroy(callno);
11886       ast_mutex_unlock(&iaxsl[callno]);
11887    }
11888 
11889    register_peer_exten(peer, 0);
11890 
11891    if (peer->dnsmgr)
11892       ast_dnsmgr_release(peer->dnsmgr);
11893 
11894    if (peer->mwi_event_sub)
11895       ast_event_unsubscribe(peer->mwi_event_sub);
11896 
11897    ast_string_field_free_memory(peer);
11898 }

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 1502 of file chan_iax2.c.

References ast_str_hash(), and iax2_peer::name.

Referenced by load_objects().

01503 {
01504    const struct iax2_peer *peer = obj;
01505 
01506    return ast_str_hash(peer->name);
01507 }

static struct iax2_peer* peer_ref ( struct iax2_peer *  peer  )  [static, read]

Definition at line 1559 of file chan_iax2.c.

References ao2_ref.

Referenced by __iax2_poke_noanswer(), handle_cli_iax2_prune_realtime(), handle_cli_iax2_unregister(), iax2_poke_peer(), realtime_peer(), reg_source_db(), socket_process(), and update_registry().

01560 {
01561    ao2_ref(peer, +1);
01562    return peer;
01563 }

static int peer_set_sock_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 13724 of file chan_iax2.c.

References iax2_peer::sockfd.

Referenced by load_module().

13725 {
13726    struct iax2_peer *peer = obj;
13727 
13728    if (peer->sockfd < 0)
13729       peer->sockfd = defaultsockfd;
13730 
13731    return 0;
13732 }

static int peer_set_srcaddr ( struct iax2_peer *  peer,
const char *  srcaddr 
) [static]

Parse the "sourceaddress" value, lookup in netsock list and set peer's sockfd. Defaults to defaultsockfd if not found.

Definition at line 11804 of file chan_iax2.c.

References iax2_trunk_peer::addr, ast_debug, ast_get_ip(), ast_log(), ast_netsock_bind(), ast_netsock_find(), ast_netsock_sockfd(), ast_netsock_unref(), ast_strdupa, check_srcaddr(), IAX_DEFAULT_PORTNO, LOG_WARNING, iax2_peer::name, qos, socket_read(), iax2_peer::sockfd, iax2_trunk_peer::sockfd, and strsep().

Referenced by build_peer().

11805 {
11806    struct sockaddr_in sin;
11807    int nonlocal = 1;
11808    int port = IAX_DEFAULT_PORTNO;
11809    int sockfd = defaultsockfd;
11810    char *tmp;
11811    char *addr;
11812    char *portstr;
11813 
11814    if (!(tmp = ast_strdupa(srcaddr)))
11815       return -1;
11816 
11817    addr = strsep(&tmp, ":");
11818    portstr = tmp;
11819 
11820    if (portstr) {
11821       port = atoi(portstr);
11822       if (port < 1)
11823          port = IAX_DEFAULT_PORTNO;
11824    }
11825    
11826    if (!ast_get_ip(&sin, addr)) {
11827       struct ast_netsock *sock;
11828       int res;
11829 
11830       sin.sin_port = 0;
11831       sin.sin_family = AF_INET;
11832       res = check_srcaddr((struct sockaddr *) &sin, sizeof(sin));
11833       if (res == 0) {
11834          /* ip address valid. */
11835          sin.sin_port = htons(port);
11836          if (!(sock = ast_netsock_find(netsock, &sin)))
11837             sock = ast_netsock_find(outsock, &sin);
11838          if (sock) {
11839             sockfd = ast_netsock_sockfd(sock);
11840             nonlocal = 0;
11841          } else {
11842             unsigned int orig_saddr = sin.sin_addr.s_addr;
11843             /* INADDR_ANY matches anyway! */
11844             sin.sin_addr.s_addr = INADDR_ANY;
11845             if (ast_netsock_find(netsock, &sin)) {
11846                sin.sin_addr.s_addr = orig_saddr;
11847                sock = ast_netsock_bind(outsock, io, srcaddr, port, qos.tos, qos.cos, socket_read, NULL);
11848                if (sock) {
11849                   sockfd = ast_netsock_sockfd(sock);
11850                   ast_netsock_unref(sock);
11851                   nonlocal = 0;
11852                } else {
11853                   nonlocal = 2;
11854                }
11855             }
11856          }
11857       }
11858    }
11859       
11860    peer->sockfd = sockfd;
11861 
11862    if (nonlocal == 1) {
11863       ast_log(LOG_WARNING, "Non-local or unbound address specified (%s) in sourceaddress for '%s', reverting to default\n",
11864          srcaddr, peer->name);
11865       return -1;
11866         } else if (nonlocal == 2) {
11867       ast_log(LOG_WARNING, "Unable to bind to sourceaddress '%s' for '%s', reverting to default\n",
11868          srcaddr, peer->name);
11869          return -1;
11870    } else {
11871       ast_debug(1, "Using sourceaddress %s for '%s'\n", srcaddr, peer->name);
11872       return 0;
11873    }
11874 }

static int peer_status ( struct iax2_peer *  peer,
char *  status,
int  statuslen 
) [static]

peer_status: Report Peer status in character string

Definition at line 3555 of file chan_iax2.c.

References ast_copy_string(), iax2_peer::lastms, and iax2_peer::maxms.

Referenced by __iax2_show_peers(), function_iaxpeer(), handle_cli_iax2_show_peer(), and manager_iax2_show_peer_list().

03556 {
03557    int res = 0;
03558    if (peer->maxms) {
03559       if (peer->lastms < 0) {
03560          ast_copy_string(status, "UNREACHABLE", statuslen);
03561       } else if (peer->lastms > peer->maxms) {
03562          snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
03563          res = 1;
03564       } else if (peer->lastms) {
03565          snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
03566          res = 1;
03567       } else {
03568          ast_copy_string(status, "UNKNOWN", statuslen);
03569       }
03570    } else { 
03571       ast_copy_string(status, "Unmonitored", statuslen);
03572       res = -1;
03573    }
03574    return res;
03575 }

static struct iax2_peer* peer_unref ( struct iax2_peer *  peer  )  [static, read]

static int peercnt_add ( struct sockaddr_in *  sin  )  [static]

Definition at line 2170 of file chan_iax2.c.

References peercnt::addr, iax2_trunk_peer::addr, ao2_alloc, ao2_find, ao2_link, ao2_lock(), ao2_ref, ao2_unlock(), ast_debug, ast_inet_ntoa(), ast_log(), peercnt::cur, peercnt::limit, LOG_ERROR, OBJ_POINTER, and set_peercnt_limit().

Referenced by __find_callno(), and complete_transfer().

02171 {
02172    struct peercnt *peercnt;
02173    unsigned long addr = sin->sin_addr.s_addr;
02174    int res = 0;
02175    struct peercnt tmp = {
02176       .addr = addr,
02177    };
02178 
02179    /* Reasoning for peercnts container lock:  Two identical ip addresses
02180     * could be added by different threads at the "same time". Without the container
02181     * lock, both threads could alloc space for the same object and attempt
02182     * to link to table.  With the lock, one would create the object and link
02183     * to table while the other would find the already created peercnt object
02184     * rather than creating a new one. */
02185    ao2_lock(peercnts);
02186    if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
02187       ao2_lock(peercnt);
02188    } else if ((peercnt = ao2_alloc(sizeof(*peercnt), NULL))) {
02189       ao2_lock(peercnt);
02190       /* create and set defaults */
02191       peercnt->addr = addr;
02192       set_peercnt_limit(peercnt);
02193       /* guarantees it does not go away after unlocking table
02194        * ao2_find automatically adds this */
02195       ao2_link(peercnts, peercnt);
02196    } else {
02197       ao2_unlock(peercnts);
02198       return -1;
02199    }
02200 
02201    /* check to see if the address has hit its callno limit.  If not increment cur. */
02202    if (peercnt->limit > peercnt->cur) {
02203       peercnt->cur++;
02204       ast_debug(1, "ip callno count incremented to %d for %s\n", peercnt->cur, ast_inet_ntoa(sin->sin_addr));
02205    } else { /* max num call numbers for this peer has been reached! */
02206       ast_log(LOG_ERROR, "maxcallnumber limit of %d for %s has been reached!\n", peercnt->limit, ast_inet_ntoa(sin->sin_addr));
02207       res = -1;
02208    }
02209 
02210    /* clean up locks and ref count */
02211    ao2_unlock(peercnt);
02212    ao2_unlock(peercnts);
02213    ao2_ref(peercnt, -1); /* decrement ref from find/alloc, only the container ref remains. */
02214 
02215    return res;
02216 }

static int peercnt_cmp_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 2010 of file chan_iax2.c.

References peercnt::addr, CMP_MATCH, and CMP_STOP.

Referenced by load_objects().

02011 {
02012    struct peercnt *peercnt1 = obj, *peercnt2 = arg;
02013    return (peercnt1->addr == peercnt2->addr) ? CMP_MATCH | CMP_STOP : 0;
02014 }

static int peercnt_hash_cb ( const void *  obj,
const int  flags 
) [static]

Definition at line 2004 of file chan_iax2.c.

References peercnt::addr.

Referenced by load_objects().

02005 {
02006    const struct peercnt *peercnt = obj;
02007    return abs((int) peercnt->addr);
02008 }

static void peercnt_modify ( unsigned char  reg,
uint16_t  limit,
struct sockaddr_in *  sin 
) [static]

Definition at line 2142 of file chan_iax2.c.

References peercnt::addr, ao2_find, ao2_ref, ast_debug, ast_inet_ntoa(), peercnt::limit, OBJ_POINTER, peercnt::reg, and set_peercnt_limit().

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

02143 {
02144    /* this function turns off and on custom callno limits set by peer registration */
02145    struct peercnt *peercnt;
02146    struct peercnt tmp = {
02147       .addr = sin->sin_addr.s_addr,
02148    };
02149 
02150    if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
02151       peercnt->reg = reg;
02152       if (limit) {
02153          peercnt->limit = limit;
02154       } else {
02155          set_peercnt_limit(peercnt);
02156       }
02157       ast_debug(1, "peercnt entry %s modified limit:%d registered:%d", ast_inet_ntoa(sin->sin_addr), peercnt->limit, peercnt->reg);
02158       ao2_ref(peercnt, -1); /* decrement ref from find */
02159    }
02160 }

static void peercnt_remove ( struct peercnt *  peercnt  )  [static]

Definition at line 2222 of file chan_iax2.c.

References peercnt::addr, ao2_lock(), ao2_unlink, ao2_unlock(), ast_debug, ast_inet_ntoa(), and peercnt::cur.

Referenced by peercnt_remove_by_addr(), and peercnt_remove_cb().

02223 {
02224    struct sockaddr_in sin = {
02225       .sin_addr.s_addr = peercnt->addr,
02226    };
02227 
02228    if (peercnt) {
02229       /* Container locked here since peercnt may be unlinked from list.  If left unlocked,
02230        * peercnt_add could try and grab this entry from the table and modify it at the
02231        * "same time" this thread attemps to unlink it.*/
02232       ao2_lock(peercnts);
02233       peercnt->cur--;
02234       ast_debug(1, "ip callno count decremented to %d for %s\n", peercnt->cur, ast_inet_ntoa(sin.sin_addr));
02235       /* if this was the last connection from the peer remove it from table */
02236       if (peercnt->cur == 0) {
02237          ao2_unlink(peercnts, peercnt);/* decrements ref from table, last ref is left to scheduler */
02238       }
02239       ao2_unlock(peercnts);
02240    }
02241 }

static int peercnt_remove_by_addr ( struct sockaddr_in *  sin  )  [static]

Definition at line 2261 of file chan_iax2.c.

References peercnt::addr, ao2_find, ao2_ref, OBJ_POINTER, and peercnt_remove().

Referenced by __find_callno(), and complete_transfer().

02262 {
02263    struct peercnt *peercnt;
02264    struct peercnt tmp = {
02265       .addr = sin->sin_addr.s_addr,
02266    };
02267 
02268    if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
02269       peercnt_remove(peercnt);
02270       ao2_ref(peercnt, -1); /* decrement ref from find */
02271    }
02272    return 0;
02273 }

static int peercnt_remove_cb ( const void *  obj  )  [static]

Definition at line 2247 of file chan_iax2.c.

References ao2_ref, and peercnt_remove().

Referenced by sched_delay_remove().

02248 {
02249    struct peercnt *peercnt = (struct peercnt *) obj;
02250 
02251    peercnt_remove(peercnt);
02252    ao2_ref(peercnt, -1); /* decrement ref from scheduler */
02253 
02254    return 0;
02255 }

static void poke_all_peers ( void   )  [static]

Definition at line 12956 of file chan_iax2.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, iax2_poke_peer(), and peer_unref().

Referenced by reload_config().

12957 {
12958    struct ao2_iterator i;
12959    struct iax2_peer *peer;
12960 
12961    i = ao2_iterator_init(peers, 0);
12962    while ((peer = ao2_iterator_next(&i))) {
12963       iax2_poke_peer(peer, 0);
12964       peer_unref(peer);
12965    }
12966    ao2_iterator_destroy(&i);
12967 }

static int prune_addr_range_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 2131 of file chan_iax2.c.

References CMP_MATCH, and addr_range::delme.

Referenced by reload_config().

02132 {
02133    struct addr_range *addr_range = obj;
02134 
02135    return addr_range->delme ? CMP_MATCH : 0;
02136 }

static void prune_peers ( void   )  [static]

Definition at line 12480 of file chan_iax2.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ast_test_flag, IAX_DELME, IAX_RTCACHEFRIENDS, peer_unref(), and unlink_peer().

Referenced by handle_cli_iax2_prune_realtime(), and reload_config().

12481 {
12482    struct iax2_peer *peer;
12483    struct ao2_iterator i;
12484 
12485    i = ao2_iterator_init(peers, 0);
12486    while ((peer = ao2_iterator_next(&i))) {
12487       if (ast_test_flag(peer, IAX_DELME) || ast_test_flag(peer, IAX_RTCACHEFRIENDS)) {
12488          unlink_peer(peer);
12489       }
12490       peer_unref(peer);
12491    }
12492    ao2_iterator_destroy(&i);
12493 }

static void prune_users ( void   )  [static]

Definition at line 12464 of file chan_iax2.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_unlink, ast_test_flag, IAX_DELME, IAX_RTCACHEFRIENDS, and user_unref().

Referenced by handle_cli_iax2_prune_realtime(), and reload_config().

12465 {
12466    struct iax2_user *user;
12467    struct ao2_iterator i;
12468 
12469    i = ao2_iterator_init(users, 0);
12470    while ((user = ao2_iterator_next(&i))) {
12471       if (ast_test_flag(user, IAX_DELME) || ast_test_flag(user, IAX_RTCACHEFRIENDS)) {
12472          ao2_unlink(users, user);
12473       }
12474       user_unref(user);
12475    }
12476    ao2_iterator_destroy(&i);
12477 }

static int pvt_cmp_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 13741 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, chan_iax2_pvt::frames_received, and match().

Referenced by load_objects().

13742 {
13743    struct chan_iax2_pvt *pvt = obj, *pvt2 = arg;
13744 
13745    /* The frames_received field is used to hold whether we're matching
13746     * against a full frame or not ... */
13747 
13748    return match(&pvt2->addr, pvt2->peercallno, pvt2->callno, pvt, 
13749       pvt2->frames_received) ? CMP_MATCH | CMP_STOP : 0;
13750 }

static void pvt_destructor ( void *  obj  )  [static]

Definition at line 1716 of file chan_iax2.c.

References chan_iax2_pvt::addr, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock(), ast_mutex_unlock(), ast_set_flag, ast_string_field_free_memory, ast_variables_destroy(), iax2_registry::callno, iax_frame::callno, chan_iax2_pvt::callno, chan_iax2_pvt::callno_entry, jb_frame::data, free_signaling_queue_entry(), iax2_destroy_helper(), iax2_frame_free(), IAX_ALREADYGONE, chan_iax2_pvt::jb, jb_destroy(), jb_getall(), JB_OK, chan_iax2_pvt::owner, chan_iax2_pvt::reg, iax_frame::retries, s, sched_delay_remove(), chan_iax2_pvt::signaling_queue, and chan_iax2_pvt::vars.

Referenced by new_iax().

01717 {
01718    struct chan_iax2_pvt *pvt = obj;
01719    struct iax_frame *cur = NULL;
01720    struct signaling_queue_entry *s = NULL;
01721 
01722    ast_mutex_lock(&iaxsl[pvt->callno]);
01723    iax2_destroy_helper(pvt);
01724    sched_delay_remove(&pvt->addr, pvt->callno_entry);
01725    pvt->callno_entry = NULL;
01726    ast_mutex_unlock(&iaxsl[pvt->callno]);
01727 
01728    /* Already gone */
01729    ast_set_flag(pvt, IAX_ALREADYGONE); 
01730 
01731    AST_LIST_LOCK(&frame_queue);
01732    AST_LIST_TRAVERSE(&frame_queue, cur, list) {
01733       /* Cancel any pending transmissions */
01734       if (cur->callno == pvt->callno) { 
01735          cur->retries = -1;
01736       }
01737    }
01738    AST_LIST_UNLOCK(&frame_queue);
01739 
01740    while ((s = AST_LIST_REMOVE_HEAD(&pvt->signaling_queue, next))) {
01741       free_signaling_queue_entry(s);
01742    }
01743 
01744    if (pvt->reg) {
01745       pvt->reg->callno = 0;
01746    }
01747 
01748    if (!pvt->owner) {
01749       jb_frame frame;
01750       if (pvt->vars) {
01751           ast_variables_destroy(pvt->vars);
01752           pvt->vars = NULL;
01753       }
01754 
01755       while (jb_getall(pvt->jb, &frame) == JB_OK) {
01756          iax2_frame_free(frame.data);
01757       }
01758 
01759       jb_destroy(pvt->jb);
01760       ast_string_field_free_memory(pvt);
01761    }
01762 }

static int pvt_hash_cb ( const void *  obj,
const int  flags 
) [static]

Definition at line 13734 of file chan_iax2.c.

References chan_iax2_pvt::peercallno.

Referenced by load_objects().

13735 {
13736    const struct chan_iax2_pvt *pvt = obj;
13737 
13738    return pvt->peercallno;
13739 }

static int queue_signalling ( struct chan_iax2_pvt *  pvt,
struct ast_frame *  f 
) [static]

All frames other than that of type AST_FRAME_IAX must be held until we have received a destination call number.

Definition at line 1692 of file chan_iax2.c.

References ast_calloc, AST_FRAME_IAX, AST_LIST_INSERT_TAIL, ast_frame::data, ast_frame::frametype, free_signaling_queue_entry(), chan_iax2_pvt::hold_signaling, ast_frame::ptr, and chan_iax2_pvt::signaling_queue.

Referenced by __send_command().

01693 {
01694    struct signaling_queue_entry *new;
01695 
01696    if (f->frametype == AST_FRAME_IAX || !pvt->hold_signaling) {
01697       return 1; /* do not queue this frame */
01698    } else if (!(new = ast_calloc(1, sizeof(struct signaling_queue_entry)))) {
01699       return -1;  /* out of memory */
01700    }
01701 
01702    memcpy(&new->f, f, sizeof(new->f)); /* copy ast_frame into our queue entry */
01703 
01704    if (new->f.datalen) { /* if there is data in this frame copy it over as well */
01705       if (!(new->f.data.ptr = ast_calloc(1, new->f.datalen))) {
01706          free_signaling_queue_entry(new);
01707          return -1;
01708       }
01709       memcpy(new->f.data.ptr, f->data.ptr, sizeof(*new->f.data.ptr));
01710    }
01711    AST_LIST_INSERT_TAIL(&pvt->signaling_queue, new, next);
01712 
01713    return 0;
01714 }

static int raw_hangup ( struct sockaddr_in *  sin,
unsigned short  src,
unsigned short  dst,
int  sockfd 
) [static]

Definition at line 7432 of file chan_iax2.c.

References ast_debug, AST_FRAME_IAX, ast_inet_ntoa(), compress_subclass(), ast_iax2_full_hdr::csub, ast_iax2_full_hdr::dcallno, IAX_COMMAND_INVAL, IAX_FLAG_FULL, iax_outputframe(), ast_iax2_full_hdr::iseqno, option_debug, ast_iax2_full_hdr::oseqno, ast_iax2_full_hdr::scallno, ast_iax2_full_hdr::ts, and ast_iax2_full_hdr::type.

Referenced by socket_process().

07433 {
07434    struct ast_iax2_full_hdr fh;
07435    fh.scallno = htons(src | IAX_FLAG_FULL);
07436    fh.dcallno = htons(dst);
07437    fh.ts = 0;
07438    fh.oseqno = 0;
07439    fh.iseqno = 0;
07440    fh.type = AST_FRAME_IAX;
07441    fh.csub = compress_subclass(IAX_COMMAND_INVAL);
07442    iax_outputframe(NULL, &fh, 0, sin, 0);
07443 #if 0
07444    if (option_debug)
07445 #endif   
07446       ast_debug(1, "Raw Hangup %s:%d, src=%d, dst=%d\n",
07447          ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), src, dst);
07448    return sendto(sockfd, &fh, sizeof(fh), 0, (struct sockaddr *)sin, sizeof(*sin));
07449 }

static struct iax2_peer * realtime_peer ( const char *  peername,
struct sockaddr_in *  sin 
) [static, read]

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 4125 of file chan_iax2.c.

References iax2_peer::addr, ao2_link, ast_copy_flags, ast_debug, ast_get_time_t(), ast_gethostbyname(), ast_inet_ntoa(), ast_load_realtime(), ast_sched_thread_del, ast_set_flag, ast_test_flag, ast_variables_destroy(), build_peer(), iax2_peer::expire, expire_registry(), hp, iax2_sched_add(), IAX_DEFAULT_REG_EXPIRE, IAX_DYNAMIC, IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, IAX_RTIGNOREREGEXPIRE, IAX_TEMPONLY, inet_aton(), iax2_peer::name, ast_variable::name, ast_variable::next, peer_ref(), peer_unref(), realtime_update_peer(), reg_source_db(), SENTINEL, ast_variable::value, and var.

Referenced by authenticate_reply(), calltoken_required(), find_peer(), and iax2_getpeername().

04126 {
04127    struct ast_variable *var = NULL;
04128    struct ast_variable *tmp;
04129    struct iax2_peer *peer=NULL;
04130    time_t regseconds = 0, nowtime;
04131    int dynamic=0;
04132 
04133    if (peername) {
04134       var = ast_load_realtime("iaxpeers", "name", peername, "host", "dynamic", SENTINEL);
04135       if (!var && sin)
04136          var = ast_load_realtime("iaxpeers", "name", peername, "host", ast_inet_ntoa(sin->sin_addr), SENTINEL);
04137    } else if (sin) {
04138       char porta[25];
04139       sprintf(porta, "%d", ntohs(sin->sin_port));
04140       var = ast_load_realtime("iaxpeers", "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", porta, SENTINEL);
04141       if (var) {
04142          /* We'll need the peer name in order to build the structure! */
04143          for (tmp = var; tmp; tmp = tmp->next) {
04144             if (!strcasecmp(tmp->name, "name"))
04145                peername = tmp->value;
04146          }
04147       }
04148    }
04149    if (!var && peername) { /* Last ditch effort */
04150       var = ast_load_realtime("iaxpeers", "name", peername, SENTINEL);
04151       /*!\note
04152        * If this one loaded something, then we need to ensure that the host
04153        * field matched.  The only reason why we can't have this as a criteria
04154        * is because we only have the IP address and the host field might be
04155        * set as a name (and the reverse PTR might not match).
04156        */
04157       if (var && sin) {
04158          for (tmp = var; tmp; tmp = tmp->next) {
04159             if (!strcasecmp(tmp->name, "host")) {
04160                struct ast_hostent ahp;
04161                struct hostent *hp;
04162                if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
04163                   /* No match */
04164                   ast_variables_destroy(var);
04165                   var = NULL;
04166                }
04167                break;
04168             }
04169          }
04170       }
04171    }
04172    if (!var)
04173       return NULL;
04174 
04175    peer = build_peer(peername, var, NULL, ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS) ? 0 : 1);
04176    
04177    if (!peer) {
04178       ast_variables_destroy(var);
04179       return NULL;
04180    }
04181 
04182    for (tmp = var; tmp; tmp = tmp->next) {
04183       /* Make sure it's not a user only... */
04184       if (!strcasecmp(tmp->name, "type")) {
04185          if (strcasecmp(tmp->value, "friend") &&
04186              strcasecmp(tmp->value, "peer")) {
04187             /* Whoops, we weren't supposed to exist! */
04188             peer = peer_unref(peer);
04189             break;
04190          } 
04191       } else if (!strcasecmp(tmp->name, "regseconds")) {
04192          ast_get_time_t(tmp->value, &regseconds, 0, NULL);
04193       } else if (!strcasecmp(tmp->name, "ipaddr")) {
04194          inet_aton(tmp->value, &(peer->addr.sin_addr));
04195       } else if (!strcasecmp(tmp->name, "port")) {
04196          peer->addr.sin_port = htons(atoi(tmp->value));
04197       } else if (!strcasecmp(tmp->name, "host")) {
04198          if (!strcasecmp(tmp->value, "dynamic"))
04199             dynamic = 1;
04200       }
04201    }
04202 
04203    ast_variables_destroy(var);
04204 
04205    if (!peer)
04206       return NULL;
04207 
04208    if (ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS)) {
04209       ast_copy_flags(peer, &globalflags, IAX_RTAUTOCLEAR|IAX_RTCACHEFRIENDS);
04210       if (ast_test_flag(peer, IAX_RTAUTOCLEAR)) {
04211          if (peer->expire > -1) {
04212             if (!ast_sched_thread_del(sched, peer->expire)) {
04213                peer->expire = -1;
04214                peer_unref(peer);
04215             }
04216          }
04217          peer->expire = iax2_sched_add(sched, (global_rtautoclear) * 1000, expire_registry, peer_ref(peer));
04218          if (peer->expire == -1)
04219             peer_unref(peer);
04220       }
04221       ao2_link(peers, peer);
04222       if (ast_test_flag(peer, IAX_DYNAMIC))
04223          reg_source_db(peer);
04224    } else {
04225       ast_set_flag(peer, IAX_TEMPONLY);   
04226    }
04227 
04228    if (!ast_test_flag(&globalflags, IAX_RTIGNOREREGEXPIRE) && dynamic) {
04229       time(&nowtime);
04230       if ((nowtime - regseconds) > IAX_DEFAULT_REG_EXPIRE) {
04231          memset(&peer->addr, 0, sizeof(peer->addr));
04232          realtime_update_peer(peer->name, &peer->addr, 0);
04233          ast_debug(1, "realtime_peer: Bah, '%s' is expired (%d/%d/%d)!\n",
04234             peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
04235       }
04236       else {
04237          ast_debug(1, "realtime_peer: Registration for '%s' still active (%d/%d/%d)!\n",
04238             peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
04239       }
04240    }
04241 
04242    return peer;
04243 }

static void realtime_update_peer ( const char *  peername,
struct sockaddr_in *  sin,
time_t  regtime 
) [static]

Definition at line 4316 of file chan_iax2.c.

References ast_inet_ntoa(), ast_update_realtime(), and SENTINEL.

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

04317 {
04318    char port[10];
04319    char regseconds[20];
04320    
04321    snprintf(regseconds, sizeof(regseconds), "%d", (int)regtime);
04322    snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
04323    ast_update_realtime("iaxpeers", "name", peername, 
04324       "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", port, 
04325       "regseconds", regseconds, SENTINEL);
04326 }

static struct iax2_user * realtime_user ( const char *  username,
struct sockaddr_in *  sin 
) [static, read]

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 4245 of file chan_iax2.c.

References ao2_link, ast_gethostbyname(), ast_inet_ntoa(), ast_load_realtime(), ast_set_flag, ast_test_flag, ast_variables_destroy(), build_user(), hp, IAX_RTCACHEFRIENDS, IAX_TEMPONLY, ast_variable::name, ast_variable::next, SENTINEL, ast_variable::value, and var.

Referenced by calltoken_required(), and check_access().

04246 {
04247    struct ast_variable *var;
04248    struct ast_variable *tmp;
04249    struct iax2_user *user=NULL;
04250 
04251    var = ast_load_realtime("iaxusers", "name", username, "host", "dynamic", SENTINEL);
04252    if (!var)
04253       var = ast_load_realtime("iaxusers", "name", username, "host", ast_inet_ntoa(sin->sin_addr), SENTINEL);
04254    if (!var && sin) {
04255       char porta[6];
04256       snprintf(porta, sizeof(porta), "%d", ntohs(sin->sin_port));
04257       var = ast_load_realtime("iaxusers", "name", username, "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", porta, SENTINEL);
04258       if (!var)
04259          var = ast_load_realtime("iaxusers", "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", porta, SENTINEL);
04260    }
04261    if (!var) { /* Last ditch effort */
04262       var = ast_load_realtime("iaxusers", "name", username, SENTINEL);
04263       /*!\note
04264        * If this one loaded something, then we need to ensure that the host
04265        * field matched.  The only reason why we can't have this as a criteria
04266        * is because we only have the IP address and the host field might be
04267        * set as a name (and the reverse PTR might not match).
04268        */
04269       if (var) {
04270          for (tmp = var; tmp; tmp = tmp->next) {
04271             if (!strcasecmp(tmp->name, "host")) {
04272                struct ast_hostent ahp;
04273                struct hostent *hp;
04274                if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
04275                   /* No match */
04276                   ast_variables_destroy(var);
04277                   var = NULL;
04278                }
04279                break;
04280             }
04281          }
04282       }
04283    }
04284    if (!var)
04285       return NULL;
04286 
04287    tmp = var;
04288    while(tmp) {
04289       /* Make sure it's not a peer only... */
04290       if (!strcasecmp(tmp->name, "type")) {
04291          if (strcasecmp(tmp->value, "friend") &&
04292              strcasecmp(tmp->value, "user")) {
04293             return NULL;
04294          } 
04295       }
04296       tmp = tmp->next;
04297    }
04298 
04299    user = build_user(username, var, NULL, !ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS));
04300 
04301    ast_variables_destroy(var);
04302 
04303    if (!user)
04304       return NULL;
04305 
04306    if (ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS)) {
04307       ast_set_flag(user, IAX_RTCACHEFRIENDS);
04308       ao2_link(users, user);
04309    } else {
04310       ast_set_flag(user, IAX_TEMPONLY);   
04311    }
04312 
04313    return user;
04314 }

static void reg_source_db ( struct iax2_peer *  p  )  [static]

Definition at line 8307 of file chan_iax2.c.

References iax2_peer::addr, ast_db_get(), AST_DEVICE_UNKNOWN, ast_devstate_changed(), ast_inet_ntoa(), ast_sched_thread_del, ast_test_flag, ast_verb, iax2_peer::expire, expire_registry(), iax2_peer::expiry, iax2_poke_peer(), iax2_regfunk, iax2_sched_add(), IAX_TEMPONLY, inet_aton(), iax2_peer::name, peer_ref(), peer_unref(), and register_peer_exten().

Referenced by realtime_peer(), and set_config().

08308 {
08309    char data[80];
08310    struct in_addr in;
08311    char *c, *d;
08312    if (!ast_test_flag(p, IAX_TEMPONLY) && (!ast_db_get("IAX/Registry", p->name, data, sizeof(data)))) {
08313       c = strchr(data, ':');
08314       if (c) {
08315          *c = '\0';
08316          c++;
08317          if (inet_aton(data, &in)) {
08318             d = strchr(c, ':');
08319             if (d) {
08320                *d = '\0';
08321                d++;
08322                ast_verb(3, "Seeding '%s' at %s:%d for %d\n", p->name,
08323                   ast_inet_ntoa(in), atoi(c), atoi(d));
08324                iax2_poke_peer(p, 0);
08325                p->expiry = atoi(d);
08326                memset(&p->addr, 0, sizeof(p->addr));
08327                p->addr.sin_family = AF_INET;
08328                p->addr.sin_addr = in;
08329                p->addr.sin_port = htons(atoi(c));
08330                if (p->expire > -1) {
08331                   if (!ast_sched_thread_del(sched, p->expire)) {
08332                      p->expire = -1;
08333                      peer_unref(p);
08334                   }
08335                }
08336                ast_devstate_changed(AST_DEVICE_UNKNOWN, "IAX2/%s", p->name); /* Activate notification */
08337                p->expire = iax2_sched_add(sched, (p->expiry + 10) * 1000, expire_registry, peer_ref(p));
08338                if (p->expire == -1)
08339                   peer_unref(p);
08340                if (iax2_regfunk)
08341                   iax2_regfunk(p->name, 1);
08342                register_peer_exten(p, 1);
08343             }              
08344                
08345          }
08346       }
08347    }
08348 }

static void register_peer_exten ( struct iax2_peer *  peer,
int  onoff 
) [static]

Definition at line 8226 of file chan_iax2.c.

References ast_add_extension(), ast_context_remove_extension(), ast_copy_string(), ast_exists_extension(), ast_free_ptr, ast_strdup, ast_strlen_zero(), ext, iax2_peer::name, iax2_peer::regexten, S_OR, and strsep().

Referenced by __expire_registry(), peer_destructor(), reg_source_db(), and update_registry().

08227 {
08228    char multi[256];
08229    char *stringp, *ext;
08230    if (!ast_strlen_zero(regcontext)) {
08231       ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
08232       stringp = multi;
08233       while((ext = strsep(&stringp, "&"))) {
08234          if (onoff) {
08235             if (!ast_exists_extension(NULL, regcontext, ext, 1, NULL))
08236                ast_add_extension(regcontext, 1, ext, 1, NULL, NULL,
08237                        "Noop", ast_strdup(peer->name), ast_free_ptr, "IAX2");
08238          } else
08239             ast_context_remove_extension(regcontext, ext, 1, NULL);
08240       }
08241    }
08242 }

static int register_verify ( int  callno,
struct sockaddr_in *  sin,
struct iax_ies *  ies 
) [static]

Verify inbound registration.

Definition at line 7606 of file chan_iax2.c.

References ast_apply_ha(), ast_check_signature, ast_clear_flag, ast_copy_string(), AST_DEVICE_UNKNOWN, ast_devstate_changed(), ast_inet_ntoa(), ast_key_get, AST_KEY_PUBLIC, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_set_flag, ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_test_flag, iax2_peer::authmethods, chan_iax2_pvt::expiry, find_peer(), iax2_peer::ha, IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_AUTH_RSA, IAX_DYNAMIC, IAX_STATE_AUTHENTICATED, iax2_peer::inkeys, LOG_NOTICE, LOG_WARNING, iax_ies::md5_result, MD5Final(), MD5Init(), MD5Update(), iax2_peer::name, iax_ies::password, peer_unref(), iax_ies::refresh, iax_ies::rsa_result, iax2_peer::secret, secret, strsep(), and iax_ies::username.

Referenced by socket_process().

07607 {
07608    char requeststr[256] = "";
07609    char peer[256] = "";
07610    char md5secret[256] = "";
07611    char rsasecret[256] = "";
07612    char secret[256] = "";
07613    struct iax2_peer *p = NULL;
07614    struct ast_key *key;
07615    char *keyn;
07616    int x;
07617    int expire = 0;
07618    int res = -1;
07619 
07620    ast_clear_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
07621    /* iaxs[callno]->peer[0] = '\0'; not necc. any more-- stringfield is pre-inited to null string */
07622    if (ies->username)
07623       ast_copy_string(peer, ies->username, sizeof(peer));
07624    if (ies->password)
07625       ast_copy_string(secret, ies->password, sizeof(secret));
07626    if (ies->md5_result)
07627       ast_copy_string(md5secret, ies->md5_result, sizeof(md5secret));
07628    if (ies->rsa_result)
07629       ast_copy_string(rsasecret, ies->rsa_result, sizeof(rsasecret));
07630    if (ies->refresh)
07631       expire = ies->refresh;
07632 
07633    if (ast_strlen_zero(peer)) {
07634       ast_log(LOG_NOTICE, "Empty registration from %s\n", ast_inet_ntoa(sin->sin_addr));
07635       return -1;
07636    }
07637 
07638    /* SLD: first call to lookup peer during registration */
07639    ast_mutex_unlock(&iaxsl[callno]);
07640    p = find_peer(peer, 1);
07641    ast_mutex_lock(&iaxsl[callno]);
07642    if (!p || !iaxs[callno]) {
07643       if (iaxs[callno]) {
07644          int plaintext = ((last_authmethod & IAX_AUTH_PLAINTEXT) | (iaxs[callno]->authmethods & IAX_AUTH_PLAINTEXT));
07645          /* Anything, as long as it's non-blank */
07646          ast_string_field_set(iaxs[callno], secret, "badsecret");
07647          /* An AUTHREQ must be sent in response to a REGREQ of an invalid peer unless
07648           * 1. A challenge already exists indicating a AUTHREQ was already sent out.
07649           * 2. A plaintext secret is present in ie as result of a previous AUTHREQ requesting it.
07650           * 3. A plaintext secret is present in the ie and the last_authmethod used by a peer happened
07651           *    to be plaintext, indicating it is an authmethod used by other peers on the system. 
07652           *
07653           * If none of these cases exist, res will be returned as 0 without authentication indicating
07654           * an AUTHREQ needs to be sent out. */
07655 
07656          if (ast_strlen_zero(iaxs[callno]->challenge) &&
07657             !(!ast_strlen_zero(secret) && plaintext)) {
07658             /* by setting res to 0, an REGAUTH will be sent */
07659             res = 0;
07660          }
07661       }
07662       if (authdebug && !p)
07663          ast_log(LOG_NOTICE, "No registration for peer '%s' (from %s)\n", peer, ast_inet_ntoa(sin->sin_addr));
07664       goto return_unref;
07665    }
07666 
07667    if (!ast_test_flag(p, IAX_DYNAMIC)) {
07668       if (authdebug)
07669          ast_log(LOG_NOTICE, "Peer '%s' is not dynamic (from %s)\n", peer, ast_inet_ntoa(sin->sin_addr));
07670       goto return_unref;
07671    }
07672 
07673    if (!ast_apply_ha(p->ha, sin)) {
07674       if (authdebug)
07675          ast_log(LOG_NOTICE, "Host %s denied access to register peer '%s'\n", ast_inet_ntoa(sin->sin_addr), p->name);
07676       goto return_unref;
07677    }
07678    ast_string_field_set(iaxs[callno], secret, p->secret);
07679    ast_string_field_set(iaxs[callno], inkeys, p->inkeys);
07680    /* Check secret against what we have on file */
07681    if (!ast_strlen_zero(rsasecret) && (p->authmethods & IAX_AUTH_RSA) && !ast_strlen_zero(iaxs[callno]->challenge)) {
07682       if (!ast_strlen_zero(p->inkeys)) {
07683          char tmpkeys[256];
07684          char *stringp=NULL;
07685          ast_copy_string(tmpkeys, p->inkeys, sizeof(tmpkeys));
07686          stringp=tmpkeys;
07687          keyn = strsep(&stringp, ":");
07688          while(keyn) {
07689             key = ast_key_get(keyn, AST_KEY_PUBLIC);
07690             if (key && !ast_check_signature(key, iaxs[callno]->challenge, rsasecret)) {
07691                ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
07692                break;
07693             } else if (!key)
07694                ast_log(LOG_WARNING, "requested inkey '%s' does not exist\n", keyn);
07695             keyn = strsep(&stringp, ":");
07696          }
07697          if (!keyn) {
07698             if (authdebug)
07699                ast_log(LOG_NOTICE, "Host %s failed RSA authentication with inkeys '%s'\n", peer, p->inkeys);
07700             goto return_unref;
07701          }
07702       } else {
07703          if (authdebug)
07704             ast_log(LOG_NOTICE, "Host '%s' trying to do RSA authentication, but we have no inkeys\n", peer);
07705          goto return_unref;
07706       }
07707    } else if (!ast_strlen_zero(md5secret) && (p->authmethods & IAX_AUTH_MD5) && !ast_strlen_zero(iaxs[callno]->challenge)) {
07708       struct MD5Context md5;
07709       unsigned char digest[16];
07710       char *tmppw, *stringp;
07711 
07712       tmppw = ast_strdupa(p->secret);
07713       stringp = tmppw;
07714       while((tmppw = strsep(&stringp, ";"))) {
07715          MD5Init(&md5);
07716          MD5Update(&md5, (unsigned char *)iaxs[callno]->challenge, strlen(iaxs[callno]->challenge));
07717          MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
07718          MD5Final(digest, &md5);
07719          for (x=0;x<16;x++)
07720             sprintf(requeststr + (x << 1), "%2.2x", digest[x]); /* safe */
07721          if (!strcasecmp(requeststr, md5secret))
07722             break;
07723       }
07724       if (tmppw) {
07725          ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
07726       } else {
07727          if (authdebug)
07728             ast_log(LOG_NOTICE, "Host %s failed MD5 authentication for '%s' (%s != %s)\n", ast_inet_ntoa(sin->sin_addr), p->name, requeststr, md5secret);
07729          goto return_unref;
07730       }
07731    } else if (!ast_strlen_zero(secret) && (p->authmethods & IAX_AUTH_PLAINTEXT)) {
07732       /* They've provided a plain text password and we support that */
07733       if (strcmp(secret, p->secret)) {
07734          if (authdebug)
07735             ast_log(LOG_NOTICE, "Host %s did not provide proper plaintext password for '%s'\n", ast_inet_ntoa(sin->sin_addr), p->name);
07736          goto return_unref;
07737       } else
07738          ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
07739    } else if (!ast_strlen_zero(iaxs[callno]->challenge) && ast_strlen_zero(md5secret) && ast_strlen_zero(rsasecret)) {
07740       /* if challenge has been sent, but no challenge response if given, reject. */
07741       goto return_unref;
07742    }
07743    ast_devstate_changed(AST_DEVICE_UNKNOWN, "IAX2/%s", p->name); /* Activate notification */
07744 
07745    /* either Authentication has taken place, or a REGAUTH must be sent before verifying registration */
07746    res = 0;
07747 
07748 return_unref:
07749    if (iaxs[callno]) {
07750       ast_string_field_set(iaxs[callno], peer, peer);
07751 
07752       /* Choose lowest expiry number */
07753       if (expire && (expire < iaxs[callno]->expiry)) {
07754          iaxs[callno]->expiry = expire;
07755       }
07756    }
07757 
07758    if (p) {
07759       peer_unref(p);
07760    }
07761    return res;
07762 }

static int registry_authrequest ( int  callno  )  [static]

Definition at line 8515 of file chan_iax2.c.

References AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), ast_random(), ast_strdupa, ast_string_field_set, chan_iax2_pvt::authmethods, iax2_peer::authmethods, iax_ie_data::buf, find_peer(), IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_AUTH_RSA, IAX_COMMAND_REGAUTH, iax_ie_append_short(), iax_ie_append_str(), IAX_IE_AUTHMETHODS, IAX_IE_CHALLENGE, IAX_IE_USERNAME, peer_unref(), iax_ie_data::pos, and send_command().

Referenced by socket_process().

08516 {
08517    struct iax_ie_data ied;
08518    struct iax2_peer *p;
08519    char challenge[10];
08520    const char *peer_name;
08521    int sentauthmethod;
08522 
08523    peer_name = ast_strdupa(iaxs[callno]->peer);
08524 
08525    /* SLD: third call to find_peer in registration */
08526    ast_mutex_unlock(&iaxsl[callno]);
08527    if ((p = find_peer(peer_name, 1))) {
08528       last_authmethod = p->authmethods;
08529    }
08530 
08531    ast_mutex_lock(&iaxsl[callno]);
08532    if (!iaxs[callno])
08533       goto return_unref;
08534 
08535    memset(&ied, 0, sizeof(ied));
08536    /* The selection of which delayed reject is sent may leak information,
08537     * if it sets a static response.  For example, if a host is known to only
08538     * use MD5 authentication, then an RSA response would indicate that the
08539     * peer does not exist, and vice-versa.
08540     * Therefore, we use whatever the last peer used (which may vary over the
08541     * course of a server, which should leak minimal information). */
08542    sentauthmethod = p ? p->authmethods : last_authmethod ? last_authmethod : (IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT);
08543    if (!p) {
08544       iaxs[callno]->authmethods = sentauthmethod;
08545    }
08546    iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, sentauthmethod);
08547    if (sentauthmethod & (IAX_AUTH_RSA | IAX_AUTH_MD5)) {
08548       /* Build the challenge */
08549       snprintf(challenge, sizeof(challenge), "%d", (int)ast_random());
08550       ast_string_field_set(iaxs[callno], challenge, challenge);
08551       iax_ie_append_str(&ied, IAX_IE_CHALLENGE, iaxs[callno]->challenge);
08552    }
08553    iax_ie_append_str(&ied, IAX_IE_USERNAME, peer_name);
08554 
08555 return_unref:
08556    if (p) {
08557       peer_unref(p);
08558    }
08559 
08560    return iaxs[callno] ? send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGAUTH, 0, ied.buf, ied.pos, -1) : -1;
08561 }

static int registry_rerequest ( struct iax_ies *  ies,
int  callno,
struct sockaddr_in *  sin 
) [static]

Definition at line 8563 of file chan_iax2.c.

References add_empty_calltoken_ie(), iax2_registry::addr, ast_copy_string(), AST_FRAME_IAX, ast_inet_ntoa(), ast_log(), ast_strlen_zero(), authenticate(), iax_ies::authmethods, iax_ie_data::buf, iax_ies::challenge, IAX_COMMAND_REGREQ, iax_ie_append_short(), iax_ie_append_str(), IAX_IE_REFRESH, IAX_IE_USERNAME, inaddrcmp(), LOG_NOTICE, LOG_WARNING, iax_ie_data::pos, iax2_registry::refresh, chan_iax2_pvt::reg, REG_STATE_AUTHSENT, REG_STATE_NOAUTH, iax2_registry::regstate, iax2_registry::secret, send_command(), iax2_registry::username, and iax_ies::username.

Referenced by socket_process().

08564 {
08565    struct iax2_registry *reg;
08566    /* Start pessimistic */
08567    struct iax_ie_data ied;
08568    char peer[256] = "";
08569    char challenge[256] = "";
08570    int res;
08571    int authmethods = 0;
08572    if (ies->authmethods)
08573       authmethods = ies->authmethods;
08574    if (ies->username)
08575       ast_copy_string(peer, ies->username, sizeof(peer));
08576    if (ies->challenge)
08577       ast_copy_string(challenge, ies->challenge, sizeof(challenge));
08578    memset(&ied, 0, sizeof(ied));
08579    reg = iaxs[callno]->reg;
08580    if (reg) {
08581          if (inaddrcmp(&reg->addr, sin)) {
08582             ast_log(LOG_WARNING, "Received unsolicited registry authenticate request from '%s'\n", ast_inet_ntoa(sin->sin_addr));
08583             return -1;
08584          }
08585          if (ast_strlen_zero(reg->secret)) {
08586             ast_log(LOG_NOTICE, "No secret associated with peer '%s'\n", reg->username);
08587             reg->regstate = REG_STATE_NOAUTH;
08588             return -1;
08589          }
08590          iax_ie_append_str(&ied, IAX_IE_USERNAME, reg->username);
08591          iax_ie_append_short(&ied, IAX_IE_REFRESH, reg->refresh);
08592          if (reg->secret[0] == '[') {
08593             char tmpkey[256];
08594             ast_copy_string(tmpkey, reg->secret + 1, sizeof(tmpkey));
08595             tmpkey[strlen(tmpkey) - 1] = '\0';
08596             res = authenticate(challenge, NULL, tmpkey, authmethods, &ied, sin, NULL);
08597          } else
08598             res = authenticate(challenge, reg->secret, NULL, authmethods, &ied, sin, NULL);
08599          if (!res) {
08600             reg->regstate = REG_STATE_AUTHSENT;
08601             add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
08602             return send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGREQ, 0, ied.buf, ied.pos, -1);
08603          } else
08604             return -1;
08605          ast_log(LOG_WARNING, "Registry acknowledge on unknown registery '%s'\n", peer);
08606    } else   
08607       ast_log(LOG_NOTICE, "Can't reregister without a reg\n");
08608    return -1;
08609 }

static char* regstate2str ( int  regstate  )  [static]

Definition at line 6723 of file chan_iax2.c.

References REG_STATE_AUTHSENT, REG_STATE_NOAUTH, REG_STATE_REGISTERED, REG_STATE_REGSENT, REG_STATE_REJECTED, REG_STATE_TIMEOUT, and REG_STATE_UNREGISTERED.

Referenced by handle_cli_iax2_show_registry(), and manager_iax2_show_registry().

06724 {
06725    switch(regstate) {
06726    case REG_STATE_UNREGISTERED:
06727       return "Unregistered";
06728    case REG_STATE_REGSENT:
06729       return "Request Sent";
06730    case REG_STATE_AUTHSENT:
06731       return "Auth. Sent";
06732    case REG_STATE_REGISTERED:
06733       return "Registered";
06734    case REG_STATE_REJECTED:
06735       return "Rejected";
06736    case REG_STATE_TIMEOUT:
06737       return "Timeout";
06738    case REG_STATE_NOAUTH:
06739       return "No Authentication";
06740    default:
06741       return "Unknown";
06742    }
06743 }

static int reload ( void   )  [static]

Definition at line 13017 of file chan_iax2.c.

References reload_config().

13018 {
13019    return reload_config();
13020 }

static int reload_config ( void   )  [static]

Definition at line 12968 of file chan_iax2.c.

References ao2_callback, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_unload_realtime(), config, debugaddr, iax2_do_register(), iax_provision_reload(), OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, poke_all_peers(), prune_addr_range_cb(), prune_peers(), prune_users(), reload_firmware(), set_config(), and set_peercnt_limit_all_cb().

Referenced by handle_cli_iax2_reload(), and reload().

12969 {
12970    static const char config[] = "iax.conf";
12971    struct iax2_registry *reg;
12972 
12973    if (set_config(config, 1) > 0) {
12974       prune_peers();
12975       prune_users();
12976       ao2_callback(callno_limits, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, prune_addr_range_cb, NULL);
12977       ao2_callback(calltoken_ignores, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, prune_addr_range_cb, NULL);
12978       ao2_callback(peercnts, OBJ_NODATA, set_peercnt_limit_all_cb, NULL);
12979       trunk_timed = trunk_untimed = 0; 
12980       trunk_nmaxmtu = trunk_maxmtu = 0;
12981       memset(&debugaddr, '\0', sizeof(debugaddr));
12982 
12983       AST_LIST_LOCK(&registrations);
12984       AST_LIST_TRAVERSE(&registrations, reg, entry)
12985          iax2_do_register(reg);
12986       AST_LIST_UNLOCK(&registrations);
12987 
12988       /* Qualify hosts, too */
12989       poke_all_peers();
12990    }
12991    
12992    reload_firmware(0);
12993    iax_provision_reload(1);
12994    ast_unload_realtime("iaxpeers");
12995 
12996    return 0;
12997 }

static void reload_firmware ( int  unload  )  [static]

Definition at line 3052 of file chan_iax2.c.

References ast_config_AST_DATA_DIR, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), ast_verb, iax_firmware::dead, destroy_firmware(), dir, errno, LOG_WARNING, and try_firmware().

Referenced by __unload_module(), load_module(), and reload_config().

03053 {
03054    struct iax_firmware *cur = NULL;
03055    DIR *fwd;
03056    struct dirent *de;
03057    char dir[256], fn[256];
03058 
03059    AST_LIST_LOCK(&firmwares);
03060 
03061    /* Mark all as dead */
03062    AST_LIST_TRAVERSE(&firmwares, cur, list)
03063       cur->dead = 1;
03064 
03065    /* Now that we have marked them dead... load new ones */
03066    if (!unload) {
03067       snprintf(dir, sizeof(dir), "%s/firmware/iax", ast_config_AST_DATA_DIR);
03068       fwd = opendir(dir);
03069       if (fwd) {
03070          while((de = readdir(fwd))) {
03071             if (de->d_name[0] != '.') {
03072                snprintf(fn, sizeof(fn), "%s/%s", dir, de->d_name);
03073                if (!try_firmware(fn)) {
03074                   ast_verb(2, "Loaded firmware '%s'\n", de->d_name);
03075                }
03076             }
03077          }
03078          closedir(fwd);
03079       } else 
03080          ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", dir, strerror(errno));
03081    }
03082 
03083    /* Clean up leftovers */
03084    AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
03085       if (!cur->dead)
03086          continue;
03087       AST_LIST_REMOVE_CURRENT(list);
03088       destroy_firmware(cur);
03089    }
03090    AST_LIST_TRAVERSE_SAFE_END;
03091 
03092    AST_LIST_UNLOCK(&firmwares);
03093 }

static void remove_by_peercallno ( struct chan_iax2_pvt *  pvt  )  [static]

Definition at line 1973 of file chan_iax2.c.

References ao2_unlink, ast_log(), LOG_ERROR, and chan_iax2_pvt::peercallno.

Referenced by complete_transfer(), iax2_destroy(), resend_with_token(), and socket_process().

01974 {
01975    if (!pvt->peercallno) {
01976       ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
01977       return;
01978    }
01979 
01980    ao2_unlink(iax_peercallno_pvts, pvt);
01981 }

static void remove_by_transfercallno ( struct chan_iax2_pvt *  pvt  )  [static]

Definition at line 1954 of file chan_iax2.c.

References ao2_unlink, ast_log(), LOG_ERROR, and chan_iax2_pvt::transfercallno.

Referenced by complete_transfer(), and iax2_destroy().

01955 {
01956    if (!pvt->transfercallno) {
01957       ast_log(LOG_ERROR, "This should not be called without a transfer call number.\n");
01958       return;
01959    }
01960 
01961    ao2_unlink(iax_transfercallno_pvts, pvt);
01962 }

static int replace_callno ( const void *  obj  )  [static]

Definition at line 2459 of file chan_iax2.c.

References ao2_link, ao2_lock(), ao2_ref, ao2_unlock(), ast_log(), callno_entry::callno, LOG_ERROR, TRUNK_CALL_START, and callno_entry::validated.

Referenced by __find_callno(), make_trunk(), and sched_delay_remove().

02460 {
02461    struct callno_entry *callno_entry = (struct callno_entry *) obj;
02462 
02463    /* the callno_pool container is locked here primarily to ensure thread
02464     * safety of the total_nonval_callno_used check and decrement */
02465    ao2_lock(callno_pool);
02466 
02467    if (!callno_entry->validated && (total_nonval_callno_used != 0)) {
02468       total_nonval_callno_used--;
02469    } else if (!callno_entry->validated && (total_nonval_callno_used == 0)) {
02470       ast_log(LOG_ERROR, "Attempted to decrement total non calltoken validated callnumbers below zero... Callno is:%d \n", callno_entry->callno);
02471    }
02472 
02473    if (callno_entry->callno < TRUNK_CALL_START) {
02474       ao2_link(callno_pool, callno_entry);
02475    } else {
02476       ao2_link(callno_pool_trunk, callno_entry);
02477    }
02478    ao2_ref(callno_entry, -1); /* only container ref remains */
02479 
02480    ao2_unlock(callno_pool);
02481    return 0;
02482 }

static void requirecalltoken_mark_auto ( const char *  name,
int  subclass 
) [static]

Definition at line 4604 of file chan_iax2.c.

References ast_strlen_zero(), CALLTOKEN_AUTO, iax2_peer::calltoken_required, iax2_user::calltoken_required, CALLTOKEN_YES, find_peer(), find_user(), IAX_COMMAND_NEW, peer_unref(), and user_unref().

Referenced by handle_call_token().

04605 {
04606    struct iax2_user *user = NULL;
04607    struct iax2_peer *peer = NULL;
04608 
04609    if (ast_strlen_zero(name)) {
04610       return; /* no username given */
04611    }
04612 
04613    if ((subclass == IAX_COMMAND_NEW) && (user = find_user(name)) && (user->calltoken_required == CALLTOKEN_AUTO)) {
04614       user->calltoken_required = CALLTOKEN_YES;
04615    } else if ((subclass != IAX_COMMAND_NEW) && (peer = find_peer(name, 1)) && (peer->calltoken_required == CALLTOKEN_AUTO)) {
04616       peer->calltoken_required = CALLTOKEN_YES;
04617    }
04618 
04619    if (peer) {
04620       peer_unref(peer);
04621    }
04622    if (user) {
04623       user_unref(user);
04624    }
04625 }

static void resend_with_token ( int  callno,
struct iax_frame *  f,
const char *  newtoken 
) [static]

Definition at line 4520 of file chan_iax2.c.

References iax_frame::af, chan_iax2_pvt::aseqno, AST_FRAME_IAX, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, iax_ie_data::buf, chan_iax2_pvt::calltoken_ie_len, iax_frame::data, iax_frame::datalen, iax_frame::dcallno, iax_frame::encmethods, ast_frame::frametype, iax2_allow_new(), iax2_frame_free(), iax_ie_append_str(), IAX_IE_CALLTOKEN, chan_iax2_pvt::iseqno, chan_iax2_pvt::oseqno, chan_iax2_pvt::peercallno, remove_by_peercallno(), chan_iax2_pvt::rseqno, send_command(), and ast_frame::subclass.

Referenced by socket_process().

04521 {
04522    struct chan_iax2_pvt *pvt = iaxs[callno];
04523    int frametype = f->af.frametype;
04524    int subclass = f->af.subclass;
04525    struct {
04526       struct ast_iax2_full_hdr fh;
04527       struct iax_ie_data ied;
04528    } data = {
04529       .ied.buf = { 0 },
04530       .ied.pos = 0,
04531    };
04532    /* total len - header len gives us the frame's IE len */
04533    int ie_data_pos = f->datalen - sizeof(struct ast_iax2_full_hdr);
04534 
04535    if (!pvt) {
04536       return;  /* this should not be possible if called from socket_process() */
04537    }
04538 
04539    /* 
04540     * Check to make sure last frame sent is valid for call token resend
04541     * 1. Frame should _NOT_ be encrypted since it starts the IAX dialog 
04542     * 2. Frame should _NOT_ already have a destination callno
04543     * 3. Frame must be a valid iax_frame subclass capable of starting dialog
04544     * 4. Pvt must have a calltoken_ie_len which represents the number of
04545     *    bytes at the end of the frame used for the previous calltoken ie.
04546     * 5. Pvt's calltoken_ie_len must be _LESS_ than the total IE length
04547     * 6. Total length of f->data must be _LESS_ than size of our data struct
04548     *    because f->data must be able to fit within data. 
04549     */
04550    if (f->encmethods || f->dcallno || !iax2_allow_new(frametype, subclass, 0)
04551       || !pvt->calltoken_ie_len || (pvt->calltoken_ie_len > ie_data_pos) ||
04552       (f->datalen > sizeof(data))) {
04553 
04554       return;  /* ignore resend, token was not valid for the dialog */
04555    }
04556 
04557    /* token is valid
04558     * 1. Copy frame data over
04559     * 2. Redo calltoken IE, it will always be the last ie in the frame.
04560     *    NOTE: Having the ie always be last is not protocol specified,
04561     *    it is only an implementation choice.  Since we only expect the ie to
04562     *    be last for frames we have sent, this can no way be affected by
04563     *    another end point.
04564     * 3. Remove frame from queue
04565     * 4. Free old frame
04566     * 5. Clear previous seqnos
04567     * 6. Resend with CALLTOKEN ie.
04568     */
04569 
04570    /* ---1.--- */
04571    memcpy(&data, f->data, f->datalen);
04572    data.ied.pos = ie_data_pos;
04573 
04574    /* ---2.--- */
04575    /* move to the beginning of the calltoken ie so we can write over it */
04576    data.ied.pos -= pvt->calltoken_ie_len;
04577    iax_ie_append_str(&data.ied, IAX_IE_CALLTOKEN, newtoken);
04578 
04579    /* make sure to update token length incase it ever has to be stripped off again */
04580    pvt->calltoken_ie_len = data.ied.pos - ie_data_pos; /* new pos minus old pos tells how big token ie is */
04581 
04582    /* ---3.--- */
04583    AST_LIST_LOCK(&frame_queue);
04584    AST_LIST_REMOVE(&frame_queue, f, list);
04585    AST_LIST_UNLOCK(&frame_queue);
04586 
04587    /* ---4.--- */
04588    iax2_frame_free(f);
04589 
04590    /* ---5.--- */
04591    pvt->oseqno = 0;
04592    pvt->rseqno = 0;
04593    pvt->iseqno = 0;
04594    pvt->aseqno = 0;
04595    if (pvt->peercallno) {
04596       remove_by_peercallno(pvt);
04597       pvt->peercallno = 0;
04598    }
04599 
04600    /* ---6.--- */
04601    send_command(pvt, AST_FRAME_IAX, subclass, 0, data.ied.buf, data.ied.pos, -1);
04602 }

static void save_osptoken ( struct iax_frame *  fr,
struct iax_ies *  ies 
) [static]

Definition at line 9044 of file chan_iax2.c.

References ast_string_field_set, iax_frame::callno, IAX_MAX_OSPBLOCK_NUM, IAX_MAX_OSPBLOCK_SIZE, IAX_MAX_OSPBUFF_SIZE, iax_ies::ospblocklength, and iax_ies::osptokenblock.

Referenced by socket_process().

09045 {
09046    int i;
09047    unsigned int length, offset = 0;
09048    char full_osptoken[IAX_MAX_OSPBUFF_SIZE];
09049 
09050    for (i = 0; i < IAX_MAX_OSPBLOCK_NUM; i++) {
09051       length = ies->ospblocklength[i];
09052       if (length != 0) {
09053          if (length > IAX_MAX_OSPBLOCK_SIZE) {
09054             /* OSP token block length wrong, clear buffer */
09055             offset = 0;
09056             break;
09057          } else {
09058             memcpy(full_osptoken + offset, ies->osptokenblock[i], length);
09059             offset += length;
09060          }
09061       } else {
09062          break;
09063       }
09064    }
09065    *(full_osptoken + offset) = '\0';
09066    if (strlen(full_osptoken) != offset) {
09067       /* OSP token length wrong, clear buffer */
09068       *full_osptoken = '\0';
09069    }
09070 
09071    ast_string_field_set(iaxs[fr->callno], osptoken, full_osptoken);
09072 }

static void save_rr ( struct iax_frame *  fr,
struct iax_ies *  ies 
) [static]

static void sched_delay_remove ( struct sockaddr_in *  sin,
struct callno_entry *  callno_entry 
) [static]

Definition at line 2531 of file chan_iax2.c.

References peercnt::addr, ao2_find, ao2_ref, ast_debug, ast_inet_ntoa(), iax2_sched_add(), MIN_REUSE_TIME, OBJ_POINTER, peercnt_remove_cb(), and replace_callno().

Referenced by pvt_destructor().

02532 {
02533    int i;
02534    struct peercnt *peercnt;
02535    struct peercnt tmp = {
02536       .addr = sin->sin_addr.s_addr,
02537    };
02538 
02539    if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
02540       /* refcount is incremented with ao2_find.  keep that ref for the scheduler */
02541       ast_debug(1, "schedule decrement of callno used for %s in %d seconds\n", ast_inet_ntoa(sin->sin_addr), MIN_REUSE_TIME);
02542       i = iax2_sched_add(sched, MIN_REUSE_TIME * 1000, peercnt_remove_cb, peercnt);
02543       if (i == -1) {
02544          ao2_ref(peercnt, -1);
02545       }
02546    }
02547 
02548    iax2_sched_add(sched, MIN_REUSE_TIME * 1000, replace_callno, callno_entry);
02549 }

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 3980 of file chan_iax2.c.

References __do_deliver(), iax_frame::af, ast_bridged_channel(), AST_CHAN_TP_WANTSJITTER, ast_codec_get_samples(), ast_debug, ast_format_rate(), AST_FRAME_CNG, AST_FRAME_VOICE, ast_samp2tv(), ast_sched_thread_del, ast_test_flag, ast_tv(), ast_tvadd(), ast_tvzero(), ast_channel::bridge, calc_rxstamp(), iax_frame::callno, jb_frame::data, ast_frame::delivery, ast_frame::frametype, iax2_frame_free(), IAX_FORCEJITTERBUF, IAX_USEJITTERBUF, chan_iax2_pvt::jb, JB_DROP, jb_getall(), JB_OK, jb_put(), jb_reset(), JB_SCHED, JB_TYPE_CONTROL, JB_TYPE_SILENCE, JB_TYPE_VOICE, chan_iax2_pvt::jbid, chan_iax2_pvt::owner, ast_channel_tech::properties, chan_iax2_pvt::rxcore, ast_frame::subclass, ast_channel::tech, iax_frame::ts, unwrap_timestamp(), and update_jbsched().

Referenced by socket_process(), and socket_process_meta().

03981 {
03982    int type, len;
03983    int ret;
03984    int needfree = 0;
03985    struct ast_channel *owner = NULL;
03986    struct ast_channel *bridge = NULL;
03987    
03988    /* Attempt to recover wrapped timestamps */
03989    unwrap_timestamp(fr);
03990 
03991    /* delivery time is sender's sent timestamp converted back into absolute time according to our clock */
03992    if ( !fromtrunk && !ast_tvzero(iaxs[fr->callno]->rxcore))
03993       fr->af.delivery = ast_tvadd(iaxs[fr->callno]->rxcore, ast_samp2tv(fr->ts, 1000));
03994    else {
03995 #if 0
03996       ast_debug(1, "schedule_delivery: set delivery to 0 as we don't have an rxcore yet, or frame is from trunk.\n");
03997 #endif
03998       fr->af.delivery = ast_tv(0,0);
03999    }
04000 
04001    type = JB_TYPE_CONTROL;
04002    len = 0;
04003 
04004    if(fr->af.frametype == AST_FRAME_VOICE) {
04005       type = JB_TYPE_VOICE;
04006       len = ast_codec_get_samples(&fr->af) / (ast_format_rate(fr->af.subclass) / 1000);
04007    } else if(fr->af.frametype == AST_FRAME_CNG) {
04008       type = JB_TYPE_SILENCE;
04009    }
04010 
04011    if ( (!ast_test_flag(iaxs[fr->callno], IAX_USEJITTERBUF)) ) {
04012       if (tsout)
04013          *tsout = fr->ts;
04014       __do_deliver(fr);
04015       return -1;
04016    }
04017 
04018    if ((owner = iaxs[fr->callno]->owner))
04019       bridge = ast_bridged_channel(owner);
04020 
04021    /* if the user hasn't requested we force the use of the jitterbuffer, and we're bridged to
04022     * a channel that can accept jitter, then flush and suspend the jb, and send this frame straight through */
04023    if ( (!ast_test_flag(iaxs[fr->callno], IAX_FORCEJITTERBUF)) && owner && bridge && (bridge->tech->properties & AST_CHAN_TP_WANTSJITTER) ) {
04024       jb_frame frame;
04025 
04026       /* deliver any frames in the jb */
04027       while (jb_getall(iaxs[fr->callno]->jb, &frame) == JB_OK) {
04028          __do_deliver(frame.data);
04029          /* __do_deliver() can make the call disappear */
04030          if (!iaxs[fr->callno])
04031             return -1;
04032       }
04033 
04034       jb_reset(iaxs[fr->callno]->jb);
04035 
04036       ast_sched_thread_del(sched, iaxs[fr->callno]->jbid);
04037 
04038       /* deliver this frame now */
04039       if (tsout)
04040          *tsout = fr->ts;
04041       __do_deliver(fr);
04042       return -1;
04043    }
04044 
04045    /* insert into jitterbuffer */
04046    /* TODO: Perhaps we could act immediately if it's not droppable and late */
04047    ret = jb_put(iaxs[fr->callno]->jb, fr, type, len, fr->ts,
04048          calc_rxstamp(iaxs[fr->callno],fr->ts));
04049    if (ret == JB_DROP) {
04050       needfree++;
04051    } else if (ret == JB_SCHED) {
04052       update_jbsched(iaxs[fr->callno]);
04053    }
04054    if (tsout)
04055       *tsout = fr->ts;
04056    if (needfree) {
04057       /* Free our iax frame */
04058       iax2_frame_free(fr);
04059       return -1;
04060    }
04061    return 0;
04062 }

static int scheduled_destroy ( const void *  vid  )  [static]

Definition at line 1657 of file chan_iax2.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax2_destroy(), LOG_DEBUG, option_debug, and PTR_TO_CALLNO.

Referenced by iax2_hangup().

01658 {
01659    unsigned short callno = PTR_TO_CALLNO(vid);
01660    ast_mutex_lock(&iaxsl[callno]);
01661    if (iaxs[callno]) {
01662       if (option_debug) {
01663          ast_log(LOG_DEBUG, "Really destroying %d now...\n", callno);
01664       }
01665       iax2_destroy(callno);
01666    }
01667    ast_mutex_unlock(&iaxsl[callno]);
01668    return 0;
01669 }

static int send_apathetic_reply ( unsigned short  callno,
unsigned short  dcallno,
struct sockaddr_in *  sin,
int  command,
int  ts,
unsigned char  seqno,
int  sockfd,
struct iax_ie_data *  ied 
) [static]

Definition at line 4484 of file chan_iax2.c.

References AST_FRAME_IAX, iax_ie_data::buf, compress_subclass(), and iax_ie_data::pos.

Referenced by handle_call_token(), and socket_process().

04487 {
04488    struct {
04489       struct ast_iax2_full_hdr f;
04490       struct iax_ie_data ied;
04491    } data;
04492    size_t size = sizeof(struct ast_iax2_full_hdr);
04493 
04494    if (ied) {
04495       size += ied->pos;
04496       memcpy(&data.ied, ied->buf, ied->pos);
04497    }
04498 
04499    data.f.scallno = htons(0x8000 | callno);
04500    data.f.dcallno = htons(dcallno);
04501    data.f.ts = htonl(ts);
04502    data.f.iseqno = seqno;
04503    data.f.oseqno = 0;
04504    data.f.type = AST_FRAME_IAX;
04505    data.f.csub = compress_subclass(command);
04506 
04507    return sendto(sockfd, &data, size, 0, (struct sockaddr *)sin, sizeof(*sin));
04508 }

static int send_command ( struct chan_iax2_pvt *  i,
char  type,
int  command,
unsigned int  ts,
const unsigned char *  data,
int  datalen,
int  seqno 
) [static]

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 7176 of file chan_iax2.c.

References __send_command(), chan_iax2_pvt::callno, and iax2_predestroy().

Referenced by __auth_reject(), __auto_hangup(), authenticate_request(), iax2_hangup(), socket_process(), and update_registry().

07177 {
07178    int call_num = i->callno;
07179    /* It is assumed that the callno has already been locked */
07180    iax2_predestroy(i->callno);
07181    if (!iaxs[call_num])
07182       return -1;
07183    return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 1);
07184 }

static int send_command_immediate ( struct chan_iax2_pvt *  i,
char  type,
int  command,
unsigned int  ts,
const unsigned char *  data,
int  datalen,
int  seqno 
) [static]

Definition at line 7186 of file chan_iax2.c.

References __send_command().

Referenced by iax2_vnak(), and socket_process().

07187 {
07188    return __send_command(i, type, command, ts, data, datalen, seqno, 1, 0, 0);
07189 }

static int send_command_locked ( unsigned short  callno,
char  type,
int  command,
unsigned int  ts,
const unsigned char *  data,
int  datalen,
int  seqno 
) [static]

Definition at line 7162 of file chan_iax2.c.

References ast_mutex_lock(), ast_mutex_unlock(), and send_command().

Referenced by iax2_answer(), iax2_digit_begin(), iax2_digit_end(), iax2_sendhtml(), iax2_sendimage(), iax2_sendtext(), iax2_setoption(), and iax2_transfer().

07163 {
07164    int res;
07165    ast_mutex_lock(&iaxsl[callno]);
07166    res = send_command(iaxs[callno], type, command, ts, data, datalen, seqno);
07167    ast_mutex_unlock(&iaxsl[callno]);
07168    return res;
07169 }

static int send_command_transfer ( struct chan_iax2_pvt *  i,
char  type,
int  command,
unsigned int  ts,
const unsigned char *  data,
int  datalen 
) [static]

Definition at line 7191 of file chan_iax2.c.

References __send_command().

Referenced by socket_process(), and try_transfer().

07192 {
07193    return __send_command(i, type, command, ts, data, datalen, 0, 0, 1, 0);
07194 }

static int send_lagrq ( const void *  data  )  [static]

Definition at line 1455 of file chan_iax2.c.

References __send_lagrq(), and schedule_action.

Referenced by __find_callno(), __send_lagrq(), and make_trunk().

01456 {
01457 #ifdef SCHED_MULTITHREADED
01458    if (schedule_action(__send_lagrq, data))
01459 #endif      
01460       __send_lagrq(data);
01461    
01462    return 0;
01463 }

static int send_packet ( struct iax_frame *  f  )  [static]

Definition at line 3165 of file chan_iax2.c.

References chan_iax2_pvt::addr, iax2_trunk_peer::addr, ast_debug, ast_inet_ntoa(), iax_frame::callno, iax_frame::data, iax_frame::datalen, errno, handle_error(), iax_showframe(), chan_iax2_pvt::peercallno, iax2_trunk_peer::sockfd, chan_iax2_pvt::transfer, transfer, iax_frame::transfer, and iax_frame::ts.

Referenced by __attempt_transmit(), iax2_send(), network_thread(), and vnak_retransmit().

03166 {
03167    int res;
03168    int callno = f->callno;
03169 
03170    /* Don't send if there was an error, but return error instead */
03171    if (!callno || !iaxs[callno] || iaxs[callno]->error)
03172        return -1;
03173    
03174    /* Called with iaxsl held */
03175    if (iaxdebug)
03176       ast_debug(3, "Sending %d on %d/%d to %s:%d\n", f->ts, callno, iaxs[callno]->peercallno, ast_inet_ntoa(iaxs[callno]->addr.sin_addr), ntohs(iaxs[callno]->addr.sin_port));
03177    
03178    if (f->transfer) {
03179       if (iaxdebug)
03180          iax_showframe(f, NULL, 0, &iaxs[callno]->transfer, f->datalen - sizeof(struct ast_iax2_full_hdr));
03181       res = sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[callno]->transfer, sizeof(iaxs[callno]->transfer));
03182    } else {
03183       if (iaxdebug)
03184          iax_showframe(f, NULL, 0, &iaxs[callno]->addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
03185       res = sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[callno]->addr, sizeof(iaxs[callno]->addr));
03186    }
03187    if (res < 0) {
03188       if (iaxdebug)
03189          ast_debug(1, "Received error: %s\n", strerror(errno));
03190       handle_error();
03191    } else
03192       res = 0;
03193 
03194    return res;
03195 }

static int send_ping ( const void *  data  )  [static]

Definition at line 1394 of file chan_iax2.c.

References __send_ping(), and schedule_action.

Referenced by __find_callno(), __send_ping(), and make_trunk().

01395 {
01396 #ifdef SCHED_MULTITHREADED
01397    if (schedule_action(__send_ping, data))
01398 #endif      
01399       __send_ping(data);
01400 
01401    return 0;
01402 }

static void send_signaling ( struct chan_iax2_pvt *  pvt  )  [static]

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.

Definition at line 1679 of file chan_iax2.c.

References AST_LIST_REMOVE_HEAD, signaling_queue_entry::f, free_signaling_queue_entry(), chan_iax2_pvt::hold_signaling, iax2_send(), s, and chan_iax2_pvt::signaling_queue.

Referenced by socket_process().

01680 {
01681    struct signaling_queue_entry *s = NULL;
01682 
01683    while ((s = AST_LIST_REMOVE_HEAD(&pvt->signaling_queue, next))) {
01684       iax2_send(pvt, &s->f, 0, -1, 0, 0, 0);
01685       free_signaling_queue_entry(s);
01686    }
01687    pvt->hold_signaling = 0;
01688 }

static int send_trunk ( struct iax2_trunk_peer *  tpeer,
struct timeval *  now 
) [static]

Definition at line 8746 of file chan_iax2.c.

References iax2_trunk_peer::addr, iax_frame::afdata, ast_debug, ast_inet_ntoa(), ast_test_flag, calc_txpeerstamp(), iax2_trunk_peer::calls, ast_iax2_meta_hdr::cmddata, iax_frame::data, ast_iax2_meta_hdr::data, iax_frame::datalen, iax_frame::direction, DIRECTION_OUTGRESS, IAX_META_TRUNK, IAX_META_TRUNK_MINI, IAX_META_TRUNK_SUPERMINI, IAX_TRUNKTIMESTAMPS, ast_iax2_meta_hdr::metacmd, iax_frame::retrans, iax2_trunk_peer::sockfd, iax_frame::transfer, transmit_trunk(), iax2_trunk_peer::trunkdata, iax2_trunk_peer::trunkdatalen, ast_iax2_meta_trunk_hdr::ts, and ast_iax2_meta_hdr::zeros.

Referenced by iax2_trunk_queue(), and timing_read().

08747 {
08748    int res = 0;
08749    struct iax_frame *fr;
08750    struct ast_iax2_meta_hdr *meta;
08751    struct ast_iax2_meta_trunk_hdr *mth;
08752    int calls = 0;
08753    
08754    /* Point to frame */
08755    fr = (struct iax_frame *)tpeer->trunkdata;
08756    /* Point to meta data */
08757    meta = (struct ast_iax2_meta_hdr *)fr->afdata;
08758    mth = (struct ast_iax2_meta_trunk_hdr *)meta->data;
08759    if (tpeer->trunkdatalen) {
08760       /* We're actually sending a frame, so fill the meta trunk header and meta header */
08761       meta->zeros = 0;
08762       meta->metacmd = IAX_META_TRUNK;
08763       if (ast_test_flag(&globalflags, IAX_TRUNKTIMESTAMPS))
08764          meta->cmddata = IAX_META_TRUNK_MINI;
08765       else
08766          meta->cmddata = IAX_META_TRUNK_SUPERMINI;
08767       mth->ts = htonl(calc_txpeerstamp(tpeer, trunkfreq, now));
08768       /* And the rest of the ast_iax2 header */
08769       fr->direction = DIRECTION_OUTGRESS;
08770       fr->retrans = -1;
08771       fr->transfer = 0;
08772       /* Any appropriate call will do */
08773       fr->data = fr->afdata;
08774       fr->datalen = tpeer->trunkdatalen + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr);
08775       res = transmit_trunk(fr, &tpeer->addr, tpeer->sockfd);
08776       calls = tpeer->calls;
08777 #if 0
08778       ast_debug(1, "Trunking %d call chunks in %d bytes to %s:%d, ts=%d\n", calls, fr->datalen, ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), ntohl(mth->ts));
08779 #endif      
08780       /* Reset transmit trunk side data */
08781       tpeer->trunkdatalen = 0;
08782       tpeer->calls = 0;
08783    }
08784    if (res < 0)
08785       return res;
08786    return calls;
08787 }

static int set_config ( const char *  config_file,
int  reload 
) [static]

Load configuration.

Definition at line 12514 of file chan_iax2.c.

References add_calltoken_ignore(), ao2_link, ast_category_browse(), ast_cdr_amaflags2int(), ast_clear_flag, ast_config_destroy(), ast_config_load, ast_context_find_or_create(), ast_copy_string(), ast_false(), AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_netsock_bind(), ast_netsock_init(), ast_netsock_list_alloc(), ast_netsock_release(), ast_netsock_sockfd(), ast_netsock_unref(), ast_parse_allow_disallow(), ast_set2_flag, ast_set_flag, ast_set_flags_to, ast_str2cos(), ast_str2tos(), ast_strlen_zero(), ast_test_flag, ast_true(), ast_variable_browse(), ast_variable_retrieve(), ast_verb, build_callno_limits(), build_peer(), build_user(), capability, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, DEFAULT_MAXMS, errno, format, gen, get_encrypt_methods(), iax2_register(), IAX_ALLOWFWDOWNLOAD, IAX_CAPABILITY_FULLBANDWIDTH, IAX_CAPABILITY_LOWBANDWIDTH, IAX_CAPABILITY_MEDBANDWIDTH, IAX_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, IAX_DEFAULT_PORTNO, IAX_DEFAULT_REG_EXPIRE, IAX_DYNAMIC, IAX_FORCE_ENCRYPT, IAX_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, IAX_RTIGNOREREGEXPIRE, IAX_RTUPDATE, IAX_SHRINKCALLERID, IAX_TRANSFERMEDIA, IAX_TRUNKTIMESTAMPS, IAX_USEJITTERBUF, iaxmaxthreadcount, iaxthreadcount, ast_variable::lineno, LOG_ERROR, LOG_NOTICE, LOG_WARNING, MAX_TRUNK_MTU, MAX_TRUNKDATA, ast_variable::name, ast_variable::next, peer_unref(), prefs, qos, reg_source_db(), secret, set_config_destroy(), socket_read(), user_unref(), and ast_variable::value.

Referenced by load_module(), and reload_config().

12515 {
12516    struct ast_config *cfg, *ucfg;
12517    int capability=iax2_capability;
12518    struct ast_variable *v;
12519    char *cat;
12520    const char *utype;
12521    const char *tosval;
12522    int format;
12523    int portno = IAX_DEFAULT_PORTNO;
12524    int  x;
12525    int mtuv; 
12526    struct iax2_user *user;
12527    struct iax2_peer *peer;
12528    struct ast_netsock *ns;
12529    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
12530 #if 0
12531    static unsigned short int last_port=0;
12532 #endif
12533 
12534    cfg = ast_config_load(config_file, config_flags);
12535 
12536    if (!cfg) {
12537       ast_log(LOG_ERROR, "Unable to load config %s\n", config_file);
12538       return -1;
12539    } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
12540       ucfg = ast_config_load("users.conf", config_flags);
12541       if (ucfg == CONFIG_STATUS_FILEUNCHANGED)
12542          return 0;
12543       /* Otherwise we need to reread both files */
12544       ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
12545       if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEINVALID) {
12546          ast_log(LOG_ERROR, "Config file %s is in an invalid format.  Aborting.\n", config_file);
12547          ast_config_destroy(ucfg);
12548          return 0;
12549       }
12550    } else if (cfg == CONFIG_STATUS_FILEINVALID) {
12551       ast_log(LOG_ERROR, "Config file %s is in an invalid format.  Aborting.\n", config_file);
12552       return 0;
12553    } else { /* iax.conf changed, gotta reread users.conf, too */
12554       ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
12555       if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
12556          ast_log(LOG_ERROR, "Config file users.conf is in an invalid format.  Aborting.\n");
12557          ast_config_destroy(cfg);
12558          return 0;
12559       }
12560    }
12561 
12562    if (reload) {
12563       set_config_destroy();
12564    }
12565 
12566    /* Reset global codec prefs */   
12567    memset(&prefs, 0 , sizeof(struct ast_codec_pref));
12568    
12569    /* Reset Global Flags */
12570    memset(&globalflags, 0, sizeof(globalflags));
12571    ast_set_flag(&globalflags, IAX_RTUPDATE);
12572    ast_set_flag(&globalflags, IAX_SHRINKCALLERID);
12573 
12574 #ifdef SO_NO_CHECK
12575    nochecksums = 0;
12576 #endif
12577    /* Reset default parking lot */
12578    default_parkinglot[0] = '\0';
12579 
12580    min_reg_expire = IAX_DEFAULT_REG_EXPIRE;
12581    max_reg_expire = IAX_DEFAULT_REG_EXPIRE;
12582    global_max_trunk_mtu = MAX_TRUNK_MTU;
12583    global_maxcallno = DEFAULT_MAXCALLNO_LIMIT;
12584    global_maxcallno_nonval = DEFAULT_MAXCALLNO_LIMIT_NONVAL;
12585 
12586    maxauthreq = 3;
12587 
12588    srvlookup = 0;
12589 
12590    v = ast_variable_browse(cfg, "general");
12591 
12592    /* Seed initial tos value */
12593    tosval = ast_variable_retrieve(cfg, "general", "tos");
12594    if (tosval) {
12595       if (ast_str2tos(tosval, &qos.tos))
12596          ast_log(LOG_WARNING, "Invalid tos value, refer to QoS documentation\n");
12597    }
12598    /* Seed initial cos value */
12599    tosval = ast_variable_retrieve(cfg, "general", "cos");
12600    if (tosval) {
12601       if (ast_str2cos(tosval, &qos.cos))
12602          ast_log(LOG_WARNING, "Invalid cos value, refer to QoS documentation\n");
12603    }
12604    while(v) {
12605       if (!strcasecmp(v->name, "bindport")){ 
12606          if (reload)
12607             ast_log(LOG_NOTICE, "Ignoring bindport on reload\n");
12608          else
12609             portno = atoi(v->value);
12610       } else if (!strcasecmp(v->name, "pingtime")) 
12611          ping_time = atoi(v->value);
12612       else if (!strcasecmp(v->name, "iaxthreadcount")) {
12613          if (reload) {
12614             if (atoi(v->value) != iaxthreadcount)
12615                ast_log(LOG_NOTICE, "Ignoring any changes to iaxthreadcount during reload\n");
12616          } else {
12617             iaxthreadcount = atoi(v->value);
12618             if (iaxthreadcount < 1) {
12619                ast_log(LOG_NOTICE, "iaxthreadcount must be at least 1.\n");
12620                iaxthreadcount = 1;
12621             } else if (iaxthreadcount > 256) {
12622                ast_log(LOG_NOTICE, "limiting iaxthreadcount to 256\n");
12623                iaxthreadcount = 256;
12624             }
12625          }
12626       } else if (!strcasecmp(v->name, "iaxmaxthreadcount")) {
12627          if (reload) {
12628             AST_LIST_LOCK(&dynamic_list);
12629             iaxmaxthreadcount = atoi(v->value);
12630             AST_LIST_UNLOCK(&dynamic_list);
12631          } else {
12632             iaxmaxthreadcount = atoi(v->value);
12633             if (iaxmaxthreadcount < 0) {
12634                ast_log(LOG_NOTICE, "iaxmaxthreadcount must be at least 0.\n");
12635                iaxmaxthreadcount = 0;
12636             } else if (iaxmaxthreadcount > 256) {
12637                ast_log(LOG_NOTICE, "Limiting iaxmaxthreadcount to 256\n");
12638                iaxmaxthreadcount = 256;
12639             }
12640          }
12641       } else if (!strcasecmp(v->name, "nochecksums")) {
12642 #ifdef SO_NO_CHECK
12643          if (ast_true(v->value))
12644             nochecksums = 1;
12645          else
12646             nochecksums = 0;
12647 #else
12648          if (ast_true(v->value))
12649             ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
12650 #endif
12651       }
12652       else if (!strcasecmp(v->name, "maxjitterbuffer")) 
12653          maxjitterbuffer = atoi(v->value);
12654       else if (!strcasecmp(v->name, "resyncthreshold")) 
12655          resyncthreshold = atoi(v->value);
12656       else if (!strcasecmp(v->name, "maxjitterinterps")) 
12657          maxjitterinterps = atoi(v->value);
12658       else if (!strcasecmp(v->name, "jittertargetextra"))
12659          jittertargetextra = atoi(v->value);
12660       else if (!strcasecmp(v->name, "lagrqtime")) 
12661          lagrq_time = atoi(v->value);
12662       else if (!strcasecmp(v->name, "maxregexpire")) 
12663          max_reg_expire = atoi(v->value);
12664       else if (!strcasecmp(v->name, "minregexpire")) 
12665          min_reg_expire = atoi(v->value);
12666       else if (!strcasecmp(v->name, "bindaddr")) {
12667          if (reload) {
12668             ast_log(LOG_NOTICE, "Ignoring bindaddr on reload\n");
12669          } else {
12670             if (!(ns = ast_netsock_bind(netsock, io, v->value, portno, qos.tos, qos.cos, socket_read, NULL))) {
12671                ast_log(LOG_WARNING, "Unable apply binding to '%s' at line %d\n", v->value, v->lineno);
12672             } else {
12673                   if (strchr(v->value, ':'))
12674                   ast_verb(2, "Binding IAX2 to '%s'\n", v->value);
12675                   else
12676                   ast_verb(2, "Binding IAX2 to '%s:%d'\n", v->value, portno);
12677                if (defaultsockfd < 0) 
12678                   defaultsockfd = ast_netsock_sockfd(ns);
12679                ast_netsock_unref(ns);
12680             }
12681          }
12682       } else if (!strcasecmp(v->name, "authdebug")) {
12683          authdebug = ast_true(v->value);
12684       } else if (!strcasecmp(v->name, "encryption")) {
12685             iax2_encryption |= get_encrypt_methods(v->value);
12686             if (!iax2_encryption) {
12687                ast_clear_flag((&globalflags), IAX_FORCE_ENCRYPT);
12688             }
12689       } else if (!strcasecmp(v->name, "forceencryption")) {
12690          if (ast_false(v->value)) {
12691             ast_clear_flag((&globalflags), IAX_FORCE_ENCRYPT);
12692          } else {
12693             iax2_encryption |= get_encrypt_methods(v->value);
12694             if (iax2_encryption) {
12695                ast_set_flag((&globalflags), IAX_FORCE_ENCRYPT);
12696             }
12697          }
12698       } else if (!strcasecmp(v->name, "transfer")) {
12699          if (!strcasecmp(v->value, "mediaonly")) {
12700             ast_set_flags_to((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA); 
12701          } else if (ast_true(v->value)) {
12702             ast_set_flags_to((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
12703          } else 
12704             ast_set_flags_to((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
12705       } else if (!strcasecmp(v->name, "codecpriority")) {
12706          if(!strcasecmp(v->value, "caller"))
12707             ast_set_flag((&globalflags), IAX_CODEC_USER_FIRST);
12708          else if(!strcasecmp(v->value, "disabled"))
12709             ast_set_flag((&globalflags), IAX_CODEC_NOPREFS);
12710          else if(!strcasecmp(v->value, "reqonly")) {
12711             ast_set_flag((&globalflags), IAX_CODEC_NOCAP);
12712             ast_set_flag((&globalflags), IAX_CODEC_NOPREFS);
12713          }
12714       } else if (!strcasecmp(v->name, "jitterbuffer"))
12715          ast_set2_flag((&globalflags), ast_true(v->value), IAX_USEJITTERBUF); 
12716       else if (!strcasecmp(v->name, "forcejitterbuffer"))
12717          ast_set2_flag((&globalflags), ast_true(v->value), IAX_FORCEJITTERBUF);  
12718       else if (!strcasecmp(v->name, "delayreject"))
12719          delayreject = ast_true(v->value);
12720       else if (!strcasecmp(v->name, "allowfwdownload"))
12721          ast_set2_flag((&globalflags), ast_true(v->value), IAX_ALLOWFWDOWNLOAD);
12722       else if (!strcasecmp(v->name, "rtcachefriends"))
12723          ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTCACHEFRIENDS);  
12724       else if (!strcasecmp(v->name, "rtignoreregexpire"))
12725          ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTIGNOREREGEXPIRE);  
12726       else if (!strcasecmp(v->name, "rtupdate"))
12727          ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTUPDATE);
12728       else if (!strcasecmp(v->name, "trunktimestamps"))
12729          ast_set2_flag(&globalflags, ast_true(v->value), IAX_TRUNKTIMESTAMPS);
12730       else if (!strcasecmp(v->name, "rtautoclear")) {
12731          int i = atoi(v->value);
12732          if(i > 0)
12733             global_rtautoclear = i;
12734          else
12735             i = 0;
12736          ast_set2_flag((&globalflags), i || ast_true(v->value), IAX_RTAUTOCLEAR);   
12737       } else if (!strcasecmp(v->name, "trunkfreq")) {
12738          trunkfreq = atoi(v->value);
12739          if (trunkfreq < 10)
12740             trunkfreq = 10;
12741       } else if (!strcasecmp(v->name, "trunkmtu")) {
12742          mtuv = atoi(v->value);
12743          if (mtuv  == 0 )  
12744             global_max_trunk_mtu = 0; 
12745          else if (mtuv >= 172 && mtuv < 4000) 
12746             global_max_trunk_mtu = mtuv; 
12747          else 
12748             ast_log(LOG_NOTICE, "trunkmtu value out of bounds (%d) at line %d\n",
12749                mtuv, v->lineno);
12750       } else if (!strcasecmp(v->name, "trunkmaxsize")) {
12751          trunkmaxsize = atoi(v->value);
12752          if (trunkmaxsize == 0)
12753             trunkmaxsize = MAX_TRUNKDATA;
12754       } else if (!strcasecmp(v->name, "autokill")) {
12755          if (sscanf(v->value, "%30d", &x) == 1) {
12756             if (x >= 0)
12757                autokill = x;
12758             else
12759                ast_log(LOG_NOTICE, "Nice try, but autokill has to be >0 or 'yes' or 'no' at line %d\n", v->lineno);
12760          } else if (ast_true(v->value)) {
12761             autokill = DEFAULT_MAXMS;
12762          } else {
12763             autokill = 0;
12764          }
12765       } else if (!strcasecmp(v->name, "bandwidth")) {
12766          if (!strcasecmp(v->value, "low")) {
12767             capability = IAX_CAPABILITY_LOWBANDWIDTH;
12768          } else if (!strcasecmp(v->value, "medium")) {
12769             capability = IAX_CAPABILITY_MEDBANDWIDTH;
12770          } else if (!strcasecmp(v->value, "high")) {
12771             capability = IAX_CAPABILITY_FULLBANDWIDTH;
12772          } else
12773             ast_log(LOG_WARNING, "bandwidth must be either low, medium, or high\n");
12774       } else if (!strcasecmp(v->name, "allow")) {
12775          ast_parse_allow_disallow(&prefs, &capability, v->value, 1);
12776       } else if (!strcasecmp(v->name, "disallow")) {
12777          ast_parse_allow_disallow(&prefs, &capability, v->value, 0);
12778       } else if (!strcasecmp(v->name, "register")) {
12779          iax2_register(v->value, v->lineno);
12780       } else if (!strcasecmp(v->name, "iaxcompat")) {
12781          iaxcompat = ast_true(v->value);
12782       } else if (!strcasecmp(v->name, "regcontext")) {
12783          ast_copy_string(regcontext, v->value, sizeof(regcontext));
12784          /* Create context if it doesn't exist already */
12785          ast_context_find_or_create(NULL, NULL, regcontext, "IAX2");
12786       } else if (!strcasecmp(v->name, "tos")) {
12787          if (ast_str2tos(v->value, &qos.tos))
12788             ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
12789       } else if (!strcasecmp(v->name, "cos")) {
12790          if (ast_str2cos(v->value, &qos.cos))
12791             ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
12792       } else if (!strcasecmp(v->name, "parkinglot")) {
12793          ast_copy_string(default_parkinglot, v->value, sizeof(default_parkinglot));
12794       } else if (!strcasecmp(v->name, "accountcode")) {
12795          ast_copy_string(accountcode, v->value, sizeof(accountcode));
12796       } else if (!strcasecmp(v->name, "mohinterpret")) {
12797          ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
12798       } else if (!strcasecmp(v->name, "mohsuggest")) {
12799          ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
12800       } else if (!strcasecmp(v->name, "amaflags")) {
12801          format = ast_cdr_amaflags2int(v->value);
12802          if (format < 0) {
12803             ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
12804          } else {
12805             amaflags = format;
12806          }
12807       } else if (!strcasecmp(v->name, "language")) {
12808          ast_copy_string(language, v->value, sizeof(language));
12809       } else if (!strcasecmp(v->name, "maxauthreq")) {
12810          maxauthreq = atoi(v->value);
12811          if (maxauthreq < 0)
12812             maxauthreq = 0;
12813       } else if (!strcasecmp(v->name, "adsi")) {
12814          adsi = ast_true(v->value);
12815       } else if (!strcasecmp(v->name, "srvlookup")) {
12816          srvlookup = ast_true(v->value);
12817       } else if (!strcasecmp(v->name, "maxcallnumbers")) {
12818          if (sscanf(v->value, "%10hu", &global_maxcallno) != 1) {
12819             ast_log(LOG_WARNING, "maxcallnumbers must be set to a valid number.  %s is not valid at line %d\n", v->value, v->lineno);
12820          }
12821       } else if (!strcasecmp(v->name, "maxcallnumbers_nonvalidated")) {
12822          if (sscanf(v->value, "%10hu", &global_maxcallno_nonval) != 1) {
12823             ast_log(LOG_WARNING, "maxcallnumbers_nonvalidated must be set to a valid number.  %s is not valid at line %d.\n", v->value, v->lineno);
12824          }
12825       } else if (!strcasecmp(v->name, "calltokenoptional")) {
12826          if (add_calltoken_ignore(v->value)) {
12827             ast_log(LOG_WARNING, "Invalid calltokenoptional address range - '%s' line %d\n", v->value, v->lineno);
12828          }
12829       } else if (!strcasecmp(v->name, "shrinkcallerid")) {
12830          if (ast_true(v->value)) {
12831             ast_set_flag((&globalflags), IAX_SHRINKCALLERID);
12832          } else if (ast_false(v->value)) {
12833             ast_clear_flag((&globalflags), IAX_SHRINKCALLERID);
12834          } else {
12835             ast_log(LOG_WARNING, "shrinkcallerid value %s is not valid at line %d.\n", v->value, v->lineno);
12836          }
12837       }/*else if (strcasecmp(v->name,"type")) */
12838       /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
12839       v = v->next;
12840    }
12841    
12842    if (defaultsockfd < 0) {
12843       if (!(ns = ast_netsock_bind(netsock, io, "0.0.0.0", portno, qos.tos, qos.cos, socket_read, NULL))) {
12844          ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
12845       } else {
12846          ast_verb(2, "Binding IAX2 to default address 0.0.0.0:%d\n", portno);
12847          defaultsockfd = ast_netsock_sockfd(ns);
12848          ast_netsock_unref(ns);
12849       }
12850    }
12851    if (reload) {
12852       ast_netsock_release(outsock);
12853       outsock = ast_netsock_list_alloc();
12854       if (!outsock) {
12855          ast_log(LOG_ERROR, "Could not allocate outsock list.\n");
12856          return -1;
12857       }
12858       ast_netsock_init(outsock);
12859    }
12860 
12861    if (min_reg_expire > max_reg_expire) {
12862       ast_log(LOG_WARNING, "Minimum registration interval of %d is more than maximum of %d, resetting minimum to %d\n",
12863          min_reg_expire, max_reg_expire, max_reg_expire);
12864       min_reg_expire = max_reg_expire;
12865    }
12866    iax2_capability = capability;
12867    
12868    if (ucfg) {
12869       struct ast_variable *gen;
12870       int genhasiax;
12871       int genregisteriax;
12872       const char *hasiax, *registeriax;
12873       
12874       genhasiax = ast_true(ast_variable_retrieve(ucfg, "general", "hasiax"));
12875       genregisteriax = ast_true(ast_variable_retrieve(ucfg, "general", "registeriax"));
12876       gen = ast_variable_browse(ucfg, "general");
12877       cat = ast_category_browse(ucfg, NULL);
12878       while (cat) {
12879          if (strcasecmp(cat, "general")) {
12880             hasiax = ast_variable_retrieve(ucfg, cat, "hasiax");
12881             registeriax = ast_variable_retrieve(ucfg, cat, "registeriax");
12882             if (ast_true(hasiax) || (!hasiax && genhasiax)) {
12883                /* Start with general parameters, then specific parameters, user and peer */
12884                user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
12885                if (user) {
12886                   ao2_link(users, user);
12887                   user = user_unref(user);
12888                }
12889                peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
12890                if (peer) {
12891                   if (ast_test_flag(peer, IAX_DYNAMIC))
12892                      reg_source_db(peer);
12893                   ao2_link(peers, peer);
12894                   peer = peer_unref(peer);
12895                }
12896             }
12897             if (ast_true(registeriax) || (!registeriax && genregisteriax)) {
12898                char tmp[256];
12899                const char *host = ast_variable_retrieve(ucfg, cat, "host");
12900                const char *username = ast_variable_retrieve(ucfg, cat, "username");
12901                const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
12902                if (!host)
12903                   host = ast_variable_retrieve(ucfg, "general", "host");
12904                if (!username)
12905                   username = ast_variable_retrieve(ucfg, "general", "username");
12906                if (!secret)
12907                   secret = ast_variable_retrieve(ucfg, "general", "secret");
12908                if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
12909                   if (!ast_strlen_zero(secret))
12910                      snprintf(tmp, sizeof(tmp), "%s:%s@%s", username, secret, host);
12911                   else
12912                      snprintf(tmp, sizeof(tmp), "%s@%s", username, host);
12913                   iax2_register(tmp, 0);
12914                }
12915             }
12916          }
12917          cat = ast_category_browse(ucfg, cat);
12918       }
12919       ast_config_destroy(ucfg);
12920    }
12921    
12922    cat = ast_category_browse(cfg, NULL);
12923    while(cat) {
12924       if (strcasecmp(cat, "general")) {
12925          utype = ast_variable_retrieve(cfg, cat, "type");
12926          if (!strcasecmp(cat, "callnumberlimits")) {
12927             build_callno_limits(ast_variable_browse(cfg, cat));
12928          } else if (utype) {
12929             if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
12930                user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
12931                if (user) {
12932                   ao2_link(users, user);
12933                   user = user_unref(user);
12934                }
12935             }
12936             if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
12937                peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
12938                if (peer) {
12939                   if (ast_test_flag(peer, IAX_DYNAMIC))
12940                      reg_source_db(peer);
12941                   ao2_link(peers, peer);
12942                   peer = peer_unref(peer);
12943                }
12944             } else if (strcasecmp(utype, "user")) {
12945                ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config_file);
12946             }
12947          } else
12948             ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
12949       }
12950       cat = ast_category_browse(cfg, cat);
12951    }
12952    ast_config_destroy(cfg);
12953    return 1;
12954 }

static void set_config_destroy ( void   )  [static]

static void set_peercnt_limit ( struct peercnt *  peercnt  )  [static]

Definition at line 2091 of file chan_iax2.c.

References peercnt::addr, addr_range_match_address_cb(), ao2_callback, ao2_ref, ast_debug, ast_inet_ntoa(), addr_range::limit, peercnt::limit, and peercnt::reg.

Referenced by peercnt_add(), peercnt_modify(), and set_peercnt_limit_all_cb().

02092 {
02093    uint16_t limit = global_maxcallno;
02094    struct addr_range *addr_range;
02095    struct sockaddr_in sin = {
02096       .sin_addr.s_addr = peercnt->addr,
02097    };
02098 
02099 
02100    if (peercnt->reg && peercnt->limit) {
02101       return; /* this peercnt has a custom limit set by a registration */
02102    }
02103 
02104    if ((addr_range = ao2_callback(callno_limits, 0, addr_range_match_address_cb, &sin))) {
02105       limit = addr_range->limit;
02106       ast_debug(1, "custom addr_range %d found for %s\n", limit, ast_inet_ntoa(sin.sin_addr));
02107       ao2_ref(addr_range, -1);
02108    }
02109 
02110    peercnt->limit = limit;
02111 }

static int set_peercnt_limit_all_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 2117 of file chan_iax2.c.

References ast_debug, and set_peercnt_limit().

Referenced by reload_config().

02118 {
02119    struct peercnt *peercnt = obj;
02120 
02121    set_peercnt_limit(peercnt);
02122    ast_debug(1, "Reset limits for peercnts table\n");
02123 
02124    return 0;
02125 }

static void signal_condition ( ast_mutex_t *  lock,
ast_cond_t *  cond 
) [static]

static int socket_process ( struct iax2_thread *  thread  )  [static]

Definition at line 9469 of file chan_iax2.c.

References chan_iax2_pvt::addr, iax_frame::af, iax_frame::afdatalen, chan_iax2_pvt::aseqno, ast_aes_decrypt_key, ast_async_goto(), ast_best_codec(), ast_bridged_channel(), ast_calloc, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, AST_CAUSE_BUSY, AST_CAUSE_CONGESTION, AST_CAUSE_FACILITY_NOT_SUBSCRIBED, AST_CAUSE_NO_ROUTE_DESTINATION, ast_channel_datastore_add(), ast_channel_trylock, ast_channel_unlock, ast_clear_flag, ast_codec_choose(), ast_codec_get_samples(), ast_codec_pref_convert(), ast_codec_pref_index(), ast_codec_pref_string(), AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_HANGUP, AST_CONTROL_HOLD, AST_CONTROL_PROGRESS, AST_CONTROL_UNHOLD, ast_datastore_alloc, ast_datastore_free(), ast_debug, AST_DEVICE_NOT_INUSE, AST_DEVICE_UNAVAILABLE, ast_devstate_changed(), ast_exists_extension(), AST_FORMAT_SLINEAR, ast_frame_byteswap_be, AST_FRAME_CONTROL, AST_FRAME_IAX, AST_FRAME_NULL, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_free, ast_getformatname(), ast_iax2_new(), ast_inet_ntoa(), AST_LIST_HEAD, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_parking_ext(), ast_sched_thread_del, ast_set_flag, ast_set_read_format(), ast_set_write_format(), AST_STATE_RING, ast_strdupa, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_var_assign(), ast_variables_destroy(), ast_verb, auth_fail(), authenticate_reply(), authenticate_request(), authenticate_verify(), chan_iax2_pvt::authmethods, iax_ie_data::buf, iax2_thread::buf, iax2_thread::buf_len, CACHE_FLAG_TRANSMITTED, iax_frame::cacheable, calc_timestamp(), iax_ies::called_number, iax2_peer::callno, chan_iax2_pvt::callno, iax_frame::callno, iax_ies::calltoken, iax_ies::calltokendata, chan_iax2_pvt::capability, iax_ies::cause, iax_ies::causecode, iax_ies::challenge, check_access(), check_provisioning(), chan_iax2_pvt::chosenformat, chan_iax2_pvt::cid_num, cid_num, iax_ies::codec_prefs, complete_dpreply(), complete_transfer(), construct_rr(), context, chan_iax2_pvt::context, ast_iax2_full_hdr::csub, ast_datastore::data, ast_frame::data, ast_frame::datalen, DATASTORE_INHERIT_FOREVER, ast_iax2_full_hdr::dcallno, DEADLOCK_AVOIDANCE, decrypt_frame(), iax_ies::devicetype, dp_lookup(), chan_iax2_pvt::encmethods, iax_ies::encmethods, ast_var_t::entries, chan_iax2_pvt::error, EVENT_FLAG_CALL, EVENT_FLAG_SYSTEM, exists(), exten, chan_iax2_pvt::exten, iax_frame::final, find_callno(), chan_iax2_pvt::first_iax_message, iax2_dpcache::flags, iax_ies::format, format, chan_iax2_pvt::frames_received, ast_frame::frametype, iax_ies::fwdesc, handle_call_token(), ast_channel::hangupcause, iax2_peer::historicms, chan_iax2_pvt::hold_signaling, iax2_ack_registry(), iax2_allow_new(), iax2_destroy(), iax2_dprequest(), iax2_poke_peer_s(), iax2_queue_control_data(), iax2_queue_frame(), iax2_sched_add(), iax2_send(), iax2_vnak(), IAX_ALLOWFWDOWNLOAD, IAX_ALREADYGONE, IAX_AUTH_MD5, IAX_CALLENCRYPTED, IAX_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, IAX_COMMAND_ACCEPT, IAX_COMMAND_ACK, IAX_COMMAND_AUTHREP, IAX_COMMAND_AUTHREQ, IAX_COMMAND_CALLTOKEN, IAX_COMMAND_DIAL, IAX_COMMAND_DPREP, IAX_COMMAND_DPREQ, IAX_COMMAND_FWDATA, IAX_COMMAND_FWDOWNL, IAX_COMMAND_HANGUP, IAX_COMMAND_INVAL, IAX_COMMAND_LAGRP, IAX_COMMAND_LAGRQ, IAX_COMMAND_NEW, IAX_COMMAND_PING, IAX_COMMAND_POKE, IAX_COMMAND_PONG, IAX_COMMAND_QUELCH, IAX_COMMAND_REGACK, IAX_COMMAND_REGAUTH, IAX_COMMAND_REGREJ, IAX_COMMAND_REGREL, IAX_COMMAND_REGREQ, IAX_COMMAND_REJECT, IAX_COMMAND_RTKEY, IAX_COMMAND_TRANSFER, IAX_COMMAND_TXACC, IAX_COMMAND_TXCNT, IAX_COMMAND_TXMEDIA, IAX_COMMAND_TXREADY, IAX_COMMAND_TXREJ, IAX_COMMAND_TXREL, IAX_COMMAND_TXREQ, IAX_COMMAND_UNQUELCH, IAX_COMMAND_UNSUPPORT, IAX_COMMAND_VNAK, IAX_DEBUGDIGEST, IAX_DELAYPBXSTART, IAX_ENCRYPTED, iax_firmware_append(), IAX_FLAG_FULL, IAX_FLAG_RETRANS, IAX_FORCE_ENCRYPT, iax_frame_wrap(), iax_ie_append_byte(), iax_ie_append_int(), iax_ie_append_short(), iax_ie_append_str(), IAX_IE_CALLNO, IAX_IE_CAUSE, IAX_IE_CAUSECODE, IAX_IE_FORMAT, IAX_IE_IAX_UNKNOWN, IAX_IMMEDIATE, iax_outputframe(), iax_park(), iax_parse_ies(), IAX_PROVISION, IAX_QUELCH, IAX_STATE_AUTHENTICATED, IAX_STATE_STARTED, IAX_STATE_TBD, IAX_TRUNK, iax_ies::iax_unknown, iaxfrdup2(), chan_iax2_pvt::iaxvars, inaddrcmp(), ast_datastore::inheritance, chan_iax2_pvt::initid, chan_iax2_pvt::inkeys, iax2_thread::iofd, iax2_thread::iosin, chan_iax2_pvt::iseqno, iax_frame::iseqno, ast_iax2_full_hdr::iseqno, chan_iax2_pvt::last, chan_iax2_pvt::last_iax_message, iax2_peer::lastms, ast_frame::len, LOG_ERROR, log_jitterstats(), LOG_NOTICE, LOG_WARNING, make_trunk(), ast_frame::mallocd, manager_event, iax2_peer::maxms, merge_encryption(), ast_iax2_meta_hdr::metacmd, chan_iax2_pvt::mohsuggest, iax_ies::musiconhold, iax2_peer::name, ast_channel::name, ast_variable::name, ast_channel::nativeformats, NEW_ALLOW, NEW_ALLOW_CALLTOKEN_VALIDATED, NEW_PREVENT, ast_variable::next, ast_frame::offset, chan_iax2_pvt::oseqno, iax_frame::oseqno, ast_iax2_full_hdr::oseqno, iax_frame::outoforder, chan_iax2_pvt::owner, pbx_builtin_setvar_helper(), peer_ref(), peer_unref(), chan_iax2_pvt::peercallno, chan_iax2_pvt::peercapability, chan_iax2_pvt::peerformat, iax2_peer::pokeexpire, iax2_peer::pokefreqnotok, iax2_peer::pokefreqok, iax_ie_data::pos, chan_iax2_pvt::prefs, iax_ies::provver, iax_ies::provverpres, ast_frame::ptr, raw_hangup(), ast_channel::readformat, iax_ies::refresh, REG_STATE_REJECTED, register_verify(), registry_authrequest(), registry_rerequest(), remove_by_peercallno(), resend_with_token(), iax_frame::retries, chan_iax2_pvt::rprefs, chan_iax2_pvt::rseqno, S_OR, ast_frame::samples, save_osptoken(), save_rr(), ast_iax2_full_hdr::scallno, schedule_delivery(), chan_iax2_pvt::secret, send_apathetic_reply(), send_command(), send_command_final(), send_command_immediate(), send_command_transfer(), send_signaling(), iax_ies::serviceident, iax2_peer::smoothing, socket_process_meta(), spawn_dp_lookup(), ast_frame::src, chan_iax2_pvt::state, stop_stuff(), store_by_peercallno(), ast_frame::subclass, iax_frame::transfer, TRANSFER_BEGIN, TRANSFER_MBEGIN, TRANSFER_MEDIA, TRANSFER_MEDIAPASS, TRANSFER_MREADY, TRANSFER_READY, TRANSFER_RELEASED, chan_iax2_pvt::transferring, try_transfer(), iax_frame::ts, ast_iax2_full_hdr::ts, ast_iax2_full_hdr::type, uncompress_subclass(), ast_channel::uniqueid, update_registry(), iax_ies::username, ast_variable::value, var, iax_ies::vars, VERBOSE_PREFIX_4, chan_iax2_pvt::videoformat, vnak_retransmit(), chan_iax2_pvt::voiceformat, and ast_iax2_meta_hdr::zeros.

Referenced by handle_deferred_full_frames(), and iax2_process_thread().

09470 {
09471    struct sockaddr_in sin;
09472    int res;
09473    int updatehistory=1;
09474    int new = NEW_PREVENT;
09475    int dcallno = 0;
09476    char decrypted = 0;
09477    struct ast_iax2_full_hdr *fh = (struct ast_iax2_full_hdr *)thread->buf;
09478    struct ast_iax2_mini_hdr *mh = (struct ast_iax2_mini_hdr *)thread->buf;
09479    struct ast_iax2_meta_hdr *meta = (struct ast_iax2_meta_hdr *)thread->buf;
09480    struct ast_iax2_video_hdr *vh = (struct ast_iax2_video_hdr *)thread->buf;
09481    struct iax_frame *fr;
09482    struct iax_frame *cur;
09483    struct ast_frame f = { 0, };
09484    struct ast_channel *c = NULL;
09485    struct iax2_dpcache *dp;
09486    struct iax2_peer *peer;
09487    struct iax_ies ies;
09488    struct iax_ie_data ied0, ied1;
09489    int format;
09490    int fd;
09491    int exists;
09492    int minivid = 0;
09493    char empty[32]="";      /* Safety measure */
09494    struct iax_frame *duped_fr;
09495    char host_pref_buf[128];
09496    char caller_pref_buf[128];
09497    struct ast_codec_pref pref;
09498    char *using_prefs = "mine";
09499 
09500    /* allocate an iax_frame with 4096 bytes of data buffer */
09501    fr = alloca(sizeof(*fr) + 4096);
09502    memset(fr, 0, sizeof(*fr));
09503    fr->afdatalen = 4096; /* From alloca() above */
09504 
09505    /* Copy frequently used parameters to the stack */
09506    res = thread->buf_len;
09507    fd = thread->iofd;
09508    memcpy(&sin, &thread->iosin, sizeof(sin));
09509 
09510    if (res < sizeof(*mh)) {
09511       ast_log(LOG_WARNING, "midget packet received (%d of %d min)\n", res, (int) sizeof(*mh));
09512       return 1;
09513    }
09514    if ((vh->zeros == 0) && (ntohs(vh->callno) & 0x8000)) {
09515       if (res < sizeof(*vh)) {
09516          ast_log(LOG_WARNING, "Rejecting packet from '%s.%d' that is flagged as a video frame but is too short\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
09517          return 1;
09518       }
09519 
09520       /* This is a video frame, get call number */
09521       fr->callno = find_callno(ntohs(vh->callno) & ~0x8000, dcallno, &sin, new, fd, 0);
09522       minivid = 1;
09523    } else if ((meta->zeros == 0) && !(ntohs(meta->metacmd) & 0x8000))
09524       return socket_process_meta(res, meta, &sin, fd, fr);
09525 
09526 #ifdef DEBUG_SUPPORT
09527    if (res >= sizeof(*fh))
09528       iax_outputframe(NULL, fh, 1, &sin, res - sizeof(*fh));
09529 #endif
09530    if (ntohs(mh->callno) & IAX_FLAG_FULL) {
09531       if (res < sizeof(*fh)) {
09532          ast_log(LOG_WARNING, "Rejecting packet from '%s.%d' that is flagged as a full frame but is too short\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
09533          return 1;
09534       }
09535 
09536       /* Get the destination call number */
09537       dcallno = ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS;
09538 
09539 
09540       /* check to make sure this full frame isn't encrypted before we attempt
09541        * to look inside of it. If it is encrypted, decrypt it first. Its ok if the
09542        * callno is not found here, that just means one hasn't been allocated for
09543        * this connection yet. */
09544       if ((dcallno != 1) && (fr->callno = find_callno(ntohs(mh->callno) & ~IAX_FLAG_FULL, dcallno, &sin, NEW_PREVENT, fd, 1))) {
09545          ast_mutex_lock(&iaxsl[fr->callno]);
09546          if (iaxs[fr->callno] && ast_test_flag(iaxs[fr->callno], IAX_ENCRYPTED)) {
09547             if (decrypt_frame(fr->callno, fh, &f, &res)) {
09548                ast_log(LOG_NOTICE, "Packet Decrypt Failed!\n");
09549                ast_mutex_unlock(&iaxsl[fr->callno]);
09550                return 1;
09551             }
09552             decrypted = 1;
09553          }
09554          ast_mutex_unlock(&iaxsl[fr->callno]);
09555       }
09556 
09557       /* Retrieve the type and subclass */
09558       f.frametype = fh->type;
09559       if (f.frametype == AST_FRAME_VIDEO) {
09560          f.subclass = uncompress_subclass(fh->csub & ~0x40) | ((fh->csub >> 6) & 0x1);
09561       } else {
09562          f.subclass = uncompress_subclass(fh->csub);
09563       }
09564 
09565       /* Deal with POKE/PONG without allocating a callno */
09566       if (f.frametype == AST_FRAME_IAX && f.subclass == IAX_COMMAND_POKE) {
09567          /* Reply back with a PONG, but don't care about the result. */
09568          send_apathetic_reply(1, ntohs(fh->scallno), &sin, IAX_COMMAND_PONG, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
09569          return 1;
09570       } else if (f.frametype == AST_FRAME_IAX && f.subclass == IAX_COMMAND_ACK && dcallno == 1) {
09571          /* Ignore */
09572          return 1;
09573       }
09574 
09575       f.datalen = res - sizeof(*fh);
09576       if (f.datalen) {
09577          if (f.frametype == AST_FRAME_IAX) {
09578             if (iax_parse_ies(&ies, thread->buf + sizeof(struct ast_iax2_full_hdr), f.datalen)) {
09579                ast_log(LOG_WARNING, "Undecodable frame received from '%s'\n", ast_inet_ntoa(sin.sin_addr));
09580                return 1;
09581             }
09582             f.data.ptr = NULL;
09583             f.datalen = 0;
09584          } else {
09585             f.data.ptr = thread->buf + sizeof(struct ast_iax2_full_hdr);
09586             memset(&ies, 0, sizeof(ies));
09587          }
09588       } else {
09589          if (f.frametype == AST_FRAME_IAX)
09590             f.data.ptr = NULL;
09591          else
09592             f.data.ptr = empty;
09593          memset(&ies, 0, sizeof(ies));
09594       }
09595 
09596       if (!dcallno && iax2_allow_new(f.frametype, f.subclass, 1)) {
09597          /* only set NEW_ALLOW if calltoken checks out */
09598          if (handle_call_token(fh, &ies, &sin, fd)) {
09599             return 1;
09600          }
09601 
09602          if (ies.calltoken && ies.calltokendata) {
09603             /* if we've gotten this far, and the calltoken ie data exists,
09604              * then calltoken validation _MUST_ have taken place.  If calltoken
09605              * data is provided, it is always validated reguardless of any
09606              * calltokenoptional or requirecalltoken options */
09607             new = NEW_ALLOW_CALLTOKEN_VALIDATED;
09608          } else {
09609             new = NEW_ALLOW;
09610          }
09611       }
09612    } else {
09613       /* Don't know anything about it yet */
09614       f.frametype = AST_FRAME_NULL;
09615       f.subclass = 0;
09616    }
09617 
09618    if (!fr->callno) {
09619       int check_dcallno = 0;
09620 
09621       /*
09622        * We enforce accurate destination call numbers for ACKs.  This forces the other
09623        * end to know the destination call number before call setup can complete.
09624        *
09625        * Discussed in the following thread:
09626        *    http://lists.digium.com/pipermail/asterisk-dev/2008-May/033217.html 
09627        */
09628 
09629       if ((ntohs(mh->callno) & IAX_FLAG_FULL) && ((f.frametype == AST_FRAME_IAX) && (f.subclass == IAX_COMMAND_ACK))) {
09630          check_dcallno = 1;
09631       }
09632 
09633       if (!(fr->callno = find_callno(ntohs(mh->callno) & ~IAX_FLAG_FULL, dcallno, &sin, new, fd, check_dcallno))) {
09634          if (f.frametype == AST_FRAME_IAX && f.subclass == IAX_COMMAND_NEW) {
09635             send_apathetic_reply(1, ntohs(fh->scallno), &sin, IAX_COMMAND_REJECT, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
09636          } else if (f.frametype == AST_FRAME_IAX && (f.subclass == IAX_COMMAND_REGREQ || f.subclass == IAX_COMMAND_REGREL)) {
09637             send_apathetic_reply(1, ntohs(fh->scallno), &sin, IAX_COMMAND_REGREJ, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
09638          }
09639          return 1;
09640       }
09641    }
09642 
09643    if (fr->callno > 0)
09644       ast_mutex_lock(&iaxsl[fr->callno]);
09645 
09646    if (!fr->callno || !iaxs[fr->callno]) {
09647       /* A call arrived for a nonexistent destination.  Unless it's an "inval"
09648          frame, reply with an inval */
09649       if (ntohs(mh->callno) & IAX_FLAG_FULL) {
09650          /* We can only raw hangup control frames */
09651          if (((f.subclass != IAX_COMMAND_INVAL) &&
09652              (f.subclass != IAX_COMMAND_TXCNT) &&
09653              (f.subclass != IAX_COMMAND_TXACC) &&
09654              (f.subclass != IAX_COMMAND_FWDOWNL))||
09655              (f.frametype != AST_FRAME_IAX))
09656             raw_hangup(&sin, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS, ntohs(mh->callno) & ~IAX_FLAG_FULL,
09657             fd);
09658       }
09659       if (fr->callno > 0) 
09660          ast_mutex_unlock(&iaxsl[fr->callno]);
09661       return 1;
09662    }
09663    if (ast_test_flag(iaxs[fr->callno], IAX_ENCRYPTED) && !decrypted) {
09664       if (decrypt_frame(fr->callno, fh, &f, &res)) {
09665          ast_log(LOG_NOTICE, "Packet Decrypt Failed!\n");
09666          ast_mutex_unlock(&iaxsl[fr->callno]);
09667          return 1;
09668       }
09669       decrypted = 1;
09670    }
09671 #ifdef DEBUG_SUPPORT
09672    if (decrypted) {
09673       iax_outputframe(NULL, fh, 3, &sin, res - sizeof(*fh));
09674    }
09675 #endif
09676 
09677    /* count this frame */
09678    iaxs[fr->callno]->frames_received++;
09679 
09680    if (!inaddrcmp(&sin, &iaxs[fr->callno]->addr) && !minivid &&
09681       f.subclass != IAX_COMMAND_TXCNT &&     /* for attended transfer */
09682       f.subclass != IAX_COMMAND_TXACC) {     /* for attended transfer */
09683       unsigned short new_peercallno;
09684       
09685       new_peercallno = (unsigned short) (ntohs(mh->callno) & ~IAX_FLAG_FULL);
09686       if (new_peercallno && new_peercallno != iaxs[fr->callno]->peercallno) {
09687          if (iaxs[fr->callno]->peercallno) {
09688             remove_by_peercallno(iaxs[fr->callno]);
09689          }
09690          iaxs[fr->callno]->peercallno = new_peercallno;
09691          store_by_peercallno(iaxs[fr->callno]);
09692       }
09693    }
09694    if (ntohs(mh->callno) & IAX_FLAG_FULL) {
09695       if (iaxdebug)
09696          ast_debug(1, "Received packet %d, (%d, %d)\n", fh->oseqno, f.frametype, f.subclass);
09697       /* Check if it's out of order (and not an ACK or INVAL) */
09698       fr->oseqno = fh->oseqno;
09699       fr->iseqno = fh->iseqno;
09700       fr->ts = ntohl(fh->ts);
09701 #ifdef IAXTESTS
09702       if (test_resync) {
09703          ast_debug(1, "Simulating frame ts resync, was %u now %u\n", fr->ts, fr->ts + test_resync);
09704          fr->ts += test_resync;
09705       }
09706 #endif /* IAXTESTS */
09707 #if 0
09708       if ( (ntohs(fh->dcallno) & IAX_FLAG_RETRANS) ||
09709            ( (f.frametype != AST_FRAME_VOICE) && ! (f.frametype == AST_FRAME_IAX &&
09710                         (f.subclass == IAX_COMMAND_NEW ||
09711                          f.subclass == IAX_COMMAND_AUTHREQ ||
09712                          f.subclass == IAX_COMMAND_ACCEPT ||
09713                          f.subclass == IAX_COMMAND_REJECT))      ) )
09714 #endif
09715       if ((ntohs(fh->dcallno) & IAX_FLAG_RETRANS) || (f.frametype != AST_FRAME_VOICE))
09716          updatehistory = 0;
09717       if ((iaxs[fr->callno]->iseqno != fr->oseqno) &&
09718          (iaxs[fr->callno]->iseqno ||
09719             ((f.subclass != IAX_COMMAND_TXCNT) &&
09720             (f.subclass != IAX_COMMAND_TXREADY) &&    /* for attended transfer */
09721             (f.subclass != IAX_COMMAND_TXREL) &&      /* for attended transfer */
09722             (f.subclass != IAX_COMMAND_UNQUELCH ) &&  /* for attended transfer */
09723             (f.subclass != IAX_COMMAND_TXACC)) ||
09724             (f.frametype != AST_FRAME_IAX))) {
09725          if (
09726           ((f.subclass != IAX_COMMAND_ACK) &&
09727            (f.subclass != IAX_COMMAND_INVAL) &&
09728            (f.subclass != IAX_COMMAND_TXCNT) &&
09729            (f.subclass != IAX_COMMAND_TXREADY) &&     /* for attended transfer */
09730            (f.subclass != IAX_COMMAND_TXREL) &&    /* for attended transfer */
09731            (f.subclass != IAX_COMMAND_UNQUELCH ) &&   /* for attended transfer */
09732            (f.subclass != IAX_COMMAND_TXACC) &&
09733            (f.subclass != IAX_COMMAND_VNAK)) ||
09734            (f.frametype != AST_FRAME_IAX)) {
09735             /* If it's not an ACK packet, it's out of order. */
09736             ast_debug(1, "Packet arrived out of order (expecting %d, got %d) (frametype = %d, subclass = %d)\n", 
09737                iaxs[fr->callno]->iseqno, fr->oseqno, f.frametype, f.subclass);
09738             /* Check to see if we need to request retransmission,
09739              * and take sequence number wraparound into account */
09740             if ((unsigned char) (iaxs[fr->callno]->iseqno - fr->oseqno) < 128) {
09741                /* If we've already seen it, ack it XXX There's a border condition here XXX */
09742                if ((f.frametype != AST_FRAME_IAX) || 
09743                      ((f.subclass != IAX_COMMAND_ACK) && (f.subclass != IAX_COMMAND_INVAL))) {
09744                   ast_debug(1, "Acking anyway\n");
09745                   /* XXX Maybe we should handle its ack to us, but then again, it's probably outdated anyway, and if
09746                      we have anything to send, we'll retransmit and get an ACK back anyway XXX */
09747                   send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
09748                }
09749             } else {
09750                /* Send a VNAK requesting retransmission */
09751                iax2_vnak(fr->callno);
09752             }
09753             ast_mutex_unlock(&iaxsl[fr->callno]);
09754             return 1;
09755          }
09756       } else {
09757          /* Increment unless it's an ACK or VNAK */
09758          if (((f.subclass != IAX_COMMAND_ACK) &&
09759              (f.subclass != IAX_COMMAND_INVAL) &&
09760              (f.subclass != IAX_COMMAND_TXCNT) &&
09761              (f.subclass != IAX_COMMAND_TXACC) &&
09762             (f.subclass != IAX_COMMAND_VNAK)) ||
09763              (f.frametype != AST_FRAME_IAX))
09764             iaxs[fr->callno]->iseqno++;
09765       }
09766       /* Ensure text frames are NULL-terminated */
09767       if (f.frametype == AST_FRAME_TEXT && thread->buf[res - 1] != '\0') {
09768          if (res < thread->buf_size)
09769             thread->buf[res++] = '\0';
09770          else /* Trims one character from the text message, but that's better than overwriting the end of the buffer. */
09771             thread->buf[res - 1] = '\0';
09772       }
09773 
09774       /* Handle implicit ACKing unless this is an INVAL, and only if this is 
09775          from the real peer, not the transfer peer */
09776       if (!inaddrcmp(&sin, &iaxs[fr->callno]->addr) && 
09777           ((f.subclass != IAX_COMMAND_INVAL) ||
09778            (f.frametype != AST_FRAME_IAX))) {
09779          unsigned char x;
09780          int call_to_destroy;
09781          /* First we have to qualify that the ACKed value is within our window */
09782          if (iaxs[fr->callno]->rseqno >= iaxs[fr->callno]->oseqno || (fr->iseqno >= iaxs[fr->callno]->rseqno && fr->iseqno < iaxs[fr->callno]->oseqno))
09783             x = fr->iseqno;
09784          else 
09785             x = iaxs[fr->callno]->oseqno;
09786          if ((x != iaxs[fr->callno]->oseqno) || (iaxs[fr->callno]->oseqno == fr->iseqno)) {
09787             /* The acknowledgement is within our window.  Time to acknowledge everything
09788                that it says to */
09789             for (x=iaxs[fr->callno]->rseqno; x != fr->iseqno; x++) {
09790                /* Ack the packet with the given timestamp */
09791                if (iaxdebug)
09792                   ast_debug(1, "Cancelling transmission of packet %d\n", x);
09793                call_to_destroy = 0;
09794                AST_LIST_LOCK(&frame_queue);
09795                AST_LIST_TRAVERSE(&frame_queue, cur, list) {
09796                   /* If it's our call, and our timestamp, mark -1 retries */
09797                   if ((fr->callno == cur->callno) && (x == cur->oseqno)) {
09798                      cur->retries = -1;
09799                      /* Destroy call if this is the end */
09800                      if (cur->final)
09801                         call_to_destroy = fr->callno;
09802                   }
09803                }
09804                AST_LIST_UNLOCK(&frame_queue);
09805                if (call_to_destroy) {
09806                   if (iaxdebug)
09807                      ast_debug(1, "Really destroying %d, having been acked on final message\n", call_to_destroy);
09808                   ast_mutex_lock(&iaxsl[call_to_destroy]);
09809                   iax2_destroy(call_to_destroy);
09810                   ast_mutex_unlock(&iaxsl[call_to_destroy]);
09811                }
09812             }
09813             /* Note how much we've received acknowledgement for */
09814             if (iaxs[fr->callno])
09815                iaxs[fr->callno]->rseqno = fr->iseqno;
09816             else {
09817                /* Stop processing now */
09818                ast_mutex_unlock(&iaxsl[fr->callno]);
09819                return 1;
09820             }
09821          } else {
09822             ast_debug(1, "Received iseqno %d not within window %d->%d\n", fr->iseqno, iaxs[fr->callno]->rseqno, iaxs[fr->callno]->oseqno);
09823          }
09824       }
09825       if (inaddrcmp(&sin, &iaxs[fr->callno]->addr) && 
09826          ((f.frametype != AST_FRAME_IAX) || 
09827           ((f.subclass != IAX_COMMAND_TXACC) &&
09828            (f.subclass != IAX_COMMAND_TXCNT)))) {
09829          /* Only messages we accept from a transfer host are TXACC and TXCNT */
09830          ast_mutex_unlock(&iaxsl[fr->callno]);
09831          return 1;
09832       }
09833 
09834       /* when we receive the first full frame for a new incoming channel,
09835          it is safe to start the PBX on the channel because we have now
09836          completed a 3-way handshake with the peer */
09837       if ((f.frametype == AST_FRAME_VOICE) ||
09838           (f.frametype == AST_FRAME_VIDEO) ||
09839           (f.frametype == AST_FRAME_IAX)) {
09840          if (ast_test_flag(iaxs[fr->callno], IAX_DELAYPBXSTART)) {
09841             ast_clear_flag(iaxs[fr->callno], IAX_DELAYPBXSTART);
09842             if (!ast_iax2_new(fr->callno, AST_STATE_RING, iaxs[fr->callno]->chosenformat)) {
09843                ast_mutex_unlock(&iaxsl[fr->callno]);
09844                return 1;
09845             }
09846          }
09847 
09848          if (ies.vars) {
09849             struct ast_datastore *variablestore = NULL;
09850             struct ast_variable *var, *prev = NULL;
09851             AST_LIST_HEAD(, ast_var_t) *varlist;
09852             if ((c = iaxs[fr->callno]->owner)) {
09853                varlist = ast_calloc(1, sizeof(*varlist));
09854                variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
09855 
09856                if (variablestore && varlist) {
09857                   variablestore->data = varlist;
09858                   variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
09859                   AST_LIST_HEAD_INIT(varlist);
09860                   ast_debug(1, "I can haz IAX vars?\n");
09861                   for (var = ies.vars; var; var = var->next) {
09862                      struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
09863                      if (prev) {
09864                         ast_free(prev);
09865                      }
09866                      prev = var;
09867                      if (!newvar) {
09868                         /* Don't abort list traversal, as this would leave ies.vars in an inconsistent state. */
09869                         ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
09870                      } else {
09871                         AST_LIST_INSERT_TAIL(varlist, newvar, entries);
09872                      }
09873                   }
09874                   if (prev) {
09875                      ast_free(prev);
09876                   }
09877                   ies.vars = NULL;
09878                   ast_channel_datastore_add(c, variablestore);
09879                } else {
09880                   ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
09881                   if (variablestore) {
09882                      ast_datastore_free(variablestore);
09883                   }
09884                   if (varlist) {
09885                      ast_free(varlist);
09886                   }
09887                }
09888             } else {
09889                /* No channel yet, so transfer the variables directly over to the pvt,
09890                 * for later inheritance. */
09891                ast_debug(1, "No channel, so populating IAXVARs to the pvt, as an intermediate step.\n");
09892                for (var = ies.vars; var && var->next; var = var->next);
09893                if (var) {
09894                   var->next = iaxs[fr->callno]->iaxvars;
09895                   iaxs[fr->callno]->iaxvars = ies.vars;
09896                   ies.vars = NULL;
09897                }
09898             }
09899          }
09900 
09901          if (ies.vars) {
09902             ast_debug(1, "I have IAX variables, but they were not processed\n");
09903          }
09904       }
09905 
09906       /* once we receive our first IAX Full Frame that is not CallToken related, send all
09907        * queued signaling frames that were being held. */
09908       if ((f.frametype == AST_FRAME_IAX) && (f.subclass != IAX_COMMAND_CALLTOKEN) && iaxs[fr->callno]->hold_signaling) {
09909          send_signaling(iaxs[fr->callno]);
09910       }
09911 
09912       if (f.frametype == AST_FRAME_VOICE) {
09913          if (f.subclass != iaxs[fr->callno]->voiceformat) {
09914                iaxs[fr->callno]->voiceformat = f.subclass;
09915                ast_debug(1, "Ooh, voice format changed to %d\n", f.subclass);
09916                if (iaxs[fr->callno]->owner) {
09917                   int orignative;
09918 retryowner:
09919                   if (ast_channel_trylock(iaxs[fr->callno]->owner)) {
09920                      DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]);
09921                      if (iaxs[fr->callno] && iaxs[fr->callno]->owner) goto retryowner;
09922                   }
09923                   if (iaxs[fr->callno]) {
09924                      if (iaxs[fr->callno]->owner) {
09925                         orignative = iaxs[fr->callno]->owner->nativeformats;
09926                         iaxs[fr->callno]->owner->nativeformats = f.subclass;
09927                         if (iaxs[fr->callno]->owner->readformat)
09928                            ast_set_read_format(iaxs[fr->callno]->owner, iaxs[fr->callno]->owner->readformat);
09929                         iaxs[fr->callno]->owner->nativeformats = orignative;
09930                         ast_channel_unlock(iaxs[fr->callno]->owner);
09931                      }
09932                   } else {
09933                      ast_debug(1, "Neat, somebody took away the channel at a magical time but i found it!\n");
09934                      /* Free remote variables (if any) */
09935                      if (ies.vars) {
09936                         ast_variables_destroy(ies.vars);
09937                         ast_debug(1, "I can haz iaxvars, but they is no good.  :-(\n");
09938                         ies.vars = NULL;
09939                      }
09940                      ast_mutex_unlock(&iaxsl[fr->callno]);
09941                      return 1;
09942                   }
09943                }
09944          }
09945       }
09946       if (f.frametype == AST_FRAME_VIDEO) {
09947          if (f.subclass != iaxs[fr->callno]->videoformat) {
09948             ast_debug(1, "Ooh, video format changed to %d\n", f.subclass & ~0x1);
09949             iaxs[fr->callno]->videoformat = f.subclass & ~0x1;
09950          }
09951       }
09952       if (f.frametype == AST_FRAME_CONTROL && iaxs[fr->callno]->owner) {
09953          if (f.subclass == AST_CONTROL_BUSY) {
09954             iaxs[fr->callno]->owner->hangupcause = AST_CAUSE_BUSY;
09955          } else if (f.subclass == AST_CONTROL_CONGESTION) {
09956             iaxs[fr->callno]->owner->hangupcause = AST_CAUSE_CONGESTION;
09957          }
09958       }
09959       if (f.frametype == AST_FRAME_IAX) {
09960          ast_sched_thread_del(sched, iaxs[fr->callno]->initid);
09961          /* Handle the IAX pseudo frame itself */
09962          if (iaxdebug)
09963             ast_debug(1, "IAX subclass %d received\n", f.subclass);
09964 
09965                         /* Update last ts unless the frame's timestamp originated with us. */
09966          if (iaxs[fr->callno]->last < fr->ts &&
09967                             f.subclass != IAX_COMMAND_ACK &&
09968                             f.subclass != IAX_COMMAND_PONG &&
09969                             f.subclass != IAX_COMMAND_LAGRP) {
09970             iaxs[fr->callno]->last = fr->ts;
09971             if (iaxdebug)
09972                ast_debug(1, "For call=%d, set last=%d\n", fr->callno, fr->ts);
09973          }
09974          iaxs[fr->callno]->last_iax_message = f.subclass;
09975          if (!iaxs[fr->callno]->first_iax_message) {
09976             iaxs[fr->callno]->first_iax_message = f.subclass;
09977          }
09978          switch(f.subclass) {
09979          case IAX_COMMAND_ACK:
09980             /* Do nothing */
09981             break;
09982          case IAX_COMMAND_QUELCH:
09983             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
09984                     /* Generate Manager Hold event, if necessary*/
09985                if (iaxs[fr->callno]->owner) {
09986                   manager_event(EVENT_FLAG_CALL, "Hold",
09987                      "Status: On\r\n"
09988                      "Channel: %s\r\n"
09989                      "Uniqueid: %s\r\n",
09990                      iaxs[fr->callno]->owner->name, 
09991                      iaxs[fr->callno]->owner->uniqueid);
09992                }
09993 
09994                ast_set_flag(iaxs[fr->callno], IAX_QUELCH);
09995                if (ies.musiconhold) {
09996                   if (iaxs[fr->callno]->owner && ast_bridged_channel(iaxs[fr->callno]->owner)) {
09997                      const char *moh_suggest = iaxs[fr->callno]->mohsuggest;
09998                      iax2_queue_control_data(fr->callno, AST_CONTROL_HOLD, 
09999                         S_OR(moh_suggest, NULL),
10000                         !ast_strlen_zero(moh_suggest) ? strlen(moh_suggest) + 1 : 0);
10001                      if (!iaxs[fr->callno]) {
10002                         ast_mutex_unlock(&iaxsl[fr->callno]);
10003                         return 1;
10004                      }
10005                   }
10006                }
10007             }
10008             break;
10009          case IAX_COMMAND_UNQUELCH:
10010             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
10011                     /* Generate Manager Unhold event, if necessary*/
10012                if (iaxs[fr->callno]->owner && ast_test_flag(iaxs[fr->callno], IAX_QUELCH)) {
10013                   manager_event(EVENT_FLAG_CALL, "Hold",
10014                      "Status: Off\r\n"
10015                      "Channel: %s\r\n"
10016                      "Uniqueid: %s\r\n",
10017                      iaxs[fr->callno]->owner->name, 
10018                      iaxs[fr->callno]->owner->uniqueid);
10019                }
10020 
10021                ast_clear_flag(iaxs[fr->callno], IAX_QUELCH);
10022                if (iaxs[fr->callno]->owner && ast_bridged_channel(iaxs[fr->callno]->owner)) {
10023                   iax2_queue_control_data(fr->callno, AST_CONTROL_UNHOLD, NULL, 0);
10024                   if (!iaxs[fr->callno]) {
10025                      ast_mutex_unlock(&iaxsl[fr->callno]);
10026                      return 1;
10027                   }
10028                }
10029             }
10030             break;
10031          case IAX_COMMAND_TXACC:
10032             if (iaxs[fr->callno]->transferring == TRANSFER_BEGIN) {
10033                /* Ack the packet with the given timestamp */
10034                AST_LIST_LOCK(&frame_queue);
10035                AST_LIST_TRAVERSE(&frame_queue, cur, list) {
10036                   /* Cancel any outstanding txcnt's */
10037                   if ((fr->callno == cur->callno) && (cur->transfer))
10038                      cur->retries = -1;
10039                }
10040                AST_LIST_UNLOCK(&frame_queue);
10041                memset(&ied1, 0, sizeof(ied1));
10042                iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->callno);
10043                send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREADY, 0, ied1.buf, ied1.pos, -1);
10044                iaxs[fr->callno]->transferring = TRANSFER_READY;
10045             }
10046             break;
10047          case IAX_COMMAND_NEW:
10048             /* Ignore if it's already up */
10049             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD))
10050                break;
10051             if (ies.provverpres && ies.serviceident && sin.sin_addr.s_addr) {
10052                ast_mutex_unlock(&iaxsl[fr->callno]);
10053                check_provisioning(&sin, fd, ies.serviceident, ies.provver);
10054                ast_mutex_lock(&iaxsl[fr->callno]);
10055                if (!iaxs[fr->callno]) {
10056                   ast_mutex_unlock(&iaxsl[fr->callno]);
10057                   return 1;
10058                }
10059             }
10060             /* If we're in trunk mode, do it now, and update the trunk number in our frame before continuing */
10061             if (ast_test_flag(iaxs[fr->callno], IAX_TRUNK)) {
10062                int new_callno;
10063                if ((new_callno = make_trunk(fr->callno, 1)) != -1)
10064                   fr->callno = new_callno;
10065             }
10066             /* For security, always ack immediately */
10067             if (delayreject)
10068                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10069             if (check_access(fr->callno, &sin, &ies)) {
10070                /* They're not allowed on */
10071                auth_fail(fr->callno, IAX_COMMAND_REJECT);
10072                if (authdebug)
10073                   ast_log(LOG_NOTICE, "Rejected connect attempt from %s, who was trying to reach '%s@%s'\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->exten, iaxs[fr->callno]->context);
10074                break;
10075             }
10076             if (ast_strlen_zero(iaxs[fr->callno]->secret) && ast_test_flag(iaxs[fr->callno], IAX_FORCE_ENCRYPT)) {
10077                auth_fail(fr->callno, IAX_COMMAND_REJECT);
10078                ast_log(LOG_WARNING, "Rejected connect attempt.  No secret present while force encrypt enabled.\n");
10079                break;
10080             }
10081             if (strcasecmp(iaxs[fr->callno]->exten, "TBD")) {
10082                const char *context, *exten, *cid_num;
10083 
10084                context = ast_strdupa(iaxs[fr->callno]->context);
10085                exten = ast_strdupa(iaxs[fr->callno]->exten);
10086                cid_num = ast_strdupa(iaxs[fr->callno]->cid_num);
10087 
10088                /* This might re-enter the IAX code and need the lock */
10089                ast_mutex_unlock(&iaxsl[fr->callno]);
10090                exists = ast_exists_extension(NULL, context, exten, 1, cid_num);
10091                ast_mutex_lock(&iaxsl[fr->callno]);
10092 
10093                if (!iaxs[fr->callno]) {
10094                   ast_mutex_unlock(&iaxsl[fr->callno]);
10095                   return 1;
10096                }
10097             } else
10098                exists = 0;
10099             /* Get OSP token if it does exist */
10100             save_osptoken(fr, &ies);
10101             if (ast_strlen_zero(iaxs[fr->callno]->secret) && ast_strlen_zero(iaxs[fr->callno]->inkeys)) {
10102                if (strcmp(iaxs[fr->callno]->exten, "TBD") && !exists) {
10103                   memset(&ied0, 0, sizeof(ied0));
10104                   iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
10105                   iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
10106                   send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10107                   if (!iaxs[fr->callno]) {
10108                      ast_mutex_unlock(&iaxsl[fr->callno]);
10109                      return 1;
10110                   }
10111                   if (authdebug)
10112                      ast_log(LOG_NOTICE, "Rejected connect attempt from %s, request '%s@%s' does not exist\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->exten, iaxs[fr->callno]->context);
10113                } else {
10114                   /* Select an appropriate format */
10115 
10116                   if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
10117                      if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
10118                         using_prefs = "reqonly";
10119                      } else {
10120                         using_prefs = "disabled";
10121                      }
10122                      format = iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability;
10123                      memset(&pref, 0, sizeof(pref));
10124                      strcpy(caller_pref_buf, "disabled");
10125                      strcpy(host_pref_buf, "disabled");
10126                   } else {
10127                      using_prefs = "mine";
10128                      /* If the information elements are in here... use them */
10129                      if (ies.codec_prefs)
10130                         ast_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
10131                      if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
10132                         /* If we are codec_first_choice we let the caller have the 1st shot at picking the codec.*/
10133                         if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
10134                            pref = iaxs[fr->callno]->rprefs;
10135                            using_prefs = "caller";
10136                         } else {
10137                            pref = iaxs[fr->callno]->prefs;
10138                         }
10139                      } else
10140                         pref = iaxs[fr->callno]->prefs;
10141                      
10142                      format = ast_codec_choose(&pref, iaxs[fr->callno]->capability & iaxs[fr->callno]->peercapability, 0);
10143                      ast_codec_pref_string(&iaxs[fr->callno]->rprefs, caller_pref_buf, sizeof(caller_pref_buf) - 1);
10144                      ast_codec_pref_string(&iaxs[fr->callno]->prefs, host_pref_buf, sizeof(host_pref_buf) - 1);
10145                   }
10146                   if (!format) {
10147                      if(!ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP))
10148                         format = iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability;
10149                      if (!format) {
10150                         memset(&ied0, 0, sizeof(ied0));
10151                         iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
10152                         iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
10153                         send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10154                         if (!iaxs[fr->callno]) {
10155                            ast_mutex_unlock(&iaxsl[fr->callno]);
10156                            return 1;
10157                         }
10158                         if (authdebug) {
10159                            if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP))
10160                               ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested 0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->capability);
10161                            else 
10162                               ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->peercapability, iaxs[fr->callno]->capability);
10163                         }
10164                      } else {
10165                         /* Pick one... */
10166                         if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
10167                            if(!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability))
10168                               format = 0;
10169                         } else {
10170                            if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
10171                               using_prefs = ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
10172                               memset(&pref, 0, sizeof(pref));
10173                               format = ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
10174                               strcpy(caller_pref_buf,"disabled");
10175                               strcpy(host_pref_buf,"disabled");
10176                            } else {
10177                               using_prefs = "mine";
10178                               if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
10179                                  /* Do the opposite of what we tried above. */
10180                                  if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
10181                                     pref = iaxs[fr->callno]->prefs;                       
10182                                  } else {
10183                                     pref = iaxs[fr->callno]->rprefs;
10184                                     using_prefs = "caller";
10185                                  }
10186                                  format = ast_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability, 1);
10187                            
10188                               } else /* if no codec_prefs IE do it the old way */
10189                                  format = ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability); 
10190                            }
10191                         }
10192 
10193                         if (!format) {
10194                            memset(&ied0, 0, sizeof(ied0));
10195                            iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
10196                            iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
10197                            ast_log(LOG_ERROR, "No best format in 0x%x???\n", iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
10198                            send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10199                            if (!iaxs[fr->callno]) {
10200                               ast_mutex_unlock(&iaxsl[fr->callno]);
10201                               return 1;
10202                            }
10203                            if (authdebug)
10204                               ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->peercapability, iaxs[fr->callno]->capability);
10205                            ast_set_flag(iaxs[fr->callno], IAX_ALREADYGONE);   
10206                            break;
10207                         }
10208                      }
10209                   }
10210                   if (format) {
10211                      /* No authentication required, let them in */
10212                      memset(&ied1, 0, sizeof(ied1));
10213                      iax_ie_append_int(&ied1, IAX_IE_FORMAT, format);
10214                      send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACCEPT, 0, ied1.buf, ied1.pos, -1);
10215                      if (strcmp(iaxs[fr->callno]->exten, "TBD")) {
10216                         ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
10217                         ast_verb(3, "Accepting UNAUTHENTICATED call from %s:\n"
10218                                     "%srequested format = %s,\n"
10219                                     "%srequested prefs = %s,\n"
10220                                     "%sactual format = %s,\n"
10221                                     "%shost prefs = %s,\n"
10222                                     "%spriority = %s\n",
10223                                     ast_inet_ntoa(sin.sin_addr), 
10224                                     VERBOSE_PREFIX_4,
10225                                     ast_getformatname(iaxs[fr->callno]->peerformat), 
10226                                     VERBOSE_PREFIX_4,
10227                                     caller_pref_buf,
10228                                     VERBOSE_PREFIX_4,
10229                                     ast_getformatname(format), 
10230                                     VERBOSE_PREFIX_4,
10231                                     host_pref_buf, 
10232                                     VERBOSE_PREFIX_4,
10233                                     using_prefs);
10234                         
10235                         iaxs[fr->callno]->chosenformat = format;
10236                         ast_set_flag(iaxs[fr->callno], IAX_DELAYPBXSTART);
10237                      } else {
10238                         ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
10239                         /* If this is a TBD call, we're ready but now what...  */
10240                         ast_verb(3, "Accepted unauthenticated TBD call from %s\n", ast_inet_ntoa(sin.sin_addr));
10241                      }
10242                   }
10243                }
10244                break;
10245             }
10246             if (iaxs[fr->callno]->authmethods & IAX_AUTH_MD5)
10247                merge_encryption(iaxs[fr->callno],ies.encmethods);
10248             else
10249                iaxs[fr->callno]->encmethods = 0;
10250             if (!authenticate_request(fr->callno) && iaxs[fr->callno])
10251                ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_AUTHENTICATED);
10252             if (!iaxs[fr->callno]) {
10253                ast_mutex_unlock(&iaxsl[fr->callno]);
10254                return 1;
10255             }
10256             break;
10257          case IAX_COMMAND_DPREQ:
10258             /* Request status in the dialplan */
10259             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD) &&
10260                !ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED) && ies.called_number) {
10261                if (iaxcompat) {
10262                   /* Spawn a thread for the lookup */
10263                   spawn_dp_lookup(fr->callno, iaxs[fr->callno]->context, ies.called_number, iaxs[fr->callno]->cid_num);
10264                } else {
10265                   /* Just look it up */
10266                   dp_lookup(fr->callno, iaxs[fr->callno]->context, ies.called_number, iaxs[fr->callno]->cid_num, 1);
10267                }
10268             }
10269             break;
10270          case IAX_COMMAND_HANGUP:
10271             ast_set_flag(iaxs[fr->callno], IAX_ALREADYGONE);
10272             ast_debug(1, "Immediately destroying %d, having received hangup\n", fr->callno);
10273             /* Set hangup cause according to remote */
10274             if (ies.causecode && iaxs[fr->callno]->owner)
10275                iaxs[fr->callno]->owner->hangupcause = ies.causecode;
10276             /* Send ack immediately, before we destroy */
10277             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10278             iax2_destroy(fr->callno);
10279             break;
10280          case IAX_COMMAND_REJECT:
10281             /* Set hangup cause according to remote */
10282             if (ies.causecode && iaxs[fr->callno]->owner)
10283                iaxs[fr->callno]->owner->hangupcause = ies.causecode;
10284 
10285             if (!ast_test_flag(iaxs[fr->callno], IAX_PROVISION)) {
10286                if (iaxs[fr->callno]->owner && authdebug)
10287                   ast_log(LOG_WARNING, "Call rejected by %s: %s\n",
10288                      ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr),
10289                      ies.cause ? ies.cause : "<Unknown>");
10290                ast_debug(1, "Immediately destroying %d, having received reject\n",
10291                   fr->callno);
10292             }
10293             /* Send ack immediately, before we destroy */
10294             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK,
10295                          fr->ts, NULL, 0, fr->iseqno);
10296             if (!ast_test_flag(iaxs[fr->callno], IAX_PROVISION))
10297                iaxs[fr->callno]->error = EPERM;
10298             iax2_destroy(fr->callno);
10299             break;
10300          case IAX_COMMAND_TRANSFER:
10301          {
10302             struct ast_channel *bridged_chan;
10303 
10304             if (iaxs[fr->callno]->owner && (bridged_chan = ast_bridged_channel(iaxs[fr->callno]->owner)) && ies.called_number) {
10305                /* Set BLINDTRANSFER channel variables */
10306 
10307                ast_mutex_unlock(&iaxsl[fr->callno]);
10308                pbx_builtin_setvar_helper(iaxs[fr->callno]->owner, "BLINDTRANSFER", bridged_chan->name);
10309                ast_mutex_lock(&iaxsl[fr->callno]);
10310                if (!iaxs[fr->callno]) {
10311                   ast_mutex_unlock(&iaxsl[fr->callno]);
10312                   return 1;
10313                }
10314 
10315                pbx_builtin_setvar_helper(bridged_chan, "BLINDTRANSFER", iaxs[fr->callno]->owner->name);
10316                if (!strcmp(ies.called_number, ast_parking_ext())) {
10317                   struct ast_channel *saved_channel = iaxs[fr->callno]->owner;
10318                   ast_mutex_unlock(&iaxsl[fr->callno]);
10319                   if (iax_park(bridged_chan, saved_channel)) {
10320                      ast_log(LOG_WARNING, "Failed to park call on '%s'\n", bridged_chan->name);
10321                   } else {
10322                      ast_debug(1, "Parked call on '%s'\n", ast_bridged_channel(iaxs[fr->callno]->owner)->name);
10323                   }
10324                   ast_mutex_lock(&iaxsl[fr->callno]);
10325                } else {
10326                   if (ast_async_goto(bridged_chan, iaxs[fr->callno]->context, ies.called_number, 1))
10327                      ast_log(LOG_WARNING, "Async goto of '%s' to '%s@%s' failed\n", bridged_chan->name, 
10328                         ies.called_number, iaxs[fr->callno]->context);
10329                   else {
10330                      ast_debug(1, "Async goto of '%s' to '%s@%s' started\n", bridged_chan->name, 
10331                         ies.called_number, iaxs[fr->callno]->context);
10332                   }
10333                }
10334             } else {
10335                ast_debug(1, "Async goto not applicable on call %d\n", fr->callno);
10336             }
10337 
10338             break;
10339          }
10340          case IAX_COMMAND_ACCEPT:
10341             /* Ignore if call is already up or needs authentication or is a TBD */
10342             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD | IAX_STATE_AUTHENTICATED))
10343                break;
10344             if (ast_test_flag(iaxs[fr->callno], IAX_PROVISION)) {
10345                /* Send ack immediately, before we destroy */
10346                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10347                iax2_destroy(fr->callno);
10348                break;
10349             }
10350             if (ies.format) {
10351                iaxs[fr->callno]->peerformat = ies.format;
10352             } else {
10353                if (iaxs[fr->callno]->owner)
10354                   iaxs[fr->callno]->peerformat = iaxs[fr->callno]->owner->nativeformats;
10355                else
10356                   iaxs[fr->callno]->peerformat = iaxs[fr->callno]->capability;
10357             }
10358             ast_verb(3, "Call accepted by %s (format %s)\n", ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr), ast_getformatname(iaxs[fr->callno]->peerformat));
10359             if (!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability)) {
10360                memset(&ied0, 0, sizeof(ied0));
10361                iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
10362                iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
10363                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10364                if (!iaxs[fr->callno]) {
10365                   ast_mutex_unlock(&iaxsl[fr->callno]);
10366                   return 1;
10367                }
10368                if (authdebug)
10369                   ast_log(LOG_NOTICE, "Rejected call to %s, format 0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->capability);
10370             } else {
10371                ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
10372                if (iaxs[fr->callno]->owner) {
10373                   /* Switch us to use a compatible format */
10374                   iaxs[fr->callno]->owner->nativeformats = iaxs[fr->callno]->peerformat;
10375                   ast_verb(3, "Format for call is %s\n", ast_getformatname(iaxs[fr->callno]->owner->nativeformats));
10376 retryowner2:
10377                   if (ast_channel_trylock(iaxs[fr->callno]->owner)) {
10378                      DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]);
10379                      if (iaxs[fr->callno] && iaxs[fr->callno]->owner) goto retryowner2;
10380                   }
10381                   
10382                   if (iaxs[fr->callno] && iaxs[fr->callno]->owner) {
10383                      /* Setup read/write formats properly. */
10384                      if (iaxs[fr->callno]->owner->writeformat)
10385                         ast_set_write_format(iaxs[fr->callno]->owner, iaxs[fr->callno]->owner->writeformat);   
10386                      if (iaxs[fr->callno]->owner->readformat)
10387                         ast_set_read_format(iaxs[fr->callno]->owner, iaxs[fr->callno]->owner->readformat);  
10388                      ast_channel_unlock(iaxs[fr->callno]->owner);
10389                   }
10390                }
10391             }
10392             if (iaxs[fr->callno]) {
10393                AST_LIST_LOCK(&dpcache);
10394                AST_LIST_TRAVERSE(&iaxs[fr->callno]->dpentries, dp, peer_list)
10395                   if (!(dp->flags & CACHE_FLAG_TRANSMITTED))
10396                      iax2_dprequest(dp, fr->callno);
10397                AST_LIST_UNLOCK(&dpcache);
10398             }
10399             break;
10400          case IAX_COMMAND_POKE:
10401             /* Send back a pong packet with the original timestamp */
10402             send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_PONG, fr->ts, NULL, 0, -1);
10403             if (!iaxs[fr->callno]) {
10404                ast_mutex_unlock(&iaxsl[fr->callno]);
10405                return 1;
10406             }
10407             break;
10408          case IAX_COMMAND_PING:
10409          {
10410             struct iax_ie_data pingied;
10411             construct_rr(iaxs[fr->callno], &pingied);
10412             /* Send back a pong packet with the original timestamp */
10413             send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_PONG, fr->ts, pingied.buf, pingied.pos, -1);
10414          }
10415             break;
10416          case IAX_COMMAND_PONG:
10417             /* Calculate ping time */
10418             iaxs[fr->callno]->pingtime =  calc_timestamp(iaxs[fr->callno], 0, &f) - fr->ts;
10419             /* save RR info */
10420             save_rr(fr, &ies);
10421 
10422             /* Good time to write jb stats for this call */
10423             log_jitterstats(fr->callno);
10424 
10425             if (iaxs[fr->callno]->peerpoke) {
10426                peer = iaxs[fr->callno]->peerpoke;
10427                if ((peer->lastms < 0)  || (peer->historicms > peer->maxms)) {
10428                   if (iaxs[fr->callno]->pingtime <= peer->maxms) {
10429                      ast_log(LOG_NOTICE, "Peer '%s' is now REACHABLE! Time: %d\n", peer->name, iaxs[fr->callno]->pingtime);
10430                      manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: IAX2\r\nPeer: IAX2/%s\r\nPeerStatus: Reachable\r\nTime: %d\r\n", peer->name, iaxs[fr->callno]->pingtime); 
10431                      ast_devstate_changed(AST_DEVICE_NOT_INUSE, "IAX2/%s", peer->name); /* Activate notification */
10432                   }
10433                } else if ((peer->historicms > 0) && (peer->historicms <= peer->maxms)) {
10434                   if (iaxs[fr->callno]->pingtime > peer->maxms) {
10435                      ast_log(LOG_NOTICE, "Peer '%s' is now TOO LAGGED (%d ms)!\n", peer->name, iaxs[fr->callno]->pingtime);
10436                      manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: IAX2\r\nPeer: IAX2/%s\r\nPeerStatus: Lagged\r\nTime: %d\r\n", peer->name, iaxs[fr->callno]->pingtime); 
10437                      ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "IAX2/%s", peer->name); /* Activate notification */
10438                   }
10439                }
10440                peer->lastms = iaxs[fr->callno]->pingtime;
10441                if (peer->smoothing && (peer->lastms > -1))
10442                   peer->historicms = (iaxs[fr->callno]->pingtime + peer->historicms) / 2;
10443                else if (peer->smoothing && peer->lastms < 0)
10444                   peer->historicms = (0 + peer->historicms) / 2;
10445                else              
10446                   peer->historicms = iaxs[fr->callno]->pingtime;
10447 
10448                /* Remove scheduled iax2_poke_noanswer */
10449                if (peer->pokeexpire > -1) {
10450                   if (!ast_sched_thread_del(sched, peer->pokeexpire)) {
10451                      peer_unref(peer);
10452                      peer->pokeexpire = -1;
10453                   }
10454                }
10455                /* Schedule the next cycle */
10456                if ((peer->lastms < 0)  || (peer->historicms > peer->maxms)) 
10457                   peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_peer_s, peer_ref(peer));
10458                else
10459                   peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqok, iax2_poke_peer_s, peer_ref(peer));
10460                if (peer->pokeexpire == -1)
10461                   peer_unref(peer);
10462                /* and finally send the ack */
10463                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10464                /* And wrap up the qualify call */
10465                iax2_destroy(fr->callno);
10466                peer->callno = 0;
10467                ast_debug(1, "Peer %s: got pong, lastms %d, historicms %d, maxms %d\n", peer->name, peer->lastms, peer->historicms, peer->maxms);
10468             }
10469             break;
10470          case IAX_COMMAND_LAGRQ:
10471          case IAX_COMMAND_LAGRP:
10472             f.src = "LAGRQ";
10473             f.mallocd = 0;
10474             f.offset = 0;
10475             f.samples = 0;
10476             iax_frame_wrap(fr, &f);
10477             if(f.subclass == IAX_COMMAND_LAGRQ) {
10478                /* Received a LAGRQ - echo back a LAGRP */
10479                fr->af.subclass = IAX_COMMAND_LAGRP;
10480                iax2_send(iaxs[fr->callno], &fr->af, fr->ts, -1, 0, 0, 0);
10481             } else {
10482                /* Received LAGRP in response to our LAGRQ */
10483                unsigned int ts;
10484                /* This is a reply we've been given, actually measure the difference */
10485                ts = calc_timestamp(iaxs[fr->callno], 0, &fr->af);
10486                iaxs[fr->callno]->lag = ts - fr->ts;
10487                if (iaxdebug)
10488                   ast_debug(1, "Peer %s lag measured as %dms\n",
10489                      ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr), iaxs[fr->callno]->lag);
10490             }
10491             break;
10492          case IAX_COMMAND_AUTHREQ:
10493             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD)) {
10494                ast_log(LOG_WARNING, "Call on %s is already up, can't start on it\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
10495                break;
10496             }
10497             if (authenticate_reply(iaxs[fr->callno], &iaxs[fr->callno]->addr, &ies, iaxs[fr->callno]->secret, iaxs[fr->callno]->outkey)) {
10498                struct ast_frame hangup_fr = { .frametype = AST_FRAME_CONTROL,
10499                         .subclass = AST_CONTROL_HANGUP,
10500                };
10501                ast_log(LOG_WARNING, 
10502                   "I don't know how to authenticate %s to %s\n", 
10503                   ies.username ? ies.username : "<unknown>", ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr));
10504                iax2_queue_frame(fr->callno, &hangup_fr);
10505             }
10506             if (!iaxs[fr->callno]) {
10507                ast_mutex_unlock(&iaxsl[fr->callno]);
10508                return 1;
10509             }
10510             break;
10511          case IAX_COMMAND_AUTHREP:
10512             /* For security, always ack immediately */
10513             if (delayreject)
10514                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10515             /* Ignore once we've started */
10516             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD)) {
10517                ast_log(LOG_WARNING, "Call on %s is already up, can't start on it\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
10518                break;
10519             }
10520             if (authenticate_verify(iaxs[fr->callno], &ies)) {
10521                if (authdebug)
10522                   ast_log(LOG_NOTICE, "Host %s failed to authenticate as %s\n", ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr), iaxs[fr->callno]->username);
10523                memset(&ied0, 0, sizeof(ied0));
10524                auth_fail(fr->callno, IAX_COMMAND_REJECT);
10525                break;
10526             }
10527             if (strcasecmp(iaxs[fr->callno]->exten, "TBD")) {
10528                /* This might re-enter the IAX code and need the lock */
10529                exists = ast_exists_extension(NULL, iaxs[fr->callno]->context, iaxs[fr->callno]->exten, 1, iaxs[fr->callno]->cid_num);
10530             } else
10531                exists = 0;
10532             if (strcmp(iaxs[fr->callno]->exten, "TBD") && !exists) {
10533                if (authdebug)
10534                   ast_log(LOG_NOTICE, "Rejected connect attempt from %s, request '%s@%s' does not exist\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->exten, iaxs[fr->callno]->context);
10535                memset(&ied0, 0, sizeof(ied0));
10536                iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
10537                iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
10538                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10539                if (!iaxs[fr->callno]) {
10540                   ast_mutex_unlock(&iaxsl[fr->callno]);
10541                   return 1;
10542                }
10543             } else {
10544                /* Select an appropriate format */
10545                if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
10546                   if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
10547                      using_prefs = "reqonly";
10548                   } else {
10549                      using_prefs = "disabled";
10550                   }
10551                   format = iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability;
10552                   memset(&pref, 0, sizeof(pref));
10553                   strcpy(caller_pref_buf, "disabled");
10554                   strcpy(host_pref_buf, "disabled");
10555                } else {
10556                   using_prefs = "mine";
10557                   if (ies.codec_prefs)
10558                      ast_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
10559                   if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
10560                      if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
10561                         pref = iaxs[fr->callno]->rprefs;
10562                         using_prefs = "caller";
10563                      } else {
10564                         pref = iaxs[fr->callno]->prefs;
10565                      }
10566                   } else /* if no codec_prefs IE do it the old way */
10567                      pref = iaxs[fr->callno]->prefs;
10568                
10569                   format = ast_codec_choose(&pref, iaxs[fr->callno]->capability & iaxs[fr->callno]->peercapability, 0);
10570                   ast_codec_pref_string(&iaxs[fr->callno]->rprefs, caller_pref_buf, sizeof(caller_pref_buf) - 1);
10571                   ast_codec_pref_string(&iaxs[fr->callno]->prefs, host_pref_buf, sizeof(host_pref_buf) - 1);
10572                }
10573                if (!format) {
10574                   if(!ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
10575                      ast_debug(1, "We don't do requested format %s, falling back to peer capability %d\n", ast_getformatname(iaxs[fr->callno]->peerformat), iaxs[fr->callno]->peercapability);
10576                      format = iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability;
10577                   }
10578                   if (!format) {
10579                      if (authdebug) {
10580                         if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) 
10581                            ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested 0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->capability);
10582                         else
10583                            ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->peercapability, iaxs[fr->callno]->capability);
10584                      }
10585                      memset(&ied0, 0, sizeof(ied0));
10586                      iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
10587                      iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
10588                      send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10589                      if (!iaxs[fr->callno]) {
10590                         ast_mutex_unlock(&iaxsl[fr->callno]);
10591                         return 1;
10592                      }
10593                   } else {
10594                      /* Pick one... */
10595                      if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
10596                         if(!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability))
10597                            format = 0;
10598                      } else {
10599                         if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
10600                            using_prefs = ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
10601                            memset(&pref, 0, sizeof(pref));
10602                            format = ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP) ?
10603                               iaxs[fr->callno]->peerformat : ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
10604                            strcpy(caller_pref_buf,"disabled");
10605                            strcpy(host_pref_buf,"disabled");
10606                         } else {
10607                            using_prefs = "mine";
10608                            if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
10609                               /* Do the opposite of what we tried above. */
10610                               if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
10611                                  pref = iaxs[fr->callno]->prefs;                 
10612                               } else {
10613                                  pref = iaxs[fr->callno]->rprefs;
10614                                  using_prefs = "caller";
10615                               }
10616                               format = ast_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability, 1);
10617                            } else /* if no codec_prefs IE do it the old way */
10618                               format = ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability); 
10619                         }
10620                      }
10621                      if (!format) {
10622                         ast_log(LOG_ERROR, "No best format in 0x%x???\n", iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
10623                         if (authdebug) {
10624                            if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP))
10625                               ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested 0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->capability);
10626                            else
10627                               ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability 0x%x/0x%x incompatible with our capability 0x%x.\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat, iaxs[fr->callno]->peercapability, iaxs[fr->callno]->capability);
10628                         }
10629                         memset(&ied0, 0, sizeof(ied0));
10630                         iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
10631                         iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
10632                         send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10633                         if (!iaxs[fr->callno]) {
10634                            ast_mutex_unlock(&iaxsl[fr->callno]);
10635                            return 1;
10636                         }
10637                      }
10638                   }
10639                }
10640                if (format) {
10641                   /* Authentication received */
10642                   memset(&ied1, 0, sizeof(ied1));
10643                   iax_ie_append_int(&ied1, IAX_IE_FORMAT, format);
10644                   send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACCEPT, 0, ied1.buf, ied1.pos, -1);
10645                   if (strcmp(iaxs[fr->callno]->exten, "TBD")) {
10646                      ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
10647                      ast_verb(3, "Accepting AUTHENTICATED call from %s:\n"
10648                                  "%srequested format = %s,\n"
10649                                  "%srequested prefs = %s,\n"
10650                                  "%sactual format = %s,\n"
10651                                  "%shost prefs = %s,\n"
10652                                  "%spriority = %s\n", 
10653                                  ast_inet_ntoa(sin.sin_addr), 
10654                                  VERBOSE_PREFIX_4,
10655                                  ast_getformatname(iaxs[fr->callno]->peerformat),
10656                                  VERBOSE_PREFIX_4,
10657                                  caller_pref_buf,
10658                                  VERBOSE_PREFIX_4,
10659                                  ast_getformatname(format),
10660                                  VERBOSE_PREFIX_4,
10661                                  host_pref_buf,
10662                                  VERBOSE_PREFIX_4,
10663                                  using_prefs);
10664 
10665                      ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
10666                      if (!(c = ast_iax2_new(fr->callno, AST_STATE_RING, format)))
10667                         iax2_destroy(fr->callno);
10668                      else if (ies.vars) {
10669                         struct ast_datastore *variablestore;
10670                         struct ast_variable *var, *prev = NULL;
10671                         AST_LIST_HEAD(, ast_var_t) *varlist;
10672                         varlist = ast_calloc(1, sizeof(*varlist));
10673                         variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
10674                         if (variablestore && varlist) {
10675                            variablestore->data = varlist;
10676                            variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
10677                            AST_LIST_HEAD_INIT(varlist);
10678                            ast_debug(1, "I can haz IAX vars? w00t\n");
10679                            for (var = ies.vars; var; var = var->next) {
10680                               struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
10681                               if (prev)
10682                                  ast_free(prev);
10683                               prev = var;
10684                               if (!newvar) {
10685                                  /* Don't abort list traversal, as this would leave ies.vars in an inconsistent state. */
10686                                  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
10687                               } else {
10688                                  AST_LIST_INSERT_TAIL(varlist, newvar, entries);
10689                               }
10690                            }
10691                            if (prev)
10692                               ast_free(prev);
10693                            ies.vars = NULL;
10694                            ast_channel_datastore_add(c, variablestore);
10695                         } else {
10696                            ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
10697                            if (variablestore)
10698                               ast_datastore_free(variablestore);
10699                            if (varlist)
10700                               ast_free(varlist);
10701                         }
10702                      }
10703                   } else {
10704                      ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
10705                      /* If this is a TBD call, we're ready but now what...  */
10706                      ast_verb(3, "Accepted AUTHENTICATED TBD call from %s\n", ast_inet_ntoa(sin.sin_addr));
10707                      if (ast_test_flag(iaxs[fr->callno], IAX_IMMEDIATE)) {
10708                         goto immediatedial;
10709                      }
10710                   }
10711                }
10712             }
10713             break;
10714          case IAX_COMMAND_DIAL:
10715 immediatedial:
10716             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD)) {
10717                ast_clear_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
10718                ast_string_field_set(iaxs[fr->callno], exten, ies.called_number ? ies.called_number : "s");
10719                if (!ast_exists_extension(NULL, iaxs[fr->callno]->context, iaxs[fr->callno]->exten, 1, iaxs[fr->callno]->cid_num)) {
10720                   if (authdebug)
10721                      ast_log(LOG_NOTICE, "Rejected dial attempt from %s, request '%s@%s' does not exist\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->exten, iaxs[fr->callno]->context);
10722                   memset(&ied0, 0, sizeof(ied0));
10723                   iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
10724                   iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
10725                   send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10726                   if (!iaxs[fr->callno]) {
10727                      ast_mutex_unlock(&iaxsl[fr->callno]);
10728                      return 1;
10729                   }
10730                } else {
10731                   ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
10732                   ast_verb(3, "Accepting DIAL from %s, formats = 0x%x\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat);
10733                   ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
10734                   send_command(iaxs[fr->callno], AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, 0, NULL, 0, -1);
10735                   if (!(c = ast_iax2_new(fr->callno, AST_STATE_RING, iaxs[fr->callno]->peerformat)))
10736                      iax2_destroy(fr->callno);
10737                   else if (ies.vars) {
10738                      struct ast_datastore *variablestore;
10739                      struct ast_variable *var, *prev = NULL;
10740                      AST_LIST_HEAD(, ast_var_t) *varlist;
10741                      varlist = ast_calloc(1, sizeof(*varlist));
10742                      variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
10743                      ast_debug(1, "I can haz IAX vars? w00t\n");
10744                      if (variablestore && varlist) {
10745                         variablestore->data = varlist;
10746                         variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
10747                         AST_LIST_HEAD_INIT(varlist);
10748                         for (var = ies.vars; var; var = var->next) {
10749                            struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
10750                            if (prev)
10751                               ast_free(prev);
10752                            prev = var;
10753                            if (!newvar) {
10754                               /* Don't abort list traversal, as this would leave ies.vars in an inconsistent state. */
10755                               ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
10756                            } else {
10757                               AST_LIST_INSERT_TAIL(varlist, newvar, entries);
10758                            }
10759                         }
10760                         if (prev)
10761                            ast_free(prev);
10762                         ies.vars = NULL;
10763                         ast_channel_datastore_add(c, variablestore);
10764                      } else {
10765                         ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
10766                         if (variablestore)
10767                            ast_datastore_free(variablestore);
10768                         if (varlist)
10769                            ast_free(varlist);
10770                      }
10771                   }
10772                }
10773             }
10774             break;
10775          case IAX_COMMAND_INVAL:
10776             iaxs[fr->callno]->error = ENOTCONN;
10777             ast_debug(1, "Immediately destroying %d, having received INVAL\n", fr->callno);
10778             iax2_destroy(fr->callno);
10779             ast_debug(1, "Destroying call %d\n", fr->callno);
10780             break;
10781          case IAX_COMMAND_VNAK:
10782             ast_debug(1, "Received VNAK: resending outstanding frames\n");
10783             /* Force retransmission */
10784             vnak_retransmit(fr->callno, fr->iseqno);
10785             break;
10786          case IAX_COMMAND_REGREQ:
10787          case IAX_COMMAND_REGREL:
10788             /* For security, always ack immediately */
10789             if (delayreject)
10790                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10791             if (register_verify(fr->callno, &sin, &ies)) {
10792                if (!iaxs[fr->callno]) {
10793                   ast_mutex_unlock(&iaxsl[fr->callno]);
10794                   return 1;
10795                }
10796                /* Send delayed failure */
10797                auth_fail(fr->callno, IAX_COMMAND_REGREJ);
10798                break;
10799             }
10800             if (!iaxs[fr->callno]) {
10801                ast_mutex_unlock(&iaxsl[fr->callno]);
10802                return 1;
10803             }
10804             if ((ast_strlen_zero(iaxs[fr->callno]->secret) && ast_strlen_zero(iaxs[fr->callno]->inkeys)) ||
10805                   ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_AUTHENTICATED)) {
10806 
10807                if (f.subclass == IAX_COMMAND_REGREL)
10808                   memset(&sin, 0, sizeof(sin));
10809                if (update_registry(&sin, fr->callno, ies.devicetype, fd, ies.refresh))
10810                   ast_log(LOG_WARNING, "Registry error\n");
10811                if (!iaxs[fr->callno]) {
10812                   ast_mutex_unlock(&iaxsl[fr->callno]);
10813                   return 1;
10814                }
10815                if (ies.provverpres && ies.serviceident && sin.sin_addr.s_addr) {
10816                   ast_mutex_unlock(&iaxsl[fr->callno]);
10817                   check_provisioning(&sin, fd, ies.serviceident, ies.provver);
10818                   ast_mutex_lock(&iaxsl[fr->callno]);
10819                   if (!iaxs[fr->callno]) {
10820                      ast_mutex_unlock(&iaxsl[fr->callno]);
10821                      return 1;
10822                   }
10823                }
10824                break;
10825             }
10826             registry_authrequest(fr->callno);
10827             if (!iaxs[fr->callno]) {
10828                ast_mutex_unlock(&iaxsl[fr->callno]);
10829                return 1;
10830             }
10831             break;
10832          case IAX_COMMAND_REGACK:
10833             if (iax2_ack_registry(&ies, &sin, fr->callno)) 
10834                ast_log(LOG_WARNING, "Registration failure\n");
10835             /* Send ack immediately, before we destroy */
10836             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10837             iax2_destroy(fr->callno);
10838             break;
10839          case IAX_COMMAND_REGREJ:
10840             if (iaxs[fr->callno]->reg) {
10841                if (authdebug) {
10842                   ast_log(LOG_NOTICE, "Registration of '%s' rejected: '%s' from: '%s'\n", iaxs[fr->callno]->reg->username, ies.cause ? ies.cause : "<unknown>", ast_inet_ntoa(sin.sin_addr));
10843                   manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: IAX2\r\nUsername: %s\r\nStatus: Rejected\r\nCause: %s\r\n", iaxs[fr->callno]->reg->username, ies.cause ? ies.cause : "<unknown>");
10844                }
10845                iaxs[fr->callno]->reg->regstate = REG_STATE_REJECTED;
10846             }
10847             /* Send ack immediately, before we destroy */
10848             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10849             iax2_destroy(fr->callno);
10850             break;
10851          case IAX_COMMAND_REGAUTH:
10852             /* Authentication request */
10853             if (registry_rerequest(&ies, fr->callno, &sin)) {
10854                memset(&ied0, 0, sizeof(ied0));
10855                iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No authority found");
10856                iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_NOT_SUBSCRIBED);
10857                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10858                if (!iaxs[fr->callno]) {
10859                   ast_mutex_unlock(&iaxsl[fr->callno]);
10860                   return 1;
10861                }
10862             }
10863             break;
10864          case IAX_COMMAND_TXREJ:
10865             iaxs[fr->callno]->transferring = 0;
10866             ast_verb(3, "Channel '%s' unable to transfer\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
10867             memset(&iaxs[fr->callno]->transfer, 0, sizeof(iaxs[fr->callno]->transfer));
10868             if (iaxs[fr->callno]->bridgecallno) {
10869                if (iaxs[iaxs[fr->callno]->bridgecallno]->transferring) {
10870                   iaxs[iaxs[fr->callno]->bridgecallno]->transferring = 0;
10871                   send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
10872                }
10873             }
10874             break;
10875          case IAX_COMMAND_TXREADY:
10876             if ((iaxs[fr->callno]->transferring == TRANSFER_BEGIN) ||
10877                 (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN)) {
10878                if (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN)
10879                   iaxs[fr->callno]->transferring = TRANSFER_MREADY;
10880                else
10881                   iaxs[fr->callno]->transferring = TRANSFER_READY;
10882                ast_verb(3, "Channel '%s' ready to transfer\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
10883                if (iaxs[fr->callno]->bridgecallno) {
10884                   if ((iaxs[iaxs[fr->callno]->bridgecallno]->transferring == TRANSFER_READY) ||
10885                       (iaxs[iaxs[fr->callno]->bridgecallno]->transferring == TRANSFER_MREADY)) {
10886                      /* They're both ready, now release them. */
10887                      if (iaxs[fr->callno]->transferring == TRANSFER_MREADY) {
10888                         ast_verb(3, "Attempting media bridge of %s and %s\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>",
10889                               iaxs[iaxs[fr->callno]->bridgecallno]->owner ? iaxs[iaxs[fr->callno]->bridgecallno]->owner->name : "<Unknown>");
10890 
10891                         iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_MEDIA;
10892                         iaxs[fr->callno]->transferring = TRANSFER_MEDIA;
10893 
10894                         memset(&ied0, 0, sizeof(ied0));
10895                         memset(&ied1, 0, sizeof(ied1));
10896                         iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
10897                         iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
10898                         send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied0.buf, ied0.pos, -1);
10899                         send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied1.buf, ied1.pos, -1);
10900                      } else {
10901                         ast_verb(3, "Releasing %s and %s\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>",
10902                               iaxs[iaxs[fr->callno]->bridgecallno]->owner ? iaxs[iaxs[fr->callno]->bridgecallno]->owner->name : "<Unknown>");
10903 
10904                         iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_RELEASED;
10905                         iaxs[fr->callno]->transferring = TRANSFER_RELEASED;
10906                         ast_set_flag(iaxs[iaxs[fr->callno]->bridgecallno], IAX_ALREADYGONE);
10907                         ast_set_flag(iaxs[fr->callno], IAX_ALREADYGONE);
10908 
10909                         /* Stop doing lag & ping requests */
10910                         stop_stuff(fr->callno);
10911                         stop_stuff(iaxs[fr->callno]->bridgecallno);
10912 
10913                         memset(&ied0, 0, sizeof(ied0));
10914                         memset(&ied1, 0, sizeof(ied1));
10915                         iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
10916                         iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
10917                         send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied0.buf, ied0.pos, -1);
10918                         send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied1.buf, ied1.pos, -1);
10919                      }
10920 
10921                   }
10922                }
10923             }
10924             break;
10925          case IAX_COMMAND_TXREQ:
10926             try_transfer(iaxs[fr->callno], &ies);
10927             break;
10928          case IAX_COMMAND_TXCNT:
10929             if (iaxs[fr->callno]->transferring)
10930                send_command_transfer(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXACC, 0, NULL, 0);
10931             break;
10932          case IAX_COMMAND_TXREL:
10933             /* Send ack immediately, rather than waiting until we've changed addresses */
10934             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10935             complete_transfer(fr->callno, &ies);
10936             stop_stuff(fr->callno); /* for attended transfer to work with libiax */
10937             break;   
10938          case IAX_COMMAND_TXMEDIA:
10939             if (iaxs[fr->callno]->transferring == TRANSFER_READY) {
10940                AST_LIST_LOCK(&frame_queue);
10941                AST_LIST_TRAVERSE(&frame_queue, cur, list) {
10942                   /* Cancel any outstanding frames and start anew */
10943                   if ((fr->callno == cur->callno) && (cur->transfer))
10944                      cur->retries = -1;
10945                }
10946                AST_LIST_UNLOCK(&frame_queue);
10947                /* Start sending our media to the transfer address, but otherwise leave the call as-is */
10948                iaxs[fr->callno]->transferring = TRANSFER_MEDIAPASS;
10949             }
10950             break;
10951          case IAX_COMMAND_RTKEY:
10952             if (!IAX_CALLENCRYPTED(iaxs[fr->callno])) {
10953                ast_log(LOG_WARNING, 
10954                   "we've been told to rotate our encryption key, "
10955                   "but this isn't an encrypted call. bad things will happen.\n"
10956                );
10957                break;
10958             }
10959 
10960             IAX_DEBUGDIGEST("Receiving", ies.challenge);
10961 
10962             ast_aes_decrypt_key((unsigned char *) ies.challenge, &iaxs[fr->callno]->dcx);
10963             break;
10964          case IAX_COMMAND_DPREP:
10965             complete_dpreply(iaxs[fr->callno], &ies);
10966             break;
10967          case IAX_COMMAND_UNSUPPORT:
10968             ast_log(LOG_NOTICE, "Peer did not understand our iax command '%d'\n", ies.iax_unknown);
10969             break;
10970          case IAX_COMMAND_FWDOWNL:
10971             /* Firmware download */
10972             if (!ast_test_flag(&globalflags, IAX_ALLOWFWDOWNLOAD)) {
10973                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_UNSUPPORT, 0, NULL, 0, -1);
10974                break;
10975             }
10976             memset(&ied0, 0, sizeof(ied0));
10977             res = iax_firmware_append(&ied0, (unsigned char *)ies.devicetype, ies.fwdesc);
10978             if (res < 0)
10979                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10980             else if (res > 0)
10981                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_FWDATA, 0, ied0.buf, ied0.pos, -1);
10982             else
10983                send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_FWDATA, 0, ied0.buf, ied0.pos, -1);
10984             if (!iaxs[fr->callno]) {
10985                ast_mutex_unlock(&iaxsl[fr->callno]);
10986                return 1;
10987             }
10988             break;
10989          case IAX_COMMAND_CALLTOKEN:
10990          {
10991             struct iax_frame *cur;
10992             int found = 0;
10993             AST_LIST_LOCK(&frame_queue);
10994             AST_LIST_TRAVERSE(&frame_queue, cur, list) {
10995                /* find the last sent frame in our frame queue for this callno.
10996                 * There are many things to take into account before resending this frame.
10997                 * All of these are taken care of in resend_with_token() */
10998                if (cur->callno == fr->callno) {
10999                   found = 1;
11000                   break;
11001                }
11002             }
11003             AST_LIST_UNLOCK(&frame_queue);
11004 
11005             /* find last sent frame */
11006             if (cur && found && ies.calltoken && ies.calltokendata) {
11007                resend_with_token(fr->callno, cur, (char *) ies.calltokendata);
11008             }
11009             break;
11010          }
11011          default:
11012             ast_debug(1, "Unknown IAX command %d on %d/%d\n", f.subclass, fr->callno, iaxs[fr->callno]->peercallno);
11013             memset(&ied0, 0, sizeof(ied0));
11014             iax_ie_append_byte(&ied0, IAX_IE_IAX_UNKNOWN, f.subclass);
11015             send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_UNSUPPORT, 0, ied0.buf, ied0.pos, -1);
11016          }
11017          /* Free remote variables (if any) */
11018          if (ies.vars) {
11019             ast_variables_destroy(ies.vars);
11020             ast_debug(1, "I can haz IAX vars, but they is no good :-(\n");
11021             ies.vars = NULL;
11022          }
11023 
11024          /* Don't actually pass these frames along */
11025          if ((f.subclass != IAX_COMMAND_ACK) && 
11026            (f.subclass != IAX_COMMAND_TXCNT) && 
11027            (f.subclass != IAX_COMMAND_TXACC) && 
11028            (f.subclass != IAX_COMMAND_INVAL) &&
11029            (f.subclass != IAX_COMMAND_VNAK)) { 
11030             if (iaxs[fr->callno] && iaxs[fr->callno]->aseqno != iaxs[fr->callno]->iseqno)
11031                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11032          }
11033          ast_mutex_unlock(&iaxsl[fr->callno]);
11034          return 1;
11035       }
11036       /* Unless this is an ACK or INVAL frame, ack it */
11037       if (iaxs[fr->callno] && iaxs[fr->callno]->aseqno != iaxs[fr->callno]->iseqno)
11038          send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11039    } else if (minivid) {
11040       f.frametype = AST_FRAME_VIDEO;
11041       if (iaxs[fr->callno]->videoformat > 0) 
11042          f.subclass = iaxs[fr->callno]->videoformat | (ntohs(vh->ts) & 0x8000 ? 1 : 0);
11043       else {
11044          ast_log(LOG_WARNING, "Received mini frame before first full video frame\n");
11045          iax2_vnak(fr->callno);
11046          ast_mutex_unlock(&iaxsl[fr->callno]);
11047          return 1;
11048       }
11049       f.datalen = res - sizeof(*vh);
11050       if (f.datalen)
11051          f.data.ptr = thread->buf + sizeof(*vh);
11052       else
11053          f.data.ptr = NULL;
11054 #ifdef IAXTESTS
11055       if (test_resync) {
11056          fr->ts = (iaxs[fr->callno]->last & 0xFFFF8000L) | ((ntohs(vh->ts) + test_resync) & 0x7fff);
11057       } else
11058 #endif /* IAXTESTS */
11059          fr->ts = (iaxs[fr->callno]->last & 0xFFFF8000L) | (ntohs(vh->ts) & 0x7fff);
11060    } else {
11061       /* A mini frame */
11062       f.frametype = AST_FRAME_VOICE;
11063       if (iaxs[fr->callno]->voiceformat > 0)
11064          f.subclass = iaxs[fr->callno]->voiceformat;
11065       else {
11066          ast_debug(1, "Received mini frame before first full voice frame\n");
11067          iax2_vnak(fr->callno);
11068          ast_mutex_unlock(&iaxsl[fr->callno]);
11069          return 1;
11070       }
11071       f.datalen = res - sizeof(struct ast_iax2_mini_hdr);
11072       if (f.datalen < 0) {
11073          ast_log(LOG_WARNING, "Datalen < 0?\n");
11074          ast_mutex_unlock(&iaxsl[fr->callno]);
11075          return 1;
11076       }
11077       if (f.datalen)
11078          f.data.ptr = thread->buf + sizeof(*mh);
11079       else
11080          f.data.ptr = NULL;
11081 #ifdef IAXTESTS
11082       if (test_resync) {
11083          fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | ((ntohs(mh->ts) + test_resync) & 0xffff);
11084       } else
11085 #endif /* IAXTESTS */
11086       fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | ntohs(mh->ts);
11087       /* FIXME? Surely right here would be the right place to undo timestamp wraparound? */
11088    }
11089    /* Don't pass any packets until we're started */
11090    if (!ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
11091       ast_mutex_unlock(&iaxsl[fr->callno]);
11092       return 1;
11093    }
11094    /* Common things */
11095    f.src = "IAX2";
11096    f.mallocd = 0;
11097    f.offset = 0;
11098    f.len = 0;
11099    if (f.datalen && (f.frametype == AST_FRAME_VOICE)) {
11100       f.samples = ast_codec_get_samples(&f);
11101       /* We need to byteswap incoming slinear samples from network byte order */
11102       if (f.subclass == AST_FORMAT_SLINEAR)
11103          ast_frame_byteswap_be(&f);
11104    } else
11105       f.samples = 0;
11106    iax_frame_wrap(fr, &f);
11107 
11108    /* If this is our most recent packet, use it as our basis for timestamping */
11109    if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts) {
11110       /*iaxs[fr->callno]->last = fr->ts; (do it afterwards cos schedule/forward_delivery needs the last ts too)*/
11111       fr->outoforder = 0;
11112    } else {
11113       if (iaxdebug && iaxs[fr->callno])
11114          ast_debug(1, "Received out of order packet... (type=%d, subclass %d, ts = %d, last = %d)\n", f.frametype, f.subclass, fr->ts, iaxs[fr->callno]->last);
11115       fr->outoforder = -1;
11116    }
11117    fr->cacheable = ((f.frametype == AST_FRAME_VOICE) || (f.frametype == AST_FRAME_VIDEO));
11118    duped_fr = iaxfrdup2(fr);
11119    if (duped_fr) {
11120       schedule_delivery(duped_fr, updatehistory, 0, &fr->ts);
11121    }
11122    if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts) {
11123       iaxs[fr->callno]->last = fr->ts;
11124 #if 1
11125       if (iaxdebug)
11126          ast_debug(1, "For call=%d, set last=%d\n", fr->callno, fr->ts);
11127 #endif
11128    }
11129 
11130    /* Always run again */
11131    ast_mutex_unlock(&iaxsl[fr->callno]);
11132    return 1;
11133 }

static int socket_process_meta ( int  packet_len,
struct ast_iax2_meta_hdr *  meta,
struct sockaddr_in *  sin,
int  sockfd,
struct iax_frame *  fr 
) [static]

Definition at line 9271 of file chan_iax2.c.

References ast_codec_get_samples(), AST_FRAME_VOICE, ast_inet_ntoa(), ast_log(), ast_mutex_unlock(), ast_test_flag, ast_tvnow(), ast_tvzero(), iax_frame::callno, ast_iax2_meta_trunk_entry::callno, ast_iax2_mini_hdr::callno, ast_iax2_meta_hdr::cmddata, ast_frame::data, ast_iax2_meta_trunk_hdr::data, ast_iax2_meta_hdr::data, ast_frame::datalen, find_callno_locked(), find_tpeer(), fix_peerts(), ast_frame::frametype, iax2_vnak(), IAX_FLAG_FULL, iax_frame_wrap(), IAX_META_TRUNK, IAX_META_TRUNK_MINI, IAX_META_TRUNK_SUPERMINI, IAX_STATE_STARTED, iaxfrdup2(), chan_iax2_pvt::last, ast_iax2_meta_trunk_entry::len, ast_iax2_meta_trunk_mini::len, iax2_trunk_peer::lock, LOG_WARNING, ast_frame::mallocd, ast_iax2_meta_hdr::metacmd, ast_iax2_meta_trunk_mini::mini, NEW_PREVENT, ast_frame::offset, iax_frame::outoforder, ast_frame::ptr, iax2_trunk_peer::rxtrunktime, ast_frame::samples, schedule_delivery(), ast_frame::src, chan_iax2_pvt::state, ast_frame::subclass, iax2_trunk_peer::trunkact, iax_frame::ts, ast_iax2_mini_hdr::ts, ast_iax2_meta_trunk_hdr::ts, and chan_iax2_pvt::voiceformat.

Referenced by socket_process().

09273 {
09274    unsigned char metatype;
09275    struct ast_iax2_meta_trunk_mini *mtm;
09276    struct ast_iax2_meta_trunk_hdr *mth;
09277    struct ast_iax2_meta_trunk_entry *mte;
09278    struct iax2_trunk_peer *tpeer;
09279    unsigned int ts;
09280    void *ptr;
09281    struct timeval rxtrunktime;
09282    struct ast_frame f = { 0, };
09283 
09284    if (packet_len < sizeof(*meta)) {
09285       ast_log(LOG_WARNING, "Rejecting packet from '%s.%d' that is flagged as a meta frame but is too short\n", 
09286          ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
09287       return 1;
09288    }
09289 
09290    if (meta->metacmd != IAX_META_TRUNK)
09291       return 1;
09292 
09293    if (packet_len < (sizeof(*meta) + sizeof(*mth))) {
09294       ast_log(LOG_WARNING, "midget meta trunk packet received (%d of %d min)\n", packet_len,
09295          (int) (sizeof(*meta) + sizeof(*mth)));
09296       return 1;
09297    }
09298    mth = (struct ast_iax2_meta_trunk_hdr *)(meta->data);
09299    ts = ntohl(mth->ts);
09300    metatype = meta->cmddata;
09301    packet_len -= (sizeof(*meta) + sizeof(*mth));
09302    ptr = mth->data;
09303    tpeer = find_tpeer(sin, sockfd);
09304    if (!tpeer) {
09305       ast_log(LOG_WARNING, "Unable to accept trunked packet from '%s:%d': No matching peer\n", 
09306          ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
09307       return 1;
09308    }
09309    tpeer->trunkact = ast_tvnow();
09310    if (!ts || ast_tvzero(tpeer->rxtrunktime))
09311       tpeer->rxtrunktime = tpeer->trunkact;
09312    rxtrunktime = tpeer->rxtrunktime;
09313    ast_mutex_unlock(&tpeer->lock);
09314    while (packet_len >= sizeof(*mte)) {
09315       /* Process channels */
09316       unsigned short callno, trunked_ts, len;
09317 
09318       if (metatype == IAX_META_TRUNK_MINI) {
09319          mtm = (struct ast_iax2_meta_trunk_mini *) ptr;
09320          ptr += sizeof(*mtm);
09321          packet_len -= sizeof(*mtm);
09322          len = ntohs(mtm->len);
09323          callno = ntohs(mtm->mini.callno);
09324          trunked_ts = ntohs(mtm->mini.ts);
09325       } else if (metatype == IAX_META_TRUNK_SUPERMINI) {
09326          mte = (struct ast_iax2_meta_trunk_entry *)ptr;
09327          ptr += sizeof(*mte);
09328          packet_len -= sizeof(*mte);
09329          len = ntohs(mte->len);
09330          callno = ntohs(mte->callno);
09331          trunked_ts = 0;
09332       } else {
09333          ast_log(LOG_WARNING, "Unknown meta trunk cmd from '%s:%d': dropping\n", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
09334          break;
09335       }
09336       /* Stop if we don't have enough data */
09337       if (len > packet_len)
09338          break;
09339       fr->callno = find_callno_locked(callno & ~IAX_FLAG_FULL, 0, sin, NEW_PREVENT, sockfd, 0);
09340       if (!fr->callno)
09341          continue;
09342 
09343       /* If it's a valid call, deliver the contents.  If not, we
09344          drop it, since we don't have a scallno to use for an INVAL */
09345       /* Process as a mini frame */
09346       memset(&f, 0, sizeof(f));
09347       f.frametype = AST_FRAME_VOICE;
09348       if (!iaxs[fr->callno]) {
09349          /* drop it */
09350       } else if (iaxs[fr->callno]->voiceformat == 0) {
09351          ast_log(LOG_WARNING, "Received trunked frame before first full voice frame\n");
09352          iax2_vnak(fr->callno);
09353       } else {
09354          f.subclass = iaxs[fr->callno]->voiceformat;
09355          f.datalen = len;
09356          if (f.datalen >= 0) {
09357             if (f.datalen)
09358                f.data.ptr = ptr;
09359             else
09360                f.data.ptr = NULL;
09361             if (trunked_ts)
09362                fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | (trunked_ts & 0xffff);
09363             else
09364                fr->ts = fix_peerts(&rxtrunktime, fr->callno, ts);
09365             /* Don't pass any packets until we're started */
09366             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
09367                struct iax_frame *duped_fr;
09368 
09369                /* Common things */
09370                f.src = "IAX2";
09371                f.mallocd = 0;
09372                f.offset = 0;
09373                if (f.datalen && (f.frametype == AST_FRAME_VOICE)) 
09374                   f.samples = ast_codec_get_samples(&f);
09375                else
09376                   f.samples = 0;
09377                fr->outoforder = 0;
09378                iax_frame_wrap(fr, &f);
09379                duped_fr = iaxfrdup2(fr);
09380                if (duped_fr)
09381                   schedule_delivery(duped_fr, 1, 1, &fr->ts);
09382                if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts)
09383                   iaxs[fr->callno]->last = fr->ts;
09384             }
09385          } else {
09386             ast_log(LOG_WARNING, "Datalen < 0?\n");
09387          }
09388       }
09389       ast_mutex_unlock(&iaxsl[fr->callno]);
09390       ptr += len;
09391       packet_len -= len;
09392    }
09393 
09394    return 1;
09395 }

static int socket_read ( int *  id,
int  fd,
short  events,
void *  cbdata 
) [static]

Definition at line 9192 of file chan_iax2.c.

References ast_copy_string(), ast_debug, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_random(), iax2_thread::buf, iax2_thread::buf_len, iax2_thread::buf_size, iax2_thread::callno, iax2_thread::cond, ast_iax2_full_hdr::csub, iax2_thread::csub, iax2_thread::curfunc, defer_full_frame(), errno, iax2_thread::ffinfo, find_idle_thread(), handle_error(), IAX_FLAG_FULL, IAX_IOSTATE_IDLE, IAX_IOSTATE_READY, inaddrcmp(), iax2_thread::iofd, iax2_thread::iosin, iax2_thread::iostate, iax2_thread::lock, LOG_WARNING, iax2_thread::readbuf, ast_iax2_full_hdr::scallno, signal_condition(), iax2_thread::sin, thread, ast_iax2_full_hdr::type, and iax2_thread::type.

Referenced by peer_set_srcaddr(), and set_config().

09193 {
09194    struct iax2_thread *thread;
09195    socklen_t len;
09196    time_t t;
09197    static time_t last_errtime = 0;
09198    struct ast_iax2_full_hdr *fh;
09199 
09200    if (!(thread = find_idle_thread())) {
09201       time(&t);
09202       if (t != last_errtime)
09203          ast_debug(1, "Out of idle IAX2 threads for I/O, pausing!\n");
09204       last_errtime = t;
09205       usleep(1);
09206       return 1;
09207    }
09208 
09209    len = sizeof(thread->iosin);
09210    thread->iofd = fd;
09211    thread->buf_len = recvfrom(fd, thread->readbuf, sizeof(thread->readbuf), 0, (struct sockaddr *) &thread->iosin, &len);
09212    thread->buf_size = sizeof(thread->readbuf);
09213    thread->buf = thread->readbuf;
09214    if (thread->buf_len < 0) {
09215       if (errno != ECONNREFUSED && errno != EAGAIN)
09216          ast_log(LOG_WARNING, "Error: %s\n", strerror(errno));
09217       handle_error();
09218       thread->iostate = IAX_IOSTATE_IDLE;
09219       signal_condition(&thread->lock, &thread->cond);
09220       return 1;
09221    }
09222    if (test_losspct && ((100.0 * ast_random() / (RAND_MAX + 1.0)) < test_losspct)) { /* simulate random loss condition */
09223       thread->iostate = IAX_IOSTATE_IDLE;
09224       signal_condition(&thread->lock, &thread->cond);
09225       return 1;
09226    }
09227    
09228    /* Determine if this frame is a full frame; if so, and any thread is currently
09229       processing a full frame for the same callno from this peer, then drop this
09230       frame (and the peer will retransmit it) */
09231    fh = (struct ast_iax2_full_hdr *) thread->buf;
09232    if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
09233       struct iax2_thread *cur = NULL;
09234       uint16_t callno = ntohs(fh->scallno) & ~IAX_FLAG_FULL;
09235       
09236       AST_LIST_LOCK(&active_list);
09237       AST_LIST_TRAVERSE(&active_list, cur, list) {
09238          if ((cur->ffinfo.callno == callno) &&
09239              !inaddrcmp(&cur->ffinfo.sin, &thread->iosin))
09240             break;
09241       }
09242       if (cur) {
09243          /* we found another thread processing a full frame for this call,
09244             so queue it up for processing later. */
09245          defer_full_frame(thread, cur);
09246          AST_LIST_UNLOCK(&active_list);
09247          thread->iostate = IAX_IOSTATE_IDLE;
09248          signal_condition(&thread->lock, &thread->cond);
09249          return 1;
09250       } else {
09251          /* this thread is going to process this frame, so mark it */
09252          thread->ffinfo.callno = callno;
09253          memcpy(&thread->ffinfo.sin, &thread->iosin, sizeof(thread->ffinfo.sin));
09254          thread->ffinfo.type = fh->type;
09255          thread->ffinfo.csub = fh->csub;
09256          AST_LIST_INSERT_HEAD(&active_list, thread, list);
09257       }
09258       AST_LIST_UNLOCK(&active_list);
09259    }
09260    
09261    /* Mark as ready and send on its way */
09262    thread->iostate = IAX_IOSTATE_READY;
09263 #ifdef DEBUG_SCHED_MULTITHREAD
09264    ast_copy_string(thread->curfunc, "socket_process", sizeof(thread->curfunc));
09265 #endif
09266    signal_condition(&thread->lock, &thread->cond);
09267 
09268    return 1;
09269 }

static void spawn_dp_lookup ( int  callno,
const char *  context,
const char *  callednum,
const char *  callerid 
) [static]

Definition at line 8908 of file chan_iax2.c.

References ast_calloc, ast_copy_string(), ast_log(), ast_pthread_create_detached, ast_strdup, dpreq_data::callednum, dpreq_data::callerid, dpreq_data::callno, dpreq_data::context, dp_lookup_thread(), and LOG_WARNING.

Referenced by socket_process().

08909 {
08910    pthread_t newthread;
08911    struct dpreq_data *dpr;
08912    
08913    if (!(dpr = ast_calloc(1, sizeof(*dpr))))
08914       return;
08915 
08916    dpr->callno = callno;
08917    ast_copy_string(dpr->context, context, sizeof(dpr->context));
08918    ast_copy_string(dpr->callednum, callednum, sizeof(dpr->callednum));
08919    if (callerid)
08920       dpr->callerid = ast_strdup(callerid);
08921    if (ast_pthread_create_detached(&newthread, NULL, dp_lookup_thread, dpr)) {
08922       ast_log(LOG_WARNING, "Unable to start lookup thread!\n");
08923    }
08924 }

static int start_network_thread ( void   )  [static]

Definition at line 11726 of file chan_iax2.c.

References ast_calloc, ast_cond_init(), ast_free, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_mutex_init(), ast_pthread_create_background, ast_verb, iax2_thread::cond, iax2_process_thread(), IAX_THREAD_TYPE_POOL, iaxthreadcount, iax2_thread::lock, LOG_WARNING, network_thread(), thread, iax2_thread::threadid, iax2_thread::threadnum, and iax2_thread::type.

Referenced by load_module().

11727 {
11728    struct iax2_thread *thread;
11729    int threadcount = 0;
11730    int x;
11731    for (x = 0; x < iaxthreadcount; x++) {
11732       thread = ast_calloc(1, sizeof(*thread));
11733       if (thread) {
11734          thread->type = IAX_THREAD_TYPE_POOL;
11735          thread->threadnum = ++threadcount;
11736          ast_mutex_init(&thread->lock);
11737          ast_cond_init(&thread->cond, NULL);
11738          if (ast_pthread_create_background(&thread->threadid, NULL, iax2_process_thread, thread)) {
11739             ast_log(LOG_WARNING, "Failed to create new thread!\n");
11740             ast_free(thread);
11741             thread = NULL;
11742          }
11743          AST_LIST_LOCK(&idle_list);
11744          AST_LIST_INSERT_TAIL(&idle_list, thread, list);
11745          AST_LIST_UNLOCK(&idle_list);
11746       }
11747    }
11748    ast_pthread_create_background(&netthreadid, NULL, network_thread, NULL);
11749    ast_verb(2, "%d helper threads started\n", threadcount);
11750    return 0;
11751 }

static void stop_stuff ( int  callno  )  [static]

Definition at line 8611 of file chan_iax2.c.

References iax2_destroy_helper().

Referenced by socket_process().

08612 {
08613    iax2_destroy_helper(iaxs[callno]);
08614 }

static void store_by_peercallno ( struct chan_iax2_pvt *  pvt  )  [static]

Definition at line 1963 of file chan_iax2.c.

References ao2_link, ast_log(), LOG_ERROR, and chan_iax2_pvt::peercallno.

Referenced by __find_callno(), complete_transfer(), and socket_process().

01964 {
01965    if (!pvt->peercallno) {
01966       ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
01967       return;
01968    }
01969 
01970    ao2_link(iax_peercallno_pvts, pvt);
01971 }

static void store_by_transfercallno ( struct chan_iax2_pvt *  pvt  )  [static]

Definition at line 1944 of file chan_iax2.c.

References ao2_link, ast_log(), LOG_ERROR, and chan_iax2_pvt::transfercallno.

Referenced by try_transfer().

01945 {
01946    if (!pvt->transfercallno) {
01947       ast_log(LOG_ERROR, "This should not be called without a transfer call number.\n");
01948       return;
01949    }
01950 
01951    ao2_link(iax_transfercallno_pvts, pvt);
01952 }

static int timing_read ( int *  id,
int  fd,
short  events,
void *  cbdata 
) [static]

Definition at line 8797 of file chan_iax2.c.

References iax2_trunk_peer::addr, ast_debug, ast_free, ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_mutex_destroy(), ast_mutex_lock(), ast_mutex_unlock(), ast_timer_ack(), ast_tvnow(), ast_verbose, iax2_trunk_expired(), iax2_trunk_peer::lock, send_trunk(), totalcalls, iax2_trunk_peer::trunkdataalloc, and iax2_trunk_peer::trunkdatalen.

Referenced by network_thread().

08798 {
08799    int res, processed = 0, totalcalls = 0;
08800    struct iax2_trunk_peer *tpeer = NULL, *drop = NULL;
08801    struct timeval now = ast_tvnow();
08802 
08803    if (iaxtrunkdebug)
08804       ast_verbose("Beginning trunk processing. Trunk queue ceiling is %d bytes per host\n", trunkmaxsize);
08805 
08806    if (timer) { 
08807       ast_timer_ack(timer, 1);
08808    }
08809 
08810    /* For each peer that supports trunking... */
08811    AST_LIST_LOCK(&tpeers);
08812    AST_LIST_TRAVERSE_SAFE_BEGIN(&tpeers, tpeer, list) {
08813       processed++;
08814       res = 0;
08815       ast_mutex_lock(&tpeer->lock);
08816       /* We can drop a single tpeer per pass.  That makes all this logic
08817          substantially easier */
08818       if (!drop && iax2_trunk_expired(tpeer, &now)) {
08819          /* Take it out of the list, but don't free it yet, because it
08820             could be in use */
08821          AST_LIST_REMOVE_CURRENT(list);
08822          drop = tpeer;
08823       } else {
08824          res = send_trunk(tpeer, &now);
08825          trunk_timed++; 
08826          if (iaxtrunkdebug)
08827             ast_verbose(" - Trunk peer (%s:%d) has %d call chunk%s in transit, %d bytes backloged and has hit a high water mark of %d bytes\n", ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), res, (res != 1) ? "s" : "", tpeer->trunkdatalen, tpeer->trunkdataalloc);
08828       }     
08829       totalcalls += res;   
08830       res = 0;
08831       ast_mutex_unlock(&tpeer->lock);
08832    }
08833    AST_LIST_TRAVERSE_SAFE_END;
08834    AST_LIST_UNLOCK(&tpeers);
08835 
08836    if (drop) {
08837       ast_mutex_lock(&drop->lock);
08838       /* Once we have this lock, we're sure nobody else is using it or could use it once we release it, 
08839          because by the time they could get tpeerlock, we've already grabbed it */
08840       ast_debug(1, "Dropping unused iax2 trunk peer '%s:%d'\n", ast_inet_ntoa(drop->addr.sin_addr), ntohs(drop->addr.sin_port));
08841       if (drop->trunkdata) {
08842          ast_free(drop->trunkdata);
08843          drop->trunkdata = NULL;
08844       }
08845       ast_mutex_unlock(&drop->lock);
08846       ast_mutex_destroy(&drop->lock);
08847       ast_free(drop);
08848       
08849    }
08850 
08851    if (iaxtrunkdebug)
08852       ast_verbose("Ending trunk processing with %d peers and %d call chunks processed\n", processed, totalcalls);
08853    iaxtrunkdebug = 0;
08854 
08855    return 1;
08856 }

static int transfercallno_pvt_cmp_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 13759 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, chan_iax2_pvt::frames_received, and match().

Referenced by load_objects().

13760 {
13761    struct chan_iax2_pvt *pvt = obj, *pvt2 = arg;
13762 
13763    /* The frames_received field is used to hold whether we're matching
13764     * against a full frame or not ... */
13765 
13766    return match(&pvt2->transfer, pvt2->transfercallno, pvt2->callno, pvt,
13767       pvt2->frames_received) ? CMP_MATCH | CMP_STOP : 0;
13768 }

static int transfercallno_pvt_hash_cb ( const void *  obj,
const int  flags 
) [static]

Definition at line 13752 of file chan_iax2.c.

References chan_iax2_pvt::transfercallno.

Referenced by load_objects().

13753 {
13754    const struct chan_iax2_pvt *pvt = obj;
13755 
13756    return pvt->transfercallno;
13757 }

static int transmit_trunk ( struct iax_frame *  f,
struct sockaddr_in *  sin,
int  sockfd 
) [static]

Definition at line 3152 of file chan_iax2.c.

References ast_debug, iax_frame::data, iax_frame::datalen, errno, and handle_error().

Referenced by send_trunk().

03153 {
03154    int res;
03155    res = sendto(sockfd, f->data, f->datalen, 0,(struct sockaddr *)sin,
03156                sizeof(*sin));
03157    if (res < 0) {
03158       ast_debug(1, "Received error: %s\n", strerror(errno));
03159       handle_error();
03160    } else
03161       res = 0;
03162    return res;
03163 }

static int try_firmware ( char *  s  )  [static]

Definition at line 2854 of file chan_iax2.c.

References ast_calloc, AST_FILE_MODE, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), ast_random(), ast_strlen_zero(), ast_iax2_firmware_header::chksum, ast_iax2_firmware_header::data, ast_iax2_firmware_header::datalen, iax_firmware::dead, ast_iax2_firmware_header::devname, errno, iax_firmware::fd, iax_firmware::fwh, IAX_FIRMWARE_MAGIC, last, LOG_WARNING, ast_iax2_firmware_header::magic, MD5Final(), MD5Init(), MD5Update(), iax_firmware::mmaplen, and ast_iax2_firmware_header::version.

Referenced by reload_firmware().

02855 {
02856    struct stat stbuf;
02857    struct iax_firmware *cur = NULL;
02858    int ifd, fd, res, len, chunk;
02859    struct ast_iax2_firmware_header *fwh, fwh2;
02860    struct MD5Context md5;
02861    unsigned char sum[16], buf[1024];
02862    char *s2, *last;
02863 
02864    if (!(s2 = alloca(strlen(s) + 100))) {
02865       ast_log(LOG_WARNING, "Alloca failed!\n");
02866       return -1;
02867    }
02868 
02869    last = strrchr(s, '/');
02870    if (last)
02871       last++;
02872    else
02873       last = s;
02874 
02875    snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, (unsigned long)ast_random());
02876 
02877    if ((res = stat(s, &stbuf) < 0)) {
02878       ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
02879       return -1;
02880    }
02881 
02882    /* Make sure it's not a directory */
02883    if (S_ISDIR(stbuf.st_mode))
02884       return -1;
02885    ifd = open(s, O_RDONLY);
02886    if (ifd < 0) {
02887       ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
02888       return -1;
02889    }
02890    fd = open(s2, O_RDWR | O_CREAT | O_EXCL, AST_FILE_MODE);
02891    if (fd < 0) {
02892       ast_log(LOG_WARNING, "Cannot open '%s' for writing: %s\n", s2, strerror(errno));
02893       close(ifd);
02894       return -1;
02895    }
02896    /* Unlink our newly created file */
02897    unlink(s2);
02898    
02899    /* Now copy the firmware into it */
02900    len = stbuf.st_size;
02901    while(len) {
02902       chunk = len;
02903       if (chunk > sizeof(buf))
02904          chunk = sizeof(buf);
02905       res = read(ifd, buf, chunk);
02906       if (res != chunk) {
02907          ast_log(LOG_WARNING, "Only read %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
02908          close(ifd);
02909          close(fd);
02910          return -1;
02911       }
02912       res = write(fd, buf, chunk);
02913       if (res != chunk) {
02914          ast_log(LOG_WARNING, "Only write %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
02915          close(ifd);
02916          close(fd);
02917          return -1;
02918       }
02919       len -= chunk;
02920    }
02921    close(ifd);
02922    /* Return to the beginning */
02923    lseek(fd, 0, SEEK_SET);
02924    if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
02925       ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
02926       close(fd);
02927       return -1;
02928    }
02929    if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
02930       ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
02931       close(fd);
02932       return -1;
02933    }
02934    if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
02935       ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
02936       close(fd);
02937       return -1;
02938    }
02939    if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero((char *)fwh2.devname)) {
02940       ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
02941       close(fd);
02942       return -1;
02943    }
02944    fwh = (struct ast_iax2_firmware_header*)mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 
02945    if (fwh == (void *) -1) {
02946       ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
02947       close(fd);
02948       return -1;
02949    }
02950    MD5Init(&md5);
02951    MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
02952    MD5Final(sum, &md5);
02953    if (memcmp(sum, fwh->chksum, sizeof(sum))) {
02954       ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
02955       munmap((void*)fwh, stbuf.st_size);
02956       close(fd);
02957       return -1;
02958    }
02959 
02960    AST_LIST_TRAVERSE(&firmwares, cur, list) {
02961       if (!strcmp((char *)cur->fwh->devname, (char *)fwh->devname)) {
02962          /* Found a candidate */
02963          if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
02964             /* The version we have on loaded is older, load this one instead */
02965             break;
02966          /* This version is no newer than what we have.  Don't worry about it.
02967             We'll consider it a proper load anyhow though */
02968          munmap((void*)fwh, stbuf.st_size);
02969          close(fd);
02970          return 0;
02971       }
02972    }
02973    
02974    if (!cur && ((cur = ast_calloc(1, sizeof(*cur))))) {
02975       cur->fd = -1;
02976       AST_LIST_INSERT_TAIL(&firmwares, cur, list);
02977    }
02978    
02979    if (cur) {
02980       if (cur->fwh)
02981          munmap((void*)cur->fwh, cur->mmaplen);
02982       if (cur->fd > -1)
02983          close(cur->fd);
02984       cur->fwh = fwh;
02985       cur->fd = fd;
02986       cur->mmaplen = stbuf.st_size;
02987       cur->dead = 0;
02988    }
02989    
02990    return 0;
02991 }

static int try_transfer ( struct chan_iax2_pvt *  pvt,
struct iax_ies *  ies 
) [static]

Definition at line 7954 of file chan_iax2.c.

References iax_ies::apparent_addr, AST_FRAME_IAX, ast_log(), iax_ie_data::buf, iax_ies::callno, IAX_COMMAND_TXCNT, iax_ie_append_int(), IAX_IE_TRANSFERID, inet_aton(), LOG_WARNING, iax_ie_data::pos, send_command_transfer(), store_by_transfercallno(), chan_iax2_pvt::transfer, TRANSFER_BEGIN, TRANSFER_NONE, chan_iax2_pvt::transfercallno, iax_ies::transferid, chan_iax2_pvt::transferid, and chan_iax2_pvt::transferring.

Referenced by socket_process().

07955 {
07956    int newcall = 0;
07957    char newip[256];
07958    struct iax_ie_data ied;
07959    struct sockaddr_in new;
07960    
07961    
07962    memset(&ied, 0, sizeof(ied));
07963    if (ies->apparent_addr)
07964       memmove(&new, ies->apparent_addr, sizeof(new));
07965    if (ies->callno)
07966       newcall = ies->callno;
07967    if (!newcall || !new.sin_addr.s_addr || !new.sin_port) {
07968       ast_log(LOG_WARNING, "Invalid transfer request\n");
07969       return -1;
07970    }
07971    pvt->transfercallno = newcall;
07972    memcpy(&pvt->transfer, &new, sizeof(pvt->transfer));
07973    inet_aton(newip, &pvt->transfer.sin_addr);
07974    pvt->transfer.sin_family = AF_INET;
07975    pvt->transferid = ies->transferid;
07976    /* only store by transfercallno if this is a new transfer,
07977     * just in case we get a duplicate TXREQ */
07978    if (pvt->transferring == TRANSFER_NONE) {
07979       store_by_transfercallno(pvt);
07980    }
07981    pvt->transferring = TRANSFER_BEGIN;
07982 
07983    if (ies->transferid)
07984       iax_ie_append_int(&ied, IAX_IE_TRANSFERID, ies->transferid);
07985    send_command_transfer(pvt, AST_FRAME_IAX, IAX_COMMAND_TXCNT, 0, ied.buf, ied.pos);
07986    return 0;
07987 }

static int uncompress_subclass ( unsigned char  csub  )  [static]

Definition at line 1485 of file chan_iax2.c.

References IAX_FLAG_SC_LOG, and IAX_MAX_SHIFT.

Referenced by decode_frame(), handle_call_token(), and socket_process().

01486 {
01487    /* If the SC_LOG flag is set, return 2^csub otherwise csub */
01488    if (csub & IAX_FLAG_SC_LOG) {
01489       /* special case for 'compressed' -1 */
01490       if (csub == 0xff)
01491          return -1;
01492       else
01493          return 1 << (csub & ~IAX_FLAG_SC_LOG & IAX_MAX_SHIFT);
01494    }
01495    else
01496       return csub;
01497 }

static void unlink_peer ( struct iax2_peer *  peer  )  [static]

Definition at line 8245 of file chan_iax2.c.

References ao2_unlink, ast_sched_thread_del, iax2_peer::expire, peer_unref(), and iax2_peer::pokeexpire.

Referenced by __expire_registry(), build_peer(), and prune_peers().

08246 {
08247    if (peer->expire > -1) {
08248       if (!ast_sched_thread_del(sched, peer->expire)) {
08249          peer->expire = -1;
08250          peer_unref(peer);
08251       }
08252    }
08253 
08254    if (peer->pokeexpire > -1) {
08255       if (!ast_sched_thread_del(sched, peer->pokeexpire)) {
08256          peer->pokeexpire = -1;
08257          peer_unref(peer);
08258       }
08259    }
08260 
08261    ao2_unlink(peers, peer);
08262 }

static int unload_module ( void   )  [static]

static void unlock_both ( unsigned short  callno0,
unsigned short  callno1 
) [static]

Definition at line 5174 of file chan_iax2.c.

References ast_mutex_unlock().

Referenced by iax2_bridge().

05175 {
05176    ast_mutex_unlock(&iaxsl[callno1]);
05177    ast_mutex_unlock(&iaxsl[callno0]);
05178 }

static void unwrap_timestamp ( struct iax_frame *  fr  )  [static]

Definition at line 3837 of file chan_iax2.c.

References iax_frame::af, ast_debug, AST_FRAME_VIDEO, iax_frame::callno, ast_frame::frametype, chan_iax2_pvt::last, and iax_frame::ts.

Referenced by schedule_delivery().

03838 {
03839    /* Video mini frames only encode the lower 15 bits of the session
03840     * timestamp, but other frame types (e.g. audio) encode 16 bits. */
03841    const int ts_shift = (fr->af.frametype == AST_FRAME_VIDEO) ? 15 : 16;
03842    const int lower_mask = (1 << ts_shift) - 1;
03843    const int upper_mask = ~lower_mask;
03844    const int last_upper = iaxs[fr->callno]->last & upper_mask;
03845 
03846    if ( (fr->ts & upper_mask) == last_upper ) {
03847       const int x = fr->ts - iaxs[fr->callno]->last;
03848       const int threshold = (ts_shift == 15) ? 25000 : 50000;
03849 
03850       if (x < -threshold) {
03851          /* Sudden big jump backwards in timestamp:
03852             What likely happened here is that miniframe timestamp has circled but we haven't
03853             gotten the update from the main packet.  We'll just pretend that we did, and
03854             update the timestamp appropriately. */
03855          fr->ts = (last_upper + (1 << ts_shift)) | (fr->ts & lower_mask);
03856          if (iaxdebug)
03857             ast_debug(1, "schedule_delivery: pushed forward timestamp\n");
03858       } else if (x > threshold) {
03859          /* Sudden apparent big jump forwards in timestamp:
03860             What's likely happened is this is an old miniframe belonging to the previous
03861             top 15 or 16-bit timestamp that has turned up out of order.
03862             Adjust the timestamp appropriately. */
03863          fr->ts = (last_upper - (1 << ts_shift)) | (fr->ts & lower_mask);
03864          if (iaxdebug)
03865             ast_debug(1, "schedule_delivery: pushed back timestamp\n");
03866       }
03867    }
03868 }

static void update_jbsched ( struct chan_iax2_pvt *  pvt  )  [static]

Definition at line 3872 of file chan_iax2.c.

References ast_tvdiff_ms(), ast_tvnow(), chan_iax2_pvt::callno, CALLNO_TO_PTR, get_from_jb(), iax2_sched_replace(), chan_iax2_pvt::jb, jb_next(), chan_iax2_pvt::jbid, and chan_iax2_pvt::rxcore.

Referenced by __get_from_jb(), and schedule_delivery().

03873 {
03874    int when;
03875    
03876    when = ast_tvdiff_ms(ast_tvnow(), pvt->rxcore);
03877    
03878    when = jb_next(pvt->jb) - when;
03879 
03880    if (when <= 0) {
03881       /* XXX should really just empty until when > 0.. */
03882       when = 1;
03883    }
03884    
03885    pvt->jbid = iax2_sched_replace(pvt->jbid, sched, when, get_from_jb, 
03886       CALLNO_TO_PTR(pvt->callno));
03887 }

static void update_max_nontrunk ( void   )  [static]

Definition at line 1872 of file chan_iax2.c.

References ast_debug, and TRUNK_CALL_START.

Referenced by __find_callno(), and make_trunk().

01873 {
01874    int max = 1;
01875    int x;
01876    /* XXX Prolly don't need locks here XXX */
01877    for (x=1;x<TRUNK_CALL_START - 1; x++) {
01878       if (iaxs[x])
01879          max = x + 1;
01880    }
01881    maxnontrunkcall = max;
01882    if (iaxdebug)
01883       ast_debug(1, "New max nontrunk callno is %d\n", max);
01884 }

static void update_max_trunk ( void   )  [static]

Definition at line 1855 of file chan_iax2.c.

References ARRAY_LEN, ast_debug, and TRUNK_CALL_START.

Referenced by iax2_destroy(), and make_trunk().

01856 {
01857    int max = TRUNK_CALL_START;
01858    int x;
01859 
01860    /* XXX Prolly don't need locks here XXX */
01861    for (x = TRUNK_CALL_START; x < ARRAY_LEN(iaxs) - 1; x++) {
01862       if (iaxs[x]) {
01863          max = x + 1;
01864       }
01865    }
01866 
01867    maxtrunkcall = max;
01868    if (iaxdebug)
01869       ast_debug(1, "New max trunk callno is %d\n", max);
01870 }

static int update_packet ( struct iax_frame *  f  )  [static]

Definition at line 3290 of file chan_iax2.c.

References build_rand_pad(), iax_frame::callno, iax_frame::data, iax_frame::datalen, iax_frame::dcallno, ast_iax2_full_hdr::dcallno, decode_frame(), iax_frame::ecx, iax_frame::encmethods, encrypt_frame(), IAX_FLAG_RETRANS, ast_iax2_full_hdr::iseqno, chan_iax2_pvt::iseqno, iax_frame::iseqno, iax_frame::mydcx, and iax_frame::semirand.

Referenced by __attempt_transmit().

03291 {
03292    /* Called with iaxsl lock held, and iaxs[callno] non-NULL */
03293    struct ast_iax2_full_hdr *fh = f->data;
03294    struct ast_frame af;
03295 
03296    /* if frame is encrypted. decrypt before updating it. */
03297    if (f->encmethods) {
03298       decode_frame(&f->mydcx, fh, &af, &f->datalen);
03299    }
03300    /* Mark this as a retransmission */
03301    fh->dcallno = ntohs(IAX_FLAG_RETRANS | f->dcallno);
03302    /* Update iseqno */
03303    f->iseqno = iaxs[f->callno]->iseqno;
03304    fh->iseqno = f->iseqno;
03305 
03306    /* Now re-encrypt the frame */
03307    if (f->encmethods) {
03308    /* since this is a retransmit frame, create a new random padding
03309     * before re-encrypting. */
03310       build_rand_pad(f->semirand, sizeof(f->semirand));
03311       encrypt_frame(&f->ecx, fh, f->semirand, &f->datalen);
03312    }
03313    return 0;
03314 }

static int update_registry ( struct sockaddr_in *  sin,
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 8356 of file chan_iax2.c.

References iax2_peer::addr, ast_app_inboxcount(), ast_db_del(), ast_db_put(), AST_DEVICE_UNAVAILABLE, AST_DEVICE_UNKNOWN, ast_devstate_changed(), ast_event_destroy(), ast_event_get_cached(), ast_event_get_ie_uint(), AST_EVENT_IE_CONTEXT, AST_EVENT_IE_END, AST_EVENT_IE_MAILBOX, AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_MWI, AST_FRAME_IAX, ast_inet_ntoa(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_thread_del, ast_strdupa, ast_strlen_zero(), ast_test_flag, ast_verb, iax_ie_data::buf, iax2_peer::cid_name, iax2_peer::cid_num, context, EVENT_FLAG_SYSTEM, iax2_peer::expire, expire_registry(), iax2_peer::expiry, find_peer(), iax2_datetime(), iax2_poke_peer(), iax2_regfunk, iax2_sched_add(), iax_check_version(), IAX_COMMAND_REGACK, IAX_HASCALLERID, IAX_IE_APPARENT_ADDR, iax_ie_append_addr(), iax_ie_append_int(), iax_ie_append_short(), iax_ie_append_str(), 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_STATE_AUTHENTICATED, IAX_TEMPONLY, inaddrcmp(), LOG_NOTICE, LOG_WARNING, mailbox, iax2_peer::mailbox, manager_event, iax2_peer::maxcallno, iax2_peer::name, peer_ref(), peer_unref(), peercnt_modify(), iax_ie_data::pos, realtime_update_peer(), register_peer_exten(), send_command_final(), iax2_peer::sockfd, strsep(), version, and iax2_peer::zonetag.

Referenced by socket_process().

08357 {
08358    /* Called from IAX thread only, with proper iaxsl lock */
08359    struct iax_ie_data ied;
08360    struct iax2_peer *p;
08361    int msgcount;
08362    char data[80];
08363    int version;
08364    const char *peer_name;
08365    int res = -1;
08366 
08367    memset(&ied, 0, sizeof(ied));
08368 
08369    peer_name = ast_strdupa(iaxs[callno]->peer);
08370 
08371    /* SLD: Another find_peer call during registration - this time when we are really updating our registration */
08372    ast_mutex_unlock(&iaxsl[callno]);
08373    if (!(p = find_peer(peer_name, 1))) {
08374       ast_mutex_lock(&iaxsl[callno]);
08375       ast_log(LOG_WARNING, "No such peer '%s'\n", peer_name);
08376       return -1;
08377    }
08378    ast_mutex_lock(&iaxsl[callno]);
08379    if (!iaxs[callno])
08380       goto return_unref;
08381 
08382    if (ast_test_flag((&globalflags), IAX_RTUPDATE) && (ast_test_flag(p, IAX_TEMPONLY|IAX_RTCACHEFRIENDS))) {
08383       if (sin->sin_addr.s_addr) {
08384          time_t nowtime;
08385          time(&nowtime);
08386          realtime_update_peer(peer_name, sin, nowtime);
08387       } else {
08388          realtime_update_peer(peer_name, sin, 0);
08389       }
08390    }
08391    if (inaddrcmp(&p->addr, sin)) {
08392       if (iax2_regfunk)
08393          iax2_regfunk(p->name, 1);
08394 
08395       /* modify entry in peercnts table as _not_ registered */
08396       peercnt_modify(0, 0, &p->addr);
08397 
08398       /* Stash the IP address from which they registered */
08399       memcpy(&p->addr, sin, sizeof(p->addr));
08400 
08401       snprintf(data, sizeof(data), "%s:%d:%d", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), p->expiry);
08402       if (!ast_test_flag(p, IAX_TEMPONLY) && sin->sin_addr.s_addr) {
08403          ast_db_put("IAX/Registry", p->name, data);
08404          ast_verb(3, "Registered IAX2 '%s' (%s) at %s:%d\n", p->name,
08405                    ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
08406          manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: IAX2\r\nPeer: IAX2/%s\r\nPeerStatus: Registered\r\n", p->name);
08407          register_peer_exten(p, 1);
08408          ast_devstate_changed(AST_DEVICE_UNKNOWN, "IAX2/%s", p->name); /* Activate notification */
08409       } else if (!ast_test_flag(p, IAX_TEMPONLY)) {
08410          ast_verb(3, "Unregistered IAX2 '%s' (%s)\n", p->name,
08411                    ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED");
08412          manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "ChannelType: IAX2\r\nPeer: IAX2/%s\r\nPeerStatus: Unregistered\r\n", p->name);
08413          register_peer_exten(p, 0);
08414          ast_db_del("IAX/Registry", p->name);
08415          ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "IAX2/%s", p->name); /* Activate notification */
08416       }
08417       /* Update the host */
08418       /* Verify that the host is really there */
08419       iax2_poke_peer(p, callno);
08420    }
08421 
08422    /* modify entry in peercnts table as registered */
08423    if (p->maxcallno) {
08424       peercnt_modify(1, p->maxcallno, &p->addr);
08425    }
08426 
08427    /* Make sure our call still exists, an INVAL at the right point may make it go away */
08428    if (!iaxs[callno]) {
08429       res = -1;
08430       goto return_unref;
08431    }
08432 
08433    /* Store socket fd */
08434    p->sockfd = fd;
08435    /* Setup the expiry */
08436    if (p->expire > -1) {
08437       if (!ast_sched_thread_del(sched, p->expire)) {
08438          p->expire = -1;
08439          peer_unref(p);
08440       }
08441    }
08442    /* treat an unspecified refresh interval as the minimum */
08443    if (!refresh)
08444       refresh = min_reg_expire;
08445    if (refresh > max_reg_expire) {
08446       ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
08447          p->name, max_reg_expire, refresh);
08448       p->expiry = max_reg_expire;
08449    } else if (refresh < min_reg_expire) {
08450       ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
08451          p->name, min_reg_expire, refresh);
08452       p->expiry = min_reg_expire;
08453    } else {
08454       p->expiry = refresh;
08455    }
08456    if (p->expiry && sin->sin_addr.s_addr) {
08457       p->expire = iax2_sched_add(sched, (p->expiry + 10) * 1000, expire_registry, peer_ref(p));
08458       if (p->expire == -1)
08459          peer_unref(p);
08460    }
08461    iax_ie_append_str(&ied, IAX_IE_USERNAME, p->name);
08462    iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime(p->zonetag));
08463    if (sin->sin_addr.s_addr) {
08464       iax_ie_append_short(&ied, IAX_IE_REFRESH, p->expiry);
08465       iax_ie_append_addr(&ied, IAX_IE_APPARENT_ADDR, &p->addr);
08466       if (!ast_strlen_zero(p->mailbox)) {
08467          struct ast_event *event;
08468          int new, old;
08469          char *mailbox, *context;
08470 
08471          context = mailbox = ast_strdupa(p->mailbox);
08472          strsep(&context, "@");
08473          if (ast_strlen_zero(context))
08474             context = "default";
08475 
08476          event = ast_event_get_cached(AST_EVENT_MWI,
08477             AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
08478             AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
08479             AST_EVENT_IE_END);
08480          if (event) {
08481             new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
08482             old = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
08483             ast_event_destroy(event);
08484          } else { /* Fall back on checking the mailbox directly */
08485             ast_app_inboxcount(p->mailbox, &new, &old);
08486          }
08487 
08488          if (new > 255) {
08489             new = 255;
08490          }
08491          if (old > 255) {
08492             old = 255;
08493          }
08494          msgcount = (old << 8) | new;
08495 
08496          iax_ie_append_short(&ied, IAX_IE_MSGCOUNT, msgcount);
08497       }
08498       if (ast_test_flag(p, IAX_HASCALLERID)) {
08499          iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, p->cid_num);
08500          iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, p->cid_name);
08501       }
08502    }
08503    version = iax_check_version(devtype);
08504    if (version) 
08505       iax_ie_append_short(&ied, IAX_IE_FIRMWAREVER, version);
08506 
08507    res = 0;
08508 
08509 return_unref:
08510    peer_unref(p);
08511 
08512    return res ? res : send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGACK, 0, ied.buf, ied.pos, -1);
08513 }

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 1532 of file chan_iax2.c.

References CMP_MATCH, CMP_STOP, and iax2_user::name.

Referenced by load_objects().

01533 {
01534    struct iax2_user *user = obj, *user2 = arg;
01535 
01536    return !strcmp(user->name, user2->name) ? CMP_MATCH | CMP_STOP : 0;
01537 }

static int user_delme_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 12426 of file chan_iax2.c.

References ast_set_flag, and IAX_DELME.

Referenced by delete_users().

12427 {
12428    struct iax2_user *user = obj;
12429 
12430    ast_set_flag(user, IAX_DELME);
12431 
12432    return 0;
12433 }

static void user_destructor ( void *  obj  )  [static]

Definition at line 12163 of file chan_iax2.c.

References ast_free_ha(), ast_string_field_free_memory, ast_variables_destroy(), iax2_user::contexts, free_context(), iax2_user::ha, and iax2_user::vars.

Referenced by build_user().

12164 {
12165    struct iax2_user *user = obj;
12166 
12167    ast_free_ha(user->ha);
12168    free_context(user->contexts);
12169    if(user->vars) {
12170       ast_variables_destroy(user->vars);
12171       user->vars = NULL;
12172    }
12173    ast_string_field_free_memory(user);
12174 }

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 1522 of file chan_iax2.c.

References ast_str_hash(), and iax2_user::name.

Referenced by load_objects().

01523 {
01524    const struct iax2_user *user = obj;
01525 
01526    return ast_str_hash(user->name);
01527 }

static struct iax2_user* user_ref ( struct iax2_user *  user  )  [static, read]

Definition at line 1579 of file chan_iax2.c.

References ao2_ref.

01580 {
01581    ao2_ref(user, +1);
01582    return user;
01583 }

static struct iax2_user* user_unref ( struct iax2_user *  user  )  [static, read]

static void vnak_retransmit ( int  callno,
int  last 
) [static]

Definition at line 8712 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, iax_frame::callno, f, iax_frame::oseqno, iax_frame::retries, and send_packet().

Referenced by socket_process().

08713 {
08714    struct iax_frame *f;
08715 
08716    AST_LIST_LOCK(&frame_queue);
08717    AST_LIST_TRAVERSE(&frame_queue, f, list) {
08718       /* Send a copy immediately */
08719       if ((f->callno == callno) && iaxs[f->callno] &&
08720          ((unsigned char ) (f->oseqno - last) < 128) &&
08721          (f->retries >= 0)) {
08722          send_packet(f);
08723       }
08724    }
08725    AST_LIST_UNLOCK(&frame_queue);
08726 }

static int wait_for_peercallno ( struct chan_iax2_pvt *  pvt  )  [static]

Note:
expects the pvt to be locked

Definition at line 5028 of file chan_iax2.c.

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

Referenced by iax2_indicate(), and iax2_setoption().

05029 {
05030    unsigned short callno = pvt->callno;
05031 
05032    if (!pvt->peercallno) {
05033       /* We don't know the remote side's call number, yet.  :( */
05034       int count = 10;
05035       while (count-- && pvt && !pvt->peercallno) {
05036          DEADLOCK_AVOIDANCE(&iaxsl[callno]);
05037          pvt = iaxs[callno];
05038       }
05039       if (!pvt->peercallno) {
05040          return -1;
05041       }
05042    }
05043 
05044    return 0;
05045 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, } [static]

Definition at line 13939 of file chan_iax2.c.

char accountcode[AST_MAX_ACCOUNT_CODE] [static]

int adsi = 0 [static]

Definition at line 332 of file chan_iax2.c.

int amaflags = 0 [static]

Definition at line 331 of file chan_iax2.c.

Definition at line 13939 of file chan_iax2.c.

int authdebug = 1 [static]

Definition at line 242 of file chan_iax2.c.

int autokill = 0 [static]

Definition at line 243 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 837 of file chan_iax2.c.

struct ao2_container* callno_pool [static]

table of available call numbers

Definition at line 796 of file chan_iax2.c.

const unsigned int CALLNO_POOL_BUCKETS = 2699 [static]

Definition at line 801 of file chan_iax2.c.

struct ao2_container* callno_pool_trunk [static]

table of available trunk call numbers

Definition at line 799 of file chan_iax2.c.

struct ao2_container* calltoken_ignores [static]

Table containing ip addresses not requiring calltoken validation

Definition at line 840 of file chan_iax2.c.

struct ast_cli_entry cli_iax2[] [static]

Definition at line 13604 of file chan_iax2.c.

unsigned int cos

Definition at line 253 of file chan_iax2.c.

struct sockaddr_in debugaddr [static]

Definition at line 1044 of file chan_iax2.c.

Referenced by handle_cli_iax2_set_debug(), iax_outputframe(), and reload_config().

uint16_t DEFAULT_MAXCALLNO_LIMIT = 2048 [static]

Definition at line 842 of file chan_iax2.c.

uint16_t DEFAULT_MAXCALLNO_LIMIT_NONVAL = 8192 [static]

Definition at line 844 of file chan_iax2.c.

char default_parkinglot[AST_MAX_CONTEXT] [static]

Definition at line 223 of file chan_iax2.c.

int defaultsockfd = -1 [static]

Definition at line 265 of file chan_iax2.c.

int delayreject = 0 [static]

Definition at line 333 of file chan_iax2.c.

int global_max_trunk_mtu [static]

Maximum MTU, 0 if not used

Definition at line 218 of file chan_iax2.c.

uint16_t global_maxcallno [static]

Definition at line 846 of file chan_iax2.c.

uint16_t global_maxcallno_nonval [static]

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

Definition at line 849 of file chan_iax2.c.

int global_rtautoclear = 120 [static]

Definition at line 386 of file chan_iax2.c.

struct ast_flags globalflags = { 0 } [static]

Definition at line 336 of file chan_iax2.c.

int iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH [static]

Definition at line 314 of file chan_iax2.c.

int iax2_encryption = 0 [static]

Definition at line 334 of file chan_iax2.c.

int(* iax2_regfunk)(const char *username, int onoff) = NULL

struct ast_switch iax2_switch [static]

Definition at line 13490 of file chan_iax2.c.

struct ast_channel_tech iax2_tech [static]

Definition at line 1158 of file chan_iax2.c.

Initial value:

 {
   .type = "IAX2_VARIABLE",
   .duplicate = iax2_dup_variable_datastore,
   .destroy = iax2_free_variable_datastore,
}

Definition at line 1202 of file chan_iax2.c.

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 1021 of file chan_iax2.c.

Another container of iax2_pvt structures.

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

Definition at line 1037 of file chan_iax2.c.

int iaxactivethreadcount = 0 [static]

Definition at line 585 of file chan_iax2.c.

Referenced by iax2_process_thread(), and iax2_process_thread_cleanup().

int iaxcompat = 0 [static]

Definition at line 244 of file chan_iax2.c.

int iaxdebug = 0 [static]

Definition at line 316 of file chan_iax2.c.

int iaxdefaultdpcache = 10 * 60 [static]

Definition at line 247 of file chan_iax2.c.

int iaxdefaulttimeout = 5 [static]

Definition at line 249 of file chan_iax2.c.

int iaxdynamicthreadcount = 0 [static]

Definition at line 583 of file chan_iax2.c.

Referenced by find_idle_thread(), and iax2_process_thread().

int iaxdynamicthreadnum = 0 [static]

Definition at line 584 of file chan_iax2.c.

Referenced by find_idle_thread().

int iaxmaxthreadcount = DEFAULT_MAX_THREAD_COUNT [static]

Definition at line 582 of file chan_iax2.c.

Referenced by find_idle_thread(), and set_config().

Initial value:

 {
   .name = "IAXPEER",
   .read = function_iaxpeer,
}

Definition at line 13412 of file chan_iax2.c.

struct chan_iax2_pvt* iaxs[IAX_MAX_CALLS+1] [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 1010 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 1030 of file chan_iax2.c.

int iaxthreadcount = DEFAULT_THREAD_COUNT [static]

Definition at line 581 of file chan_iax2.c.

Referenced by handle_cli_iax2_show_threads(), set_config(), and start_network_thread().

int iaxtrunkdebug = 0 [static]

Definition at line 318 of file chan_iax2.c.

Initial value:

 {
   .name = "IAXVAR",
   .read = acf_iaxvar_read,
   .write = acf_iaxvar_write,
}

Definition at line 9463 of file chan_iax2.c.

struct io_context* io [static]

Definition at line 311 of file chan_iax2.c.

int jittertargetextra = 40 [static]

Definition at line 235 of file chan_iax2.c.

int lagrq_time = 10 [static]

Definition at line 231 of file chan_iax2.c.

char language[MAX_LANGUAGE] = "" [static]

Definition at line 225 of file chan_iax2.c.

int last_authmethod = 0 [static]

Definition at line 245 of file chan_iax2.c.

const time_t MAX_CALLTOKEN_DELAY = 10 [static]

Definition at line 814 of file chan_iax2.c.

int max_reg_expire [static]

Definition at line 257 of file chan_iax2.c.

int max_retries = 4 [static]

Definition at line 229 of file chan_iax2.c.

int maxauthreq = 3 [static]

Definition at line 228 of file chan_iax2.c.

int maxjitterbuffer = 1000 [static]

Definition at line 232 of file chan_iax2.c.

int maxjitterinterps = 10 [static]

Definition at line 234 of file chan_iax2.c.

int maxnontrunkcall = 1 [static]

Definition at line 1111 of file chan_iax2.c.

int maxtrunkcall = TRUNK_CALL_START [static]

Definition at line 1110 of file chan_iax2.c.

int min_reg_expire [static]

Definition at line 256 of file chan_iax2.c.

char mohinterpret[MAX_MUSICCLASS] [static]

Definition at line 329 of file chan_iax2.c.

char mohsuggest[MAX_MUSICCLASS] [static]

struct ast_netsock_list* netsock [static]

Definition at line 263 of file chan_iax2.c.

pthread_t netthreadid = AST_PTHREADT_NULL [static]

Definition at line 338 of file chan_iax2.c.

struct ast_netsock_list* outsock [static]

used if sourceaddress specified and bindaddr == INADDR_ANY

Definition at line 264 of file chan_iax2.c.

char* papp = "IAX2Provision" [static]

Definition at line 11389 of file chan_iax2.c.

struct ao2_container* peercnts [static]

Table containing peercnt objects for every ip address consuming a callno

Definition at line 834 of file chan_iax2.c.

struct ao2_container* peers [static]

Definition at line 828 of file chan_iax2.c.

int ping_time = 21 [static]

Definition at line 230 of file chan_iax2.c.

struct ast_codec_pref prefs [static]

struct { ... } qos [static]

Referenced by peer_set_srcaddr(), and set_config().

int randomcalltokendata [static]

Definition at line 812 of file chan_iax2.c.

char regcontext[AST_MAX_CONTEXT] = "" [static]

Definition at line 226 of file chan_iax2.c.

int resyncthreshold = 1000 [static]

Definition at line 233 of file chan_iax2.c.

struct ast_sched_thread* sched [static]

Definition at line 312 of file chan_iax2.c.

int srvlookup = 0 [static]

Definition at line 259 of file chan_iax2.c.

Referenced by build_peer().

const char tdesc[] = "Inter Asterisk eXchange Driver (Ver 2)" [static]

Definition at line 211 of file chan_iax2.c.

int test_losspct = 0 [static]

Definition at line 320 of file chan_iax2.c.

struct ast_timer* timer [static]

unsigned int tos

Definition at line 252 of file chan_iax2.c.

uint16_t total_nonval_callno_used = 0 [static]

Definition at line 851 of file chan_iax2.c.

int trunk_maxmtu [static]

Definition at line 219 of file chan_iax2.c.

int trunk_nmaxmtu [static]

Trunk MTU statistics

Definition at line 219 of file chan_iax2.c.

int trunk_timed [static]

Definition at line 219 of file chan_iax2.c.

int trunk_untimed [static]

Definition at line 219 of file chan_iax2.c.

int trunkfreq = 20 [static]

Definition at line 239 of file chan_iax2.c.

int trunkmaxsize = MAX_TRUNKDATA [static]

Definition at line 240 of file chan_iax2.c.

struct ao2_container* users [static]

Definition at line 831 of file chan_iax2.c.


Generated on Wed Aug 11 01:07:12 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9