Fri Jul 3 20:37:13 2009

Asterisk developer's documentation


chan_iax2.c File Reference

Implementation of Inter-Asterisk eXchange Version 2. More...

#include "asterisk.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.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 <string.h>
#include <strings.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <regex.h>
#include <sys/ioctl.h>
#include "asterisk/dahdi_compat.h"
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/config.h"
#include "asterisk/options.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/astobj2.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  ast_firmware_list
struct  ast_iax2_queue
struct  chan_iax2_pvt
struct  create_addr_info
struct  dpreq_data
struct  dynamic_list
struct  iax2_context
struct  iax2_dpcache
struct  iax2_peer
struct  iax2_pkt_buf
struct  iax2_registry
struct  iax2_thread
struct  iax2_trunk_peer
struct  iax2_user
struct  iax_dual
struct  iax_firmware
struct  iax_rr
struct  idle_list
struct  parsed_dial_string
struct  registrations

Defines

#define CACHE_FLAG_CANEXIST   (1 << 2)
#define CACHE_FLAG_EXISTS   (1 << 0)
#define CACHE_FLAG_MATCHMORE   (1 << 7)
#define CACHE_FLAG_NONEXISTENT   (1 << 1)
#define CACHE_FLAG_PENDING   (1 << 3)
#define CACHE_FLAG_TIMEOUT   (1 << 4)
#define CACHE_FLAG_TRANSMITTED   (1 << 5)
#define CACHE_FLAG_UNKNOWN   (1 << 6)
#define CALLNO_TO_PTR(a)   ((void *)(unsigned long)(a))
#define DEBUG_SCHED_MULTITHREAD
#define DEBUG_SUPPORT
#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\n"
#define FORMAT   "%-20.20s %-6.6s %-10.10s %-20.20s %8d %s\n"
#define FORMAT   "%-15.15s %-15d %-15d\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\n"
#define FORMAT2   "%-20.20s %-6.6s %-10.10s %-20.20s %8.8s %s\n"
#define FORMAT2   "%-15.15s %-15.15s %-15.15s\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_CAPABILITY_FULLBANDWIDTH   0xFFFF
#define IAX_CAPABILITY_LOWBANDWIDTH
#define IAX_CAPABILITY_LOWFREE
#define IAX_CAPABILITY_MEDBANDWIDTH
#define IAX_IOSTATE_IDLE   0
#define IAX_IOSTATE_PROCESSING   2
#define IAX_IOSTATE_READY   1
#define IAX_IOSTATE_SCHEDREADY   3
#define IAX_TYPE_DYNAMIC   2
#define IAX_TYPE_POOL   1
#define IPTOS_MINCOST   0x02
#define MAX_JITTER_BUFFER   50
#define MAX_PEER_BUCKETS   1
#define MAX_RETRY_TIME   10000
#define MAX_TIMESTAMP_SKEW   160
#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 NEW_ALLOW   1
#define NEW_FORCE   2
#define NEW_PREVENT   0
#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   ARRAY_LEN(iaxs) / 2
#define TS_GAP_FOR_JB_RESYNC   5000

Enumerations

enum  { IAX_STATE_STARTED = (1 << 0), IAX_STATE_AUTHENTICATED = (1 << 1), IAX_STATE_TBD = (1 << 2), IAX_STATE_UNCHANGED = (1 << 3) }
enum  {
  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)
}
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 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, aes_encrypt_ctx *ecx, aes_decrypt_ctx *dcx)
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 struct iax2_context * build_context (char *context)
static void build_enc_keys (const unsigned char *digest, aes_encrypt_ctx *ecx, aes_decrypt_ctx *dcx)
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 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 *tv)
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 int complete_dpreply (struct chan_iax2_pvt *pvt, struct iax_ies *ies)
static char * complete_iax2_show_peer (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 decode_frame (aes_decrypt_ctx *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 int encrypt_frame (aes_encrypt_ctx *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 unsigned int fix_peerts (struct timeval *tv, int callno, unsigned int ts)
static void free_context (struct iax2_context *con)
static int function_iaxpeer (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
static int get_auth_methods (char *value)
static int get_encrypt_methods (const char *s)
static int get_from_jb (const void *p)
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 iax2_answer (struct ast_channel *c)
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_debug (int fd, int argc, char *argv[])
static int iax2_do_jb_debug (int fd, int argc, char *argv[])
static int iax2_do_register (struct iax2_registry *reg)
static int iax2_do_register_s (const void *data)
static int iax2_do_trunk_debug (int fd, int argc, char *argv[])
static void iax2_dprequest (struct iax2_dpcache *dp, int callno)
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 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_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_no_debug (int fd, int argc, char *argv[])
static int iax2_no_jb_debug (int fd, int argc, char *argv[])
static int iax2_no_trunk_debug (int fd, int argc, char *argv[])
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_prov_cmd (int fd, int argc, char *argv[])
static char * iax2_prov_complete_template_3rd (const char *line, const char *word, int pos, int state)
static int iax2_provision (struct sockaddr_in *end, int sockfd, char *dest, const char *template, int force)
static int iax2_prune_realtime (int fd, int argc, char *argv[])
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 (char *value, int lineno)
static int iax2_reload (int fd, int argc, char *argv[])
static struct ast_channel * iax2_request (const char *type, int format, void *data, int *cause)
static int iax2_sched_add (struct sched_context *con, int when, ast_sched_cb callback, const void *data)
static int iax2_send (struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
static int iax2_sendhtml (struct ast_channel *c, int subclass, const char *data, int datalen)
static int iax2_sendimage (struct ast_channel *c, struct ast_frame *img)
static int iax2_sendtext (struct ast_channel *c, const char *text)
static int iax2_setoption (struct ast_channel *c, int option, void *data, int datalen)
static int iax2_show_cache (int fd, int argc, char *argv[])
static int iax2_show_channels (int fd, int argc, char *argv[])
static int iax2_show_firmware (int fd, int argc, char *argv[])
static int iax2_show_netstats (int fd, int argc, char *argv[])
static int iax2_show_peer (int fd, int argc, char *argv[])
 Show one peer in detail.
static int iax2_show_peers (int fd, int argc, char *argv[])
static int iax2_show_registry (int fd, int argc, char *argv[])
static int iax2_show_stats (int fd, int argc, char *argv[])
static int iax2_show_threads (int fd, int argc, char *argv[])
static int iax2_show_users (int fd, int argc, char *argv[])
static int iax2_start_transfer (unsigned short callno0, unsigned short callno1, int mediaonly)
static int iax2_test_losspct (int fd, int argc, char *argv[])
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 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 void lock_both (unsigned short callno0, unsigned short callno1)
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_peers (struct mansession *s, const struct message *m)
static int match (struct sockaddr_in *sin, unsigned short callno, unsigned short dcallno, struct chan_iax2_pvt *cur, int check_dcallno)
static void memcpy_decrypt (unsigned char *dst, const unsigned char *src, int len, aes_decrypt_ctx *dcx)
static void memcpy_encrypt (unsigned char *dst, const unsigned char *src, int len, aes_encrypt_ctx *ecx)
static void merge_encryption (struct chan_iax2_pvt *p, unsigned int enc)
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 void poke_all_peers (void)
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 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 save_rr (struct iax_frame *fr, struct iax_ies *ies)
static void * sched_thread (void *ignore)
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)
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 int send_trunk (struct iax2_trunk_peer *tpeer, struct timeval *now)
static int set_config (char *config_file, int reload)
 Load configuration.
static void set_config_destroy (void)
static void set_timing (void)
static void signal_condition (ast_mutex_t *lock, ast_cond_t *cond)
static int socket_process (struct iax2_thread *thread)
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 int timing_read (int *id, int fd, short events, void *cbdata)
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 | AST_MODFLAG_BUILDSUM, .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 const struct
ast_module_info * 
ast_module_info = &__mod_info
static int authdebug = 1
static int autokill = 0
static struct ast_cli_entry cli_iax2 []
static struct ast_cli_entry cli_iax2_jb_debug_deprecated
static struct ast_cli_entry cli_iax2_no_debug_deprecated
static struct ast_cli_entry cli_iax2_no_jb_debug_deprecated
static struct ast_cli_entry cli_iax2_no_trunk_debug_deprecated
static struct ast_cli_entry cli_iax2_trunk_debug_deprecated
static char context [80] = "default"
static char debug_jb_usage []
static char debug_trunk_usage []
static char debug_usage []
static int defaultsockfd = -1
static int delayreject = 0
static struct iax2_dpcache * dpcache
static ast_mutex_t dpcache_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static int global_rtautoclear = 120
static struct ast_flags globalflags = { 0 }
static int iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH
static int iax2_encryption = 0
enum { ... }  iax2_flags
int(* iax2_regfunk )(const char *username, int onoff) = NULL
static char iax2_reload_usage []
enum { ... }  iax2_state
static struct ast_switch iax2_switch
static struct ast_channel_tech iax2_tech
static char iax2_test_losspct_usage []
static struct ao2_container * iax_peercallno_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 ast_iax2_queue iaxq
static struct chan_iax2_pvt * iaxs [IAX_MAX_CALLS]
static ast_mutex_t iaxsl [ARRAY_LEN(iaxs)]
static int iaxthreadcount = DEFAULT_THREAD_COUNT
static int iaxtrunkdebug = 0
static struct io_context * io
static int lagrq_time = 10
static char language [MAX_LANGUAGE] = ""
static struct timeval lastused [ARRAY_LEN(iaxs)]
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 char no_debug_jb_usage []
static char no_debug_trunk_usage []
static char no_debug_usage []
static struct ast_netsock_list * outsock
static char * papp = "IAX2Provision"
static char * pdescrip
static struct ao2_container * peers
static int ping_time = 21
static struct ast_codec_pref prefs
static char prune_realtime_usage []
static char * psyn = "Provision a calling IAXy with a given template"
static char regcontext [AST_MAX_CONTEXT] = ""
static int resyncthreshold = 1000
static struct sched_context * sched
static ast_cond_t sched_cond
static ast_mutex_t sched_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static pthread_t schedthreadid = AST_PTHREADT_NULL
static char show_cache_usage []
static char show_channels_usage []
static char show_firmware_usage []
static char show_netstats_usage []
static char show_peer_usage []
static char show_peers_usage []
static char show_prov_usage []
static char show_reg_usage []
static char show_stats_usage []
static char show_threads_usage []
static char show_users_usage []
static const char tdesc [] = "Inter Asterisk eXchange Driver (Ver 2)"
static int test_losspct = 0
static int timingfd = -1
static unsigned int tos = 0
static ast_mutex_t tpeerlock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static struct iax2_trunk_peer * tpeers
static int trunkfreq = 20
static struct ao2_container * users
static struct ast_firmware_list waresl


Detailed Description

Implementation of Inter-Asterisk eXchange Version 2.

Author:
Mark Spencer <markster@digium.com>
See also

Definition in file chan_iax2.c.


Define Documentation

#define CACHE_FLAG_CANEXIST   (1 << 2)

Extension can exist

Definition at line 663 of file chan_iax2.c.

Referenced by complete_dpreply(), iax2_canmatch(), and iax2_show_cache().

#define CACHE_FLAG_EXISTS   (1 << 0)

Extension exists

Definition at line 659 of file chan_iax2.c.

Referenced by complete_dpreply(), iax2_exec(), iax2_exists(), and iax2_show_cache().

#define CACHE_FLAG_MATCHMORE   (1 << 7)

Matchmore

Definition at line 673 of file chan_iax2.c.

Referenced by complete_dpreply(), iax2_matchmore(), and iax2_show_cache().

#define CACHE_FLAG_NONEXISTENT   (1 << 1)

Extension is nonexistent

Definition at line 661 of file chan_iax2.c.

Referenced by complete_dpreply(), and iax2_show_cache().

#define CACHE_FLAG_PENDING   (1 << 3)

Waiting to hear back response

Definition at line 665 of file chan_iax2.c.

Referenced by complete_dpreply(), find_cache(), and iax2_show_cache().

#define CACHE_FLAG_TIMEOUT   (1 << 4)

Timed out

Definition at line 667 of file chan_iax2.c.

Referenced by find_cache(), and iax2_show_cache().

#define CACHE_FLAG_TRANSMITTED   (1 << 5)

Request transmitted

Definition at line 669 of file chan_iax2.c.

Referenced by iax2_dprequest(), iax2_show_cache(), and socket_process().

#define CACHE_FLAG_UNKNOWN   (1 << 6)

Timeout

Definition at line 671 of file chan_iax2.c.

Referenced by complete_dpreply(), and iax2_show_cache().

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

Definition at line 123 of file chan_iax2.c.

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

#define DEBUG_SCHED_MULTITHREAD

Definition at line 111 of file chan_iax2.c.

#define DEBUG_SUPPORT

Definition at line 131 of file chan_iax2.c.

#define DEFAULT_DROP   3

Definition at line 129 of file chan_iax2.c.

#define DEFAULT_FREQ_NOTOK   10 * 1000

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

Referenced by build_peer(), and handle_response_peerpoke().

#define DEFAULT_MAX_THREAD_COUNT   100

Definition at line 126 of file chan_iax2.c.

#define DEFAULT_MAXMS   2000

Definition at line 194 of file chan_iax2.c.

#define DEFAULT_RETRY_TIME   1000

Definition at line 127 of file chan_iax2.c.

Referenced by __find_callno(), and complete_transfer().

#define DEFAULT_THREAD_COUNT   10

Definition at line 125 of file chan_iax2.c.

#define DEFAULT_TRUNKDATA   640 * 10

40ms, uncompressed linear * 10 channels

Definition at line 439 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\n"

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

#define FORMAT   "%-15.15s %-15d %-15d\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\n"

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

#define FORMAT2   "%-15.15s %-15.15s %-15.15s\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 136 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 360 of file chan_iax2.c.

Referenced by iax2_trunk_queue().

#define IAX_CAPABILITY_FULLBANDWIDTH   0xFFFF

Definition at line 177 of file chan_iax2.c.

Referenced by cache_get_callno_locked(), and set_config().

#define IAX_CAPABILITY_LOWBANDWIDTH

Value:

Definition at line 185 of file chan_iax2.c.

Referenced by set_config().

#define IAX_CAPABILITY_LOWFREE

Value:

Definition at line 190 of file chan_iax2.c.

#define IAX_CAPABILITY_MEDBANDWIDTH

Value:

Definition at line 179 of file chan_iax2.c.

Referenced by set_config().

#define IAX_IOSTATE_IDLE   0

Definition at line 694 of file chan_iax2.c.

Referenced by iax2_process_thread(), and socket_read().

#define IAX_IOSTATE_PROCESSING   2

Definition at line 696 of file chan_iax2.c.

Referenced by iax2_process_thread().

#define IAX_IOSTATE_READY   1

Definition at line 695 of file chan_iax2.c.

Referenced by iax2_process_thread(), and socket_read().

#define IAX_IOSTATE_SCHEDREADY   3

Definition at line 697 of file chan_iax2.c.

Referenced by __schedule_action(), and iax2_process_thread().

#define IAX_TYPE_DYNAMIC   2

#define IAX_TYPE_POOL   1

Definition at line 699 of file chan_iax2.c.

Referenced by start_network_thread().

#define IPTOS_MINCOST   0x02

Definition at line 114 of file chan_iax2.c.

#define MAX_JITTER_BUFFER   50

Definition at line 436 of file chan_iax2.c.

#define MAX_PEER_BUCKETS   1

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

Referenced by load_module(), and set_config().

#define MAX_RETRY_TIME   10000

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

#define MAX_TRUNKDATA   640 * 200

40ms, uncompressed linear * 200 channels

Definition at line 440 of file chan_iax2.c.

Referenced by iax2_trunk_queue(), and timing_read().

#define MAX_USER_BUCKETS   MAX_PEER_BUCKETS

Definition at line 650 of file chan_iax2.c.

Referenced by load_module().

#define MEMORY_SIZE   100

Definition at line 128 of file chan_iax2.c.

#define MIN_JITTER_BUFFER   10

Definition at line 437 of file chan_iax2.c.

#define MIN_RETRY_TIME   100

Definition at line 433 of file chan_iax2.c.

Referenced by iax2_send().

#define MIN_REUSE_TIME   60

Definition at line 133 of file chan_iax2.c.

Referenced by __find_callno(), and make_trunk().

#define NEW_ALLOW   1

Definition at line 1453 of file chan_iax2.c.

Referenced by __find_callno(), and socket_process().

#define NEW_FORCE   2

#define NEW_PREVENT   0

Definition at line 1452 of file chan_iax2.c.

Referenced by socket_process().

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

#define SCHED_MULTITHREADED

Definition at line 107 of file chan_iax2.c.

#define schedule_action ( func,
data   )     __schedule_action(func, data, __PRETTY_FUNCTION__)

#define TRUNK_CALL_START   ARRAY_LEN(iaxs) / 2

Definition at line 827 of file chan_iax2.c.

Referenced by __find_callno(), make_trunk(), update_max_nontrunk(), and update_max_trunk().

#define TS_GAP_FOR_JB_RESYNC   5000

Definition at line 445 of file chan_iax2.c.


Enumeration Type Documentation

anonymous enum

Enumerator:
IAX_STATE_STARTED 
IAX_STATE_AUTHENTICATED 
IAX_STATE_TBD 
IAX_STATE_UNCHANGED 

Definition at line 230 of file chan_iax2.c.

00230      {
00231    IAX_STATE_STARTED =     (1 << 0),
00232    IAX_STATE_AUTHENTICATED =  (1 << 1),
00233    IAX_STATE_TBD =      (1 << 2),
00234    IAX_STATE_UNCHANGED =      (1 << 3),
00235 } iax2_state;

anonymous enum

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?

Definition at line 242 of file chan_iax2.c.

00242      {
00243    IAX_HASCALLERID =    (1 << 0),   /*!< CallerID has been specified */
00244    IAX_DELME =    (1 << 1),   /*!< Needs to be deleted */
00245    IAX_TEMPONLY =    (1 << 2),   /*!< Temporary (realtime) */
00246    IAX_TRUNK =    (1 << 3),   /*!< Treat as a trunk */
00247    IAX_NOTRANSFER =  (1 << 4),   /*!< Don't native bridge */
00248    IAX_USEJITTERBUF =   (1 << 5),   /*!< Use jitter buffer */
00249    IAX_DYNAMIC =     (1 << 6),   /*!< dynamic peer */
00250    IAX_SENDANI =     (1 << 7),   /*!< Send ANI along with CallerID */
00251         /* (1 << 8) is currently unused due to the deprecation of an old option. Go ahead, take it! */
00252    IAX_ALREADYGONE = (1 << 9),   /*!< Already disconnected */
00253    IAX_PROVISION =      (1 << 10),  /*!< This is a provisioning request */
00254    IAX_QUELCH =      (1 << 11),  /*!< Whether or not we quelch audio */
00255    IAX_ENCRYPTED =      (1 << 12),  /*!< Whether we should assume encrypted tx/rx */
00256    IAX_KEYPOPULATED =   (1 << 13),  /*!< Whether we have a key populated */
00257    IAX_CODEC_USER_FIRST =  (1 << 14),  /*!< are we willing to let the other guy choose the codec? */
00258    IAX_CODEC_NOPREFS =     (1 << 15),  /*!< Force old behaviour by turning off prefs */
00259    IAX_CODEC_NOCAP =    (1 << 16),  /*!< only consider requested format and ignore capabilities*/
00260    IAX_RTCACHEFRIENDS =    (1 << 17),  /*!< let realtime stay till your reload */
00261    IAX_RTUPDATE =       (1 << 18),  /*!< Send a realtime update */
00262    IAX_RTAUTOCLEAR =    (1 << 19),  /*!< erase me on expire */ 
00263    IAX_FORCEJITTERBUF = (1 << 20),  /*!< Force jitterbuffer, even when bridged to a channel that can take jitter */ 
00264    IAX_RTIGNOREREGEXPIRE = (1 << 21),  /*!< When using realtime, ignore registration expiration */
00265    IAX_TRUNKTIMESTAMPS =   (1 << 22),  /*!< Send trunk timestamps */
00266    IAX_TRANSFERMEDIA =  (1 << 23),      /*!< When doing IAX2 transfers, transfer media only */
00267    IAX_MAXAUTHREQ =        (1 << 24),      /*!< Maximum outstanding AUTHREQ restriction is in place */
00268    IAX_DELAYPBXSTART =  (1 << 25),  /*!< Don't start a PBX on the channel until the peer sends us a
00269                        response, so that we've achieved a three-way handshake with
00270                        them before sending voice or anything else*/
00271    IAX_ALLOWFWDOWNLOAD = (1 << 26), /*!< Allow the FWDOWNL command? */
00272 } iax2_flags;

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


Function Documentation

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

Definition at line 2220 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, iax2_registry::callno, ast_iax2_queue::count, 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, iaxq, LOG_WARNING, MAX_RETRY_TIME, ast_channel::name, iax_frame::oseqno, chan_iax2_pvt::owner, ast_iax2_queue::queue, 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().

02221 {
02222    /* Attempt to transmit the frame to the remote peer...
02223       Called without iaxsl held. */
02224    struct iax_frame *f = (struct iax_frame *)data;
02225    int freeme=0;
02226    int callno = f->callno;
02227    /* Make sure this call is still active */
02228    if (callno) 
02229       ast_mutex_lock(&iaxsl[callno]);
02230    if (callno && iaxs[callno]) {
02231       if ((f->retries < 0) /* Already ACK'd */ ||
02232           (f->retries >= max_retries) /* Too many attempts */) {
02233             /* Record an error if we've transmitted too many times */
02234             if (f->retries >= max_retries) {
02235                if (f->transfer) {
02236                   /* Transfer timeout */
02237                   send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
02238                } else if (f->final) {
02239                   if (f->final) 
02240                      iax2_destroy(callno);
02241                } else {
02242                   if (iaxs[callno]->owner)
02243                      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);
02244                   iaxs[callno]->error = ETIMEDOUT;
02245                   if (iaxs[callno]->owner) {
02246                      struct ast_frame fr = { 0, };
02247                      /* Hangup the fd */
02248                      fr.frametype = AST_FRAME_CONTROL;
02249                      fr.subclass = AST_CONTROL_HANGUP;
02250                      iax2_queue_frame(callno, &fr); // XXX
02251                      /* Remember, owner could disappear */
02252                      if (iaxs[callno] && iaxs[callno]->owner)
02253                         iaxs[callno]->owner->hangupcause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
02254                   } else {
02255                      if (iaxs[callno]->reg) {
02256                         memset(&iaxs[callno]->reg->us, 0, sizeof(iaxs[callno]->reg->us));
02257                         iaxs[callno]->reg->regstate = REG_STATE_TIMEOUT;
02258                         iaxs[callno]->reg->refresh = IAX_DEFAULT_REG_EXPIRE;
02259                      }
02260                      iax2_destroy(callno);
02261                   }
02262                }
02263 
02264             }
02265             freeme++;
02266       } else {
02267          /* Update it if it needs it */
02268          update_packet(f);
02269          /* Attempt transmission */
02270          send_packet(f);
02271          f->retries++;
02272          /* Try again later after 10 times as long */
02273          f->retrytime *= 10;
02274          if (f->retrytime > MAX_RETRY_TIME)
02275             f->retrytime = MAX_RETRY_TIME;
02276          /* Transfer messages max out at one second */
02277          if (f->transfer && (f->retrytime > 1000))
02278             f->retrytime = 1000;
02279          f->retrans = iax2_sched_add(sched, f->retrytime, attempt_transmit, f);
02280       }
02281    } else {
02282       /* Make sure it gets freed */
02283       f->retries = -1;
02284       freeme++;
02285    }
02286    if (callno)
02287       ast_mutex_unlock(&iaxsl[callno]);
02288    /* Do not try again */
02289    if (freeme) {
02290       /* Don't attempt delivery, just remove it from the queue */
02291       AST_LIST_LOCK(&iaxq.queue);
02292       AST_LIST_REMOVE(&iaxq.queue, f, list);
02293       iaxq.count--;
02294       AST_LIST_UNLOCK(&iaxq.queue);
02295       f->retrans = -1;
02296       /* Free the IAX frame */
02297       iax2_frame_free(f);
02298    }
02299 }

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

Definition at line 6477 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, iax2_registry::callno, 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().

06478 {
06479    /* Called from IAX thread only, without iaxs lock */
06480    int callno = (int)(long)(nothing);
06481    struct iax_ie_data ied;
06482    ast_mutex_lock(&iaxsl[callno]);
06483    if (iaxs[callno]) {
06484       memset(&ied, 0, sizeof(ied));
06485       if (iaxs[callno]->authfail == IAX_COMMAND_REGREJ) {
06486          iax_ie_append_str(&ied, IAX_IE_CAUSE, "Registration Refused");
06487          iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_REJECTED);
06488       } else if (iaxs[callno]->authfail == IAX_COMMAND_REJECT) {
06489          iax_ie_append_str(&ied, IAX_IE_CAUSE, "No authority found");
06490          iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_NOT_SUBSCRIBED);
06491       }
06492       send_command_final(iaxs[callno], AST_FRAME_IAX, iaxs[callno]->authfail, 0, ied.buf, ied.pos, -1);
06493    }
06494    ast_mutex_unlock(&iaxsl[callno]);
06495 }

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

Definition at line 3187 of file chan_iax2.c.

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

Referenced by auto_congest().

03188 {
03189    int callno = PTR_TO_CALLNO(nothing);
03190    struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_CONGESTION };
03191    ast_mutex_lock(&iaxsl[callno]);
03192    if (iaxs[callno]) {
03193       iaxs[callno]->initid = -1;
03194       iax2_queue_frame(callno, &f);
03195       ast_log(LOG_NOTICE, "Auto-congesting call due to slow response\n");
03196    }
03197    ast_mutex_unlock(&iaxsl[callno]);
03198 }

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

Definition at line 6526 of file chan_iax2.c.

References AST_CAUSE_NO_USER_RESPONSE, AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), iax_ie_data::buf, iax2_registry::callno, 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().

06527 {
06528    /* Called from IAX thread only, without iaxs lock */
06529    int callno = (int)(long)(nothing);
06530    struct iax_ie_data ied;
06531    ast_mutex_lock(&iaxsl[callno]);
06532    if (iaxs[callno]) {
06533       memset(&ied, 0, sizeof(ied));
06534       iax_ie_append_str(&ied, IAX_IE_CAUSE, "Timeout");
06535       iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_NO_USER_RESPONSE);
06536       send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, ied.buf, ied.pos, -1);
06537    }
06538    ast_mutex_unlock(&iaxsl[callno]);
06539 }

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 2079 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().

02080 {
02081    /* Just deliver the packet by using queueing.  This is called by
02082      the IAX thread with the iaxsl lock held. */
02083    struct iax_frame *fr = data;
02084    fr->retrans = -1;
02085    ast_clear_flag(&fr->af, AST_FRFLAG_HAS_TIMING_INFO);
02086    if (iaxs[fr->callno] && !ast_test_flag(iaxs[fr->callno], IAX_ALREADYGONE))
02087       iax2_queue_frame(fr->callno, &fr->af);
02088    /* Free our iax frame */
02089    iax2_frame_free(fr);
02090    /* And don't run again */
02091    return 0;
02092 }

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

Definition at line 6165 of file chan_iax2.c.

References iax2_peer::addr, ast_db_del(), ast_device_state_changed(), ast_log(), ast_test_flag, EVENT_FLAG_SYSTEM, iax2_peer::expire, iax2_peer::expiry, iax2_regfunk, IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, IAX_RTUPDATE, IAX_TEMPONLY, LOG_DEBUG, manager_event(), iax2_peer::name, option_debug, peer_unref(), realtime_update_peer(), register_peer_exten(), and unlink_peer().

Referenced by expire_registry().

06166 {
06167    struct iax2_peer *peer = (struct iax2_peer *) data;
06168 
06169    if (!peer)
06170       return;
06171 
06172    peer->expire = -1;
06173 
06174    if (option_debug)
06175       ast_log(LOG_DEBUG, "Expiring registration for peer '%s'\n", peer->name);
06176    if (ast_test_flag((&globalflags), IAX_RTUPDATE) && (ast_test_flag(peer, IAX_TEMPONLY|IAX_RTCACHEFRIENDS)))
06177       realtime_update_peer(peer->name, &peer->addr, 0);
06178    manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: IAX2/%s\r\nPeerStatus: Unregistered\r\nCause: Expired\r\n", peer->name);
06179    /* Reset the address */
06180    memset(&peer->addr, 0, sizeof(peer->addr));
06181    /* Reset expiry value */
06182    peer->expiry = min_reg_expire;
06183    if (!ast_test_flag(peer, IAX_TEMPONLY))
06184       ast_db_del("IAX/Registry", peer->name);
06185    register_peer_exten(peer, 0);
06186    ast_device_state_changed("IAX2/%s", peer->name); /* Activate notification */
06187    if (iax2_regfunk)
06188       iax2_regfunk(peer->name, 0);
06189 
06190    if (ast_test_flag(peer, IAX_RTAUTOCLEAR))
06191       unlink_peer(peer);
06192 
06193    peer_unref(peer);
06194 }

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]

Note:
Calling this function while holding another pvt lock can cause a deadlock.

Definition at line 1543 of file chan_iax2.c.

References chan_iax2_pvt::addr, chan_iax2_pvt::amaflags, ao2_find(), ao2_ref(), ast_copy_flags, ast_inet_ntoa(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_random(), ast_string_field_set, ast_tvnow(), chan_iax2_pvt::callno, DEFAULT_RETRY_TIME, chan_iax2_pvt::expiry, iax2_getpeername(), iax2_sched_add(), IAX_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_TRANSFERMEDIA, IAX_USEJITTERBUF, chan_iax2_pvt::lagid, lastused, LOG_DEBUG, LOG_WARNING, match(), MIN_REUSE_TIME, NEW_ALLOW, new_iax(), option_debug, chan_iax2_pvt::peercallno, chan_iax2_pvt::pingid, chan_iax2_pvt::pingtime, send_lagrq(), send_ping(), chan_iax2_pvt::sockfd, store_by_peercallno(), TRUNK_CALL_START, and update_max_nontrunk().

Referenced by find_callno(), and find_callno_locked().

01544 {
01545    int res = 0;
01546    int x;
01547    struct timeval now;
01548    char host[80];
01549 
01550    if (new <= NEW_ALLOW) {
01551       if (callno) {
01552          struct chan_iax2_pvt *pvt;
01553          struct chan_iax2_pvt tmp_pvt = {
01554             .callno = dcallno,
01555             .peercallno = callno,
01556             /* hack!! */
01557             .frames_received = check_dcallno,
01558          };
01559  
01560          memcpy(&tmp_pvt.addr, sin, sizeof(tmp_pvt.addr));
01561  
01562          if ((pvt = ao2_find(iax_peercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
01563             if (return_locked) {
01564                ast_mutex_lock(&iaxsl[pvt->callno]);
01565             }
01566             res = pvt->callno;
01567             ao2_ref(pvt, -1);
01568             pvt = NULL;
01569             return res;
01570          }
01571       }
01572 
01573       /* This will occur on the first response to a message that we initiated,
01574        * such as a PING. */
01575       if (dcallno) {
01576          ast_mutex_lock(&iaxsl[dcallno]);
01577       }
01578       if (callno && dcallno && iaxs[dcallno] && !iaxs[dcallno]->peercallno && match(sin, callno, dcallno, iaxs[dcallno], check_dcallno)) {
01579          iaxs[dcallno]->peercallno = callno;
01580          res = dcallno;
01581          store_by_peercallno(iaxs[dcallno]);
01582          if (!res || !return_locked) {
01583             ast_mutex_unlock(&iaxsl[dcallno]);
01584          }
01585          return res;
01586       }
01587       if (dcallno) {
01588          ast_mutex_unlock(&iaxsl[dcallno]);
01589       }
01590 
01591 #ifdef IAX_OLD_FIND
01592       /* If we get here, we SHOULD NOT find a call structure for this
01593          callno; if we do, it means that there is a call structure that
01594          has a peer callno but did NOT get entered into the hash table,
01595          which is bad.
01596 
01597          If we find a call structure using this old, slow method, output a log
01598          message so we'll know about it. After a few months of leaving this in
01599          place, if we don't hear about people seeing these messages, we can
01600          remove this code for good.
01601       */
01602 
01603       for (x = 1; !res && x < maxnontrunkcall; x++) {
01604          ast_mutex_lock(&iaxsl[x]);
01605          if (iaxs[x]) {
01606             /* Look for an exact match */
01607             if (match(sin, callno, dcallno, iaxs[x], check_dcallno)) {
01608                res = x;
01609             }
01610          }
01611          if (!res || !return_locked)
01612             ast_mutex_unlock(&iaxsl[x]);
01613       }
01614 
01615       for (x = TRUNK_CALL_START; !res && x < maxtrunkcall; x++) {
01616          ast_mutex_lock(&iaxsl[x]);
01617          if (iaxs[x]) {
01618             /* Look for an exact match */
01619             if (match(sin, callno, dcallno, iaxs[x], check_dcallno)) {
01620                res = x;
01621             }
01622          }
01623          if (!res || !return_locked)
01624             ast_mutex_unlock(&iaxsl[x]);
01625       }
01626 
01627       if (res) {
01628          ast_log(LOG_WARNING, "Old call search code found call number %d that was not in hash table!\n", res);
01629       }
01630 #endif
01631    }
01632    if (!res && (new >= NEW_ALLOW)) {
01633       int start, found = 0;
01634 
01635       /* It may seem odd that we look through the peer list for a name for
01636        * this *incoming* call.  Well, it is weird.  However, users don't
01637        * have an IP address/port number that we can match against.  So,
01638        * this is just checking for a peer that has that IP/port and
01639        * assuming that we have a user of the same name.  This isn't always
01640        * correct, but it will be changed if needed after authentication. */
01641       if (!iax2_getpeername(*sin, host, sizeof(host)))
01642          snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
01643 
01644       now = ast_tvnow();
01645       start = 2 + (ast_random() % (TRUNK_CALL_START - 1));
01646       for (x = start; 1; x++) {
01647          if (x == TRUNK_CALL_START) {
01648             x = 1;
01649             continue;
01650          }
01651 
01652          /* Find first unused call number that hasn't been used in a while */
01653          ast_mutex_lock(&iaxsl[x]);
01654          if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) {
01655             found = 1;
01656             break;
01657          }
01658          ast_mutex_unlock(&iaxsl[x]);
01659          
01660          if (x == start - 1) {
01661             break;
01662          }
01663       }
01664       /* We've still got lock held if we found a spot */
01665       if (x == start - 1 && !found) {
01666          ast_log(LOG_WARNING, "No more space\n");
01667          return 0;
01668       }
01669       iaxs[x] = new_iax(sin, host);
01670       update_max_nontrunk();
01671       if (iaxs[x]) {
01672          if (option_debug && iaxdebug)
01673             ast_log(LOG_DEBUG, "Creating new call structure %d\n", x);
01674          iaxs[x]->sockfd = sockfd;
01675          iaxs[x]->addr.sin_port = sin->sin_port;
01676          iaxs[x]->addr.sin_family = sin->sin_family;
01677          iaxs[x]->addr.sin_addr.s_addr = sin->sin_addr.s_addr;
01678          iaxs[x]->peercallno = callno;
01679          iaxs[x]->callno = x;
01680          iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
01681          iaxs[x]->expiry = min_reg_expire;
01682          iaxs[x]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
01683          iaxs[x]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
01684          iaxs[x]->amaflags = amaflags;
01685          ast_copy_flags(iaxs[x], (&globalflags), IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);
01686          
01687          ast_string_field_set(iaxs[x], accountcode, accountcode);
01688          ast_string_field_set(iaxs[x], mohinterpret, mohinterpret);
01689          ast_string_field_set(iaxs[x], mohsuggest, mohsuggest);
01690 
01691          if (iaxs[x]->peercallno) {
01692             store_by_peercallno(iaxs[x]);
01693          }
01694       } else {
01695          ast_log(LOG_WARNING, "Out of resources\n");
01696          ast_mutex_unlock(&iaxsl[x]);
01697          return 0;
01698       }
01699       if (!return_locked)
01700          ast_mutex_unlock(&iaxsl[x]);
01701       res = x;
01702    }
01703    return res;
01704 }

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

Definition at line 2623 of file chan_iax2.c.

References __do_deliver(), ast_backtrace(), ast_codec_interp_len(), AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_samp2tv(), ast_test_flag, ast_tvadd(), ast_tvdiff_ms(), iax2_registry::callno, 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, LOG_ERROR, jb_frame::ms, iax2_registry::next, 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().

02624 {
02625    int callno = PTR_TO_CALLNO(p);
02626    struct chan_iax2_pvt *pvt = NULL;
02627    struct iax_frame *fr;
02628    jb_frame frame;
02629    int ret;
02630    long now;
02631    long next;
02632    struct timeval tv;
02633    
02634    /* Make sure we have a valid private structure before going on */
02635    ast_mutex_lock(&iaxsl[callno]);
02636    pvt = iaxs[callno];
02637    if (!pvt) {
02638       /* No go! */
02639       ast_mutex_unlock(&iaxsl[callno]);
02640       return;
02641    }
02642 
02643     if( pvt->jb == NULL ) {
02644    ast_log( LOG_ERROR, "__get_from_jb(): why p->jb is null?\n" );
02645    ast_backtrace();
02646    return;
02647     }
02648 
02649    pvt->jbid = -1;
02650    
02651    gettimeofday(&tv,NULL);
02652    /* round up a millisecond since ast_sched_runq does; */
02653    /* prevents us from spinning while waiting for our now */
02654    /* to catch up with runq's now */
02655    tv.tv_usec += 1000;
02656    
02657    now = ast_tvdiff_ms(tv, pvt->rxcore);
02658    
02659    if(now >= (next = jb_next(pvt->jb))) {
02660       ret = jb_get(pvt->jb,&frame,now,ast_codec_interp_len(pvt->voiceformat));
02661       switch(ret) {
02662       case JB_OK:
02663          fr = frame.data;
02664          __do_deliver(fr);
02665          /* __do_deliver() can cause the call to disappear */
02666          pvt = iaxs[callno];
02667          break;
02668       case JB_INTERP:
02669       {
02670          struct ast_frame af = { 0, };
02671          
02672          /* create an interpolation frame */
02673          af.frametype = AST_FRAME_VOICE;
02674          af.subclass = pvt->voiceformat;
02675          af.samples  = frame.ms * 8;
02676          af.src  = "IAX2 JB interpolation";
02677          af.delivery = ast_tvadd(pvt->rxcore, ast_samp2tv(next, 1000));
02678          af.offset = AST_FRIENDLY_OFFSET;
02679          
02680          /* queue the frame:  For consistency, we would call __do_deliver here, but __do_deliver wants an iax_frame,
02681           * which we'd need to malloc, and then it would free it.  That seems like a drag */
02682          if (!ast_test_flag(iaxs[callno], IAX_ALREADYGONE)) {
02683             iax2_queue_frame(callno, &af);
02684             /* iax2_queue_frame() could cause the call to disappear */
02685             pvt = iaxs[callno];
02686          }
02687       }
02688          break;
02689       case JB_DROP:
02690          iax2_frame_free(frame.data);
02691          break;
02692       case JB_NOFRAME:
02693       case JB_EMPTY:
02694          /* do nothing */
02695          break;
02696       default:
02697          /* shouldn't happen */
02698          break;
02699       }
02700    }
02701    if (pvt)
02702       update_jbsched(pvt);
02703    ast_mutex_unlock(&iaxsl[callno]);
02704 }

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

Definition at line 5854 of file chan_iax2.c.

References iax2_registry::expire, and iax2_do_register().

Referenced by iax2_do_register_s().

05855 {
05856    struct iax2_registry *reg = (struct iax2_registry *)data;
05857    reg->expire = -1;
05858    iax2_do_register(reg);
05859 }

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

Definition at line 8970 of file chan_iax2.c.

References ast_device_state_changed(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax2_peer::callno, iax2_registry::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().

08971 {
08972    struct iax2_peer *peer = (struct iax2_peer *)data;
08973    int callno;
08974 
08975    if (peer->lastms > -1) {
08976       ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Time: %d\n", peer->name, peer->lastms);
08977       manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: IAX2/%s\r\nPeerStatus: Unreachable\r\nTime: %d\r\n", peer->name, peer->lastms);
08978       ast_device_state_changed("IAX2/%s", peer->name); /* Activate notification */
08979    }
08980    if ((callno = peer->callno) > 0) {
08981       ast_mutex_lock(&iaxsl[callno]);
08982       iax2_destroy(callno);
08983       ast_mutex_unlock(&iaxsl[callno]);
08984    }
08985    peer->callno = 0;
08986    peer->lastms = -1;
08987    /* Try again quickly */
08988    peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_peer_s, peer_ref(peer));
08989    if (peer->pokeexpire == -1)
08990       peer_unref(peer);
08991 }

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

Definition at line 6589 of file chan_iax2.c.

References iax2_poke_peer(), and peer_unref().

Referenced by iax2_poke_peer_s().

06590 {
06591    struct iax2_peer *peer = (struct iax2_peer *)data;
06592    iax2_poke_peer(peer, 0);
06593    peer_unref(peer);
06594 }

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

Definition at line 4647 of file chan_iax2.c.

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

Referenced by iax2_show_peers(), and manager_iax2_show_peers().

04648 {
04649    regex_t regexbuf;
04650    int havepattern = 0;
04651    int total_peers = 0;
04652    int online_peers = 0;
04653    int offline_peers = 0;
04654    int unmonitored_peers = 0;
04655    struct ao2_iterator i;
04656 
04657 #define FORMAT2 "%-15.15s  %-15.15s %s  %-15.15s  %-8s  %s %-10s%s"
04658 #define FORMAT "%-15.15s  %-15.15s %s  %-15.15s  %-5d%s  %s %-10s%s"
04659 
04660    struct iax2_peer *peer = NULL;
04661    char name[256];
04662    int registeredonly=0;
04663    char *term = manager ? "\r\n" : "\n";
04664 
04665    switch (argc) {
04666    case 6:
04667       if (!strcasecmp(argv[3], "registered"))
04668          registeredonly = 1;
04669       else
04670          return RESULT_SHOWUSAGE;
04671       if (!strcasecmp(argv[4], "like")) {
04672          if (regcomp(&regexbuf, argv[5], REG_EXTENDED | REG_NOSUB))
04673             return RESULT_SHOWUSAGE;
04674          havepattern = 1;
04675       } else
04676          return RESULT_SHOWUSAGE;
04677       break;
04678    case 5:
04679       if (!strcasecmp(argv[3], "like")) {
04680          if (regcomp(&regexbuf, argv[4], REG_EXTENDED | REG_NOSUB))
04681             return RESULT_SHOWUSAGE;
04682          havepattern = 1;
04683       } else
04684          return RESULT_SHOWUSAGE;
04685       break;
04686    case 4:
04687       if (!strcasecmp(argv[3], "registered"))
04688          registeredonly = 1;
04689       else
04690          return RESULT_SHOWUSAGE;
04691       break;
04692    case 3:
04693       break;
04694    default:
04695       return RESULT_SHOWUSAGE;
04696    }
04697 
04698 
04699    if (s)
04700       astman_append(s, FORMAT2, "Name/Username", "Host", "   ", "Mask", "Port", "   ", "Status", term);
04701    else
04702       ast_cli(fd, FORMAT2, "Name/Username", "Host", "   ", "Mask", "Port", "   ", "Status", term);
04703 
04704    i = ao2_iterator_init(peers, 0);
04705    for (peer = ao2_iterator_next(&i); peer; 
04706       peer_unref(peer), peer = ao2_iterator_next(&i)) {
04707       char nm[20];
04708       char status[20];
04709       char srch[2000];
04710       int retstatus;
04711 
04712       if (registeredonly && !peer->addr.sin_addr.s_addr)
04713          continue;
04714       if (havepattern && regexec(&regexbuf, peer->name, 0, NULL, 0))
04715          continue;
04716 
04717       if (!ast_strlen_zero(peer->username))
04718          snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
04719       else
04720          ast_copy_string(name, peer->name, sizeof(name));
04721       
04722       retstatus = peer_status(peer, status, sizeof(status));
04723       if (retstatus > 0)
04724          online_peers++;
04725       else if (!retstatus)
04726          offline_peers++;
04727       else
04728          unmonitored_peers++;
04729       
04730       ast_copy_string(nm, ast_inet_ntoa(peer->mask), sizeof(nm));
04731       
04732       snprintf(srch, sizeof(srch), FORMAT, name, 
04733           peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
04734           ast_test_flag(peer, IAX_DYNAMIC) ? "(D)" : "(S)",
04735           nm,
04736           ntohs(peer->addr.sin_port), ast_test_flag(peer, IAX_TRUNK) ? "(T)" : "   ",
04737           peer->encmethods ? "(E)" : "   ", status, term);
04738       
04739       if (s)
04740          astman_append(s, FORMAT, name, 
04741                   peer->addr.sin_addr.s_addr ? ast_inet_ntoa( peer->addr.sin_addr) : "(Unspecified)",
04742                   ast_test_flag(peer, IAX_DYNAMIC) ? "(D)" : "(S)",
04743                   nm,
04744                   ntohs(peer->addr.sin_port), ast_test_flag(peer, IAX_TRUNK) ? "(T)" : "   ",
04745                   peer->encmethods ? "(E)" : "   ", status, term);
04746       else
04747          ast_cli(fd, FORMAT, name, 
04748             peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "(Unspecified)",
04749             ast_test_flag(peer, IAX_DYNAMIC) ? "(D)" : "(S)",
04750             nm,
04751             ntohs(peer->addr.sin_port), ast_test_flag(peer, IAX_TRUNK) ? "(T)" : "   ",
04752             peer->encmethods ? "(E)" : "   ", status, term);
04753       total_peers++;
04754    }
04755 
04756    if (s)
04757       astman_append(s,"%d iax2 peers [%d online, %d offline, %d unmonitored]%s", total_peers, online_peers, offline_peers, unmonitored_peers, term);
04758    else
04759       ast_cli(fd,"%d iax2 peers [%d online, %d offline, %d unmonitored]%s", total_peers, online_peers, offline_peers, unmonitored_peers, term);
04760 
04761    if (havepattern)
04762       regfree(&regexbuf);
04763 
04764    return RESULT_SUCCESS;
04765 #undef FORMAT
04766 #undef FORMAT2
04767 }

static void __reg_module ( void   )  [static]

Definition at line 11346 of file chan_iax2.c.

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

Definition at line 956 of file chan_iax2.c.

References ast_copy_string(), ast_log(), iax2_thread::cond, iax2_thread::curfunc, find_idle_thread(), IAX_IOSTATE_SCHEDREADY, iax2_thread::iostate, iax2_thread::lock, LOG_DEBUG, option_debug, iax2_thread::scheddata, iax2_thread::schedfunc, signal_condition(), t, and thread.

00957 {
00958    struct iax2_thread *thread = NULL;
00959    static time_t lasterror;
00960    static time_t t;
00961 
00962    thread = find_idle_thread();
00963 
00964    if (thread != NULL) {
00965       thread->schedfunc = func;
00966       thread->scheddata = data;
00967       thread->iostate = IAX_IOSTATE_SCHEDREADY;
00968 #ifdef DEBUG_SCHED_MULTITHREAD
00969       ast_copy_string(thread->curfunc, funcname, sizeof(thread->curfunc));
00970 #endif
00971       signal_condition(&thread->lock, &thread->cond);
00972       return 0;
00973    }
00974    time(&t);
00975    if (t != lasterror && option_debug) 
00976       ast_log(LOG_DEBUG, "Out of idle IAX2 threads for scheduling!\n");
00977    lasterror = t;
00978 
00979    return -1;
00980 }

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

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

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

05150 {
05151    struct ast_frame f = { 0, };
05152 
05153    f.frametype = type;
05154    f.subclass = command;
05155    f.datalen = datalen;
05156    f.src = __FUNCTION__;
05157    f.data = (void *) data;
05158 
05159    return iax2_send(i, &f, ts, seqno, now, transfer, final);
05160 }

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

Definition at line 1041 of file chan_iax2.c.

References AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax2_registry::callno, iax2_sched_add(), IAX_COMMAND_LAGRQ, chan_iax2_pvt::lagid, LOG_WARNING, send_command(), and send_lagrq().

Referenced by send_lagrq().

01042 {
01043    int callno = (long) data;
01044 
01045    ast_mutex_lock(&iaxsl[callno]);
01046 
01047    if (iaxs[callno]) {
01048       if (iaxs[callno]->peercallno) {
01049          send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
01050          iaxs[callno]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, data);
01051       } else {
01052          /* I am the schedule, so I'm allowed to do this */
01053          iaxs[callno]->lagid = -1;
01054       }
01055    } else {
01056       ast_log(LOG_WARNING, "I was supposed to send a LAGRQ with callno %d, but no such call exists (and I cannot remove lagid, either).\n", callno);
01057    }
01058 
01059    ast_mutex_unlock(&iaxsl[callno]);
01060 }

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

Definition at line 996 of file chan_iax2.c.

References AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), iax2_registry::callno, iax2_sched_add(), IAX_COMMAND_PING, LOG_DEBUG, option_debug, chan_iax2_pvt::pingid, send_command(), and send_ping().

Referenced by send_ping().

00997 {
00998    int callno = (long) data;
00999 
01000    ast_mutex_lock(&iaxsl[callno]);
01001 
01002    if (iaxs[callno]) {
01003       if (iaxs[callno]->peercallno) {
01004          send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PING, 0, NULL, 0, -1);
01005          iaxs[callno]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, data);
01006       } else {
01007          /* I am the schedule, so I'm allowed to do this */
01008          iaxs[callno]->pingid = -1;
01009       }
01010    } else if (option_debug > 0) {
01011       ast_log(LOG_DEBUG, "I was supposed to send a PING with callno %d, but no such call exists (and I cannot remove pingid, either).\n", callno);
01012    }
01013 
01014    ast_mutex_unlock(&iaxsl[callno]);
01015 }

static int __unload_module ( void   )  [static]

Definition at line 11097 of file chan_iax2.c.

References ao2_ref(), ARRAY_LEN, ast_channel_unregister(), ast_cli_unregister_multiple(), ast_cond_signal(), AST_LIST_HEAD_DESTROY, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_manager_unregister(), ast_mutex_destroy(), ast_mutex_lock(), ast_mutex_unlock(), ast_netsock_release(), AST_PTHREADT_NULL, ast_unregister_application(), ast_unregister_switch(), delete_users(), iax2_destroy(), iax_provision_unload(), iaxactivethreadcount, iaxq, ast_firmware_list::lock, ast_iax2_queue::queue, reload_firmware(), sched_context_destroy(), sched_lock, thread, iax2_thread::threadid, and waresl.

Referenced by load_module(), and unload_module().

11098 {
11099    struct iax2_thread *thread = NULL;
11100    int x;
11101 
11102    /* Make sure threads do not hold shared resources when they are canceled */
11103    
11104    /* Grab the sched lock resource to keep it away from threads about to die */
11105    /* Cancel the network thread, close the net socket */
11106    if (netthreadid != AST_PTHREADT_NULL) {
11107       AST_LIST_LOCK(&iaxq.queue);
11108       ast_mutex_lock(&sched_lock);
11109       pthread_cancel(netthreadid);
11110       ast_cond_signal(&sched_cond);
11111       ast_mutex_unlock(&sched_lock);   /* Release the schedule lock resource */
11112       AST_LIST_UNLOCK(&iaxq.queue);
11113       pthread_join(netthreadid, NULL);
11114    }
11115    if (schedthreadid != AST_PTHREADT_NULL) {
11116       ast_mutex_lock(&sched_lock);  
11117       pthread_cancel(schedthreadid);
11118       ast_cond_signal(&sched_cond);
11119       ast_mutex_unlock(&sched_lock);   
11120       pthread_join(schedthreadid, NULL);
11121    }
11122    
11123    /* Call for all threads to halt */
11124    AST_LIST_LOCK(&idle_list);
11125    AST_LIST_TRAVERSE_SAFE_BEGIN(&idle_list, thread, list) {
11126       AST_LIST_REMOVE_CURRENT(&idle_list, list);
11127       pthread_cancel(thread->threadid);
11128    }
11129    AST_LIST_TRAVERSE_SAFE_END
11130    AST_LIST_UNLOCK(&idle_list);
11131 
11132    AST_LIST_LOCK(&active_list);
11133    AST_LIST_TRAVERSE_SAFE_BEGIN(&active_list, thread, list) {
11134       AST_LIST_REMOVE_CURRENT(&active_list, list);
11135       pthread_cancel(thread->threadid);
11136    }
11137    AST_LIST_TRAVERSE_SAFE_END
11138    AST_LIST_UNLOCK(&active_list);
11139 
11140    AST_LIST_LOCK(&dynamic_list);
11141         AST_LIST_TRAVERSE_SAFE_BEGIN(&dynamic_list, thread, list) {
11142       AST_LIST_REMOVE_CURRENT(&dynamic_list, list);
11143       pthread_cancel(thread->threadid);
11144         }
11145    AST_LIST_TRAVERSE_SAFE_END
11146         AST_LIST_UNLOCK(&dynamic_list);
11147 
11148    AST_LIST_HEAD_DESTROY(&iaxq.queue);
11149 
11150    /* Wait for threads to exit */
11151    while(0 < iaxactivethreadcount)
11152       usleep(10000);
11153    
11154    ast_netsock_release(netsock);
11155    ast_netsock_release(outsock);
11156    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
11157       if (iaxs[x]) {
11158          iax2_destroy(x);
11159       }
11160    }
11161    ast_manager_unregister( "IAXpeers" );
11162    ast_manager_unregister( "IAXnetstats" );
11163    ast_unregister_application(papp);
11164    ast_cli_unregister_multiple(cli_iax2, sizeof(cli_iax2) / sizeof(struct ast_cli_entry));
11165    ast_unregister_switch(&iax2_switch);
11166    ast_channel_unregister(&iax2_tech);
11167    delete_users();
11168    iax_provision_unload();
11169    sched_context_destroy(sched);
11170    reload_firmware(1);
11171 
11172    ast_mutex_destroy(&waresl.lock);
11173 
11174    for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
11175       ast_mutex_destroy(&iaxsl[x]);
11176    }
11177 
11178    ao2_ref(peers, -1);
11179    ao2_ref(users, -1);
11180    ao2_ref(iax_peercallno_pvts, -1);
11181 
11182    return 0;
11183 }

static void __unreg_module ( void   )  [static]

Definition at line 11346 of file chan_iax2.c.

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

Definition at line 5201 of file chan_iax2.c.

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

Referenced by check_access().

05202 {
05203    while(con) {
05204       if (!strcmp(con->context, context) || !strcmp(con->context, "*"))
05205          return -1;
05206       con = con->next;
05207    }
05208    return 0;
05209 }

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

Definition at line 4975 of file chan_iax2.c.

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

Referenced by iax2_show_netstats(), and manager_iax2_show_netstats().

04976 {
04977    int x;
04978    int numchans = 0;
04979    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
04980       ast_mutex_lock(&iaxsl[x]);
04981       if (iaxs[x]) {
04982          int localjitter, localdelay, locallost, locallosspct, localdropped, localooo;
04983          char *fmt;
04984          jb_info jbinfo;
04985          
04986          if(ast_test_flag(iaxs[x], IAX_USEJITTERBUF)) {
04987             jb_getinfo(iaxs[x]->jb, &jbinfo);
04988             localjitter = jbinfo.jitter;
04989             localdelay = jbinfo.current - jbinfo.min;
04990             locallost = jbinfo.frames_lost;
04991             locallosspct = jbinfo.losspct/1000;
04992             localdropped = jbinfo.frames_dropped;
04993             localooo = jbinfo.frames_ooo;
04994          } else {
04995             localjitter = -1;
04996             localdelay = 0;
04997             locallost = -1;
04998             locallosspct = -1;
04999             localdropped = 0;
05000             localooo = -1;
05001          }
05002          if (limit_fmt)
05003             fmt = "%-25.25s %4d %4d %4d %5d %3d %5d %4d %6d %4d %4d %5d %3d %5d %4d %6d\n";
05004          else
05005             fmt = "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n";
05006          if (s)
05007             
05008             astman_append(s, fmt,
05009                      iaxs[x]->owner ? iaxs[x]->owner->name : "(None)",
05010                      iaxs[x]->pingtime,
05011                      localjitter, 
05012                      localdelay,
05013                      locallost,
05014                      locallosspct,
05015                      localdropped,
05016                      localooo,
05017                      iaxs[x]->frames_received/1000,
05018                      iaxs[x]->remote_rr.jitter,
05019                      iaxs[x]->remote_rr.delay,
05020                      iaxs[x]->remote_rr.losscnt,
05021                      iaxs[x]->remote_rr.losspct,
05022                      iaxs[x]->remote_rr.dropped,
05023                      iaxs[x]->remote_rr.ooo,
05024                      iaxs[x]->remote_rr.packets/1000);
05025          else
05026             ast_cli(fd, fmt,
05027                iaxs[x]->owner ? iaxs[x]->owner->name : "(None)",
05028                iaxs[x]->pingtime,
05029                localjitter, 
05030                localdelay,
05031                locallost,
05032                locallosspct,
05033                localdropped,
05034                localooo,
05035                iaxs[x]->frames_received/1000,
05036                iaxs[x]->remote_rr.jitter,
05037                iaxs[x]->remote_rr.delay,
05038                iaxs[x]->remote_rr.losscnt,
05039                iaxs[x]->remote_rr.losspct,
05040                iaxs[x]->remote_rr.dropped,
05041                iaxs[x]->remote_rr.ooo,
05042                iaxs[x]->remote_rr.packets/1000
05043                );
05044          numchans++;
05045       }
05046       ast_mutex_unlock(&iaxsl[x]);
05047    }
05048    return numchans;
05049 }

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 3851 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_channel_alloc(), ast_channel_free(), ast_copy_string(), ast_hangup(), 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(), 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, chan_iax2_pvt::dnid, ast_channel::exten, chan_iax2_pvt::exten, chan_iax2_pvt::host, chan_iax2_pvt::language, LOG_WARNING, ast_channel::name, ast_variable::name, ast_channel::nativeformats, ast_variable::next, chan_iax2_pvt::owner, 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, chan_iax2_pvt::vars, and ast_channel::writeformat.

Referenced by iax2_request(), and socket_process().

03852 {
03853    struct ast_channel *tmp;
03854    struct chan_iax2_pvt *i;
03855    struct ast_variable *v = NULL;
03856 
03857    if (!(i = iaxs[callno])) {
03858       ast_log(LOG_WARNING, "No IAX2 pvt found for callno '%d' !\n", callno);
03859       return NULL;
03860    }
03861 
03862    /* Don't hold call lock */
03863    ast_mutex_unlock(&iaxsl[callno]);
03864    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);
03865    ast_mutex_lock(&iaxsl[callno]);
03866    if (i != iaxs[callno]) {
03867       if (tmp) {
03868          /* unlock and relock iaxsl[callno] to preserve locking order */
03869          ast_mutex_unlock(&iaxsl[callno]);
03870          ast_channel_free(tmp);
03871          ast_mutex_lock(&iaxsl[callno]);
03872       }
03873       return NULL;
03874    }
03875 
03876    if (!tmp)
03877       return NULL;
03878    tmp->tech = &iax2_tech;
03879    /* We can support any format by default, until we get restricted */
03880    tmp->nativeformats = capability;
03881    tmp->readformat = tmp->rawreadformat = ast_best_codec(capability);
03882    tmp->writeformat = tmp->rawwriteformat = ast_best_codec(capability);
03883    tmp->tech_pvt = CALLNO_TO_PTR(i->callno);
03884 
03885    /* Don't use ast_set_callerid() here because it will
03886     * generate a NewCallerID event before the NewChannel event */
03887    if (!ast_strlen_zero(i->ani))
03888       tmp->cid.cid_ani = ast_strdup(i->ani);
03889    else
03890       tmp->cid.cid_ani = ast_strdup(i->cid_num);
03891    tmp->cid.cid_dnid = ast_strdup(i->dnid);
03892    tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
03893    tmp->cid.cid_pres = i->calling_pres;
03894    tmp->cid.cid_ton = i->calling_ton;
03895    tmp->cid.cid_tns = i->calling_tns;
03896    if (!ast_strlen_zero(i->language))
03897       ast_string_field_set(tmp, language, i->language);
03898    if (!ast_strlen_zero(i->accountcode))
03899       ast_string_field_set(tmp, accountcode, i->accountcode);
03900    if (i->amaflags)
03901       tmp->amaflags = i->amaflags;
03902    ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
03903    ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
03904    if (i->adsi)
03905       tmp->adsicpe = i->peeradsicpe;
03906    else
03907       tmp->adsicpe = AST_ADSI_UNAVAILABLE;
03908    i->owner = tmp;
03909    i->capability = capability;
03910 
03911    for (v = i->vars ; v ; v = v->next)
03912       pbx_builtin_setvar_helper(tmp, v->name, v->value);
03913 
03914    if (state != AST_STATE_DOWN) {
03915       if (ast_pbx_start(tmp)) {
03916          ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
03917          ast_hangup(tmp);
03918          i->owner = NULL;
03919          return NULL;
03920       }
03921    }
03922 
03923    ast_module_ref(ast_module_info->self);
03924    
03925    return tmp;
03926 }

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

Definition at line 2301 of file chan_iax2.c.

References __attempt_transmit(), and schedule_action.

Referenced by __attempt_transmit(), and network_thread().

02302 {
02303 #ifdef SCHED_MULTITHREADED
02304    if (schedule_action(__attempt_transmit, data))
02305 #endif      
02306       __attempt_transmit(data);
02307    return 0;
02308 }

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

Definition at line 6511 of file chan_iax2.c.

References AST_SCHED_DEL, auth_reject(), chan_iax2_pvt::authfail, chan_iax2_pvt::authid, and iax2_sched_add().

Referenced by socket_process().

06512 {
06513    /* Schedule sending the authentication failure in one second, to prevent
06514       guessing */
06515    if (iaxs[callno]) {
06516       iaxs[callno]->authfail = failcode;
06517       if (delayreject) {
06518          AST_SCHED_DEL(sched, iaxs[callno]->authid);
06519          iaxs[callno]->authid = iax2_sched_add(sched, 1000, auth_reject, (void *)(long)callno);
06520       } else
06521          auth_reject((void *)(long)callno);
06522    }
06523    return 0;
06524 }

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

Definition at line 6497 of file chan_iax2.c.

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

Referenced by auth_fail().

06498 {
06499    int callno = (int)(long)(data);
06500    ast_mutex_lock(&iaxsl[callno]);
06501    if (iaxs[callno])
06502       iaxs[callno]->authid = -1;
06503    ast_mutex_unlock(&iaxsl[callno]);
06504 #ifdef SCHED_MULTITHREADED
06505    if (schedule_action(__auth_reject, data))
06506 #endif      
06507       __auth_reject(data);
06508    return 0;
06509 }

static int authenticate ( const char *  challenge,
const char *  secret,
const char *  keyn,
int  authmethods,
struct iax_ie_data *  ied,
struct sockaddr_in *  sin,
aes_encrypt_ctx *  ecx,
aes_decrypt_ctx *  dcx 
) [static]

Definition at line 5723 of file chan_iax2.c.

References ast_inet_ntoa(), ast_key_get, AST_KEY_PRIVATE, ast_log(), ast_sign, ast_strlen_zero(), build_enc_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().

05724 {
05725    int res = -1;
05726    int x;
05727    if (!ast_strlen_zero(keyn)) {
05728       if (!(authmethods & IAX_AUTH_RSA)) {
05729          if (ast_strlen_zero(secret)) 
05730             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));
05731       } else if (ast_strlen_zero(challenge)) {
05732          ast_log(LOG_NOTICE, "No challenge provided for RSA authentication to %s\n", ast_inet_ntoa(sin->sin_addr));
05733       } else {
05734          char sig[256];
05735          struct ast_key *key;
05736          key = ast_key_get(keyn, AST_KEY_PRIVATE);
05737          if (!key) {
05738             ast_log(LOG_NOTICE, "Unable to find private key '%s'\n", keyn);
05739          } else {
05740             if (ast_sign(key, (char*)challenge, sig)) {
05741                ast_log(LOG_NOTICE, "Unable to sign challenge with key\n");
05742                res = -1;
05743             } else {
05744                iax_ie_append_str(ied, IAX_IE_RSA_RESULT, sig);
05745                res = 0;
05746             }
05747          }
05748       }
05749    } 
05750    /* Fall back */
05751    if (res && !ast_strlen_zero(secret)) {
05752       if ((authmethods & IAX_AUTH_MD5) && !ast_strlen_zero(challenge)) {
05753          struct MD5Context md5;
05754          unsigned char digest[16];
05755          char digres[128];
05756          MD5Init(&md5);
05757          MD5Update(&md5, (unsigned char *)challenge, strlen(challenge));
05758          MD5Update(&md5, (unsigned char *)secret, strlen(secret));
05759          MD5Final(digest, &md5);
05760          /* If they support md5, authenticate with it.  */
05761          for (x=0;x<16;x++)
05762             sprintf(digres + (x << 1),  "%2.2x", digest[x]); /* safe */
05763          if (ecx && dcx)
05764             build_enc_keys(digest, ecx, dcx);
05765          iax_ie_append_str(ied, IAX_IE_MD5_RESULT, digres);
05766          res = 0;
05767       } else if (authmethods & IAX_AUTH_PLAINTEXT) {
05768          iax_ie_append_str(ied, IAX_IE_PASSWORD, secret);
05769          res = 0;
05770       } else
05771          ast_log(LOG_NOTICE, "No way to send secret to peer '%s' (their methods: %d)\n", ast_inet_ntoa(sin->sin_addr), authmethods);
05772    }
05773    return res;
05774 }

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

References iax2_peer::addr, ao2_iterator_init(), ao2_iterator_next(), AST_FRAME_IAX, ast_mutex_lock(), ast_mutex_unlock(), ast_set_flag, ast_strdupa, ast_string_field_set, ast_strlen_zero(), authenticate(), iax_ies::authmethods, iax2_peer::authmethods, iax_ie_data::buf, chan_iax2_pvt::callno, iax2_registry::callno, chan_iax2_pvt::challenge, iax_ies::challenge, chan_iax2_pvt::dcx, chan_iax2_pvt::ecx, chan_iax2_pvt::encmethods, iax_ies::encmethods, IAX_AUTH_MD5, IAX_COMMAND_AUTHREP, IAX_ENCRYPTED, IAX_KEYPOPULATED, iax2_peer::mask, merge_encryption(), iax2_peer::name, iax2_peer::outkey, chan_iax2_pvt::peer, peer_unref(), iax_ie_data::pos, realtime_peer(), iax2_peer::secret, send_command(), chan_iax2_pvt::username, iax2_peer::username, and iax_ies::username.

Referenced by socket_process().

05781 {
05782    struct iax2_peer *peer = NULL;
05783    /* Start pessimistic */
05784    int res = -1;
05785    int authmethods = 0;
05786    struct iax_ie_data ied;
05787    uint16_t callno = p->callno;
05788 
05789    memset(&ied, 0, sizeof(ied));
05790    
05791    if (ies->username)
05792       ast_string_field_set(p, username, ies->username);
05793    if (ies->challenge)
05794       ast_string_field_set(p, challenge, ies->challenge);
05795    if (ies->authmethods)
05796       authmethods = ies->authmethods;
05797    if (authmethods & IAX_AUTH_MD5)
05798       merge_encryption(p, ies->encmethods);
05799    else
05800       p->encmethods = 0;
05801 
05802    /* Check for override RSA authentication first */
05803    if (!ast_strlen_zero(override) || !ast_strlen_zero(okey)) {
05804       /* Normal password authentication */
05805       res = authenticate(p->challenge, override, okey, authmethods, &ied, sin, &p->ecx, &p->dcx);
05806    } else {
05807       struct ao2_iterator i = ao2_iterator_init(peers, 0);
05808       while ((peer = ao2_iterator_next(&i))) {
05809          if ((ast_strlen_zero(p->peer) || !strcmp(p->peer, peer->name)) 
05810              /* No peer specified at our end, or this is the peer */
05811              && (ast_strlen_zero(peer->username) || (!strcmp(peer->username, p->username)))
05812              /* No username specified in peer rule, or this is the right username */
05813              && (!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)))
05814              /* No specified host, or this is our host */
05815             ) {
05816             res = authenticate(p->challenge, peer->secret, peer->outkey, authmethods, &ied, sin, &p->ecx, &p->dcx);
05817             if (!res) {
05818                peer_unref(peer);
05819                break;
05820             }
05821          }
05822          peer_unref(peer);
05823       }
05824       if (!peer) {
05825          /* We checked our list and didn't find one.  It's unlikely, but possible, 
05826             that we're trying to authenticate *to* a realtime peer */
05827          const char *peer_name = ast_strdupa(p->peer);
05828          ast_mutex_unlock(&iaxsl[callno]);
05829          if ((peer = realtime_peer(peer_name, NULL))) {
05830             ast_mutex_lock(&iaxsl[callno]);
05831             if (!(p = iaxs[callno])) {
05832                peer_unref(peer);
05833                return -1;
05834             }
05835             res = authenticate(p->challenge, peer->secret,peer->outkey, authmethods, &ied, sin, &p->ecx, &p->dcx);
05836             peer_unref(peer);
05837          }
05838          if (!peer) {
05839             ast_mutex_lock(&iaxsl[callno]);
05840             if (!(p = iaxs[callno]))
05841                return -1;
05842          }
05843       }
05844    }
05845    if (ies->encmethods)
05846       ast_set_flag(p, IAX_ENCRYPTED | IAX_KEYPOPULATED);
05847    if (!res)
05848       res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREP, 0, ied.buf, ied.pos, -1);
05849    return res;
05850 }

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 5458 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, iax_ie_data::pos, send_command(), send_command_final(), user_unref(), and chan_iax2_pvt::username.

Referenced by socket_process().

05459 {
05460    struct iax_ie_data ied;
05461    int res = -1, authreq_restrict = 0;
05462    char challenge[10];
05463    struct chan_iax2_pvt *p = iaxs[call_num];
05464 
05465    memset(&ied, 0, sizeof(ied));
05466 
05467    /* If an AUTHREQ restriction is in place, make sure we can send an AUTHREQ back */
05468    if (ast_test_flag(p, IAX_MAXAUTHREQ)) {
05469       struct iax2_user *user, tmp_user = {
05470          .name = p->username, 
05471       };
05472 
05473       user = ao2_find(users, &tmp_user, OBJ_POINTER);
05474       if (user) {
05475          if (user->curauthreq == user->maxauthreq)
05476             authreq_restrict = 1;
05477          else
05478             user->curauthreq++;
05479          user = user_unref(user);
05480       }
05481    }
05482 
05483    /* If the AUTHREQ limit test failed, send back an error */
05484    if (authreq_restrict) {
05485       iax_ie_append_str(&ied, IAX_IE_CAUSE, "Unauthenticated call limit reached");
05486       iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_CALL_REJECTED);
05487       send_command_final(p, AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied.buf, ied.pos, -1);
05488       return 0;
05489    }
05490 
05491    iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, p->authmethods);
05492    if (p->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_RSA)) {
05493       snprintf(challenge, sizeof(challenge), "%d", (int)ast_random());
05494       ast_string_field_set(p, challenge, challenge);
05495       /* snprintf(p->challenge, sizeof(p->challenge), "%d", (int)ast_random()); */
05496       iax_ie_append_str(&ied, IAX_IE_CHALLENGE, p->challenge);
05497    }
05498    if (p->encmethods)
05499       iax_ie_append_short(&ied, IAX_IE_ENCRYPTION, p->encmethods);
05500 
05501    iax_ie_append_str(&ied,IAX_IE_USERNAME, p->username);
05502 
05503    res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREQ, 0, ied.buf, ied.pos, -1);
05504 
05505    if (p->encmethods)
05506       ast_set_flag(p, IAX_ENCRYPTED);
05507 
05508    return res;
05509 }

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

Definition at line 5511 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::challenge, iax2_user::curauthreq, IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_AUTH_RSA, IAX_MAXAUTHREQ, IAX_STATE_AUTHENTICATED, chan_iax2_pvt::inkeys, LOG_WARNING, iax_ies::md5_result, MD5Final(), MD5Init(), MD5Update(), iax2_user::name, iax_ies::password, iax_ies::rsa_result, chan_iax2_pvt::secret, iax2_registry::secret, chan_iax2_pvt::state, user_unref(), and chan_iax2_pvt::username.

Referenced by socket_process().

05512 {
05513    char requeststr[256];
05514    char md5secret[256] = "";
05515    char secret[256] = "";
05516    char rsasecret[256] = "";
05517    int res = -1; 
05518    int x;
05519    struct iax2_user *user, tmp_user = {
05520       .name = p->username, 
05521    };
05522 
05523    user = ao2_find(users, &tmp_user, OBJ_POINTER);
05524    if (user) {
05525       if (ast_test_flag(p, IAX_MAXAUTHREQ)) {
05526          ast_atomic_fetchadd_int(&user->curauthreq, -1);
05527          ast_clear_flag(p, IAX_MAXAUTHREQ);
05528       }
05529       ast_string_field_set(p, host, user->name);
05530       user = user_unref(user);
05531    }
05532 
05533    if (!ast_test_flag(&p->state, IAX_STATE_AUTHENTICATED))
05534       return res;
05535    if (ies->password)
05536       ast_copy_string(secret, ies->password, sizeof(secret));
05537    if (ies->md5_result)
05538       ast_copy_string(md5secret, ies->md5_result, sizeof(md5secret));
05539    if (ies->rsa_result)
05540       ast_copy_string(rsasecret, ies->rsa_result, sizeof(rsasecret));
05541    if ((p->authmethods & IAX_AUTH_RSA) && !ast_strlen_zero(rsasecret) && !ast_strlen_zero(p->inkeys)) {
05542       struct ast_key *key;
05543       char *keyn;
05544       char tmpkey[256];
05545       char *stringp=NULL;
05546       ast_copy_string(tmpkey, p->inkeys, sizeof(tmpkey));
05547       stringp=tmpkey;
05548       keyn = strsep(&stringp, ":");
05549       while(keyn) {
05550          key = ast_key_get(keyn, AST_KEY_PUBLIC);
05551          if (key && !ast_check_signature(key, p->challenge, rsasecret)) {
05552             res = 0;
05553             break;
05554          } else if (!key)
05555             ast_log(LOG_WARNING, "requested inkey '%s' for RSA authentication does not exist\n", keyn);
05556          keyn = strsep(&stringp, ":");
05557       }
05558    } else if (p->authmethods & IAX_AUTH_MD5) {
05559       struct MD5Context md5;
05560       unsigned char digest[16];
05561       char *tmppw, *stringp;
05562       
05563       tmppw = ast_strdupa(p->secret);
05564       stringp = tmppw;
05565       while((tmppw = strsep(&stringp, ";"))) {
05566          MD5Init(&md5);
05567          MD5Update(&md5, (unsigned char *)p->challenge, strlen(p->challenge));
05568          MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
05569          MD5Final(digest, &md5);
05570          /* If they support md5, authenticate with it.  */
05571          for (x=0;x<16;x++)
05572             sprintf(requeststr + (x << 1), "%2.2x", digest[x]); /* safe */
05573          if (!strcasecmp(requeststr, md5secret)) {
05574             res = 0;
05575             break;
05576          }
05577       }
05578    } else if (p->authmethods & IAX_AUTH_PLAINTEXT) {
05579       if (!strcmp(secret, p->secret))
05580          res = 0;
05581    }
05582    return res;
05583 }

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

Definition at line 3200 of file chan_iax2.c.

References __auto_congest(), and schedule_action.

Referenced by iax2_call().

03201 {
03202 #ifdef SCHED_MULTITHREADED
03203    if (schedule_action(__auto_congest, data))
03204 #endif      
03205       __auto_congest(data);
03206    return 0;
03207 }

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

Definition at line 6541 of file chan_iax2.c.

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

Referenced by iax2_dprequest(), and iax2_provision().

06542 {
06543    int callno = (int)(long)(data);
06544    ast_mutex_lock(&iaxsl[callno]);
06545    if (iaxs[callno]) {
06546       iaxs[callno]->autoid = -1;
06547    }
06548    ast_mutex_unlock(&iaxsl[callno]);
06549 #ifdef SCHED_MULTITHREADED
06550    if (schedule_action(__auto_hangup, data))
06551 #endif      
06552       __auto_hangup(data);
06553    return 0;
06554 }

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

Definition at line 9290 of file chan_iax2.c.

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

Referenced by build_user().

09291 {
09292    struct iax2_context *con;
09293 
09294    if ((con = ast_calloc(1, sizeof(*con))))
09295       ast_copy_string(con->context, context, sizeof(con->context));
09296    
09297    return con;
09298 }

static void build_enc_keys ( const unsigned char *  digest,
aes_encrypt_ctx *  ecx,
aes_decrypt_ctx *  dcx 
) [static]

Definition at line 4233 of file chan_iax2.c.

References aes_decrypt_key128(), and aes_encrypt_key128().

Referenced by authenticate(), and decrypt_frame().

04234 {
04235    aes_encrypt_key128(digest, ecx);
04236    aes_decrypt_key128(digest, dcx);
04237 }

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 9437 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_free_ha(), ast_get_ip(), ast_log(), ast_parse_allow_disallow(), AST_SCHED_DEL, ast_set2_flag, ast_set_flag, ast_set_flags_to, ast_string_field_init, ast_string_field_set, ast_strlen_zero(), ast_test_flag, ast_true(), iax2_peer::authmethods, iax2_peer::capability, cid_name, cid_num, 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_FORCEJITTERBUF, IAX_HASCALLERID, IAX_NOTRANSFER, IAX_SENDANI, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, iax2_peer::inkeys, ast_variable::lineno, LOG_NOTICE, LOG_WARNING, iax2_peer::mailbox, mailbox, iax2_peer::mask, iax2_peer::maxms, ast_variable::name, iax2_peer::name, ast_variable::next, iax2_peer::outkey, peer_destructor(), peer_set_srcaddr(), peer_unref(), iax2_peer::peercontext, iax2_peer::pokeexpire, iax2_peer::pokefreqnotok, iax2_peer::pokefreqok, prefs, iax2_peer::prefs, iax2_peer::regexten, S_OR, iax2_registry::secret, iax2_peer::smoothing, iax2_peer::sockfd, unlink_peer(), ast_variable::value, and zonetag.

Referenced by realtime_peer(), and set_config().

09438 {
09439    struct iax2_peer *peer = NULL;
09440    struct ast_ha *oldha = NULL;
09441    int maskfound=0;
09442    int found=0;
09443    int firstpass=1;
09444    struct iax2_peer tmp_peer = {
09445       .name = name,
09446    };
09447 
09448    if (!temponly) {
09449       peer = ao2_find(peers, &tmp_peer, OBJ_POINTER);
09450       if (peer && !ast_test_flag(peer, IAX_DELME))
09451          firstpass = 0;
09452    }
09453 
09454    if (peer) {
09455       found++;
09456       if (firstpass) {
09457          oldha = peer->ha;
09458          peer->ha = NULL;
09459       }
09460       unlink_peer(peer);
09461    } else if ((peer = ao2_alloc(sizeof(*peer), peer_destructor))) {
09462       peer->expire = -1;
09463       peer->pokeexpire = -1;
09464       peer->sockfd = defaultsockfd;
09465       if (ast_string_field_init(peer, 32))
09466          peer = peer_unref(peer);
09467    }
09468 
09469    if (peer) {
09470       if (firstpass) {
09471          ast_copy_flags(peer, &globalflags, IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);
09472          peer->encmethods = iax2_encryption;
09473          peer->adsi = adsi;
09474          ast_string_field_set(peer,secret,"");
09475          if (!found) {
09476             ast_string_field_set(peer, name, name);
09477             peer->addr.sin_port = htons(IAX_DEFAULT_PORTNO);
09478             peer->expiry = min_reg_expire;
09479          }
09480          peer->prefs = prefs;
09481          peer->capability = iax2_capability;
09482          peer->smoothing = 0;
09483          peer->pokefreqok = DEFAULT_FREQ_OK;
09484          peer->pokefreqnotok = DEFAULT_FREQ_NOTOK;
09485          ast_string_field_set(peer,context,"");
09486          ast_string_field_set(peer,peercontext,"");
09487          ast_clear_flag(peer, IAX_HASCALLERID);
09488          ast_string_field_set(peer, cid_name, "");
09489          ast_string_field_set(peer, cid_num, "");
09490       }
09491 
09492       if (!v) {
09493          v = alt;
09494          alt = NULL;
09495       }
09496       while(v) {
09497          if (!strcasecmp(v->name, "secret")) {
09498             ast_string_field_set(peer, secret, v->value);
09499          } else if (!strcasecmp(v->name, "mailbox")) {
09500             ast_string_field_set(peer, mailbox, v->value);
09501          } else if (!strcasecmp(v->name, "hasvoicemail")) {
09502             if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
09503                ast_string_field_set(peer, mailbox, name);
09504             }
09505          } else if (!strcasecmp(v->name, "mohinterpret")) {
09506             ast_string_field_set(peer, mohinterpret, v->value);
09507          } else if (!strcasecmp(v->name, "mohsuggest")) {
09508             ast_string_field_set(peer, mohsuggest, v->value);
09509          } else if (!strcasecmp(v->name, "dbsecret")) {
09510             ast_string_field_set(peer, dbsecret, v->value);
09511          } else if (!strcasecmp(v->name, "trunk")) {
09512             ast_set2_flag(peer, ast_true(v->value), IAX_TRUNK);   
09513             if (ast_test_flag(peer, IAX_TRUNK) && (timingfd < 0)) {
09514                ast_log(LOG_WARNING, "Unable to support trunking on peer '%s' without timing\n", peer->name);
09515                ast_clear_flag(peer, IAX_TRUNK);
09516             }
09517          } else if (!strcasecmp(v->name, "auth")) {
09518             peer->authmethods = get_auth_methods(v->value);
09519          } else if (!strcasecmp(v->name, "encryption")) {
09520             peer->encmethods = get_encrypt_methods(v->value);
09521          } else if (!strcasecmp(v->name, "notransfer")) {
09522             ast_log(LOG_NOTICE, "The option 'notransfer' is deprecated in favor of 'transfer' which has options 'yes', 'no', and 'mediaonly'\n");
09523             ast_clear_flag(peer, IAX_TRANSFERMEDIA);  
09524             ast_set2_flag(peer, ast_true(v->value), IAX_NOTRANSFER); 
09525          } else if (!strcasecmp(v->name, "transfer")) {
09526             if (!strcasecmp(v->value, "mediaonly")) {
09527                ast_set_flags_to(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA);  
09528             } else if (ast_true(v->value)) {
09529                ast_set_flags_to(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
09530             } else 
09531                ast_set_flags_to(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
09532          } else if (!strcasecmp(v->name, "jitterbuffer")) {
09533             ast_set2_flag(peer, ast_true(v->value), IAX_USEJITTERBUF);  
09534          } else if (!strcasecmp(v->name, "forcejitterbuffer")) {
09535             ast_set2_flag(peer, ast_true(v->value), IAX_FORCEJITTERBUF);   
09536          } else if (!strcasecmp(v->name, "host")) {
09537             if (!strcasecmp(v->value, "dynamic")) {
09538                /* They'll register with us */
09539                ast_set_flag(peer, IAX_DYNAMIC); 
09540                if (!found) {
09541                   /* Initialize stuff iff we're not found, otherwise
09542                      we keep going with what we had */
09543                   memset(&peer->addr.sin_addr, 0, 4);
09544                   if (peer->addr.sin_port) {
09545                      /* If we've already got a port, make it the default rather than absolute */
09546                      peer->defaddr.sin_port = peer->addr.sin_port;
09547                      peer->addr.sin_port = 0;
09548                   }
09549                }
09550             } else {
09551                /* Non-dynamic.  Make sure we become that way if we're not */
09552                AST_SCHED_DEL(sched, peer->expire);
09553                ast_clear_flag(peer, IAX_DYNAMIC);
09554                if (ast_dnsmgr_lookup(v->value, &peer->addr.sin_addr, &peer->dnsmgr))
09555                   return peer_unref(peer);
09556                if (!peer->addr.sin_port)
09557                   peer->addr.sin_port = htons(IAX_DEFAULT_PORTNO);
09558             }
09559             if (!maskfound)
09560                inet_aton("255.255.255.255", &peer->mask);
09561          } else if (!strcasecmp(v->name, "defaultip")) {
09562             if (ast_get_ip(&peer->defaddr, v->value))
09563                return peer_unref(peer);
09564          } else if (!strcasecmp(v->name, "sourceaddress")) {
09565             peer_set_srcaddr(peer, v->value);
09566          } else if (!strcasecmp(v->name, "permit") ||
09567                   !strcasecmp(v->name, "deny")) {
09568             peer->ha = ast_append_ha(v->name, v->value, peer->ha);
09569          } else if (!strcasecmp(v->name, "mask")) {
09570             maskfound++;
09571             inet_aton(v->value, &peer->mask);
09572          } else if (!strcasecmp(v->name, "context")) {
09573             ast_string_field_set(peer, context, v->value);
09574          } else if (!strcasecmp(v->name, "regexten")) {
09575             ast_string_field_set(peer, regexten, v->value);
09576          } else if (!strcasecmp(v->name, "peercontext")) {
09577             ast_string_field_set(peer, peercontext, v->value);
09578          } else if (!strcasecmp(v->name, "port")) {
09579             if (ast_test_flag(peer, IAX_DYNAMIC))
09580                peer->defaddr.sin_port = htons(atoi(v->value));
09581             else
09582                peer->addr.sin_port = htons(atoi(v->value));
09583          } else if (!strcasecmp(v->name, "username")) {
09584             ast_string_field_set(peer, username, v->value);
09585          } else if (!strcasecmp(v->name, "allow")) {
09586             ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
09587          } else if (!strcasecmp(v->name, "disallow")) {
09588             ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
09589          } else if (!strcasecmp(v->name, "callerid")) {
09590             if (!ast_strlen_zero(v->value)) {
09591                char name2[80];
09592                char num2[80];
09593                ast_callerid_split(v->value, name2, sizeof(name2), num2, sizeof(num2));
09594                ast_string_field_set(peer, cid_name, name2);
09595                ast_string_field_set(peer, cid_num, num2);
09596             } else {
09597                ast_string_field_set(peer, cid_name, "");
09598                ast_string_field_set(peer, cid_num, "");
09599             }
09600             ast_set_flag(peer, IAX_HASCALLERID);
09601          } else if (!strcasecmp(v->name, "fullname")) {
09602             ast_string_field_set(peer, cid_name, S_OR(v->value, ""));
09603             ast_set_flag(peer, IAX_HASCALLERID);
09604          } else if (!strcasecmp(v->name, "cid_number")) {
09605             ast_string_field_set(peer, cid_num, S_OR(v->value, ""));
09606             ast_set_flag(peer, IAX_HASCALLERID);
09607          } else if (!strcasecmp(v->name, "sendani")) {
09608             ast_set2_flag(peer, ast_true(v->value), IAX_SENDANI); 
09609          } else if (!strcasecmp(v->name, "inkeys")) {
09610             ast_string_field_set(peer, inkeys, v->value);
09611          } else if (!strcasecmp(v->name, "outkey")) {
09612             ast_string_field_set(peer, outkey, v->value);
09613          } else if (!strcasecmp(v->name, "qualify")) {
09614             if (!strcasecmp(v->value, "no")) {
09615                peer->maxms = 0;
09616             } else if (!strcasecmp(v->value, "yes")) {
09617                peer->maxms = DEFAULT_MAXMS;
09618             } else if (sscanf(v->value, "%d", &peer->maxms) != 1) {
09619                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);
09620                peer->maxms = 0;
09621             }
09622          } else if (!strcasecmp(v->name, "qualifysmoothing")) {
09623             peer->smoothing = ast_true(v->value);
09624          } else if (!strcasecmp(v->name, "qualifyfreqok")) {
09625             if (sscanf(v->value, "%d", &peer->pokefreqok) != 1) {
09626                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);
09627             }
09628          } else if (!strcasecmp(v->name, "qualifyfreqnotok")) {
09629             if (sscanf(v->value, "%d", &peer->pokefreqnotok) != 1) {
09630                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);
09631             } else ast_log(LOG_WARNING, "Set peer->pokefreqnotok to %d\n", peer->pokefreqnotok);
09632          } else if (!strcasecmp(v->name, "timezone")) {
09633             ast_string_field_set(peer, zonetag, v->value);
09634          } else if (!strcasecmp(v->name, "adsi")) {
09635             peer->adsi = ast_true(v->value);
09636          }/* else if (strcasecmp(v->name,"type")) */
09637          /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
09638          v = v->next;
09639          if (!v) {
09640             v = alt;
09641             alt = NULL;
09642          }
09643       }
09644       if (!peer->authmethods)
09645          peer->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
09646       ast_clear_flag(peer, IAX_DELME); 
09647       /* Make sure these are IPv4 addresses */
09648       peer->addr.sin_family = AF_INET;
09649    }
09650    if (oldha)
09651       ast_free_ha(oldha);
09652    return peer;
09653 }

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 9669 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_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(), 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_FORCEJITTERBUF, IAX_HASCALLERID, IAX_NOTRANSFER, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, iax2_user::inkeys, ast_variable::lineno, LOG_NOTICE, LOG_WARNING, iax2_user::maxauthreq, ast_variable::name, iax2_user::name, ast_variable::next, iax2_context::next, prefs, iax2_user::prefs, iax2_registry::secret, iax2_user::secret, user_destructor(), user_unref(), ast_variable::value, and iax2_user::vars.

Referenced by realtime_user(), and set_config().

09670 {
09671    struct iax2_user *user = NULL;
09672    struct iax2_context *con, *conl = NULL;
09673    struct ast_ha *oldha = NULL;
09674    struct iax2_context *oldcon = NULL;
09675    int format;
09676    int firstpass=1;
09677    int oldcurauthreq = 0;
09678    char *varname = NULL, *varval = NULL;
09679    struct ast_variable *tmpvar = NULL;
09680    struct iax2_user tmp_user = {
09681       .name = name,
09682    };
09683 
09684    if (!temponly) {
09685       user = ao2_find(users, &tmp_user, OBJ_POINTER);
09686       if (user && !ast_test_flag(user, IAX_DELME))
09687          firstpass = 0;
09688    }
09689 
09690    if (user) {
09691       if (firstpass) {
09692          oldcurauthreq = user->curauthreq;
09693          oldha = user->ha;
09694          oldcon = user->contexts;
09695          user->ha = NULL;
09696          user->contexts = NULL;
09697       }
09698       /* Already in the list, remove it and it will be added back (or FREE'd) */
09699       ao2_unlink(users, user);
09700    } else {
09701       user = ao2_alloc(sizeof(*user), user_destructor);
09702    }
09703    
09704    if (user) {
09705       if (firstpass) {
09706          ast_string_field_free_memory(user);
09707          memset(user, 0, sizeof(struct iax2_user));
09708          if (ast_string_field_init(user, 32)) {
09709             user = user_unref(user);
09710             goto cleanup;
09711          }
09712          user->maxauthreq = maxauthreq;
09713          user->curauthreq = oldcurauthreq;
09714          user->prefs = prefs;
09715          user->capability = iax2_capability;
09716          user->encmethods = iax2_encryption;
09717          user->adsi = adsi;
09718          ast_string_field_set(user, name, name);
09719          ast_string_field_set(user, language, language);
09720          ast_copy_flags(user, &globalflags, IAX_USEJITTERBUF | IAX_FORCEJITTERBUF | IAX_CODEC_USER_FIRST | IAX_CODEC_NOPREFS | IAX_CODEC_NOCAP);   
09721          ast_clear_flag(user, IAX_HASCALLERID);
09722          ast_string_field_set(user, cid_name, "");
09723          ast_string_field_set(user, cid_num, "");
09724       }
09725       if (!v) {
09726          v = alt;
09727          alt = NULL;
09728       }
09729       while(v) {
09730          if (!strcasecmp(v->name, "context")) {
09731             con = build_context(v->value);
09732             if (con) {
09733                if (conl)
09734                   conl->next = con;
09735                else
09736                   user->contexts = con;
09737                conl = con;
09738             }
09739          } else if (!strcasecmp(v->name, "permit") ||
09740                   !strcasecmp(v->name, "deny")) {
09741             user->ha = ast_append_ha(v->name, v->value, user->ha);
09742          } else if (!strcasecmp(v->name, "setvar")) {
09743             varname = ast_strdupa(v->value);
09744             if (varname && (varval = strchr(varname,'='))) {
09745                *varval = '\0';
09746                varval++;
09747                if((tmpvar = ast_variable_new(varname, varval))) {
09748                   tmpvar->next = user->vars; 
09749                   user->vars = tmpvar;
09750                }
09751             }
09752          } else if (!strcasecmp(v->name, "allow")) {
09753             ast_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
09754          } else if (!strcasecmp(v->name, "disallow")) {
09755             ast_parse_allow_disallow(&user->prefs, &user->capability,v->value, 0);
09756          } else if (!strcasecmp(v->name, "trunk")) {
09757             ast_set2_flag(user, ast_true(v->value), IAX_TRUNK);   
09758             if (ast_test_flag(user, IAX_TRUNK) && (timingfd < 0)) {
09759                ast_log(LOG_WARNING, "Unable to support trunking on user '%s' without timing\n", user->name);
09760                ast_clear_flag(user, IAX_TRUNK);
09761             }
09762          } else if (!strcasecmp(v->name, "auth")) {
09763             user->authmethods = get_auth_methods(v->value);
09764          } else if (!strcasecmp(v->name, "encryption")) {
09765             user->encmethods = get_encrypt_methods(v->value);
09766          } else if (!strcasecmp(v->name, "notransfer")) {
09767             ast_log(LOG_NOTICE, "The option 'notransfer' is deprecated in favor of 'transfer' which has options 'yes', 'no', and 'mediaonly'\n");
09768             ast_clear_flag(user, IAX_TRANSFERMEDIA);  
09769             ast_set2_flag(user, ast_true(v->value), IAX_NOTRANSFER); 
09770          } else if (!strcasecmp(v->name, "transfer")) {
09771             if (!strcasecmp(v->value, "mediaonly")) {
09772                ast_set_flags_to(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA);  
09773             } else if (ast_true(v->value)) {
09774                ast_set_flags_to(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
09775             } else 
09776                ast_set_flags_to(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
09777          } else if (!strcasecmp(v->name, "codecpriority")) {
09778             if(!strcasecmp(v->value, "caller"))
09779                ast_set_flag(user, IAX_CODEC_USER_FIRST);
09780             else if(!strcasecmp(v->value, "disabled"))
09781                ast_set_flag(user, IAX_CODEC_NOPREFS);
09782             else if(!strcasecmp(v->value, "reqonly")) {
09783                ast_set_flag(user, IAX_CODEC_NOCAP);
09784                ast_set_flag(user, IAX_CODEC_NOPREFS);
09785             }
09786          } else if (!strcasecmp(v->name, "jitterbuffer")) {
09787             ast_set2_flag(user, ast_true(v->value), IAX_USEJITTERBUF);
09788          } else if (!strcasecmp(v->name, "forcejitterbuffer")) {
09789             ast_set2_flag(user, ast_true(v->value), IAX_FORCEJITTERBUF);
09790          } else if (!strcasecmp(v->name, "dbsecret")) {
09791             ast_string_field_set(user, dbsecret, v->value);
09792          } else if (!strcasecmp(v->name, "secret")) {
09793             if (!ast_strlen_zero(user->secret)) {
09794                char *old = ast_strdupa(user->secret);
09795 
09796                ast_string_field_build(user, secret, "%s;%s", old, v->value);
09797             } else
09798                ast_string_field_set(user, secret, v->value);
09799          } else if (!strcasecmp(v->name, "callerid")) {
09800             if (!ast_strlen_zero(v->value) && strcasecmp(v->value, "asreceived")) {
09801                char name2[80];
09802                char num2[80];
09803                ast_callerid_split(v->value, name2, sizeof(name2), num2, sizeof(num2));
09804                ast_string_field_set(user, cid_name, name2);
09805                ast_string_field_set(user, cid_num, num2);
09806                ast_set_flag(user, IAX_HASCALLERID);
09807             } else {
09808                ast_clear_flag(user, IAX_HASCALLERID);
09809                ast_string_field_set(user, cid_name, "");
09810                ast_string_field_set(user, cid_num, "");
09811             }
09812          } else if (!strcasecmp(v->name, "fullname")) {
09813             if (!ast_strlen_zero(v->value)) {
09814                ast_string_field_set(user, cid_name, v->value);
09815                ast_set_flag(user, IAX_HASCALLERID);
09816             } else {
09817                ast_string_field_set(user, cid_name, "");
09818                if (ast_strlen_zero(user->cid_num))
09819                   ast_clear_flag(user, IAX_HASCALLERID);
09820             }
09821          } else if (!strcasecmp(v->name, "cid_number")) {
09822             if (!ast_strlen_zero(v->value)) {
09823                ast_string_field_set(user, cid_num, v->value);
09824                ast_set_flag(user, IAX_HASCALLERID);
09825             } else {
09826                ast_string_field_set(user, cid_num, "");
09827                if (ast_strlen_zero(user->cid_name))
09828                   ast_clear_flag(user, IAX_HASCALLERID);
09829             }
09830          } else if (!strcasecmp(v->name, "accountcode")) {
09831             ast_string_field_set(user, accountcode, v->value);
09832          } else if (!strcasecmp(v->name, "mohinterpret")) {
09833             ast_string_field_set(user, mohinterpret, v->value);
09834          } else if (!strcasecmp(v->name, "mohsuggest")) {
09835             ast_string_field_set(user, mohsuggest, v->value);
09836          } else if (!strcasecmp(v->name, "language")) {
09837             ast_string_field_set(user, language, v->value);
09838          } else if (!strcasecmp(v->name, "amaflags")) {
09839             format = ast_cdr_amaflags2int(v->value);
09840             if (format < 0) {
09841                ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
09842             } else {
09843                user->amaflags = format;
09844             }
09845          } else if (!strcasecmp(v->name, "inkeys")) {
09846             ast_string_field_set(user, inkeys, v->value);
09847          } else if (!strcasecmp(v->name, "maxauthreq")) {
09848             user->maxauthreq = atoi(v->value);
09849             if (user->maxauthreq < 0)
09850                user->maxauthreq = 0;
09851          } else if (!strcasecmp(v->name, "adsi")) {
09852             user->adsi = ast_true(v->value);
09853          }/* else if (strcasecmp(v->name,"type")) */
09854          /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
09855          v = v->next;
09856          if (!v) {
09857             v = alt;
09858             alt = NULL;
09859          }
09860       }
09861       if (!user->authmethods) {
09862          if (!ast_strlen_zero(user->secret)) {
09863             user->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
09864             if (!ast_strlen_zero(user->inkeys))
09865                user->authmethods |= IAX_AUTH_RSA;
09866          } else if (!ast_strlen_zero(user->inkeys)) {
09867             user->authmethods = IAX_AUTH_RSA;
09868          } else {
09869             user->authmethods = IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT;
09870          }
09871       }
09872       ast_clear_flag(user, IAX_DELME);
09873    }
09874 cleanup:
09875    if (oldha)
09876       ast_free_ha(oldha);
09877    if (oldcon)
09878       free_context(oldcon);
09879    return user;
09880 }

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

Definition at line 10386 of file chan_iax2.c.

References ARRAY_LEN, AST_FRAME_IAX, ast_log(), ast_mutex_trylock(), ast_mutex_unlock(), ast_strdupa, ast_string_field_set, ast_strlen_zero(), iax_ie_data::buf, iax2_registry::callno, 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_DEBUG, LOG_WARNING, NEW_FORCE, option_debug, parse_dial_string(), parsed_dial_string::password, parsed_dial_string::peer, iax_ie_data::pos, iax2_registry::secret, send_command(), create_addr_info::sockfd, and parsed_dial_string::username.

Referenced by find_cache().

10387 {
10388    struct sockaddr_in sin;
10389    int x;
10390    int callno;
10391    struct iax_ie_data ied;
10392    struct create_addr_info cai;
10393    struct parsed_dial_string pds;
10394    char *tmpstr;
10395 
10396    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
10397       /* Look for an *exact match* call.  Once a call is negotiated, it can only
10398          look up entries for a single context */
10399       if (!ast_mutex_trylock(&iaxsl[x])) {
10400          if (iaxs[x] && !strcasecmp(data, iaxs[x]->dproot))
10401             return x;
10402          ast_mutex_unlock(&iaxsl[x]);
10403       }
10404    }
10405 
10406    /* No match found, we need to create a new one */
10407 
10408    memset(&cai, 0, sizeof(cai));
10409    memset(&ied, 0, sizeof(ied));
10410    memset(&pds, 0, sizeof(pds));
10411 
10412    tmpstr = ast_strdupa(data);
10413    parse_dial_string(tmpstr, &pds);
10414 
10415    if (ast_strlen_zero(pds.peer)) {
10416       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", data);
10417       return -1;
10418    }
10419 
10420    /* Populate our address from the given */
10421    if (create_addr(pds.peer, NULL, &sin, &cai))
10422       return -1;
10423 
10424    if (option_debug)
10425       ast_log(LOG_DEBUG, "peer: %s, username: %s, password: %s, context: %s\n",
10426          pds.peer, pds.username, pds.password, pds.context);
10427 
10428    callno = find_callno_locked(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
10429    if (callno < 1) {
10430       ast_log(LOG_WARNING, "Unable to create call\n");
10431       return -1;
10432    }
10433 
10434    ast_string_field_set(iaxs[callno], dproot, data);
10435    iaxs[callno]->capability = IAX_CAPABILITY_FULLBANDWIDTH;
10436 
10437    iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
10438    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, "TBD");
10439    /* the string format is slightly different from a standard dial string,
10440       because the context appears in the 'exten' position
10441    */
10442    if (pds.exten)
10443       iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, pds.exten);
10444    if (pds.username)
10445       iax_ie_append_str(&ied, IAX_IE_USERNAME, pds.username);
10446    iax_ie_append_int(&ied, IAX_IE_FORMAT, IAX_CAPABILITY_FULLBANDWIDTH);
10447    iax_ie_append_int(&ied, IAX_IE_CAPABILITY, IAX_CAPABILITY_FULLBANDWIDTH);
10448    /* Keep password handy */
10449    if (pds.password)
10450       ast_string_field_set(iaxs[callno], secret, pds.password);
10451    if (pds.key)
10452       ast_string_field_set(iaxs[callno], outkey, pds.key);
10453    /* Start the call going */
10454    send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
10455 
10456    return callno;
10457 }

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

Definition at line 4101 of file chan_iax2.c.

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

Referenced by schedule_delivery().

04102 {
04103    /* Returns where in "receive time" we are.  That is, how many ms
04104       since we received (or would have received) the frame with timestamp 0 */
04105    int ms;
04106 #ifdef IAXTESTS
04107    int jit;
04108 #endif /* IAXTESTS */
04109    /* Setup rxcore if necessary */
04110    if (ast_tvzero(p->rxcore)) {
04111       p->rxcore = ast_tvnow();
04112       if (option_debug && iaxdebug)
04113          ast_log(LOG_DEBUG, "calc_rxstamp: call=%d: rxcore set to %d.%6.6d - %dms\n",
04114                p->callno, (int)(p->rxcore.tv_sec), (int)(p->rxcore.tv_usec), offset);
04115       p->rxcore = ast_tvsub(p->rxcore, ast_samp2tv(offset, 1000));
04116 #if 1
04117       if (option_debug && iaxdebug)
04118          ast_log(LOG_DEBUG, "calc_rxstamp: call=%d: works out as %d.%6.6d\n",
04119                p->callno, (int)(p->rxcore.tv_sec),(int)( p->rxcore.tv_usec));
04120 #endif
04121    }
04122 
04123    ms = ast_tvdiff_ms(ast_tvnow(), p->rxcore);
04124 #ifdef IAXTESTS
04125    if (test_jit) {
04126       if (!test_jitpct || ((100.0 * ast_random() / (RAND_MAX + 1.0)) < test_jitpct)) {
04127          jit = (int)((float)test_jit * ast_random() / (RAND_MAX + 1.0));
04128          if ((int)(2.0 * ast_random() / (RAND_MAX + 1.0)))
04129             jit = -jit;
04130          ms += jit;
04131       }
04132    }
04133    if (test_late) {
04134       ms += test_late;
04135       test_late = 0;
04136    }
04137 #endif /* IAXTESTS */
04138    return ms;
04139 }

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

Definition at line 3972 of file chan_iax2.c.

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

Referenced by iax2_send(), and socket_process().

03973 {
03974    int ms;
03975    int voice = 0;
03976    int genuine = 0;
03977    int adjust;
03978    struct timeval *delivery = NULL;
03979 
03980 
03981    /* What sort of frame do we have?: voice is self-explanatory
03982       "genuine" means an IAX frame - things like LAGRQ/RP, PING/PONG, ACK
03983       non-genuine frames are CONTROL frames [ringing etc], DTMF
03984       The "genuine" distinction is needed because genuine frames must get a clock-based timestamp,
03985       the others need a timestamp slaved to the voice frames so that they go in sequence
03986    */
03987    if (f) {
03988       if (f->frametype == AST_FRAME_VOICE) {
03989          voice = 1;
03990          delivery = &f->delivery;
03991       } else if (f->frametype == AST_FRAME_IAX) {
03992          genuine = 1;
03993       } else if (f->frametype == AST_FRAME_CNG) {
03994          p->notsilenttx = 0;  
03995       }
03996    }
03997    if (ast_tvzero(p->offset)) {
03998       gettimeofday(&p->offset, NULL);
03999       /* Round to nearest 20ms for nice looking traces */
04000       p->offset.tv_usec -= p->offset.tv_usec % 20000;
04001    }
04002    /* If the timestamp is specified, just send it as is */
04003    if (ts)
04004       return ts;
04005    /* If we have a time that the frame arrived, always use it to make our timestamp */
04006    if (delivery && !ast_tvzero(*delivery)) {
04007       ms = ast_tvdiff_ms(*delivery, p->offset);
04008       if (option_debug > 2 && iaxdebug)
04009          ast_log(LOG_DEBUG, "calc_timestamp: call %d/%d: Timestamp slaved to delivery time\n", p->callno, iaxs[p->callno]->peercallno);
04010    } else {
04011       ms = ast_tvdiff_ms(ast_tvnow(), p->offset);
04012       if (ms < 0)
04013          ms = 0;
04014       if (voice) {
04015          /* On a voice frame, use predicted values if appropriate */
04016          if (p->notsilenttx && abs(ms - p->nextpred) <= MAX_TIMESTAMP_SKEW) {
04017             /* Adjust our txcore, keeping voice and non-voice synchronized */
04018             /* AN EXPLANATION:
04019                When we send voice, we usually send "calculated" timestamps worked out
04020                on the basis of the number of samples sent. When we send other frames,
04021                we usually send timestamps worked out from the real clock.
04022                The problem is that they can tend to drift out of step because the 
04023                   source channel's clock and our clock may not be exactly at the same rate.
04024                We fix this by continuously "tweaking" p->offset.  p->offset is "time zero"
04025                for this call.  Moving it adjusts timestamps for non-voice frames.
04026                We make the adjustment in the style of a moving average.  Each time we
04027                adjust p->offset by 10% of the difference between our clock-derived
04028                timestamp and the predicted timestamp.  That's why you see "10000"
04029                below even though IAX2 timestamps are in milliseconds.
04030                The use of a moving average avoids offset moving too radically.
04031                Generally, "adjust" roams back and forth around 0, with offset hardly
04032                changing at all.  But if a consistent different starts to develop it
04033                will be eliminated over the course of 10 frames (200-300msecs) 
04034             */
04035             adjust = (ms - p->nextpred);
04036             if (adjust < 0)
04037                p->offset = ast_tvsub(p->offset, ast_samp2tv(abs(adjust), 10000));
04038             else if (adjust > 0)
04039                p->offset = ast_tvadd(p->offset, ast_samp2tv(adjust, 10000));
04040 
04041             if (!p->nextpred) {
04042                p->nextpred = ms; /*f->samples / 8;*/
04043                if (p->nextpred <= p->lastsent)
04044                   p->nextpred = p->lastsent + 3;
04045             }
04046             ms = p->nextpred;
04047          } else {
04048                 /* in this case, just use the actual
04049             * time, since we're either way off
04050             * (shouldn't happen), or we're  ending a
04051             * silent period -- and seed the next
04052             * predicted time.  Also, round ms to the
04053             * next multiple of frame size (so our
04054             * silent periods are multiples of
04055             * frame size too) */
04056 
04057             if (option_debug && iaxdebug && abs(ms - p->nextpred) > MAX_TIMESTAMP_SKEW )
04058                ast_log(LOG_DEBUG, "predicted timestamp skew (%u) > max (%u), using real ts instead.\n",
04059                   abs(ms - p->nextpred), MAX_TIMESTAMP_SKEW);
04060 
04061             if (f->samples >= 8) /* check to make sure we dont core dump */
04062             {
04063                int diff = ms % (f->samples / 8);
04064                if (diff)
04065                    ms += f->samples/8 - diff;
04066             }
04067 
04068             p->nextpred = ms;
04069             p->notsilenttx = 1;
04070          }
04071       } else if ( f->frametype == AST_FRAME_VIDEO ) {
04072          /*
04073          * IAX2 draft 03 says that timestamps MUST be in order.
04074          * It does not say anything about several frames having the same timestamp
04075          * When transporting video, we can have a frame that spans multiple iax packets
04076          * (so called slices), so it would make sense to use the same timestamp for all of
04077          * them
04078          * We do want to make sure that frames don't go backwards though
04079          */
04080          if ( (unsigned int)ms < p->lastsent )
04081             ms = p->lastsent;
04082       } else {
04083          /* On a dataframe, use last value + 3 (to accomodate jitter buffer shrinking) if appropriate unless
04084             it's a genuine frame */
04085          if (genuine) {
04086             /* genuine (IAX LAGRQ etc) must keep their clock-based stamps */
04087             if (ms <= p->lastsent)
04088                ms = p->lastsent + 3;
04089          } else if (abs(ms - p->lastsent) <= MAX_TIMESTAMP_SKEW) {
04090             /* non-genuine frames (!?) (DTMF, CONTROL) should be pulled into the predicted stream stamps */
04091             ms = p->lastsent + 3;
04092          }
04093       }
04094    }
04095    p->lastsent = ms;
04096    if (voice)
04097       p->nextpred = p->nextpred + f->samples / 8;
04098    return ms;
04099 }

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

Definition at line 3928 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().

03929 {
03930    unsigned long int mssincetx; /* unsigned to handle overflows */
03931    long int ms, pred;
03932 
03933    tpeer->trunkact = *tv;
03934    mssincetx = ast_tvdiff_ms(*tv, tpeer->lasttxtime);
03935    if (mssincetx > 5000 || ast_tvzero(tpeer->txtrunktime)) {
03936       /* If it's been at least 5 seconds since the last time we transmitted on this trunk, reset our timers */
03937       tpeer->txtrunktime = *tv;
03938       tpeer->lastsent = 999999;
03939    }
03940    /* Update last transmit time now */
03941    tpeer->lasttxtime = *tv;
03942    
03943    /* Calculate ms offset */
03944    ms = ast_tvdiff_ms(*tv, tpeer->txtrunktime);
03945    /* Predict from last value */
03946    pred = tpeer->lastsent + sampms;
03947    if (abs(ms - pred) < MAX_TIMESTAMP_SKEW)
03948       ms = pred;
03949    
03950    /* We never send the same timestamp twice, so fudge a little if we must */
03951    if (ms == tpeer->lastsent)
03952       ms = tpeer->lastsent + 1;
03953    tpeer->lastsent = ms;
03954    return ms;
03955 }

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

Definition at line 5212 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_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, 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, iax2_user::contexts, iax2_user::dbsecret, iax_ies::dnid, iax2_user::encmethods, chan_iax2_pvt::encmethods, exten, iax_ies::format, iax2_user::ha, iax2_getpeertrunk(), IAX_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, IAX_FORCEJITTERBUF, IAX_HASCALLERID, IAX_MAXAUTHREQ, IAX_NOTRANSFER, IAX_PROTO_VERSION, 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, 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(), iax2_registry::secret, iax2_user::secret, user_unref(), iax_ies::username, ast_variable::value, chan_iax2_pvt::vars, iax2_user::vars, and iax_ies::version.

Referenced by socket_process().

05213 {
05214    /* Start pessimistic */
05215    int res = -1;
05216    int version = 2;
05217    struct iax2_user *user = NULL, *best = NULL;
05218    int bestscore = 0;
05219    int gotcapability = 0;
05220    struct ast_variable *v = NULL, *tmpvar = NULL;
05221    struct ao2_iterator i;
05222 
05223    if (!iaxs[callno])
05224       return res;
05225    if (ies->called_number)
05226       ast_string_field_set(iaxs[callno], exten, ies->called_number);
05227    if (ies->calling_number) {
05228       ast_shrink_phone_number(ies->calling_number);
05229       ast_string_field_set(iaxs[callno], cid_num, ies->calling_number);
05230    }
05231    if (ies->calling_name)
05232       ast_string_field_set(iaxs[callno], cid_name, ies->calling_name);
05233    if (ies->calling_ani)
05234       ast_string_field_set(iaxs[callno], ani, ies->calling_ani);
05235    if (ies->dnid)
05236       ast_string_field_set(iaxs[callno], dnid, ies->dnid);
05237    if (ies->rdnis)
05238       ast_string_field_set(iaxs[callno], rdnis, ies->rdnis);
05239    if (ies->called_context)
05240       ast_string_field_set(iaxs[callno], context, ies->called_context);
05241    if (ies->language)
05242       ast_string_field_set(iaxs[callno], language, ies->language);
05243    if (ies->username)
05244       ast_string_field_set(iaxs[callno], username, ies->username);
05245    if (ies->calling_ton > -1)
05246       iaxs[callno]->calling_ton = ies->calling_ton;
05247    if (ies->calling_tns > -1)
05248       iaxs[callno]->calling_tns = ies->calling_tns;
05249    if (ies->calling_pres > -1)
05250       iaxs[callno]->calling_pres = ies->calling_pres;
05251    if (ies->format)
05252       iaxs[callno]->peerformat = ies->format;
05253    if (ies->adsicpe)
05254       iaxs[callno]->peeradsicpe = ies->adsicpe;
05255    if (ies->capability) {
05256       gotcapability = 1;
05257       iaxs[callno]->peercapability = ies->capability;
05258    } 
05259    if (ies->version)
05260       version = ies->version;
05261 
05262    /* Use provided preferences until told otherwise for actual preferences */
05263    if(ies->codec_prefs) {
05264       ast_codec_pref_convert(&iaxs[callno]->rprefs, ies->codec_prefs, 32, 0);
05265       ast_codec_pref_convert(&iaxs[callno]->prefs, ies->codec_prefs, 32, 0);
05266    }
05267 
05268    if (!gotcapability) 
05269       iaxs[callno]->peercapability = iaxs[callno]->peerformat;
05270    if (version > IAX_PROTO_VERSION) {
05271       ast_log(LOG_WARNING, "Peer '%s' has too new a protocol version (%d) for me\n", 
05272          ast_inet_ntoa(sin->sin_addr), version);
05273       return res;
05274    }
05275    /* Search the userlist for a compatible entry, and fill in the rest */
05276    i = ao2_iterator_init(users, 0);
05277    while ((user = ao2_iterator_next(&i))) {
05278       if ((ast_strlen_zero(iaxs[callno]->username) ||          /* No username specified */
05279          !strcmp(iaxs[callno]->username, user->name)) /* Or this username specified */
05280          && ast_apply_ha(user->ha, sin)   /* Access is permitted from this IP */
05281          && (ast_strlen_zero(iaxs[callno]->context) ||         /* No context specified */
05282               apply_context(user->contexts, iaxs[callno]->context))) {        /* Context is permitted */
05283          if (!ast_strlen_zero(iaxs[callno]->username)) {
05284             /* Exact match, stop right now. */
05285             if (best)
05286                user_unref(best);
05287             best = user;
05288             break;
05289          } else if (ast_strlen_zero(user->secret) && ast_strlen_zero(user->dbsecret) && ast_strlen_zero(user->inkeys)) {
05290             /* No required authentication */
05291             if (user->ha) {
05292                /* There was host authentication and we passed, bonus! */
05293                if (bestscore < 4) {
05294                   bestscore = 4;
05295                   if (best)
05296                      user_unref(best);
05297                   best = user;
05298                   continue;
05299                }
05300             } else {
05301                /* No host access, but no secret, either, not bad */
05302                if (bestscore < 3) {
05303                   bestscore = 3;
05304                   if (best)
05305                      user_unref(best);
05306                   best = user;
05307                   continue;
05308                }
05309             }
05310          } else {
05311             if (user->ha) {
05312                /* Authentication, but host access too, eh, it's something.. */
05313                if (bestscore < 2) {
05314                   bestscore = 2;
05315                   if (best)
05316                      user_unref(best);
05317                   best = user;
05318                   continue;
05319                }
05320             } else {
05321                /* Authentication and no host access...  This is our baseline */
05322                if (bestscore < 1) {
05323                   bestscore = 1;
05324                   if (best)
05325                      user_unref(best);
05326                   best = user;
05327                   continue;
05328                }
05329             }
05330          }
05331       }
05332       user_unref(user);
05333    }
05334    user = best;
05335    if (!user && !ast_strlen_zero(iaxs[callno]->username)) {
05336       user = realtime_user(iaxs[callno]->username, sin);
05337       if (user && !ast_strlen_zero(iaxs[callno]->context) &&         /* No context specified */
05338           !apply_context(user->contexts, iaxs[callno]->context)) {      /* Context is permitted */
05339          user = user_unref(user);
05340       }
05341    }
05342    if (user) {
05343       /* We found our match (use the first) */
05344       /* copy vars */
05345       for (v = user->vars ; v ; v = v->next) {
05346          if((tmpvar = ast_variable_new(v->name, v->value))) {
05347             tmpvar->next = iaxs[callno]->vars; 
05348             iaxs[callno]->vars = tmpvar;
05349          }
05350       }
05351       /* If a max AUTHREQ restriction is in place, activate it */
05352       if (user->maxauthreq > 0)
05353          ast_set_flag(iaxs[callno], IAX_MAXAUTHREQ);
05354       iaxs[callno]->prefs = user->prefs;
05355       ast_copy_flags(iaxs[callno], user, IAX_CODEC_USER_FIRST);
05356       ast_copy_flags(iaxs[callno], user, IAX_CODEC_NOPREFS);
05357       ast_copy_flags(iaxs[callno], user, IAX_CODEC_NOCAP);
05358       iaxs[callno]->encmethods = user->encmethods;
05359       /* Store the requested username if not specified */
05360       if (ast_strlen_zero(iaxs[callno]->username))
05361          ast_string_field_set(iaxs[callno], username, user->name);
05362       /* Store whether this is a trunked call, too, of course, and move if appropriate */
05363       ast_copy_flags(iaxs[callno], user, IAX_TRUNK);
05364       iaxs[callno]->capability = user->capability;
05365       /* And use the default context */
05366       if (ast_strlen_zero(iaxs[callno]->context)) {
05367          if (user->contexts)
05368             ast_string_field_set(iaxs[callno], context, user->contexts->context);
05369          else
05370             ast_string_field_set(iaxs[callno], context, context);
05371       }
05372       /* And any input keys */
05373       ast_string_field_set(iaxs[callno], inkeys, user->inkeys);
05374       /* And the permitted authentication methods */
05375       iaxs[callno]->authmethods = user->authmethods;
05376       iaxs[callno]->adsi = user->adsi;
05377       /* If the user has callerid, override the remote caller id. */
05378       if (ast_test_flag(user, IAX_HASCALLERID)) {
05379          iaxs[callno]->calling_tns = 0;
05380          iaxs[callno]->calling_ton = 0;
05381          ast_string_field_set(iaxs[callno], cid_num, user->cid_num);
05382          ast_string_field_set(iaxs[callno], cid_name, user->cid_name);
05383          ast_string_field_set(iaxs[callno], ani, user->cid_num);
05384          iaxs[callno]->calling_pres = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
05385       } else if (ast_strlen_zero(iaxs[callno]->cid_num) && ast_strlen_zero(iaxs[callno]->cid_name)) {
05386          iaxs[callno]->calling_pres = AST_PRES_NUMBER_NOT_AVAILABLE;
05387       } /* else user is allowed to set their own CID settings */
05388       if (!ast_strlen_zero(user->accountcode))
05389          ast_string_field_set(iaxs[callno], accountcode, user->accountcode);
05390       if (!ast_strlen_zero(user->mohinterpret))
05391          ast_string_field_set(iaxs[callno], mohinterpret, user->mohinterpret);
05392       if (!ast_strlen_zero(user->mohsuggest))
05393          ast_string_field_set(iaxs[callno], mohsuggest, user->mohsuggest);
05394       if (user->amaflags)
05395          iaxs[callno]->amaflags = user->amaflags;
05396       if (!ast_strlen_zero(user->language))
05397          ast_string_field_set(iaxs[callno], language, user->language);
05398       ast_copy_flags(iaxs[callno], user, IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);   
05399       /* Keep this check last */
05400       if (!ast_strlen_zero(user->dbsecret)) {
05401          char *family, *key=NULL;
05402          char buf[80];
05403          family = ast_strdupa(user->dbsecret);
05404          key = strchr(family, '/');
05405          if (key) {
05406             *key = '\0';
05407             key++;
05408          }
05409          if (!key || ast_db_get(family, key, buf, sizeof(buf)))
05410             ast_log(LOG_WARNING, "Unable to retrieve database password for family/key '%s'!\n", user->dbsecret);
05411          else
05412             ast_string_field_set(iaxs[callno], secret, buf);
05413       } else
05414          ast_string_field_set(iaxs[callno], secret, user->secret);
05415       res = 0;
05416       user = user_unref(user);
05417    }
05418    ast_set2_flag(iaxs[callno], iax2_getpeertrunk(*sin), IAX_TRUNK);  
05419    return res;
05420 }

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

Definition at line 6899 of file chan_iax2.c.

References ast_log(), iax2_provision(), iax_provision_version(), LOG_DEBUG, and option_debug.

Referenced by socket_process().

06900 {
06901    unsigned int ourver;
06902    char rsi[80];
06903    snprintf(rsi, sizeof(rsi), "si-%s", si);
06904    if (iax_provision_version(&ourver, rsi, 1))
06905       return 0;
06906    if (option_debug)
06907       ast_log(LOG_DEBUG, "Service identifier '%s', we think '%08x', they think '%08x'\n", si, ourver, ver);
06908    if (ourver != ver) 
06909       iax2_provision(sin, sockfd, NULL, rsi, 1);
06910    return 0;
06911 }

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

References ast_log(), errno, LOG_DEBUG, LOG_ERROR, and option_debug.

Referenced by peer_set_srcaddr().

09317 {
09318    int sd;
09319    int res;
09320    
09321    sd = socket(AF_INET, SOCK_DGRAM, 0);
09322    if (sd < 0) {
09323       ast_log(LOG_ERROR, "Socket: %s\n", strerror(errno));
09324       return -1;
09325    }
09326 
09327    res = bind(sd, sa, salen);
09328    if (res < 0) {
09329       if (option_debug)
09330          ast_log(LOG_DEBUG, "Can't bind: %s\n", strerror(errno));
09331       close(sd);
09332       return 1;
09333    }
09334 
09335    close(sd);
09336    return 0;
09337 }

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

Definition at line 5899 of file chan_iax2.c.

References ARRAY_LEN, ast_copy_string(), ast_log(), ast_mutex_lock(), ast_mutex_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, dpcache_lock, chan_iax2_pvt::dpentries, iax_ies::dpstatus, errno, iax2_dpcache::expiry, expiry, iax2_dpcache::exten, exten, iax2_dpcache::flags, IAX_DPSTATUS_CANEXIST, IAX_DPSTATUS_EXISTS, IAX_DPSTATUS_IGNOREPAT, IAX_DPSTATUS_MATCHMORE, IAX_DPSTATUS_NONEXISTENT, LOG_WARNING, iax2_dpcache::orig, iax2_dpcache::peer, iax_ies::refresh, and iax2_dpcache::waiters.

Referenced by socket_process().

05900 {
05901    char exten[256] = "";
05902    int status = CACHE_FLAG_UNKNOWN;
05903    int expiry = iaxdefaultdpcache;
05904    int x;
05905    int matchmore = 0;
05906    struct iax2_dpcache *dp, *prev;
05907    
05908    if (ies->called_number)
05909       ast_copy_string(exten, ies->called_number, sizeof(exten));
05910 
05911    if (ies->dpstatus & IAX_DPSTATUS_EXISTS)
05912       status = CACHE_FLAG_EXISTS;
05913    else if (ies->dpstatus & IAX_DPSTATUS_CANEXIST)
05914       status = CACHE_FLAG_CANEXIST;
05915    else if (ies->dpstatus & IAX_DPSTATUS_NONEXISTENT)
05916       status = CACHE_FLAG_NONEXISTENT;
05917 
05918    if (ies->dpstatus & IAX_DPSTATUS_IGNOREPAT) {
05919       /* Don't really do anything with this */
05920    }
05921    if (ies->refresh)
05922       expiry = ies->refresh;
05923    if (ies->dpstatus & IAX_DPSTATUS_MATCHMORE)
05924       matchmore = CACHE_FLAG_MATCHMORE;
05925    ast_mutex_lock(&dpcache_lock);
05926    prev = NULL;
05927    dp = pvt->dpentries;
05928    while(dp) {
05929       if (!strcmp(dp->exten, exten)) {
05930          /* Let them go */
05931          if (prev)
05932             prev->peer = dp->peer;
05933          else
05934             pvt->dpentries = dp->peer;
05935          dp->peer = NULL;
05936          dp->callno = 0;
05937          dp->expiry.tv_sec = dp->orig.tv_sec + expiry;
05938          if (dp->flags & CACHE_FLAG_PENDING) {
05939             dp->flags &= ~CACHE_FLAG_PENDING;
05940             dp->flags |= status;
05941             dp->flags |= matchmore;
05942          }
05943          /* Wake up waiters */
05944          for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
05945             if (dp->waiters[x] > -1) {
05946                if (write(dp->waiters[x], "asdf", 4) < 0) {
05947                   ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
05948                }
05949             }
05950          }
05951       }
05952       prev = dp;
05953       dp = dp->peer;
05954    }
05955    ast_mutex_unlock(&dpcache_lock);
05956    return 0;
05957 }

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

Definition at line 2463 of file chan_iax2.c.

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

02464 {
02465    int which = 0;
02466    struct iax2_peer *peer;
02467    char *res = NULL;
02468    int wordlen = strlen(word);
02469    struct ao2_iterator i;
02470 
02471    /* 0 - iax2; 1 - show; 2 - peer; 3 - <peername> */
02472    if (pos != 3)
02473       return NULL;
02474 
02475    i = ao2_iterator_init(peers, 0);
02476    while ((peer = ao2_iterator_next(&i))) {
02477       if (!strncasecmp(peer->name, word, wordlen) && ++which > state) {
02478          res = ast_strdup(peer->name);
02479          peer_unref(peer);
02480          break;
02481       }
02482       peer_unref(peer);
02483    }
02484 
02485    return res;
02486 }

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

Definition at line 5959 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(), iaxq, 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, chan_iax2_pvt::pingtime, ast_iax2_queue::queue, remove_by_peercallno(), 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().

05960 {
05961    int peercallno = 0;
05962    struct chan_iax2_pvt *pvt = iaxs[callno];
05963    struct iax_frame *cur;
05964    jb_frame frame;
05965 
05966    if (ies->callno)
05967       peercallno = ies->callno;
05968 
05969    if (peercallno < 1) {
05970       ast_log(LOG_WARNING, "Invalid transfer request\n");
05971       return -1;
05972    }
05973    memcpy(&pvt->addr, &pvt->transfer, sizeof(pvt->addr));
05974    memset(&pvt->transfer, 0, sizeof(pvt->transfer));
05975    /* Reset sequence numbers */
05976    pvt->oseqno = 0;
05977    pvt->rseqno = 0;
05978    pvt->iseqno = 0;
05979    pvt->aseqno = 0;
05980 
05981    if (pvt->peercallno) {
05982       remove_by_peercallno(pvt);
05983    }
05984    pvt->peercallno = peercallno;
05985    store_by_peercallno(pvt);
05986 
05987    pvt->transferring = TRANSFER_NONE;
05988    pvt->svoiceformat = -1;
05989    pvt->voiceformat = 0;
05990    pvt->svideoformat = -1;
05991    pvt->videoformat = 0;
05992    pvt->transfercallno = -1;
05993    memset(&pvt->rxcore, 0, sizeof(pvt->rxcore));
05994    memset(&pvt->offset, 0, sizeof(pvt->offset));
05995    /* reset jitterbuffer */
05996    while(jb_getall(pvt->jb,&frame) == JB_OK)
05997       iax2_frame_free(frame.data);
05998    jb_reset(pvt->jb);
05999    pvt->lag = 0;
06000    pvt->last = 0;
06001    pvt->lastsent = 0;
06002    pvt->nextpred = 0;
06003    pvt->pingtime = DEFAULT_RETRY_TIME;
06004    AST_LIST_LOCK(&iaxq.queue);
06005    AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
06006       /* We must cancel any packets that would have been transmitted
06007          because now we're talking to someone new.  It's okay, they
06008          were transmitted to someone that didn't care anyway. */
06009       if (callno == cur->callno) 
06010          cur->retries = -1;
06011    }
06012    AST_LIST_UNLOCK(&iaxq.queue);
06013    return 0; 
06014 }

static unsigned char compress_subclass ( int  subclass  )  [static]

Definition at line 1072 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().

01073 {
01074    int x;
01075    int power=-1;
01076    /* If it's 128 or smaller, just return it */
01077    if (subclass < IAX_FLAG_SC_LOG)
01078       return subclass;
01079    /* Otherwise find its power */
01080    for (x = 0; x < IAX_MAX_SHIFT; x++) {
01081       if (subclass & (1 << x)) {
01082          if (power > -1) {
01083             ast_log(LOG_WARNING, "Can't compress subclass %d\n", subclass);
01084             return 0;
01085          } else
01086             power = x;
01087       }
01088    }
01089    return power | IAX_FLAG_SC_LOG;
01090 }

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

Definition at line 6913 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().

06914 {
06915    jb_info stats;
06916    jb_getinfo(pvt->jb, &stats);
06917    
06918    memset(iep, 0, sizeof(*iep));
06919 
06920    iax_ie_append_int(iep,IAX_IE_RR_JITTER, stats.jitter);
06921    if(stats.frames_in == 0) stats.frames_in = 1;
06922    iax_ie_append_int(iep,IAX_IE_RR_LOSS, ((0xff & (stats.losspct/1000)) << 24 | (stats.frames_lost & 0x00ffffff)));
06923    iax_ie_append_int(iep,IAX_IE_RR_PKTS, stats.frames_in);
06924    iax_ie_append_short(iep,IAX_IE_RR_DELAY, stats.current - stats.min);
06925    iax_ie_append_int(iep,IAX_IE_RR_DROPPED, stats.frames_dropped);
06926    iax_ie_append_int(iep,IAX_IE_RR_OOO, stats.frames_ooo);
06927 }

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

Definition at line 3092 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_gethostbyname(), 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, hp, IAX_DEFAULT_PORTNO, IAX_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_SENDANI, IAX_TRANSFERMEDIA, IAX_TRUNK, IAX_USEJITTERBUF, iax2_peer::lastms, LOG_DEBUG, 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().

03093 {
03094    struct ast_hostent ahp;
03095    struct hostent *hp;
03096    struct iax2_peer *peer;
03097    int res = -1;
03098    struct ast_codec_pref ourprefs;
03099 
03100    ast_clear_flag(cai, IAX_SENDANI | IAX_TRUNK);
03101    cai->sockfd = defaultsockfd;
03102    cai->maxtime = 0;
03103    sin->sin_family = AF_INET;
03104 
03105    if (!(peer = find_peer(peername, 1))) {
03106       cai->found = 0;
03107 
03108       hp = ast_gethostbyname(peername, &ahp);
03109       if (hp) {
03110          memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
03111          sin->sin_port = htons(IAX_DEFAULT_PORTNO);
03112          /* use global iax prefs for unknown peer/user */
03113          /* But move the calling channel's native codec to the top of the preference list */
03114          memcpy(&ourprefs, &prefs, sizeof(ourprefs));
03115          if (c)
03116             ast_codec_pref_prepend(&ourprefs, c->nativeformats, 1);
03117          ast_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
03118          return 0;
03119       } else {
03120          ast_log(LOG_WARNING, "No such host: %s\n", peername);
03121          return -1;
03122       }
03123    }
03124 
03125    cai->found = 1;
03126    
03127    /* if the peer has no address (current or default), return failure */
03128    if (!(peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr))
03129       goto return_unref;
03130 
03131    /* if the peer is being monitored and is currently unreachable, return failure */
03132    if (peer->maxms && ((peer->lastms > peer->maxms) || (peer->lastms < 0)))
03133       goto return_unref;
03134 
03135    ast_copy_flags(cai, peer, IAX_SENDANI | IAX_TRUNK | IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);
03136    cai->maxtime = peer->maxms;
03137    cai->capability = peer->capability;
03138    cai->encmethods = peer->encmethods;
03139    cai->sockfd = peer->sockfd;
03140    cai->adsi = peer->adsi;
03141    memcpy(&ourprefs, &peer->prefs, sizeof(ourprefs));
03142    /* Move the calling channel's native codec to the top of the preference list */
03143    if (c) {
03144       ast_log(LOG_DEBUG, "prepending %x to prefs\n", c->nativeformats);
03145       ast_codec_pref_prepend(&ourprefs, c->nativeformats, 1);
03146    }
03147    ast_codec_pref_convert(&ourprefs, cai->prefs, sizeof(cai->prefs), 1);
03148    ast_copy_string(cai->context, peer->context, sizeof(cai->context));
03149    ast_copy_string(cai->peercontext, peer->peercontext, sizeof(cai->peercontext));
03150    ast_copy_string(cai->username, peer->username, sizeof(cai->username));
03151    ast_copy_string(cai->timezone, peer->zonetag, sizeof(cai->timezone));
03152    ast_copy_string(cai->outkey, peer->outkey, sizeof(cai->outkey));
03153    ast_copy_string(cai->mohinterpret, peer->mohinterpret, sizeof(cai->mohinterpret));
03154    ast_copy_string(cai->mohsuggest, peer->mohsuggest, sizeof(cai->mohsuggest));
03155    if (ast_strlen_zero(peer->dbsecret)) {
03156       ast_copy_string(cai->secret, peer->secret, sizeof(cai->secret));
03157    } else {
03158       char *family;
03159       char *key = NULL;
03160 
03161       family = ast_strdupa(peer->dbsecret);
03162       key = strchr(family, '/');
03163       if (key)
03164          *key++ = '\0';
03165       if (!key || ast_db_get(family, key, cai->secret, sizeof(cai->secret))) {
03166          ast_log(LOG_WARNING, "Unable to retrieve database password for family/key '%s'!\n", peer->dbsecret);
03167          goto return_unref;
03168       }
03169    }
03170 
03171    if (peer->addr.sin_addr.s_addr) {
03172       sin->sin_addr = peer->addr.sin_addr;
03173       sin->sin_port = peer->addr.sin_port;
03174    } else {
03175       sin->sin_addr = peer->defaddr.sin_addr;
03176       sin->sin_port = peer->defaddr.sin_port;
03177    }
03178 
03179    res = 0;
03180 
03181 return_unref:
03182    peer_unref(peer);
03183 
03184    return res;
03185 }

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

Definition at line 4287 of file chan_iax2.c.

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

Referenced by decrypt_frame().

04288 {
04289    int padding;
04290    unsigned char *workspace;
04291 
04292    workspace = alloca(*datalen);
04293    memset(f, 0, sizeof(*f));
04294    if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
04295       struct ast_iax2_full_enc_hdr *efh = (struct ast_iax2_full_enc_hdr *)fh;
04296       if (*datalen < 16 + sizeof(struct ast_iax2_full_hdr))
04297          return -1;
04298       /* Decrypt */
04299       memcpy_decrypt(workspace, efh->encdata, *datalen - sizeof(struct ast_iax2_full_enc_hdr), dcx);
04300 
04301       padding = 16 + (workspace[15] & 0xf);
04302       if (option_debug && iaxdebug)
04303          ast_log(LOG_DEBUG, "Decoding full frame with length %d (padding = %d) (15=%02x)\n", *datalen, padding, workspace[15]);
04304       if (*datalen < padding + sizeof(struct ast_iax2_full_hdr))
04305          return -1;
04306 
04307       *datalen -= padding;
04308       memcpy(efh->encdata, workspace + padding, *datalen - sizeof(struct ast_iax2_full_enc_hdr));
04309       f->frametype = fh->type;
04310       if (f->frametype == AST_FRAME_VIDEO) {
04311          f->subclass = uncompress_subclass(fh->csub & ~0x40) | ((fh->csub >> 6) & 0x1);
04312       } else {
04313          f->subclass = uncompress_subclass(fh->csub);
04314       }
04315    } else {
04316       struct ast_iax2_mini_enc_hdr *efh = (struct ast_iax2_mini_enc_hdr *)fh;
04317       if (option_debug && iaxdebug)
04318          ast_log(LOG_DEBUG, "Decoding mini with length %d\n", *datalen);
04319       if (*datalen < 16 + sizeof(struct ast_iax2_mini_hdr))
04320          return -1;
04321       /* Decrypt */
04322       memcpy_decrypt(workspace, efh->encdata, *datalen - sizeof(struct ast_iax2_mini_enc_hdr), dcx);
04323       padding = 16 + (workspace[15] & 0x0f);
04324       if (*datalen < padding + sizeof(struct ast_iax2_mini_hdr))
04325          return -1;
04326       *datalen -= padding;
04327       memcpy(efh->encdata, workspace + padding, *datalen - sizeof(struct ast_iax2_mini_enc_hdr));
04328    }
04329    return 0;
04330 }

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

Definition at line 4373 of file chan_iax2.c.

References ast_set_flag, ast_strdupa, ast_test_flag, build_enc_keys(), decode_frame(), IAX_KEYPOPULATED, MD5Final(), MD5Init(), MD5Update(), and iax2_registry::secret.

Referenced by socket_process().

04374 {
04375    int res=-1;
04376    if (!ast_test_flag(iaxs[callno], IAX_KEYPOPULATED)) {
04377       /* Search for possible keys, given secrets */
04378       struct MD5Context md5;
04379       unsigned char digest[16];
04380       char *tmppw, *stringp;
04381       
04382       tmppw = ast_strdupa(iaxs[callno]->secret);
04383       stringp = tmppw;
04384       while ((tmppw = strsep(&stringp, ";"))) {
04385          MD5Init(&md5);
04386          MD5Update(&md5, (unsigned char *)iaxs[callno]->challenge, strlen(iaxs[callno]->challenge));
04387          MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
04388          MD5Final(digest, &md5);
04389          build_enc_keys(digest, &iaxs[callno]->ecx, &iaxs[callno]->dcx);
04390          res = decode_frame(&iaxs[callno]->dcx, fh, f, datalen);
04391          if (!res) {
04392             ast_set_flag(iaxs[callno], IAX_KEYPOPULATED);
04393             break;
04394          }
04395       }
04396    } else 
04397       res = decode_frame(&iaxs[callno]->dcx, fh, f, datalen);
04398    return res;
04399 }

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 6975 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().

06976 {
06977    struct iax2_pkt_buf *pkt_buf, *cur_pkt_buf;
06978    struct ast_iax2_full_hdr *fh, *cur_fh;
06979 
06980    if (!(pkt_buf = ast_calloc(1, sizeof(*pkt_buf) + from_here->buf_len)))
06981       return;
06982 
06983    pkt_buf->len = from_here->buf_len;
06984    memcpy(pkt_buf->buf, from_here->buf, pkt_buf->len);
06985 
06986    fh = (struct ast_iax2_full_hdr *) pkt_buf->buf;
06987    ast_mutex_lock(&to_here->lock);
06988    AST_LIST_TRAVERSE_SAFE_BEGIN(&to_here->full_frames, cur_pkt_buf, entry) {
06989       cur_fh = (struct ast_iax2_full_hdr *) cur_pkt_buf->buf;
06990       if (fh->oseqno < cur_fh->oseqno) {
06991          AST_LIST_INSERT_BEFORE_CURRENT(&to_here->full_frames, pkt_buf, entry);
06992          break;
06993       }
06994    }
06995    AST_LIST_TRAVERSE_SAFE_END
06996 
06997    if (!cur_pkt_buf)
06998       AST_LIST_INSERT_TAIL(&to_here->full_frames, pkt_buf, entry);
06999    
07000    ast_mutex_unlock(&to_here->lock);
07001 }

static void delete_users ( void   )  [static]

Definition at line 9900 of file chan_iax2.c.

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

09901 {
09902    struct iax2_registry *reg;
09903 
09904    ao2_callback(users, 0, user_delme_cb, NULL);
09905 
09906    AST_LIST_LOCK(&registrations);
09907    while ((reg = AST_LIST_REMOVE_HEAD(&registrations, entry))) {
09908       ast_sched_del(sched, reg->expire);
09909       if (reg->callno) {
09910          int callno = reg->callno;
09911          ast_mutex_lock(&iaxsl[callno]);
09912          if (iaxs[callno]) {
09913             iaxs[callno]->reg = NULL;
09914             iax2_destroy(callno);
09915          }
09916          ast_mutex_unlock(&iaxsl[callno]);
09917       }
09918       if (reg->dnsmgr)
09919          ast_dnsmgr_release(reg->dnsmgr);
09920       free(reg);
09921    }
09922    AST_LIST_UNLOCK(&registrations);
09923 
09924    ao2_callback(peers, 0, peer_delme_cb, NULL);
09925 }

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

Definition at line 1806 of file chan_iax2.c.

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

Referenced by reload_firmware().

01807 {
01808    /* Close firmware */
01809    if (cur->fwh) {
01810       munmap((void*)cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
01811    }
01812    close(cur->fd);
01813    free(cur);
01814 }

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

Definition at line 6747 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().

06748 {
06749    unsigned short dpstatus = 0;
06750    struct iax_ie_data ied1;
06751    int mm;
06752 
06753    memset(&ied1, 0, sizeof(ied1));
06754    mm = ast_matchmore_extension(NULL, context, callednum, 1, callerid);
06755    /* Must be started */
06756    if (!strcmp(callednum, ast_parking_ext()) || ast_exists_extension(NULL, context, callednum, 1, callerid)) {
06757       dpstatus = IAX_DPSTATUS_EXISTS;
06758    } else if (ast_canmatch_extension(NULL, context, callednum, 1, callerid)) {
06759       dpstatus = IAX_DPSTATUS_CANEXIST;
06760    } else {
06761       dpstatus = IAX_DPSTATUS_NONEXISTENT;
06762    }
06763    if (ast_ignore_pattern(context, callednum))
06764       dpstatus |= IAX_DPSTATUS_IGNOREPAT;
06765    if (mm)
06766       dpstatus |= IAX_DPSTATUS_MATCHMORE;
06767    if (!skiplock)
06768       ast_mutex_lock(&iaxsl[callno]);
06769    if (iaxs[callno]) {
06770       iax_ie_append_str(&ied1, IAX_IE_CALLED_NUMBER, callednum);
06771       iax_ie_append_short(&ied1, IAX_IE_DPSTATUS, dpstatus);
06772       iax_ie_append_short(&ied1, IAX_IE_REFRESH, iaxdefaultdpcache);
06773       send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_DPREP, 0, ied1.buf, ied1.pos, -1);
06774    }
06775    if (!skiplock)
06776       ast_mutex_unlock(&iaxsl[callno]);
06777 }

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

Definition at line 6779 of file chan_iax2.c.

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

Referenced by spawn_dp_lookup().

06780 {
06781    /* Look up for dpreq */
06782    struct dpreq_data *dpr = data;
06783    dp_lookup(dpr->callno, dpr->context, dpr->callednum, dpr->callerid, 0);
06784    if (dpr->callerid)
06785       free(dpr->callerid);
06786    free(dpr);
06787    return NULL;
06788 }

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

Definition at line 4332 of file chan_iax2.c.

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

Referenced by iax2_send().

04333 {
04334    int padding;
04335    unsigned char *workspace;
04336    workspace = alloca(*datalen + 32);
04337    if (!workspace)
04338       return -1;
04339    if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
04340       struct ast_iax2_full_enc_hdr *efh = (struct ast_iax2_full_enc_hdr *)fh;
04341       if (option_debug && iaxdebug)
04342          ast_log(LOG_DEBUG, "Encoding full frame %d/%d with length %d\n", fh->type, fh->csub, *datalen);
04343       padding = 16 - ((*datalen - sizeof(struct ast_iax2_full_enc_hdr)) % 16);
04344       padding = 16 + (padding & 0xf);
04345       memcpy(workspace, poo, padding);
04346       memcpy(workspace + padding, efh->encdata, *datalen - sizeof(struct ast_iax2_full_enc_hdr));
04347       workspace[15] &= 0xf0;
04348       workspace[15] |= (padding & 0xf);
04349       if (option_debug && iaxdebug)
04350          ast_log(LOG_DEBUG, "Encoding full frame %d/%d with length %d + %d padding (15=%02x)\n", fh->type, fh->csub, *datalen, padding, workspace[15]);
04351       *datalen += padding;
04352       memcpy_encrypt(efh->encdata, workspace, *datalen - sizeof(struct ast_iax2_full_enc_hdr), ecx);
04353       if (*datalen >= 32 + sizeof(struct ast_iax2_full_enc_hdr))
04354          memcpy(poo, workspace + *datalen - 32, 32);
04355    } else {
04356       struct ast_iax2_mini_enc_hdr *efh = (struct ast_iax2_mini_enc_hdr *)fh;
04357       if (option_debug && iaxdebug)
04358          ast_log(LOG_DEBUG, "Encoding mini frame with length %d\n", *datalen);
04359       padding = 16 - ((*datalen - sizeof(struct ast_iax2_mini_enc_hdr)) % 16);
04360       padding = 16 + (padding & 0xf);
04361       memcpy(workspace, poo, padding);
04362       memcpy(workspace + padding, efh->encdata, *datalen - sizeof(struct ast_iax2_mini_enc_hdr));
04363       workspace[15] &= 0xf0;
04364       workspace[15] |= (padding & 0x0f);
04365       *datalen += padding;
04366       memcpy_encrypt(efh->encdata, workspace, *datalen - sizeof(struct ast_iax2_mini_enc_hdr), ecx);
04367       if (*datalen >= 32 + sizeof(struct ast_iax2_mini_enc_hdr))
04368          memcpy(poo, workspace + *datalen - 32, 32);
04369    }
04370    return 0;
04371 }

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

Definition at line 6196 of file chan_iax2.c.

References __expire_registry(), and schedule_action.

Referenced by iax2_prune_realtime(), realtime_peer(), reg_source_db(), and update_registry().

06197 {
06198 #ifdef SCHED_MULTITHREADED
06199    if (schedule_action(__expire_registry, data))
06200 #endif      
06201       __expire_registry(data);
06202    return 0;
06203 }

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

References ARRAY_LEN, ast_calloc, ast_channel_defer_dtmf(), ast_channel_undefer_dtmf(), ast_copy_string(), ast_frfree, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_read(), ast_test_flag, ast_tvcmp(), ast_waitfor_nandfds(), CACHE_FLAG_PENDING, CACHE_FLAG_TIMEOUT, cache_get_callno_locked(), iax2_dpcache::callno, iax2_registry::callno, dpcache, dpcache_lock, chan_iax2_pvt::dpentries, errno, iax2_dpcache::expiry, iax2_dpcache::exten, f, iax2_dpcache::flags, free, iax2_dprequest(), IAX_STATE_STARTED, LOG_WARNING, iax2_dpcache::next, iax2_registry::next, iax2_dpcache::orig, iax2_dpcache::peer, iax2_dpcache::peercontext, timeout, and iax2_dpcache::waiters.

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

10460 {
10461    struct iax2_dpcache *dp, *prev = NULL, *next;
10462    struct timeval tv;
10463    int x;
10464    int com[2];
10465    int timeout;
10466    int old=0;
10467    int outfd;
10468    int abort;
10469    int callno;
10470    struct ast_channel *c;
10471    struct ast_frame *f;
10472    gettimeofday(&tv, NULL);
10473    dp = dpcache;
10474    while(dp) {
10475       next = dp->next;
10476       /* Expire old caches */
10477       if (ast_tvcmp(tv, dp->expiry) > 0) {
10478             /* It's expired, let it disappear */
10479             if (prev)
10480                prev->next = dp->next;
10481             else
10482                dpcache = dp->next;
10483             if (!dp->peer && !(dp->flags & CACHE_FLAG_PENDING) && !dp->callno) {
10484                /* Free memory and go again */
10485                free(dp);
10486             } else {
10487                ast_log(LOG_WARNING, "DP still has peer field or pending or callno (flags = %d, peer = %p callno = %d)\n", dp->flags, dp->peer, dp->callno);
10488             }
10489             dp = next;
10490             continue;
10491       }
10492       /* We found an entry that matches us! */
10493       if (!strcmp(dp->peercontext, data) && !strcmp(dp->exten, exten)) 
10494          break;
10495       prev = dp;
10496       dp = next;
10497    }
10498    if (!dp) {
10499       /* No matching entry.  Create a new one. */
10500       /* First, can we make a callno? */
10501       callno = cache_get_callno_locked(data);
10502       if (callno < 0) {
10503          ast_log(LOG_WARNING, "Unable to generate call for '%s'\n", data);
10504          return NULL;
10505       }
10506       if (!(dp = ast_calloc(1, sizeof(*dp)))) {
10507          ast_mutex_unlock(&iaxsl[callno]);
10508          return NULL;
10509       }
10510       ast_copy_string(dp->peercontext, data, sizeof(dp->peercontext));
10511       ast_copy_string(dp->exten, exten, sizeof(dp->exten));
10512       gettimeofday(&dp->expiry, NULL);
10513       dp->orig = dp->expiry;
10514       /* Expires in 30 mins by default */
10515       dp->expiry.tv_sec += iaxdefaultdpcache;
10516       dp->next = dpcache;
10517       dp->flags = CACHE_FLAG_PENDING;
10518       for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
10519          dp->waiters[x] = -1;
10520       dpcache = dp;
10521       dp->peer = iaxs[callno]->dpentries;
10522       iaxs[callno]->dpentries = dp;
10523       /* Send the request if we're already up */
10524       if (ast_test_flag(&iaxs[callno]->state, IAX_STATE_STARTED))
10525          iax2_dprequest(dp, callno);
10526       ast_mutex_unlock(&iaxsl[callno]);
10527    }
10528    /* By here we must have a dp */
10529    if (dp->flags & CACHE_FLAG_PENDING) {
10530       /* Okay, here it starts to get nasty.  We need a pipe now to wait
10531          for a reply to come back so long as it's pending */
10532       for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++) {
10533          /* Find an empty slot */
10534          if (dp->waiters[x] < 0)
10535             break;
10536       }
10537       if (x >= sizeof(dp->waiters) / sizeof(dp->waiters[0])) {
10538          ast_log(LOG_WARNING, "No more waiter positions available\n");
10539          return NULL;
10540       }
10541       if (pipe(com)) {
10542          ast_log(LOG_WARNING, "Unable to create pipe for comm\n");
10543          return NULL;
10544       }
10545       dp->waiters[x] = com[1];
10546       /* Okay, now we wait */
10547       timeout = iaxdefaulttimeout * 1000;
10548       /* Temporarily unlock */
10549       ast_mutex_unlock(&dpcache_lock);
10550       /* Defer any dtmf */
10551       if (chan)
10552          old = ast_channel_defer_dtmf(chan);
10553       abort = 0;
10554       while(timeout) {
10555          c = ast_waitfor_nandfds(&chan, chan ? 1 : 0, &com[0], 1, NULL, &outfd, &timeout);
10556          if (outfd > -1) {
10557             break;
10558          }
10559          if (c) {
10560             f = ast_read(c);
10561             if (f)
10562                ast_frfree(f);
10563             else {
10564                /* Got hung up on, abort! */
10565                break;
10566                abort = 1;
10567             }
10568          }
10569       }
10570       if (!timeout) {
10571          ast_log(LOG_WARNING, "Timeout waiting for %s exten %s\n", data, exten);
10572       }
10573       ast_mutex_lock(&dpcache_lock);
10574       dp->waiters[x] = -1;
10575       close(com[1]);
10576       close(com[0]);
10577       if (abort) {
10578          /* Don't interpret anything, just abort.  Not sure what th epoint
10579            of undeferring dtmf on a hung up channel is but hey whatever */
10580          if (!old && chan)
10581             ast_channel_undefer_dtmf(chan);
10582          return NULL;
10583       }
10584       if (!(dp->flags & CACHE_FLAG_TIMEOUT)) {
10585          /* Now to do non-independent analysis the results of our wait */
10586          if (dp->flags & CACHE_FLAG_PENDING) {
10587             /* Still pending... It's a timeout.  Wake everybody up.  Consider it no longer
10588                pending.  Don't let it take as long to timeout. */
10589             dp->flags &= ~CACHE_FLAG_PENDING;
10590             dp->flags |= CACHE_FLAG_TIMEOUT;
10591             /* Expire after only 60 seconds now.  This is designed to help reduce backlog in heavily loaded
10592                systems without leaving it unavailable once the server comes back online */
10593             dp->expiry.tv_sec = dp->orig.tv_sec + 60;
10594             for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
10595                if (dp->waiters[x] > -1) {
10596                   if (write(dp->waiters[x], "asdf", 4) < 0) {
10597                      ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
10598                   }
10599                }
10600             }
10601          }
10602       }
10603       /* Our caller will obtain the rest */
10604       if (!old && chan)
10605          ast_channel_undefer_dtmf(chan);
10606    }
10607    return dp;  
10608 }

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

References __find_callno().

Referenced by iax2_poke_peer(), and socket_process().

01706                                                                                                                                     {
01707 
01708    return __find_callno(callno, dcallno, sin, new, sockfd, 0, full_frame);
01709 }

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

References __find_callno().

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

01711                                                                                                                                            {
01712 
01713    return __find_callno(callno, dcallno, sin, new, sockfd, 1, full_frame);
01714 }

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

Definition at line 907 of file chan_iax2.c.

References ast_calloc, ast_cond_init(), AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_mutex_init(), ast_pthread_create, iax2_thread::cond, iax2_thread::ffinfo, free, iax2_process_thread(), IAX_TYPE_DYNAMIC, iaxdynamicthreadcount, iaxdynamicthreadnum, iaxmaxthreadcount, iax2_thread::lock, iax2_thread::ready_for_signal, thread, iax2_thread::threadid, iax2_thread::threadnum, and iax2_thread::type.

Referenced by __schedule_action(), and socket_read().

00908 {
00909    pthread_attr_t attr;
00910    struct iax2_thread *thread = NULL;
00911 
00912    /* Pop the head of the list off */
00913    AST_LIST_LOCK(&idle_list);
00914    thread = AST_LIST_REMOVE_HEAD(&idle_list, list);
00915    AST_LIST_UNLOCK(&idle_list);
00916 
00917    /* If no idle thread is available from the regular list, try dynamic */
00918    if (thread == NULL) {
00919       AST_LIST_LOCK(&dynamic_list);
00920       thread = AST_LIST_REMOVE_HEAD(&dynamic_list, list);
00921       /* Make sure we absolutely have a thread... if not, try to make one if allowed */
00922       if (thread == NULL && iaxmaxthreadcount > iaxdynamicthreadcount) {
00923          /* We need to MAKE a thread! */
00924          if ((thread = ast_calloc(1, sizeof(*thread)))) {
00925             thread->threadnum = iaxdynamicthreadnum++;
00926             thread->type = IAX_TYPE_DYNAMIC;
00927             ast_mutex_init(&thread->lock);
00928             ast_cond_init(&thread->cond, NULL);
00929             pthread_attr_init(&attr);
00930             pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);   
00931             if (ast_pthread_create(&thread->threadid, &attr, iax2_process_thread, thread)) {
00932                free(thread);
00933                thread = NULL;
00934             } else {
00935                /* All went well and the thread is up, so increment our count */
00936                iaxdynamicthreadcount++;
00937                
00938                /* Wait for the thread to be ready before returning it to the caller */
00939                while (!thread->ready_for_signal)
00940                   usleep(1);
00941             }
00942          }
00943       }
00944       AST_LIST_UNLOCK(&dynamic_list);
00945    }
00946 
00947    /* this thread is not processing a full frame (since it is idle),
00948       so ensure that the field for the full frame call number is empty */
00949    if (thread)
00950       memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
00951 
00952    return thread;
00953 }

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

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

01151 {
01152    struct iax2_peer *peer = NULL;
01153    struct iax2_peer tmp_peer = {
01154       .name = name,
01155    };
01156 
01157    peer = ao2_find(peers, &tmp_peer, OBJ_POINTER);
01158 
01159    /* Now go for realtime if applicable */
01160    if(!peer && realtime)
01161       peer = realtime_peer(name, NULL);
01162 
01163    return peer;
01164 }

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

Definition at line 4141 of file chan_iax2.c.

References iax2_trunk_peer::addr, ast_calloc, ast_inet_ntoa(), ast_log(), ast_mutex_init(), ast_mutex_lock(), ast_mutex_unlock(), ast_tvnow(), inaddrcmp(), iax2_trunk_peer::lastsent, iax2_trunk_peer::lock, LOG_DEBUG, iax2_trunk_peer::next, option_debug, iax2_trunk_peer::sockfd, tpeerlock, tpeers, and iax2_trunk_peer::trunkact.

Referenced by iax2_trunk_queue(), and socket_process().

04142 {
04143    struct iax2_trunk_peer *tpeer;
04144    
04145    /* Finds and locks trunk peer */
04146    ast_mutex_lock(&tpeerlock);
04147    for (tpeer = tpeers; tpeer; tpeer = tpeer->next) {
04148       /* We don't lock here because tpeer->addr *never* changes */
04149       if (!inaddrcmp(&tpeer->addr, sin)) {
04150          ast_mutex_lock(&tpeer->lock);
04151          break;
04152       }
04153    }
04154    if (!tpeer) {
04155       if ((tpeer = ast_calloc(1, sizeof(*tpeer)))) {
04156          ast_mutex_init(&tpeer->lock);
04157          tpeer->lastsent = 9999;
04158          memcpy(&tpeer->addr, sin, sizeof(tpeer->addr));
04159          tpeer->trunkact = ast_tvnow();
04160          ast_mutex_lock(&tpeer->lock);
04161          tpeer->next = tpeers;
04162          tpeer->sockfd = fd;
04163          tpeers = tpeer;
04164 #ifdef SO_NO_CHECK
04165          setsockopt(tpeer->sockfd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
04166 #endif
04167          if (option_debug)
04168             ast_log(LOG_DEBUG, "Created trunk peer for '%s:%d'\n", ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port));
04169       }
04170    }
04171    ast_mutex_unlock(&tpeerlock);
04172    return tpeer;
04173 }

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

Definition at line 3957 of file chan_iax2.c.

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

Referenced by socket_process().

03958 {
03959    long ms; /* NOT unsigned */
03960    if (ast_tvzero(iaxs[callno]->rxcore)) {
03961       /* Initialize rxcore time if appropriate */
03962       gettimeofday(&iaxs[callno]->rxcore, NULL);
03963       /* Round to nearest 20ms so traces look pretty */
03964       iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
03965    }
03966    /* Calculate difference between trunk and channel */
03967    ms = ast_tvdiff_ms(*tv, iaxs[callno]->rxcore);
03968    /* Return as the sum of trunk time and the difference between trunk and real time */
03969    return ms + ts;
03970 }

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

Definition at line 9076 of file chan_iax2.c.

References free, and iax2_context::next.

Referenced by build_user(), and user_destructor().

09077 {
09078    struct iax2_context *conl;
09079    while(con) {
09080       conl = con;
09081       con = con->next;
09082       free(conl);
09083    }
09084 }

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

Todo:
: will be removed after the 1.4 relese

Definition at line 10732 of file chan_iax2.c.

References iax2_peer::addr, iax2_registry::addr, ast_codec_pref_index(), ast_copy_string(), ast_getformatname(), ast_getformatname_multiple(), ast_inet_ntoa(), ast_strdupa, ast_test_flag, iax2_registry::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.

10733 {
10734    struct iax2_peer *peer;
10735    char *peername, *colname;
10736 
10737    peername = ast_strdupa(data);
10738 
10739    /* if our channel, return the IP address of the endpoint of current channel */
10740    if (!strcmp(peername,"CURRENTCHANNEL")) {
10741            unsigned short callno;
10742       if (chan->tech != &iax2_tech)
10743          return -1;
10744       callno = PTR_TO_CALLNO(chan->tech_pvt);   
10745       ast_copy_string(buf, iaxs[callno]->addr.sin_addr.s_addr ? ast_inet_ntoa(iaxs[callno]->addr.sin_addr) : "", len);
10746       return 0;
10747    }
10748 
10749    if ((colname = strchr(peername, ':'))) /*! \todo : will be removed after the 1.4 relese */
10750       *colname++ = '\0';
10751    else if ((colname = strchr(peername, '|')))
10752       *colname++ = '\0';
10753    else
10754       colname = "ip";
10755 
10756    if (!(peer = find_peer(peername, 1)))
10757       return -1;
10758 
10759    if (!strcasecmp(colname, "ip")) {
10760       ast_copy_string(buf, peer->addr.sin_addr.s_addr ? ast_inet_ntoa(peer->addr.sin_addr) : "", len);
10761    } else  if (!strcasecmp(colname, "status")) {
10762       peer_status(peer, buf, len); 
10763    } else  if (!strcasecmp(colname, "mailbox")) {
10764       ast_copy_string(buf, peer->mailbox, len);
10765    } else  if (!strcasecmp(colname, "context")) {
10766       ast_copy_string(buf, peer->context, len);
10767    } else  if (!strcasecmp(colname, "expire")) {
10768       snprintf(buf, len, "%d", peer->expire);
10769    } else  if (!strcasecmp(colname, "dynamic")) {
10770       ast_copy_string(buf, (ast_test_flag(peer, IAX_DYNAMIC) ? "yes" : "no"), len);
10771    } else  if (!strcasecmp(colname, "callerid_name")) {
10772       ast_copy_string(buf, peer->cid_name, len);
10773    } else  if (!strcasecmp(colname, "callerid_num")) {
10774       ast_copy_string(buf, peer->cid_num, len);
10775    } else  if (!strcasecmp(colname, "codecs")) {
10776       ast_getformatname_multiple(buf, len -1, peer->capability);
10777    } else  if (!strncasecmp(colname, "codec[", 6)) {
10778       char *codecnum, *ptr;
10779       int index = 0, codec = 0;
10780       
10781       codecnum = strchr(colname, '[');
10782       *codecnum = '\0';
10783       codecnum++;
10784       if ((ptr = strchr(codecnum, ']'))) {
10785          *ptr = '\0';
10786       }
10787       index = atoi(codecnum);
10788       if((codec = ast_codec_pref_index(&peer->prefs, index))) {
10789          ast_copy_string(buf, ast_getformatname(codec), len);
10790       } else {
10791          buf[0] = '\0';
10792       }
10793    } else {
10794       buf[0] = '\0';
10795    }
10796 
10797    peer_unref(peer);
10798 
10799    return 0;
10800 }

static int get_auth_methods ( char *  value  )  [static]

Definition at line 9300 of file chan_iax2.c.

References IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, and IAX_AUTH_RSA.

Referenced by build_peer(), and build_user().

09301 {
09302    int methods = 0;
09303    if (strstr(value, "rsa"))
09304       methods |= IAX_AUTH_RSA;
09305    if (strstr(value, "md5"))
09306       methods |= IAX_AUTH_MD5;
09307    if (strstr(value, "plaintext"))
09308       methods |= IAX_AUTH_PLAINTEXT;
09309    return methods;
09310 }

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

Definition at line 1027 of file chan_iax2.c.

References ast_true(), and IAX_ENCRYPT_AES128.

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

01028 {
01029    int e;
01030    if (!strcasecmp(s, "aes128"))
01031       e = IAX_ENCRYPT_AES128;
01032    else if (ast_true(s))
01033       e = IAX_ENCRYPT_AES128;
01034    else
01035       e = 0;
01036    return e;
01037 }

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

Definition at line 2706 of file chan_iax2.c.

References __get_from_jb(), and schedule_action.

Referenced by update_jbsched().

02707 {
02708 #ifdef SCHED_MULTITHREADED
02709    if (schedule_action(__get_from_jb, data))
02710 #endif      
02711       __get_from_jb(data);
02712    return 0;
02713 }

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

Handle any deferred full frames for this thread.

Definition at line 6945 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().

06946 {
06947    struct iax2_pkt_buf *pkt_buf;
06948 
06949    ast_mutex_lock(&thread->lock);
06950 
06951    while ((pkt_buf = AST_LIST_REMOVE_HEAD(&thread->full_frames, entry))) {
06952       ast_mutex_unlock(&thread->lock);
06953 
06954       thread->buf = pkt_buf->buf;
06955       thread->buf_len = pkt_buf->len;
06956       thread->buf_size = pkt_buf->len + 1;
06957       
06958       socket_process(thread);
06959 
06960       thread->buf = NULL;
06961       ast_free(pkt_buf);
06962 
06963       ast_mutex_lock(&thread->lock);
06964    }
06965 
06966    ast_mutex_unlock(&thread->lock);
06967 }

static int handle_error ( void   )  [static]

Definition at line 2094 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().

02095 {
02096    /* XXX Ideally we should figure out why an error occured and then abort those
02097       rather than continuing to try.  Unfortunately, the published interface does
02098       not seem to work XXX */
02099 #if 0
02100    struct sockaddr_in *sin;
02101    int res;
02102    struct msghdr m;
02103    struct sock_extended_err e;
02104    m.msg_name = NULL;
02105    m.msg_namelen = 0;
02106    m.msg_iov = NULL;
02107    m.msg_control = &e;
02108    m.msg_controllen = sizeof(e);
02109    m.msg_flags = 0;
02110    res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
02111    if (res < 0)
02112       ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
02113    else {
02114       if (m.msg_controllen) {
02115          sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
02116          if (sin) 
02117             ast_log(LOG_WARNING, "Receive error from %s\n", ast_inet_ntoa(sin->sin_addr));
02118          else
02119             ast_log(LOG_WARNING, "No address detected??\n");
02120       } else {
02121          ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
02122       }
02123    }
02124 #endif
02125    return 0;
02126 }

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

Acknowledgment received for OUR registration.

Definition at line 6017 of file chan_iax2.c.

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

Referenced by socket_process().

06018 {
06019    struct iax2_registry *reg;
06020    /* Start pessimistic */
06021    char peer[256] = "";
06022    char msgstatus[60];
06023    int refresh = 60;
06024    char ourip[256] = "<Unspecified>";
06025    struct sockaddr_in oldus;
06026    struct sockaddr_in us;
06027    int oldmsgs;
06028 
06029    memset(&us, 0, sizeof(us));
06030    if (ies->apparent_addr)
06031       bcopy(ies->apparent_addr, &us, sizeof(us));
06032    if (ies->username)
06033       ast_copy_string(peer, ies->username, sizeof(peer));
06034    if (ies->refresh)
06035       refresh = ies->refresh;
06036    if (ies->calling_number) {
06037       /* We don't do anything with it really, but maybe we should */
06038    }
06039    reg = iaxs[callno]->reg;
06040    if (!reg) {
06041       ast_log(LOG_WARNING, "Registry acknowledge on unknown registry '%s'\n", peer);
06042       return -1;
06043    }
06044    memcpy(&oldus, &reg->us, sizeof(oldus));
06045    oldmsgs = reg->messages;
06046    if (inaddrcmp(&reg->addr, sin)) {
06047       ast_log(LOG_WARNING, "Received unsolicited registry ack from '%s'\n", ast_inet_ntoa(sin->sin_addr));
06048       return -1;
06049    }
06050    memcpy(&reg->us, &us, sizeof(reg->us));
06051    if (ies->msgcount >= 0)
06052       reg->messages = ies->msgcount & 0xffff;      /* only low 16 bits are used in the transmission of the IE */
06053    /* always refresh the registration at the interval requested by the server
06054       we are registering to
06055    */
06056    reg->refresh = refresh;
06057    AST_SCHED_DEL(sched, reg->expire);
06058    reg->expire = iax2_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
06059    if (inaddrcmp(&oldus, &reg->us) || (reg->messages != oldmsgs)) {
06060       if (option_verbose > 2) {
06061          if (reg->messages > 255)
06062             snprintf(msgstatus, sizeof(msgstatus), " with %d new and %d old messages waiting", reg->messages & 0xff, reg->messages >> 8);
06063          else if (reg->messages > 1)
06064             snprintf(msgstatus, sizeof(msgstatus), " with %d new messages waiting\n", reg->messages);
06065          else if (reg->messages > 0)
06066             snprintf(msgstatus, sizeof(msgstatus), " with 1 new message waiting\n");
06067          else
06068             snprintf(msgstatus, sizeof(msgstatus), " with no messages waiting\n");
06069          snprintf(ourip, sizeof(ourip), "%s:%d", ast_inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port));
06070          ast_verbose(VERBOSE_PREFIX_3 "Registered IAX2 to '%s', who sees us as %s%s\n", ast_inet_ntoa(sin->sin_addr), ourip, msgstatus);
06071       }
06072       manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: IAX2\r\nDomain: %s\r\nStatus: Registered\r\n", ast_inet_ntoa(sin->sin_addr));
06073    }
06074    reg->regstate = REG_STATE_REGISTERED;
06075    return 0;
06076 }

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

Definition at line 3763 of file chan_iax2.c.

References AST_CONTROL_ANSWER, AST_FRAME_CONTROL, ast_log(), iax2_registry::callno, LOG_DEBUG, option_debug, PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

03764 {
03765    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
03766    if (option_debug)
03767       ast_log(LOG_DEBUG, "Answering IAX2 call\n");
03768    return send_command_locked(callno, AST_FRAME_CONTROL, AST_CONTROL_ANSWER, 0, NULL, 0, -1);
03769 }

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 3609 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_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_tvzero(), ast_verbose(), ast_waitfor_n(), ast_write(), chan_iax2_pvt::bridgecallno, f, ast_frame::frametype, iax2_start_transfer(), IAX_LINGER_TIMEOUT, IAX_NOTRANSFER, IAX_TRANSFERMEDIA, lock_both(), LOG_WARNING, ast_channel::nativeformats, option_verbose, PTR_TO_CALLNO, ast_channel::tech, ast_channel::tech_pvt, TRANSFER_RELEASED, tv, unlock_both(), and VERBOSE_PREFIX_3.

03610 {
03611    struct ast_channel *cs[3];
03612    struct ast_channel *who, *other;
03613    int to = -1;
03614    int res = -1;
03615    int transferstarted=0;
03616    struct ast_frame *f;
03617    unsigned short callno0 = PTR_TO_CALLNO(c0->tech_pvt);
03618    unsigned short callno1 = PTR_TO_CALLNO(c1->tech_pvt);
03619    struct timeval waittimer = {0, 0}, tv;
03620 
03621    lock_both(callno0, callno1);
03622    if (!iaxs[callno0] || !iaxs[callno1]) {
03623       unlock_both(callno0, callno1);
03624       return AST_BRIDGE_FAILED;
03625    }
03626    /* Put them in native bridge mode */
03627    if (!flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1)) {
03628       iaxs[callno0]->bridgecallno = callno1;
03629       iaxs[callno1]->bridgecallno = callno0;
03630    }
03631    /* If the bridge got retried, don't queue up more packets - the transfer request will be retransmitted as necessary */
03632    if (iaxs[callno0]->transferring && iaxs[callno1]->transferring) {
03633       transferstarted = 1;
03634    }
03635    unlock_both(callno0, callno1);
03636 
03637    /* If not, try to bridge until we can execute a transfer, if we can */
03638    cs[0] = c0;
03639    cs[1] = c1;
03640    for (/* ever */;;) {
03641       /* Check in case we got masqueraded into */
03642       if ((c0->tech != &iax2_tech) || (c1->tech != &iax2_tech)) {
03643          if (option_verbose > 2)
03644             ast_verbose(VERBOSE_PREFIX_3 "Can't masquerade, we're different...\n");
03645          /* Remove from native mode */
03646          if (c0->tech == &iax2_tech) {
03647             ast_mutex_lock(&iaxsl[callno0]);
03648             iaxs[callno0]->bridgecallno = 0;
03649             ast_mutex_unlock(&iaxsl[callno0]);
03650          }
03651          if (c1->tech == &iax2_tech) {
03652             ast_mutex_lock(&iaxsl[callno1]);
03653             iaxs[callno1]->bridgecallno = 0;
03654             ast_mutex_unlock(&iaxsl[callno1]);
03655          }
03656          return AST_BRIDGE_FAILED_NOWARN;
03657       }
03658       if (c0->nativeformats != c1->nativeformats) {
03659          if (option_verbose > 2) {
03660             char buf0[255];
03661             char buf1[255];
03662             ast_getformatname_multiple(buf0, sizeof(buf0) -1, c0->nativeformats);
03663             ast_getformatname_multiple(buf1, sizeof(buf1) -1, c1->nativeformats);
03664             ast_verbose(VERBOSE_PREFIX_3 "Operating with different codecs %d[%s] %d[%s] , can't native bridge...\n", c0->nativeformats, buf0, c1->nativeformats, buf1);
03665          }
03666          /* Remove from native mode */
03667          lock_both(callno0, callno1);
03668          if (iaxs[callno0])
03669             iaxs[callno0]->bridgecallno = 0;
03670          if (iaxs[callno1])
03671             iaxs[callno1]->bridgecallno = 0;
03672          unlock_both(callno0, callno1);
03673          return AST_BRIDGE_FAILED_NOWARN;
03674       }
03675       /* check if transfered and if we really want native bridging */
03676       if (!transferstarted && !ast_test_flag(iaxs[callno0], IAX_NOTRANSFER) && !ast_test_flag(iaxs[callno1], IAX_NOTRANSFER)) {
03677          /* Try the transfer */
03678          if (iax2_start_transfer(callno0, callno1, (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1)) ||
03679                      ast_test_flag(iaxs[callno0], IAX_TRANSFERMEDIA) | ast_test_flag(iaxs[callno1], IAX_TRANSFERMEDIA)))
03680             ast_log(LOG_WARNING, "Unable to start the transfer\n");
03681          transferstarted = 1;
03682       }
03683       if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
03684          /* Call has been transferred.  We're no longer involved */
03685          gettimeofday(&tv, NULL);
03686          if (ast_tvzero(waittimer)) {
03687             waittimer = tv;
03688          } else if (tv.tv_sec - waittimer.tv_sec > IAX_LINGER_TIMEOUT) {
03689             c0->_softhangup |= AST_SOFTHANGUP_DEV;
03690             c1->_softhangup |= AST_SOFTHANGUP_DEV;
03691             *fo = NULL;
03692             *rc = c0;
03693             res = AST_BRIDGE_COMPLETE;
03694             break;
03695          }
03696       }
03697       to = 1000;
03698       who = ast_waitfor_n(cs, 2, &to);
03699       if (timeoutms > -1) {
03700          timeoutms -= (1000 - to);
03701          if (timeoutms < 0)
03702             timeoutms = 0;
03703       }
03704       if (!who) {
03705          if (!timeoutms) {
03706             res = AST_BRIDGE_RETRY;
03707             break;
03708          }
03709          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
03710             res = AST_BRIDGE_FAILED;
03711             break;
03712          }
03713          continue;
03714       }
03715       f = ast_read(who);
03716       if (!f) {
03717          *fo = NULL;
03718          *rc = who;
03719          res = AST_BRIDGE_COMPLETE;
03720          break;
03721       }
03722       if ((f->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
03723          *fo = f;
03724          *rc = who;
03725          res =  AST_BRIDGE_COMPLETE;
03726          break;
03727       }
03728       other = (who == c0) ? c1 : c0;  /* the 'other' channel */
03729       if ((f->frametype == AST_FRAME_VOICE) ||
03730           (f->frametype == AST_FRAME_TEXT) ||
03731           (f->frametype == AST_FRAME_VIDEO) || 
03732           (f->frametype == AST_FRAME_IMAGE) ||
03733           (f->frametype == AST_FRAME_DTMF)) {
03734          /* monitored dtmf take out of the bridge.
03735           * check if we monitor the specific source.
03736           */
03737          int monitored_source = (who == c0) ? AST_BRIDGE_DTMF_CHANNEL_0 : AST_BRIDGE_DTMF_CHANNEL_1;
03738          if (f->frametype == AST_FRAME_DTMF && (flags & monitored_source)) {
03739             *rc = who;
03740             *fo = f;
03741             res = AST_BRIDGE_COMPLETE;
03742             /* Remove from native mode */
03743             break;
03744          }
03745          /* everything else goes to the other side */
03746          ast_write(other, f);
03747       }
03748       ast_frfree(f);
03749       /* Swap who gets priority */
03750       cs[2] = cs[0];
03751       cs[0] = cs[1];
03752       cs[1] = cs[2];
03753    }
03754    lock_both(callno0, callno1);
03755    if(iaxs[callno0])
03756       iaxs[callno0]->bridgecallno = 0;
03757    if(iaxs[callno1])
03758       iaxs[callno1]->bridgecallno = 0;
03759    unlock_both(callno0, callno1);
03760    return res;
03761 }

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

Definition at line 3306 of file chan_iax2.c.

References ast_channel::_state, create_addr_info::adsi, chan_iax2_pvt::adsi, ast_channel::adsicpe, ast_copy_string(), AST_FRAME_IAX, 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, auto_congest(), iax_ie_data::buf, iax2_registry::callno, 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, create_addr_info::context, ast_channel::context, parsed_dial_string::context, create_addr(), chan_iax2_pvt::encmethods, create_addr_info::encmethods, parsed_dial_string::exten, iax2_datetime(), iax2_sched_add(), IAX_COMMAND_NEW, IAX_IE_ADSICPE, iax_ie_append(), iax_ie_append_byte(), iax_ie_append_int(), 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_RDNIS, IAX_IE_USERNAME, IAX_IE_VERSION, 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::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, 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, iax2_registry::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, and parsed_dial_string::username.

03307 {
03308    struct sockaddr_in sin;
03309    char *l=NULL, *n=NULL, *tmpstr;
03310    struct iax_ie_data ied;
03311    char *defaultrdest = "s";
03312    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
03313    struct parsed_dial_string pds;
03314    struct create_addr_info cai;
03315 
03316    if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
03317       ast_log(LOG_WARNING, "Channel is already in use (%s)?\n", c->name);
03318       return -1;
03319    }
03320 
03321    memset(&cai, 0, sizeof(cai));
03322    cai.encmethods = iax2_encryption;
03323 
03324    memset(&pds, 0, sizeof(pds));
03325    tmpstr = ast_strdupa(dest);
03326    parse_dial_string(tmpstr, &pds);
03327 
03328    if (ast_strlen_zero(pds.peer)) {
03329       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", dest);
03330       return -1;
03331    }
03332 
03333    if (!pds.exten) {
03334       pds.exten = defaultrdest;
03335    }
03336 
03337    if (create_addr(pds.peer, c, &sin, &cai)) {
03338       ast_log(LOG_WARNING, "No address associated with '%s'\n", pds.peer);
03339       return -1;
03340    }
03341 
03342    if (!pds.username && !ast_strlen_zero(cai.username))
03343       pds.username = cai.username;
03344    if (!pds.password && !ast_strlen_zero(cai.secret))
03345       pds.password = cai.secret;
03346    if (!pds.key && !ast_strlen_zero(cai.outkey))
03347       pds.key = cai.outkey;
03348    if (!pds.context && !ast_strlen_zero(cai.peercontext))
03349       pds.context = cai.peercontext;
03350 
03351    /* Keep track of the context for outgoing calls too */
03352    ast_copy_string(c->context, cai.context, sizeof(c->context));
03353 
03354    if (pds.port)
03355       sin.sin_port = htons(atoi(pds.port));
03356 
03357    l = c->cid.cid_num;
03358    n = c->cid.cid_name;
03359 
03360    /* Now build request */ 
03361    memset(&ied, 0, sizeof(ied));
03362 
03363    /* On new call, first IE MUST be IAX version of caller */
03364    iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
03365    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, pds.exten);
03366    if (pds.options && strchr(pds.options, 'a')) {
03367       /* Request auto answer */
03368       iax_ie_append(&ied, IAX_IE_AUTOANSWER);
03369    }
03370 
03371    iax_ie_append_str(&ied, IAX_IE_CODEC_PREFS, cai.prefs);
03372 
03373    if (l) {
03374       iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, l);
03375       iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES, c->cid.cid_pres);
03376    } else {
03377       if (n)
03378          iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES, c->cid.cid_pres);
03379       else
03380          iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES, AST_PRES_NUMBER_NOT_AVAILABLE);
03381    }
03382 
03383    iax_ie_append_byte(&ied, IAX_IE_CALLINGTON, c->cid.cid_ton);
03384    iax_ie_append_short(&ied, IAX_IE_CALLINGTNS, c->cid.cid_tns);
03385 
03386    if (n)
03387       iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, n);
03388    if (ast_test_flag(iaxs[callno], IAX_SENDANI) && c->cid.cid_ani)
03389       iax_ie_append_str(&ied, IAX_IE_CALLING_ANI, c->cid.cid_ani);
03390 
03391    if (!ast_strlen_zero(c->language))
03392       iax_ie_append_str(&ied, IAX_IE_LANGUAGE, c->language);
03393    if (!ast_strlen_zero(c->cid.cid_dnid))
03394       iax_ie_append_str(&ied, IAX_IE_DNID, c->cid.cid_dnid);
03395    if (!ast_strlen_zero(c->cid.cid_rdnis))
03396       iax_ie_append_str(&ied, IAX_IE_RDNIS, c->cid.cid_rdnis);
03397 
03398    if (pds.context)
03399       iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, pds.context);
03400 
03401    if (pds.username)
03402       iax_ie_append_str(&ied, IAX_IE_USERNAME, pds.username);
03403 
03404    if (cai.encmethods)
03405       iax_ie_append_short(&ied, IAX_IE_ENCRYPTION, cai.encmethods);
03406 
03407    ast_mutex_lock(&iaxsl[callno]);
03408 
03409    if (!ast_strlen_zero(c->context))
03410       ast_string_field_set(iaxs[callno], context, c->context);
03411 
03412    if (pds.username)
03413       ast_string_field_set(iaxs[callno], username, pds.username);
03414 
03415    iaxs[callno]->encmethods = cai.encmethods;
03416 
03417    iaxs[callno]->adsi = cai.adsi;
03418    
03419    ast_string_field_set(iaxs[callno], mohinterpret, cai.mohinterpret);
03420    ast_string_field_set(iaxs[callno], mohsuggest, cai.mohsuggest);
03421 
03422    if (pds.key)
03423       ast_string_field_set(iaxs[callno], outkey, pds.key);
03424    if (pds.password)
03425       ast_string_field_set(iaxs[callno], secret, pds.password);
03426 
03427    iax_ie_append_int(&ied, IAX_IE_FORMAT, c->nativeformats);
03428    iax_ie_append_int(&ied, IAX_IE_CAPABILITY, iaxs[callno]->capability);
03429    iax_ie_append_short(&ied, IAX_IE_ADSICPE, c->adsicpe);
03430    iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime(cai.timezone));
03431 
03432    if (iaxs[callno]->maxtime) {
03433       /* Initialize pingtime and auto-congest time */
03434       iaxs[callno]->pingtime = iaxs[callno]->maxtime / 2;
03435       iaxs[callno]->initid = iax2_sched_add(sched, iaxs[callno]->maxtime * 2, auto_congest, CALLNO_TO_PTR(callno));
03436    } else if (autokill) {
03437       iaxs[callno]->pingtime = autokill / 2;
03438       iaxs[callno]->initid = iax2_sched_add(sched, autokill * 2, auto_congest, CALLNO_TO_PTR(callno));
03439    }
03440 
03441    /* send the command using the appropriate socket for this peer */
03442    iaxs[callno]->sockfd = cai.sockfd;
03443 
03444    /* Transmit the string in a "NEW" request */
03445    send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
03446 
03447    ast_mutex_unlock(&iaxsl[callno]);
03448    ast_setstate(c, AST_STATE_RINGING);
03449    
03450    return 0;
03451 }

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

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), CACHE_FLAG_CANEXIST, dpcache_lock, find_cache(), iax2_dpcache::flags, LOG_NOTICE, and LOG_WARNING.

10635 {
10636    int res = 0;
10637    struct iax2_dpcache *dp;
10638 #if 0
10639    ast_log(LOG_NOTICE, "iax2_canmatch: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
10640 #endif
10641    if ((priority != 1) && (priority != 2))
10642       return 0;
10643    ast_mutex_lock(&dpcache_lock);
10644    dp = find_cache(chan, data, context, exten, priority);
10645    if (dp) {
10646       if (dp->flags & CACHE_FLAG_CANEXIST)
10647          res= 1;
10648    }
10649    ast_mutex_unlock(&dpcache_lock);
10650    if (!dp) {
10651       ast_log(LOG_WARNING, "Unable to make DP cache\n");
10652    }
10653    return res;
10654 }

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

Definition at line 3209 of file chan_iax2.c.

References ast_localtime(), ast_strlen_zero(), and t.

Referenced by iax2_call(), and update_registry().

03210 {
03211    time_t t;
03212    struct tm tm;
03213    unsigned int tmp;
03214    time(&t);
03215    if (!ast_strlen_zero(tz))
03216       ast_localtime(&t, &tm, tz);
03217    else
03218       ast_localtime(&t, &tm, NULL);
03219    tmp  = (tm.tm_sec >> 1) & 0x1f;        /* 5 bits of seconds */
03220    tmp |= (tm.tm_min & 0x3f) << 5;        /* 6 bits of minutes */
03221    tmp |= (tm.tm_hour & 0x1f) << 11;      /* 5 bits of hours */
03222    tmp |= (tm.tm_mday & 0x1f) << 16;      /* 5 bits of day of month */
03223    tmp |= ((tm.tm_mon + 1) & 0xf) << 21;     /* 4 bits of month */
03224    tmp |= ((tm.tm_year - 100) & 0x7f) << 25; /* 7 bits of year */
03225    return tmp;
03226 }

static void iax2_destroy ( int  callno  )  [static]

Definition at line 1290 of file chan_iax2.c.

References ao2_ref(), ast_log(), ast_mutex_trylock(), ast_mutex_unlock(), ast_queue_hangup(), AST_SCHED_DEL_SPINLOCK, DEADLOCK_AVOIDANCE, lastused, ast_channel::lock, LOG_DEBUG, option_debug, chan_iax2_pvt::owner, chan_iax2_pvt::peercallno, remove_by_peercallno(), 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().

01291 {
01292    struct chan_iax2_pvt *pvt;
01293    struct ast_channel *owner;
01294 
01295 retry:
01296    pvt = iaxs[callno];
01297    gettimeofday(&lastused[callno], NULL);
01298    
01299    owner = pvt ? pvt->owner : NULL;
01300 
01301    if (owner) {
01302       if (ast_mutex_trylock(&owner->lock)) {
01303          if (option_debug > 2)
01304             ast_log(LOG_DEBUG, "Avoiding IAX destroy deadlock\n");
01305          DEADLOCK_AVOIDANCE(&iaxsl[callno]);
01306          goto retry;
01307       }
01308    }
01309    if (!owner && iaxs[callno]) {
01310       AST_SCHED_DEL_SPINLOCK(sched, iaxs[callno]->lagid, &iaxsl[callno]);
01311       AST_SCHED_DEL_SPINLOCK(sched, iaxs[callno]->pingid, &iaxsl[callno]);
01312       iaxs[callno] = NULL;
01313    }
01314 
01315    if (pvt) {
01316       if (!owner) {
01317          pvt->owner = NULL;
01318       } else {
01319          /* If there's an owner, prod it to give up */
01320          /* It is ok to use ast_queue_hangup() here instead of iax2_queue_hangup()
01321           * because we already hold the owner channel lock. */
01322          ast_queue_hangup(owner);
01323       }
01324 
01325       if (pvt->peercallno) {
01326          remove_by_peercallno(pvt);
01327       }
01328 
01329       if (!owner) {
01330          ao2_ref(pvt, -1);
01331          pvt = NULL;
01332       }
01333    }
01334 
01335    if (owner) {
01336       ast_mutex_unlock(&owner->lock);
01337    }
01338 
01339    if (callno & 0x4000) {
01340       update_max_trunk();
01341    }
01342 }

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

Definition at line 1220 of file chan_iax2.c.

References ao2_find(), ast_atomic_fetchadd_int(), ast_clear_flag, AST_SCHED_DEL, ast_test_flag, chan_iax2_pvt::authid, chan_iax2_pvt::autoid, iax2_user::curauthreq, IAX_MAXAUTHREQ, chan_iax2_pvt::initid, chan_iax2_pvt::jbid, chan_iax2_pvt::lagid, iax2_user::name, chan_iax2_pvt::pingid, user_unref(), and chan_iax2_pvt::username.

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

01221 {
01222    /* Decrement AUTHREQ count if needed */
01223    if (ast_test_flag(pvt, IAX_MAXAUTHREQ)) {
01224       struct iax2_user *user;
01225       struct iax2_user tmp_user = {
01226          .name = pvt->username,
01227       };
01228 
01229       user = ao2_find(users, &tmp_user, OBJ_POINTER);
01230       if (user) {
01231          ast_atomic_fetchadd_int(&user->curauthreq, -1);
01232          user = user_unref(user);       
01233       }
01234 
01235       ast_clear_flag(pvt, IAX_MAXAUTHREQ);
01236    }
01237 
01238    /* No more pings or lagrq's */
01239    AST_SCHED_DEL(sched, pvt->pingid);
01240    AST_SCHED_DEL(sched, pvt->lagid);
01241    AST_SCHED_DEL(sched, pvt->autoid);
01242    AST_SCHED_DEL(sched, pvt->authid);
01243    AST_SCHED_DEL(sched, pvt->initid);
01244    AST_SCHED_DEL(sched, pvt->jbid);
01245 }

static int iax2_devicestate ( void *  data  )  [static]

Part of the device state notification system ---.

Definition at line 10825 of file chan_iax2.c.

References iax2_peer::addr, 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_DEBUG, LOG_WARNING, iax2_peer::maxms, option_debug, parse_dial_string(), parsed_dial_string::peer, and peer_unref().

10826 {
10827    struct parsed_dial_string pds;
10828    char *tmp = ast_strdupa(data);
10829    struct iax2_peer *p;
10830    int res = AST_DEVICE_INVALID;
10831 
10832    memset(&pds, 0, sizeof(pds));
10833    parse_dial_string(tmp, &pds);
10834 
10835    if (ast_strlen_zero(pds.peer)) {
10836       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", (char *) data);
10837       return res;
10838    }
10839    
10840    if (option_debug > 2)
10841       ast_log(LOG_DEBUG, "Checking device state for device %s\n", pds.peer);
10842 
10843    /* SLD: FIXME: second call to find_peer during registration */
10844    if (!(p = find_peer(pds.peer, 1)))
10845       return res;
10846 
10847    res = AST_DEVICE_UNAVAILABLE;
10848    if (option_debug > 2) 
10849       ast_log(LOG_DEBUG, "iax2_devicestate: Found peer. What's device state of %s? addr=%d, defaddr=%d maxms=%d, lastms=%d\n",
10850          pds.peer, p->addr.sin_addr.s_addr, p->defaddr.sin_addr.s_addr, p->maxms, p->lastms);
10851    
10852    if ((p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) &&
10853        (!p->maxms || ((p->lastms > -1) && (p->historicms <= p->maxms)))) {
10854       /* Peer is registered, or have default IP address
10855          and a valid registration */
10856       if (p->historicms == 0 || p->historicms <= p->maxms)
10857          /* let the core figure out whether it is in use or not */
10858          res = AST_DEVICE_UNKNOWN;  
10859    }
10860 
10861    peer_unref(p);
10862 
10863    return res;
10864 }

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

Definition at line 2825 of file chan_iax2.c.

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

02826 {
02827    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_DTMF_BEGIN, digit, 0, NULL, 0, -1);
02828 }

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

Definition at line 2830 of file chan_iax2.c.

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

02831 {
02832    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_DTMF_END, digit, 0, NULL, 0, -1);
02833 }

static int iax2_do_debug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 5063 of file chan_iax2.c.

References ast_cli(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

05064 {
05065    if (argc < 2 || argc > 3)
05066       return RESULT_SHOWUSAGE;
05067    iaxdebug = 1;
05068    ast_cli(fd, "IAX2 Debugging Enabled\n");
05069    return RESULT_SUCCESS;
05070 }

static int iax2_do_jb_debug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 5081 of file chan_iax2.c.

References ast_cli(), jb_debug_output(), jb_error_output(), jb_setoutput(), jb_warning_output(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

05082 {
05083    if (argc < 3 || argc > 4)
05084       return RESULT_SHOWUSAGE;
05085    jb_setoutput(jb_error_output, jb_warning_output, jb_debug_output);
05086    ast_cli(fd, "IAX2 Jitterbuffer Debugging Enabled\n");
05087    return RESULT_SUCCESS;
05088 }

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

Definition at line 8794 of file chan_iax2.c.

References iax2_registry::addr, ast_dnsmgr_changed(), ast_dnsmgr_refresh(), AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_SCHED_DEL, iax_ie_data::buf, iax2_registry::callno, iax2_registry::dnsmgr, iax2_registry::expire, find_callno_locked(), iax2_destroy(), iax2_do_register_s(), iax2_sched_add(), IAX_COMMAND_REGREQ, iax_ie_append_short(), iax_ie_append_str(), IAX_IE_REFRESH, IAX_IE_USERNAME, LOG_DEBUG, LOG_WARNING, NEW_FORCE, option_debug, 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().

08795 {
08796    struct iax_ie_data ied;
08797    if (option_debug && iaxdebug)
08798       ast_log(LOG_DEBUG, "Sending registration request for '%s'\n", reg->username);
08799 
08800    if (reg->dnsmgr && 
08801        ((reg->regstate == REG_STATE_TIMEOUT) || !reg->addr.sin_addr.s_addr)) {
08802       /* Maybe the IP has changed, force DNS refresh */
08803       ast_dnsmgr_refresh(reg->dnsmgr);
08804    }
08805    
08806    /*
08807     * if IP has Changed, free allocated call to create a new one with new IP
08808     * call has the pointer to IP and must be updated to the new one
08809     */
08810    if (reg->dnsmgr && ast_dnsmgr_changed(reg->dnsmgr) && (reg->callno > 0)) {
08811       int callno = reg->callno;
08812       ast_mutex_lock(&iaxsl[callno]);
08813       iax2_destroy(callno);
08814       ast_mutex_unlock(&iaxsl[callno]);
08815       reg->callno = 0;
08816    }
08817    if (!reg->addr.sin_addr.s_addr) {
08818       if (option_debug && iaxdebug)
08819          ast_log(LOG_DEBUG, "Unable to send registration request for '%s' without IP address\n", reg->username);
08820       /* Setup the next registration attempt */
08821       AST_SCHED_DEL(sched, reg->expire);
08822       reg->expire  = iax2_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
08823       return -1;
08824    }
08825 
08826    if (!reg->callno) {
08827       if (option_debug)
08828          ast_log(LOG_DEBUG, "Allocate call number\n");
08829       reg->callno = find_callno_locked(0, 0, &reg->addr, NEW_FORCE, defaultsockfd, 0);
08830       if (reg->callno < 1) {
08831          ast_log(LOG_WARNING, "Unable to create call for registration\n");
08832          return -1;
08833       } else if (option_debug)
08834          ast_log(LOG_DEBUG, "Registration created on call %d\n", reg->callno);
08835       iaxs[reg->callno]->reg = reg;
08836       ast_mutex_unlock(&iaxsl[reg->callno]);
08837    }
08838    /* Schedule the next registration attempt */
08839    AST_SCHED_DEL(sched, reg->expire);
08840    /* Setup the next registration a little early */
08841    reg->expire  = iax2_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
08842    /* Send the request */
08843    memset(&ied, 0, sizeof(ied));
08844    iax_ie_append_str(&ied, IAX_IE_USERNAME, reg->username);
08845    iax_ie_append_short(&ied, IAX_IE_REFRESH, reg->refresh);
08846    send_command(iaxs[reg->callno],AST_FRAME_IAX, IAX_COMMAND_REGREQ, 0, ied.buf, ied.pos, -1);
08847    reg->regstate = REG_STATE_REGSENT;
08848    return 0;
08849 }

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

Definition at line 5861 of file chan_iax2.c.

References __iax2_do_register_s(), and schedule_action.

Referenced by iax2_ack_registry(), and iax2_do_register().

05862 {
05863 #ifdef SCHED_MULTITHREADED
05864    if (schedule_action(__iax2_do_register_s, data))
05865 #endif      
05866       __iax2_do_register_s(data);
05867    return 0;
05868 }

static int iax2_do_trunk_debug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 5072 of file chan_iax2.c.

References ast_cli(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

05073 {
05074    if (argc < 3 || argc > 4)
05075       return RESULT_SHOWUSAGE;
05076    iaxtrunkdebug = 1;
05077    ast_cli(fd, "IAX2 Trunk Debug Requested\n");
05078    return RESULT_SUCCESS;
05079 }

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

Definition at line 6556 of file chan_iax2.c.

References AST_FRAME_IAX, AST_SCHED_DEL, auto_hangup(), chan_iax2_pvt::autoid, iax_ie_data::buf, CACHE_FLAG_TRANSMITTED, iax2_dpcache::exten, iax2_dpcache::flags, iax2_sched_add(), 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().

06557 {
06558    struct iax_ie_data ied;
06559    /* Auto-hangup with 30 seconds of inactivity */
06560    AST_SCHED_DEL(sched, iaxs[callno]->autoid);
06561    iaxs[callno]->autoid = iax2_sched_add(sched, 30000, auto_hangup, (void *)(long)callno);
06562    memset(&ied, 0, sizeof(ied));
06563    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, dp->exten);
06564    send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_DPREQ, 0, ied.buf, ied.pos, -1);
06565    dp->flags |= CACHE_FLAG_TRANSMITTED;
06566 }

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

References ast_copy_string(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_verbose(), CACHE_FLAG_EXISTS, dpcache_lock, find_cache(), iax2_dpcache::flags, LOG_NOTICE, LOG_WARNING, option_verbose, pbx_builtin_getvar_helper(), pbx_exec(), pbx_findapp(), and VERBOSE_PREFIX_3.

10681 {
10682    char odata[256];
10683    char req[256];
10684    char *ncontext;
10685    struct iax2_dpcache *dp;
10686    struct ast_app *dial;
10687 #if 0
10688    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);
10689 #endif
10690    if (priority == 2) {
10691       /* Indicate status, can be overridden in dialplan */
10692       const char *dialstatus = pbx_builtin_getvar_helper(chan, "DIALSTATUS");
10693       if (dialstatus) {
10694          dial = pbx_findapp(dialstatus);
10695          if (dial) 
10696             pbx_exec(chan, dial, "");
10697       }
10698       return -1;
10699    } else if (priority != 1)
10700       return -1;
10701    ast_mutex_lock(&dpcache_lock);
10702    dp = find_cache(chan, data, context, exten, priority);
10703    if (dp) {
10704       if (dp->flags & CACHE_FLAG_EXISTS) {
10705          ast_copy_string(odata, data, sizeof(odata));
10706          ncontext = strchr(odata, '/');
10707          if (ncontext) {
10708             *ncontext = '\0';
10709             ncontext++;
10710             snprintf(req, sizeof(req), "IAX2/%s/%s@%s", odata, exten, ncontext);
10711          } else {
10712             snprintf(req, sizeof(req), "IAX2/%s/%s", odata, exten);
10713          }
10714          if (option_verbose > 2)
10715             ast_verbose(VERBOSE_PREFIX_3 "Executing Dial('%s')\n", req);
10716       } else {
10717          ast_mutex_unlock(&dpcache_lock);
10718          ast_log(LOG_WARNING, "Can't execute nonexistent extension '%s[@%s]' in data '%s'\n", exten, context, data);
10719          return -1;
10720       }
10721    }
10722    ast_mutex_unlock(&dpcache_lock);
10723    dial = pbx_findapp("Dial");
10724    if (dial) {
10725       return pbx_exec(chan, dial, req);
10726    } else {
10727       ast_log(LOG_WARNING, "No dial application registered\n");
10728    }
10729    return -1;
10730 }

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

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), CACHE_FLAG_EXISTS, dpcache_lock, find_cache(), iax2_dpcache::flags, LOG_NOTICE, and LOG_WARNING.

10612 {
10613    struct iax2_dpcache *dp;
10614    int res = 0;
10615 #if 0
10616    ast_log(LOG_NOTICE, "iax2_exists: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
10617 #endif
10618    if ((priority != 1) && (priority != 2))
10619       return 0;
10620    ast_mutex_lock(&dpcache_lock);
10621    dp = find_cache(chan, data, context, exten, priority);
10622    if (dp) {
10623       if (dp->flags & CACHE_FLAG_EXISTS)
10624          res= 1;
10625    }
10626    ast_mutex_unlock(&dpcache_lock);
10627    if (!dp) {
10628       ast_log(LOG_WARNING, "Unable to make DP cache\n");
10629    }
10630    return res;
10631 }

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

Definition at line 2852 of file chan_iax2.c.

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

02853 {
02854    unsigned short callno = PTR_TO_CALLNO(newchan->tech_pvt);
02855    ast_mutex_lock(&iaxsl[callno]);
02856    if (iaxs[callno])
02857       iaxs[callno]->owner = newchan;
02858    else
02859       ast_log(LOG_WARNING, "Uh, this isn't a good sign...\n");
02860    ast_mutex_unlock(&iaxsl[callno]);
02861    return 0;
02862 }

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

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

Definition at line 1190 of file chan_iax2.c.

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

Referenced by __find_callno().

01191 {
01192    struct iax2_peer *peer = NULL;
01193    int res = 0;
01194    struct ao2_iterator i;
01195 
01196    i = ao2_iterator_init(peers, 0);
01197    while ((peer = ao2_iterator_next(&i))) {
01198       if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
01199           (peer->addr.sin_port == sin.sin_port)) {
01200          ast_copy_string(host, peer->name, len);
01201          peer_unref(peer);
01202          res = 1;
01203          break;
01204       }
01205       peer_unref(peer);
01206    }
01207 
01208    if (!peer) {
01209       peer = realtime_peer(NULL, &sin);
01210       if (peer) {
01211          ast_copy_string(host, peer->name, len);
01212          peer_unref(peer);
01213          res = 1;
01214       }
01215    }
01216 
01217    return res;
01218 }

static int iax2_getpeertrunk ( struct sockaddr_in  sin  )  [static]

Definition at line 3830 of file chan_iax2.c.

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

Referenced by check_access().

03831 {
03832    struct iax2_peer *peer;
03833    int res = 0;
03834    struct ao2_iterator i;
03835 
03836    i = ao2_iterator_init(peers, 0);
03837    while ((peer = ao2_iterator_next(&i))) {
03838       if ((peer->addr.sin_addr.s_addr == sin.sin_addr.s_addr) &&
03839           (peer->addr.sin_port == sin.sin_port)) {
03840          res = ast_test_flag(peer, IAX_TRUNK);
03841          peer_unref(peer);
03842          break;
03843       }
03844       peer_unref(peer);
03845    }
03846 
03847    return res;
03848 }

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

Definition at line 3453 of file chan_iax2.c.

References AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_add(), ast_test_flag, ast_verbose(), iax_ie_data::buf, iax2_registry::callno, CALLNO_TO_PTR, ast_channel::hangupcause, iax2_destroy(), iax2_predestroy(), IAX_ALREADYGONE, IAX_COMMAND_HANGUP, iax_ie_append_byte(), IAX_IE_CAUSECODE, LOG_DEBUG, LOG_WARNING, ast_channel::name, option_debug, option_verbose, iax_ie_data::pos, PTR_TO_CALLNO, scheduled_destroy(), send_command_final(), ast_channel::tech_pvt, and VERBOSE_PREFIX_3.

03454 {
03455    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
03456    struct iax_ie_data ied;
03457    int alreadygone;
03458    memset(&ied, 0, sizeof(ied));
03459    ast_mutex_lock(&iaxsl[callno]);
03460    if (callno && iaxs[callno]) {
03461       if (option_debug)
03462          ast_log(LOG_DEBUG, "We're hanging up %s with cause %i now...\n", c->name, c->hangupcause);
03463       alreadygone = ast_test_flag(iaxs[callno], IAX_ALREADYGONE);
03464       /* Send the hangup unless we have had a transmission error or are already gone */
03465       iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, (unsigned char)c->hangupcause);
03466       if (!iaxs[callno]->error && !alreadygone) {
03467          if (send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, ied.buf, ied.pos, -1)) {
03468             ast_log(LOG_WARNING, "No final packet could be sent for callno %d\n", callno);
03469          }
03470          if (!iaxs[callno]) {
03471             ast_mutex_unlock(&iaxsl[callno]);
03472             return 0;
03473          }
03474       }
03475       /* Explicitly predestroy it */
03476       iax2_predestroy(callno);
03477       /* If we were already gone to begin with, destroy us now */
03478       if (iaxs[callno] && alreadygone) {
03479          if (option_debug)
03480             ast_log(LOG_DEBUG, "Really destroying %s now...\n", c->name);
03481          iax2_destroy(callno);
03482       } else if (iaxs[callno]) {
03483          ast_sched_add(sched, 10000, scheduled_destroy, CALLNO_TO_PTR(callno));
03484       }
03485    } else if (c->tech_pvt) {
03486       /* If this call no longer exists, but the channel still
03487        * references it we need to set the channel's tech_pvt to null
03488        * to avoid ast_channel_free() trying to free it.
03489        */
03490       c->tech_pvt = NULL;
03491    }
03492    ast_mutex_unlock(&iaxsl[callno]);
03493    if (option_verbose > 2) 
03494       ast_verbose(VERBOSE_PREFIX_3 "Hungup '%s'\n", c->name);
03495    return 0;
03496 }

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

Definition at line 3771 of file chan_iax2.c.

References AST_CONTROL_HOLD, AST_CONTROL_UNHOLD, AST_FRAME_CONTROL, ast_log(), ast_moh_start(), ast_moh_stop(), ast_mutex_lock(), ast_mutex_unlock(), iax2_registry::callno, LOG_DEBUG, chan_iax2_pvt::mohinterpret, option_debug, PTR_TO_CALLNO, send_command(), ast_channel::tech_pvt, and wait_for_peercallno().

03772 {
03773    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
03774    struct chan_iax2_pvt *pvt;
03775    int res = 0;
03776 
03777    if (option_debug && iaxdebug)
03778       ast_log(LOG_DEBUG, "Indicating condition %d\n", condition);
03779 
03780    ast_mutex_lock(&iaxsl[callno]);
03781    pvt = iaxs[callno];
03782 
03783    if (wait_for_peercallno(pvt)) {
03784       res = -1;
03785       goto done;
03786    }
03787 
03788    switch (condition) {
03789    case AST_CONTROL_HOLD:
03790       if (strcasecmp(pvt->mohinterpret, "passthrough")) {
03791          ast_moh_start(c, data, pvt->mohinterpret);
03792          goto done;
03793       }
03794       break;
03795    case AST_CONTROL_UNHOLD:
03796       if (strcasecmp(pvt->mohinterpret, "passthrough")) {
03797          ast_moh_stop(c);
03798          goto done;
03799       }
03800    }
03801 
03802    res = send_command(pvt, AST_FRAME_CONTROL, condition, 0, data, datalen, -1);
03803 
03804 done:
03805    ast_mutex_unlock(&iaxsl[callno]);
03806 
03807    return res;
03808 }

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

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), CACHE_FLAG_MATCHMORE, dpcache_lock, find_cache(), iax2_dpcache::flags, LOG_NOTICE, and LOG_WARNING.

10658 {
10659    int res = 0;
10660    struct iax2_dpcache *dp;
10661 #if 0
10662    ast_log(LOG_NOTICE, "iax2_matchmore: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
10663 #endif
10664    if ((priority != 1) && (priority != 2))
10665       return 0;
10666    ast_mutex_lock(&dpcache_lock);
10667    dp = find_cache(chan, data, context, exten, priority);
10668    if (dp) {
10669       if (dp->flags & CACHE_FLAG_MATCHMORE)
10670          res= 1;
10671    }
10672    ast_mutex_unlock(&dpcache_lock);
10673    if (!dp) {
10674       ast_log(LOG_WARNING, "Unable to make DP cache\n");
10675    }
10676    return res;
10677 }

static int iax2_no_debug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 5090 of file chan_iax2.c.

References ast_cli(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

05091 {
05092    if (argc < 3 || argc > 4)
05093       return RESULT_SHOWUSAGE;
05094    iaxdebug = 0;
05095    ast_cli(fd, "IAX2 Debugging Disabled\n");
05096    return RESULT_SUCCESS;
05097 }

static int iax2_no_jb_debug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 5108 of file chan_iax2.c.

References ast_cli(), jb_debug_output(), jb_error_output(), jb_setoutput(), jb_warning_output(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

05109 {
05110    if (argc < 4 || argc > 5)
05111       return RESULT_SHOWUSAGE;
05112    jb_setoutput(jb_error_output, jb_warning_output, NULL);
05113    jb_debug_output("\n");
05114    ast_cli(fd, "IAX2 Jitterbuffer Debugging Disabled\n");
05115    return RESULT_SUCCESS;
05116 }

static int iax2_no_trunk_debug ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 5099 of file chan_iax2.c.

References ast_cli(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

05100 {
05101    if (argc < 4 || argc > 5)
05102       return RESULT_SHOWUSAGE;
05103    iaxtrunkdebug = 0;
05104    ast_cli(fd, "IAX2 Trunk Debugging Disabled\n");
05105    return RESULT_SUCCESS;
05106 }

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

Definition at line 8993 of file chan_iax2.c.

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

Referenced by iax2_poke_peer().

08994 {
08995    struct iax2_peer *peer = (struct iax2_peer *)data;
08996    peer->pokeexpire = -1;
08997 #ifdef SCHED_MULTITHREADED
08998    if (schedule_action(__iax2_poke_noanswer, data))
08999 #endif      
09000       __iax2_poke_noanswer(data);
09001    peer_unref(peer);
09002    return 0;
09003 }

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

Definition at line 9014 of file chan_iax2.c.

References iax2_peer::addr, AST_FRAME_IAX, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_del(), iax2_peer::callno, iax2_registry::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, 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().

09015 {
09016    int callno;
09017    if (!peer->maxms || (!peer->addr.sin_addr.s_addr && !peer->dnsmgr)) {
09018       /* IF we have no IP without dnsmgr, or this isn't to be monitored, return
09019         immediately after clearing things out */
09020       peer->lastms = 0;
09021       peer->historicms = 0;
09022       peer->pokeexpire = -1;
09023       peer->callno = 0;
09024       return 0;
09025    }
09026 
09027    /* The peer could change the callno inside iax2_destroy, since we do deadlock avoidance */
09028    if ((callno = peer->callno) > 0) {
09029       ast_log(LOG_NOTICE, "Still have a callno...\n");
09030       ast_mutex_lock(&iaxsl[callno]);
09031       iax2_destroy(callno);
09032       ast_mutex_unlock(&iaxsl[callno]);
09033    }
09034    if (heldcall)
09035       ast_mutex_unlock(&iaxsl[heldcall]);
09036    callno = peer->callno = find_callno(0, 0, &peer->addr, NEW_FORCE, peer->sockfd, 0);
09037    if (heldcall)
09038       ast_mutex_lock(&iaxsl[heldcall]);
09039    if (peer->callno < 1) {
09040       ast_log(LOG_WARNING, "Unable to allocate call for poking peer '%s'\n", peer->name);
09041       return -1;
09042    }
09043 
09044    /* Speed up retransmission times for this qualify call */
09045    iaxs[peer->callno]->pingtime = peer->maxms / 4 + 1;
09046    iaxs[peer->callno]->peerpoke = peer;
09047    
09048    /* Remove any pending pokeexpire task */
09049    if (peer->pokeexpire > -1) {
09050       if (!ast_sched_del(sched, peer->pokeexpire)) {
09051          peer->pokeexpire = -1;
09052          peer_unref(peer);
09053       }
09054    }
09055 
09056    /* Queue up a new task to handle no reply */
09057    /* If the host is already unreachable then use the unreachable interval instead */
09058    if (peer->lastms < 0) {
09059       peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_noanswer, peer_ref(peer));
09060    } else
09061       peer->pokeexpire = iax2_sched_add(sched, DEFAULT_MAXMS * 2, iax2_poke_noanswer, peer_ref(peer));
09062 
09063    if (peer->pokeexpire == -1)
09064       peer_unref(peer);
09065 
09066    /* And send the poke */
09067    ast_mutex_lock(&iaxsl[callno]);
09068    if (iaxs[callno]) {
09069       send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_POKE, 0, NULL, 0, -1);
09070    }
09071    ast_mutex_unlock(&iaxsl[callno]);
09072 
09073    return 0;
09074 }

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

Definition at line 9005 of file chan_iax2.c.

References iax2_poke_peer().

Referenced by load_module().

09006 {
09007    struct iax2_peer *peer = obj;
09008 
09009    iax2_poke_peer(peer, 0);
09010 
09011    return 0;
09012 }

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

Definition at line 6596 of file chan_iax2.c.

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

Referenced by __iax2_poke_noanswer(), and socket_process().

06597 {
06598    struct iax2_peer *peer = (struct iax2_peer *)data;
06599    peer->pokeexpire = -1;
06600 #ifdef SCHED_MULTITHREADED
06601    if (schedule_action(__iax2_poke_peer_s, data))
06602 #endif      
06603       __iax2_poke_peer_s(data);
06604    return 0;
06605 }

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 2186 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().

02187 {
02188    struct ast_channel *c;
02189    struct chan_iax2_pvt *pvt = iaxs[callno];
02190 
02191    if (!pvt)
02192       return -1;
02193    if (!ast_test_flag(pvt, IAX_ALREADYGONE)) {
02194       iax2_destroy_helper(pvt);
02195       ast_set_flag(pvt, IAX_ALREADYGONE); 
02196    }
02197    c = pvt->owner;
02198    if (c) {
02199       c->tech_pvt = NULL;
02200       iax2_queue_hangup(callno);
02201       pvt->owner = NULL;
02202       ast_module_unref(ast_module_info->self);
02203    }
02204    return 0;
02205 }

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

References iax2_thread::actions, ast_atomic_fetchadd_int(), ast_cond_timedwait(), ast_cond_wait(), AST_LIST_INSERT_HEAD, 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_TYPE_DYNAMIC, iaxactivethreadcount, iaxdynamicthreadcount, insert_idle_thread(), iax2_thread::iostate, iax2_thread::lock, iax2_thread::ready_for_signal, iax2_thread::scheddata, iax2_thread::schedfunc, socket_process(), t, thread, and iax2_thread::type.

Referenced by find_idle_thread(), and start_network_thread().

08670 {
08671    struct iax2_thread *thread = data;
08672    struct timeval tv;
08673    struct timespec ts;
08674    int put_into_idle = 0;
08675 
08676    ast_atomic_fetchadd_int(&iaxactivethreadcount,1);
08677    pthread_cleanup_push(iax2_process_thread_cleanup, data);
08678    for(;;) {
08679       /* Wait for something to signal us to be awake */
08680       ast_mutex_lock(&thread->lock);
08681 
08682       /* Flag that we're ready to accept signals */
08683       thread->ready_for_signal = 1;
08684       
08685       /* Put into idle list if applicable */
08686       if (put_into_idle)
08687          insert_idle_thread(thread);
08688 
08689       if (thread->type == IAX_TYPE_DYNAMIC) {
08690          struct iax2_thread *t = NULL;
08691          /* Wait to be signalled or time out */
08692          tv = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
08693          ts.tv_sec = tv.tv_sec;
08694          ts.tv_nsec = tv.tv_usec * 1000;
08695          if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT) {
08696             /* This thread was never put back into the available dynamic
08697              * thread list, so just go away. */
08698             if (!put_into_idle) {
08699                ast_mutex_unlock(&thread->lock);
08700                break;
08701             }
08702             AST_LIST_LOCK(&dynamic_list);
08703             /* Account for the case where this thread is acquired *right* after a timeout */
08704             if ((t = AST_LIST_REMOVE(&dynamic_list, thread, list)))
08705                iaxdynamicthreadcount--;
08706             AST_LIST_UNLOCK(&dynamic_list);
08707             if (t) {
08708                /* This dynamic thread timed out waiting for a task and was
08709                 * not acquired immediately after the timeout, 
08710                 * so it's time to go away. */
08711                ast_mutex_unlock(&thread->lock);
08712                break;
08713             }
08714             /* Someone grabbed our thread *right* after we timed out.
08715              * Wait for them to set us up with something to do and signal
08716              * us to continue. */
08717             tv = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
08718             ts.tv_sec = tv.tv_sec;
08719             ts.tv_nsec = tv.tv_usec * 1000;
08720             if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT)
08721             {
08722                ast_mutex_unlock(&thread->lock);
08723                break;
08724             }
08725          }
08726       } else {
08727          ast_cond_wait(&thread->cond, &thread->lock);
08728       }
08729 
08730       /* Go back into our respective list */
08731       put_into_idle = 1;
08732 
08733       ast_mutex_unlock(&thread->lock);
08734 
08735       if (thread->iostate == IAX_IOSTATE_IDLE)
08736          continue;
08737 
08738       /* Add ourselves to the active list now */
08739       AST_LIST_LOCK(&active_list);
08740       AST_LIST_INSERT_HEAD(&active_list, thread, list);
08741       AST_LIST_UNLOCK(&active_list);
08742 
08743       /* See what we need to do */
08744       switch(thread->iostate) {
08745       case IAX_IOSTATE_READY:
08746          thread->actions++;
08747          thread->iostate = IAX_IOSTATE_PROCESSING;
08748          socket_process(thread);
08749          handle_deferred_full_frames(thread);
08750          break;
08751       case IAX_IOSTATE_SCHEDREADY:
08752          thread->actions++;
08753          thread->iostate = IAX_IOSTATE_PROCESSING;
08754 #ifdef SCHED_MULTITHREADED
08755          thread->schedfunc(thread->scheddata);
08756 #endif      
08757          break;
08758       }
08759       time(&thread->checktime);
08760       thread->iostate = IAX_IOSTATE_IDLE;
08761 #ifdef DEBUG_SCHED_MULTITHREAD
08762       thread->curfunc[0]='\0';
08763 #endif      
08764 
08765       /* Now... remove ourselves from the active list, and return to the idle list */
08766       AST_LIST_LOCK(&active_list);
08767       AST_LIST_REMOVE(&active_list, thread, list);
08768       AST_LIST_UNLOCK(&active_list);
08769 
08770       /* Make sure another frame didn't sneak in there after we thought we were done. */
08771       handle_deferred_full_frames(thread);
08772    }
08773 
08774    /*!\note For some reason, idle threads are exiting without being removed
08775     * from an idle list, which is causing memory corruption.  Forcibly remove
08776     * it from the list, if it's there.
08777     */
08778    AST_LIST_LOCK(&idle_list);
08779    AST_LIST_REMOVE(&idle_list, thread, list);
08780    AST_LIST_UNLOCK(&idle_list);
08781 
08782    AST_LIST_LOCK(&dynamic_list);
08783    AST_LIST_REMOVE(&dynamic_list, thread, list);
08784    AST_LIST_UNLOCK(&dynamic_list);
08785 
08786    /* I am exiting here on my own volition, I need to clean up my own data structures
08787    * Assume that I am no longer in any of the lists (idle, active, or dynamic)
08788    */
08789    pthread_cleanup_pop(1);
08790 
08791    return NULL;
08792 }

static void iax2_process_thread_cleanup ( void *  data  )  [static]

Definition at line 8660 of file chan_iax2.c.

References ast_atomic_dec_and_test(), ast_cond_destroy(), ast_mutex_destroy(), iax2_thread::cond, free, iaxactivethreadcount, iax2_thread::lock, and thread.

Referenced by iax2_process_thread().

08661 {
08662    struct iax2_thread *thread = data;
08663    ast_mutex_destroy(&thread->lock);
08664    ast_cond_destroy(&thread->cond);
08665    free(thread);
08666    ast_atomic_dec_and_test(&iaxactivethreadcount);
08667 }

static int iax2_prov_cmd ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 8948 of file chan_iax2.c.

References ast_cli(), iax2_provision(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

08949 {
08950    int force = 0;
08951    int res;
08952    if (argc < 4)
08953       return RESULT_SHOWUSAGE;
08954    if ((argc > 4)) {
08955       if (!strcasecmp(argv[4], "forced"))
08956          force = 1;
08957       else
08958          return RESULT_SHOWUSAGE;
08959    }
08960    res = iax2_provision(NULL, -1, argv[2], argv[3], force);
08961    if (res < 0)
08962       ast_cli(fd, "Unable to find peer/address '%s'\n", argv[2]);
08963    else if (res < 1)
08964       ast_cli(fd, "No template (including wildcard) matching '%s'\n", argv[3]);
08965    else
08966       ast_cli(fd, "Provisioning '%s' with template '%s'%s\n", argv[2], argv[3], force ? ", forced" : "");
08967    return RESULT_SUCCESS;
08968 }

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

Definition at line 8851 of file chan_iax2.c.

References iax_prov_complete_template().

08852 {
08853    if (pos != 3)
08854       return NULL;
08855    return iax_prov_complete_template(line, word, pos, state);
08856 }

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

Definition at line 8858 of file chan_iax2.c.

References AST_FRAME_IAX, ast_log(), ast_mutex_unlock(), AST_SCHED_DEL, ast_set_flag, auto_hangup(), chan_iax2_pvt::autoid, iax_ie_data::buf, iax2_registry::callno, create_addr(), find_callno_locked(), iax2_sched_add(), IAX_COMMAND_PROVISION, iax_ie_append_raw(), IAX_IE_PROVISIONING, IAX_PROVISION, iax_provision_build(), LOG_DEBUG, NEW_FORCE, option_debug, iax_ie_data::pos, send_command(), and create_addr_info::sockfd.

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

08859 {
08860    /* Returns 1 if provisioned, -1 if not able to find destination, or 0 if no provisioning
08861       is found for template */
08862    struct iax_ie_data provdata;
08863    struct iax_ie_data ied;
08864    unsigned int sig;
08865    struct sockaddr_in sin;
08866    int callno;
08867    struct create_addr_info cai;
08868 
08869    memset(&cai, 0, sizeof(cai));
08870 
08871    if (option_debug)
08872       ast_log(LOG_DEBUG, "Provisioning '%s' from template '%s'\n", dest, template);
08873 
08874    if (iax_provision_build(&provdata, &sig, template, force)) {
08875       if (option_debug)
08876          ast_log(LOG_DEBUG, "No provisioning found for template '%s'\n", template);
08877       return 0;
08878    }
08879 
08880    if (end) {
08881       memcpy(&sin, end, sizeof(sin));
08882       cai.sockfd = sockfd;
08883    } else if (create_addr(dest, NULL, &sin, &cai))
08884       return -1;
08885 
08886    /* Build the rest of the message */
08887    memset(&ied, 0, sizeof(ied));
08888    iax_ie_append_raw(&ied, IAX_IE_PROVISIONING, provdata.buf, provdata.pos);
08889 
08890    callno = find_callno_locked(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
08891    if (!callno)
08892       return -1;
08893 
08894    if (iaxs[callno]) {
08895       /* Schedule autodestruct in case they don't ever give us anything back */
08896       AST_SCHED_DEL(sched, iaxs[callno]->autoid);
08897       iaxs[callno]->autoid = iax2_sched_add(sched, 15000, auto_hangup, (void *)(long)callno);
08898       ast_set_flag(iaxs[callno], IAX_PROVISION);
08899       /* Got a call number now, so go ahead and send the provisioning information */
08900       send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PROVISION, 0, ied.buf, ied.pos, -1);
08901    }
08902    ast_mutex_unlock(&iaxsl[callno]);
08903 
08904    return 1;
08905 }

static int iax2_prune_realtime ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 2310 of file chan_iax2.c.

References ast_cli(), ast_set_flag, ast_test_flag, expire_registry(), find_peer(), IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, peer_ref(), peer_unref(), reload_config(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

02311 {
02312    struct iax2_peer *peer;
02313 
02314    if (argc != 4)
02315         return RESULT_SHOWUSAGE;
02316    if (!strcmp(argv[3],"all")) {
02317       reload_config();
02318       ast_cli(fd, "OK cache is flushed.\n");
02319    } else if ((peer = find_peer(argv[3], 0))) {
02320       if(ast_test_flag(peer, IAX_RTCACHEFRIENDS)) {
02321          ast_set_flag(peer, IAX_RTAUTOCLEAR);
02322          expire_registry(peer_ref(peer));
02323          ast_cli(fd, "OK peer %s was removed from the cache.\n", argv[3]);
02324       } else {
02325          ast_cli(fd, "SORRY peer %s is not eligible for this operation.\n", argv[3]);
02326       }
02327       peer_unref(peer);
02328    } else {
02329       ast_cli(fd, "SORRY peer %s was not found in the cache.\n", argv[3]);
02330    }
02331    
02332    return RESULT_SUCCESS;
02333 }

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

References ast_mutex_trylock(), ast_mutex_unlock(), ast_queue_control_data(), DEADLOCK_AVOIDANCE, ast_channel::lock, and chan_iax2_pvt::owner.

Referenced by socket_process().

01790 {
01791    for (;;) {
01792       if (iaxs[callno] && iaxs[callno]->owner) {
01793          if (ast_mutex_trylock(&iaxs[callno]->owner->lock)) {
01794             /* Avoid deadlock by pausing and trying again */
01795             DEADLOCK_AVOIDANCE(&iaxsl[callno]);
01796          } else {
01797             ast_queue_control_data(iaxs[callno]->owner, control, data, datalen);
01798             ast_mutex_unlock(&iaxs[callno]->owner->lock);
01799             break;
01800          }
01801       } else
01802          break;
01803    }
01804    return 0;
01805 }

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

References ast_mutex_trylock(), ast_mutex_unlock(), ast_queue_frame(), DEADLOCK_AVOIDANCE, ast_channel::lock, and chan_iax2_pvt::owner.

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

01727 {
01728    for (;;) {
01729       if (iaxs[callno] && iaxs[callno]->owner) {
01730          if (ast_mutex_trylock(&iaxs[callno]->owner->lock)) {
01731             /* Avoid deadlock by pausing and trying again */
01732             DEADLOCK_AVOIDANCE(&iaxsl[callno]);
01733          } else {
01734             ast_queue_frame(iaxs[callno]->owner, f);
01735             ast_mutex_unlock(&iaxs[callno]->owner->lock);
01736             break;
01737          }
01738       } else
01739          break;
01740    }
01741    return 0;
01742 }

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

References ast_mutex_trylock(), ast_mutex_unlock(), ast_queue_hangup(), DEADLOCK_AVOIDANCE, ast_channel::lock, and chan_iax2_pvt::owner.

Referenced by iax2_predestroy().

01758 {
01759    for (;;) {
01760       if (iaxs[callno] && iaxs[callno]->owner) {
01761          if (ast_mutex_trylock(&iaxs[callno]->owner->lock)) {
01762             /* Avoid deadlock by pausing and trying again */
01763             DEADLOCK_AVOIDANCE(&iaxsl[callno]);
01764          } else {
01765             ast_queue_hangup(iaxs[callno]->owner);
01766             ast_mutex_unlock(&iaxs[callno]->owner->lock);
01767             break;
01768          }
01769       } else
01770          break;
01771    }
01772    return 0;
01773 }

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

Definition at line 3562 of file chan_iax2.c.

References ast_log(), ast_null_frame, and LOG_NOTICE.

03563 {
03564    ast_log(LOG_NOTICE, "I should never be called!\n");
03565    return &ast_null_frame;
03566 }

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

Definition at line 6078 of file chan_iax2.c.

References iax2_registry::addr, ast_calloc, ast_copy_string(), ast_dnsmgr_lookup(), AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), copy(), iax2_registry::dnsmgr, iax2_registry::expire, free, hostname, IAX_DEFAULT_PORTNO, IAX_DEFAULT_REG_EXPIRE, LOG_WARNING, iax2_registry::refresh, iax2_registry::secret, and iax2_registry::username.

Referenced by set_config().

06079 {
06080    struct iax2_registry *reg;
06081    char copy[256];
06082    char *username, *hostname, *secret;
06083    char *porta;
06084    char *stringp=NULL;
06085    
06086    if (!value)
06087       return -1;
06088    ast_copy_string(copy, value, sizeof(copy));
06089    stringp=copy;
06090    username = strsep(&stringp, "@");
06091    hostname = strsep(&stringp, "@");
06092    if (!hostname) {
06093       ast_log(LOG_WARNING, "Format for registration is user[:secret]@host[:port] at line %d\n", lineno);
06094       return -1;
06095    }
06096    stringp=username;
06097    username = strsep(&stringp, ":");
06098    secret = strsep(&stringp, ":");
06099    stringp=hostname;
06100    hostname = strsep(&stringp, ":");
06101    porta = strsep(&stringp, ":");
06102    
06103    if (porta && !atoi(porta)) {
06104       ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
06105       return -1;
06106    }
06107    if (!(reg = ast_calloc(1, sizeof(*reg))))
06108       return -1;
06109    if (ast_dnsmgr_lookup(hostname, &reg->addr.sin_addr, &reg->dnsmgr) < 0) {
06110       free(reg);
06111       return -1;
06112    }
06113    ast_copy_string(reg->username, username, sizeof(reg->username));
06114    if (secret)
06115       ast_copy_string(reg->secret, secret, sizeof(reg->secret));
06116    reg->expire = -1;
06117    reg->refresh = IAX_DEFAULT_REG_EXPIRE;
06118    reg->addr.sin_family = AF_INET;
06119    reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO);
06120    AST_LIST_LOCK(&registrations);
06121    AST_LIST_INSERT_HEAD(&registrations, reg, entry);
06122    AST_LIST_UNLOCK(&registrations);
06123    
06124    return 0;
06125 }

static int iax2_reload ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 10376 of file chan_iax2.c.

References reload_config().

10377 {
10378    return reload_config();
10379 }

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

Definition at line 9086 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(), iax2_registry::callno, 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.

09087 {
09088    int callno;
09089    int res;
09090    int fmt, native;
09091    struct sockaddr_in sin;
09092    struct ast_channel *c;
09093    struct parsed_dial_string pds;
09094    struct create_addr_info cai;
09095    char *tmpstr;
09096 
09097    memset(&pds, 0, sizeof(pds));
09098    tmpstr = ast_strdupa(data);
09099    parse_dial_string(tmpstr, &pds);
09100 
09101    if (ast_strlen_zero(pds.peer)) {
09102       ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", (char *) data);
09103       return NULL;
09104    }
09105           
09106    memset(&cai, 0, sizeof(cai));
09107    cai.capability = iax2_capability;
09108 
09109    ast_copy_flags(&cai, &globalflags, IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF);
09110    
09111    /* Populate our address from the given */
09112    if (create_addr(pds.peer, NULL, &sin, &cai)) {
09113       *cause = AST_CAUSE_UNREGISTERED;
09114       return NULL;
09115    }
09116 
09117    if (pds.port)
09118       sin.sin_port = htons(atoi(pds.port));
09119 
09120    callno = find_callno_locked(0, 0, &sin, NEW_FORCE, cai.sockfd, 0);
09121    if (callno < 1) {
09122       ast_log(LOG_WARNING, "Unable to create call\n");
09123       *cause = AST_CAUSE_CONGESTION;
09124       return NULL;
09125    }
09126 
09127    /* If this is a trunk, update it now */
09128    ast_copy_flags(iaxs[callno], &cai, IAX_TRUNK | IAX_SENDANI | IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF | IAX_FORCEJITTERBUF); 
09129    if (ast_test_flag(&cai, IAX_TRUNK)) {
09130       int new_callno;
09131       if ((new_callno = make_trunk(callno, 1)) != -1)
09132          callno = new_callno;
09133    }
09134    iaxs[callno]->maxtime = cai.maxtime;
09135    if (cai.found)
09136       ast_string_field_set(iaxs[callno], host, pds.peer);
09137 
09138    c = ast_iax2_new(callno, AST_STATE_DOWN, cai.capability);
09139 
09140    ast_mutex_unlock(&iaxsl[callno]);
09141 
09142    if (c) {
09143       /* Choose a format we can live with */
09144       if (c->nativeformats & format) 
09145          c->nativeformats &= format;
09146       else {
09147          native = c->nativeformats;
09148          fmt = format;
09149          res = ast_translator_best_choice(&fmt, &native);
09150          if (res < 0) {
09151             ast_log(LOG_WARNING, "Unable to create translator path for %s to %s on %s\n",
09152                ast_getformatname(c->nativeformats), ast_getformatname(fmt), c->name);
09153             ast_hangup(c);
09154             return NULL;
09155          }
09156          c->nativeformats = native;
09157       }
09158       c->readformat = ast_best_codec(c->nativeformats);
09159       c->writeformat = c->readformat;
09160    }
09161 
09162    return c;
09163 }

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

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

References chan_iax2_pvt::addr, iax_frame::af, iax_frame::afdatalen, 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, chan_iax2_pvt::ecx, encrypt_frame(), iax_frame::final, ast_frame::frametype, iax2_transmit(), iax2_trunk_queue(), IAX_COMMAND_ACK, IAX_ENCRYPTED, IAX_FLAG_FULL, iax_frame_new(), iax_frame_wrap(), IAX_KEYPOPULATED, iax_showframe(), IAX_TRUNK, ast_iax2_full_hdr::iseqno, chan_iax2_pvt::iseqno, iax_frame::iseqno, chan_iax2_pvt::lastsent, chan_iax2_pvt::lastvsent, LOG_WARNING, MAX_RETRY_TIME, MIN_RETRY_TIME, ast_iax2_full_hdr::oseqno, chan_iax2_pvt::oseqno, iax_frame::oseqno, chan_iax2_pvt::peercallno, chan_iax2_pvt::pingtime, iax_frame::retries, iax_frame::retrytime, ast_iax2_full_hdr::scallno, 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(), and socket_process().

04402 {
04403    /* Queue a packet for delivery on a given private structure.  Use "ts" for
04404       timestamp, or calculate if ts is 0.  Send immediately without retransmission
04405       or delayed, with retransmission */
04406    struct ast_iax2_full_hdr *fh;
04407    struct ast_iax2_mini_hdr *mh;
04408    struct ast_iax2_video_hdr *vh;
04409    struct {
04410       struct iax_frame fr2;
04411       unsigned char buffer[4096];
04412    } frb;
04413    struct iax_frame *fr;
04414    int res;
04415    int sendmini=0;
04416    unsigned int lastsent;
04417    unsigned int fts;
04418 
04419    frb.fr2.afdatalen = sizeof(frb.buffer);
04420 
04421    if (!pvt) {
04422       ast_log(LOG_WARNING, "No private structure for packet?\n");
04423       return -1;
04424    }
04425    
04426    lastsent = pvt->lastsent;
04427 
04428    /* Calculate actual timestamp */
04429    fts = calc_timestamp(pvt, ts, f);
04430 
04431    /* Bail here if this is an "interp" frame; we don't want or need to send these placeholders out
04432     * (the endpoint should detect the lost packet itself).  But, we want to do this here, so that we
04433     * increment the "predicted timestamps" for voice, if we're predecting */
04434    if(f->frametype == AST_FRAME_VOICE && f->datalen == 0)
04435        return 0;
04436 
04437 
04438    if ((ast_test_flag(pvt, IAX_TRUNK) || 
04439          (((fts & 0xFFFF0000L) == (lastsent & 0xFFFF0000L)) ||
04440          ((fts & 0xFFFF0000L) == ((lastsent + 0x10000) & 0xFFFF0000L))))
04441       /* High two bytes are the same on timestamp, or sending on a trunk */ &&
04442        (f->frametype == AST_FRAME_VOICE) 
04443       /* is a voice frame */ &&
04444       (f->subclass == pvt->svoiceformat) 
04445       /* is the same type */ ) {
04446          /* Force immediate rather than delayed transmission */
04447          now = 1;
04448          /* Mark that mini-style frame is appropriate */
04449          sendmini = 1;
04450    }
04451    if ( f->frametype == AST_FRAME_VIDEO ) {
04452       /*
04453        * If the lower 15 bits of the timestamp roll over, or if
04454        * the video format changed then send a full frame.
04455        * Otherwise send a mini video frame
04456        */
04457       if (((fts & 0xFFFF8000L) == (pvt->lastvsent & 0xFFFF8000L)) &&
04458           ((f->subclass & ~0x1) == pvt->svideoformat)
04459          ) {
04460          now = 1;
04461          sendmini = 1;
04462       } else {
04463          now = 0;
04464          sendmini = 0;
04465       }
04466       pvt->lastvsent = fts;
04467    }
04468    /* Allocate an iax_frame */
04469    if (now) {
04470       fr = &frb.fr2;
04471    } else
04472       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));
04473    if (!fr) {
04474       ast_log(LOG_WARNING, "Out of memory\n");
04475       return -1;
04476    }
04477    /* Copy our prospective frame into our immediate or retransmitted wrapper */
04478    iax_frame_wrap(fr, f);
04479 
04480    fr->ts = fts;
04481    fr->callno = pvt->callno;
04482    fr->transfer = transfer;
04483    fr->final = final;
04484    if (!sendmini) {
04485       /* We need a full frame */
04486       if (seqno > -1)
04487          fr->oseqno = seqno;
04488       else
04489          fr->oseqno = pvt->oseqno++;
04490       fr->iseqno = pvt->iseqno;
04491       fh = (struct ast_iax2_full_hdr *)(fr->af.data - sizeof(struct ast_iax2_full_hdr));
04492       fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
04493       fh->ts = htonl(fr->ts);
04494       fh->oseqno = fr->oseqno;
04495       if (transfer) {
04496          fh->iseqno = 0;
04497       } else
04498          fh->iseqno = fr->iseqno;
04499       /* Keep track of the last thing we've acknowledged */
04500       if (!transfer)
04501          pvt->aseqno = fr->iseqno;
04502       fh->type = fr->af.frametype & 0xFF;
04503       if (fr->af.frametype == AST_FRAME_VIDEO)
04504          fh->csub = compress_subclass(fr->af.subclass & ~0x1) | ((fr->af.subclass & 0x1) << 6);
04505       else
04506          fh->csub = compress_subclass(fr->af.subclass);
04507       if (transfer) {
04508          fr->dcallno = pvt->transfercallno;
04509       } else
04510          fr->dcallno = pvt->peercallno;
04511       fh->dcallno = htons(fr->dcallno);
04512       fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_full_hdr);
04513       fr->data = fh;
04514       fr->retries = 0;
04515       /* Retry after 2x the ping time has passed */
04516       fr->retrytime = pvt->pingtime * 2;
04517       if (fr->retrytime < MIN_RETRY_TIME)
04518          fr->retrytime = MIN_RETRY_TIME;
04519       if (fr->retrytime > MAX_RETRY_TIME)
04520          fr->retrytime = MAX_RETRY_TIME;
04521       /* Acks' don't get retried */
04522       if ((f->frametype == AST_FRAME_IAX) && (f->subclass == IAX_COMMAND_ACK))
04523          fr->retries = -1;
04524       else if (f->frametype == AST_FRAME_VOICE)
04525          pvt->svoiceformat = f->subclass;
04526       else if (f->frametype == AST_FRAME_VIDEO)
04527          pvt->svideoformat = f->subclass & ~0x1;
04528       if (ast_test_flag(pvt, IAX_ENCRYPTED)) {
04529          if (ast_test_flag(pvt, IAX_KEYPOPULATED)) {
04530             if (iaxdebug) {
04531                if (fr->transfer)
04532                   iax_showframe(fr, NULL, 2, &pvt->transfer, fr->datalen - sizeof(struct ast_iax2_full_hdr));
04533                else
04534                   iax_showframe(fr, NULL, 2, &pvt->addr, fr->datalen - sizeof(struct ast_iax2_full_hdr));
04535             }
04536             encrypt_frame(&pvt->ecx, fh, pvt->semirand, &fr->datalen);
04537          } else
04538             ast_log(LOG_WARNING, "Supposed to send packet encrypted, but no key?\n");
04539       }
04540    
04541       if (now) {
04542          res = send_packet(fr);
04543       } else
04544          res = iax2_transmit(fr);
04545    } else {
04546       if (ast_test_flag(pvt, IAX_TRUNK)) {
04547          iax2_trunk_queue(pvt, fr);
04548          res = 0;
04549       } else if (fr->af.frametype == AST_FRAME_VIDEO) {
04550          /* Video frame have no sequence number */
04551          fr->oseqno = -1;
04552          fr->iseqno = -1;
04553          vh = (struct ast_iax2_video_hdr *)(fr->af.data - sizeof(struct ast_iax2_video_hdr));
04554          vh->zeros = 0;
04555          vh->callno = htons(0x8000 | fr->callno);
04556          vh->ts = htons((fr->ts & 0x7FFF) | (fr->af.subclass & 0x1 ? 0x8000 : 0));
04557          fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_video_hdr);
04558          fr->data = vh;
04559          fr->retries = -1;
04560          res = send_packet(fr);        
04561       } else {
04562          /* Mini-frames have no sequence number */
04563          fr->oseqno = -1;
04564          fr->iseqno = -1;
04565          /* Mini frame will do */
04566          mh = (struct ast_iax2_mini_hdr *)(fr->af.data - sizeof(struct ast_iax2_mini_hdr));
04567          mh->callno = htons(fr->callno);
04568          mh->ts = htons(fr->ts & 0xFFFF);
04569          fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
04570          fr->data = mh;
04571          fr->retries = -1;
04572          if (pvt->transferring == TRANSFER_MEDIAPASS)
04573             fr->transfer = 1;
04574          if (ast_test_flag(pvt, IAX_ENCRYPTED)) {
04575             if (ast_test_flag(pvt, IAX_KEYPOPULATED)) {
04576                encrypt_frame(&pvt->ecx, (struct ast_iax2_full_hdr *)mh, pvt->semirand, &fr->datalen);
04577             } else
04578                ast_log(LOG_WARNING, "Supposed to send packet encrypted, but no key?\n");
04579          }
04580          res = send_packet(fr);
04581       }
04582    }
04583    return res;
04584 }

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

Definition at line 2847 of file chan_iax2.c.

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

02848 {
02849    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_HTML, subclass, 0, (unsigned char *)data, datalen, -1);
02850 }

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

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

02836 {
02837    
02838    return send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_TEXT,
02839       0, 0, (unsigned char *)text, strlen(text) + 1, -1);
02840 }

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

Definition at line 3520 of file chan_iax2.c.

References AST_CONTROL_OPTION, AST_FRAME_CONTROL, ast_malloc, ast_mutex_lock(), ast_mutex_unlock(), AST_OPTION_FLAG_REQUEST, AST_OPTION_RXGAIN, AST_OPTION_TXGAIN, iax2_registry::callno, ast_option_header::data, errno, ast_option_header::flag, free, ast_option_header::option, PTR_TO_CALLNO, send_command_locked(), ast_channel::tech_pvt, and wait_for_peercallno().

03521 {
03522    struct ast_option_header *h;
03523    int res;
03524 
03525    switch (option) {
03526    case AST_OPTION_TXGAIN:
03527    case AST_OPTION_RXGAIN:
03528       /* these two cannot be sent, because they require a result */
03529       errno = ENOSYS;
03530       return -1;
03531    default:
03532    {
03533       unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
03534       struct chan_iax2_pvt *pvt;
03535 
03536       ast_mutex_lock(&iaxsl[callno]);
03537       pvt = iaxs[callno];
03538 
03539       if (wait_for_peercallno(pvt)) {
03540          ast_mutex_unlock(&iaxsl[callno]);
03541          return -1;
03542       }
03543 
03544       ast_mutex_unlock(&iaxsl[callno]);
03545 
03546       if (!(h = ast_malloc(datalen + sizeof(*h)))) {
03547          return -1;
03548       }
03549 
03550       h->flag = AST_OPTION_FLAG_REQUEST;
03551       h->option = htons(option);
03552       memcpy(h->data, data, datalen);
03553       res = send_command_locked(PTR_TO_CALLNO(c->tech_pvt), AST_FRAME_CONTROL,
03554                  AST_CONTROL_OPTION, 0, (unsigned char *) h,
03555                  datalen + sizeof(*h), -1);
03556       free(h);
03557       return res;
03558    }
03559    }
03560 }

static int iax2_show_cache ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 2514 of file chan_iax2.c.

References ast_cli(), ast_copy_string(), ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), CACHE_FLAG_CANEXIST, CACHE_FLAG_EXISTS, CACHE_FLAG_MATCHMORE, CACHE_FLAG_NONEXISTENT, CACHE_FLAG_PENDING, CACHE_FLAG_TIMEOUT, CACHE_FLAG_TRANSMITTED, CACHE_FLAG_UNKNOWN, dpcache, dpcache_lock, iax2_dpcache::expiry, iax2_dpcache::exten, iax2_dpcache::flags, iax2_dpcache::next, iax2_dpcache::peercontext, RESULT_SUCCESS, s, and iax2_dpcache::waiters.

02515 {
02516    struct iax2_dpcache *dp;
02517    char tmp[1024], *pc;
02518    int s;
02519    int x,y;
02520    struct timeval tv;
02521    gettimeofday(&tv, NULL);
02522    ast_mutex_lock(&dpcache_lock);
02523    dp = dpcache;
02524    ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8.8s %s\n", "Peer/Context", "Exten", "Exp.", "Wait.", "Flags");
02525    while(dp) {
02526       s = dp->expiry.tv_sec - tv.tv_sec;
02527       tmp[0] = '\0';
02528       if (dp->flags & CACHE_FLAG_EXISTS)
02529          strncat(tmp, "EXISTS|", sizeof(tmp) - strlen(tmp) - 1);
02530       if (dp->flags & CACHE_FLAG_NONEXISTENT)
02531          strncat(tmp, "NONEXISTENT|", sizeof(tmp) - strlen(tmp) - 1);
02532       if (dp->flags & CACHE_FLAG_CANEXIST)
02533          strncat(tmp, "CANEXIST|", sizeof(tmp) - strlen(tmp) - 1);
02534       if (dp->flags & CACHE_FLAG_PENDING)
02535          strncat(tmp, "PENDING|", sizeof(tmp) - strlen(tmp) - 1);
02536       if (dp->flags & CACHE_FLAG_TIMEOUT)
02537          strncat(tmp, "TIMEOUT|", sizeof(tmp) - strlen(tmp) - 1);
02538       if (dp->flags & CACHE_FLAG_TRANSMITTED)
02539          strncat(tmp, "TRANSMITTED|", sizeof(tmp) - strlen(tmp) - 1);
02540       if (dp->flags & CACHE_FLAG_MATCHMORE)
02541          strncat(tmp, "MATCHMORE|", sizeof(tmp) - strlen(tmp) - 1);
02542       if (dp->flags & CACHE_FLAG_UNKNOWN)
02543          strncat(tmp, "UNKNOWN|", sizeof(tmp) - strlen(tmp) - 1);
02544       /* Trim trailing pipe */
02545       if (!ast_strlen_zero(tmp))
02546          tmp[strlen(tmp) - 1] = '\0';
02547       else
02548          ast_copy_string(tmp, "(none)", sizeof(tmp));
02549       y=0;
02550       pc = strchr(dp->peercontext, '@');
02551       if (!pc)
02552          pc = dp->peercontext;
02553       else
02554          pc++;
02555       for (x=0;x<sizeof(dp->waiters) / sizeof(dp->waiters[0]); x++)
02556          if (dp->waiters[x] > -1)
02557             y++;
02558       if (s > 0)
02559          ast_cli(fd, "%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->exten, s, y, tmp);
02560       else
02561          ast_cli(fd, "%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->exten, "(expired)", y, tmp);
02562       dp = dp->next;
02563    }
02564    ast_mutex_unlock(&dpcache_lock);
02565    return RESULT_SUCCESS;
02566 }

static int iax2_show_channels ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 4928 of file chan_iax2.c.

References iax2_registry::addr, ARRAY_LEN, ast_cli(), ast_getformatname(), ast_inet_ntoa(), ast_mutex_lock(), ast_mutex_unlock(), ast_test_flag, iax2_registry::callno, jb_info::current, iax_rr::delay, FORMAT, FORMAT2, IAX_USEJITTERBUF, jb_getinfo(), jb_info::jitter, jb_info::min, chan_iax2_pvt::remote_rr, RESULT_SHOWUSAGE, RESULT_SUCCESS, and S_OR.

04929 {
04930 #define FORMAT2 "%-20.20s  %-15.15s  %-10.10s  %-11.11s  %-11.11s  %-7.7s  %-6.6s  %-6.6s  %s\n"
04931 #define FORMAT  "%-20.20s  %-15.15s  %-10.10s  %5.5d/%5.5d  %5.5d/%5.5d  %-5.5dms  %-4.4dms  %-4.4dms  %-6.6s\n"
04932 #define FORMATB "%-20.20s  %-15.15s  %-10.10s  %5.5d/%5.5d  %5.5d/%5.5d  [Native Bridged to ID=%5.5d]\n"
04933    int x;
04934    int numchans = 0;
04935 
04936    if (argc != 3)
04937       return RESULT_SHOWUSAGE;
04938    ast_cli(fd, FORMAT2, "Channel", "Peer", "Username", "ID (Lo/Rem)", "Seq (Tx/Rx)", "Lag", "Jitter", "JitBuf", "Format");
04939    for (x = 0; x < ARRAY_LEN(iaxs); x++) {
04940       ast_mutex_lock(&iaxsl[x]);
04941       if (iaxs[x]) {
04942          int lag, jitter, localdelay;
04943          jb_info jbinfo;
04944          
04945          if(ast_test_flag(iaxs[x], IAX_USEJITTERBUF)) {
04946             jb_getinfo(iaxs[x]->jb, &jbinfo);
04947             jitter = jbinfo.jitter;
04948             localdelay = jbinfo.current - jbinfo.min;
04949          } else {
04950             jitter = -1;
04951             localdelay = 0;
04952          }
04953          lag = iaxs[x]->remote_rr.delay;
04954          ast_cli(fd, FORMAT,
04955             iaxs[x]->owner ? iaxs[x]->owner->name : "(None)",
04956             ast_inet_ntoa(iaxs[x]->addr.sin_addr), 
04957             S_OR(iaxs[x]->username, "(None)"),
04958             iaxs[x]->callno, iaxs[x]->peercallno,
04959             iaxs[x]->oseqno, iaxs[x]->iseqno,
04960             lag,
04961             jitter,
04962             localdelay,
04963             ast_getformatname(iaxs[x]->voiceformat) );
04964          numchans++;
04965       }
04966       ast_mutex_unlock(&iaxsl[x]);
04967    }
04968    ast_cli(fd, "%d active IAX channel%s\n", numchans, (numchans != 1) ? "s" : "");
04969    return RESULT_SUCCESS;
04970 #undef FORMAT
04971 #undef FORMAT2
04972 #undef FORMATB
04973 }

static int iax2_show_firmware ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 4839 of file chan_iax2.c.

References ast_cli(), ast_mutex_lock(), ast_mutex_unlock(), ast_iax2_firmware_header::datalen, ast_iax2_firmware_header::devname, FORMAT, FORMAT2, iax_firmware::fwh, ast_firmware_list::lock, iax_firmware::next, RESULT_SHOWUSAGE, RESULT_SUCCESS, ast_iax2_firmware_header::version, ast_firmware_list::wares, and waresl.

04840 {
04841 #define FORMAT2 "%-15.15s  %-15.15s %-15.15s\n"
04842 #if !defined(__FreeBSD__)
04843 #define FORMAT "%-15.15s  %-15d %-15d\n"
04844 #else /* __FreeBSD__ */
04845 #define FORMAT "%-15.15s  %-15d %-15d\n" /* XXX 2.95 ? */
04846 #endif /* __FreeBSD__ */
04847    struct iax_firmware *cur;
04848    if ((argc != 3) && (argc != 4))
04849       return RESULT_SHOWUSAGE;
04850    ast_mutex_lock(&waresl.lock);
04851    
04852    ast_cli(fd, FORMAT2, "Device", "Version", "Size");
04853    for (cur = waresl.wares;cur;cur = cur->next) {
04854       if ((argc == 3) || (!strcasecmp(argv[3], (char *)cur->fwh->devname))) 
04855          ast_cli(fd, FORMAT, cur->fwh->devname, ntohs(cur->fwh->version),
04856             (int)ntohl(cur->fwh->datalen));
04857    }
04858    ast_mutex_unlock(&waresl.lock);
04859    return RESULT_SUCCESS;
04860 #undef FORMAT
04861 #undef FORMAT2
04862 }

static int iax2_show_netstats ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 5051 of file chan_iax2.c.

References ast_cli(), ast_cli_netstats(), RESULT_SHOWUSAGE, and RESULT_SUCCESS.

05052 {
05053    int numchans = 0;
05054    if (argc != 3)
05055       return RESULT_SHOWUSAGE;
05056    ast_cli(fd, "                                -------- LOCAL ---------------------  -------- REMOTE --------------------\n");
05057    ast_cli(fd, "Channel                    RTT  Jit  Del  Lost   %%  Drop  OOO  Kpkts  Jit  Del  Lost   %%  Drop  OOO  Kpkts\n");
05058    numchans = ast_cli_netstats(NULL, fd, 1);
05059    ast_cli(fd, "%d active IAX channel%s\n", numchans, (numchans != 1) ? "s" : "");
05060    return RESULT_SUCCESS;
05061 }

static int iax2_show_peer ( int  fd,
int  argc,
char *  argv[] 
) [static]

Show one peer in detail.

Definition at line 2404 of file chan_iax2.c.

References iax2_peer::addr, ast_callerid_merge(), ast_cli(), ast_codec_pref_index(), ast_getformatname(), ast_getformatname_multiple(), ast_inet_ntoa(), ast_strlen_zero(), ast_test_flag, iax2_peer::capability, iax2_peer::cid_name, iax2_peer::cid_num, iax2_peer::context, iax2_peer::defaddr, iax2_peer::expire, find_peer(), iax2_peer::ha, IAX_DYNAMIC, iax2_peer::mailbox, iax2_peer::name, peer_status(), peer_unref(), iax2_peer::pokefreqnotok, iax2_peer::pokefreqok, iax2_peer::prefs, RESULT_SHOWUSAGE, RESULT_SUCCESS, iax2_peer::secret, iax2_peer::smoothing, and iax2_peer::username.

02405 {
02406    char status[30];
02407    char cbuf[256];
02408    struct iax2_peer *peer;
02409    char codec_buf[512];
02410    int x = 0, codec = 0, load_realtime = 0;
02411 
02412    if (argc < 4)
02413       return RESULT_SHOWUSAGE;
02414 
02415    load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? 1 : 0;
02416 
02417    peer = find_peer(argv[3], load_realtime);
02418    if (peer) {
02419       ast_cli(fd,"\n\n");
02420       ast_cli(fd, "  * Name       : %s\n", peer->name);
02421       ast_cli(fd, "  Secret       : %s\n", ast_strlen_zero(peer->secret)?"<Not set>":"<Set>");
02422       ast_cli(fd, "  Context      : %s\n", peer->context);
02423       ast_cli(fd, "  Mailbox      : %s\n", peer->mailbox);
02424       ast_cli(fd, "  Dynamic      : %s\n", ast_test_flag(peer, IAX_DYNAMIC) ? "Yes":"No");
02425       ast_cli(fd, "  Callerid     : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
02426       ast_cli(fd, "  Expire       : %d\n", peer->expire);
02427       ast_cli(fd, "  ACL          : %s\n", (peer->ha?"Yes":"No"));
02428       ast_cli(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));
02429       ast_cli(fd, "  Defaddr->IP  : %s Port %d\n", ast_inet_ntoa(peer->defaddr.sin_addr), ntohs(peer->defaddr.sin_port));
02430       ast_cli(fd, "  Username     : %s\n", peer->username);
02431       ast_cli(fd, "  Codecs       : ");
02432       ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
02433       ast_cli(fd, "%s\n", codec_buf);
02434 
02435       ast_cli(fd, "  Codec Order  : (");
02436       for(x = 0; x < 32 ; x++) {
02437          codec = ast_codec_pref_index(&peer->prefs,x);
02438          if(!codec)
02439             break;
02440          ast_cli(fd, "%s", ast_getformatname(codec));
02441          if(x < 31 && ast_codec_pref_index(&peer->prefs,x+1))
02442             ast_cli(fd, "|");
02443       }
02444 
02445       if (!x)
02446          ast_cli(fd, "none");
02447       ast_cli(fd, ")\n");
02448 
02449       ast_cli(fd, "  Status       : ");
02450       peer_status(peer, status, sizeof(status));   
02451       ast_cli(fd, "%s\n",status);
02452       ast_cli(fd, "  Qualify      : every %dms when OK, every %dms when UNREACHABLE (sample smoothing %s)\n", peer->pokefreqok, peer->pokefreqnotok, peer->smoothing ? "On" : "Off");
02453       ast_cli(fd,"\n");
02454       peer_unref(peer);
02455    } else {
02456       ast_cli(fd,"Peer %s not found.\n", argv[3]);
02457       ast_cli(fd,"\n");
02458    }
02459 
02460    return RESULT_SUCCESS;
02461 }

static int iax2_show_peers ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 4828 of file chan_iax2.c.

References __iax2_show_peers().

04829 {
04830    return __iax2_show_peers(0, fd, NULL, argc, argv);
04831 }

static int iax2_show_registry ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 4900 of file chan_iax2.c.

References iax2_registry::addr, ast_cli(), ast_copy_string(), ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, iax2_registry::dnsmgr, FORMAT, FORMAT2, iax2_registry::refresh, iax2_registry::regstate, regstate2str(), RESULT_SHOWUSAGE, RESULT_SUCCESS, iax2_registry::us, and iax2_registry::username.

04901 {
04902 #define FORMAT2 "%-20.20s  %-6.6s  %-10.10s  %-20.20s %8.8s  %s\n"
04903 #define FORMAT  "%-20.20s  %-6.6s  %-10.10s  %-20.20s %8d  %s\n"
04904    struct iax2_registry *reg = NULL;
04905 
04906    char host[80];
04907    char perceived[80];
04908    if (argc != 3)
04909       return RESULT_SHOWUSAGE;
04910    ast_cli(fd, FORMAT2, "Host", "dnsmgr", "Username", "Perceived", "Refresh", "State");
04911    AST_LIST_LOCK(&registrations);
04912    AST_LIST_TRAVERSE(&registrations, reg, entry) {
04913       snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(reg->addr.sin_addr), ntohs(reg->addr.sin_port));
04914       if (reg->us.sin_addr.s_addr) 
04915          snprintf(perceived, sizeof(perceived), "%s:%d", ast_inet_ntoa(reg->us.sin_addr), ntohs(reg->us.sin_port));
04916       else
04917          ast_copy_string(perceived, "<Unregistered>", sizeof(perceived));
04918       ast_cli(fd, FORMAT, host, 
04919                (reg->dnsmgr) ? "Y" : "N", 
04920                reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
04921    }
04922    AST_LIST_UNLOCK(&registrations);
04923    return RESULT_SUCCESS;
04924 #undef FORMAT
04925 #undef FORMAT2
04926 }

static int iax2_show_stats ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 2488 of file chan_iax2.c.

References ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, iax_frame::final, iax_get_frames(), iax_get_iframes(), iax_get_oframes(), iaxq, ast_iax2_queue::queue, RESULT_SHOWUSAGE, RESULT_SUCCESS, and iax_frame::retries.

02489 {
02490    struct iax_frame *cur;
02491    int cnt = 0, dead=0, final=0;
02492 
02493    if (argc != 3)
02494       return RESULT_SHOWUSAGE;
02495 
02496    AST_LIST_LOCK(&iaxq.queue);
02497    AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
02498       if (cur->retries < 0)
02499          dead++;
02500       if (cur->final)
02501          final++;
02502       cnt++;
02503    }
02504    AST_LIST_UNLOCK(&iaxq.queue);
02505 
02506    ast_cli(fd, "    IAX Statistics\n");
02507    ast_cli(fd, "---------------------\n");
02508    ast_cli(fd, "Outstanding frames: %d (%d ingress, %d egress)\n", iax_get_frames(), iax_get_iframes(), iax_get_oframes());
02509    ast_cli(fd, "Packets in transmit queue: %d dead, %d final, %d total\n\n", dead, final, cnt);
02510    
02511    return RESULT_SUCCESS;
02512 }

static int iax2_show_threads ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 4769 of file chan_iax2.c.

References iax2_thread::actions, ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, iax2_thread::checktime, iax2_thread::curfunc, IAX_TYPE_DYNAMIC, iaxthreadcount, iax2_thread::iostate, RESULT_SHOWUSAGE, RESULT_SUCCESS, t, thread, iax2_thread::threadnum, and iax2_thread::type.

04770 {
04771    struct iax2_thread *thread = NULL;
04772    time_t t;
04773    int threadcount = 0, dynamiccount = 0;
04774    char type;
04775 
04776    if (argc != 3)
04777       return RESULT_SHOWUSAGE;
04778       
04779    ast_cli(fd, "IAX2 Thread Information\n");
04780    time(&t);
04781    ast_cli(fd, "Idle Threads:\n");
04782    AST_LIST_LOCK(&idle_list);
04783    AST_LIST_TRAVERSE(&idle_list, thread, list) {
04784 #ifdef DEBUG_SCHED_MULTITHREAD
04785       ast_cli(fd, "Thread %d: state=%d, update=%d, actions=%d, func ='%s'\n", 
04786          thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
04787 #else
04788       ast_cli(fd, "Thread %d: state=%d, update=%d, actions=%d\n", 
04789          thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
04790 #endif
04791       threadcount++;
04792    }
04793    AST_LIST_UNLOCK(&idle_list);
04794    ast_cli(fd, "Active Threads:\n");
04795    AST_LIST_LOCK(&active_list);
04796    AST_LIST_TRAVERSE(&active_list, thread, list) {
04797       if (thread->type == IAX_TYPE_DYNAMIC)
04798          type = 'D';
04799       else
04800          type = 'P';
04801 #ifdef DEBUG_SCHED_MULTITHREAD
04802       ast_cli(fd, "Thread %c%d: state=%d, update=%d, actions=%d, func ='%s'\n", 
04803          type, thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
04804 #else
04805       ast_cli(fd, "Thread %c%d: state=%d, update=%d, actions=%d\n", 
04806          type, thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
04807 #endif
04808       threadcount++;
04809    }
04810    AST_LIST_UNLOCK(&active_list);
04811    ast_cli(fd, "Dynamic Threads:\n");
04812         AST_LIST_LOCK(&dynamic_list);
04813         AST_LIST_TRAVERSE(&dynamic_list, thread, list) {
04814 #ifdef DEBUG_SCHED_MULTITHREAD
04815                 ast_cli(fd, "Thread %d: state=%d, update=%d, actions=%d, func ='%s'\n",
04816                         thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
04817 #else
04818                 ast_cli(fd, "Thread %d: state=%d, update=%d, actions=%d\n",
04819                         thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
04820 #endif
04821       dynamiccount++;
04822         }
04823         AST_LIST_UNLOCK(&dynamic_list);
04824    ast_cli(fd, "%d of %d threads accounted for with %d dynamic threads\n", threadcount, iaxthreadcount, dynamiccount);
04825    return RESULT_SUCCESS;
04826 }

static int iax2_show_users ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 4586 of file chan_iax2.c.

References ao2_iterator_init(), ao2_iterator_next(), ast_cli(), ast_copy_string(), ast_strlen_zero(), ast_test_flag, iax2_user::authmethods, iax2_context::context, iax2_user::contexts, FORMAT, FORMAT2, iax2_user::ha, IAX_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, iax2_user::inkeys, iax2_user::name, RESULT_SHOWUSAGE, RESULT_SUCCESS, iax2_user::secret, and user_unref().

04587 {
04588    regex_t regexbuf;
04589    int havepattern = 0;
04590 
04591 #define FORMAT "%-15.15s  %-20.20s  %-15.15s  %-15.15s  %-5.5s  %-5.10s\n"
04592 #define FORMAT2 "%-15.15s  %-20.20s  %-15.15d  %-15.15s  %-5.5s  %-5.10s\n"
04593 
04594    struct iax2_user *user = NULL;
04595    char auth[90];
04596    char *pstr = "";
04597    struct ao2_iterator i;
04598 
04599    switch (argc) {
04600    case 5:
04601       if (!strcasecmp(argv[3], "like")) {
04602          if (regcomp(&regexbuf, argv[4], REG_EXTENDED | REG_NOSUB))
04603             return RESULT_SHOWUSAGE;
04604          havepattern = 1;
04605       } else
04606          return RESULT_SHOWUSAGE;
04607    case 3:
04608       break;
04609    default:
04610       return RESULT_SHOWUSAGE;
04611    }
04612 
04613    ast_cli(fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C","Codec Pref");
04614    i = ao2_iterator_init(users, 0);
04615    for (user = ao2_iterator_next(&i); user; 
04616       user_unref(user), user = ao2_iterator_next(&i)) {
04617       if (havepattern && regexec(&regexbuf, user->name, 0, NULL, 0))
04618          continue;
04619       
04620       if (!ast_strlen_zero(user->secret)) {
04621          ast_copy_string(auth,user->secret,sizeof(auth));
04622       } else if (!ast_strlen_zero(user->inkeys)) {
04623          snprintf(auth, sizeof(auth), "Key: %-15.15s ", user->inkeys);
04624       } else
04625          ast_copy_string(auth, "-no secret-", sizeof(auth));
04626       
04627       if(ast_test_flag(user,IAX_CODEC_NOCAP))
04628          pstr = "REQ Only";
04629       else if(ast_test_flag(user,IAX_CODEC_NOPREFS))
04630          pstr = "Disabled";
04631       else
04632          pstr = ast_test_flag(user,IAX_CODEC_USER_FIRST) ? "Caller" : "Host";
04633       
04634       ast_cli(fd, FORMAT2, user->name, auth, user->authmethods, 
04635          user->contexts ? user->contexts->context : context,
04636          user->ha ? "Yes" : "No", pstr);
04637    }
04638 
04639    if (havepattern)
04640       regfree(&regexbuf);
04641 
04642    return RESULT_SUCCESS;
04643 #undef FORMAT
04644 #undef FORMAT2
04645 }

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

Definition at line 3568 of file chan_iax2.c.

References iax2_registry::addr, AST_FRAME_IAX, ast_random(), iax_ie_data::buf, 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_ie_data::pos, send_command(), TRANSFER_BEGIN, TRANSFER_MBEGIN, and chan_iax2_pvt::transferring.

Referenced by iax2_bridge().

03569 {
03570    int res;
03571    struct iax_ie_data ied0;
03572    struct iax_ie_data ied1;
03573    unsigned int transferid = (unsigned int)ast_random();
03574    memset(&ied0, 0, sizeof(ied0));
03575    iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr);
03576    iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno);
03577    iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, transferid);
03578 
03579    memset(&ied1, 0, sizeof(ied1));
03580    iax_ie_append_addr(&ied1, IAX_IE_APPARENT_ADDR, &iaxs[callno0]->addr);
03581    iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[callno0]->peercallno);
03582    iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
03583    
03584    res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
03585    if (res)
03586       return -1;
03587    res = send_command(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos, -1);
03588    if (res)
03589       return -1;
03590    iaxs[callno0]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
03591    iaxs[callno1]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
03592    return 0;
03593 }

static int iax2_test_losspct ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 2335 of file chan_iax2.c.

References RESULT_SHOWUSAGE, and RESULT_SUCCESS.

02336 {
02337        if (argc != 4)
02338                return RESULT_SHOWUSAGE;
02339 
02340        test_losspct = atoi(argv[3]);
02341 
02342        return RESULT_SUCCESS;
02343 }

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

Definition at line 3810 of file chan_iax2.c.

References ast_copy_string(), AST_FRAME_IAX, ast_log(), iax_ie_data::buf, iax2_registry::callno, IAX_COMMAND_TRANSFER, iax_ie_append_str(), IAX_IE_CALLED_CONTEXT, IAX_IE_CALLED_NUMBER, LOG_DEBUG, ast_channel::name, option_debug, iax_ie_data::pos, PTR_TO_CALLNO, send_command_locked(), and ast_channel::tech_pvt.

03811 {
03812    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
03813    struct iax_ie_data ied;
03814    char tmp[256], *context;
03815    ast_copy_string(tmp, dest, sizeof(tmp));
03816    context = strchr(tmp, '@');
03817    if (context) {
03818       *context = '\0';
03819       context++;
03820    }
03821    memset(&ied, 0, sizeof(ied));
03822    iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, tmp);
03823    if (context)
03824       iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, context);
03825    if (option_debug)
03826       ast_log(LOG_DEBUG, "Transferring '%s' to '%s'\n", c->name, dest);
03827    return send_command_locked(callno, AST_FRAME_IAX, IAX_COMMAND_TRANSFER, 0, ied.buf, ied.pos, -1);
03828 }

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

Definition at line 2806 of file chan_iax2.c.

References AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, AST_PTHREADT_NULL, ast_iax2_queue::count, iaxq, ast_iax2_queue::queue, sched_lock, iax_frame::sentyet, and signal_condition().

Referenced by iax2_send().

02807 {
02808    /* Lock the queue and place this packet at the end */
02809    /* By setting this to 0, the network thread will send it for us, and
02810       queue retransmission if necessary */
02811    fr->sentyet = 0;
02812    AST_LIST_LOCK(&iaxq.queue);
02813    AST_LIST_INSERT_TAIL(&iaxq.queue, fr, list);
02814    iaxq.count++;
02815    AST_LIST_UNLOCK(&iaxq.queue);
02816    /* Wake up the network and scheduler thread */
02817    if (netthreadid != AST_PTHREADT_NULL)
02818       pthread_kill(netthreadid, SIGURG);
02819    signal_condition(&sched_lock, &sched_cond);
02820    return 0;
02821 }

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

Definition at line 6651 of file chan_iax2.c.

References iax2_trunk_peer::trunkact.

Referenced by timing_read().

06652 {
06653    /* Drop when trunk is about 5 seconds idle */
06654    if (now->tv_sec > tpeer->trunkact.tv_sec + 5) 
06655       return 1;
06656    return 0;
06657 }

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

Definition at line 4175 of file chan_iax2.c.

References iax2_trunk_peer::addr, chan_iax2_pvt::addr, iax_frame::af, ast_inet_ntoa(), ast_log(), ast_mutex_unlock(), ast_realloc, ast_test_flag, 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_DEBUG, LOG_WARNING, MAX_TRUNKDATA, ast_iax2_meta_trunk_mini::mini, option_debug, 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().

04176 {
04177    struct ast_frame *f;
04178    struct iax2_trunk_peer *tpeer;
04179    void *tmp, *ptr;
04180    struct ast_iax2_meta_trunk_entry *met;
04181    struct ast_iax2_meta_trunk_mini *mtm;
04182 
04183    f = &fr->af;
04184    tpeer = find_tpeer(&pvt->addr, pvt->sockfd);
04185    if (tpeer) {
04186       if (tpeer->trunkdatalen + f->datalen + 4 >= tpeer->trunkdataalloc) {
04187          /* Need to reallocate space */
04188          if (tpeer->trunkdataalloc < MAX_TRUNKDATA) {
04189             if (!(tmp = ast_realloc(tpeer->trunkdata, tpeer->trunkdataalloc + DEFAULT_TRUNKDATA + IAX2_TRUNK_PREFACE))) {
04190                ast_mutex_unlock(&tpeer->lock);
04191                return -1;
04192             }
04193             
04194             tpeer->trunkdataalloc += DEFAULT_TRUNKDATA;
04195             tpeer->trunkdata = tmp;
04196             if (option_debug)
04197                ast_log(LOG_DEBUG, "Expanded trunk '%s:%d' to %d bytes\n", ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), tpeer->trunkdataalloc);
04198          } else {
04199             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));
04200             ast_mutex_unlock(&tpeer->lock);
04201             return -1;
04202          }
04203       }
04204 
04205       /* Append to meta frame */
04206       ptr = tpeer->trunkdata + IAX2_TRUNK_PREFACE + tpeer->trunkdatalen;
04207       if (ast_test_flag(&globalflags, IAX_TRUNKTIMESTAMPS)) {
04208          mtm = (struct ast_iax2_meta_trunk_mini *)ptr;
04209          mtm->len = htons(f->datalen);
04210          mtm->mini.callno = htons(pvt->callno);
04211          mtm->mini.ts = htons(0xffff & fr->ts);
04212          ptr += sizeof(struct ast_iax2_meta_trunk_mini);
04213          tpeer->trunkdatalen += sizeof(struct ast_iax2_meta_trunk_mini);
04214       } else {
04215          met = (struct ast_iax2_meta_trunk_entry *)ptr;
04216          /* Store call number and length in meta header */
04217          met->callno = htons(pvt->callno);
04218          met->len = htons(f->datalen);
04219          /* Advance pointers/decrease length past trunk entry header */
04220          ptr += sizeof(struct ast_iax2_meta_trunk_entry);
04221          tpeer->trunkdatalen += sizeof(struct ast_iax2_meta_trunk_entry);
04222       }
04223       /* Copy actual trunk data */
04224       memcpy(ptr, f->data, f->datalen);
04225       tpeer->trunkdatalen += f->datalen;
04226 
04227       tpeer->calls++;
04228       ast_mutex_unlock(&tpeer->lock);
04229    }
04230    return 0;
04231 }

static int iax2_vnak ( int  callno  )  [static]

Definition at line 6568 of file chan_iax2.c.

References AST_FRAME_IAX, IAX_COMMAND_VNAK, and send_command_immediate().

Referenced by socket_process().

06569 {
06570    return send_command_immediate(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_VNAK, 0, NULL, 0, iaxs[callno]->iseqno);
06571 }

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

Definition at line 5118 of file chan_iax2.c.

References AST_FRAME_NULL, AST_FRAME_VOICE, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_test_flag, iax2_registry::callno, errno, ast_frame::frametype, iax2_send(), IAX_ALREADYGONE, IAX_QUELCH, IAX_STATE_STARTED, LOG_DEBUG, option_debug, PTR_TO_CALLNO, and ast_channel::tech_pvt.

05119 {
05120    unsigned short callno = PTR_TO_CALLNO(c->tech_pvt);
05121    int res = -1;
05122    ast_mutex_lock(&iaxsl[callno]);
05123    if (iaxs[callno]) {
05124    /* If there's an outstanding error, return failure now */
05125       if (!iaxs[callno]->error) {
05126          if (ast_test_flag(iaxs[callno], IAX_ALREADYGONE))
05127             res = 0;
05128             /* Don't waste bandwidth sending null frames */
05129          else if (f->frametype == AST_FRAME_NULL)
05130             res = 0;
05131          else if ((f->frametype == AST_FRAME_VOICE) && ast_test_flag(iaxs[callno], IAX_QUELCH))
05132             res = 0;
05133          else if (!ast_test_flag(&iaxs[callno]->state, IAX_STATE_STARTED))
05134             res = 0;
05135          else
05136          /* Simple, just queue for transmission */
05137             res = iax2_send(iaxs[callno], f, 0, -1, 0, 0, 0);
05138       } else {
05139          if (option_debug)
05140             ast_log(LOG_DEBUG, "Write error: %s\n", strerror(errno));
05141       }
05142    }
05143    /* If it's already gone, just return */
05144    ast_mutex_unlock(&iaxsl[callno]);
05145    return res;
05146 }

static int iax_check_version ( char *  dev  )  [static]

Definition at line 1961 of file chan_iax2.c.

References ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), ast_iax2_firmware_header::devname, iax_firmware::fwh, ast_firmware_list::lock, iax_firmware::next, ast_iax2_firmware_header::version, ast_firmware_list::wares, and waresl.

Referenced by update_registry().

01962 {
01963    int res = 0;
01964    struct iax_firmware *cur;
01965    if (!ast_strlen_zero(dev)) {
01966       ast_mutex_lock(&waresl.lock);
01967       cur = waresl.wares;
01968       while(cur) {
01969          if (!strcmp(dev, (char *)cur->fwh->devname)) {
01970             res = ntohs(cur->fwh->version);
01971             break;
01972          }
01973          cur = cur->next;
01974       }
01975       ast_mutex_unlock(&waresl.lock);
01976    }
01977    return res;
01978 }

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

Definition at line 762 of file chan_iax2.c.

References ast_verbose().

Referenced by load_module().

00763 {
00764    if (iaxdebug)
00765       ast_verbose("%s", data);
00766 }

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

Definition at line 768 of file chan_iax2.c.

References ast_log(), and LOG_WARNING.

Referenced by load_module().

00769 {
00770    ast_log(LOG_WARNING, "%s", data);
00771 }

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

Definition at line 1980 of file chan_iax2.c.

References ast_mutex_lock(), ast_mutex_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, IAX_IE_FWBLOCKDESC, ast_firmware_list::lock, iax_firmware::next, ast_firmware_list::wares, and waresl.

Referenced by socket_process().

01981 {
01982    int res = -1;
01983    unsigned int bs = desc & 0xff;
01984    unsigned int start = (desc >> 8) & 0xffffff;
01985    unsigned int bytes;
01986    struct iax_firmware *cur;
01987    if (!ast_strlen_zero((char *)dev) && bs) {
01988       start *= bs;
01989       ast_mutex_lock(&waresl.lock);
01990       cur = waresl.wares;
01991       while(cur) {
01992          if (!strcmp((char *)dev, (char *)cur->fwh->devname)) {
01993             iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
01994             if (start < ntohl(cur->fwh->datalen)) {
01995                bytes = ntohl(cur->fwh->datalen) - start;
01996                if (bytes > bs)
01997                   bytes = bs;
01998                iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
01999             } else {
02000                bytes = 0;
02001                iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
02002             }
02003             if (bytes == bs)
02004                res = 0;
02005             else
02006                res = 1;
02007             break;
02008          }
02009          cur = cur->next;
02010       }
02011       ast_mutex_unlock(&waresl.lock);
02012    }
02013    return res;
02014 }

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

Definition at line 6839 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_hangup(), ast_log(), ast_pthread_create_background, AST_STATE_DOWN, iax_dual::chan1, iax_dual::chan2, ast_channel::context, ast_channel::exten, free, iax_park_thread(), LOG_WARNING, ast_channel::name, ast_channel::priority, ast_channel::readformat, and ast_channel::writeformat.

Referenced by socket_process().

06840 {
06841    struct iax_dual *d;
06842    struct ast_channel *chan1m, *chan2m;
06843    pthread_t th;
06844    chan1m = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan1->exten, chan1->context, chan1->amaflags, "Parking/%s", chan1->name);
06845    chan2m = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, chan2->accountcode, chan2->exten, chan2->context, chan2->amaflags, "IAXPeer/%s",chan2->name);
06846    if (chan2m && chan1m) {
06847       /* Make formats okay */
06848       chan1m->readformat = chan1->readformat;
06849       chan1m->writeformat = chan1->writeformat;
06850       ast_channel_masquerade(chan1m, chan1);
06851       /* Setup the extensions and such */
06852       ast_copy_string(chan1m->context, chan1->context, sizeof(chan1m->context));
06853       ast_copy_string(chan1m->exten, chan1->exten, sizeof(chan1m->exten));
06854       chan1m->priority = chan1->priority;
06855       
06856       /* We make a clone of the peer channel too, so we can play
06857          back the announcement */
06858       /* Make formats okay */
06859       chan2m->readformat = chan2->readformat;
06860       chan2m->writeformat = chan2->writeformat;
06861       ast_channel_masquerade(chan2m, chan2);
06862       /* Setup the extensions and such */
06863       ast_copy_string(chan2m->context, chan2->context, sizeof(chan2m->context));
06864       ast_copy_string(chan2m->exten, chan2->exten, sizeof(chan2m->exten));
06865       chan2m->priority = chan2->priority;
06866       if (ast_do_masquerade(chan2m)) {
06867          ast_log(LOG_WARNING, "Masquerade failed :(\n");
06868          ast_hangup(chan2m);
06869          return -1;
06870       }
06871    } else {
06872       if (chan1m)
06873          ast_hangup(chan1m);
06874       if (chan2m)
06875          ast_hangup(chan2m);
06876       return -1;
06877    }
06878    if ((d = ast_calloc(1, sizeof(*d)))) {
06879       pthread_attr_t attr;
06880 
06881       pthread_attr_init(&attr);
06882       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06883 
06884       d->chan1 = chan1m;
06885       d->chan2 = chan2m;
06886       if (!ast_pthread_create_background(&th, &attr, iax_park_thread, d)) {
06887          pthread_attr_destroy(&attr);
06888          return 0;
06889       }
06890       pthread_attr_destroy(&attr);
06891       free(d);
06892    }
06893    return -1;
06894 }

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

Definition at line 6819 of file chan_iax2.c.

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

Referenced by iax_park().

06820 {
06821    struct ast_channel *chan1, *chan2;
06822    struct iax_dual *d;
06823    struct ast_frame *f;
06824    int ext;
06825    int res;
06826    d = stuff;
06827    chan1 = d->chan1;
06828    chan2 = d->chan2;
06829    free(d);
06830    f = ast_read(chan1);
06831    if (f)
06832       ast_frfree(f);
06833    res = ast_park_call(chan1, chan2, 0, &ext);
06834    ast_hangup(chan2);
06835    ast_log(LOG_NOTICE, "Parked on extension '%d'\n", ext);
06836    return NULL;
06837 }

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

Definition at line 1436 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().

01437 {
01438    struct iax_frame *new = iax_frame_new(DIRECTION_INGRESS, fr->af.datalen, fr->cacheable);
01439    if (new) {
01440       size_t afdatalen = new->afdatalen;
01441       memcpy(new, fr, sizeof(*new));
01442       iax_frame_wrap(new, &fr->af);
01443       new->afdatalen = afdatalen;
01444       new->data = NULL;
01445       new->datalen = 0;
01446       new->direction = DIRECTION_INGRESS;
01447       new->retrans = -1;
01448    }
01449    return new;
01450 }

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

Definition at line 892 of file chan_iax2.c.

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

Referenced by iax2_process_thread().

00893 {
00894    if (thread->type == IAX_TYPE_DYNAMIC) {
00895       AST_LIST_LOCK(&dynamic_list);
00896       AST_LIST_INSERT_TAIL(&dynamic_list, thread, list);
00897       AST_LIST_UNLOCK(&dynamic_list);
00898    } else {
00899       AST_LIST_LOCK(&idle_list);
00900       AST_LIST_INSERT_TAIL(&idle_list, thread, list);
00901       AST_LIST_UNLOCK(&idle_list);
00902    }
00903 
00904    return;
00905 }

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

Definition at line 797 of file chan_iax2.c.

References ast_verbose().

Referenced by iax2_do_jb_debug(), and iax2_no_jb_debug().

00798 {
00799    va_list args;
00800    char buf[1024];
00801 
00802    va_start(args, fmt);
00803    vsnprintf(buf, 1024, fmt, args);
00804    va_end(args);
00805 
00806    ast_verbose("%s", buf);
00807 }

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

Definition at line 773 of file chan_iax2.c.

References ast_log(), and LOG_ERROR.

Referenced by iax2_do_jb_debug(), iax2_no_jb_debug(), and load_module().

00774 {
00775    va_list args;
00776    char buf[1024];
00777 
00778    va_start(args, fmt);
00779    vsnprintf(buf, 1024, fmt, args);
00780    va_end(args);
00781 
00782    ast_log(LOG_ERROR, "%s", buf);
00783 }

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

Definition at line 785 of file chan_iax2.c.

References ast_log(), and LOG_WARNING.

Referenced by iax2_do_jb_debug(), iax2_no_jb_debug(), and load_module().

00786 {
00787    va_list args;
00788    char buf[1024];
00789 
00790    va_start(args, fmt);
00791    vsnprintf(buf, 1024, fmt, args);
00792    va_end(args);
00793 
00794    ast_log(LOG_WARNING, "%s", buf);
00795 }

static int load_module ( void   )  [static]

Load IAX2 module, load configuraiton ---.

Definition at line 11220 of file chan_iax2.c.

References __unload_module(), ao2_callback(), ao2_container_alloc(), ao2_ref(), ARRAY_LEN, ast_channel_register(), ast_cli_register_multiple(), ast_cond_init(), ast_custom_function_register(), AST_LIST_HEAD_INIT, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_manager_register, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, ast_mutex_init(), ast_netsock_init(), ast_netsock_list_alloc(), ast_netsock_release(), ast_register_application(), ast_register_switch(), ast_verbose(), config, errno, iax2_do_register(), iax2_poke_peer_cb(), iax2_prov_app(), iax_debug_output(), iax_error_output(), IAX_MAX_CALLS, iax_provision_reload(), iax_set_error(), iax_set_output(), iaxq, io_context_create(), jb_error_output(), jb_setoutput(), jb_warning_output(), ast_firmware_list::lock, LOG_ERROR, LOG_WARNING, manager_iax2_show_netstats(), manager_iax2_show_peers(), MAX_PEER_BUCKETS, MAX_USER_BUCKETS, option_verbose, peer_cmp_cb(), peer_hash_cb(), peer_set_sock_cb(), pvt_cmp_cb(), pvt_hash_cb(), ast_iax2_queue::queue, reload_firmware(), sched_context_create(), set_config(), start_network_thread(), user_cmp_cb(), user_hash_cb(), VERBOSE_PREFIX_2, and waresl.

11221 {
11222    char *config = "iax.conf";
11223    int res = 0;
11224    int x;
11225    struct iax2_registry *reg = NULL;
11226 
11227    peers = ao2_container_alloc(MAX_PEER_BUCKETS, peer_hash_cb, peer_cmp_cb);
11228    if (!peers)
11229       return AST_MODULE_LOAD_FAILURE;
11230    users = ao2_container_alloc(MAX_USER_BUCKETS, user_hash_cb, user_cmp_cb);
11231    if (!users) {
11232       ao2_ref(peers, -1);
11233       return AST_MODULE_LOAD_FAILURE;
11234    }
11235    iax_peercallno_pvts = ao2_container_alloc(IAX_MAX_CALLS, pvt_hash_cb, pvt_cmp_cb);
11236    if (!iax_peercallno_pvts) {
11237       ao2_ref(peers, -1);
11238       ao2_ref(users, -1);
11239       return AST_MODULE_LOAD_FAILURE;
11240    }
11241 
11242    ast_custom_function_register(&iaxpeer_function);
11243 
11244    iax_set_output(iax_debug_output);
11245    iax_set_error(iax_error_output);
11246    jb_setoutput(jb_error_output, jb_warning_output, NULL);
11247    
11248 #ifdef HAVE_ZAPTEL
11249 #ifdef ZAPTEL_TIMERACK
11250    timingfd = open("/dev/zap/timer", O_RDWR);
11251    if (timingfd < 0)
11252 #endif
11253       timingfd = open("/dev/zap/pseudo", O_RDWR);
11254    if (timingfd < 0) 
11255       ast_log(LOG_WARNING, "Unable to open IAX timing interface: %s\n", strerror(errno));
11256 #elif defined(HAVE_DAHDI)
11257 #ifdef DAHDI_TIMERACK
11258    timingfd = open("/dev/dahdi/timer", O_RDWR);
11259    if (timingfd < 0)
11260 #endif
11261       timingfd = open("/dev/dahdi/pseudo", O_RDWR);
11262    if (timingfd < 0) 
11263       ast_log(LOG_WARNING, "Unable to open IAX timing interface: %s\n", strerror(errno));
11264 #endif
11265 
11266    memset(iaxs, 0, sizeof(iaxs));
11267 
11268    for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
11269       ast_mutex_init(&iaxsl[x]);
11270    }
11271    
11272    ast_cond_init(&sched_cond, NULL);
11273 
11274    io = io_context_create();
11275    sched = sched_context_create();
11276    
11277    if (!io || !sched) {
11278       ast_log(LOG_ERROR, "Out of memory\n");
11279       return -1;
11280    }
11281 
11282    netsock = ast_netsock_list_alloc();
11283    if (!netsock) {
11284       ast_log(LOG_ERROR, "Could not allocate netsock list.\n");
11285       return -1;
11286    }
11287    ast_netsock_init(netsock);
11288 
11289    outsock = ast_netsock_list_alloc();
11290    if (!outsock) {
11291       ast_log(LOG_ERROR, "Could not allocate outsock list.\n");
11292       return -1;
11293    }
11294    ast_netsock_init(outsock);
11295 
11296    ast_mutex_init(&waresl.lock);
11297 
11298    AST_LIST_HEAD_INIT(&iaxq.queue);
11299    
11300    ast_cli_register_multiple(cli_iax2, sizeof(cli_iax2) / sizeof(struct ast_cli_entry));
11301 
11302    ast_register_application(papp, iax2_prov_app, psyn, pdescrip);
11303    
11304    ast_manager_register( "IAXpeers", 0, manager_iax2_show_peers, "List IAX Peers" );
11305    ast_manager_register( "IAXnetstats", 0, manager_iax2_show_netstats, "Show IAX Netstats" );
11306 
11307    if(set_config(config, 0) == -1)
11308       return AST_MODULE_LOAD_DECLINE;
11309 
11310    if (ast_channel_register(&iax2_tech)) {
11311       ast_log(LOG_ERROR, "Unable to register channel class %s\n", "IAX2");
11312       __unload_module();
11313       return -1;
11314    }
11315 
11316    if (ast_register_switch(&iax2_switch)) 
11317       ast_log(LOG_ERROR, "Unable to register IAX switch\n");
11318 
11319    res = start_network_thread();
11320    if (!res) {
11321       if (option_verbose > 1) 
11322          ast_verbose(VERBOSE_PREFIX_2 "IAX Ready and Listening\n");
11323    } else {
11324       ast_log(LOG_ERROR, "Unable to start network thread\n");
11325       ast_netsock_release(netsock);
11326       ast_netsock_release(outsock);
11327    }
11328 
11329    AST_LIST_LOCK(&registrations);
11330    AST_LIST_TRAVERSE(&registrations, reg, entry)
11331       iax2_do_register(reg);
11332    AST_LIST_UNLOCK(&registrations); 
11333 
11334    ao2_callback(peers, 0, peer_set_sock_cb, NULL);
11335    ao2_callback(peers, 0, iax2_poke_peer_cb, NULL);
11336 
11337    reload_firmware(0);
11338    iax_provision_reload();
11339    return res;
11340 }

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

Definition at line 3595 of file chan_iax2.c.

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

Referenced by iax2_bridge().

03596 {
03597    ast_mutex_lock(&iaxsl[callno0]);
03598    while (ast_mutex_trylock(&iaxsl[callno1])) {
03599       DEADLOCK_AVOIDANCE(&iaxsl[callno0]);
03600    }
03601 }

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

References ARRAY_LEN, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_SCHED_DEL, chan_iax2_pvt::callno, iax2_sched_add(), chan_iax2_pvt::lagid, lastused, LOG_DEBUG, LOG_WARNING, MIN_REUSE_TIME, option_debug, chan_iax2_pvt::pingid, send_lagrq(), send_ping(), TRUNK_CALL_START, update_max_nontrunk(), and update_max_trunk().

Referenced by iax2_request(), and socket_process().

01491 {
01492    int x;
01493    int res= 0;
01494    struct timeval now;
01495    if (iaxs[callno]->oseqno) {
01496       ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
01497       return -1;
01498    }
01499    if (callno & TRUNK_CALL_START) {
01500       ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
01501       return -1;
01502    }
01503    gettimeofday(&now, NULL);
01504    for (x = TRUNK_CALL_START; x < ARRAY_LEN(iaxs) - 1; x++) {
01505       ast_mutex_lock(&iaxsl[x]);
01506       if (!iaxs[x] && ((now.tv_sec - lastused[x].tv_sec) > MIN_REUSE_TIME)) {
01507          /* Update the two timers that should have been started */
01508          /*!
01509           * \note We delete these before switching the slot, because if
01510           * they fire in the meantime, they will generate a warning.
01511           */
01512          AST_SCHED_DEL(sched, iaxs[callno]->pingid);
01513          AST_SCHED_DEL(sched, iaxs[callno]->lagid);
01514          iaxs[x] = iaxs[callno];
01515          iaxs[x]->callno = x;
01516          iaxs[callno] = NULL;
01517          iaxs[x]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
01518          iaxs[x]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
01519          if (locked)
01520             ast_mutex_unlock(&iaxsl[callno]);
01521          res = x;
01522          if (!locked)
01523             ast_mutex_unlock(&iaxsl[x]);
01524          break;
01525       }
01526       ast_mutex_unlock(&iaxsl[x]);
01527    }
01528    if (x >= ARRAY_LEN(iaxs) - 1) {
01529       ast_log(LOG_WARNING, "Unable to trunk call: Insufficient space\n");
01530       return -1;
01531    }
01532    if (option_debug)
01533       ast_log(LOG_DEBUG, "Made call %d into trunk call %d\n", callno, x);
01534    /* We move this call from a non-trunked to a trunked call */
01535    update_max_trunk();
01536    update_max_nontrunk();
01537    return res;
01538 }

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

Definition at line 4832 of file chan_iax2.c.

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

Referenced by load_module().

04833 {
04834    ast_cli_netstats(s, -1, 0);
04835    astman_append(s, "\r\n");
04836    return RESULT_SUCCESS;
04837 }

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

Definition at line 4865 of file chan_iax2.c.

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

Referenced by load_module().

04866 {
04867    char *a[] = { "iax2", "show", "users" };
04868    int ret;
04869    const char *id = astman_get_header(m,"ActionID");
04870 
04871    if (!ast_strlen_zero(id))
04872       astman_append(s, "ActionID: %s\r\n",id);
04873    ret = __iax2_show_peers(1, -1, s, 3, a );
04874    astman_append(s, "\r\n\r\n" );
04875    return ret;
04876 } /* /JDG */

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

Definition at line 1456 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 __find_callno(), ao2_callback(), ast_parse_device_state(), check_blacklist(), find_command(), get_sip_pvt_byid_locked(), handle_updates(), pbx_find_extension(), pvt_cmp_cb(), realtime_switch_common(), and softhangup_exec().

01457 {
01458    if ((cur->addr.sin_addr.s_addr == sin->sin_addr.s_addr) &&
01459       (cur->addr.sin_port == sin->sin_port)) {
01460       /* This is the main host */
01461       if ( (cur->peercallno == 0 || cur->peercallno == callno) &&
01462           (check_dcallno ? dcallno == cur->callno : 1) ) {
01463          /* That's us.  Be sure we keep track of the peer call number */
01464          return 1;
01465       }
01466    }
01467    if ((cur->transfer.sin_addr.s_addr == sin->sin_addr.s_addr) &&
01468        (cur->transfer.sin_port == sin->sin_port) && (cur->transferring)) {
01469       /* We're transferring */
01470       if ((dcallno == cur->callno) || (cur->transferring == TRANSFER_MEDIAPASS && cur->transfercallno == callno))
01471          return 1;
01472    }
01473    return 0;
01474 }

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

Definition at line 4239 of file chan_iax2.c.

References aes_decrypt(), ast_log(), and LOG_WARNING.

Referenced by decode_frame().

04240 {
04241 #if 0
04242    /* Debug with "fake encryption" */
04243    int x;
04244    if (len % 16)
04245       ast_log(LOG_WARNING, "len should be multiple of 16, not %d!\n", len);
04246    for (x=0;x<len;x++)
04247       dst[x] = src[x] ^ 0xff;
04248 #else 
04249    unsigned char lastblock[16] = { 0 };
04250    int x;
04251    while(len > 0) {
04252       aes_decrypt(src, dst, dcx);
04253       for (x=0;x<16;x++)
04254          dst[x] ^= lastblock[x];
04255       memcpy(lastblock, src, sizeof(lastblock));
04256       dst += 16;
04257       src += 16;
04258       len -= 16;
04259    }
04260 #endif
04261 }

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

Definition at line 4263 of file chan_iax2.c.

References aes_encrypt(), ast_log(), and LOG_WARNING.

Referenced by encrypt_frame().

04264 {
04265 #if 0
04266    /* Debug with "fake encryption" */
04267    int x;
04268    if (len % 16)
04269       ast_log(LOG_WARNING, "len should be multiple of 16, not %d!\n", len);
04270    for (x=0;x<len;x++)
04271       dst[x] = src[x] ^ 0xff;
04272 #else
04273    unsigned char curblock[16] = { 0 };
04274    int x;
04275    while(len > 0) {
04276       for (x=0;x<16;x++)
04277          curblock[x] ^= src[x];
04278       aes_encrypt(curblock, dst, ecx);
04279       memcpy(curblock, dst, sizeof(curblock)); 
04280       dst += 16;
04281       src += 16;
04282       len -= 16;
04283    }
04284 #endif
04285 }

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

Definition at line 5440 of file chan_iax2.c.

References chan_iax2_pvt::encmethods, and IAX_ENCRYPT_AES128.

Referenced by authenticate_reply(), and socket_process().

05441 {
05442    /* Select exactly one common encryption if there are any */
05443    p->encmethods &= enc;
05444    if (p->encmethods) {
05445       if (p->encmethods & IAX_ENCRYPT_AES128)
05446          p->encmethods = IAX_ENCRYPT_AES128;
05447       else
05448          p->encmethods = 0;
05449    }
05450 }

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

Definition at line 9192 of file chan_iax2.c.

References 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_log(), ast_mutex_trylock(), ast_mutex_unlock(), attempt_transmit(), iax_frame::callno, ast_iax2_queue::count, f, iax2_sched_add(), iax_frame_free(), iaxq, LOG_DEBUG, option_debug, ast_iax2_queue::queue, iax_frame::retrans, iax_frame::retries, iax_frame::retrytime, send_packet(), iax_frame::sentyet, and timing_read().

Referenced by start_network_thread().

09193 {
09194    /* Our job is simple: Send queued messages, retrying if necessary.  Read frames 
09195       from the network, and queue them for delivery to the channels */
09196    int res, count, wakeup;
09197    struct iax_frame *f;
09198 
09199    if (timingfd > -1)
09200       ast_io_add(io, timingfd, timing_read, AST_IO_IN | AST_IO_PRI, NULL);
09201    
09202    for(;;) {
09203       pthread_testcancel();
09204 
09205       /* Go through the queue, sending messages which have not yet been
09206          sent, and scheduling retransmissions if appropriate */
09207       AST_LIST_LOCK(&iaxq.queue);
09208       count = 0;
09209       wakeup = -1;
09210       AST_LIST_TRAVERSE_SAFE_BEGIN(&iaxq.queue, f, list) {
09211          if (f->sentyet)
09212             continue;
09213          
09214          /* Try to lock the pvt, if we can't... don't fret - defer it till later */
09215          if (ast_mutex_trylock(&iaxsl[f->callno])) {
09216             wakeup = 1;
09217             continue;
09218          }
09219 
09220          f->sentyet++;
09221 
09222          if (iaxs[f->callno]) {
09223             send_packet(f);
09224             count++;
09225          } 
09226 
09227          ast_mutex_unlock(&iaxsl[f->callno]);
09228 
09229          if (f->retries < 0) {
09230             /* This is not supposed to be retransmitted */
09231             AST_LIST_REMOVE_CURRENT(&iaxq.queue, list);
09232             iaxq.count--;
09233             /* Free the iax frame */
09234             iax_frame_free(f);
09235          } else {
09236             /* We need reliable delivery.  Schedule a retransmission */
09237             f->retries++;
09238             f->retrans = iax2_sched_add(sched, f->retrytime, attempt_transmit, f);
09239          }
09240       }
09241       AST_LIST_TRAVERSE_SAFE_END
09242       AST_LIST_UNLOCK(&iaxq.queue);
09243 
09244       pthread_testcancel();
09245 
09246       if (option_debug && count >= 20)
09247          ast_log(LOG_DEBUG, "chan_iax2: Sent %d queued outbound frames all at once\n", count);
09248 
09249       /* Now do the IO, and run scheduled tasks */
09250       res = ast_io_wait(io, wakeup);
09251       if (res >= 0) {
09252          if (option_debug && res >= 20)
09253             ast_log(LOG_DEBUG, "chan_iax2: ast_io_wait ran %d I/Os all at once\n", res);
09254       }
09255    }
09256    return NULL;
09257 }

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

Definition at line 1397 of file chan_iax2.c.

References ao2_alloc(), ao2_ref(), ast_string_field_init, ast_string_field_set, chan_iax2_pvt::authid, chan_iax2_pvt::autoid, chan_iax2_pvt::bridgecallno, chan_iax2_pvt::callno, exten, chan_iax2_pvt::initid, chan_iax2_pvt::jb, jb_new(), jb_setconf(), chan_iax2_pvt::jbid, chan_iax2_pvt::lagid, jb_conf::max_contig_interp, jb_conf::max_jitterbuf, chan_iax2_pvt::peercallno, chan_iax2_pvt::pingid, prefs, chan_iax2_pvt::prefs, pvt_destructor(), jb_conf::resync_threshold, and chan_iax2_pvt::transfercallno.

Referenced by __find_callno().

01398 {
01399    struct chan_iax2_pvt *tmp;
01400    jb_conf jbconf;
01401 
01402    if (!(tmp = ao2_alloc(sizeof(*tmp), pvt_destructor))) {
01403       return NULL;
01404    }
01405 
01406    if (ast_string_field_init(tmp, 32)) {
01407       ao2_ref(tmp, -1);
01408       tmp = NULL;
01409       return NULL;
01410    }
01411       
01412    tmp->prefs = prefs;
01413    tmp->callno = 0;
01414    tmp->peercallno = 0;
01415    tmp->transfercallno = 0;
01416    tmp->bridgecallno = 0;
01417    tmp->pingid = -1;
01418    tmp->lagid = -1;
01419    tmp->autoid = -1;
01420    tmp->authid = -1;
01421    tmp->initid = -1;
01422 
01423    ast_string_field_set(tmp,exten, "s");
01424    ast_string_field_set(tmp,host, host);
01425 
01426    tmp->jb = jb_new();
01427    tmp->jbid = -1;
01428    jbconf.max_jitterbuf = maxjitterbuffer;
01429    jbconf.resync_threshold = resyncthreshold;
01430    jbconf.max_contig_interp = maxjitterinterps;
01431    jb_setconf(tmp->jb,&jbconf);
01432 
01433    return tmp;
01434 }

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 3266 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, and parsed_dial_string::username.

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

03267 {
03268    if (ast_strlen_zero(data))
03269       return;
03270 
03271    pds->peer = strsep(&data, "/");
03272    pds->exten = strsep(&data, "/");
03273    pds->options = data;
03274 
03275    if (pds->exten) {
03276       data = pds->exten;
03277       pds->exten = strsep(&data, "@");
03278       pds->context = data;
03279    }
03280 
03281    if (strchr(pds->peer, '@')) {
03282       data = pds->peer;
03283       pds->username = strsep(&data, "@");
03284       pds->peer = data;
03285    }
03286 
03287    if (pds->username) {
03288       data = pds->username;
03289       pds->username = strsep(&data, ":");
03290       pds->password = data;
03291    }
03292 
03293    data = pds->peer;
03294    pds->peer = strsep(&data, ":");
03295    pds->port = data;
03296 
03297    /* check for a key name wrapped in [] in the secret position, if found,
03298       move it to the key field instead
03299    */
03300    if (pds->password && (pds->password[0] == '[')) {
03301       pds->key = ast_strip_quoted(pds->password, "[", "]");
03302       pds->password = NULL;
03303    }
03304 }

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

References iax2_peer::name.

Referenced by load_module().

01120 {
01121    struct iax2_peer *peer = obj, *peer2 = arg;
01122 
01123    return !strcmp(peer->name, peer2->name) ? CMP_MATCH | CMP_STOP : 0;
01124 }

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

Definition at line 9882 of file chan_iax2.c.

References ast_set_flag, and IAX_DELME.

Referenced by delete_users().

09883 {
09884    struct iax2_peer *peer = obj;
09885 
09886    ast_set_flag(peer, IAX_DELME);
09887 
09888    return 0;
09889 }

static void peer_destructor ( void *  obj  )  [static]

Definition at line 9415 of file chan_iax2.c.

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

Referenced by build_peer().

09416 {
09417    struct iax2_peer *peer = obj;
09418    int callno = peer->callno;
09419 
09420    ast_free_ha(peer->ha);
09421 
09422    if (callno > 0) {
09423       ast_mutex_lock(&iaxsl[callno]);
09424       iax2_destroy(callno);
09425       ast_mutex_unlock(&iaxsl[callno]);
09426    }
09427 
09428    register_peer_exten(peer, 0);
09429 
09430    if (peer->dnsmgr)
09431       ast_dnsmgr_release(peer->dnsmgr);
09432 
09433    ast_string_field_free_memory(peer);
09434 }

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

References ast_str_hash(), and iax2_peer::name.

Referenced by load_module().

01110 {
01111    const struct iax2_peer *peer = obj;
01112 
01113    return ast_str_hash(peer->name);
01114 }

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

Definition at line 1166 of file chan_iax2.c.

References ao2_ref().

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

01167 {
01168    ao2_ref(peer, +1);
01169    return peer;
01170 }

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

Definition at line 11191 of file chan_iax2.c.

References iax2_peer::sockfd.

Referenced by load_module().

11192 {
11193    struct iax2_peer *peer = obj;
11194 
11195    if (peer->sockfd < 0)
11196       peer->sockfd = defaultsockfd;
11197 
11198    return 0;
11199 }

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

References iax2_registry::addr, 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_DEBUG, LOG_WARNING, iax2_peer::name, option_debug, socket_read(), and iax2_peer::sockfd.

Referenced by build_peer().

09343 {
09344    struct sockaddr_in sin;
09345    int nonlocal = 1;
09346    int port = IAX_DEFAULT_PORTNO;
09347    int sockfd = defaultsockfd;
09348    char *tmp;
09349    char *addr;
09350    char *portstr;
09351 
09352    if (!(tmp = ast_strdupa(srcaddr)))
09353       return -1;
09354 
09355    addr = strsep(&tmp, ":");
09356    portstr = tmp;
09357 
09358    if (portstr) {
09359       port = atoi(portstr);
09360       if (port < 1)
09361          port = IAX_DEFAULT_PORTNO;
09362    }
09363    
09364    if (!ast_get_ip(&sin, addr)) {
09365       struct ast_netsock *sock;
09366       int res;
09367 
09368       sin.sin_port = 0;
09369       sin.sin_family = AF_INET;
09370       res = check_srcaddr((struct sockaddr *) &sin, sizeof(sin));
09371       if (res == 0) {
09372          /* ip address valid. */
09373          sin.sin_port = htons(port);
09374          if (!(sock = ast_netsock_find(netsock, &sin)))
09375             sock = ast_netsock_find(outsock, &sin);
09376          if (sock) {
09377             sockfd = ast_netsock_sockfd(sock);
09378             nonlocal = 0;
09379          } else {
09380             unsigned int orig_saddr = sin.sin_addr.s_addr;
09381             /* INADDR_ANY matches anyway! */
09382             sin.sin_addr.s_addr = INADDR_ANY;
09383             if (ast_netsock_find(netsock, &sin)) {
09384                sin.sin_addr.s_addr = orig_saddr;
09385                sock = ast_netsock_bind(outsock, io, srcaddr, port, tos, socket_read, NULL);
09386                if (sock) {
09387                   sockfd = ast_netsock_sockfd(sock);
09388                   ast_netsock_unref(sock);
09389                   nonlocal = 0;
09390                } else {
09391                   nonlocal = 2;
09392                }
09393             }
09394          }
09395       }
09396    }
09397       
09398    peer->sockfd = sockfd;
09399 
09400    if (nonlocal == 1) {
09401       ast_log(LOG_WARNING, "Non-local or unbound address specified (%s) in sourceaddress for '%s', reverting to default\n",
09402          srcaddr, peer->name);
09403       return -1;
09404         } else if (nonlocal == 2) {
09405       ast_log(LOG_WARNING, "Unable to bind to sourceaddress '%s' for '%s', reverting to default\n",
09406          srcaddr, peer->name);
09407          return -1;
09408    } else {
09409       if (option_debug)
09410          ast_log(LOG_DEBUG, "Using sourceaddress %s for '%s'\n", srcaddr, peer->name);
09411       return 0;
09412    }
09413 }

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

peer_status: Report Peer status in character string

Definition at line 2381 of file chan_iax2.c.

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

Referenced by __iax2_show_peers(), function_iaxpeer(), and iax2_show_peer().

02382 {
02383    int res = 0;
02384    if (peer->maxms) {
02385       if (peer->lastms < 0) {
02386          ast_copy_string(status, "UNREACHABLE", statuslen);
02387       } else if (peer->lastms > peer->maxms) {
02388          snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
02389          res = 1;
02390       } else if (peer->lastms) {
02391          snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
02392          res = 1;
02393       } else {
02394          ast_copy_string(status, "UNKNOWN", statuslen);
02395       }
02396    } else { 
02397       ast_copy_string(status, "Unmonitored", statuslen);
02398       res = -1;
02399    }
02400    return res;
02401 }

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

static void poke_all_peers ( void   )  [static]

Definition at line 10344 of file chan_iax2.c.

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

Referenced by reload_config().

10345 {
10346    struct ao2_iterator i;
10347    struct iax2_peer *peer;
10348 
10349    i = ao2_iterator_init(peers, 0);
10350    while ((peer = ao2_iterator_next(&i))) {
10351       iax2_poke_peer(peer, 0);
10352       peer_unref(peer);
10353    }
10354 }

static void prune_peers ( void   )  [static]

Definition at line 9941 of file chan_iax2.c.

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

Referenced by reload_config().

09942 {
09943    struct iax2_peer *peer;
09944    struct ao2_iterator i;
09945 
09946    i = ao2_iterator_init(peers, 0);
09947    while ((peer = ao2_iterator_next(&i))) {
09948       if (ast_test_flag(peer, IAX_DELME))
09949          unlink_peer(peer);
09950       peer_unref(peer);
09951    }
09952 }

static void prune_users ( void   )  [static]

Definition at line 9927 of file chan_iax2.c.

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

Referenced by reload_config().

09928 {
09929    struct iax2_user *user;
09930    struct ao2_iterator i;
09931 
09932    i = ao2_iterator_init(users, 0);
09933    while ((user = ao2_iterator_next(&i))) {
09934       if (ast_test_flag(user, IAX_DELME))
09935          ao2_unlink(users, user);
09936       user_unref(user);
09937    }
09938 }

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

Definition at line 11208 of file chan_iax2.c.

References chan_iax2_pvt::frames_received, and match().

Referenced by load_module().

11209 {
11210    struct chan_iax2_pvt *pvt = obj, *pvt2 = arg;
11211 
11212    /* The frames_received field is used to hold whether we're matching
11213     * against a full frame or not ... */
11214 
11215    return match(&pvt2->addr, pvt2->peercallno, pvt2->callno, pvt, 
11216       pvt2->frames_received) ? CMP_MATCH | CMP_STOP : 0;
11217 }

static void pvt_destructor ( void *  obj  )  [static]

Definition at line 1358 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_set_flag, ast_string_field_free_memory, ast_variables_destroy(), iax2_registry::callno, chan_iax2_pvt::callno, iax_frame::callno, jb_frame::data, iax2_destroy_helper(), iax2_frame_free(), IAX_ALREADYGONE, iaxq, chan_iax2_pvt::jb, jb_destroy(), jb_getall(), JB_OK, chan_iax2_pvt::owner, ast_iax2_queue::queue, chan_iax2_pvt::reg, iax_frame::retries, and chan_iax2_pvt::vars.

Referenced by new_iax().

01359 {
01360    struct chan_iax2_pvt *pvt = obj;
01361    struct iax_frame *cur = NULL;
01362 
01363    iax2_destroy_helper(pvt);
01364 
01365    /* Already gone */
01366    ast_set_flag(pvt, IAX_ALREADYGONE); 
01367 
01368    AST_LIST_LOCK(&iaxq.queue);
01369    AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
01370       /* Cancel any pending transmissions */
01371       if (cur->callno == pvt->callno) { 
01372          cur->retries = -1;
01373       }
01374    }
01375    AST_LIST_UNLOCK(&iaxq.queue);
01376 
01377    if (pvt->reg) {
01378       pvt->reg->callno = 0;
01379    }
01380 
01381    if (!pvt->owner) {
01382       jb_frame frame;
01383       if (pvt->vars) {
01384           ast_variables_destroy(pvt->vars);
01385           pvt->vars = NULL;
01386       }
01387 
01388       while (jb_getall(pvt->jb, &frame) == JB_OK) {
01389          iax2_frame_free(frame.data);
01390       }
01391 
01392       jb_destroy(pvt->jb);
01393       ast_string_field_free_memory(pvt);
01394    }
01395 }

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

Definition at line 11201 of file chan_iax2.c.

References chan_iax2_pvt::peercallno.

Referenced by load_module().

11202 {
11203    const struct chan_iax2_pvt *pvt = obj;
11204 
11205    return pvt->peercallno;
11206 }

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

Definition at line 5422 of file chan_iax2.c.

References AST_FRAME_IAX, ast_inet_ntoa(), ast_log(), compress_subclass(), ast_iax2_full_hdr::csub, ast_iax2_full_hdr::dcallno, IAX_COMMAND_INVAL, IAX_FLAG_FULL, iax_showframe(), ast_iax2_full_hdr::iseqno, LOG_DEBUG, 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().

05423 {
05424    struct ast_iax2_full_hdr fh;
05425    fh.scallno = htons(src | IAX_FLAG_FULL);
05426    fh.dcallno = htons(dst);
05427    fh.ts = 0;
05428    fh.oseqno = 0;
05429    fh.iseqno = 0;
05430    fh.type = AST_FRAME_IAX;
05431    fh.csub = compress_subclass(IAX_COMMAND_INVAL);
05432    if (iaxdebug)
05433        iax_showframe(NULL, &fh, 0, sin, 0);
05434    if (option_debug)
05435       ast_log(LOG_DEBUG, "Raw Hangup %s:%d, src=%d, dst=%d\n",
05436          ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), src, dst);
05437    return sendto(sockfd, &fh, sizeof(fh), 0, (struct sockaddr *)sin, sizeof(*sin));
05438 }

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

References iax2_peer::addr, ast_copy_flags, ast_get_time_t(), ast_gethostbyname(), ast_inet_ntoa(), ast_load_realtime(), ast_log(), ast_sched_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, LOG_DEBUG, iax2_peer::name, ast_variable::name, ast_variable::next, option_debug, peer_ref(), peer_unref(), realtime_update_peer(), reg_source_db(), ast_variable::value, and var.

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

02869 {
02870    struct ast_variable *var = NULL;
02871    struct ast_variable *tmp;
02872    struct iax2_peer *peer=NULL;
02873    time_t regseconds = 0, nowtime;
02874    int dynamic=0;
02875 
02876    if (peername) {
02877       var = ast_load_realtime("iaxpeers", "name", peername, "host", "dynamic", NULL);
02878       if (!var && sin)
02879          var = ast_load_realtime("iaxpeers", "name", peername, "host", ast_inet_ntoa(sin->sin_addr), NULL);
02880    } else if (sin) {
02881       char porta[25];
02882       sprintf(porta, "%d", ntohs(sin->sin_port));
02883       var = ast_load_realtime("iaxpeers", "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", porta, NULL);
02884       if (var) {
02885          /* We'll need the peer name in order to build the structure! */
02886          for (tmp = var; tmp; tmp = tmp->next) {
02887             if (!strcasecmp(tmp->name, "name"))
02888                peername = tmp->value;
02889          }
02890       }
02891    }
02892    if (!var && peername) { /* Last ditch effort */
02893       var = ast_load_realtime("iaxpeers", "name", peername, NULL);
02894       /*!\note
02895        * If this one loaded something, then we need to ensure that the host
02896        * field matched.  The only reason why we can't have this as a criteria
02897        * is because we only have the IP address and the host field might be
02898        * set as a name (and the reverse PTR might not match).
02899        */
02900       if (var && sin) {
02901          for (tmp = var; tmp; tmp = tmp->next) {
02902             if (!strcasecmp(tmp->name, "host")) {
02903                struct ast_hostent ahp;
02904                struct hostent *hp;
02905                if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(&hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
02906                   /* No match */
02907                   ast_variables_destroy(var);
02908                   var = NULL;
02909                }
02910                break;
02911             }
02912          }
02913       }
02914    }
02915    if (!var)
02916       return NULL;
02917 
02918    peer = build_peer(peername, var, NULL, ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS) ? 0 : 1);
02919    
02920    if (!peer) {
02921       ast_variables_destroy(var);
02922       return NULL;
02923    }
02924 
02925    for (tmp = var; tmp; tmp = tmp->next) {
02926       /* Make sure it's not a user only... */
02927       if (!strcasecmp(tmp->name, "type")) {
02928          if (strcasecmp(tmp->value, "friend") &&
02929              strcasecmp(tmp->value, "peer")) {
02930             /* Whoops, we weren't supposed to exist! */
02931             peer = peer_unref(peer);
02932             break;
02933          } 
02934       } else if (!strcasecmp(tmp->name, "regseconds")) {
02935          ast_get_time_t(tmp->value, &regseconds, 0, NULL);
02936       } else if (!strcasecmp(tmp->name, "ipaddr")) {
02937          inet_aton(tmp->value, &(peer->addr.sin_addr));
02938       } else if (!strcasecmp(tmp->name, "port")) {
02939          peer->addr.sin_port = htons(atoi(tmp->value));
02940       } else if (!strcasecmp(tmp->name, "host")) {
02941          if (!strcasecmp(tmp->value, "dynamic"))
02942             dynamic = 1;
02943       }
02944    }
02945 
02946    ast_variables_destroy(var);
02947 
02948    if (!peer)
02949       return NULL;
02950 
02951    if (ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS)) {
02952       ast_copy_flags(peer, &globalflags, IAX_RTAUTOCLEAR|IAX_RTCACHEFRIENDS);
02953       if (ast_test_flag(peer, IAX_RTAUTOCLEAR)) {
02954          if (peer->expire > -1) {
02955             if (!ast_sched_del(sched, peer->expire)) {
02956                peer->expire = -1;
02957                peer_unref(peer);
02958             }
02959          }
02960          peer->expire = iax2_sched_add(sched, (global_rtautoclear) * 1000, expire_registry, peer_ref(peer));
02961          if (peer->expire == -1)
02962             peer_unref(peer);
02963       }
02964       ao2_link(peers, peer);
02965       if (ast_test_flag(peer, IAX_DYNAMIC))
02966          reg_source_db(peer);
02967    } else {
02968       ast_set_flag(peer, IAX_TEMPONLY);   
02969    }
02970 
02971    if (!ast_test_flag(&globalflags, IAX_RTIGNOREREGEXPIRE) && dynamic) {
02972       time(&nowtime);
02973       if ((nowtime - regseconds) > IAX_DEFAULT_REG_EXPIRE) {
02974          memset(&peer->addr, 0, sizeof(peer->addr));
02975          realtime_update_peer(peer->name, &peer->addr, 0);
02976          if (option_debug)
02977             ast_log(LOG_DEBUG, "realtime_peer: Bah, '%s' is expired (%d/%d/%d)!\n",
02978                   peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
02979       }
02980       else {
02981          if (option_debug)
02982             ast_log(LOG_DEBUG, "realtime_peer: Registration for '%s' still active (%d/%d/%d)!\n",
02983                   peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
02984       }
02985    }
02986 
02987    return peer;
02988 }

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

Definition at line 3061 of file chan_iax2.c.

References ast_inet_ntoa(), and ast_update_realtime().

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

03062 {
03063    char port[10];
03064    char regseconds[20];
03065    
03066    snprintf(regseconds, sizeof(regseconds), "%d", (int)regtime);
03067    snprintf(port, sizeof(port), "%d", ntohs(sin->sin_port));
03068    ast_update_realtime("iaxpeers", "name", peername, 
03069       "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", port, 
03070       "regseconds", regseconds, NULL);
03071 }

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

References 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, ast_variable::value, and var.

Referenced by check_access().

02991 {
02992    struct ast_variable *var;
02993    struct ast_variable *tmp;
02994    struct iax2_user *user=NULL;
02995 
02996    var = ast_load_realtime("iaxusers", "name", username, "host", "dynamic", NULL);
02997    if (!var)
02998       var = ast_load_realtime("iaxusers", "name", username, "host", ast_inet_ntoa(sin->sin_addr), NULL);
02999    if (!var && sin) {
03000       char porta[6];
03001       snprintf(porta, sizeof(porta), "%d", ntohs(sin->sin_port));
03002       var = ast_load_realtime("iaxusers", "name", username, "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", porta, NULL);
03003       if (!var)
03004          var = ast_load_realtime("iaxusers", "ipaddr", ast_inet_ntoa(sin->sin_addr), "port", porta, NULL);
03005    }
03006    if (!var) { /* Last ditch effort */
03007       var = ast_load_realtime("iaxusers", "name", username, NULL);
03008       /*!\note
03009        * If this one loaded something, then we need to ensure that the host
03010        * field matched.  The only reason why we can't have this as a criteria
03011        * is because we only have the IP address and the host field might be
03012        * set as a name (and the reverse PTR might not match).
03013        */
03014       if (var) {
03015          for (tmp = var; tmp; tmp = tmp->next) {
03016             if (!strcasecmp(tmp->name, "host")) {
03017                struct ast_hostent ahp;
03018                struct hostent *hp;
03019                if (!(hp = ast_gethostbyname(tmp->value, &ahp)) || (memcmp(&hp->h_addr, &sin->sin_addr, sizeof(hp->h_addr)))) {
03020                   /* No match */
03021                   ast_variables_destroy(var);
03022                   var = NULL;
03023                }
03024                break;
03025             }
03026          }
03027       }
03028    }
03029    if (!var)
03030       return NULL;
03031 
03032    tmp = var;
03033    while(tmp) {
03034       /* Make sure it's not a peer only... */
03035       if (!strcasecmp(tmp->name, "type")) {
03036          if (strcasecmp(tmp->value, "friend") &&
03037              strcasecmp(tmp->value, "user")) {
03038             return NULL;
03039          } 
03040       }
03041       tmp = tmp->next;
03042    }
03043 
03044    user = build_user(username, var, NULL, !ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS));
03045 
03046    ast_variables_destroy(var);
03047 
03048    if (!user)
03049       return NULL;
03050 
03051    if (ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS)) {
03052       ast_set_flag(user, IAX_RTCACHEFRIENDS);
03053       ao2_link(users, user);
03054    } else {
03055       ast_set_flag(user, IAX_TEMPONLY);   
03056    }
03057 
03058    return user;
03059 }

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

Definition at line 6207 of file chan_iax2.c.

References iax2_peer::addr, ast_db_get(), ast_device_state_changed(), ast_inet_ntoa(), ast_sched_del(), ast_test_flag, ast_verbose(), iax2_peer::expire, expire_registry(), iax2_peer::expiry, iax2_poke_peer(), iax2_regfunk, iax2_sched_add(), IAX_TEMPONLY, iax2_peer::name, option_verbose, peer_ref(), peer_unref(), register_peer_exten(), and VERBOSE_PREFIX_3.

Referenced by realtime_peer(), and set_config().

06208 {
06209    char data[80];
06210    struct in_addr in;
06211    char *c, *d;
06212    if (!ast_test_flag(p, IAX_TEMPONLY) && (!ast_db_get("IAX/Registry", p->name, data, sizeof(data)))) {
06213       c = strchr(data, ':');
06214       if (c) {
06215          *c = '\0';
06216          c++;
06217          if (inet_aton(data, &in)) {
06218             d = strchr(c, ':');
06219             if (d) {
06220                *d = '\0';
06221                d++;
06222                if (option_verbose > 2)
06223                   ast_verbose(VERBOSE_PREFIX_3 "Seeding '%s' at %s:%d for %d\n", p->name, 
06224                   ast_inet_ntoa(in), atoi(c), atoi(d));
06225                iax2_poke_peer(p, 0);
06226                p->expiry = atoi(d);
06227                memset(&p->addr, 0, sizeof(p->addr));
06228                p->addr.sin_family = AF_INET;
06229                p->addr.sin_addr = in;
06230                p->addr.sin_port = htons(atoi(c));
06231                if (p->expire > -1) {
06232                   if (!ast_sched_del(sched, p->expire)) {
06233                      p->expire = -1;
06234                      peer_unref(p);
06235                   }
06236                }
06237                ast_device_state_changed("IAX2/%s", p->name); /* Activate notification */
06238                p->expire = iax2_sched_add(sched, (p->expiry + 10) * 1000, expire_registry, peer_ref(p));
06239                if (p->expire == -1)
06240                   peer_unref(p);
06241                if (iax2_regfunk)
06242                   iax2_regfunk(p->name, 1);
06243                register_peer_exten(p, 1);
06244             }              
06245                
06246          }
06247       }
06248    }
06249 }

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

Definition at line 6127 of file chan_iax2.c.

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

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

06128 {
06129    char multi[256];
06130    char *stringp, *ext;
06131    if (!ast_strlen_zero(regcontext)) {
06132       ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
06133       stringp = multi;
06134       while((ext = strsep(&stringp, "&"))) {
06135          if (onoff) {
06136             if (!ast_exists_extension(NULL, regcontext, ext, 1, NULL))
06137                ast_add_extension(regcontext, 1, ext, 1, NULL, NULL,
06138                        "Noop", ast_strdup(peer->name), ast_free, "IAX2");
06139          } else
06140             ast_context_remove_extension(regcontext, ext, 1, NULL);
06141       }
06142    }
06143 }

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

Verify inbound registration.

Definition at line 5586 of file chan_iax2.c.

References iax2_peer::addr, ast_apply_ha(), ast_check_signature, ast_clear_flag, ast_copy_string(), ast_device_state_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, iax2_registry::expire, chan_iax2_pvt::expiry, expiry, find_peer(), iax2_peer::ha, IAX_AUTH_MD5, IAX_AUTH_PLAINTEXT, IAX_AUTH_RSA, IAX_DYNAMIC, IAX_STATE_AUTHENTICATED, IAX_STATE_UNCHANGED, inaddrcmp(), 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, iax2_registry::secret, and iax_ies::username.

Referenced by socket_process().

05587 {
05588    char requeststr[256] = "";
05589    char peer[256] = "";
05590    char md5secret[256] = "";
05591    char rsasecret[256] = "";
05592    char secret[256] = "";
05593    struct iax2_peer *p = NULL;
05594    struct ast_key *key;
05595    char *keyn;
05596    int x;
05597    int expire = 0;
05598    int res = -1;
05599 
05600    ast_clear_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED | IAX_STATE_UNCHANGED);
05601    /* iaxs[callno]->peer[0] = '\0'; not necc. any more-- stringfield is pre-inited to null string */
05602    if (ies->username)
05603       ast_copy_string(peer, ies->username, sizeof(peer));
05604    if (ies->password)
05605       ast_copy_string(secret, ies->password, sizeof(secret));
05606    if (ies->md5_result)
05607       ast_copy_string(md5secret, ies->md5_result, sizeof(md5secret));
05608    if (ies->rsa_result)
05609       ast_copy_string(rsasecret, ies->rsa_result, sizeof(rsasecret));
05610    if (ies->refresh)
05611       expire = ies->refresh;
05612 
05613    if (ast_strlen_zero(peer)) {
05614       ast_log(LOG_NOTICE, "Empty registration from %s\n", ast_inet_ntoa(sin->sin_addr));
05615       return -1;
05616    }
05617 
05618    /* SLD: first call to lookup peer during registration */
05619    ast_mutex_unlock(&iaxsl[callno]);
05620    p = find_peer(peer, 1);
05621    ast_mutex_lock(&iaxsl[callno]);
05622    if (!p || !iaxs[callno]) {
05623       if (authdebug && !p)
05624          ast_log(LOG_NOTICE, "No registration for peer '%s' (from %s)\n", peer, ast_inet_ntoa(sin->sin_addr));
05625       goto return_unref;
05626    }
05627 
05628    if (!ast_test_flag(p, IAX_DYNAMIC)) {
05629       if (authdebug)
05630          ast_log(LOG_NOTICE, "Peer '%s' is not dynamic (from %s)\n", peer, ast_inet_ntoa(sin->sin_addr));
05631       goto return_unref;
05632    }
05633 
05634    if (!ast_apply_ha(p->ha, sin)) {
05635       if (authdebug)
05636          ast_log(LOG_NOTICE, "Host %s denied access to register peer '%s'\n", ast_inet_ntoa(sin->sin_addr), p->name);
05637       goto return_unref;
05638    }
05639    if (!inaddrcmp(&p->addr, sin))
05640       ast_set_flag(&iaxs[callno]->state, IAX_STATE_UNCHANGED);
05641    ast_string_field_set(iaxs[callno], secret, p->secret);
05642    ast_string_field_set(iaxs[callno], inkeys, p->inkeys);
05643    /* Check secret against what we have on file */
05644    if (!ast_strlen_zero(rsasecret) && (p->authmethods & IAX_AUTH_RSA) && !ast_strlen_zero(iaxs[callno]->challenge)) {
05645       if (!ast_strlen_zero(p->inkeys)) {
05646          char tmpkeys[256];
05647          char *stringp=NULL;
05648          ast_copy_string(tmpkeys, p->inkeys, sizeof(tmpkeys));
05649          stringp=tmpkeys;
05650          keyn = strsep(&stringp, ":");
05651          while(keyn) {
05652             key = ast_key_get(keyn, AST_KEY_PUBLIC);
05653             if (key && !ast_check_signature(key, iaxs[callno]->challenge, rsasecret)) {
05654                ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
05655                break;
05656             } else if (!key) 
05657                ast_log(LOG_WARNING, "requested inkey '%s' does not exist\n", keyn);
05658             keyn = strsep(&stringp, ":");
05659          }
05660          if (!keyn) {
05661             if (authdebug)
05662                ast_log(LOG_NOTICE, "Host %s failed RSA authentication with inkeys '%s'\n", peer, p->inkeys);
05663             goto return_unref;
05664          }
05665       } else {
05666          if (authdebug)
05667             ast_log(LOG_NOTICE, "Host '%s' trying to do RSA authentication, but we have no inkeys\n", peer);
05668          goto return_unref;
05669       }
05670    } else if (!ast_strlen_zero(md5secret) && (p->authmethods & IAX_AUTH_MD5) && !ast_strlen_zero(iaxs[callno]->challenge)) {
05671       struct MD5Context md5;
05672       unsigned char digest[16];
05673       char *tmppw, *stringp;
05674       
05675       tmppw = ast_strdupa(p->secret);
05676       stringp = tmppw;
05677       while((tmppw = strsep(&stringp, ";"))) {
05678          MD5Init(&md5);
05679          MD5Update(&md5, (unsigned char *)iaxs[callno]->challenge, strlen(iaxs[callno]->challenge));
05680          MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
05681          MD5Final(digest, &md5);
05682          for (x=0;x<16;x++)
05683             sprintf(requeststr + (x << 1), "%2.2x", digest[x]); /* safe */
05684          if (!strcasecmp(requeststr, md5secret)) 
05685             break;
05686       }
05687       if (tmppw) {
05688          ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
05689       } else {
05690          if (authdebug)
05691             ast_log(LOG_NOTICE, "Host %s failed MD5 authentication for '%s' (%s != %s)\n", ast_inet_ntoa(sin->sin_addr), p->name, requeststr, md5secret);
05692          goto return_unref;
05693       }
05694    } else if (!ast_strlen_zero(secret) && (p->authmethods & IAX_AUTH_PLAINTEXT)) {
05695       /* They've provided a plain text password and we support that */
05696       if (strcmp(secret, p->secret)) {
05697          if (authdebug)
05698             ast_log(LOG_NOTICE, "Host %s did not provide proper plaintext password for '%s'\n", ast_inet_ntoa(sin->sin_addr), p->name);
05699          goto return_unref;
05700       } else
05701          ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
05702    } else if (!ast_strlen_zero(md5secret) || !ast_strlen_zero(secret)) {
05703       if (authdebug)
05704          ast_log(LOG_NOTICE, "Inappropriate authentication received\n");
05705       goto return_unref;
05706    }
05707    ast_string_field_set(iaxs[callno], peer, peer);
05708    /* Choose lowest expiry number */
05709    if (expire && (expire < iaxs[callno]->expiry)) 
05710       iaxs[callno]->expiry = expire;
05711 
05712    ast_device_state_changed("IAX2/%s", p->name); /* Activate notification */
05713 
05714    res = 0;
05715 
05716 return_unref:
05717    if (p)
05718       peer_unref(p);
05719 
05720    return res;
05721 }

static int registry_authrequest ( int  callno  )  [static]

Definition at line 6386 of file chan_iax2.c.

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

Referenced by socket_process().

06387 {
06388    struct iax_ie_data ied;
06389    struct iax2_peer *p;
06390    char challenge[10];
06391    const char *peer_name;
06392    int res = -1;
06393 
06394    peer_name = ast_strdupa(iaxs[callno]->peer);
06395 
06396    /* SLD: third call to find_peer in registration */
06397    ast_mutex_unlock(&iaxsl[callno]);
06398    p = find_peer(peer_name, 1);
06399    ast_mutex_lock(&iaxsl[callno]);
06400    if (!iaxs[callno])
06401       goto return_unref;
06402    if (!p) {
06403       ast_log(LOG_WARNING, "No such peer '%s'\n", peer_name);
06404       goto return_unref;
06405    }
06406    
06407    memset(&ied, 0, sizeof(ied));
06408    iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, p->authmethods);
06409    if (p->authmethods & (IAX_AUTH_RSA | IAX_AUTH_MD5)) {
06410       /* Build the challenge */
06411       snprintf(challenge, sizeof(challenge), "%d", (int)ast_random());
06412       ast_string_field_set(iaxs[callno], challenge, challenge);
06413       iax_ie_append_str(&ied, IAX_IE_CHALLENGE, iaxs[callno]->challenge);
06414    }
06415    iax_ie_append_str(&ied, IAX_IE_USERNAME, peer_name);
06416 
06417    res = 0;
06418 
06419 return_unref:
06420    peer_unref(p);
06421 
06422    return res ? res : send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGAUTH, 0, ied.buf, ied.pos, -1);;
06423 }

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

Definition at line 6425 of file chan_iax2.c.

References 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().

06426 {
06427    struct iax2_registry *reg;
06428    /* Start pessimistic */
06429    struct iax_ie_data ied;
06430    char peer[256] = "";
06431    char challenge[256] = "";
06432    int res;
06433    int authmethods = 0;
06434    if (ies->authmethods)
06435       authmethods = ies->authmethods;
06436    if (ies->username)
06437       ast_copy_string(peer, ies->username, sizeof(peer));
06438    if (ies->challenge)
06439       ast_copy_string(challenge, ies->challenge, sizeof(challenge));
06440    memset(&ied, 0, sizeof(ied));
06441    reg = iaxs[callno]->reg;
06442    if (reg) {
06443          if (inaddrcmp(&reg->addr, sin)) {
06444             ast_log(LOG_WARNING, "Received unsolicited registry authenticate request from '%s'\n", ast_inet_ntoa(sin->sin_addr));
06445             return -1;
06446          }
06447          if (ast_strlen_zero(reg->secret)) {
06448             ast_log(LOG_NOTICE, "No secret associated with peer '%s'\n", reg->username);
06449             reg->regstate = REG_STATE_NOAUTH;
06450             return -1;
06451          }
06452          iax_ie_append_str(&ied, IAX_IE_USERNAME, reg->username);
06453          iax_ie_append_short(&ied, IAX_IE_REFRESH, reg->refresh);
06454          if (reg->secret[0] == '[') {
06455             char tmpkey[256];
06456             ast_copy_string(tmpkey, reg->secret + 1, sizeof(tmpkey));
06457             tmpkey[strlen(tmpkey) - 1] = '\0';
06458             res = authenticate(challenge, NULL, tmpkey, authmethods, &ied, sin, NULL, NULL);
06459          } else
06460             res = authenticate(challenge, reg->secret, NULL, authmethods, &ied, sin, NULL, NULL);
06461          if (!res) {
06462             reg->regstate = REG_STATE_AUTHSENT;
06463             return send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGREQ, 0, ied.buf, ied.pos, -1);
06464          } else
06465             return -1;
06466          ast_log(LOG_WARNING, "Registry acknowledge on unknown registery '%s'\n", peer);
06467    } else   
06468       ast_log(LOG_NOTICE, "Can't reregister without a reg\n");
06469    return -1;
06470 }

static char* regstate2str ( int  regstate  )  [static]

Definition at line 4878 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 iax2_show_registry().

04879 {
04880    switch(regstate) {
04881    case REG_STATE_UNREGISTERED:
04882       return "Unregistered";
04883    case REG_STATE_REGSENT:
04884       return "Request Sent";
04885    case REG_STATE_AUTHSENT:
04886       return "Auth. Sent";
04887    case REG_STATE_REGISTERED:
04888       return "Registered";
04889    case REG_STATE_REJECTED:
04890       return "Rejected";
04891    case REG_STATE_TIMEOUT:
04892       return "Timeout";
04893    case REG_STATE_NOAUTH:
04894       return "No Authentication";
04895    default:
04896       return "Unknown";
04897    }
04898 }

static int reload ( void   )  [static]

Definition at line 10381 of file chan_iax2.c.

References reload_config().

10382 {
10383    return reload_config();
10384 }

static int reload_config ( void   )  [static]

Definition at line 10355 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, config, iax2_do_register(), iax_provision_reload(), poke_all_peers(), prune_peers(), prune_users(), reload_firmware(), and set_config().

10356 {
10357    char *config = "iax.conf";
10358    struct iax2_registry *reg;
10359 
10360    if (set_config(config, 1) > 0) {
10361       prune_peers();
10362       prune_users();
10363       AST_LIST_LOCK(&registrations);
10364       AST_LIST_TRAVERSE(&registrations, reg, entry)
10365          iax2_do_register(reg);
10366       AST_LIST_UNLOCK(&registrations);
10367       /* Qualify hosts, too */
10368       poke_all_peers();
10369    }
10370    reload_firmware(0);
10371    iax_provision_reload();
10372 
10373    return 0;
10374 }

static void reload_firmware ( int  unload  )  [static]

Definition at line 2017 of file chan_iax2.c.

References ast_config_AST_DATA_DIR, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_verbose(), iax_firmware::dead, destroy_firmware(), errno, ast_firmware_list::lock, LOG_WARNING, iax_firmware::next, option_verbose, try_firmware(), VERBOSE_PREFIX_2, ast_firmware_list::wares, and waresl.

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

02018 {
02019    struct iax_firmware *cur, *curl, *curp;
02020    DIR *fwd;
02021    struct dirent *de;
02022    char dir[256];
02023    char fn[256];
02024    /* Mark all as dead */
02025    ast_mutex_lock(&waresl.lock);
02026    cur = waresl.wares;
02027    while(cur) {
02028       cur->dead = 1;
02029       cur = cur->next;
02030    }
02031 
02032    /* Now that we've freed them, load the new ones */
02033    if (!unload) {
02034       snprintf(dir, sizeof(dir), "%s/firmware/iax", (char *)ast_config_AST_DATA_DIR);
02035       fwd = opendir(dir);
02036       if (fwd) {
02037          while((de = readdir(fwd))) {
02038             if (de->d_name[0] != '.') {
02039                snprintf(fn, sizeof(fn), "%s/%s", dir, de->d_name);
02040                if (!try_firmware(fn)) {
02041                   if (option_verbose > 1)
02042                      ast_verbose(VERBOSE_PREFIX_2 "Loaded firmware '%s'\n", de->d_name);
02043                }
02044             }
02045          }
02046          closedir(fwd);
02047       } else 
02048          ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", dir, strerror(errno));
02049    }
02050 
02051    /* Clean up leftovers */
02052    cur = waresl.wares;
02053    curp = NULL;
02054    while(cur) {
02055       curl = cur;
02056       cur = cur->next;
02057       if (curl->dead) {
02058          if (curp) {
02059             curp->next = cur;
02060          } else {
02061             waresl.wares = cur;
02062          }
02063          destroy_firmware(curl);
02064       } else {
02065          curp = cur;
02066       }
02067    }
02068    ast_mutex_unlock(&waresl.lock);
02069 }

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

Definition at line 1257 of file chan_iax2.c.

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

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

01258 {
01259    if (!pvt->peercallno) {
01260       ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
01261       return;
01262    }
01263 
01264    ao2_unlink(iax_peercallno_pvts, pvt);
01265 }

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

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

Definition at line 9165 of file chan_iax2.c.

References ast_cond_timedwait(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_samp2tv(), ast_sched_runq(), ast_sched_wait(), ast_tvadd(), ast_tvnow(), LOG_DEBUG, option_debug, and sched_lock.

Referenced by start_network_thread().

09166 {
09167    int count;
09168    int res;
09169    struct timeval tv;
09170    struct timespec ts;
09171 
09172    for (;;) {
09173       pthread_testcancel();
09174       ast_mutex_lock(&sched_lock);
09175       res = ast_sched_wait(sched);
09176       if ((res > 1000) || (res < 0))
09177          res = 1000;
09178       tv = ast_tvadd(ast_tvnow(), ast_samp2tv(res, 1000));
09179       ts.tv_sec = tv.tv_sec;
09180       ts.tv_nsec = tv.tv_usec * 1000;
09181       ast_cond_timedwait(&sched_cond, &sched_lock, &ts);
09182       ast_mutex_unlock(&sched_lock);
09183       pthread_testcancel();
09184 
09185       count = ast_sched_runq(sched);
09186       if (option_debug && count >= 20)
09187          ast_log(LOG_DEBUG, "chan_iax2: ast_sched_runq ran %d scheduled tasks all at once\n", count);
09188    }
09189    return NULL;
09190 }

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

References __do_deliver(), iax_frame::af, ast_bridged_channel(), AST_CHAN_TP_WANTSJITTER, ast_codec_get_samples(), AST_FRAME_CNG, AST_FRAME_VOICE, ast_log(), ast_samp2tv(), AST_SCHED_DEL, ast_test_flag, ast_tv(), ast_tvadd(), ast_tvzero(), 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, LOG_DEBUG, option_debug, chan_iax2_pvt::owner, ast_channel_tech::properties, chan_iax2_pvt::rxcore, ast_channel::tech, iax_frame::ts, unwrap_timestamp(), and update_jbsched().

Referenced by socket_process().

02722 {
02723    int type, len;
02724    int ret;
02725    int needfree = 0;
02726    struct ast_channel *owner = NULL;
02727    struct ast_channel *bridge = NULL;
02728    
02729    /* Attempt to recover wrapped timestamps */
02730    unwrap_timestamp(fr);
02731 
02732    /* delivery time is sender's sent timestamp converted back into absolute time according to our clock */
02733    if ( !fromtrunk && !ast_tvzero(iaxs[fr->callno]->rxcore))
02734       fr->af.delivery = ast_tvadd(iaxs[fr->callno]->rxcore, ast_samp2tv(fr->ts, 1000));
02735    else {
02736 #if 0
02737       if (option_debug)
02738          ast_log(LOG_DEBUG, "schedule_delivery: set delivery to 0 as we don't have an rxcore yet, or frame is from trunk.\n");
02739 #endif
02740       fr->af.delivery = ast_tv(0,0);
02741    }
02742 
02743    type = JB_TYPE_CONTROL;
02744    len = 0;
02745 
02746    if(fr->af.frametype == AST_FRAME_VOICE) {
02747       type = JB_TYPE_VOICE;
02748       len = ast_codec_get_samples(&fr->af) / 8;
02749    } else if(fr->af.frametype == AST_FRAME_CNG) {
02750       type = JB_TYPE_SILENCE;
02751    }
02752 
02753    if ( (!ast_test_flag(iaxs[fr->callno], IAX_USEJITTERBUF)) ) {
02754       if (tsout)
02755          *tsout = fr->ts;
02756       __do_deliver(fr);
02757       return -1;
02758    }
02759 
02760    if ((owner = iaxs[fr->callno]->owner))
02761       bridge = ast_bridged_channel(owner);
02762 
02763    /* if the user hasn't requested we force the use of the jitterbuffer, and we're bridged to
02764     * a channel that can accept jitter, then flush and suspend the jb, and send this frame straight through */
02765    if ( (!ast_test_flag(iaxs[fr->callno], IAX_FORCEJITTERBUF)) && owner && bridge && (bridge->tech->properties & AST_CHAN_TP_WANTSJITTER) ) {
02766       jb_frame frame;
02767 
02768       /* deliver any frames in the jb */
02769       while (jb_getall(iaxs[fr->callno]->jb, &frame) == JB_OK) {
02770          __do_deliver(frame.data);
02771          /* __do_deliver() can make the call disappear */
02772          if (!iaxs[fr->callno])
02773             return -1;
02774       }
02775 
02776       jb_reset(iaxs[fr->callno]->jb);
02777 
02778       AST_SCHED_DEL(sched, iaxs[fr->callno]->jbid);
02779 
02780       /* deliver this frame now */
02781       if (tsout)
02782          *tsout = fr->ts;
02783       __do_deliver(fr);
02784       return -1;
02785    }
02786 
02787    /* insert into jitterbuffer */
02788    /* TODO: Perhaps we could act immediately if it's not droppable and late */
02789    ret = jb_put(iaxs[fr->callno]->jb, fr, type, len, fr->ts,
02790          calc_rxstamp(iaxs[fr->callno],fr->ts));
02791    if (ret == JB_DROP) {
02792       needfree++;
02793    } else if (ret == JB_SCHED) {
02794       update_jbsched(iaxs[fr->callno]);
02795    }
02796    if (tsout)
02797       *tsout = fr->ts;
02798    if (needfree) {
02799       /* Free our iax frame */
02800       iax2_frame_free(fr);
02801       return -1;
02802    }
02803    return 0;
02804 }

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

Definition at line 1344 of file chan_iax2.c.

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

Referenced by iax2_hangup().

01345 {
01346    short callno = PTR_TO_CALLNO(vid);
01347    ast_mutex_lock(&iaxsl[callno]);
01348    if (iaxs[callno]) {
01349       if (option_debug) {
01350          ast_log(LOG_DEBUG, "Really destroying %d now...\n", callno);
01351       }
01352       iax2_destroy(callno);
01353    }
01354    ast_mutex_unlock(&iaxsl[callno]);
01355    return 0;
01356 }

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

Definition at line 3239 of file chan_iax2.c.

References AST_FRAME_IAX, compress_subclass(), and ast_iax2_full_hdr::scallno.

Referenced by socket_process().

03240 {
03241    struct ast_iax2_full_hdr f = { .scallno = htons(0x8000 | callno), .dcallno = htons(dcallno),
03242       .ts = htonl(ts), .iseqno = seqno, .oseqno = 0, .type = AST_FRAME_IAX,
03243       .csub = compress_subclass(command) };
03244 
03245    return sendto(defaultsockfd, &f, sizeof(f), 0, (struct sockaddr *)sin, sizeof(*sin));
03246 }

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 5181 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().

05182 {
05183    int call_num = i->callno;
05184    /* It is assumed that the callno has already been locked */
05185    iax2_predestroy(i->callno);
05186    if (!iaxs[call_num])
05187       return -1;
05188    return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 1);
05189 }

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

References __send_command().

Referenced by iax2_vnak(), and socket_process().

05192 {
05193    return __send_command(i, type, command, ts, data, datalen, seqno, 1, 0, 0);
05194 }

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 5167 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().

05168 {
05169    int res;
05170    ast_mutex_lock(&iaxsl[callno]);
05171    res = send_command(iaxs[callno], type, command, ts, data, datalen, seqno);
05172    ast_mutex_unlock(&iaxsl[callno]);
05173    return res;
05174 }

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

References __send_command().

Referenced by socket_process(), and try_transfer().

05197 {
05198    return __send_command(i, type, command, ts, data, datalen, 0, 0, 1, 0);
05199 }

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

Definition at line 1062 of file chan_iax2.c.

References __send_lagrq(), and schedule_action.

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

01063 {
01064 #ifdef SCHED_MULTITHREADED
01065    if (schedule_action(__send_lagrq, data))
01066 #endif      
01067       __send_lagrq(data);
01068    
01069    return 0;
01070 }

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

Definition at line 2142 of file chan_iax2.c.

References chan_iax2_pvt::addr, iax2_registry::addr, ast_backtrace(), ast_inet_ntoa(), ast_log(), iax_frame::callno, iax2_registry::callno, iax_frame::data, iax_frame::datalen, errno, handle_error(), iax_showframe(), LOG_DEBUG, LOG_ERROR, option_debug, chan_iax2_pvt::peercallno, chan_iax2_pvt::transfer, transfer, iax_frame::transfer, and iax_frame::ts.

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

02143 {
02144    int res;
02145    int callno;
02146 
02147    if( f == NULL ) {
02148        ast_log(LOG_ERROR, "send_packet( NULL )\n");
02149        ast_backtrace();
02150        return -1;
02151    }
02152 
02153    callno = f->callno;
02154 
02155    /* Don't send if there was an error, but return error instead */
02156    if (!callno || !iaxs[callno] || iaxs[callno]->error)
02157        return -1;
02158    
02159    /* Called with iaxsl held */
02160    if (option_debug > 2 && iaxdebug)
02161       ast_log(LOG_DEBUG, "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));
02162    if (f->transfer) {
02163       if (iaxdebug)
02164          iax_showframe(f, NULL, 0, &iaxs[callno]->transfer, f->datalen - sizeof(struct ast_iax2_full_hdr));
02165       res = sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[callno]->transfer,
02166                sizeof(iaxs[callno]->transfer));
02167    } else {
02168       if (iaxdebug)
02169          iax_showframe(f, NULL, 0, &iaxs[callno]->addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
02170       res = sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0,(struct sockaddr *)&iaxs[callno]->addr,
02171                sizeof(iaxs[callno]->addr));
02172    }
02173    if (res < 0) {
02174       if (option_debug && iaxdebug)
02175          ast_log(LOG_DEBUG, "Received error: %s\n", strerror(errno));
02176       handle_error();
02177    } else
02178       res = 0;
02179    return res;
02180 }

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

Definition at line 1017 of file chan_iax2.c.

References __send_ping(), and schedule_action.

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

01018 {
01019 #ifdef SCHED_MULTITHREADED
01020    if (schedule_action(__send_ping, data))
01021 #endif      
01022       __send_ping(data);
01023 
01024    return 0;
01025 }

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

Definition at line 6607 of file chan_iax2.c.

References iax2_trunk_peer::addr, iax_frame::afdata, ast_inet_ntoa(), ast_log(), 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, LOG_DEBUG, ast_iax2_meta_hdr::metacmd, option_debug, 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 timing_read().

06608 {
06609    int res = 0;
06610    struct iax_frame *fr;
06611    struct ast_iax2_meta_hdr *meta;
06612    struct ast_iax2_meta_trunk_hdr *mth;
06613    int calls = 0;
06614    
06615    /* Point to frame */
06616    fr = (struct iax_frame *)tpeer->trunkdata;
06617    /* Point to meta data */
06618    meta = (struct ast_iax2_meta_hdr *)fr->afdata;
06619    mth = (struct ast_iax2_meta_trunk_hdr *)meta->data;
06620    if (tpeer->trunkdatalen) {
06621       /* We're actually sending a frame, so fill the meta trunk header and meta header */
06622       meta->zeros = 0;
06623       meta->metacmd = IAX_META_TRUNK;
06624       if (ast_test_flag(&globalflags, IAX_TRUNKTIMESTAMPS))
06625          meta->cmddata = IAX_META_TRUNK_MINI;
06626       else
06627          meta->cmddata = IAX_META_TRUNK_SUPERMINI;
06628       mth->ts = htonl(calc_txpeerstamp(tpeer, trunkfreq, now));
06629       /* And the rest of the ast_iax2 header */
06630       fr->direction = DIRECTION_OUTGRESS;
06631       fr->retrans = -1;
06632       fr->transfer = 0;
06633       /* Any appropriate call will do */
06634       fr->data = fr->afdata;
06635       fr->datalen = tpeer->trunkdatalen + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr);
06636       res = transmit_trunk(fr, &tpeer->addr, tpeer->sockfd);
06637       calls = tpeer->calls;
06638 #if 0
06639       if (option_debug)
06640          ast_log(LOG_DEBUG, "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));
06641 #endif      
06642       /* Reset transmit trunk side data */
06643       tpeer->trunkdatalen = 0;
06644       tpeer->calls = 0;
06645    }
06646    if (res < 0)
06647       return res;
06648    return calls;
06649 }

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

Load configuration.

Definition at line 9985 of file chan_iax2.c.

References __ao2_link(), ast_category_browse(), ast_cdr_amaflags2int(), ast_clear_flag, ast_config_destroy(), ast_config_load(), ast_context_create(), ast_context_find(), ast_copy_string(), 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_str2tos(), ast_strlen_zero(), ast_test_flag, ast_true(), ast_variable_browse(), ast_variable_retrieve(), ast_verbose(), build_peer(), build_user(), capability, 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_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_RTAUTOCLEAR, IAX_RTCACHEFRIENDS, IAX_RTIGNOREREGEXPIRE, IAX_RTUPDATE, IAX_TRANSFERMEDIA, IAX_TRUNKTIMESTAMPS, IAX_USEJITTERBUF, iaxmaxthreadcount, iaxthreadcount, ast_variable::lineno, LOG_ERROR, LOG_NOTICE, LOG_WARNING, MAX_PEER_BUCKETS, ast_variable::name, ast_variable::next, option_verbose, peer_unref(), portno, prefs, reg_source_db(), iax2_registry::secret, set_config_destroy(), set_timing(), socket_read(), user_unref(), ast_variable::value, and VERBOSE_PREFIX_2.

Referenced by load_module(), and reload_config().

09986 {
09987    struct ast_config *cfg, *ucfg;
09988    int capability=iax2_capability;
09989    struct ast_variable *v;
09990    char *cat;
09991    const char *utype;
09992    const char *tosval;
09993    int format;
09994    int portno = IAX_DEFAULT_PORTNO;
09995    int  x;
09996    struct iax2_user *user;
09997    struct iax2_peer *peer;
09998    struct ast_netsock *ns;
09999 #if 0
10000    static unsigned short int last_port=0;
10001 #endif
10002 
10003    cfg = ast_config_load(config_file);
10004    
10005    if (!cfg) {
10006       ast_log(LOG_ERROR, "Unable to load config %s\n", config_file);
10007       return -1;
10008    }
10009 
10010    if (reload) {
10011       set_config_destroy();
10012    }
10013 
10014    /* Reset global codec prefs */   
10015    memset(&prefs, 0 , sizeof(struct ast_codec_pref));
10016    
10017    /* Reset Global Flags */
10018    memset(&globalflags, 0, sizeof(globalflags));
10019    ast_set_flag(&globalflags, IAX_RTUPDATE);
10020 
10021 #ifdef SO_NO_CHECK
10022    nochecksums = 0;
10023 #endif
10024 
10025    min_reg_expire = IAX_DEFAULT_REG_EXPIRE;
10026    max_reg_expire = IAX_DEFAULT_REG_EXPIRE;
10027 
10028    maxauthreq = 3;
10029 
10030    v = ast_variable_browse(cfg, "general");
10031 
10032    /* Seed initial tos value */
10033    tosval = ast_variable_retrieve(cfg, "general", "tos");
10034    if (tosval) {
10035       if (ast_str2tos(tosval, &tos))
10036          ast_log(LOG_WARNING, "Invalid tos value, see doc/ip-tos.txt for more information.\n");
10037    }
10038    while(v) {
10039       if (!strcasecmp(v->name, "bindport")){ 
10040          if (reload)
10041             ast_log(LOG_NOTICE, "Ignoring bindport on reload\n");
10042          else
10043             portno = atoi(v->value);
10044       } else if (!strcasecmp(v->name, "pingtime")) 
10045          ping_time = atoi(v->value);
10046       else if (!strcasecmp(v->name, "iaxthreadcount")) {
10047          if (reload) {
10048             if (atoi(v->value) != iaxthreadcount)
10049                ast_log(LOG_NOTICE, "Ignoring any changes to iaxthreadcount during reload\n");
10050          } else {
10051             iaxthreadcount = atoi(v->value);
10052             if (iaxthreadcount < 1) {
10053                ast_log(LOG_NOTICE, "iaxthreadcount must be at least 1.\n");
10054                iaxthreadcount = 1;
10055             } else if (iaxthreadcount > 256) {
10056                ast_log(LOG_NOTICE, "limiting iaxthreadcount to 256\n");
10057                iaxthreadcount = 256;
10058             }
10059          }
10060       } else if (!strcasecmp(v->name, "iaxmaxthreadcount")) {
10061          if (reload) {
10062             AST_LIST_LOCK(&dynamic_list);
10063             iaxmaxthreadcount = atoi(v->value);
10064             AST_LIST_UNLOCK(&dynamic_list);
10065          } else {
10066             iaxmaxthreadcount = atoi(v->value);
10067             if (iaxmaxthreadcount < 0) {
10068                ast_log(LOG_NOTICE, "iaxmaxthreadcount must be at least 0.\n");
10069                iaxmaxthreadcount = 0;
10070             } else if (iaxmaxthreadcount > 256) {
10071                ast_log(LOG_NOTICE, "Limiting iaxmaxthreadcount to 256\n");
10072                iaxmaxthreadcount = 256;
10073             }
10074          }
10075       } else if (!strcasecmp(v->name, "nochecksums")) {
10076 #ifdef SO_NO_CHECK
10077          if (ast_true(v->value))
10078             nochecksums = 1;
10079          else
10080             nochecksums = 0;
10081 #else
10082          if (ast_true(v->value))
10083             ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
10084 #endif
10085       }
10086       else if (!strcasecmp(v->name, "maxjitterbuffer")) 
10087          maxjitterbuffer = atoi(v->value);
10088       else if (!strcasecmp(v->name, "resyncthreshold")) 
10089          resyncthreshold = atoi(v->value);
10090       else if (!strcasecmp(v->name, "maxjitterinterps")) 
10091          maxjitterinterps = atoi(v->value);
10092       else if (!strcasecmp(v->name, "lagrqtime")) 
10093          lagrq_time = atoi(v->value);
10094       else if (!strcasecmp(v->name, "maxregexpire")) 
10095          max_reg_expire = atoi(v->value);
10096       else if (!strcasecmp(v->name, "minregexpire")) 
10097          min_reg_expire = atoi(v->value);
10098       else if (!strcasecmp(v->name, "bindaddr")) {
10099          if (reload) {
10100             ast_log(LOG_NOTICE, "Ignoring bindaddr on reload\n");
10101          } else {
10102             if (!(ns = ast_netsock_bind(netsock, io, v->value, portno, tos, socket_read, NULL))) {
10103                ast_log(LOG_WARNING, "Unable apply binding to '%s' at line %d\n", v->value, v->lineno);
10104             } else {
10105                if (option_verbose > 1) {
10106                   if (strchr(v->value, ':'))
10107                      ast_verbose(VERBOSE_PREFIX_2 "Binding IAX2 to '%s'\n", v->value);
10108                   else
10109                      ast_verbose(VERBOSE_PREFIX_2 "Binding IAX2 to '%s:%d'\n", v->value, portno);
10110                }
10111                if (defaultsockfd < 0) 
10112                   defaultsockfd = ast_netsock_sockfd(ns);
10113                ast_netsock_unref(ns);
10114             }
10115          }
10116       } else if (!strcasecmp(v->name, "authdebug"))
10117          authdebug = ast_true(v->value);
10118       else if (!strcasecmp(v->name, "encryption"))
10119          iax2_encryption = get_encrypt_methods(v->value);
10120       else if (!strcasecmp(v->name, "notransfer")) {
10121          ast_log(LOG_NOTICE, "The option 'notransfer' is deprecated in favor of 'transfer' which has options 'yes', 'no', and 'mediaonly'\n");
10122          ast_clear_flag((&globalflags), IAX_TRANSFERMEDIA); 
10123          ast_set2_flag((&globalflags), ast_true(v->value), IAX_NOTRANSFER);   
10124       } else if (!strcasecmp(v->name, "transfer")) {
10125          if (!strcasecmp(v->value, "mediaonly")) {
10126             ast_set_flags_to((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA); 
10127          } else if (ast_true(v->value)) {
10128             ast_set_flags_to((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
10129          } else 
10130             ast_set_flags_to((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
10131       } else if (!strcasecmp(v->name, "codecpriority")) {
10132          if(!strcasecmp(v->value, "caller"))
10133             ast_set_flag((&globalflags), IAX_CODEC_USER_FIRST);
10134          else if(!strcasecmp(v->value, "disabled"))
10135             ast_set_flag((&globalflags), IAX_CODEC_NOPREFS);
10136          else if(!strcasecmp(v->value, "reqonly")) {
10137             ast_set_flag((&globalflags), IAX_CODEC_NOCAP);
10138             ast_set_flag((&globalflags), IAX_CODEC_NOPREFS);
10139          }
10140       } else if (!strcasecmp(v->name, "jitterbuffer"))
10141          ast_set2_flag((&globalflags), ast_true(v->value), IAX_USEJITTERBUF); 
10142       else if (!strcasecmp(v->name, "forcejitterbuffer"))
10143          ast_set2_flag((&globalflags), ast_true(v->value), IAX_FORCEJITTERBUF);  
10144       else if (!strcasecmp(v->name, "delayreject"))
10145          delayreject = ast_true(v->value);
10146       else if (!strcasecmp(v->name, "allowfwdownload"))
10147          ast_set2_flag((&globalflags), ast_true(v->value), IAX_ALLOWFWDOWNLOAD);
10148       else if (!strcasecmp(v->name, "rtcachefriends"))
10149          ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTCACHEFRIENDS);  
10150       else if (!strcasecmp(v->name, "rtignoreregexpire"))
10151          ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTIGNOREREGEXPIRE);  
10152       else if (!strcasecmp(v->name, "rtupdate"))
10153          ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTUPDATE);
10154       else if (!strcasecmp(v->name, "trunktimestamps"))
10155          ast_set2_flag(&globalflags, ast_true(v->value), IAX_TRUNKTIMESTAMPS);
10156       else if (!strcasecmp(v->name, "rtautoclear")) {
10157          int i = atoi(v->value);
10158          if(i > 0)
10159             global_rtautoclear = i;
10160          else
10161             i = 0;
10162          ast_set2_flag((&globalflags), i || ast_true(v->value), IAX_RTAUTOCLEAR);   
10163       } else if (!strcasecmp(v->name, "trunkfreq")) {
10164          trunkfreq = atoi(v->value);
10165          if (trunkfreq < 10)
10166             trunkfreq = 10;
10167       } else if (!strcasecmp(v->name, "autokill")) {
10168          if (sscanf(v->value, "%d", &x) == 1) {
10169             if (x >= 0)
10170                autokill = x;
10171             else
10172                ast_log(LOG_NOTICE, "Nice try, but autokill has to be >0 or 'yes' or 'no' at line %d\n", v->lineno);
10173          } else if (ast_true(v->value)) {
10174             autokill = DEFAULT_MAXMS;
10175          } else {
10176             autokill = 0;
10177          }
10178       } else if (!strcasecmp(v->name, "bandwidth")) {
10179          if (!strcasecmp(v->value, "low")) {
10180             capability = IAX_CAPABILITY_LOWBANDWIDTH;
10181          } else if (!strcasecmp(v->value, "medium")) {
10182             capability = IAX_CAPABILITY_MEDBANDWIDTH;
10183          } else if (!strcasecmp(v->value, "high")) {
10184             capability = IAX_CAPABILITY_FULLBANDWIDTH;
10185          } else
10186             ast_log(LOG_WARNING, "bandwidth must be either low, medium, or high\n");
10187       } else if (!strcasecmp(v->name, "allow")) {
10188          ast_parse_allow_disallow(&prefs, &capability, v->value, 1);
10189       } else if (!strcasecmp(v->name, "disallow")) {
10190          ast_parse_allow_disallow(&prefs, &capability, v->value, 0);
10191       } else if (!strcasecmp(v->name, "register")) {
10192          iax2_register(v->value, v->lineno);
10193       } else if (!strcasecmp(v->name, "iaxcompat")) {
10194          iaxcompat = ast_true(v->value);
10195       } else if (!strcasecmp(v->name, "regcontext")) {
10196          ast_copy_string(regcontext, v->value, sizeof(regcontext));
10197          /* Create context if it doesn't exist already */
10198          if (!ast_context_find(regcontext))
10199             ast_context_create(NULL, regcontext, "IAX2");
10200       } else if (!strcasecmp(v->name, "tos")) {
10201          if (ast_str2tos(v->value, &tos))
10202             ast_log(LOG_WARNING, "Invalid tos value at line %d, see doc/ip-tos.txt for more information.'\n", v->lineno);
10203       } else if (!strcasecmp(v->name, "accountcode")) {
10204          ast_copy_string(accountcode, v->value, sizeof(accountcode));
10205       } else if (!strcasecmp(v->name, "mohinterpret")) {
10206          ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
10207       } else if (!strcasecmp(v->name, "mohsuggest")) {
10208          ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
10209       } else if (!strcasecmp(v->name, "amaflags")) {
10210          format = ast_cdr_amaflags2int(v->value);
10211          if (format < 0) {
10212             ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
10213          } else {
10214             amaflags = format;
10215          }
10216       } else if (!strcasecmp(v->name, "language")) {
10217          ast_copy_string(language, v->value, sizeof(language));
10218       } else if (!strcasecmp(v->name, "maxauthreq")) {
10219          maxauthreq = atoi(v->value);
10220          if (maxauthreq < 0)
10221             maxauthreq = 0;
10222       } else if (!strcasecmp(v->name, "adsi")) {
10223          adsi = ast_true(v->value);
10224       } /*else if (strcasecmp(v->name,"type")) */
10225       /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
10226       v = v->next;
10227    }
10228    
10229    if (defaultsockfd < 0) {
10230       if (!(ns = ast_netsock_bind(netsock, io, "0.0.0.0", portno, tos, socket_read, NULL))) {
10231          ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
10232       } else {
10233          if (option_verbose > 1)
10234             ast_verbose(VERBOSE_PREFIX_2 "Binding IAX2 to default address 0.0.0.0:%d\n", portno);
10235          defaultsockfd = ast_netsock_sockfd(ns);
10236          ast_netsock_unref(ns);
10237       }
10238    }
10239    if (reload) {
10240       ast_netsock_release(outsock);
10241       outsock = ast_netsock_list_alloc();
10242       if (!outsock) {
10243          ast_log(LOG_ERROR, "Could not allocate outsock list.\n");
10244          return -1;
10245       }
10246       ast_netsock_init(outsock);
10247    }
10248 
10249    if (min_reg_expire > max_reg_expire) {
10250       ast_log(LOG_WARNING, "Minimum registration interval of %d is more than maximum of %d, resetting minimum to %d\n",
10251          min_reg_expire, max_reg_expire, max_reg_expire);
10252       min_reg_expire = max_reg_expire;
10253    }
10254    iax2_capability = capability;
10255    
10256    ucfg = ast_config_load("users.conf");
10257    if (ucfg) {
10258       struct ast_variable *gen;
10259       int genhasiax;
10260       int genregisteriax;
10261       const char *hasiax, *registeriax;
10262       
10263       genhasiax = ast_true(ast_variable_retrieve(ucfg, "general", "hasiax"));
10264       genregisteriax = ast_true(ast_variable_retrieve(ucfg, "general", "registeriax"));
10265       gen = ast_variable_browse(ucfg, "general");
10266       cat = ast_category_browse(ucfg, NULL);
10267       while (cat) {
10268          if (strcasecmp(cat, "general")) {
10269             hasiax = ast_variable_retrieve(ucfg, cat, "hasiax");
10270             registeriax = ast_variable_retrieve(ucfg, cat, "registeriax");
10271             if (ast_true(hasiax) || (!hasiax && genhasiax)) {
10272                /* Start with general parameters, then specific parameters, user and peer */
10273                user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
10274                if (user) {
10275                   __ao2_link(users, user, (MAX_PEER_BUCKETS == 1) ? 1 : 0);
10276                   user = user_unref(user);
10277                }
10278                peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
10279                if (peer) {
10280                   if (ast_test_flag(peer, IAX_DYNAMIC))
10281                      reg_source_db(peer);
10282                   __ao2_link(peers, peer, (MAX_PEER_BUCKETS == 1) ? 1 : 0);
10283                   peer = peer_unref(peer);
10284                }
10285             }
10286             if (ast_true(registeriax) || (!registeriax && genregisteriax)) {
10287                char tmp[256];
10288                const char *host = ast_variable_retrieve(ucfg, cat, "host");
10289                const char *username = ast_variable_retrieve(ucfg, cat, "username");
10290                const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
10291                if (!host)
10292                   host = ast_variable_retrieve(ucfg, "general", "host");
10293                if (!username)
10294                   username = ast_variable_retrieve(ucfg, "general", "username");
10295                if (!secret)
10296                   secret = ast_variable_retrieve(ucfg, "general", "secret");
10297                if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
10298                   if (!ast_strlen_zero(secret))
10299                      snprintf(tmp, sizeof(tmp), "%s:%s@%s", username, secret, host);
10300                   else
10301                      snprintf(tmp, sizeof(tmp), "%s@%s", username, host);
10302                   iax2_register(tmp, 0);
10303                }
10304             }
10305          }
10306          cat = ast_category_browse(ucfg, cat);
10307       }
10308       ast_config_destroy(ucfg);
10309    }
10310    
10311    cat = ast_category_browse(cfg, NULL);
10312    while(cat) {
10313       if (strcasecmp(cat, "general")) {
10314          utype = ast_variable_retrieve(cfg, cat, "type");
10315          if (utype) {
10316             if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
10317                user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
10318                if (user) {
10319                   __ao2_link(users, user, (MAX_PEER_BUCKETS == 1) ? 1 : 0);
10320                   user = user_unref(user);
10321                }
10322             }
10323             if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
10324                peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
10325                if (peer) {
10326                   if (ast_test_flag(peer, IAX_DYNAMIC))
10327                      reg_source_db(peer);
10328                   __ao2_link(peers, peer, (MAX_PEER_BUCKETS == 1) ? 1 : 0);
10329                   peer = peer_unref(peer);
10330                }
10331             } else if (strcasecmp(utype, "user")) {
10332                ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config_file);
10333             }
10334          } else
10335             ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
10336       }
10337       cat = ast_category_browse(cfg, cat);
10338    }
10339    ast_config_destroy(cfg);
10340    set_timing();
10341    return 1;
10342 }

static void set_config_destroy ( void   )  [static]

Definition at line 9969 of file chan_iax2.c.

References ast_clear_flag, delete_users(), IAX_FORCEJITTERBUF, IAX_NOTRANSFER, IAX_TRANSFERMEDIA, and IAX_USEJITTERBUF.

Referenced by set_config().

09970 {
09971    strcpy(accountcode, "");
09972    strcpy(language, "");
09973    strcpy(mohinterpret, "default");
09974    strcpy(mohsuggest, "");
09975    amaflags = 0;
09976    delayreject = 0;
09977    ast_clear_flag((&globalflags), IAX_NOTRANSFER); 
09978    ast_clear_flag((&globalflags), IAX_TRANSFERMEDIA); 
09979    ast_clear_flag((&globalflags), IAX_USEJITTERBUF);  
09980    ast_clear_flag((&globalflags), IAX_FORCEJITTERBUF);   
09981    delete_users();
09982 }

static void set_timing ( void   )  [static]

Definition at line 9954 of file chan_iax2.c.

References ast_log(), and LOG_WARNING.

Referenced by set_config().

09955 {
09956 #ifdef HAVE_DAHDI
09957    int bs = trunkfreq * 8;
09958    if (timingfd > -1) {
09959       if (
09960 #ifdef DAHDI_TIMERACK
09961          ioctl(timingfd, DAHDI_TIMERCONFIG, &bs) &&
09962 #endif         
09963          ioctl(timingfd, DAHDI_SET_BLOCKSIZE, &bs))
09964          ast_log(LOG_WARNING, "Unable to set blocksize on timing source\n");
09965    }
09966 #endif
09967 }

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

Definition at line 755 of file chan_iax2.c.

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

Referenced by __schedule_action(), iax2_sched_add(), iax2_transmit(), and socket_read().

00756 {
00757    ast_mutex_lock(lock);
00758    ast_cond_signal(cond);
00759    ast_mutex_unlock(lock);
00760 }

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

Definition at line 7081 of file chan_iax2.c.

References chan_iax2_pvt::addr, iax_frame::af, iax_frame::afdatalen, chan_iax2_pvt::aseqno, ast_async_goto(), ast_best_codec(), ast_bridged_channel(), AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, AST_CAUSE_BUSY, AST_CAUSE_CONGESTION, AST_CAUSE_FACILITY_NOT_SUBSCRIBED, AST_CAUSE_NO_ROUTE_DESTINATION, 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_device_state_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_getformatname(), ast_iax2_new(), ast_inet_ntoa(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_mutex_lock(), ast_mutex_trylock(), ast_mutex_unlock(), ast_parking_ext(), ast_sched_del(), AST_SCHED_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_tvnow(), ast_tvzero(), ast_verbose(), 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, ast_iax2_meta_trunk_entry::callno, ast_iax2_mini_hdr::callno, iax2_registry::callno, iax_frame::callno, chan_iax2_pvt::capability, iax_ies::cause, iax_ies::causecode, check_access(), check_provisioning(), chan_iax2_pvt::chosenformat, chan_iax2_pvt::cid_num, cid_num, ast_iax2_meta_hdr::cmddata, iax_ies::codec_prefs, complete_dpreply(), complete_transfer(), construct_rr(), chan_iax2_pvt::context, ast_iax2_full_hdr::csub, ast_frame::data, ast_iax2_meta_trunk_hdr::data, ast_iax2_meta_hdr::data, ast_frame::datalen, ast_iax2_full_hdr::dcallno, DEADLOCK_AVOIDANCE, decrypt_frame(), iax_ies::devicetype, dp_lookup(), dpcache_lock, chan_iax2_pvt::encmethods, iax_ies::encmethods, chan_iax2_pvt::error, EVENT_FLAG_CALL, EVENT_FLAG_SYSTEM, exists(), exten, chan_iax2_pvt::exten, iax_frame::final, find_callno(), find_callno_locked(), find_tpeer(), fix_peerts(), iax2_dpcache::flags, iax_ies::format, format, chan_iax2_pvt::frames_received, ast_frame::frametype, iax_ies::fwdesc, ast_channel::hangupcause, iax2_peer::historicms, iax2_ack_registry(), 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_CODEC_NOCAP, IAX_CODEC_NOPREFS, IAX_CODEC_USER_FIRST, IAX_COMMAND_ACCEPT, IAX_COMMAND_ACK, IAX_COMMAND_AUTHREP, IAX_COMMAND_AUTHREQ, 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_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_DELAYPBXSTART, IAX_ENCRYPTED, iax_firmware_append(), IAX_FLAG_FULL, IAX_FLAG_RETRANS, 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_META_TRUNK, IAX_META_TRUNK_MINI, IAX_META_TRUNK_SUPERMINI, iax_park(), iax_parse_ies(), IAX_PROVISION, IAX_QUELCH, iax_showframe(), IAX_STATE_AUTHENTICATED, IAX_STATE_STARTED, IAX_STATE_TBD, IAX_STATE_UNCHANGED, IAX_TRUNK, iax_ies::iax_unknown, iaxfrdup2(), iaxq, inaddrcmp(), 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, iax2_peer::lastms, ast_frame::len, ast_iax2_meta_trunk_entry::len, ast_iax2_meta_trunk_mini::len, ast_channel::lock, iax2_trunk_peer::lock, LOG_DEBUG, LOG_ERROR, LOG_NOTICE, LOG_WARNING, make_trunk(), ast_frame::mallocd, manager_event(), iax2_peer::maxms, merge_encryption(), ast_iax2_meta_hdr::metacmd, ast_iax2_meta_trunk_mini::mini, chan_iax2_pvt::mohsuggest, iax_ies::musiconhold, iax2_peer::name, ast_channel::name, ast_channel::nativeformats, NEW_ALLOW, NEW_PREVENT, ast_frame::offset, option_debug, option_verbose, chan_iax2_pvt::oseqno, iax_frame::oseqno, ast_iax2_full_hdr::oseqno, iax_frame::outoforder, chan_iax2_pvt::owner, pbx_builtin_setvar_helper(), iax2_dpcache::peer, 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_iax2_queue::queue, raw_hangup(), ast_channel::readformat, iax_ies::refresh, REG_STATE_REJECTED, register_verify(), registry_authrequest(), registry_rerequest(), remove_by_peercallno(), iax_frame::retries, chan_iax2_pvt::rprefs, chan_iax2_pvt::rseqno, iax2_trunk_peer::rxtrunktime, S_OR, ast_frame::samples, 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(), iax_ies::serviceident, iax2_peer::smoothing, 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, iax2_trunk_peer::trunkact, try_transfer(), ast_iax2_full_hdr::ts, iax_frame::ts, ast_iax2_mini_hdr::ts, ast_iax2_meta_trunk_hdr::ts, ast_iax2_full_hdr::type, uncompress_subclass(), ast_channel::uniqueid, update_registry(), iax_ies::username, VERBOSE_PREFIX_3, 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().

07082 {
07083    struct sockaddr_in sin;
07084    int res;
07085    int updatehistory=1;
07086    int new = NEW_PREVENT;
07087    void *ptr;
07088    int dcallno = 0;
07089    struct ast_iax2_full_hdr *fh = (struct ast_iax2_full_hdr *)thread->buf;
07090    struct ast_iax2_mini_hdr *mh = (struct ast_iax2_mini_hdr *)thread->buf;
07091    struct ast_iax2_meta_hdr *meta = (struct ast_iax2_meta_hdr *)thread->buf;
07092    struct ast_iax2_video_hdr *vh = (struct ast_iax2_video_hdr *)thread->buf;
07093    struct ast_iax2_meta_trunk_hdr *mth;
07094    struct ast_iax2_meta_trunk_entry *mte;
07095    struct ast_iax2_meta_trunk_mini *mtm;
07096    struct iax_frame *fr;
07097    struct iax_frame *cur;
07098    struct ast_frame f = { 0, };
07099    struct ast_channel *c;
07100    struct iax2_dpcache *dp;
07101    struct iax2_peer *peer;
07102    struct iax2_trunk_peer *tpeer;
07103    struct timeval rxtrunktime;
07104    struct iax_ies ies;
07105    struct iax_ie_data ied0, ied1;
07106    int format;
07107    int fd;
07108    int exists;
07109    int minivid = 0;
07110    unsigned int ts;
07111    char empty[32]="";      /* Safety measure */
07112    struct iax_frame *duped_fr;
07113    char host_pref_buf[128];
07114    char caller_pref_buf[128];
07115    struct ast_codec_pref pref;
07116    char *using_prefs = "mine";
07117 
07118    /* allocate an iax_frame with 4096 bytes of data buffer */
07119    fr = alloca(sizeof(*fr) + 4096);
07120    memset(fr, 0, sizeof(*fr));
07121    fr->afdatalen = 4096; /* From alloca() above */
07122 
07123    /* Copy frequently used parameters to the stack */
07124    res = thread->buf_len;
07125    fd = thread->iofd;
07126    memcpy(&sin, &thread->iosin, sizeof(sin));
07127 
07128    if (res < sizeof(*mh)) {
07129       ast_log(LOG_WARNING, "midget packet received (%d of %zd min)\n", res, sizeof(*mh));
07130       return 1;
07131    }
07132    if ((vh->zeros == 0) && (ntohs(vh->callno) & 0x8000)) {
07133       if (res < sizeof(*vh)) {
07134          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));
07135          return 1;
07136       }
07137 
07138       /* This is a video frame, get call number */
07139       fr->callno = find_callno(ntohs(vh->callno) & ~0x8000, dcallno, &sin, new, fd, 0);
07140       minivid = 1;
07141    } else if ((meta->zeros == 0) && !(ntohs(meta->metacmd) & 0x8000)) {
07142       unsigned char metatype;
07143 
07144       if (res < sizeof(*meta)) {
07145          ast_log(LOG_WARNING, "Rejecting packet from '%s.%d' that is flagged as a meta frame but is too short\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
07146          return 1;
07147       }
07148 
07149       /* This is a meta header */
07150       switch(meta->metacmd) {
07151       case IAX_META_TRUNK:
07152          if (res < (sizeof(*meta) + sizeof(*mth))) {
07153             ast_log(LOG_WARNING, "midget meta trunk packet received (%d of %zd min)\n", res,
07154                sizeof(*meta) + sizeof(*mth));
07155             return 1;
07156          }
07157          mth = (struct ast_iax2_meta_trunk_hdr *)(meta->data);
07158          ts = ntohl(mth->ts);
07159          metatype = meta->cmddata;
07160          res -= (sizeof(*meta) + sizeof(*mth));
07161          ptr = mth->data;
07162          tpeer = find_tpeer(&sin, fd);
07163          if (!tpeer) {
07164             ast_log(LOG_WARNING, "Unable to accept trunked packet from '%s:%d': No matching peer\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
07165             return 1;
07166          }
07167          tpeer->trunkact = ast_tvnow();
07168          if (!ts || ast_tvzero(tpeer->rxtrunktime))
07169             tpeer->rxtrunktime = tpeer->trunkact;
07170          rxtrunktime = tpeer->rxtrunktime;
07171          ast_mutex_unlock(&tpeer->lock);
07172          while(res >= sizeof(*mte)) {
07173             /* Process channels */
07174             unsigned short callno, trunked_ts, len;
07175 
07176             if (metatype == IAX_META_TRUNK_MINI) {
07177                mtm = (struct ast_iax2_meta_trunk_mini *)ptr;
07178                ptr += sizeof(*mtm);
07179                res -= sizeof(*mtm);
07180                len = ntohs(mtm->len);
07181                callno = ntohs(mtm->mini.callno);
07182                trunked_ts = ntohs(mtm->mini.ts);
07183             } else if (metatype == IAX_META_TRUNK_SUPERMINI) {
07184                mte = (struct ast_iax2_meta_trunk_entry *)ptr;
07185                ptr += sizeof(*mte);
07186                res -= sizeof(*mte);
07187                len = ntohs(mte->len);
07188                callno = ntohs(mte->callno);
07189                trunked_ts = 0;
07190             } else {
07191                ast_log(LOG_WARNING, "Unknown meta trunk cmd from '%s:%d': dropping\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
07192                break;
07193             }
07194             /* Stop if we don't have enough data */
07195             if (len > res)
07196                break;
07197             fr->callno = find_callno_locked(callno & ~IAX_FLAG_FULL, 0, &sin, NEW_PREVENT, fd, 0);
07198             if (fr->callno) {
07199                /* If it's a valid call, deliver the contents.  If not, we
07200                   drop it, since we don't have a scallno to use for an INVAL */
07201                /* Process as a mini frame */
07202                memset(&f, 0, sizeof(f));
07203                f.frametype = AST_FRAME_VOICE;
07204                if (iaxs[fr->callno]) {
07205                   if (iaxs[fr->callno]->voiceformat > 0) {
07206                      f.subclass = iaxs[fr->callno]->voiceformat;
07207                      f.datalen = len;
07208                      if (f.datalen >= 0) {
07209                         if (f.datalen)
07210                            f.data = ptr;
07211                         if(trunked_ts) {
07212                            fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | (trunked_ts & 0xffff);
07213                         } else
07214                            fr->ts = fix_peerts(&rxtrunktime, fr->callno, ts);
07215                         /* Don't pass any packets until we're started */
07216                         if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
07217                            /* Common things */
07218                            f.src = "IAX2";
07219                            if (f.datalen && (f.frametype == AST_FRAME_VOICE)) 
07220                               f.samples = ast_codec_get_samples(&f);
07221                            iax_frame_wrap(fr, &f);
07222                            duped_fr = iaxfrdup2(fr);
07223                            if (duped_fr) {
07224                               schedule_delivery(duped_fr, updatehistory, 1, &fr->ts);
07225                            }
07226                            /* It is possible for the pvt structure to go away after we call schedule_delivery */
07227                            if (fr && fr->callno && iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts) {
07228                               iaxs[fr->callno]->last = fr->ts;
07229 #if 1
07230                               if (option_debug && iaxdebug)
07231                                  ast_log(LOG_DEBUG, "For call=%d, set last=%d\n", fr->callno, fr->ts);
07232 #endif
07233                            }
07234                         }
07235                      } else {
07236                         ast_log(LOG_WARNING, "Datalen < 0?\n");
07237                      }
07238                   } else {
07239                      ast_log(LOG_WARNING, "Received trunked frame before first full voice frame\n");
07240                      iax2_vnak(fr->callno);
07241                   }
07242                }
07243                ast_mutex_unlock(&iaxsl[fr->callno]);
07244             }
07245             ptr += len;
07246             res -= len;
07247          }
07248          
07249       }
07250       return 1;
07251    }
07252 
07253 #ifdef DEBUG_SUPPORT
07254    if (iaxdebug && (res >= sizeof(*fh)))
07255       iax_showframe(NULL, fh, 1, &sin, res - sizeof(*fh));
07256 #endif
07257    if (ntohs(mh->callno) & IAX_FLAG_FULL) {
07258       if (res < sizeof(*fh)) {
07259          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));
07260          return 1;
07261       }
07262 
07263       /* Get the destination call number */
07264       dcallno = ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS;
07265       /* Retrieve the type and subclass */
07266       f.frametype = fh->type;
07267       if (f.frametype == AST_FRAME_VIDEO) {
07268          f.subclass = uncompress_subclass(fh->csub & ~0x40) | ((fh->csub >> 6) & 0x1);
07269       } else {
07270          f.subclass = uncompress_subclass(fh->csub);
07271       }
07272 
07273       /* Deal with POKE/PONG without allocating a callno */
07274       if (f.frametype == AST_FRAME_IAX && f.subclass == IAX_COMMAND_POKE) {
07275          /* Reply back with a PONG, but don't care about the result. */
07276          send_apathetic_reply(1, ntohs(fh->scallno), &sin, IAX_COMMAND_PONG, ntohs(fh->ts), fh->iseqno + 1);
07277          return 1;
07278       } else if (f.frametype == AST_FRAME_IAX && f.subclass == IAX_COMMAND_ACK && dcallno == 1) {
07279          /* Ignore */
07280          return 1;
07281       }
07282 
07283       if ((f.frametype == AST_FRAME_IAX) && ((f.subclass == IAX_COMMAND_NEW) || (f.subclass == IAX_COMMAND_REGREQ) ||
07284                          (f.subclass == IAX_COMMAND_POKE) || (f.subclass == IAX_COMMAND_FWDOWNL) ||
07285                          (f.subclass == IAX_COMMAND_REGREL)))
07286          new = NEW_ALLOW;
07287    } else {
07288       /* Don't know anything about it yet */
07289       f.frametype = AST_FRAME_NULL;
07290       f.subclass = 0;
07291    }
07292 
07293    if (!fr->callno) {
07294       int check_dcallno = 0;
07295 
07296       /*
07297        * We enforce accurate destination call numbers for all full frames except
07298        * LAGRQ and PING commands.  This is because older versions of Asterisk
07299        * schedule these commands to get sent very quickly, and they will sometimes
07300        * be sent before they receive the first frame from the other side.  When
07301        * that happens, it doesn't contain the destination call number.  However,
07302        * not checking it for these frames is safe.
07303        * 
07304        * Discussed in the following thread:
07305        *    http://lists.digium.com/pipermail/asterisk-dev/2008-May/033217.html 
07306        */
07307 
07308       if (ntohs(mh->callno) & IAX_FLAG_FULL) {
07309          check_dcallno = f.frametype == AST_FRAME_IAX ? (f.subclass != IAX_COMMAND_PING && f.subclass != IAX_COMMAND_LAGRQ) : 1;
07310       }
07311 
07312       fr->callno = find_callno(ntohs(mh->callno) & ~IAX_FLAG_FULL, dcallno, &sin, new, fd, check_dcallno);
07313    }
07314 
07315    if (fr->callno > 0)
07316       ast_mutex_lock(&iaxsl[fr->callno]);
07317 
07318    if (!fr->callno || !iaxs[fr->callno]) {
07319       /* A call arrived for a nonexistent destination.  Unless it's an "inval"
07320          frame, reply with an inval */
07321       if (ntohs(mh->callno) & IAX_FLAG_FULL) {
07322          /* We can only raw hangup control frames */
07323          if (((f.subclass != IAX_COMMAND_INVAL) &&
07324              (f.subclass != IAX_COMMAND_TXCNT) &&
07325              (f.subclass != IAX_COMMAND_TXACC) &&
07326              (f.subclass != IAX_COMMAND_FWDOWNL))||
07327              (f.frametype != AST_FRAME_IAX))
07328             raw_hangup(&sin, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS, ntohs(mh->callno) & ~IAX_FLAG_FULL,
07329             fd);
07330       }
07331       if (fr->callno > 0) 
07332          ast_mutex_unlock(&iaxsl[fr->callno]);
07333       return 1;
07334    }
07335    if (ast_test_flag(iaxs[fr->callno], IAX_ENCRYPTED)) {
07336       if (decrypt_frame(fr->callno, fh, &f, &res)) {
07337          ast_log(LOG_NOTICE, "Packet Decrypt Failed!\n");
07338          ast_mutex_unlock(&iaxsl[fr->callno]);
07339          return 1;
07340       }
07341 #ifdef DEBUG_SUPPORT
07342       else if (iaxdebug)
07343          iax_showframe(NULL, fh, 3, &sin, res - sizeof(*fh));
07344 #endif
07345    }
07346 
07347    /* count this frame */
07348    iaxs[fr->callno]->frames_received++;
07349 
07350    if (!inaddrcmp(&sin, &iaxs[fr->callno]->addr) && !minivid &&
07351       f.subclass != IAX_COMMAND_TXCNT &&     /* for attended transfer */
07352       f.subclass != IAX_COMMAND_TXACC) {     /* for attended transfer */
07353       unsigned short new_peercallno;
07354 
07355       new_peercallno = (unsigned short) (ntohs(mh->callno) & ~IAX_FLAG_FULL);
07356       if (new_peercallno && new_peercallno != iaxs[fr->callno]->peercallno) {
07357          if (iaxs[fr->callno]->peercallno) {
07358             remove_by_peercallno(iaxs[fr->callno]);
07359          }
07360          iaxs[fr->callno]->peercallno = new_peercallno;
07361          store_by_peercallno(iaxs[fr->callno]);
07362       }
07363    }
07364    if (ntohs(mh->callno) & IAX_FLAG_FULL) {
07365       if (option_debug  && iaxdebug)
07366          ast_log(LOG_DEBUG, "Received packet %d, (%d, %d)\n", fh->oseqno, f.frametype, f.subclass);
07367       /* Check if it's out of order (and not an ACK or INVAL) */
07368       fr->oseqno = fh->oseqno;
07369       fr->iseqno = fh->iseqno;
07370       fr->ts = ntohl(fh->ts);
07371 #ifdef IAXTESTS
07372       if (test_resync) {
07373          if (option_debug)
07374             ast_log(LOG_DEBUG, "Simulating frame ts resync, was %u now %u\n", fr->ts, fr->ts + test_resync);
07375          fr->ts += test_resync;
07376       }
07377 #endif /* IAXTESTS */
07378 #if 0
07379       if ( (ntohs(fh->dcallno) & IAX_FLAG_RETRANS) ||
07380            ( (f.frametype != AST_FRAME_VOICE) && ! (f.frametype == AST_FRAME_IAX &&
07381                         (f.subclass == IAX_COMMAND_NEW ||
07382                          f.subclass == IAX_COMMAND_AUTHREQ ||
07383                          f.subclass == IAX_COMMAND_ACCEPT ||
07384                          f.subclass == IAX_COMMAND_REJECT))      ) )
07385 #endif
07386       if ((ntohs(fh->dcallno) & IAX_FLAG_RETRANS) || (f.frametype != AST_FRAME_VOICE))
07387          updatehistory = 0;
07388       if ((iaxs[fr->callno]->iseqno != fr->oseqno) &&
07389          (iaxs[fr->callno]->iseqno ||
07390             ((f.subclass != IAX_COMMAND_TXCNT) &&
07391             (f.subclass != IAX_COMMAND_TXREADY) &&    /* for attended transfer */
07392             (f.subclass != IAX_COMMAND_TXREL) &&      /* for attended transfer */
07393             (f.subclass != IAX_COMMAND_UNQUELCH ) &&  /* for attended transfer */
07394             (f.subclass != IAX_COMMAND_TXACC)) ||
07395             (f.frametype != AST_FRAME_IAX))) {
07396          if (
07397           ((f.subclass != IAX_COMMAND_ACK) &&
07398            (f.subclass != IAX_COMMAND_INVAL) &&
07399            (f.subclass != IAX_COMMAND_TXCNT) &&
07400            (f.subclass != IAX_COMMAND_TXREADY) &&     /* for attended transfer */
07401            (f.subclass != IAX_COMMAND_TXREL) &&    /* for attended transfer */
07402            (f.subclass != IAX_COMMAND_UNQUELCH ) &&   /* for attended transfer */
07403            (f.subclass != IAX_COMMAND_TXACC) &&
07404            (f.subclass != IAX_COMMAND_VNAK)) ||
07405            (f.frametype != AST_FRAME_IAX)) {
07406             /* If it's not an ACK packet, it's out of order. */
07407             if (option_debug)
07408                ast_log(LOG_DEBUG, "Packet arrived out of order (expecting %d, got %d) (frametype = %d, subclass = %d)\n", 
07409                   iaxs[fr->callno]->iseqno, fr->oseqno, f.frametype, f.subclass);
07410             /* Check to see if we need to request retransmission,
07411              * and take sequence number wraparound into account */
07412             if ((unsigned char) (iaxs[fr->callno]->iseqno - fr->oseqno) < 128) {
07413                /* If we've already seen it, ack it XXX There's a border condition here XXX */
07414                if ((f.frametype != AST_FRAME_IAX) || 
07415                      ((f.subclass != IAX_COMMAND_ACK) && (f.subclass != IAX_COMMAND_INVAL))) {
07416                   if (option_debug)
07417                      ast_log(LOG_DEBUG, "Acking anyway\n");
07418                   /* XXX Maybe we should handle its ack to us, but then again, it's probably outdated anyway, and if
07419                      we have anything to send, we'll retransmit and get an ACK back anyway XXX */
07420                   send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
07421                }
07422             } else {
07423                /* Send a VNAK requesting retransmission */
07424                iax2_vnak(fr->callno);
07425             }
07426             ast_mutex_unlock(&iaxsl[fr->callno]);
07427             return 1;
07428          }
07429       } else {
07430          /* Increment unless it's an ACK or VNAK */
07431          if (((f.subclass != IAX_COMMAND_ACK) &&
07432              (f.subclass != IAX_COMMAND_INVAL) &&
07433              (f.subclass != IAX_COMMAND_TXCNT) &&
07434              (f.subclass != IAX_COMMAND_TXACC) &&
07435             (f.subclass != IAX_COMMAND_VNAK)) ||
07436              (f.frametype != AST_FRAME_IAX))
07437             iaxs[fr->callno]->iseqno++;
07438       }
07439       /* A full frame */
07440       if (res < sizeof(*fh)) {
07441          ast_log(LOG_WARNING, "midget packet received (%d of %zd min)\n", res, sizeof(*fh));
07442          ast_mutex_unlock(&iaxsl[fr->callno]);
07443          return 1;
07444       }
07445       /* Ensure text frames are NULL-terminated */
07446       if (f.frametype == AST_FRAME_TEXT && thread->buf[res - 1] != '\0') {
07447          if (res < thread->buf_size)
07448             thread->buf[res++] = '\0';
07449          else /* Trims one character from the text message, but that's better than overwriting the end of the buffer. */
07450             thread->buf[res - 1] = '\0';
07451       }
07452       f.datalen = res - sizeof(*fh);
07453 
07454       /* Handle implicit ACKing unless this is an INVAL, and only if this is 
07455          from the real peer, not the transfer peer */
07456       if (!inaddrcmp(&sin, &iaxs[fr->callno]->addr) && 
07457           ((f.subclass != IAX_COMMAND_INVAL) ||
07458            (f.frametype != AST_FRAME_IAX))) {
07459          unsigned char x;
07460          int call_to_destroy;
07461          /* XXX This code is not very efficient.  Surely there is a better way which still
07462                 properly handles boundary conditions? XXX */
07463          /* First we have to qualify that the ACKed value is within our window */
07464          for (x=iaxs[fr->callno]->rseqno; x != iaxs[fr->callno]->oseqno; x++)
07465             if (fr->iseqno == x)
07466                break;
07467          if ((x != iaxs[fr->callno]->oseqno) || (iaxs[fr->callno]->oseqno == fr->iseqno)) {
07468             /* The acknowledgement is within our window.  Time to acknowledge everything
07469                that it says to */
07470             for (x=iaxs[fr->callno]->rseqno; x != fr->iseqno; x++) {
07471                /* Ack the packet with the given timestamp */
07472                if (option_debug && iaxdebug)
07473                   ast_log(LOG_DEBUG, "Cancelling transmission of packet %d\n", x);
07474                call_to_destroy = 0;
07475                AST_LIST_LOCK(&iaxq.queue);
07476                AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
07477                   /* If it's our call, and our timestamp, mark -1 retries */
07478                   if ((fr->callno == cur->callno) && (x == cur->oseqno)) {
07479                      cur->retries = -1;
07480                      /* Destroy call if this is the end */
07481                      if (cur->final)
07482                         call_to_destroy = fr->callno;
07483                   }
07484                }
07485                AST_LIST_UNLOCK(&iaxq.queue);
07486                if (call_to_destroy) {
07487                   if (iaxdebug && option_debug)
07488                      ast_log(LOG_DEBUG, "Really destroying %d, having been acked on final message\n", call_to_destroy);
07489                   ast_mutex_lock(&iaxsl[call_to_destroy]);
07490                   iax2_destroy(call_to_destroy);
07491                   ast_mutex_unlock(&iaxsl[call_to_destroy]);
07492                }
07493             }
07494             /* Note how much we've received acknowledgement for */
07495             if (iaxs[fr->callno])
07496                iaxs[fr->callno]->rseqno = fr->iseqno;
07497             else {
07498                /* Stop processing now */
07499                ast_mutex_unlock(&iaxsl[fr->callno]);
07500                return 1;
07501             }
07502          } else if (option_debug)
07503             ast_log(LOG_DEBUG, "Received iseqno %d not within window %d->%d\n", fr->iseqno, iaxs[fr->callno]->rseqno, iaxs[fr->callno]->oseqno);
07504       }
07505       if (inaddrcmp(&sin, &iaxs[fr->callno]->addr) && 
07506          ((f.frametype != AST_FRAME_IAX) || 
07507           ((f.subclass != IAX_COMMAND_TXACC) &&
07508            (f.subclass != IAX_COMMAND_TXCNT)))) {
07509          /* Only messages we accept from a transfer host are TXACC and TXCNT */
07510          ast_mutex_unlock(&iaxsl[fr->callno]);
07511          return 1;
07512       }
07513 
07514       if (f.datalen) {
07515          if (f.frametype == AST_FRAME_IAX) {
07516             if (iax_parse_ies(&ies, thread->buf + sizeof(*fh), f.datalen)) {
07517                ast_log(LOG_WARNING, "Undecodable frame received from '%s'\n", ast_inet_ntoa(sin.sin_addr));
07518                ast_mutex_unlock(&iaxsl[fr->callno]);
07519                return 1;
07520             }
07521             f.data = NULL;
07522             f.datalen = 0;
07523          } else
07524             f.data = thread->buf + sizeof(*fh);
07525       } else {
07526          if (f.frametype == AST_FRAME_IAX)
07527             f.data = NULL;
07528          else
07529             f.data = empty;
07530          memset(&ies, 0, sizeof(ies));
07531       }
07532 
07533       /* when we receive the first full frame for a new incoming channel,
07534          it is safe to start the PBX on the channel because we have now
07535          completed a 3-way handshake with the peer */
07536       if ((f.frametype == AST_FRAME_VOICE) ||
07537           (f.frametype == AST_FRAME_VIDEO) ||
07538           (f.frametype == AST_FRAME_IAX)) {
07539          if (ast_test_flag(iaxs[fr->callno], IAX_DELAYPBXSTART)) {
07540             ast_clear_flag(iaxs[fr->callno], IAX_DELAYPBXSTART);
07541             if (!ast_iax2_new(fr->callno, AST_STATE_RING, iaxs[fr->callno]->chosenformat)) {
07542                ast_mutex_unlock(&iaxsl[fr->callno]);
07543                return 1;
07544             }
07545          }
07546       }
07547 
07548       if (f.frametype == AST_FRAME_VOICE) {
07549          if (f.subclass != iaxs[fr->callno]->voiceformat) {
07550                iaxs[fr->callno]->voiceformat = f.subclass;
07551                if (option_debug)
07552                   ast_log(LOG_DEBUG, "Ooh, voice format changed to %d\n", f.subclass);
07553                if (iaxs[fr->callno]->owner) {
07554                   int orignative;
07555 retryowner:
07556                   if (ast_mutex_trylock(&iaxs[fr->callno]->owner->lock)) {
07557                      DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]);
07558                      if (iaxs[fr->callno] && iaxs[fr->callno]->owner) goto retryowner;
07559                   }
07560                   if (iaxs[fr->callno]) {
07561                      if (iaxs[fr->callno]->owner) {
07562                         orignative = iaxs[fr->callno]->owner->nativeformats;
07563                         iaxs[fr->callno]->owner->nativeformats = f.subclass;
07564                         if (iaxs[fr->callno]->owner->readformat)
07565                            ast_set_read_format(iaxs[fr->callno]->owner, iaxs[fr->callno]->owner->readformat);
07566                         iaxs[fr->callno]->owner->nativeformats = orignative;
07567                         ast_mutex_unlock(&iaxs[fr->callno]->owner->lock);
07568                      }
07569                   } else {
07570                      if (option_debug)
07571                         ast_log(LOG_DEBUG, "Neat, somebody took away the channel at a magical time but i found it!\n");
07572                      ast_mutex_unlock(&iaxsl[fr->callno]);
07573                      return 1;
07574                   }
07575                }
07576          }
07577       }
07578       if (f.frametype == AST_FRAME_VIDEO) {
07579          if (f.subclass != iaxs[fr->callno]->videoformat) {
07580             if (option_debug)
07581                ast_log(LOG_DEBUG, "Ooh, video format changed to %d\n", f.subclass & ~0x1);
07582             iaxs[fr->callno]->videoformat = f.subclass & ~0x1;
07583          }
07584       }
07585       if (f.frametype == AST_FRAME_CONTROL && iaxs[fr->callno]->owner) {
07586          if (f.subclass == AST_CONTROL_BUSY) {
07587             iaxs[fr->callno]->owner->hangupcause = AST_CAUSE_BUSY;
07588          } else if (f.subclass == AST_CONTROL_CONGESTION) {
07589             iaxs[fr->callno]->owner->hangupcause = AST_CAUSE_CONGESTION;
07590          }
07591       }
07592       if (f.frametype == AST_FRAME_IAX) {
07593          AST_SCHED_DEL(sched, iaxs[fr->callno]->initid);
07594          /* Handle the IAX pseudo frame itself */
07595          if (option_debug && iaxdebug)
07596             ast_log(LOG_DEBUG, "IAX subclass %d received\n", f.subclass);
07597 
07598                         /* Update last ts unless the frame's timestamp originated with us. */
07599          if (iaxs[fr->callno]->last < fr->ts &&
07600                             f.subclass != IAX_COMMAND_ACK &&
07601                             f.subclass != IAX_COMMAND_PONG &&
07602                             f.subclass != IAX_COMMAND_LAGRP) {
07603             iaxs[fr->callno]->last = fr->ts;
07604             if (option_debug && iaxdebug)
07605                ast_log(LOG_DEBUG, "For call=%d, set last=%d\n", fr->callno, fr->ts);
07606          }
07607 
07608          switch(f.subclass) {
07609          case IAX_COMMAND_ACK:
07610             /* Do nothing */
07611             break;
07612          case IAX_COMMAND_QUELCH:
07613             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
07614                     /* Generate Manager Hold event, if necessary*/
07615                if (iaxs[fr->callno]->owner) {
07616                   manager_event(EVENT_FLAG_CALL, "Hold",
07617                      "Channel: %s\r\n"
07618                      "Uniqueid: %s\r\n",
07619                      iaxs[fr->callno]->owner->name, 
07620                      iaxs[fr->callno]->owner->uniqueid);
07621                }
07622 
07623                ast_set_flag(iaxs[fr->callno], IAX_QUELCH);
07624                if (ies.musiconhold) {
07625                   if (iaxs[fr->callno]->owner && ast_bridged_channel(iaxs[fr->callno]->owner)) {
07626                      const char *mohsuggest = iaxs[fr->callno]->mohsuggest;
07627                      iax2_queue_control_data(fr->callno, AST_CONTROL_HOLD, 
07628                         S_OR(mohsuggest, NULL),
07629                         !ast_strlen_zero(mohsuggest) ? strlen(mohsuggest) + 1 : 0);
07630                      if (!iaxs[fr->callno]) {
07631                         ast_mutex_unlock(&iaxsl[fr->callno]);
07632                         return 1;
07633                      }
07634                   }
07635                }
07636             }
07637             break;
07638          case IAX_COMMAND_UNQUELCH:
07639             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
07640                     /* Generate Manager Unhold event, if necessary*/
07641                if (iaxs[fr->callno]->owner && ast_test_flag(iaxs[fr->callno], IAX_QUELCH)) {
07642                   manager_event(EVENT_FLAG_CALL, "Unhold",
07643                      "Channel: %s\r\n"
07644                      "Uniqueid: %s\r\n",
07645                      iaxs[fr->callno]->owner->name, 
07646                      iaxs[fr->callno]->owner->uniqueid);
07647                }
07648 
07649                ast_clear_flag(iaxs[fr->callno], IAX_QUELCH);
07650                if (iaxs[fr->callno]->owner && ast_bridged_channel(iaxs[fr->callno]->owner)) {
07651                   iax2_queue_control_data(fr->callno, AST_CONTROL_UNHOLD, NULL, 0);
07652                   if (!iaxs[fr->callno]) {
07653                      ast_mutex_unlock(&iaxsl[fr->callno]);
07654                      return 1;
07655                   }
07656                }
07657             }
07658             break;
07659          case IAX_COMMAND_TXACC:
07660             if (iaxs[fr->callno]->transferring == TRANSFER_BEGIN) {
07661                /* Ack the packet with the given timestamp */
07662                AST_LIST_LOCK(&iaxq.queue);
07663                AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
07664                   /* Cancel any outstanding txcnt's */
07665                   if ((fr->callno == cur->callno) && (cur->transfer))
07666                      cur->retries = -1;
07667                }
07668                AST_LIST_UNLOCK(&iaxq.queue);
07669                memset(&ied1, 0, sizeof(ied1));
07670                iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->callno);
07671                send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREADY, 0, ied1.buf, ied1.pos, -1);
07672                iaxs[fr->callno]->transferring = TRANSFER_READY;
07673             }
07674             break;
07675          case IAX_COMMAND_NEW:
07676             /* Ignore if it's already up */
07677             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD))
07678                break;
07679             if (ies.provverpres && ies.serviceident && sin.sin_addr.s_addr) {
07680                ast_mutex_unlock(&iaxsl[fr->callno]);
07681                check_provisioning(&sin, fd, ies.serviceident, ies.provver);
07682                ast_mutex_lock(&iaxsl[fr->callno]);
07683                if (!iaxs[fr->callno]) {
07684                   ast_mutex_unlock(&iaxsl[fr->callno]);
07685                   return 1;
07686                }
07687             }
07688             /* If we're in trunk mode, do it now, and update the trunk number in our frame before continuing */
07689             if (ast_test_flag(iaxs[fr->callno], IAX_TRUNK)) {
07690                int new_callno;
07691                if ((new_callno = make_trunk(fr->callno, 1)) != -1)
07692                   fr->callno = new_callno;
07693             }
07694             /* For security, always ack immediately */
07695             if (delayreject)
07696                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
07697             if (check_access(fr->callno, &sin, &ies)) {
07698                /* They're not allowed on */
07699                auth_fail(fr->callno, IAX_COMMAND_REJECT);
07700                if (authdebug)
07701                   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);
07702                break;
07703             }
07704             if (strcasecmp(iaxs[fr->callno]->exten, "TBD")) {
07705                const char *context, *exten, *cid_num;
07706 
07707                context = ast_strdupa(iaxs[fr->callno]->context);
07708                exten = ast_strdupa(iaxs[fr->callno]->exten);
07709                cid_num = ast_strdupa(iaxs[fr->callno]->cid_num);
07710 
07711                /* This might re-enter the IAX code and need the lock */
07712                ast_mutex_unlock(&iaxsl[fr->callno]);
07713                exists = ast_exists_extension(NULL, context, exten, 1, cid_num);
07714                ast_mutex_lock(&iaxsl[fr->callno]);
07715 
07716                if (!iaxs[fr->callno]) {
07717                   ast_mutex_unlock(&iaxsl[fr->callno]);
07718                   return 1;
07719                }
07720             } else
07721                exists = 0;
07722             if (ast_strlen_zero(iaxs[fr->callno]->secret) && ast_strlen_zero(iaxs[fr->callno]->inkeys)) {
07723                if (strcmp(iaxs[fr->callno]->exten, "TBD") && !exists) {
07724                   memset(&ied0, 0, sizeof(ied0));
07725                   iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
07726                   iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
07727                   send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
07728                   if (!iaxs[fr->callno]) {
07729                      ast_mutex_unlock(&iaxsl[fr->callno]);
07730                      return 1;
07731                   }
07732                   if (authdebug)
07733                      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);
07734                } else {
07735                   /* Select an appropriate format */
07736 
07737                   if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
07738                      if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
07739                         using_prefs = "reqonly";
07740                      } else {
07741                         using_prefs = "disabled";
07742                      }
07743                      format = iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability;
07744                      memset(&pref, 0, sizeof(pref));
07745                      strcpy(caller_pref_buf, "disabled");
07746                      strcpy(host_pref_buf, "disabled");
07747                   } else {
07748                      using_prefs = "mine";
07749                      /* If the information elements are in here... use them */
07750                      if (ies.codec_prefs)
07751                         ast_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
07752                      if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
07753                         /* If we are codec_first_choice we let the caller have the 1st shot at picking the codec.*/
07754                         if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
07755                            pref = iaxs[fr->callno]->rprefs;
07756                            using_prefs = "caller";
07757                         } else {
07758                            pref = iaxs[fr->callno]->prefs;
07759                         }
07760                      } else
07761                         pref = iaxs[fr->callno]->prefs;
07762                      
07763                      format = ast_codec_choose(&pref, iaxs[fr->callno]->capability & iaxs[fr->callno]->peercapability, 0);
07764                      ast_codec_pref_string(&iaxs[fr->callno]->rprefs, caller_pref_buf, sizeof(caller_pref_buf) - 1);
07765                      ast_codec_pref_string(&iaxs[fr->callno]->prefs, host_pref_buf, sizeof(host_pref_buf) - 1);
07766                   }
07767                   if (!format) {
07768                      if(!ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP))
07769                         format = iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability;
07770                      if (!format) {
07771                         memset(&ied0, 0, sizeof(ied0));
07772                         iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
07773                         iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
07774                         send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
07775                         if (!iaxs[fr->callno]) {
07776                            ast_mutex_unlock(&iaxsl[fr->callno]);
07777                            return 1;
07778                         }
07779                         if (authdebug) {
07780                            if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP))
07781                               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);
07782                            else 
07783                               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);
07784                         }
07785                      } else {
07786                         /* Pick one... */
07787                         if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
07788                            if(!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability))
07789                               format = 0;
07790                         } else {
07791                            if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
07792                               using_prefs = ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
07793                               memset(&pref, 0, sizeof(pref));
07794                               format = ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
07795                               strcpy(caller_pref_buf,"disabled");
07796                               strcpy(host_pref_buf,"disabled");
07797                            } else {
07798                               using_prefs = "mine";
07799                               if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
07800                                  /* Do the opposite of what we tried above. */
07801                                  if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
07802                                     pref = iaxs[fr->callno]->prefs;                       
07803                                  } else {
07804                                     pref = iaxs[fr->callno]->rprefs;
07805                                     using_prefs = "caller";
07806                                  }
07807                                  format = ast_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability, 1);
07808                            
07809                               } else /* if no codec_prefs IE do it the old way */
07810                                  format = ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability); 
07811                            }
07812                         }
07813 
07814                         if (!format) {
07815                            memset(&ied0, 0, sizeof(ied0));
07816                            iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
07817                            iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
07818                            ast_log(LOG_ERROR, "No best format in 0x%x???\n", iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
07819                            send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
07820                            if (!iaxs[fr->callno]) {
07821                               ast_mutex_unlock(&iaxsl[fr->callno]);
07822                               return 1;
07823                            }
07824                            if (authdebug)
07825                               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);
07826                            ast_set_flag(iaxs[fr->callno], IAX_ALREADYGONE);   
07827                            break;
07828                         }
07829                      }
07830                   }
07831                   if (format) {
07832                      /* No authentication required, let them in */
07833                      memset(&ied1, 0, sizeof(ied1));
07834                      iax_ie_append_int(&ied1, IAX_IE_FORMAT, format);
07835                      send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACCEPT, 0, ied1.buf, ied1.pos, -1);
07836                      if (strcmp(iaxs[fr->callno]->exten, "TBD")) {
07837                         ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
07838                         if (option_verbose > 2) 
07839                            ast_verbose(VERBOSE_PREFIX_3 "Accepting UNAUTHENTICATED call from %s:\n"
07840                                     "%srequested format = %s,\n"
07841                                     "%srequested prefs = %s,\n"
07842                                     "%sactual format = %s,\n"
07843                                     "%shost prefs = %s,\n"
07844                                     "%spriority = %s\n",
07845                                     ast_inet_ntoa(sin.sin_addr), 
07846                                     VERBOSE_PREFIX_4,
07847                                     ast_getformatname(iaxs[fr->callno]->peerformat), 
07848                                     VERBOSE_PREFIX_4,
07849                                     caller_pref_buf,
07850                                     VERBOSE_PREFIX_4,
07851                                     ast_getformatname(format), 
07852                                     VERBOSE_PREFIX_4,
07853                                     host_pref_buf, 
07854                                     VERBOSE_PREFIX_4,
07855                                     using_prefs);
07856                         
07857                         iaxs[fr->callno]->chosenformat = format;
07858                         ast_set_flag(iaxs[fr->callno], IAX_DELAYPBXSTART);
07859                      } else {
07860                         ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
07861                         /* If this is a TBD call, we're ready but now what...  */
07862                         if (option_verbose > 2)
07863                            ast_verbose(VERBOSE_PREFIX_3 "Accepted unauthenticated TBD call from %s\n", ast_inet_ntoa(sin.sin_addr));
07864                      }
07865                   }
07866                }
07867                break;
07868             }
07869             if (iaxs[fr->callno]->authmethods & IAX_AUTH_MD5)
07870                merge_encryption(iaxs[fr->callno],ies.encmethods);
07871             else
07872                iaxs[fr->callno]->encmethods = 0;
07873             if (!authenticate_request(fr->callno) && iaxs[fr->callno])
07874                ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_AUTHENTICATED);
07875             if (!iaxs[fr->callno]) {
07876                ast_mutex_unlock(&iaxsl[fr->callno]);
07877                return 1;
07878             }
07879             break;
07880          case IAX_COMMAND_DPREQ:
07881             /* Request status in the dialplan */
07882             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD) &&
07883                !ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED) && ies.called_number) {
07884                if (iaxcompat) {
07885                   /* Spawn a thread for the lookup */
07886                   spawn_dp_lookup(fr->callno, iaxs[fr->callno]->context, ies.called_number, iaxs[fr->callno]->cid_num);
07887                } else {
07888                   /* Just look it up */
07889                   dp_lookup(fr->callno, iaxs[fr->callno]->context, ies.called_number, iaxs[fr->callno]->cid_num, 1);
07890                }
07891             }
07892             break;
07893          case IAX_COMMAND_HANGUP:
07894             ast_set_flag(iaxs[fr->callno], IAX_ALREADYGONE);
07895             if (option_debug)
07896                ast_log(LOG_DEBUG, "Immediately destroying %d, having received hangup\n", fr->callno);
07897             /* Set hangup cause according to remote */
07898             if (ies.causecode && iaxs[fr->callno]->owner)
07899                iaxs[fr->callno]->owner->hangupcause = ies.causecode;
07900             /* Send ack immediately, before we destroy */
07901             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
07902             iax2_destroy(fr->callno);
07903             break;
07904          case IAX_COMMAND_REJECT:
07905             /* Set hangup cause according to remote */
07906             if (ies.causecode && iaxs[fr->callno]->owner)
07907                iaxs[fr->callno]->owner->hangupcause = ies.causecode;
07908 
07909             if (!ast_test_flag(iaxs[fr->callno], IAX_PROVISION)) {
07910                if (iaxs[fr->callno]->owner && authdebug)
07911                   ast_log(LOG_WARNING, "Call rejected by %s: %s\n",
07912                      ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr),
07913                      ies.cause ? ies.cause : "<Unknown>");
07914                if (option_debug)
07915                   ast_log(LOG_DEBUG, "Immediately destroying %d, having received reject\n",
07916                      fr->callno);
07917             }
07918             /* Send ack immediately, before we destroy */
07919             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK,
07920                          fr->ts, NULL, 0, fr->iseqno);
07921             if (!ast_test_flag(iaxs[fr->callno], IAX_PROVISION))
07922                iaxs[fr->callno]->error = EPERM;
07923             iax2_destroy(fr->callno);
07924             break;
07925          case IAX_COMMAND_TRANSFER:
07926          {
07927             struct ast_channel *bridged_chan;
07928 
07929             if (iaxs[fr->callno]->owner && (bridged_chan = ast_bridged_channel(iaxs[fr->callno]->owner)) && ies.called_number) {
07930                /* Set BLINDTRANSFER channel variables */
07931 
07932                ast_mutex_unlock(&iaxsl[fr->callno]);
07933                pbx_builtin_setvar_helper(iaxs[fr->callno]->owner, "BLINDTRANSFER", bridged_chan->name);
07934                ast_mutex_lock(&iaxsl[fr->callno]);
07935                if (!iaxs[fr->callno]) {
07936                   ast_mutex_unlock(&iaxsl[fr->callno]);
07937                   return 1;
07938                }
07939 
07940                pbx_builtin_setvar_helper(bridged_chan, "BLINDTRANSFER", iaxs[fr->callno]->owner->name);
07941                if (!strcmp(ies.called_number, ast_parking_ext())) {
07942                   struct ast_channel *saved_channel = iaxs[fr->callno]->owner;
07943                   ast_mutex_unlock(&iaxsl[fr->callno]);
07944                   if (iax_park(bridged_chan, saved_channel)) {
07945                      ast_log(LOG_WARNING, "Failed to park call on '%s'\n", bridged_chan->name);
07946                   } else {
07947                      ast_log(LOG_DEBUG, "Parked call on '%s'\n", bridged_chan->name);
07948                   }
07949                   ast_mutex_lock(&iaxsl[fr->callno]);
07950                } else {
07951                   if (ast_async_goto(bridged_chan, iaxs[fr->callno]->context, ies.called_number, 1))
07952                      ast_log(LOG_WARNING, "Async goto of '%s' to '%s@%s' failed\n", bridged_chan->name, 
07953                         ies.called_number, iaxs[fr->callno]->context);
07954                   else
07955                      ast_log(LOG_DEBUG, "Async goto of '%s' to '%s@%s' started\n", bridged_chan->name, 
07956                         ies.called_number, iaxs[fr->callno]->context);
07957                }
07958             } else
07959                   ast_log(LOG_DEBUG, "Async goto not applicable on call %d\n", fr->callno);
07960 
07961             break;
07962          }
07963          case IAX_COMMAND_ACCEPT:
07964             /* Ignore if call is already up or needs authentication or is a TBD */
07965             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD | IAX_STATE_AUTHENTICATED))
07966                break;
07967             if (ast_test_flag(iaxs[fr->callno], IAX_PROVISION)) {
07968                /* Send ack immediately, before we destroy */
07969                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
07970                iax2_destroy(fr->callno);
07971                break;
07972             }
07973             if (ies.format) {
07974                iaxs[fr->callno]->peerformat = ies.format;
07975             } else {
07976                if (iaxs[fr->callno]->owner)
07977                   iaxs[fr->callno]->peerformat = iaxs[fr->callno]->owner->nativeformats;
07978                else
07979                   iaxs[fr->callno]->peerformat = iaxs[fr->callno]->capability;
07980             }
07981             if (option_verbose > 2)
07982                ast_verbose(VERBOSE_PREFIX_3 "Call accepted by %s (format %s)\n", ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr), ast_getformatname(iaxs[fr->callno]->peerformat));
07983             if (!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability)) {
07984                memset(&ied0, 0, sizeof(ied0));
07985                iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
07986                iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
07987                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
07988                if (!iaxs[fr->callno]) {
07989                   ast_mutex_unlock(&iaxsl[fr->callno]);
07990                   return 1;
07991                }
07992                if (authdebug)
07993                   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);
07994             } else {
07995                ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
07996                if (iaxs[fr->callno]->owner) {
07997                   /* Switch us to use a compatible format */
07998                   iaxs[fr->callno]->owner->nativeformats = iaxs[fr->callno]->peerformat;
07999                   if (option_verbose > 2)
08000                      ast_verbose(VERBOSE_PREFIX_3 "Format for call is %s\n", ast_getformatname(iaxs[fr->callno]->owner->nativeformats));
08001 retryowner2:
08002                   if (ast_mutex_trylock(&iaxs[fr->callno]->owner->lock)) {
08003                      DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]);
08004                      if (iaxs[fr->callno] && iaxs[fr->callno]->owner) goto retryowner2;
08005                   }
08006                   
08007                   if (iaxs[fr->callno] && iaxs[fr->callno]->owner) {
08008                      /* Setup read/write formats properly. */
08009                      if (iaxs[fr->callno]->owner->writeformat)
08010                         ast_set_write_format(iaxs[fr->callno]->owner, iaxs[fr->callno]->owner->writeformat);   
08011                      if (iaxs[fr->callno]->owner->readformat)
08012                         ast_set_read_format(iaxs[fr->callno]->owner, iaxs[fr->callno]->owner->readformat);  
08013                      ast_mutex_unlock(&iaxs[fr->callno]->owner->lock);
08014                   }
08015                }
08016             }
08017             if (iaxs[fr->callno]) {
08018                ast_mutex_lock(&dpcache_lock);
08019                dp = iaxs[fr->callno]->dpentries;
08020                while(dp) {
08021                   if (!(dp->flags & CACHE_FLAG_TRANSMITTED)) {
08022                      iax2_dprequest(dp, fr->callno);
08023                   }
08024                   dp = dp->peer;
08025                }
08026                ast_mutex_unlock(&dpcache_lock);
08027             }
08028             break;
08029          case IAX_COMMAND_POKE:
08030             /* Send back a pong packet with the original timestamp */
08031             send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_PONG, fr->ts, NULL, 0, -1);
08032             if (!iaxs[fr->callno]) {
08033                ast_mutex_unlock(&iaxsl[fr->callno]);
08034                return 1;
08035             }
08036             break;
08037          case IAX_COMMAND_PING:
08038          {
08039             struct iax_ie_data pingied;
08040             construct_rr(iaxs[fr->callno], &pingied);
08041             /* Send back a pong packet with the original timestamp */
08042             send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_PONG, fr->ts, pingied.buf, pingied.pos, -1);
08043          }
08044             break;
08045          case IAX_COMMAND_PONG:
08046             /* Calculate ping time */
08047             iaxs[fr->callno]->pingtime =  calc_timestamp(iaxs[fr->callno], 0, &f) - fr->ts;
08048             /* save RR info */
08049             save_rr(fr, &ies);
08050 
08051             if (iaxs[fr->callno]->peerpoke) {
08052                peer = iaxs[fr->callno]->peerpoke;
08053                if ((peer->lastms < 0)  || (peer->historicms > peer->maxms)) {
08054                   if (iaxs[fr->callno]->pingtime <= peer->maxms) {
08055                      ast_log(LOG_NOTICE, "Peer '%s' is now REACHABLE! Time: %d\n", peer->name, iaxs[fr->callno]->pingtime);
08056                      manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: IAX2/%s\r\nPeerStatus: Reachable\r\nTime: %d\r\n", peer->name, iaxs[fr->callno]->pingtime); 
08057                      ast_device_state_changed("IAX2/%s", peer->name); /* Activate notification */
08058                   }
08059                } else if ((peer->historicms > 0) && (peer->historicms <= peer->maxms)) {
08060                   if (iaxs[fr->callno]->pingtime > peer->maxms) {
08061                      ast_log(LOG_NOTICE, "Peer '%s' is now TOO LAGGED (%d ms)!\n", peer->name, iaxs[fr->callno]->pingtime);
08062                      manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: IAX2/%s\r\nPeerStatus: Lagged\r\nTime: %d\r\n", peer->name, iaxs[fr->callno]->pingtime); 
08063                      ast_device_state_changed("IAX2/%s", peer->name); /* Activate notification */
08064                   }
08065                }
08066                peer->lastms = iaxs[fr->callno]->pingtime;
08067                if (peer->smoothing && (peer->lastms > -1))
08068                   peer->historicms = (iaxs[fr->callno]->pingtime + peer->historicms) / 2;
08069                else if (peer->smoothing && peer->lastms < 0)
08070                   peer->historicms = (0 + peer->historicms) / 2;
08071                else              
08072                   peer->historicms = iaxs[fr->callno]->pingtime;
08073 
08074                /* Remove scheduled iax2_poke_noanswer */
08075                if (peer->pokeexpire > -1) {
08076                   if (!ast_sched_del(sched, peer->pokeexpire)) {
08077                      peer_unref(peer);
08078                      peer->pokeexpire = -1;
08079                   }
08080                }
08081                /* Schedule the next cycle */
08082                if ((peer->lastms < 0)  || (peer->historicms > peer->maxms)) 
08083                   peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_peer_s, peer_ref(peer));
08084                else
08085                   peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqok, iax2_poke_peer_s, peer_ref(peer));
08086                if (peer->pokeexpire == -1)
08087                   peer_unref(peer);
08088                /* and finally send the ack */
08089                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08090                /* And wrap up the qualify call */
08091                iax2_destroy(fr->callno);
08092                peer->callno = 0;
08093                if (option_debug)
08094                   ast_log(LOG_DEBUG, "Peer %s: got pong, lastms %d, historicms %d, maxms %d\n", peer->name, peer->lastms, peer->historicms, peer->maxms);
08095             }
08096             break;
08097          case IAX_COMMAND_LAGRQ:
08098          case IAX_COMMAND_LAGRP:
08099             f.src = "LAGRQ";
08100             f.mallocd = 0;
08101             f.offset = 0;
08102             f.samples = 0;
08103             iax_frame_wrap(fr, &f);
08104             if(f.subclass == IAX_COMMAND_LAGRQ) {
08105                /* Received a LAGRQ - echo back a LAGRP */
08106                fr->af.subclass = IAX_COMMAND_LAGRP;
08107                iax2_send(iaxs[fr->callno], &fr->af, fr->ts, -1, 0, 0, 0);
08108             } else {
08109                /* Received LAGRP in response to our LAGRQ */
08110                unsigned int ts;
08111                /* This is a reply we've been given, actually measure the difference */
08112                ts = calc_timestamp(iaxs[fr->callno], 0, &fr->af);
08113                iaxs[fr->callno]->lag = ts - fr->ts;
08114                if (option_debug && iaxdebug)
08115                   ast_log(LOG_DEBUG, "Peer %s lag measured as %dms\n",
08116                      ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr), iaxs[fr->callno]->lag);
08117             }
08118             break;
08119          case IAX_COMMAND_AUTHREQ:
08120             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD)) {
08121                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>");
08122                break;
08123             }
08124             if (authenticate_reply(iaxs[fr->callno], &iaxs[fr->callno]->addr, &ies, iaxs[fr->callno]->secret, iaxs[fr->callno]->outkey)) {
08125                struct ast_frame hangup_fr = { .frametype = AST_FRAME_CONTROL,
08126                         .subclass = AST_CONTROL_HANGUP,
08127                };
08128                ast_log(LOG_WARNING, 
08129                   "I don't know how to authenticate %s to %s\n", 
08130                   ies.username ? ies.username : "<unknown>", ast_inet_ntoa(iaxs[fr->callno]->addr.sin_addr));
08131                iax2_queue_frame(fr->callno, &hangup_fr);
08132             }
08133             if (!iaxs[fr->callno]) {
08134                ast_mutex_unlock(&iaxsl[fr->callno]);
08135                return 1;
08136             }
08137             break;
08138          case IAX_COMMAND_AUTHREP:
08139             /* For security, always ack immediately */
08140             if (delayreject)
08141                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08142             /* Ignore once we've started */
08143             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD)) {
08144                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>");
08145                break;
08146             }
08147             if (authenticate_verify(iaxs[fr->callno], &ies)) {
08148                if (authdebug)
08149                   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);
08150                memset(&ied0, 0, sizeof(ied0));
08151                auth_fail(fr->callno, IAX_COMMAND_REJECT);
08152                break;
08153             }
08154             if (strcasecmp(iaxs[fr->callno]->exten, "TBD")) {
08155                /* This might re-enter the IAX code and need the lock */
08156                exists = ast_exists_extension(NULL, iaxs[fr->callno]->context, iaxs[fr->callno]->exten, 1, iaxs[fr->callno]->cid_num);
08157             } else
08158                exists = 0;
08159             if (strcmp(iaxs[fr->callno]->exten, "TBD") && !exists) {
08160                if (authdebug)
08161                   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);
08162                memset(&ied0, 0, sizeof(ied0));
08163                iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
08164                iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
08165                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
08166                if (!iaxs[fr->callno]) {
08167                   ast_mutex_unlock(&iaxsl[fr->callno]);
08168                   return 1;
08169                }
08170             } else {
08171                /* Select an appropriate format */
08172                if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
08173                   if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
08174                      using_prefs = "reqonly";
08175                   } else {
08176                      using_prefs = "disabled";
08177                   }
08178                   format = iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability;
08179                   memset(&pref, 0, sizeof(pref));
08180                   strcpy(caller_pref_buf, "disabled");
08181                   strcpy(host_pref_buf, "disabled");
08182                } else {
08183                   using_prefs = "mine";
08184                   if (ies.codec_prefs)
08185                      ast_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
08186                   if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
08187                      if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
08188                         pref = iaxs[fr->callno]->rprefs;
08189                         using_prefs = "caller";
08190                      } else {
08191                         pref = iaxs[fr->callno]->prefs;
08192                      }
08193                   } else /* if no codec_prefs IE do it the old way */
08194                      pref = iaxs[fr->callno]->prefs;
08195                
08196                   format = ast_codec_choose(&pref, iaxs[fr->callno]->capability & iaxs[fr->callno]->peercapability, 0);
08197                   ast_codec_pref_string(&iaxs[fr->callno]->rprefs, caller_pref_buf, sizeof(caller_pref_buf) - 1);
08198                   ast_codec_pref_string(&iaxs[fr->callno]->prefs, host_pref_buf, sizeof(host_pref_buf) - 1);
08199                }
08200                if (!format) {
08201                   if(!ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
08202                      if (option_debug)
08203                         ast_log(LOG_DEBUG, "We don't do requested format %s, falling back to peer capability %d\n", ast_getformatname(iaxs[fr->callno]->peerformat), iaxs[fr->callno]->peercapability);
08204                      format = iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability;
08205                   }
08206                   if (!format) {
08207                      if (authdebug) {
08208                         if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) 
08209                            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);
08210                         else
08211                            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);
08212                      }
08213                      memset(&ied0, 0, sizeof(ied0));
08214                      iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
08215                      iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
08216                      send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
08217                      if (!iaxs[fr->callno]) {
08218                         ast_mutex_unlock(&iaxsl[fr->callno]);
08219                         return 1;
08220                      }
08221                   } else {
08222                      /* Pick one... */
08223                      if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
08224                         if(!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability))
08225                            format = 0;
08226                      } else {
08227                         if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
08228                            using_prefs = ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
08229                            memset(&pref, 0, sizeof(pref));
08230                            format = ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP) ?
08231                               iaxs[fr->callno]->peerformat : ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
08232                            strcpy(caller_pref_buf,"disabled");
08233                            strcpy(host_pref_buf,"disabled");
08234                         } else {
08235                            using_prefs = "mine";
08236                            if (ast_codec_pref_index(&iaxs[fr->callno]->rprefs, 0)) {
08237                               /* Do the opposite of what we tried above. */
08238                               if (ast_test_flag(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
08239                                  pref = iaxs[fr->callno]->prefs;                 
08240                               } else {
08241                                  pref = iaxs[fr->callno]->rprefs;
08242                                  using_prefs = "caller";
08243                               }
08244                               format = ast_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability, 1);
08245                            } else /* if no codec_prefs IE do it the old way */
08246                               format = ast_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability); 
08247                         }
08248                      }
08249                      if (!format) {
08250                         ast_log(LOG_ERROR, "No best format in 0x%x???\n", iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
08251                         if (authdebug) {
08252                            if(ast_test_flag(iaxs[fr->callno], IAX_CODEC_NOCAP))
08253                               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);
08254                            else
08255                               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);
08256                         }
08257                         memset(&ied0, 0, sizeof(ied0));
08258                         iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
08259                         iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
08260                         send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
08261                         if (!iaxs[fr->callno]) {
08262                            ast_mutex_unlock(&iaxsl[fr->callno]);
08263                            return 1;
08264                         }
08265                      }
08266                   }
08267                }
08268                if (format) {
08269                   /* Authentication received */
08270                   memset(&ied1, 0, sizeof(ied1));
08271                   iax_ie_append_int(&ied1, IAX_IE_FORMAT, format);
08272                   send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACCEPT, 0, ied1.buf, ied1.pos, -1);
08273                   if (strcmp(iaxs[fr->callno]->exten, "TBD")) {
08274                      ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
08275                      if (option_verbose > 2) 
08276                         ast_verbose(VERBOSE_PREFIX_3 "Accepting AUTHENTICATED call from %s:\n"
08277                                  "%srequested format = %s,\n"
08278                                  "%srequested prefs = %s,\n"
08279                                  "%sactual format = %s,\n"
08280                                  "%shost prefs = %s,\n"
08281                                  "%spriority = %s\n", 
08282                                  ast_inet_ntoa(sin.sin_addr), 
08283                                  VERBOSE_PREFIX_4,
08284                                  ast_getformatname(iaxs[fr->callno]->peerformat),
08285                                  VERBOSE_PREFIX_4,
08286                                  caller_pref_buf,
08287                                  VERBOSE_PREFIX_4,
08288                                  ast_getformatname(format),
08289                                  VERBOSE_PREFIX_4,
08290                                  host_pref_buf,
08291                                  VERBOSE_PREFIX_4,
08292                                  using_prefs);
08293 
08294                      ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
08295                      if(!(c = ast_iax2_new(fr->callno, AST_STATE_RING, format)))
08296                         iax2_destroy(fr->callno);
08297                   } else {
08298                      ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
08299                      /* If this is a TBD call, we're ready but now what...  */
08300                      if (option_verbose > 2)
08301                         ast_verbose(VERBOSE_PREFIX_3 "Accepted AUTHENTICATED TBD call from %s\n", ast_inet_ntoa(sin.sin_addr));
08302                   }
08303                }
08304             }
08305             break;
08306          case IAX_COMMAND_DIAL:
08307             if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD)) {
08308                ast_clear_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
08309                ast_string_field_set(iaxs[fr->callno], exten, ies.called_number ? ies.called_number : "s");
08310                if (!ast_exists_extension(NULL, iaxs[fr->callno]->context, iaxs[fr->callno]->exten, 1, iaxs[fr->callno]->cid_num)) {
08311                   if (authdebug)
08312                      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);
08313                   memset(&ied0, 0, sizeof(ied0));
08314                   iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
08315                   iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
08316                   send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
08317                   if (!iaxs[fr->callno]) {
08318                      ast_mutex_unlock(&iaxsl[fr->callno]);
08319                      return 1;
08320                   }
08321                } else {
08322                   ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
08323                   if (option_verbose > 2) 
08324                      ast_verbose(VERBOSE_PREFIX_3 "Accepting DIAL from %s, formats = 0x%x\n", ast_inet_ntoa(sin.sin_addr), iaxs[fr->callno]->peerformat);
08325                   ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
08326                   send_command(iaxs[fr->callno], AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, 0, NULL, 0, -1);
08327                   if(!(c = ast_iax2_new(fr->callno, AST_STATE_RING, iaxs[fr->callno]->peerformat)))
08328                      iax2_destroy(fr->callno);
08329                }
08330             }
08331             break;
08332          case IAX_COMMAND_INVAL:
08333             iaxs[fr->callno]->error = ENOTCONN;
08334             if (option_debug)
08335                ast_log(LOG_DEBUG, "Immediately destroying %d, having received INVAL\n", fr->callno);
08336             iax2_destroy(fr->callno);
08337             if (option_debug)
08338                ast_log(LOG_DEBUG, "Destroying call %d\n", fr->callno);
08339             break;
08340          case IAX_COMMAND_VNAK:
08341             if (option_debug)
08342                ast_log(LOG_DEBUG, "Received VNAK: resending outstanding frames\n");
08343             /* Force retransmission */
08344             vnak_retransmit(fr->callno, fr->iseqno);
08345             break;
08346          case IAX_COMMAND_REGREQ:
08347          case IAX_COMMAND_REGREL:
08348             /* For security, always ack immediately */
08349             if (delayreject)
08350                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08351             if (register_verify(fr->callno, &sin, &ies)) {
08352                if (!iaxs[fr->callno]) {
08353                   ast_mutex_unlock(&iaxsl[fr->callno]);
08354                   return 1;
08355                }
08356                /* Send delayed failure */
08357                auth_fail(fr->callno, IAX_COMMAND_REGREJ);
08358                break;
08359             }
08360             if (!iaxs[fr->callno]) {
08361                ast_mutex_unlock(&iaxsl[fr->callno]);
08362                return 1;
08363             }
08364             if ((ast_strlen_zero(iaxs[fr->callno]->secret) && ast_strlen_zero(iaxs[fr->callno]->inkeys)) || 
08365                   ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_AUTHENTICATED | IAX_STATE_UNCHANGED)) {
08366                if (f.subclass == IAX_COMMAND_REGREL)
08367                   memset(&sin, 0, sizeof(sin));
08368                if (update_registry(&sin, fr->callno, ies.devicetype, fd, ies.refresh))
08369                   ast_log(LOG_WARNING, "Registry error\n");
08370                if (!iaxs[fr->callno]) {
08371                   ast_mutex_unlock(&iaxsl[fr->callno]);
08372                   return 1;
08373                }
08374                if (ies.provverpres && ies.serviceident && sin.sin_addr.s_addr) {
08375                   ast_mutex_unlock(&iaxsl[fr->callno]);
08376                   check_provisioning(&sin, fd, ies.serviceident, ies.provver);
08377                   ast_mutex_lock(&iaxsl[fr->callno]);
08378                   if (!iaxs[fr->callno]) {
08379                      ast_mutex_unlock(&iaxsl[fr->callno]);
08380                      return 1;
08381                   }
08382                }
08383                break;
08384             }
08385             registry_authrequest(fr->callno);
08386             if (!iaxs[fr->callno]) {
08387                ast_mutex_unlock(&iaxsl[fr->callno]);
08388                return 1;
08389             }
08390             break;
08391          case IAX_COMMAND_REGACK:
08392             if (iax2_ack_registry(&ies, &sin, fr->callno)) 
08393                ast_log(LOG_WARNING, "Registration failure\n");
08394             /* Send ack immediately, before we destroy */
08395             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08396             iax2_destroy(fr->callno);
08397             break;
08398          case IAX_COMMAND_REGREJ:
08399             if (iaxs[fr->callno]->reg) {
08400                if (authdebug) {
08401                   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));
08402                   manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelDriver: IAX2\r\nUsername: %s\r\nStatus: Rejected\r\nCause: %s\r\n", iaxs[fr->callno]->reg->username, ies.cause ? ies.cause : "<unknown>");
08403                }
08404                iaxs[fr->callno]->reg->regstate = REG_STATE_REJECTED;
08405             }
08406             /* Send ack immediately, before we destroy */
08407             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08408             iax2_destroy(fr->callno);
08409             break;
08410          case IAX_COMMAND_REGAUTH:
08411             /* Authentication request */
08412             if (registry_rerequest(&ies, fr->callno, &sin)) {
08413                memset(&ied0, 0, sizeof(ied0));
08414                iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No authority found");
08415                iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_NOT_SUBSCRIBED);
08416                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
08417                if (!iaxs[fr->callno]) {
08418                   ast_mutex_unlock(&iaxsl[fr->callno]);
08419                   return 1;
08420                }
08421             }
08422             break;
08423          case IAX_COMMAND_TXREJ:
08424             iaxs[fr->callno]->transferring = 0;
08425             if (option_verbose > 2) 
08426                ast_verbose(VERBOSE_PREFIX_3 "Channel '%s' unable to transfer\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
08427             memset(&iaxs[fr->callno]->transfer, 0, sizeof(iaxs[fr->callno]->transfer));
08428             if (iaxs[fr->callno]->bridgecallno) {
08429                if (iaxs[iaxs[fr->callno]->bridgecallno]->transferring) {
08430                   iaxs[iaxs[fr->callno]->bridgecallno]->transferring = 0;
08431                   send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
08432                }
08433             }
08434             break;
08435          case IAX_COMMAND_TXREADY:
08436             if ((iaxs[fr->callno]->transferring == TRANSFER_BEGIN) ||
08437                 (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN)) {
08438                if (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN)
08439                   iaxs[fr->callno]->transferring = TRANSFER_MREADY;
08440                else
08441                   iaxs[fr->callno]->transferring = TRANSFER_READY;
08442                if (option_verbose > 2) 
08443                   ast_verbose(VERBOSE_PREFIX_3 "Channel '%s' ready to transfer\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>");
08444                if (iaxs[fr->callno]->bridgecallno) {
08445                   if ((iaxs[iaxs[fr->callno]->bridgecallno]->transferring == TRANSFER_READY) ||
08446                       (iaxs[iaxs[fr->callno]->bridgecallno]->transferring == TRANSFER_MREADY)) {
08447                      /* They're both ready, now release them. */
08448                      if (iaxs[fr->callno]->transferring == TRANSFER_MREADY) {
08449                         if (option_verbose > 2) 
08450                            ast_verbose(VERBOSE_PREFIX_3 "Attempting media bridge of %s and %s\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>",
08451                               iaxs[iaxs[fr->callno]->bridgecallno]->owner ? iaxs[iaxs[fr->callno]->bridgecallno]->owner->name : "<Unknown>");
08452 
08453                         iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_MEDIA;
08454                         iaxs[fr->callno]->transferring = TRANSFER_MEDIA;
08455 
08456                         memset(&ied0, 0, sizeof(ied0));
08457                         memset(&ied1, 0, sizeof(ied1));
08458                         iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
08459                         iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
08460                         send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied0.buf, ied0.pos, -1);
08461                         send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied1.buf, ied1.pos, -1);
08462                      } else {
08463                         if (option_verbose > 2) 
08464                            ast_verbose(VERBOSE_PREFIX_3 "Releasing %s and %s\n", iaxs[fr->callno]->owner ? iaxs[fr->callno]->owner->name : "<Unknown>",
08465                               iaxs[iaxs[fr->callno]->bridgecallno]->owner ? iaxs[iaxs[fr->callno]->bridgecallno]->owner->name : "<Unknown>");
08466 
08467                         iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_RELEASED;
08468                         iaxs[fr->callno]->transferring = TRANSFER_RELEASED;
08469                         ast_set_flag(iaxs[iaxs[fr->callno]->bridgecallno], IAX_ALREADYGONE);
08470                         ast_set_flag(iaxs[fr->callno], IAX_ALREADYGONE);
08471 
08472                         /* Stop doing lag & ping requests */
08473                         stop_stuff(fr->callno);
08474                         stop_stuff(iaxs[fr->callno]->bridgecallno);
08475 
08476                         memset(&ied0, 0, sizeof(ied0));
08477                         memset(&ied1, 0, sizeof(ied1));
08478                         iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
08479                         iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
08480                         send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied0.buf, ied0.pos, -1);
08481                         send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied1.buf, ied1.pos, -1);
08482                      }
08483 
08484                   }
08485                }
08486             }
08487             break;
08488          case IAX_COMMAND_TXREQ:
08489             try_transfer(iaxs[fr->callno], &ies);
08490             break;
08491          case IAX_COMMAND_TXCNT:
08492             if (iaxs[fr->callno]->transferring)
08493                send_command_transfer(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXACC, 0, NULL, 0);
08494             break;
08495          case IAX_COMMAND_TXREL:
08496             /* Send ack immediately, rather than waiting until we've changed addresses */
08497             send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08498             complete_transfer(fr->callno, &ies);
08499             stop_stuff(fr->callno); /* for attended transfer to work with libiax */
08500             break;   
08501          case IAX_COMMAND_TXMEDIA:
08502             if (iaxs[fr->callno]->transferring == TRANSFER_READY) {
08503                                         AST_LIST_LOCK(&iaxq.queue);
08504                                         AST_LIST_TRAVERSE(&iaxq.queue, cur, list) {
08505                                                 /* Cancel any outstanding frames and start anew */
08506                                                 if ((fr->callno == cur->callno) && (cur->transfer)) {
08507                                                         cur->retries = -1;
08508                                                 }
08509                                         }
08510                                         AST_LIST_UNLOCK(&iaxq.queue);
08511                /* Start sending our media to the transfer address, but otherwise leave the call as-is */
08512                iaxs[fr->callno]->transferring = TRANSFER_MEDIAPASS;
08513             }
08514             break;   
08515          case IAX_COMMAND_DPREP:
08516             complete_dpreply(iaxs[fr->callno], &ies);
08517             break;
08518          case IAX_COMMAND_UNSUPPORT:
08519             ast_log(LOG_NOTICE, "Peer did not understand our iax command '%d'\n", ies.iax_unknown);
08520             break;
08521          case IAX_COMMAND_FWDOWNL:
08522             /* Firmware download */
08523             if (!ast_test_flag(&globalflags, IAX_ALLOWFWDOWNLOAD)) {
08524                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_UNSUPPORT, 0, NULL, 0, -1);
08525                break;
08526             }
08527             memset(&ied0, 0, sizeof(ied0));
08528             res = iax_firmware_append(&ied0, (unsigned char *)ies.devicetype, ies.fwdesc);
08529             if (res < 0)
08530                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
08531             else if (res > 0)
08532                send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_FWDATA, 0, ied0.buf, ied0.pos, -1);
08533             else
08534                send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_FWDATA, 0, ied0.buf, ied0.pos, -1);
08535             if (!iaxs[fr->callno]) {
08536                ast_mutex_unlock(&iaxsl[fr->callno]);
08537                return 1;
08538             }
08539             break;
08540          default:
08541             if (option_debug)
08542                ast_log(LOG_DEBUG, "Unknown IAX command %d on %d/%d\n", f.subclass, fr->callno, iaxs[fr->callno]->peercallno);
08543             memset(&ied0, 0, sizeof(ied0));
08544             iax_ie_append_byte(&ied0, IAX_IE_IAX_UNKNOWN, f.subclass);
08545             send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_UNSUPPORT, 0, ied0.buf, ied0.pos, -1);
08546          }
08547          /* Don't actually pass these frames along */
08548          if ((f.subclass != IAX_COMMAND_ACK) && 
08549            (f.subclass != IAX_COMMAND_TXCNT) && 
08550            (f.subclass != IAX_COMMAND_TXACC) && 
08551            (f.subclass != IAX_COMMAND_INVAL) &&
08552            (f.subclass != IAX_COMMAND_VNAK)) { 
08553             if (iaxs[fr->callno] && iaxs[fr->callno]->aseqno != iaxs[fr->callno]->iseqno)
08554                send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08555          }
08556          ast_mutex_unlock(&iaxsl[fr->callno]);
08557          return 1;
08558       }
08559       /* Unless this is an ACK or INVAL frame, ack it */
08560       if (iaxs[fr->callno] && iaxs[fr->callno]->aseqno != iaxs[fr->callno]->iseqno)
08561          send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
08562    } else if (minivid) {
08563       f.frametype = AST_FRAME_VIDEO;
08564       if (iaxs[fr->callno]->videoformat > 0) 
08565          f.subclass = iaxs[fr->callno]->videoformat | (ntohs(vh->ts) & 0x8000 ? 1 : 0);
08566       else {
08567          ast_log(LOG_WARNING, "Received mini frame before first full video frame\n");
08568          iax2_vnak(fr->callno);
08569          ast_mutex_unlock(&iaxsl[fr->callno]);
08570          return 1;
08571       }
08572       f.datalen = res - sizeof(*vh);
08573       if (f.datalen)
08574          f.data = thread->buf + sizeof(*vh);
08575       else
08576          f.data = NULL;
08577 #ifdef IAXTESTS
08578       if (test_resync) {
08579          fr->ts = (iaxs[fr->callno]->last & 0xFFFF8000L) | ((ntohs(vh->ts) + test_resync) & 0x7fff);
08580       } else
08581 #endif /* IAXTESTS */
08582          fr->ts = (iaxs[fr->callno]->last & 0xFFFF8000L) | (ntohs(vh->ts) & 0x7fff);
08583    } else {
08584       /* A mini frame */
08585       f.frametype = AST_FRAME_VOICE;
08586       if (iaxs[fr->callno]->voiceformat > 0)
08587          f.subclass = iaxs[fr->callno]->voiceformat;
08588       else {
08589          if (option_debug)
08590             ast_log(LOG_DEBUG, "Received mini frame before first full voice frame\n");
08591          iax2_vnak(fr->callno);
08592          ast_mutex_unlock(&iaxsl[fr->callno]);
08593          return 1;
08594       }
08595       f.datalen = res - sizeof(struct ast_iax2_mini_hdr);
08596       if (f.datalen < 0) {
08597          ast_log(LOG_WARNING, "Datalen < 0?\n");
08598          ast_mutex_unlock(&iaxsl[fr->callno]);
08599          return 1;
08600       }
08601       if (f.datalen)
08602          f.data = thread->buf + sizeof(*mh);
08603       else
08604          f.data = NULL;
08605 #ifdef IAXTESTS
08606       if (test_resync) {
08607          fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | ((ntohs(mh->ts) + test_resync) & 0xffff);
08608       } else
08609 #endif /* IAXTESTS */
08610       fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | ntohs(mh->ts);
08611       /* FIXME? Surely right here would be the right place to undo timestamp wraparound? */
08612    }
08613    /* Don't pass any packets until we're started */
08614    if (!ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
08615       ast_mutex_unlock(&iaxsl[fr->callno]);
08616       return 1;
08617    }
08618    /* Common things */
08619    f.src = "IAX2";
08620    f.mallocd = 0;
08621    f.offset = 0;
08622    f.len = 0;
08623    if (f.datalen && (f.frametype == AST_FRAME_VOICE)) {
08624       f.samples = ast_codec_get_samples(&f);
08625       /* We need to byteswap incoming slinear samples from network byte order */
08626       if (f.subclass == AST_FORMAT_SLINEAR)
08627          ast_frame_byteswap_be(&f);
08628    } else
08629       f.samples = 0;
08630    iax_frame_wrap(fr, &f);
08631 
08632    /* If this is our most recent packet, use it as our basis for timestamping */
08633    if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts) {
08634       /*iaxs[fr->callno]->last = fr->ts; (do it afterwards cos schedule/forward_delivery needs the last ts too)*/
08635       fr->outoforder = 0;
08636    } else {
08637       if (option_debug && iaxdebug && iaxs[fr->callno])
08638          ast_log(LOG_DEBUG, "Received out of order packet... (type=%d, subclass %d, ts = %d, last = %d)\n", f.frametype, f.subclass, fr->ts, iaxs[fr->callno]->last);
08639       fr->outoforder = -1;
08640    }
08641    fr->cacheable = ((f.frametype == AST_FRAME_VOICE) || (f.frametype == AST_FRAME_VIDEO));
08642    duped_fr = iaxfrdup2(fr);
08643    if (duped_fr) {
08644       schedule_delivery(duped_fr, updatehistory, 0, &fr->ts);
08645    }
08646    if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts) {
08647       iaxs[fr->callno]->last = fr->ts;
08648 #if 1
08649       if (option_debug && iaxdebug)
08650          ast_log(LOG_DEBUG, "For call=%d, set last=%d\n", fr->callno, fr->ts);
08651 #endif
08652    }
08653 
08654    /* Always run again */
08655    ast_mutex_unlock(&iaxsl[fr->callno]);
08656    return 1;
08657 }

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

Definition at line 7003 of file chan_iax2.c.

References ast_copy_string(), 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_registry::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_DEBUG, LOG_WARNING, option_debug, iax2_thread::readbuf, ast_iax2_full_hdr::scallno, signal_condition(), iax2_thread::sin, t, thread, ast_iax2_full_hdr::type, and iax2_thread::type.

Referenced by peer_set_srcaddr(), and set_config().

07004 {
07005    struct iax2_thread *thread;
07006    socklen_t len;
07007    time_t t;
07008    static time_t last_errtime = 0;
07009    struct ast_iax2_full_hdr *fh;
07010 
07011    if (!(thread = find_idle_thread())) {
07012       time(&t);
07013       if (t != last_errtime && option_debug)
07014          ast_log(LOG_DEBUG, "Out of idle IAX2 threads for I/O, pausing!\n");
07015       last_errtime = t;
07016       usleep(1);
07017       return 1;
07018    }
07019 
07020    len = sizeof(thread->iosin);
07021    thread->iofd = fd;
07022    thread->buf_len = recvfrom(fd, thread->readbuf, sizeof(thread->readbuf), 0, (struct sockaddr *) &thread->iosin, &len);
07023    thread->buf_size = sizeof(thread->readbuf);
07024    thread->buf = thread->readbuf;
07025    if (thread->buf_len < 0) {
07026       if (errno != ECONNREFUSED && errno != EAGAIN)
07027          ast_log(LOG_WARNING, "Error: %s\n", strerror(errno));
07028       handle_error();
07029       thread->iostate = IAX_IOSTATE_IDLE;
07030       signal_condition(&thread->lock, &thread->cond);
07031       return 1;
07032    }
07033    if (test_losspct && ((100.0 * ast_random() / (RAND_MAX + 1.0)) < test_losspct)) { /* simulate random loss condition */
07034       thread->iostate = IAX_IOSTATE_IDLE;
07035       signal_condition(&thread->lock, &thread->cond);
07036       return 1;
07037    }
07038    
07039    /* Determine if this frame is a full frame; if so, and any thread is currently
07040       processing a full frame for the same callno from this peer, then drop this
07041       frame (and the peer will retransmit it) */
07042    fh = (struct ast_iax2_full_hdr *) thread->buf;
07043    if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
07044       struct iax2_thread *cur = NULL;
07045       uint16_t callno = ntohs(fh->scallno) & ~IAX_FLAG_FULL;
07046       
07047       AST_LIST_LOCK(&active_list);
07048       AST_LIST_TRAVERSE(&active_list, cur, list) {
07049          if ((cur->ffinfo.callno == callno) &&
07050              !inaddrcmp(&cur->ffinfo.sin, &thread->iosin))
07051             break;
07052       }
07053       if (cur) {
07054          /* we found another thread processing a full frame for this call,
07055             so queue it up for processing later. */
07056          defer_full_frame(thread, cur);
07057          AST_LIST_UNLOCK(&active_list);
07058          thread->iostate = IAX_IOSTATE_IDLE;
07059          signal_condition(&thread->lock, &thread->cond);
07060          return 1;
07061       } else {
07062          /* this thread is going to process this frame, so mark it */
07063          thread->ffinfo.callno = callno;
07064          memcpy(&thread->ffinfo.sin, &thread->iosin, sizeof(thread->ffinfo.sin));
07065          thread->ffinfo.type = fh->type;
07066          thread->ffinfo.csub = fh->csub;
07067       }
07068       AST_LIST_UNLOCK(&active_list);
07069    }
07070    
07071    /* Mark as ready and send on its way */
07072    thread->iostate = IAX_IOSTATE_READY;
07073 #ifdef DEBUG_SCHED_MULTITHREAD
07074    ast_copy_string(thread->curfunc, "socket_process", sizeof(thread->curfunc));
07075 #endif
07076    signal_condition(&thread->lock, &thread->cond);
07077 
07078    return 1;
07079 }

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

Definition at line 6790 of file chan_iax2.c.

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

Referenced by socket_process().

06791 {
06792    pthread_t newthread;
06793    struct dpreq_data *dpr;
06794    pthread_attr_t attr;
06795    
06796    if (!(dpr = ast_calloc(1, sizeof(*dpr))))
06797       return;
06798 
06799    pthread_attr_init(&attr);
06800    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);   
06801 
06802    dpr->callno = callno;
06803    ast_copy_string(dpr->context, context, sizeof(dpr->context));
06804    ast_copy_string(dpr->callednum, callednum, sizeof(dpr->callednum));
06805    if (callerid)
06806       dpr->callerid = ast_strdup(callerid);
06807    if (ast_pthread_create(&newthread, &attr, dp_lookup_thread, dpr)) {
06808       ast_log(LOG_WARNING, "Unable to start lookup thread!\n");
06809    }
06810 
06811    pthread_attr_destroy(&attr);
06812 }

static int start_network_thread ( void   )  [static]

Definition at line 9259 of file chan_iax2.c.

References ast_calloc, ast_cond_init(), AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_mutex_init(), ast_pthread_create, ast_pthread_create_background, ast_verbose(), iax2_thread::cond, free, iax2_process_thread(), IAX_TYPE_POOL, iaxthreadcount, iax2_thread::lock, LOG_WARNING, network_thread(), option_verbose, sched_thread(), thread, iax2_thread::threadid, iax2_thread::threadnum, iax2_thread::type, and VERBOSE_PREFIX_2.

Referenced by load_module().

09260 {
09261    pthread_attr_t attr;
09262    int threadcount = 0;
09263    int x;
09264    for (x = 0; x < iaxthreadcount; x++) {
09265       struct iax2_thread *thread = ast_calloc(1, sizeof(struct iax2_thread));
09266       if (thread) {
09267          thread->type = IAX_TYPE_POOL;
09268          thread->threadnum = ++threadcount;
09269          ast_mutex_init(&thread->lock);
09270          ast_cond_init(&thread->cond, NULL);
09271          pthread_attr_init(&attr);
09272          pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);   
09273          if (ast_pthread_create(&thread->threadid, &attr, iax2_process_thread, thread)) {
09274             ast_log(LOG_WARNING, "Failed to create new thread!\n");
09275             free(thread);
09276             thread = NULL;
09277          }
09278          AST_LIST_LOCK(&idle_list);
09279          AST_LIST_INSERT_TAIL(&idle_list, thread, list);
09280          AST_LIST_UNLOCK(&idle_list);
09281       }
09282    }
09283    ast_pthread_create_background(&schedthreadid, NULL, sched_thread, NULL);
09284    ast_pthread_create_background(&netthreadid, NULL, network_thread, NULL);
09285    if (option_verbose > 1)
09286       ast_verbose(VERBOSE_PREFIX_2 "%d helper threads started\n", threadcount);
09287    return 0;
09288 }

static void stop_stuff ( int  callno  )  [static]

Definition at line 6472 of file chan_iax2.c.

References iax2_destroy_helper().

Referenced by socket_process().

06473 {
06474    iax2_destroy_helper(iaxs[callno]);
06475 }

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

Definition at line 1247 of file chan_iax2.c.

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

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

01248 {
01249    if (!pvt->peercallno) {
01250       ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
01251       return;
01252    }
01253 
01254    ao2_link(iax_peercallno_pvts, pvt);
01255 }

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

Definition at line 6659 of file chan_iax2.c.

References iax2_trunk_peer::addr, ast_inet_ntoa(), AST_IO_PRI, ast_log(), ast_mutex_destroy(), ast_mutex_lock(), ast_mutex_unlock(), ast_verbose(), free, iax2_trunk_expired(), iax2_trunk_peer::lock, LOG_DEBUG, LOG_WARNING, MAX_TRUNKDATA, iax2_trunk_peer::next, option_debug, send_trunk(), tpeerlock, tpeers, iax2_trunk_peer::trunkdataalloc, and iax2_trunk_peer::trunkdatalen.

Referenced by network_thread().

06660 {
06661    char buf[1024];
06662    int res;
06663    struct iax2_trunk_peer *tpeer, *prev = NULL, *drop=NULL;
06664    int processed = 0;
06665    int totalcalls = 0;
06666 #ifdef DAHDI_TIMERACK
06667    int x = 1;
06668 #endif
06669    struct timeval now;
06670    if (iaxtrunkdebug)
06671       ast_verbose("Beginning trunk processing. Trunk queue ceiling is %d bytes per host\n", MAX_TRUNKDATA);
06672    gettimeofday(&now, NULL);
06673    if (events & AST_IO_PRI) {
06674 #ifdef DAHDI_TIMERACK
06675       /* Great, this is a timing interface, just call the ioctl */
06676       if (ioctl(fd, DAHDI_TIMERACK, &x)) {
06677          ast_log(LOG_WARNING, "Unable to acknowledge timer. IAX trunking will fail!\n");
06678          usleep(1);
06679          return -1;
06680       }
06681 #endif      
06682    } else {
06683       /* Read and ignore from the pseudo channel for timing */
06684       res = read(fd, buf, sizeof(buf));
06685       if (res < 1) {
06686          ast_log(LOG_WARNING, "Unable to read from timing fd\n");
06687          return 1;
06688       }
06689    }
06690    /* For each peer that supports trunking... */
06691    ast_mutex_lock(&tpeerlock);
06692    tpeer = tpeers;
06693    while(tpeer) {
06694       processed++;
06695       res = 0;
06696       ast_mutex_lock(&tpeer->lock);
06697       /* We can drop a single tpeer per pass.  That makes all this logic
06698          substantially easier */
06699       if (!drop && iax2_trunk_expired(tpeer, &now)) {
06700          /* Take it out of the list, but don't free it yet, because it
06701             could be in use */
06702          if (prev)
06703             prev->next = tpeer->next;
06704          else
06705             tpeers = tpeer->next;
06706          drop = tpeer;
06707       } else {
06708          res = send_trunk(tpeer, &now);
06709          if (iaxtrunkdebug)
06710             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);
06711       }     
06712       totalcalls += res;   
06713       res = 0;
06714       ast_mutex_unlock(&tpeer->lock);
06715       prev = tpeer;
06716       tpeer = tpeer->next;
06717    }
06718    ast_mutex_unlock(&tpeerlock);
06719    if (drop) {
06720       ast_mutex_lock(&drop->lock);
06721       /* Once we have this lock, we're sure nobody else is using it or could use it once we release it, 
06722          because by the time they could get tpeerlock, we've already grabbed it */
06723       if (option_debug)
06724          ast_log(LOG_DEBUG, "Dropping unused iax2 trunk peer '%s:%d'\n", ast_inet_ntoa(drop->addr.sin_addr), ntohs(drop->addr.sin_port));
06725       if (drop->trunkdata) {
06726          free(drop->trunkdata);
06727          drop->trunkdata = NULL;
06728       }
06729       ast_mutex_unlock(&drop->lock);
06730       ast_mutex_destroy(&drop->lock);
06731       free(drop);
06732       
06733    }
06734    if (iaxtrunkdebug)
06735       ast_verbose("Ending trunk processing with %d peers and %d call chunks processed\n", processed, totalcalls);
06736    iaxtrunkdebug =0;
06737    return 1;
06738 }

static int transmit_trunk ( struct iax_frame *  f,
struct sockaddr_in *  sin,
int  sockfd 
) [static]

Definition at line 2128 of file chan_iax2.c.

References ast_log(), iax_frame::data, iax_frame::datalen, errno, handle_error(), LOG_DEBUG, and option_debug.

Referenced by send_trunk().

02129 {
02130    int res;
02131    res = sendto(sockfd, f->data, f->datalen, 0,(struct sockaddr *)sin,
02132                sizeof(*sin));
02133    if (res < 0) {
02134       if (option_debug)
02135          ast_log(LOG_DEBUG, "Received error: %s\n", strerror(errno));
02136       handle_error();
02137    } else
02138       res = 0;
02139    return res;
02140 }

static int try_firmware ( char *  s  )  [static]

Definition at line 1816 of file chan_iax2.c.

References ast_calloc, 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, iax_firmware::next, ast_iax2_firmware_header::version, ast_firmware_list::wares, and waresl.

Referenced by reload_firmware().

01817 {
01818    struct stat stbuf;
01819    struct iax_firmware *cur;
01820    int ifd;
01821    int fd;
01822    int res;
01823    
01824    struct ast_iax2_firmware_header *fwh, fwh2;
01825    struct MD5Context md5;
01826    unsigned char sum[16];
01827    unsigned char buf[1024];
01828    int len, chunk;
01829    char *s2;
01830    char *last;
01831    s2 = alloca(strlen(s) + 100);
01832    if (!s2) {
01833       ast_log(LOG_WARNING, "Alloca failed!\n");
01834       return -1;
01835    }
01836    last = strrchr(s, '/');
01837    if (last)
01838       last++;
01839    else
01840       last = s;
01841    snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, (unsigned long)ast_random());
01842    res = stat(s, &stbuf);
01843    if (res < 0) {
01844       ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
01845       return -1;
01846    }
01847    /* Make sure it's not a directory */
01848    if (S_ISDIR(stbuf.st_mode))
01849       return -1;
01850    ifd = open(s, O_RDONLY);
01851    if (ifd < 0) {
01852       ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
01853       return -1;
01854    }
01855    fd = open(s2, O_RDWR | O_CREAT | O_EXCL, 0600);
01856    if (fd < 0) {
01857       ast_log(LOG_WARNING, "Cannot open '%s' for writing: %s\n", s2, strerror(errno));
01858       close(ifd);
01859       return -1;
01860    }
01861    /* Unlink our newly created file */
01862    unlink(s2);
01863    
01864    /* Now copy the firmware into it */
01865    len = stbuf.st_size;
01866    while(len) {
01867       chunk = len;
01868       if (chunk > sizeof(buf))
01869          chunk = sizeof(buf);
01870       res = read(ifd, buf, chunk);
01871       if (res != chunk) {
01872          ast_log(LOG_WARNING, "Only read %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
01873          close(ifd);
01874          close(fd);
01875          return -1;
01876       }
01877       res = write(fd, buf, chunk);
01878       if (res != chunk) {
01879          ast_log(LOG_WARNING, "Only write %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
01880          close(ifd);
01881          close(fd);
01882          return -1;
01883       }
01884       len -= chunk;
01885    }
01886    close(ifd);
01887    /* Return to the beginning */
01888    lseek(fd, 0, SEEK_SET);
01889    if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
01890       ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
01891       close(fd);
01892       return -1;
01893    }
01894    if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
01895       ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
01896       close(fd);
01897       return -1;
01898    }
01899    if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
01900       ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
01901       close(fd);
01902       return -1;
01903    }
01904    if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero((char *)fwh2.devname)) {
01905       ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
01906       close(fd);
01907       return -1;
01908    }
01909    fwh = (struct ast_iax2_firmware_header*)mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 
01910    if (fwh == (void *) -1) {
01911       ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
01912       close(fd);
01913       return -1;
01914    }
01915    MD5Init(&md5);
01916    MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
01917    MD5Final(sum, &md5);
01918    if (memcmp(sum, fwh->chksum, sizeof(sum))) {
01919       ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
01920       munmap((void*)fwh, stbuf.st_size);
01921       close(fd);
01922       return -1;
01923    }
01924    cur = waresl.wares;
01925    while(cur) {
01926       if (!strcmp((char *)cur->fwh->devname, (char *)fwh->devname)) {
01927          /* Found a candidate */
01928          if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
01929             /* The version we have on loaded is older, load this one instead */
01930             break;
01931          /* This version is no newer than what we have.  Don't worry about it.
01932             We'll consider it a proper load anyhow though */
01933          munmap((void*)fwh, stbuf.st_size);
01934          close(fd);
01935          return 0;
01936       }
01937       cur = cur->next;
01938    }
01939    if (!cur) {
01940       /* Allocate a new one and link it */
01941       if ((cur = ast_calloc(1, sizeof(*cur)))) {
01942          cur->fd = -1;
01943          cur->next = waresl.wares;
01944          waresl.wares = cur;
01945       }
01946    }
01947    if (cur) {
01948       if (cur->fwh) {
01949          munmap((void*)cur->fwh, cur->mmaplen);
01950       }
01951       if (cur->fd > -1)
01952          close(cur->fd);
01953       cur->fwh = fwh;
01954       cur->fd = fd;
01955       cur->mmaplen = stbuf.st_size;
01956       cur->dead = 0;
01957    }
01958    return 0;
01959 }

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

Definition at line 5870 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, LOG_WARNING, iax_ie_data::pos, send_command_transfer(), chan_iax2_pvt::transfer, TRANSFER_BEGIN, chan_iax2_pvt::transfercallno, iax_ies::transferid, chan_iax2_pvt::transferid, and chan_iax2_pvt::transferring.

Referenced by socket_process().

05871 {
05872    int newcall = 0;
05873    char newip[256];
05874    struct iax_ie_data ied;
05875    struct sockaddr_in new;
05876    
05877    
05878    memset(&ied, 0, sizeof(ied));
05879    if (ies->apparent_addr)
05880       bcopy(ies->apparent_addr, &new, sizeof(new));
05881    if (ies->callno)
05882       newcall = ies->callno;
05883    if (!newcall || !new.sin_addr.s_addr || !new.sin_port) {
05884       ast_log(LOG_WARNING, "Invalid transfer request\n");
05885       return -1;
05886    }
05887    pvt->transfercallno = newcall;
05888    memcpy(&pvt->transfer, &new, sizeof(pvt->transfer));
05889    inet_aton(newip, &pvt->transfer.sin_addr);
05890    pvt->transfer.sin_family = AF_INET;
05891    pvt->transferring = TRANSFER_BEGIN;
05892    pvt->transferid = ies->transferid;
05893    if (ies->transferid)
05894       iax_ie_append_int(&ied, IAX_IE_TRANSFERID, ies->transferid);
05895    send_command_transfer(pvt, AST_FRAME_IAX, IAX_COMMAND_TXCNT, 0, ied.buf, ied.pos);
05896    return 0; 
05897 }

static int uncompress_subclass ( unsigned char  csub  )  [static]

Definition at line 1092 of file chan_iax2.c.

References IAX_FLAG_SC_LOG, and IAX_MAX_SHIFT.

Referenced by decode_frame(), and socket_process().

01093 {
01094    /* If the SC_LOG flag is set, return 2^csub otherwise csub */
01095    if (csub & IAX_FLAG_SC_LOG) {
01096       /* special case for 'compressed' -1 */
01097       if (csub == 0xff)
01098          return -1;
01099       else
01100          return 1 << (csub & ~IAX_FLAG_SC_LOG & IAX_MAX_SHIFT);
01101    }
01102    else
01103       return csub;
01104 }

static void unlink_peer ( struct iax2_peer *  peer  )  [static]

Definition at line 6146 of file chan_iax2.c.

References ao2_unlink(), ast_sched_del(), iax2_peer::expire, peer_unref(), and iax2_peer::pokeexpire.

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

06147 {
06148    if (peer->expire > -1) {
06149       if (!ast_sched_del(sched, peer->expire)) {
06150          peer->expire = -1;
06151          peer_unref(peer);
06152       }
06153    }
06154 
06155    if (peer->pokeexpire > -1) {
06156       if (!ast_sched_del(sched, peer->pokeexpire)) {
06157          peer->pokeexpire = -1;
06158          peer_unref(peer);
06159       }
06160    }
06161 
06162    ao2_unlink(peers, peer);
06163 }

static int unload_module ( void   )  [static]

Definition at line 11185 of file chan_iax2.c.

References __unload_module(), and ast_custom_function_unregister().

11186 {
11187    ast_custom_function_unregister(&iaxpeer_function);
11188    return __unload_module();
11189 }

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

Definition at line 3603 of file chan_iax2.c.

References ast_mutex_unlock().

Referenced by iax2_bridge().

03604 {
03605    ast_mutex_unlock(&iaxsl[callno1]);
03606    ast_mutex_unlock(&iaxsl[callno0]);
03607 }

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

Definition at line 2570 of file chan_iax2.c.

References iax_frame::af, AST_FRAME_VIDEO, ast_log(), iax_frame::callno, ast_frame::frametype, chan_iax2_pvt::last, LOG_DEBUG, option_debug, and iax_frame::ts.

Referenced by schedule_delivery().

02571 {
02572    /* Video mini frames only encode the lower 15 bits of the session
02573     * timestamp, but other frame types (e.g. audio) encode 16 bits. */
02574    const int ts_shift = (fr->af.frametype == AST_FRAME_VIDEO) ? 15 : 16;
02575    const int lower_mask = (1 << ts_shift) - 1;
02576    const int upper_mask = ~lower_mask;
02577    const int last_upper = iaxs[fr->callno]->last & upper_mask;
02578 
02579    if ( (fr->ts & upper_mask) == last_upper ) {
02580       const int x = fr->ts - iaxs[fr->callno]->last;
02581       const int threshold = (ts_shift == 15) ? 25000 : 50000;
02582 
02583       if (x < -threshold) {
02584          /* Sudden big jump backwards in timestamp:
02585             What likely happened here is that miniframe timestamp has circled but we haven't
02586             gotten the update from the main packet.  We'll just pretend that we did, and
02587             update the timestamp appropriately. */
02588          fr->ts = (last_upper + (1 << ts_shift)) | (fr->ts & lower_mask);
02589          if (option_debug && iaxdebug)
02590             ast_log(LOG_DEBUG, "schedule_delivery: pushed forward timestamp\n");
02591       } else if (x > threshold) {
02592          /* Sudden apparent big jump forwards in timestamp:
02593             What's likely happened is this is an old miniframe belonging to the previous
02594             top 15 or 16-bit timestamp that has turned up out of order.
02595             Adjust the timestamp appropriately. */
02596          fr->ts = (last_upper - (1 << ts_shift)) | (fr->ts & lower_mask);
02597          if (option_debug && iaxdebug)
02598             ast_log(LOG_DEBUG, "schedule_delivery: pushed back timestamp\n");
02599       }
02600    }
02601 }

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

Definition at line 2605 of file chan_iax2.c.

References AST_SCHED_DEL, ast_tvdiff_ms(), ast_tvnow(), chan_iax2_pvt::callno, CALLNO_TO_PTR, get_from_jb(), iax2_sched_add(), chan_iax2_pvt::jb, jb_next(), chan_iax2_pvt::jbid, and chan_iax2_pvt::rxcore.

Referenced by __get_from_jb(), and schedule_delivery().

02606 {
02607    int when;
02608    
02609    when = ast_tvdiff_ms(ast_tvnow(), pvt->rxcore);
02610    
02611    when = jb_next(pvt->jb) - when;
02612 
02613    AST_SCHED_DEL(sched, pvt->jbid);
02614 
02615    if(when <= 0) {
02616       /* XXX should really just empty until when > 0.. */
02617       when = 1;
02618    }
02619    
02620    pvt->jbid = iax2_sched_add(sched, when, get_from_jb, CALLNO_TO_PTR(pvt->callno));
02621 }

static void update_max_nontrunk ( void   )  [static]

Definition at line 1476 of file chan_iax2.c.

References ast_log(), LOG_DEBUG, option_debug, and TRUNK_CALL_START.

Referenced by __find_callno(), and make_trunk().

01477 {
01478    int max = 1;
01479    int x;
01480    /* XXX Prolly don't need locks here XXX */
01481    for (x=1;x<TRUNK_CALL_START - 1; x++) {
01482       if (iaxs[x])
01483          max = x + 1;
01484    }
01485    maxnontrunkcall = max;
01486    if (option_debug && iaxdebug)
01487       ast_log(LOG_DEBUG, "New max nontrunk callno is %d\n", max);
01488 }

static void update_max_trunk ( void   )  [static]

Definition at line 1267 of file chan_iax2.c.

References ARRAY_LEN, ast_log(), LOG_DEBUG, option_debug, and TRUNK_CALL_START.

Referenced by iax2_destroy(), and make_trunk().

01268 {
01269    int max = TRUNK_CALL_START;
01270    int x;
01271 
01272    /* XXX Prolly don't need locks here XXX */
01273    for (x = TRUNK_CALL_START; x < ARRAY_LEN(iaxs) - 1; x++) {
01274       if (iaxs[x]) {
01275          max = x + 1;
01276       }
01277    }
01278 
01279    maxtrunkcall = max;
01280    if (option_debug && iaxdebug)
01281       ast_log(LOG_DEBUG, "New max trunk callno is %d\n", max);
01282 }

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

Definition at line 2207 of file chan_iax2.c.

References iax_frame::callno, iax_frame::data, iax_frame::dcallno, ast_iax2_full_hdr::dcallno, IAX_FLAG_RETRANS, ast_iax2_full_hdr::iseqno, chan_iax2_pvt::iseqno, and iax_frame::iseqno.

Referenced by __attempt_transmit().

02208 {
02209    /* Called with iaxsl lock held, and iaxs[callno] non-NULL */
02210    struct ast_iax2_full_hdr *fh = f->data;
02211    /* Mark this as a retransmission */
02212    fh->dcallno = ntohs(IAX_FLAG_RETRANS | f->dcallno);
02213    /* Update iseqno */
02214    f->iseqno = iaxs[f->callno]->iseqno;
02215    fh->iseqno = f->iseqno;
02216    return 0;
02217 }

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

References iax2_peer::addr, ast_app_inboxcount(), ast_db_del(), ast_db_put(), ast_device_state_changed(), AST_FRAME_IAX, ast_inet_ntoa(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_sched_del(), ast_strdupa, ast_strlen_zero(), ast_test_flag, ast_verbose(), iax_ie_data::buf, iax2_peer::cid_name, iax2_peer::cid_num, 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, iax2_peer::mailbox, manager_event(), iax2_peer::name, option_verbose, peer_ref(), peer_unref(), iax_ie_data::pos, realtime_update_peer(), register_peer_exten(), send_command_final(), iax2_peer::sockfd, VERBOSE_PREFIX_3, and iax2_peer::zonetag.

Referenced by socket_process().

06258 {
06259    /* Called from IAX thread only, with proper iaxsl lock */
06260    struct iax_ie_data ied;
06261    struct iax2_peer *p;
06262    int msgcount;
06263    char data[80];
06264    int version;
06265    const char *peer_name;
06266    int res = -1;
06267 
06268    memset(&ied, 0, sizeof(ied));
06269 
06270    peer_name = ast_strdupa(iaxs[callno]->peer);
06271 
06272    /* SLD: Another find_peer call during registration - this time when we are really updating our registration */
06273    ast_mutex_unlock(&iaxsl[callno]);
06274    if (!(p = find_peer(peer_name, 1))) {
06275       ast_mutex_lock(&iaxsl[callno]);
06276       ast_log(LOG_WARNING, "No such peer '%s'\n", peer_name);
06277       return -1;
06278    }
06279    ast_mutex_lock(&iaxsl[callno]);
06280    if (!iaxs[callno])
06281       goto return_unref;
06282 
06283    if (ast_test_flag((&globalflags), IAX_RTUPDATE) && (ast_test_flag(p, IAX_TEMPONLY|IAX_RTCACHEFRIENDS))) {
06284       if (sin->sin_addr.s_addr) {
06285          time_t nowtime;
06286          time(&nowtime);
06287          realtime_update_peer(peer_name, sin, nowtime);
06288       } else {
06289          realtime_update_peer(peer_name, sin, 0);
06290       }
06291    }
06292    if (inaddrcmp(&p->addr, sin)) {
06293       if (iax2_regfunk)
06294          iax2_regfunk(p->name, 1);
06295       /* Stash the IP address from which they registered */
06296       memcpy(&p->addr, sin, sizeof(p->addr));
06297       snprintf(data, sizeof(data), "%s:%d:%d", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), p->expiry);
06298       if (!ast_test_flag(p, IAX_TEMPONLY) && sin->sin_addr.s_addr) {
06299          ast_db_put("IAX/Registry", p->name, data);
06300          if  (option_verbose > 2)
06301             ast_verbose(VERBOSE_PREFIX_3 "Registered IAX2 '%s' (%s) at %s:%d\n", p->name, 
06302                    ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED", ast_inet_ntoa(sin->sin_addr), ntohs(sin->sin_port));
06303          manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: IAX2/%s\r\nPeerStatus: Registered\r\n", p->name);
06304          register_peer_exten(p, 1);
06305          ast_device_state_changed("IAX2/%s", p->name); /* Activate notification */
06306       } else if (!ast_test_flag(p, IAX_TEMPONLY)) {
06307          if  (option_verbose > 2)
06308             ast_verbose(VERBOSE_PREFIX_3 "Unregistered IAX2 '%s' (%s)\n", p->name, 
06309                    ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED");
06310          manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: IAX2/%s\r\nPeerStatus: Unregistered\r\n", p->name);
06311          register_peer_exten(p, 0);
06312          ast_db_del("IAX/Registry", p->name);
06313          ast_device_state_changed("IAX2/%s", p->name); /* Activate notification */
06314       }
06315       /* Update the host */
06316       /* Verify that the host is really there */
06317       iax2_poke_peer(p, callno);
06318    }     
06319 
06320    /* Make sure our call still exists, an INVAL at the right point may make it go away */
06321    if (!iaxs[callno]) {
06322       res = -1;
06323       goto return_unref;
06324    }
06325 
06326    /* Store socket fd */
06327    p->sockfd = fd;
06328    /* Setup the expiry */
06329    if (p->expire > -1) {
06330       if (!ast_sched_del(sched, p->expire)) {
06331          p->expire = -1;
06332          peer_unref(p);
06333       }
06334    }
06335    /* treat an unspecified refresh interval as the minimum */
06336    if (!refresh)
06337       refresh = min_reg_expire;
06338    if (refresh > max_reg_expire) {
06339       ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
06340          p->name, max_reg_expire, refresh);
06341       p->expiry = max_reg_expire;
06342    } else if (refresh < min_reg_expire) {
06343       ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
06344          p->name, min_reg_expire, refresh);
06345       p->expiry = min_reg_expire;
06346    } else {
06347       p->expiry = refresh;
06348    }
06349    if (p->expiry && sin->sin_addr.s_addr) {
06350       p->expire = iax2_sched_add(sched, (p->expiry + 10) * 1000, expire_registry, peer_ref(p));
06351       if (p->expire == -1)
06352          peer_unref(p);
06353    }
06354    iax_ie_append_str(&ied, IAX_IE_USERNAME, p->name);
06355    iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime(p->zonetag));
06356    if (sin->sin_addr.s_addr) {
06357       iax_ie_append_short(&ied, IAX_IE_REFRESH, p->expiry);
06358       iax_ie_append_addr(&ied, IAX_IE_APPARENT_ADDR, &p->addr);
06359       if (!ast_strlen_zero(p->mailbox)) {
06360          int new, old;
06361          ast_app_inboxcount(p->mailbox, &new, &old);
06362          if (new > 255)
06363             new = 255;
06364          if (old > 255)
06365             old = 255;
06366          msgcount = (old << 8) | new;
06367          iax_ie_append_short(&ied, IAX_IE_MSGCOUNT, msgcount);
06368       }
06369       if (ast_test_flag(p, IAX_HASCALLERID)) {
06370          iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, p->cid_num);
06371          iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, p->cid_name);
06372       }
06373    }
06374    version = iax_check_version(devtype);
06375    if (version) 
06376       iax_ie_append_short(&ied, IAX_IE_FIRMWAREVER, version);
06377 
06378    res = 0;
06379 
06380 return_unref:
06381    peer_unref(p);
06382 
06383    return res ? res : send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGACK, 0, ied.buf, ied.pos, -1);
06384 }

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

References iax2_user::name.

Referenced by load_module().

01140 {
01141    struct iax2_user *user = obj, *user2 = arg;
01142 
01143    return !strcmp(user->name, user2->name) ? CMP_MATCH | CMP_STOP : 0;
01144 }

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

Definition at line 9891 of file chan_iax2.c.

References ast_set_flag, and IAX_DELME.

Referenced by delete_users().

09892 {
09893    struct iax2_user *user = obj;
09894 
09895    ast_set_flag(user, IAX_DELME);
09896 
09897    return 0;
09898 }

static void user_destructor ( void *  obj  )  [static]

Definition at line 9655 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().

09656 {
09657    struct iax2_user *user = obj;
09658 
09659    ast_free_ha(user->ha);
09660    free_context(user->contexts);
09661    if(user->vars) {
09662       ast_variables_destroy(user->vars);
09663       user->vars = NULL;
09664    }
09665    ast_string_field_free_memory(user);
09666 }

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

References ast_str_hash(), and iax2_user::name.

Referenced by load_module().

01130 {
01131    const struct iax2_user *user = obj;
01132 
01133    return ast_str_hash(user->name);
01134 }

static struct iax2_user* user_ref ( struct iax2_user *  user  )  [static, read]

Definition at line 1178 of file chan_iax2.c.

References ao2_ref().

01179 {
01180    ao2_ref(user, +1);
01181    return user;
01182 }

static struct iax2_user* user_unref ( struct iax2_user *  user  )  [static, read]

Definition at line 1184 of file chan_iax2.c.

References ao2_ref().

Referenced by authenticate_request(), authenticate_verify(), build_user(), check_access(), iax2_destroy_helper(), iax2_show_users(), prune_users(), and set_config().

01185 {
01186    ao2_ref(user, -1);
01187    return NULL;
01188 }

static void vnak_retransmit ( int  callno,
int  last 
) [static]

Definition at line 6573 of file chan_iax2.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, iax_frame::callno, f, iaxq, iax_frame::oseqno, ast_iax2_queue::queue, iax_frame::retries, and send_packet().

Referenced by socket_process().

06574 {
06575    struct iax_frame *f;
06576 
06577    AST_LIST_LOCK(&iaxq.queue);
06578    AST_LIST_TRAVERSE(&iaxq.queue, f, list) {
06579       /* Send a copy immediately */
06580       if ((f->callno == callno) && iaxs[f->callno] &&
06581          ((unsigned char ) (f->oseqno - last) < 128) &&
06582          (f->retries >= 0)) {
06583          send_packet(f);
06584       }
06585    }
06586    AST_LIST_UNLOCK(&iaxq.queue);
06587 }

static int wait_for_peercallno ( struct chan_iax2_pvt *  pvt  )  [static]

Note:
expects the pvt to be locked

Definition at line 3501 of file chan_iax2.c.

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

Referenced by iax2_indicate(), and iax2_setoption().

03502 {
03503    unsigned short callno = pvt->callno;
03504 
03505    if (!pvt->peercallno) {
03506       /* We don't know the remote side's call number, yet.  :( */
03507       int count = 10;
03508       while (count-- && pvt && !pvt->peercallno) {
03509          DEADLOCK_AVOIDANCE(&iaxsl[callno]);
03510          pvt = iaxs[callno];
03511       }
03512       if (!pvt->peercallno) {
03513          return -1;
03514       }
03515    }
03516 
03517    return 0;
03518 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .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 11346 of file chan_iax2.c.

char accountcode[AST_MAX_ACCOUNT_CODE] [static]

int adsi = 0 [static]

Definition at line 219 of file chan_iax2.c.

int amaflags = 0 [static]

Definition at line 218 of file chan_iax2.c.

Referenced by pgsql_log().

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 11346 of file chan_iax2.c.

int authdebug = 1 [static]

Definition at line 155 of file chan_iax2.c.

int autokill = 0 [static]

Definition at line 156 of file chan_iax2.c.

struct ast_cli_entry cli_iax2[] [static]

Definition at line 11001 of file chan_iax2.c.

Initial value:

 {
   { "iax2", "jb", "debug", NULL },
   iax2_do_jb_debug, NULL,
   NULL }

Definition at line 10981 of file chan_iax2.c.

Initial value:

 {
   { "iax2", "no", "debug", NULL },
   iax2_no_debug, NULL,
   NULL }

Definition at line 10986 of file chan_iax2.c.

Initial value:

 {
   { "iax2", "no", "jb", "debug", NULL },
   iax2_no_jb_debug, NULL,
   NULL }

Definition at line 10996 of file chan_iax2.c.

Initial value:

 {
   { "iax2", "no", "trunk", "debug", NULL },
   iax2_no_trunk_debug, NULL,
   NULL }

Definition at line 10991 of file chan_iax2.c.

Initial value:

 {
   { "iax2", "trunk", "debug", NULL },
   iax2_do_trunk_debug, NULL,
   NULL }

Definition at line 10976 of file chan_iax2.c.

char context[80] = "default" [static]

Definition at line 142 of file chan_iax2.c.

char debug_jb_usage[] [static]

Initial value:

"Usage: iax2 set debug jb\n"
"       Enables jitterbuffer debugging information\n"

Definition at line 10950 of file chan_iax2.c.

char debug_trunk_usage[] [static]

Initial value:

"Usage: iax2 set debug trunk\n"
"       Requests current status of IAX trunking\n"

Definition at line 10942 of file chan_iax2.c.

char debug_usage[] [static]

Initial value:

 
"Usage: iax2 set debug\n"
"       Enables dumping of IAX packets for debugging purposes\n"

Definition at line 10934 of file chan_iax2.c.

int defaultsockfd = -1 [static]

Definition at line 172 of file chan_iax2.c.

int delayreject = 0 [static]

Definition at line 220 of file chan_iax2.c.

struct iax2_dpcache * dpcache [static]

Referenced by find_cache(), and iax2_show_cache().

ast_mutex_t dpcache_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

int global_rtautoclear = 120 [static]

Definition at line 274 of file chan_iax2.c.

struct ast_flags globalflags = { 0 } [static]

Definition at line 223 of file chan_iax2.c.

int iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH [static]

Definition at line 201 of file chan_iax2.c.

int iax2_encryption = 0 [static]

Definition at line 221 of file chan_iax2.c.

enum { ... } iax2_flags

int(* iax2_regfunk)(const char *username, int onoff) = NULL

char iax2_reload_usage[] [static]

Initial value:

"Usage: iax2 reload\n"
"       Reloads IAX configuration from iax.conf\n"

Definition at line 10892 of file chan_iax2.c.

enum { ... } iax2_state

struct ast_switch iax2_switch [static]

Definition at line 10866 of file chan_iax2.c.

struct ast_channel_tech iax2_tech [static]

Definition at line 864 of file chan_iax2.c.

char iax2_test_losspct_usage[] [static]

Initial value:

"Usage: iax2 test losspct <percentage>\n"
"       For testing, throws away <percentage> percent of incoming packets\n"

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

int iaxactivethreadcount = 0 [static]

Definition at line 451 of file chan_iax2.c.

Referenced by __unload_module(), iax2_process_thread(), and iax2_process_thread_cleanup().

int iaxcompat = 0 [static]

Definition at line 157 of file chan_iax2.c.

int iaxdebug = 0 [static]

Definition at line 203 of file chan_iax2.c.

int iaxdefaultdpcache = 10 * 60 [static]

Definition at line 159 of file chan_iax2.c.

int iaxdefaulttimeout = 5 [static]

Definition at line 161 of file chan_iax2.c.

int iaxdynamicthreadcount = 0 [static]

Definition at line 449 of file chan_iax2.c.

Referenced by find_idle_thread(), and iax2_process_thread().

int iaxdynamicthreadnum = 0 [static]

Definition at line 450 of file chan_iax2.c.

Referenced by find_idle_thread().

int iaxmaxthreadcount = DEFAULT_MAX_THREAD_COUNT [static]

Definition at line 448 of file chan_iax2.c.

Referenced by find_idle_thread(), and set_config().

Definition at line 10802 of file chan_iax2.c.

struct ast_iax2_queue iaxq [static]

struct chan_iax2_pvt* iaxs[IAX_MAX_CALLS] [static]

Definition at line 810 of file chan_iax2.c.

Referenced by iax_showframe().

ast_mutex_t iaxsl[ARRAY_LEN(iaxs)] [static]

Definition at line 811 of file chan_iax2.c.

int iaxthreadcount = DEFAULT_THREAD_COUNT [static]

Definition at line 447 of file chan_iax2.c.

Referenced by iax2_show_threads(), set_config(), and start_network_thread().

int iaxtrunkdebug = 0 [static]

Definition at line 205 of file chan_iax2.c.

struct io_context* io [static]

Definition at line 198 of file chan_iax2.c.

int lagrq_time = 10 [static]

Definition at line 150 of file chan_iax2.c.

char language[MAX_LANGUAGE] = "" [static]

Definition at line 144 of file chan_iax2.c.

struct timeval lastused[ARRAY_LEN(iaxs)] [static]

Definition at line 812 of file chan_iax2.c.

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

int max_reg_expire [static]

Definition at line 166 of file chan_iax2.c.

int max_retries = 4 [static]

Definition at line 148 of file chan_iax2.c.

int maxauthreq = 3 [static]

Definition at line 147 of file chan_iax2.c.

int maxjitterbuffer = 1000 [static]

Definition at line 151 of file chan_iax2.c.

int maxjitterinterps = 10 [static]

Definition at line 153 of file chan_iax2.c.

int maxnontrunkcall = 1 [static]

Definition at line 830 of file chan_iax2.c.

int maxtrunkcall = TRUNK_CALL_START [static]

Definition at line 829 of file chan_iax2.c.

int min_reg_expire [static]

Definition at line 165 of file chan_iax2.c.

char mohinterpret[MAX_MUSICCLASS] [static]

Definition at line 216 of file chan_iax2.c.

char mohsuggest[MAX_MUSICCLASS] [static]

Definition at line 217 of file chan_iax2.c.

Referenced by check_user_full(), create_addr_from_peer(), and sip_alloc().

struct ast_netsock_list* netsock [static]

Definition at line 170 of file chan_iax2.c.

pthread_t netthreadid = AST_PTHREADT_NULL [static]

Definition at line 225 of file chan_iax2.c.

char no_debug_jb_usage[] [static]

Initial value:

"Usage: iax2 set debug jb off\n"
"       Disables jitterbuffer debugging information\n"

Definition at line 10954 of file chan_iax2.c.

char no_debug_trunk_usage[] [static]

Initial value:

"Usage: iax2 set debug trunk off\n"
"       Requests current status of IAX trunking\n"

Definition at line 10946 of file chan_iax2.c.

char no_debug_usage[] [static]

Initial value:

 
"Usage: iax2 set debug off\n"
"       Disables dumping of IAX packets for debugging purposes\n"

Definition at line 10938 of file chan_iax2.c.

struct ast_netsock_list* outsock [static]

used if sourceaddress specified and bindaddr == INADDR_ANY

Definition at line 171 of file chan_iax2.c.

char* papp = "IAX2Provision" [static]

Definition at line 8907 of file chan_iax2.c.

char* pdescrip [static]

Initial value:

 
"  IAX2Provision([template]): Provisions the calling IAXy (assuming\n"
"the calling entity is in fact an IAXy) with the given template or\n"
"default if one is not specified.  Returns -1 on error or 0 on success.\n"

Definition at line 8909 of file chan_iax2.c.

struct ao2_container* peers [static]

Definition at line 648 of file chan_iax2.c.

int ping_time = 21 [static]

Definition at line 149 of file chan_iax2.c.

struct ast_codec_pref prefs [static]

char prune_realtime_usage[] [static]

Initial value:

"Usage: iax2 prune realtime [<peername>|all]\n"
"       Prunes object(s) from the cache\n"

Definition at line 10888 of file chan_iax2.c.

char* psyn = "Provision a calling IAXy with a given template" [static]

Definition at line 8908 of file chan_iax2.c.

char regcontext[AST_MAX_CONTEXT] = "" [static]

Definition at line 145 of file chan_iax2.c.

int resyncthreshold = 1000 [static]

Definition at line 152 of file chan_iax2.c.

struct sched_context* sched [static]

Definition at line 199 of file chan_iax2.c.

Definition at line 228 of file chan_iax2.c.

ast_mutex_t sched_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 227 of file chan_iax2.c.

Referenced by __unload_module(), iax2_sched_add(), iax2_transmit(), and sched_thread().

pthread_t schedthreadid = AST_PTHREADT_NULL [static]

Definition at line 226 of file chan_iax2.c.

char show_cache_usage[] [static]

Initial value:

"Usage: iax2 show cache\n"
"       Display currently cached IAX Dialplan results.\n"

Definition at line 10880 of file chan_iax2.c.

char show_channels_usage[] [static]

Initial value:

 
"Usage: iax2 show channels\n"
"       Lists all currently active IAX channels.\n"

Definition at line 10908 of file chan_iax2.c.

char show_firmware_usage[] [static]

Initial value:

 
"Usage: iax2 show firmware\n"
"       Lists all known IAX firmware images.\n"

Definition at line 10926 of file chan_iax2.c.

char show_netstats_usage[] [static]

Initial value:

 
"Usage: iax2 show netstats\n"
"       Lists network status for all currently active IAX channels.\n"

Definition at line 10912 of file chan_iax2.c.

char show_peer_usage[] [static]

Initial value:

"Usage: iax2 show peer <name>\n"
"       Display details on specific IAX peer\n"

Definition at line 10884 of file chan_iax2.c.

char show_peers_usage[] [static]

Initial value:

 
"Usage: iax2 show peers [registered] [like <pattern>]\n"
"       Lists all known IAX2 peers.\n"
"       Optional 'registered' argument lists only peers with known addresses.\n"
"       Optional regular expression pattern is used to filter the peer list.\n"

Definition at line 10920 of file chan_iax2.c.

char show_prov_usage[] [static]

Definition at line 10896 of file chan_iax2.c.

char show_reg_usage[] [static]

Initial value:

"Usage: iax2 show registry\n"
"       Lists all registration requests and status.\n"

Definition at line 10930 of file chan_iax2.c.

char show_stats_usage[] [static]

Initial value:

"Usage: iax2 show stats\n"
"       Display statistics on IAX channel driver.\n"

Definition at line 10876 of file chan_iax2.c.

char show_threads_usage[] [static]

Initial value:

 
"Usage: iax2 show threads\n"
"       Lists status of IAX helper threads\n"

Definition at line 10916 of file chan_iax2.c.

char show_users_usage[] [static]

Initial value:

 
"Usage: iax2 show users [like <pattern>]\n"
"       Lists all known IAX2 users.\n"
"       Optional regular expression pattern is used to filter the user list.\n"

Definition at line 10903 of file chan_iax2.c.

const char tdesc[] = "Inter Asterisk eXchange Driver (Ver 2)" [static]

Definition at line 140 of file chan_iax2.c.

int test_losspct = 0 [static]

Definition at line 207 of file chan_iax2.c.

int timingfd = -1 [static]

Definition at line 168 of file chan_iax2.c.

unsigned int tos = 0 [static]

Definition at line 163 of file chan_iax2.c.

ast_mutex_t tpeerlock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 380 of file chan_iax2.c.

Referenced by find_tpeer(), and timing_read().

struct iax2_trunk_peer * tpeers [static]

Referenced by find_tpeer(), and timing_read().

int trunkfreq = 20 [static]

Definition at line 154 of file chan_iax2.c.

struct ao2_container* users [static]

Definition at line 651 of file chan_iax2.c.

struct ast_firmware_list waresl [static]


Generated on Fri Jul 3 20:37:14 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9