![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
chan_iax2.c
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2006, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * 00021 * \brief Implementation of Inter-Asterisk eXchange Version 2 00022 * 00023 * \author Mark Spencer <markster@digium.com> 00024 * 00025 * \par See also 00026 * \arg \ref Config_iax 00027 * 00028 * \ingroup channel_drivers 00029 */ 00030 00031 /*** MODULEINFO 00032 <use>zaptel</use> 00033 ***/ 00034 00035 #include "asterisk.h" 00036 00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 53128 $") 00038 00039 #include <stdlib.h> 00040 #include <stdio.h> 00041 #include <sys/types.h> 00042 #include <sys/mman.h> 00043 #include <dirent.h> 00044 #include <sys/socket.h> 00045 #include <netinet/in.h> 00046 #include <arpa/inet.h> 00047 #include <netinet/in_systm.h> 00048 #include <netinet/ip.h> 00049 #include <sys/time.h> 00050 #include <sys/signal.h> 00051 #include <signal.h> 00052 #include <string.h> 00053 #include <strings.h> 00054 #include <errno.h> 00055 #include <unistd.h> 00056 #include <netdb.h> 00057 #include <fcntl.h> 00058 #include <sys/stat.h> 00059 #include <regex.h> 00060 00061 #include "asterisk/zapata.h" 00062 00063 #include "asterisk/lock.h" 00064 #include "asterisk/frame.h" 00065 #include "asterisk/channel.h" 00066 #include "asterisk/logger.h" 00067 #include "asterisk/module.h" 00068 #include "asterisk/pbx.h" 00069 #include "asterisk/sched.h" 00070 #include "asterisk/io.h" 00071 #include "asterisk/config.h" 00072 #include "asterisk/options.h" 00073 #include "asterisk/cli.h" 00074 #include "asterisk/translate.h" 00075 #include "asterisk/md5.h" 00076 #include "asterisk/cdr.h" 00077 #include "asterisk/crypto.h" 00078 #include "asterisk/acl.h" 00079 #include "asterisk/manager.h" 00080 #include "asterisk/callerid.h" 00081 #include "asterisk/app.h" 00082 #include "asterisk/astdb.h" 00083 #include "asterisk/musiconhold.h" 00084 #include "asterisk/features.h" 00085 #include "asterisk/utils.h" 00086 #include "asterisk/causes.h" 00087 #include "asterisk/localtime.h" 00088 #include "asterisk/aes.h" 00089 #include "asterisk/dnsmgr.h" 00090 #include "asterisk/devicestate.h" 00091 #include "asterisk/netsock.h" 00092 #include "asterisk/stringfields.h" 00093 #include "asterisk/linkedlists.h" 00094 00095 #include "iax2.h" 00096 #include "iax2-parser.h" 00097 #include "iax2-provision.h" 00098 #include "jitterbuf.h" 00099 00100 /* Define SCHED_MULTITHREADED to run the scheduler in a special 00101 multithreaded mode. */ 00102 #define SCHED_MULTITHREADED 00103 00104 /* Define DEBUG_SCHED_MULTITHREADED to keep track of where each 00105 thread is actually doing. */ 00106 #define DEBUG_SCHED_MULTITHREAD 00107 00108 #ifndef IPTOS_MINCOST 00109 #define IPTOS_MINCOST 0x02 00110 #endif 00111 00112 #ifdef SO_NO_CHECK 00113 static int nochecksums = 0; 00114 #endif 00115 00116 00117 #define PTR_TO_CALLNO(a) ((unsigned short)(unsigned long)(a)) 00118 #define CALLNO_TO_PTR(a) ((void *)(unsigned long)(a)) 00119 00120 #define DEFAULT_THREAD_COUNT 10 00121 #define DEFAULT_MAX_THREAD_COUNT 100 00122 #define DEFAULT_RETRY_TIME 1000 00123 #define MEMORY_SIZE 100 00124 #define DEFAULT_DROP 3 00125 /* Flag to use with trunk calls, keeping these calls high up. It halves our effective use 00126 but keeps the division between trunked and non-trunked better. */ 00127 #define TRUNK_CALL_START 0x4000 00128 00129 #define DEBUG_SUPPORT 00130 00131 #define MIN_REUSE_TIME 60 /* Don't reuse a call number within 60 seconds */ 00132 00133 /* Sample over last 100 units to determine historic jitter */ 00134 #define GAMMA (0.01) 00135 00136 static struct ast_codec_pref prefs; 00137 00138 static const char tdesc[] = "Inter Asterisk eXchange Driver (Ver 2)"; 00139 00140 00141 /*! \brief Maximum transmission unit for the UDP packet in the trunk not to be 00142 fragmented. This is based on 1516 - ethernet - ip - udp - iax minus one g711 frame = 1240 */ 00143 #define MAX_TRUNK_MTU 1240 00144 00145 static int global_max_trunk_mtu; /*!< Maximum MTU, 0 if not used */ 00146 static int trunk_timed, trunk_untimed, trunk_maxmtu, trunk_nmaxmtu ; /*!< Trunk MTU statistics */ 00147 00148 00149 static char context[80] = "default"; 00150 00151 static char language[MAX_LANGUAGE] = ""; 00152 static char regcontext[AST_MAX_CONTEXT] = ""; 00153 00154 static int maxauthreq = 3; 00155 static int max_retries = 4; 00156 static int ping_time = 20; 00157 static int lagrq_time = 10; 00158 static int maxtrunkcall = TRUNK_CALL_START; 00159 static int maxnontrunkcall = 1; 00160 static int maxjitterbuffer=1000; 00161 static int resyncthreshold=1000; 00162 static int maxjitterinterps=10; 00163 static int jittertargetextra = 40; /* number of milliseconds the new jitter buffer adds on to its size */ 00164 00165 #define MAX_TRUNKDATA 640 * 200 /*!< 40ms, uncompressed linear * 200 channels */ 00166 00167 static int trunkfreq = 20; 00168 static int trunkmaxsize = MAX_TRUNKDATA; 00169 00170 static int authdebug = 1; 00171 static int autokill = 0; 00172 static int iaxcompat = 0; 00173 00174 static int iaxdefaultdpcache=10 * 60; /* Cache dialplan entries for 10 minutes by default */ 00175 00176 static int iaxdefaulttimeout = 5; /* Default to wait no more than 5 seconds for a reply to come back */ 00177 00178 static unsigned int tos = 0; 00179 00180 static int min_reg_expire; 00181 static int max_reg_expire; 00182 00183 static int srvlookup = 0; 00184 00185 static int timingfd = -1; /* Timing file descriptor */ 00186 00187 static struct ast_netsock_list *netsock; 00188 static int defaultsockfd = -1; 00189 00190 int (*iax2_regfunk)(const char *username, int onoff) = NULL; 00191 00192 /* Ethernet, etc */ 00193 #define IAX_CAPABILITY_FULLBANDWIDTH 0xFFFF 00194 /* T1, maybe ISDN */ 00195 #define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \ 00196 ~AST_FORMAT_SLINEAR & \ 00197 ~AST_FORMAT_ULAW & \ 00198 ~AST_FORMAT_ALAW & \ 00199 ~AST_FORMAT_G722) 00200 /* A modem */ 00201 #define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \ 00202 ~AST_FORMAT_G726 & \ 00203 ~AST_FORMAT_G726_AAL2 & \ 00204 ~AST_FORMAT_ADPCM) 00205 00206 #define IAX_CAPABILITY_LOWFREE (IAX_CAPABILITY_LOWBANDWIDTH & \ 00207 ~AST_FORMAT_G723_1) 00208 00209 00210 #define DEFAULT_MAXMS 2000 /* Must be faster than 2 seconds by default */ 00211 #define DEFAULT_FREQ_OK 60 * 1000 /* How often to check for the host to be up */ 00212 #define DEFAULT_FREQ_NOTOK 10 * 1000 /* How often to check, if the host is down... */ 00213 00214 static struct io_context *io; 00215 static struct sched_context *sched; 00216 00217 static int iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH; 00218 00219 static int iaxdebug = 0; 00220 00221 static int iaxtrunkdebug = 0; 00222 00223 static int test_losspct = 0; 00224 #ifdef IAXTESTS 00225 static int test_late = 0; 00226 static int test_resync = 0; 00227 static int test_jit = 0; 00228 static int test_jitpct = 0; 00229 #endif /* IAXTESTS */ 00230 00231 static char accountcode[AST_MAX_ACCOUNT_CODE]; 00232 static char mohinterpret[MAX_MUSICCLASS]; 00233 static char mohsuggest[MAX_MUSICCLASS]; 00234 static int amaflags = 0; 00235 static int adsi = 0; 00236 static int delayreject = 0; 00237 static int iax2_encryption = 0; 00238 00239 static struct ast_flags globalflags = { 0 }; 00240 00241 static pthread_t netthreadid = AST_PTHREADT_NULL; 00242 static pthread_t schedthreadid = AST_PTHREADT_NULL; 00243 AST_MUTEX_DEFINE_STATIC(sched_lock); 00244 static ast_cond_t sched_cond; 00245 00246 enum iax2_state { 00247 IAX_STATE_STARTED = (1 << 0), 00248 IAX_STATE_AUTHENTICATED = (1 << 1), 00249 IAX_STATE_TBD = (1 << 2), 00250 IAX_STATE_UNCHANGED = (1 << 3), 00251 }; 00252 00253 struct iax2_context { 00254 char context[AST_MAX_CONTEXT]; 00255 struct iax2_context *next; 00256 }; 00257 00258 enum iax2_flags { 00259 IAX_HASCALLERID = (1 << 0), /*!< CallerID has been specified */ 00260 IAX_DELME = (1 << 1), /*!< Needs to be deleted */ 00261 IAX_TEMPONLY = (1 << 2), /*!< Temporary (realtime) */ 00262 IAX_TRUNK = (1 << 3), /*!< Treat as a trunk */ 00263 IAX_NOTRANSFER = (1 << 4), /*!< Don't native bridge */ 00264 IAX_USEJITTERBUF = (1 << 5), /*!< Use jitter buffer */ 00265 IAX_DYNAMIC = (1 << 6), /*!< dynamic peer */ 00266 IAX_SENDANI = (1 << 7), /*!< Send ANI along with CallerID */ 00267 /* (1 << 8) is currently unused due to the deprecation of an old option. Go ahead, take it! */ 00268 IAX_ALREADYGONE = (1 << 9), /*!< Already disconnected */ 00269 IAX_PROVISION = (1 << 10), /*!< This is a provisioning request */ 00270 IAX_QUELCH = (1 << 11), /*!< Whether or not we quelch audio */ 00271 IAX_ENCRYPTED = (1 << 12), /*!< Whether we should assume encrypted tx/rx */ 00272 IAX_KEYPOPULATED = (1 << 13), /*!< Whether we have a key populated */ 00273 IAX_CODEC_USER_FIRST = (1 << 14), /*!< are we willing to let the other guy choose the codec? */ 00274 IAX_CODEC_NOPREFS = (1 << 15), /*!< Force old behaviour by turning off prefs */ 00275 IAX_CODEC_NOCAP = (1 << 16), /*!< only consider requested format and ignore capabilities*/ 00276 IAX_RTCACHEFRIENDS = (1 << 17), /*!< let realtime stay till your reload */ 00277 IAX_RTUPDATE = (1 << 18), /*!< Send a realtime update */ 00278 IAX_RTAUTOCLEAR = (1 << 19), /*!< erase me on expire */ 00279 IAX_FORCEJITTERBUF = (1 << 20), /*!< Force jitterbuffer, even when bridged to a channel that can take jitter */ 00280 IAX_RTIGNOREREGEXPIRE = (1 << 21), /*!< When using realtime, ignore registration expiration */ 00281 IAX_TRUNKTIMESTAMPS = (1 << 22), /*!< Send trunk timestamps */ 00282 IAX_TRANSFERMEDIA = (1 << 23), /*!< When doing IAX2 transfers, transfer media only */ 00283 IAX_MAXAUTHREQ = (1 << 24), /*!< Maximum outstanding AUTHREQ restriction is in place */ 00284 }; 00285 00286 static int global_rtautoclear = 120; 00287 00288 static int reload_config(void); 00289 static int iax2_reload(int fd, int argc, char *argv[]); 00290 00291 00292 struct iax2_user { 00293 AST_DECLARE_STRING_FIELDS( 00294 AST_STRING_FIELD(name); 00295 AST_STRING_FIELD(secret); 00296 AST_STRING_FIELD(dbsecret); 00297 AST_STRING_FIELD(accountcode); 00298 AST_STRING_FIELD(mohinterpret); 00299 AST_STRING_FIELD(mohsuggest); 00300 AST_STRING_FIELD(inkeys); /*!< Key(s) this user can use to authenticate to us */ 00301 AST_STRING_FIELD(language); 00302 AST_STRING_FIELD(cid_num); 00303 AST_STRING_FIELD(cid_name); 00304 ); 00305 00306 int authmethods; 00307 int encmethods; 00308 int amaflags; 00309 int adsi; 00310 unsigned int flags; 00311 int capability; 00312 int maxauthreq; /*!< Maximum allowed outstanding AUTHREQs */ 00313 int curauthreq; /*!< Current number of outstanding AUTHREQs */ 00314 struct ast_codec_pref prefs; 00315 struct ast_ha *ha; 00316 struct iax2_context *contexts; 00317 struct ast_variable *vars; 00318 AST_LIST_ENTRY(iax2_user) entry; 00319 }; 00320 00321 struct iax2_peer { 00322 AST_DECLARE_STRING_FIELDS( 00323 AST_STRING_FIELD(name); 00324 AST_STRING_FIELD(username); 00325 AST_STRING_FIELD(secret); 00326 AST_STRING_FIELD(dbsecret); 00327 AST_STRING_FIELD(outkey); /*!< What key we use to talk to this peer */ 00328 00329 AST_STRING_FIELD(regexten); /*!< Extension to register (if regcontext is used) */ 00330 AST_STRING_FIELD(context); /*!< For transfers only */ 00331 AST_STRING_FIELD(peercontext); /*!< Context to pass to peer */ 00332 AST_STRING_FIELD(mailbox); /*!< Mailbox */ 00333 AST_STRING_FIELD(mohinterpret); 00334 AST_STRING_FIELD(mohsuggest); 00335 AST_STRING_FIELD(inkeys); /*!< Key(s) this peer can use to authenticate to us */ 00336 /* Suggested caller id if registering */ 00337 AST_STRING_FIELD(cid_num); /*!< Default context (for transfer really) */ 00338 AST_STRING_FIELD(cid_name); /*!< Default context (for transfer really) */ 00339 AST_STRING_FIELD(zonetag); /*!< Time Zone */ 00340 ); 00341 struct ast_codec_pref prefs; 00342 struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */ 00343 struct sockaddr_in addr; 00344 int formats; 00345 int sockfd; /*!< Socket to use for transmission */ 00346 struct in_addr mask; 00347 int adsi; 00348 unsigned int flags; 00349 00350 /* Dynamic Registration fields */ 00351 struct sockaddr_in defaddr; /*!< Default address if there is one */ 00352 int authmethods; /*!< Authentication methods (IAX_AUTH_*) */ 00353 int encmethods; /*!< Encryption methods (IAX_ENCRYPT_*) */ 00354 00355 int expire; /*!< Schedule entry for expiry */ 00356 int expiry; /*!< How soon to expire */ 00357 int capability; /*!< Capability */ 00358 00359 /* Qualification */ 00360 int callno; /*!< Call number of POKE request */ 00361 int pokeexpire; /*!< Scheduled qualification-related task (ie iax2_poke_peer_s or iax2_poke_noanswer) */ 00362 int lastms; /*!< How long last response took (in ms), or -1 for no response */ 00363 int maxms; /*!< Max ms we will accept for the host to be up, 0 to not monitor */ 00364 00365 int pokefreqok; /*!< How often to check if the host is up */ 00366 int pokefreqnotok; /*!< How often to check when the host has been determined to be down */ 00367 int historicms; /*!< How long recent average responses took */ 00368 int smoothing; /*!< Sample over how many units to determine historic ms */ 00369 00370 struct ast_ha *ha; 00371 AST_LIST_ENTRY(iax2_peer) entry; 00372 }; 00373 00374 #define IAX2_TRUNK_PREFACE (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr)) 00375 00376 struct iax2_trunk_peer { 00377 ast_mutex_t lock; 00378 int sockfd; 00379 struct sockaddr_in addr; 00380 struct timeval txtrunktime; /*!< Transmit trunktime */ 00381 struct timeval rxtrunktime; /*!< Receive trunktime */ 00382 struct timeval lasttxtime; /*!< Last transmitted trunktime */ 00383 struct timeval trunkact; /*!< Last trunk activity */ 00384 unsigned int lastsent; /*!< Last sent time */ 00385 /* Trunk data and length */ 00386 unsigned char *trunkdata; 00387 unsigned int trunkdatalen; 00388 unsigned int trunkdataalloc; 00389 int trunkmaxmtu; 00390 int trunkerror; 00391 int calls; 00392 AST_LIST_ENTRY(iax2_trunk_peer) list; 00393 }; 00394 00395 static AST_LIST_HEAD_STATIC(tpeers, iax2_trunk_peer); 00396 00397 struct iax_firmware { 00398 AST_LIST_ENTRY(iax_firmware) list; 00399 int fd; 00400 int mmaplen; 00401 int dead; 00402 struct ast_iax2_firmware_header *fwh; 00403 unsigned char *buf; 00404 }; 00405 00406 enum iax_reg_state { 00407 REG_STATE_UNREGISTERED = 0, 00408 REG_STATE_REGSENT, 00409 REG_STATE_AUTHSENT, 00410 REG_STATE_REGISTERED, 00411 REG_STATE_REJECTED, 00412 REG_STATE_TIMEOUT, 00413 REG_STATE_NOAUTH 00414 }; 00415 00416 enum iax_transfer_state { 00417 TRANSFER_NONE = 0, 00418 TRANSFER_BEGIN, 00419 TRANSFER_READY, 00420 TRANSFER_RELEASED, 00421 TRANSFER_PASSTHROUGH, 00422 TRANSFER_MBEGIN, 00423 TRANSFER_MREADY, 00424 TRANSFER_MRELEASED, 00425 TRANSFER_MPASSTHROUGH, 00426 TRANSFER_MEDIA, 00427 TRANSFER_MEDIAPASS 00428 }; 00429 00430 struct iax2_registry { 00431 struct sockaddr_in addr; /*!< Who we connect to for registration purposes */ 00432 char username[80]; 00433 char secret[80]; /*!< Password or key name in []'s */ 00434 char random[80]; 00435 int expire; /*!< Sched ID of expiration */ 00436 int refresh; /*!< How often to refresh */ 00437 enum iax_reg_state regstate; 00438 int messages; /*!< Message count, low 8 bits = new, high 8 bits = old */ 00439 int callno; /*!< Associated call number if applicable */ 00440 struct sockaddr_in us; /*!< Who the server thinks we are */ 00441 struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */ 00442 AST_LIST_ENTRY(iax2_registry) entry; 00443 }; 00444 00445 static AST_LIST_HEAD_STATIC(registrations, iax2_registry); 00446 00447 /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */ 00448 #define MIN_RETRY_TIME 100 00449 #define MAX_RETRY_TIME 10000 00450 00451 #define MAX_JITTER_BUFFER 50 00452 #define MIN_JITTER_BUFFER 10 00453 00454 #define DEFAULT_TRUNKDATA 640 * 10 /*!< 40ms, uncompressed linear * 10 channels */ 00455 00456 #define MAX_TIMESTAMP_SKEW 160 /*!< maximum difference between actual and predicted ts for sending */ 00457 00458 /* If consecutive voice frame timestamps jump by more than this many milliseconds, then jitter buffer will resync */ 00459 #define TS_GAP_FOR_JB_RESYNC 5000 00460 00461 static int iaxthreadcount = DEFAULT_THREAD_COUNT; 00462 static int iaxmaxthreadcount = DEFAULT_MAX_THREAD_COUNT; 00463 static int iaxdynamicthreadcount = 0; 00464 00465 struct iax_rr { 00466 int jitter; 00467 int losspct; 00468 int losscnt; 00469 int packets; 00470 int delay; 00471 int dropped; 00472 int ooo; 00473 }; 00474 00475 struct chan_iax2_pvt { 00476 /*! Socket to send/receive on for this call */ 00477 int sockfd; 00478 /*! Last received voice format */ 00479 int voiceformat; 00480 /*! Last received video format */ 00481 int videoformat; 00482 /*! Last sent voice format */ 00483 int svoiceformat; 00484 /*! Last sent video format */ 00485 int svideoformat; 00486 /*! What we are capable of sending */ 00487 int capability; 00488 /*! Last received timestamp */ 00489 unsigned int last; 00490 /*! Last sent timestamp - never send the same timestamp twice in a single call */ 00491 unsigned int lastsent; 00492 /*! Next outgoing timestamp if everything is good */ 00493 unsigned int nextpred; 00494 /*! True if the last voice we transmitted was not silence/CNG */ 00495 unsigned int notsilenttx:1; 00496 /*! Ping time */ 00497 unsigned int pingtime; 00498 /*! Max time for initial response */ 00499 int maxtime; 00500 /*! Peer Address */ 00501 struct sockaddr_in addr; 00502 /*! Actual used codec preferences */ 00503 struct ast_codec_pref prefs; 00504 /*! Requested codec preferences */ 00505 struct ast_codec_pref rprefs; 00506 /*! Our call number */ 00507 unsigned short callno; 00508 /*! Peer callno */ 00509 unsigned short peercallno; 00510 /*! Peer selected format */ 00511 int peerformat; 00512 /*! Peer capability */ 00513 int peercapability; 00514 /*! timeval that we base our transmission on */ 00515 struct timeval offset; 00516 /*! timeval that we base our delivery on */ 00517 struct timeval rxcore; 00518 /*! The jitterbuffer */ 00519 jitterbuf *jb; 00520 /*! active jb read scheduler id */ 00521 int jbid; 00522 /*! LAG */ 00523 int lag; 00524 /*! Error, as discovered by the manager */ 00525 int error; 00526 /*! Owner if we have one */ 00527 struct ast_channel *owner; 00528 /*! What's our state? */ 00529 struct ast_flags state; 00530 /*! Expiry (optional) */ 00531 int expiry; 00532 /*! Next outgoing sequence number */ 00533 unsigned char oseqno; 00534 /*! Next sequence number they have not yet acknowledged */ 00535 unsigned char rseqno; 00536 /*! Next incoming sequence number */ 00537 unsigned char iseqno; 00538 /*! Last incoming sequence number we have acknowledged */ 00539 unsigned char aseqno; 00540 00541 AST_DECLARE_STRING_FIELDS( 00542 /*! Peer name */ 00543 AST_STRING_FIELD(peer); 00544 /*! Default Context */ 00545 AST_STRING_FIELD(context); 00546 /*! Caller ID if available */ 00547 AST_STRING_FIELD(cid_num); 00548 AST_STRING_FIELD(cid_name); 00549 /*! Hidden Caller ID (i.e. ANI) if appropriate */ 00550 AST_STRING_FIELD(ani); 00551 /*! DNID */ 00552 AST_STRING_FIELD(dnid); 00553 /*! RDNIS */ 00554 AST_STRING_FIELD(rdnis); 00555 /*! Requested Extension */ 00556 AST_STRING_FIELD(exten); 00557 /*! Expected Username */ 00558 AST_STRING_FIELD(username); 00559 /*! Expected Secret */ 00560 AST_STRING_FIELD(secret); 00561 /*! MD5 challenge */ 00562 AST_STRING_FIELD(challenge); 00563 /*! Public keys permitted keys for incoming authentication */ 00564 AST_STRING_FIELD(inkeys); 00565 /*! Private key for outgoing authentication */ 00566 AST_STRING_FIELD(outkey); 00567 /*! Preferred language */ 00568 AST_STRING_FIELD(language); 00569 /*! Hostname/peername for naming purposes */ 00570 AST_STRING_FIELD(host); 00571 00572 AST_STRING_FIELD(dproot); 00573 AST_STRING_FIELD(accountcode); 00574 AST_STRING_FIELD(mohinterpret); 00575 AST_STRING_FIELD(mohsuggest); 00576 ); 00577 00578 /*! permitted authentication methods */ 00579 int authmethods; 00580 /*! permitted encryption methods */ 00581 int encmethods; 00582 /*! Encryption AES-128 Key */ 00583 aes_encrypt_ctx ecx; 00584 /*! Decryption AES-128 Key */ 00585 aes_decrypt_ctx dcx; 00586 /*! 32 bytes of semi-random data */ 00587 unsigned char semirand[32]; 00588 /*! Associated registry */ 00589 struct iax2_registry *reg; 00590 /*! Associated peer for poking */ 00591 struct iax2_peer *peerpoke; 00592 /*! IAX_ flags */ 00593 unsigned int flags; 00594 int adsi; 00595 00596 /*! Transferring status */ 00597 enum iax_transfer_state transferring; 00598 /*! Transfer identifier */ 00599 int transferid; 00600 /*! Who we are IAX transferring to */ 00601 struct sockaddr_in transfer; 00602 /*! What's the new call number for the transfer */ 00603 unsigned short transfercallno; 00604 /*! Transfer decrypt AES-128 Key */ 00605 aes_encrypt_ctx tdcx; 00606 00607 /*! Status of knowledge of peer ADSI capability */ 00608 int peeradsicpe; 00609 00610 /*! Who we are bridged to */ 00611 unsigned short bridgecallno; 00612 00613 int pingid; /*!< Transmit PING request */ 00614 int lagid; /*!< Retransmit lag request */ 00615 int autoid; /*!< Auto hangup for Dialplan requestor */ 00616 int authid; /*!< Authentication rejection ID */ 00617 int authfail; /*!< Reason to report failure */ 00618 int initid; /*!< Initial peer auto-congest ID (based on qualified peers) */ 00619 int calling_ton; 00620 int calling_tns; 00621 int calling_pres; 00622 int amaflags; 00623 AST_LIST_HEAD_NOLOCK(, iax2_dpcache) dpentries; 00624 struct ast_variable *vars; 00625 /*! last received remote rr */ 00626 struct iax_rr remote_rr; 00627 /*! Current base time: (just for stats) */ 00628 int min; 00629 /*! Dropped frame count: (just for stats) */ 00630 int frames_dropped; 00631 /*! received frame count: (just for stats) */ 00632 int frames_received; 00633 }; 00634 00635 static AST_LIST_HEAD_STATIC(queue, iax_frame); 00636 00637 static AST_LIST_HEAD_STATIC(users, iax2_user); 00638 00639 static AST_LIST_HEAD_STATIC(peers, iax2_peer); 00640 00641 static AST_LIST_HEAD_STATIC(firmwares, iax_firmware); 00642 00643 enum { 00644 /*! Extension exists */ 00645 CACHE_FLAG_EXISTS = (1 << 0), 00646 /*! Extension is nonexistent */ 00647 CACHE_FLAG_NONEXISTENT = (1 << 1), 00648 /*! Extension can exist */ 00649 CACHE_FLAG_CANEXIST = (1 << 2), 00650 /*! Waiting to hear back response */ 00651 CACHE_FLAG_PENDING = (1 << 3), 00652 /*! Timed out */ 00653 CACHE_FLAG_TIMEOUT = (1 << 4), 00654 /*! Request transmitted */ 00655 CACHE_FLAG_TRANSMITTED = (1 << 5), 00656 /*! Timeout */ 00657 CACHE_FLAG_UNKNOWN = (1 << 6), 00658 /*! Matchmore */ 00659 CACHE_FLAG_MATCHMORE = (1 << 7), 00660 }; 00661 00662 struct iax2_dpcache { 00663 char peercontext[AST_MAX_CONTEXT]; 00664 char exten[AST_MAX_EXTENSION]; 00665 struct timeval orig; 00666 struct timeval expiry; 00667 int flags; 00668 unsigned short callno; 00669 int waiters[256]; 00670 AST_LIST_ENTRY(iax2_dpcache) cache_list; 00671 AST_LIST_ENTRY(iax2_dpcache) peer_list; 00672 }; 00673 00674 static AST_LIST_HEAD_STATIC(dpcache, iax2_dpcache); 00675 00676 static void reg_source_db(struct iax2_peer *p); 00677 static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in *sin); 00678 00679 static void destroy_peer(struct iax2_peer *peer); 00680 static int ast_cli_netstats(struct mansession *s, int fd, int limit_fmt); 00681 00682 enum iax2_thread_iostate { 00683 IAX_IOSTATE_IDLE, 00684 IAX_IOSTATE_READY, 00685 IAX_IOSTATE_PROCESSING, 00686 IAX_IOSTATE_SCHEDREADY, 00687 }; 00688 00689 enum iax2_thread_type { 00690 IAX_THREAD_TYPE_POOL, 00691 IAX_THREAD_TYPE_DYNAMIC, 00692 }; 00693 00694 struct iax2_thread { 00695 AST_LIST_ENTRY(iax2_thread) list; 00696 enum iax2_thread_type type; 00697 enum iax2_thread_iostate iostate; 00698 #ifdef SCHED_MULTITHREADED 00699 void (*schedfunc)(void *); 00700 void *scheddata; 00701 #endif 00702 #ifdef DEBUG_SCHED_MULTITHREAD 00703 char curfunc[80]; 00704 #endif 00705 int actions; 00706 pthread_t threadid; 00707 int threadnum; 00708 struct sockaddr_in iosin; 00709 unsigned char buf[4096]; 00710 int iores; 00711 int iofd; 00712 time_t checktime; 00713 ast_mutex_t lock; 00714 ast_cond_t cond; 00715 }; 00716 00717 /* Thread lists */ 00718 static AST_LIST_HEAD_STATIC(idle_list, iax2_thread); 00719 static AST_LIST_HEAD_STATIC(active_list, iax2_thread); 00720 static AST_LIST_HEAD_STATIC(dynamic_list, iax2_thread); 00721 00722 static void *iax2_process_thread(void *data); 00723 00724 static void signal_condition(ast_mutex_t *lock, ast_cond_t *cond) 00725 { 00726 ast_mutex_lock(lock); 00727 ast_cond_signal(cond); 00728 ast_mutex_unlock(lock); 00729 } 00730 00731 static void iax_debug_output(const char *data) 00732 { 00733 if (iaxdebug) 00734 ast_verbose("%s", data); 00735 } 00736 00737 static void iax_error_output(const char *data) 00738 { 00739 ast_log(LOG_WARNING, "%s", data); 00740 } 00741 00742 static void jb_error_output(const char *fmt, ...) 00743 { 00744 va_list args; 00745 char buf[1024]; 00746 00747 va_start(args, fmt); 00748 vsnprintf(buf, sizeof(buf), fmt, args); 00749 va_end(args); 00750 00751 ast_log(LOG_ERROR, buf); 00752 } 00753 00754 static void jb_warning_output(const char *fmt, ...) 00755 { 00756 va_list args; 00757 char buf[1024]; 00758 00759 va_start(args, fmt); 00760 vsnprintf(buf, sizeof(buf), fmt, args); 00761 va_end(args); 00762 00763 ast_log(LOG_WARNING, buf); 00764 } 00765 00766 static void jb_debug_output(const char *fmt, ...) 00767 { 00768 va_list args; 00769 char buf[1024]; 00770 00771 va_start(args, fmt); 00772 vsnprintf(buf, sizeof(buf), fmt, args); 00773 va_end(args); 00774 00775 ast_verbose(buf); 00776 } 00777 00778 /*! 00779 * \brief an array of iax2 pvt structures 00780 * 00781 * The container for active chan_iax2_pvt structures is implemented as an 00782 * array for extremely quick direct access to the correct pvt structure 00783 * based on the local call number. The local call number is used as the 00784 * index into the array where the associated pvt structure is stored. 00785 */ 00786 static struct chan_iax2_pvt *iaxs[IAX_MAX_CALLS]; 00787 /*! 00788 * \brief chan_iax2_pvt structure locks 00789 * 00790 * These locks are used when accessing a pvt structure in the iaxs array. 00791 * The index used here is the same as used in the iaxs array. It is the 00792 * local call number for the associated pvt struct. 00793 */ 00794 static ast_mutex_t iaxsl[IAX_MAX_CALLS]; 00795 /*! 00796 * \brief The last time a call number was used 00797 * 00798 * It is important to know the last time that a call number was used locally so 00799 * that it is not used again too soon. The reason for this is the same as the 00800 * reason that the TCP protocol state machine requires a "TIME WAIT" state. 00801 * 00802 * For example, say that a call is up. Then, the remote side sends a HANGUP, 00803 * which we respond to with an ACK. However, there is no way to know whether 00804 * the ACK made it there successfully. If it were to get lost, the remote 00805 * side may retransmit the HANGUP. If in the meantime, this call number has 00806 * been reused locally, given the right set of circumstances, this retransmitted 00807 * HANGUP could potentially improperly hang up the new session. So, to avoid 00808 * this potential issue, we must wait a specified timeout period before reusing 00809 * a local call number. 00810 * 00811 * The specified time that we must wait before reusing a local call number is 00812 * defined as MIN_REUSE_TIME, with a default of 60 seconds. 00813 */ 00814 static struct timeval lastused[IAX_MAX_CALLS]; 00815 00816 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); 00817 static int expire_registry(void *data); 00818 static int iax2_answer(struct ast_channel *c); 00819 static int iax2_call(struct ast_channel *c, char *dest, int timeout); 00820 static int iax2_devicestate(void *data); 00821 static int iax2_digit_begin(struct ast_channel *c, char digit); 00822 static int iax2_digit_end(struct ast_channel *c, char digit, unsigned int duration); 00823 static int iax2_do_register(struct iax2_registry *reg); 00824 static int iax2_fixup(struct ast_channel *oldchannel, struct ast_channel *newchan); 00825 static int iax2_hangup(struct ast_channel *c); 00826 static int iax2_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen); 00827 static int iax2_poke_peer(struct iax2_peer *peer, int heldcall); 00828 static int iax2_provision(struct sockaddr_in *end, int sockfd, char *dest, const char *template, int force); 00829 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final); 00830 static int iax2_sendhtml(struct ast_channel *c, int subclass, const char *data, int datalen); 00831 static int iax2_sendimage(struct ast_channel *c, struct ast_frame *img); 00832 static int