Asterisk - The Open Source Telephony Project  21.4.1
chan_iax2.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Implementation of Inter-Asterisk eXchange Version 2
22  * as specified in RFC 5456
23  *
24  * \author Mark Spencer <markster@digium.com>
25  *
26  * \ingroup channel_drivers
27  *
28  * \todo Implement musicclass settings for IAX2 devices
29  */
30 
31 /*! \li \ref chan_iax2.c uses the configuration file \ref iax.conf
32  * \addtogroup configuration_file
33  */
34 
35 /*! \page iax.conf iax.conf
36  * \verbinclude iax.conf.sample
37  */
38 
39 /*!
40  * \todo XXX The IAX2 channel driver needs its native bridge
41  * code converted to the new bridge technology scheme.
42  *
43  * \note The chan_dahdi native bridge code can be used as an
44  * example. It also appears that chan_iax2 also has a native
45  * transfer check like chan_dahdi to eliminate tromboned calls.
46  *
47  * \note The existing native bridge code is marked with the
48  * IAX2_NATIVE_BRIDGING conditional.
49  */
50 
51 /*** MODULEINFO
52  <use type="module">res_crypto</use>
53  <use type="external">crypto</use>
54  <support_level>core</support_level>
55  ***/
56 
57 #include "asterisk.h"
58 
59 #include <sys/mman.h>
60 #include <dirent.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <sys/time.h>
67 #include <signal.h>
68 #include <netdb.h>
69 #include <fcntl.h>
70 #include <sys/stat.h>
71 #include <regex.h>
72 
73 #include "asterisk/paths.h"
74 
75 #include "asterisk/lock.h"
76 #include "asterisk/frame.h"
77 #include "asterisk/channel.h"
78 #include "asterisk/module.h"
79 #include "asterisk/pbx.h"
80 #include "asterisk/sched.h"
81 #include "asterisk/io.h"
82 #include "asterisk/config.h"
83 #include "asterisk/cli.h"
84 #include "asterisk/translate.h"
85 #include "asterisk/md5.h"
86 #include "asterisk/crypto.h"
87 #include "asterisk/acl.h"
88 #include "asterisk/manager.h"
89 #include "asterisk/callerid.h"
90 #include "asterisk/app.h"
91 #include "asterisk/mwi.h"
92 #include "asterisk/astdb.h"
93 #include "asterisk/musiconhold.h"
94 #include "asterisk/features.h"
95 #include "asterisk/utils.h"
96 #include "asterisk/causes.h"
97 #include "asterisk/localtime.h"
98 #include "asterisk/dnsmgr.h"
99 #include "asterisk/devicestate.h"
100 #include "asterisk/stringfields.h"
101 #include "asterisk/linkedlists.h"
102 #include "asterisk/astobj2.h"
103 #include "asterisk/timing.h"
104 #include "asterisk/taskprocessor.h"
105 #include "asterisk/test.h"
108 #include "asterisk/bridge.h"
109 #include "asterisk/stasis.h"
110 #include "asterisk/stasis_system.h"
111 #include "asterisk/stasis_channels.h"
112 #include "asterisk/format_cache.h"
114 #include "asterisk/format_cap.h"
115 
116 #include "iax2/include/iax2.h"
117 #include "iax2/include/firmware.h"
118 #include "iax2/include/parser.h"
119 #include "iax2/include/provision.h"
120 #include "iax2/include/codec_pref.h"
122 #include "iax2/include/netsock.h"
123 
124 #include "jitterbuf.h"
125 
126 /*** DOCUMENTATION
127  <application name="IAX2Provision" language="en_US">
128  <synopsis>
129  Provision a calling IAXy with a given template.
130  </synopsis>
131  <syntax>
132  <parameter name="template">
133  <para>If not specified, defaults to <literal>default</literal>.</para>
134  </parameter>
135  </syntax>
136  <description>
137  <para>Provisions the calling IAXy (assuming the calling entity is in fact an IAXy) with the
138  given <replaceable>template</replaceable>. Returns <literal>-1</literal> on error
139  or <literal>0</literal> on success.</para>
140  </description>
141  </application>
142  <function name="IAXPEER" language="en_US">
143  <synopsis>
144  Gets IAX peer information.
145  </synopsis>
146  <syntax>
147  <parameter name="peername" required="true">
148  <enumlist>
149  <enum name="CURRENTCHANNEL">
150  <para>If <replaceable>peername</replaceable> is specified to this value, return the IP address of the
151  endpoint of the current channel</para>
152  </enum>
153  </enumlist>
154  </parameter>
155  <parameter name="item">
156  <para>If <replaceable>peername</replaceable> is specified, valid items are:</para>
157  <enumlist>
158  <enum name="ip">
159  <para>(default) The IP address.</para>
160  </enum>
161  <enum name="status">
162  <para>The peer's status (if <literal>qualify=yes</literal>)</para>
163  </enum>
164  <enum name="mailbox">
165  <para>The configured mailbox.</para>
166  </enum>
167  <enum name="context">
168  <para>The configured context.</para>
169  </enum>
170  <enum name="expire">
171  <para>The epoch time of the next expire.</para>
172  </enum>
173  <enum name="dynamic">
174  <para>Is it dynamic? (yes/no).</para>
175  </enum>
176  <enum name="callerid_name">
177  <para>The configured Caller ID name.</para>
178  </enum>
179  <enum name="callerid_num">
180  <para>The configured Caller ID number.</para>
181  </enum>
182  <enum name="codecs">
183  <para>The configured codecs.</para>
184  </enum>
185  <enum name="codec[x]">
186  <para>Preferred codec index number <replaceable>x</replaceable> (beginning
187  with <literal>0</literal>)</para>
188  </enum>
189  </enumlist>
190  </parameter>
191  </syntax>
192  <description>
193  <para>Gets information associated with the specified IAX2 peer.</para>
194  </description>
195  </function>
196  <function name="IAXVAR" language="en_US">
197  <synopsis>
198  Sets or retrieves a remote variable.
199  </synopsis>
200  <syntax>
201  <parameter name="varname" required="true" />
202  </syntax>
203  <description>
204  <para>Gets or sets a variable that is sent to a remote IAX2 peer during call setup.</para>
205  </description>
206  </function>
207  <info name="CHANNEL" language="en_US" tech="IAX">
208  <enumlist>
209  <enum name="osptoken">
210  <para>R/O Get the peer's osptoken.</para>
211  </enum>
212  <enum name="peerip">
213  <para>R/O Get the peer's ip address.</para>
214  </enum>
215  <enum name="peername">
216  <para>R/O Get the peer's username.</para>
217  </enum>
218  <enum name="secure_signaling">
219  <para>R/O Get the if the IAX channel is secured.</para>
220  </enum>
221  <enum name="secure_media">
222  <para>R/O Get the if the IAX channel is secured.</para>
223  </enum>
224  </enumlist>
225  </info>
226  <info name="Dial_Resource" language="en_US" tech="IAX2">
227  <para>The general syntax is:</para>
228  <para><literal>Dial(IAX2/[username[:password]@]peer[:port][/exten[@context]][/options]</literal></para>
229  <para>IAX2 optionally allows modifiers to be specified after the extension.</para>
230  <enumlist>
231  <enum name="a">
232  <para>Request auto answer (supporting equipment/configuration required)</para>
233  </enum>
234  </enumlist>
235  </info>
236  <manager name="IAXpeers" language="en_US">
237  <synopsis>
238  List IAX peers.
239  </synopsis>
240  <syntax>
241  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
242  </syntax>
243  <description>
244  </description>
245  </manager>
246  <manager name="IAXpeerlist" language="en_US">
247  <synopsis>
248  List IAX Peers.
249  </synopsis>
250  <syntax>
251  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
252  </syntax>
253  <description>
254  <para>List all the IAX peers.</para>
255  </description>
256  </manager>
257  <manager name="IAXnetstats" language="en_US">
258  <synopsis>
259  Show IAX Netstats.
260  </synopsis>
261  <syntax />
262  <description>
263  <para>Show IAX channels network statistics.</para>
264  </description>
265  </manager>
266  <manager name="IAXregistry" language="en_US">
267  <synopsis>
268  Show IAX registrations.
269  </synopsis>
270  <syntax>
271  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
272  </syntax>
273  <description>
274  <para>Show IAX registrations.</para>
275  </description>
276  </manager>
277  ***/
278 
279 /* Define SCHED_MULTITHREADED to run the scheduler in a special
280  multithreaded mode. */
281 #define SCHED_MULTITHREADED
282 
283 /* Define DEBUG_SCHED_MULTITHREADED to keep track of where each
284  thread is actually doing. */
285 #define DEBUG_SCHED_MULTITHREAD
286 
287 
288 #ifdef SO_NO_CHECK
289 static int nochecksums = 0;
290 #endif
291 
292 #define PTR_TO_CALLNO(a) ((unsigned short)(unsigned long)(a))
293 #define CALLNO_TO_PTR(a) ((void *)(unsigned long)(a))
294 
295 #define DEFAULT_THREAD_COUNT 10
296 #define DEFAULT_MAX_THREAD_COUNT 100
297 #define DEFAULT_RETRY_TIME 1000
298 #define MEMORY_SIZE 100
299 #define DEFAULT_DROP 3
300 
301 #define DEBUG_SUPPORT
302 
303 #define MIN_REUSE_TIME 60 /* Don't reuse a call number within 60 seconds */
304 
305 /* Sample over last 100 units to determine historic jitter */
306 #define GAMMA (0.01)
307 
308 static struct iax2_codec_pref prefs_global;
309 
310 static const char tdesc[] = "Inter Asterisk eXchange Driver (Ver 2)";
311 
312 
313 /*! \brief Maximum transmission unit for the UDP packet in the trunk not to be
314  fragmented. This is based on 1516 - ethernet - ip - udp - iax minus one g711 frame = 1240 */
315 #define MAX_TRUNK_MTU 1240
316 
317 static int global_max_trunk_mtu; /*!< Maximum MTU, 0 if not used */
318 static int trunk_timed, trunk_untimed, trunk_maxmtu, trunk_nmaxmtu ; /*!< Trunk MTU statistics */
319 
320 #define DEFAULT_CONTEXT "default"
321 
322 static char default_parkinglot[AST_MAX_CONTEXT];
323 
324 static char language[MAX_LANGUAGE] = "";
325 static char regcontext[AST_MAX_CONTEXT] = "";
326 
327 static struct stasis_subscription *network_change_sub; /*!< subscription id for network change events */
328 static struct stasis_subscription *acl_change_sub; /*!< subscription id for ACL change events */
329 static int network_change_sched_id = -1;
330 
331 static int maxauthreq = 3;
332 static int max_retries = 4;
333 static int ping_time = 21;
334 static int lagrq_time = 10;
335 static int maxjitterbuffer=1000;
336 static int resyncthreshold=1000;
337 static int maxjitterinterps=10;
338 static int jittertargetextra = 40; /* number of milliseconds the new jitter buffer adds on to its size */
339 
340 #define MAX_TRUNKDATA 640 * 200 /*!< 40ms, uncompressed linear * 200 channels */
341 
342 static int trunkfreq = 20;
343 static int trunkmaxsize = MAX_TRUNKDATA;
344 
345 static int authdebug = 0;
346 static int autokill = 0;
347 static int iaxcompat = 0;
348 static int last_authmethod = 0;
349 
350 static int iaxdefaultdpcache=10 * 60; /* Cache dialplan entries for 10 minutes by default */
351 
352 static int iaxdefaulttimeout = 5; /* Default to wait no more than 5 seconds for a reply to come back */
353 
354 static struct {
355  unsigned int tos;
356  unsigned int cos;
357 } qos = { 0, 0 };
358 
359 static int min_reg_expire;
360 static int max_reg_expire;
361 
362 static int srvlookup = 0;
363 
364 static struct ast_timer *timer; /* Timer for trunking */
365 
366 static struct ast_netsock_list *netsock;
367 static struct ast_netsock_list *outsock; /*!< used if sourceaddress specified and bindaddr == INADDR_ANY */
368 static int defaultsockfd = -1;
369 
370 static int (*iax2_regfunk)(const char *username, int onoff) = NULL;
371 
372 /* Ethernet, etc */
373 #define IAX_CAPABILITY_FULLBANDWIDTH 0xFFFF
374 /* T1, maybe ISDN */
375 #define IAX_CAPABILITY_MEDBANDWIDTH (IAX_CAPABILITY_FULLBANDWIDTH & \
376  ~AST_FORMAT_SLIN & \
377  ~AST_FORMAT_SLIN16 & \
378  ~AST_FORMAT_SIREN7 & \
379  ~AST_FORMAT_SIREN14 & \
380  ~AST_FORMAT_G719 & \
381  ~AST_FORMAT_ULAW & \
382  ~AST_FORMAT_ALAW & \
383  ~AST_FORMAT_G722)
384 /* A modem */
385 #define IAX_CAPABILITY_LOWBANDWIDTH (IAX_CAPABILITY_MEDBANDWIDTH & \
386  ~AST_FORMAT_G726 & \
387  ~AST_FORMAT_G726_AAL2 & \
388  ~AST_FORMAT_ADPCM)
389 
390 #define IAX_CAPABILITY_LOWFREE (IAX_CAPABILITY_LOWBANDWIDTH & \
391  ~AST_FORMAT_G723)
392 
393 
394 #define DEFAULT_MAXMS 2000 /* Must be faster than 2 seconds by default */
395 #define DEFAULT_FREQ_OK 60 * 1000 /* How often to check for the host to be up */
396 #define DEFAULT_FREQ_NOTOK 10 * 1000 /* How often to check, if the host is down... */
397 
398 /*! \brief Name of effective auth method */
399 static const char *auth_method_labels[] = {
400  [0] = "none",
401  [IAX_AUTH_PLAINTEXT] = "plaintext",
402  [IAX_AUTH_MD5] = "MD5",
403  [IAX_AUTH_RSA] = "RSA",
404 };
405 
406 /* Max length is length of |RSA|MD5|plaintext (18 + 1 for NUL = 19) */
407 #define AUTH_METHOD_NAMES_BUFSIZE 19
408 
409 /*!
410  * \brief Get names of all auth methods
411  * \param Bit field of auth methods
412  * \param[out] buf Buffer into which to write the names. Must be of size AUTH_METHOD_NAMES_BUFSIZE.
413  * \return Auth methods name
414  */
415 static char *auth_method_names(int authmethods, char *restrict buf)
416 {
417  char *pos = buf;
418 
419  *pos = '\0';
420 
421  if (authmethods & IAX_AUTH_RSA) {
422  pos += sprintf(pos, "|RSA");
423  }
424  if (authmethods & IAX_AUTH_MD5) {
425  pos += sprintf(pos, "|MD5");
426  }
427  if (authmethods & IAX_AUTH_PLAINTEXT) {
428  pos += sprintf(pos, "|plaintext");
429  }
430 
431  if (pos == buf) { /* No auth methods */
432  strcpy(buf, "none");
433  return buf;
434  }
435 
436  return buf + 1; /* Skip leading | */
437 }
438 
439 /* if a pvt has encryption setup done and is running on the call */
440 #define IAX_CALLENCRYPTED(pvt) \
441  (ast_test_flag64(pvt, IAX_ENCRYPTED) && ast_test_flag64(pvt, IAX_KEYPOPULATED))
442 
443 #define IAX_DEBUGDIGEST(msg, key) do { \
444  int idx; \
445  char digest[33] = ""; \
446  \
447  if (!iaxdebug) \
448  break; \
449  \
450  for (idx = 0; idx < 16; idx++) \
451  sprintf(digest + (idx << 1), "%02hhx", (unsigned char) key[idx]); \
452  \
453  ast_log(LOG_NOTICE, msg " IAX_COMMAND_RTKEY to rotate key to '%s'\n", digest); \
454  } while(0)
455 
456 static struct io_context *io;
457 static struct ast_sched_context *sched;
458 
459 static iax2_format iax2_capability = IAX_CAPABILITY_FULLBANDWIDTH;
460 
461 static int iaxdebug = 0;
462 
463 static int iaxtrunkdebug = 0;
464 
465 static int test_losspct = 0;
466 #ifdef IAXTESTS
467 static int test_late = 0;
468 static int test_resync = 0;
469 static int test_jit = 0;
470 static int test_jitpct = 0;
471 #endif /* IAXTESTS */
472 
473 static char accountcode[AST_MAX_ACCOUNT_CODE];
474 static char mohinterpret[MAX_MUSICCLASS];
475 static char mohsuggest[MAX_MUSICCLASS];
476 static int amaflags = 0;
477 static int adsi = 0;
478 static int delayreject = 0;
479 static int iax2_encryption = 0;
480 static int iax2_authmethods = 0;
481 
482 static struct ast_flags64 globalflags = { 0 };
483 
484 static pthread_t netthreadid = AST_PTHREADT_NULL;
485 
486 enum iax2_state {
487  IAX_STATE_STARTED = (1 << 0),
488  IAX_STATE_AUTHENTICATED = (1 << 1),
489  IAX_STATE_TBD = (1 << 2),
490 };
491 
492 struct iax2_context {
493  char context[AST_MAX_CONTEXT];
494  struct iax2_context *next;
495 };
496 
497 
498 #define IAX_HASCALLERID (uint64_t)(1LLU << 0) /*!< CallerID has been specified */
499 #define IAX_DELME (uint64_t)(1LLU << 1) /*!< Needs to be deleted */
500 #define IAX_TEMPONLY (uint64_t)(1LLU << 2) /*!< Temporary (realtime) */
501 #define IAX_TRUNK (uint64_t)(1LLU << 3) /*!< Treat as a trunk */
502 #define IAX_NOTRANSFER (uint64_t)(1LLU << 4) /*!< Don't native bridge */
503 #define IAX_USEJITTERBUF (uint64_t)(1LLU << 5) /*!< Use jitter buffer */
504 #define IAX_DYNAMIC (uint64_t)(1LLU << 6) /*!< dynamic peer */
505 #define IAX_SENDANI (uint64_t)(1LLU << 7) /*!< Send ANI along with CallerID */
506 #define IAX_RTSAVE_SYSNAME (uint64_t)(1LLU << 8) /*!< Save Systname on Realtime Updates */
507 #define IAX_ALREADYGONE (uint64_t)(1LLU << 9) /*!< Already disconnected */
508 #define IAX_PROVISION (uint64_t)(1LLU << 10) /*!< This is a provisioning request */
509 #define IAX_QUELCH (uint64_t)(1LLU << 11) /*!< Whether or not we quelch audio */
510 #define IAX_ENCRYPTED (uint64_t)(1LLU << 12) /*!< Whether we should assume encrypted tx/rx */
511 #define IAX_KEYPOPULATED (uint64_t)(1LLU << 13) /*!< Whether we have a key populated */
512 #define IAX_CODEC_USER_FIRST (uint64_t)(1LLU << 14) /*!< are we willing to let the other guy choose the codec? */
513 #define IAX_CODEC_NOPREFS (uint64_t)(1LLU << 15) /*!< Force old behaviour by turning off prefs */
514 #define IAX_CODEC_NOCAP (uint64_t)(1LLU << 16) /*!< only consider requested format and ignore capabilities*/
515 #define IAX_RTCACHEFRIENDS (uint64_t)(1LLU << 17) /*!< let realtime stay till your reload */
516 #define IAX_RTUPDATE (uint64_t)(1LLU << 18) /*!< Send a realtime update */
517 #define IAX_RTAUTOCLEAR (uint64_t)(1LLU << 19) /*!< erase me on expire */
518 #define IAX_RTIGNOREREGEXPIRE (uint64_t)(1LLU << 21) /*!< When using realtime, ignore registration expiration */
519 #define IAX_TRUNKTIMESTAMPS (uint64_t)(1LLU << 22) /*!< Send trunk timestamps */
520 #define IAX_TRANSFERMEDIA (uint64_t)(1LLU << 23) /*!< When doing IAX2 transfers, transfer media only */
521 #define IAX_MAXAUTHREQ (uint64_t)(1LLU << 24) /*!< Maximum outstanding AUTHREQ restriction is in place */
522 #define IAX_DELAYPBXSTART (uint64_t)(1LLU << 25) /*!< Don't start a PBX on the channel until the peer sends us a response, so that we've achieved a three-way handshake with them before sending voice or anything else */
523 #define IAX_ALLOWFWDOWNLOAD (uint64_t)(1LLU << 26) /*!< Allow the FWDOWNL command? */
524 #define IAX_IMMEDIATE (uint64_t)(1LLU << 27) /*!< Allow immediate off-hook to extension s */
525 #define IAX_SENDCONNECTEDLINE (uint64_t)(1LLU << 28) /*!< Allow sending of connected line updates */
526 #define IAX_RECVCONNECTEDLINE (uint64_t)(1LLU << 29) /*!< Allow receiving of connected line updates */
527 #define IAX_FORCE_ENCRYPT (uint64_t)(1LLU << 30) /*!< Forces call encryption, if encryption not possible hangup */
528 #define IAX_SHRINKCALLERID (uint64_t)(1LLU << 31) /*!< Turn on and off caller id shrinking */
529 static int global_rtautoclear = 120;
530 
531 static int reload_config(int forced_reload);
532 
533 /*!
534  * \brief Call token validation settings.
535  */
537  /*! \brief Default calltoken required unless the ip is in the ignorelist */
539  /*! \brief Require call token validation. */
541  /*! \brief Require call token validation after a successful registration
542  * using call token validation occurs. */
544  /*! \brief Do not require call token validation. */
546 };
547 
548 struct iax2_user {
550  AST_STRING_FIELD(name);
551  AST_STRING_FIELD(secret);
552  AST_STRING_FIELD(dbsecret);
553  AST_STRING_FIELD(accountcode);
554  AST_STRING_FIELD(mohinterpret);
555  AST_STRING_FIELD(mohsuggest);
556  AST_STRING_FIELD(inkeys); /*!< Key(s) this user can use to authenticate to us */
557  AST_STRING_FIELD(language);
558  AST_STRING_FIELD(cid_num);
559  AST_STRING_FIELD(cid_name);
560  AST_STRING_FIELD(parkinglot); /*!< Default parkinglot for device */
561  );
562 
563  int authmethods;
564  int encmethods;
565  int amaflags;
566  int adsi;
567  uint64_t flags;
568  iax2_format capability;
569  int maxauthreq; /*!< Maximum allowed outstanding AUTHREQs */
570  int curauthreq; /*!< Current number of outstanding AUTHREQs */
571  struct iax2_codec_pref prefs;
572  struct ast_acl_list *acl;
573  struct iax2_context *contexts;
574  struct ast_variable *vars;
575  enum calltoken_peer_enum calltoken_required; /*!< Is calltoken validation required or not, can be YES, NO, or AUTO */
576 };
577 
578 struct iax2_peer {
580  AST_STRING_FIELD(name);
581  AST_STRING_FIELD(username);
582  AST_STRING_FIELD(description); /*!< Description of the peer */
583  AST_STRING_FIELD(secret);
584  AST_STRING_FIELD(dbsecret);
585  AST_STRING_FIELD(outkey); /*!< What key we use to talk to this peer */
586 
587  AST_STRING_FIELD(regexten); /*!< Extension to register (if regcontext is used) */
588  AST_STRING_FIELD(context); /*!< For transfers only */
589  AST_STRING_FIELD(peercontext); /*!< Context to pass to peer */
590  AST_STRING_FIELD(mailbox); /*!< Mailbox */
591  AST_STRING_FIELD(mohinterpret);
592  AST_STRING_FIELD(mohsuggest);
593  AST_STRING_FIELD(inkeys); /*!< Key(s) this peer can use to authenticate to us */
594  /* Suggested caller id if registering */
595  AST_STRING_FIELD(cid_num); /*!< Default context (for transfer really) */
596  AST_STRING_FIELD(cid_name); /*!< Default context (for transfer really) */
597  AST_STRING_FIELD(zonetag); /*!< Time Zone */
598  AST_STRING_FIELD(parkinglot); /*!< Default parkinglot for device */
599  );
600  struct iax2_codec_pref prefs;
601  struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */
602  struct ast_sockaddr addr;
603  int formats;
604  int sockfd; /*!< Socket to use for transmission */
605  struct ast_sockaddr mask;
606  int adsi;
607  uint64_t flags;
608 
609  /* Dynamic Registration fields */
610  struct ast_sockaddr defaddr; /*!< Default address if there is one */
611  int authmethods; /*!< Authentication methods (IAX_AUTH_*) */
612  int encmethods; /*!< Encryption methods (IAX_ENCRYPT_*) */
613 
614  int expire; /*!< Schedule entry for expiry */
615  int expiry; /*!< How soon to expire */
616  iax2_format capability; /*!< Capability */
617 
618  /* Qualification */
619  int callno; /*!< Call number of POKE request */
620  int pokeexpire; /*!< Scheduled qualification-related task (ie iax2_poke_peer_s or iax2_poke_noanswer) */
621  int lastms; /*!< How long last response took (in ms), or -1 for no response */
622  int maxms; /*!< Max ms we will accept for the host to be up, 0 to not monitor */
623 
624  int pokefreqok; /*!< How often to check if the host is up */
625  int pokefreqnotok; /*!< How often to check when the host has been determined to be down */
626  int historicms; /*!< How long recent average responses took */
627  int smoothing; /*!< Sample over how many units to determine historic ms */
628  uint16_t maxcallno; /*!< Max call number limit for this peer. Set on registration */
629 
630  struct ast_mwi_subscriber *mwi_event_sub; /*!< This subscription lets pollmailboxes know which mailboxes need to be polled */
631 
632  struct ast_acl_list *acl;
633  enum calltoken_peer_enum calltoken_required; /*!< Is calltoken validation required or not, can be YES, NO, or AUTO */
634 
635  struct ast_endpoint *endpoint; /*!< Endpoint structure for this peer */
636 };
637 
638 #define IAX2_TRUNK_PREFACE (sizeof(struct iax_frame) + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr))
639 
641  ast_mutex_t lock;
642  int sockfd;
643  struct ast_sockaddr addr;
644  struct timeval txtrunktime; /*!< Transmit trunktime */
645  struct timeval rxtrunktime; /*!< Receive trunktime */
646  struct timeval lasttxtime; /*!< Last transmitted trunktime */
647  struct timeval trunkact; /*!< Last trunk activity */
648  unsigned int lastsent; /*!< Last sent time */
649  /* Trunk data and length */
650  unsigned char *trunkdata;
651  unsigned int trunkdatalen;
652  unsigned int trunkdataalloc;
653  int trunkmaxmtu;
654  int trunkerror;
655  int calls;
657 };
658 
660 
661 enum iax_reg_state {
662  REG_STATE_UNREGISTERED = 0,
663  REG_STATE_REGSENT,
664  REG_STATE_AUTHSENT,
665  REG_STATE_REGISTERED,
666  REG_STATE_REJECTED,
667  REG_STATE_TIMEOUT,
668  REG_STATE_NOAUTH
669 };
670 
671 enum iax_transfer_state {
672  TRANSFER_NONE = 0,
673  TRANSFER_BEGIN,
674  TRANSFER_READY,
675  TRANSFER_RELEASED,
676  TRANSFER_PASSTHROUGH,
677  TRANSFER_MBEGIN,
678  TRANSFER_MREADY,
679  TRANSFER_MRELEASED,
680  TRANSFER_MPASSTHROUGH,
681  TRANSFER_MEDIA,
682  TRANSFER_MEDIAPASS
683 };
684 
686  struct ast_sockaddr addr; /*!< Who we connect to for registration purposes */
687  char username[80];
688  char secret[80]; /*!< Password or key name in []'s */
689  int expire; /*!< Sched ID of expiration */
690  int refresh; /*!< How often to refresh */
691  enum iax_reg_state regstate;
692  int messages; /*!< Message count, low 8 bits = new, high 8 bits = old */
693  int callno; /*!< Associated call number if applicable */
694  struct ast_sockaddr us; /*!< Who the server thinks we are */
695  struct ast_dnsmgr_entry *dnsmgr; /*!< DNS refresh manager */
697  int port;
698  char hostname[];
699 };
700 
702 
703 /* Don't retry more frequently than every 10 ms, or less frequently than every 5 seconds */
704 #define MIN_RETRY_TIME 100
705 #define MAX_RETRY_TIME 10000
706 
707 #define MAX_JITTER_BUFFER 50
708 #define MIN_JITTER_BUFFER 10
709 
710 #define DEFAULT_TRUNKDATA 640 * 10 /*!< 40ms, uncompressed linear * 10 channels */
711 
712 #define MAX_TIMESTAMP_SKEW 160 /*!< maximum difference between actual and predicted ts for sending */
713 
714 /* If consecutive voice frame timestamps jump by more than this many milliseconds, then jitter buffer will resync */
715 #define TS_GAP_FOR_JB_RESYNC 5000
716 
717 /* used for first_iax_message and last_iax_message. If this bit is set it was TX, else RX */
718 #define MARK_IAX_SUBCLASS_TX 0x8000
719 
720 static int iaxthreadcount = DEFAULT_THREAD_COUNT;
721 static int iaxmaxthreadcount = DEFAULT_MAX_THREAD_COUNT;
722 static int iaxdynamicthreadcount = 0;
723 static int iaxdynamicthreadnum = 0;
724 static int iaxactivethreadcount = 0;
725 
726 struct iax_rr {
727  int jitter;
728  int losspct;
729  int losscnt;
730  int packets;
731  int delay;
732  int dropped;
733  int ooo;
734 };
735 
736 struct iax2_pvt_ref;
737 
738 /* We use the high order bit as the validated flag, and the lower 15 as the
739  * actual call number */
740 typedef uint16_t callno_entry;
741 
743  /*! Socket to send/receive on for this call */
744  int sockfd;
745  /*! ast_callid bound to dialog */
746  ast_callid callid;
747  /*! Last received voice format */
749  /*! Last received video format */
751  /*! Last sent voice format */
753  /*! Last sent video format */
755  /*! What we are capable of sending */
757  /*! Last received timestamp */
758  unsigned int last;
759  /*! Last sent timestamp - never send the same timestamp twice in a single call */
760  unsigned int lastsent;
761  /*! Timestamp of the last video frame sent */
762  unsigned int lastvsent;
763  /*! Next outgoing timestamp if everything is good */
764  unsigned int nextpred;
765  /*! iax frame subclass that began iax2_pvt entry. 0x8000 bit is set on TX */
767  /*! Last iax frame subclass sent or received for a iax2_pvt. 0x8000 bit is set on TX */
769  /*! True if the last voice we transmitted was not silence/CNG */
770  unsigned int notsilenttx:1;
771  /*! Ping time */
772  unsigned int pingtime;
773  /*! Max time for initial response */
774  int maxtime;
775  /*! Peer Address */
776  struct ast_sockaddr addr;
777  /*! Actual used codec preferences */
778  struct iax2_codec_pref prefs;
779  /*! Requested codec preferences */
780  struct iax2_codec_pref rprefs;
781  /*! Our call number */
782  unsigned short callno;
783  /*! Our callno_entry entry */
784  callno_entry callno_entry;
785  /*! Peer callno */
786  unsigned short peercallno;
787  /*! Negotiated format, this is only used to remember what format was
788  chosen for an unauthenticated call so that the channel can get
789  created later using the right format */
791  /*! Peer selected format */
793  /*! Peer capability */
795  /*! timeval that we base our transmission on */
796  struct timeval offset;
797  /*! timeval that we base our delivery on */
798  struct timeval rxcore;
799  /*! The jitterbuffer */
801  /*! active jb read scheduler id */
802  int jbid;
803  /*! LAG */
804  int lag;
805  /*! Error, as discovered by the manager */
806  int error;
807  /*! Owner if we have one */
809  /*! What's our state? */
810  struct ast_flags state;
811  /*! Expiry (optional) */
812  int expiry;
813  /*! Next outgoing sequence number */
814  unsigned char oseqno;
815  /*! Next sequence number they have not yet acknowledged */
816  unsigned char rseqno;
817  /*! Next incoming sequence number */
818  unsigned char iseqno;
819  /*! Last incoming sequence number we have acknowledged */
820  unsigned char aseqno;
821 
823  /*! Peer name */
824  AST_STRING_FIELD(peer);
825  /*! Default Context */
826  AST_STRING_FIELD(context);
827  /*! Caller ID if available */
828  AST_STRING_FIELD(cid_num);
829  AST_STRING_FIELD(cid_name);
830  /*! Hidden Caller ID (i.e. ANI) if appropriate */
831  AST_STRING_FIELD(ani);
832  /*! DNID */
833  AST_STRING_FIELD(dnid);
834  /*! RDNIS */
835  AST_STRING_FIELD(rdnis);
836  /*! Requested Extension */
837  AST_STRING_FIELD(exten);
838  /*! Expected Username */
839  AST_STRING_FIELD(username);
840  /*! Expected Secret */
841  AST_STRING_FIELD(secret);
842  /*! MD5 challenge */
843  AST_STRING_FIELD(challenge);
844  /*! Public keys permitted keys for incoming authentication */
845  AST_STRING_FIELD(inkeys);
846  /*! Private key for outgoing authentication */
847  AST_STRING_FIELD(outkey);
848  /*! Preferred language */
849  AST_STRING_FIELD(language);
850  /*! Hostname/peername for naming purposes */
851  AST_STRING_FIELD(host);
852 
853  AST_STRING_FIELD(dproot);
854  AST_STRING_FIELD(accountcode);
855  AST_STRING_FIELD(mohinterpret);
856  AST_STRING_FIELD(mohsuggest);
857  /*! received OSP token */
858  AST_STRING_FIELD(osptoken);
859  /*! Default parkinglot */
860  AST_STRING_FIELD(parkinglot);
861  );
862  /*! AUTHREJ all AUTHREP frames */
863  int authrej;
864  /*! permitted authentication methods */
866  /*! effective authentication method */
868  /*! permitted encryption methods */
870  /*! Encryption AES-128 Key */
872  /*! Decryption AES-128 Key corresponding to ecx */
874  /*! Decryption AES-128 Key used to decrypt peer frames */
876  /*! scheduler id associated with iax_key_rotate
877  * for encrypted calls*/
879  /*! 32 bytes of semi-random data */
880  unsigned char semirand[32];
881  /*! Associated registry */
882  struct iax2_registry *reg;
883  /*! Associated peer for poking */
885  /*! IAX_ flags */
886  uint64_t flags;
887  int adsi;
888 
889  /*! Transferring status */
890  enum iax_transfer_state transferring;
891  /*! Transfer identifier */
893  /*! Who we are IAX transferring to */
894  struct ast_sockaddr transfer;
895  /*! What's the new call number for the transfer */
896  unsigned short transfercallno;
897  /*! Transfer encrypt AES-128 Key */
899 
900  /*! Status of knowledge of peer ADSI capability */
902 
903  /*! Callno of native bridge peer. (Valid if nonzero) */
904  unsigned short bridgecallno;
905 
906  int pingid; /*!< Transmit PING request */
907  int lagid; /*!< Retransmit lag request */
908  int autoid; /*!< Auto hangup for Dialplan requestor */
909  int authid; /*!< Authentication rejection ID */
910  int authfail; /*!< Reason to report failure */
911  int initid; /*!< Initial peer auto-congest ID (based on qualified peers) */
912  int calling_ton;
913  int calling_tns;
914  int calling_pres;
915  int calling_ani2;
916  int amaflags;
917  AST_LIST_HEAD_NOLOCK(, iax2_dpcache) dpentries;
918  /*! variables inherited from the user definition */
919  struct ast_variable *vars;
920  /*! variables transmitted in a NEW packet */
921  struct ast_variable *iaxvars;
922  /*! last received remote rr */
923  struct iax_rr remote_rr;
924  /*! Current base time: (just for stats) */
925  int min;
926  /*! Dropped frame count: (just for stats) */
927  int frames_dropped;
928  /*! received frame count: (just for stats) */
929  int frames_received;
930  /*! Destroying this call initiated. */
931  int destroy_initiated;
932  /*! num bytes used for calltoken ie, even an empty ie should contain 2 */
933  unsigned char calltoken_ie_len;
934  /*! hold all signaling frames from the pbx thread until we have a destination callno */
935  char hold_signaling;
936  /*! frame queue for signaling frames from pbx thread waiting for destination callno */
938 };
939 
941  struct ast_frame f;
942  AST_LIST_ENTRY(signaling_queue_entry) next;
943 };
944 
945 enum callno_type {
946  CALLNO_TYPE_NORMAL,
947  CALLNO_TYPE_TRUNK,
948 };
949 
950 #define PTR_TO_CALLNO_ENTRY(a) ((uint16_t)(unsigned long)(a))
951 #define CALLNO_ENTRY_TO_PTR(a) ((void *)(unsigned long)(a))
952 
953 #define CALLNO_ENTRY_SET_VALIDATED(a) ((a) |= 0x8000)
954 #define CALLNO_ENTRY_IS_VALIDATED(a) ((a) & 0x8000)
955 #define CALLNO_ENTRY_GET_CALLNO(a) ((a) & 0x7FFF)
956 
958  size_t capacity;
959  size_t available;
960  callno_entry numbers[IAX_MAX_CALLS / 2 + 1];
961 };
962 
963 AST_MUTEX_DEFINE_STATIC(callno_pool_lock);
964 
965 /*! table of available call numbers */
967 
968 /*! table of available trunk call numbers */
970 
971 /*!
972  * \brief a list of frames that may need to be retransmitted
973  *
974  * \note The contents of this list do not need to be explicitly destroyed
975  * on module unload. This is because all active calls are destroyed, and
976  * all frames in this queue will get destroyed as a part of that process.
977  *
978  * \note Contents protected by the iaxsl[] locks
979  */
980 static AST_LIST_HEAD_NOLOCK(, iax_frame) frame_queue[IAX_MAX_CALLS];
981 
982 static struct ast_taskprocessor *transmit_processor;
983 
984 static int randomcalltokendata;
985 
986 static time_t max_calltoken_delay = 10;
987 
988 /*!
989  * This module will get much higher performance when doing a lot of
990  * user and peer lookups if the number of buckets is increased from 1.
991  * However, to maintain old behavior for Asterisk 1.4, these are set to
992  * 1 by default. When using multiple buckets, search order through these
993  * containers is considered random, so you will not be able to depend on
994  * the order the entires are specified in iax.conf for matching order. */
995 #ifdef LOW_MEMORY
996 #define MAX_PEER_BUCKETS 17
997 #else
998 #define MAX_PEER_BUCKETS 563
999 #endif
1000 static struct ao2_container *peers;
1001 
1002 #define MAX_USER_BUCKETS MAX_PEER_BUCKETS
1003 static struct ao2_container *users;
1004 
1005 /*! Table containing peercnt objects for every ip address consuming a callno */
1006 static struct ao2_container *peercnts;
1007 
1008 /*! Table containing custom callno limit rules for a range of ip addresses. */
1010 
1011 /*! Table containing ip addresses not requiring calltoken validation */
1013 
1014 static uint16_t DEFAULT_MAXCALLNO_LIMIT = 2048;
1015 
1016 static uint16_t DEFAULT_MAXCALLNO_LIMIT_NONVAL = 8192;
1017 
1018 static uint16_t global_maxcallno;
1019 
1020 /*! Total num of call numbers allowed to be allocated without calltoken validation */
1021 static uint16_t global_maxcallno_nonval;
1022 
1023 static uint16_t total_nonval_callno_used = 0;
1024 
1025 /*! peer connection private, keeps track of all the call numbers
1026  * consumed by a single ip address */
1027 struct peercnt {
1028  /*! ip address consuming call numbers */
1029  struct ast_sockaddr addr;
1030  /*! Number of call numbers currently used by this ip address */
1031  uint16_t cur;
1032  /*! Max call numbers allowed for this ip address */
1033  uint16_t limit;
1034  /*! Specifies whether limit is set by a registration or not, if so normal
1035  * limit setting rules do not apply to this address. */
1036  unsigned char reg;
1037 };
1038 
1039 /*! used by both callno_limits and calltoken_ignores containers */
1040 struct addr_range {
1041  /*! ip address range for custom callno limit rule */
1042  struct ast_ha ha;
1043  /*! callno limit for this ip address range, only used in callno_limits container */
1044  uint16_t limit;
1045  /*! delete me marker for reloads */
1046  unsigned char delme;
1047 };
1048 
1049 enum {
1050  /*! Extension exists */
1051  CACHE_FLAG_EXISTS = (1 << 0),
1052  /*! Extension is nonexistent */
1054  /*! Extension can exist */
1056  /*! Waiting to hear back response */
1058  /*! Timed out */
1060  /*! Request transmitted */
1062  /*! Timeout */
1064  /*! Matchmore */
1066 };
1067 
1069  char peercontext[AST_MAX_CONTEXT];
1070  char exten[AST_MAX_EXTENSION];
1071  struct timeval orig;
1072  struct timeval expiry;
1073  int flags;
1074  unsigned short callno;
1075  int waiters[256];
1076  AST_LIST_ENTRY(iax2_dpcache) cache_list;
1077  AST_LIST_ENTRY(iax2_dpcache) peer_list;
1078 };
1079 
1081 
1082 static void reg_source_db(struct iax2_peer *p);
1083 static struct iax2_peer *realtime_peer(const char *peername, struct ast_sockaddr *addr);
1084 static struct iax2_user *realtime_user(const char *username, struct ast_sockaddr *addr);
1085 
1086 static int ast_cli_netstats(struct mansession *s, int fd, int limit_fmt);
1087 static char *complete_iax2_peers(const char *line, const char *word, int pos, int state, uint64_t flags);
1088 static char *complete_iax2_unregister(const char *line, const char *word, int pos, int state);
1089 
1090 enum iax2_thread_iostate {
1091  IAX_IOSTATE_IDLE,
1092  IAX_IOSTATE_READY,
1093  IAX_IOSTATE_PROCESSING,
1094  IAX_IOSTATE_SCHEDREADY,
1095 };
1096 
1097 enum iax2_thread_type {
1098  IAX_THREAD_TYPE_POOL,
1099  IAX_THREAD_TYPE_DYNAMIC,
1100 };
1101 
1104  size_t len;
1105  unsigned char buf[1];
1106 };
1107 
1108 struct iax2_thread {
1110  enum iax2_thread_type type;
1111  enum iax2_thread_iostate iostate;
1112 #ifdef SCHED_MULTITHREADED
1113  void (*schedfunc)(const void *);
1114  const void *scheddata;
1115 #endif
1116 #ifdef DEBUG_SCHED_MULTITHREAD
1117  char curfunc[80];
1118 #endif
1119  int actions;
1120  pthread_t threadid;
1121  int threadnum;
1122  struct ast_sockaddr ioaddr;
1123  unsigned char readbuf[4096];
1124  unsigned char *buf;
1125  ssize_t buf_len;
1126  size_t buf_size;
1127  int iofd;
1128  time_t checktime;
1129  ast_mutex_t lock;
1130  ast_cond_t cond;
1131  ast_mutex_t init_lock;
1132  ast_cond_t init_cond;
1133  /*! if this thread is processing a full frame,
1134  some information about that frame will be stored
1135  here, so we can avoid dispatching any more full
1136  frames for that callno to other threads */
1137  struct {
1138  unsigned short callno;
1139  struct ast_sockaddr addr;
1140  unsigned char type;
1141  unsigned char csub;
1142  } ffinfo;
1143  /*! Queued up full frames for processing. If more full frames arrive for
1144  * a call which this thread is already processing a full frame for, they
1145  * are queued up here. */
1146  AST_LIST_HEAD_NOLOCK(, iax2_pkt_buf) full_frames;
1147  unsigned char stop;
1148 };
1149 
1150 /* Thread lists */
1154 
1155 static void *iax2_process_thread(void *data);
1156 static void iax2_destroy(int callno);
1157 
1158 static void signal_condition(ast_mutex_t *lock, ast_cond_t *cond)
1159 {
1160  ast_mutex_lock(lock);
1161  ast_cond_signal(cond);
1162  ast_mutex_unlock(lock);
1163 }
1164 
1165 /*!
1166  * \brief an array of iax2 pvt structures
1167  *
1168  * The container for active chan_iax2_pvt structures is implemented as an
1169  * array for extremely quick direct access to the correct pvt structure
1170  * based on the local call number. The local call number is used as the
1171  * index into the array where the associated pvt structure is stored.
1172  */
1173 static struct chan_iax2_pvt *iaxs[IAX_MAX_CALLS];
1174 
1175 static ast_callid iax_pvt_callid_get(int callno)
1176 {
1177  return iaxs[callno]->callid;
1178 }
1179 
1180 static void iax_pvt_callid_set(int callno, ast_callid callid)
1181 {
1182  iaxs[callno]->callid = callid;
1183 }
1184 
1185 static void iax_pvt_callid_new(int callno)
1186 {
1187  ast_callid callid = ast_create_callid();
1188  char buffer[AST_CALLID_BUFFER_LENGTH];
1189  ast_callid_strnprint(buffer, sizeof(buffer), callid);
1190  iax_pvt_callid_set(callno, callid);
1191 }
1192 
1193 /*!
1194  * \brief Another container of iax2_pvt structures
1195  *
1196  * Active IAX2 pvt structs are also stored in this container, if they are a part
1197  * of an active call where we know the remote side's call number. The reason
1198  * for this is that incoming media frames do not contain our call number. So,
1199  * instead of having to iterate the entire iaxs array, we use this container to
1200  * look up calls where the remote side is using a given call number.
1201  */
1203 
1204 /*!
1205  * \brief chan_iax2_pvt structure locks
1206  *
1207  * These locks are used when accessing a pvt structure in the iaxs array.
1208  * The index used here is the same as used in the iaxs array. It is the
1209  * local call number for the associated pvt struct.
1210  */
1211 static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)];
1212 
1213 /*!
1214  * \brief Another container of iax2_pvt structures
1215  *
1216  * Active IAX2 pvt structs used during transfering a call are stored here.
1217  */
1219 
1220 /* Flag to use with trunk calls, keeping these calls high up. It halves our effective use
1221  but keeps the division between trunked and non-trunked better. */
1222 #define TRUNK_CALL_START (IAX_MAX_CALLS / 2)
1223 
1224 /* Debug routines... */
1225 static struct ast_sockaddr debugaddr;
1226 
1227 static void iax_outputframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct ast_sockaddr *addr, int datalen)
1228 {
1229  if (iaxdebug ||
1230  (addr && !ast_sockaddr_isnull(&debugaddr) &&
1231  (!ast_sockaddr_port(&debugaddr) ||
1232  ast_sockaddr_port(&debugaddr) == ast_sockaddr_port(addr)) &&
1233  !ast_sockaddr_cmp_addr(&debugaddr, addr))) {
1234 
1235  if (iaxdebug) {
1236  iax_showframe(f, fhi, rx, addr, datalen);
1237  } else {
1238  iaxdebug = 1;
1239  iax_showframe(f, fhi, rx, addr, datalen);
1240  iaxdebug = 0;
1241  }
1242  }
1243 }
1244 
1245 static void iax_debug_output(const char *data)
1246 {
1247  if (iaxdebug)
1248  ast_verbose("%s", data);
1249 }
1250 
1251 static void iax_error_output(const char *data)
1252 {
1253  ast_log(LOG_WARNING, "%s", data);
1254 }
1255 
1256 static void __attribute__((format(printf, 1, 2))) jb_error_output(const char *fmt, ...)
1257 {
1258  va_list args;
1259  char buf[1024];
1260 
1261  va_start(args, fmt);
1262  vsnprintf(buf, sizeof(buf), fmt, args);
1263  va_end(args);
1264 
1265  ast_log(LOG_ERROR, "%s", buf);
1266 }
1267 
1268 static void __attribute__((format(printf, 1, 2))) jb_warning_output(const char *fmt, ...)
1269 {
1270  va_list args;
1271  char buf[1024];
1272 
1273  va_start(args, fmt);
1274  vsnprintf(buf, sizeof(buf), fmt, args);
1275  va_end(args);
1276 
1277  ast_log(LOG_WARNING, "%s", buf);
1278 }
1279 
1280 static void __attribute__((format(printf, 1, 2))) jb_debug_output(const char *fmt, ...)
1281 {
1282  va_list args;
1283  char buf[1024];
1284 
1285  va_start(args, fmt);
1286  vsnprintf(buf, sizeof(buf), fmt, args);
1287  va_end(args);
1288 
1289  ast_verbose("%s", buf);
1290 }
1291 
1292 static int expire_registry(const void *data);
1293 static int iax2_answer(struct ast_channel *c);
1294 static int iax2_call(struct ast_channel *c, const char *dest, int timeout);
1295 static int iax2_devicestate(const char *data);
1296 static int iax2_digit_begin(struct ast_channel *c, char digit);
1297 static int iax2_digit_end(struct ast_channel *c, char digit, unsigned int duration);
1298 static int iax2_do_register(struct iax2_registry *reg);
1299 static int iax2_fixup(struct ast_channel *oldchannel, struct ast_channel *newchan);
1300 static int iax2_hangup(struct ast_channel *c);
1301 static int iax2_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
1302 static int iax2_poke_peer(struct iax2_peer *peer, int heldcall);
1303 static int iax2_provision(struct ast_sockaddr *end, int sockfd, const char *dest, const char *template, int force);
1304 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final);
1305 static int iax2_sendhtml(struct ast_channel *c, int subclass, const char *data, int datalen);
1306 static int iax2_sendimage(struct ast_channel *c, struct ast_frame *img);
1307 static int iax2_sendtext(struct ast_channel *c, const char *text);
1308 static int iax2_setoption(struct ast_channel *c, int option, void *data, int datalen);
1309 static int iax2_queryoption(struct ast_channel *c, int option, void *data, int *datalen);
1310 static int iax2_transfer(struct ast_channel *c, const char *dest);
1311 static int iax2_write(struct ast_channel *c, struct ast_frame *f);
1312 static int iax2_sched_add(struct ast_sched_context *sched, int when, ast_sched_cb callback, const void *data);
1313 
1314 static int send_trunk(struct iax2_trunk_peer *tpeer, struct timeval *now);
1315 static int send_command(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int);
1316 static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int);
1317 static int send_command_immediate(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int);
1318 static int send_command_locked(unsigned short callno, char, int, unsigned int, const unsigned char *, int, int);
1319 static int send_command_transfer(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int);
1320 static struct ast_channel *iax2_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
1321 static struct ast_frame *iax2_read(struct ast_channel *c);
1322 static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly);
1323 static struct iax2_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly);
1324 static void realtime_update_peer(const char *peername, struct ast_sockaddr *sockaddr, time_t regtime);
1325 static void *iax2_dup_variable_datastore(void *);
1326 static void prune_peers(void);
1327 static void prune_users(void);
1328 static void iax2_free_variable_datastore(void *);
1329 
1330 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen);
1331 static int decode_frame(ast_aes_decrypt_key *dcx, struct ast_iax2_full_hdr *fh, struct ast_frame *f, int *datalen);
1332 static int encrypt_frame(ast_aes_encrypt_key *ecx, struct ast_iax2_full_hdr *fh, unsigned char *poo, int *datalen);
1333 static void build_ecx_key(const unsigned char *digest, struct chan_iax2_pvt *pvt);
1334 static void build_rand_pad(unsigned char *buf, ssize_t len);
1335 static int get_unused_callno(enum callno_type type, int validated, callno_entry *entry);
1336 static int replace_callno(const void *obj);
1337 static void sched_delay_remove(struct ast_sockaddr *addr, callno_entry entry);
1338 static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message);
1339 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message);
1340 
1341 static struct ast_channel_tech iax2_tech = {
1342  .type = "IAX2",
1343  .description = tdesc,
1344  .properties = AST_CHAN_TP_WANTSJITTER,
1345  .requester = iax2_request,
1346  .devicestate = iax2_devicestate,
1347  .send_digit_begin = iax2_digit_begin,
1348  .send_digit_end = iax2_digit_end,
1349  .send_text = iax2_sendtext,
1350  .send_image = iax2_sendimage,
1351  .send_html = iax2_sendhtml,
1352  .call = iax2_call,
1353  .hangup = iax2_hangup,
1354  .answer = iax2_answer,
1355  .read = iax2_read,
1356  .write = iax2_write,
1357  .write_video = iax2_write,
1358  .indicate = iax2_indicate,
1359  .setoption = iax2_setoption,
1360  .queryoption = iax2_queryoption,
1361  .transfer = iax2_transfer,
1362  .fixup = iax2_fixup,
1363  .func_channel_read = acf_channel_read,
1364 };
1365 
1366 /*!
1367  * \internal
1368  * \brief Obtain the owner channel lock if the owner exists.
1369  *
1370  * \param callno IAX2 call id.
1371  *
1372  * \note Assumes the iaxsl[callno] lock is already obtained.
1373  *
1374  * \note
1375  * IMPORTANT NOTE!!! Any time this function is used, even if
1376  * iaxs[callno] was valid before calling it, it may no longer be
1377  * valid after calling it. This function may unlock and lock
1378  * the mutex associated with this callno, meaning that another
1379  * thread may grab it and destroy the call.
1380  */
1381 static void iax2_lock_owner(int callno)
1382 {
1383  for (;;) {
1384  if (!iaxs[callno] || !iaxs[callno]->owner) {
1385  /* There is no owner lock to get. */
1386  break;
1387  }
1388  if (!ast_channel_trylock(iaxs[callno]->owner)) {
1389  /* We got the lock */
1390  break;
1391  }
1392  /* Avoid deadlock by pausing and trying again */
1393  DEADLOCK_AVOIDANCE(&iaxsl[callno]);
1394  }
1395 }
1396 
1397 /*!
1398  * \internal
1399  * \brief Check if a control subtype is allowed on the wire.
1400  *
1401  * \param subtype Control frame subtype to check if allowed to/from the wire.
1402  *
1403  * \retval non-zero if allowed.
1404  */
1405 static int iax2_is_control_frame_allowed(int subtype)
1406 {
1407  enum ast_control_frame_type control = subtype;
1408  int is_allowed;
1409 
1410  /*
1411  * Note: If we compare the enumeration type, which does not have any
1412  * negative constants, the compiler may optimize this code away.
1413  * Therefore, we must perform an integer comparison here.
1414  */
1415  if (subtype == -1) {
1416  return -1;
1417  }
1418 
1419  /* Default to not allowing control frames to pass. */
1420  is_allowed = 0;
1421 
1422  /*
1423  * The switch default is not present in order to take advantage
1424  * of the compiler complaining of a missing enum case.
1425  */
1426  switch (control) {
1427  /*
1428  * These control frames make sense to send/receive across the link.
1429  */
1430  case AST_CONTROL_HANGUP:
1431  case AST_CONTROL_RING:
1432  case AST_CONTROL_RINGING:
1433  case AST_CONTROL_ANSWER:
1434  case AST_CONTROL_BUSY:
1436  case AST_CONTROL_OFFHOOK:
1438  case AST_CONTROL_FLASH:
1439  case AST_CONTROL_WINK:
1440  case AST_CONTROL_OPTION:
1441  case AST_CONTROL_RADIO_KEY:
1443  case AST_CONTROL_PROGRESS:
1445  case AST_CONTROL_HOLD:
1446  case AST_CONTROL_UNHOLD:
1447  case AST_CONTROL_VIDUPDATE:
1451  case AST_CONTROL_AOC:
1453  case AST_CONTROL_MCID:
1454  is_allowed = -1;
1455  break;
1456 
1457  /*
1458  * These control frames do not make sense to send/receive across the link.
1459  */
1460  case _XXX_AST_CONTROL_T38:
1461  /* The control value is deprecated in favor of AST_CONTROL_T38_PARAMETERS. */
1462  case AST_CONTROL_SRCUPDATE:
1463  /* Across an IAX link the source is still the same. */
1464  case AST_CONTROL_TRANSFER:
1465  /* A success/fail status report from calling ast_transfer() on this machine. */
1466  case AST_CONTROL_CC:
1467  /* The payload contains pointers that are valid for the sending machine only. */
1468  case AST_CONTROL_SRCCHANGE:
1469  /* Across an IAX link the source is still the same. */
1471  /* The action can only be done by the sending machine. */
1472  case AST_CONTROL_END_OF_Q:
1473  /* This frame would cause the call to unexpectedly hangup. */
1475  /* Only meaningful across a bridge on this machine for direct-media exchange. */
1477  /* Intended only for the sending machine's local channel structure. */
1479  /* Intended only for masquerades when calling ast_indicate_data(). */
1481  /* Intended only for internal stream topology manipulation. */
1483  /* Intended only for internal stream topology change notification. */
1490  /* None of these playback stream control frames should go across the link. */
1495  /* None of these media recording control frames should go across the link. */
1496  break;
1497  }
1498  return is_allowed;
1499 }
1500 
1501 static void network_change_stasis_subscribe(void)
1502 {
1503  if (!network_change_sub) {
1504  network_change_sub = stasis_subscribe(ast_system_topic(),
1505  network_change_stasis_cb, NULL);
1506  stasis_subscription_accept_message_type(network_change_sub, ast_network_change_type());
1508  }
1509 }
1510 
1511 static void network_change_stasis_unsubscribe(void)
1512 {
1513  network_change_sub = stasis_unsubscribe_and_join(network_change_sub);
1514 }
1515 
1516 static void acl_change_stasis_subscribe(void)
1517 {
1518  if (!acl_change_sub) {
1519  acl_change_sub = stasis_subscribe(ast_security_topic(),
1520  acl_change_stasis_cb, NULL);
1523  }
1524 }
1525 
1526 static void acl_change_stasis_unsubscribe(void)
1527 {
1528  acl_change_sub = stasis_unsubscribe_and_join(acl_change_sub);
1529 }
1530 
1531 static int network_change_sched_cb(const void *data)
1532 {
1533  struct iax2_registry *reg;
1534  network_change_sched_id = -1;
1535  AST_LIST_LOCK(&registrations);
1536  AST_LIST_TRAVERSE(&registrations, reg, entry) {
1537  iax2_do_register(reg);
1538  }
1539  AST_LIST_UNLOCK(&registrations);
1540 
1541  return 0;
1542 }
1543 
1544 static void network_change_stasis_cb(void *data, struct stasis_subscription *sub,
1545  struct stasis_message *message)
1546 {
1547  /* This callback is only concerned with network change messages from the system topic. */
1548  if (stasis_message_type(message) != ast_network_change_type()) {
1549  return;
1550  }
1551 
1552  ast_verb(1, "IAX, got a network change message, renewing all IAX registrations.\n");
1553  if (network_change_sched_id == -1) {
1554  network_change_sched_id = iax2_sched_add(sched, 1000, network_change_sched_cb, NULL);
1555  }
1556 }
1557 
1558 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub,
1559  struct stasis_message *message)
1560 {
1561  if (stasis_message_type(message) != ast_named_acl_change_type()) {
1562  return;
1563  }
1564 
1565  ast_log(LOG_NOTICE, "Reloading chan_iax2 in response to ACL change event.\n");
1566  reload_config(1);
1567 }
1568 
1569 static const struct ast_datastore_info iax2_variable_datastore_info = {
1570  .type = "IAX2_VARIABLE",
1571  .duplicate = iax2_dup_variable_datastore,
1572  .destroy = iax2_free_variable_datastore,
1573 };
1574 
1575 static void *iax2_dup_variable_datastore(void *old)
1576 {
1577  AST_LIST_HEAD(, ast_var_t) *oldlist = old, *newlist;
1578  struct ast_var_t *oldvar, *newvar;
1579 
1580  newlist = ast_calloc(sizeof(*newlist), 1);
1581  if (!newlist) {
1582  ast_log(LOG_ERROR, "Unable to duplicate iax2 variables\n");
1583  return NULL;
1584  }
1585 
1586  AST_LIST_HEAD_INIT(newlist);
1587  AST_LIST_LOCK(oldlist);
1588  AST_LIST_TRAVERSE(oldlist, oldvar, entries) {
1589  newvar = ast_var_assign(ast_var_name(oldvar), ast_var_value(oldvar));
1590  if (newvar)
1591  AST_LIST_INSERT_TAIL(newlist, newvar, entries);
1592  else
1593  ast_log(LOG_ERROR, "Unable to duplicate iax2 variable '%s'\n", ast_var_name(oldvar));
1594  }
1595  AST_LIST_UNLOCK(oldlist);
1596  return newlist;
1597 }
1598 
1599 static void iax2_free_variable_datastore(void *old)
1600 {
1601  AST_LIST_HEAD(, ast_var_t) *oldlist = old;
1602  struct ast_var_t *oldvar;
1603 
1604  AST_LIST_LOCK(oldlist);
1605  while ((oldvar = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
1606  ast_free(oldvar);
1607  }
1608  AST_LIST_UNLOCK(oldlist);
1609  AST_LIST_HEAD_DESTROY(oldlist);
1610  ast_free(oldlist);
1611 }
1612 
1613 
1614 /* WARNING: insert_idle_thread should only ever be called within the
1615  * context of an iax2_process_thread() thread.
1616  */
1617 static void insert_idle_thread(struct iax2_thread *thread)
1618 {
1619  if (thread->type == IAX_THREAD_TYPE_DYNAMIC) {
1621  AST_LIST_INSERT_TAIL(&dynamic_list, thread, list);
1623  } else {
1625  AST_LIST_INSERT_TAIL(&idle_list, thread, list);
1627  }
1628 
1629  return;
1630 }
1631 
1632 static struct iax2_thread *find_idle_thread(void)
1633 {
1634  struct iax2_thread *thread = NULL;
1635 
1636  /* Pop the head of the idle list off */
1638  thread = AST_LIST_REMOVE_HEAD(&idle_list, list);
1640 
1641  /* If we popped a thread off the idle list, just return it */
1642  if (thread) {
1643  memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
1644  return thread;
1645  }
1646 
1647  /* Pop the head of the dynamic list off */
1649  thread = AST_LIST_REMOVE_HEAD(&dynamic_list, list);
1651 
1652  /* If we popped a thread off the dynamic list, just return it */
1653  if (thread) {
1654  memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
1655  return thread;
1656  }
1657 
1658  /* If we can't create a new dynamic thread for any reason, return no thread at all */
1659  if (iaxdynamicthreadcount >= iaxmaxthreadcount || !(thread = ast_calloc(1, sizeof(*thread))))
1660  return NULL;
1661 
1662  /* Set default values */
1663  ast_atomic_fetchadd_int(&iaxdynamicthreadcount, 1);
1664  thread->threadnum = ast_atomic_fetchadd_int(&iaxdynamicthreadnum, 1);
1665  thread->type = IAX_THREAD_TYPE_DYNAMIC;
1666 
1667  /* Initialize lock and condition */
1668  ast_mutex_init(&thread->lock);
1669  ast_cond_init(&thread->cond, NULL);
1670  ast_mutex_init(&thread->init_lock);
1671  ast_cond_init(&thread->init_cond, NULL);
1672  ast_mutex_lock(&thread->init_lock);
1673 
1674  /* Create thread and send it on it's way */
1675  if (ast_pthread_create_background(&thread->threadid, NULL, iax2_process_thread, thread)) {
1676  ast_cond_destroy(&thread->cond);
1677  ast_mutex_destroy(&thread->lock);
1678  ast_mutex_unlock(&thread->init_lock);
1679  ast_cond_destroy(&thread->init_cond);
1680  ast_mutex_destroy(&thread->init_lock);
1681  ast_free(thread);
1682  return NULL;
1683  }
1684 
1685  /* this thread is not processing a full frame (since it is idle),
1686  so ensure that the field for the full frame call number is empty */
1687  memset(&thread->ffinfo, 0, sizeof(thread->ffinfo));
1688 
1689  /* Wait for the thread to be ready before returning it to the caller */
1690  ast_cond_wait(&thread->init_cond, &thread->init_lock);
1691 
1692  /* Done with init_lock */
1693  ast_mutex_unlock(&thread->init_lock);
1694 
1695  return thread;
1696 }
1697 
1698 #ifdef SCHED_MULTITHREADED
1699 static int __schedule_action(void (*func)(const void *data), const void *data, const char *funcname)
1700 {
1701  struct iax2_thread *thread;
1702  static time_t lasterror;
1703  time_t t;
1704 
1705  thread = find_idle_thread();
1706  if (thread != NULL) {
1707  thread->schedfunc = func;
1708  thread->scheddata = data;
1709  thread->iostate = IAX_IOSTATE_SCHEDREADY;
1710 #ifdef DEBUG_SCHED_MULTITHREAD
1711  ast_copy_string(thread->curfunc, funcname, sizeof(thread->curfunc));
1712 #endif
1713  signal_condition(&thread->lock, &thread->cond);
1714  return 0;
1715  }
1716  time(&t);
1717  if (t != lasterror) {
1718  lasterror = t;
1719  ast_debug(1, "Out of idle IAX2 threads for scheduling! (%s)\n", funcname);
1720  }
1721 
1722  return -1;
1723 }
1724 #define schedule_action(func, data) __schedule_action(func, data, __PRETTY_FUNCTION__)
1725 #endif
1726 
1727 static int iax2_sched_replace(int id, struct ast_sched_context *con, int when,
1728  ast_sched_cb callback, const void *data)
1729 {
1730  return ast_sched_replace(id, con, when, callback, data);
1731 }
1732 
1733 static int iax2_sched_add(struct ast_sched_context *con, int when,
1734  ast_sched_cb callback, const void *data)
1735 {
1736  return ast_sched_add(con, when, callback, data);
1737 }
1738 
1739 /*!
1740  * \brief Acquire the iaxsl[callno] if call exists and not having ongoing hangup.
1741  * \param callno Call number to lock.
1742  * \retval 0 If call disappeared or has ongoing hangup procedure.
1743  * \retval 1 If call found and mutex is locked.
1744  */
1746 {
1747  ast_mutex_lock(&iaxsl[callno]);
1748 
1749  /* We acquired the lock; but the call was already destroyed (we came after full hang up procedures)
1750  * or destroy initiated (in middle of hang up procedure. */
1751  if (!iaxs[callno] || iaxs[callno]->destroy_initiated) {
1752  ast_debug(3, "I wanted to lock callno %d, but it is dead or going to die.\n", callno);
1753  ast_mutex_unlock(&iaxsl[callno]);
1754  return 0;
1755  }
1756 
1757  /* Lock acquired, and callno is alive and kicking. */
1758  return 1;
1759 }
1760 
1761 static int send_ping(const void *data);
1762 
1763 static void __send_ping(const void *data)
1764 {
1765  int callno = PTR_TO_CALLNO(data);
1766 
1767  if (iax2_lock_callno_unless_destroyed(callno) == 0) {
1768  ast_debug(3, "Hangup initiated on call %d, aborting __send_ping\n", callno);
1769  return;
1770  }
1771 
1772  /* Mark pingid as invalid scheduler id. */
1773  iaxs[callno]->pingid = -1;
1774 
1775  /* callno is now locked. */
1776  if (iaxs[callno]->peercallno) {
1777  /* Send PING packet. */
1778  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PING, 0, NULL, 0, -1);
1779 
1780  /* Schedule sending next ping. */
1781  iaxs[callno]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, data);
1782  }
1783 
1784  ast_mutex_unlock(&iaxsl[callno]);
1785 }
1786 
1787 static int send_ping(const void *data)
1788 {
1789 #ifdef SCHED_MULTITHREADED
1790  if (schedule_action(__send_ping, data))
1791 #endif
1792  __send_ping(data);
1793 
1794  return 0;
1795 }
1796 
1797 static void encmethods_to_str(int e, struct ast_str **buf)
1798 {
1799  ast_str_set(buf, 0, "(");
1800  if (e & IAX_ENCRYPT_AES128) {
1801  ast_str_append(buf, 0, "aes128");
1802  }
1803  if (e & IAX_ENCRYPT_KEYROTATE) {
1804  ast_str_append(buf, 0, ",keyrotate");
1805  }
1806  if (ast_str_strlen(*buf) > 1) {
1807  ast_str_append(buf, 0, ")");
1808  } else {
1809  ast_str_set(buf, 0, "No");
1810  }
1811 }
1812 
1813 static int get_encrypt_methods(const char *s)
1814 {
1815  int e;
1816  if (!strcasecmp(s, "aes128"))
1817  e = IAX_ENCRYPT_AES128 | IAX_ENCRYPT_KEYROTATE;
1818  else if (ast_true(s))
1819  e = IAX_ENCRYPT_AES128 | IAX_ENCRYPT_KEYROTATE;
1820  else
1821  e = 0;
1822  return e;
1823 }
1824 
1825 static int send_lagrq(const void *data);
1826 
1827 static void __send_lagrq(const void *data)
1828 {
1829  int callno = PTR_TO_CALLNO(data);
1830 
1831  if (iax2_lock_callno_unless_destroyed(callno) == 0) {
1832  ast_debug(3, "Hangup initiated on call %d, aborting __send_lagrq\n", callno);
1833  return;
1834  }
1835 
1836  /* Mark lagid as invalid scheduler id. */
1837  iaxs[callno]->lagid = -1;
1838 
1839  /* callno is now locked. */
1840  if (iaxs[callno]->peercallno) {
1841  /* Send LAGRQ packet. */
1842  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_LAGRQ, 0, NULL, 0, -1);
1843 
1844  /* Schedule sending next lagrq. */
1845  iaxs[callno]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, data);
1846  }
1847 
1848  ast_mutex_unlock(&iaxsl[callno]);
1849 }
1850 
1851 static int send_lagrq(const void *data)
1852 {
1853 #ifdef SCHED_MULTITHREADED
1854  if (schedule_action(__send_lagrq, data))
1855 #endif
1856  __send_lagrq(data);
1857  return 0;
1858 }
1859 
1860 static unsigned char compress_subclass(iax2_format subclass)
1861 {
1862  int x;
1863  int power=-1;
1864  /* If it's 64 or smaller, just return it */
1865  if (subclass < IAX_FLAG_SC_LOG)
1866  return subclass;
1867  /* Otherwise find its power */
1868  for (x = 0; x < IAX_MAX_SHIFT; x++) {
1869  if (subclass & (1LL << x)) {
1870  if (power > -1) {
1871  ast_log(LOG_WARNING, "Can't compress subclass %lld\n", (long long) subclass);
1872  return 0;
1873  } else
1874  power = x;
1875  }
1876  }
1877  return power | IAX_FLAG_SC_LOG;
1878 }
1879 
1880 static iax2_format uncompress_subclass(unsigned char csub)
1881 {
1882  /* If the SC_LOG flag is set, return 2^csub otherwise csub */
1883  if (csub & IAX_FLAG_SC_LOG) {
1884  /* special case for 'compressed' -1 */
1885  if (csub == 0xff)
1886  return -1;
1887  else
1888  return 1LL << (csub & ~IAX_FLAG_SC_LOG & IAX_MAX_SHIFT);
1889  }
1890  else
1891  return csub;
1892 }
1893 
1894 static struct ast_format *codec_choose_from_prefs(struct iax2_codec_pref *pref, struct ast_format_cap *cap)
1895 {
1896  int x;
1897  struct ast_format *found_format = NULL;
1898 
1899  for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
1900  struct ast_format *pref_format;
1901  uint64_t pref_bitfield;
1902 
1903  pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
1904  if (!pref_bitfield) {
1905  break;
1906  }
1907 
1908  pref_format = ast_format_compatibility_bitfield2format(pref_bitfield);
1909  if (!pref_format) {
1910  /* The bitfield is not associated with any format. */
1911  continue;
1912  }
1913  found_format = ast_format_cap_get_compatible_format(cap, pref_format);
1914  if (found_format) {
1915  break;
1916  }
1917  }
1918 
1919  if (found_format && (ast_format_get_type(found_format) == AST_MEDIA_TYPE_AUDIO)) {
1920  return found_format;
1921  }
1922 
1923  ast_debug(4, "Could not find preferred codec - Returning zero codec.\n");
1924  ao2_cleanup(found_format);
1925  return NULL;
1926 }
1927 
1928 static iax2_format iax2_codec_choose(struct iax2_codec_pref *pref, iax2_format formats)
1929 {
1930  struct ast_format_cap *cap;
1931  struct ast_format *tmpfmt;
1932  iax2_format format = 0;
1933 
1936  tmpfmt = codec_choose_from_prefs(pref, cap);
1937  if (!tmpfmt) {
1938  ao2_ref(cap, -1);
1939  return 0;
1940  }
1941 
1943  ao2_ref(tmpfmt, -1);
1944  ao2_ref(cap, -1);
1945  }
1946 
1947  return format;
1948 }
1949 
1950 const char *iax2_getformatname(iax2_format format)
1951 {
1952  struct ast_format *tmpfmt;
1953 
1955  if (!tmpfmt) {
1956  return "Unknown";
1957  }
1958 
1959  return ast_format_get_name(tmpfmt);
1960 }
1961 
1962 static const char *iax2_getformatname_multiple(iax2_format format, struct ast_str **codec_buf)
1963 {
1965 
1966  if (!cap) {
1967  return "(Nothing)";
1968  }
1970  ast_format_cap_get_names(cap, codec_buf);
1971  ao2_ref(cap, -1);
1972 
1973  return ast_str_buffer(*codec_buf);
1974 }
1975 
1976 static int iax2_parse_allow_disallow(struct iax2_codec_pref *pref, iax2_format *formats, const char *list, int allowing)
1977 {
1978  int res, i;
1979  struct ast_format_cap *cap;
1980 
1981  /* We want to add the formats to the cap in the preferred order */
1983  if (!cap || iax2_codec_pref_to_cap(pref, cap)) {
1984  ao2_cleanup(cap);
1985  return 1;
1986  }
1987 
1988  res = ast_format_cap_update_by_allow_disallow(cap, list, allowing);
1989 
1990  /* Adjust formats bitfield and pref list to match. */
1992  iax2_codec_pref_remove_missing(pref, *formats);
1993 
1994  for (i = 0; i < ast_format_cap_count(cap); i++) {
1995  struct ast_format *fmt = ast_format_cap_get_format(cap, i);
1996 
1998  ao2_ref(fmt, -1);
1999  }
2000 
2001  ao2_ref(cap, -1);
2002 
2003  return res;
2004 }
2005 
2006 /*!
2007  * \note The only member of the peer passed here guaranteed to be set is the name field
2008  */
2009 static int peer_hash_cb(const void *obj, const int flags)
2010 {
2011  const struct iax2_peer *peer = obj;
2012  const char *name = obj;
2013 
2014  return ast_str_hash(flags & OBJ_KEY ? name : peer->name);
2015 }
2016 
2017 /*!
2018  * \note The only member of the peer passed here guaranteed to be set is the name field
2019  */
2020 static int peer_cmp_cb(void *obj, void *arg, int flags)
2021 {
2022  struct iax2_peer *peer = obj, *peer2 = arg;
2023  const char *name = arg;
2024 
2025  return !strcmp(peer->name, flags & OBJ_KEY ? name : peer2->name) ?
2026  CMP_MATCH | CMP_STOP : 0;
2027 }
2028 
2029 /*!
2030  * \note The only member of the user passed here guaranteed to be set is the name field
2031  */
2032 static int user_hash_cb(const void *obj, const int flags)
2033 {
2034  const struct iax2_user *user = obj;
2035  const char *name = obj;
2036 
2037  return ast_str_hash(flags & OBJ_KEY ? name : user->name);
2038 }
2039 
2040 /*!
2041  * \note The only member of the user passed here guaranteed to be set is the name field
2042  */
2043 static int user_cmp_cb(void *obj, void *arg, int flags)
2044 {
2045  struct iax2_user *user = obj, *user2 = arg;
2046  const char *name = arg;
2047 
2048  return !strcmp(user->name, flags & OBJ_KEY ? name : user2->name) ?
2049  CMP_MATCH | CMP_STOP : 0;
2050 }
2051 
2052 /*!
2053  * \note This funtion calls realtime_peer -> reg_source_db -> iax2_poke_peer -> find_callno,
2054  * so do not call it with a pvt lock held.
2055  */
2056 static struct iax2_peer *find_peer(const char *name, int realtime)
2057 {
2058  struct iax2_peer *peer = NULL;
2059 
2060  peer = ao2_find(peers, name, OBJ_KEY);
2061 
2062  /* Now go for realtime if applicable */
2063  if (!peer && realtime) {
2064  peer = realtime_peer(name, NULL);
2065  }
2066  return peer;
2067 }
2068 
2069 static struct iax2_peer *peer_ref(struct iax2_peer *peer)
2070 {
2071  ao2_ref(peer, +1);
2072  return peer;
2073 }
2074 
2075 static inline struct iax2_peer *peer_unref(struct iax2_peer *peer)
2076 {
2077  ao2_ref(peer, -1);
2078  return NULL;
2079 }
2080 
2081 static struct iax2_user *find_user(const char *name)
2082 {
2083  return ao2_find(users, name, OBJ_KEY);
2084 }
2085 
2086 static inline struct iax2_user *user_unref(struct iax2_user *user)
2087 {
2088  ao2_ref(user, -1);
2089  return NULL;
2090 }
2091 
2092 static int iax2_getpeername(struct ast_sockaddr addr, char *host, int len)
2093 {
2094  struct iax2_peer *peer = NULL;
2095  int res = 0;
2096  struct ao2_iterator i;
2097 
2098  i = ao2_iterator_init(peers, 0);
2099  while ((peer = ao2_iterator_next(&i))) {
2100 
2101  if (!ast_sockaddr_cmp(&peer->addr, &addr)) {
2102  ast_copy_string(host, peer->name, len);
2103  peer_unref(peer);
2104  res = 1;
2105  break;
2106  }
2107  peer_unref(peer);
2108  }
2110 
2111  if (!peer) {
2112  peer = realtime_peer(NULL, &addr);
2113  if (peer) {
2114  ast_copy_string(host, peer->name, len);
2115  peer_unref(peer);
2116  res = 1;
2117  }
2118  }
2119 
2120  return res;
2121 }
2122 
2123 /* Call AST_SCHED_DEL on a scheduled task if it is found in scheduler. */
2124 static int iax2_delete_from_sched(const void* data)
2125 {
2126  int sched_id = (int)(long)data;
2127 
2128  AST_SCHED_DEL(sched, sched_id);
2129 
2130  return 0;
2131 }
2132 
2133 /*!\note Assumes the lock on the pvt is already held, when
2134  * iax2_destroy_helper() is called. */
2135 static void iax2_destroy_helper(struct chan_iax2_pvt *pvt)
2136 {
2137  /* Decrement AUTHREQ count if needed */
2138  if (ast_test_flag64(pvt, IAX_MAXAUTHREQ)) {
2139  struct iax2_user *user;
2140 
2141  user = ao2_find(users, pvt->username, OBJ_KEY);
2142  if (user) {
2143  ast_atomic_fetchadd_int(&user->curauthreq, -1);
2144  user_unref(user);
2145  }
2146 
2147  ast_clear_flag64(pvt, IAX_MAXAUTHREQ);
2148  }
2149 
2150 
2151  /* Mark call destroy initiated flag. */
2152  pvt->destroy_initiated = 1;
2153 
2154  /*
2155  * Schedule deleting the scheduled (but didn't run yet) PINGs or LAGRQs.
2156  * Already running tasks will be terminated because of destroy_initiated.
2157  *
2158  * Don't call AST_SCHED_DEL from this thread for pingid and lagid because
2159  * it leads to a deadlock between the scheduler thread callback locking
2160  * the callno mutex and this thread which holds the callno mutex one or
2161  * more times. It is better to have another thread delete the scheduled
2162  * callbacks which doesn't lock the callno mutex.
2163  */
2164  iax2_sched_add(sched, 0, iax2_delete_from_sched, (void*)(long)pvt->pingid);
2165  iax2_sched_add(sched, 0, iax2_delete_from_sched, (void*)(long)pvt->lagid);
2166 
2167  pvt->pingid = -1;
2168  pvt->lagid = -1;
2169 
2170  AST_SCHED_DEL(sched, pvt->autoid);
2171  AST_SCHED_DEL(sched, pvt->authid);
2172  AST_SCHED_DEL(sched, pvt->initid);
2173  AST_SCHED_DEL(sched, pvt->jbid);
2174  AST_SCHED_DEL(sched, pvt->keyrotateid);
2175 }
2176 
2177 static void iax2_frame_free(struct iax_frame *fr)
2178 {
2179  AST_SCHED_DEL(sched, fr->retrans);
2180  iax_frame_free(fr);
2181 }
2182 
2183 static int scheduled_destroy(const void *vid)
2184 {
2185  unsigned short callno = PTR_TO_CALLNO(vid);
2186  ast_mutex_lock(&iaxsl[callno]);
2187  if (iaxs[callno]) {
2188  ast_debug(1, "Really destroying %d now...\n", callno);
2189  iax2_destroy(callno);
2190  }
2191  ast_mutex_unlock(&iaxsl[callno]);
2192  return 0;
2193 }
2194 
2195 static void free_signaling_queue_entry(struct signaling_queue_entry *s)
2196 {
2197  if (s->f.datalen) {
2198  ast_free(s->f.data.ptr);
2199  }
2200  ast_free(s);
2201 }
2202 
2203 /*! \brief This function must be called once we are sure the other side has
2204  * given us a call number. All signaling is held here until that point. */
2205 static void send_signaling(struct chan_iax2_pvt *pvt)
2206 {
2207  struct signaling_queue_entry *s = NULL;
2208 
2209  while ((s = AST_LIST_REMOVE_HEAD(&pvt->signaling_queue, next))) {
2210  iax2_send(pvt, &s->f, 0, -1, 0, 0, 0);
2211  free_signaling_queue_entry(s);
2212  }
2213  pvt->hold_signaling = 0;
2214 }
2215 
2216 /*! \brief All frames other than that of type AST_FRAME_IAX must be held until
2217  * we have received a destination call number. */
2218 static int queue_signalling(struct chan_iax2_pvt *pvt, struct ast_frame *f)
2219 {
2220  struct signaling_queue_entry *qe;
2221 
2222  if (f->frametype == AST_FRAME_IAX || !pvt->hold_signaling) {
2223  return 1; /* do not queue this frame */
2224  } else if (!(qe = ast_calloc(1, sizeof(struct signaling_queue_entry)))) {
2225  return -1; /* out of memory */
2226  }
2227 
2228  /* copy ast_frame into our queue entry */
2229  qe->f = *f;
2230  if (qe->f.datalen) {
2231  /* if there is data in this frame copy it over as well */
2232  if (!(qe->f.data.ptr = ast_malloc(qe->f.datalen))) {
2233  free_signaling_queue_entry(qe);
2234  return -1;
2235  }
2236  memcpy(qe->f.data.ptr, f->data.ptr, qe->f.datalen);
2237  }
2238  AST_LIST_INSERT_TAIL(&pvt->signaling_queue, qe, next);
2239 
2240  return 0;
2241 }
2242 
2243 static void pvt_destructor(void *obj)
2244 {
2245  struct chan_iax2_pvt *pvt = obj;
2246  struct iax_frame *cur = NULL;
2247  struct signaling_queue_entry *s = NULL;
2248 
2249  ast_mutex_lock(&iaxsl[pvt->callno]);
2250 
2251  iax2_destroy_helper(pvt);
2252 
2253  sched_delay_remove(&pvt->addr, pvt->callno_entry);
2254  pvt->callno_entry = 0;
2255 
2256  /* Already gone */
2257  ast_set_flag64(pvt, IAX_ALREADYGONE);
2258 
2259  AST_LIST_TRAVERSE(&frame_queue[pvt->callno], cur, list) {
2260  /* Cancel any pending transmissions */
2261  cur->retries = -1;
2262  }
2263 
2264  ast_mutex_unlock(&iaxsl[pvt->callno]);
2265 
2266  while ((s = AST_LIST_REMOVE_HEAD(&pvt->signaling_queue, next))) {
2267  free_signaling_queue_entry(s);
2268  }
2269 
2270  if (pvt->reg) {
2271  pvt->reg->callno = 0;
2272  }
2273 
2274  if (!pvt->owner) {
2275  jb_frame frame;
2276  if (pvt->vars) {
2278  pvt->vars = NULL;
2279  }
2280 
2281  while (jb_getall(pvt->jb, &frame) == JB_OK) {
2282  iax2_frame_free(frame.data);
2283  }
2284 
2285  jb_destroy(pvt->jb);
2287  }
2288 }
2289 
2290 static struct chan_iax2_pvt *new_iax(struct ast_sockaddr *addr, const char *host)
2291 {
2292  struct chan_iax2_pvt *tmp;
2293  jb_conf jbconf;
2294 
2295  if (!(tmp = ao2_alloc(sizeof(*tmp), pvt_destructor))) {
2296  return NULL;
2297  }
2298 
2299  tmp->pingid = -1;
2300  tmp->lagid = -1;
2301  tmp->autoid = -1;
2302  tmp->authid = -1;
2303  tmp->initid = -1;
2304  tmp->keyrotateid = -1;
2305  tmp->jbid = -1;
2306 
2307  if (ast_string_field_init(tmp, 32)) {
2308  ao2_ref(tmp, -1);
2309  tmp = NULL;
2310  return NULL;
2311  }
2312 
2313  tmp->prefs = prefs_global;
2314 
2315  ast_string_field_set(tmp,exten, "s");
2316  ast_string_field_set(tmp,host, host);
2317 
2318  tmp->jb = jb_new();
2319  jbconf.max_jitterbuf = maxjitterbuffer;
2320  jbconf.resync_threshold = resyncthreshold;
2321  jbconf.max_contig_interp = maxjitterinterps;
2322  jbconf.target_extra = jittertargetextra;
2323  jb_setconf(tmp->jb,&jbconf);
2324 
2325  AST_LIST_HEAD_INIT_NOLOCK(&tmp->dpentries);
2326 
2327  tmp->hold_signaling = 1;
2328  AST_LIST_HEAD_INIT_NOLOCK(&tmp->signaling_queue);
2329 
2330  return tmp;
2331 }
2332 
2333 static struct iax_frame *iaxfrdup2(struct iax_frame *fr)
2334 {
2335  struct iax_frame *new = iax_frame_new(DIRECTION_INGRESS, fr->af.datalen, fr->cacheable);
2336  if (new) {
2337  size_t afdatalen = new->afdatalen;
2338  memcpy(new, fr, sizeof(*new));
2339  iax_frame_wrap(new, &fr->af);
2340  new->afdatalen = afdatalen;
2341  new->data = NULL;
2342  new->datalen = 0;
2343  new->direction = DIRECTION_INGRESS;
2344  new->retrans = -1;
2345  }
2346  return new;
2347 }
2348 /* keep these defined in this order. They are used in find_callno to
2349  * determine whether or not a new call number should be allowed. */
2350 enum {
2351  /* do not allow a new call number, only search ones in use for match */
2352  NEW_PREVENT = 0,
2353  /* search for match first, then allow a new one to be allocated */
2354  NEW_ALLOW = 1,
2355  /* do not search for match, force a new call number */
2356  NEW_FORCE = 2,
2357  /* do not search for match, force a new call number. Signifies call number
2358  * has been calltoken validated */
2359  NEW_ALLOW_CALLTOKEN_VALIDATED = 3,
2360 };
2361 
2362 static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
2363 {
2364  if (!ast_sockaddr_cmp(&cur->addr, addr)) {
2365  /* This is the main host */
2366  if ( (cur->peercallno == 0 || cur->peercallno == callno) &&
2367  (check_dcallno ? dcallno == cur->callno : 1) ) {
2368  /* That's us. Be sure we keep track of the peer call number */
2369  return 1;
2370  }
2371  }
2372  if (!ast_sockaddr_cmp(&cur->transfer, addr) && cur->transferring) {
2373  /* We're transferring */
2374  if ((dcallno == cur->callno) || (cur->transferring == TRANSFER_MEDIAPASS && cur->transfercallno == callno))
2375  return 1;
2376  }
2377  return 0;
2378 }
2379 
2380 static int make_trunk(unsigned short callno, int locked)
2381 {
2382  int x;
2383  int res= 0;
2384  callno_entry entry;
2385  if (iaxs[callno]->oseqno) {
2386  ast_log(LOG_WARNING, "Can't make trunk once a call has started!\n");
2387  return -1;
2388  }
2389  if (callno >= TRUNK_CALL_START) {
2390  ast_log(LOG_WARNING, "Call %d is already a trunk\n", callno);
2391  return -1;
2392  }
2393 
2394  if (get_unused_callno(
2395  CALLNO_TYPE_TRUNK,
2396  CALLNO_ENTRY_IS_VALIDATED(iaxs[callno]->callno_entry),
2397  &entry)) {
2398  ast_log(LOG_WARNING, "Unable to trunk call: Insufficient space\n");
2399  return -1;
2400  }
2401 
2402  x = CALLNO_ENTRY_GET_CALLNO(entry);
2403  ast_mutex_lock(&iaxsl[x]);
2404 
2405  /*!
2406  * \note We delete these before switching the slot, because if
2407  * they fire in the meantime, they will generate a warning.
2408  */
2409  AST_SCHED_DEL(sched, iaxs[callno]->pingid);
2410  AST_SCHED_DEL(sched, iaxs[callno]->lagid);
2411  iaxs[callno]->lagid = iaxs[callno]->pingid = -1;
2412  iaxs[x] = iaxs[callno];
2413  iaxs[x]->callno = x;
2414 
2415  /* since we copied over the pvt from a different callno, make sure the old entry is replaced
2416  * before assigning the new one */
2417  if (iaxs[x]->callno_entry) {
2418  iax2_sched_add(
2419  sched,
2420  MIN_REUSE_TIME * 1000,
2421  replace_callno,
2422  CALLNO_ENTRY_TO_PTR(iaxs[x]->callno_entry));
2423 
2424  }
2425  iaxs[x]->callno_entry = entry;
2426 
2427  iaxs[callno] = NULL;
2428  /* Update the two timers that should have been started */
2429  iaxs[x]->pingid = iax2_sched_add(sched,
2430  ping_time * 1000, send_ping, (void *)(long)x);
2431  iaxs[x]->lagid = iax2_sched_add(sched,
2432  lagrq_time * 1000, send_lagrq, (void *)(long)x);
2433 
2434  if (locked)
2435  ast_mutex_unlock(&iaxsl[callno]);
2436  res = x;
2437  if (!locked)
2438  ast_mutex_unlock(&iaxsl[x]);
2439 
2440  /* We moved this call from a non-trunked to a trunked call */
2441  ast_debug(1, "Made call %d into trunk call %d\n", callno, x);
2442 
2443  return res;
2444 }
2445 
2446 static void store_by_transfercallno(struct chan_iax2_pvt *pvt)
2447 {
2448  if (!pvt->transfercallno) {
2449  ast_log(LOG_ERROR, "This should not be called without a transfer call number.\n");
2450  return;
2451  }
2452 
2453  ao2_link(iax_transfercallno_pvts, pvt);
2454 }
2455 
2456 static void remove_by_transfercallno(struct chan_iax2_pvt *pvt)
2457 {
2458  if (!pvt->transfercallno) {
2459  ast_log(LOG_ERROR, "This should not be called without a transfer call number.\n");
2460  return;
2461  }
2462 
2463  ao2_unlink(iax_transfercallno_pvts, pvt);
2464 }
2465 static void store_by_peercallno(struct chan_iax2_pvt *pvt)
2466 {
2467  if (!pvt->peercallno) {
2468  ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
2469  return;
2470  }
2471 
2472  ao2_link(iax_peercallno_pvts, pvt);
2473 }
2474 
2475 static void remove_by_peercallno(struct chan_iax2_pvt *pvt)
2476 {
2477  if (!pvt->peercallno) {
2478  ast_log(LOG_ERROR, "This should not be called without a peer call number.\n");
2479  return;
2480  }
2481 
2482  ao2_unlink(iax_peercallno_pvts, pvt);
2483 }
2484 
2485 static int addr_range_delme_cb(void *obj, void *arg, int flags)
2486 {
2487  struct addr_range *lim = obj;
2488  lim->delme = 1;
2489  return 0;
2490 }
2491 
2492 static int addr_range_hash_cb(const void *obj, const int flags)
2493 {
2494  const struct addr_range *lim = obj;
2495  return abs(ast_sockaddr_hash(&lim->ha.addr));
2496 }
2497 
2498 static int addr_range_cmp_cb(void *obj, void *arg, int flags)
2499 {
2500  struct addr_range *lim1 = obj, *lim2 = arg;
2501  return (!(ast_sockaddr_cmp_addr(&lim1->ha.addr, &lim2->ha.addr)) &&
2502  !(ast_sockaddr_cmp_addr(&lim1->ha.netmask, &lim2->ha.netmask))) ?
2503  CMP_MATCH | CMP_STOP : 0;
2504 }
2505 
2506 static int peercnt_hash_cb(const void *obj, const int flags)
2507 {
2508  const struct peercnt *peercnt = obj;
2509 
2510  if (ast_sockaddr_isnull(&peercnt->addr)) {
2511  return 0;
2512  }
2513  return ast_sockaddr_hash(&peercnt->addr);
2514 }
2515 
2516 static int peercnt_cmp_cb(void *obj, void *arg, int flags)
2517 {
2518  struct peercnt *peercnt1 = obj, *peercnt2 = arg;
2519  return !ast_sockaddr_cmp_addr(&peercnt1->addr, &peercnt2->addr) ? CMP_MATCH | CMP_STOP : 0;
2520 }
2521 
2522 static int addr_range_match_address_cb(void *obj, void *arg, int flags)
2523 {
2524  struct addr_range *addr_range = obj;
2525  struct ast_sockaddr *addr = arg;
2526  struct ast_sockaddr tmp_addr;
2527 
2528  ast_sockaddr_apply_netmask(addr, &addr_range->ha.netmask, &tmp_addr);
2529 
2530  if (!ast_sockaddr_cmp_addr(&tmp_addr, &addr_range->ha.addr)) {
2531  return CMP_MATCH | CMP_STOP;
2532  }
2533  return 0;
2534 }
2535 
2536 /*!
2537  * \internal
2538  *
2539  * \brief compares addr to calltoken_ignores table to determine if validation is required.
2540  */
2541 static int calltoken_required(struct ast_sockaddr *addr, const char *name, int subclass)
2542 {
2543  struct addr_range *addr_range;
2544  struct iax2_peer *peer = NULL;
2545  struct iax2_user *user = NULL;
2546  /* if no username is given, check for guest accounts */
2547  const char *find = S_OR(name, "guest");
2548  int res = 1; /* required by default */
2549  int optional = 0;
2551  /* There are only two cases in which calltoken validation is not required.
2552  * Case 1. sin falls within the list of address ranges specified in the calltoken optional table and
2553  * the peer definition has not set the requirecalltoken option.
2554  * Case 2. Username is a valid peer/user, and that peer has requirecalltoken set either auto or no.
2555  */
2556 
2557  /* ----- Case 1 ----- */
2558  if ((addr_range = ao2_callback(calltoken_ignores, 0, addr_range_match_address_cb, addr))) {
2559  ao2_ref(addr_range, -1);
2560  optional = 1;
2561  }
2562 
2563  /* ----- Case 2 ----- */
2564  if ((subclass == IAX_COMMAND_NEW) && (user = find_user(find))) {
2565  calltoken_required = user->calltoken_required;
2566  } else if ((subclass == IAX_COMMAND_NEW) && (user = realtime_user(find, addr))) {
2567  calltoken_required = user->calltoken_required;
2568  } else if ((subclass != IAX_COMMAND_NEW) && (peer = find_peer(find, 0))) {
2569  calltoken_required = peer->calltoken_required;
2570  } else if ((subclass != IAX_COMMAND_NEW) && (peer = realtime_peer(find, addr))) {
2571  calltoken_required = peer->calltoken_required;
2572  }
2573 
2574  if (peer) {
2575  peer_unref(peer);
2576  }
2577  if (user) {
2578  user_unref(user);
2579  }
2580 
2581  ast_debug(1, "Determining if address %s with username %s requires calltoken validation. Optional = %d calltoken_required = %u \n", ast_sockaddr_stringify_addr(addr), name, optional, calltoken_required);
2582  if (((calltoken_required == CALLTOKEN_NO) || (calltoken_required == CALLTOKEN_AUTO)) ||
2583  (optional && (calltoken_required == CALLTOKEN_DEFAULT))) {
2584  res = 0;
2585  }
2586 
2587  return res;
2588 }
2589 
2590 /*!
2591  * \internal
2592  *
2593  * \brief set peercnt callno limit.
2594  *
2595  * \details
2596  * First looks in custom definitions. If not found, global limit
2597  * is used. Entries marked as reg already have
2598  * a custom limit set by a registration and are not modified.
2599  */
2600 static void set_peercnt_limit(struct peercnt *peercnt)
2601 {
2602  uint16_t limit = global_maxcallno;
2603  struct addr_range *addr_range;
2604  struct ast_sockaddr addr;
2605 
2606  ast_sockaddr_copy(&addr, &peercnt->addr);
2607 
2608  if (peercnt->reg && peercnt->limit) {
2609  return; /* this peercnt has a custom limit set by a registration */
2610  }
2611 
2612  if ((addr_range = ao2_callback(callno_limits, 0, addr_range_match_address_cb, &addr))) {
2613  limit = addr_range->limit;
2614  ast_debug(1, "custom addr_range %d found for %s\n", limit, ast_sockaddr_stringify(&addr));
2615  ao2_ref(addr_range, -1);
2616  }
2617 
2618  peercnt->limit = limit;
2619 }
2620 
2621 /*!
2622  * \internal
2623  * \brief sets limits for all peercnts in table. done on reload to reflect changes in conf.
2624  */
2625 static int set_peercnt_limit_all_cb(void *obj, void *arg, int flags)
2626 {
2627  struct peercnt *peercnt = obj;
2628 
2629  set_peercnt_limit(peercnt);
2630  ast_debug(1, "Reset limits for peercnts table\n");
2631 
2632  return 0;
2633 }
2634 
2635 /*!
2636  * \internal
2637  * \brief returns match if delme is set.
2638  */
2639 static int prune_addr_range_cb(void *obj, void *arg, int flags)
2640 {
2641  struct addr_range *addr_range = obj;
2642 
2643  return addr_range->delme ? CMP_MATCH : 0;
2644 }
2645 
2646 /*!
2647  * \internal
2648  * \brief modifies peercnt entry in peercnts table. Used to set custom limit or mark a registered ip
2649  */
2650 static void peercnt_modify(unsigned char reg, uint16_t limit, struct ast_sockaddr *sockaddr)
2651 {
2652  /* this function turns off and on custom callno limits set by peer registration */
2653  struct peercnt *peercnt;
2654  struct peercnt tmp;
2655 
2656  ast_sockaddr_copy(&tmp.addr, sockaddr);
2657 
2658  if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
2659  peercnt->reg = reg;
2660  if (limit) {
2661  peercnt->limit = limit;
2662  } else {
2663  set_peercnt_limit(peercnt);
2664  }
2665  ast_debug(1, "peercnt entry %s modified limit:%d registered:%d", ast_sockaddr_stringify_addr(sockaddr), peercnt->limit, peercnt->reg);
2666  ao2_ref(peercnt, -1); /* decrement ref from find */
2667  }
2668 }
2669 
2670 /*!
2671  * \internal
2672  * \brief adds an ip to the peercnts table, increments connection count if it already exists
2673  *
2674  * \details First searches for the address in the peercnts table. If found
2675  * the current count is incremented. If not found a new peercnt is allocated
2676  * and linked into the peercnts table with a call number count of 1.
2677  */
2678 static int peercnt_add(struct ast_sockaddr *addr)
2679 {
2680  struct peercnt *peercnt;
2681  int res = 0;
2682  struct peercnt tmp;
2683 
2684  ast_sockaddr_copy(&tmp.addr, addr);
2685 
2686  /* Reasoning for peercnts container lock: Two identical ip addresses
2687  * could be added by different threads at the "same time". Without the container
2688  * lock, both threads could alloc space for the same object and attempt
2689  * to link to table. With the lock, one would create the object and link
2690  * to table while the other would find the already created peercnt object
2691  * rather than creating a new one. */
2692  ao2_lock(peercnts);
2693  if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
2694  ao2_lock(peercnt);
2695  } else if ((peercnt = ao2_alloc(sizeof(*peercnt), NULL))) {
2696  ao2_lock(peercnt);
2697  /* create and set defaults */
2698  ast_sockaddr_copy(&peercnt->addr, addr);
2699  set_peercnt_limit(peercnt);
2700  /* guarantees it does not go away after unlocking table
2701  * ao2_find automatically adds this */
2702  ao2_link(peercnts, peercnt);
2703  } else {
2704  ao2_unlock(peercnts);
2705  return -1;
2706  }
2707 
2708  /* check to see if the address has hit its callno limit. If not increment cur. */
2709  if (peercnt->limit > peercnt->cur) {
2710  peercnt->cur++;
2711  ast_debug(1, "ip callno count incremented to %d for %s\n", peercnt->cur, ast_sockaddr_stringify_addr(addr));
2712  } else { /* max num call numbers for this peer has been reached! */
2713  ast_log(LOG_ERROR, "maxcallnumber limit of %d for %s has been reached!\n", peercnt->limit, ast_sockaddr_stringify_addr(addr));
2714  res = -1;
2715  }
2716 
2717  /* clean up locks and ref count */
2718  ao2_unlock(peercnt);
2719  ao2_unlock(peercnts);
2720  ao2_ref(peercnt, -1); /* decrement ref from find/alloc, only the container ref remains. */
2721 
2722  return res;
2723 }
2724 
2725 /*!
2726  * \internal
2727  * \brief decrements a peercnts table entry
2728  */
2729 static void peercnt_remove(struct peercnt *peercnt)
2730 {
2731  struct ast_sockaddr addr;
2732 
2733  ast_sockaddr_copy(&addr, &peercnt->addr);
2734 
2735  /*
2736  * Container locked here since peercnt may be unlinked from
2737  * list. If left unlocked, peercnt_add could try and grab this
2738  * entry from the table and modify it at the "same time" this
2739  * thread attempts to unlink it.
2740  */
2741  ao2_lock(peercnts);
2742  peercnt->cur--;
2743  ast_debug(1, "ip callno count decremented to %d for %s\n", peercnt->cur, ast_sockaddr_stringify_addr(&addr));
2744  /* if this was the last connection from the peer remove it from table */
2745  if (peercnt->cur == 0) {
2746  ao2_unlink(peercnts, peercnt);/* decrements ref from table, last ref is left to scheduler */
2747  }
2748  ao2_unlock(peercnts);
2749 }
2750 
2751 /*!
2752  * \internal
2753  * \brief called by scheduler to decrement object
2754  */
2755 static int peercnt_remove_cb(const void *obj)
2756 {
2757  struct peercnt *peercnt = (struct peercnt *) obj;
2758 
2759  peercnt_remove(peercnt);
2760  ao2_ref(peercnt, -1); /* decrement ref from scheduler */
2761 
2762  return 0;
2763 }
2764 
2765 /*!
2766  * \internal
2767  * \brief decrements peercnts connection count, finds by addr
2768  */
2769 static int peercnt_remove_by_addr(struct ast_sockaddr *addr)
2770 {
2771  struct peercnt *peercnt;
2772  struct peercnt tmp;
2773 
2774  ast_sockaddr_copy(&tmp.addr, addr);
2775 
2776  if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
2777  peercnt_remove(peercnt);
2778  ao2_ref(peercnt, -1); /* decrement ref from find */
2779  }
2780  return 0;
2781 }
2782 
2783 /*!
2784  * \internal
2785  * \brief Create callno_limit entry based on configuration
2786  */
2787 static void build_callno_limits(struct ast_variable *v)
2788 {
2789  struct addr_range *addr_range = NULL;
2790  struct addr_range tmp;
2791  struct ast_ha *ha;
2792  int limit;
2793  int error;
2794  int found;
2795 
2796  for (; v; v = v->next) {
2797  limit = -1;
2798  error = 0;
2799  found = 0;
2800  ha = ast_append_ha("permit", v->name, NULL, &error);
2801 
2802  /* check for valid config information */
2803  if (error) {
2804  ast_log(LOG_ERROR, "Call number limit for %s could not be added, Invalid address range\n.", v->name);
2805  continue;
2806  } else if ((sscanf(v->value, "%d", &limit) != 1) || (limit < 0)) {
2807  ast_log(LOG_ERROR, "Call number limit for %s could not be added. Invalid limit %s\n.", v->name, v->value);
2808  ast_free_ha(ha);
2809  continue;
2810  }
2811 
2812  ast_copy_ha(ha, &tmp.ha);
2813  /* find or create the addr_range */
2814  if ((addr_range = ao2_find(callno_limits, &tmp, OBJ_POINTER))) {
2815  ao2_lock(addr_range);
2816  found = 1;
2817  } else if (!(addr_range = ao2_alloc(sizeof(*addr_range), NULL))) {
2818  ast_free_ha(ha);
2819  return; /* out of memory */
2820  }
2821 
2822  /* copy over config data into addr_range object */
2823  ast_copy_ha(ha, &addr_range->ha); /* this is safe because only one ha is possible for each limit */
2824  ast_free_ha(ha); /* cleanup the tmp ha */
2825  addr_range->limit = limit;
2826  addr_range->delme = 0;
2827 
2828  /* cleanup */
2829  if (found) {
2830  ao2_unlock(addr_range);
2831  } else {
2832  ao2_link(callno_limits, addr_range);
2833  }
2834  ao2_ref(addr_range, -1); /* decrement ref from ao2_find and ao2_alloc, only container ref remains */
2835  }
2836 }
2837 
2838 /*!
2839  * \internal
2840  * \brief Create calltoken_ignores entry based on configuration
2841  */
2842 static int add_calltoken_ignore(const char *addr)
2843 {
2844  struct addr_range tmp;
2845  struct addr_range *addr_range = NULL;
2846  struct ast_ha *ha = NULL;
2847  int error = 0;
2848 
2849  if (ast_strlen_zero(addr)) {
2850  ast_log(LOG_WARNING, "invalid calltokenoptional (null)\n");
2851  return -1;
2852  }
2853 
2854  ha = ast_append_ha("permit", addr, NULL, &error);
2855 
2856  /* check for valid config information */
2857  if (error) {
2858  ast_log(LOG_WARNING, "Error %d creating calltokenoptional entry %s\n", error, addr);
2859  return -1;
2860  }
2861 
2862  ast_copy_ha(ha, &tmp.ha);
2863  /* find or create the addr_range */
2864  if ((addr_range = ao2_find(calltoken_ignores, &tmp, OBJ_POINTER))) {
2865  ao2_lock(addr_range);
2866  addr_range->delme = 0;
2867  ao2_unlock(addr_range);
2868  } else if ((addr_range = ao2_alloc(sizeof(*addr_range), NULL))) {
2869  /* copy over config data into addr_range object */
2870  ast_copy_ha(ha, &addr_range->ha); /* this is safe because only one ha is possible */
2871  ao2_link(calltoken_ignores, addr_range);
2872  } else {
2873  ast_free_ha(ha);
2874  return -1;
2875  }
2876 
2877  ast_free_ha(ha);
2878  ao2_ref(addr_range, -1); /* decrement ref from ao2_find and ao2_alloc, only container ref remains */
2879 
2880  return 0;
2881 }
2882 
2883 static char *handle_cli_iax2_show_callno_limits(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2884 {
2885  struct ao2_iterator i;
2886  struct peercnt *peercnt;
2887  struct ast_sockaddr addr;
2888  int found = 0;
2889 
2890  switch (cmd) {
2891  case CLI_INIT:
2892  e->command = "iax2 show callnumber usage";
2893  e->usage =
2894  "Usage: iax2 show callnumber usage [IP address]\n"
2895  " Shows current IP addresses which are consuming iax2 call numbers\n";
2896  return NULL;
2897  case CLI_GENERATE:
2898  return NULL;
2899  case CLI_HANDLER:
2900  if (a->argc < 4 || a->argc > 5)
2901  return CLI_SHOWUSAGE;
2902 
2903  if (a->argc == 4) {
2904  ast_cli(a->fd, "%-45s %-12s %-12s\n", "Address", "Callno Usage", "Callno Limit");
2905  }
2906 
2907  i = ao2_iterator_init(peercnts, 0);
2908  while ((peercnt = ao2_iterator_next(&i))) {
2909  ast_sockaddr_copy(&addr, &peercnt->addr);
2910 
2911  if (a->argc == 5) {
2912  if (!strcasecmp(a->argv[4], ast_sockaddr_stringify(&addr))) {
2913  ast_cli(a->fd, "%-45s %-12s %-12s\n", "Address", "Callno Usage", "Callno Limit");
2914  ast_cli(a->fd, "%-45s %-12d %-12d\n", ast_sockaddr_stringify(&addr), peercnt->cur, peercnt->limit);
2915  ao2_ref(peercnt, -1);
2916  found = 1;
2917  break;
2918  }
2919  } else {
2920  ast_cli(a->fd, "%-45s %-12d %-12d\n", ast_sockaddr_stringify(&addr), peercnt->cur, peercnt->limit);
2921  }
2922  ao2_ref(peercnt, -1);
2923  }
2925 
2926  if (a->argc == 4) {
2927  size_t pool_avail = callno_pool.available;
2928  size_t trunk_pool_avail = callno_pool_trunk.available;
2929 
2930  ast_cli(a->fd, "\nNon-CallToken Validation Callno Limit: %d\n"
2931  "Non-CallToken Validated Callno Used: %d\n",
2932  global_maxcallno_nonval,
2933  total_nonval_callno_used);
2934 
2935  ast_cli(a->fd, "Total Available Callno: %zu\n"
2936  "Regular Callno Available: %zu\n"
2937  "Trunk Callno Available: %zu\n",
2938  pool_avail + trunk_pool_avail,
2939  pool_avail,
2940  trunk_pool_avail);
2941  } else if (a->argc == 5 && !found) {
2942  ast_cli(a->fd, "No call number table entries for %s found\n", a->argv[4] );
2943  }
2944 
2945 
2946  return CLI_SUCCESS;
2947  default:
2948  return NULL;
2949  }
2950 }
2951 
2952 static int get_unused_callno(enum callno_type type, int validated, callno_entry *entry)
2953 {
2954  struct call_number_pool *pool = NULL;
2955  callno_entry swap;
2956  size_t choice;
2957 
2958  switch (type) {
2959  case CALLNO_TYPE_NORMAL:
2960  pool = &callno_pool;
2961  break;
2962  case CALLNO_TYPE_TRUNK:
2963  pool = &callno_pool_trunk;
2964  break;
2965  default:
2966  ast_assert(0);
2967  break;
2968  }
2969 
2970  /* If we fail, make sure this has a defined value */
2971  *entry = 0;
2972 
2973  /* We lock here primarily to ensure thread safety of the
2974  * total_nonval_callno_used check and increment */
2975  ast_mutex_lock(&callno_pool_lock);
2976 
2977  /* Bail out if we don't have any available call numbers */
2978  if (!pool->available) {
2979  ast_log(LOG_WARNING, "Out of call numbers\n");
2980  ast_mutex_unlock(&callno_pool_lock);
2981  return 1;
2982  }
2983 
2984  /* Only a certain number of non-validated call numbers should be allocated.
2985  * If there ever is an attack, this separates the calltoken validating users
2986  * from the non-calltoken validating users. */
2987  if (!validated && total_nonval_callno_used >= global_maxcallno_nonval) {
2988  ast_log(LOG_WARNING,
2989  "NON-CallToken callnumber limit is reached. Current: %d Max: %d\n",
2990  total_nonval_callno_used,
2991  global_maxcallno_nonval);
2992  ast_mutex_unlock(&callno_pool_lock);
2993  return 1;
2994  }
2995 
2996  /* We use a modified Fisher-Yates-Durstenfeld Shuffle to maintain a list of
2997  * available call numbers. The array of call numbers begins as an ordered
2998  * list from 1 -> n, and we keep a running tally of how many remain unclaimed
2999  * - let's call that x. When a call number is needed we pick a random index
3000  * into the array between 0 and x and use that as our call number. In a
3001  * typical FYD shuffle, we would swap the value that we are extracting with
3002  * the number at x, but in our case we swap and don't touch the value at x
3003  * because it is effectively invisible. We rely on the rest of the IAX2 core
3004  * to return the number to us at some point. Finally, we decrement x by 1
3005  * which establishes our new unused range.
3006  *
3007  * When numbers are returned to the pool, we put them just past x and bump x
3008  * by 1 so that this number is now available for re-use. */
3009 
3010  choice = ast_random() % pool->available;
3011 
3012  *entry = pool->numbers[choice];
3013  swap = pool->numbers[pool->available - 1];
3014 
3015  pool->numbers[choice] = swap;
3016  pool->available--;
3017 
3018  if (validated) {
3019  CALLNO_ENTRY_SET_VALIDATED(*entry);
3020  } else {
3021  total_nonval_callno_used++;
3022  }
3023 
3024  ast_mutex_unlock(&callno_pool_lock);
3025 
3026  return 0;
3027 }
3028 
3029 static int replace_callno(const void *obj)
3030 {
3031  callno_entry entry = PTR_TO_CALLNO_ENTRY(obj);
3032  struct call_number_pool *pool;
3033 
3034  /* We lock here primarily to ensure thread safety of the
3035  * total_nonval_callno_used check and decrement */
3036  ast_mutex_lock(&callno_pool_lock);
3037 
3038  if (!CALLNO_ENTRY_IS_VALIDATED(entry)) {
3039  if (total_nonval_callno_used) {
3040  total_nonval_callno_used--;
3041  } else {
3042  ast_log(LOG_ERROR,
3043  "Attempted to decrement total non calltoken validated "
3044  "callnumbers below zero. Callno is: %d\n",
3045  CALLNO_ENTRY_GET_CALLNO(entry));
3046  }
3047  }
3048 
3049  if (CALLNO_ENTRY_GET_CALLNO(entry) < TRUNK_CALL_START) {
3050  pool = &callno_pool;
3051  } else {
3052  pool = &callno_pool_trunk;
3053  }
3054 
3055  ast_assert(pool->capacity > pool->available);
3056 
3057  /* This clears the validated flag */
3058  entry = CALLNO_ENTRY_GET_CALLNO(entry);
3059 
3060  pool->numbers[pool->available] = entry;
3061  pool->available++;
3062 
3063  ast_mutex_unlock(&callno_pool_lock);
3064 
3065  return 0;
3066 }
3067 
3068 static int create_callno_pools(void)
3069 {
3070  uint16_t i;
3071 
3072  callno_pool.available = callno_pool_trunk.available = 0;
3073 
3074  /* We start at 2. 0 and 1 are reserved. */
3075  for (i = 2; i < TRUNK_CALL_START; i++) {
3076  callno_pool.numbers[callno_pool.available] = i;
3077  callno_pool.available++;
3078  }
3079 
3080  for (i = TRUNK_CALL_START; i < IAX_MAX_CALLS; i++) {
3081  callno_pool_trunk.numbers[callno_pool_trunk.available] = i;
3082  callno_pool_trunk.available++;
3083  }
3084 
3085  callno_pool.capacity = callno_pool.available;
3086  callno_pool_trunk.capacity = callno_pool_trunk.available;
3087 
3088  ast_assert(callno_pool.capacity && callno_pool_trunk.capacity);
3089 
3090  return 0;
3091 }
3092 
3093 /*!
3094  * \internal
3095  * \brief Schedules delayed removal of iax2_pvt call number data
3096  *
3097  * \note After MIN_REUSE_TIME has passed for a destroyed iax2_pvt, the callno is
3098  * available again, and the address from the previous connection must be decremented
3099  * from the peercnts table. This function schedules these operations to take place.
3100  */
3101 static void sched_delay_remove(struct ast_sockaddr *addr, callno_entry entry)
3102 {
3103  int i;
3104  struct peercnt *peercnt;
3105  struct peercnt tmp;
3106 
3107  ast_sockaddr_copy(&tmp.addr, addr);
3108 
3109  if ((peercnt = ao2_find(peercnts, &tmp, OBJ_POINTER))) {
3110  /* refcount is incremented with ao2_find. keep that ref for the scheduler */
3111  ast_debug(1, "schedule decrement of callno used for %s in %d seconds\n", ast_sockaddr_stringify_addr(addr), MIN_REUSE_TIME);
3112  i = iax2_sched_add(sched, MIN_REUSE_TIME * 1000, peercnt_remove_cb, peercnt);
3113  if (i == -1) {
3114  ao2_ref(peercnt, -1);
3115  }
3116  }
3117 
3118  iax2_sched_add(
3119  sched,
3120  MIN_REUSE_TIME * 1000,
3121  replace_callno,
3122  CALLNO_ENTRY_TO_PTR(entry));
3123 }
3124 
3125 /*!
3126  * \internal
3127  * \brief returns whether or not a frame is capable of starting a new IAX2 dialog.
3128  *
3129  * \note For this implementation, inbound pokes should _NOT_ be capable of allocating
3130  * a new callno.
3131  */
3132 static inline int attribute_pure iax2_allow_new(int frametype, int subclass, int inbound)
3133 {
3134  if (frametype != AST_FRAME_IAX) {
3135  return 0;
3136  }
3137  switch (subclass) {
3138  case IAX_COMMAND_NEW:
3139  case IAX_COMMAND_REGREQ:
3140  case IAX_COMMAND_FWDOWNL:
3141  case IAX_COMMAND_REGREL:
3142  return 1;
3143  case IAX_COMMAND_POKE:
3144  if (!inbound) {
3145  return 1;
3146  }
3147  break;
3148  }
3149  return 0;
3150 }
3151 
3152 /*!
3153  * \note Calling this function while holding another pvt lock can cause a deadlock.
3154  */
3155 static int __find_callno(unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int return_locked, int check_dcallno)
3156 {
3157  int res = 0;
3158  int x;
3159  /* this call is calltoken validated as long as it is either NEW_FORCE
3160  * or NEW_ALLOW_CALLTOKEN_VALIDATED */
3161  int validated = (new > NEW_ALLOW) ? 1 : 0;
3162  char host[80];
3163 
3164  if (new <= NEW_ALLOW) {
3165  if (callno) {
3166  struct chan_iax2_pvt *pvt;
3167  struct chan_iax2_pvt tmp_pvt = {
3168  .callno = dcallno,
3169  .peercallno = callno,
3170  .transfercallno = callno,
3171  /* hack!! */
3172  .frames_received = check_dcallno,
3173  };
3174 
3175  ast_sockaddr_copy(&tmp_pvt.addr, addr);
3176  /* this works for finding normal call numbers not involving transfering */
3177  if ((pvt = ao2_find(iax_peercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
3178  if (return_locked) {
3179  ast_mutex_lock(&iaxsl[pvt->callno]);
3180  }
3181  res = pvt->callno;
3182  ao2_ref(pvt, -1);
3183  pvt = NULL;
3184  return res;
3185  }
3186  /* this searches for transfer call numbers that might not get caught otherwise */
3187  memset(&tmp_pvt.addr, 0, sizeof(tmp_pvt.addr));
3188  ast_sockaddr_copy(&tmp_pvt.transfer, addr);
3189  if ((pvt = ao2_find(iax_transfercallno_pvts, &tmp_pvt, OBJ_POINTER))) {
3190  if (return_locked) {
3191  ast_mutex_lock(&iaxsl[pvt->callno]);
3192  }
3193  res = pvt->callno;
3194  ao2_ref(pvt, -1);
3195  pvt = NULL;
3196  return res;
3197  }
3198  }
3199  /* This will occur on the first response to a message that we initiated,
3200  * such as a PING. */
3201  if (dcallno) {
3202  ast_mutex_lock(&iaxsl[dcallno]);
3203  }
3204  if (callno && dcallno && iaxs[dcallno] && !iaxs[dcallno]->peercallno && match(addr, callno, dcallno, iaxs[dcallno], check_dcallno)) {
3205  iaxs[dcallno]->peercallno = callno;
3206  res = dcallno;
3207  store_by_peercallno(iaxs[dcallno]);
3208  if (!res || !return_locked) {
3209  ast_mutex_unlock(&iaxsl[dcallno]);
3210  }
3211  return res;
3212  }
3213  if (dcallno) {
3214  ast_mutex_unlock(&iaxsl[dcallno]);
3215  }
3216  }
3217  if (!res && (new >= NEW_ALLOW)) {
3218  callno_entry entry;
3219 
3220  /* It may seem odd that we look through the peer list for a name for
3221  * this *incoming* call. Well, it is weird. However, users don't
3222  * have an IP address/port number that we can match against. So,
3223  * this is just checking for a peer that has that IP/port and
3224  * assuming that we have a user of the same name. This isn't always
3225  * correct, but it will be changed if needed after authentication. */
3226  if (!iax2_getpeername(*addr, host, sizeof(host)))
3227  snprintf(host, sizeof(host), "%s", ast_sockaddr_stringify(addr));
3228 
3229  if (peercnt_add(addr)) {
3230  /* This address has hit its callnumber limit. When the limit
3231  * is reached, the connection is not added to the peercnts table.*/
3232  return 0;
3233  }
3234 
3235  if (get_unused_callno(CALLNO_TYPE_NORMAL, validated, &entry)) {
3236  /* since we ran out of space, remove the peercnt
3237  * entry we added earlier */
3238  peercnt_remove_by_addr(addr);
3239  ast_log(LOG_WARNING, "No more space\n");
3240  return 0;
3241  }
3242  x = CALLNO_ENTRY_GET_CALLNO(entry);
3243  ast_mutex_lock(&iaxsl[x]);
3244 
3245  iaxs[x] = new_iax(addr, host);
3246  if (iaxs[x]) {
3247  if (iaxdebug)
3248  ast_debug(1, "Creating new call structure %d\n", x);
3249  iaxs[x]->callno_entry = entry;
3250  iaxs[x]->sockfd = sockfd;
3251  ast_sockaddr_copy(&iaxs[x]->addr, addr);
3252  iaxs[x]->peercallno = callno;
3253  iaxs[x]->callno = x;
3254  iaxs[x]->pingtime = DEFAULT_RETRY_TIME;
3255  iaxs[x]->expiry = min_reg_expire;
3256  iaxs[x]->pingid = iax2_sched_add(sched, ping_time * 1000, send_ping, (void *)(long)x);
3257  iaxs[x]->lagid = iax2_sched_add(sched, lagrq_time * 1000, send_lagrq, (void *)(long)x);
3258  iaxs[x]->amaflags = amaflags;
3260  ast_string_field_set(iaxs[x], accountcode, accountcode);
3261  ast_string_field_set(iaxs[x], mohinterpret, mohinterpret);
3262  ast_string_field_set(iaxs[x], mohsuggest, mohsuggest);
3263  ast_string_field_set(iaxs[x], parkinglot, default_parkinglot);
3264 
3265  if (iaxs[x]->peercallno) {
3266  store_by_peercallno(iaxs[x]);
3267  }
3268  } else {
3269  ast_log(LOG_WARNING, "Out of resources\n");
3270  ast_mutex_unlock(&iaxsl[x]);
3271  replace_callno(CALLNO_ENTRY_TO_PTR(entry));
3272  return 0;
3273  }
3274  if (!return_locked)
3275  ast_mutex_unlock(&iaxsl[x]);
3276  res = x;
3277  }
3278  return res;
3279 }
3280 
3281 static int find_callno(unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int full_frame) {
3282  return __find_callno(callno, dcallno, addr, new, sockfd, 0, full_frame);
3283 }
3284 
3285 static int find_callno_locked(unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int full_frame) {
3286 
3287  return __find_callno(callno, dcallno, addr, new, sockfd, 1, full_frame);
3288 }
3289 
3290 /*!
3291  * \brief Queue a frame to a call's owning asterisk channel
3292  *
3293  * \pre This function assumes that iaxsl[callno] is locked when called.
3294  *
3295  * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3296  * was valid before calling it, it may no longer be valid after calling it.
3297  * This function may unlock and lock the mutex associated with this callno,
3298  * meaning that another thread may grab it and destroy the call.
3299  */
3300 static int iax2_queue_frame(int callno, struct ast_frame *f)
3301 {
3302  iax2_lock_owner(callno);
3303  if (iaxs[callno] && iaxs[callno]->owner) {
3304  ast_queue_frame(iaxs[callno]->owner, f);
3305  ast_channel_unlock(iaxs[callno]->owner);
3306  }
3307  return 0;
3308 }
3309 
3310 /*!
3311  * \brief Queue a hold frame on the ast_channel owner
3312  *
3313  * This function queues a hold frame on the owner of the IAX2 pvt struct that
3314  * is active for the given call number.
3315  *
3316  * \pre Assumes lock for callno is already held.
3317  *
3318  * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3319  * was valid before calling it, it may no longer be valid after calling it.
3320  * This function may unlock and lock the mutex associated with this callno,
3321  * meaning that another thread may grab it and destroy the call.
3322  */
3323 static int iax2_queue_hold(int callno, const char *musicclass)
3324 {
3325  iax2_lock_owner(callno);
3326  if (iaxs[callno] && iaxs[callno]->owner) {
3327  ast_queue_hold(iaxs[callno]->owner, musicclass);
3328  ast_channel_unlock(iaxs[callno]->owner);
3329  }
3330  return 0;
3331 }
3332 
3333 /*!
3334  * \brief Queue an unhold frame on the ast_channel owner
3335  *
3336  * This function queues an unhold frame on the owner of the IAX2 pvt struct that
3337  * is active for the given call number.
3338  *
3339  * \pre Assumes lock for callno is already held.
3340  *
3341  * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3342  * was valid before calling it, it may no longer be valid after calling it.
3343  * This function may unlock and lock the mutex associated with this callno,
3344  * meaning that another thread may grab it and destroy the call.
3345  */
3346 static int iax2_queue_unhold(int callno)
3347 {
3348  iax2_lock_owner(callno);
3349  if (iaxs[callno] && iaxs[callno]->owner) {
3350  ast_queue_unhold(iaxs[callno]->owner);
3351  ast_channel_unlock(iaxs[callno]->owner);
3352  }
3353  return 0;
3354 }
3355 
3356 /*!
3357  * \brief Queue a hangup frame on the ast_channel owner
3358  *
3359  * This function queues a hangup frame on the owner of the IAX2 pvt struct that
3360  * is active for the given call number.
3361  *
3362  * \pre Assumes lock for callno is already held.
3363  *
3364  * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3365  * was valid before calling it, it may no longer be valid after calling it.
3366  * This function may unlock and lock the mutex associated with this callno,
3367  * meaning that another thread may grab it and destroy the call.
3368  */
3369 static int iax2_queue_hangup(int callno)
3370 {
3371  iax2_lock_owner(callno);
3372  if (iaxs[callno] && iaxs[callno]->owner) {
3373  ast_queue_hangup(iaxs[callno]->owner);
3374  ast_channel_unlock(iaxs[callno]->owner);
3375  }
3376  return 0;
3377 }
3378 
3379 /*!
3380  * \note This function assumes that iaxsl[callno] is locked when called.
3381  *
3382  * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
3383  * was valid before calling it, it may no longer be valid after calling it.
3384  * This function calls iax2_queue_frame(), which may unlock and lock the mutex
3385  * associated with this callno, meaning that another thread may grab it and destroy the call.
3386  */
3387 static int __do_deliver(void *data)
3388 {
3389  /* Just deliver the packet by using queueing. This is called by
3390  the IAX thread with the iaxsl lock held. */
3391  struct iax_frame *fr = data;
3392  fr->retrans = -1;
3393  ast_clear_flag(&fr->af, AST_FRFLAG_HAS_TIMING_INFO);
3394  if (iaxs[fr->callno] && !ast_test_flag64(iaxs[fr->callno], IAX_ALREADYGONE))
3395  iax2_queue_frame(fr->callno, &fr->af);
3396  /* Free our iax frame */
3397  iax2_frame_free(fr);
3398  /* And don't run again */
3399  return 0;
3400 }
3401 
3402 static int handle_error(void)
3403 {
3404  /* XXX Ideally we should figure out why an error occurred and then abort those
3405  rather than continuing to try. Unfortunately, the published interface does
3406  not seem to work XXX */
3407 #if 0
3408  struct sockaddr_in *sin;
3409  int res;
3410  struct msghdr m;
3411  struct sock_extended_err e;
3412  m.msg_name = NULL;
3413  m.msg_namelen = 0;
3414  m.msg_iov = NULL;
3415  m.msg_control = &e;
3416  m.msg_controllen = sizeof(e);
3417  m.msg_flags = 0;
3418  res = recvmsg(netsocket, &m, MSG_ERRQUEUE);
3419  if (res < 0)
3420  ast_log(LOG_WARNING, "Error detected, but unable to read error: %s\n", strerror(errno));
3421  else {
3422  if (m.msg_controllen) {
3423  sin = (struct sockaddr_in *)SO_EE_OFFENDER(&e);
3424  if (sin)
3425  ast_log(LOG_WARNING, "Receive error from %s\n", ast_inet_ntoa(sin->sin_addr));
3426  else
3427  ast_log(LOG_WARNING, "No address detected??\n");
3428  } else {
3429  ast_log(LOG_WARNING, "Local error: %s\n", strerror(e.ee_errno));
3430  }
3431  }
3432 #endif
3433  return 0;
3434 }
3435 
3436 static int transmit_trunk(struct iax_frame *f, struct ast_sockaddr *addr, int sockfd)
3437 {
3438  int res;
3439  res = ast_sendto(sockfd, f->data, f->datalen, 0, addr);
3440 
3441  if (res < 0) {
3442  ast_debug(1, "Received error: %s\n", strerror(errno));
3443  handle_error();
3444  } else
3445  res = 0;
3446  return res;
3447 }
3448 
3449 static int send_packet(struct iax_frame *f)
3450 {
3451  int res;
3452  int callno = f->callno;
3453 
3454  /* Don't send if there was an error, but return error instead */
3455  if (!callno || !iaxs[callno] || iaxs[callno]->error)
3456  return -1;
3457 
3458  /* Called with iaxsl held */
3459  if (iaxdebug) {
3460  ast_debug(8, "Sending %u on %d/%d to %s\n", f->ts, callno, iaxs[callno]->peercallno, ast_sockaddr_stringify(&iaxs[callno]->addr));
3461  }
3462  if (f->transfer) {
3463  iax_outputframe(f, NULL, 0, &iaxs[callno]->transfer, f->datalen - sizeof(struct ast_iax2_full_hdr));
3464  res = ast_sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0, &iaxs[callno]->transfer);
3465  } else {
3466  iax_outputframe(f, NULL, 0, &iaxs[callno]->addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
3467  res = ast_sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0, &iaxs[callno]->addr);
3468  }
3469  if (res < 0) {
3470  if (iaxdebug)
3471  ast_debug(1, "Received error: %s\n", strerror(errno));
3472  handle_error();
3473  } else
3474  res = 0;
3475 
3476  return res;
3477 }
3478 
3479 /*!
3480  * \note Since this function calls iax2_queue_hangup(), the pvt struct
3481  * for the given call number may disappear during its execution.
3482  */
3483 static int iax2_predestroy(int callno)
3484 {
3485  struct ast_channel *c = NULL;
3486  struct chan_iax2_pvt *pvt = iaxs[callno];
3487 
3488  if (!pvt)
3489  return -1;
3490 
3491  if (!ast_test_flag64(pvt, IAX_ALREADYGONE)) {
3492  iax2_destroy_helper(pvt);
3493  ast_set_flag64(pvt, IAX_ALREADYGONE);
3494  }
3495 
3496  if ((c = pvt->owner)) {
3497  ast_channel_tech_pvt_set(c, NULL);
3498  iax2_queue_hangup(callno);
3499  pvt->owner = NULL;
3501  }
3502 
3503  return 0;
3504 }
3505 
3506 static void iax2_destroy(int callno)
3507 {
3508  struct chan_iax2_pvt *pvt = NULL;
3509  struct ast_channel *owner = NULL;
3510 
3511 retry:
3512  if ((pvt = iaxs[callno])) {
3513 #if 0
3514  /* iax2_destroy_helper gets called from this function later on. When
3515  * called twice, we get the (previously) familiar FRACK! errors in
3516  * devmode, from the scheduler. An alternative to this approach is to
3517  * reset the scheduler entries to -1 when they're deleted in
3518  * iax2_destroy_helper(). That approach was previously decided to be
3519  * "wrong" because "the memory is going to be deallocated anyway. Why
3520  * should we be resetting those values?" */
3521  iax2_destroy_helper(pvt);
3522 #endif
3523  }
3524 
3525  owner = pvt ? pvt->owner : NULL;
3526 
3527  if (owner) {
3528  if (ast_channel_trylock(owner)) {
3529  ast_debug(3, "Avoiding IAX destroy deadlock\n");
3530  DEADLOCK_AVOIDANCE(&iaxsl[callno]);
3531  goto retry;
3532  }
3533  }
3534 
3535  if (!owner) {
3536  iaxs[callno] = NULL;
3537  }
3538 
3539  if (pvt) {
3540  if (!owner) {
3541  pvt->owner = NULL;
3542  } else {
3543  /* If there's an owner, prod it to give up */
3544  /* It is ok to use ast_queue_hangup() here instead of iax2_queue_hangup()
3545  * because we already hold the owner channel lock. */
3546  ast_queue_hangup(owner);
3547  }
3548 
3549  if (pvt->peercallno) {
3550  remove_by_peercallno(pvt);
3551  }
3552 
3553  if (pvt->transfercallno) {
3554  remove_by_transfercallno(pvt);
3555  }
3556 
3557  if (!owner) {
3558  ao2_ref(pvt, -1);
3559  pvt = NULL;
3560  }
3561  }
3562 
3563  if (owner) {
3564  ast_channel_unlock(owner);
3565  }
3566 }
3567 
3568 static int update_packet(struct iax_frame *f)
3569 {
3570  /* Called with iaxsl lock held, and iaxs[callno] non-NULL */
3571  struct ast_iax2_full_hdr *fh = f->data;
3572  struct ast_frame af;
3573 
3574  /* if frame is encrypted. decrypt before updating it. */
3575  if (f->encmethods) {
3576  decode_frame(&f->mydcx, fh, &af, &f->datalen);
3577  }
3578  /* Mark this as a retransmission */
3579  fh->dcallno = ntohs(IAX_FLAG_RETRANS | f->dcallno);
3580  /* Update iseqno */
3581  f->iseqno = iaxs[f->callno]->iseqno;
3582  fh->iseqno = f->iseqno;
3583 
3584  /* Now re-encrypt the frame */
3585  if (f->encmethods) {
3586  /* since this is a retransmit frame, create a new random padding
3587  * before re-encrypting. */
3588  build_rand_pad(f->semirand, sizeof(f->semirand));
3589  encrypt_frame(&f->ecx, fh, f->semirand, &f->datalen);
3590  }
3591  return 0;
3592 }
3593 
3594 static int attempt_transmit(const void *data);
3595 static void __attempt_transmit(const void *data)
3596 {
3597  /* Attempt to transmit the frame to the remote peer...
3598  Called without iaxsl held. */
3599  struct iax_frame *f = (struct iax_frame *)data;
3600  int freeme = 0;
3601  int callno = f->callno;
3602 
3603  /* Make sure this call is still active */
3604  if (callno)
3605  ast_mutex_lock(&iaxsl[callno]);
3606  if (callno && iaxs[callno]) {
3607  if (f->retries < 0) {
3608  /* Already ACK'd */
3609  freeme = 1;
3610  } else if (f->retries >= max_retries) {
3611  /* Too many attempts. Record an error. */
3612  if (f->transfer) {
3613  /* Transfer timeout */
3614  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
3615  } else if (f->final) {
3616  iax2_destroy(callno);
3617  } else {
3618  if (iaxs[callno]->owner) {
3619  ast_log(LOG_WARNING, "Max retries exceeded to host %s on %s (type = %u, subclass = %d, ts=%u, seqno=%d)\n",
3621  ast_channel_name(iaxs[f->callno]->owner),
3622  f->af.frametype,
3623  f->af.subclass.integer,
3624  f->ts,
3625  f->oseqno);
3626  }
3627  iaxs[callno]->error = ETIMEDOUT;
3628  if (iaxs[callno]->owner) {
3629  struct ast_frame fr = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = AST_CAUSE_DESTINATION_OUT_OF_ORDER };
3630  /* Hangup the fd */
3631  iax2_queue_frame(callno, &fr); /* XXX */
3632  /* Remember, owner could disappear */
3633  if (iaxs[callno] && iaxs[callno]->owner)
3634  ast_channel_hangupcause_set(iaxs[callno]->owner, AST_CAUSE_DESTINATION_OUT_OF_ORDER);
3635  } else {
3636  if (iaxs[callno]->reg) {
3637  memset(&iaxs[callno]->reg->us, 0, sizeof(iaxs[callno]->reg->us));
3638  iaxs[callno]->reg->regstate = REG_STATE_TIMEOUT;
3639  iaxs[callno]->reg->refresh = IAX_DEFAULT_REG_EXPIRE;
3640  }
3641  iax2_destroy(callno);
3642  }
3643  }
3644  freeme = 1;
3645  } else {
3646  /* Update it if it needs it */
3647  update_packet(f);
3648  /* Attempt transmission */
3649  send_packet(f);
3650  f->retries++;
3651  /* Try again later after 10 times as long */
3652  f->retrytime *= 10;
3653  if (f->retrytime > MAX_RETRY_TIME)
3654  f->retrytime = MAX_RETRY_TIME;
3655  /* Transfer messages max out at one second */
3656  if (f->transfer && (f->retrytime > 1000))
3657  f->retrytime = 1000;
3658  f->retrans = iax2_sched_add(sched, f->retrytime, attempt_transmit, f);
3659  }
3660  } else {
3661  /* Make sure it gets freed */
3662  f->retries = -1;
3663  freeme = 1;
3664  }
3665 
3666  if (freeme) {
3667  /* Don't attempt delivery, just remove it from the queue */
3668  AST_LIST_REMOVE(&frame_queue[callno], f, list);
3669  ast_mutex_unlock(&iaxsl[callno]);
3670  f->retrans = -1; /* this is safe because this is the scheduled function */
3671  /* Free the IAX frame */
3672  iax2_frame_free(f);
3673  } else if (callno) {
3674  ast_mutex_unlock(&iaxsl[callno]);
3675  }
3676 }
3677 
3678 static int attempt_transmit(const void *data)
3679 {
3680 #ifdef SCHED_MULTITHREADED
3681  if (schedule_action(__attempt_transmit, data))
3682 #endif
3683  __attempt_transmit(data);
3684  return 0;
3685 }
3686 
3687 static char *handle_cli_iax2_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3688 {
3689  struct iax2_peer *peer = NULL;
3690  struct iax2_user *user = NULL;
3691  static const char * const choices[] = { "all", NULL };
3692  char *cmplt;
3693 
3694  switch (cmd) {
3695  case CLI_INIT:
3696  e->command = "iax2 prune realtime";
3697  e->usage =
3698  "Usage: iax2 prune realtime [<peername>|all]\n"
3699  " Prunes object(s) from the cache\n";
3700  return NULL;
3701  case CLI_GENERATE:
3702  if (a->pos == 3) {
3703  cmplt = ast_cli_complete(a->word, choices, a->n);
3704  if (!cmplt)
3705  cmplt = complete_iax2_peers(a->line, a->word, a->pos, a->n - sizeof(choices), IAX_RTCACHEFRIENDS);
3706  return cmplt;
3707  }
3708  return NULL;
3709  }
3710  if (a->argc != 4)
3711  return CLI_SHOWUSAGE;
3712  if (!strcmp(a->argv[3], "all")) {
3713  prune_users();
3714  prune_peers();
3715  ast_cli(a->fd, "Cache flushed successfully.\n");
3716  return CLI_SUCCESS;
3717  }
3718  peer = find_peer(a->argv[3], 0);
3719  user = find_user(a->argv[3]);
3720  if (peer || user) {
3721  if (peer) {
3722  if (ast_test_flag64(peer, IAX_RTCACHEFRIENDS)) {
3723  ast_set_flag64(peer, IAX_RTAUTOCLEAR);
3724  expire_registry(peer_ref(peer));
3725  ast_cli(a->fd, "Peer %s was removed from the cache.\n", a->argv[3]);
3726  } else {
3727  ast_cli(a->fd, "Peer %s is not eligible for this operation.\n", a->argv[3]);
3728  }
3729  peer_unref(peer);
3730  }
3731  if (user) {
3732  if (ast_test_flag64(user, IAX_RTCACHEFRIENDS)) {
3733  ast_set_flag64(user, IAX_RTAUTOCLEAR);
3734  ast_cli(a->fd, "User %s was removed from the cache.\n", a->argv[3]);
3735  } else {
3736  ast_cli(a->fd, "User %s is not eligible for this operation.\n", a->argv[3]);
3737  }
3738  ao2_unlink(users,user);
3739  user_unref(user);
3740  }
3741  } else {
3742  ast_cli(a->fd, "%s was not found in the cache.\n", a->argv[3]);
3743  }
3744 
3745  return CLI_SUCCESS;
3746 }
3747 
3748 static char *handle_cli_iax2_test_losspct(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3749 {
3750  switch (cmd) {
3751  case CLI_INIT:
3752  e->command = "iax2 test losspct";
3753  e->usage =
3754  "Usage: iax2 test losspct <percentage>\n"
3755  " For testing, throws away <percentage> percent of incoming packets\n";
3756  return NULL;
3757  case CLI_GENERATE:
3758  return NULL;
3759  }
3760  if (a->argc != 4)
3761  return CLI_SHOWUSAGE;
3762 
3763  test_losspct = atoi(a->argv[3]);
3764 
3765  return CLI_SUCCESS;
3766 }
3767 
3768 #ifdef IAXTESTS
3769 static char *handle_cli_iax2_test_late(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3770 {
3771  switch (cmd) {
3772  case CLI_INIT:
3773  e->command = "iax2 test late";
3774  e->usage =
3775  "Usage: iax2 test late <ms>\n"
3776  " For testing, count the next frame as <ms> ms late\n";
3777  return NULL;
3778  case CLI_GENERATE:
3779  return NULL;
3780  }
3781 
3782  if (a->argc != 4)
3783  return CLI_SHOWUSAGE;
3784 
3785  test_late = atoi(a->argv[3]);
3786 
3787  return CLI_SUCCESS;
3788 }
3789 
3790 static char *handle_cli_iax2_test_resync(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3791 {
3792  switch (cmd) {
3793  case CLI_INIT:
3794  e->command = "iax2 test resync";
3795  e->usage =
3796  "Usage: iax2 test resync <ms>\n"
3797  " For testing, adjust all future frames by <ms> ms\n";
3798  return NULL;
3799  case CLI_GENERATE:
3800  return NULL;
3801  }
3802 
3803  if (a->argc != 4)
3804  return CLI_SHOWUSAGE;
3805 
3806  test_resync = atoi(a->argv[3]);
3807 
3808  return CLI_SUCCESS;
3809 }
3810 
3811 static char *handle_cli_iax2_test_jitter(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3812 {
3813  switch (cmd) {
3814  case CLI_INIT:
3815  e->command = "iax2 test jitter";
3816  e->usage =
3817  "Usage: iax2 test jitter <ms> <pct>\n"
3818  " For testing, simulate maximum jitter of +/- <ms> on <pct>\n"
3819  " percentage of packets. If <pct> is not specified, adds\n"
3820  " jitter to all packets.\n";
3821  return NULL;
3822  case CLI_GENERATE:
3823  return NULL;
3824  }
3825 
3826  if (a->argc < 4 || a->argc > 5)
3827  return CLI_SHOWUSAGE;
3828 
3829  test_jit = atoi(a->argv[3]);
3830  if (a->argc == 5)
3831  test_jitpct = atoi(a->argv[4]);
3832 
3833  return CLI_SUCCESS;
3834 }
3835 #endif /* IAXTESTS */
3836 
3837 /*! \brief peer_status: Report Peer status in character string */
3838 /* returns 1 if peer is online, -1 if unmonitored */
3839 static int peer_status(struct iax2_peer *peer, char *status, int statuslen)
3840 {
3841  int res = 0;
3842  if (peer->maxms) {
3843  if (peer->lastms < 0) {
3844  ast_copy_string(status, "UNREACHABLE", statuslen);
3845  } else if (peer->lastms > peer->maxms) {
3846  snprintf(status, statuslen, "LAGGED (%d ms)", peer->lastms);
3847  res = 1;
3848  } else if (peer->lastms) {
3849  snprintf(status, statuslen, "OK (%d ms)", peer->lastms);
3850  res = 1;
3851  } else {
3852  ast_copy_string(status, "UNKNOWN", statuslen);
3853  }
3854  } else {
3855  ast_copy_string(status, "Unmonitored", statuslen);
3856  res = -1;
3857  }
3858  return res;
3859 }
3860 
3861 /*! \brief Show one peer in detail */
3862 static char *handle_cli_iax2_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3863 {
3864  char status[64];
3865  char cbuf[256];
3866  struct iax2_peer *peer;
3867  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
3868  struct ast_str *encmethods = ast_str_alloca(256);
3869  int load_realtime = 0;
3870 
3871  switch (cmd) {
3872  case CLI_INIT:
3873  e->command = "iax2 show peer";
3874  e->usage =
3875  "Usage: iax2 show peer <name>\n"
3876  " Display details on specific IAX peer\n";
3877  return NULL;
3878  case CLI_GENERATE:
3879  if (a->pos == 3)
3880  return complete_iax2_peers(a->line, a->word, a->pos, a->n, 0);
3881  return NULL;
3882  }
3883 
3884  if (a->argc < 4)
3885  return CLI_SHOWUSAGE;
3886 
3887  load_realtime = (a->argc == 5 && !strcmp(a->argv[4], "load")) ? 1 : 0;
3888 
3889  peer = find_peer(a->argv[3], load_realtime);
3890  if (peer) {
3891  char *str_addr, *str_defaddr;
3892  char *str_port, *str_defport;
3893 
3894  str_addr = ast_strdupa(ast_sockaddr_stringify_addr(&peer->addr));
3895  str_port = ast_strdupa(ast_sockaddr_stringify_port(&peer->addr));
3896  str_defaddr = ast_strdupa(ast_sockaddr_stringify_addr(&peer->defaddr));
3897  str_defport = ast_strdupa(ast_sockaddr_stringify_port(&peer->defaddr));
3898 
3899  encmethods_to_str(peer->encmethods, &encmethods);
3900  ast_cli(a->fd, "\n\n");
3901  ast_cli(a->fd, " * Name : %s\n", peer->name);
3902  ast_cli(a->fd, " Description : %s\n", peer->description);
3903  ast_cli(a->fd, " Secret : %s\n", ast_strlen_zero(peer->secret) ? "<Not set>" : "<Set>");
3904  ast_cli(a->fd, " Context : %s\n", peer->context);
3905  ast_cli(a->fd, " Parking lot : %s\n", peer->parkinglot);
3906  ast_cli(a->fd, " Mailbox : %s\n", peer->mailbox);
3907  ast_cli(a->fd, " Dynamic : %s\n", ast_test_flag64(peer, IAX_DYNAMIC) ? "Yes" : "No");
3908  ast_cli(a->fd, " Callnum limit: %d\n", peer->maxcallno);
3909  ast_cli(a->fd, " Calltoken req: %s\n", (peer->calltoken_required == CALLTOKEN_YES) ? "Yes" : ((peer->calltoken_required == CALLTOKEN_AUTO) ? "Auto" : "No"));
3910  ast_cli(a->fd, " Trunk : %s\n", ast_test_flag64(peer, IAX_TRUNK) ? "Yes" : "No");
3911  ast_cli(a->fd, " Encryption : %s\n", peer->encmethods ? ast_str_buffer(encmethods) : "No");
3912  ast_cli(a->fd, " Callerid : %s\n", ast_callerid_merge(cbuf, sizeof(cbuf), peer->cid_name, peer->cid_num, "<unspecified>"));
3913  ast_cli(a->fd, " Expire : %d\n", peer->expire);
3914  ast_cli(a->fd, " ACL : %s\n", (ast_acl_list_is_empty(peer->acl) ? "No" : "Yes"));
3915  ast_cli(a->fd, " Addr->IP : %s Port %s\n", str_addr ? str_addr : "(Unspecified)", str_port);
3916  ast_cli(a->fd, " Defaddr->IP : %s Port %s\n", str_defaddr, str_defport);
3917  ast_cli(a->fd, " Username : %s\n", peer->username);
3918  ast_cli(a->fd, " Codecs : %s\n", iax2_getformatname_multiple(peer->capability, &codec_buf));
3919 
3920  if (iax2_codec_pref_string(&peer->prefs, cbuf, sizeof(cbuf)) < 0) {
3921  strcpy(cbuf, "Error"); /* Safe */
3922  }
3923  ast_cli(a->fd, " Codec Order : %s\n", cbuf);
3924 
3925  peer_status(peer, status, sizeof(status));
3926  ast_cli(a->fd, " Status : %s\n", status);
3927  ast_cli(a->fd, " Qualify : every %dms when OK, every %dms when UNREACHABLE (sample smoothing %s)\n", peer->pokefreqok, peer->pokefreqnotok, peer->smoothing ? "On" : "Off");
3928  ast_cli(a->fd, "\n");
3929  peer_unref(peer);
3930  } else {
3931  ast_cli(a->fd, "Peer %s not found.\n", a->argv[3]);
3932  ast_cli(a->fd, "\n");
3933  }
3934 
3935  return CLI_SUCCESS;
3936 }
3937 
3938 static char *complete_iax2_peers(const char *line, const char *word, int pos, int state, uint64_t flags)
3939 {
3940  int which = 0;
3941  struct iax2_peer *peer;
3942  char *res = NULL;
3943  int wordlen = strlen(word);
3944  struct ao2_iterator i;
3945 
3946  i = ao2_iterator_init(peers, 0);
3947  while ((peer = ao2_iterator_next(&i))) {
3948  if (!strncasecmp(peer->name, word, wordlen) && ++which > state
3949  && (!flags || ast_test_flag64(peer, flags))) {
3950  res = ast_strdup(peer->name);
3951  peer_unref(peer);
3952  break;
3953  }
3954  peer_unref(peer);
3955  }
3957 
3958  return res;
3959 }
3960 
3961 static char *handle_cli_iax2_show_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3962 {
3963  struct iax_frame *cur;
3964  int cnt = 0, dead = 0, final = 0, i = 0;
3965 
3966  switch (cmd) {
3967  case CLI_INIT:
3968  e->command = "iax2 show stats";
3969  e->usage =
3970  "Usage: iax2 show stats\n"
3971  " Display statistics on IAX channel driver.\n";
3972  return NULL;
3973  case CLI_GENERATE:
3974  return NULL;
3975  }
3976 
3977  if (a->argc != 3)
3978  return CLI_SHOWUSAGE;
3979 
3980  for (i = 0; i < ARRAY_LEN(frame_queue); i++) {
3981  ast_mutex_lock(&iaxsl[i]);
3982  AST_LIST_TRAVERSE(&frame_queue[i], cur, list) {
3983  if (cur->retries < 0)
3984  dead++;
3985  if (cur->final)
3986  final++;
3987  cnt++;
3988  }
3989  ast_mutex_unlock(&iaxsl[i]);
3990  }
3991 
3992  ast_cli(a->fd, " IAX Statistics\n");
3993  ast_cli(a->fd, "---------------------\n");
3994  ast_cli(a->fd, "Outstanding frames: %d (%d ingress, %d egress)\n", iax_get_frames(), iax_get_iframes(), iax_get_oframes());
3995  ast_cli(a->fd, "%d timed and %d untimed transmits; MTU %d/%d/%d\n", trunk_timed, trunk_untimed,
3996  trunk_maxmtu, trunk_nmaxmtu, global_max_trunk_mtu);
3997  ast_cli(a->fd, "Packets in transmit queue: %d dead, %d final, %d total\n\n", dead, final, cnt);
3998 
3999  trunk_timed = trunk_untimed = 0;
4000  if (trunk_maxmtu > trunk_nmaxmtu)
4001  trunk_nmaxmtu = trunk_maxmtu;
4002 
4003  return CLI_SUCCESS;
4004 }
4005 
4006 /*! \brief Set trunk MTU from CLI */
4007 static char *handle_cli_iax2_set_mtu(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
4008 {
4009  int mtuv;
4010 
4011  switch (cmd) {
4012  case CLI_INIT:
4013  e->command = "iax2 set mtu";
4014  e->usage =
4015  "Usage: iax2 set mtu <value>\n"
4016  " Set the system-wide IAX IP mtu to <value> bytes net or\n"
4017  " zero to disable. Disabling means that the operating system\n"
4018  " must handle fragmentation of UDP packets when the IAX2 trunk\n"
4019  " packet exceeds the UDP payload size. This is substantially\n"
4020  " below the IP mtu. Try 1240 on ethernets. Must be 172 or\n"
4021  " greater for G.711 samples.\n";
4022  return NULL;
4023  case CLI_GENERATE:
4024  return NULL;
4025  }
4026 
4027  if (a->argc != 4)
4028  return CLI_SHOWUSAGE;
4029  if (strncasecmp(a->argv[3], "default", strlen(a->argv[3])) == 0)
4030  mtuv = MAX_TRUNK_MTU;
4031  else
4032  mtuv = atoi(a->argv[3]);
4033 
4034  if (mtuv == 0) {
4035  ast_cli(a->fd, "Trunk MTU control disabled (mtu was %d)\n", global_max_trunk_mtu);
4037  return CLI_SUCCESS;
4038  }
4039  if (mtuv < 172 || mtuv > 4000) {
4040  ast_cli(a->fd, "Trunk MTU must be between 172 and 4000\n");
4041  return CLI_SHOWUSAGE;
4042  }
4043  ast_cli(a->fd, "Trunk MTU changed from %d to %d\n", global_max_trunk_mtu, mtuv);
4044  global_max_trunk_mtu = mtuv;
4045  return CLI_SUCCESS;
4046 }
4047 
4048 static char *handle_cli_iax2_show_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
4049 {
4050  struct iax2_dpcache *dp = NULL;
4051  char tmp[1024], *pc = NULL;
4052  int s, x, y;
4053  struct timeval now = ast_tvnow();
4054 
4055  switch (cmd) {
4056  case CLI_INIT:
4057  e->command = "iax2 show cache";
4058  e->usage =
4059  "Usage: iax2 show cache\n"
4060  " Display currently cached IAX Dialplan results.\n";
4061  return NULL;
4062  case CLI_GENERATE:
4063  return NULL;
4064  }
4065 
4067 
4068  ast_cli(a->fd, "%-20.20s %-12.12s %-9.9s %-8.8s %s\n", "Peer/Context", "Exten", "Exp.", "Wait.", "Flags");
4069 
4070  AST_LIST_TRAVERSE(&dpcache, dp, cache_list) {
4071  s = dp->expiry.tv_sec - now.tv_sec;
4072  tmp[0] = '\0';
4073  if (dp->flags & CACHE_FLAG_EXISTS)
4074  strncat(tmp, "EXISTS|", sizeof(tmp) - strlen(tmp) - 1);
4075  if (dp->flags & CACHE_FLAG_NONEXISTENT)
4076  strncat(tmp, "NONEXISTENT|", sizeof(tmp) - strlen(tmp) - 1);
4077  if (dp->flags & CACHE_FLAG_CANEXIST)
4078  strncat(tmp, "CANEXIST|", sizeof(tmp) - strlen(tmp) - 1);
4079  if (dp->flags & CACHE_FLAG_PENDING)
4080  strncat(tmp, "PENDING|", sizeof(tmp) - strlen(tmp) - 1);
4081  if (dp->flags & CACHE_FLAG_TIMEOUT)
4082  strncat(tmp, "TIMEOUT|", sizeof(tmp) - strlen(tmp) - 1);
4083  if (dp->flags & CACHE_FLAG_TRANSMITTED)
4084  strncat(tmp, "TRANSMITTED|", sizeof(tmp) - strlen(tmp) - 1);
4085  if (dp->flags & CACHE_FLAG_MATCHMORE)
4086  strncat(tmp, "MATCHMORE|", sizeof(tmp) - strlen(tmp) - 1);
4087  if (dp->flags & CACHE_FLAG_UNKNOWN)
4088  strncat(tmp, "UNKNOWN|", sizeof(tmp) - strlen(tmp) - 1);
4089  /* Trim trailing pipe */
4090  if (!ast_strlen_zero(tmp)) {
4091  tmp[strlen(tmp) - 1] = '\0';
4092  } else {
4093  ast_copy_string(tmp, "(none)", sizeof(tmp));
4094  }
4095  y = 0;
4096  pc = strchr(dp->peercontext, '@');
4097  if (!pc) {
4098  pc = dp->peercontext;
4099  } else {
4100  pc++;
4101  }
4102  for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
4103  if (dp->waiters[x] > -1)
4104  y++;
4105  }
4106  if (s > 0) {
4107  ast_cli(a->fd, "%-20.20s %-12.12s %-9d %-8d %s\n", pc, dp->exten, s, y, tmp);
4108  } else {
4109  ast_cli(a->fd, "%-20.20s %-12.12s %-9.9s %-8d %s\n", pc, dp->exten, "(expired)", y, tmp);
4110  }
4111  }
4112 
4114 
4115  return CLI_SUCCESS;
4116 }
4117 
4118 static unsigned int calc_rxstamp(struct chan_iax2_pvt *p, unsigned int offset);
4119 
4120 static void unwrap_timestamp(struct iax_frame *fr)
4121 {
4122  /* Video mini frames only encode the lower 15 bits of the session
4123  * timestamp, but other frame types (e.g. audio) encode 16 bits. */
4124  const int ts_shift = (fr->af.frametype == AST_FRAME_VIDEO) ? 15 : 16;
4125  const int lower_mask = (1 << ts_shift) - 1;
4126  const int upper_mask = ~lower_mask;
4127  const int last_upper = iaxs[fr->callno]->last & upper_mask;
4128 
4129  if ( (fr->ts & upper_mask) == last_upper ) {
4130  const int x = fr->ts - iaxs[fr->callno]->last;
4131  const int threshold = (ts_shift == 15) ? 25000 : 50000;
4132 
4133  if (x < -threshold) {
4134  /* Sudden big jump backwards in timestamp:
4135  What likely happened here is that miniframe timestamp has circled but we haven't
4136  gotten the update from the main packet. We'll just pretend that we did, and
4137  update the timestamp appropriately. */
4138  fr->ts = (last_upper + (1 << ts_shift)) | (fr->ts & lower_mask);
4139  if (iaxdebug)
4140  ast_debug(1, "schedule_delivery: pushed forward timestamp\n");
4141  } else if (x > threshold) {
4142  /* Sudden apparent big jump forwards in timestamp:
4143  What's likely happened is this is an old miniframe belonging to the previous
4144  top 15 or 16-bit timestamp that has turned up out of order.
4145  Adjust the timestamp appropriately. */
4146  fr->ts = (last_upper - (1 << ts_shift)) | (fr->ts & lower_mask);
4147  if (iaxdebug)
4148  ast_debug(1, "schedule_delivery: pushed back timestamp\n");
4149  }
4150  }
4151 }
4152 
4153 static int get_from_jb(const void *p);
4154 
4155 static void update_jbsched(struct chan_iax2_pvt *pvt)
4156 {
4157  int when;
4158 
4159  when = ast_tvdiff_ms(ast_tvnow(), pvt->rxcore);
4160 
4161  when = jb_next(pvt->jb) - when;
4162 
4163  if (when <= 0) {
4164  /* XXX should really just empty until when > 0.. */
4165  when = 1;
4166  }
4167 
4168  pvt->jbid = iax2_sched_replace(pvt->jbid, sched, when, get_from_jb,
4169  CALLNO_TO_PTR(pvt->callno));
4170 }
4171 
4172 static void __get_from_jb(const void *p)
4173 {
4174  int callno = PTR_TO_CALLNO(p);
4175  struct chan_iax2_pvt *pvt = NULL;
4176  struct iax_frame *fr;
4177  jb_frame frame;
4178  int ret;
4179  long ms;
4180  long next;
4181  struct timeval now = ast_tvnow();
4182  struct ast_format *voicefmt;
4183 
4184  /* Make sure we have a valid private structure before going on */
4185  ast_mutex_lock(&iaxsl[callno]);
4186  pvt = iaxs[callno];
4187  if (!pvt) {
4188  /* No go! */
4189  ast_mutex_unlock(&iaxsl[callno]);
4190  return;
4191  }
4192 
4193  pvt->jbid = -1;
4194 
4195  /* round up a millisecond since ast_sched_runq does; */
4196  /* prevents us from spinning while waiting for our now */
4197  /* to catch up with runq's now */
4198  now.tv_usec += 1000;
4199 
4200  ms = ast_tvdiff_ms(now, pvt->rxcore);
4201  if (ms >= (next = jb_next(pvt->jb))) {
4203  if (!voicefmt) {
4204  /* pvt->voiceformat won't be set if we haven't received any voice frames yet.
4205  * In this case, fall back to using the format negotiated during call setup,
4206  * so we don't stall the jitterbuffer completely. */
4208  }
4209  if (!voicefmt) {
4210  /* Really shouldn't happen, but if it does, should be looked into */
4211  ast_log(LOG_WARNING, "No voice format and no peer format available on %s, backlogging frame\n", ast_channel_name(pvt->owner));
4212  goto cleanup; /* Don't crash if there's no voice format */
4213  }
4214  ret = jb_get(pvt->jb, &frame, ms, ast_format_get_default_ms(voicefmt));
4215  switch(ret) {
4216  case JB_OK:
4217  fr = frame.data;
4218  __do_deliver(fr);
4219  /* __do_deliver() can cause the call to disappear */
4220  pvt = iaxs[callno];
4221  break;
4222  case JB_INTERP:
4223  {
4224  struct ast_frame af = { 0, };
4225 
4226  /* create an interpolation frame */
4228  af.subclass.format = voicefmt;
4229  af.samples = frame.ms * (ast_format_get_sample_rate(voicefmt) / 1000);
4230  af.src = "IAX2 JB interpolation";
4231  af.delivery = ast_tvadd(pvt->rxcore, ast_samp2tv(next, 1000));
4233 
4234  /* queue the frame: For consistency, we would call __do_deliver here, but __do_deliver wants an iax_frame,
4235  * which we'd need to malloc, and then it would free it. That seems like a drag */
4236  if (!ast_test_flag64(iaxs[callno], IAX_ALREADYGONE)) {
4237  iax2_queue_frame(callno, &af);
4238  /* iax2_queue_frame() could cause the call to disappear */
4239  pvt = iaxs[callno];
4240  }
4241  }
4242  break;
4243  case JB_DROP:
4244  iax2_frame_free(frame.data);
4245  break;
4246  case JB_NOFRAME:
4247  case JB_EMPTY:
4248  /* do nothing */
4249  break;
4250  default:
4251  /* shouldn't happen */
4252  break;
4253  }
4254  }
4255 cleanup:
4256  if (pvt)
4257  update_jbsched(pvt);
4258  ast_mutex_unlock(&iaxsl[callno]);
4259 }
4260 
4261 static int get_from_jb(const void *data)
4262 {
4263 #ifdef SCHED_MULTITHREADED
4264  if (schedule_action(__get_from_jb, data))
4265 #endif
4266  __get_from_jb(data);
4267  return 0;
4268 }
4269 
4270 /*!
4271  * \note This function assumes fr->callno is locked
4272  *
4273  * \note IMPORTANT NOTE!!! Any time this function is used, even if iaxs[callno]
4274  * was valid before calling it, it may no longer be valid after calling it.
4275  */
4276 static int schedule_delivery(struct iax_frame *fr, int updatehistory, int fromtrunk, unsigned int *tsout)
4277 {
4278  int type, len;
4279  int ret;
4280  int needfree = 0;
4281 
4282  /*
4283  * Clear fr->af.data if there is no data in the buffer. Things
4284  * like AST_CONTROL_HOLD without a suggested music class must
4285  * have a NULL pointer.
4286  */
4287  if (!fr->af.datalen) {
4288  memset(&fr->af.data, 0, sizeof(fr->af.data));
4289  }
4290 
4291  /* Attempt to recover wrapped timestamps */
4292  unwrap_timestamp(fr);
4293 
4294  /* delivery time is sender's sent timestamp converted back into absolute time according to our clock */
4295  if ( !fromtrunk && !ast_tvzero(iaxs[fr->callno]->rxcore))
4296  fr->af.delivery = ast_tvadd(iaxs[fr->callno]->rxcore, ast_samp2tv(fr->ts, 1000));
4297  else {
4298 #if 0
4299  ast_debug(1, "schedule_delivery: set delivery to 0 as we don't have an rxcore yet, or frame is from trunk.\n");
4300 #endif
4301  fr->af.delivery = ast_tv(0,0);
4302  }
4303 
4304  type = JB_TYPE_CONTROL;
4305  len = 0;
4306 
4307  if(fr->af.frametype == AST_FRAME_VOICE) {
4308  type = JB_TYPE_VOICE;
4310  } else if(fr->af.frametype == AST_FRAME_CNG) {
4311  type = JB_TYPE_SILENCE;
4312  }
4313 
4314  if ( (!ast_test_flag64(iaxs[fr->callno], IAX_USEJITTERBUF)) ) {
4315  if (tsout)
4316  *tsout = fr->ts;
4317  __do_deliver(fr);
4318  return -1;
4319  }
4320 
4321  /* insert into jitterbuffer */
4322  /* TODO: Perhaps we could act immediately if it's not droppable and late */
4323  ret = jb_put(iaxs[fr->callno]->jb, fr, type, len, fr->ts,
4324  calc_rxstamp(iaxs[fr->callno],fr->ts));
4325  if (ret == JB_DROP) {
4326  needfree++;
4327  } else if (ret == JB_SCHED) {
4328  update_jbsched(iaxs[fr->callno]);
4329  }
4330  if (tsout)
4331  *tsout = fr->ts;
4332  if (needfree) {
4333  /* Free our iax frame */
4334  iax2_frame_free(fr);
4335  return -1;
4336  }
4337  return 0;
4338 }
4339 
4340 static int transmit_frame(void *data)
4341 {
4342  struct iax_frame *fr = data;
4343 
4344  ast_mutex_lock(&iaxsl[fr->callno]);
4345 
4346  fr->sentyet = 1;
4347 
4348  if (iaxs[fr->callno]) {
4349  send_packet(fr);
4350  }
4351 
4352  if (fr->retries < 0) {
4353  ast_mutex_unlock(&iaxsl[fr->callno]);
4354  /* No retransmit requested */
4355  iax_frame_free(fr);
4356  } else {
4357  /* We need reliable delivery. Schedule a retransmission */
4358  AST_LIST_INSERT_TAIL(&frame_queue[fr->callno], fr, list);
4359  fr->retries++;
4360  fr->retrans = iax2_sched_add(sched, fr->retrytime, attempt_transmit, fr);
4361  ast_mutex_unlock(&iaxsl[fr->callno]);
4362  }
4363 
4364  return 0;
4365 }
4366 
4367 static int iax2_transmit(struct iax_frame *fr)
4368 {
4369  fr->sentyet = 0;
4370 
4371  return ast_taskprocessor_push(transmit_processor, transmit_frame, fr);
4372 }
4373 
4374 static int iax2_digit_begin(struct ast_channel *c, char digit)
4375 {
4376  return send_command_locked(PTR_TO_CALLNO(ast_channel_tech_pvt(c)), AST_FRAME_DTMF_BEGIN, digit, 0, NULL, 0, -1);
4377 }
4378 
4379 static int iax2_digit_end(struct ast_channel *c, char digit, unsigned int duration)
4380 {
4381  return send_command_locked(PTR_TO_CALLNO(ast_channel_tech_pvt(c)), AST_FRAME_DTMF_END, digit, 0, NULL, 0, -1);
4382 }
4383 
4384 static int iax2_sendtext(struct ast_channel *c, const char *text)
4385 {
4386 
4387  return send_command_locked(PTR_TO_CALLNO(ast_channel_tech_pvt(c)), AST_FRAME_TEXT,
4388  0, 0, (unsigned char *)text, strlen(text) + 1, -1);
4389 }
4390 
4391 static int iax2_sendimage(struct ast_channel *c, struct ast_frame *img)
4392 {
4393  return send_command_locked(PTR_TO_CALLNO(ast_channel_tech_pvt(c)), AST_FRAME_IMAGE, img->subclass.integer, 0, img->data.ptr, img->datalen, -1);
4394 }
4395 
4396 static int iax2_sendhtml(struct ast_channel *c, int subclass, const char *data, int datalen)
4397 {
4398  return send_command_locked(PTR_TO_CALLNO(ast_channel_tech_pvt(c)), AST_FRAME_HTML, subclass, 0, (unsigned char *)data, datalen, -1);
4399 }
4400 
4401 static int iax2_fixup(struct ast_channel *oldchannel, struct ast_channel *newchan)
4402 {
4403  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(newchan));
4404  ast_mutex_lock(&iaxsl[callno]);
4405  if (iaxs[callno])
4406  iaxs[callno]->owner = newchan;
4407  else
4408  ast_log(LOG_WARNING, "Uh, this isn't a good sign...\n");
4409  ast_mutex_unlock(&iaxsl[callno]);
4410  return 0;
4411 }
4412 
4413 /*!
4414  * \note This function calls reg_source_db -> iax2_poke_peer -> find_callno,
4415  * so do not call this with a pvt lock held.
4416  */
4417 static struct iax2_peer *realtime_peer(const char *peername, struct ast_sockaddr *addr)
4418 {
4419  struct ast_variable *var = NULL;
4420  struct ast_variable *tmp;
4421  struct iax2_peer *peer=NULL;
4422  time_t regseconds = 0, nowtime;
4423  int dynamic=0;
4424  char *str_addr, *str_port;
4425 
4426  str_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
4427  str_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
4428 
4429  if (peername) {
4430  var = ast_load_realtime("iaxpeers", "name", peername, "host", "dynamic", SENTINEL);
4431  if (!var && !ast_sockaddr_isnull(addr)) {
4432  var = ast_load_realtime("iaxpeers", "name", peername, "host", str_addr, SENTINEL);
4433  }
4434  } else if (!ast_sockaddr_isnull(addr)) {
4435  var = ast_load_realtime("iaxpeers", "ipaddr", str_addr, "port", str_port, SENTINEL);
4436  if (var) {
4437  /* We'll need the peer name in order to build the structure! */
4438  for (tmp = var; tmp; tmp = tmp->next) {
4439  if (!strcasecmp(tmp->name, "name"))
4440  peername = tmp->value;
4441  }
4442  }
4443  }
4444  if (!var && peername) { /* Last ditch effort */
4445  var = ast_load_realtime("iaxpeers", "name", peername, SENTINEL);
4446  /*!\note
4447  * If this one loaded something, then we need to ensure that the host
4448  * field matched. The only reason why we can't have this as a criteria
4449  * is because we only have the IP address and the host field might be
4450  * set as a name (and the reverse PTR might not match).
4451  */
4452  if (var && !ast_sockaddr_isnull(addr)) {
4453  for (tmp = var; tmp; tmp = tmp->next) {
4454  if (!strcasecmp(tmp->name, "host")) {
4455  struct ast_sockaddr *hostaddr = NULL;
4456 
4457  if (!ast_sockaddr_resolve(&hostaddr, tmp->value, PARSE_PORT_FORBID, AST_AF_UNSPEC)
4458  || ast_sockaddr_cmp_addr(hostaddr, addr)) {
4459  /* No match */
4460  ast_variables_destroy(var);
4461  var = NULL;
4462  }
4463  ast_free(hostaddr);
4464  break;
4465  }
4466  }
4467  }
4468  }
4469  if (!var)
4470  return NULL;
4471 
4472  peer = build_peer(peername, var, NULL, ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS) ? 0 : 1);
4473 
4474  if (!peer) {
4475  ast_variables_destroy(var);
4476  return NULL;
4477  }
4478 
4479  for (tmp = var; tmp; tmp = tmp->next) {
4480  /* Make sure it's not a user only... */
4481  if (!strcasecmp(tmp->name, "type")) {
4482  if (strcasecmp(tmp->value, "friend") &&
4483  strcasecmp(tmp->value, "peer")) {
4484  /* Whoops, we weren't supposed to exist! */
4485  peer = peer_unref(peer);
4486  break;
4487  }
4488  } else if (!strcasecmp(tmp->name, "regseconds")) {
4489  ast_get_time_t(tmp->value, &regseconds, 0, NULL);
4490  } else if (!strcasecmp(tmp->name, "ipaddr")) {
4491  int setport = ast_sockaddr_port(&peer->addr);
4492  if (ast_parse_arg(tmp->value, PARSE_ADDR | PARSE_PORT_FORBID, NULL)) {
4493  ast_log(LOG_WARNING, "Failed to parse sockaddr '%s' for ipaddr of realtime peer '%s'\n", tmp->value, tmp->name);
4494  } else {
4495  ast_sockaddr_parse(&peer->addr, tmp->value, 0);
4496  }
4497  ast_sockaddr_set_port(&peer->addr, setport);
4498  } else if (!strcasecmp(tmp->name, "port")) {
4499  int bindport;
4500  if (ast_parse_arg(tmp->value, PARSE_UINT32 | PARSE_IN_RANGE, &bindport, 0, 65535)) {
4501  bindport = IAX_DEFAULT_PORTNO;
4502  }
4503  ast_sockaddr_set_port(&peer->addr, bindport);
4504  } else if (!strcasecmp(tmp->name, "host")) {
4505  if (!strcasecmp(tmp->value, "dynamic"))
4506  dynamic = 1;
4507  }
4508  }
4509 
4510  ast_variables_destroy(var);
4511 
4512  if (ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS)) {
4513  ast_copy_flags64(peer, &globalflags, IAX_RTAUTOCLEAR|IAX_RTCACHEFRIENDS);
4514  if (ast_test_flag64(peer, IAX_RTAUTOCLEAR)) {
4515  if (peer->expire > -1) {
4516  if (!AST_SCHED_DEL(sched, peer->expire)) {
4517  peer->expire = -1;
4518  peer_unref(peer);
4519  }
4520  }
4521  peer->expire = iax2_sched_add(sched, (global_rtautoclear) * 1000, expire_registry, peer_ref(peer));
4522  if (peer->expire == -1)
4523  peer_unref(peer);
4524  }
4525  ao2_link(peers, peer);
4526  if (ast_test_flag64(peer, IAX_DYNAMIC))
4527  reg_source_db(peer);
4528  } else {
4529  ast_set_flag64(peer, IAX_TEMPONLY);
4530  }
4531 
4532  if (!ast_test_flag64(&globalflags, IAX_RTIGNOREREGEXPIRE) && dynamic) {
4533  time(&nowtime);
4534  if ((nowtime - regseconds) > IAX_DEFAULT_REG_EXPIRE) {
4535  memset(&peer->addr, 0, sizeof(peer->addr));
4536  realtime_update_peer(peer->name, &peer->addr, 0);
4537  ast_debug(1, "realtime_peer: Bah, '%s' is expired (%d/%d/%d)!\n",
4538  peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
4539  }
4540  else {
4541  ast_debug(1, "realtime_peer: Registration for '%s' still active (%d/%d/%d)!\n",
4542  peername, (int)(nowtime - regseconds), (int)regseconds, (int)nowtime);
4543  }
4544  }
4545 
4546  return peer;
4547 }
4548 
4549 static struct iax2_user *realtime_user(const char *username, struct ast_sockaddr *addr)
4550 {
4551  struct ast_variable *var;
4552  struct ast_variable *tmp;
4553  struct iax2_user *user=NULL;
4554  char *str_addr, *str_port;
4555 
4556  str_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
4557  str_port = ast_strdupa(ast_sockaddr_stringify_port(addr));
4558 
4559  var = ast_load_realtime("iaxusers", "name", username, "host", "dynamic", SENTINEL);
4560  if (!var)
4561  var = ast_load_realtime("iaxusers", "name", username, "host", str_addr, SENTINEL);
4562  if (!var && !ast_sockaddr_isnull(addr)) {
4563  var = ast_load_realtime("iaxusers", "name", username, "ipaddr", str_addr, "port", str_port, SENTINEL);
4564  if (!var)
4565  var = ast_load_realtime("iaxusers", "ipaddr", str_addr, "port", str_port, SENTINEL);
4566  }
4567  if (!var) { /* Last ditch effort */
4568  var = ast_load_realtime("iaxusers", "name", username, SENTINEL);
4569  /*!\note
4570  * If this one loaded something, then we need to ensure that the host
4571  * field matched. The only reason why we can't have this as a criteria
4572  * is because we only have the IP address and the host field might be
4573  * set as a name (and the reverse PTR might not match).
4574  */
4575  if (var) {
4576  for (tmp = var; tmp; tmp = tmp->next) {
4577  if (!strcasecmp(tmp->name, "host")) {
4578  struct ast_sockaddr *hostaddr = NULL;
4579 
4580  if (!ast_sockaddr_resolve(&hostaddr, tmp->value, PARSE_PORT_FORBID, AST_AF_UNSPEC)
4581  || ast_sockaddr_cmp_addr(hostaddr, addr)) {
4582  /* No match */
4583  ast_variables_destroy(var);
4584  var = NULL;
4585  }
4586  ast_free(hostaddr);
4587  break;
4588  }
4589  }
4590  }
4591  }
4592  if (!var)
4593  return NULL;
4594 
4595  tmp = var;
4596  while(tmp) {
4597  /* Make sure it's not a peer only... */
4598  if (!strcasecmp(tmp->name, "type")) {
4599  if (strcasecmp(tmp->value, "friend") &&
4600  strcasecmp(tmp->value, "user")) {
4601  return NULL;
4602  }
4603  }
4604  tmp = tmp->next;
4605  }
4606 
4607  user = build_user(username, var, NULL, !ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS));
4608 
4609  ast_variables_destroy(var);
4610 
4611  if (!user)
4612  return NULL;
4613 
4614  if (ast_test_flag64((&globalflags), IAX_RTCACHEFRIENDS)) {
4615  ast_set_flag64(user, IAX_RTCACHEFRIENDS);
4616  ao2_link(users, user);
4617  } else {
4618  ast_set_flag64(user, IAX_TEMPONLY);
4619  }
4620 
4621  return user;
4622 }
4623 
4624 static void realtime_update_peer(const char *peername, struct ast_sockaddr *sockaddr, time_t regtime)
4625 {
4626  char regseconds[20];
4627  const char *sysname = ast_config_AST_SYSTEM_NAME;
4628  char *syslabel = NULL;
4629  char *port;
4630 
4631  if (ast_strlen_zero(sysname)) /* No system name, disable this */
4632  sysname = NULL;
4633  else if (ast_test_flag64(&globalflags, IAX_RTSAVE_SYSNAME))
4634  syslabel = "regserver";
4635 
4636  snprintf(regseconds, sizeof(regseconds), "%d", (int)regtime);
4637  port = ast_strdupa(ast_sockaddr_stringify_port(sockaddr));
4638  ast_update_realtime("iaxpeers", "name", peername,
4639  "ipaddr", ast_sockaddr_isnull(sockaddr) ? "" : ast_sockaddr_stringify_addr(sockaddr),
4640  "port", ast_sockaddr_isnull(sockaddr) ? "" : port,
4641  "regseconds", regseconds, syslabel, sysname, SENTINEL); /* note syslabel can be NULL */
4642 }
4643 
4645  iax2_format capability;
4646  uint64_t flags;
4647  struct iax2_codec_pref prefs;
4648  int maxtime;
4649  int encmethods;
4650  int authmethods;
4651  int found;
4652  int sockfd;
4653  int adsi;
4654  char username[80];
4655  char secret[80];
4656  char outkey[80];
4657  char timezone[80];
4658  char cid_num[80];
4659  char cid_name[80];
4660  char context[AST_MAX_CONTEXT];
4661  char peercontext[AST_MAX_CONTEXT];
4662  char mohinterpret[MAX_MUSICCLASS];
4663  char mohsuggest[MAX_MUSICCLASS];
4664 };
4665 
4666 static int create_addr(const char *peername, struct ast_channel *c, struct ast_sockaddr *addr, struct create_addr_info *cai)
4667 {
4668  struct iax2_peer *peer;
4669  int res = -1;
4670 
4671  ast_clear_flag64(cai, IAX_SENDANI | IAX_TRUNK);
4672  cai->sockfd = defaultsockfd;
4673  cai->maxtime = 0;
4674 
4675  if (!(peer = find_peer(peername, 1))) {
4676  struct ast_sockaddr peer_addr;
4677 
4678  peer_addr.ss.ss_family = AST_AF_UNSPEC;
4679  cai->found = 0;
4680  if (ast_get_ip_or_srv(&peer_addr, peername, srvlookup ? "_iax._udp" : NULL)) {
4681  ast_log(LOG_WARNING, "No such host: %s\n", peername);
4682  return -1;
4683  }
4684 
4685  if (!ast_sockaddr_port(&peer_addr)) {
4686  ast_sockaddr_set_port(&peer_addr, IAX_DEFAULT_PORTNO);
4687  }
4688 
4689  ast_sockaddr_copy(addr, &peer_addr);
4690  /*
4691  * Use The global iax prefs for unknown peer/user.
4692  * However, move the calling channel's native codec to
4693  * the top of the preference list.
4694  */
4695  cai->prefs = prefs_global;
4696  if (c) {
4697  int i;
4698 
4699  for (i = 0; i < ast_format_cap_count(ast_channel_nativeformats(c)); i++) {
4700  struct ast_format *format = ast_format_cap_get_format(
4701  ast_channel_nativeformats(c), i);
4702  iax2_codec_pref_prepend(&cai->prefs, format,
4703  ast_format_cap_get_format_framing(ast_channel_nativeformats(c), format),
4704  1);
4705  ao2_ref(format, -1);
4706  }
4707  }
4708  return 0;
4709  }
4710 
4711  cai->found = 1;
4712 
4713  /* if the peer has no address (current or default), return failure */
4714  if (ast_sockaddr_isnull(&peer->addr) && ast_sockaddr_isnull(&peer->defaddr)) {
4715  goto return_unref;
4716  }
4717 
4718  /* if the peer is being monitored and is currently unreachable, return failure */
4719  if (peer->maxms && ((peer->lastms > peer->maxms) || (peer->lastms < 0)))
4720  goto return_unref;
4721 
4723  cai->maxtime = peer->maxms;
4724  cai->capability = peer->capability;
4725  cai->encmethods = peer->encmethods;
4726  cai->authmethods = peer->authmethods;
4727  cai->sockfd = peer->sockfd;
4728  cai->adsi = peer->adsi;
4729  cai->prefs = peer->prefs;
4730  /* Move the calling channel's native codec to the top of the preference list */
4731  if (c) {
4732  int i;
4733 
4734  for (i = 0; i < ast_format_cap_count(ast_channel_nativeformats(c)); i++) {
4735  struct ast_format *tmpfmt = ast_format_cap_get_format(
4736  ast_channel_nativeformats(c), i);
4737  iax2_codec_pref_prepend(&cai->prefs, tmpfmt,
4738  ast_format_cap_get_format_framing(ast_channel_nativeformats(c), tmpfmt),
4739  1);
4740  ao2_ref(tmpfmt, -1);
4741  }
4742  }
4743  ast_copy_string(cai->context, peer->context, sizeof(cai->context));
4744  ast_copy_string(cai->peercontext, peer->peercontext, sizeof(cai->peercontext));
4745  ast_copy_string(cai->username, peer->username, sizeof(cai->username));
4746  ast_copy_string(cai->timezone, peer->zonetag, sizeof(cai->timezone));
4747  ast_copy_string(cai->outkey, peer->outkey, sizeof(cai->outkey));
4748  ast_copy_string(cai->cid_num, peer->cid_num, sizeof(cai->cid_num));
4749  ast_copy_string(cai->cid_name, peer->cid_name, sizeof(cai->cid_name));
4750  ast_copy_string(cai->mohinterpret, peer->mohinterpret, sizeof(cai->mohinterpret));
4751  ast_copy_string(cai->mohsuggest, peer->mohsuggest, sizeof(cai->mohsuggest));
4752  if (ast_strlen_zero(peer->dbsecret)) {
4753  ast_copy_string(cai->secret, peer->secret, sizeof(cai->secret));
4754  } else {
4755  char *family;
4756  char *key = NULL;
4757 
4758  family = ast_strdupa(peer->dbsecret);
4759  key = strchr(family, '/');
4760  if (key)
4761  *key++ = '\0';
4762  if (!key || ast_db_get(family, key, cai->secret, sizeof(cai->secret))) {
4763  ast_log(LOG_WARNING, "Unable to retrieve database password for family/key '%s'!\n", peer->dbsecret);
4764  goto return_unref;
4765  }
4766  }
4767 
4768  if (!ast_sockaddr_isnull(&peer->addr)) {
4769  ast_sockaddr_copy(addr, &peer->addr);
4770  } else {
4771  ast_sockaddr_copy(addr, &peer->defaddr);
4772  }
4773 
4774  res = 0;
4775 
4776 return_unref:
4777  peer_unref(peer);
4778 
4779  return res;
4780 }
4781 
4782 static void __auto_congest(const void *nothing)
4783 {
4784  int callno = PTR_TO_CALLNO(nothing);
4786  ast_mutex_lock(&iaxsl[callno]);
4787  if (iaxs[callno]) {
4788  iaxs[callno]->initid = -1;
4789  iax2_queue_frame(callno, &f);
4790  ast_log(LOG_NOTICE, "Auto-congesting call due to slow response\n");
4791  }
4792  ast_mutex_unlock(&iaxsl[callno]);
4793 }
4794 
4795 static int auto_congest(const void *data)
4796 {
4797 #ifdef SCHED_MULTITHREADED
4798  if (schedule_action(__auto_congest, data))
4799 #endif
4800  __auto_congest(data);
4801  return 0;
4802 }
4803 
4804 static unsigned int iax2_datetime(const char *tz)
4805 {
4806  struct timeval t = ast_tvnow();
4807  struct ast_tm tm;
4808  unsigned int tmp;
4809  ast_localtime(&t, &tm, ast_strlen_zero(tz) ? NULL : tz);
4810  tmp = (tm.tm_sec >> 1) & 0x1f; /* 5 bits of seconds */
4811  tmp |= (tm.tm_min & 0x3f) << 5; /* 6 bits of minutes */
4812  tmp |= (tm.tm_hour & 0x1f) << 11; /* 5 bits of hours */
4813  tmp |= (tm.tm_mday & 0x1f) << 16; /* 5 bits of day of month */
4814  tmp |= ((tm.tm_mon + 1) & 0xf) << 21; /* 4 bits of month */
4815  tmp |= ((tm.tm_year - 100) & 0x7f) << 25; /* 7 bits of year */
4816  return tmp;
4817 }
4818 
4820  char *username;
4821  char *password;
4822  char *key;
4823  char *peer;
4824  char *port;
4825  char *exten;
4826  char *context;
4827  char *options;
4828 };
4829 
4830 static int send_apathetic_reply(unsigned short callno, unsigned short dcallno,
4831  struct ast_sockaddr *addr, int command, int ts, unsigned char seqno,
4832  int sockfd, struct iax_ie_data *ied)
4833 {
4834  struct {
4835  struct ast_iax2_full_hdr f;
4836  struct iax_ie_data ied;
4837  } data;
4838  size_t size = sizeof(struct ast_iax2_full_hdr);
4839 
4840  if (ied) {
4841  size += ied->pos;
4842  memcpy(&data.ied, ied->buf, ied->pos);
4843  }
4844 
4845  data.f.scallno = htons(0x8000 | callno);
4846  data.f.dcallno = htons(dcallno & ~IAX_FLAG_RETRANS);
4847  data.f.ts = htonl(ts);
4848  data.f.iseqno = seqno;
4849  data.f.oseqno = 0;
4850  data.f.type = AST_FRAME_IAX;
4851  data.f.csub = compress_subclass(command);
4852 
4853  iax_outputframe(NULL, &data.f, 0, addr, size - sizeof(struct ast_iax2_full_hdr));
4854 
4855  return ast_sendto(sockfd, &data, size, 0, addr);
4856 }
4857 
4858 static void add_empty_calltoken_ie(struct chan_iax2_pvt *pvt, struct iax_ie_data *ied)
4859 {
4860  /* first make sure their are two empty bytes left in ied->buf */
4861  if (pvt && ied && (2 < ((int) sizeof(ied->buf) - ied->pos))) {
4862  ied->buf[ied->pos++] = IAX_IE_CALLTOKEN; /* type */
4863  ied->buf[ied->pos++] = 0; /* data size, ZERO in this case */
4864  pvt->calltoken_ie_len = 2;
4865  }
4866 }
4867 
4868 static void resend_with_token(int callno, struct iax_frame *f, const char *newtoken)
4869 {
4870  struct chan_iax2_pvt *pvt = iaxs[callno];
4871  int frametype = f->af.frametype;
4872  int subclass = f->af.subclass.integer;
4873  struct {
4874  struct ast_iax2_full_hdr fh;
4875  struct iax_ie_data ied;
4876  } data = {
4877  .ied.buf = { 0 },
4878  .ied.pos = 0,
4879  };
4880  /* total len - header len gives us the frame's IE len */
4881  int ie_data_pos = f->datalen - sizeof(struct ast_iax2_full_hdr);
4882 
4883  if (!pvt) {
4884  return; /* this should not be possible if called from socket_process() */
4885  }
4886 
4887  /*
4888  * Check to make sure last frame sent is valid for call token resend
4889  * 1. Frame should _NOT_ be encrypted since it starts the IAX dialog
4890  * 2. Frame should _NOT_ already have a destination callno
4891  * 3. Frame must be a valid iax_frame subclass capable of starting dialog
4892  * 4. Pvt must have a calltoken_ie_len which represents the number of
4893  * bytes at the end of the frame used for the previous calltoken ie.
4894  * 5. Pvt's calltoken_ie_len must be _LESS_ than the total IE length
4895  * 6. Total length of f->data must be _LESS_ than size of our data struct
4896  * because f->data must be able to fit within data.
4897  */
4898  if (f->encmethods || f->dcallno || !iax2_allow_new(frametype, subclass, 0)
4899  || !pvt->calltoken_ie_len || (pvt->calltoken_ie_len > ie_data_pos) ||
4900  (f->datalen > sizeof(data))) {
4901 
4902  return; /* ignore resend, token was not valid for the dialog */
4903  }
4904 
4905  /* token is valid
4906  * 1. Copy frame data over
4907  * 2. Redo calltoken IE, it will always be the last ie in the frame.
4908  * NOTE: Having the ie always be last is not protocol specified,
4909  * it is only an implementation choice. Since we only expect the ie to
4910  * be last for frames we have sent, this can no way be affected by
4911  * another end point.
4912  * 3. Remove frame from queue
4913  * 4. Free old frame
4914  * 5. Clear previous seqnos
4915  * 6. Resend with CALLTOKEN ie.
4916  */
4917 
4918  /* ---1.--- */
4919  memcpy(&data, f->data, f->datalen);
4920  data.ied.pos = ie_data_pos;
4921 
4922  /* ---2.--- */
4923  /* move to the beginning of the calltoken ie so we can write over it */
4924  data.ied.pos -= pvt->calltoken_ie_len;
4925  iax_ie_append_str(&data.ied, IAX_IE_CALLTOKEN, newtoken);
4926 
4927  /* make sure to update token length incase it ever has to be stripped off again */
4928  pvt->calltoken_ie_len = data.ied.pos - ie_data_pos; /* new pos minus old pos tells how big token ie is */
4929 
4930  /* ---3.--- */
4931  AST_LIST_REMOVE(&frame_queue[callno], f, list);
4932 
4933  /* ---4.--- */
4934  iax2_frame_free(f);
4935 
4936  /* ---5.--- */
4937  pvt->oseqno = 0;
4938  pvt->rseqno = 0;
4939  pvt->iseqno = 0;
4940  pvt->aseqno = 0;
4941  if (pvt->peercallno) {
4942  remove_by_peercallno(pvt);
4943  pvt->peercallno = 0;
4944  }
4945 
4946  /* ---6.--- */
4947  send_command(pvt, AST_FRAME_IAX, subclass, 0, data.ied.buf, data.ied.pos, -1);
4948 }
4949 
4950 static void requirecalltoken_mark_auto(const char *name, int subclass)
4951 {
4952  struct iax2_user *user = NULL;
4953  struct iax2_peer *peer = NULL;
4954 
4955  if (ast_strlen_zero(name)) {
4956  return; /* no username given */
4957  }
4958 
4959  if ((subclass == IAX_COMMAND_NEW) && (user = find_user(name)) && (user->calltoken_required == CALLTOKEN_AUTO)) {
4961  } else if ((subclass != IAX_COMMAND_NEW) && (peer = find_peer(name, 1)) && (peer->calltoken_required == CALLTOKEN_AUTO)) {
4963  }
4964 
4965  if (peer) {
4966  peer_unref(peer);
4967  }
4968  if (user) {
4969  user_unref(user);
4970  }
4971 }
4972 
4973 /*!
4974  * \internal
4975  *
4976  * \brief handles calltoken logic for a received iax_frame.
4977  *
4978  * \note frametype must be AST_FRAME_IAX.
4979  *
4980  * \note
4981  * Three different cases are possible here.
4982  * Case 1. An empty calltoken is provided. This means the client supports
4983  * calltokens but has not yet received one from us. In this case
4984  * a full calltoken IE is created and sent in a calltoken fullframe.
4985  * Case 2. A full calltoken is received and must be checked for validity.
4986  * Case 3. No calltoken is received indicating that the client does not
4987  * support calltokens. In this case it is up to the configuration
4988  * to decide how this should be handled (reject or permit without calltoken)
4989  */
4990 static int handle_call_token(struct ast_iax2_full_hdr *fh, struct iax_ies *ies,
4991  struct ast_sockaddr *addr, int fd)
4992 {
4993 #define CALLTOKEN_HASH_FORMAT "%s%u%d" /* address + port + ts + randomcalldata */
4994 #define CALLTOKEN_IE_FORMAT "%u?%s" /* time + ? + (40 char hash) */
4995  struct ast_str *buf = ast_str_alloca(256);
4996  time_t t = time(NULL);
4997  char hash[41]; /* 40 char sha1 hash */
4998  int subclass = uncompress_subclass(fh->csub);
4999 
5000  /* ----- Case 1 ----- */
5001  if (ies->calltoken && !ies->calltokendata) { /* empty calltoken is provided, client supports calltokens */
5002  struct iax_ie_data ied = {
5003  .buf = { 0 },
5004  .pos = 0,
5005  };
5006 
5007  /* create the hash with their address data and our timestamp */
5008  ast_str_set(&buf, 0, CALLTOKEN_HASH_FORMAT, ast_sockaddr_stringify(addr), (unsigned int) t, randomcalltokendata);
5009  ast_sha1_hash(hash, ast_str_buffer(buf));
5010 
5011  ast_str_set(&buf, 0, CALLTOKEN_IE_FORMAT, (unsigned int) t, hash);
5012  iax_ie_append_str(&ied, IAX_IE_CALLTOKEN, ast_str_buffer(buf));
5013  send_apathetic_reply(1, ntohs(fh->scallno), addr, IAX_COMMAND_CALLTOKEN, ntohl(fh->ts), fh->iseqno + 1, fd, &ied);
5014 
5015  return 1;
5016 
5017  /* ----- Case 2 ----- */
5018  } else if (ies->calltoken && ies->calltokendata) { /* calltoken received, check to see if it is valid */
5019  char *rec_hash = NULL; /* the received hash, make sure it matches with ours. */
5020  char *rec_ts = NULL; /* received timestamp */
5021  unsigned int rec_time; /* received time_t */
5022 
5023  /* split the timestamp from the hash data */
5024  rec_hash = strchr((char *) ies->calltokendata, '?');
5025  if (rec_hash) {
5026  *rec_hash++ = '\0';
5027  rec_ts = (char *) ies->calltokendata;
5028  }
5029 
5030  /* check that we have valid data before we do any comparisons */
5031  if (!rec_hash || !rec_ts) {
5032  goto reject;
5033  } else if (sscanf(rec_ts, "%u", &rec_time) != 1) {
5034  goto reject;
5035  }
5036 
5037  /* create a hash with their address and the _TOKEN'S_ timestamp */
5038  ast_str_set(&buf, 0, CALLTOKEN_HASH_FORMAT, ast_sockaddr_stringify(addr), (unsigned int) rec_time, randomcalltokendata);
5039  ast_sha1_hash(hash, ast_str_buffer(buf));
5040 
5041  /* compare hashes and then check timestamp delay */
5042  if (strcmp(hash, rec_hash)) {
5043  ast_log(LOG_WARNING, "Address %s failed CallToken hash inspection\n", ast_sockaddr_stringify(addr));
5044  goto reject; /* received hash does not match ours, reject */
5045  } else if ((t < rec_time) || ((t - rec_time) >= max_calltoken_delay)) {
5046  ast_log(LOG_WARNING, "Too much delay in IAX2 calltoken timestamp from address %s\n", ast_sockaddr_stringify(addr));
5047  goto reject; /* too much delay, reject */
5048  }
5049 
5050  /* at this point the call token is valid, returning 0
5051  * will allow socket_process to continue as usual */
5052  requirecalltoken_mark_auto(ies->username, subclass);
5053  return 0;
5054 
5055  /* ----- Case 3 ----- */
5056  } else { /* calltokens are not supported for this client, how do we respond? */
5057  if (calltoken_required(addr, ies->username, subclass)) {
5058  ast_log(LOG_ERROR, "Call rejected, CallToken Support required. If unexpected, resolve by placing address %s in the calltokenoptional list or setting user %s requirecalltoken=no\n", ast_sockaddr_stringify(addr), S_OR(ies->username, "guest"));
5059  goto reject;
5060  }
5061  return 0; /* calltoken is not required for this addr, so permit it. */
5062  }
5063 
5064 reject:
5065  /* received frame has failed calltoken inspection, send apathetic reject messages */
5066  if (subclass == IAX_COMMAND_REGREQ || subclass == IAX_COMMAND_REGREL) {
5067  send_apathetic_reply(1, ntohs(fh->scallno), addr, IAX_COMMAND_REGREJ, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
5068  } else {
5069  send_apathetic_reply(1, ntohs(fh->scallno), addr, IAX_COMMAND_REJECT, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
5070  }
5071 
5072  return 1;
5073 }
5074 
5075 /*!
5076  * \brief Parses an IAX dial string into its component parts.
5077  * \param data the string to be parsed
5078  * \param pds pointer to a \c struct \c parsed_dial_string to be filled in
5079  *
5080  * This function parses the string and fills the structure
5081  * with pointers to its component parts. The input string
5082  * will be modified.
5083  *
5084  * \note This function supports both plaintext passwords and RSA
5085  * key names; if the password string is formatted as '[keyname]',
5086  * then the keyname will be placed into the key field, and the
5087  * password field will be set to NULL.
5088  *
5089  * \note The dial string format is:
5090  * \verbatim [username[:password]@]peer[:port][/exten[@context]][/options] \endverbatim
5091  */
5092 static void parse_dial_string(char *data, struct parsed_dial_string *pds)
5093 {
5094  char *outkey = NULL;
5095 
5096  if (ast_strlen_zero(data))
5097  return;
5098 
5099  pds->peer = strsep(&data, "/");
5100  pds->exten = strsep(&data, "/");
5101  pds->options = data;
5102 
5103  if (pds->exten) {
5104  data = pds->exten;
5105  pds->exten = strsep(&data, "@");
5106  pds->context = data;
5107  }
5108 
5109  if (strchr(pds->peer, '@')) {
5110  data = pds->peer;
5111  pds->username = strsep(&data, "@");
5112  pds->peer = data;
5113  }
5114 
5115  if (pds->username) {
5116  data = pds->username;
5117  pds->username = strsep(&data, ":");
5118  pds->password = strsep(&data, ":");
5119  outkey = data;
5120  }
5121 
5122  data = pds->peer;
5123  pds->peer = strsep(&data, ":");
5124  pds->port = data;
5125 
5126  /*
5127  * Check for a key name wrapped in [] in the password position.
5128  * If found, move it to the key field instead.
5129  * Also allow for both key and secret to be specified, now that
5130  * encryption is possible with RSA authentication.
5131  */
5132 
5133  if (pds->password && (pds->password[0] == '[')) { /* key (then maybe secret) */
5134  pds->key = ast_strip_quoted(pds->password, "[", "]");
5135  if (ast_strlen_zero(outkey)) {
5136  pds->password = NULL;
5137  ast_debug(1, "Outkey (%s), no secret\n", pds->key);
5138  } else {
5139  pds->password = outkey;
5140  ast_debug(1, "Outkey (%s) and secret (%s)\n", pds->key, pds->password);
5141  }
5142  } else if (outkey && (outkey[0] == '[')) { /* secret, then key */
5143  pds->key = ast_strip_quoted(outkey, "[", "]");
5144  if (ast_strlen_zero(pds->password)) {
5145  ast_debug(1, "Outkey (%s), no secret\n", pds->key);
5146  } else {
5147  ast_debug(1, "Outkey (%s) and secret (%s)\n", pds->key, pds->password);
5148  }
5149  }
5150 }
5151 
5152 static int iax2_call(struct ast_channel *c, const char *dest, int timeout)
5153 {
5154  struct ast_sockaddr addr;
5155  char *l=NULL, *n=NULL, *tmpstr;
5156  struct iax_ie_data ied;
5157  char *defaultrdest = "s";
5158  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5159  struct parsed_dial_string pds;
5160  struct create_addr_info cai;
5161  struct ast_var_t *var;
5162  struct ast_datastore *variablestore = ast_channel_datastore_find(c, &iax2_variable_datastore_info, NULL);
5163  const char* osp_token_ptr;
5164  unsigned int osp_token_length;
5165  unsigned char osp_block_index;
5166  unsigned int osp_block_length;
5167  unsigned char osp_buffer[256];
5168  char encoded_prefs[32];
5169  iax2_format iax2_tmpfmt;
5170 
5172  ast_log(LOG_WARNING, "Channel is already in use (%s)?\n", ast_channel_name(c));
5173  return -1;
5174  }
5175 
5176  memset(&cai, 0, sizeof(cai));
5177  cai.encmethods = iax2_encryption;
5178  cai.authmethods = iax2_authmethods;
5179 
5180  memset(&pds, 0, sizeof(pds));
5181  tmpstr = ast_strdupa(dest);
5182  parse_dial_string(tmpstr, &pds);
5183 
5184  if (ast_strlen_zero(pds.peer)) {
5185  ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", dest);
5186  return -1;
5187  }
5188  if (!pds.exten) {
5189  pds.exten = defaultrdest;
5190  }
5191  if (create_addr(pds.peer, c, &addr, &cai)) {
5192  ast_log(LOG_WARNING, "No address associated with '%s'\n", pds.peer);
5193  return -1;
5194  }
5195 
5196  if (ast_test_flag64(&cai, IAX_FORCE_ENCRYPT) ||
5197  ast_test_flag64(iaxs[callno], IAX_FORCE_ENCRYPT)) {
5198  if (!cai.encmethods) {
5199  ast_log(LOG_WARNING, "Encryption forced for call, but not enabled\n");
5200  ast_channel_hangupcause_set(c, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
5201  return -1;
5202  }
5203  if (((cai.authmethods & IAX_AUTH_RSA) || (cai.authmethods & IAX_AUTH_MD5) || (cai.authmethods & IAX_AUTH_PLAINTEXT)) &&
5204  ast_strlen_zero(cai.secret) && ast_strlen_zero(pds.password)) {
5205  ast_log(LOG_WARNING, "Call terminated. Encryption forced but no secret provided\n");
5206  return -1;
5207  }
5208  }
5209 
5210  if (!pds.username && !ast_strlen_zero(cai.username))
5211  pds.username = cai.username;
5212  if (!pds.password && !ast_strlen_zero(cai.secret))
5213  pds.password = cai.secret;
5214  if (!pds.key && !ast_strlen_zero(cai.outkey))
5215  pds.key = cai.outkey;
5216  if (!pds.context && !ast_strlen_zero(cai.peercontext))
5217  pds.context = cai.peercontext;
5218 
5219  /* Keep track of the context for outgoing calls too */
5220  ast_channel_context_set(c, cai.context);
5221 
5222  if (pds.port) {
5223  int bindport;
5224  if (ast_parse_arg(pds.port, PARSE_UINT32 | PARSE_IN_RANGE, &bindport, 0, 65535)) {
5225  ast_sockaddr_set_port(&addr, bindport);
5226  }
5227  }
5228 
5229  l = ast_channel_connected(c)->id.number.valid ? ast_channel_connected(c)->id.number.str : NULL;
5230  n = ast_channel_connected(c)->id.name.valid ? ast_channel_connected(c)->id.name.str : NULL;
5231 
5232  /* Now build request */
5233  memset(&ied, 0, sizeof(ied));
5234 
5235  /* On new call, first IE MUST be IAX version of caller */
5236  iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
5237  iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, pds.exten);
5238  if (pds.options && strchr(pds.options, 'a')) {
5239  /* Request auto answer */
5240  iax_ie_append(&ied, IAX_IE_AUTOANSWER);
5241  }
5242 
5243  /* WARNING: this breaks down at 190 bits! */
5244  iax2_codec_pref_convert(&cai.prefs, encoded_prefs, sizeof(encoded_prefs), 1);
5245  iax_ie_append_str(&ied, IAX_IE_CODEC_PREFS, encoded_prefs);
5246 
5247  if (l) {
5248  iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, l);
5249  iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES,
5250  ast_party_id_presentation(&ast_channel_connected(c)->id));
5251  } else if (n) {
5252  iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES,
5253  ast_party_id_presentation(&ast_channel_connected(c)->id));
5254  } else {
5255  iax_ie_append_byte(&ied, IAX_IE_CALLINGPRES, AST_PRES_NUMBER_NOT_AVAILABLE);
5256  }
5257 
5258  iax_ie_append_byte(&ied, IAX_IE_CALLINGTON, ast_channel_connected(c)->id.number.plan);
5259  iax_ie_append_short(&ied, IAX_IE_CALLINGTNS, ast_channel_dialed(c)->transit_network_select);
5260  iax_ie_append_int(&ied, IAX_IE_CALLINGANI2, ast_channel_connected(c)->ani2);
5261 
5262  if (n)
5263  iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, n);
5264  if (ast_test_flag64(iaxs[callno], IAX_SENDANI)
5265  && ast_channel_connected(c)->ani.number.valid
5266  && ast_channel_connected(c)->ani.number.str) {
5267  iax_ie_append_str(&ied, IAX_IE_CALLING_ANI, ast_channel_connected(c)->ani.number.str);
5268  }
5269 
5270  if (!ast_strlen_zero(ast_channel_language(c)))
5271  iax_ie_append_str(&ied, IAX_IE_LANGUAGE, ast_channel_language(c));
5272  if (!ast_strlen_zero(ast_channel_dialed(c)->number.str)) {
5273  iax_ie_append_str(&ied, IAX_IE_DNID, ast_channel_dialed(c)->number.str);
5274  }
5275  if (ast_channel_redirecting(c)->from.number.valid
5276  && !ast_strlen_zero(ast_channel_redirecting(c)->from.number.str)) {
5277  iax_ie_append_str(&ied, IAX_IE_RDNIS, ast_channel_redirecting(c)->from.number.str);
5278  }
5279 
5280  if (pds.context)
5281  iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, pds.context);
5282 
5283  if (pds.username)
5284  iax_ie_append_str(&ied, IAX_IE_USERNAME, pds.username);
5285 
5286  if (cai.encmethods)
5287  iax_ie_append_short(&ied, IAX_IE_ENCRYPTION, cai.encmethods);
5288 
5289  ast_mutex_lock(&iaxsl[callno]);
5290 
5291  if (!ast_strlen_zero(ast_channel_context(c)))
5292  ast_string_field_set(iaxs[callno], context, ast_channel_context(c));
5293 
5294  if (pds.username)
5295  ast_string_field_set(iaxs[callno], username, pds.username);
5296 
5297  iaxs[callno]->encmethods = cai.encmethods;
5298 
5299  iaxs[callno]->adsi = cai.adsi;
5300 
5301  ast_string_field_set(iaxs[callno], mohinterpret, cai.mohinterpret);
5302  ast_string_field_set(iaxs[callno], mohsuggest, cai.mohsuggest);
5303 
5304  if (pds.key)
5305  ast_string_field_set(iaxs[callno], outkey, pds.key);
5306  if (pds.password)
5307  ast_string_field_set(iaxs[callno], secret, pds.password);
5308 
5309  iax2_tmpfmt = iax2_format_compatibility_cap2bitfield(ast_channel_nativeformats(c));
5310  iax_ie_append_int(&ied, IAX_IE_FORMAT, (int) iax2_tmpfmt);
5311  iax_ie_append_versioned_uint64(&ied, IAX_IE_FORMAT2, 0, iax2_tmpfmt);
5312 
5313  iax_ie_append_int(&ied, IAX_IE_CAPABILITY, (int) iaxs[callno]->capability);
5314  iax_ie_append_versioned_uint64(&ied, IAX_IE_CAPABILITY2, 0, iaxs[callno]->capability);
5315  iax_ie_append_short(&ied, IAX_IE_ADSICPE, ast_channel_adsicpe(c));
5316  iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime(cai.timezone));
5317 
5318  if (iaxs[callno]->maxtime) {
5319  /* Initialize pingtime and auto-congest time */
5320  iaxs[callno]->pingtime = iaxs[callno]->maxtime / 2;
5321  iaxs[callno]->initid = iax2_sched_add(sched, iaxs[callno]->maxtime * 2, auto_congest, CALLNO_TO_PTR(callno));
5322  } else if (autokill) {
5323  iaxs[callno]->pingtime = autokill / 2;
5324  iaxs[callno]->initid = iax2_sched_add(sched, autokill * 2, auto_congest, CALLNO_TO_PTR(callno));
5325  }
5326 
5327  /* Check if there is an OSP token */
5328  osp_token_ptr = pbx_builtin_getvar_helper(c, "IAX2OSPTOKEN");
5329  if (!ast_strlen_zero(osp_token_ptr)) {
5330  if ((osp_token_length = strlen(osp_token_ptr)) <= IAX_MAX_OSPTOKEN_SIZE) {
5331  osp_block_index = 0;
5332  while (osp_token_length > 0) {
5333  osp_block_length = IAX_MAX_OSPBLOCK_SIZE < osp_token_length ? IAX_MAX_OSPBLOCK_SIZE : osp_token_length;
5334  osp_buffer[0] = osp_block_index;
5335  memcpy(osp_buffer + 1, osp_token_ptr, osp_block_length);
5336  iax_ie_append_raw(&ied, IAX_IE_OSPTOKEN, osp_buffer, osp_block_length + 1);
5337  osp_block_index++;
5338  osp_token_ptr += osp_block_length;
5339  osp_token_length -= osp_block_length;
5340  }
5341  } else
5342  ast_log(LOG_WARNING, "OSP token is too long\n");
5343  } else if (iaxdebug)
5344  ast_debug(1, "OSP token is undefined\n");
5345 
5346  /* send the command using the appropriate socket for this peer */
5347  iaxs[callno]->sockfd = cai.sockfd;
5348 
5349  /* Add remote vars */
5350  if (variablestore) {
5351  AST_LIST_HEAD(, ast_var_t) *variablelist = variablestore->data;
5352  ast_debug(1, "Found an IAX variable store on this channel\n");
5353  AST_LIST_LOCK(variablelist);
5354  AST_LIST_TRAVERSE(variablelist, var, entries) {
5355  char tmp[256];
5356  int i;
5357  ast_debug(1, "Found IAXVAR '%s' with value '%s' (to transmit)\n", ast_var_name(var), ast_var_value(var));
5358  /* Automatically divide the value up into sized chunks */
5359  for (i = 0; i < strlen(ast_var_value(var)); i += 255 - (strlen(ast_var_name(var)) + 1)) {
5360  snprintf(tmp, sizeof(tmp), "%s=%s", ast_var_name(var), ast_var_value(var) + i);
5361  iax_ie_append_str(&ied, IAX_IE_VARIABLE, tmp);
5362  }
5363  }
5364  AST_LIST_UNLOCK(variablelist);
5365  }
5366 
5367  /* Transmit the string in a "NEW" request */
5368  add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
5369  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
5370 
5371  ast_mutex_unlock(&iaxsl[callno]);
5373 
5374  return 0;
5375 }
5376 
5377 static int iax2_hangup(struct ast_channel *c)
5378 {
5379  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5380  struct iax_ie_data ied;
5381  int alreadygone;
5382  memset(&ied, 0, sizeof(ied));
5383  ast_mutex_lock(&iaxsl[callno]);
5384  if (callno && iaxs[callno]) {
5385  ast_debug(1, "We're hanging up %s now...\n", ast_channel_name(c));
5386  alreadygone = ast_test_flag64(iaxs[callno], IAX_ALREADYGONE);
5387  /* Send the hangup unless we have had a transmission error or are already gone */
5388  iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, (unsigned char)ast_channel_hangupcause(c));
5389  if (!iaxs[callno]->error && !alreadygone) {
5390  if (send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, ied.buf, ied.pos, -1)) {
5391  ast_log(LOG_WARNING, "No final packet could be sent for callno %d\n", callno);
5392  }
5393  if (!iaxs[callno]) {
5394  ast_mutex_unlock(&iaxsl[callno]);
5395  return 0;
5396  }
5397  }
5398  /* Explicitly predestroy it */
5399  iax2_predestroy(callno);
5400  /* If we were already gone to begin with, destroy us now */
5401  if (iaxs[callno] && alreadygone) {
5402  ast_debug(1, "Really destroying %s now...\n", ast_channel_name(c));
5403  iax2_destroy(callno);
5404  } else if (iaxs[callno]) {
5405  if (ast_sched_add(sched, 10000, scheduled_destroy, CALLNO_TO_PTR(callno)) < 0) {
5406  ast_log(LOG_ERROR, "Unable to schedule iax2 callno %d destruction?!! Destroying immediately.\n", callno);
5407  iax2_destroy(callno);
5408  }
5409  }
5410  } else if (ast_channel_tech_pvt(c)) {
5411  /* If this call no longer exists, but the channel still
5412  * references it we need to set the channel's tech_pvt to null
5413  * to avoid ast_channel_free() trying to free it.
5414  */
5415  ast_channel_tech_pvt_set(c, NULL);
5416  }
5417  ast_mutex_unlock(&iaxsl[callno]);
5418  ast_verb(3, "Hungup '%s'\n", ast_channel_name(c));
5419  return 0;
5420 }
5421 
5422 /*!
5423  * \note expects the pvt to be locked
5424  */
5425 static int wait_for_peercallno(struct chan_iax2_pvt *pvt)
5426 {
5427  unsigned short callno = pvt->callno;
5428 
5429  if (!pvt->peercallno) {
5430  /* We don't know the remote side's call number, yet. :( */
5431  int count = 10;
5432  while (count-- && pvt && !pvt->peercallno) {
5433  DEADLOCK_AVOIDANCE(&iaxsl[callno]);
5434  pvt = iaxs[callno];
5435  }
5436  if (!pvt || !pvt->peercallno) {
5437  return -1;
5438  }
5439  }
5440 
5441  return 0;
5442 }
5443 
5444 static int iax2_setoption(struct ast_channel *c, int option, void *data, int datalen)
5445 {
5446  struct ast_option_header *h;
5447  int res;
5448 
5449  switch (option) {
5450  case AST_OPTION_TXGAIN:
5451  case AST_OPTION_RXGAIN:
5452  /* these two cannot be sent, because they require a result */
5453  errno = ENOSYS;
5454  return -1;
5455  case AST_OPTION_OPRMODE:
5456  errno = EINVAL;
5457  return -1;
5459  case AST_OPTION_SECURE_MEDIA:
5460  {
5461  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5462  ast_mutex_lock(&iaxsl[callno]);
5463  if ((*(int *) data)) {
5464  ast_set_flag64(iaxs[callno], IAX_FORCE_ENCRYPT);
5465  } else {
5466  ast_clear_flag64(iaxs[callno], IAX_FORCE_ENCRYPT);
5467  }
5468  ast_mutex_unlock(&iaxsl[callno]);
5469  return 0;
5470  }
5471  /* These options are sent to the other side across the network where
5472  * they will be passed to whatever channel is bridged there. Don't
5473  * do anything silly like pass an option that transmits pointers to
5474  * memory on this machine to a remote machine to use */
5476  case AST_OPTION_TDD:
5477  case AST_OPTION_RELAXDTMF:
5478  case AST_OPTION_AUDIO_MODE:
5480  case AST_OPTION_FAX_DETECT:
5481  {
5482  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5483  struct chan_iax2_pvt *pvt;
5484 
5485  ast_mutex_lock(&iaxsl[callno]);
5486  pvt = iaxs[callno];
5487 
5488  if (wait_for_peercallno(pvt)) {
5489  ast_mutex_unlock(&iaxsl[callno]);
5490  return -1;
5491  }
5492 
5493  ast_mutex_unlock(&iaxsl[callno]);
5494 
5495  if (!(h = ast_malloc(datalen + sizeof(*h)))) {
5496  return -1;
5497  }
5498 
5499  h->flag = AST_OPTION_FLAG_REQUEST;
5500  h->option = htons(option);
5501  memcpy(h->data, data, datalen);
5502  res = send_command_locked(PTR_TO_CALLNO(ast_channel_tech_pvt(c)), AST_FRAME_CONTROL,
5503  AST_CONTROL_OPTION, 0, (unsigned char *) h,
5504  datalen + sizeof(*h), -1);
5505  ast_free(h);
5506  return res;
5507  }
5508  default:
5509  return -1;
5510  }
5511 
5512  /* Just in case someone does a break instead of a return */
5513  return -1;
5514 }
5515 
5516 static int iax2_queryoption(struct ast_channel *c, int option, void *data, int *datalen)
5517 {
5518  switch (option) {
5520  case AST_OPTION_SECURE_MEDIA:
5521  {
5522  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5523  ast_mutex_lock(&iaxsl[callno]);
5524  *((int *) data) = ast_test_flag64(iaxs[callno], IAX_FORCE_ENCRYPT) ? 1 : 0;
5525  ast_mutex_unlock(&iaxsl[callno]);
5526  return 0;
5527  }
5528  default:
5529  return -1;
5530  }
5531 }
5532 
5533 static struct ast_frame *iax2_read(struct ast_channel *c)
5534 {
5535  ast_debug(1, "I should never be called!\n");
5536  return &ast_null_frame;
5537 }
5538 
5539 static int iax2_key_rotate(const void *vpvt)
5540 {
5541  int res = 0;
5542  struct chan_iax2_pvt *pvt = (void *) vpvt;
5543  struct MD5Context md5;
5544  char key[17] = "";
5545  struct iax_ie_data ied = {
5546  .pos = 0,
5547  };
5548 
5549  ast_mutex_lock(&iaxsl[pvt->callno]);
5550  pvt->keyrotateid = ast_sched_add(sched, 120000 + (ast_random() % 180001), iax2_key_rotate, vpvt);
5551 
5552  snprintf(key, sizeof(key), "%lX", (unsigned long)ast_random());
5553 
5554  MD5Init(&md5);
5555  MD5Update(&md5, (unsigned char *) key, strlen(key));
5556  MD5Final((unsigned char *) key, &md5);
5557 
5558  IAX_DEBUGDIGEST("Sending", key);
5559 
5560  iax_ie_append_raw(&ied, IAX_IE_CHALLENGE, key, 16);
5561 
5562  res = send_command(pvt, AST_FRAME_IAX, IAX_COMMAND_RTKEY, 0, ied.buf, ied.pos, -1);
5563 
5564  build_ecx_key((unsigned char *) key, pvt);
5565 
5566  ast_mutex_unlock(&iaxsl[pvt->callno]);
5567 
5568  return res;
5569 }
5570 
5571 #if defined(IAX2_NATIVE_BRIDGING)
5572 static int iax2_start_transfer(unsigned short callno0, unsigned short callno1, int mediaonly)
5573 {
5574  int res;
5575  struct iax_ie_data ied0;
5576  struct iax_ie_data ied1;
5577  unsigned int transferid = (unsigned int)ast_random();
5578 
5579  if (IAX_CALLENCRYPTED(iaxs[callno0]) || IAX_CALLENCRYPTED(iaxs[callno1])) {
5580  ast_debug(1, "transfers are not supported for encrypted calls at this time\n");
5581  ast_set_flag64(iaxs[callno0], IAX_NOTRANSFER);
5582  ast_set_flag64(iaxs[callno1], IAX_NOTRANSFER);
5583  return 0;
5584  }
5585 
5586  memset(&ied0, 0, sizeof(ied0));
5587  iax_ie_append_addr(&ied0, IAX_IE_APPARENT_ADDR, &iaxs[callno1]->addr);
5588  iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[callno1]->peercallno);
5589  iax_ie_append_int(&ied0, IAX_IE_TRANSFERID, transferid);
5590 
5591  memset(&ied1, 0, sizeof(ied1));
5592  iax_ie_append_addr(&ied1, IAX_IE_APPARENT_ADDR, &iaxs[callno0]->addr);
5593  iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[callno0]->peercallno);
5594  iax_ie_append_int(&ied1, IAX_IE_TRANSFERID, transferid);
5595 
5596  res = send_command(iaxs[callno0], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied0.buf, ied0.pos, -1);
5597  if (res)
5598  return -1;
5599  res = send_command(iaxs[callno1], AST_FRAME_IAX, IAX_COMMAND_TXREQ, 0, ied1.buf, ied1.pos, -1);
5600  if (res)
5601  return -1;
5602  iaxs[callno0]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
5603  iaxs[callno1]->transferring = mediaonly ? TRANSFER_MBEGIN : TRANSFER_BEGIN;
5604  return 0;
5605 }
5606 #endif /* defined(IAX2_NATIVE_BRIDGING) */
5607 
5608 #if defined(IAX2_NATIVE_BRIDGING)
5609 static void lock_both(unsigned short callno0, unsigned short callno1)
5610 {
5611  ast_mutex_lock(&iaxsl[callno0]);
5612  while (ast_mutex_trylock(&iaxsl[callno1])) {
5613  DEADLOCK_AVOIDANCE(&iaxsl[callno0]);
5614  }
5615 }
5616 #endif /* defined(IAX2_NATIVE_BRIDGING) */
5617 
5618 #if defined(IAX2_NATIVE_BRIDGING)
5619 static void unlock_both(unsigned short callno0, unsigned short callno1)
5620 {
5621  ast_mutex_unlock(&iaxsl[callno1]);
5622  ast_mutex_unlock(&iaxsl[callno0]);
5623 }
5624 #endif /* defined(IAX2_NATIVE_BRIDGING) */
5625 
5626 #if defined(IAX2_NATIVE_BRIDGING)
5627 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)
5628 {
5629  struct ast_channel *cs[3];
5630  struct ast_channel *who, *other;
5631  int to = -1;
5632  int res = -1;
5633  int transferstarted=0;
5634  struct ast_frame *f;
5635  unsigned short callno0 = PTR_TO_CALLNO(ast_channel_tech_pvt(c0));
5636  unsigned short callno1 = PTR_TO_CALLNO(ast_channel_tech_pvt(c1));
5637  struct timeval waittimer = {0, 0};
5638 
5639  /* We currently do not support native bridging if a timeoutms value has been provided */
5640  if (timeoutms > 0) {
5641  return AST_BRIDGE_FAILED;
5642  }
5643 
5644  timeoutms = -1;
5645 
5646  lock_both(callno0, callno1);
5647  if (!iaxs[callno0] || !iaxs[callno1]) {
5648  unlock_both(callno0, callno1);
5649  return AST_BRIDGE_FAILED;
5650  }
5651  /* Put them in native bridge mode */
5653  iaxs[callno0]->bridgecallno = callno1;
5654  iaxs[callno1]->bridgecallno = callno0;
5655  }
5656  unlock_both(callno0, callno1);
5657 
5658  /* If not, try to bridge until we can execute a transfer, if we can */
5659  cs[0] = c0;
5660  cs[1] = c1;
5661  for (/* ever */;;) {
5662  /* Check in case we got masqueraded into */
5663  if ((ast_channel_tech(c0) != &iax2_tech) || (ast_channel_tech(c1) != &iax2_tech)) {
5664  ast_verb(3, "Can't masquerade, we're different...\n");
5665  /* Remove from native mode */
5666  if (ast_channel_tech(c0) == &iax2_tech) {
5667  ast_mutex_lock(&iaxsl[callno0]);
5668  iaxs[callno0]->bridgecallno = 0;
5669  ast_mutex_unlock(&iaxsl[callno0]);
5670  }
5671  if (ast_channel_tech(c1) == &iax2_tech) {
5672  ast_mutex_lock(&iaxsl[callno1]);
5673  iaxs[callno1]->bridgecallno = 0;
5674  ast_mutex_unlock(&iaxsl[callno1]);
5675  }
5676  return AST_BRIDGE_FAILED_NOWARN;
5677  }
5678  if (!(ast_format_cap_identical(ast_channel_nativeformats(c0), ast_channel_nativeformats(c1)))) {
5679  struct ast_str *c0_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5680  struct ast_str *c1_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
5681 
5682  ast_verb(3, "Operating with different codecs [%s] [%s] , can't native bridge...\n",
5683  ast_format_cap_get_names(ast_channel_nativeformats(c0), &c0_buf),
5684  ast_format_cap_get_names(ast_channel_nativeformats(c1), &c1_buf));
5685 
5686  /* Remove from native mode */
5687  lock_both(callno0, callno1);
5688  if (iaxs[callno0])
5689  iaxs[callno0]->bridgecallno = 0;
5690  if (iaxs[callno1])
5691  iaxs[callno1]->bridgecallno = 0;
5692  unlock_both(callno0, callno1);
5693  return AST_BRIDGE_FAILED_NOWARN;
5694  }
5695  /* check if transferred and if we really want native bridging */
5696  if (!transferstarted && !ast_test_flag64(iaxs[callno0], IAX_NOTRANSFER) && !ast_test_flag64(iaxs[callno1], IAX_NOTRANSFER)) {
5697  /* Try the transfer */
5698  if (iax2_start_transfer(callno0, callno1, (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1)) ||
5699  ast_test_flag64(iaxs[callno0], IAX_TRANSFERMEDIA) | ast_test_flag64(iaxs[callno1], IAX_TRANSFERMEDIA)))
5700  ast_log(LOG_WARNING, "Unable to start the transfer\n");
5701  transferstarted = 1;
5702  }
5703  if ((iaxs[callno0]->transferring == TRANSFER_RELEASED) && (iaxs[callno1]->transferring == TRANSFER_RELEASED)) {
5704  /* Call has been transferred. We're no longer involved */
5705  struct timeval now = ast_tvnow();
5706  if (ast_tvzero(waittimer)) {
5707  waittimer = now;
5708  } else if (now.tv_sec - waittimer.tv_sec > IAX_LINGER_TIMEOUT) {
5709  ast_channel_softhangup_internal_flag_add(c0, AST_SOFTHANGUP_DEV);
5710  ast_channel_softhangup_internal_flag_add(c1, AST_SOFTHANGUP_DEV);
5711  *fo = NULL;
5712  *rc = c0;
5713  res = AST_BRIDGE_COMPLETE;
5714  break;
5715  }
5716  }
5717  to = 1000;
5718  who = ast_waitfor_n(cs, 2, &to);
5719  /* XXX This will need to be updated to calculate
5720  * timeout correctly once timeoutms is allowed to be
5721  * > 0. Right now, this can go badly if the waitfor
5722  * times out in less than a millisecond
5723  */
5724  if (timeoutms > -1) {
5725  timeoutms -= (1000 - to);
5726  if (timeoutms < 0)
5727  timeoutms = 0;
5728  }
5729  if (!who) {
5730  if (!timeoutms) {
5731  res = AST_BRIDGE_RETRY;
5732  break;
5733  }
5734  if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
5735  res = AST_BRIDGE_FAILED;
5736  break;
5737  }
5738  continue;
5739  }
5740  f = ast_read(who);
5741  if (!f) {
5742  *fo = NULL;
5743  *rc = who;
5744  res = AST_BRIDGE_COMPLETE;
5745  break;
5746  }
5747  other = (who == c0) ? c1 : c0; /* the 'other' channel */
5748  if (f->frametype == AST_FRAME_CONTROL) {
5749  switch (f->subclass.integer) {
5750  case AST_CONTROL_VIDUPDATE:
5751  case AST_CONTROL_SRCUPDATE:
5752  case AST_CONTROL_SRCCHANGE:
5754  ast_write(other, f);
5755  break;
5757  ast_channel_hangupcause_hash_set(other, f->data.ptr, f->datalen);
5758  break;
5759  default:
5760  *fo = f;
5761  *rc = who;
5762  res = AST_BRIDGE_COMPLETE;
5763  break;
5764  }
5765  if (res == AST_BRIDGE_COMPLETE) {
5766  break;
5767  }
5768  } else if (f->frametype == AST_FRAME_VOICE
5769  || f->frametype == AST_FRAME_TEXT
5770  || f->frametype == AST_FRAME_VIDEO
5771  || f->frametype == AST_FRAME_IMAGE) {
5772  ast_write(other, f);
5773  } else if (f->frametype == AST_FRAME_DTMF) {
5774  /* monitored dtmf take out of the bridge.
5775  * check if we monitor the specific source.
5776  */
5777  int monitored_source = (who == c0) ? AST_BRIDGE_DTMF_CHANNEL_0 : AST_BRIDGE_DTMF_CHANNEL_1;
5778 
5779  if (flags & monitored_source) {
5780  *rc = who;
5781  *fo = f;
5782  res = AST_BRIDGE_COMPLETE;
5783  /* Remove from native mode */
5784  break;
5785  }
5786  ast_write(other, f);
5787  }
5788  ast_frfree(f);
5789  /* Swap who gets priority */
5790  cs[2] = cs[0];
5791  cs[0] = cs[1];
5792  cs[1] = cs[2];
5793  }
5794  lock_both(callno0, callno1);
5795  if(iaxs[callno0])
5796  iaxs[callno0]->bridgecallno = 0;
5797  if(iaxs[callno1])
5798  iaxs[callno1]->bridgecallno = 0;
5799  unlock_both(callno0, callno1);
5800  return res;
5801 }
5802 #endif /* defined(IAX2_NATIVE_BRIDGING) */
5803 
5804 static int iax2_answer(struct ast_channel *c)
5805 {
5806  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5807  ast_debug(1, "Answering IAX2 call\n");
5808  return send_command_locked(callno, AST_FRAME_CONTROL, AST_CONTROL_ANSWER, 0, NULL, 0, -1);
5809 }
5810 
5811 static int iax2_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen)
5812 {
5813  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5814  struct chan_iax2_pvt *pvt;
5815  int res = 0;
5816 
5817  if (iaxdebug)
5818  ast_debug(1, "Indicating condition %d\n", condition);
5819 
5820  ast_mutex_lock(&iaxsl[callno]);
5821  pvt = iaxs[callno];
5822 
5823  if (wait_for_peercallno(pvt)) {
5824  res = -1;
5825  goto done;
5826  }
5827 
5828  switch (condition) {
5829  case AST_CONTROL_HOLD:
5830  if (strcasecmp(pvt->mohinterpret, "passthrough")) {
5831  ast_moh_start(c, data, pvt->mohinterpret);
5832  goto done;
5833  }
5834  break;
5835  case AST_CONTROL_UNHOLD:
5836  if (strcasecmp(pvt->mohinterpret, "passthrough")) {
5837  ast_moh_stop(c);
5838  goto done;
5839  }
5840  break;
5843  if (!ast_test_flag64(pvt, IAX_SENDCONNECTEDLINE)) {
5844  /* We are not configured to allow sending these updates. */
5845  ast_debug(2, "Callno %d: Config blocked sending control frame %d.\n",
5846  callno, condition);
5847  goto done;
5848  }
5849  break;
5852  res = -1;
5853  goto done;
5854  }
5855 
5856  res = send_command(pvt, AST_FRAME_CONTROL, condition, 0, data, datalen, -1);
5857 
5858 done:
5859  ast_mutex_unlock(&iaxsl[callno]);
5860 
5861  return res;
5862 }
5863 
5864 static int iax2_transfer(struct ast_channel *c, const char *dest)
5865 {
5866  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
5867  struct iax_ie_data ied = { "", };
5868  char tmp[256], *context;
5870  ast_copy_string(tmp, dest, sizeof(tmp));
5871  context = strchr(tmp, '@');
5872  if (context) {
5873  *context = '\0';
5874  context++;
5875  }
5876  iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, tmp);
5877  if (context)
5878  iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, context);
5879  ast_debug(1, "Transferring '%s' to '%s'\n", ast_channel_name(c), dest);
5880  ast_queue_control_data(c, AST_CONTROL_TRANSFER, &message, sizeof(message));
5881  return send_command_locked(callno, AST_FRAME_IAX, IAX_COMMAND_TRANSFER, 0, ied.buf, ied.pos, -1);
5882 }
5883 
5884 static int iax2_getpeertrunk(struct ast_sockaddr addr)
5885 {
5886  struct iax2_peer *peer;
5887  int res = 0;
5888  struct ao2_iterator i;
5889 
5890  i = ao2_iterator_init(peers, 0);
5891  while ((peer = ao2_iterator_next(&i))) {
5892 
5893  if (!ast_sockaddr_cmp(&peer->addr, &addr)) {
5894  res = ast_test_flag64(peer, IAX_TRUNK);
5895  peer_unref(peer);
5896  break;
5897  }
5898  peer_unref(peer);
5899  }
5901 
5902  return res;
5903 }
5904 
5905 /*! \brief Create new call, interface with the PBX core */
5906 static struct ast_channel *ast_iax2_new(int callno, int state, iax2_format capability,
5907  struct iax2_codec_pref *prefs, const struct ast_assigned_ids *assignedids,
5908  const struct ast_channel *requestor, unsigned int cachable)
5909 {
5910  struct ast_channel *tmp = NULL;
5911  struct chan_iax2_pvt *i;
5912  struct iax2_peer *peer;
5913  struct ast_variable *v = NULL;
5914  struct ast_format_cap *native;
5915  struct ast_format *tmpfmt;
5916  ast_callid callid;
5917  char *peer_name = NULL;
5918 
5919  if (!(i = iaxs[callno])) {
5920  ast_log(LOG_WARNING, "No IAX2 pvt found for callno '%d' !\n", callno);
5921  return NULL;
5922  }
5923 
5924  if (!capability) {
5925  ast_log(LOG_WARNING, "No formats specified for call to: IAX2/%s-%d\n",
5926  i->host, i->callno);
5927  return NULL;
5928  }
5930  if (!native) {
5931  return NULL;
5932  }
5933  if (iax2_codec_pref_best_bitfield2cap(capability, prefs, native)
5934  || !ast_format_cap_count(native)) {
5935  ast_log(LOG_WARNING, "No requested formats available for call to: IAX2/%s-%d\n",
5936  i->host, i->callno);
5937  ao2_ref(native, -1);
5938  return NULL;
5939  }
5940 
5941  if (!ast_strlen_zero(i->peer)) {
5942  peer_name = ast_strdupa(i->peer);
5943  } else if (!ast_strlen_zero(i->host)) {
5944  peer_name = ast_strdupa(i->host);
5945  }
5946 
5947  /* Don't hold call lock while making a channel or looking up a peer */
5948  ast_mutex_unlock(&iaxsl[callno]);
5949 
5950  if (!ast_strlen_zero(peer_name)) {
5951  peer = find_peer(peer_name, 1);
5952  if (peer && peer->endpoint) {
5953  tmp = ast_channel_alloc_with_endpoint(1, state, i->cid_num, i->cid_name,
5954  i->accountcode, i->exten, i->context, assignedids, requestor,
5955  i->amaflags, peer->endpoint, "IAX2/%s-%d", i->host, i->callno);
5956  }
5957  ao2_cleanup(peer);
5958  }
5959 
5960  if (!tmp) {
5961  tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, i->accountcode,
5962  i->exten, i->context, assignedids, requestor, i->amaflags, "IAX2/%s-%d",
5963  i->host, i->callno);
5964  }
5965 
5966  ast_mutex_lock(&iaxsl[callno]);
5967  if (i != iaxs[callno]) {
5968  if (tmp) {
5969  /* unlock and relock iaxsl[callno] to preserve locking order */
5970  ast_mutex_unlock(&iaxsl[callno]);
5971  ast_channel_unlock(tmp);
5972  tmp = ast_channel_release(tmp);
5973  ast_mutex_lock(&iaxsl[callno]);
5974  }
5975  ao2_ref(native, -1);
5976  return NULL;
5977  }
5978  if (!tmp) {
5979  ao2_ref(native, -1);
5980  return NULL;
5981  }
5982 
5984 
5985  if ((callid = iaxs[callno]->callid)) {
5986  ast_channel_callid_set(tmp, callid);
5987  }
5988 
5989  ast_channel_tech_set(tmp, &iax2_tech);
5990 
5991  /* We can support any format by default, until we get restricted */
5992  ast_channel_nativeformats_set(tmp, native);
5993  tmpfmt = ast_format_cap_get_format(native, 0);
5994 
5995  ast_channel_set_readformat(tmp, tmpfmt);
5996  ast_channel_set_rawreadformat(tmp, tmpfmt);
5997  ast_channel_set_writeformat(tmp, tmpfmt);
5998  ast_channel_set_rawwriteformat(tmp, tmpfmt);
5999 
6000  ao2_ref(tmpfmt, -1);
6001  ao2_ref(native, -1);
6002 
6003  ast_channel_tech_pvt_set(tmp, CALLNO_TO_PTR(i->callno));
6004 
6005  if (!ast_strlen_zero(i->parkinglot))
6006  ast_channel_parkinglot_set(tmp, i->parkinglot);
6007  /* Don't use ast_set_callerid() here because it will
6008  * generate a NewCallerID event before the NewChannel event */
6009  if (!ast_strlen_zero(i->ani)) {
6010  ast_channel_caller(tmp)->ani.number.valid = 1;
6011  ast_channel_caller(tmp)->ani.number.str = ast_strdup(i->ani);
6012  } else if (!ast_strlen_zero(i->cid_num)) {
6013  ast_channel_caller(tmp)->ani.number.valid = 1;
6014  ast_channel_caller(tmp)->ani.number.str = ast_strdup(i->cid_num);
6015  }
6016  ast_channel_dialed(tmp)->number.str = ast_strdup(i->dnid);
6017  if (!ast_strlen_zero(i->rdnis)) {
6018  ast_channel_redirecting(tmp)->from.number.valid = 1;
6019  ast_channel_redirecting(tmp)->from.number.str = ast_strdup(i->rdnis);
6020  }
6021  ast_channel_caller(tmp)->ani2 = i->calling_ani2;
6022  ast_channel_caller(tmp)->id.name.presentation = i->calling_pres;
6023  ast_channel_caller(tmp)->id.number.presentation = i->calling_pres;
6024  ast_channel_caller(tmp)->id.number.plan = i->calling_ton;
6025  ast_channel_dialed(tmp)->transit_network_select = i->calling_tns;
6026  if (!ast_strlen_zero(i->language))
6027  ast_channel_language_set(tmp, i->language);
6028  if (!ast_strlen_zero(i->accountcode))
6029  ast_channel_accountcode_set(tmp, i->accountcode);
6030  if (i->amaflags)
6031  ast_channel_amaflags_set(tmp, i->amaflags);
6032  ast_channel_context_set(tmp, i->context);
6033  ast_channel_exten_set(tmp, i->exten);
6034  if (i->adsi)
6035  ast_channel_adsicpe_set(tmp, i->peeradsicpe);
6036  else
6037  ast_channel_adsicpe_set(tmp, AST_ADSI_UNAVAILABLE);
6038  i->owner = tmp;
6039  i->capability = capability;
6040 
6041  if (!cachable) {
6042  ast_set_flag(ast_channel_flags(tmp), AST_FLAG_DISABLE_DEVSTATE_CACHE);
6043  }
6044 
6045  /* Set inherited variables */
6046  if (i->vars) {
6047  for (v = i->vars ; v ; v = v->next)
6048  pbx_builtin_setvar_helper(tmp, v->name, v->value);
6049  }
6050  if (i->iaxvars) {
6051  struct ast_datastore *variablestore;
6052  struct ast_variable *var, *prev = NULL;
6053  AST_LIST_HEAD(, ast_var_t) *varlist;
6054  ast_debug(1, "Loading up the channel with IAXVARs\n");
6055  varlist = ast_calloc(1, sizeof(*varlist));
6056  variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
6057  if (variablestore && varlist) {
6058  variablestore->data = varlist;
6059  variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
6060  AST_LIST_HEAD_INIT(varlist);
6061  for (var = i->iaxvars; var; var = var->next) {
6062  struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
6063  if (prev)
6064  ast_free(prev);
6065  prev = var;
6066  if (!newvar) {
6067  /* Don't abort list traversal, as this would leave i->iaxvars in an inconsistent state. */
6068  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
6069  } else {
6070  AST_LIST_INSERT_TAIL(varlist, newvar, entries);
6071  }
6072  }
6073  if (prev)
6074  ast_free(prev);
6075  i->iaxvars = NULL;
6076  ast_channel_datastore_add(i->owner, variablestore);
6077  } else {
6078  if (variablestore) {
6079  ast_datastore_free(variablestore);
6080  }
6081  if (varlist) {
6082  ast_free(varlist);
6083  }
6084  }
6085  }
6086 
6088  ast_channel_unlock(tmp);
6089 
6090  if (state != AST_STATE_DOWN) {
6091  if (ast_pbx_start(tmp)) {
6092  ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
6093  /* unlock and relock iaxsl[callno] to preserve locking order */
6094  ast_mutex_unlock(&iaxsl[callno]);
6095  ast_hangup(tmp);
6096  ast_mutex_lock(&iaxsl[callno]);
6097  return NULL;
6098  }
6099  }
6100 
6102  return tmp;
6103 }
6104 
6105 static unsigned int calc_txpeerstamp(struct iax2_trunk_peer *tpeer, int sampms, struct timeval *now)
6106 {
6107  unsigned long int mssincetx; /* unsigned to handle overflows */
6108  long int ms, pred;
6109 
6110  tpeer->trunkact = *now;
6111  mssincetx = ast_tvdiff_ms(*now, tpeer->lasttxtime);
6112  if (mssincetx > 5000 || ast_tvzero(tpeer->txtrunktime)) {
6113  /* If it's been at least 5 seconds since the last time we transmitted on this trunk, reset our timers */
6114  tpeer->txtrunktime = *now;
6115  tpeer->lastsent = 999999;
6116  }
6117  /* Update last transmit time now */
6118  tpeer->lasttxtime = *now;
6119 
6120  /* Calculate ms offset */
6121  ms = ast_tvdiff_ms(*now, tpeer->txtrunktime);
6122  /* Predict from last value */
6123  pred = tpeer->lastsent + sampms;
6124  if (labs(ms - pred) < MAX_TIMESTAMP_SKEW)
6125  ms = pred;
6126 
6127  /* We never send the same timestamp twice, so fudge a little if we must */
6128  if (ms == tpeer->lastsent)
6129  ms = tpeer->lastsent + 1;
6130  tpeer->lastsent = ms;
6131  return ms;
6132 }
6133 
6134 static unsigned int fix_peerts(struct timeval *rxtrunktime, int callno, unsigned int ts)
6135 {
6136  long ms; /* NOT unsigned */
6137  if (ast_tvzero(iaxs[callno]->rxcore)) {
6138  /* Initialize rxcore time if appropriate */
6139  iaxs[callno]->rxcore = ast_tvnow();
6140  /* Round to nearest 20ms so traces look pretty */
6141  iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
6142  }
6143  /* Calculate difference between trunk and channel */
6144  ms = ast_tvdiff_ms(*rxtrunktime, iaxs[callno]->rxcore);
6145  /* Return as the sum of trunk time and the difference between trunk and real time */
6146  return ms + ts;
6147 }
6148 
6149 static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, struct ast_frame *f)
6150 {
6151  int ms;
6152  int voice = 0;
6153  int genuine = 0;
6154  int adjust;
6155  int rate = 0;
6156  struct timeval *delivery = NULL;
6157 
6158 
6159  /* What sort of frame do we have?: voice is self-explanatory
6160  "genuine" means an IAX frame - things like LAGRQ/RP, PING/PONG, ACK
6161  non-genuine frames are CONTROL frames [ringing etc], DTMF
6162  The "genuine" distinction is needed because genuine frames must get a clock-based timestamp,
6163  the others need a timestamp slaved to the voice frames so that they go in sequence
6164  */
6165  if (f->frametype == AST_FRAME_VOICE) {
6166  voice = 1;
6167  rate = ast_format_get_sample_rate(f->subclass.format) / 1000;
6168  delivery = &f->delivery;
6169  } else if (f->frametype == AST_FRAME_IAX) {
6170  genuine = 1;
6171  } else if (f->frametype == AST_FRAME_CNG) {
6172  p->notsilenttx = 0;
6173  }
6174 
6175  if (ast_tvzero(p->offset)) {
6176  p->offset = ast_tvnow();
6177  /* Round to nearest 20ms for nice looking traces */
6178  p->offset.tv_usec -= p->offset.tv_usec % 20000;
6179  }
6180  /* If the timestamp is specified, just send it as is */
6181  if (ts)
6182  return ts;
6183  /* If we have a time that the frame arrived, always use it to make our timestamp */
6184  if (delivery && !ast_tvzero(*delivery)) {
6185  ms = ast_tvdiff_ms(*delivery, p->offset);
6186  if (ms < 0) {
6187  ms = 0;
6188  }
6189  if (iaxdebug)
6190  ast_debug(3, "calc_timestamp: call %d/%d: Timestamp slaved to delivery time\n", p->callno, iaxs[p->callno]->peercallno);
6191  } else {
6192  ms = ast_tvdiff_ms(ast_tvnow(), p->offset);
6193  if (ms < 0)
6194  ms = 0;
6195  if (voice) {
6196  /* On a voice frame, use predicted values if appropriate */
6197  adjust = (ms - p->nextpred);
6198  if (p->notsilenttx && abs(adjust) <= MAX_TIMESTAMP_SKEW) {
6199  /* Adjust our txcore, keeping voice and non-voice synchronized */
6200  /* AN EXPLANATION:
6201  When we send voice, we usually send "calculated" timestamps worked out
6202  on the basis of the number of samples sent. When we send other frames,
6203  we usually send timestamps worked out from the real clock.
6204  The problem is that they can tend to drift out of step because the
6205  source channel's clock and our clock may not be exactly at the same rate.
6206  We fix this by continuously "tweaking" p->offset. p->offset is "time zero"
6207  for this call. Moving it adjusts timestamps for non-voice frames.
6208  We make the adjustment in the style of a moving average. Each time we
6209  adjust p->offset by 10% of the difference between our clock-derived
6210  timestamp and the predicted timestamp. That's why you see "10000"
6211  below even though IAX2 timestamps are in milliseconds.
6212  The use of a moving average avoids offset moving too radically.
6213  Generally, "adjust" roams back and forth around 0, with offset hardly
6214  changing at all. But if a consistent different starts to develop it
6215  will be eliminated over the course of 10 frames (200-300msecs)
6216  */
6217  if (adjust < 0)
6218  p->offset = ast_tvsub(p->offset, ast_samp2tv(abs(adjust), 10000));
6219  else if (adjust > 0)
6220  p->offset = ast_tvadd(p->offset, ast_samp2tv(adjust, 10000));
6221 
6222  if (!p->nextpred) {
6223  p->nextpred = ms; /*f->samples / rate;*/
6224  if (p->nextpred <= p->lastsent)
6225  p->nextpred = p->lastsent + 3;
6226  }
6227  ms = p->nextpred;
6228  } else {
6229  /* in this case, just use the actual
6230  * time, since we're either way off
6231  * (shouldn't happen), or we're ending a
6232  * silent period -- and seed the next
6233  * predicted time. Also, round ms to the
6234  * next multiple of frame size (so our
6235  * silent periods are multiples of
6236  * frame size too) */
6237 
6238  if (iaxdebug && abs(adjust) > MAX_TIMESTAMP_SKEW )
6239  ast_debug(1, "predicted timestamp skew (%d) > max (%d), using real ts instead.\n",
6240  abs(adjust), MAX_TIMESTAMP_SKEW);
6241 
6242  if (f->samples >= rate) /* check to make sure we don't core dump */
6243  {
6244  int diff = ms % (f->samples / rate);
6245  if (diff)
6246  ms += f->samples/rate - diff;
6247  }
6248 
6249  p->nextpred = ms;
6250  p->notsilenttx = 1;
6251  }
6252  } else if ( f->frametype == AST_FRAME_VIDEO ) {
6253  /*
6254  * IAX2 draft 03 says that timestamps MUST be in order.
6255  * It does not say anything about several frames having the same timestamp
6256  * When transporting video, we can have a frame that spans multiple iax packets
6257  * (so called slices), so it would make sense to use the same timestamp for all of
6258  * them
6259  * We do want to make sure that frames don't go backwards though
6260  */
6261  if ( (unsigned int)ms < p->lastsent )
6262  ms = p->lastsent;
6263  } else {
6264  /* On a dataframe, use last value + 3 (to accomodate jitter buffer shrinking) if appropriate unless
6265  it's a genuine frame */
6266  adjust = (ms - p->lastsent);
6267  if (genuine) {
6268  /* genuine (IAX LAGRQ etc) must keep their clock-based stamps */
6269  if (ms <= p->lastsent)
6270  ms = p->lastsent + 3;
6271  } else if (abs(adjust) <= MAX_TIMESTAMP_SKEW) {
6272  /* non-genuine frames (!?) (DTMF, CONTROL) should be pulled into the predicted stream stamps */
6273  ms = p->lastsent + 3;
6274  }
6275  }
6276  }
6277  p->lastsent = ms;
6278  if (voice) {
6279  p->nextpred = p->nextpred + f->samples / rate;
6280  }
6281  return ms;
6282 }
6283 
6284 static unsigned int calc_rxstamp(struct chan_iax2_pvt *p, unsigned int offset)
6285 {
6286  /* Returns where in "receive time" we are. That is, how many ms
6287  since we received (or would have received) the frame with timestamp 0 */
6288  int ms;
6289 #ifdef IAXTESTS
6290  int jit;
6291 #endif /* IAXTESTS */
6292  /* Setup rxcore if necessary */
6293  if (ast_tvzero(p->rxcore)) {
6294  p->rxcore = ast_tvnow();
6295  if (iaxdebug)
6296  ast_debug(1, "calc_rxstamp: call=%d: rxcore set to %d.%6.6d - %ums\n",
6297  p->callno, (int)(p->rxcore.tv_sec), (int)(p->rxcore.tv_usec), offset);
6298  p->rxcore = ast_tvsub(p->rxcore, ast_samp2tv(offset, 1000));
6299 #if 1
6300  if (iaxdebug)
6301  ast_debug(1, "calc_rxstamp: call=%d: works out as %d.%6.6d\n",
6302  p->callno, (int)(p->rxcore.tv_sec),(int)( p->rxcore.tv_usec));
6303 #endif
6304  }
6305 
6306  ms = ast_tvdiff_ms(ast_tvnow(), p->rxcore);
6307 #ifdef IAXTESTS
6308  if (test_jit) {
6309  if (!test_jitpct || ((100.0 * ast_random() / (RAND_MAX + 1.0)) < test_jitpct)) {
6310  jit = (int)((float)test_jit * ast_random() / (RAND_MAX + 1.0));
6311  if ((int)(2.0 * ast_random() / (RAND_MAX + 1.0)))
6312  jit = -jit;
6313  ms += jit;
6314  }
6315  }
6316  if (test_late) {
6317  ms += test_late;
6318  test_late = 0;
6319  }
6320 #endif /* IAXTESTS */
6321  return ms;
6322 }
6323 
6324 static struct iax2_trunk_peer *find_tpeer(struct ast_sockaddr *addr, int fd)
6325 {
6326  struct iax2_trunk_peer *tpeer = NULL;
6327 
6328  /* Finds and locks trunk peer */
6330 
6331  AST_LIST_TRAVERSE(&tpeers, tpeer, list) {
6332  if (!ast_sockaddr_cmp(&tpeer->addr, addr)) {
6333  ast_mutex_lock(&tpeer->lock);
6334  break;
6335  }
6336  }
6337 
6338  if (!tpeer) {
6339  if ((tpeer = ast_calloc(1, sizeof(*tpeer)))) {
6340  ast_mutex_init(&tpeer->lock);
6341  tpeer->lastsent = 9999;
6342  ast_sockaddr_copy(&tpeer->addr, addr);
6343  tpeer->trunkact = ast_tvnow();
6344  ast_mutex_lock(&tpeer->lock);
6345  tpeer->sockfd = fd;
6346 
6347 #ifdef SO_NO_CHECK
6348  setsockopt(tpeer->sockfd, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
6349 #endif
6350  ast_debug(1, "Created trunk peer for '%s'\n", ast_sockaddr_stringify(&tpeer->addr));
6351  AST_LIST_INSERT_TAIL(&tpeers, tpeer, list);
6352  }
6353  }
6354 
6356 
6357  return tpeer;
6358 }
6359 
6360 static int iax2_trunk_queue(struct chan_iax2_pvt *pvt, struct iax_frame *fr)
6361 {
6362  struct ast_frame *f;
6363  struct iax2_trunk_peer *tpeer;
6364  void *tmp, *ptr;
6365  struct timeval now;
6366  struct ast_iax2_meta_trunk_entry *met;
6367  struct ast_iax2_meta_trunk_mini *mtm;
6368 
6369  f = &fr->af;
6370  tpeer = find_tpeer(&pvt->addr, pvt->sockfd);
6371  if (tpeer) {
6372 
6373  if (tpeer->trunkdatalen + f->datalen + 4 >= tpeer->trunkdataalloc) {
6374  /* Need to reallocate space */
6375  if (tpeer->trunkdataalloc < trunkmaxsize) {
6376  if (!(tmp = ast_realloc(tpeer->trunkdata, tpeer->trunkdataalloc + DEFAULT_TRUNKDATA + IAX2_TRUNK_PREFACE))) {
6377  ast_mutex_unlock(&tpeer->lock);
6378  return -1;
6379  }
6380 
6381  tpeer->trunkdataalloc += DEFAULT_TRUNKDATA;
6382  tpeer->trunkdata = tmp;
6383  ast_debug(1, "Expanded trunk '%s' to %u bytes\n", ast_sockaddr_stringify(&tpeer->addr), tpeer->trunkdataalloc);
6384  } else {
6385  ast_log(LOG_WARNING, "Maximum trunk data space exceeded to %s\n", ast_sockaddr_stringify(&tpeer->addr));
6386  ast_mutex_unlock(&tpeer->lock);
6387  return -1;
6388  }
6389  }
6390 
6391  /* Append to meta frame */
6392  ptr = tpeer->trunkdata + IAX2_TRUNK_PREFACE + tpeer->trunkdatalen;
6393  if (ast_test_flag64(&globalflags, IAX_TRUNKTIMESTAMPS)) {
6394  mtm = (struct ast_iax2_meta_trunk_mini *)ptr;
6395  mtm->len = htons(f->datalen);
6396  mtm->mini.callno = htons(pvt->callno);
6397  mtm->mini.ts = htons(0xffff & fr->ts);
6398  ptr += sizeof(struct ast_iax2_meta_trunk_mini);
6399  tpeer->trunkdatalen += sizeof(struct ast_iax2_meta_trunk_mini);
6400  } else {
6401  met = (struct ast_iax2_meta_trunk_entry *)ptr;
6402  /* Store call number and length in meta header */
6403  met->callno = htons(pvt->callno);
6404  met->len = htons(f->datalen);
6405  /* Advance pointers/decrease length past trunk entry header */
6406  ptr += sizeof(struct ast_iax2_meta_trunk_entry);
6407  tpeer->trunkdatalen += sizeof(struct ast_iax2_meta_trunk_entry);
6408  }
6409  /* Copy actual trunk data */
6410  memcpy(ptr, f->data.ptr, f->datalen);
6411  tpeer->trunkdatalen += f->datalen;
6412 
6413  tpeer->calls++;
6414 
6415  /* track the largest mtu we actually have sent */
6416  if (tpeer->trunkdatalen + f->datalen + 4 > trunk_maxmtu)
6417  trunk_maxmtu = tpeer->trunkdatalen + f->datalen + 4 ;
6418 
6419  /* if we have enough for a full MTU, ship it now without waiting */
6420  if (global_max_trunk_mtu > 0 && tpeer->trunkdatalen + f->datalen + 4 >= global_max_trunk_mtu) {
6421  now = ast_tvnow();
6422  send_trunk(tpeer, &now);
6423  trunk_untimed ++;
6424  }
6425 
6426  ast_mutex_unlock(&tpeer->lock);
6427  }
6428  return 0;
6429 }
6430 
6431 /* IAX2 encryption requires 16 to 32 bytes of random padding to be present
6432  * before the encryption data. This function randomizes that data. */
6433 static void build_rand_pad(unsigned char *buf, ssize_t len)
6434 {
6435  long tmp;
6436  for (tmp = ast_random(); len > 0; tmp = ast_random()) {
6437  memcpy(buf, (unsigned char *) &tmp, (len > sizeof(tmp)) ? sizeof(tmp) : len);
6438  buf += sizeof(tmp);
6439  len -= sizeof(tmp);
6440  }
6441 }
6442 
6443 static int invalid_key(ast_aes_decrypt_key *ecx)
6444 {
6445 #ifdef HAVE_OPENSSL
6446  int i;
6447  for (i = 0; i < 60; i++) {
6448  if (ecx->raw[i]) {
6449  return 0; /* stop if we encounter anything non-zero */
6450  }
6451  }
6452  /* if ast_aes_encrypt or ast_aes_decrypt is called, then we'll crash when calling AES_encrypt or AES_decrypt */
6453  return -1;
6454 #else
6455  return 0; /* Can't verify, but doesn't matter anyways */
6456 #endif
6457 }
6458 
6459 static void build_encryption_keys(const unsigned char *digest, struct chan_iax2_pvt *pvt)
6460 {
6461  build_ecx_key(digest, pvt);
6462  ast_aes_set_decrypt_key(digest, &pvt->dcx);
6463 }
6464 
6465 static void build_ecx_key(const unsigned char *digest, struct chan_iax2_pvt *pvt)
6466 {
6467  /* it is required to hold the corresponding decrypt key to our encrypt key
6468  * in the pvt struct because queued frames occasionally need to be decrypted and
6469  * re-encrypted when updated for a retransmission */
6470  build_rand_pad(pvt->semirand, sizeof(pvt->semirand));
6471  ast_aes_set_encrypt_key(digest, &pvt->ecx);
6472  ast_aes_set_decrypt_key(digest, &pvt->mydcx);
6473 }
6474 
6475 static void memcpy_decrypt(unsigned char *dst, const unsigned char *src, int len, ast_aes_decrypt_key *dcx)
6476 {
6477 #if 0
6478  /* Debug with "fake encryption" */
6479  int x;
6480  if (len % 16)
6481  ast_log(LOG_WARNING, "len should be multiple of 16, not %d!\n", len);
6482  for (x=0;x<len;x++)
6483  dst[x] = src[x] ^ 0xff;
6484 #else
6485  unsigned char lastblock[16] = { 0 };
6486  int x;
6487  while(len > 0) {
6488  ast_aes_decrypt(src, dst, dcx);
6489  for (x=0;x<16;x++)
6490  dst[x] ^= lastblock[x];
6491  memcpy(lastblock, src, sizeof(lastblock));
6492  dst += 16;
6493  src += 16;
6494  len -= 16;
6495  }
6496 #endif
6497 }
6498 
6499 static void memcpy_encrypt(unsigned char *dst, const unsigned char *src, int len, ast_aes_encrypt_key *ecx)
6500 {
6501 #if 0
6502  /* Debug with "fake encryption" */
6503  int x;
6504  if (len % 16)
6505  ast_log(LOG_WARNING, "len should be multiple of 16, not %d!\n", len);
6506  for (x=0;x<len;x++)
6507  dst[x] = src[x] ^ 0xff;
6508 #else
6509  unsigned char curblock[16] = { 0 };
6510  int x;
6511  while(len > 0) {
6512  for (x=0;x<16;x++)
6513  curblock[x] ^= src[x];
6514  ast_aes_encrypt(curblock, dst, ecx);
6515  memcpy(curblock, dst, sizeof(curblock));
6516  dst += 16;
6517  src += 16;
6518  len -= 16;
6519  }
6520 #endif
6521 }
6522 
6523 static int decode_frame(ast_aes_decrypt_key *dcx, struct ast_iax2_full_hdr *fh, struct ast_frame *f, int *datalen)
6524 {
6525  int padding;
6526  unsigned char *workspace;
6527 
6528  workspace = ast_alloca(*datalen);
6529  memset(f, 0, sizeof(*f));
6530  if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
6531  struct ast_iax2_full_enc_hdr *efh = (struct ast_iax2_full_enc_hdr *)fh;
6532  if (*datalen < 16 + sizeof(struct ast_iax2_full_hdr))
6533  return -1;
6534  /* Decrypt */
6535  memcpy_decrypt(workspace, efh->encdata, *datalen - sizeof(struct ast_iax2_full_enc_hdr), dcx);
6536 
6537  padding = 16 + (workspace[15] & 0x0f);
6538  if (iaxdebug)
6539  ast_debug(1, "Decoding full frame with length %d (padding = %d) (15=%02hhx)\n", *datalen, padding, workspace[15]);
6540  if (*datalen < padding + sizeof(struct ast_iax2_full_hdr))
6541  return -1;
6542 
6543  *datalen -= padding;
6544  memcpy(efh->encdata, workspace + padding, *datalen - sizeof(struct ast_iax2_full_enc_hdr));
6545  f->frametype = fh->type;
6546  if (f->frametype == AST_FRAME_VIDEO) {
6547  f->subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub & ~0x40) | ((fh->csub >> 6) & 0x1));
6548  if (!f->subclass.format) {
6550  }
6551  } else if (f->frametype == AST_FRAME_VOICE) {
6552  f->subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub));
6553  if (!f->subclass.format) {
6555  }
6556  } else {
6557  f->subclass.integer = uncompress_subclass(fh->csub);
6558  }
6559  } else {
6560  struct ast_iax2_mini_enc_hdr *efh = (struct ast_iax2_mini_enc_hdr *)fh;
6561  if (iaxdebug)
6562  ast_debug(5, "Decoding mini with length %d\n", *datalen);
6563  if (*datalen < 16 + sizeof(struct ast_iax2_mini_hdr))
6564  return -1;
6565  /* Decrypt */
6566  memcpy_decrypt(workspace, efh->encdata, *datalen - sizeof(struct ast_iax2_mini_enc_hdr), dcx);
6567  padding = 16 + (workspace[15] & 0x0f);
6568  if (*datalen < padding + sizeof(struct ast_iax2_mini_hdr))
6569  return -1;
6570  *datalen -= padding;
6571  memcpy(efh->encdata, workspace + padding, *datalen - sizeof(struct ast_iax2_mini_enc_hdr));
6572  }
6573  return 0;
6574 }
6575 
6576 static int encrypt_frame(ast_aes_encrypt_key *ecx, struct ast_iax2_full_hdr *fh, unsigned char *poo, int *datalen)
6577 {
6578  int padding;
6579  unsigned char *workspace;
6580  workspace = ast_alloca(*datalen + 32);
6581  if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
6582  struct ast_iax2_full_enc_hdr *efh = (struct ast_iax2_full_enc_hdr *)fh;
6583  if (iaxdebug)
6584  ast_debug(1, "Encoding full frame %d/%d with length %d\n", fh->type, fh->csub, *datalen);
6585  padding = 16 - ((*datalen - sizeof(struct ast_iax2_full_enc_hdr)) % 16);
6586  padding = 16 + (padding & 0xf);
6587  memcpy(workspace, poo, padding);
6588  memcpy(workspace + padding, efh->encdata, *datalen - sizeof(struct ast_iax2_full_enc_hdr));
6589  workspace[15] &= 0xf0;
6590  workspace[15] |= (padding & 0xf);
6591  if (iaxdebug)
6592  ast_debug(1, "Encoding full frame %d/%d with length %d + %d padding (15=%02hhx)\n", fh->type, fh->csub, *datalen, padding, workspace[15]);
6593  *datalen += padding;
6594  memcpy_encrypt(efh->encdata, workspace, *datalen - sizeof(struct ast_iax2_full_enc_hdr), ecx);
6595  if (*datalen >= 32 + sizeof(struct ast_iax2_full_enc_hdr))
6596  memcpy(poo, workspace + *datalen - 32, 32);
6597  } else {
6598  struct ast_iax2_mini_enc_hdr *efh = (struct ast_iax2_mini_enc_hdr *)fh;
6599  if (iaxdebug)
6600  ast_debug(5, "Encoding mini frame with length %d\n", *datalen);
6601  padding = 16 - ((*datalen - sizeof(struct ast_iax2_mini_enc_hdr)) % 16);
6602  padding = 16 + (padding & 0xf);
6603  memcpy(workspace, poo, padding);
6604  memcpy(workspace + padding, efh->encdata, *datalen - sizeof(struct ast_iax2_mini_enc_hdr));
6605  workspace[15] &= 0xf0;
6606  workspace[15] |= (padding & 0x0f);
6607  *datalen += padding;
6608  memcpy_encrypt(efh->encdata, workspace, *datalen - sizeof(struct ast_iax2_mini_enc_hdr), ecx);
6609  if (*datalen >= 32 + sizeof(struct ast_iax2_mini_enc_hdr))
6610  memcpy(poo, workspace + *datalen - 32, 32);
6611  }
6612  return 0;
6613 }
6614 
6615 static int decrypt_frame(int callno, struct ast_iax2_full_hdr *fh, struct ast_frame *f, int *datalen)
6616 {
6617  int res=-1;
6618  if (!ast_test_flag64(iaxs[callno], IAX_KEYPOPULATED)) {
6619  /* Search for possible keys, given secrets */
6620  struct MD5Context md5;
6621  unsigned char digest[16];
6622  char *tmppw, *stringp;
6623 
6624  tmppw = ast_strdupa(iaxs[callno]->secret);
6625  stringp = tmppw;
6626  while ((tmppw = strsep(&stringp, ";"))) {
6627  MD5Init(&md5);
6628  MD5Update(&md5, (unsigned char *)iaxs[callno]->challenge, strlen(iaxs[callno]->challenge));
6629  MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
6630  MD5Final(digest, &md5);
6631  build_encryption_keys(digest, iaxs[callno]);
6632  res = decode_frame(&iaxs[callno]->dcx, fh, f, datalen);
6633  if (!res) {
6634  ast_set_flag64(iaxs[callno], IAX_KEYPOPULATED);
6635  break;
6636  }
6637  }
6638  } else
6639  res = decode_frame(&iaxs[callno]->dcx, fh, f, datalen);
6640  return res;
6641 }
6642 
6643 static int iax2_send(struct chan_iax2_pvt *pvt, struct ast_frame *f, unsigned int ts, int seqno, int now, int transfer, int final)
6644 {
6645  /* Queue a packet for delivery on a given private structure. Use "ts" for
6646  timestamp, or calculate if ts is 0. Send immediately without retransmission
6647  or delayed, with retransmission */
6648  struct ast_iax2_full_hdr *fh;
6649  struct ast_iax2_mini_hdr *mh;
6650  struct ast_iax2_video_hdr *vh;
6651  struct {
6652  struct iax_frame fr2;
6653  unsigned char buffer[4096];
6654  } frb;
6655  struct iax_frame *fr;
6656  int res;
6657  int sendmini=0;
6658  unsigned int lastsent;
6659  unsigned int fts;
6660 
6661  frb.fr2.afdatalen = sizeof(frb.buffer);
6662 
6663  if (!pvt) {
6664  ast_log(LOG_WARNING, "No private structure for packet?\n");
6665  return -1;
6666  }
6667 
6668  lastsent = pvt->lastsent;
6669 
6670  /* Calculate actual timestamp */
6671  fts = calc_timestamp(pvt, ts, f);
6672 
6673  /* Bail here if this is an "interp" frame; we don't want or need to send these placeholders out
6674  * (the endpoint should detect the lost packet itself). But, we want to do this here, so that we
6675  * increment the "predicted timestamps" for voice, if we're predicting */
6676  if(f->frametype == AST_FRAME_VOICE && f->datalen == 0)
6677  return 0;
6678 #if 0
6679  ast_log(LOG_NOTICE,
6680  "f->frametype %c= AST_FRAME_VOICE, %sencrypted, %srotation scheduled...\n",
6681  *("=!" + (f->frametype == AST_FRAME_VOICE)),
6682  IAX_CALLENCRYPTED(pvt) ? "" : "not ",
6683  pvt->keyrotateid != -1 ? "" : "no "
6684  );
6685 #endif
6686  if (pvt->keyrotateid == -1 && f->frametype == AST_FRAME_VOICE && IAX_CALLENCRYPTED(pvt)) {
6687  iax2_key_rotate(pvt);
6688  }
6689 
6690  if ((ast_test_flag64(pvt, IAX_TRUNK) ||
6691  (((fts & 0xFFFF0000L) == (lastsent & 0xFFFF0000L)) ||
6692  ((fts & 0xFFFF0000L) == ((lastsent + 0x10000) & 0xFFFF0000L))))
6693  /* High two bytes are the same on timestamp, or sending on a trunk */ &&
6694  (f->frametype == AST_FRAME_VOICE)
6695  /* is a voice frame */ &&
6698  /* is the same type */ ) {
6699  /* Force immediate rather than delayed transmission */
6700  now = 1;
6701  /* Mark that mini-style frame is appropriate */
6702  sendmini = 1;
6703  }
6704  if ( f->frametype == AST_FRAME_VIDEO ) {
6705  /*
6706  * If the lower 15 bits of the timestamp roll over, or if
6707  * the video format changed then send a full frame.
6708  * Otherwise send a mini video frame
6709  */
6710  if (((fts & 0xFFFF8000L) == (pvt->lastvsent & 0xFFFF8000L)) &&
6713  ) {
6714  now = 1;
6715  sendmini = 1;
6716  } else {
6717  now = 0;
6718  sendmini = 0;
6719  }
6720  pvt->lastvsent = fts;
6721  }
6722  if (f->frametype == AST_FRAME_IAX) {
6723  /* 0x8000 marks this message as TX:, this bit will be stripped later */
6724  pvt->last_iax_message = f->subclass.integer | MARK_IAX_SUBCLASS_TX;
6725  if (!pvt->first_iax_message) {
6726  pvt->first_iax_message = pvt->last_iax_message;
6727  }
6728  }
6729  /* Allocate an iax_frame */
6730  if (now) {
6731  fr = &frb.fr2;
6732  } else
6733  fr = iax_frame_new(DIRECTION_OUTGRESS, ast_test_flag64(pvt, IAX_ENCRYPTED) ? f->datalen + 32 : f->datalen, (f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_VIDEO));
6734  if (!fr) {
6735  ast_log(LOG_WARNING, "Out of memory\n");
6736  return -1;
6737  }
6738  /* Copy our prospective frame into our immediate or retransmitted wrapper */
6739  iax_frame_wrap(fr, f);
6740 
6741  fr->ts = fts;
6742  fr->callno = pvt->callno;
6743  fr->transfer = transfer;
6744  fr->final = final;
6745  fr->encmethods = 0;
6746  if (!sendmini) {
6747  /* We need a full frame */
6748  if (seqno > -1)
6749  fr->oseqno = seqno;
6750  else
6751  fr->oseqno = pvt->oseqno++;
6752  fr->iseqno = pvt->iseqno;
6753  fh = (struct ast_iax2_full_hdr *)(fr->af.data.ptr - sizeof(struct ast_iax2_full_hdr));
6754  fh->scallno = htons(fr->callno | IAX_FLAG_FULL);
6755  fh->ts = htonl(fr->ts);
6756  fh->oseqno = fr->oseqno;
6757  if (transfer) {
6758  fh->iseqno = 0;
6759  } else
6760  fh->iseqno = fr->iseqno;
6761  /* Keep track of the last thing we've acknowledged */
6762  if (!transfer)
6763  pvt->aseqno = fr->iseqno;
6764  fh->type = fr->af.frametype & 0xFF;
6765 
6766  if (fr->af.frametype == AST_FRAME_VIDEO) {
6768  tmpfmt |= fr->af.subclass.frame_ending ? 0x1LL : 0;
6769  fh->csub = compress_subclass(tmpfmt | ((tmpfmt & 0x1LL) << 6));
6770  } else if (fr->af.frametype == AST_FRAME_VOICE) {
6771  fh->csub = compress_subclass(ast_format_compatibility_format2bitfield(fr->af.subclass.format));
6772  } else {
6773  fh->csub = compress_subclass(fr->af.subclass.integer);
6774  }
6775 
6776  if (transfer) {
6777  fr->dcallno = pvt->transfercallno;
6778  } else
6779  fr->dcallno = pvt->peercallno;
6780  fh->dcallno = htons(fr->dcallno);
6781  fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_full_hdr);
6782  fr->data = fh;
6783  fr->retries = 0;
6784  /* Retry after 2x the ping time has passed */
6785  fr->retrytime = pvt->pingtime * 2;
6786  if (fr->retrytime < MIN_RETRY_TIME)
6787  fr->retrytime = MIN_RETRY_TIME;
6788  if (fr->retrytime > MAX_RETRY_TIME)
6789  fr->retrytime = MAX_RETRY_TIME;
6790  /* Acks' don't get retried */
6791  if ((f->frametype == AST_FRAME_IAX) && (f->subclass.integer == IAX_COMMAND_ACK))
6792  fr->retries = -1;
6793  else if (f->frametype == AST_FRAME_VOICE)
6795  else if (f->frametype == AST_FRAME_VIDEO)
6797  if (ast_test_flag64(pvt, IAX_ENCRYPTED)) {
6798  if (ast_test_flag64(pvt, IAX_KEYPOPULATED)) {
6799  if (fr->transfer)
6800  iax_outputframe(fr, NULL, 2, &pvt->transfer, fr->datalen - sizeof(struct ast_iax2_full_hdr));
6801  else
6802  iax_outputframe(fr, NULL, 2, &pvt->addr, fr->datalen - sizeof(struct ast_iax2_full_hdr));
6803  encrypt_frame(&pvt->ecx, fh, pvt->semirand, &fr->datalen);
6804  fr->encmethods = pvt->encmethods;
6805  fr->ecx = pvt->ecx;
6806  fr->mydcx = pvt->mydcx;
6807  memcpy(fr->semirand, pvt->semirand, sizeof(fr->semirand));
6808  } else
6809  ast_log(LOG_WARNING, "Supposed to send packet encrypted, but no key?\n");
6810  }
6811 
6812  if (now) {
6813  res = send_packet(fr);
6814  } else
6815  res = iax2_transmit(fr);
6816  } else {
6817  if (ast_test_flag64(pvt, IAX_TRUNK)) {
6818  iax2_trunk_queue(pvt, fr);
6819  res = 0;
6820  } else if (fr->af.frametype == AST_FRAME_VIDEO) {
6821  /* Video frame have no sequence number */
6822  fr->oseqno = -1;
6823  fr->iseqno = -1;
6824  vh = (struct ast_iax2_video_hdr *)(fr->af.data.ptr - sizeof(struct ast_iax2_video_hdr));
6825  vh->zeros = 0;
6826  vh->callno = htons(0x8000 | fr->callno);
6827  vh->ts = htons((fr->ts & 0x7FFF) | (fr->af.subclass.frame_ending ? 0x8000 : 0));
6828  fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_video_hdr);
6829  fr->data = vh;
6830  fr->retries = -1;
6831  res = send_packet(fr);
6832  } else {
6833  /* Mini-frames have no sequence number */
6834  fr->oseqno = -1;
6835  fr->iseqno = -1;
6836  /* Mini frame will do */
6837  mh = (struct ast_iax2_mini_hdr *)(fr->af.data.ptr - sizeof(struct ast_iax2_mini_hdr));
6838  mh->callno = htons(fr->callno);
6839  mh->ts = htons(fr->ts & 0xFFFF);
6840  fr->datalen = fr->af.datalen + sizeof(struct ast_iax2_mini_hdr);
6841  fr->data = mh;
6842  fr->retries = -1;
6843  if (pvt->transferring == TRANSFER_MEDIAPASS)
6844  fr->transfer = 1;
6845  if (ast_test_flag64(pvt, IAX_ENCRYPTED)) {
6846  if (ast_test_flag64(pvt, IAX_KEYPOPULATED)) {
6847  encrypt_frame(&pvt->ecx, (struct ast_iax2_full_hdr *)mh, pvt->semirand, &fr->datalen);
6848  } else
6849  ast_log(LOG_WARNING, "Supposed to send packet encrypted, but no key?\n");
6850  }
6851  res = send_packet(fr);
6852  }
6853  }
6854  return res;
6855 }
6856 
6857 static char *handle_cli_iax2_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
6858 {
6859  regex_t regexbuf;
6860  int havepattern = 0;
6861 
6862 #define FORMAT "%-15.15s %-20.20s %-15.15s %-15.15s %-5.5s %-5.10s\n"
6863 #define FORMAT2 "%-15.15s %-20.20s %-15.15d %-15.15s %-5.5s %-5.10s\n"
6864 
6865  struct iax2_user *user = NULL;
6866  char auth[90];
6867  char *pstr = "";
6868  struct ao2_iterator i;
6869 
6870  switch (cmd) {
6871  case CLI_INIT:
6872  e->command = "iax2 show users [like]";
6873  e->usage =
6874  "Usage: iax2 show users [like <pattern>]\n"
6875  " Lists all known IAX2 users.\n"
6876  " Optional regular expression pattern is used to filter the user list.\n";
6877  return NULL;
6878  case CLI_GENERATE:
6879  return NULL;
6880  }
6881 
6882  switch (a->argc) {
6883  case 5:
6884  if (!strcasecmp(a->argv[3], "like")) {
6885  if (regcomp(&regexbuf, a->argv[4], REG_EXTENDED | REG_NOSUB))
6886  return CLI_SHOWUSAGE;
6887  havepattern = 1;
6888  } else
6889  return CLI_SHOWUSAGE;
6890  case 3:
6891  break;
6892  default:
6893  return CLI_SHOWUSAGE;
6894  }
6895 
6896  ast_cli(a->fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C","Codec Pref");
6897  i = ao2_iterator_init(users, 0);
6898  for (; (user = ao2_iterator_next(&i)); user_unref(user)) {
6899  if (havepattern && regexec(&regexbuf, user->name, 0, NULL, 0))
6900  continue;
6901 
6902  if (!ast_strlen_zero(user->secret)) {
6903  ast_copy_string(auth,user->secret, sizeof(auth));
6904  } else if (!ast_strlen_zero(user->inkeys)) {
6905  snprintf(auth, sizeof(auth), "Key: %-15.15s ", user->inkeys);
6906  } else
6907  ast_copy_string(auth, "-no secret-", sizeof(auth));
6908 
6909  if(ast_test_flag64(user, IAX_CODEC_NOCAP))
6910  pstr = "REQ Only";
6911  else if(ast_test_flag64(user, IAX_CODEC_NOPREFS))
6912  pstr = "Disabled";
6913  else
6914  pstr = ast_test_flag64(user, IAX_CODEC_USER_FIRST) ? "Caller" : "Host";
6915 
6916  ast_cli(a->fd, FORMAT2, user->name, auth, user->authmethods,
6917  user->contexts ? user->contexts->context : DEFAULT_CONTEXT,
6918  ast_acl_list_is_empty(user->acl) ? "No" : "Yes", pstr);
6919  }
6921 
6922  if (havepattern)
6923  regfree(&regexbuf);
6924 
6925  return CLI_SUCCESS;
6926 #undef FORMAT
6927 #undef FORMAT2
6928 }
6929 
6931  regex_t regexbuf;
6932  int havepattern;
6933  char idtext[256];
6934  int registeredonly;
6935  int peerlist;
6936  int total_peers;
6937  int online_peers;
6938  int offline_peers;
6939  int unmonitored_peers;
6940 };
6941 
6942 #define PEERS_FORMAT2 "%-15.15s %-40.40s %s %-40.40s %-9s %s %-11s %-32.32s\n"
6943 #define PEERS_FORMAT "%-15.15s %-40.40s %s %-40.40s %-6s%s %s %-11s %-32.32s\n"
6944 
6945 static void _iax2_show_peers_one(int fd, struct mansession *s, struct show_peers_context *cont, struct iax2_peer *peer)
6946 {
6947  char name[256] = "";
6948  char status[64];
6949  int retstatus;
6950  struct ast_str *encmethods = ast_str_alloca(256);
6951 
6952  char *tmp_host, *tmp_mask, *tmp_port;
6953 
6954  tmp_host = ast_strdupa(ast_sockaddr_stringify_addr(&peer->addr));
6955  tmp_mask = ast_strdupa(ast_sockaddr_stringify_addr(&peer->mask));
6956  tmp_port = ast_strdupa(ast_sockaddr_stringify_port(&peer->addr));
6957 
6958  if (!ast_strlen_zero(peer->username)) {
6959  snprintf(name, sizeof(name), "%s/%s", peer->name, peer->username);
6960  } else {
6961  ast_copy_string(name, peer->name, sizeof(name));
6962  }
6963 
6964  encmethods_to_str(peer->encmethods, &encmethods);
6965  retstatus = peer_status(peer, status, sizeof(status));
6966  if (retstatus > 0) {
6967  cont->online_peers++;
6968  } else if (!retstatus) {
6969  cont->offline_peers++;
6970  } else {
6971  cont->unmonitored_peers++;
6972  }
6973 
6974  if (s) {
6975  if (cont->peerlist) { /* IAXpeerlist */
6976  astman_append(s,
6977  "Event: PeerEntry\r\n%s"
6978  "Channeltype: IAX\r\n",
6979  cont->idtext);
6980  if (!ast_strlen_zero(peer->username)) {
6981  astman_append(s,
6982  "ObjectName: %s\r\n"
6983  "ObjectUsername: %s\r\n",
6984  peer->name,
6985  peer->username);
6986  } else {
6987  astman_append(s,
6988  "ObjectName: %s\r\n",
6989  name);
6990  }
6991  } else { /* IAXpeers */
6992  astman_append(s,
6993  "Event: PeerEntry\r\n%s"
6994  "Channeltype: IAX2\r\n"
6995  "ObjectName: %s\r\n",
6996  cont->idtext,
6997  name);
6998  }
6999  astman_append(s,
7000  "ChanObjectType: peer\r\n"
7001  "IPaddress: %s\r\n",
7002  tmp_host);
7003  if (cont->peerlist) { /* IAXpeerlist */
7004  astman_append(s,
7005  "Mask: %s\r\n"
7006  "Port: %s\r\n",
7007  tmp_mask,
7008  tmp_port);
7009  } else { /* IAXpeers */
7010  astman_append(s,
7011  "IPport: %s\r\n",
7012  tmp_port);
7013  }
7014  astman_append(s,
7015  "Dynamic: %s\r\n"
7016  "Trunk: %s\r\n"
7017  "Encryption: %s\r\n"
7018  "Status: %s\r\n",
7019  ast_test_flag64(peer, IAX_DYNAMIC) ? "yes" : "no",
7020  ast_test_flag64(peer, IAX_TRUNK) ? "yes" : "no",
7021  peer->encmethods ? ast_str_buffer(encmethods) : "no",
7022  status);
7023  if (cont->peerlist) { /* IAXpeerlist */
7024  astman_append(s, "\r\n");
7025  } else { /* IAXpeers */
7026  astman_append(s,
7027  "Description: %s\r\n\r\n",
7028  peer->description);
7029  }
7030  } else {
7031  ast_cli(fd, PEERS_FORMAT,
7032  name,
7033  tmp_host,
7034  ast_test_flag64(peer, IAX_DYNAMIC) ? "(D)" : "(S)",
7035  tmp_mask,
7036  tmp_port,
7037  ast_test_flag64(peer, IAX_TRUNK) ? "(T)" : " ",
7038  peer->encmethods ? "(E)" : " ",
7039  status,
7040  peer->description);
7041  }
7042 
7043  cont->total_peers++;
7044 }
7045 
7046 static int __iax2_show_peers(int fd, int *total, struct mansession *s, const int argc, const char * const argv[])
7047 {
7048  struct show_peers_context cont = {
7049  .havepattern = 0,
7050  .idtext = "",
7051  .registeredonly = 0,
7052 
7053  .peerlist = 0,
7054 
7055  .total_peers = 0,
7056  .online_peers = 0,
7057  .offline_peers = 0,
7058  .unmonitored_peers = 0,
7059  };
7060 
7061  struct ao2_iterator i;
7062 
7063  struct iax2_peer *peer = NULL;
7064 
7065  switch (argc) {
7066  case 6:
7067  if (!strcasecmp(argv[3], "registered"))
7068  cont.registeredonly = 1;
7069  else
7070  return RESULT_SHOWUSAGE;
7071  if (!strcasecmp(argv[4], "like")) {
7072  if (regcomp(&cont.regexbuf, argv[5], REG_EXTENDED | REG_NOSUB))
7073  return RESULT_SHOWUSAGE;
7074  cont.havepattern = 1;
7075  } else
7076  return RESULT_SHOWUSAGE;
7077  break;
7078  case 5:
7079  if (!strcasecmp(argv[3], "like")) {
7080  if (regcomp(&cont.regexbuf, argv[4], REG_EXTENDED | REG_NOSUB))
7081  return RESULT_SHOWUSAGE;
7082  cont.havepattern = 1;
7083  } else
7084  return RESULT_SHOWUSAGE;
7085  break;
7086  case 4:
7087  if (!strcasecmp(argv[3], "registered")) {
7088  cont.registeredonly = 1;
7089  } else {
7090  return RESULT_SHOWUSAGE;
7091  }
7092  break;
7093  case 3:
7094  break;
7095  default:
7096  return RESULT_SHOWUSAGE;
7097  }
7098 
7099 
7100  if (!s) {
7101  ast_cli(fd, PEERS_FORMAT2, "Name/Username", "Host", " ", "Mask", "Port", " ", "Status", "Description");
7102  }
7103 
7104  i = ao2_iterator_init(peers, 0);
7105  for (; (peer = ao2_iterator_next(&i)); peer_unref(peer)) {
7106 
7107  if (cont.registeredonly && ast_sockaddr_isnull(&peer->addr)) {
7108  continue;
7109  }
7110  if (cont.havepattern && regexec(&cont.regexbuf, peer->name, 0, NULL, 0)) {
7111  continue;
7112  }
7113 
7114  _iax2_show_peers_one(fd, s, &cont, peer);
7115 
7116  }
7118 
7119  if (!s) {
7120  ast_cli(fd,"%d iax2 peers [%d online, %d offline, %d unmonitored]\n",
7121  cont.total_peers, cont.online_peers, cont.offline_peers, cont.unmonitored_peers);
7122  }
7123 
7124  if (cont.havepattern) {
7125  regfree(&cont.regexbuf);
7126  }
7127 
7128  if (total) {
7129  *total = cont.total_peers;
7130  }
7131 
7132  return RESULT_SUCCESS;
7133 
7134 }
7135 #undef PEERS_FORMAT2
7136 #undef PEERS_FORMAT
7137 
7138 static char *handle_cli_iax2_show_threads(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7139 {
7140  struct iax2_thread *thread = NULL;
7141  time_t t;
7142  int threadcount = 0, dynamiccount = 0;
7143  char type;
7144 
7145  switch (cmd) {
7146  case CLI_INIT:
7147  e->command = "iax2 show threads";
7148  e->usage =
7149  "Usage: iax2 show threads\n"
7150  " Lists status of IAX helper threads\n";
7151  return NULL;
7152  case CLI_GENERATE:
7153  return NULL;
7154  }
7155  if (a->argc != 3)
7156  return CLI_SHOWUSAGE;
7157 
7158  ast_cli(a->fd, "IAX2 Thread Information\n");
7159  time(&t);
7160  ast_cli(a->fd, "Idle Threads:\n");
7162  AST_LIST_TRAVERSE(&idle_list, thread, list) {
7163 #ifdef DEBUG_SCHED_MULTITHREAD
7164  ast_cli(a->fd, "Thread %d: state=%u, update=%d, actions=%d, func='%s'\n",
7165  thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
7166 #else
7167  ast_cli(a->fd, "Thread %d: state=%u, update=%d, actions=%d\n",
7168  thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
7169 #endif
7170  threadcount++;
7171  }
7173  ast_cli(a->fd, "Active Threads:\n");
7175  AST_LIST_TRAVERSE(&active_list, thread, list) {
7176  if (thread->type == IAX_THREAD_TYPE_DYNAMIC)
7177  type = 'D';
7178  else
7179  type = 'P';
7180 #ifdef DEBUG_SCHED_MULTITHREAD
7181  ast_cli(a->fd, "Thread %c%d: state=%u, update=%d, actions=%d, func='%s'\n",
7182  type, thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
7183 #else
7184  ast_cli(a->fd, "Thread %c%d: state=%u, update=%d, actions=%d\n",
7185  type, thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
7186 #endif
7187  threadcount++;
7188  }
7190  ast_cli(a->fd, "Dynamic Threads:\n");
7192  AST_LIST_TRAVERSE(&dynamic_list, thread, list) {
7193 #ifdef DEBUG_SCHED_MULTITHREAD
7194  ast_cli(a->fd, "Thread %d: state=%u, update=%d, actions=%d, func='%s'\n",
7195  thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions, thread->curfunc);
7196 #else
7197  ast_cli(a->fd, "Thread %d: state=%u, update=%d, actions=%d\n",
7198  thread->threadnum, thread->iostate, (int)(t - thread->checktime), thread->actions);
7199 #endif
7200  dynamiccount++;
7201  }
7203  ast_cli(a->fd, "%d of %d threads accounted for with %d dynamic threads\n", threadcount, iaxthreadcount, dynamiccount);
7204  return CLI_SUCCESS;
7205 }
7206 
7207 static char *handle_cli_iax2_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7208 {
7209  struct iax2_peer *p;
7210 
7211  switch (cmd) {
7212  case CLI_INIT:
7213  e->command = "iax2 unregister";
7214  e->usage =
7215  "Usage: iax2 unregister <peername>\n"
7216  " Unregister (force expiration) an IAX2 peer from the registry.\n";
7217  return NULL;
7218  case CLI_GENERATE:
7219  return complete_iax2_unregister(a->line, a->word, a->pos, a->n);
7220  }
7221 
7222  if (a->argc != 3)
7223  return CLI_SHOWUSAGE;
7224 
7225  p = find_peer(a->argv[2], 1);
7226  if (p) {
7227  if (p->expire > -1) {
7228  struct iax2_peer *peer;
7229 
7230  peer = ao2_find(peers, a->argv[2], OBJ_KEY);
7231  if (peer) {
7232  expire_registry(peer_ref(peer)); /* will release its own reference when done */
7233  peer_unref(peer); /* ref from ao2_find() */
7234  ast_cli(a->fd, "Peer %s unregistered\n", a->argv[2]);
7235  } else {
7236  ast_cli(a->fd, "Peer %s not found\n", a->argv[2]);
7237  }
7238  } else {
7239  ast_cli(a->fd, "Peer %s not registered\n", a->argv[2]);
7240  }
7241  peer_unref(p);
7242  } else {
7243  ast_cli(a->fd, "Peer unknown: %s. Not unregistered\n", a->argv[2]);
7244  }
7245  return CLI_SUCCESS;
7246 }
7247 
7248 static char *complete_iax2_unregister(const char *line, const char *word, int pos, int state)
7249 {
7250  int which = 0;
7251  struct iax2_peer *p = NULL;
7252  char *res = NULL;
7253  int wordlen = strlen(word);
7254 
7255  /* 0 - iax2; 1 - unregister; 2 - <peername> */
7256  if (pos == 2) {
7257  struct ao2_iterator i = ao2_iterator_init(peers, 0);
7258  while ((p = ao2_iterator_next(&i))) {
7259  if (!strncasecmp(p->name, word, wordlen) &&
7260  ++which > state && p->expire > -1) {
7261  res = ast_strdup(p->name);
7262  peer_unref(p);
7263  break;
7264  }
7265  peer_unref(p);
7266  }
7268  }
7269 
7270  return res;
7271 }
7272 
7273 static char *handle_cli_iax2_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7274 {
7275  switch (cmd) {
7276  case CLI_INIT:
7277  e->command = "iax2 show peers";
7278  e->usage =
7279  "Usage: iax2 show peers [registered] [like <pattern>]\n"
7280  " Lists all known IAX2 peers.\n"
7281  " Optional 'registered' argument lists only peers with known addresses.\n"
7282  " Optional regular expression pattern is used to filter the peer list.\n";
7283  return NULL;
7284  case CLI_GENERATE:
7285  return NULL;
7286  }
7287 
7288  switch (__iax2_show_peers(a->fd, NULL, NULL, a->argc, a->argv)) {
7289  case RESULT_SHOWUSAGE:
7290  return CLI_SHOWUSAGE;
7291  case RESULT_FAILURE:
7292  return CLI_FAILURE;
7293  default:
7294  return CLI_SUCCESS;
7295  }
7296 }
7297 
7298 static int manager_iax2_show_netstats(struct mansession *s, const struct message *m)
7299 {
7300  ast_cli_netstats(s, -1, 0);
7301  astman_append(s, "\r\n");
7302  return RESULT_SUCCESS;
7303 }
7304 
7305 static int firmware_show_callback(struct ast_iax2_firmware_header *header,
7306  void *user_data)
7307 {
7308  int *fd = user_data;
7309 
7310  ast_cli(*fd, "%-15.15s %-15d %-15d\n",
7311  header->devname,
7312  ntohs(header->version),
7313  (int) ntohl(header->datalen));
7314 
7315  return 0;
7316 }
7317 
7318 static char *handle_cli_iax2_show_firmware(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7319 {
7320  switch (cmd) {
7321  case CLI_INIT:
7322  e->command = "iax2 show firmware";
7323  e->usage =
7324  "Usage: iax2 show firmware\n"
7325  " Lists all known IAX firmware images.\n";
7326  return NULL;
7327  case CLI_GENERATE:
7328  return NULL;
7329  }
7330 
7331  if (a->argc != 3 && a->argc != 4)
7332  return CLI_SHOWUSAGE;
7333 
7334  ast_cli(a->fd, "%-15.15s %-15.15s %-15.15s\n", "Device", "Version", "Size");
7335 
7336  iax_firmware_traverse(
7337  a->argc == 3 ? NULL : a->argv[3],
7338  firmware_show_callback,
7339  (void *) &a->fd);
7340 
7341  return CLI_SUCCESS;
7342 }
7343 
7344 /*! \brief callback to display iax peers in manager */
7345 static int manager_iax2_show_peers(struct mansession *s, const struct message *m)
7346 {
7347  static const char * const a[] = { "iax2", "show", "peers" };
7348  const char *id = astman_get_header(m,"ActionID");
7349  char idtext[256] = "";
7350  int total = 0;
7351 
7352  if (!ast_strlen_zero(id))
7353  snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
7354 
7355  astman_send_listack(s, m, "Peer status list will follow", "start");
7356 
7357  /* List the peers in separate manager events */
7358  __iax2_show_peers(-1, &total, s, 3, a);
7359 
7360  /* Send final confirmation */
7361  astman_send_list_complete_start(s, m, "PeerlistComplete", total);
7363  return 0;
7364 }
7365 
7366 /*! \brief callback to display iax peers in manager format */
7367 static int manager_iax2_show_peer_list(struct mansession *s, const struct message *m)
7368 {
7369  struct show_peers_context cont = {
7370  .havepattern = 0,
7371  .idtext = "",
7372  .registeredonly = 0,
7373 
7374  .peerlist = 1,
7375 
7376  .total_peers = 0,
7377  .online_peers = 0,
7378  .offline_peers = 0,
7379  .unmonitored_peers = 0,
7380  };
7381 
7382  struct iax2_peer *peer = NULL;
7383  struct ao2_iterator i;
7384 
7385  const char *id = astman_get_header(m,"ActionID");
7386 
7387  if (!ast_strlen_zero(id)) {
7388  snprintf(cont.idtext, sizeof(cont.idtext), "ActionID: %s\r\n", id);
7389  }
7390 
7391  astman_send_listack(s, m, "IAX Peer status list will follow", "start");
7392 
7393  i = ao2_iterator_init(peers, 0);
7394  for (; (peer = ao2_iterator_next(&i)); peer_unref(peer)) {
7395  _iax2_show_peers_one(-1, s, &cont, peer);
7396  }
7398 
7399  astman_send_list_complete_start(s, m, "PeerlistComplete", cont.total_peers);
7401 
7402  return RESULT_SUCCESS;
7403 }
7404 
7405 
7406 static char *regstate2str(int regstate)
7407 {
7408  switch(regstate) {
7409  case REG_STATE_UNREGISTERED:
7410  return "Unregistered";
7411  case REG_STATE_REGSENT:
7412  return "Request Sent";
7413  case REG_STATE_AUTHSENT:
7414  return "Auth. Sent";
7415  case REG_STATE_REGISTERED:
7416  return "Registered";
7417  case REG_STATE_REJECTED:
7418  return "Rejected";
7419  case REG_STATE_TIMEOUT:
7420  return "Timeout";
7421  case REG_STATE_NOAUTH:
7422  return "No Authentication";
7423  default:
7424  return "Unknown";
7425  }
7426 }
7427 
7428 static char *handle_cli_iax2_show_registry(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7429 {
7430 #define FORMAT2 "%-45.45s %-6.6s %-10.10s %-45.45s %8.8s %s\n"
7431 #define FORMAT "%-45.45s %-6.6s %-10.10s %-45.45s %8d %s\n"
7432 
7433  struct iax2_registry *reg = NULL;
7434  char host[80];
7435  char perceived[80];
7436  int counter = 0;
7437 
7438  switch (cmd) {
7439  case CLI_INIT:
7440  e->command = "iax2 show registry";
7441  e->usage =
7442  "Usage: iax2 show registry\n"
7443  " Lists all registration requests and status.\n";
7444  return NULL;
7445  case CLI_GENERATE:
7446  return NULL;
7447  }
7448  if (a->argc != 3)
7449  return CLI_SHOWUSAGE;
7450  ast_cli(a->fd, FORMAT2, "Host", "dnsmgr", "Username", "Perceived", "Refresh", "State");
7451  AST_LIST_LOCK(&registrations);
7452  AST_LIST_TRAVERSE(&registrations, reg, entry) {
7453  snprintf(host, sizeof(host), "%s", ast_sockaddr_stringify(&reg->addr));
7454 
7455  snprintf(perceived, sizeof(perceived), "%s", ast_sockaddr_isnull(&reg->us) ? "<Unregistered>" : ast_sockaddr_stringify(&reg->us));
7456 
7457  ast_cli(a->fd, FORMAT, host,
7458  (reg->dnsmgr) ? "Y" : "N",
7459  reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
7460  counter++;
7461  }
7462  AST_LIST_UNLOCK(&registrations);
7463  ast_cli(a->fd, "%d IAX2 registrations.\n", counter);
7464  return CLI_SUCCESS;
7465 #undef FORMAT
7466 #undef FORMAT2
7467 }
7468 
7469 static int manager_iax2_show_registry(struct mansession *s, const struct message *m)
7470 {
7471  const char *id = astman_get_header(m, "ActionID");
7472  struct iax2_registry *reg = NULL;
7473  char idtext[256] = "";
7474  char host[80] = "";
7475  char perceived[80] = "";
7476  int total = 0;
7477 
7478  if (!ast_strlen_zero(id))
7479  snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
7480 
7481  astman_send_listack(s, m, "Registrations will follow", "start");
7482 
7483  AST_LIST_LOCK(&registrations);
7484  AST_LIST_TRAVERSE(&registrations, reg, entry) {
7485  snprintf(host, sizeof(host), "%s", ast_sockaddr_stringify(&reg->addr));
7486 
7487  snprintf(perceived, sizeof(perceived), "%s", ast_sockaddr_isnull(&reg->us) ? "<Unregistered>" : ast_sockaddr_stringify(&reg->us));
7488 
7489  astman_append(s,
7490  "Event: RegistryEntry\r\n"
7491  "%s"
7492  "Host: %s\r\n"
7493  "DNSmanager: %s\r\n"
7494  "Username: %s\r\n"
7495  "Perceived: %s\r\n"
7496  "Refresh: %d\r\n"
7497  "State: %s\r\n"
7498  "\r\n", idtext, host, (reg->dnsmgr) ? "Y" : "N", reg->username, perceived,
7499  reg->refresh, regstate2str(reg->regstate));
7500 
7501  total++;
7502  }
7503  AST_LIST_UNLOCK(&registrations);
7504 
7505  astman_send_list_complete_start(s, m, "RegistrationsComplete", total);
7507 
7508  return 0;
7509 }
7510 
7511 static char *handle_cli_iax2_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7512 {
7513 #define FORMAT2 "%-20.20s %-40.40s %-10.10s %-11.11s %-11.11s %-7.7s %-6.6s %-6.6s %s %s %9s\n"
7514 #define FORMAT "%-20.20s %-40.40s %-10.10s %5.5d/%5.5d %5.5d/%5.5d %-5.5dms %-4.4dms %-4.4dms %-6.6s %s%s %3s%s\n"
7515 #define FORMATB "%-20.20s %-40.40s %-10.10s %5.5d/%5.5d %5.5d/%5.5d [Native Bridged to ID=%5.5d]\n"
7516  int x;
7517  int numchans = 0;
7518  char first_message[10] = { 0, };
7519  char last_message[10] = { 0, };
7520 
7521  switch (cmd) {
7522  case CLI_INIT:
7523  e->command = "iax2 show channels";
7524  e->usage =
7525  "Usage: iax2 show channels\n"
7526  " Lists all currently active IAX channels.\n";
7527  return NULL;
7528  case CLI_GENERATE:
7529  return NULL;
7530  }
7531 
7532  if (a->argc != 3)
7533  return CLI_SHOWUSAGE;
7534  ast_cli(a->fd, FORMAT2, "Channel", "Peer", "Username", "ID (Lo/Rem)", "Seq (Tx/Rx)", "Lag", "Jitter", "JitBuf", "Format", "FirstMsg", "LastMsg");
7535  for (x = 0; x < ARRAY_LEN(iaxs); x++) {
7536  ast_mutex_lock(&iaxsl[x]);
7537  if (iaxs[x]) {
7538  int lag, jitter, localdelay;
7539  jb_info jbinfo;
7540  if (ast_test_flag64(iaxs[x], IAX_USEJITTERBUF)) {
7541  jb_getinfo(iaxs[x]->jb, &jbinfo);
7542  jitter = jbinfo.jitter;
7543  localdelay = jbinfo.current - jbinfo.min;
7544  } else {
7545  jitter = -1;
7546  localdelay = 0;
7547  }
7548 
7549  iax_frame_subclass2str(iaxs[x]->first_iax_message & ~MARK_IAX_SUBCLASS_TX, first_message, sizeof(first_message));
7550  iax_frame_subclass2str(iaxs[x]->last_iax_message & ~MARK_IAX_SUBCLASS_TX, last_message, sizeof(last_message));
7551  lag = iaxs[x]->remote_rr.delay;
7552  ast_cli(a->fd, FORMAT,
7553  iaxs[x]->owner ? ast_channel_name(iaxs[x]->owner) : "(None)",
7554  ast_sockaddr_stringify_addr(&iaxs[x]->addr),
7555  S_OR(iaxs[x]->username, "(None)"),
7556  iaxs[x]->callno, iaxs[x]->peercallno,
7557  iaxs[x]->oseqno, iaxs[x]->iseqno,
7558  lag,
7559  jitter,
7560  localdelay,
7561  iax2_getformatname(iaxs[x]->voiceformat),
7562  (iaxs[x]->first_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
7563  first_message,
7564  (iaxs[x]->last_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
7565  last_message);
7566  numchans++;
7567  }
7568  ast_mutex_unlock(&iaxsl[x]);
7569  }
7570  ast_cli(a->fd, "%d active IAX channel%s\n", numchans, (numchans != 1) ? "s" : "");
7571  return CLI_SUCCESS;
7572 #undef FORMAT
7573 #undef FORMAT2
7574 #undef FORMATB
7575 }
7576 
7577 static int ast_cli_netstats(struct mansession *s, int fd, int limit_fmt)
7578 {
7579  int x;
7580  int numchans = 0;
7581  char first_message[10] = { 0, };
7582  char last_message[10] = { 0, };
7583 #define ACN_FORMAT1 "%-24.25s %4u %4d %4d %5d %3d %5d %4d %6d %4d %4d %5d %3d %5d %4d %6d %s%s %4s%s\n"
7584 #define ACN_FORMAT2 "%s %u %d %d %d %d %d %d %d %d %d %d %d %d %d %d %s%s %s%s\n"
7585  for (x = 0; x < ARRAY_LEN(iaxs); x++) {
7586  ast_mutex_lock(&iaxsl[x]);
7587  if (iaxs[x]) {
7588  int localjitter, localdelay, locallost, locallosspct, localdropped, localooo;
7589  jb_info jbinfo;
7590  iax_frame_subclass2str(iaxs[x]->first_iax_message & ~MARK_IAX_SUBCLASS_TX, first_message, sizeof(first_message));
7591  iax_frame_subclass2str(iaxs[x]->last_iax_message & ~MARK_IAX_SUBCLASS_TX, last_message, sizeof(last_message));
7592 
7593  if(ast_test_flag64(iaxs[x], IAX_USEJITTERBUF)) {
7594  jb_getinfo(iaxs[x]->jb, &jbinfo);
7595  localjitter = jbinfo.jitter;
7596  localdelay = jbinfo.current - jbinfo.min;
7597  locallost = jbinfo.frames_lost;
7598  locallosspct = jbinfo.losspct/1000;
7599  localdropped = jbinfo.frames_dropped;
7600  localooo = jbinfo.frames_ooo;
7601  } else {
7602  localjitter = -1;
7603  localdelay = 0;
7604  locallost = -1;
7605  locallosspct = -1;
7606  localdropped = 0;
7607  localooo = -1;
7608  }
7609  if (s)
7610  astman_append(s, limit_fmt ? ACN_FORMAT1 : ACN_FORMAT2,
7611  iaxs[x]->owner ? ast_channel_name(iaxs[x]->owner) : "(None)",
7612  iaxs[x]->pingtime,
7613  localjitter,
7614  localdelay,
7615  locallost,
7616  locallosspct,
7617  localdropped,
7618  localooo,
7619  iaxs[x]->frames_received/1000,
7620  iaxs[x]->remote_rr.jitter,
7621  iaxs[x]->remote_rr.delay,
7622  iaxs[x]->remote_rr.losscnt,
7623  iaxs[x]->remote_rr.losspct,
7624  iaxs[x]->remote_rr.dropped,
7625  iaxs[x]->remote_rr.ooo,
7626  iaxs[x]->remote_rr.packets/1000,
7627  (iaxs[x]->first_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
7628  first_message,
7629  (iaxs[x]->last_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
7630  last_message);
7631  else
7632  ast_cli(fd, limit_fmt ? ACN_FORMAT1 : ACN_FORMAT2,
7633  iaxs[x]->owner ? ast_channel_name(iaxs[x]->owner) : "(None)",
7634  iaxs[x]->pingtime,
7635  localjitter,
7636  localdelay,
7637  locallost,
7638  locallosspct,
7639  localdropped,
7640  localooo,
7641  iaxs[x]->frames_received/1000,
7642  iaxs[x]->remote_rr.jitter,
7643  iaxs[x]->remote_rr.delay,
7644  iaxs[x]->remote_rr.losscnt,
7645  iaxs[x]->remote_rr.losspct,
7646  iaxs[x]->remote_rr.dropped,
7647  iaxs[x]->remote_rr.ooo,
7648  iaxs[x]->remote_rr.packets/1000,
7649  (iaxs[x]->first_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
7650  first_message,
7651  (iaxs[x]->last_iax_message & MARK_IAX_SUBCLASS_TX) ? "Tx:" : "Rx:",
7652  last_message);
7653  numchans++;
7654  }
7655  ast_mutex_unlock(&iaxsl[x]);
7656  }
7657 
7658  return numchans;
7659 }
7660 
7661 static char *handle_cli_iax2_show_netstats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7662 {
7663  int numchans = 0;
7664 
7665  switch (cmd) {
7666  case CLI_INIT:
7667  e->command = "iax2 show netstats";
7668  e->usage =
7669  "Usage: iax2 show netstats\n"
7670  " Lists network status for all currently active IAX channels.\n";
7671  return NULL;
7672  case CLI_GENERATE:
7673  return NULL;
7674  }
7675  if (a->argc != 3)
7676  return CLI_SHOWUSAGE;
7677  ast_cli(a->fd, " -------- LOCAL --------------------- -------- REMOTE --------------------\n");
7678  ast_cli(a->fd, "Channel RTT Jit Del Lost %% Drop OOO Kpkts Jit Del Lost %% Drop OOO Kpkts FirstMsg LastMsg\n");
7679  numchans = ast_cli_netstats(NULL, a->fd, 1);
7680  ast_cli(a->fd, "%d active IAX channel%s\n", numchans, (numchans != 1) ? "s" : "");
7681  return CLI_SUCCESS;
7682 }
7683 
7684 static char *handle_cli_iax2_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7685 {
7686  switch (cmd) {
7687  case CLI_INIT:
7688  e->command = "iax2 set debug {on|off|peer}";
7689  e->usage =
7690  "Usage: iax2 set debug {on|off|peer peername}\n"
7691  " Enables/Disables dumping of IAX packets for debugging purposes.\n";
7692  return NULL;
7693  case CLI_GENERATE:
7694  if (a->pos == 4 && !strcasecmp(a->argv[3], "peer"))
7695  return complete_iax2_peers(a->line, a->word, a->pos, a->n, 0);
7696  return NULL;
7697  }
7698 
7699  if (a->argc < e->args || a->argc > e->args + 1)
7700  return CLI_SHOWUSAGE;
7701 
7702  if (!strcasecmp(a->argv[3], "peer")) {
7703  struct iax2_peer *peer;
7704 
7705  if (a->argc != e->args + 1)
7706  return CLI_SHOWUSAGE;
7707 
7708  peer = find_peer(a->argv[4], 1);
7709 
7710  if (!peer) {
7711  ast_cli(a->fd, "IAX2 peer '%s' does not exist\n", a->argv[e->args-1]);
7712  return CLI_FAILURE;
7713  }
7714 
7715  ast_sockaddr_copy(&debugaddr, &peer->addr);
7716 
7717  ast_cli(a->fd, "IAX2 Debugging Enabled for IP: %s\n", ast_sockaddr_stringify_port(&debugaddr));
7718 
7719  ao2_ref(peer, -1);
7720  } else if (!strncasecmp(a->argv[3], "on", 2)) {
7721  iaxdebug = 1;
7722  ast_cli(a->fd, "IAX2 Debugging Enabled\n");
7723  } else {
7724  iaxdebug = 0;
7725  memset(&debugaddr, 0, sizeof(debugaddr));
7726  ast_cli(a->fd, "IAX2 Debugging Disabled\n");
7727  }
7728  return CLI_SUCCESS;
7729 }
7730 
7731 static char *handle_cli_iax2_set_debug_trunk(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7732 {
7733  switch (cmd) {
7734  case CLI_INIT:
7735  e->command = "iax2 set debug trunk {on|off}";
7736  e->usage =
7737  "Usage: iax2 set debug trunk {on|off}\n"
7738  " Enables/Disables debugging of IAX trunking\n";
7739  return NULL;
7740  case CLI_GENERATE:
7741  return NULL;
7742  }
7743 
7744  if (a->argc != e->args)
7745  return CLI_SHOWUSAGE;
7746 
7747  if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
7748  iaxtrunkdebug = 1;
7749  ast_cli(a->fd, "IAX2 Trunk Debugging Enabled\n");
7750  } else {
7751  iaxtrunkdebug = 0;
7752  ast_cli(a->fd, "IAX2 Trunk Debugging Disabled\n");
7753  }
7754  return CLI_SUCCESS;
7755 }
7756 
7757 static char *handle_cli_iax2_set_debug_jb(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
7758 {
7759  switch (cmd) {
7760  case CLI_INIT:
7761  e->command = "iax2 set debug jb {on|off}";
7762  e->usage =
7763  "Usage: iax2 set debug jb {on|off}\n"
7764  " Enables/Disables jitterbuffer debugging information\n";
7765  return NULL;
7766  case CLI_GENERATE:
7767  return NULL;
7768  }
7769 
7770  if (a->argc != e->args)
7771  return CLI_SHOWUSAGE;
7772 
7773  if (!strncasecmp(a->argv[e->args -1], "on", 2)) {
7774  jb_setoutput(jb_error_output, jb_warning_output, jb_debug_output);
7775  ast_cli(a->fd, "IAX2 Jitterbuffer Debugging Enabled\n");
7776  } else {
7777  jb_setoutput(jb_error_output, jb_warning_output, NULL);
7778  ast_cli(a->fd, "IAX2 Jitterbuffer Debugging Disabled\n");
7779  }
7780  return CLI_SUCCESS;
7781 }
7782 
7783 static int iax2_write(struct ast_channel *c, struct ast_frame *f)
7784 {
7785  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(c));
7786  int res = -1;
7787  ast_mutex_lock(&iaxsl[callno]);
7788  if (iaxs[callno]) {
7789  /* If there's an outstanding error, return failure now */
7790  if (!iaxs[callno]->error) {
7791  if (ast_test_flag64(iaxs[callno], IAX_ALREADYGONE))
7792  res = 0;
7793  /* Don't waste bandwidth sending null frames */
7794  else if (f->frametype == AST_FRAME_NULL)
7795  res = 0;
7796  else if ((f->frametype == AST_FRAME_VOICE) && ast_test_flag64(iaxs[callno], IAX_QUELCH))
7797  res = 0;
7798  else if (!ast_test_flag(&iaxs[callno]->state, IAX_STATE_STARTED))
7799  res = 0;
7800  else
7801  /* Simple, just queue for transmission */
7802  res = iax2_send(iaxs[callno], f, 0, -1, 0, 0, 0);
7803  } else {
7804  ast_debug(1, "Write error: %s\n", strerror(errno));
7805  }
7806  }
7807  /* If it's already gone, just return */
7808  ast_mutex_unlock(&iaxsl[callno]);
7809  return res;
7810 }
7811 
7812 static int __send_command(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno,
7813  int now, int transfer, int final)
7814 {
7815  struct ast_frame f = { 0, };
7816  int res = 0;
7817 
7818  f.frametype = type;
7819  f.subclass.integer = command;
7820  f.datalen = datalen;
7821  f.src = __FUNCTION__;
7822  f.data.ptr = (void *) data;
7823 
7824  if ((res = queue_signalling(i, &f)) <= 0) {
7825  return res;
7826  }
7827 
7828  return iax2_send(i, &f, ts, seqno, now, transfer, final);
7829 }
7830 
7831 static int send_command(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno)
7832 {
7833  if (type == AST_FRAME_CONTROL && !iax2_is_control_frame_allowed(command)) {
7834  /* Control frame should not go out on the wire. */
7835  ast_debug(2, "Callno %d: Blocked sending control frame %d.\n",
7836  i->callno, command);
7837  return 0;
7838  }
7839  return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 0);
7840 }
7841 
7842 static int send_command_locked(unsigned short callno, char type, int command, unsigned int ts, const unsigned char *data, int datalen, int seqno)
7843 {
7844  int res;
7845  ast_mutex_lock(&iaxsl[callno]);
7846  res = send_command(iaxs[callno], type, command, ts, data, datalen, seqno);
7847  ast_mutex_unlock(&iaxsl[callno]);
7848  return res;
7849 }
7850 
7851 /*!
7852  * \note Since this function calls iax2_predestroy() -> iax2_queue_hangup(),
7853  * the pvt struct for the given call number may disappear during its
7854  * execution.
7855  */
7856 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)
7857 {
7858  int call_num = i->callno;
7859  /* It is assumed that the callno has already been locked */
7860  iax2_predestroy(i->callno);
7861  if (!iaxs[call_num])
7862  return -1;
7863  return __send_command(i, type, command, ts, data, datalen, seqno, 0, 0, 1);
7864 }
7865 
7866 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)
7867 {
7868  return __send_command(i, type, command, ts, data, datalen, seqno, 1, 0, 0);
7869 }
7870 
7871 static int send_command_transfer(struct chan_iax2_pvt *i, char type, int command, unsigned int ts, const unsigned char *data, int datalen)
7872 {
7873  return __send_command(i, type, command, ts, data, datalen, 0, 0, 1, 0);
7874 }
7875 
7876 static int apply_context(struct iax2_context *con, const char *context)
7877 {
7878  while(con) {
7879  if (!strcmp(con->context, context) || !strcmp(con->context, "*"))
7880  return -1;
7881  con = con->next;
7882  }
7883  return 0;
7884 }
7885 
7886 
7887 static int check_access(int callno, struct ast_sockaddr *addr, struct iax_ies *ies)
7888 {
7889  /* Start pessimistic */
7890  int res = -1;
7891  int version = 2;
7892  struct iax2_user *user = NULL, *best = NULL;
7893  int bestscore = 0;
7894  int gotcapability = 0;
7895  struct ast_variable *v = NULL, *tmpvar = NULL;
7896  struct ao2_iterator i;
7897 
7898  if (!iaxs[callno])
7899  return res;
7900  if (ies->called_number)
7901  ast_string_field_set(iaxs[callno], exten, ies->called_number);
7902  if (ies->calling_number) {
7903  if (ast_test_flag64(&globalflags, IAX_SHRINKCALLERID)) {
7904  ast_shrink_phone_number(ies->calling_number);
7905  }
7906  ast_string_field_set(iaxs[callno], cid_num, ies->calling_number);
7907  }
7908  if (ies->calling_name)
7909  ast_string_field_set(iaxs[callno], cid_name, ies->calling_name);
7910  if (ies->calling_ani)
7911  ast_string_field_set(iaxs[callno], ani, ies->calling_ani);
7912  if (ies->dnid)
7913  ast_string_field_set(iaxs[callno], dnid, ies->dnid);
7914  if (ies->rdnis)
7915  ast_string_field_set(iaxs[callno], rdnis, ies->rdnis);
7916  if (ies->called_context)
7917  ast_string_field_set(iaxs[callno], context, ies->called_context);
7918  if (ies->language)
7919  ast_string_field_set(iaxs[callno], language, ies->language);
7920  if (ies->username)
7921  ast_string_field_set(iaxs[callno], username, ies->username);
7922  if (ies->calling_ton > -1)
7923  iaxs[callno]->calling_ton = ies->calling_ton;
7924  if (ies->calling_tns > -1)
7925  iaxs[callno]->calling_tns = ies->calling_tns;
7926  if (ies->calling_pres > -1)
7927  iaxs[callno]->calling_pres = ies->calling_pres;
7928  if (ies->calling_ani2 > -1)
7929  iaxs[callno]->calling_ani2 = ies->calling_ani2;
7930  if (ies->format)
7931  iaxs[callno]->peerformat = ies->format;
7932  if (ies->adsicpe)
7933  iaxs[callno]->peeradsicpe = ies->adsicpe;
7934  if (ies->capability) {
7935  gotcapability = 1;
7936  iaxs[callno]->peercapability = ies->capability;
7937  }
7938  if (ies->version)
7939  version = ies->version;
7940 
7941  /* Use provided preferences until told otherwise for actual preferences */
7942  if (ies->codec_prefs) {
7943  iax2_codec_pref_convert(&iaxs[callno]->rprefs, ies->codec_prefs, 32, 0);
7944  } else {
7945  memset(&iaxs[callno]->rprefs, 0, sizeof(iaxs[callno]->rprefs));
7946  }
7947  iaxs[callno]->prefs = iaxs[callno]->rprefs;
7948 
7949  if (!gotcapability) {
7950  iaxs[callno]->peercapability = iaxs[callno]->peerformat;
7951  }
7952  if (version > IAX_PROTO_VERSION) {
7953  ast_log(LOG_WARNING, "Peer '%s' has too new a protocol version (%d) for me\n",
7954  ast_sockaddr_stringify_addr(addr), version);
7955  return res;
7956  }
7957  /* Search the userlist for a compatible entry, and fill in the rest */
7958  i = ao2_iterator_init(users, 0);
7959  while ((user = ao2_iterator_next(&i))) {
7960  if ((ast_strlen_zero(iaxs[callno]->username) || /* No username specified */
7961  !strcmp(iaxs[callno]->username, user->name)) /* Or this username specified */
7962  && (ast_apply_acl(user->acl, addr, "IAX2 user ACL: ") == AST_SENSE_ALLOW) /* Access is permitted from this IP */
7963  && (ast_strlen_zero(iaxs[callno]->context) || /* No context specified */
7964  apply_context(user->contexts, iaxs[callno]->context))) { /* Context is permitted */
7965  if (!ast_strlen_zero(iaxs[callno]->username)) {
7966  /* Exact match, stop right now. */
7967  if (best)
7968  user_unref(best);
7969  best = user;
7970  break;
7971  } else if (ast_strlen_zero(user->secret) && ast_strlen_zero(user->dbsecret) && ast_strlen_zero(user->inkeys)) {
7972  /* No required authentication */
7973  if (user->acl) {
7974  /* There was host authentication and we passed, bonus! */
7975  if (bestscore < 4) {
7976  bestscore = 4;
7977  if (best)
7978  user_unref(best);
7979  best = user;
7980  continue;
7981  }
7982  } else {
7983  /* No host access, but no secret, either, not bad */
7984  if (bestscore < 3) {
7985  bestscore = 3;
7986  if (best)
7987  user_unref(best);
7988  best = user;
7989  continue;
7990  }
7991  }
7992  } else {
7993  if (user->acl) {
7994  /* Authentication, but host access too, eh, it's something.. */
7995  if (bestscore < 2) {
7996  bestscore = 2;
7997  if (best)
7998  user_unref(best);
7999  best = user;
8000  continue;
8001  }
8002  } else {
8003  /* Authentication and no host access... This is our baseline */
8004  if (bestscore < 1) {
8005  bestscore = 1;
8006  if (best)
8007  user_unref(best);
8008  best = user;
8009  continue;
8010  }
8011  }
8012  }
8013  }
8014  user_unref(user);
8015  }
8017  user = best;
8018  if (!user && !ast_strlen_zero(iaxs[callno]->username)) {
8019  user = realtime_user(iaxs[callno]->username, addr);
8020  if (user && (ast_apply_acl(user->acl, addr, "IAX2 user ACL: ") == AST_SENSE_DENY /* Access is denied from this IP */
8021  || (!ast_strlen_zero(iaxs[callno]->context) && /* No context specified */
8022  !apply_context(user->contexts, iaxs[callno]->context)))) { /* Context is permitted */
8023  user = user_unref(user);
8024  }
8025  }
8026  if (user) {
8027  /* We found our match (use the first) */
8028  /* copy vars */
8029  for (v = user->vars ; v ; v = v->next) {
8030  if ((tmpvar = ast_variable_new(v->name, v->value, v->file))) {
8031  if (ast_variable_list_replace(&iaxs[callno]->vars, tmpvar)) {
8032  tmpvar->next = iaxs[callno]->vars;
8033  iaxs[callno]->vars = tmpvar;
8034  }
8035  }
8036  }
8037  /* If a max AUTHREQ restriction is in place, activate it */
8038  if (user->maxauthreq > 0)
8039  ast_set_flag64(iaxs[callno], IAX_MAXAUTHREQ);
8040  iaxs[callno]->prefs = user->prefs;
8041  ast_copy_flags64(iaxs[callno], user, IAX_CODEC_USER_FIRST | IAX_IMMEDIATE | IAX_CODEC_NOPREFS | IAX_CODEC_NOCAP | IAX_FORCE_ENCRYPT);
8042  iaxs[callno]->encmethods = user->encmethods;
8043  /* Store the requested username if not specified */
8044  if (ast_strlen_zero(iaxs[callno]->username))
8045  ast_string_field_set(iaxs[callno], username, user->name);
8046  /* Store whether this is a trunked call, too, of course, and move if appropriate */
8047  ast_copy_flags64(iaxs[callno], user, IAX_TRUNK);
8048  iaxs[callno]->capability = user->capability;
8049  /* And use the default context */
8050  if (ast_strlen_zero(iaxs[callno]->context)) {
8051  if (user->contexts)
8052  ast_string_field_set(iaxs[callno], context, user->contexts->context);
8053  else
8054  ast_string_field_set(iaxs[callno], context, DEFAULT_CONTEXT);
8055  }
8056  /* And any input keys */
8057  ast_string_field_set(iaxs[callno], inkeys, user->inkeys);
8058  /* And the permitted authentication methods */
8059  iaxs[callno]->authmethods = user->authmethods;
8060  iaxs[callno]->adsi = user->adsi;
8061  /* If the user has callerid, override the remote caller id. */
8062  if (ast_test_flag64(user, IAX_HASCALLERID)) {
8063  iaxs[callno]->calling_tns = 0;
8064  iaxs[callno]->calling_ton = 0;
8065  ast_string_field_set(iaxs[callno], cid_num, user->cid_num);
8066  ast_string_field_set(iaxs[callno], cid_name, user->cid_name);
8067  ast_string_field_set(iaxs[callno], ani, user->cid_num);
8068  iaxs[callno]->calling_pres = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
8069  } else if (ast_strlen_zero(iaxs[callno]->cid_num) && ast_strlen_zero(iaxs[callno]->cid_name)) {
8070  iaxs[callno]->calling_pres = AST_PRES_NUMBER_NOT_AVAILABLE;
8071  } /* else user is allowed to set their own CID settings */
8072  if (!ast_strlen_zero(user->accountcode))
8073  ast_string_field_set(iaxs[callno], accountcode, user->accountcode);
8074  if (!ast_strlen_zero(user->mohinterpret))
8075  ast_string_field_set(iaxs[callno], mohinterpret, user->mohinterpret);
8076  if (!ast_strlen_zero(user->mohsuggest))
8077  ast_string_field_set(iaxs[callno], mohsuggest, user->mohsuggest);
8078  if (!ast_strlen_zero(user->parkinglot))
8079  ast_string_field_set(iaxs[callno], parkinglot, user->parkinglot);
8080  if (user->amaflags)
8081  iaxs[callno]->amaflags = user->amaflags;
8082  if (!ast_strlen_zero(user->language))
8083  ast_string_field_set(iaxs[callno], language, user->language);
8085  /* Keep this check last */
8086  if (!ast_strlen_zero(user->dbsecret)) {
8087  char *family, *key=NULL;
8088  char buf[80];
8089  family = ast_strdupa(user->dbsecret);
8090  key = strchr(family, '/');
8091  if (key) {
8092  *key = '\0';
8093  key++;
8094  }
8095  if (!key || ast_db_get(family, key, buf, sizeof(buf)))
8096  ast_log(LOG_WARNING, "Unable to retrieve database password for family/key '%s'!\n", user->dbsecret);
8097  else
8098  ast_string_field_set(iaxs[callno], secret, buf);
8099  } else
8100  ast_string_field_set(iaxs[callno], secret, user->secret);
8101  res = 0;
8102  user = user_unref(user);
8103  } else {
8104  /* user was not found, but we should still fake an AUTHREQ.
8105  * Set authmethods to the last known authmethod used by the system
8106  * Set a fake secret, it's not looked at, just required to attempt authentication.
8107  * Set authrej so the AUTHREP is rejected without even looking at its contents */
8108  iaxs[callno]->authmethods = last_authmethod ? last_authmethod : IAX_AUTH_MD5;
8109  ast_string_field_set(iaxs[callno], secret, "badsecret");
8110  iaxs[callno]->authrej = 1;
8111  if (!ast_strlen_zero(iaxs[callno]->username)) {
8112  /* only send the AUTHREQ if a username was specified. */
8113  res = 0;
8114  }
8115  }
8116  ast_set2_flag64(iaxs[callno], iax2_getpeertrunk(*addr), IAX_TRUNK);
8117  return res;
8118 }
8119 
8120 static int raw_hangup(struct ast_sockaddr *addr, unsigned short src, unsigned short dst, int sockfd)
8121 {
8122  struct ast_iax2_full_hdr fh;
8123  fh.scallno = htons(src | IAX_FLAG_FULL);
8124  fh.dcallno = htons(dst);
8125  fh.ts = 0;
8126  fh.oseqno = 0;
8127  fh.iseqno = 0;
8128  fh.type = AST_FRAME_IAX;
8129  fh.csub = compress_subclass(IAX_COMMAND_INVAL);
8130  iax_outputframe(NULL, &fh, 0, addr, 0);
8131 
8132  ast_debug(1, "Raw Hangup %s, src=%d, dst=%d\n", ast_sockaddr_stringify(addr), src, dst);
8133  return ast_sendto(sockfd, &fh, sizeof(fh), 0, addr);
8134 }
8135 
8136 static void merge_encryption(struct chan_iax2_pvt *p, unsigned int enc)
8137 {
8138  /* Select exactly one common encryption if there are any */
8139  p->encmethods &= enc;
8140  if (p->encmethods) {
8141  if (!(p->encmethods & IAX_ENCRYPT_KEYROTATE)){ /* if key rotation is not supported, turn off keyrotation. */
8142  p->keyrotateid = -2;
8143  }
8144  if (p->encmethods & IAX_ENCRYPT_AES128)
8145  p->encmethods = IAX_ENCRYPT_AES128;
8146  else
8147  p->encmethods = 0;
8148  }
8149 }
8150 
8151 /*!
8152  * \pre iaxsl[call_num] is locked
8153  *
8154  * \note Since this function calls send_command_final(), the pvt struct for the given
8155  * call number may disappear while executing this function.
8156  */
8157 static int authenticate_request(int call_num)
8158 {
8159  struct iax_ie_data ied;
8160  int res = -1, authreq_restrict = 0;
8161  char challenge[10];
8162  struct chan_iax2_pvt *p = iaxs[call_num];
8163 
8164  memset(&ied, 0, sizeof(ied));
8165 
8166  /* If an AUTHREQ restriction is in place, make sure we can send an AUTHREQ back */
8167  if (ast_test_flag64(p, IAX_MAXAUTHREQ)) {
8168  struct iax2_user *user;
8169 
8170  user = ao2_find(users, p->username, OBJ_KEY);
8171  if (user) {
8172  if (user->curauthreq == user->maxauthreq)
8173  authreq_restrict = 1;
8174  else
8175  user->curauthreq++;
8176  user = user_unref(user);
8177  }
8178  }
8179 
8180  /* If the AUTHREQ limit test failed, send back an error */
8181  if (authreq_restrict) {
8182  iax_ie_append_str(&ied, IAX_IE_CAUSE, "Unauthenticated call limit reached");
8183  iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_CALL_REJECTED);
8184  send_command_final(p, AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied.buf, ied.pos, -1);
8185  return 0;
8186  }
8187 
8188  iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, p->authmethods);
8189  if (p->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_RSA)) {
8190  snprintf(challenge, sizeof(challenge), "%d", (int)ast_random());
8191  ast_string_field_set(p, challenge, challenge);
8192  /* snprintf(p->challenge, sizeof(p->challenge), "%d", (int)ast_random()); */
8193  iax_ie_append_str(&ied, IAX_IE_CHALLENGE, p->challenge);
8194  }
8195  if (p->encmethods)
8196  iax_ie_append_short(&ied, IAX_IE_ENCRYPTION, p->encmethods);
8197 
8198  iax_ie_append_str(&ied,IAX_IE_USERNAME, p->username);
8199 
8200  res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREQ, 0, ied.buf, ied.pos, -1);
8201 
8202  if (p->encmethods)
8203  ast_set_flag64(p, IAX_ENCRYPTED);
8204 
8205  return res;
8206 }
8207 
8208 static int authenticate_verify(struct chan_iax2_pvt *p, struct iax_ies *ies)
8209 {
8210  char requeststr[256];
8211  char md5secret[256] = "";
8212  char secret[256] = "";
8213  char rsasecret[256] = "";
8214  int res = -1;
8215  int x;
8216  struct iax2_user *user;
8217 
8218  if (p->authrej) {
8219  return res;
8220  }
8221 
8222  user = ao2_find(users, p->username, OBJ_KEY);
8223  if (user) {
8224  if (ast_test_flag64(p, IAX_MAXAUTHREQ)) {
8225  ast_atomic_fetchadd_int(&user->curauthreq, -1);
8226  ast_clear_flag64(p, IAX_MAXAUTHREQ);
8227  }
8228  ast_string_field_set(p, host, user->name);
8229  user = user_unref(user);
8230  }
8231  if (ast_test_flag64(p, IAX_FORCE_ENCRYPT) && !p->encmethods) {
8232  ast_log(LOG_WARNING, "Call Terminated, incoming call is unencrypted while force encrypt is enabled.\n");
8233  return res;
8234  }
8235  if (!ast_test_flag(&p->state, IAX_STATE_AUTHENTICATED))
8236  return res;
8237  if (ies->password)
8238  ast_copy_string(secret, ies->password, sizeof(secret));
8239  if (ies->md5_result)
8240  ast_copy_string(md5secret, ies->md5_result, sizeof(md5secret));
8241  if (ies->rsa_result)
8242  ast_copy_string(rsasecret, ies->rsa_result, sizeof(rsasecret));
8243  if ((p->authmethods & IAX_AUTH_RSA) && !ast_strlen_zero(rsasecret) && !ast_strlen_zero(p->inkeys)) {
8244  struct ast_key *key;
8245  char *keyn;
8246  char *tmpkey;
8247  char *stringp=NULL;
8248  if (!(tmpkey = ast_strdup(p->inkeys))) {
8249  ast_log(LOG_ERROR, "Unable to create a temporary string for parsing stored 'inkeys'\n");
8250  return res;
8251  }
8252  stringp = tmpkey;
8253  keyn = strsep(&stringp, ":");
8254  while(keyn) {
8255  key = ast_key_get(keyn, AST_KEY_PUBLIC);
8256  if (key && !ast_check_signature(key, p->challenge, rsasecret)) {
8257  res = 0;
8258  p->eff_auth_method = IAX_AUTH_RSA;
8259  break;
8260  } else if (!key) {
8261  ast_log(LOG_WARNING, "Requested inkey '%s' for RSA authentication does not exist\n", keyn);
8262  }
8263  keyn = strsep(&stringp, ":");
8264  }
8265  ast_free(tmpkey);
8266  if (res && authdebug) {
8267  ast_log(LOG_WARNING, "No RSA public keys on file matched incoming call\n");
8268  }
8269  } else if (p->authmethods & IAX_AUTH_MD5) {
8270  struct MD5Context md5;
8271  unsigned char digest[16];
8272  char *tmppw, *stringp;
8273  tmppw = ast_strdupa(p->secret);
8274  stringp = tmppw;
8275  while((tmppw = strsep(&stringp, ";"))) {
8276  MD5Init(&md5);
8277  MD5Update(&md5, (unsigned char *)p->challenge, strlen(p->challenge));
8278  MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
8279  MD5Final(digest, &md5);
8280  /* If they support md5, authenticate with it. */
8281  for (x=0;x<16;x++)
8282  sprintf(requeststr + (x << 1), "%02hhx", digest[x]); /* safe */
8283  if (!strcasecmp(requeststr, md5secret)) {
8284  res = 0;
8285  p->eff_auth_method = IAX_AUTH_MD5;
8286  break;
8287  } else if (authdebug) {
8288  ast_log(LOG_WARNING, "MD5 secret mismatch\n");
8289  }
8290  }
8291  } else if (p->authmethods & IAX_AUTH_PLAINTEXT) {
8292  if (!strcmp(secret, p->secret)) {
8293  res = 0;
8294  p->eff_auth_method = IAX_AUTH_PLAINTEXT;
8295  } else if (authdebug) {
8296  ast_log(LOG_WARNING, "Plaintext secret mismatch\n");
8297  }
8298  }
8299  return res;
8300 }
8301 
8302 /*! \brief Verify inbound registration */
8303 static int register_verify(int callno, struct ast_sockaddr *addr, struct iax_ies *ies)
8304 {
8305  char requeststr[256] = "";
8306  char peer[256] = "";
8307  char md5secret[256] = "";
8308  char rsasecret[256] = "";
8309  char secret[256] = "";
8310  struct iax2_peer *p = NULL;
8311  struct ast_key *key;
8312  char *keyn;
8313  int x;
8314  int expire = 0;
8315  int res = -1;
8316 
8317  ast_clear_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
8318  /* iaxs[callno]->peer[0] = '\0'; not necc. any more-- stringfield is pre-inited to null string */
8319  if (ies->username)
8320  ast_copy_string(peer, ies->username, sizeof(peer));
8321  if (ies->password)
8322  ast_copy_string(secret, ies->password, sizeof(secret));
8323  if (ies->md5_result)
8324  ast_copy_string(md5secret, ies->md5_result, sizeof(md5secret));
8325  if (ies->rsa_result)
8326  ast_copy_string(rsasecret, ies->rsa_result, sizeof(rsasecret));
8327  if (ies->refresh)
8328  expire = ies->refresh;
8329 
8330  if (ast_strlen_zero(peer)) {
8331  ast_log(LOG_NOTICE, "Empty registration from %s\n", ast_sockaddr_stringify_addr(addr));
8332  return -1;
8333  }
8334 
8335  /* SLD: first call to lookup peer during registration */
8336  ast_mutex_unlock(&iaxsl[callno]);
8337  p = find_peer(peer, 1);
8338  ast_mutex_lock(&iaxsl[callno]);
8339  if (!p || !iaxs[callno]) {
8340  if (iaxs[callno]) {
8341  int plaintext = ((last_authmethod & IAX_AUTH_PLAINTEXT) | (iaxs[callno]->authmethods & IAX_AUTH_PLAINTEXT));
8342  /* Anything, as long as it's non-blank */
8343  ast_string_field_set(iaxs[callno], secret, "badsecret");
8344  /* An AUTHREQ must be sent in response to a REGREQ of an invalid peer unless
8345  * 1. A challenge already exists indicating a AUTHREQ was already sent out.
8346  * 2. A plaintext secret is present in ie as result of a previous AUTHREQ requesting it.
8347  * 3. A plaintext secret is present in the ie and the last_authmethod used by a peer happened
8348  * to be plaintext, indicating it is an authmethod used by other peers on the system.
8349  *
8350  * If none of these cases exist, res will be returned as 0 without authentication indicating
8351  * an AUTHREQ needs to be sent out. */
8352 
8353  if (ast_strlen_zero(iaxs[callno]->challenge) &&
8354  !(!ast_strlen_zero(secret) && plaintext)) {
8355  /* by setting res to 0, an REGAUTH will be sent */
8356  res = 0;
8357  }
8358  }
8359  if (authdebug && !p)
8360  ast_log(LOG_NOTICE, "No registration for peer '%s' (from %s)\n", peer, ast_sockaddr_stringify_addr(addr));
8361  goto return_unref;
8362  }
8363 
8364  if (!ast_test_flag64(p, IAX_DYNAMIC)) {
8365  if (authdebug)
8366  ast_log(LOG_NOTICE, "Peer '%s' is not dynamic (from %s)\n", peer, ast_sockaddr_stringify_addr(addr));
8367  goto return_unref;
8368  }
8369 
8370  if (!ast_apply_acl(p->acl, addr, "IAX2 Peer ACL: ")) {
8371  if (authdebug)
8372  ast_log(LOG_NOTICE, "Host %s denied access to register peer '%s'\n", ast_sockaddr_stringify_addr(addr), p->name);
8373  goto return_unref;
8374  }
8375  ast_string_field_set(iaxs[callno], secret, p->secret);
8376  ast_string_field_set(iaxs[callno], inkeys, p->inkeys);
8377  /* Check secret against what we have on file */
8378  if (!ast_strlen_zero(rsasecret) && (p->authmethods & IAX_AUTH_RSA) && !ast_strlen_zero(iaxs[callno]->challenge)) {
8379  if (!ast_strlen_zero(p->inkeys)) {
8380  char *tmpkey;
8381  char *stringp=NULL;
8382  if (!(tmpkey = ast_strdup(p->inkeys))) {
8383  ast_log(LOG_ERROR, "Unable to create a temporary string for parsing stored 'inkeys'\n");
8384  goto return_unref;
8385  }
8386  stringp = tmpkey;
8387  keyn = strsep(&stringp, ":");
8388  while(keyn) {
8389  key = ast_key_get(keyn, AST_KEY_PUBLIC);
8390  if (key && !ast_check_signature(key, iaxs[callno]->challenge, rsasecret)) {
8391  ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
8392  break;
8393  } else if (!key)
8394  ast_log(LOG_WARNING, "requested inkey '%s' does not exist\n", keyn);
8395  keyn = strsep(&stringp, ":");
8396  }
8397  ast_free(tmpkey);
8398  if (!keyn) {
8399  if (authdebug)
8400  ast_log(LOG_NOTICE, "Host %s failed RSA authentication with inkeys '%s'\n", peer, p->inkeys);
8401  goto return_unref;
8402  }
8403  } else {
8404  if (authdebug)
8405  ast_log(LOG_NOTICE, "Host '%s' trying to do RSA authentication, but we have no inkeys\n", peer);
8406  goto return_unref;
8407  }
8408  } else if (!ast_strlen_zero(md5secret) && (p->authmethods & IAX_AUTH_MD5) && !ast_strlen_zero(iaxs[callno]->challenge)) {
8409  struct MD5Context md5;
8410  unsigned char digest[16];
8411  char *tmppw, *stringp;
8412 
8413  tmppw = ast_strdupa(p->secret);
8414  stringp = tmppw;
8415  while((tmppw = strsep(&stringp, ";"))) {
8416  MD5Init(&md5);
8417  MD5Update(&md5, (unsigned char *)iaxs[callno]->challenge, strlen(iaxs[callno]->challenge));
8418  MD5Update(&md5, (unsigned char *)tmppw, strlen(tmppw));
8419  MD5Final(digest, &md5);
8420  for (x=0;x<16;x++)
8421  sprintf(requeststr + (x << 1), "%02hhx", digest[x]); /* safe */
8422  if (!strcasecmp(requeststr, md5secret))
8423  break;
8424  }
8425  if (tmppw) {
8426  ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
8427  } else {
8428  if (authdebug)
8429  ast_log(LOG_NOTICE, "Host %s failed MD5 authentication for '%s' (%s != %s)\n", ast_sockaddr_stringify_addr(addr), p->name, requeststr, md5secret);
8430  goto return_unref;
8431  }
8432  } else if (!ast_strlen_zero(secret) && (p->authmethods & IAX_AUTH_PLAINTEXT)) {
8433  /* They've provided a plain text password and we support that */
8434  if (strcmp(secret, p->secret)) {
8435  if (authdebug)
8436  ast_log(LOG_NOTICE, "Host %s did not provide proper plaintext password for '%s'\n", ast_sockaddr_stringify_addr(addr), p->name);
8437  goto return_unref;
8438  } else
8439  ast_set_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED);
8440  } else if (!ast_strlen_zero(iaxs[callno]->challenge) && ast_strlen_zero(md5secret) && ast_strlen_zero(rsasecret)) {
8441  /* if challenge has been sent, but no challenge response if given, reject. */
8442  goto return_unref;
8443  }
8444  ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "IAX2/%s", p->name); /* Activate notification */
8445 
8446  /* either Authentication has taken place, or a REGAUTH must be sent before verifying registration */
8447  res = 0;
8448 
8449 return_unref:
8450  if (iaxs[callno]) {
8451  ast_string_field_set(iaxs[callno], peer, peer);
8452 
8453  /* Choose lowest expiry number */
8454  if (expire && (expire < iaxs[callno]->expiry)) {
8455  iaxs[callno]->expiry = expire;
8456  }
8457  }
8458 
8459  if (p) {
8460  peer_unref(p);
8461  }
8462  return res;
8463 }
8464 
8465 static int authenticate(const char *challenge, const char *secret, const char *keyn, int authmethods, struct iax_ie_data *ied, struct ast_sockaddr *addr, struct chan_iax2_pvt *pvt)
8466 {
8467  int res = -1;
8468  int x;
8469  if (!ast_strlen_zero(keyn)) {
8470  if (!(authmethods & IAX_AUTH_RSA)) {
8471  if (ast_strlen_zero(secret)) {
8472  ast_log(LOG_WARNING, "Asked to authenticate to %s with an RSA key, but they don't allow RSA authentication\n", ast_sockaddr_stringify_addr(addr));
8473  }
8474  } else if (ast_strlen_zero(challenge)) {
8475  ast_log(LOG_WARNING, "No challenge provided for RSA authentication to %s\n", ast_sockaddr_stringify_addr(addr));
8476  } else {
8477  char sig[256];
8478  struct ast_key *key;
8479  key = ast_key_get(keyn, AST_KEY_PRIVATE);
8480  if (!key) {
8481  ast_log(LOG_WARNING, "Unable to find private key '%s'\n", keyn);
8482  } else {
8483  if (ast_sign(key, (char*)challenge, sig)) {
8484  ast_log(LOG_WARNING, "Unable to sign challenge with key\n");
8485  res = -1;
8486  } else {
8487  iax_ie_append_str(ied, IAX_IE_RSA_RESULT, sig);
8488  if (pvt) {
8489  pvt->eff_auth_method = IAX_AUTH_RSA;
8490  }
8491  res = 0;
8492  }
8493  }
8494 
8495  if (pvt && !ast_strlen_zero(secret)) {
8496  struct MD5Context md5;
8497  unsigned char digest[16];
8498 
8499  MD5Init(&md5);
8500  MD5Update(&md5, (unsigned char *) challenge, strlen(challenge));
8501  MD5Update(&md5, (unsigned char *) secret, strlen(secret));
8502  MD5Final(digest, &md5);
8503 
8504  build_encryption_keys(digest, pvt);
8505  }
8506  }
8507  }
8508  /* Fall back */
8509  if (res && !ast_strlen_zero(secret)) {
8510  if ((authmethods & IAX_AUTH_MD5) && !ast_strlen_zero(challenge)) {
8511  struct MD5Context md5;
8512  unsigned char digest[16];
8513  char digres[128];
8514  MD5Init(&md5);
8515  MD5Update(&md5, (unsigned char *)challenge, strlen(challenge));
8516  MD5Update(&md5, (unsigned char *)secret, strlen(secret));
8517  MD5Final(digest, &md5);
8518  /* If they support md5, authenticate with it. */
8519  for (x=0;x<16;x++)
8520  sprintf(digres + (x << 1), "%02hhx", digest[x]); /* safe */
8521  if (pvt) {
8522  build_encryption_keys(digest, pvt);
8523  pvt->eff_auth_method = IAX_AUTH_MD5;
8524  }
8525  iax_ie_append_str(ied, IAX_IE_MD5_RESULT, digres);
8526  res = 0;
8527  } else if (authmethods & IAX_AUTH_PLAINTEXT) {
8528  iax_ie_append_str(ied, IAX_IE_PASSWORD, secret);
8529  if (pvt) {
8530  pvt->eff_auth_method = IAX_AUTH_PLAINTEXT;
8531  }
8532  res = 0;
8533  } else
8534  ast_log(LOG_WARNING, "No way to send secret to peer '%s' (their methods: %d)\n", ast_sockaddr_stringify_addr(addr), authmethods);
8535  }
8536  return res;
8537 }
8538 
8539 /*!
8540  * \note This function calls realtime_peer -> reg_source_db -> iax2_poke_peer -> find_callno,
8541  * so do not call this function with a pvt lock held.
8542  */
8543 static int authenticate_reply(struct chan_iax2_pvt *p, struct ast_sockaddr *addr, struct iax_ies *ies, const char *override, const char *okey)
8544 {
8545  struct iax2_peer *peer = NULL;
8546  /* Start pessimistic */
8547  int res = -1;
8548  int authmethods = 0;
8549  struct iax_ie_data ied;
8550  uint16_t callno = p->callno;
8551 
8552  memset(&ied, 0, sizeof(ied));
8553 
8554  if (ies->username)
8555  ast_string_field_set(p, username, ies->username);
8556  if (ies->challenge)
8557  ast_string_field_set(p, challenge, ies->challenge);
8558  if (ies->authmethods)
8559  authmethods = ies->authmethods;
8560  if (authmethods & IAX_AUTH_MD5)
8561  merge_encryption(p, ies->encmethods);
8562  else
8563  p->encmethods = 0;
8564 
8565  /* Check for override RSA authentication first */
8566  if (!ast_strlen_zero(override) || !ast_strlen_zero(okey)) {
8567  /* Normal password authentication */
8568  res = authenticate(p->challenge, override, okey, authmethods, &ied, addr, p);
8569  } else {
8570  struct ao2_iterator i = ao2_iterator_init(peers, 0);
8571  while ((peer = ao2_iterator_next(&i))) {
8572  struct ast_sockaddr peer_addr;
8573  struct ast_sockaddr tmp_sockaddr1;
8574  struct ast_sockaddr tmp_sockaddr2;
8575 
8576  ast_sockaddr_copy(&peer_addr, &peer->addr);
8577 
8578  ast_sockaddr_apply_netmask(addr, &peer->mask, &tmp_sockaddr1);
8579  ast_sockaddr_apply_netmask(&peer_addr, &peer->mask, &tmp_sockaddr2);
8580 
8581  if ((ast_strlen_zero(p->peer) || !strcmp(p->peer, peer->name))
8582  /* No peer specified at our end, or this is the peer */
8583  && (ast_strlen_zero(peer->username) || (!strcmp(peer->username, p->username)))
8584  /* No username specified in peer rule, or this is the right username */
8585  && (ast_sockaddr_isnull(&peer_addr) || !(ast_sockaddr_cmp_addr(&tmp_sockaddr1, &tmp_sockaddr2)))
8586  /* No specified host, or this is our host */
8587  ) {
8588  res = authenticate(p->challenge, peer->secret, peer->outkey, authmethods, &ied, addr, p);
8589  if (!res) {
8590  peer_unref(peer);
8591  break;
8592  }
8593  }
8594  peer_unref(peer);
8595  }
8597  if (!peer) {
8598  /* We checked our list and didn't find one. It's unlikely, but possible,
8599  that we're trying to authenticate *to* a realtime peer */
8600  const char *peer_name = ast_strdupa(p->peer);
8601  ast_mutex_unlock(&iaxsl[callno]);
8602  if ((peer = realtime_peer(peer_name, NULL))) {
8603  ast_mutex_lock(&iaxsl[callno]);
8604  if (!(p = iaxs[callno])) {
8605  peer_unref(peer);
8606  return -1;
8607  }
8608  res = authenticate(p->challenge, peer->secret,peer->outkey, authmethods, &ied, addr, p);
8609  peer_unref(peer);
8610  }
8611  if (!peer) {
8612  ast_mutex_lock(&iaxsl[callno]);
8613  if (!(p = iaxs[callno]))
8614  return -1;
8615  }
8616  }
8617  }
8618 
8619  if (!(ies->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_PLAINTEXT)) && (ies->authmethods & IAX_AUTH_RSA) && ast_strlen_zero(okey)) {
8620  /* If the only thing available is RSA, and we don't have an outkey, we can't do it... */
8621  ast_log(LOG_WARNING, "Call terminated. RSA authentication requires an outkey\n");
8622  return -1;
8623  }
8624 
8625  if (ies->encmethods) {
8626  if (ast_strlen_zero(p->secret) &&
8627  ((ies->authmethods & IAX_AUTH_RSA) || (ies->authmethods & IAX_AUTH_MD5) || (ies->authmethods & IAX_AUTH_PLAINTEXT))) {
8628  ast_log(LOG_WARNING, "Call terminated. Encryption requested by peer but no secret available locally\n");
8629  return -1;
8630  }
8631  /* Don't even THINK about trying to encrypt or decrypt anything if we don't have valid keys, for some reason... */
8632  /* If either of these happens, it's our fault, not the user's. But we should abort rather than crash. */
8633  ast_assert_return(!invalid_key(&p->ecx), -1);
8634  ast_assert_return(!invalid_key(&p->dcx), -1);
8635  ast_set_flag64(p, IAX_ENCRYPTED | IAX_KEYPOPULATED);
8636  } else if (ast_test_flag64(iaxs[callno], IAX_FORCE_ENCRYPT)) {
8637  ast_log(LOG_NOTICE, "Call initiated without encryption while forceencryption=yes option is set\n");
8638  return -1; /* if force encryption is yes, and no encryption methods, then return -1 to hangup */
8639  }
8640  if (!res) {
8641  struct ast_datastore *variablestore;
8642  struct ast_variable *var, *prev = NULL;
8643  AST_LIST_HEAD(, ast_var_t) *varlist;
8644  varlist = ast_calloc(1, sizeof(*varlist));
8645  variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
8646  if (variablestore && varlist && p->owner) {
8647  variablestore->data = varlist;
8648  variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
8649  AST_LIST_HEAD_INIT(varlist);
8650  for (var = ies->vars; var; var = var->next) {
8651  struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
8652  if (prev)
8653  ast_free(prev);
8654  prev = var;
8655  if (!newvar) {
8656  /* Don't abort list traversal, as this would leave ies->vars in an inconsistent state. */
8657  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
8658  } else {
8659  AST_LIST_INSERT_TAIL(varlist, newvar, entries);
8660  }
8661  }
8662  if (prev)
8663  ast_free(prev);
8664  ies->vars = NULL;
8665  ast_channel_datastore_add(p->owner, variablestore);
8666  } else {
8667  if (p->owner)
8668  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
8669  if (variablestore)
8670  ast_datastore_free(variablestore);
8671  if (varlist)
8672  ast_free(varlist);
8673  }
8674  }
8675 
8676  if (!res)
8677  res = send_command(p, AST_FRAME_IAX, IAX_COMMAND_AUTHREP, 0, ied.buf, ied.pos, -1);
8678  return res;
8679 }
8680 
8681 static int iax2_do_register(struct iax2_registry *reg);
8682 
8683 static void __iax2_do_register_s(const void *data)
8684 {
8685  struct iax2_registry *reg = (struct iax2_registry *)data;
8686 
8687  if (ast_sockaddr_isnull(&reg->addr)) {
8688  reg->addr.ss.ss_family = AST_AF_UNSPEC;
8689  ast_dnsmgr_lookup(reg->hostname, &reg->addr, &reg->dnsmgr, srvlookup ? "_iax._udp" : NULL);
8690  if (!ast_sockaddr_port(&reg->addr)) {
8691  ast_sockaddr_set_port(&reg->addr, reg->port);
8692  } else {
8693  reg->port = ast_sockaddr_port(&reg->addr);
8694  }
8695  }
8696 
8697  reg->expire = -1;
8698  iax2_do_register(reg);
8699 }
8700 
8701 static int iax2_do_register_s(const void *data)
8702 {
8703 #ifdef SCHED_MULTITHREADED
8704  if (schedule_action(__iax2_do_register_s, data))
8705 #endif
8706  __iax2_do_register_s(data);
8707  return 0;
8708 }
8709 
8710 static int try_transfer(struct chan_iax2_pvt *pvt, struct iax_ies *ies)
8711 {
8712  int newcall = 0;
8713  struct iax_ie_data ied;
8714  struct ast_sockaddr new = { {0,} };
8715 
8716  memset(&ied, 0, sizeof(ied));
8717  if (!ast_sockaddr_isnull(&ies->apparent_addr)) {
8718  ast_sockaddr_copy(&new, &ies->apparent_addr);
8719  }
8720  if (ies->callno) {
8721  newcall = ies->callno;
8722  }
8723  if (!newcall || ast_sockaddr_isnull(&new)) {
8724  ast_log(LOG_WARNING, "Invalid transfer request\n");
8725  return -1;
8726  }
8727  pvt->transfercallno = newcall;
8728  ast_sockaddr_copy(&pvt->transfer, &new);
8729  pvt->transferid = ies->transferid;
8730  /* only store by transfercallno if this is a new transfer,
8731  * just in case we get a duplicate TXREQ */
8732  if (pvt->transferring == TRANSFER_NONE) {
8733  store_by_transfercallno(pvt);
8734  }
8735  pvt->transferring = TRANSFER_BEGIN;
8736 
8737  if (ies->transferid) {
8738  iax_ie_append_int(&ied, IAX_IE_TRANSFERID, ies->transferid);
8739  }
8740  send_command_transfer(pvt, AST_FRAME_IAX, IAX_COMMAND_TXCNT, 0, ied.buf, ied.pos);
8741  return 0;
8742 }
8743 
8744 static int complete_dpreply(struct chan_iax2_pvt *pvt, struct iax_ies *ies)
8745 {
8746  char exten[256] = "";
8747  int status = CACHE_FLAG_UNKNOWN, expiry = iaxdefaultdpcache, x, matchmore = 0;
8748  struct iax2_dpcache *dp = NULL;
8749 
8750  if (ies->called_number)
8751  ast_copy_string(exten, ies->called_number, sizeof(exten));
8752 
8753  if (ies->dpstatus & IAX_DPSTATUS_EXISTS)
8754  status = CACHE_FLAG_EXISTS;
8755  else if (ies->dpstatus & IAX_DPSTATUS_CANEXIST)
8756  status = CACHE_FLAG_CANEXIST;
8757  else if (ies->dpstatus & IAX_DPSTATUS_NONEXISTENT)
8758  status = CACHE_FLAG_NONEXISTENT;
8759 
8760  if (ies->refresh)
8761  expiry = ies->refresh;
8762  if (ies->dpstatus & IAX_DPSTATUS_MATCHMORE)
8763  matchmore = CACHE_FLAG_MATCHMORE;
8764 
8766  AST_LIST_TRAVERSE_SAFE_BEGIN(&dpcache, dp, peer_list) {
8767  if (strcmp(dp->exten, exten))
8768  continue;
8769  AST_LIST_REMOVE_CURRENT(peer_list);
8770  dp->callno = 0;
8771  dp->expiry.tv_sec = dp->orig.tv_sec + expiry;
8772  if (dp->flags & CACHE_FLAG_PENDING) {
8773  dp->flags &= ~CACHE_FLAG_PENDING;
8774  dp->flags |= status;
8775  dp->flags |= matchmore;
8776  }
8777  /* Wake up waiters */
8778  for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
8779  if (dp->waiters[x] > -1) {
8780  if (write(dp->waiters[x], "asdf", 4) < 0) {
8781  }
8782  }
8783  }
8784  }
8787 
8788  return 0;
8789 }
8790 
8791 static int complete_transfer(int callno, struct iax_ies *ies)
8792 {
8793  int peercallno = 0;
8794  struct chan_iax2_pvt *pvt = iaxs[callno];
8795  struct iax_frame *cur;
8796  jb_frame frame;
8797 
8798  if (ies->callno)
8799  peercallno = ies->callno;
8800 
8801  if (peercallno < 1) {
8802  ast_log(LOG_WARNING, "Invalid transfer request\n");
8803  return -1;
8804  }
8805  remove_by_transfercallno(pvt);
8806  /* since a transfer has taken place, the address will change.
8807  * This must be accounted for in the peercnts table. Remove
8808  * the old address and add the new one */
8809  peercnt_remove_by_addr(&pvt->addr);
8810  peercnt_add(&pvt->transfer);
8811  /* now copy over the new address */
8812  memcpy(&pvt->addr, &pvt->transfer, sizeof(pvt->addr));
8813  memset(&pvt->transfer, 0, sizeof(pvt->transfer));
8814  /* Reset sequence numbers */
8815  pvt->oseqno = 0;
8816  pvt->rseqno = 0;
8817  pvt->iseqno = 0;
8818  pvt->aseqno = 0;
8819 
8820  if (pvt->peercallno) {
8821  remove_by_peercallno(pvt);
8822  }
8823  pvt->peercallno = peercallno;
8824  /*this is where the transfering call switches hash tables */
8825  store_by_peercallno(pvt);
8826  pvt->transferring = TRANSFER_NONE;
8827  pvt->svoiceformat = -1;
8828  pvt->voiceformat = 0;
8829  pvt->svideoformat = -1;
8830  pvt->videoformat = 0;
8831  pvt->transfercallno = 0;
8832  memset(&pvt->rxcore, 0, sizeof(pvt->rxcore));
8833  memset(&pvt->offset, 0, sizeof(pvt->offset));
8834  /* reset jitterbuffer */
8835  while(jb_getall(pvt->jb,&frame) == JB_OK)
8836  iax2_frame_free(frame.data);
8837  jb_reset(pvt->jb);
8838  pvt->lag = 0;
8839  pvt->last = 0;
8840  pvt->lastsent = 0;
8841  pvt->nextpred = 0;
8842  pvt->pingtime = DEFAULT_RETRY_TIME;
8843  AST_LIST_TRAVERSE(&frame_queue[callno], cur, list) {
8844  /* We must cancel any packets that would have been transmitted
8845  because now we're talking to someone new. It's okay, they
8846  were transmitted to someone that didn't care anyway. */
8847  cur->retries = -1;
8848  }
8849  return 0;
8850 }
8851 
8852 static void iax2_publish_registry(const char *username, const char *domain, const char *status, const char *cause)
8853 {
8854  ast_system_publish_registry("IAX2", username, domain, status, cause);
8855 }
8856 
8857 /*! \brief Acknowledgment received for OUR registration */
8858 static int iax2_ack_registry(struct iax_ies *ies, struct ast_sockaddr *addr, int callno)
8859 {
8860  struct iax2_registry *reg;
8861  /* Start pessimistic */
8862  char peer[256] = "";
8863  char msgstatus[60];
8864  int refresh = 60;
8865  char ourip[256] = "<Unspecified>";
8866  struct ast_sockaddr oldus;
8867  struct ast_sockaddr us;
8868  int oldmsgs;
8869 
8870  if (!ast_sockaddr_isnull(&ies->apparent_addr)) {
8871  ast_sockaddr_copy(&us, &ies->apparent_addr);
8872  }
8873  if (ies->username) {
8874  ast_copy_string(peer, ies->username, sizeof(peer));
8875  }
8876  if (ies->refresh) {
8877  refresh = ies->refresh;
8878  }
8879  if (ies->calling_number) {
8880  /* We don't do anything with it really, but maybe we should */
8881  }
8882  reg = iaxs[callno]->reg;
8883  if (!reg) {
8884  ast_log(LOG_WARNING, "Registry acknowledge on unknown registry '%s'\n", peer);
8885  return -1;
8886  }
8887  ast_sockaddr_copy(&oldus, &reg->us);
8888  oldmsgs = reg->messages;
8889  if (ast_sockaddr_cmp(&reg->addr, addr)) {
8890  ast_log(LOG_WARNING, "Received unsolicited registry ack from '%s'\n", ast_sockaddr_stringify(addr));
8891  return -1;
8892  }
8893  ast_sockaddr_copy(&reg->us, &us);
8894  if (ies->msgcount >= 0) {
8895  reg->messages = ies->msgcount & 0xffff; /* only low 16 bits are used in the transmission of the IE */
8896  }
8897  /* always refresh the registration at the interval requested by the server
8898  we are registering to
8899  */
8900  reg->refresh = refresh;
8901  reg->expire = iax2_sched_replace(reg->expire, sched,
8902  (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
8903  if (ast_sockaddr_cmp(&oldus, &reg->us) || (reg->messages != oldmsgs)) {
8904 
8905  if (reg->messages > 255) {
8906  snprintf(msgstatus, sizeof(msgstatus), " with %d new and %d old messages waiting", reg->messages & 0xff, reg->messages >> 8);
8907  } else if (reg->messages > 1) {
8908  snprintf(msgstatus, sizeof(msgstatus), " with %d new messages waiting", reg->messages);
8909  } else if (reg->messages > 0) {
8910  ast_copy_string(msgstatus, " with 1 new message waiting", sizeof(msgstatus));
8911  } else {
8912  ast_copy_string(msgstatus, " with no messages waiting", sizeof(msgstatus));
8913  }
8914 
8915  snprintf(ourip, sizeof(ourip), "%s", ast_sockaddr_stringify(&reg->us));
8916 
8917  ast_verb(3, "Registered IAX2 to '%s', who sees us as %s%s\n", ast_sockaddr_stringify(addr), ourip, msgstatus);
8918  iax2_publish_registry(reg->username, ast_sockaddr_stringify(addr), "Registered", NULL);
8919  }
8920  reg->regstate = REG_STATE_REGISTERED;
8921  return 0;
8922 }
8923 
8924 static int iax2_append_register(const char *hostname, const char *username,
8925  const char *secret, const char *porta)
8926 {
8927  struct iax2_registry *reg;
8928 
8929  if (!(reg = ast_calloc(1, sizeof(*reg) + strlen(hostname) + 1))) {
8930  return -1;
8931  }
8932 
8933  reg->addr.ss.ss_family = AST_AF_UNSPEC;
8934  if (ast_dnsmgr_lookup(hostname, &reg->addr, &reg->dnsmgr, srvlookup ? "_iax._udp" : NULL) < 0) {
8935  ast_free(reg);
8936  return -1;
8937  }
8938 
8939  ast_copy_string(reg->username, username, sizeof(reg->username));
8940  strcpy(reg->hostname, hostname); /* Note: This is safe */
8941 
8942  if (secret) {
8943  ast_copy_string(reg->secret, secret, sizeof(reg->secret));
8944  }
8945 
8946  reg->expire = -1;
8948 
8949  reg->port = ast_sockaddr_port(&reg->addr);
8950 
8951  if (!porta && !reg->port) {
8952  reg->port = IAX_DEFAULT_PORTNO;
8953  } else if (porta) {
8954  sscanf(porta, "%5d", &reg->port);
8955  }
8956 
8957  ast_sockaddr_set_port(&reg->addr, reg->port);
8958 
8959  AST_LIST_LOCK(&registrations);
8960  AST_LIST_INSERT_HEAD(&registrations, reg, entry);
8961  AST_LIST_UNLOCK(&registrations);
8962 
8963  return 0;
8964 }
8965 
8966 static int iax2_register(const char *value, int lineno)
8967 {
8968  char copy[256];
8969  char *username, *hostname, *secret;
8970  char *porta;
8971  char *stringp=NULL;
8972 
8973  if (!value)
8974  return -1;
8975 
8976  ast_copy_string(copy, value, sizeof(copy));
8977  stringp = copy;
8978  username = strsep(&stringp, "@");
8979  hostname = strsep(&stringp, "@");
8980 
8981  if (!hostname) {
8982  ast_log(LOG_WARNING, "Format for registration is user[:secret]@host[:port] at line %d\n", lineno);
8983  return -1;
8984  }
8985 
8986  stringp = username;
8987  username = strsep(&stringp, ":");
8988  secret = strsep(&stringp, ":");
8989  stringp = hostname;
8990  hostname = strsep(&stringp, ":");
8991  porta = strsep(&stringp, ":");
8992 
8993  if (porta && !atoi(porta)) {
8994  ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
8995  return -1;
8996  }
8997 
8998  return iax2_append_register(hostname, username, secret, porta);
8999 }
9000 
9001 
9002 static void register_peer_exten(struct iax2_peer *peer, int onoff)
9003 {
9004  char multi[256];
9005  char *stringp, *ext;
9006  if (!ast_strlen_zero(regcontext)) {
9007  ast_copy_string(multi, S_OR(peer->regexten, peer->name), sizeof(multi));
9008  stringp = multi;
9009  while((ext = strsep(&stringp, "&"))) {
9010  if (onoff) {
9011  if (!ast_exists_extension(NULL, regcontext, ext, 1, NULL))
9012  ast_add_extension(regcontext, 1, ext, 1, NULL, NULL,
9013  "Noop", ast_strdup(peer->name), ast_free_ptr, "IAX2");
9014  } else
9015  ast_context_remove_extension(regcontext, ext, 1, NULL);
9016  }
9017  }
9018 }
9019 static void prune_peers(void);
9020 
9021 static void unlink_peer(struct iax2_peer *peer)
9022 {
9023  if (peer->expire > -1) {
9024  if (!AST_SCHED_DEL(sched, peer->expire)) {
9025  peer->expire = -1;
9026  peer_unref(peer);
9027  }
9028  }
9029 
9030  if (peer->pokeexpire > -1) {
9031  if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
9032  peer->pokeexpire = -1;
9033  peer_unref(peer);
9034  }
9035  }
9036 
9037  ao2_unlink(peers, peer);
9038 }
9039 
9040 static void __expire_registry(const void *data)
9041 {
9042  struct iax2_peer *peer = (struct iax2_peer *) data;
9043  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
9044 
9045  if (!peer)
9046  return;
9047  if (peer->expire == -1) {
9048  /* Removed already (possibly through CLI), ignore */
9049  return;
9050  }
9051 
9052  peer->expire = -1;
9053 
9054  ast_debug(1, "Expiring registration for peer '%s'\n", peer->name);
9055  if (ast_test_flag64((&globalflags), IAX_RTUPDATE) && (ast_test_flag64(peer, IAX_TEMPONLY|IAX_RTCACHEFRIENDS)))
9056  realtime_update_peer(peer->name, &peer->addr, 0);
9058  blob = ast_json_pack("{s: s, s: s}",
9059  "peer_status", "Unregistered",
9060  "cause", "Expired");
9062  /* modify entry in peercnts table as _not_ registered */
9063  peercnt_modify((unsigned char) 0, 0, &peer->addr);
9064  /* Reset the address */
9065  ast_sockaddr_setnull(&peer->addr);
9066  /* Reset expiry value */
9067  peer->expiry = min_reg_expire;
9068  if (!ast_test_flag64(peer, IAX_TEMPONLY))
9069  ast_db_del("IAX/Registry", peer->name);
9070  register_peer_exten(peer, 0);
9071  ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "IAX2/%s", peer->name); /* Activate notification */
9072  if (iax2_regfunk)
9073  iax2_regfunk(peer->name, 0);
9074 
9075  if (ast_test_flag64(peer, IAX_RTAUTOCLEAR))
9076  unlink_peer(peer);
9077 
9078  peer_unref(peer);
9079 }
9080 
9081 static int expire_registry(const void *data)
9082 {
9083 #ifdef SCHED_MULTITHREADED
9084  if (schedule_action(__expire_registry, data))
9085 #endif
9086  __expire_registry(data);
9087  return 0;
9088 }
9089 
9090 static void reg_source_db(struct iax2_peer *p)
9091 {
9092  char data[80];
9093  char *expiry;
9094 
9095  if (ast_test_flag64(p, IAX_TEMPONLY) || ast_db_get("IAX/Registry", p->name, data, sizeof(data))) {
9096  return;
9097  }
9098 
9099  expiry = strrchr(data, ':');
9100  if (!expiry) {
9101  ast_log(LOG_NOTICE, "IAX/Registry astdb entry missing expiry: '%s'\n", data);
9102  return;
9103  }
9104  *expiry++ = '\0';
9105 
9106  if (!ast_sockaddr_parse(&p->addr, data, PARSE_PORT_REQUIRE)) {
9107  ast_log(LOG_NOTICE, "IAX/Registry astdb host:port invalid - '%s'\n", data);
9108  return;
9109  }
9110 
9111  p->expiry = atoi(expiry);
9112 
9113  ast_verb(3, "Seeding '%s' at %s for %d\n", p->name,
9114  ast_sockaddr_stringify(&p->addr), p->expiry);
9115 
9116  iax2_poke_peer(p, 0);
9117  if (p->expire > -1) {
9118  if (!AST_SCHED_DEL(sched, p->expire)) {
9119  p->expire = -1;
9120  peer_unref(p);
9121  }
9122  }
9123 
9124  ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "IAX2/%s", p->name); /* Activate notification */
9125 
9126  p->expire = iax2_sched_add(sched, (p->expiry + 10) * 1000, expire_registry, peer_ref(p));
9127  if (p->expire == -1) {
9128  peer_unref(p);
9129  }
9130 
9131  if (iax2_regfunk) {
9132  iax2_regfunk(p->name, 1);
9133  }
9134 
9135  register_peer_exten(p, 1);
9136 }
9137 
9138 /*!
9139  * \pre iaxsl[callno] is locked
9140  *
9141  * \note Since this function calls send_command_final(), the pvt struct for
9142  * the given call number may disappear while executing this function.
9143  */
9144 static int update_registry(struct ast_sockaddr *addr, int callno, char *devtype, int fd, unsigned short refresh)
9145 {
9146 
9147  /* Called from IAX thread only, with proper iaxsl lock */
9148  struct iax_ie_data ied = {
9149  .pos = 0,
9150  };
9151  struct iax2_peer *p;
9152  int msgcount;
9153  char data[80];
9154  uint16_t version;
9155  const char *peer_name;
9156  int res = -1;
9157  char *str_addr;
9158 
9159  peer_name = ast_strdupa(iaxs[callno]->peer);
9160 
9161  /* SLD: Another find_peer call during registration - this time when we are really updating our registration */
9162  ast_mutex_unlock(&iaxsl[callno]);
9163  if (!(p = find_peer(peer_name, 1))) {
9164  ast_mutex_lock(&iaxsl[callno]);
9165  ast_log(LOG_WARNING, "No such peer '%s'\n", peer_name);
9166  return -1;
9167  }
9168  ast_mutex_lock(&iaxsl[callno]);
9169  if (!iaxs[callno])
9170  goto return_unref;
9171 
9172  if (ast_test_flag64((&globalflags), IAX_RTUPDATE) && (ast_test_flag64(p, IAX_TEMPONLY|IAX_RTCACHEFRIENDS))) {
9173  if (!ast_sockaddr_isnull(addr)) {
9174  time_t nowtime;
9175  time(&nowtime);
9176  realtime_update_peer(peer_name, addr, nowtime);
9177  } else {
9178  realtime_update_peer(peer_name, addr, 0);
9179  }
9180  }
9181 
9182  /* treat an unspecified refresh interval as the minimum */
9183  if (!refresh) {
9184  refresh = min_reg_expire;
9185  }
9186  if (refresh > max_reg_expire) {
9187  ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
9188  p->name, max_reg_expire, refresh);
9189  p->expiry = max_reg_expire;
9190  } else if (refresh < min_reg_expire) {
9191  ast_log(LOG_NOTICE, "Restricting registration for peer '%s' to %d seconds (requested %d)\n",
9192  p->name, min_reg_expire, refresh);
9193  p->expiry = min_reg_expire;
9194  } else {
9195  p->expiry = refresh;
9196  }
9197 
9198  if (ast_sockaddr_cmp(&p->addr, addr)) {
9199  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
9200 
9201  if (iax2_regfunk) {
9202  iax2_regfunk(p->name, 1);
9203  }
9204 
9205  /* modify entry in peercnts table as _not_ registered */
9206  peercnt_modify((unsigned char) 0, 0, &p->addr);
9207 
9208  /* Stash the IP address from which they registered */
9209  ast_sockaddr_copy(&p->addr, addr);
9210 
9211  str_addr = ast_strdupa(ast_sockaddr_stringify_addr(addr));
9212 
9213  snprintf(data, sizeof(data), "%s:%d", ast_sockaddr_stringify(addr), p->expiry);
9214 
9215  if (!ast_test_flag64(p, IAX_TEMPONLY) && !ast_sockaddr_isnull(addr)) {
9216  ast_db_put("IAX/Registry", p->name, data);
9217  ast_verb(3, "Registered IAX2 '%s' (%s) at %s\n",
9218  p->name,
9219  ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED",
9220  ast_sockaddr_stringify(addr));
9222  blob = ast_json_pack("{s: s, s: s, s: i}",
9223  "peer_status", "Registered",
9224  "address", str_addr,
9225  "port", ast_sockaddr_port(addr));
9226  register_peer_exten(p, 1);
9227  ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "IAX2/%s", p->name); /* Activate notification */
9228  } else if (!ast_test_flag64(p, IAX_TEMPONLY)) {
9229  ast_verb(3, "Unregistered IAX2 '%s' (%s)\n",
9230  p->name,
9231  ast_test_flag(&iaxs[callno]->state, IAX_STATE_AUTHENTICATED) ? "AUTHENTICATED" : "UNAUTHENTICATED");
9233  blob = ast_json_pack("{s: s}",
9234  "peer_status", "Unregistered");
9235  register_peer_exten(p, 0);
9236  ast_db_del("IAX/Registry", p->name);
9237  ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "IAX2/%s", p->name); /* Activate notification */
9238  }
9239 
9241 
9242  /* Update the host */
9243  /* Verify that the host is really there */
9244  iax2_poke_peer(p, callno);
9245  }
9246 
9247  /* modify entry in peercnts table as registered */
9248  if (p->maxcallno) {
9249  peercnt_modify((unsigned char) 1, p->maxcallno, &p->addr);
9250  }
9251 
9252  /* Make sure our call still exists, an INVAL at the right point may make it go away */
9253  if (!iaxs[callno]) {
9254  res = -1;
9255  goto return_unref;
9256  }
9257 
9258  /* Store socket fd */
9259  p->sockfd = fd;
9260  /* Setup the expiry */
9261  if (p->expire > -1) {
9262  if (!AST_SCHED_DEL(sched, p->expire)) {
9263  p->expire = -1;
9264  peer_unref(p);
9265  }
9266  }
9267 
9268  if (p->expiry && !ast_sockaddr_isnull(addr)) {
9269  p->expire = iax2_sched_add(sched, (p->expiry + 10) * 1000, expire_registry, peer_ref(p));
9270  if (p->expire == -1)
9271  peer_unref(p);
9272  }
9273  iax_ie_append_str(&ied, IAX_IE_USERNAME, p->name);
9274  iax_ie_append_int(&ied, IAX_IE_DATETIME, iax2_datetime(p->zonetag));
9275  if (!ast_sockaddr_isnull(addr)) {
9276  struct ast_sockaddr peer_addr;
9277 
9278  ast_sockaddr_copy(&peer_addr, &p->addr);
9279 
9280  iax_ie_append_short(&ied, IAX_IE_REFRESH, p->expiry);
9281  iax_ie_append_addr(&ied, IAX_IE_APPARENT_ADDR, &peer_addr);
9282  if (!ast_strlen_zero(p->mailbox)) {
9283  int new, old;
9284  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
9285 
9287  if (msg) {
9288  struct ast_mwi_state *mwi_state = stasis_message_data(msg);
9289  new = mwi_state->new_msgs;
9290  old = mwi_state->old_msgs;
9291  } else { /* Fall back on checking the mailbox directly */
9292  ast_app_inboxcount(p->mailbox, &new, &old);
9293  }
9294 
9295  if (new > 255) {
9296  new = 255;
9297  }
9298  if (old > 255) {
9299  old = 255;
9300  }
9301  msgcount = (old << 8) | new;
9302 
9303  iax_ie_append_short(&ied, IAX_IE_MSGCOUNT, msgcount);
9304  }
9305  if (ast_test_flag64(p, IAX_HASCALLERID)) {
9306  iax_ie_append_str(&ied, IAX_IE_CALLING_NUMBER, p->cid_num);
9307  iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, p->cid_name);
9308  }
9309  }
9310  if (iax_firmware_get_version(devtype, &version)) {
9311  iax_ie_append_short(&ied, IAX_IE_FIRMWAREVER, version);
9312  }
9313 
9314  res = 0;
9315 
9316 return_unref:
9317  peer_unref(p);
9318 
9319  return res ? res : send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGACK, 0, ied.buf, ied.pos, -1);
9320 }
9321 
9322 static int registry_authrequest(int callno)
9323 {
9324  struct iax_ie_data ied;
9325  struct iax2_peer *p;
9326  char challenge[10];
9327  const char *peer_name;
9328  int sentauthmethod;
9329 
9330  peer_name = ast_strdupa(iaxs[callno]->peer);
9331 
9332  /* SLD: third call to find_peer in registration */
9333  ast_mutex_unlock(&iaxsl[callno]);
9334  if ((p = find_peer(peer_name, 1))) {
9335  last_authmethod = p->authmethods;
9336  }
9337 
9338  ast_mutex_lock(&iaxsl[callno]);
9339  if (!iaxs[callno])
9340  goto return_unref;
9341 
9342  memset(&ied, 0, sizeof(ied));
9343  /* The selection of which delayed reject is sent may leak information,
9344  * if it sets a static response. For example, if a host is known to only
9345  * use MD5 authentication, then an RSA response would indicate that the
9346  * peer does not exist, and vice-versa.
9347  * Therefore, we use whatever the last peer used (which may vary over the
9348  * course of a server, which should leak minimal information). */
9349  sentauthmethod = p ? p->authmethods : last_authmethod ? last_authmethod : IAX_AUTH_MD5;
9350  if (!p) {
9351  iaxs[callno]->authmethods = sentauthmethod;
9352  }
9353  iax_ie_append_short(&ied, IAX_IE_AUTHMETHODS, sentauthmethod);
9354  if (sentauthmethod & (IAX_AUTH_RSA | IAX_AUTH_MD5)) {
9355  /* Build the challenge */
9356  snprintf(challenge, sizeof(challenge), "%d", (int)ast_random());
9357  ast_string_field_set(iaxs[callno], challenge, challenge);
9358  iax_ie_append_str(&ied, IAX_IE_CHALLENGE, iaxs[callno]->challenge);
9359  }
9360  iax_ie_append_str(&ied, IAX_IE_USERNAME, peer_name);
9361 
9362 return_unref:
9363  if (p) {
9364  peer_unref(p);
9365  }
9366 
9367  return iaxs[callno] ? send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGAUTH, 0, ied.buf, ied.pos, -1) : -1;
9368 }
9369 
9370 static int registry_rerequest(struct iax_ies *ies, int callno, struct ast_sockaddr *addr)
9371 {
9372  struct iax2_registry *reg;
9373  /* Start pessimistic */
9374  struct iax_ie_data ied;
9375  char peer[256] = "";
9376  char challenge[256] = "";
9377  int res;
9378  int authmethods = 0;
9379  if (ies->authmethods)
9380  authmethods = ies->authmethods;
9381  if (ies->username)
9382  ast_copy_string(peer, ies->username, sizeof(peer));
9383  if (ies->challenge)
9384  ast_copy_string(challenge, ies->challenge, sizeof(challenge));
9385  memset(&ied, 0, sizeof(ied));
9386  reg = iaxs[callno]->reg;
9387  if (reg) {
9388 
9389  if (ast_sockaddr_cmp(&reg->addr, addr)) {
9390  ast_log(LOG_WARNING, "Received unsolicited registry authenticate request from '%s'\n", ast_sockaddr_stringify(addr));
9391  return -1;
9392  }
9393  if (ast_strlen_zero(reg->secret)) {
9394  ast_log(LOG_NOTICE, "No secret associated with peer '%s'\n", reg->username);
9395  reg->regstate = REG_STATE_NOAUTH;
9396  return -1;
9397  }
9398  iax_ie_append_str(&ied, IAX_IE_USERNAME, reg->username);
9399  iax_ie_append_short(&ied, IAX_IE_REFRESH, reg->refresh);
9400  if (reg->secret[0] == '[') {
9401  char tmpkey[256];
9402  ast_copy_string(tmpkey, reg->secret + 1, sizeof(tmpkey));
9403  tmpkey[strlen(tmpkey) - 1] = '\0';
9404  res = authenticate(challenge, NULL, tmpkey, authmethods, &ied, addr, NULL);
9405  } else
9406  res = authenticate(challenge, reg->secret, NULL, authmethods, &ied, addr, NULL);
9407  if (!res) {
9408  reg->regstate = REG_STATE_AUTHSENT;
9409  add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
9410  return send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_REGREQ, 0, ied.buf, ied.pos, -1);
9411  } else
9412  return -1;
9413  ast_log(LOG_WARNING, "Registry acknowledge on unknown registry '%s'\n", peer);
9414  } else
9415  ast_log(LOG_NOTICE, "Can't reregister without a reg\n");
9416  return -1;
9417 }
9418 
9419 static void stop_stuff(int callno)
9420 {
9421  iax2_destroy_helper(iaxs[callno]);
9422 }
9423 
9424 static void __auth_reject(const void *nothing)
9425 {
9426  /* Called from IAX thread only, without iaxs lock */
9427  int callno = (int)(long)(nothing);
9428  struct iax_ie_data ied;
9429  ast_mutex_lock(&iaxsl[callno]);
9430  if (iaxs[callno]) {
9431  memset(&ied, 0, sizeof(ied));
9432  if (iaxs[callno]->authfail == IAX_COMMAND_REGREJ) {
9433  iax_ie_append_str(&ied, IAX_IE_CAUSE, "Registration Refused");
9434  iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_REJECTED);
9435  } else if (iaxs[callno]->authfail == IAX_COMMAND_REJECT) {
9436  iax_ie_append_str(&ied, IAX_IE_CAUSE, "No authority found");
9437  iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_NOT_SUBSCRIBED);
9438  }
9439  send_command_final(iaxs[callno], AST_FRAME_IAX, iaxs[callno]->authfail, 0, ied.buf, ied.pos, -1);
9440  }
9441  ast_mutex_unlock(&iaxsl[callno]);
9442 }
9443 
9444 static int auth_reject(const void *data)
9445 {
9446  int callno = (int)(long)(data);
9447  ast_mutex_lock(&iaxsl[callno]);
9448  if (iaxs[callno])
9449  iaxs[callno]->authid = -1;
9450  ast_mutex_unlock(&iaxsl[callno]);
9451 #ifdef SCHED_MULTITHREADED
9452  if (schedule_action(__auth_reject, data))
9453 #endif
9454  __auth_reject(data);
9455  return 0;
9456 }
9457 
9458 static int auth_fail(int callno, int failcode)
9459 {
9460  /* Schedule sending the authentication failure in one second, to prevent
9461  guessing */
9462  if (iaxs[callno]) {
9463  iaxs[callno]->authfail = failcode;
9464  if (delayreject) {
9465  iaxs[callno]->authid = iax2_sched_replace(iaxs[callno]->authid,
9466  sched, 1000, auth_reject, (void *)(long)callno);
9467  } else
9468  auth_reject((void *)(long)callno);
9469  }
9470  return 0;
9471 }
9472 
9473 static void __auto_hangup(const void *nothing)
9474 {
9475  /* Called from IAX thread only, without iaxs lock */
9476  int callno = (int)(long)(nothing);
9477  struct iax_ie_data ied;
9478  ast_mutex_lock(&iaxsl[callno]);
9479  if (iaxs[callno]) {
9480  memset(&ied, 0, sizeof(ied));
9481  iax_ie_append_str(&ied, IAX_IE_CAUSE, "Timeout");
9482  iax_ie_append_byte(&ied, IAX_IE_CAUSECODE, AST_CAUSE_NO_USER_RESPONSE);
9483  send_command_final(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_HANGUP, 0, ied.buf, ied.pos, -1);
9484  }
9485  ast_mutex_unlock(&iaxsl[callno]);
9486 }
9487 
9488 static int auto_hangup(const void *data)
9489 {
9490  int callno = (int)(long)(data);
9491  ast_mutex_lock(&iaxsl[callno]);
9492  if (iaxs[callno]) {
9493  iaxs[callno]->autoid = -1;
9494  }
9495  ast_mutex_unlock(&iaxsl[callno]);
9496 #ifdef SCHED_MULTITHREADED
9497  if (schedule_action(__auto_hangup, data))
9498 #endif
9499  __auto_hangup(data);
9500  return 0;
9501 }
9502 
9503 static void iax2_dprequest(struct iax2_dpcache *dp, int callno)
9504 {
9505  struct iax_ie_data ied;
9506  /* Auto-hangup with 30 seconds of inactivity */
9507  iaxs[callno]->autoid = iax2_sched_replace(iaxs[callno]->autoid,
9508  sched, 30000, auto_hangup, (void *)(long)callno);
9509  memset(&ied, 0, sizeof(ied));
9510  iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, dp->exten);
9511  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_DPREQ, 0, ied.buf, ied.pos, -1);
9512  dp->flags |= CACHE_FLAG_TRANSMITTED;
9513 }
9514 
9515 static int iax2_vnak(int callno)
9516 {
9517  return send_command_immediate(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_VNAK, 0, NULL, 0, iaxs[callno]->iseqno);
9518 }
9519 
9520 static void vnak_retransmit(int callno, int last)
9521 {
9522  struct iax_frame *f;
9523 
9524  AST_LIST_TRAVERSE(&frame_queue[callno], f, list) {
9525  /* Send a copy immediately */
9526  if (((unsigned char) (f->oseqno - last) < 128) &&
9527  (f->retries >= 0)) {
9528  send_packet(f);
9529  }
9530  }
9531 }
9532 
9533 static void __iax2_poke_peer_s(const void *data)
9534 {
9535  struct iax2_peer *peer = (struct iax2_peer *)data;
9536  iax2_poke_peer(peer, 0);
9537  peer_unref(peer);
9538 }
9539 
9540 static int iax2_poke_peer_s(const void *data)
9541 {
9542  struct iax2_peer *peer = (struct iax2_peer *)data;
9543  peer->pokeexpire = -1;
9544 #ifdef SCHED_MULTITHREADED
9545  if (schedule_action(__iax2_poke_peer_s, data))
9546 #endif
9547  __iax2_poke_peer_s(data);
9548  return 0;
9549 }
9550 
9551 static int send_trunk(struct iax2_trunk_peer *tpeer, struct timeval *now)
9552 {
9553  int res = 0;
9554  struct iax_frame *fr;
9555  struct ast_iax2_meta_hdr *meta;
9556  struct ast_iax2_meta_trunk_hdr *mth;
9557  int calls = 0;
9558 
9559  /* Point to frame */
9560  fr = (struct iax_frame *)tpeer->trunkdata;
9561  /* Point to meta data */
9562  meta = (struct ast_iax2_meta_hdr *)fr->afdata;
9563  mth = (struct ast_iax2_meta_trunk_hdr *)meta->data;
9564  if (tpeer->trunkdatalen) {
9565  /* We're actually sending a frame, so fill the meta trunk header and meta header */
9566  meta->zeros = 0;
9567  meta->metacmd = IAX_META_TRUNK;
9568  if (ast_test_flag64(&globalflags, IAX_TRUNKTIMESTAMPS))
9569  meta->cmddata = IAX_META_TRUNK_MINI;
9570  else
9572  mth->ts = htonl(calc_txpeerstamp(tpeer, trunkfreq, now));
9573  /* And the rest of the ast_iax2 header */
9574  fr->direction = DIRECTION_OUTGRESS;
9575  fr->retrans = -1;
9576  fr->transfer = 0;
9577  /* Any appropriate call will do */
9578  fr->data = fr->afdata;
9579  fr->datalen = tpeer->trunkdatalen + sizeof(struct ast_iax2_meta_hdr) + sizeof(struct ast_iax2_meta_trunk_hdr);
9580  res = transmit_trunk(fr, &tpeer->addr, tpeer->sockfd);
9581  calls = tpeer->calls;
9582 #if 0
9583  ast_debug(1, "Trunking %d call chunks in %d bytes to %s:%d, ts=%d\n", calls, fr->datalen, ast_inet_ntoa(tpeer->addr.sin_addr), ntohs(tpeer->addr.sin_port), ntohl(mth->ts));
9584 #endif
9585  /* Reset transmit trunk side data */
9586  tpeer->trunkdatalen = 0;
9587  tpeer->calls = 0;
9588  }
9589  if (res < 0)
9590  return res;
9591  return calls;
9592 }
9593 
9594 static inline int iax2_trunk_expired(struct iax2_trunk_peer *tpeer, struct timeval *now)
9595 {
9596  /* Drop when trunk is about 5 seconds idle */
9597  if (now->tv_sec > tpeer->trunkact.tv_sec + 5)
9598  return 1;
9599  return 0;
9600 }
9601 
9602 static int timing_read(int *id, int fd, short events, void *cbdata)
9603 {
9604  int res, processed = 0, totalcalls = 0;
9605  struct iax2_trunk_peer *tpeer = NULL, *drop = NULL;
9606  struct timeval now = ast_tvnow();
9607 
9608  if (iaxtrunkdebug) {
9609  ast_verbose("Beginning trunk processing. Trunk queue ceiling is %d bytes per host\n", trunkmaxsize);
9610  }
9611 
9612  if (timer) {
9613  if (ast_timer_ack(timer, 1) < 0) {
9614  ast_log(LOG_ERROR, "Timer failed acknowledge\n");
9615  return 0;
9616  }
9617  }
9618 
9619  /* For each peer that supports trunking... */
9621  AST_LIST_TRAVERSE_SAFE_BEGIN(&tpeers, tpeer, list) {
9622  processed++;
9623  res = 0;
9624  ast_mutex_lock(&tpeer->lock);
9625  /* We can drop a single tpeer per pass. That makes all this logic
9626  substantially easier */
9627  if (!drop && iax2_trunk_expired(tpeer, &now)) {
9628  /* Take it out of the list, but don't free it yet, because it
9629  could be in use */
9631  drop = tpeer;
9632  } else {
9633  res = send_trunk(tpeer, &now);
9634  trunk_timed++;
9635  if (iaxtrunkdebug) {
9636  ast_verbose(" - Trunk peer (%s) has %d call chunk%s in transit, %u bytes backlogged and has hit a high water mark of %u bytes\n",
9637  ast_sockaddr_stringify(&tpeer->addr),
9638  res,
9639  (res != 1) ? "s" : "",
9640  tpeer->trunkdatalen,
9641  tpeer->trunkdataalloc);
9642  }
9643  }
9644  totalcalls += res;
9645  res = 0;
9646  ast_mutex_unlock(&tpeer->lock);
9647  }
9650 
9651  if (drop) {
9652  ast_mutex_lock(&drop->lock);
9653  /* Once we have this lock, we're sure nobody else is using it or could use it once we release it,
9654  because by the time they could get tpeerlock, we've already grabbed it */
9655  ast_debug(1, "Dropping unused iax2 trunk peer '%s'\n", ast_sockaddr_stringify(&drop->addr));
9656  if (drop->trunkdata) {
9657  ast_free(drop->trunkdata);
9658  drop->trunkdata = NULL;
9659  }
9660  ast_mutex_unlock(&drop->lock);
9661  ast_mutex_destroy(&drop->lock);
9662  ast_free(drop);
9663  }
9664 
9665  if (iaxtrunkdebug) {
9666  ast_verbose("Ending trunk processing with %d peers and %d call chunks processed\n", processed, totalcalls);
9667  }
9668  iaxtrunkdebug = 0;
9669 
9670  return 1;
9671 }
9672 
9673 struct dpreq_data {
9674  int callno;
9675  char context[AST_MAX_EXTENSION];
9676  char callednum[AST_MAX_EXTENSION];
9677  char *callerid;
9678 };
9679 
9680 static void dp_lookup(int callno, const char *context, const char *callednum, const char *callerid, int skiplock)
9681 {
9682  unsigned short dpstatus = 0;
9683  struct iax_ie_data ied1;
9684  int mm;
9685 
9686  memset(&ied1, 0, sizeof(ied1));
9687  mm = ast_matchmore_extension(NULL, context, callednum, 1, callerid);
9688  /* Must be started */
9689  if (ast_exists_extension(NULL, context, callednum, 1, callerid)) {
9690  dpstatus = IAX_DPSTATUS_EXISTS;
9691  } else if (ast_canmatch_extension(NULL, context, callednum, 1, callerid)) {
9692  dpstatus = IAX_DPSTATUS_CANEXIST;
9693  } else {
9694  dpstatus = IAX_DPSTATUS_NONEXISTENT;
9695  }
9696  if (ast_ignore_pattern(context, callednum))
9697  dpstatus |= IAX_DPSTATUS_IGNOREPAT;
9698  if (mm)
9699  dpstatus |= IAX_DPSTATUS_MATCHMORE;
9700  if (!skiplock)
9701  ast_mutex_lock(&iaxsl[callno]);
9702  if (iaxs[callno]) {
9703  iax_ie_append_str(&ied1, IAX_IE_CALLED_NUMBER, callednum);
9704  iax_ie_append_short(&ied1, IAX_IE_DPSTATUS, dpstatus);
9705  iax_ie_append_short(&ied1, IAX_IE_REFRESH, iaxdefaultdpcache);
9706  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_DPREP, 0, ied1.buf, ied1.pos, -1);
9707  }
9708  if (!skiplock)
9709  ast_mutex_unlock(&iaxsl[callno]);
9710 }
9711 
9712 static void *dp_lookup_thread(void *data)
9713 {
9714  /* Look up for dpreq */
9715  struct dpreq_data *dpr = data;
9716  dp_lookup(dpr->callno, dpr->context, dpr->callednum, dpr->callerid, 0);
9717  if (dpr->callerid)
9718  ast_free(dpr->callerid);
9719  ast_free(dpr);
9720  return NULL;
9721 }
9722 
9723 static void spawn_dp_lookup(int callno, const char *context, const char *callednum, const char *callerid)
9724 {
9725  pthread_t newthread;
9726  struct dpreq_data *dpr;
9727 
9728  if (!(dpr = ast_calloc(1, sizeof(*dpr))))
9729  return;
9730 
9731  dpr->callno = callno;
9732  ast_copy_string(dpr->context, context, sizeof(dpr->context));
9733  ast_copy_string(dpr->callednum, callednum, sizeof(dpr->callednum));
9734  if (callerid)
9735  dpr->callerid = ast_strdup(callerid);
9736  if (ast_pthread_create_detached(&newthread, NULL, dp_lookup_thread, dpr)) {
9737  ast_log(LOG_WARNING, "Unable to start lookup thread!\n");
9738  }
9739 }
9740 
9741 static int check_provisioning(struct ast_sockaddr *addr, int sockfd, char *si, unsigned int ver)
9742 {
9743  unsigned int ourver;
9744  char rsi[80];
9745  snprintf(rsi, sizeof(rsi), "si-%s", si);
9746  if (iax_provision_version(&ourver, rsi, 1))
9747  return 0;
9748  ast_debug(1, "Service identifier '%s', we think '%08x', they think '%08x'\n", si, ourver, ver);
9749  if (ourver != ver)
9750  iax2_provision(addr, sockfd, NULL, rsi, 1);
9751  return 0;
9752 }
9753 
9754 static void construct_rr(struct chan_iax2_pvt *pvt, struct iax_ie_data *iep)
9755 {
9756  jb_info stats;
9757  jb_getinfo(pvt->jb, &stats);
9758 
9759  memset(iep, 0, sizeof(*iep));
9760 
9761  iax_ie_append_int(iep,IAX_IE_RR_JITTER, stats.jitter);
9762  if(stats.frames_in == 0) stats.frames_in = 1;
9763  iax_ie_append_int(iep,IAX_IE_RR_LOSS, ((0xff & (stats.losspct/1000)) << 24 | (stats.frames_lost & 0x00ffffff)));
9764  iax_ie_append_int(iep,IAX_IE_RR_PKTS, stats.frames_in);
9765  iax_ie_append_short(iep,IAX_IE_RR_DELAY, stats.current - stats.min);
9766  iax_ie_append_int(iep,IAX_IE_RR_DROPPED, stats.frames_dropped);
9767  iax_ie_append_int(iep,IAX_IE_RR_OOO, stats.frames_ooo);
9768 }
9769 
9770 static void save_rr(struct iax_frame *fr, struct iax_ies *ies)
9771 {
9772  iaxs[fr->callno]->remote_rr.jitter = ies->rr_jitter;
9773  iaxs[fr->callno]->remote_rr.losspct = ies->rr_loss >> 24;
9774  iaxs[fr->callno]->remote_rr.losscnt = ies->rr_loss & 0xffffff;
9775  iaxs[fr->callno]->remote_rr.packets = ies->rr_pkts;
9776  iaxs[fr->callno]->remote_rr.delay = ies->rr_delay;
9777  iaxs[fr->callno]->remote_rr.dropped = ies->rr_dropped;
9778  iaxs[fr->callno]->remote_rr.ooo = ies->rr_ooo;
9779 }
9780 
9781 static void save_osptoken(struct iax_frame *fr, struct iax_ies *ies)
9782 {
9783  int i;
9784  unsigned int length, offset = 0;
9785  char full_osptoken[IAX_MAX_OSPBUFF_SIZE];
9786 
9787  for (i = 0; i < IAX_MAX_OSPBLOCK_NUM; i++) {
9788  length = ies->ospblocklength[i];
9789  if (length != 0) {
9790  if (length > IAX_MAX_OSPBLOCK_SIZE) {
9791  /* OSP token block length wrong, clear buffer */
9792  offset = 0;
9793  break;
9794  } else {
9795  memcpy(full_osptoken + offset, ies->osptokenblock[i], length);
9796  offset += length;
9797  }
9798  } else {
9799  break;
9800  }
9801  }
9802  *(full_osptoken + offset) = '\0';
9803  if (strlen(full_osptoken) != offset) {
9804  /* OSP token length wrong, clear buffer */
9805  *full_osptoken = '\0';
9806  }
9807 
9808  ast_string_field_set(iaxs[fr->callno], osptoken, full_osptoken);
9809 }
9810 
9811 static void log_jitterstats(unsigned short callno)
9812 {
9813  int localjitter = -1, localdelay = 0, locallost = -1, locallosspct = -1, localdropped = 0, localooo = -1, localpackets = -1;
9814  jb_info jbinfo;
9815 
9816  ast_mutex_lock(&iaxsl[callno]);
9817  if (iaxs[callno] && iaxs[callno]->owner && ast_channel_name(iaxs[callno]->owner)) {
9818  if(ast_test_flag64(iaxs[callno], IAX_USEJITTERBUF)) {
9819  jb_getinfo(iaxs[callno]->jb, &jbinfo);
9820  localjitter = jbinfo.jitter;
9821  localdelay = jbinfo.current - jbinfo.min;
9822  locallost = jbinfo.frames_lost;
9823  locallosspct = jbinfo.losspct/1000;
9824  localdropped = jbinfo.frames_dropped;
9825  localooo = jbinfo.frames_ooo;
9826  localpackets = jbinfo.frames_in;
9827  }
9828  ast_debug(3, "JB STATS:%s ping=%u ljitterms=%d ljbdelayms=%d ltotlost=%d lrecentlosspct=%d ldropped=%d looo=%d lrecvd=%d rjitterms=%d rjbdelayms=%d rtotlost=%d rrecentlosspct=%d rdropped=%d rooo=%d rrecvd=%d\n",
9829  ast_channel_name(iaxs[callno]->owner),
9830  iaxs[callno]->pingtime,
9831  localjitter,
9832  localdelay,
9833  locallost,
9834  locallosspct,
9835  localdropped,
9836  localooo,
9837  localpackets,
9838  iaxs[callno]->remote_rr.jitter,
9839  iaxs[callno]->remote_rr.delay,
9840  iaxs[callno]->remote_rr.losscnt,
9841  iaxs[callno]->remote_rr.losspct/1000,
9842  iaxs[callno]->remote_rr.dropped,
9843  iaxs[callno]->remote_rr.ooo,
9844  iaxs[callno]->remote_rr.packets);
9845  }
9846  ast_mutex_unlock(&iaxsl[callno]);
9847 }
9848 
9849 static int socket_process(struct iax2_thread *thread);
9850 
9851 /*!
9852  * \brief Handle any deferred full frames for this thread
9853  */
9854 static void handle_deferred_full_frames(struct iax2_thread *thread)
9855 {
9856  struct iax2_pkt_buf *pkt_buf;
9857 
9858  ast_mutex_lock(&thread->lock);
9859 
9860  while ((pkt_buf = AST_LIST_REMOVE_HEAD(&thread->full_frames, entry))) {
9861  ast_mutex_unlock(&thread->lock);
9862 
9863  thread->buf = pkt_buf->buf;
9864  thread->buf_len = pkt_buf->len;
9865  thread->buf_size = pkt_buf->len + 1;
9866 
9867  socket_process(thread);
9868 
9869  thread->buf = NULL;
9870  ast_free(pkt_buf);
9871 
9872  ast_mutex_lock(&thread->lock);
9873  }
9874 
9875  ast_mutex_unlock(&thread->lock);
9876 }
9877 
9878 /*!
9879  * \brief Queue the last read full frame for processing by a certain thread
9880  *
9881  * If there are already any full frames queued, they are sorted
9882  * by sequence number.
9883  */
9884 static void defer_full_frame(struct iax2_thread *from_here, struct iax2_thread *to_here)
9885 {
9886  struct iax2_pkt_buf *pkt_buf, *cur_pkt_buf;
9887  struct ast_iax2_full_hdr *fh, *cur_fh;
9888 
9889  if (!(pkt_buf = ast_calloc(1, sizeof(*pkt_buf) + from_here->buf_len)))
9890  return;
9891 
9892  pkt_buf->len = from_here->buf_len;
9893  memcpy(pkt_buf->buf, from_here->buf, pkt_buf->len);
9894 
9895  fh = (struct ast_iax2_full_hdr *) pkt_buf->buf;
9896  ast_mutex_lock(&to_here->lock);
9897  AST_LIST_TRAVERSE_SAFE_BEGIN(&to_here->full_frames, cur_pkt_buf, entry) {
9898  cur_fh = (struct ast_iax2_full_hdr *) cur_pkt_buf->buf;
9899  if (fh->oseqno < cur_fh->oseqno) {
9900  AST_LIST_INSERT_BEFORE_CURRENT(pkt_buf, entry);
9901  break;
9902  }
9903  }
9905 
9906  if (!cur_pkt_buf)
9907  AST_LIST_INSERT_TAIL(&to_here->full_frames, pkt_buf, entry);
9908 
9909  to_here->iostate = IAX_IOSTATE_READY;
9910  ast_cond_signal(&to_here->cond);
9911 
9912  ast_mutex_unlock(&to_here->lock);
9913 }
9914 
9915 static int socket_read(int *id, int fd, short events, void *cbdata)
9916 {
9917  struct iax2_thread *thread;
9918  time_t t;
9919  static time_t last_errtime = 0;
9920  struct ast_iax2_full_hdr *fh;
9921 
9922  if (!(thread = find_idle_thread())) {
9923  time(&t);
9924  if (t != last_errtime) {
9925  last_errtime = t;
9926  ast_debug(1, "Out of idle IAX2 threads for I/O, pausing!\n");
9927  }
9928  usleep(1);
9929  return 1;
9930  }
9931 
9932  thread->iofd = fd;
9933  thread->buf_len = ast_recvfrom(fd, thread->readbuf, sizeof(thread->readbuf), 0, &thread->ioaddr);
9934  thread->buf_size = sizeof(thread->readbuf);
9935  thread->buf = thread->readbuf;
9936  if (thread->buf_len < 0) {
9937  if (errno != ECONNREFUSED && errno != EAGAIN)
9938  ast_log(LOG_WARNING, "Error: %s\n", strerror(errno));
9939  handle_error();
9940  thread->iostate = IAX_IOSTATE_IDLE;
9941  signal_condition(&thread->lock, &thread->cond);
9942  return 1;
9943  }
9944  if (test_losspct && ((100.0 * ast_random() / (RAND_MAX + 1.0)) < test_losspct)) { /* simulate random loss condition */
9945  thread->iostate = IAX_IOSTATE_IDLE;
9946  signal_condition(&thread->lock, &thread->cond);
9947  return 1;
9948  }
9949 
9950  /* Determine if this frame is a full frame; if so, and any thread is currently
9951  processing a full frame for the same callno from this peer, then drop this
9952  frame (and the peer will retransmit it) */
9953  fh = (struct ast_iax2_full_hdr *) thread->buf;
9954  if (ntohs(fh->scallno) & IAX_FLAG_FULL) {
9955  struct iax2_thread *cur = NULL;
9956  uint16_t callno = ntohs(fh->scallno) & ~IAX_FLAG_FULL;
9957 
9959  AST_LIST_TRAVERSE(&active_list, cur, list) {
9960  if ((cur->ffinfo.callno == callno) &&
9961  !ast_sockaddr_cmp_addr(&cur->ffinfo.addr, &thread->ioaddr))
9962  break;
9963  }
9964  if (cur) {
9965  /* we found another thread processing a full frame for this call,
9966  so queue it up for processing later. */
9967  defer_full_frame(thread, cur);
9969  thread->iostate = IAX_IOSTATE_IDLE;
9970  signal_condition(&thread->lock, &thread->cond);
9971  return 1;
9972  } else {
9973  /* this thread is going to process this frame, so mark it */
9974  thread->ffinfo.callno = callno;
9975  ast_sockaddr_copy(&thread->ffinfo.addr, &thread->ioaddr);
9976  thread->ffinfo.type = fh->type;
9977  thread->ffinfo.csub = fh->csub;
9978  AST_LIST_INSERT_HEAD(&active_list, thread, list);
9979  }
9981  }
9982 
9983  /* Mark as ready and send on its way */
9984  thread->iostate = IAX_IOSTATE_READY;
9985 #ifdef DEBUG_SCHED_MULTITHREAD
9986  ast_copy_string(thread->curfunc, "socket_process", sizeof(thread->curfunc));
9987 #endif
9988  signal_condition(&thread->lock, &thread->cond);
9989 
9990  return 1;
9991 }
9992 
9993 static int socket_process_meta(int packet_len, struct ast_iax2_meta_hdr *meta, struct ast_sockaddr *addr, int sockfd,
9994  struct iax_frame *fr)
9995 {
9996  unsigned char metatype;
9997  struct ast_iax2_meta_trunk_mini *mtm;
9998  struct ast_iax2_meta_trunk_hdr *mth;
9999  struct ast_iax2_meta_trunk_entry *mte;
10000  struct iax2_trunk_peer *tpeer;
10001  unsigned int ts;
10002  void *ptr;
10003  struct timeval rxtrunktime;
10004  struct ast_frame f = { 0, };
10005 
10006  if (packet_len < sizeof(*meta)) {
10007  ast_log(LOG_WARNING, "Rejecting packet from '%s' that is flagged as a meta frame but is too short\n",
10008  ast_sockaddr_stringify(addr));
10009  return 1;
10010  }
10011 
10012  if (meta->metacmd != IAX_META_TRUNK)
10013  return 1;
10014 
10015  if (packet_len < (sizeof(*meta) + sizeof(*mth))) {
10016  ast_log(LOG_WARNING, "midget meta trunk packet received (%d of %d min)\n", packet_len,
10017  (int) (sizeof(*meta) + sizeof(*mth)));
10018  return 1;
10019  }
10020  mth = (struct ast_iax2_meta_trunk_hdr *)(meta->data);
10021  ts = ntohl(mth->ts);
10022  metatype = meta->cmddata;
10023  packet_len -= (sizeof(*meta) + sizeof(*mth));
10024  ptr = mth->data;
10025  tpeer = find_tpeer(addr, sockfd);
10026  if (!tpeer) {
10027  ast_log(LOG_WARNING, "Unable to accept trunked packet from '%s': No matching peer\n",
10028  ast_sockaddr_stringify(addr));
10029  return 1;
10030  }
10031  tpeer->trunkact = ast_tvnow();
10032  if (!ts || ast_tvzero(tpeer->rxtrunktime))
10033  tpeer->rxtrunktime = tpeer->trunkact;
10034  rxtrunktime = tpeer->rxtrunktime;
10035  ast_mutex_unlock(&tpeer->lock);
10036  while (packet_len >= sizeof(*mte)) {
10037  /* Process channels */
10038  unsigned short callno, trunked_ts, len;
10039 
10040  if (metatype == IAX_META_TRUNK_MINI) {
10041  mtm = (struct ast_iax2_meta_trunk_mini *) ptr;
10042  ptr += sizeof(*mtm);
10043  packet_len -= sizeof(*mtm);
10044  len = ntohs(mtm->len);
10045  callno = ntohs(mtm->mini.callno);
10046  trunked_ts = ntohs(mtm->mini.ts);
10047  } else if (metatype == IAX_META_TRUNK_SUPERMINI) {
10048  mte = (struct ast_iax2_meta_trunk_entry *)ptr;
10049  ptr += sizeof(*mte);
10050  packet_len -= sizeof(*mte);
10051  len = ntohs(mte->len);
10052  callno = ntohs(mte->callno);
10053  trunked_ts = 0;
10054  } else {
10055  ast_log(LOG_WARNING, "Unknown meta trunk cmd from '%s': dropping\n", ast_sockaddr_stringify(addr));
10056  break;
10057  }
10058  /* Stop if we don't have enough data */
10059  if (len > packet_len)
10060  break;
10061  fr->callno = find_callno_locked(callno & ~IAX_FLAG_FULL, 0, addr, NEW_PREVENT, sockfd, 0);
10062  if (!fr->callno)
10063  continue;
10064 
10065  /* If it's a valid call, deliver the contents. If not, we
10066  drop it, since we don't have a scallno to use for an INVAL */
10067  /* Process as a mini frame */
10068  memset(&f, 0, sizeof(f));
10070  if (!iaxs[fr->callno]) {
10071  /* drop it */
10072  } else if (iaxs[fr->callno]->voiceformat == 0) {
10073  ast_log(LOG_WARNING, "Received trunked frame before first full voice frame\n");
10074  iax2_vnak(fr->callno);
10076  iaxs[fr->callno]->voiceformat))) {
10077  f.datalen = len;
10078  if (f.datalen >= 0) {
10079  if (f.datalen)
10080  f.data.ptr = ptr;
10081  else
10082  f.data.ptr = NULL;
10083  if (trunked_ts)
10084  fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | (trunked_ts & 0xffff);
10085  else
10086  fr->ts = fix_peerts(&rxtrunktime, fr->callno, ts);
10087  /* Don't pass any packets until we're started */
10088  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
10089  struct iax_frame *duped_fr;
10090 
10091  /* Common things */
10092  f.src = "IAX2";
10093  f.mallocd = 0;
10094  f.offset = 0;
10095  if (f.datalen && (f.frametype == AST_FRAME_VOICE))
10097  else
10098  f.samples = 0;
10099  fr->outoforder = 0;
10100  iax_frame_wrap(fr, &f);
10101  duped_fr = iaxfrdup2(fr);
10102  if (duped_fr)
10103  schedule_delivery(duped_fr, 1, 1, &fr->ts);
10104  if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts)
10105  iaxs[fr->callno]->last = fr->ts;
10106  }
10107  } else {
10108  ast_log(LOG_WARNING, "Datalen < 0?\n");
10109  }
10110  }
10111  ast_mutex_unlock(&iaxsl[fr->callno]);
10112  ptr += len;
10113  packet_len -= len;
10114  }
10115 
10116  return 1;
10117 }
10118 
10119 static int acf_iaxvar_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
10120 {
10121  struct ast_datastore *variablestore;
10122  AST_LIST_HEAD(, ast_var_t) *varlist;
10123  struct ast_var_t *var;
10124 
10125  if (!chan) {
10126  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
10127  return -1;
10128  }
10129 
10130  variablestore = ast_channel_datastore_find(chan, &iax2_variable_datastore_info, NULL);
10131  if (!variablestore) {
10132  *buf = '\0';
10133  return 0;
10134  }
10135  varlist = variablestore->data;
10136 
10137  AST_LIST_LOCK(varlist);
10138  AST_LIST_TRAVERSE(varlist, var, entries) {
10139  if (strcmp(var->name, data) == 0) {
10140  ast_copy_string(buf, var->value, len);
10141  break;
10142  }
10143  }
10144  AST_LIST_UNLOCK(varlist);
10145  return 0;
10146 }
10147 
10148 static int acf_iaxvar_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
10149 {
10150  struct ast_datastore *variablestore;
10151  AST_LIST_HEAD(, ast_var_t) *varlist;
10152  struct ast_var_t *var;
10153 
10154  if (!chan) {
10155  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
10156  return -1;
10157  }
10158 
10159  variablestore = ast_channel_datastore_find(chan, &iax2_variable_datastore_info, NULL);
10160  if (!variablestore) {
10161  variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
10162  if (!variablestore) {
10163  ast_log(LOG_ERROR, "Memory allocation error\n");
10164  return -1;
10165  }
10166  varlist = ast_calloc(1, sizeof(*varlist));
10167  if (!varlist) {
10168  ast_datastore_free(variablestore);
10169  ast_log(LOG_ERROR, "Unable to assign new variable '%s'\n", data);
10170  return -1;
10171  }
10172 
10173  AST_LIST_HEAD_INIT(varlist);
10174  variablestore->data = varlist;
10175  variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
10176  ast_channel_datastore_add(chan, variablestore);
10177  } else
10178  varlist = variablestore->data;
10179 
10180  AST_LIST_LOCK(varlist);
10181  AST_LIST_TRAVERSE_SAFE_BEGIN(varlist, var, entries) {
10182  if (strcmp(var->name, data) == 0) {
10183  AST_LIST_REMOVE_CURRENT(entries);
10184  ast_var_delete(var);
10185  break;
10186  }
10187  }
10189  var = ast_var_assign(data, value);
10190  if (var)
10191  AST_LIST_INSERT_TAIL(varlist, var, entries);
10192  else
10193  ast_log(LOG_ERROR, "Unable to assign new variable '%s'\n", data);
10194  AST_LIST_UNLOCK(varlist);
10195  return 0;
10196 }
10197 
10198 static struct ast_custom_function iaxvar_function = {
10199  .name = "IAXVAR",
10200  .read = acf_iaxvar_read,
10201  .write = acf_iaxvar_write,
10202 };
10203 
10204 static void set_hangup_source_and_cause(int callno, unsigned char causecode)
10205 {
10206  iax2_lock_owner(callno);
10207  if (iaxs[callno] && iaxs[callno]->owner) {
10208  struct ast_channel *owner;
10209  const char *name;
10210 
10211  owner = iaxs[callno]->owner;
10212  if (causecode) {
10213  ast_channel_hangupcause_set(owner, causecode);
10214  }
10215  name = ast_strdupa(ast_channel_name(owner));
10216  ast_channel_ref(owner);
10217  ast_channel_unlock(owner);
10218  ast_mutex_unlock(&iaxsl[callno]);
10219  ast_set_hangupsource(owner, name, 0);
10220  ast_channel_unref(owner);
10221  ast_mutex_lock(&iaxsl[callno]);
10222  }
10223 }
10224 
10225 static int socket_process_helper(struct iax2_thread *thread)
10226 {
10227  struct ast_sockaddr addr;
10228  int res;
10229  int updatehistory=1;
10230  int new = NEW_PREVENT;
10231  int dcallno = 0;
10232  char decrypted = 0;
10233  struct ast_iax2_full_hdr *fh = (struct ast_iax2_full_hdr *)thread->buf;
10234  struct ast_iax2_mini_hdr *mh = (struct ast_iax2_mini_hdr *)thread->buf;
10235  struct ast_iax2_meta_hdr *meta = (struct ast_iax2_meta_hdr *)thread->buf;
10236  struct ast_iax2_video_hdr *vh = (struct ast_iax2_video_hdr *)thread->buf;
10237  struct iax_frame *fr;
10238  struct iax_frame *cur;
10239  struct ast_frame f = { 0, };
10240  struct ast_channel *c = NULL;
10241  struct iax2_dpcache *dp;
10242  struct iax2_peer *peer;
10243  struct iax_ies ies;
10244  struct iax_ie_data ied0, ied1;
10245  iax2_format format;
10246  int fd;
10247  int exists;
10248  int minivid = 0;
10249  char empty[32]=""; /* Safety measure */
10250  struct iax_frame *duped_fr;
10251  char host_pref_buf[128];
10252  char caller_pref_buf[128];
10253  struct iax2_codec_pref pref;
10254  char *using_prefs = "mine";
10255 
10256  /* allocate an iax_frame with 4096 bytes of data buffer */
10257  fr = ast_alloca(sizeof(*fr) + 4096);
10258  memset(fr, 0, sizeof(*fr));
10259  fr->afdatalen = 4096; /* From ast_alloca() above */
10260 
10261  /* Copy frequently used parameters to the stack */
10262  res = thread->buf_len;
10263  fd = thread->iofd;
10264  ast_sockaddr_copy(&addr, &thread->ioaddr);
10265 
10266  if (res < sizeof(*mh)) {
10267  ast_log(LOG_WARNING, "midget packet received (%d of %d min)\n", res, (int) sizeof(*mh));
10268  return 1;
10269  }
10270  if ((vh->zeros == 0) && (ntohs(vh->callno) & 0x8000)) {
10271  if (res < sizeof(*vh)) {
10272  ast_log(LOG_WARNING, "Rejecting packet from '%s' that is flagged as a video frame but is too short\n",
10273  ast_sockaddr_stringify(&addr));
10274  return 1;
10275  }
10276 
10277  /* This is a video frame, get call number */
10278  fr->callno = find_callno(ntohs(vh->callno) & ~0x8000, dcallno, &addr, new, fd, 0);
10279  minivid = 1;
10280  } else if ((meta->zeros == 0) && !(ntohs(meta->metacmd) & 0x8000))
10281  return socket_process_meta(res, meta, &addr, fd, fr);
10282 
10283 #ifdef DEBUG_SUPPORT
10284  if (res >= sizeof(*fh))
10285  iax_outputframe(NULL, fh, 1, &addr, res - sizeof(*fh));
10286 #endif
10287  if (ntohs(mh->callno) & IAX_FLAG_FULL) {
10288  if (res < sizeof(*fh)) {
10289  ast_log(LOG_WARNING, "Rejecting packet from '%s' that is flagged as a full frame but is too short\n",
10290  ast_sockaddr_stringify(&addr));
10291  return 1;
10292  }
10293 
10294  /* Get the destination call number */
10295  dcallno = ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS;
10296 
10297 
10298  /* check to make sure this full frame isn't encrypted before we attempt
10299  * to look inside of it. If it is encrypted, decrypt it first. Its ok if the
10300  * callno is not found here, that just means one hasn't been allocated for
10301  * this connection yet. */
10302  if ((dcallno != 1) && (fr->callno = find_callno(ntohs(mh->callno) & ~IAX_FLAG_FULL, dcallno, &addr, NEW_PREVENT, fd, 1))) {
10303  ast_mutex_lock(&iaxsl[fr->callno]);
10304  if (iaxs[fr->callno] && ast_test_flag64(iaxs[fr->callno], IAX_ENCRYPTED)) {
10305  if (decrypt_frame(fr->callno, fh, &f, &res)) {
10306  ast_log(LOG_NOTICE, "Packet Decrypt Failed!\n");
10307  ast_mutex_unlock(&iaxsl[fr->callno]);
10308  return 1;
10309  }
10310  decrypted = 1;
10311  }
10312  ast_mutex_unlock(&iaxsl[fr->callno]);
10313  }
10314 
10315  /* Retrieve the type and subclass */
10316  f.frametype = fh->type;
10317  if (f.frametype == AST_FRAME_VIDEO) {
10318  f.subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub & ~0x40));
10319  if (!f.subclass.format) {
10320  return 1;
10321  }
10322  if ((fh->csub >> 6) & 0x1) {
10323  f.subclass.frame_ending = 1;
10324  }
10325  } else if (f.frametype == AST_FRAME_VOICE) {
10326  f.subclass.format = ast_format_compatibility_bitfield2format(uncompress_subclass(fh->csub));
10327  if (!f.subclass.format) {
10328  return 1;
10329  }
10330  } else {
10331  f.subclass.integer = uncompress_subclass(fh->csub);
10332  }
10333 
10334  /* Deal with POKE/PONG without allocating a callno */
10336  /* Reply back with a PONG, but don't care about the result. */
10337  send_apathetic_reply(1, ntohs(fh->scallno), &addr, IAX_COMMAND_PONG, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
10338  return 1;
10339  } else if (f.frametype == AST_FRAME_IAX && f.subclass.integer == IAX_COMMAND_ACK && dcallno == 1) {
10340  /* Ignore */
10341  return 1;
10342  }
10343 
10344  f.datalen = res - sizeof(*fh);
10345  if (f.datalen) {
10346  if (f.frametype == AST_FRAME_IAX) {
10347  if (iax_parse_ies(&ies, thread->buf + sizeof(struct ast_iax2_full_hdr), f.datalen)) {
10348  ast_log(LOG_WARNING, "Undecodable frame received from '%s'\n", ast_sockaddr_stringify(&addr));
10349  ast_variables_destroy(ies.vars);
10350  return 1;
10351  }
10352  f.data.ptr = NULL;
10353  f.datalen = 0;
10354  } else {
10355  f.data.ptr = thread->buf + sizeof(struct ast_iax2_full_hdr);
10356  memset(&ies, 0, sizeof(ies));
10357  }
10358  } else {
10359  if (f.frametype == AST_FRAME_IAX)
10360  f.data.ptr = NULL;
10361  else
10362  f.data.ptr = empty;
10363  memset(&ies, 0, sizeof(ies));
10364  }
10365 
10366  if (!dcallno && iax2_allow_new(f.frametype, f.subclass.integer, 1)) {
10367  /* only set NEW_ALLOW if calltoken checks out */
10368  if (handle_call_token(fh, &ies, &addr, fd)) {
10369  ast_variables_destroy(ies.vars);
10370  return 1;
10371  }
10372 
10373  if (ies.calltoken && ies.calltokendata) {
10374  /* if we've gotten this far, and the calltoken ie data exists,
10375  * then calltoken validation _MUST_ have taken place. If calltoken
10376  * data is provided, it is always validated reguardless of any
10377  * calltokenoptional or requirecalltoken options */
10378  new = NEW_ALLOW_CALLTOKEN_VALIDATED;
10379  } else {
10380  new = NEW_ALLOW;
10381  }
10382  }
10383  } else {
10384  /* Don't know anything about it yet */
10386  f.subclass.integer = 0;
10387  memset(&ies, 0, sizeof(ies));
10388  }
10389 
10390  if (!fr->callno) {
10391  int check_dcallno = 0;
10392 
10393  /*
10394  * We enforce accurate destination call numbers for ACKs. This forces the other
10395  * end to know the destination call number before call setup can complete.
10396  *
10397  * Discussed in the following thread:
10398  * http://lists.digium.com/pipermail/asterisk-dev/2008-May/033217.html
10399  */
10400 
10401  if ((ntohs(mh->callno) & IAX_FLAG_FULL) && ((f.frametype == AST_FRAME_IAX) && (f.subclass.integer == IAX_COMMAND_ACK))) {
10402  check_dcallno = 1;
10403  }
10404 
10405  if (!(fr->callno = find_callno(ntohs(mh->callno) & ~IAX_FLAG_FULL, dcallno, &addr, new, fd, check_dcallno))) {
10406  if (f.frametype == AST_FRAME_IAX && f.subclass.integer == IAX_COMMAND_NEW) {
10407  send_apathetic_reply(1, ntohs(fh->scallno), &addr, IAX_COMMAND_REJECT, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
10409  send_apathetic_reply(1, ntohs(fh->scallno), &addr, IAX_COMMAND_REGREJ, ntohl(fh->ts), fh->iseqno + 1, fd, NULL);
10410  }
10411  ast_variables_destroy(ies.vars);
10412  return 1;
10413  }
10414  }
10415 
10416  if (fr->callno > 0) {
10417  ast_callid mount_callid;
10418  ast_mutex_lock(&iaxsl[fr->callno]);
10419  if (iaxs[fr->callno] && ((mount_callid = iax_pvt_callid_get(fr->callno)))) {
10420  /* Bind to thread */
10421  ast_callid_threadassoc_add(mount_callid);
10422  }
10423  }
10424 
10425  if (!fr->callno || !iaxs[fr->callno]) {
10426  /* A call arrived for a nonexistent destination. Unless it's an "inval"
10427  frame, reply with an inval */
10428  if (ntohs(mh->callno) & IAX_FLAG_FULL) {
10429  /* We can only raw hangup control frames */
10430  if (((f.subclass.integer != IAX_COMMAND_INVAL) &&
10434  (f.frametype != AST_FRAME_IAX))
10435  raw_hangup(&addr, ntohs(fh->dcallno) & ~IAX_FLAG_RETRANS, ntohs(mh->callno) & ~IAX_FLAG_FULL,
10436  fd);
10437  }
10438  if (fr->callno > 0){
10439  ast_mutex_unlock(&iaxsl[fr->callno]);
10440  }
10441  ast_variables_destroy(ies.vars);
10442  return 1;
10443  }
10444  if (ast_test_flag64(iaxs[fr->callno], IAX_ENCRYPTED) && !decrypted) {
10445  if (decrypt_frame(fr->callno, fh, &f, &res)) {
10446  ast_log(LOG_WARNING, "Packet Decrypt Failed!\n");
10447  ast_variables_destroy(ies.vars);
10448  ast_mutex_unlock(&iaxsl[fr->callno]);
10449  return 1;
10450  }
10451  decrypted = 1;
10452  }
10453 
10454 #ifdef DEBUG_SUPPORT
10455  if (decrypted) {
10456  iax_outputframe(NULL, fh, 3, &addr, res - sizeof(*fh));
10457  }
10458 #endif
10459 
10460  if (iaxs[fr->callno]->owner && fh->type == AST_FRAME_IAX &&
10461  (fh->csub == IAX_COMMAND_HANGUP
10462  || fh->csub == IAX_COMMAND_REJECT
10463  || fh->csub == IAX_COMMAND_REGREJ
10464  || fh->csub == IAX_COMMAND_TXREJ)) {
10465  struct ast_control_pvt_cause_code *cause_code;
10466  int data_size = sizeof(*cause_code);
10467  char subclass[40] = "";
10468 
10469  /* get subclass text */
10470  iax_frame_subclass2str(fh->csub, subclass, sizeof(subclass));
10471 
10472  /* add length of "IAX2 " */
10473  data_size += 5;
10474  /* for IAX hangup frames, add length of () and number */
10475  data_size += 3;
10476  if (ies.causecode > 9) {
10477  data_size++;
10478  }
10479  if (ies.causecode > 99) {
10480  data_size++;
10481  }
10482  /* add length of subclass */
10483  data_size += strlen(subclass);
10484 
10485  cause_code = ast_alloca(data_size);
10486  memset(cause_code, 0, data_size);
10487  ast_copy_string(cause_code->chan_name, ast_channel_name(iaxs[fr->callno]->owner), AST_CHANNEL_NAME);
10488 
10489  cause_code->ast_cause = ies.causecode;
10490  snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "IAX2 %s(%d)", subclass, ies.causecode);
10491 
10492  iax2_lock_owner(fr->callno);
10493  if (iaxs[fr->callno] && iaxs[fr->callno]->owner) {
10494  ast_queue_control_data(iaxs[fr->callno]->owner, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
10495  ast_channel_hangupcause_hash_set(iaxs[fr->callno]->owner, cause_code, data_size);
10496  ast_channel_unlock(iaxs[fr->callno]->owner);
10497  }
10498  if (!iaxs[fr->callno]) {
10499  ast_variables_destroy(ies.vars);
10500  ast_mutex_unlock(&iaxsl[fr->callno]);
10501  return 1;
10502  }
10503  }
10504 
10505  /* count this frame */
10506  iaxs[fr->callno]->frames_received++;
10507 
10508  if (!ast_sockaddr_cmp(&addr, &iaxs[fr->callno]->addr) && !minivid &&
10509  f.subclass.integer != IAX_COMMAND_TXCNT && /* for attended transfer */
10510  f.subclass.integer != IAX_COMMAND_TXACC) { /* for attended transfer */
10511  unsigned short new_peercallno;
10512 
10513  new_peercallno = (unsigned short) (ntohs(mh->callno) & ~IAX_FLAG_FULL);
10514  if (new_peercallno && new_peercallno != iaxs[fr->callno]->peercallno) {
10515  if (iaxs[fr->callno]->peercallno) {
10516  remove_by_peercallno(iaxs[fr->callno]);
10517  }
10518  iaxs[fr->callno]->peercallno = new_peercallno;
10519  store_by_peercallno(iaxs[fr->callno]);
10520  }
10521  }
10522  if (ntohs(mh->callno) & IAX_FLAG_FULL) {
10523  if (iaxdebug)
10524  ast_debug(1, "Received packet %d, (%u, %d)\n", fh->oseqno, f.frametype, f.subclass.integer);
10525  /* Check if it's out of order (and not an ACK or INVAL) */
10526  fr->oseqno = fh->oseqno;
10527  fr->iseqno = fh->iseqno;
10528  fr->ts = ntohl(fh->ts);
10529 #ifdef IAXTESTS
10530  if (test_resync) {
10531  ast_debug(1, "Simulating frame ts resync, was %u now %u\n", fr->ts, fr->ts + test_resync);
10532  fr->ts += test_resync;
10533  }
10534 #endif /* IAXTESTS */
10535 #if 0
10536  if ( (ntohs(fh->dcallno) & IAX_FLAG_RETRANS) ||
10537  ( (f.frametype != AST_FRAME_VOICE) && ! (f.frametype == AST_FRAME_IAX &&
10538  (f.subclass == IAX_COMMAND_NEW ||
10539  f.subclass == IAX_COMMAND_AUTHREQ ||
10540  f.subclass == IAX_COMMAND_ACCEPT ||
10541  f.subclass == IAX_COMMAND_REJECT)) ) )
10542 #endif
10543  if ((ntohs(fh->dcallno) & IAX_FLAG_RETRANS) || (f.frametype != AST_FRAME_VOICE))
10544  updatehistory = 0;
10545  if ((iaxs[fr->callno]->iseqno != fr->oseqno) &&
10546  (iaxs[fr->callno]->iseqno ||
10548  (f.subclass.integer != IAX_COMMAND_TXREADY) && /* for attended transfer */
10549  (f.subclass.integer != IAX_COMMAND_TXREL) && /* for attended transfer */
10550  (f.subclass.integer != IAX_COMMAND_UNQUELCH ) && /* for attended transfer */
10552  (f.frametype != AST_FRAME_IAX))) {
10553  if (
10554  ((f.subclass.integer != IAX_COMMAND_ACK) &&
10555  (f.subclass.integer != IAX_COMMAND_INVAL) &&
10557  (f.subclass.integer != IAX_COMMAND_TXREADY) && /* for attended transfer */
10558  (f.subclass.integer != IAX_COMMAND_TXREL) && /* for attended transfer */
10559  (f.subclass.integer != IAX_COMMAND_UNQUELCH ) && /* for attended transfer */
10561  (f.subclass.integer != IAX_COMMAND_VNAK)) ||
10562  (f.frametype != AST_FRAME_IAX)) {
10563  /* If it's not an ACK packet, it's out of order. */
10564  ast_debug(1, "Packet arrived out of order (expecting %d, got %d) (frametype = %u, subclass = %d)\n",
10565  iaxs[fr->callno]->iseqno, fr->oseqno, f.frametype, f.subclass.integer);
10566  /* Check to see if we need to request retransmission,
10567  * and take sequence number wraparound into account */
10568  if ((unsigned char) (iaxs[fr->callno]->iseqno - fr->oseqno) < 128) {
10569  /* If we've already seen it, ack it XXX There's a border condition here XXX */
10570  if ((f.frametype != AST_FRAME_IAX) ||
10571  ((f.subclass.integer != IAX_COMMAND_ACK) && (f.subclass.integer != IAX_COMMAND_INVAL))) {
10572  ast_debug(1, "Acking anyway\n");
10573  /* XXX Maybe we should handle its ack to us, but then again, it's probably outdated anyway, and if
10574  we have anything to send, we'll retransmit and get an ACK back anyway XXX */
10575  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10576  }
10577  } else {
10578  /* Send a VNAK requesting retransmission */
10579  iax2_vnak(fr->callno);
10580  }
10581  ast_variables_destroy(ies.vars);
10582  ast_mutex_unlock(&iaxsl[fr->callno]);
10583  return 1;
10584  }
10585  } else {
10586  /* Increment unless it's an ACK or VNAK */
10587  if (((f.subclass.integer != IAX_COMMAND_ACK) &&
10588  (f.subclass.integer != IAX_COMMAND_INVAL) &&
10591  (f.subclass.integer != IAX_COMMAND_VNAK)) ||
10592  (f.frametype != AST_FRAME_IAX))
10593  iaxs[fr->callno]->iseqno++;
10594  }
10595  /* Ensure text frames are NULL-terminated */
10596  if (f.frametype == AST_FRAME_TEXT && thread->buf[res - 1] != '\0') {
10597  if (res < thread->buf_size)
10598  thread->buf[res++] = '\0';
10599  else /* Trims one character from the text message, but that's better than overwriting the end of the buffer. */
10600  thread->buf[res - 1] = '\0';
10601  }
10602 
10603  /* Handle implicit ACKing unless this is an INVAL, and only if this is
10604  from the real peer, not the transfer peer */
10605  if (!ast_sockaddr_cmp(&addr, &iaxs[fr->callno]->addr) &&
10606  ((f.subclass.integer != IAX_COMMAND_INVAL) ||
10607  (f.frametype != AST_FRAME_IAX))) {
10608  unsigned char x;
10609  int call_to_destroy;
10610  /* First we have to qualify that the ACKed value is within our window */
10611  if (iaxs[fr->callno]->rseqno >= iaxs[fr->callno]->oseqno || (fr->iseqno >= iaxs[fr->callno]->rseqno && fr->iseqno < iaxs[fr->callno]->oseqno))
10612  x = fr->iseqno;
10613  else
10614  x = iaxs[fr->callno]->oseqno;
10615  if ((x != iaxs[fr->callno]->oseqno) || (iaxs[fr->callno]->oseqno == fr->iseqno)) {
10616  /* The acknowledgement is within our window. Time to acknowledge everything
10617  that it says to */
10618  for (x=iaxs[fr->callno]->rseqno; x != fr->iseqno; x++) {
10619  /* Ack the packet with the given timestamp */
10620  if (iaxdebug)
10621  ast_debug(1, "Cancelling transmission of packet %d\n", x);
10622  call_to_destroy = 0;
10623  AST_LIST_TRAVERSE(&frame_queue[fr->callno], cur, list) {
10624  /* If it's our call, and our timestamp, mark -1 retries */
10625  if (x == cur->oseqno) {
10626  cur->retries = -1;
10627  /* Destroy call if this is the end */
10628  if (cur->final)
10629  call_to_destroy = fr->callno;
10630  }
10631  }
10632  if (call_to_destroy) {
10633  if (iaxdebug)
10634  ast_debug(1, "Really destroying %d, having been acked on final message\n", call_to_destroy);
10635  ast_mutex_lock(&iaxsl[call_to_destroy]);
10636  iax2_destroy(call_to_destroy);
10637  ast_mutex_unlock(&iaxsl[call_to_destroy]);
10638  }
10639  }
10640  /* Note how much we've received acknowledgement for */
10641  if (iaxs[fr->callno])
10642  iaxs[fr->callno]->rseqno = fr->iseqno;
10643  else {
10644  /* Stop processing now */
10645  ast_variables_destroy(ies.vars);
10646  ast_mutex_unlock(&iaxsl[fr->callno]);
10647  return 1;
10648  }
10649  } else {
10650  ast_debug(1, "Received iseqno %d not within window %d->%d\n", fr->iseqno, iaxs[fr->callno]->rseqno, iaxs[fr->callno]->oseqno);
10651  }
10652  }
10653  if (ast_sockaddr_cmp(&addr, &iaxs[fr->callno]->addr) &&
10654  ((f.frametype != AST_FRAME_IAX) ||
10656  (f.subclass.integer != IAX_COMMAND_TXCNT)))) {
10657  /* Only messages we accept from a transfer host are TXACC and TXCNT */
10658  ast_variables_destroy(ies.vars);
10659  ast_mutex_unlock(&iaxsl[fr->callno]);
10660  return 1;
10661  }
10662 
10663  /* when we receive the first full frame for a new incoming channel,
10664  it is safe to start the PBX on the channel because we have now
10665  completed a 3-way handshake with the peer */
10666  if ((f.frametype == AST_FRAME_VOICE) ||
10667  (f.frametype == AST_FRAME_VIDEO) ||
10668  (f.frametype == AST_FRAME_IAX)) {
10669  if (ast_test_flag64(iaxs[fr->callno], IAX_DELAYPBXSTART)) {
10670  ast_clear_flag64(iaxs[fr->callno], IAX_DELAYPBXSTART);
10672  iaxs[fr->callno]->chosenformat, &iaxs[fr->callno]->rprefs, NULL, NULL,
10673  ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_AUTHENTICATED))) {
10674  ast_variables_destroy(ies.vars);
10675  ast_mutex_unlock(&iaxsl[fr->callno]);
10676  return 1;
10677  }
10678  }
10679 
10680  if (ies.vars) {
10681  struct ast_datastore *variablestore = NULL;
10682  struct ast_variable *var, *prev = NULL;
10683  AST_LIST_HEAD(, ast_var_t) *varlist;
10684 
10685  iax2_lock_owner(fr->callno);
10686  if (!iaxs[fr->callno]) {
10687  ast_variables_destroy(ies.vars);
10688  ast_mutex_unlock(&iaxsl[fr->callno]);
10689  return 1;
10690  }
10691  if ((c = iaxs[fr->callno]->owner)) {
10692  varlist = ast_calloc(1, sizeof(*varlist));
10693  variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
10694 
10695  if (variablestore && varlist) {
10696  variablestore->data = varlist;
10697  variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
10698  AST_LIST_HEAD_INIT(varlist);
10699  ast_debug(1, "I can haz IAX vars?\n");
10700  for (var = ies.vars; var; var = var->next) {
10701  struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
10702  if (prev) {
10703  ast_free(prev);
10704  }
10705  prev = var;
10706  if (!newvar) {
10707  /* Don't abort list traversal, as this would leave ies.vars in an inconsistent state. */
10708  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
10709  } else {
10710  AST_LIST_INSERT_TAIL(varlist, newvar, entries);
10711  }
10712  }
10713  if (prev) {
10714  ast_free(prev);
10715  }
10716  ies.vars = NULL;
10717  ast_channel_datastore_add(c, variablestore);
10718  } else {
10719  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
10720  if (variablestore) {
10721  ast_datastore_free(variablestore);
10722  }
10723  if (varlist) {
10724  ast_free(varlist);
10725  }
10726  }
10727  ast_channel_unlock(c);
10728  } else {
10729  /* No channel yet, so transfer the variables directly over to the pvt,
10730  * for later inheritance. */
10731  ast_debug(1, "No channel, so populating IAXVARs to the pvt, as an intermediate step.\n");
10732  for (var = ies.vars; var && var->next; var = var->next);
10733  if (var) {
10734  var->next = iaxs[fr->callno]->iaxvars;
10735  iaxs[fr->callno]->iaxvars = ies.vars;
10736  ies.vars = NULL;
10737  }
10738  }
10739  }
10740 
10741  if (ies.vars) {
10742  ast_debug(1, "I have IAX variables, but they were not processed\n");
10743  }
10744  }
10745 
10746  /* once we receive our first IAX Full Frame that is not CallToken related, send all
10747  * queued signaling frames that were being held. */
10748  if ((f.frametype == AST_FRAME_IAX) && (f.subclass.integer != IAX_COMMAND_CALLTOKEN) && iaxs[fr->callno]->hold_signaling) {
10749  send_signaling(iaxs[fr->callno]);
10750  }
10751 
10752  if (f.frametype == AST_FRAME_VOICE) {
10755  ast_debug(1, "Ooh, voice format changed to '%s'\n", ast_format_get_name(f.subclass.format));
10756  if (iaxs[fr->callno]->owner) {
10757  iax2_lock_owner(fr->callno);
10758  if (iaxs[fr->callno]) {
10759  if (iaxs[fr->callno]->owner) {
10761  if (native) {
10762  ast_format_cap_append(native, f.subclass.format, 0);
10763  ast_channel_nativeformats_set(iaxs[fr->callno]->owner, native);
10764  if (ast_channel_readformat(iaxs[fr->callno]->owner)) {
10765  ast_set_read_format(iaxs[fr->callno]->owner, ast_channel_readformat(iaxs[fr->callno]->owner));
10766  }
10767  ao2_ref(native, -1);
10768  }
10769  ast_channel_unlock(iaxs[fr->callno]->owner);
10770  }
10771  } else {
10772  ast_debug(1, "Neat, somebody took away the channel at a magical time but i found it!\n");
10773  /* Free remote variables (if any) */
10774  if (ies.vars) {
10775  ast_variables_destroy(ies.vars);
10776  ast_debug(1, "I can haz iaxvars, but they is no good. :-(\n");
10777  ies.vars = NULL;
10778  }
10779  ast_mutex_unlock(&iaxsl[fr->callno]);
10780  return 1;
10781  }
10782  }
10783  }
10784  }
10785  if (f.frametype == AST_FRAME_VIDEO) {
10787  ast_debug(1, "Ooh, video format changed to %s\n", ast_format_get_name(f.subclass.format));
10789  }
10790  }
10791  if (f.frametype == AST_FRAME_IAX) {
10792  AST_SCHED_DEL(sched, iaxs[fr->callno]->initid);
10793  /* Handle the IAX pseudo frame itself */
10794  if (iaxdebug)
10795  ast_debug(1, "IAX subclass %d received\n", f.subclass.integer);
10796 
10797  /* Update last ts unless the frame's timestamp originated with us. */
10798  if (iaxs[fr->callno]->last < fr->ts &&
10799  f.subclass.integer != IAX_COMMAND_ACK &&
10800  f.subclass.integer != IAX_COMMAND_PONG &&
10801  f.subclass.integer != IAX_COMMAND_LAGRP) {
10802  iaxs[fr->callno]->last = fr->ts;
10803  if (iaxdebug)
10804  ast_debug(1, "For call=%d, set last=%u\n", fr->callno, fr->ts);
10805  }
10806  iaxs[fr->callno]->last_iax_message = f.subclass.integer;
10807  if (!iaxs[fr->callno]->first_iax_message) {
10808  iaxs[fr->callno]->first_iax_message = f.subclass.integer;
10809  }
10810  switch(f.subclass.integer) {
10811  case IAX_COMMAND_ACK:
10812  /* Do nothing */
10813  break;
10814  case IAX_COMMAND_QUELCH:
10815  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
10816  ast_set_flag64(iaxs[fr->callno], IAX_QUELCH);
10817  if (ies.musiconhold) {
10818  const char *moh_suggest;
10819 
10820  iax2_lock_owner(fr->callno);
10821  if (!iaxs[fr->callno] || !iaxs[fr->callno]->owner) {
10822  break;
10823  }
10824 
10825  /*
10826  * We already hold the owner lock so we do not
10827  * need to check iaxs[fr->callno] after it returns.
10828  */
10829  moh_suggest = iaxs[fr->callno]->mohsuggest;
10830  iax2_queue_hold(fr->callno, moh_suggest);
10831  ast_channel_unlock(iaxs[fr->callno]->owner);
10832  }
10833  }
10834  break;
10835  case IAX_COMMAND_UNQUELCH:
10836  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
10837  iax2_lock_owner(fr->callno);
10838  if (!iaxs[fr->callno]) {
10839  break;
10840  }
10841 
10842  ast_clear_flag64(iaxs[fr->callno], IAX_QUELCH);
10843  if (!iaxs[fr->callno]->owner) {
10844  break;
10845  }
10846 
10847  /*
10848  * We already hold the owner lock so we do not
10849  * need to check iaxs[fr->callno] after it returns.
10850  */
10852  ast_channel_unlock(iaxs[fr->callno]->owner);
10853  }
10854  break;
10855  case IAX_COMMAND_TXACC:
10856  if (iaxs[fr->callno]->transferring == TRANSFER_BEGIN) {
10857  /* Ack the packet with the given timestamp */
10858  AST_LIST_TRAVERSE(&frame_queue[fr->callno], cur, list) {
10859  /* Cancel any outstanding txcnt's */
10860  if (cur->transfer) {
10861  cur->retries = -1;
10862  }
10863  }
10864  memset(&ied1, 0, sizeof(ied1));
10865  iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->callno);
10866  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREADY, 0, ied1.buf, ied1.pos, -1);
10867  iaxs[fr->callno]->transferring = TRANSFER_READY;
10868  }
10869  break;
10870  case IAX_COMMAND_NEW:
10871  /* Ignore if it's already up */
10872  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD))
10873  break;
10874  if (ies.provverpres && ies.serviceident && !(ast_sockaddr_isnull(&addr))) {
10875  ast_mutex_unlock(&iaxsl[fr->callno]);
10876  check_provisioning(&addr, fd, ies.serviceident, ies.provver);
10877  ast_mutex_lock(&iaxsl[fr->callno]);
10878  if (!iaxs[fr->callno]) {
10879  break;
10880  }
10881  }
10882  /* If we're in trunk mode, do it now, and update the trunk number in our frame before continuing */
10883  if (ast_test_flag64(iaxs[fr->callno], IAX_TRUNK)) {
10884  int new_callno;
10885  if ((new_callno = make_trunk(fr->callno, 1)) != -1)
10886  fr->callno = new_callno;
10887  }
10888  /* For security, always ack immediately */
10889  if (delayreject)
10890  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
10891  if (check_access(fr->callno, &addr, &ies)) {
10892  /* They're not allowed on */
10893  auth_fail(fr->callno, IAX_COMMAND_REJECT);
10894  if (authdebug) {
10895  ast_log(LOG_NOTICE, "Rejected connect attempt from %s, who was trying to reach '%s@%s'\n",
10896  ast_sockaddr_stringify(&addr), iaxs[fr->callno]->exten, iaxs[fr->callno]->context);
10897  }
10898  break;
10899  }
10900  if (ast_strlen_zero(iaxs[fr->callno]->secret) && ast_test_flag64(iaxs[fr->callno], IAX_FORCE_ENCRYPT)) {
10901  auth_fail(fr->callno, IAX_COMMAND_REJECT);
10902  ast_log(LOG_WARNING, "Rejected connect attempt. No secret present while force encrypt enabled.\n");
10903  break;
10904  }
10905  if (strcasecmp(iaxs[fr->callno]->exten, "TBD")) {
10906  const char *context, *exten, *cid_num;
10907 
10908  context = ast_strdupa(iaxs[fr->callno]->context);
10909  exten = ast_strdupa(iaxs[fr->callno]->exten);
10910  cid_num = ast_strdupa(iaxs[fr->callno]->cid_num);
10911 
10912  /* This might re-enter the IAX code and need the lock */
10913  ast_mutex_unlock(&iaxsl[fr->callno]);
10914  exists = ast_exists_extension(NULL, context, exten, 1, cid_num);
10915  ast_mutex_lock(&iaxsl[fr->callno]);
10916 
10917  if (!iaxs[fr->callno]) {
10918  break;
10919  }
10920  } else
10921  exists = 0;
10922  /* Get OSP token if it does exist */
10923  save_osptoken(fr, &ies);
10924  if (ast_strlen_zero(iaxs[fr->callno]->secret) && ast_strlen_zero(iaxs[fr->callno]->inkeys)) {
10925  if (strcmp(iaxs[fr->callno]->exten, "TBD") && !exists) {
10926  memset(&ied0, 0, sizeof(ied0));
10927  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
10928  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
10929  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10930  if (!iaxs[fr->callno]) {
10931  break;
10932  }
10933  if (authdebug) {
10934  ast_log(LOG_NOTICE, "Rejected connect attempt from %s, request '%s@%s' does not exist\n",
10935  ast_sockaddr_stringify(&addr), iaxs[fr->callno]->exten, iaxs[fr->callno]->context);
10936  }
10937  } else {
10938  /* Select an appropriate format */
10939 
10940  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
10941  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
10942  using_prefs = "reqonly";
10943  } else {
10944  using_prefs = "disabled";
10945  }
10946  format = iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability;
10947  memset(&pref, 0, sizeof(pref));
10948  strcpy(caller_pref_buf, "disabled");
10949  strcpy(host_pref_buf, "disabled");
10950  } else {
10951  struct ast_format *tmpfmt;
10952  using_prefs = "mine";
10953  /* If the information elements are in here... use them */
10954  if (ies.codec_prefs)
10955  iax2_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
10956  if (iax2_codec_pref_index(&iaxs[fr->callno]->rprefs, 0, &tmpfmt)) {
10957  /* If we are codec_first_choice we let the caller have the 1st shot at picking the codec.*/
10958  if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
10959  pref = iaxs[fr->callno]->rprefs;
10960  using_prefs = "caller";
10961  } else {
10962  pref = iaxs[fr->callno]->prefs;
10963  }
10964  } else
10965  pref = iaxs[fr->callno]->prefs;
10966 
10967  format = iax2_codec_choose(&pref, iaxs[fr->callno]->capability & iaxs[fr->callno]->peercapability);
10968  iax2_codec_pref_string(&iaxs[fr->callno]->rprefs, caller_pref_buf, sizeof(caller_pref_buf) - 1);
10969  iax2_codec_pref_string(&iaxs[fr->callno]->prefs, host_pref_buf, sizeof(host_pref_buf) - 1);
10970  }
10971  if (!format) {
10972  if(!ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP))
10973  format = iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability;
10974  if (!format) {
10975  memset(&ied0, 0, sizeof(ied0));
10976  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
10977  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
10978  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
10979  if (!iaxs[fr->callno]) {
10980  break;
10981  }
10982  if (authdebug) {
10983  struct ast_str *peer_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
10984  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
10985  struct ast_str *peer_form_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
10986 
10987  if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
10988  ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested '%s' incompatible with our capability '%s'.\n",
10989  ast_sockaddr_stringify(&addr),
10990  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_form_buf),
10991  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
10992  } else {
10993  ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability '%s'/'%s' incompatible with our capability '%s'.\n",
10994  ast_sockaddr_stringify(&addr),
10995  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_form_buf),
10996  iax2_getformatname_multiple(iaxs[fr->callno]->peercapability, &peer_buf),
10997  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
10998  }
10999  }
11000  } else {
11001  /* Pick one... */
11002  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
11003  if(!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability))
11004  format = 0;
11005  } else {
11006  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
11007  using_prefs = ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
11008  memset(&pref, 0, sizeof(pref));
11009  format = iax2_format_compatibility_best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
11010  strcpy(caller_pref_buf,"disabled");
11011  strcpy(host_pref_buf,"disabled");
11012  } else {
11013  struct ast_format *tmpfmt;
11014  using_prefs = "mine";
11015  if (iax2_codec_pref_index(&iaxs[fr->callno]->rprefs, 0, &tmpfmt)) {
11016  /* Do the opposite of what we tried above. */
11017  if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
11018  pref = iaxs[fr->callno]->prefs;
11019  } else {
11020  pref = iaxs[fr->callno]->rprefs;
11021  using_prefs = "caller";
11022  }
11023  format = iax2_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
11024  } else /* if no codec_prefs IE do it the old way */
11025  format = iax2_format_compatibility_best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
11026  }
11027  }
11028 
11029  if (!format) {
11030  struct ast_str *peer_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11031  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11032  struct ast_str *peer_form_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11033 
11034  memset(&ied0, 0, sizeof(ied0));
11035  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
11036  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
11037  ast_log(LOG_ERROR, "No best format in '%s'???\n", iax2_getformatname_multiple(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability, &cap_buf));
11038  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11039  if (!iaxs[fr->callno]) {
11040  break;
11041  }
11042  if (authdebug) {
11043  ast_log(LOG_NOTICE, "Rejected connect attempt from %s, requested/capability '%s'/'%s' incompatible with our capability '%s'.\n",
11044  ast_sockaddr_stringify(&addr),
11045  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_form_buf),
11046  iax2_getformatname_multiple(iaxs[fr->callno]->peercapability, &peer_buf),
11047  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
11048  }
11049  ast_set_flag64(iaxs[fr->callno], IAX_ALREADYGONE);
11050  break;
11051  }
11052  }
11053  }
11054  if (format) {
11055  /* No authentication required, let them in */
11056  memset(&ied1, 0, sizeof(ied1));
11057  iax_ie_append_int(&ied1, IAX_IE_FORMAT, format);
11058  iax_ie_append_versioned_uint64(&ied1, IAX_IE_FORMAT2, 0, format);
11059  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACCEPT, 0, ied1.buf, ied1.pos, -1);
11060  if (strcmp(iaxs[fr->callno]->exten, "TBD")) {
11061  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
11062  ast_verb(3, "Accepting UNAUTHENTICATED call from %s:\n"
11063  "%srequested format = %s,\n"
11064  "%srequested prefs = %s,\n"
11065  "%sactual format = %s,\n"
11066  "%shost prefs = %s,\n"
11067  "%spriority = %s\n",
11068  ast_sockaddr_stringify(&addr),
11069  VERBOSE_PREFIX_4,
11070  iax2_getformatname(iaxs[fr->callno]->peerformat),
11071  VERBOSE_PREFIX_4,
11072  caller_pref_buf,
11073  VERBOSE_PREFIX_4,
11074  iax2_getformatname(format),
11075  VERBOSE_PREFIX_4,
11076  host_pref_buf,
11077  VERBOSE_PREFIX_4,
11078  using_prefs);
11079 
11080  iaxs[fr->callno]->chosenformat = format;
11081 
11082  /* Since this is a new call, we should go ahead and set the callid for it. */
11083  iax_pvt_callid_new(fr->callno);
11084  ast_set_flag64(iaxs[fr->callno], IAX_DELAYPBXSTART);
11085  } else {
11086  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
11087  /* If this is a TBD call, we're ready but now what... */
11088  ast_verb(3, "Accepted unauthenticated TBD call from %s\n", ast_sockaddr_stringify(&addr));
11089  }
11090  }
11091  }
11092  break;
11093  }
11094  if (iaxs[fr->callno]->authmethods & (IAX_AUTH_MD5 | IAX_AUTH_RSA))
11095  merge_encryption(iaxs[fr->callno], ies.encmethods);
11096  else
11097  iaxs[fr->callno]->encmethods = 0;
11098  if (!authenticate_request(fr->callno) && iaxs[fr->callno])
11099  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_AUTHENTICATED);
11100  break;
11101  case IAX_COMMAND_DPREQ:
11102  /* Request status in the dialplan */
11103  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD) &&
11104  !ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED) && ies.called_number) {
11105  if (iaxcompat) {
11106  /* Spawn a thread for the lookup */
11107  spawn_dp_lookup(fr->callno, iaxs[fr->callno]->context, ies.called_number, iaxs[fr->callno]->cid_num);
11108  } else {
11109  /* Just look it up */
11110  dp_lookup(fr->callno, iaxs[fr->callno]->context, ies.called_number, iaxs[fr->callno]->cid_num, 1);
11111  }
11112  }
11113  break;
11114  case IAX_COMMAND_HANGUP:
11115  ast_set_flag64(iaxs[fr->callno], IAX_ALREADYGONE);
11116  ast_debug(1, "Immediately destroying %d, having received hangup\n", fr->callno);
11117  /* Set hangup cause according to remote and hangupsource */
11118  if (iaxs[fr->callno]->owner) {
11119  set_hangup_source_and_cause(fr->callno, ies.causecode);
11120  if (!iaxs[fr->callno]) {
11121  break;
11122  }
11123  }
11124 
11125  /* Send ack immediately, before we destroy */
11126  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11127  iax2_destroy(fr->callno);
11128  break;
11129  case IAX_COMMAND_REJECT:
11130  /* Set hangup cause according to remote and hangup source */
11131  if (iaxs[fr->callno]->owner) {
11132  set_hangup_source_and_cause(fr->callno, ies.causecode);
11133  if (!iaxs[fr->callno]) {
11134  break;
11135  }
11136  }
11137 
11138  if (!ast_test_flag64(iaxs[fr->callno], IAX_PROVISION)) {
11139  if (iaxs[fr->callno]->owner && authdebug)
11140  ast_log(LOG_WARNING, "Call rejected by %s: %s\n",
11141  ast_sockaddr_stringify(&addr),
11142  ies.cause ? ies.cause : "<Unknown>");
11143  ast_debug(1, "Immediately destroying %d, having received reject\n",
11144  fr->callno);
11145  }
11146  /* Send ack immediately, before we destroy */
11147  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK,
11148  fr->ts, NULL, 0, fr->iseqno);
11149  if (!ast_test_flag64(iaxs[fr->callno], IAX_PROVISION))
11150  iaxs[fr->callno]->error = EPERM;
11151  iax2_destroy(fr->callno);
11152  break;
11153  case IAX_COMMAND_TRANSFER:
11154  {
11155  iax2_lock_owner(fr->callno);
11156  if (!iaxs[fr->callno]) {
11157  /* Initiating call went away before we could transfer. */
11158  break;
11159  }
11160  if (iaxs[fr->callno]->owner) {
11161  struct ast_channel *owner = iaxs[fr->callno]->owner;
11162  char *context = ast_strdupa(iaxs[fr->callno]->context);
11163 
11164  ast_channel_ref(owner);
11165  ast_channel_unlock(owner);
11166  ast_mutex_unlock(&iaxsl[fr->callno]);
11167 
11168  if (ast_bridge_transfer_blind(1, owner, ies.called_number,
11169  context, NULL, NULL) != AST_BRIDGE_TRANSFER_SUCCESS) {
11170  ast_log(LOG_WARNING, "Blind transfer of '%s' to '%s@%s' failed\n",
11171  ast_channel_name(owner), ies.called_number,
11172  context);
11173  }
11174 
11175  ast_channel_unref(owner);
11176  ast_mutex_lock(&iaxsl[fr->callno]);
11177  }
11178 
11179  break;
11180  }
11181  case IAX_COMMAND_ACCEPT:
11182  /* Ignore if call is already up or needs authentication or is a TBD */
11183  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD | IAX_STATE_AUTHENTICATED))
11184  break;
11185  if (ast_test_flag64(iaxs[fr->callno], IAX_PROVISION)) {
11186  /* Send ack immediately, before we destroy */
11187  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11188  iax2_destroy(fr->callno);
11189  break;
11190  }
11191  if (ies.format) {
11192  iaxs[fr->callno]->peerformat = ies.format;
11193  } else {
11194  if (iaxs[fr->callno]->owner)
11195  iaxs[fr->callno]->peerformat = iax2_format_compatibility_cap2bitfield(ast_channel_nativeformats(iaxs[fr->callno]->owner));
11196  else
11197  iaxs[fr->callno]->peerformat = iaxs[fr->callno]->capability;
11198  }
11199  ast_verb(3, "Call accepted by %s (format %s)\n", ast_sockaddr_stringify(&addr),
11200  iax2_getformatname(iaxs[fr->callno]->peerformat));
11201  if (!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability)) {
11202  memset(&ied0, 0, sizeof(ied0));
11203  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
11204  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
11205  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11206  if (!iaxs[fr->callno]) {
11207  break;
11208  }
11209  if (authdebug) {
11210  struct ast_str *peer_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11211  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11212 
11213  ast_log(LOG_NOTICE, "Rejected call to %s, format %s incompatible with our capability %s.\n",
11214  ast_sockaddr_stringify(&addr),
11215  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_buf),
11216  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
11217  }
11218  } else {
11220 
11221  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
11222  iax2_lock_owner(fr->callno);
11223  if (iaxs[fr->callno] && iaxs[fr->callno]->owner && native) {
11224  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11225 
11226  /* Switch us to use a compatible format */
11228  iaxs[fr->callno]->peerformat, &iaxs[fr->callno]->rprefs,
11229  native);
11230  ast_channel_nativeformats_set(iaxs[fr->callno]->owner, native);
11231  ast_verb(3, "Format for call is %s\n", ast_format_cap_get_names(ast_channel_nativeformats(iaxs[fr->callno]->owner), &cap_buf));
11232 
11233  /* Setup read/write formats properly. */
11234  if (ast_channel_writeformat(iaxs[fr->callno]->owner))
11235  ast_set_write_format(iaxs[fr->callno]->owner, ast_channel_writeformat(iaxs[fr->callno]->owner));
11236  if (ast_channel_readformat(iaxs[fr->callno]->owner))
11237  ast_set_read_format(iaxs[fr->callno]->owner, ast_channel_readformat(iaxs[fr->callno]->owner));
11238  ast_channel_unlock(iaxs[fr->callno]->owner);
11239  }
11240 
11241  ao2_cleanup(native);
11242  }
11243  if (iaxs[fr->callno]) {
11245  AST_LIST_TRAVERSE(&iaxs[fr->callno]->dpentries, dp, peer_list)
11246  if (!(dp->flags & CACHE_FLAG_TRANSMITTED))
11247  iax2_dprequest(dp, fr->callno);
11249  }
11250  break;
11251  case IAX_COMMAND_POKE:
11252  /* Send back a pong packet with the original timestamp */
11253  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_PONG, fr->ts, NULL, 0, -1);
11254  break;
11255  case IAX_COMMAND_PING:
11256  {
11257  struct iax_ie_data pingied;
11258  construct_rr(iaxs[fr->callno], &pingied);
11259  /* Send back a pong packet with the original timestamp */
11260  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_PONG, fr->ts, pingied.buf, pingied.pos, -1);
11261  }
11262  break;
11263  case IAX_COMMAND_PONG:
11264  /* Calculate ping time */
11265  iaxs[fr->callno]->pingtime = calc_timestamp(iaxs[fr->callno], 0, &f) - fr->ts;
11266  /* save RR info */
11267  save_rr(fr, &ies);
11268 
11269  /* Good time to write jb stats for this call */
11270  log_jitterstats(fr->callno);
11271 
11272  if (iaxs[fr->callno]->peerpoke) {
11273  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
11274  peer = iaxs[fr->callno]->peerpoke;
11275  if ((peer->lastms < 0) || (peer->historicms > peer->maxms)) {
11276  if (iaxs[fr->callno]->pingtime <= peer->maxms) {
11277  ast_log(LOG_NOTICE, "Peer '%s' is now REACHABLE! Time: %u\n", peer->name, iaxs[fr->callno]->pingtime);
11279  blob = ast_json_pack("{s: s, s: I}",
11280  "peer_status", "Reachable",
11281  "time", (ast_json_int_t)iaxs[fr->callno]->pingtime);
11282  ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "IAX2/%s", peer->name); /* Activate notification */
11283  }
11284  } else if ((peer->historicms > 0) && (peer->historicms <= peer->maxms)) {
11285  if (iaxs[fr->callno]->pingtime > peer->maxms) {
11286  ast_log(LOG_NOTICE, "Peer '%s' is now TOO LAGGED (%u ms)!\n", peer->name, iaxs[fr->callno]->pingtime);
11288  blob = ast_json_pack("{s: s, s: I}",
11289  "peer_status", "Lagged",
11290  "time", (ast_json_int_t)iaxs[fr->callno]->pingtime);
11291  ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "IAX2/%s", peer->name); /* Activate notification */
11292  }
11293  }
11295  peer->lastms = iaxs[fr->callno]->pingtime;
11296  if (peer->smoothing && (peer->lastms > -1))
11297  peer->historicms = (iaxs[fr->callno]->pingtime + peer->historicms) / 2;
11298  else if (peer->smoothing && peer->lastms < 0)
11299  peer->historicms = (0 + peer->historicms) / 2;
11300  else
11301  peer->historicms = iaxs[fr->callno]->pingtime;
11302 
11303  /* Remove scheduled iax2_poke_noanswer */
11304  if (peer->pokeexpire > -1) {
11305  if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
11306  peer_unref(peer);
11307  peer->pokeexpire = -1;
11308  }
11309  }
11310  /* Schedule the next cycle */
11311  if ((peer->lastms < 0) || (peer->historicms > peer->maxms))
11312  peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_peer_s, peer_ref(peer));
11313  else
11314  peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqok, iax2_poke_peer_s, peer_ref(peer));
11315  if (peer->pokeexpire == -1)
11316  peer_unref(peer);
11317  /* and finally send the ack */
11318  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11319  /* And wrap up the qualify call */
11320  iax2_destroy(fr->callno);
11321  peer->callno = 0;
11322  ast_debug(1, "Peer %s: got pong, lastms %d, historicms %d, maxms %d\n", peer->name, peer->lastms, peer->historicms, peer->maxms);
11323  }
11324  break;
11325  case IAX_COMMAND_LAGRQ:
11326  case IAX_COMMAND_LAGRP:
11327  f.src = "LAGRQ";
11328  f.mallocd = 0;
11329  f.offset = 0;
11330  f.samples = 0;
11331  iax_frame_wrap(fr, &f);
11332  if (f.subclass.integer == IAX_COMMAND_LAGRQ) {
11333  /* Received a LAGRQ - echo back a LAGRP */
11334  fr->af.subclass.integer = IAX_COMMAND_LAGRP;
11335  iax2_send(iaxs[fr->callno], &fr->af, fr->ts, -1, 0, 0, 0);
11336  } else {
11337  /* Received LAGRP in response to our LAGRQ */
11338  unsigned int ts;
11339  /* This is a reply we've been given, actually measure the difference */
11340  ts = calc_timestamp(iaxs[fr->callno], 0, &fr->af);
11341  iaxs[fr->callno]->lag = ts - fr->ts;
11342  if (iaxdebug)
11343  ast_debug(1, "Peer %s lag measured as %dms\n",
11344  ast_sockaddr_stringify(&addr), iaxs[fr->callno]->lag);
11345  }
11346  break;
11347  case IAX_COMMAND_AUTHREQ:
11348  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD)) {
11349  ast_log(LOG_WARNING, "Call on %s is already up, can't start on it\n", iaxs[fr->callno]->owner ? ast_channel_name(iaxs[fr->callno]->owner) : "<Unknown>");
11350  break;
11351  }
11352  if (authenticate_reply(iaxs[fr->callno], &iaxs[fr->callno]->addr, &ies, iaxs[fr->callno]->secret, iaxs[fr->callno]->outkey)) {
11353  struct ast_frame hangup_fr = { .frametype = AST_FRAME_CONTROL,
11354  .subclass.integer = AST_CONTROL_HANGUP,
11355  };
11356  ast_log(LOG_WARNING,
11357  "I don't know how to authenticate %s to %s\n",
11358  ies.username ? ies.username : "<unknown>", ast_sockaddr_stringify(&addr));
11359  iax2_queue_frame(fr->callno, &hangup_fr);
11360  }
11361  break;
11362  case IAX_COMMAND_AUTHREP:
11363  /* For security, always ack immediately */
11364  if (delayreject)
11365  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11366  /* Ignore once we've started */
11367  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED | IAX_STATE_TBD)) {
11368  ast_log(LOG_WARNING, "Call on %s is already up, can't start on it\n", iaxs[fr->callno]->owner ? ast_channel_name(iaxs[fr->callno]->owner) : "<Unknown>");
11369  break;
11370  }
11371  if (authenticate_verify(iaxs[fr->callno], &ies)) {
11372  if (authdebug)
11373  ast_log(LOG_WARNING, "Host %s failed to authenticate as %s\n", ast_sockaddr_stringify(&addr),
11374  iaxs[fr->callno]->username);
11375  memset(&ied0, 0, sizeof(ied0));
11376  auth_fail(fr->callno, IAX_COMMAND_REJECT);
11377  break;
11378  }
11379  if (strcasecmp(iaxs[fr->callno]->exten, "TBD")) {
11380  /* This might re-enter the IAX code and need the lock */
11381  exists = ast_exists_extension(NULL, iaxs[fr->callno]->context, iaxs[fr->callno]->exten, 1, iaxs[fr->callno]->cid_num);
11382  } else
11383  exists = 0;
11384  if (strcmp(iaxs[fr->callno]->exten, "TBD") && !exists) {
11385  if (authdebug)
11386  ast_log(LOG_WARNING, "Rejected connect attempt from %s, request '%s@%s' does not exist\n",
11387  ast_sockaddr_stringify(&addr),
11388  iaxs[fr->callno]->exten,
11389  iaxs[fr->callno]->context);
11390  memset(&ied0, 0, sizeof(ied0));
11391  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
11392  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
11393  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11394  if (!iaxs[fr->callno]) {
11395  break;
11396  }
11397  } else {
11398  /* Select an appropriate format */
11399  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
11400  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
11401  using_prefs = "reqonly";
11402  } else {
11403  using_prefs = "disabled";
11404  }
11405  format = iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability;
11406  memset(&pref, 0, sizeof(pref));
11407  strcpy(caller_pref_buf, "disabled");
11408  strcpy(host_pref_buf, "disabled");
11409  } else {
11410  struct ast_format *tmpfmt;
11411  using_prefs = "mine";
11412  if (ies.codec_prefs)
11413  iax2_codec_pref_convert(&iaxs[fr->callno]->rprefs, ies.codec_prefs, 32, 0);
11414  if (iax2_codec_pref_index(&iaxs[fr->callno]->rprefs, 0, &tmpfmt)) {
11415  if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
11416  pref = iaxs[fr->callno]->rprefs;
11417  using_prefs = "caller";
11418  } else {
11419  pref = iaxs[fr->callno]->prefs;
11420  }
11421  } else /* if no codec_prefs IE do it the old way */
11422  pref = iaxs[fr->callno]->prefs;
11423  format = iax2_codec_choose(&pref, iaxs[fr->callno]->capability & iaxs[fr->callno]->peercapability);
11424  iax2_codec_pref_string(&iaxs[fr->callno]->rprefs, caller_pref_buf, sizeof(caller_pref_buf) - 1);
11425  iax2_codec_pref_string(&iaxs[fr->callno]->prefs, host_pref_buf, sizeof(host_pref_buf) - 1);
11426  }
11427  if (!format) {
11428  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11429  struct ast_str *peer_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11430  struct ast_str *peer_form_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11431 
11432  if(!ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
11433  ast_debug(1, "We don't do requested format %s, falling back to peer capability '%s'\n",
11434  iax2_getformatname(iaxs[fr->callno]->peerformat),
11435  iax2_getformatname_multiple(iaxs[fr->callno]->peercapability, &peer_buf));
11436  format = iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability;
11437  }
11438  if (!format) {
11439  if (authdebug) {
11440  if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
11441  ast_log(LOG_WARNING, "Rejected connect attempt from %s, requested '%s' incompatible with our capability '%s'.\n",
11442  ast_sockaddr_stringify(&addr),
11443  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_form_buf),
11444  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
11445  } else {
11446  ast_log(LOG_WARNING, "Rejected connect attempt from %s, requested/capability '%s'/'%s' incompatible with our capability '%s'.\n",
11447  ast_sockaddr_stringify(&addr),
11448  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_form_buf),
11449  iax2_getformatname_multiple(iaxs[fr->callno]->peercapability, &peer_buf),
11450  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
11451  }
11452  }
11453  memset(&ied0, 0, sizeof(ied0));
11454  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
11455  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
11456  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11457  if (!iaxs[fr->callno]) {
11458  break;
11459  }
11460  } else {
11461  /* Pick one... */
11462  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
11463  if(!(iaxs[fr->callno]->peerformat & iaxs[fr->callno]->capability))
11464  format = 0;
11465  } else {
11466  if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
11467  using_prefs = ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
11468  memset(&pref, 0, sizeof(pref));
11469  format = ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)
11470  ? iaxs[fr->callno]->peerformat
11471  : iax2_format_compatibility_best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
11472  strcpy(caller_pref_buf,"disabled");
11473  strcpy(host_pref_buf,"disabled");
11474  } else {
11475  struct ast_format *tmpfmt;
11476  using_prefs = "mine";
11477  if (iax2_codec_pref_index(&iaxs[fr->callno]->rprefs, 0, &tmpfmt)) {
11478  /* Do the opposite of what we tried above. */
11479  if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_USER_FIRST)) {
11480  pref = iaxs[fr->callno]->prefs;
11481  } else {
11482  pref = iaxs[fr->callno]->rprefs;
11483  using_prefs = "caller";
11484  }
11485  format = iax2_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
11486  } else /* if no codec_prefs IE do it the old way */
11487  format = iax2_format_compatibility_best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
11488  }
11489  }
11490  if (!format) {
11491  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11492  struct ast_str *peer_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11493  struct ast_str *peer_form_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11494 
11495  ast_log(LOG_ERROR, "No best format in %s???\n",
11496  iax2_getformatname_multiple(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability, &cap_buf));
11497  if (authdebug) {
11498  if (ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)) {
11499  ast_log(LOG_WARNING, "Rejected connect attempt from %s, requested '%s' incompatible with our capability '%s'.\n",
11500  ast_sockaddr_stringify(&addr),
11501  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_form_buf),
11502  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
11503  } else {
11504  ast_log(LOG_WARNING, "Rejected connect attempt from %s, requested/capability '%s'/'%s' incompatible with our capability '%s'.\n",
11505  ast_sockaddr_stringify(&addr),
11506  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &peer_form_buf),
11507  iax2_getformatname_multiple(iaxs[fr->callno]->peercapability, &peer_buf),
11508  iax2_getformatname_multiple(iaxs[fr->callno]->capability, &cap_buf));
11509  }
11510  }
11511  memset(&ied0, 0, sizeof(ied0));
11512  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "Unable to negotiate codec");
11513  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
11514  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11515  if (!iaxs[fr->callno]) {
11516  break;
11517  }
11518  }
11519  }
11520  }
11521  if (format) {
11522  /* Authentication received */
11523  memset(&ied1, 0, sizeof(ied1));
11524  iax_ie_append_int(&ied1, IAX_IE_FORMAT, format);
11525  iax_ie_append_versioned_uint64(&ied1, IAX_IE_FORMAT2, 0, format);
11526  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACCEPT, 0, ied1.buf, ied1.pos, -1);
11527  if (strcmp(iaxs[fr->callno]->exten, "TBD")) {
11528  char authmethodnames[AUTH_METHOD_NAMES_BUFSIZE];
11529  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
11530  ast_verb(3, "Accepting AUTHENTICATED call from %s:\n"
11531  "%srequested auth methods = (%s),\n"
11532  "%sactual auth method = %s,\n"
11533  "%sencrypted = %s,\n"
11534  "%srequested format = %s,\n"
11535  "%srequested prefs = %s,\n"
11536  "%sactual format = %s,\n"
11537  "%shost prefs = %s,\n"
11538  "%spriority = %s\n",
11539  ast_sockaddr_stringify(&addr),
11540  VERBOSE_PREFIX_4,
11541  auth_method_names(iaxs[fr->callno]->authmethods, authmethodnames),
11542  VERBOSE_PREFIX_4,
11543  auth_method_labels[iaxs[fr->callno]->eff_auth_method],
11544  VERBOSE_PREFIX_4,
11545  IAX_CALLENCRYPTED(iaxs[fr->callno]) ? "yes" : "no",
11546  VERBOSE_PREFIX_4,
11547  iax2_getformatname(iaxs[fr->callno]->peerformat),
11548  VERBOSE_PREFIX_4,
11549  caller_pref_buf,
11550  VERBOSE_PREFIX_4,
11551  iax2_getformatname(format),
11552  VERBOSE_PREFIX_4,
11553  host_pref_buf,
11554  VERBOSE_PREFIX_4,
11555  using_prefs);
11556 
11557  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
11558  c = ast_iax2_new(fr->callno, AST_STATE_RING, format,
11559  &iaxs[fr->callno]->rprefs, NULL, NULL, 1);
11560  if (!c) {
11561  iax2_destroy(fr->callno);
11562  } else if (ies.vars) {
11563  struct ast_datastore *variablestore;
11564  struct ast_variable *var, *prev = NULL;
11565  AST_LIST_HEAD(, ast_var_t) *varlist;
11566  varlist = ast_calloc(1, sizeof(*varlist));
11567  variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
11568  if (variablestore && varlist) {
11569  variablestore->data = varlist;
11570  variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
11571  AST_LIST_HEAD_INIT(varlist);
11572  ast_debug(1, "I can haz IAX vars? w00t\n");
11573  for (var = ies.vars; var; var = var->next) {
11574  struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
11575  if (prev)
11576  ast_free(prev);
11577  prev = var;
11578  if (!newvar) {
11579  /* Don't abort list traversal, as this would leave ies.vars in an inconsistent state. */
11580  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
11581  } else {
11582  AST_LIST_INSERT_TAIL(varlist, newvar, entries);
11583  }
11584  }
11585  if (prev)
11586  ast_free(prev);
11587  ies.vars = NULL;
11588  ast_channel_datastore_add(c, variablestore);
11589  } else {
11590  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
11591  if (variablestore)
11592  ast_datastore_free(variablestore);
11593  if (varlist)
11594  ast_free(varlist);
11595  }
11596  }
11597  } else {
11598  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
11599  /* If this is a TBD call, we're ready but now what... */
11600  ast_verb(3, "Accepted AUTHENTICATED TBD call from %s\n", ast_sockaddr_stringify(&addr));
11601  if (ast_test_flag64(iaxs[fr->callno], IAX_IMMEDIATE)) {
11602  goto immediatedial;
11603  }
11604  }
11605  }
11606  }
11607  break;
11608  case IAX_COMMAND_DIAL:
11609 immediatedial:
11610  if (ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD)) {
11611  ast_clear_flag(&iaxs[fr->callno]->state, IAX_STATE_TBD);
11612  ast_string_field_set(iaxs[fr->callno], exten, ies.called_number ? ies.called_number : "s");
11613  if (!ast_exists_extension(NULL, iaxs[fr->callno]->context, iaxs[fr->callno]->exten, 1, iaxs[fr->callno]->cid_num)) {
11614  if (authdebug)
11615  ast_log(LOG_WARNING, "Rejected dial attempt from %s, request '%s@%s' does not exist\n",
11616  ast_sockaddr_stringify(&addr),
11617  iaxs[fr->callno]->exten,
11618  iaxs[fr->callno]->context);
11619  memset(&ied0, 0, sizeof(ied0));
11620  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No such context/extension");
11621  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_NO_ROUTE_DESTINATION);
11622  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11623  if (!iaxs[fr->callno]) {
11624  break;
11625  }
11626  } else {
11627  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
11628  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
11629  ast_verb(3, "Accepting DIAL from %s, formats = %s\n",
11630  ast_sockaddr_stringify(&addr),
11631  iax2_getformatname_multiple(iaxs[fr->callno]->peerformat, &cap_buf));
11632  ast_set_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED);
11633  send_command(iaxs[fr->callno], AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, 0, NULL, 0, -1);
11635  iaxs[fr->callno]->peerformat, &iaxs[fr->callno]->rprefs,
11636  NULL, NULL, 1);
11637  if (!c) {
11638  iax2_destroy(fr->callno);
11639  } else if (ies.vars) {
11640  struct ast_datastore *variablestore;
11641  struct ast_variable *var, *prev = NULL;
11642  AST_LIST_HEAD(, ast_var_t) *varlist;
11643  varlist = ast_calloc(1, sizeof(*varlist));
11644  variablestore = ast_datastore_alloc(&iax2_variable_datastore_info, NULL);
11645  ast_debug(1, "I can haz IAX vars? w00t\n");
11646  if (variablestore && varlist) {
11647  variablestore->data = varlist;
11648  variablestore->inheritance = DATASTORE_INHERIT_FOREVER;
11649  AST_LIST_HEAD_INIT(varlist);
11650  for (var = ies.vars; var; var = var->next) {
11651  struct ast_var_t *newvar = ast_var_assign(var->name, var->value);
11652  if (prev)
11653  ast_free(prev);
11654  prev = var;
11655  if (!newvar) {
11656  /* Don't abort list traversal, as this would leave ies.vars in an inconsistent state. */
11657  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
11658  } else {
11659  AST_LIST_INSERT_TAIL(varlist, newvar, entries);
11660  }
11661  }
11662  if (prev)
11663  ast_free(prev);
11664  ies.vars = NULL;
11665  ast_channel_datastore_add(c, variablestore);
11666  } else {
11667  ast_log(LOG_ERROR, "Memory allocation error while processing IAX2 variables\n");
11668  if (variablestore)
11669  ast_datastore_free(variablestore);
11670  if (varlist)
11671  ast_free(varlist);
11672  }
11673  }
11674  }
11675  }
11676  break;
11677  case IAX_COMMAND_INVAL:
11678  iaxs[fr->callno]->error = ENOTCONN;
11679  ast_debug(1, "Immediately destroying %d, having received INVAL\n", fr->callno);
11680  iax2_destroy(fr->callno);
11681  ast_debug(1, "Destroying call %d\n", fr->callno);
11682  break;
11683  case IAX_COMMAND_VNAK:
11684  ast_debug(1, "Received VNAK: resending outstanding frames\n");
11685  /* Force retransmission */
11686  vnak_retransmit(fr->callno, fr->iseqno);
11687  break;
11688  case IAX_COMMAND_REGREQ:
11689  case IAX_COMMAND_REGREL:
11690  /* For security, always ack immediately */
11691  if (delayreject)
11692  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11693  if (register_verify(fr->callno, &addr, &ies)) {
11694  if (!iaxs[fr->callno]) {
11695  break;
11696  }
11697  /* Send delayed failure */
11698  auth_fail(fr->callno, IAX_COMMAND_REGREJ);
11699  break;
11700  }
11701  if (!iaxs[fr->callno]) {
11702  break;
11703  }
11704  if ((ast_strlen_zero(iaxs[fr->callno]->secret) && ast_strlen_zero(iaxs[fr->callno]->inkeys)) ||
11705  ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_AUTHENTICATED)) {
11706 
11707  if (f.subclass.integer == IAX_COMMAND_REGREL) {
11708  ast_sockaddr_setnull(&addr);
11709  }
11710  if (update_registry(&addr, fr->callno, ies.devicetype, fd, ies.refresh)) {
11711  ast_log(LOG_WARNING, "Registry error\n");
11712  }
11713  if (!iaxs[fr->callno]) {
11714  break;
11715  }
11716  if (ies.provverpres && ies.serviceident && !(ast_sockaddr_isnull(&addr))) {
11717  ast_mutex_unlock(&iaxsl[fr->callno]);
11718  check_provisioning(&addr, fd, ies.serviceident, ies.provver);
11719  ast_mutex_lock(&iaxsl[fr->callno]);
11720  }
11721  break;
11722  }
11723  registry_authrequest(fr->callno);
11724  break;
11725  case IAX_COMMAND_REGACK:
11726  if (iax2_ack_registry(&ies, &addr, fr->callno)) {
11727  ast_log(LOG_WARNING, "Registration failure\n");
11728  }
11729  /* Send ack immediately, before we destroy */
11730  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11731  iax2_destroy(fr->callno);
11732  break;
11733  case IAX_COMMAND_REGREJ:
11734  if (iaxs[fr->callno]->reg) {
11735  if (authdebug) {
11736  ast_log(LOG_NOTICE, "Registration of '%s' rejected: '%s' from: '%s'\n",
11737  iaxs[fr->callno]->reg->username, ies.cause ? ies.cause : "<unknown>",
11738  ast_sockaddr_stringify(&addr));
11739  }
11740  iax2_publish_registry(iaxs[fr->callno]->reg->username, ast_sockaddr_stringify(&addr), "Rejected", S_OR(ies.cause, "<unknown>"));
11741  iaxs[fr->callno]->reg->regstate = REG_STATE_REJECTED;
11742  }
11743  /* Send ack immediately, before we destroy */
11744  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11745  iax2_destroy(fr->callno);
11746  break;
11747  case IAX_COMMAND_REGAUTH:
11748  /* Authentication request */
11749  if (registry_rerequest(&ies, fr->callno, &addr)) {
11750  memset(&ied0, 0, sizeof(ied0));
11751  iax_ie_append_str(&ied0, IAX_IE_CAUSE, "No authority found");
11752  iax_ie_append_byte(&ied0, IAX_IE_CAUSECODE, AST_CAUSE_FACILITY_NOT_SUBSCRIBED);
11753  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11754  }
11755  break;
11756  case IAX_COMMAND_TXREJ:
11757  while (iaxs[fr->callno]
11758  && iaxs[fr->callno]->bridgecallno
11759  && ast_mutex_trylock(&iaxsl[iaxs[fr->callno]->bridgecallno])) {
11760  DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]);
11761  }
11762  if (!iaxs[fr->callno]) {
11763  break;
11764  }
11765 
11766  iaxs[fr->callno]->transferring = TRANSFER_NONE;
11767  ast_verb(3, "Channel '%s' unable to transfer\n", iaxs[fr->callno]->owner ? ast_channel_name(iaxs[fr->callno]->owner) : "<Unknown>");
11768  memset(&iaxs[fr->callno]->transfer, 0, sizeof(iaxs[fr->callno]->transfer));
11769 
11770  if (!iaxs[fr->callno]->bridgecallno) {
11771  break;
11772  }
11773 
11774  if (iaxs[iaxs[fr->callno]->bridgecallno]
11775  && iaxs[iaxs[fr->callno]->bridgecallno]->transferring) {
11776  iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_NONE;
11777  send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREJ, 0, NULL, 0, -1);
11778  }
11779  ast_mutex_unlock(&iaxsl[iaxs[fr->callno]->bridgecallno]);
11780  break;
11781  case IAX_COMMAND_TXREADY:
11782  while (iaxs[fr->callno]
11783  && iaxs[fr->callno]->bridgecallno
11784  && ast_mutex_trylock(&iaxsl[iaxs[fr->callno]->bridgecallno])) {
11785  DEADLOCK_AVOIDANCE(&iaxsl[fr->callno]);
11786  }
11787  if (!iaxs[fr->callno]) {
11788  break;
11789  }
11790 
11791  if (iaxs[fr->callno]->transferring == TRANSFER_BEGIN) {
11792  iaxs[fr->callno]->transferring = TRANSFER_READY;
11793  } else if (iaxs[fr->callno]->transferring == TRANSFER_MBEGIN) {
11794  iaxs[fr->callno]->transferring = TRANSFER_MREADY;
11795  } else {
11796  if (iaxs[fr->callno]->bridgecallno) {
11797  ast_mutex_unlock(&iaxsl[iaxs[fr->callno]->bridgecallno]);
11798  }
11799  break;
11800  }
11801  ast_verb(3, "Channel '%s' ready to transfer\n", iaxs[fr->callno]->owner ? ast_channel_name(iaxs[fr->callno]->owner) : "<Unknown>");
11802 
11803  if (!iaxs[fr->callno]->bridgecallno) {
11804  break;
11805  }
11806 
11807  if (!iaxs[iaxs[fr->callno]->bridgecallno]
11808  || (iaxs[iaxs[fr->callno]->bridgecallno]->transferring != TRANSFER_READY
11809  && iaxs[iaxs[fr->callno]->bridgecallno]->transferring != TRANSFER_MREADY)) {
11810  ast_mutex_unlock(&iaxsl[iaxs[fr->callno]->bridgecallno]);
11811  break;
11812  }
11813 
11814  /* Both sides are ready */
11815 
11816  /* XXX what isn't checked here is that both sides match transfer types. */
11817 
11818  if (iaxs[fr->callno]->transferring == TRANSFER_MREADY) {
11819  ast_verb(3, "Attempting media bridge of %s and %s\n", iaxs[fr->callno]->owner ? ast_channel_name(iaxs[fr->callno]->owner) : "<Unknown>",
11820  iaxs[iaxs[fr->callno]->bridgecallno]->owner ? ast_channel_name(iaxs[iaxs[fr->callno]->bridgecallno]->owner) : "<Unknown>");
11821 
11822  iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_MEDIA;
11823  iaxs[fr->callno]->transferring = TRANSFER_MEDIA;
11824 
11825  memset(&ied0, 0, sizeof(ied0));
11826  memset(&ied1, 0, sizeof(ied1));
11827  iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
11828  iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
11829  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied0.buf, ied0.pos, -1);
11830  send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXMEDIA, 0, ied1.buf, ied1.pos, -1);
11831  } else {
11832  ast_verb(3, "Releasing %s and %s\n", iaxs[fr->callno]->owner ? ast_channel_name(iaxs[fr->callno]->owner) : "<Unknown>",
11833  iaxs[iaxs[fr->callno]->bridgecallno]->owner ? ast_channel_name(iaxs[iaxs[fr->callno]->bridgecallno]->owner) : "<Unknown>");
11834 
11835  iaxs[iaxs[fr->callno]->bridgecallno]->transferring = TRANSFER_RELEASED;
11836  iaxs[fr->callno]->transferring = TRANSFER_RELEASED;
11837  ast_set_flag64(iaxs[iaxs[fr->callno]->bridgecallno], IAX_ALREADYGONE);
11838  ast_set_flag64(iaxs[fr->callno], IAX_ALREADYGONE);
11839 
11840  /* Stop doing lag & ping requests */
11841  stop_stuff(fr->callno);
11842  stop_stuff(iaxs[fr->callno]->bridgecallno);
11843 
11844  memset(&ied0, 0, sizeof(ied0));
11845  memset(&ied1, 0, sizeof(ied1));
11846  iax_ie_append_short(&ied0, IAX_IE_CALLNO, iaxs[iaxs[fr->callno]->bridgecallno]->peercallno);
11847  iax_ie_append_short(&ied1, IAX_IE_CALLNO, iaxs[fr->callno]->peercallno);
11848  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied0.buf, ied0.pos, -1);
11849  send_command(iaxs[iaxs[fr->callno]->bridgecallno], AST_FRAME_IAX, IAX_COMMAND_TXREL, 0, ied1.buf, ied1.pos, -1);
11850  }
11851  ast_mutex_unlock(&iaxsl[iaxs[fr->callno]->bridgecallno]);
11852  break;
11853  case IAX_COMMAND_TXREQ:
11854  try_transfer(iaxs[fr->callno], &ies);
11855  break;
11856  case IAX_COMMAND_TXCNT:
11857  if (iaxs[fr->callno]->transferring)
11858  send_command_transfer(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_TXACC, 0, NULL, 0);
11859  break;
11860  case IAX_COMMAND_TXREL:
11861  /* Send ack immediately, rather than waiting until we've changed addresses */
11862  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11863  complete_transfer(fr->callno, &ies);
11864  stop_stuff(fr->callno); /* for attended transfer to work with libiax */
11865  break;
11866  case IAX_COMMAND_TXMEDIA:
11867  if (iaxs[fr->callno]->transferring == TRANSFER_READY) {
11868  AST_LIST_TRAVERSE(&frame_queue[fr->callno], cur, list) {
11869  /* Cancel any outstanding frames and start anew */
11870  if (cur->transfer) {
11871  cur->retries = -1;
11872  }
11873  }
11874  /* Start sending our media to the transfer address, but otherwise leave the call as-is */
11875  iaxs[fr->callno]->transferring = TRANSFER_MEDIAPASS;
11876  }
11877  break;
11878  case IAX_COMMAND_RTKEY:
11879  if (!IAX_CALLENCRYPTED(iaxs[fr->callno])) {
11880  ast_log(LOG_WARNING,
11881  "we've been told to rotate our encryption key, "
11882  "but this isn't an encrypted call. bad things will happen.\n"
11883  );
11884  break;
11885  }
11886 
11887  IAX_DEBUGDIGEST("Receiving", ies.challenge);
11888 
11889  ast_aes_set_decrypt_key((unsigned char *) ies.challenge, &iaxs[fr->callno]->dcx);
11890  break;
11891  case IAX_COMMAND_DPREP:
11892  complete_dpreply(iaxs[fr->callno], &ies);
11893  break;
11894  case IAX_COMMAND_UNSUPPORT:
11895  ast_log(LOG_NOTICE, "Peer did not understand our iax command '%d'\n", ies.iax_unknown);
11896  break;
11897  case IAX_COMMAND_FWDOWNL:
11898  /* Firmware download */
11899  if (!ast_test_flag64(&globalflags, IAX_ALLOWFWDOWNLOAD)) {
11900  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_UNSUPPORT, 0, NULL, 0, -1);
11901  break;
11902  }
11903  memset(&ied0, 0, sizeof(ied0));
11904  res = iax_firmware_append(&ied0, ies.devicetype, ies.fwdesc);
11905  if (res < 0)
11906  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
11907  else if (res > 0)
11908  send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_FWDATA, 0, ied0.buf, ied0.pos, -1);
11909  else
11910  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_FWDATA, 0, ied0.buf, ied0.pos, -1);
11911  break;
11912  case IAX_COMMAND_CALLTOKEN:
11913  {
11914  struct iax_frame *cur;
11915  /* find last sent frame */
11916  if ((cur = AST_LIST_LAST(&frame_queue[fr->callno])) && ies.calltoken && ies.calltokendata) {
11917  resend_with_token(fr->callno, cur, (char *) ies.calltokendata);
11918  }
11919  break;
11920  }
11921  default:
11922  ast_debug(1, "Unknown IAX command %d on %d/%d\n", f.subclass.integer, fr->callno, iaxs[fr->callno]->peercallno);
11923  memset(&ied0, 0, sizeof(ied0));
11924  iax_ie_append_byte(&ied0, IAX_IE_IAX_UNKNOWN, f.subclass.integer);
11925  send_command(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_UNSUPPORT, 0, ied0.buf, ied0.pos, -1);
11926  }
11927  /* Free remote variables (if any) */
11928  if (ies.vars) {
11929  ast_variables_destroy(ies.vars);
11930  ast_debug(1, "I can haz IAX vars, but they is no good :-(\n");
11931  ies.vars = NULL;
11932  }
11933 
11934  /* Don't actually pass these frames along */
11935  if ((f.subclass.integer != IAX_COMMAND_ACK) &&
11938  (f.subclass.integer != IAX_COMMAND_INVAL) &&
11940  if (iaxs[fr->callno] && iaxs[fr->callno]->aseqno != iaxs[fr->callno]->iseqno) {
11941  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11942  }
11943  }
11944  ast_mutex_unlock(&iaxsl[fr->callno]);
11945  return 1;
11946  }
11947  /* Unless this is an ACK or INVAL frame, ack it */
11948  if (iaxs[fr->callno] && iaxs[fr->callno]->aseqno != iaxs[fr->callno]->iseqno)
11949  send_command_immediate(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_ACK, fr->ts, NULL, 0,fr->iseqno);
11950  } else if (minivid) {
11952  if (iaxs[fr->callno]->videoformat > 0) {
11953  if (ntohs(vh->ts) & 0x8000LL) {
11954  f.subclass.frame_ending = 1;
11955  }
11956  f.subclass.format = ast_format_compatibility_bitfield2format(iaxs[fr->callno]->videoformat);
11957  if (!f.subclass.format) {
11958  ast_variables_destroy(ies.vars);
11959  ast_mutex_unlock(&iaxsl[fr->callno]);
11960  return 1;
11961  }
11962  } else {
11963  ast_log(LOG_WARNING, "Received mini frame before first full video frame\n");
11964  iax2_vnak(fr->callno);
11965  ast_variables_destroy(ies.vars);
11966  ast_mutex_unlock(&iaxsl[fr->callno]);
11967  return 1;
11968  }
11969  f.datalen = res - sizeof(*vh);
11970  if (f.datalen)
11971  f.data.ptr = thread->buf + sizeof(*vh);
11972  else
11973  f.data.ptr = NULL;
11974 #ifdef IAXTESTS
11975  if (test_resync) {
11976  fr->ts = (iaxs[fr->callno]->last & 0xFFFF8000L) | ((ntohs(vh->ts) + test_resync) & 0x7fff);
11977  } else
11978 #endif /* IAXTESTS */
11979  fr->ts = (iaxs[fr->callno]->last & 0xFFFF8000L) | (ntohs(vh->ts) & 0x7fff);
11980  } else {
11981  /* A mini frame */
11983  if (iaxs[fr->callno]->voiceformat > 0) {
11984  f.subclass.format = ast_format_compatibility_bitfield2format(iaxs[fr->callno]->voiceformat);
11985  if (!f.subclass.format) {
11986  ast_variables_destroy(ies.vars);
11987  ast_mutex_unlock(&iaxsl[fr->callno]);
11988  return 1;
11989  }
11990  } else {
11991  ast_debug(1, "Received mini frame before first full voice frame\n");
11992  iax2_vnak(fr->callno);
11993  ast_variables_destroy(ies.vars);
11994  ast_mutex_unlock(&iaxsl[fr->callno]);
11995  return 1;
11996  }
11997  f.datalen = res - sizeof(struct ast_iax2_mini_hdr);
11998  if (f.datalen < 0) {
11999  ast_log(LOG_WARNING, "Datalen < 0?\n");
12000  ast_variables_destroy(ies.vars);
12001  ast_mutex_unlock(&iaxsl[fr->callno]);
12002  return 1;
12003  }
12004  if (f.datalen)
12005  f.data.ptr = thread->buf + sizeof(*mh);
12006  else
12007  f.data.ptr = NULL;
12008 #ifdef IAXTESTS
12009  if (test_resync) {
12010  fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | ((ntohs(mh->ts) + test_resync) & 0xffff);
12011  } else
12012 #endif /* IAXTESTS */
12013  fr->ts = (iaxs[fr->callno]->last & 0xFFFF0000L) | ntohs(mh->ts);
12014  /* FIXME? Surely right here would be the right place to undo timestamp wraparound? */
12015  }
12016 
12017  /* Don't pass any packets until we're started */
12018  if (!iaxs[fr->callno]
12019  || !ast_test_flag(&iaxs[fr->callno]->state, IAX_STATE_STARTED)) {
12020  ast_variables_destroy(ies.vars);
12021  ast_mutex_unlock(&iaxsl[fr->callno]);
12022  return 1;
12023  }
12024 
12025  if (f.frametype == AST_FRAME_CONTROL) {
12026  if (!iax2_is_control_frame_allowed(f.subclass.integer)) {
12027  /* Control frame not allowed to come from the wire. */
12028  ast_debug(2, "Callno %d: Blocked receiving control frame %d.\n",
12029  fr->callno, f.subclass.integer);
12030  ast_variables_destroy(ies.vars);
12031  ast_mutex_unlock(&iaxsl[fr->callno]);
12032  return 1;
12033  }
12036  if (iaxs[fr->callno]
12037  && !ast_test_flag64(iaxs[fr->callno], IAX_RECVCONNECTEDLINE)) {
12038  /* We are not configured to allow receiving these updates. */
12039  ast_debug(2, "Callno %d: Config blocked receiving control frame %d.\n",
12040  fr->callno, f.subclass.integer);
12041  ast_variables_destroy(ies.vars);
12042  ast_mutex_unlock(&iaxsl[fr->callno]);
12043  return 1;
12044  }
12045  }
12046 
12047  iax2_lock_owner(fr->callno);
12048  if (iaxs[fr->callno] && iaxs[fr->callno]->owner) {
12049  if (f.subclass.integer == AST_CONTROL_BUSY) {
12050  ast_channel_hangupcause_set(iaxs[fr->callno]->owner, AST_CAUSE_BUSY);
12051  } else if (f.subclass.integer == AST_CONTROL_CONGESTION) {
12052  ast_channel_hangupcause_set(iaxs[fr->callno]->owner, AST_CAUSE_CONGESTION);
12053  }
12054  ast_channel_unlock(iaxs[fr->callno]->owner);
12055  }
12056  }
12057 
12058  if (f.frametype == AST_FRAME_CONTROL
12060  && iaxs[fr->callno]) {
12061  struct ast_party_connected_line connected;
12062 
12063  /*
12064  * Process a received connected line update.
12065  *
12066  * Initialize defaults.
12067  */
12068  ast_party_connected_line_init(&connected);
12069  connected.id.number.presentation = iaxs[fr->callno]->calling_pres;
12070  connected.id.name.presentation = iaxs[fr->callno]->calling_pres;
12071 
12072  if (!ast_connected_line_parse_data(f.data.ptr, f.datalen, &connected)) {
12073  ast_string_field_set(iaxs[fr->callno], cid_num, connected.id.number.str);
12074  ast_string_field_set(iaxs[fr->callno], cid_name, connected.id.name.str);
12075  iaxs[fr->callno]->calling_pres = ast_party_id_presentation(&connected.id);
12076 
12077  iax2_lock_owner(fr->callno);
12078  if (iaxs[fr->callno] && iaxs[fr->callno]->owner) {
12079  ast_set_callerid(iaxs[fr->callno]->owner,
12080  S_COR(connected.id.number.valid, connected.id.number.str, ""),
12081  S_COR(connected.id.name.valid, connected.id.name.str, ""),
12082  NULL);
12083  ast_channel_caller(iaxs[fr->callno]->owner)->id.number.presentation = connected.id.number.presentation;
12084  ast_channel_caller(iaxs[fr->callno]->owner)->id.name.presentation = connected.id.name.presentation;
12085  ast_channel_unlock(iaxs[fr->callno]->owner);
12086  }
12087  }
12088  ast_party_connected_line_free(&connected);
12089  }
12090 
12091  /* Common things */
12092  f.src = "IAX2";
12093  f.mallocd = 0;
12094  f.offset = 0;
12095  f.len = 0;
12096  if (f.datalen && (f.frametype == AST_FRAME_VOICE)) {
12098  /* We need to byteswap incoming slinear samples from network byte order */
12100  ast_frame_byteswap_be(&f);
12101  } else
12102  f.samples = 0;
12103  iax_frame_wrap(fr, &f);
12104 
12105  /* If this is our most recent packet, use it as our basis for timestamping */
12106  if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts) {
12107  /*iaxs[fr->callno]->last = fr->ts; (do it afterwards cos schedule/forward_delivery needs the last ts too)*/
12108  fr->outoforder = 0;
12109  } else {
12110  if (iaxdebug && iaxs[fr->callno]) {
12111  ast_debug(1, "Received out of order packet... (type=%u, subclass %d, ts = %u, last = %u)\n", f.frametype, f.subclass.integer, fr->ts, iaxs[fr->callno]->last);
12112  }
12113  fr->outoforder = 1;
12114  }
12115  fr->cacheable = ((f.frametype == AST_FRAME_VOICE) || (f.frametype == AST_FRAME_VIDEO));
12116  if (iaxs[fr->callno]) {
12117  duped_fr = iaxfrdup2(fr);
12118  if (duped_fr) {
12119  schedule_delivery(duped_fr, updatehistory, 0, &fr->ts);
12120  }
12121  }
12122  if (iaxs[fr->callno] && iaxs[fr->callno]->last < fr->ts) {
12123  iaxs[fr->callno]->last = fr->ts;
12124 #if 1
12125  if (iaxdebug)
12126  ast_debug(8, "For call=%d, set last=%u\n", fr->callno, fr->ts);
12127 #endif
12128  }
12129 
12130  /* Always run again */
12131  ast_variables_destroy(ies.vars);
12132  ast_mutex_unlock(&iaxsl[fr->callno]);
12133  return 1;
12134 }
12135 
12136 static int socket_process(struct iax2_thread *thread)
12137 {
12138  int res = socket_process_helper(thread);
12141  }
12142  return res;
12143 }
12144 
12145 /* Function to clean up process thread if it is cancelled */
12146 static void iax2_process_thread_cleanup(void *data)
12147 {
12148  struct iax2_thread *thread = data;
12149  ast_mutex_destroy(&thread->lock);
12150  ast_cond_destroy(&thread->cond);
12151  ast_mutex_destroy(&thread->init_lock);
12152  ast_cond_destroy(&thread->init_cond);
12153  ast_free(thread);
12154  /* Ignore check_return warning from Coverity for ast_atomic_dec_and_test below */
12155  ast_atomic_dec_and_test(&iaxactivethreadcount);
12156 }
12157 
12158 static void *iax2_process_thread(void *data)
12159 {
12160  struct iax2_thread *thread = data;
12161  struct timeval wait;
12162  struct timespec ts;
12163  int put_into_idle = 0;
12164  int first_time = 1;
12165  int old_state;
12166 
12167  ast_atomic_fetchadd_int(&iaxactivethreadcount, 1);
12168 
12169  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
12170  pthread_cleanup_push(iax2_process_thread_cleanup, data);
12171 
12172  for (;;) {
12173  /* Wait for something to signal us to be awake */
12174  ast_mutex_lock(&thread->lock);
12175 
12176  if (thread->stop) {
12177  ast_mutex_unlock(&thread->lock);
12178  break;
12179  }
12180 
12181  /* Flag that we're ready to accept signals */
12182  if (first_time) {
12183  signal_condition(&thread->init_lock, &thread->init_cond);
12184  first_time = 0;
12185  }
12186 
12187  /* Put into idle list if applicable */
12188  if (put_into_idle) {
12189  insert_idle_thread(thread);
12190  }
12191 
12192  if (thread->type == IAX_THREAD_TYPE_DYNAMIC) {
12193  struct iax2_thread *t = NULL;
12194  /* Wait to be signalled or time out */
12195  wait = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
12196  ts.tv_sec = wait.tv_sec;
12197  ts.tv_nsec = wait.tv_usec * 1000;
12198  if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT) {
12199  /* This thread was never put back into the available dynamic
12200  * thread list, so just go away. */
12201  if (!put_into_idle || thread->stop) {
12202  ast_mutex_unlock(&thread->lock);
12203  break;
12204  }
12206  /* Account for the case where this thread is acquired *right* after a timeout */
12207  if ((t = AST_LIST_REMOVE(&dynamic_list, thread, list)))
12208  ast_atomic_fetchadd_int(&iaxdynamicthreadcount, -1);
12210  if (t) {
12211  /* This dynamic thread timed out waiting for a task and was
12212  * not acquired immediately after the timeout,
12213  * so it's time to go away. */
12214  ast_mutex_unlock(&thread->lock);
12215  break;
12216  }
12217  /* Someone grabbed our thread *right* after we timed out.
12218  * Wait for them to set us up with something to do and signal
12219  * us to continue. */
12220  wait = ast_tvadd(ast_tvnow(), ast_samp2tv(30000, 1000));
12221  ts.tv_sec = wait.tv_sec;
12222  ts.tv_nsec = wait.tv_usec * 1000;
12223  if (ast_cond_timedwait(&thread->cond, &thread->lock, &ts) == ETIMEDOUT) {
12224  ast_mutex_unlock(&thread->lock);
12225  break;
12226  }
12227  }
12228  } else {
12229  ast_cond_wait(&thread->cond, &thread->lock);
12230  }
12231 
12232  /* Go back into our respective list */
12233  put_into_idle = 1;
12234 
12235  ast_mutex_unlock(&thread->lock);
12236 
12237  if (thread->stop) {
12238  break;
12239  }
12240 
12241  /* See what we need to do */
12242  switch (thread->iostate) {
12243  case IAX_IOSTATE_IDLE:
12244  continue;
12245  case IAX_IOSTATE_READY:
12246  thread->actions++;
12247  thread->iostate = IAX_IOSTATE_PROCESSING;
12248  socket_process(thread);
12250  break;
12251  case IAX_IOSTATE_SCHEDREADY:
12252  thread->actions++;
12253  thread->iostate = IAX_IOSTATE_PROCESSING;
12254 #ifdef SCHED_MULTITHREADED
12255  thread->schedfunc(thread->scheddata);
12256 #endif
12257  break;
12258  default:
12259  break;
12260  }
12261 
12262  /* The network thread added us to the active_thread list when we were given
12263  * frames to process, Now that we are done, we must remove ourselves from
12264  * the active list, and return to the idle list */
12266  AST_LIST_REMOVE(&active_list, thread, list);
12268 
12269  /* Make sure another frame didn't sneak in there after we thought we were done. */
12271 
12272  time(&thread->checktime);
12273  thread->iostate = IAX_IOSTATE_IDLE;
12274 #ifdef DEBUG_SCHED_MULTITHREAD
12275  thread->curfunc[0]='\0';
12276 #endif
12277  }
12278 
12279  /*!
12280  * \note For some reason, idle threads are exiting without being
12281  * removed from an idle list, which is causing memory
12282  * corruption. Forcibly remove it from the list, if it's there.
12283  */
12285  AST_LIST_REMOVE(&idle_list, thread, list);
12287 
12289  AST_LIST_REMOVE(&dynamic_list, thread, list);
12291 
12292  if (!thread->stop) {
12293  /* Nobody asked me to stop so nobody is waiting to join me. */
12294  pthread_detach(pthread_self());
12295  }
12296 
12297  /* I am exiting here on my own volition, I need to clean up my own data structures
12298  * Assume that I am no longer in any of the lists (idle, active, or dynamic)
12299  */
12300  pthread_cleanup_pop(1);
12301  return NULL;
12302 }
12303 
12304 static int iax2_do_register(struct iax2_registry *reg)
12305 {
12306  struct iax_ie_data ied;
12307  if (iaxdebug)
12308  ast_debug(1, "Sending registration request for '%s'\n", reg->username);
12309 
12310  if (reg->dnsmgr &&
12311  ((reg->regstate == REG_STATE_TIMEOUT) || ast_sockaddr_isnull(&reg->addr))) {
12312  /* Maybe the IP has changed, force DNS refresh */
12313  ast_dnsmgr_refresh(reg->dnsmgr);
12314  }
12315 
12316  /*
12317  * if IP has Changed, free allocated call to create a new one with new IP
12318  * call has the pointer to IP and must be updated to the new one
12319  */
12320  if (reg->dnsmgr && ast_dnsmgr_changed(reg->dnsmgr) && (reg->callno > 0)) {
12321  int callno = reg->callno;
12322  ast_mutex_lock(&iaxsl[callno]);
12323  iax2_destroy(callno);
12324  ast_mutex_unlock(&iaxsl[callno]);
12325  reg->callno = 0;
12326  }
12327  if (ast_sockaddr_isnull(&reg->addr)) {
12328  if (iaxdebug)
12329  ast_debug(1, "Unable to send registration request for '%s' without IP address\n", reg->username);
12330  /* Setup the next registration attempt */
12331  reg->expire = iax2_sched_replace(reg->expire, sched,
12332  (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
12333  return -1;
12334  }
12335  if (!ast_sockaddr_port(&reg->addr) && reg->port) {
12336  ast_sockaddr_set_port(&reg->addr, reg->port);
12337  }
12338 
12339  if (!reg->callno) {
12340 
12341  ast_debug(3, "Allocate call number\n");
12342 
12343  reg->callno = find_callno_locked(0, 0, &reg->addr, NEW_FORCE, defaultsockfd, 0);
12344  if (reg->callno < 1) {
12345  ast_log(LOG_WARNING, "Unable to create call for registration\n");
12346  return -1;
12347  } else
12348  ast_debug(3, "Registration created on call %d\n", reg->callno);
12349  iaxs[reg->callno]->reg = reg;
12350  ast_mutex_unlock(&iaxsl[reg->callno]);
12351  }
12352  /* Setup the next registration a little early */
12353  reg->expire = iax2_sched_replace(reg->expire, sched,
12354  (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
12355  /* Send the request */
12356  memset(&ied, 0, sizeof(ied));
12357  iax_ie_append_str(&ied, IAX_IE_USERNAME, reg->username);
12358  iax_ie_append_short(&ied, IAX_IE_REFRESH, reg->refresh);
12359  add_empty_calltoken_ie(iaxs[reg->callno], &ied); /* this _MUST_ be the last ie added */
12360  send_command(iaxs[reg->callno],AST_FRAME_IAX, IAX_COMMAND_REGREQ, 0, ied.buf, ied.pos, -1);
12361  reg->regstate = REG_STATE_REGSENT;
12362  return 0;
12363 }
12364 
12365 static int iax2_provision(struct ast_sockaddr *end, int sockfd, const char *dest, const char *template, int force)
12366 {
12367  /* Returns 1 if provisioned, -1 if not able to find destination, or 0 if no provisioning
12368  is found for template */
12369  struct iax_ie_data provdata;
12370  struct iax_ie_data ied;
12371  unsigned int sig;
12372  struct ast_sockaddr addr;
12373  int callno;
12374  struct create_addr_info cai;
12375 
12376  memset(&cai, 0, sizeof(cai));
12377 
12378  ast_debug(1, "Provisioning '%s' from template '%s'\n", dest, template);
12379 
12380  if (iax_provision_build(&provdata, &sig, template, force)) {
12381  ast_debug(1, "No provisioning found for template '%s'\n", template);
12382  return 0;
12383  }
12384 
12385  if (end) {
12386  ast_sockaddr_copy(&addr, end);
12387  cai.sockfd = sockfd;
12388  } else if (create_addr(dest, NULL, &addr, &cai))
12389  return -1;
12390 
12391  /* Build the rest of the message */
12392  memset(&ied, 0, sizeof(ied));
12393  iax_ie_append_raw(&ied, IAX_IE_PROVISIONING, provdata.buf, provdata.pos);
12394 
12395  callno = find_callno_locked(0, 0, &addr, NEW_FORCE, cai.sockfd, 0);
12396  if (!callno)
12397  return -1;
12398 
12399  if (iaxs[callno]) {
12400  /* Schedule autodestruct in case they don't ever give us anything back */
12401  iaxs[callno]->autoid = iax2_sched_replace(iaxs[callno]->autoid,
12402  sched, 15000, auto_hangup, (void *)(long)callno);
12403  ast_set_flag64(iaxs[callno], IAX_PROVISION);
12404  /* Got a call number now, so go ahead and send the provisioning information */
12405  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_PROVISION, 0, ied.buf, ied.pos, -1);
12406  }
12407  ast_mutex_unlock(&iaxsl[callno]);
12408 
12409  return 1;
12410 }
12411 
12412 static char *papp = "IAX2Provision";
12413 
12414 /*! iax2provision
12415 \ingroup applications
12416 */
12417 static int iax2_prov_app(struct ast_channel *chan, const char *data)
12418 {
12419  int res;
12420  char *sdata;
12421  char *opts;
12422  int force =0;
12423  unsigned short callno = PTR_TO_CALLNO(ast_channel_tech_pvt(chan));
12424  if (ast_strlen_zero(data))
12425  data = "default";
12426  sdata = ast_strdupa(data);
12427  opts = strchr(sdata, '|');
12428  if (opts)
12429  *opts='\0';
12430 
12431  if (ast_channel_tech(chan) != &iax2_tech) {
12432  ast_log(LOG_NOTICE, "Can't provision a non-IAX device!\n");
12433  return -1;
12434  }
12435  if (!callno || !iaxs[callno] || ast_sockaddr_isnull(&iaxs[callno]->addr)) {
12436  ast_log(LOG_NOTICE, "Can't provision something with no IP?\n");
12437  return -1;
12438  }
12439  res = iax2_provision(&iaxs[callno]->addr, iaxs[callno]->sockfd, NULL, sdata, force);
12440  ast_verb(3, "Provisioned IAXY at '%s' with '%s'= %d\n",
12441  ast_sockaddr_stringify(&iaxs[callno]->addr),
12442  sdata, res);
12443  return res;
12444 }
12445 
12446 static char *handle_cli_iax2_provision(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
12447 {
12448  int force = 0;
12449  int res;
12450 
12451  switch (cmd) {
12452  case CLI_INIT:
12453  e->command = "iax2 provision";
12454  e->usage =
12455  "Usage: iax2 provision <host> <template> [forced]\n"
12456  " Provisions the given peer or IP address using a template\n"
12457  " matching either 'template' or '*' if the template is not\n"
12458  " found. If 'forced' is specified, even empty provisioning\n"
12459  " fields will be provisioned as empty fields.\n";
12460  return NULL;
12461  case CLI_GENERATE:
12462  if (a->pos == 3)
12463  return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
12464  return NULL;
12465  }
12466 
12467  if (a->argc < 4)
12468  return CLI_SHOWUSAGE;
12469  if (a->argc > 4) {
12470  if (!strcasecmp(a->argv[4], "forced"))
12471  force = 1;
12472  else
12473  return CLI_SHOWUSAGE;
12474  }
12475  res = iax2_provision(NULL, -1, a->argv[2], a->argv[3], force);
12476  if (res < 0)
12477  ast_cli(a->fd, "Unable to find peer/address '%s'\n", a->argv[2]);
12478  else if (res < 1)
12479  ast_cli(a->fd, "No template (including wildcard) matching '%s'\n", a->argv[3]);
12480  else
12481  ast_cli(a->fd, "Provisioning '%s' with template '%s'%s\n", a->argv[2], a->argv[3], force ? ", forced" : "");
12482  return CLI_SUCCESS;
12483 }
12484 
12485 static void __iax2_poke_noanswer(const void *data)
12486 {
12487  struct iax2_peer *peer = (struct iax2_peer *)data;
12488  int callno;
12489 
12490  if (peer->lastms > -1) {
12491  RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
12492 
12493  ast_log(LOG_NOTICE, "Peer '%s' is now UNREACHABLE! Time: %d\n", peer->name, peer->lastms);
12495  blob = ast_json_pack("{s: s, s: i}",
12496  "peer_status", "Unreachable",
12497  "time", peer->lastms);
12499  ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "IAX2/%s", peer->name); /* Activate notification */
12500  }
12501  if ((callno = peer->callno) > 0) {
12502  ast_mutex_lock(&iaxsl[callno]);
12503  iax2_destroy(callno);
12504  ast_mutex_unlock(&iaxsl[callno]);
12505  }
12506  peer->callno = 0;
12507  peer->lastms = -1;
12508  /* Try again quickly */
12509  peer->pokeexpire = iax2_sched_add(sched, peer->pokefreqnotok, iax2_poke_peer_s, peer_ref(peer));
12510  if (peer->pokeexpire == -1)
12511  peer_unref(peer);
12512 }
12513 
12514 static int iax2_poke_noanswer(const void *data)
12515 {
12516  struct iax2_peer *peer = (struct iax2_peer *)data;
12517  peer->pokeexpire = -1;
12518 #ifdef SCHED_MULTITHREADED
12519  if (schedule_action(__iax2_poke_noanswer, data))
12520 #endif
12521  __iax2_poke_noanswer(data);
12522  peer_unref(peer);
12523  return 0;
12524 }
12525 
12526 static int iax2_poke_peer_cb(void *obj, void *arg, int flags)
12527 {
12528  struct iax2_peer *peer = obj;
12529 
12530  iax2_poke_peer(peer, 0);
12531 
12532  return 0;
12533 }
12534 
12535 static int iax2_poke_peer(struct iax2_peer *peer, int heldcall)
12536 {
12537  int callno;
12538  int poke_timeout;
12539 
12540  if (!peer->maxms || (ast_sockaddr_isnull(&peer->addr) && !peer->dnsmgr)) {
12541  /* IF we have no IP without dnsmgr, or this isn't to be monitored, return
12542  immediately after clearing things out */
12543  peer->lastms = 0;
12544  peer->historicms = 0;
12545  peer->pokeexpire = -1;
12546  peer->callno = 0;
12547  return 0;
12548  }
12549 
12550  /* The peer could change the callno inside iax2_destroy, since we do deadlock avoidance */
12551  if ((callno = peer->callno) > 0) {
12552  ast_log(LOG_NOTICE, "Still have a callno...\n");
12553  ast_mutex_lock(&iaxsl[callno]);
12554  iax2_destroy(callno);
12555  ast_mutex_unlock(&iaxsl[callno]);
12556  }
12557  if (heldcall)
12558  ast_mutex_unlock(&iaxsl[heldcall]);
12559  callno = peer->callno = find_callno(0, 0, &peer->addr, NEW_FORCE, peer->sockfd, 0);
12560  if (heldcall)
12561  ast_mutex_lock(&iaxsl[heldcall]);
12562  if (callno < 1) {
12563  ast_log(LOG_WARNING, "Unable to allocate call for poking peer '%s'\n", peer->name);
12564  return -1;
12565  }
12566 
12567  if (peer->pokeexpire > -1) {
12568  if (!AST_SCHED_DEL(sched, peer->pokeexpire)) {
12569  peer->pokeexpire = -1;
12570  peer_unref(peer);
12571  }
12572  }
12573 
12574  if (peer->lastms < 0){
12575  /* If the host is already unreachable then use time less than the unreachable
12576  * interval. 5/6 is arbitrary multiplier to get value less than
12577  * peer->pokefreqnotok. Value less than peer->pokefreqnotok is used to expire
12578  * current POKE before starting new POKE (which is scheduled after
12579  * peer->pokefreqnotok). */
12580  poke_timeout = peer->pokefreqnotok * 5 / 6;
12581  } else {
12582  /* If the host is reachable, use timeout large enough to allow for multiple
12583  * POKE retries. Limit this value to less than peer->pokefreqok. 5/6 is arbitrary
12584  * multiplier to get value less than peer->pokefreqok. Value less than
12585  * peer->pokefreqok is used to expire current POKE before starting new POKE
12586  * (which is scheduled after peer->pokefreqok). */
12587  poke_timeout = MIN(MAX_RETRY_TIME * 2 + peer->maxms, peer->pokefreqok * 5 / 6);
12588  }
12589 
12590  /* Queue up a new task to handle no reply */
12591  peer->pokeexpire = iax2_sched_add(sched, poke_timeout, iax2_poke_noanswer, peer_ref(peer));
12592 
12593  if (peer->pokeexpire == -1)
12594  peer_unref(peer);
12595 
12596  /* And send the poke */
12597  ast_mutex_lock(&iaxsl[callno]);
12598  if (iaxs[callno]) {
12599  struct iax_ie_data ied = {
12600  .buf = { 0 },
12601  .pos = 0,
12602  };
12603 
12604  /* Speed up retransmission times for this qualify call */
12605  iaxs[callno]->pingtime = peer->maxms / 8;
12606  iaxs[callno]->peerpoke = peer;
12607 
12608  add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
12609  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_POKE, 0, ied.buf, ied.pos, -1);
12610  }
12611  ast_mutex_unlock(&iaxsl[callno]);
12612 
12613  return 0;
12614 }
12615 
12616 static void free_context(struct iax2_context *con)
12617 {
12618  struct iax2_context *conl;
12619  while(con) {
12620  conl = con;
12621  con = con->next;
12622  ast_free(conl);
12623  }
12624 }
12625 
12626 static struct ast_channel *iax2_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
12627 {
12628  int callno;
12629  int res;
12630  struct ast_sockaddr addr;
12631  struct ast_channel *c;
12632  struct parsed_dial_string pds;
12633  struct create_addr_info cai;
12634  char *tmpstr;
12635  ast_callid callid;
12636 
12637  memset(&pds, 0, sizeof(pds));
12638  tmpstr = ast_strdupa(data);
12639  parse_dial_string(tmpstr, &pds);
12640 
12641  callid = ast_read_threadstorage_callid();
12642 
12643  if (ast_strlen_zero(pds.peer)) {
12644  ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", data);
12645  return NULL;
12646  }
12647  memset(&cai, 0, sizeof(cai));
12648  cai.capability = iax2_capability;
12649 
12651 
12652  /* Populate our address from the given */
12653  if (create_addr(pds.peer, NULL, &addr, &cai)) {
12654  *cause = AST_CAUSE_UNREGISTERED;
12655  return NULL;
12656  }
12657 
12658  if (pds.port) {
12659  int bindport;
12660  ast_parse_arg(pds.port, PARSE_UINT32 | PARSE_IN_RANGE, &bindport, 0, 65535);
12661  ast_sockaddr_set_port(&addr, bindport);
12662  }
12663 
12664  callno = find_callno_locked(0, 0, &addr, NEW_FORCE, cai.sockfd, 0);
12665  if (callno < 1) {
12666  ast_log(LOG_WARNING, "Unable to create call\n");
12667  *cause = AST_CAUSE_CONGESTION;
12668  return NULL;
12669  }
12670 
12671  /* If this is a trunk, update it now */
12673  if (ast_test_flag64(&cai, IAX_TRUNK)) {
12674  int new_callno;
12675  if ((new_callno = make_trunk(callno, 1)) != -1)
12676  callno = new_callno;
12677  }
12678  iaxs[callno]->maxtime = cai.maxtime;
12679  if (callid) {
12680  iax_pvt_callid_set(callno, callid);
12681  }
12682 
12683  if (cai.found) {
12684  ast_string_field_set(iaxs[callno], host, pds.peer);
12685  }
12686 
12687  c = ast_iax2_new(callno, AST_STATE_DOWN, cai.capability, &cai.prefs, assignedids,
12688  requestor, cai.found);
12689 
12690  ast_mutex_unlock(&iaxsl[callno]);
12691 
12692  if (c) {
12693  struct ast_format_cap *joint;
12694  struct ast_format *format;
12695  if (callid) {
12696  ast_channel_lock(c);
12697  ast_channel_callid_set(c, callid);
12698  ast_channel_unlock(c);
12699  }
12700 
12702  if (!joint) {
12703  ast_hangup(c);
12704  return NULL;
12705  }
12706 
12707  ast_format_cap_get_compatible(ast_channel_nativeformats(c), cap, joint);
12708 
12709  /* If there is no joint format find one through translation */
12710  if (!ast_format_cap_count(joint)) {
12711  struct ast_format *best_fmt_cap = NULL;
12712  struct ast_format *best_fmt_native = NULL;
12713 
12714  res = ast_translator_best_choice(cap, ast_channel_nativeformats(c), &best_fmt_cap, &best_fmt_native);
12715  if (res < 0) {
12716  struct ast_str *native_cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
12717  struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
12718 
12719  ast_log(LOG_WARNING, "Unable to create translator path for %s to %s on %s\n",
12720  ast_format_cap_get_names(ast_channel_nativeformats(c), &native_cap_buf),
12721  ast_format_cap_get_names(cap, &cap_buf),
12722  ast_channel_name(c));
12723  ast_hangup(c);
12724  ao2_ref(joint, -1);
12725  return NULL;
12726  }
12727  ast_format_cap_append(joint, best_fmt_native, 0);
12728  ao2_ref(best_fmt_cap, -1);
12729  ao2_ref(best_fmt_native, -1);
12730  }
12731  ast_channel_nativeformats_set(c, joint);
12732  format = ast_format_cap_get_format(ast_channel_nativeformats(c), 0);
12733  ast_channel_set_readformat(c, format);
12734  ast_channel_set_writeformat(c, format);
12735 
12736  ao2_ref(joint, -1);
12737  ao2_ref(format, -1);
12738  }
12739 
12740  return c;
12741 }
12742 
12743 static void *network_thread(void *ignore)
12744 {
12745  int res;
12746 
12747  if (timer) {
12748  ast_io_add(io, ast_timer_fd(timer), timing_read, AST_IO_IN | AST_IO_PRI, NULL);
12749  }
12750 
12751  for (;;) {
12752  pthread_testcancel();
12753  /* Wake up once a second just in case SIGURG was sent while
12754  * we weren't in poll(), to make sure we don't hang when trying
12755  * to unload. */
12756  res = ast_io_wait(io, 1000);
12757  /* Timeout(=0), and EINTR is not a thread exit condition. We do
12758  * not want to exit the thread loop on these conditions. */
12759  if (res < 0 && errno != -EINTR) {
12760  ast_log(LOG_ERROR, "IAX2 network thread unexpected exit: %s\n", strerror(errno));
12761  break;
12762  }
12763  }
12764 
12765  return NULL;
12766 }
12767 
12768 static int start_network_thread(void)
12769 {
12770  struct iax2_thread *thread;
12771  int threadcount = 0;
12772  int x;
12773  for (x = 0; x < iaxthreadcount; x++) {
12774  thread = ast_calloc(1, sizeof(*thread));
12775  if (thread) {
12776  thread->type = IAX_THREAD_TYPE_POOL;
12777  thread->threadnum = ++threadcount;
12778  ast_mutex_init(&thread->lock);
12779  ast_cond_init(&thread->cond, NULL);
12780  ast_mutex_init(&thread->init_lock);
12781  ast_cond_init(&thread->init_cond, NULL);
12782 
12783  ast_mutex_lock(&thread->init_lock);
12784 
12785  if (ast_pthread_create_background(&thread->threadid, NULL, iax2_process_thread, thread)) {
12786  ast_log(LOG_WARNING, "Failed to create new thread!\n");
12787  ast_mutex_destroy(&thread->lock);
12788  ast_cond_destroy(&thread->cond);
12789  ast_mutex_unlock(&thread->init_lock);
12790  ast_mutex_destroy(&thread->init_lock);
12791  ast_cond_destroy(&thread->init_cond);
12792  ast_free(thread);
12793  thread = NULL;
12794  continue;
12795  }
12796  /* Wait for the thread to be ready */
12797  ast_cond_wait(&thread->init_cond, &thread->init_lock);
12798 
12799  /* Done with init_lock */
12800  ast_mutex_unlock(&thread->init_lock);
12801 
12803  AST_LIST_INSERT_TAIL(&idle_list, thread, list);
12805  }
12806  }
12807  if (ast_pthread_create_background(&netthreadid, NULL, network_thread, NULL)) {
12808  ast_log(LOG_ERROR, "Failed to create new thread!\n");
12809  return -1;
12810  }
12811  ast_verb(2, "%d helper threads started\n", threadcount);
12812  return 0;
12813 }
12814 
12815 static struct iax2_context *build_context(const char *context)
12816 {
12817  struct iax2_context *con;
12818 
12819  if ((con = ast_calloc(1, sizeof(*con))))
12820  ast_copy_string(con->context, context, sizeof(con->context));
12821 
12822  return con;
12823 }
12824 
12825 static int get_auth_methods(const char *value)
12826 {
12827  int methods = 0;
12828  if (strstr(value, "rsa"))
12829  methods |= IAX_AUTH_RSA;
12830  if (strstr(value, "md5"))
12831  methods |= IAX_AUTH_MD5;
12832  if (strstr(value, "plaintext"))
12833  methods |= IAX_AUTH_PLAINTEXT;
12834  return methods;
12835 }
12836 
12837 
12838 /*! \brief Check if address can be used as packet source.
12839  \retval 0 address available
12840  \retval 1 address unavailable
12841  \retval -1 error
12842 */
12843 static int check_srcaddr(struct ast_sockaddr *addr)
12844 {
12845  int sd;
12846 
12847  sd = socket(addr->ss.ss_family, SOCK_DGRAM, 0);
12848  if (sd < 0) {
12849  ast_log(LOG_ERROR, "Socket: %s\n", strerror(errno));
12850  return -1;
12851  }
12852 
12853  if (ast_bind(sd, addr) < 0) {
12854  ast_debug(1, "Can't bind: %s\n", strerror(errno));
12855  close(sd);
12856  return 1;
12857  }
12858 
12859  close(sd);
12860  return 0;
12861 }
12862 
12863 /*! \brief Parse the "sourceaddress" value,
12864  lookup in netsock list and set peer's sockfd. Defaults to defaultsockfd if
12865  not found. */
12866 static int peer_set_srcaddr(struct iax2_peer *peer, const char *srcaddr)
12867 {
12868  struct ast_sockaddr addr;
12869  int nonlocal = 1;
12870  int port = IAX_DEFAULT_PORTNO;
12871  int sockfd = defaultsockfd;
12872  char *tmp;
12873  char *host;
12874  char *portstr;
12875 
12876  tmp = ast_strdupa(srcaddr);
12877  ast_sockaddr_split_hostport(tmp, &host, &portstr, 0);
12878 
12879  if (portstr) {
12880  port = atoi(portstr);
12881  if (port < 1)
12882  port = IAX_DEFAULT_PORTNO;
12883  }
12884 
12885  addr.ss.ss_family = AST_AF_UNSPEC;
12886  if (!ast_get_ip(&addr, host)) {
12887  struct ast_netsock *sock;
12888 
12889  if (check_srcaddr(&addr) == 0) {
12890  /* ip address valid. */
12891  ast_sockaddr_set_port(&addr, port);
12892 
12893  if (!(sock = ast_netsock_find(netsock, &addr)))
12894  sock = ast_netsock_find(outsock, &addr);
12895  if (sock) {
12896  sockfd = ast_netsock_sockfd(sock);
12897  nonlocal = 0;
12898  } else {
12899  /* INADDR_ANY matches anyway! */
12900  ast_sockaddr_parse(&addr, "0.0.0.0", 0);
12901  ast_sockaddr_set_port(&addr, port);
12902  if (ast_netsock_find(netsock, &addr)) {
12903  sock = ast_netsock_bind(outsock, io, srcaddr, port, qos.tos, qos.cos, socket_read, NULL);
12904  if (sock) {
12905  sockfd = ast_netsock_sockfd(sock);
12906  ast_netsock_unref(sock);
12907  nonlocal = 0;
12908  } else {
12909  nonlocal = 2;
12910  }
12911  }
12912  }
12913  }
12914  }
12915 
12916  peer->sockfd = sockfd;
12917 
12918  if (nonlocal == 1) {
12919  ast_log(LOG_WARNING,
12920  "Non-local or unbound address specified (%s) in sourceaddress for '%s', reverting to default\n",
12921  srcaddr,
12922  peer->name);
12923  return -1;
12924  } else if (nonlocal == 2) {
12925  ast_log(LOG_WARNING,
12926  "Unable to bind to sourceaddress '%s' for '%s', reverting to default\n",
12927  srcaddr,
12928  peer->name);
12929  return -1;
12930  } else {
12931  ast_debug(1, "Using sourceaddress %s for '%s'\n", srcaddr, peer->name);
12932  return 0;
12933  }
12934 }
12935 
12936 static void peer_destructor(void *obj)
12937 {
12938  struct iax2_peer *peer = obj;
12939  int callno = peer->callno;
12940 
12941  ast_free_acl_list(peer->acl);
12942 
12943  if (callno > 0) {
12944  ast_mutex_lock(&iaxsl[callno]);
12945  iax2_destroy(callno);
12946  ast_mutex_unlock(&iaxsl[callno]);
12947  }
12948 
12949  register_peer_exten(peer, 0);
12950 
12951  if (peer->dnsmgr)
12952  ast_dnsmgr_release(peer->dnsmgr);
12953 
12954  if (peer->mwi_event_sub) {
12956  }
12957 
12959 
12961 }
12962 
12963 /*! \brief Create peer structure based on configuration */
12964 static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
12965 {
12966  struct iax2_peer *peer = NULL;
12967  struct ast_acl_list *oldacl = NULL;
12968  int maskfound = 0;
12969  int found = 0;
12970  int firstpass = 1;
12971  int subscribe_acl_change = 0;
12972 
12973  if (!temponly) {
12974  peer = ao2_find(peers, name, OBJ_KEY);
12975  if (peer && !ast_test_flag64(peer, IAX_DELME))
12976  firstpass = 0;
12977  }
12978 
12979  if (peer) {
12980  found++;
12981  if (firstpass) {
12982  oldacl = peer->acl;
12983  peer->acl = NULL;
12984  }
12985  unlink_peer(peer);
12986  } else if ((peer = ao2_alloc(sizeof(*peer), peer_destructor))) {
12987  peer->expire = -1;
12988  peer->pokeexpire = -1;
12989  peer->sockfd = defaultsockfd;
12990  if (ast_string_field_init(peer, 32))
12991  peer = peer_unref(peer);
12992  if (!(peer->endpoint = ast_endpoint_create("IAX2", name))) {
12993  peer = peer_unref(peer);
12994  }
12995  }
12996 
12997  if (peer) {
12998  if (firstpass) {
12999  ast_copy_flags64(peer, &globalflags, IAX_USEJITTERBUF | IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE | IAX_FORCE_ENCRYPT);
13000  peer->encmethods = iax2_encryption;
13001  peer->authmethods = iax2_authmethods;
13002  peer->adsi = adsi;
13003  ast_string_field_set(peer, secret, "");
13004  if (!found) {
13005  ast_string_field_set(peer, name, name);
13006  ast_sockaddr_parse(&peer->addr, "0.0.0.0", 0);
13007  ast_sockaddr_set_port(&peer->addr, IAX_DEFAULT_PORTNO);
13008  peer->expiry = min_reg_expire;
13009  }
13010  peer->prefs = prefs_global;
13011  peer->capability = iax2_capability;
13012  peer->smoothing = 0;
13013  peer->pokefreqok = DEFAULT_FREQ_OK;
13014  peer->pokefreqnotok = DEFAULT_FREQ_NOTOK;
13015  peer->maxcallno = 0;
13016  peercnt_modify((unsigned char) 0, 0, &peer->addr);
13018  ast_string_field_set(peer,context,"");
13019  ast_string_field_set(peer,peercontext,"");
13020  ast_clear_flag64(peer, IAX_HASCALLERID);
13021  ast_string_field_set(peer, cid_name, "");
13022  ast_string_field_set(peer, cid_num, "");
13023  ast_string_field_set(peer, mohinterpret, mohinterpret);
13024  ast_string_field_set(peer, mohsuggest, mohsuggest);
13025  }
13026 
13027  if (!v) {
13028  v = alt;
13029  alt = NULL;
13030  }
13031  while(v) {
13032  if (!strcasecmp(v->name, "secret")) {
13033  ast_string_field_set(peer, secret, v->value);
13034  } else if (!strcasecmp(v->name, "mailbox")) {
13035  ast_string_field_set(peer, mailbox, v->value);
13036  } else if (!strcasecmp(v->name, "hasvoicemail")) {
13037  if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
13038  /*
13039  * hasvoicemail is a users.conf legacy voicemail enable method.
13040  * hasvoicemail is only going to work for app_voicemail mailboxes.
13041  */
13042  if (strchr(name, '@')) {
13043  ast_string_field_set(peer, mailbox, name);
13044  } else {
13045  ast_string_field_build(peer, mailbox, "%s@default", name);
13046  }
13047  }
13048  } else if (!strcasecmp(v->name, "mohinterpret")) {
13049  ast_string_field_set(peer, mohinterpret, v->value);
13050  } else if (!strcasecmp(v->name, "mohsuggest")) {
13051  ast_string_field_set(peer, mohsuggest, v->value);
13052  } else if (!strcasecmp(v->name, "dbsecret")) {
13053  ast_string_field_set(peer, dbsecret, v->value);
13054  } else if (!strcasecmp(v->name, "description")) {
13055  ast_string_field_set(peer, description, v->value);
13056  } else if (!strcasecmp(v->name, "trunk")) {
13057  ast_set2_flag64(peer, ast_true(v->value), IAX_TRUNK);
13058  if (ast_test_flag64(peer, IAX_TRUNK) && !timer) {
13059  ast_log(LOG_WARNING, "Unable to support trunking on peer '%s' without a timing interface\n", peer->name);
13060  ast_clear_flag64(peer, IAX_TRUNK);
13061  }
13062  } else if (!strcasecmp(v->name, "auth")) {
13063  peer->authmethods = get_auth_methods(v->value);
13064  if (peer->authmethods & IAX_AUTH_PLAINTEXT) {
13065  ast_log(LOG_WARNING, "Auth method for peer '%s' is set to deprecated 'plaintext' at line %d of iax.conf\n", peer->name, v->lineno);
13066  }
13067  } else if (!strcasecmp(v->name, "encryption")) {
13068  peer->encmethods |= get_encrypt_methods(v->value);
13069  if (!peer->encmethods) {
13070  ast_clear_flag64(peer, IAX_FORCE_ENCRYPT);
13071  }
13072  } else if (!strcasecmp(v->name, "forceencryption")) {
13073  if (ast_false(v->value)) {
13074  ast_clear_flag64(peer, IAX_FORCE_ENCRYPT);
13075  } else {
13076  peer->encmethods |= get_encrypt_methods(v->value);
13077  if (peer->encmethods) {
13078  ast_set_flag64(peer, IAX_FORCE_ENCRYPT);
13079  }
13080  }
13081  } else if (!strcasecmp(v->name, "transfer")) {
13082  if (!strcasecmp(v->value, "mediaonly")) {
13083  ast_set_flags_to64(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA);
13084  } else if (ast_true(v->value)) {
13085  ast_set_flags_to64(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
13086  } else
13087  ast_set_flags_to64(peer, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
13088  } else if (!strcasecmp(v->name, "jitterbuffer")) {
13089  ast_set2_flag64(peer, ast_true(v->value), IAX_USEJITTERBUF);
13090  } else if (!strcasecmp(v->name, "host")) {
13091  if (!strcasecmp(v->value, "dynamic")) {
13092  /* They'll register with us */
13093  ast_set_flag64(peer, IAX_DYNAMIC);
13094  if (!found) {
13095  int peer_port = ast_sockaddr_port(&peer->addr);
13096  if (peer_port) {
13097  ast_sockaddr_set_port(&peer->defaddr, peer_port);
13098  }
13099  ast_sockaddr_setnull(&peer->addr);
13100  }
13101  } else {
13102  /* Non-dynamic. Make sure we become that way if we're not */
13103  AST_SCHED_DEL(sched, peer->expire);
13104  ast_clear_flag64(peer, IAX_DYNAMIC);
13105  if (peer->dnsmgr) {
13106  // Make sure we refresh dnsmgr if we're using it
13107  ast_dnsmgr_refresh(peer->dnsmgr);
13108  } else {
13109  // Or just invalidate the address
13110  peer->addr.ss.ss_family = AST_AF_UNSPEC;
13111  }
13112  if (ast_dnsmgr_lookup(v->value, &peer->addr, &peer->dnsmgr, srvlookup ? "_iax._udp" : NULL)) {
13113  return peer_unref(peer);
13114  }
13115  if (!ast_sockaddr_port(&peer->addr)) {
13116  ast_sockaddr_set_port(&peer->addr, IAX_DEFAULT_PORTNO);
13117  }
13118  }
13119  } else if (!strcasecmp(v->name, "defaultip")) {
13120  struct ast_sockaddr peer_defaddr_tmp;
13121  peer_defaddr_tmp.ss.ss_family = AF_UNSPEC;
13122  if (ast_get_ip(&peer_defaddr_tmp, v->value)) {
13123  return peer_unref(peer);
13124  }
13125  ast_sockaddr_set_port(&peer_defaddr_tmp, ast_sockaddr_port(&peer->defaddr));
13126  ast_sockaddr_copy(&peer->defaddr, &peer_defaddr_tmp);
13127  } else if (!strcasecmp(v->name, "sourceaddress")) {
13128  peer_set_srcaddr(peer, v->value);
13129  } else if (!strcasecmp(v->name, "permit") ||
13130  !strcasecmp(v->name, "deny") ||
13131  !strcasecmp(v->name, "acl")) {
13132  ast_append_acl(v->name, v->value, &peer->acl, NULL, &subscribe_acl_change);
13133  } else if (!strcasecmp(v->name, "mask")) {
13134  maskfound++;
13135  ast_sockaddr_parse(&peer->mask, v->value, 0);
13136  } else if (!strcasecmp(v->name, "context")) {
13137  ast_string_field_set(peer, context, v->value);
13138  } else if (!strcasecmp(v->name, "regexten")) {
13139  ast_string_field_set(peer, regexten, v->value);
13140  } else if (!strcasecmp(v->name, "peercontext")) {
13141  ast_string_field_set(peer, peercontext, v->value);
13142  } else if (!strcasecmp(v->name, "port")) {
13143  int bindport;
13144  if (ast_parse_arg(v->value, PARSE_UINT32 | PARSE_IN_RANGE, &bindport, 0, 65535)) {
13145  bindport = IAX_DEFAULT_PORTNO;
13146  }
13147  if (ast_test_flag64(peer, IAX_DYNAMIC)) {
13148  ast_sockaddr_set_port(&peer->defaddr, bindport);
13149  } else {
13150  ast_sockaddr_set_port(&peer->addr, bindport);
13151  }
13152  } else if (!strcasecmp(v->name, "username")) {
13153  ast_string_field_set(peer, username, v->value);
13154  } else if (!strcasecmp(v->name, "allow")) {
13155  iax2_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 1);
13156  } else if (!strcasecmp(v->name, "disallow")) {
13157  iax2_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, 0);
13158  } else if (!strcasecmp(v->name, "callerid")) {
13159  if (!ast_strlen_zero(v->value)) {
13160  char name2[80];
13161  char num2[80];
13162  ast_callerid_split(v->value, name2, sizeof(name2), num2, sizeof(num2));
13163  ast_string_field_set(peer, cid_name, name2);
13164  ast_string_field_set(peer, cid_num, num2);
13165  } else {
13166  ast_string_field_set(peer, cid_name, "");
13167  ast_string_field_set(peer, cid_num, "");
13168  }
13169  ast_set_flag64(peer, IAX_HASCALLERID);
13170  } else if (!strcasecmp(v->name, "fullname")) {
13171  ast_string_field_set(peer, cid_name, S_OR(v->value, ""));
13172  ast_set_flag64(peer, IAX_HASCALLERID);
13173  } else if (!strcasecmp(v->name, "cid_number")) {
13174  ast_string_field_set(peer, cid_num, S_OR(v->value, ""));
13175  ast_set_flag64(peer, IAX_HASCALLERID);
13176  } else if (!strcasecmp(v->name, "sendani")) {
13177  ast_set2_flag64(peer, ast_true(v->value), IAX_SENDANI);
13178  } else if (!strcasecmp(v->name, "inkeys")) {
13179  ast_string_field_set(peer, inkeys, v->value);
13180  } else if (!strcasecmp(v->name, "outkey")) {
13181  ast_string_field_set(peer, outkey, v->value);
13182  } else if (!strcasecmp(v->name, "qualify")) {
13183  if (!strcasecmp(v->value, "no")) {
13184  peer->maxms = 0;
13185  } else if (!strcasecmp(v->value, "yes")) {
13186  peer->maxms = DEFAULT_MAXMS;
13187  } else if (sscanf(v->value, "%30d", &peer->maxms) != 1) {
13188  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);
13189  peer->maxms = 0;
13190  }
13191  } else if (!strcasecmp(v->name, "qualifysmoothing")) {
13192  peer->smoothing = ast_true(v->value);
13193  } else if (!strcasecmp(v->name, "qualifyfreqok")) {
13194  if (sscanf(v->value, "%30d", &peer->pokefreqok) != 1) {
13195  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);
13196  }
13197  } else if (!strcasecmp(v->name, "qualifyfreqnotok")) {
13198  if (sscanf(v->value, "%30d", &peer->pokefreqnotok) != 1) {
13199  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);
13200  }
13201  } else if (!strcasecmp(v->name, "timezone")) {
13202  ast_string_field_set(peer, zonetag, v->value);
13203  } else if (!strcasecmp(v->name, "adsi")) {
13204  peer->adsi = ast_true(v->value);
13205  } else if (!strcasecmp(v->name, "connectedline")) {
13206  if (ast_true(v->value)) {
13207  ast_set_flag64(peer, IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE);
13208  } else if (!strcasecmp(v->value, "send")) {
13209  ast_clear_flag64(peer, IAX_RECVCONNECTEDLINE);
13210  ast_set_flag64(peer, IAX_SENDCONNECTEDLINE);
13211  } else if (!strcasecmp(v->value, "receive")) {
13212  ast_clear_flag64(peer, IAX_SENDCONNECTEDLINE);
13213  ast_set_flag64(peer, IAX_RECVCONNECTEDLINE);
13214  } else {
13215  ast_clear_flag64(peer, IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE);
13216  }
13217  } else if (!strcasecmp(v->name, "maxcallnumbers")) {
13218  if (sscanf(v->value, "%10hu", &peer->maxcallno) != 1) {
13219  ast_log(LOG_WARNING, "maxcallnumbers must be set to a valid number. %s is not valid at line %d.\n", v->value, v->lineno);
13220  } else {
13221  peercnt_modify((unsigned char) 1, peer->maxcallno, &peer->addr);
13222  }
13223  } else if (!strcasecmp(v->name, "requirecalltoken")) {
13224  /* default is required unless in optional ip list */
13225  if (ast_false(v->value)) {
13227  } else if (!strcasecmp(v->value, "auto")) {
13229  } else if (ast_true(v->value)) {
13231  } else {
13232  ast_log(LOG_WARNING, "requirecalltoken must be set to a valid value. at line %d\n", v->lineno);
13233  }
13234  } /* else if (strcasecmp(v->name,"type")) */
13235  /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
13236  v = v->next;
13237  if (!v) {
13238  v = alt;
13239  alt = NULL;
13240  }
13241  }
13242  if (!peer->authmethods)
13243  peer->authmethods = IAX_AUTH_MD5;
13244  ast_clear_flag64(peer, IAX_DELME);
13245  }
13246 
13247  if (!maskfound && !ast_sockaddr_isnull(&peer->addr)) {
13248  if (ast_sockaddr_is_ipv4_mapped(&peer->addr)) {
13249  ast_sockaddr_parse(&peer->mask, "::ffff:ffff:ffff", 0);
13250  } else if (ast_sockaddr_is_ipv6(&peer->addr)) {
13251  ast_sockaddr_parse(&peer->mask, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
13252  } else {
13253  ast_sockaddr_parse(&peer->mask, "255.255.255.255", 0);
13254  }
13255  }
13256 
13257  if (oldacl) {
13258  ast_free_acl_list(oldacl);
13259  }
13260 
13261  if (!ast_strlen_zero(peer->mailbox) && !peer->mwi_event_sub) {
13262  /* The MWI subscriptions exist just so the core knows we care about those
13263  * mailboxes. However, we just grab the events out of the cache when it
13264  * is time to send MWI, since it is only sent with a REGACK. */
13266  }
13267 
13268  if (subscribe_acl_change) {
13269  acl_change_stasis_subscribe();
13270  }
13271 
13272  return peer;
13273 }
13274 
13275 static void user_destructor(void *obj)
13276 {
13277  struct iax2_user *user = obj;
13278 
13279  ast_free_acl_list(user->acl);
13280  free_context(user->contexts);
13281  if(user->vars) {
13282  ast_variables_destroy(user->vars);
13283  user->vars = NULL;
13284  }
13286 }
13287 
13288 /*! \brief Create in-memory user structure from configuration */
13289 static struct iax2_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
13290 {
13291  struct iax2_user *user = NULL;
13292  struct iax2_context *con, *conl = NULL;
13293  struct ast_acl_list *oldacl = NULL;
13294  struct iax2_context *oldcon = NULL;
13295  int format;
13296  int firstpass=1;
13297  int oldcurauthreq = 0;
13298  int subscribe_acl_change = 0;
13299  char *varname = NULL, *varval = NULL;
13300  struct ast_variable *tmpvar = NULL;
13301 
13302  if (!temponly) {
13303  user = ao2_find(users, name, OBJ_KEY);
13304  if (user && !ast_test_flag64(user, IAX_DELME))
13305  firstpass = 0;
13306  }
13307 
13308  if (user) {
13309  if (firstpass) {
13310  oldcurauthreq = user->curauthreq;
13311  oldacl = user->acl;
13312  oldcon = user->contexts;
13313  user->acl = NULL;
13314  user->contexts = NULL;
13315  }
13316  /* Already in the list, remove it and it will be added back (or FREE'd) */
13317  ao2_unlink(users, user);
13318  } else {
13319  user = ao2_alloc(sizeof(*user), user_destructor);
13320  }
13321 
13322  if (user) {
13323  if (firstpass) {
13325  memset(user, 0, sizeof(struct iax2_user));
13326  if (ast_string_field_init(user, 32)) {
13327  user = user_unref(user);
13328  goto cleanup;
13329  }
13330  user->maxauthreq = maxauthreq;
13331  user->curauthreq = oldcurauthreq;
13332  user->prefs = prefs_global;
13333  user->capability = iax2_capability;
13334  user->encmethods = iax2_encryption;
13335  user->authmethods = iax2_authmethods;
13336  user->adsi = adsi;
13338  ast_string_field_set(user, name, name);
13339  ast_string_field_set(user, language, language);
13341  ast_clear_flag64(user, IAX_HASCALLERID);
13342  ast_string_field_set(user, cid_name, "");
13343  ast_string_field_set(user, cid_num, "");
13344  ast_string_field_set(user, accountcode, accountcode);
13345  ast_string_field_set(user, mohinterpret, mohinterpret);
13346  ast_string_field_set(user, mohsuggest, mohsuggest);
13347  }
13348  if (!v) {
13349  v = alt;
13350  alt = NULL;
13351  }
13352  while(v) {
13353  if (!strcasecmp(v->name, "context")) {
13354  con = build_context(v->value);
13355  if (con) {
13356  if (conl)
13357  conl->next = con;
13358  else
13359  user->contexts = con;
13360  conl = con;
13361  }
13362  } else if (!strcasecmp(v->name, "permit") ||
13363  !strcasecmp(v->name, "deny") ||
13364  !strcasecmp(v->name, "acl")) {
13365  ast_append_acl(v->name, v->value, &user->acl, NULL, &subscribe_acl_change);
13366  } else if (!strcasecmp(v->name, "setvar")) {
13367  varname = ast_strdupa(v->value);
13368  if ((varval = strchr(varname, '='))) {
13369  *varval = '\0';
13370  varval++;
13371  if ((tmpvar = ast_variable_new(varname, varval, ""))) {
13372  if (ast_variable_list_replace(&user->vars, tmpvar)) {
13373  tmpvar->next = user->vars;
13374  user->vars = tmpvar;
13375  }
13376  }
13377  }
13378  } else if (!strcasecmp(v->name, "allow")) {
13379  iax2_parse_allow_disallow(&user->prefs, &user->capability, v->value, 1);
13380  } else if (!strcasecmp(v->name, "disallow")) {
13381  iax2_parse_allow_disallow(&user->prefs, &user->capability,v->value, 0);
13382  } else if (!strcasecmp(v->name, "trunk")) {
13383  ast_set2_flag64(user, ast_true(v->value), IAX_TRUNK);
13384  if (ast_test_flag64(user, IAX_TRUNK) && !timer) {
13385  ast_log(LOG_WARNING, "Unable to support trunking on user '%s' without a timing interface\n", user->name);
13386  ast_clear_flag64(user, IAX_TRUNK);
13387  }
13388  } else if (!strcasecmp(v->name, "auth")) {
13389  user->authmethods = get_auth_methods(v->value);
13390  if (user->authmethods & IAX_AUTH_PLAINTEXT) {
13391  ast_log(LOG_WARNING, "Auth method for user '%s' is set to deprecated 'plaintext' at line %d of iax.conf\n", user->name, v->lineno);
13392  }
13393  } else if (!strcasecmp(v->name, "encryption")) {
13394  user->encmethods |= get_encrypt_methods(v->value);
13395  if (!user->encmethods) {
13396  ast_clear_flag64(user, IAX_FORCE_ENCRYPT);
13397  }
13398  } else if (!strcasecmp(v->name, "forceencryption")) {
13399  if (ast_false(v->value)) {
13400  ast_clear_flag64(user, IAX_FORCE_ENCRYPT);
13401  } else {
13402  user->encmethods |= get_encrypt_methods(v->value);
13403  if (user->encmethods) {
13404  ast_set_flag64(user, IAX_FORCE_ENCRYPT);
13405  }
13406  }
13407  } else if (!strcasecmp(v->name, "transfer")) {
13408  if (!strcasecmp(v->value, "mediaonly")) {
13409  ast_set_flags_to64(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA);
13410  } else if (ast_true(v->value)) {
13411  ast_set_flags_to64(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
13412  } else
13413  ast_set_flags_to64(user, IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
13414  } else if (!strcasecmp(v->name, "codecpriority")) {
13415  if(!strcasecmp(v->value, "caller"))
13416  ast_set_flag64(user, IAX_CODEC_USER_FIRST);
13417  else if(!strcasecmp(v->value, "disabled"))
13418  ast_set_flag64(user, IAX_CODEC_NOPREFS);
13419  else if(!strcasecmp(v->value, "reqonly")) {
13420  ast_set_flag64(user, IAX_CODEC_NOCAP);
13421  ast_set_flag64(user, IAX_CODEC_NOPREFS);
13422  }
13423  } else if (!strcasecmp(v->name, "immediate")) {
13424  ast_set2_flag64(user, ast_true(v->value), IAX_IMMEDIATE);
13425  } else if (!strcasecmp(v->name, "jitterbuffer")) {
13426  ast_set2_flag64(user, ast_true(v->value), IAX_USEJITTERBUF);
13427  } else if (!strcasecmp(v->name, "dbsecret")) {
13428  ast_string_field_set(user, dbsecret, v->value);
13429  } else if (!strcasecmp(v->name, "secret")) {
13430  if (!ast_strlen_zero(user->secret)) {
13431  char *old = ast_strdupa(user->secret);
13432 
13433  ast_string_field_build(user, secret, "%s;%s", old, v->value);
13434  } else
13435  ast_string_field_set(user, secret, v->value);
13436  } else if (!strcasecmp(v->name, "callerid")) {
13437  if (!ast_strlen_zero(v->value) && strcasecmp(v->value, "asreceived")) {
13438  char name2[80];
13439  char num2[80];
13440  ast_callerid_split(v->value, name2, sizeof(name2), num2, sizeof(num2));
13441  ast_string_field_set(user, cid_name, name2);
13442  ast_string_field_set(user, cid_num, num2);
13443  ast_set_flag64(user, IAX_HASCALLERID);
13444  } else {
13445  ast_clear_flag64(user, IAX_HASCALLERID);
13446  ast_string_field_set(user, cid_name, "");
13447  ast_string_field_set(user, cid_num, "");
13448  }
13449  } else if (!strcasecmp(v->name, "fullname")) {
13450  if (!ast_strlen_zero(v->value)) {
13451  ast_string_field_set(user, cid_name, v->value);
13452  ast_set_flag64(user, IAX_HASCALLERID);
13453  } else {
13454  ast_string_field_set(user, cid_name, "");
13455  if (ast_strlen_zero(user->cid_num))
13456  ast_clear_flag64(user, IAX_HASCALLERID);
13457  }
13458  } else if (!strcasecmp(v->name, "cid_number")) {
13459  if (!ast_strlen_zero(v->value)) {
13460  ast_string_field_set(user, cid_num, v->value);
13461  ast_set_flag64(user, IAX_HASCALLERID);
13462  } else {
13463  ast_string_field_set(user, cid_num, "");
13464  if (ast_strlen_zero(user->cid_name))
13465  ast_clear_flag64(user, IAX_HASCALLERID);
13466  }
13467  } else if (!strcasecmp(v->name, "accountcode")) {
13468  ast_string_field_set(user, accountcode, v->value);
13469  } else if (!strcasecmp(v->name, "mohinterpret")) {
13470  ast_string_field_set(user, mohinterpret, v->value);
13471  } else if (!strcasecmp(v->name, "mohsuggest")) {
13472  ast_string_field_set(user, mohsuggest, v->value);
13473  } else if (!strcasecmp(v->name, "parkinglot")) {
13474  ast_string_field_set(user, parkinglot, v->value);
13475  } else if (!strcasecmp(v->name, "language")) {
13476  ast_string_field_set(user, language, v->value);
13477  } else if (!strcasecmp(v->name, "amaflags")) {
13478  format = ast_channel_string2amaflag(v->value);
13479  if (format < 0) {
13480  ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
13481  } else {
13482  user->amaflags = format;
13483  }
13484  } else if (!strcasecmp(v->name, "inkeys")) {
13485  ast_string_field_set(user, inkeys, v->value);
13486  } else if (!strcasecmp(v->name, "maxauthreq")) {
13487  user->maxauthreq = atoi(v->value);
13488  if (user->maxauthreq < 0)
13489  user->maxauthreq = 0;
13490  } else if (!strcasecmp(v->name, "adsi")) {
13491  user->adsi = ast_true(v->value);
13492  } else if (!strcasecmp(v->name, "connectedline")) {
13493  if (ast_true(v->value)) {
13494  ast_set_flag64(user, IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE);
13495  } else if (!strcasecmp(v->value, "send")) {
13496  ast_clear_flag64(user, IAX_RECVCONNECTEDLINE);
13497  ast_set_flag64(user, IAX_SENDCONNECTEDLINE);
13498  } else if (!strcasecmp(v->value, "receive")) {
13499  ast_clear_flag64(user, IAX_SENDCONNECTEDLINE);
13500  ast_set_flag64(user, IAX_RECVCONNECTEDLINE);
13501  } else {
13502  ast_clear_flag64(user, IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE);
13503  }
13504  } else if (!strcasecmp(v->name, "requirecalltoken")) {
13505  /* default is required unless in optional ip list */
13506  if (ast_false(v->value)) {
13508  } else if (!strcasecmp(v->value, "auto")) {
13510  } else if (ast_true(v->value)) {
13512  } else {
13513  ast_log(LOG_WARNING, "requirecalltoken must be set to a valid value. at line %d\n", v->lineno);
13514  }
13515  } /* else if (strcasecmp(v->name,"type")) */
13516  /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
13517  v = v->next;
13518  if (!v) {
13519  v = alt;
13520  alt = NULL;
13521  }
13522  }
13523  if (!user->authmethods) {
13524  if (!ast_strlen_zero(user->secret)) {
13525  user->authmethods = IAX_AUTH_MD5;
13526  if (!ast_strlen_zero(user->inkeys))
13527  user->authmethods |= IAX_AUTH_RSA;
13528  } else if (!ast_strlen_zero(user->inkeys)) {
13529  user->authmethods = IAX_AUTH_RSA;
13530  } else {
13531  user->authmethods = IAX_AUTH_MD5;
13532  }
13533  }
13534  ast_clear_flag64(user, IAX_DELME);
13535  }
13536 cleanup:
13537  if (oldacl) {
13538  ast_free_acl_list(oldacl);
13539  }
13540  if (oldcon) {
13541  free_context(oldcon);
13542  }
13543 
13544  if (subscribe_acl_change) {
13545  acl_change_stasis_subscribe();
13546  }
13547 
13548  return user;
13549 }
13550 
13551 static int peer_delme_cb(void *obj, void *arg, int flags)
13552 {
13553  struct iax2_peer *peer = obj;
13554 
13555  ast_set_flag64(peer, IAX_DELME);
13556 
13557  return 0;
13558 }
13559 
13560 static int user_delme_cb(void *obj, void *arg, int flags)
13561 {
13562  struct iax2_user *user = obj;
13563 
13564  ast_set_flag64(user, IAX_DELME);
13565 
13566  return 0;
13567 }
13568 
13569 static void delete_users(void)
13570 {
13571  struct iax2_registry *reg;
13572 
13573  ao2_callback(users, OBJ_NODATA, user_delme_cb, NULL);
13574 
13575  AST_LIST_LOCK(&registrations);
13576  while ((reg = AST_LIST_REMOVE_HEAD(&registrations, entry))) {
13577  if (sched) {
13578  AST_SCHED_DEL(sched, reg->expire);
13579  }
13580  if (reg->callno) {
13581  int callno = reg->callno;
13582  ast_mutex_lock(&iaxsl[callno]);
13583  if (iaxs[callno]) {
13584  iaxs[callno]->reg = NULL;
13585  iax2_destroy(callno);
13586  }
13587  ast_mutex_unlock(&iaxsl[callno]);
13588  }
13589  if (reg->dnsmgr)
13590  ast_dnsmgr_release(reg->dnsmgr);
13591  ast_free(reg);
13592  }
13593  AST_LIST_UNLOCK(&registrations);
13594 
13595  ao2_callback(peers, OBJ_NODATA, peer_delme_cb, NULL);
13596 }
13597 
13598 static void prune_users(void)
13599 {
13600  struct iax2_user *user;
13601  struct ao2_iterator i;
13602 
13603  i = ao2_iterator_init(users, 0);
13604  while ((user = ao2_iterator_next(&i))) {
13605  if (ast_test_flag64(user, IAX_DELME) || ast_test_flag64(user, IAX_RTCACHEFRIENDS)) {
13606  ao2_unlink(users, user);
13607  }
13608  user_unref(user);
13609  }
13611 }
13612 
13613 /* Prune peers who still are supposed to be deleted */
13614 static void prune_peers(void)
13615 {
13616  struct iax2_peer *peer;
13617  struct ao2_iterator i;
13618 
13619  i = ao2_iterator_init(peers, 0);
13620  while ((peer = ao2_iterator_next(&i))) {
13621  if (ast_test_flag64(peer, IAX_DELME) || ast_test_flag64(peer, IAX_RTCACHEFRIENDS)) {
13622  unlink_peer(peer);
13623  }
13624  peer_unref(peer);
13625  }
13627 }
13628 
13629 static void set_config_destroy(void)
13630 {
13631  strcpy(accountcode, "");
13632  strcpy(language, "");
13633  strcpy(mohinterpret, "");
13634  strcpy(mohsuggest, "");
13635  trunkmaxsize = MAX_TRUNKDATA;
13636  amaflags = 0;
13637  delayreject = 0;
13638  ast_clear_flag64((&globalflags), IAX_NOTRANSFER | IAX_TRANSFERMEDIA | IAX_USEJITTERBUF |
13640  delete_users();
13641  ao2_callback(callno_limits, OBJ_NODATA, addr_range_delme_cb, NULL);
13642  ao2_callback(calltoken_ignores, OBJ_NODATA, addr_range_delme_cb, NULL);
13643 }
13644 
13645 /*! \brief Load configuration */
13646 static int set_config(const char *config_file, int reload, int forced)
13647 {
13648  struct ast_config *cfg, *ucfg;
13649  iax2_format capability;
13650  struct ast_variable *v;
13651  char *cat;
13652  const char *utype;
13653  const char *tosval;
13654  int format;
13655  int portno = IAX_DEFAULT_PORTNO;
13656  int x;
13657  int mtuv;
13658  int subscribe_network_change = 1;
13659  struct iax2_user *user;
13660  struct iax2_peer *peer;
13661  struct ast_netsock *ns;
13662  struct ast_flags config_flags = { (reload && !forced) ? CONFIG_FLAG_FILEUNCHANGED : 0 };
13663  struct ast_sockaddr bindaddr;
13664  struct iax2_codec_pref prefs_new;
13665 
13666  cfg = ast_config_load(config_file, config_flags);
13667 
13668  if (!cfg) {
13669  ast_log(LOG_ERROR, "Unable to load config %s\n", config_file);
13670  return -1;
13671  } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
13672  ucfg = ast_config_load("users.conf", config_flags);
13673  if (ucfg == CONFIG_STATUS_FILEUNCHANGED)
13674  return 0;
13675  /* Otherwise we need to reread both files */
13676  ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
13677  if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEINVALID) {
13678  ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config_file);
13679  ast_config_destroy(ucfg);
13680  return 0;
13681  }
13682  if (!cfg) {
13683  /* should have been able to load the config here */
13684  ast_log(LOG_ERROR, "Unable to load config %s again\n", config_file);
13685  return -1;
13686  }
13687  } else if (cfg == CONFIG_STATUS_FILEINVALID) {
13688  ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config_file);
13689  return 0;
13690  } else { /* iax.conf changed, gotta reread users.conf, too */
13691  ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
13692  if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
13693  ast_log(LOG_ERROR, "Config file users.conf is in an invalid format. Aborting.\n");
13694  ast_config_destroy(cfg);
13695  return 0;
13696  }
13697  }
13698 
13699  if (reload) {
13700  set_config_destroy();
13701  }
13702 
13703  ast_sockaddr_parse(&bindaddr, "0.0.0.0:0", 0);
13704 
13705  /* Setup new codec prefs */
13706  capability = iax2_codec_pref_from_bitfield(&prefs_new, IAX_CAPABILITY_FULLBANDWIDTH);
13707 
13708  /* Reset Global Flags */
13709  memset(&globalflags, 0, sizeof(globalflags));
13710  ast_set_flag64(&globalflags, IAX_RTUPDATE);
13711  ast_set_flag64((&globalflags), IAX_SHRINKCALLERID);
13712 
13713 #ifdef SO_NO_CHECK
13714  nochecksums = 0;
13715 #endif
13716  /* Reset default parking lot */
13717  default_parkinglot[0] = '\0';
13718 
13719  min_reg_expire = IAX_DEFAULT_REG_EXPIRE;
13720  max_reg_expire = IAX_DEFAULT_REG_EXPIRE;
13722  global_maxcallno = DEFAULT_MAXCALLNO_LIMIT;
13723  global_maxcallno_nonval = DEFAULT_MAXCALLNO_LIMIT_NONVAL;
13724 
13725  maxauthreq = 3;
13726 
13727  srvlookup = 0;
13728  iax2_authmethods = 0;
13729 
13730  v = ast_variable_browse(cfg, "general");
13731 
13732  /* Seed initial tos value */
13733  tosval = ast_variable_retrieve(cfg, "general", "tos");
13734  if (tosval) {
13735  if (ast_str2tos(tosval, &qos.tos))
13736  ast_log(LOG_WARNING, "Invalid tos value, refer to QoS documentation\n");
13737  }
13738  /* Seed initial cos value */
13739  tosval = ast_variable_retrieve(cfg, "general", "cos");
13740  if (tosval) {
13741  if (ast_str2cos(tosval, &qos.cos))
13742  ast_log(LOG_WARNING, "Invalid cos value, refer to QoS documentation\n");
13743  }
13744  while(v) {
13745  if (!strcasecmp(v->name, "bindport")) {
13746  if (reload) {
13747  ast_log(LOG_NOTICE, "Ignoring bindport on reload\n");
13748  }
13749  else if (ast_parse_arg(v->value, PARSE_UINT32 | PARSE_IN_RANGE, &portno, 1024, 65535)) {
13750  portno = IAX_DEFAULT_PORTNO;
13751  }
13752  } else if (!strcasecmp(v->name, "pingtime")){
13753  ping_time = atoi(v->value);
13754  }
13755  else if (!strcasecmp(v->name, "iaxthreadcount")) {
13756  if (reload) {
13757  if (atoi(v->value) != iaxthreadcount)
13758  ast_log(LOG_NOTICE, "Ignoring any changes to iaxthreadcount during reload\n");
13759  } else {
13760  iaxthreadcount = atoi(v->value);
13761  if (iaxthreadcount < 1) {
13762  ast_log(LOG_NOTICE, "iaxthreadcount must be at least 1.\n");
13763  iaxthreadcount = 1;
13764  } else if (iaxthreadcount > 256) {
13765  ast_log(LOG_NOTICE, "limiting iaxthreadcount to 256\n");
13766  iaxthreadcount = 256;
13767  }
13768  }
13769  } else if (!strcasecmp(v->name, "iaxmaxthreadcount")) {
13770  if (reload) {
13772  iaxmaxthreadcount = atoi(v->value);
13774  } else {
13775  iaxmaxthreadcount = atoi(v->value);
13776  if (iaxmaxthreadcount < 0) {
13777  ast_log(LOG_NOTICE, "iaxmaxthreadcount must be at least 0.\n");
13778  iaxmaxthreadcount = 0;
13779  } else if (iaxmaxthreadcount > 256) {
13780  ast_log(LOG_NOTICE, "Limiting iaxmaxthreadcount to 256\n");
13781  iaxmaxthreadcount = 256;
13782  }
13783  }
13784  } else if (!strcasecmp(v->name, "nochecksums")) {
13785 #ifdef SO_NO_CHECK
13786  if (ast_true(v->value))
13787  nochecksums = 1;
13788  else
13789  nochecksums = 0;
13790 #else
13791  if (ast_true(v->value))
13792  ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
13793 #endif
13794  }
13795  else if (!strcasecmp(v->name, "maxjitterbuffer"))
13796  maxjitterbuffer = atoi(v->value);
13797  else if (!strcasecmp(v->name, "resyncthreshold"))
13798  resyncthreshold = atoi(v->value);
13799  else if (!strcasecmp(v->name, "maxjitterinterps"))
13800  maxjitterinterps = atoi(v->value);
13801  else if (!strcasecmp(v->name, "jittertargetextra"))
13802  jittertargetextra = atoi(v->value);
13803  else if (!strcasecmp(v->name, "lagrqtime"))
13804  lagrq_time = atoi(v->value);
13805  else if (!strcasecmp(v->name, "maxregexpire"))
13806  max_reg_expire = atoi(v->value);
13807  else if (!strcasecmp(v->name, "minregexpire"))
13808  min_reg_expire = atoi(v->value);
13809  else if (!strcasecmp(v->name, "bindaddr")) {
13810  if (reload) {
13811  ast_log(LOG_NOTICE, "Ignoring bindaddr on reload\n");
13812  } else {
13813 
13814  if (!ast_parse_arg(v->value, PARSE_ADDR, NULL)) {
13815 
13816  ast_sockaddr_parse(&bindaddr, v->value, 0);
13817 
13818  if (!ast_sockaddr_port(&bindaddr)) {
13819  ast_sockaddr_set_port(&bindaddr, portno);
13820  }
13821 
13822  if (!(ns = ast_netsock_bindaddr(netsock, io, &bindaddr, qos.tos, qos.cos, socket_read, NULL))) {
13823  ast_log(LOG_WARNING, "Unable to apply binding to '%s' at line %d\n", v->value, v->lineno);
13824  } else {
13825  ast_verb(2, "Binding IAX2 to address %s\n", ast_sockaddr_stringify(&bindaddr));
13826 
13827  if (defaultsockfd < 0) {
13828  defaultsockfd = ast_netsock_sockfd(ns);
13829  }
13830  ast_netsock_unref(ns);
13831  }
13832 
13833  } else {
13834  ast_log(LOG_WARNING, "Invalid address '%s' specified, at line %d\n", v->value, v->lineno);
13835  }
13836  }
13837  } else if (!strcasecmp(v->name, "auth")) {
13838  iax2_authmethods = get_auth_methods(v->value);
13839  if (iax2_authmethods & IAX_AUTH_PLAINTEXT) {
13840  ast_log(LOG_WARNING, "Default auth method is set to deprecated 'plaintext' at line %d of iax.conf\n", v->lineno);
13841  }
13842  } else if (!strcasecmp(v->name, "authdebug")) {
13843  authdebug = ast_true(v->value);
13844  } else if (!strcasecmp(v->name, "encryption")) {
13845  iax2_encryption |= get_encrypt_methods(v->value);
13846  if (!iax2_encryption) {
13847  ast_clear_flag64((&globalflags), IAX_FORCE_ENCRYPT);
13848  }
13849  } else if (!strcasecmp(v->name, "forceencryption")) {
13850  if (ast_false(v->value)) {
13851  ast_clear_flag64((&globalflags), IAX_FORCE_ENCRYPT);
13852  } else {
13853  iax2_encryption |= get_encrypt_methods(v->value);
13854  if (iax2_encryption) {
13855  ast_set_flag64((&globalflags), IAX_FORCE_ENCRYPT);
13856  }
13857  }
13858  } else if (!strcasecmp(v->name, "transfer")) {
13859  if (!strcasecmp(v->value, "mediaonly")) {
13860  ast_set_flags_to64((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_TRANSFERMEDIA);
13861  } else if (ast_true(v->value)) {
13862  ast_set_flags_to64((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, 0);
13863  } else
13864  ast_set_flags_to64((&globalflags), IAX_NOTRANSFER|IAX_TRANSFERMEDIA, IAX_NOTRANSFER);
13865  } else if (!strcasecmp(v->name, "codecpriority")) {
13866  if(!strcasecmp(v->value, "caller"))
13867  ast_set_flag64((&globalflags), IAX_CODEC_USER_FIRST);
13868  else if(!strcasecmp(v->value, "disabled"))
13869  ast_set_flag64((&globalflags), IAX_CODEC_NOPREFS);
13870  else if(!strcasecmp(v->value, "reqonly")) {
13871  ast_set_flag64((&globalflags), IAX_CODEC_NOCAP);
13872  ast_set_flag64((&globalflags), IAX_CODEC_NOPREFS);
13873  }
13874  } else if (!strcasecmp(v->name, "jitterbuffer"))
13875  ast_set2_flag64((&globalflags), ast_true(v->value), IAX_USEJITTERBUF);
13876  else if (!strcasecmp(v->name, "delayreject"))
13877  delayreject = ast_true(v->value);
13878  else if (!strcasecmp(v->name, "allowfwdownload"))
13879  ast_set2_flag64((&globalflags), ast_true(v->value), IAX_ALLOWFWDOWNLOAD);
13880  else if (!strcasecmp(v->name, "rtcachefriends"))
13881  ast_set2_flag64((&globalflags), ast_true(v->value), IAX_RTCACHEFRIENDS);
13882  else if (!strcasecmp(v->name, "rtignoreregexpire"))
13883  ast_set2_flag64((&globalflags), ast_true(v->value), IAX_RTIGNOREREGEXPIRE);
13884  else if (!strcasecmp(v->name, "rtupdate"))
13885  ast_set2_flag64((&globalflags), ast_true(v->value), IAX_RTUPDATE);
13886  else if (!strcasecmp(v->name, "rtsavesysname"))
13887  ast_set2_flag64((&globalflags), ast_true(v->value), IAX_RTSAVE_SYSNAME);
13888  else if (!strcasecmp(v->name, "trunktimestamps"))
13889  ast_set2_flag64(&globalflags, ast_true(v->value), IAX_TRUNKTIMESTAMPS);
13890  else if (!strcasecmp(v->name, "rtautoclear")) {
13891  int i = atoi(v->value);
13892  if(i > 0)
13893  global_rtautoclear = i;
13894  else
13895  i = 0;
13896  ast_set2_flag64((&globalflags), i || ast_true(v->value), IAX_RTAUTOCLEAR);
13897  } else if (!strcasecmp(v->name, "trunkfreq")) {
13898  trunkfreq = atoi(v->value);
13899  if (trunkfreq < 10) {
13900  ast_log(LOG_NOTICE, "trunkfreq must be between 10ms and 1000ms, using 10ms instead.\n");
13901  trunkfreq = 10;
13902  } else if (trunkfreq > 1000) {
13903  ast_log(LOG_NOTICE, "trunkfreq must be between 10ms and 1000ms, using 1000ms instead.\n");
13904  trunkfreq = 1000;
13905  }
13906  if (timer) {
13907  ast_timer_set_rate(timer, 1000 / trunkfreq);
13908  }
13909  } else if (!strcasecmp(v->name, "trunkmtu")) {
13910  mtuv = atoi(v->value);
13911  if (mtuv == 0 )
13913  else if (mtuv >= 172 && mtuv < 4000)
13914  global_max_trunk_mtu = mtuv;
13915  else
13916  ast_log(LOG_NOTICE, "trunkmtu value out of bounds (%d) at line %d\n",
13917  mtuv, v->lineno);
13918  } else if (!strcasecmp(v->name, "trunkmaxsize")) {
13919  trunkmaxsize = atoi(v->value);
13920  if (trunkmaxsize == 0)
13921  trunkmaxsize = MAX_TRUNKDATA;
13922  } else if (!strcasecmp(v->name, "autokill")) {
13923  if (sscanf(v->value, "%30d", &x) == 1) {
13924  if (x >= 0)
13925  autokill = x;
13926  else
13927  ast_log(LOG_NOTICE, "Nice try, but autokill has to be >0 or 'yes' or 'no' at line %d\n", v->lineno);
13928  } else if (ast_true(v->value)) {
13929  autokill = DEFAULT_MAXMS;
13930  } else {
13931  autokill = 0;
13932  }
13933  } else if (!strcasecmp(v->name, "bandwidth")) {
13934  if (!strcasecmp(v->value, "low")) {
13935  capability = iax2_codec_pref_from_bitfield(&prefs_new,
13936  IAX_CAPABILITY_LOWBANDWIDTH);
13937  } else if (!strcasecmp(v->value, "medium")) {
13938  capability = iax2_codec_pref_from_bitfield(&prefs_new,
13939  IAX_CAPABILITY_MEDBANDWIDTH);
13940  } else if (!strcasecmp(v->value, "high")) {
13941  capability = iax2_codec_pref_from_bitfield(&prefs_new,
13942  IAX_CAPABILITY_FULLBANDWIDTH);
13943  } else {
13944  ast_log(LOG_WARNING, "bandwidth must be either low, medium, or high\n");
13945  }
13946  } else if (!strcasecmp(v->name, "allow")) {
13947  iax2_parse_allow_disallow(&prefs_new, &capability, v->value, 1);
13948  } else if (!strcasecmp(v->name, "disallow")) {
13949  iax2_parse_allow_disallow(&prefs_new, &capability, v->value, 0);
13950  } else if (!strcasecmp(v->name, "register")) {
13951  iax2_register(v->value, v->lineno);
13952  } else if (!strcasecmp(v->name, "iaxcompat")) {
13953  iaxcompat = ast_true(v->value);
13954  } else if (!strcasecmp(v->name, "regcontext")) {
13955  ast_copy_string(regcontext, v->value, sizeof(regcontext));
13956  /* Create context if it doesn't exist already */
13957  ast_context_find_or_create(NULL, NULL, regcontext, "IAX2");
13958  } else if (!strcasecmp(v->name, "tos")) {
13959  if (ast_str2tos(v->value, &qos.tos))
13960  ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
13961  } else if (!strcasecmp(v->name, "cos")) {
13962  if (ast_str2cos(v->value, &qos.cos))
13963  ast_log(LOG_WARNING, "Invalid cos value at line %d, refer to QoS documentation\n", v->lineno);
13964  } else if (!strcasecmp(v->name, "parkinglot")) {
13965  ast_copy_string(default_parkinglot, v->value, sizeof(default_parkinglot));
13966  } else if (!strcasecmp(v->name, "accountcode")) {
13967  ast_copy_string(accountcode, v->value, sizeof(accountcode));
13968  } else if (!strcasecmp(v->name, "mohinterpret")) {
13969  ast_copy_string(mohinterpret, v->value, sizeof(mohinterpret));
13970  } else if (!strcasecmp(v->name, "mohsuggest")) {
13971  ast_copy_string(mohsuggest, v->value, sizeof(mohsuggest));
13972  } else if (!strcasecmp(v->name, "amaflags")) {
13973  format = ast_channel_string2amaflag(v->value);
13974  if (format < 0) {
13975  ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
13976  } else {
13977  amaflags = format;
13978  }
13979  } else if (!strcasecmp(v->name, "language")) {
13980  ast_copy_string(language, v->value, sizeof(language));
13981  } else if (!strcasecmp(v->name, "maxauthreq")) {
13982  maxauthreq = atoi(v->value);
13983  if (maxauthreq < 0)
13984  maxauthreq = 0;
13985  } else if (!strcasecmp(v->name, "adsi")) {
13986  adsi = ast_true(v->value);
13987  } else if (!strcasecmp(v->name, "srvlookup")) {
13988  srvlookup = ast_true(v->value);
13989  } else if (!strcasecmp(v->name, "connectedline")) {
13990  if (ast_true(v->value)) {
13991  ast_set_flag64((&globalflags), IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE);
13992  } else if (!strcasecmp(v->value, "send")) {
13993  ast_clear_flag64((&globalflags), IAX_RECVCONNECTEDLINE);
13994  ast_set_flag64((&globalflags), IAX_SENDCONNECTEDLINE);
13995  } else if (!strcasecmp(v->value, "receive")) {
13996  ast_clear_flag64((&globalflags), IAX_SENDCONNECTEDLINE);
13997  ast_set_flag64((&globalflags), IAX_RECVCONNECTEDLINE);
13998  } else {
13999  ast_clear_flag64((&globalflags), IAX_SENDCONNECTEDLINE | IAX_RECVCONNECTEDLINE);
14000  }
14001  } else if (!strcasecmp(v->name, "maxcallnumbers")) {
14002  if (sscanf(v->value, "%10hu", &global_maxcallno) != 1) {
14003  ast_log(LOG_WARNING, "maxcallnumbers must be set to a valid number. %s is not valid at line %d\n", v->value, v->lineno);
14004  }
14005  } else if (!strcasecmp(v->name, "maxcallnumbers_nonvalidated")) {
14006  if (sscanf(v->value, "%10hu", &global_maxcallno_nonval) != 1) {
14007  ast_log(LOG_WARNING, "maxcallnumbers_nonvalidated must be set to a valid number. %s is not valid at line %d.\n", v->value, v->lineno);
14008  }
14009  } else if (!strcasecmp(v->name, "calltokenoptional")) {
14010  if (add_calltoken_ignore(v->value)) {
14011  ast_log(LOG_WARNING, "Invalid calltokenoptional address range - '%s' line %d\n", v->value, v->lineno);
14012  return -1;
14013  }
14014  } else if (!strcasecmp(v->name, "calltokenexpiration")) {
14015  int temp = -1;
14016  sscanf(v->value, "%u", &temp);
14017  if( temp <= 0 ){
14018  ast_log(LOG_WARNING, "Invalid calltokenexpiration value %s. Should be integer greater than 0.\n", v->value);
14019  } else {
14020  max_calltoken_delay = temp;
14021  }
14022  } else if (!strcasecmp(v->name, "subscribe_network_change_event")) {
14023  if (ast_true(v->value)) {
14024  subscribe_network_change = 1;
14025  } else if (ast_false(v->value)) {
14026  subscribe_network_change = 0;
14027  } else {
14028  ast_log(LOG_WARNING, "subscribe_network_change_event value %s is not valid at line %d.\n", v->value, v->lineno);
14029  }
14030  } else if (!strcasecmp(v->name, "shrinkcallerid")) {
14031  if (ast_true(v->value)) {
14032  ast_set_flag64((&globalflags), IAX_SHRINKCALLERID);
14033  } else if (ast_false(v->value)) {
14034  ast_clear_flag64((&globalflags), IAX_SHRINKCALLERID);
14035  } else {
14036  ast_log(LOG_WARNING, "shrinkcallerid value %s is not valid at line %d.\n", v->value, v->lineno);
14037  }
14038  }/*else if (strcasecmp(v->name,"type")) */
14039  /* ast_log(LOG_WARNING, "Ignoring %s\n", v->name); */
14040  v = v->next;
14041  }
14042 
14043  if (subscribe_network_change) {
14044  network_change_stasis_subscribe();
14045  } else {
14046  network_change_stasis_unsubscribe();
14047  }
14048 
14049  if (defaultsockfd < 0) {
14050 
14051  ast_sockaddr_set_port(&bindaddr, portno);
14052 
14053  if (!(ns = ast_netsock_bindaddr(netsock, io, &bindaddr, qos.tos, qos.cos, socket_read, NULL))) {
14054  ast_log(LOG_ERROR, "Unable to create network socket: %s\n", strerror(errno));
14055  } else {
14056  ast_verb(2, "Binding IAX2 to default address %s\n", ast_sockaddr_stringify(&bindaddr));
14057  defaultsockfd = ast_netsock_sockfd(ns);
14058  ast_netsock_unref(ns);
14059  }
14060  }
14061  if (reload) {
14062  ast_netsock_release(outsock);
14063  outsock = ast_netsock_list_alloc();
14064  if (!outsock) {
14065  ast_log(LOG_ERROR, "Could not allocate outsock list.\n");
14066  return -1;
14067  }
14068  ast_netsock_init(outsock);
14069  }
14070 
14071  if (min_reg_expire > max_reg_expire) {
14072  ast_log(LOG_WARNING, "Minimum registration interval of %d is more than maximum of %d, resetting minimum to %d\n",
14073  min_reg_expire, max_reg_expire, max_reg_expire);
14074  min_reg_expire = max_reg_expire;
14075  }
14076  prefs_global = prefs_new;
14077  iax2_capability = capability;
14078 
14079  if (ucfg) {
14080  struct ast_variable *gen;
14081  int genhasiax;
14082  int genregisteriax;
14083  const char *hasiax, *registeriax;
14084 
14085  genhasiax = ast_true(ast_variable_retrieve(ucfg, "general", "hasiax"));
14086  genregisteriax = ast_true(ast_variable_retrieve(ucfg, "general", "registeriax"));
14087  gen = ast_variable_browse(ucfg, "general");
14088  cat = ast_category_browse(ucfg, NULL);
14089  while (cat) {
14090  if (strcasecmp(cat, "general")) {
14091  hasiax = ast_variable_retrieve(ucfg, cat, "hasiax");
14092  registeriax = ast_variable_retrieve(ucfg, cat, "registeriax");
14093  if (ast_true(hasiax) || (!hasiax && genhasiax)) {
14094  /* Start with general parameters, then specific parameters, user and peer */
14095  user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
14096  if (user) {
14097  ao2_link(users, user);
14098  user = user_unref(user);
14099  }
14100  peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
14101  if (peer) {
14102  if (ast_test_flag64(peer, IAX_DYNAMIC)) {
14103  reg_source_db(peer);
14104  }
14105  ao2_link(peers, peer);
14106  peer = peer_unref(peer);
14107  }
14108  }
14109  if (ast_true(registeriax) || (!registeriax && genregisteriax)) {
14110  char tmp[256];
14111  const char *host = ast_variable_retrieve(ucfg, cat, "host");
14112  const char *username = ast_variable_retrieve(ucfg, cat, "username");
14113  const char *secret = ast_variable_retrieve(ucfg, cat, "secret");
14114  if (!host)
14115  host = ast_variable_retrieve(ucfg, "general", "host");
14116  if (!username)
14117  username = ast_variable_retrieve(ucfg, "general", "username");
14118  if (!secret)
14119  secret = ast_variable_retrieve(ucfg, "general", "secret");
14120  if (!ast_strlen_zero(username) && !ast_strlen_zero(host)) {
14121  if (!ast_strlen_zero(secret))
14122  snprintf(tmp, sizeof(tmp), "%s:%s@%s", username, secret, host);
14123  else
14124  snprintf(tmp, sizeof(tmp), "%s@%s", username, host);
14125  iax2_register(tmp, 0);
14126  }
14127  }
14128  }
14129  cat = ast_category_browse(ucfg, cat);
14130  }
14131  ast_config_destroy(ucfg);
14132  }
14133 
14134  cat = ast_category_browse(cfg, NULL);
14135  while(cat) {
14136  if (strcasecmp(cat, "general")) {
14137  utype = ast_variable_retrieve(cfg, cat, "type");
14138  if (!strcasecmp(cat, "callnumberlimits")) {
14139  build_callno_limits(ast_variable_browse(cfg, cat));
14140  } else if (utype) {
14141  if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
14142  user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
14143  if (user) {
14144  ao2_link(users, user);
14145  user = user_unref(user);
14146  }
14147  }
14148  if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
14149  peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
14150  if (peer) {
14151  if (ast_test_flag64(peer, IAX_DYNAMIC))
14152  reg_source_db(peer);
14153  ao2_link(peers, peer);
14154  peer = peer_unref(peer);
14155  }
14156  } else if (strcasecmp(utype, "user")) {
14157  ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config_file);
14158  }
14159  } else
14160  ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
14161  }
14162  cat = ast_category_browse(cfg, cat);
14163  }
14164  ast_config_destroy(cfg);
14165  return 1;
14166 }
14167 
14168 static void poke_all_peers(void)
14169 {
14170  struct ao2_iterator i;
14171  struct iax2_peer *peer;
14172 
14173  i = ao2_iterator_init(peers, 0);
14174  while ((peer = ao2_iterator_next(&i))) {
14175  iax2_poke_peer(peer, 0);
14176  peer_unref(peer);
14177  }
14179 }
14180 static int reload_config(int forced_reload)
14181 {
14182  static const char config[] = "iax.conf";
14183  struct iax2_registry *reg;
14184 
14185  if (set_config(config, 1, forced_reload) > 0) {
14186  prune_peers();
14187  prune_users();
14188  ao2_callback(callno_limits, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, prune_addr_range_cb, NULL);
14189  ao2_callback(calltoken_ignores, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, prune_addr_range_cb, NULL);
14190  ao2_callback(peercnts, OBJ_NODATA, set_peercnt_limit_all_cb, NULL);
14191  trunk_timed = trunk_untimed = 0;
14192  trunk_nmaxmtu = trunk_maxmtu = 0;
14193  memset(&debugaddr, '\0', sizeof(debugaddr));
14194 
14195  AST_LIST_LOCK(&registrations);
14196  AST_LIST_TRAVERSE(&registrations, reg, entry)
14197  iax2_do_register(reg);
14198  AST_LIST_UNLOCK(&registrations);
14199 
14200  /* Qualify hosts, too */
14201  poke_all_peers();
14202  }
14203 
14204  iax_firmware_reload();
14205  iax_provision_reload(1);
14206  ast_unload_realtime("iaxpeers");
14207 
14208  return 0;
14209 }
14210 
14211 static char *handle_cli_iax2_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14212 {
14213  switch (cmd) {
14214  case CLI_INIT:
14215  e->command = "iax2 reload";
14216  e->usage =
14217  "Usage: iax2 reload\n"
14218  " Reloads IAX configuration from iax.conf\n";
14219  return NULL;
14220  case CLI_GENERATE:
14221  return NULL;
14222  }
14223 
14224  reload_config(0);
14225 
14226  return CLI_SUCCESS;
14227 }
14228 
14229 static int reload(void)
14230 {
14231  return reload_config(0);
14232 }
14233 
14234 static int cache_get_callno_locked(const char *data)
14235 {
14236  struct ast_sockaddr addr;
14237  int x;
14238  int callno;
14239  struct iax_ie_data ied;
14240  struct create_addr_info cai;
14241  struct parsed_dial_string pds;
14242  char *tmpstr;
14243 
14244  for (x = 0; x < ARRAY_LEN(iaxs); x++) {
14245  /* Look for an *exact match* call. Once a call is negotiated, it can only
14246  look up entries for a single context */
14247  if (!ast_mutex_trylock(&iaxsl[x])) {
14248  if (iaxs[x] && !strcasecmp(data, iaxs[x]->dproot))
14249  return x;
14250  ast_mutex_unlock(&iaxsl[x]);
14251  }
14252  }
14253 
14254  /* No match found, we need to create a new one */
14255 
14256  memset(&cai, 0, sizeof(cai));
14257  memset(&ied, 0, sizeof(ied));
14258  memset(&pds, 0, sizeof(pds));
14259 
14260  tmpstr = ast_strdupa(data);
14261  parse_dial_string(tmpstr, &pds);
14262 
14263  if (ast_strlen_zero(pds.peer)) {
14264  ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", data);
14265  return -1;
14266  }
14267 
14268  /* Populate our address from the given */
14269  if (create_addr(pds.peer, NULL, &addr, &cai))
14270  return -1;
14271 
14272  ast_debug(1, "peer: %s, username: %s, password: %s, context: %s\n",
14273  pds.peer, pds.username, pds.password, pds.context);
14274 
14275  callno = find_callno_locked(0, 0, &addr, NEW_FORCE, cai.sockfd, 0);
14276  if (callno < 1) {
14277  ast_log(LOG_WARNING, "Unable to create call\n");
14278  return -1;
14279  }
14280 
14281  ast_string_field_set(iaxs[callno], dproot, data);
14282  iaxs[callno]->capability = IAX_CAPABILITY_FULLBANDWIDTH;
14283 
14284  iax_ie_append_short(&ied, IAX_IE_VERSION, IAX_PROTO_VERSION);
14285  iax_ie_append_str(&ied, IAX_IE_CALLED_NUMBER, "TBD");
14286  /* the string format is slightly different from a standard dial string,
14287  because the context appears in the 'exten' position
14288  */
14289  if (pds.exten)
14290  iax_ie_append_str(&ied, IAX_IE_CALLED_CONTEXT, pds.exten);
14291  if (pds.username)
14292  iax_ie_append_str(&ied, IAX_IE_USERNAME, pds.username);
14293  iax_ie_append_int(&ied, IAX_IE_FORMAT, IAX_CAPABILITY_FULLBANDWIDTH);
14294  iax_ie_append_int(&ied, IAX_IE_CAPABILITY, IAX_CAPABILITY_FULLBANDWIDTH);
14295  /* Keep password handy */
14296  if (pds.password)
14297  ast_string_field_set(iaxs[callno], secret, pds.password);
14298  if (pds.key)
14299  ast_string_field_set(iaxs[callno], outkey, pds.key);
14300  /* Start the call going */
14301  add_empty_calltoken_ie(iaxs[callno], &ied); /* this _MUST_ be the last ie added */
14302  send_command(iaxs[callno], AST_FRAME_IAX, IAX_COMMAND_NEW, 0, ied.buf, ied.pos, -1);
14303 
14304  return callno;
14305 }
14306 
14307 static struct iax2_dpcache *find_cache(struct ast_channel *chan, const char *data, const char *context, const char *exten, int priority)
14308 {
14309  struct iax2_dpcache *dp = NULL;
14310  struct timeval now = ast_tvnow();
14311  int x, com[2], timeout, doabort, callno;
14312 
14313  AST_LIST_TRAVERSE_SAFE_BEGIN(&dpcache, dp, cache_list) {
14314  if (ast_tvcmp(now, dp->expiry) > 0) {
14315  AST_LIST_REMOVE_CURRENT(cache_list);
14316  if ((dp->flags & CACHE_FLAG_PENDING) || dp->callno)
14317  ast_log(LOG_WARNING, "DP still has peer field or pending or callno (flags = %d, peer = blah, callno = %d)\n", dp->flags, dp->callno);
14318  else
14319  ast_free(dp);
14320  continue;
14321  }
14322  if (!strcmp(dp->peercontext, data) && !strcmp(dp->exten, exten))
14323  break;
14324  }
14326 
14327  if (!dp) {
14328  /* No matching entry. Create a new one. */
14329  /* First, can we make a callno? */
14330  if ((callno = cache_get_callno_locked(data)) < 0) {
14331  ast_log(LOG_WARNING, "Unable to generate call for '%s'\n", data);
14332  return NULL;
14333  }
14334  if (!(dp = ast_calloc(1, sizeof(*dp)))) {
14335  ast_mutex_unlock(&iaxsl[callno]);
14336  return NULL;
14337  }
14338  ast_copy_string(dp->peercontext, data, sizeof(dp->peercontext));
14339  ast_copy_string(dp->exten, exten, sizeof(dp->exten));
14340  dp->expiry = ast_tvnow();
14341  dp->orig = dp->expiry;
14342  /* Expires in 30 mins by default */
14343  dp->expiry.tv_sec += iaxdefaultdpcache;
14344  dp->flags = CACHE_FLAG_PENDING;
14345  for (x = 0; x < ARRAY_LEN(dp->waiters); x++)
14346  dp->waiters[x] = -1;
14347  /* Insert into the lists */
14348  AST_LIST_INSERT_TAIL(&dpcache, dp, cache_list);
14349  AST_LIST_INSERT_TAIL(&iaxs[callno]->dpentries, dp, peer_list);
14350  /* Send the request if we're already up */
14351  if (ast_test_flag(&iaxs[callno]->state, IAX_STATE_STARTED))
14352  iax2_dprequest(dp, callno);
14353  ast_mutex_unlock(&iaxsl[callno]);
14354  }
14355 
14356  /* By here we must have a dp */
14357  if (dp->flags & CACHE_FLAG_PENDING) {
14358  int res;
14359  struct pollfd pfd;
14360  /* Okay, here it starts to get nasty. We need a pipe now to wait
14361  for a reply to come back so long as it's pending */
14362  for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
14363  /* Find an empty slot */
14364  if (dp->waiters[x] < 0)
14365  break;
14366  }
14367  if (x >= ARRAY_LEN(dp->waiters)) {
14368  ast_log(LOG_WARNING, "No more waiter positions available\n");
14369  return NULL;
14370  }
14371  if (pipe(com)) {
14372  ast_log(LOG_WARNING, "Unable to create pipe for comm\n");
14373  return NULL;
14374  }
14375  dp->waiters[x] = com[1];
14376  /* Okay, now we wait */
14377  timeout = iaxdefaulttimeout * 1000;
14378  /* Temporarily unlock */
14380  doabort = 0;
14381 
14382  /* chan is in autoservice here, so do NOT service it here! */
14383  pfd.fd = com[0];
14384  pfd.events = POLLIN;
14385  pfd.revents = 0;
14386  /* Wait for pipe activity... if the channel hangs up, we'll catch it on the way out. */
14387  res = ast_poll(&pfd, 1, timeout);
14388  if (res < 0) {
14389  ast_log(LOG_WARNING, "poll returned < 0: %s\n", strerror(errno));
14390  return NULL;
14391  } else if (!pfd.revents) {
14392  ast_log(LOG_WARNING, "Timeout waiting for %s exten %s\n", data, exten);
14393  }
14394 
14395  if (chan && ast_check_hangup(chan)) {
14396  doabort = 1;
14397  }
14398 
14400  dp->waiters[x] = -1;
14401  close(com[1]);
14402  close(com[0]);
14403  if (doabort) {
14404  /* Don't interpret anything, just abort. */
14405  return NULL;
14406  }
14407  if (!(dp->flags & CACHE_FLAG_TIMEOUT)) {
14408  /* Now to do non-independent analysis the results of our wait */
14409  if (dp->flags & CACHE_FLAG_PENDING) {
14410  /* Still pending... It's a timeout. Wake everybody up. Consider it no longer
14411  pending. Don't let it take as long to timeout. */
14412  dp->flags &= ~CACHE_FLAG_PENDING;
14413  dp->flags |= CACHE_FLAG_TIMEOUT;
14414  /* Expire after only 60 seconds now. This is designed to help reduce backlog in heavily loaded
14415  systems without leaving it unavailable once the server comes back online */
14416  dp->expiry.tv_sec = dp->orig.tv_sec + 60;
14417  for (x = 0; x < ARRAY_LEN(dp->waiters); x++) {
14418  if (dp->waiters[x] > -1) {
14419  if (write(dp->waiters[x], "asdf", 4) < 0) {
14420  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
14421  }
14422  }
14423  }
14424  }
14425  }
14426  /* Our caller will obtain the rest */
14427  }
14428  return dp;
14429 }
14430 
14431 /*! \brief Part of the IAX2 switch interface */
14432 static int iax2_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
14433 {
14434  int res = 0;
14435  struct iax2_dpcache *dp = NULL;
14436 #if 0
14437  ast_log(LOG_NOTICE, "iax2_exists: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
14438 #endif
14439  if ((priority != 1) && (priority != 2))
14440  return 0;
14441 
14443  if ((dp = find_cache(chan, data, context, exten, priority))) {
14444  if (dp->flags & CACHE_FLAG_EXISTS)
14445  res = 1;
14446  } else {
14447  ast_log(LOG_WARNING, "Unable to make DP cache\n");
14448  }
14450 
14451  return res;
14452 }
14453 
14454 /*! \brief part of the IAX2 dial plan switch interface */
14455 static int iax2_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
14456 {
14457  int res = 0;
14458  struct iax2_dpcache *dp = NULL;
14459 #if 0
14460  ast_log(LOG_NOTICE, "iax2_canmatch: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
14461 #endif
14462  if ((priority != 1) && (priority != 2))
14463  return 0;
14464 
14466  if ((dp = find_cache(chan, data, context, exten, priority))) {
14467  if (dp->flags & CACHE_FLAG_CANEXIST)
14468  res = 1;
14469  } else {
14470  ast_log(LOG_WARNING, "Unable to make DP cache\n");
14471  }
14473 
14474  return res;
14475 }
14476 
14477 /*! \brief Part of the IAX2 Switch interface */
14478 static int iax2_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
14479 {
14480  int res = 0;
14481  struct iax2_dpcache *dp = NULL;
14482 #if 0
14483  ast_log(LOG_NOTICE, "iax2_matchmore: con: %s, exten: %s, pri: %d, cid: %s, data: %s\n", context, exten, priority, callerid ? callerid : "<unknown>", data);
14484 #endif
14485  if ((priority != 1) && (priority != 2))
14486  return 0;
14487 
14489  if ((dp = find_cache(chan, data, context, exten, priority))) {
14490  if (dp->flags & CACHE_FLAG_MATCHMORE)
14491  res = 1;
14492  } else {
14493  ast_log(LOG_WARNING, "Unable to make DP cache\n");
14494  }
14496 
14497  return res;
14498 }
14499 
14500 /*! \brief Execute IAX2 dialplan switch */
14501 static int iax2_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
14502 {
14503  char odata[256];
14504  char req[sizeof(odata) + AST_MAX_CONTEXT + AST_MAX_EXTENSION + sizeof("IAX2//@")];
14505  char *ncontext;
14506  struct iax2_dpcache *dp = NULL;
14507  struct ast_app *dial = NULL;
14508 #if 0
14509  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);
14510 #endif
14511  if (priority == 2) {
14512  /* Indicate status, can be overridden in dialplan */
14513  const char *dialstatus = pbx_builtin_getvar_helper(chan, "DIALSTATUS");
14514  if (dialstatus) {
14515  dial = pbx_findapp(dialstatus);
14516  if (dial)
14517  pbx_exec(chan, dial, "");
14518  }
14519  return -1;
14520  } else if (priority != 1)
14521  return -1;
14522 
14524  if ((dp = find_cache(chan, data, context, exten, priority))) {
14525  if (dp->flags & CACHE_FLAG_EXISTS) {
14526  ast_copy_string(odata, data, sizeof(odata));
14527  ncontext = strchr(odata, '/');
14528  if (ncontext) {
14529  *ncontext = '\0';
14530  ncontext++;
14531  snprintf(req, sizeof(req), "IAX2/%s/%s@%s", odata, exten, ncontext);
14532  } else {
14533  snprintf(req, sizeof(req), "IAX2/%s/%s", odata, exten);
14534  }
14535  ast_verb(3, "Executing Dial('%s')\n", req);
14536  } else {
14538  ast_log(LOG_WARNING, "Can't execute nonexistent extension '%s[@%s]' in data '%s'\n", exten, context, data);
14539  return -1;
14540  }
14541  }
14543 
14544  if ((dial = pbx_findapp("Dial")))
14545  return pbx_exec(chan, dial, req);
14546  else
14547  ast_log(LOG_WARNING, "No dial application registered\n");
14548 
14549  return -1;
14550 }
14551 
14552 static int function_iaxpeer(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
14553 {
14554  struct iax2_peer *peer;
14555  char *peername, *colname;
14556 
14557  peername = ast_strdupa(data);
14558 
14559  /* if our channel, return the IP address of the endpoint of current channel */
14560  if (!strcmp(peername,"CURRENTCHANNEL")) {
14561  unsigned short callno;
14562  if (!chan || ast_channel_tech(chan) != &iax2_tech) {
14563  return -1;
14564  }
14565  callno = PTR_TO_CALLNO(ast_channel_tech_pvt(chan));
14566  ast_copy_string(buf, !ast_sockaddr_isnull(&iaxs[callno]->addr) ? ast_sockaddr_stringify_addr(&iaxs[callno]->addr) : "", len);
14567  return 0;
14568  }
14569 
14570  if ((colname = strchr(peername, ',')))
14571  *colname++ = '\0';
14572  else
14573  colname = "ip";
14574 
14575  if (!(peer = find_peer(peername, 1)))
14576  return -1;
14577 
14578  if (!strcasecmp(colname, "ip")) {
14579  ast_copy_string(buf, ast_sockaddr_stringify_addr(&peer->addr), len);
14580  } else if (!strcasecmp(colname, "status")) {
14581  peer_status(peer, buf, len);
14582  } else if (!strcasecmp(colname, "mailbox")) {
14583  ast_copy_string(buf, peer->mailbox, len);
14584  } else if (!strcasecmp(colname, "context")) {
14585  ast_copy_string(buf, peer->context, len);
14586  } else if (!strcasecmp(colname, "expire")) {
14587  snprintf(buf, len, "%d", peer->expire);
14588  } else if (!strcasecmp(colname, "dynamic")) {
14589  ast_copy_string(buf, (ast_test_flag64(peer, IAX_DYNAMIC) ? "yes" : "no"), len);
14590  } else if (!strcasecmp(colname, "callerid_name")) {
14591  ast_copy_string(buf, peer->cid_name, len);
14592  } else if (!strcasecmp(colname, "callerid_num")) {
14593  ast_copy_string(buf, peer->cid_num, len);
14594  } else if (!strcasecmp(colname, "codecs")) {
14595  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
14596 
14597  iax2_getformatname_multiple(peer->capability, &codec_buf);
14598  ast_copy_string(buf, ast_str_buffer(codec_buf), len);
14599  } else if (!strncasecmp(colname, "codec[", 6)) {
14600  char *codecnum, *ptr;
14601  struct ast_format *tmpfmt;
14602 
14603  /* skip over "codec" to the '[' */
14604  codecnum = colname + 5;
14605  *codecnum = '\0';
14606  codecnum++;
14607  if ((ptr = strchr(codecnum, ']'))) {
14608  *ptr = '\0';
14609  }
14610  if((iax2_codec_pref_index(&peer->prefs, atoi(codecnum), &tmpfmt))) {
14611  ast_copy_string(buf, ast_format_get_name(tmpfmt), len);
14612  } else {
14613  buf[0] = '\0';
14614  }
14615  } else {
14616  buf[0] = '\0';
14617  }
14618 
14619  peer_unref(peer);
14620 
14621  return 0;
14622 }
14623 
14624 static struct ast_custom_function iaxpeer_function = {
14625  .name = "IAXPEER",
14626  .read = function_iaxpeer,
14627 };
14628 
14629 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen)
14630 {
14631  struct chan_iax2_pvt *pvt;
14632  unsigned int callno;
14633  int res = 0;
14634 
14635  if (!chan || ast_channel_tech(chan) != &iax2_tech) {
14636  ast_log(LOG_ERROR, "This function requires a valid IAX2 channel\n");
14637  return -1;
14638  }
14639 
14640  callno = PTR_TO_CALLNO(ast_channel_tech_pvt(chan));
14641  ast_mutex_lock(&iaxsl[callno]);
14642  if (!(pvt = iaxs[callno])) {
14643  ast_mutex_unlock(&iaxsl[callno]);
14644  return -1;
14645  }
14646 
14647  if (!strcasecmp(args, "osptoken")) {
14648  ast_copy_string(buf, pvt->osptoken, buflen);
14649  } else if (!strcasecmp(args, "peerip")) {
14650  ast_copy_string(buf, !ast_sockaddr_isnull(&pvt->addr) ? ast_sockaddr_stringify_addr(&pvt->addr) : "", buflen);
14651  } else if (!strcasecmp(args, "peername")) {
14652  ast_copy_string(buf, pvt->username, buflen);
14653  } else if (!strcasecmp(args, "secure_signaling") || !strcasecmp(args, "secure_media")) {
14654  snprintf(buf, buflen, "%s", IAX_CALLENCRYPTED(pvt) ? "1" : "");
14655  } else {
14656  res = -1;
14657  }
14658 
14659  ast_mutex_unlock(&iaxsl[callno]);
14660 
14661  return res;
14662 }
14663 
14664 /*! \brief Part of the device state notification system ---*/
14665 static int iax2_devicestate(const char *data)
14666 {
14667  struct parsed_dial_string pds;
14668  char *tmp = ast_strdupa(data);
14669  struct iax2_peer *p;
14670  int res = AST_DEVICE_INVALID;
14671 
14672  memset(&pds, 0, sizeof(pds));
14673  parse_dial_string(tmp, &pds);
14674 
14675  if (ast_strlen_zero(pds.peer)) {
14676  ast_log(LOG_WARNING, "No peer provided in the IAX2 dial string '%s'\n", data);
14677  return res;
14678  }
14679 
14680  ast_debug(3, "Checking device state for device %s\n", pds.peer);
14681 
14682  /* SLD: FIXME: second call to find_peer during registration */
14683  if (!(p = find_peer(pds.peer, 1)))
14684  return res;
14685 
14686  res = AST_DEVICE_UNAVAILABLE;
14687 
14688  ast_debug(3, "Found peer. What's device state of %s? addr=%s, defaddr=%s maxms=%d, lastms=%d\n",
14689  pds.peer, ast_sockaddr_stringify(&p->addr), ast_sockaddr_stringify(&p->defaddr), p->maxms, p->lastms);
14690 
14691  if (((!ast_sockaddr_isnull(&p->addr)) || (!ast_sockaddr_isnull(&p->defaddr))) &&
14692  (!p->maxms || ((p->lastms > -1) && (p->historicms <= p->maxms)))) {
14693  /* Peer is registered, or have default IP address
14694  and a valid registration */
14695  if (p->historicms == 0 || p->historicms <= p->maxms)
14696  /* let the core figure out whether it is in use or not */
14697  res = AST_DEVICE_UNKNOWN;
14698  }
14699 
14700  peer_unref(p);
14701 
14702  return res;
14703 }
14704 
14705 static struct ast_switch iax2_switch =
14706 {
14707  .name = "IAX2",
14708  .description = "IAX Remote Dialplan Switch",
14709  .exists = iax2_exists,
14710  .canmatch = iax2_canmatch,
14711  .exec = iax2_exec,
14712  .matchmore = iax2_matchmore,
14713 };
14714 
14715 static struct ast_cli_entry cli_iax2[] = {
14716  AST_CLI_DEFINE(handle_cli_iax2_provision, "Provision an IAX device"),
14717  AST_CLI_DEFINE(handle_cli_iax2_prune_realtime, "Prune a cached realtime lookup"),
14718  AST_CLI_DEFINE(handle_cli_iax2_reload, "Reload IAX configuration"),
14719  AST_CLI_DEFINE(handle_cli_iax2_set_mtu, "Set the IAX systemwide trunking MTU"),
14720  AST_CLI_DEFINE(handle_cli_iax2_set_debug, "Enable/Disable IAX debugging"),
14721  AST_CLI_DEFINE(handle_cli_iax2_set_debug_trunk, "Enable/Disable IAX trunk debugging"),
14722  AST_CLI_DEFINE(handle_cli_iax2_set_debug_jb, "Enable/Disable IAX jitterbuffer debugging"),
14723  AST_CLI_DEFINE(handle_cli_iax2_show_cache, "Display IAX cached dialplan"),
14724  AST_CLI_DEFINE(handle_cli_iax2_show_channels, "List active IAX channels"),
14725  AST_CLI_DEFINE(handle_cli_iax2_show_firmware, "List available IAX firmware"),
14726  AST_CLI_DEFINE(handle_cli_iax2_show_netstats, "List active IAX channel netstats"),
14727  AST_CLI_DEFINE(handle_cli_iax2_show_peer, "Show details on specific IAX peer"),
14728  AST_CLI_DEFINE(handle_cli_iax2_show_peers, "List defined IAX peers"),
14729  AST_CLI_DEFINE(handle_cli_iax2_show_registry, "Display IAX registration status"),
14730  AST_CLI_DEFINE(handle_cli_iax2_show_stats, "Display IAX statistics"),
14731  AST_CLI_DEFINE(handle_cli_iax2_show_threads, "Display IAX helper thread info"),
14732  AST_CLI_DEFINE(handle_cli_iax2_show_users, "List defined IAX users"),
14733  AST_CLI_DEFINE(handle_cli_iax2_test_losspct, "Set IAX2 incoming frame loss percentage"),
14734  AST_CLI_DEFINE(handle_cli_iax2_unregister, "Unregister (force expiration) an IAX2 peer from the registry"),
14735  AST_CLI_DEFINE(handle_cli_iax2_show_callno_limits, "Show current entries in IP call number limit table"),
14736 #ifdef IAXTESTS
14737  AST_CLI_DEFINE(handle_cli_iax2_test_jitter, "Simulates jitter for testing"),
14738  AST_CLI_DEFINE(handle_cli_iax2_test_late, "Test the receipt of a late frame"),
14739  AST_CLI_DEFINE(handle_cli_iax2_test_resync, "Test a resync in received timestamps"),
14740 #endif /* IAXTESTS */
14741 };
14742 
14743 static void cleanup_thread_list(void *head)
14744 {
14745  AST_LIST_HEAD(iax2_thread_list, iax2_thread);
14746  struct iax2_thread_list *list_head = head;
14747  struct iax2_thread *thread;
14748 
14749  AST_LIST_LOCK(list_head);
14750  while ((thread = AST_LIST_REMOVE_HEAD(list_head, list))) {
14751  pthread_t thread_id = thread->threadid;
14752 
14753  thread->stop = 1;
14754  signal_condition(&thread->lock, &thread->cond);
14755 
14756  AST_LIST_UNLOCK(list_head);
14757  pthread_join(thread_id, NULL);
14758  AST_LIST_LOCK(list_head);
14759  }
14760  AST_LIST_UNLOCK(list_head);
14761 }
14762 
14763 static int __unload_module(void)
14764 {
14765  int x;
14766 
14767  network_change_stasis_unsubscribe();
14768  acl_change_stasis_unsubscribe();
14769 
14770  ast_manager_unregister("IAXpeers");
14771  ast_manager_unregister("IAXpeerlist");
14772  ast_manager_unregister("IAXnetstats");
14773  ast_manager_unregister("IAXregistry");
14775  ast_cli_unregister_multiple(cli_iax2, ARRAY_LEN(cli_iax2));
14776  ast_unregister_switch(&iax2_switch);
14777  ast_channel_unregister(&iax2_tech);
14778 
14779  if (netthreadid != AST_PTHREADT_NULL) {
14780  pthread_cancel(netthreadid);
14781  pthread_kill(netthreadid, SIGURG);
14782  pthread_join(netthreadid, NULL);
14783  }
14784 
14785  for (x = 0; x < ARRAY_LEN(iaxs); x++) {
14786  if (iaxs[x]) {
14787  iax2_destroy(x);
14788  }
14789  }
14790 
14791  /* Call for all threads to halt */
14792  cleanup_thread_list(&active_list);
14793  cleanup_thread_list(&dynamic_list);
14794  cleanup_thread_list(&idle_list);
14795 
14796  ast_netsock_release(netsock);
14797  ast_netsock_release(outsock);
14798  for (x = 0; x < ARRAY_LEN(iaxs); x++) {
14799  if (iaxs[x]) {
14800  iax2_destroy(x);
14801  }
14802  }
14803  ast_manager_unregister( "IAXpeers" );
14804  ast_manager_unregister( "IAXpeerlist" );
14805  ast_manager_unregister( "IAXnetstats" );
14806  ast_manager_unregister( "IAXregistry" );
14808  ast_cli_unregister_multiple(cli_iax2, ARRAY_LEN(cli_iax2));
14809  ast_unregister_switch(&iax2_switch);
14810  ast_channel_unregister(&iax2_tech);
14811  delete_users();
14812  iax_provision_unload();
14813  iax_firmware_unload();
14814 
14815  for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
14816  ast_mutex_destroy(&iaxsl[x]);
14817  }
14818 
14819  ao2_ref(peers, -1);
14820  ao2_ref(users, -1);
14821  ao2_ref(iax_peercallno_pvts, -1);
14822  ao2_ref(iax_transfercallno_pvts, -1);
14823  ao2_ref(callno_limits, -1);
14824  ao2_ref(calltoken_ignores, -1);
14825  if (timer) {
14826  ast_timer_close(timer);
14827  timer = NULL;
14828  }
14829  transmit_processor = ast_taskprocessor_unreference(transmit_processor);
14830 
14831  ast_sched_clean_by_callback(sched, peercnt_remove_cb, peercnt_remove_cb);
14833  sched = NULL;
14834  ao2_ref(peercnts, -1);
14835 
14836  ast_context_destroy_by_name(regcontext, "IAX2");
14837  ast_unload_realtime("iaxpeers");
14838 
14839  ao2_ref(iax2_tech.capabilities, -1);
14840  iax2_tech.capabilities = NULL;
14841  return 0;
14842 }
14843 
14844 static int unload_module(void)
14845 {
14846  ast_custom_function_unregister(&iaxpeer_function);
14847  ast_custom_function_unregister(&iaxvar_function);
14848  return __unload_module();
14849 }
14850 
14851 static int peer_set_sock_cb(void *obj, void *arg, int flags)
14852 {
14853  struct iax2_peer *peer = obj;
14854 
14855  if (peer->sockfd < 0)
14856  peer->sockfd = defaultsockfd;
14857 
14858  return 0;
14859 }
14860 
14861 static int pvt_hash_cb(const void *obj, const int flags)
14862 {
14863  const struct chan_iax2_pvt *pvt = obj;
14864 
14865  return pvt->peercallno;
14866 }
14867 
14868 static int pvt_cmp_cb(void *obj, void *arg, int flags)
14869 {
14870  struct chan_iax2_pvt *pvt = obj, *pvt2 = arg;
14871 
14872  /* The frames_received field is used to hold whether we're matching
14873  * against a full frame or not ... */
14874 
14875  return match(&pvt2->addr, pvt2->peercallno, pvt2->callno, pvt,
14876  pvt2->frames_received) ? CMP_MATCH | CMP_STOP : 0;
14877 }
14878 
14879 static int transfercallno_pvt_hash_cb(const void *obj, const int flags)
14880 {
14881  const struct chan_iax2_pvt *pvt = obj;
14882 
14883  return pvt->transfercallno;
14884 }
14885 
14886 static int transfercallno_pvt_cmp_cb(void *obj, void *arg, int flags)
14887 {
14888  struct chan_iax2_pvt *pvt = obj, *pvt2 = arg;
14889 
14890  /* The frames_received field is used to hold whether we're matching
14891  * against a full frame or not ... */
14892 
14893  return match(&pvt2->transfer, pvt2->transfercallno, pvt2->callno, pvt,
14894  pvt2->frames_received) ? CMP_MATCH | CMP_STOP : 0;
14895 }
14896 
14897 static int load_objects(void)
14898 {
14899  peers = users = iax_peercallno_pvts = iax_transfercallno_pvts = NULL;
14900  peercnts = callno_limits = calltoken_ignores = NULL;
14901 
14903  peer_hash_cb, NULL, peer_cmp_cb);
14904  if (!peers) {
14905  goto container_fail;
14906  }
14907 
14908  users = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_USER_BUCKETS,
14909  user_hash_cb, NULL, user_cmp_cb);
14910  if (!users) {
14911  goto container_fail;
14912  }
14913 
14914  iax_peercallno_pvts = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
14915  IAX_MAX_CALLS, pvt_hash_cb, NULL, pvt_cmp_cb);
14916  if (!iax_peercallno_pvts) {
14917  goto container_fail;
14918  }
14919 
14920  iax_transfercallno_pvts = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
14921  IAX_MAX_CALLS, transfercallno_pvt_hash_cb, NULL, transfercallno_pvt_cmp_cb);
14922  if (!iax_transfercallno_pvts) {
14923  goto container_fail;
14924  }
14925 
14927  peercnt_hash_cb, NULL, peercnt_cmp_cb);
14928  if (!peercnts) {
14929  goto container_fail;
14930  }
14931 
14933  MAX_PEER_BUCKETS, addr_range_hash_cb, NULL, addr_range_cmp_cb);
14934  if (!callno_limits) {
14935  goto container_fail;
14936  }
14937 
14938  calltoken_ignores = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
14939  MAX_PEER_BUCKETS, addr_range_hash_cb, NULL, addr_range_cmp_cb);
14940  if (!calltoken_ignores) {
14941  goto container_fail;
14942  }
14943 
14944  if (create_callno_pools()) {
14945  goto container_fail;
14946  }
14947 
14948  transmit_processor = ast_taskprocessor_get("iax2_transmit", TPS_REF_DEFAULT);
14949  if (!transmit_processor) {
14950  goto container_fail;
14951  }
14952 
14953  return 0;
14954 
14955 container_fail:
14956  if (peers) {
14957  ao2_ref(peers, -1);
14958  }
14959  if (users) {
14960  ao2_ref(users, -1);
14961  }
14962  if (iax_peercallno_pvts) {
14963  ao2_ref(iax_peercallno_pvts, -1);
14964  }
14965  if (iax_transfercallno_pvts) {
14966  ao2_ref(iax_transfercallno_pvts, -1);
14967  }
14968  if (peercnts) {
14969  ao2_ref(peercnts, -1);
14970  }
14971  if (callno_limits) {
14972  ao2_ref(callno_limits, -1);
14973  }
14974  if (calltoken_ignores) {
14975  ao2_ref(calltoken_ignores, -1);
14976  }
14977  return -1;
14978 }
14979 
14980 /*!
14981  * \brief Load the module
14982  *
14983  * Module loading including tests for configuration or dependencies.
14984  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
14985  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
14986  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
14987  * configuration file or other non-critical problem return
14988  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
14989  */
14990 static int load_module(void)
14991 {
14992  static const char config[] = "iax.conf";
14993  int x = 0;
14994  struct iax2_registry *reg = NULL;
14995 
14997  return AST_MODULE_LOAD_DECLINE;
14998  }
14999  ast_format_cap_append_by_type(iax2_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
15000 
15001  if (load_objects()) {
15002  ao2_ref(iax2_tech.capabilities, -1);
15003  iax2_tech.capabilities = NULL;
15004  return AST_MODULE_LOAD_DECLINE;
15005  }
15006 
15007  memset(iaxs, 0, sizeof(iaxs));
15008 
15009  for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
15010  ast_mutex_init(&iaxsl[x]);
15011  }
15012 
15013  if (!(sched = ast_sched_context_create())) {
15014  ast_log(LOG_ERROR, "Failed to create scheduler thread\n");
15015  ao2_ref(iax2_tech.capabilities, -1);
15016  iax2_tech.capabilities = NULL;
15017  return AST_MODULE_LOAD_DECLINE;
15018  }
15019 
15020  if (ast_sched_start_thread(sched)) {
15022  ao2_ref(iax2_tech.capabilities, -1);
15023  iax2_tech.capabilities = NULL;
15024  sched = NULL;
15025  return AST_MODULE_LOAD_DECLINE;
15026  }
15027 
15028  if (!(io = io_context_create())) {
15029  ast_log(LOG_ERROR, "Failed to create I/O context\n");
15031  ao2_ref(iax2_tech.capabilities, -1);
15032  iax2_tech.capabilities = NULL;
15033  sched = NULL;
15034  return AST_MODULE_LOAD_DECLINE;
15035  }
15036 
15037  if (!(netsock = ast_netsock_list_alloc())) {
15038  ast_log(LOG_ERROR, "Failed to create netsock list\n");
15039  io_context_destroy(io);
15041  ao2_ref(iax2_tech.capabilities, -1);
15042  iax2_tech.capabilities = NULL;
15043  sched = NULL;
15044  return AST_MODULE_LOAD_DECLINE;
15045  }
15046  ast_netsock_init(netsock);
15047 
15048  outsock = ast_netsock_list_alloc();
15049  if (!outsock) {
15050  ast_log(LOG_ERROR, "Could not allocate outsock list.\n");
15051  io_context_destroy(io);
15053  ao2_ref(iax2_tech.capabilities, -1);
15054  iax2_tech.capabilities = NULL;
15055  sched = NULL;
15056  return AST_MODULE_LOAD_DECLINE;
15057  }
15058  ast_netsock_init(outsock);
15059 
15060  randomcalltokendata = ast_random();
15061 
15062  iax_set_output(iax_debug_output);
15063  iax_set_error(iax_error_output);
15064  jb_setoutput(jb_error_output, jb_warning_output, NULL);
15065 
15066  if ((timer = ast_timer_open())) {
15067  ast_timer_set_rate(timer, 1000 / trunkfreq);
15068  }
15069 
15070  if (set_config(config, 0, 0) == -1) {
15071  if (timer) {
15072  ast_timer_close(timer);
15073  timer = NULL;
15074  }
15075  __unload_module();
15076  return AST_MODULE_LOAD_DECLINE;
15077  }
15078 
15079  ast_cli_register_multiple(cli_iax2, ARRAY_LEN(cli_iax2));
15080 
15082 
15083  ast_custom_function_register(&iaxpeer_function);
15084  ast_custom_function_register(&iaxvar_function);
15085 
15086  ast_manager_register_xml("IAXpeers", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_peers);
15087  ast_manager_register_xml("IAXpeerlist", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_peer_list);
15088  ast_manager_register_xml("IAXnetstats", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_netstats);
15089  ast_manager_register_xml("IAXregistry", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_iax2_show_registry);
15090 
15091  if (ast_channel_register(&iax2_tech)) {
15092  ast_log(LOG_ERROR, "Unable to register channel class %s\n", "IAX2");
15093  __unload_module();
15094  return AST_MODULE_LOAD_DECLINE;
15095  }
15096 
15097  if (ast_register_switch(&iax2_switch)) {
15098  ast_log(LOG_ERROR, "Unable to register IAX switch\n");
15099  }
15100 
15101  if (start_network_thread()) {
15102  ast_log(LOG_ERROR, "Unable to start network thread\n");
15103  __unload_module();
15104  return AST_MODULE_LOAD_DECLINE;
15105  } else {
15106  ast_verb(2, "IAX Ready and Listening\n");
15107  }
15108 
15109  AST_LIST_LOCK(&registrations);
15110  AST_LIST_TRAVERSE(&registrations, reg, entry)
15111  iax2_do_register(reg);
15112  AST_LIST_UNLOCK(&registrations);
15113 
15114  ao2_callback(peers, 0, peer_set_sock_cb, NULL);
15115  ao2_callback(peers, 0, iax2_poke_peer_cb, NULL);
15116 
15117 
15118  iax_firmware_reload();
15119  iax_provision_reload(0);
15120 
15121  ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL);
15122 
15123  network_change_stasis_subscribe();
15124 
15125  return AST_MODULE_LOAD_SUCCESS;
15126 }
15127 
15128 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Inter Asterisk eXchange (Ver 2)",
15129  .support_level = AST_MODULE_SUPPORT_CORE,
15130  .load = load_module,
15131  .unload = unload_module,
15132  .reload = reload,
15133  .load_pri = AST_MODPRI_CHANNEL_DRIVER,
15134  .requires = "dnsmgr",
15135  .optional_modules = "res_crypto",
15136 );
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:286
int ast_io_wait(struct io_context *ioc, int howlong)
Waits for IO.
Definition: io.c:278
static ast_mutex_t iaxsl[ARRAY_LEN(iaxs)]
chan_iax2_pvt structure locks
Definition: chan_iax2.c:1211
const char * name
Definition: pbx.h:119
const char * type
Definition: datastore.h:32
int ast_sched_start_thread(struct ast_sched_context *con)
Start a thread for processing scheduler entries.
Definition: sched.c:197
unsigned int notsilenttx
Definition: chan_iax2.c:770
struct ast_variable * next
long max_jitterbuf
Definition: jitterbuf.h:69
struct ast_channel * ast_waitfor_n(struct ast_channel **chan, int n, int *ms)
Waits for input on a group of channels Wait for input on an array of channels for a given # of millis...
Definition: channel.c:3157
Require call token validation after a successful registration using call token validation occurs...
Definition: chan_iax2.c:543
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1150
struct ast_frame af
Definition: parser.h:142
static int load_module(void)
Load the module.
Definition: chan_iax2.c:14990
static uint16_t global_maxcallno_nonval
Definition: chan_iax2.c:1021
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2022
unsigned int datalen
Definition: iax2.h:299
int presentation
Q.931 encoded presentation-indicator encoded field.
Definition: channel.h:277
void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
Set caller ID number, name and ANI and generate AMI event.
Definition: channel.c:7334
#define IAX_IE_IAX_UNKNOWN
Definition: iax2.h:153
int ast_matchmore_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Looks to see if adding anything to this extension might match something. (exists ^ canmatch) ...
Definition: pbx.c:4195
const ast_string_field secret
Definition: chan_iax2.c:861
const ast_string_field rdnis
Definition: chan_iax2.c:861
Main Channel structure associated with a channel.
struct timeval lasttxtime
Definition: chan_iax2.c:646
static int iax2_ack_registry(struct iax_ies *ies, struct ast_sockaddr *addr, int callno)
Acknowledgment received for OUR registration.
Definition: chan_iax2.c:8858
Music on hold handling.
void ast_sched_clean_by_callback(struct ast_sched_context *con, ast_sched_cb match, ast_sched_cb cleanup_cb)
Clean all scheduled events with matching callback.
Definition: sched.c:409
#define IAX_RTSAVE_SYSNAME
Definition: chan_iax2.c:506
char * str
Subscriber phone number (Malloced)
Definition: channel.h:291
#define MAX_PEER_BUCKETS
Definition: chan_iax2.c:998
#define IAX_TRUNKTIMESTAMPS
Definition: chan_iax2.c:519
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags, const struct ast_sockaddr *dest_addr)
Wrapper around sendto(2) that uses ast_sockaddr.
Definition: netsock2.c:614
uint16_t limit
Definition: chan_iax2.c:1033
uint64_t iax2_format_compatibility_cap2bitfield(const struct ast_format_cap *cap)
Convert a format capabilities structure to a bitfield.
#define IAX_IE_CAPABILITY2
Definition: iax2.h:187
Security Event Reporting API.
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
unsigned char csub
Definition: iax2.h:237
struct timeval rxcore
Definition: chan_iax2.c:798
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
Asterisk locking-related definitions:
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:3310
static int make_trunk(unsigned short callno, int locked)
Definition: chan_iax2.c:2380
#define IAX_IE_CALLINGTNS
Definition: iax2.h:170
const ast_string_field osptoken
Definition: chan_iax2.c:861
Asterisk main include file. File version handling, generic pbx functions.
struct ast_format * ast_format_compatibility_bitfield2format(uint64_t bitfield)
Convert a bitfield to its respective format structure.
int encmethods
Definition: chan_iax2.c:612
unsigned char iseqno
Definition: chan_iax2.c:818
void * ast_mwi_unsubscribe(struct ast_mwi_subscriber *sub)
Unsubscribe from the stasis topic and MWI.
Definition: mwi.c:254
#define ast_realloc(p, len)
A wrapper for realloc()
Definition: astmm.h:226
struct stasis_topic * ast_system_topic(void)
A Stasis Message Bus API topic which publishes messages regarding system changes. ...
#define AST_LIST_HEAD(name, type)
Defines a structure to be used to hold a list of specified type.
Definition: linkedlists.h:173
const ast_string_field cid_num
Definition: chan_iax2.c:861
char * str
Subscriber phone number (Malloced)
Definition: channel.h:386
char chan_name[AST_CHANNEL_NAME]
static struct iax2_peer * find_peer(const char *name, int realtime)
Definition: chan_iax2.c:2056
const ast_string_field exten
Definition: chan_iax2.c:861
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
int expire
Definition: chan_iax2.c:614
uint64_t ast_format_compatibility_format2bitfield(const struct ast_format *format)
Convert a format structure to its respective bitfield.
static int iax2_predestroy(int callno)
Definition: chan_iax2.c:3483
#define AST_OPTION_TXGAIN
unsigned char calltoken_ie_len
Definition: chan_iax2.c:933
ast_aes_decrypt_key dcx
Definition: chan_iax2.c:875
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827
unsigned char aseqno
Definition: chan_iax2.c:820
const ast_string_field dnid
Definition: chan_iax2.c:861
int presentation
Q.931 presentation-indicator and screening-indicator encoded fields.
Definition: channel.h:295
static void handle_deferred_full_frames(struct iax2_thread *thread)
Handle any deferred full frames for this thread.
Definition: chan_iax2.c:9854
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
static struct iax2_peer * realtime_peer(const char *peername, struct ast_sockaddr *addr)
Definition: chan_iax2.c:4417
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
iax2_format svoiceformat
Definition: chan_iax2.c:752
iax2_format videoformat
Definition: chan_iax2.c:750
Definition: iax2.h:282
struct ast_party_id id
Connected party ID.
Definition: channel.h:458
static struct ao2_container * iax_peercallno_pvts
Another container of iax2_pvt structures.
Definition: chan_iax2.c:1202
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
unsigned short scallno
Definition: iax2.h:231
struct iax2_registry * reg
Definition: chan_iax2.c:882
struct ast_endpoint * ast_endpoint_create(const char *tech, const char *resource)
Create an endpoint struct.
size_t afdatalen
Definition: parser.h:144
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
Definition: chan_iax2.c:14455
#define IAX_USEJITTERBUF
Definition: chan_iax2.c:503
Device state management.
const ast_string_field description
Definition: chan_iax2.c:599
Support for translation of data formats. translate.c.
#define IAX_SHRINKCALLERID
Definition: chan_iax2.c:528
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:167
#define MAX_MUSICCLASS
Definition: channel.h:173
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_party_name name
Subscriber name.
Definition: channel.h:340
#define OBJ_KEY
Definition: astobj2.h:1151
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:570
struct ast_party_id from
Who is redirecting the call (Sent to the party the call is redirected toward)
Definition: channel.h:527
ast_aes_encrypt_key ecx
Definition: chan_iax2.c:871
struct ast_ha ha
Definition: chan_iax2.c:1042
#define IAX_META_TRUNK_MINI
Definition: iax2.h:208
unsigned short version
Definition: iax2.h:297
struct ast_variable * vars
Definition: chan_iax2.c:919
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.
Definition: chan_iax2.c:14478
int ast_queue_unhold(struct ast_channel *chan)
Queue an unhold frame.
Definition: channel.c:1216
unsigned char oseqno
Definition: chan_iax2.c:814
#define IAX_IE_VARIABLE
Definition: iax2.h:183
#define IAX_IE_RR_LOSS
Definition: iax2.h:178
#define OBJ_POINTER
Definition: astobj2.h:1150
iax2_format peercapability
Definition: chan_iax2.c:794
struct ast_channel * ast_channel_release(struct ast_channel *chan)
Unlink and release reference to a channel.
Definition: channel.c:1584
int sockfd
Definition: chan_iax2.c:604
const char * iax2_getformatname(iax2_format format)
iax2 wrapper function for ast_getformatname
Definition: chan_iax2.c:1950
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
Definition: manager.c:3467
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
descriptor for a cli entry.
Definition: cli.h:171
#define IAX_IE_DATETIME
Definition: iax2.h:161
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
Create a new thread and start the PBX.
Definition: pbx.c:4708
static int iax2_queue_frame(int callno, struct ast_frame *f)
Queue a frame to a call's owning asterisk channel.
Definition: chan_iax2.c:3300
long losspct
Definition: jitterbuf.h:90
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:324
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
unsigned char oseqno
Definition: iax2.h:234
AST_JSON_INT_T ast_json_int_t
Primarily used to cast when packing to an "I" type.
Definition: json.h:87
enum iax_transfer_state transferring
Definition: chan_iax2.c:890
int historicms
Definition: chan_iax2.c:626
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1821
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
Media Format Bitfield Compatibility API.
struct ast_taskprocessor * ast_taskprocessor_get(const char *name, enum ast_tps_options create)
Get a reference to a taskprocessor with the specified name and create the taskprocessor if necessary...
Provide cryptographic signature routines.
int ast_unload_realtime(const char *family)
Release any resources cached for a realtime family.
Definition: main/config.c:3576
unsigned char semirand[32]
Definition: chan_iax2.c:880
#define IAX_ENCRYPT_KEYROTATE
Definition: iax2.h:202
#define IAX_META_TRUNK
Definition: iax2.h:204
struct iax2_codec_pref rprefs
Definition: chan_iax2.c:780
#define IAX_FORCE_ENCRYPT
Definition: chan_iax2.c:527
const ast_string_field inkeys
Definition: chan_iax2.c:561
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
#define IAX_DEFAULT_REG_EXPIRE
Definition: iax2.h:123
#define IAX_QUELCH
Definition: chan_iax2.c:509
#define IAX_DELAYPBXSTART
Definition: chan_iax2.c:522
#define IAX_LINGER_TIMEOUT
Definition: iax2.h:126
#define IAX_IE_CALLNO
Definition: iax2.h:151
#define IAX_DELME
Definition: chan_iax2.c:499
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.
Definition: chan_iax2.c:14501
Structure for variables, used for configurations and for channel variables.
static pj_pool_t * pool
Global memory pool for configuration and timers.
pthread_t thread
Definition: app_sla.c:329
#define IAX_IE_MD5_RESULT
Definition: iax2.h:146
const ast_string_field challenge
Definition: chan_iax2.c:861
ast_control_transfer
#define MAX_TIMESTAMP_SKEW
Definition: chan_iax2.c:712
unsigned short bridgecallno
Definition: chan_iax2.c:904
struct ast_variable * iaxvars
Definition: chan_iax2.c:921
#define IAX_IE_FIRMWAREVER
Definition: iax2.h:164
enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now)
queue a frame
Definition: jitterbuf.c:525
long frames_lost
Definition: jitterbuf.h:82
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:117
return a reference to a taskprocessor, create one if it does not exist
Definition: taskprocessor.h:76
#define IAX_ENCRYPTED
Definition: chan_iax2.c:510
Test Framework API.
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
#define AST_IO_IN
Definition: io.h:34
unsigned char delme
Definition: chan_iax2.c:1046
static struct stasis_subscription * network_change_sub
Definition: chan_iax2.c:327
const ast_string_field language
Definition: chan_iax2.c:861
Definition: sched.c:76
void ast_timer_close(struct ast_timer *handle)
Close an opened timing handle.
Definition: timing.c:154
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
Structure to pass both assignedid values to channel drivers.
Definition: channel.h:604
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
Definition: stasis.c:1077
int ast_ignore_pattern(const char *context, const char *pattern)
Checks to see if a number should be ignored.
Definition: pbx.c:6879
#define IAX_IE_RR_DELAY
Definition: iax2.h:180
struct stasis_message_type * ast_named_acl_change_type(void)
a stasis_message_type for changes against a named ACL or the set of all named ACLs ...
Structure for a data store type.
Definition: datastore.h:31
ast_channel_state
ast_channel states
Definition: channelstate.h:35
unsigned int lastvsent
Definition: chan_iax2.c:762
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition: timing.c:122
static int wait_for_peercallno(struct chan_iax2_pvt *pvt)
Definition: chan_iax2.c:5425
unsigned char semirand[32]
Definition: parser.h:138
uint64_t iax2_codec_pref_from_bitfield(struct iax2_codec_pref *pref, uint64_t bitfield)
Create codec preference list from the given bitfield formats.
Definition: codec_pref.c:481
char * str
Subscriber name (Malloced)
Definition: channel.h:264
int * ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data)
Adds an IO context.
Definition: io.c:162
void ast_unregister_switch(struct ast_switch *sw)
Unregister an alternative switch.
Definition: pbx_switch.c:76
Scheduler ID holder.
Definition: sched.c:70
int ast_callid_threadassoc_add(ast_callid callid)
Adds a known callid to thread storage of the calling thread.
Definition: logger.c:2320
ast_callid callid
Definition: chan_iax2.c:746
Definition of a media format.
Definition: format.c:43
static struct @114 frame_queue[IAX_MAX_CALLS]
a list of frames that may need to be retransmitted
void iax2_codec_pref_remove_missing(struct iax2_codec_pref *pref, uint64_t bitfield)
Removes format from the pref list that aren't in the bitfield.
Definition: codec_pref.c:288
#define IAX_CODEC_NOPREFS
Definition: chan_iax2.c:513
Background DNS update manager.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Add all codecs Asterisk knows about for a specific type to the capabilities structure.
Definition: format_cap.c:216
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
unsigned short transfercallno
Definition: chan_iax2.c:896
int callno
Definition: chan_iax2.c:619
void iax2_codec_pref_prepend(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing, int only_if_existing)
Prepend an audio codec to a preference list, removing it first if it was already there.
Definition: codec_pref.c:432
ast_control_frame_type
Internal control frame subtype field values.
static int copy(char *infile, char *outfile)
Utility function to copy a file.
#define IAX_IE_AUTOANSWER
Definition: iax2.h:155
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:341
struct ast_dnsmgr_entry * dnsmgr
Definition: chan_iax2.c:695
#define IAX_SENDANI
Definition: chan_iax2.c:505
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition: format_cap.c:395
void ast_endpoint_set_state(struct ast_endpoint *endpoint, enum ast_endpoint_state state)
Updates the state of the given endpoint.
struct ast_flags state
Definition: chan_iax2.c:810
ast_callid ast_read_threadstorage_callid(void)
extracts the callerid from the thread
Definition: logger.c:2298
Wrapper for an ast_acl linked list.
Definition: acl.h:76
#define IAX_CODEC_USER_FIRST
Definition: chan_iax2.c:512
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:539
unsigned int last
Definition: chan_iax2.c:758
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
Structure for a data store object.
Definition: datastore.h:64
void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
Destroy the connected line information contents.
Definition: channel.c:2072
#define AST_OPTION_TDD
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2399
Definition: parser.h:27
Definition: dnsmgr.c:66
int smoothing
Definition: chan_iax2.c:627
int ast_str2tos(const char *value, unsigned int *tos)
Convert a string to the appropriate TOS value.
Definition: acl.c:966
jitterbuf * jb
Definition: chan_iax2.c:800
static int iax2_devicestate(const char *data)
Part of the device state notification system —.
Definition: chan_iax2.c:14665
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3326
#define MAX_TRUNKDATA
Definition: chan_iax2.c:340
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
calltoken_peer_enum
Call token validation settings.
Definition: chan_iax2.c:536
#define IAX_IE_CHALLENGE
Definition: iax2.h:145
enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl)
get a frame for time now (receiver's time) return value is one of JB_OK: You've got frame! JB_DROP: H...
Definition: jitterbuf.c:785
int iax2_format_compatibility_bitfield2cap(uint64_t bitfield, struct ast_format_cap *cap)
Convert a bitfield to a format capabilities structure.
I/O Management (derived from Cheops-NG)
static void send_signaling(struct chan_iax2_pvt *pvt)
This function must be called once we are sure the other side has given us a call number. All signaling is held here until that point.
Definition: chan_iax2.c:2205
static int schedule_delivery(struct iax_frame *fr, int updatehistory, int fromtrunk, unsigned int *tsout)
Definition: chan_iax2.c:4276
callno_entry callno_entry
Definition: chan_iax2.c:784
int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
Force a refresh of a dnsmgr entry.
Definition: dnsmgr.c:239
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:856
static struct iax2_user * build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
Create in-memory user structure from configuration.
Definition: chan_iax2.c:13289
#define IAX_IE_DPSTATUS
Definition: iax2.h:150
list of users found in the config file
#define IAX_IE_LANGUAGE
Definition: iax2.h:140
Structure used to handle a large number of boolean flags == used only in app_dial?
Definition: utils.h:204
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static int update_registry(struct ast_sockaddr *addr, int callno, char *devtype, int fd, unsigned short refresh)
Definition: chan_iax2.c:9144
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:7776
void ast_free_ptr(void *ptr)
free() wrapper
Definition: main/astmm.c:1739
Socket address structure.
Definition: netsock2.h:97
int iax2_codec_pref_best_bitfield2cap(uint64_t bitfield, struct iax2_codec_pref *prefs, struct ast_format_cap *cap)
Convert a bitfield to a format capabilities structure in the "best" order.
Definition: codec_pref.c:112
#define IAX_PROVISION
Definition: chan_iax2.c:508
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
Definition: netsock2.c:590
int ast_update_realtime(const char *family, const char *keyfield, const char *lookup,...) attribute_sentinel
Update realtime configuration.
Definition: main/config.c:3659
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
Definition: netsock2.c:413
struct ast_format * ast_format_none
Built-in "null" format.
Definition: format_cache.c:246
static int manager_iax2_show_peer_list(struct mansession *s, const struct message *m)
callback to display iax peers in manager format
Definition: chan_iax2.c:7367
#define IAX_IE_RR_JITTER
Definition: iax2.h:177
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
const ast_string_field inkeys
Definition: chan_iax2.c:599
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
#define IAX_IE_CALLING_NUMBER
Definition: iax2.h:132
int ast_canmatch_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Looks for a valid matching extension.
Definition: pbx.c:4190
ast_aes_decrypt_key mydcx
Definition: parser.h:136
unsigned int nextpred
Definition: chan_iax2.c:764
int ast_translator_best_choice(struct ast_format_cap *dst_cap, struct ast_format_cap *src_cap, struct ast_format **dst_fmt_out, struct ast_format **src_fmt_out)
Chooses the best translation path.
Definition: translate.c:1402
unsigned short callno
Definition: iax2.h:272
Implementation of the IAX2 protocol.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
struct ast_frame_subclass subclass
const ast_string_field parkinglot
Definition: chan_iax2.c:861
static struct ao2_container * iax_transfercallno_pvts
Another container of iax2_pvt structures.
Definition: chan_iax2.c:1218
#define IAX_IE_CALLING_ANI
Definition: iax2.h:133
#define MAX_LANGUAGE
Definition: channel.h:172
int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar)
Simply remove extension from context.
Definition: pbx.c:4948
uint16_t maxcallno
Definition: chan_iax2.c:628
Utility functions.
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:138
#define IAX_META_TRUNK_SUPERMINI
Definition: iax2.h:207
int authmethods
Definition: chan_iax2.c:611
int args
This gets set in ast_cli_register()
Definition: cli.h:185
int AST_OPTIONAL_API_NAME() ast_check_signature(struct ast_key *key, const char *msg, const char *sig)
base64 decode then sent to __ast_check_signature_bin
Definition: res_crypto.c:673
int ast_context_destroy_by_name(const char *context, const char *registrar)
Destroy a context by name.
Definition: pbx.c:8205
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
Definition: manager.c:3050
unsigned short callno
Definition: iax2.h:250
unsigned int pingtime
Definition: chan_iax2.c:772
const char * name
Definition: pbx.h:162
#define AST_LIST_HEAD_DESTROY(head)
Destroys a list head structure.
Definition: linkedlists.h:653
int expiry
Definition: chan_iax2.c:615
static void iax2_destroy_helper(struct chan_iax2_pvt *pvt)
Definition: chan_iax2.c:2135
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition: main/cli.c:1846
unsigned char type
Definition: iax2.h:236
int ast_format_cap_update_by_allow_disallow(struct ast_format_cap *cap, const char *list, int allowing)
Parse an "allow" or "deny" list and modify a format capabilities structure accordingly.
Definition: format_cap.c:320
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.
Definition: chan_iax2.c:9884
#define IAX_IE_ENCRYPTION
Definition: iax2.h:173
Number structure.
Definition: app_followme.c:154
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
#define AST_MAX_ACCOUNT_CODE
Definition: channel.h:170
static int register_verify(int callno, struct ast_sockaddr *addr, struct iax_ies *ies)
Verify inbound registration.
Definition: chan_iax2.c:8303
jitterbuf * jb_new(void)
new jitterbuf
Definition: jitterbuf.c:86
enum ama_flags ast_channel_string2amaflag(const char *flag)
Convert a string to a detail record AMA flag.
Definition: channel.c:4360
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:517
Custom localtime functions for multiple timezones.
uint64_t iax2_codec_pref_order_value_to_format_bitfield(int order_value)
Convert an iax2_codec_pref order value into a format bitfield.
Definition: codec_pref.c:367
internal representation of ACL entries In principle user applications would have no need for this...
Definition: acl.h:51
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
unsigned short dcallno
Definition: iax2.h:232
struct ast_party_id id
Caller party ID.
Definition: channel.h:420
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Definition: netsock2.h:358
Configuration File Parser.
#define IAX_DYNAMIC
Definition: chan_iax2.c:504
static const char config_file[]
unsigned int ts
Definition: iax2.h:278
int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed)
Parse a time (integer) string.
Definition: utils.c:2446
char * ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
Strip leading/trailing whitespace and quotes from a string.
Definition: utils.c:1818
static int global_max_trunk_mtu
Definition: chan_iax2.c:317
struct ast_iax2_mini_hdr mini
Definition: iax2.h:290
static struct iax2_peer * build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int temponly)
Create peer structure based on configuration.
Definition: chan_iax2.c:12964
unsigned int sentyet
Definition: parser.h:116
long frames_in
Definition: jitterbuf.h:79
long jb_next(jitterbuf *jb)
when is the next frame due out, in receiver's time (0=EMPTY) This value may change as frames are adde...
Definition: jitterbuf.c:767
#define MAX_TRUNK_MTU
Maximum transmission unit for the UDP packet in the trunk not to be fragmented. This is based on 1516...
Definition: chan_iax2.c:315
int ast_callid_threadassoc_remove(void)
Removes callid from thread storage of the calling thread.
Definition: logger.c:2339
long resync_threshold
Definition: jitterbuf.h:70
static void * iax2_process_thread(void *data)
Definition: chan_iax2.c:12158
static void cleanup(void)
Clean up any old apps that we don't need any more.
Definition: res_stasis.c:327
#define ast_config_load(filename, flags)
Load a config file.
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:510
struct ast_module * self
Definition: module.h:356
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
Definition: netsock2.c:164
ast_mutex_t lock
enum ast_transfer_result ast_bridge_transfer_blind(int is_external, struct ast_channel *transferer, const char *exten, const char *context, transfer_channel_cb new_channel_cb, void *user_data)
Blind transfer target to the extension and context provided.
Definition: bridge.c:4425
struct ast_party_id ani
Automatic Number Identification (ANI)
Definition: channel.h:427
#define IAX_IE_FORMAT2
Definition: iax2.h:188
General Asterisk PBX channel definitions.
int ast_realtime_require_field(const char *family,...) attribute_sentinel
Inform realtime what fields that may be stored.
Definition: main/config.c:3549
struct timeval txtrunktime
Definition: chan_iax2.c:644
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
#define IAX_IE_CALLINGANI2
Definition: iax2.h:190
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
Set the source of the hangup in this channel and it's bridge.
Definition: channel.c:2499
Asterisk file paths, configured in asterisk.conf.
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
void io_context_destroy(struct io_context *ioc)
Destroys a context.
Definition: io.c:107
const char * src
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition: manager.c:3475
unsigned int cacheable
Definition: parser.h:124
#define AST_IO_PRI
Definition: io.h:38
long frames_ooo
Definition: jitterbuf.h:84
#define IAX_IE_CAUSECODE
Definition: iax2.h:172
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5762
Network socket handling.
static int iax2_lock_callno_unless_destroyed(int callno)
Acquire the iaxsl[callno] if call exists and not having ongoing hangup.
Definition: chan_iax2.c:1745
#define AST_OPTION_RELAXDTMF
int ast_variable_list_replace(struct ast_variable **head, struct ast_variable *replacement)
Replace a variable in the given list with a new value.
Definition: main/config.c:667
struct timeval offset
Definition: chan_iax2.c:796
iax2_format peerformat
Definition: chan_iax2.c:792
void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
Free a DNS manager entry.
Definition: dnsmgr.c:136
#define IAX_IE_VERSION
Definition: iax2.h:141
static int peer_hash_cb(const void *obj, const int flags)
Definition: chan_iax2.c:2009
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
void ast_channel_stage_snapshot_done(struct ast_channel *chan)
Clear flag to indicate channel snapshot is being staged, and publish snapshot.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
int iax2_codec_pref_to_cap(struct iax2_codec_pref *pref, struct ast_format_cap *cap)
Convert a preference structure to a capabilities structure.
Definition: codec_pref.c:91
static struct call_number_pool callno_pool_trunk
Definition: chan_iax2.c:969
#define IAX_HASCALLERID
Definition: chan_iax2.c:498
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Access Control of various sorts.
IAX Firmware Support header file.
void ast_system_publish_registry(const char *channeltype, const char *username, const char *domain, const char *status, const char *cause)
Publish a channel driver outgoing registration message.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
Global IO variables are now in a struct in order to be made threadsafe.
Definition: io.c:71
#define IAX_RTCACHEFRIENDS
Definition: chan_iax2.c:515
#define AST_MAX_EXTENSION
Definition: channel.h:134
unsigned short peercallno
Definition: chan_iax2.c:786
Scheduler Routines (derived from cheops)
const ast_string_field zonetag
Definition: chan_iax2.c:599
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:303
struct stasis_cache * ast_mwi_state_cache(void)
Backend cache for ast_mwi_topic_cached().
Definition: mwi.c:94
unsigned short len
Definition: iax2.h:284
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
unsigned short zeros
Definition: iax2.h:264
Asterisk internal frame definitions.
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:87
#define IAX_IE_CALLING_NAME
Definition: iax2.h:134
In case you didn't read that giant block of text above the mansession_session struct, the mansession is named this solely to keep the API the same in Asterisk. This structure really represents data that is different from Manager action to Manager action. The mansession_session pointer contained within points to session-specific data.
Definition: manager.c:1785
struct ast_format * ast_format_cap_get_compatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
Find if input ast_format is within the capabilities of the ast_format_cap object then return the comp...
Definition: format_cap.c:546
const ast_string_field regexten
Definition: chan_iax2.c:599
const ast_string_field username
Definition: chan_iax2.c:861
unsigned int lastsent
Definition: chan_iax2.c:648
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:282
#define IAX_IE_PROVISIONING
Definition: iax2.h:159
Require call token validation.
Definition: chan_iax2.c:540
#define AST_BRIDGE_DTMF_CHANNEL_1
Report DTMF on channel 1.
Definition: channel.h:2372
static struct ao2_container * calltoken_ignores
Definition: chan_iax2.c:1012
static struct stasis_rest_handlers events
REST handler for /api-docs/events.json.
int ast_atomic_dec_and_test(volatile int *p)
decrement *p by 1 and return true if the variable has reached 0.
Definition: lock.h:767
int AST_OPTIONAL_API_NAME() ast_sign(struct ast_key *key, char *msg, char *sig)
wrapper for __ast_sign_bin then base64 encode it
Definition: res_crypto.c:584
unsigned int ast_codec_samples_count(struct ast_frame *frame)
Get the number of samples contained within a frame.
Definition: codec.c:379
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
static struct ao2_container * formats
Cached formats.
Definition: format_cache.c:260
void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
Default calltoken required unless the ip is in the ignorelist.
Definition: chan_iax2.c:538
int last_iax_message
Definition: chan_iax2.c:768
A set of macros to manage forward-linked lists.
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
long frames_dropped
Definition: jitterbuf.h:83
#define ast_debug(level,...)
Log a DEBUG message.
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5803
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:4175
static int iax2_queue_unhold(int callno)
Queue an unhold frame on the ast_channel owner.
Definition: chan_iax2.c:3346
static int authenticate_reply(struct chan_iax2_pvt *p, struct ast_sockaddr *addr, struct iax_ies *ies, const char *override, const char *okey)
Definition: chan_iax2.c:8543
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
Queue a hold frame.
Definition: channel.c:1191
#define IAX_IE_DNID
Definition: iax2.h:143
unsigned short callno
Definition: iax2.h:283
enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
Apply a set of rules to a given IP address.
Definition: acl.c:799
Structure to describe a channel "technology", ie a channel driver See for examples: ...
Definition: channel.h:628
int ast_sockaddr_hash(const struct ast_sockaddr *addr)
Computes a hash value from the address. The port is ignored.
Definition: netsock2.c:548
Core PBX routines and definitions.
int ast_sockaddr_apply_netmask(const struct ast_sockaddr *addr, const struct ast_sockaddr *netmask, struct ast_sockaddr *result)
Apply a netmask to an address and store the result in a separate structure.
Definition: netsock2.c:357
iax2_format capability
Definition: chan_iax2.c:756
uint16_t limit
Definition: chan_iax2.c:1044
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel's frame queue.
Definition: channel.c:1139
list of actions registered
Definition: manager.c:1825
int lastms
Definition: chan_iax2.c:621
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:445
static int __do_deliver(void *data)
Definition: chan_iax2.c:3387
#define IAX_RECVCONNECTEDLINE
Definition: chan_iax2.c:526
enum calltoken_peer_enum calltoken_required
Definition: chan_iax2.c:575
int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
Determines if an ACL is empty or if it contains entries.
Definition: acl.c:540
unsigned int direction
Definition: parser.h:122
static int peer_status(struct iax2_peer *peer, char *status, int statuslen)
peer_status: Report Peer status in character string
Definition: chan_iax2.c:3839
int pokefreqok
Definition: chan_iax2.c:624
Format Capabilities API.
long target_extra
Definition: jitterbuf.h:72
int first_iax_message
Definition: chan_iax2.c:766
int destroy_initiated
Definition: chan_iax2.c:931
void ast_sha1_hash(char *output, const char *input)
Produces SHA1 hash based on input string.
Definition: utils.c:266
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:291
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
Definition: sched.c:238
jitterbuf: an application-independent jitterbuffer jitterbuf.c
#define IAX_IE_CALLTOKEN
Definition: iax2.h:185
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
unsigned char reg
Definition: chan_iax2.c:1036
static int iax2_queue_hangup(int callno)
Queue a hangup frame on the ast_channel owner.
Definition: chan_iax2.c:3369
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:8057
iax2_format voiceformat
Definition: chan_iax2.c:748
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
Definition: linkedlists.h:225
ast_aes_decrypt_key mydcx
Definition: chan_iax2.c:873
#define IAX_IE_CALLED_NUMBER
Definition: iax2.h:131
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.
int ast_connected_line_parse_data(const unsigned char *data, size_t datalen, struct ast_party_connected_line *connected)
Parse connected line indication frame data.
Definition: channel.c:8785
const ast_string_field outkey
Definition: chan_iax2.c:599
IAX2 Provisioning protocol.
#define AST_OPTION_DIGIT_DETECT
int curauthreq
Definition: chan_iax2.c:570
unsigned short ts
Definition: iax2.h:273
int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
Acknowledge a timer event.
Definition: timing.c:171
#define IAX_IE_ADSICPE
Definition: iax2.h:142
#define IAX_MAX_OSPBLOCK_SIZE
Definition: iax2.h:192
int new_msgs
Definition: mwi.h:459
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:137
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
unsigned int final
Definition: parser.h:120
uint16_t cur
Definition: chan_iax2.c:1031
#define IAX_CODEC_NOCAP
Definition: chan_iax2.c:514
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: utils.c:2199
#define IAX_TRUNK
Definition: chan_iax2.c:501
int pokeexpire
Definition: chan_iax2.c:620
Implementation of Inter-Asterisk eXchange, version 2 parser.c parser.h chan_iax2.c.
Support for dynamic strings.
Definition: strings.h:623
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
unsigned int transfer
Definition: parser.h:118
#define AST_OPTION_RXGAIN
const ast_string_field parkinglot
Definition: chan_iax2.c:561
Media Format Bitfield Compatibility API.
void * data
Definition: parser.h:104
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition: astobj2.h:1578
const ast_string_field peer
Definition: chan_iax2.c:861
int iax2_codec_pref_string(struct iax2_codec_pref *pref, char *buf, size_t size)
Dump audio codec preference list into a string.
Definition: codec_pref.c:178
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:532
static struct chan_iax2_pvt * iaxs[IAX_MAX_CALLS]
an array of iax2 pvt structures
Definition: chan_iax2.c:1173
unsigned short ts
Definition: iax2.h:251
int plan
Q.931 Type-Of-Number and Numbering-Plan encoded fields.
Definition: channel.h:293
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
static char * handle_cli_iax2_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Show one peer in detail.
Definition: chan_iax2.c:3862
unsigned short callno
Definition: parser.h:100
enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf)
set jitterbuf conf
Definition: jitterbuf.c:825
static struct ast_netsock_list * outsock
Definition: chan_iax2.c:367
struct ast_acl_list * ast_free_acl_list(struct ast_acl_list *acl)
Free a list of ACLs.
Definition: acl.c:233
#define IAX_IE_AUTHMETHODS
Definition: iax2.h:144
unsigned char cmddata
Definition: iax2.h:266
struct iax2_peer * peerpoke
Definition: chan_iax2.c:884
struct ast_mwi_subscriber * mwi_event_sub
Definition: chan_iax2.c:630
struct stasis_message_type * ast_endpoint_state_type(void)
Message type for endpoint state changes.
int ani2
Automatic Number Identification 2 (Info Digits)
Definition: channel.h:433
const ast_string_field inkeys
Definition: chan_iax2.c:861
Definition: file.c:69
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
Definition: chan_iax2.c:940
Connected Line/Party information.
Definition: channel.h:456
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
ast_aes_encrypt_key ecx
Definition: parser.h:134
const ast_string_field name
int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
Turn on music on hold on a given channel.
Definition: channel.c:7766
ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags, struct ast_sockaddr *src_addr)
Wrapper around recvfrom(2) that uses struct ast_sockaddr.
Definition: netsock2.c:606
Do not require call token validation.
Definition: chan_iax2.c:545
unsigned int ts
Definition: parser.h:110
int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
Get the IP address given a hostname.
Definition: acl.c:999
void iax2_codec_pref_append(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing)
Append a audio codec to a preference list, removing it first if it was already there.
Definition: codec_pref.c:420
unsigned int ast_format_cap_get_format_framing(const struct ast_format_cap *cap, const struct ast_format *format)
Get the framing for a format.
Definition: format_cap.c:443
#define AST_LIST_LAST(head)
Returns the last entry contained in a list.
Definition: linkedlists.h:429
#define IAX_IE_PASSWORD
Definition: iax2.h:137
#define ast_module_ref(mod)
Hold a reference to the module.
Definition: module.h:457
const char * ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
Get the names of codecs of a set of formats.
Definition: format_cap.c:734
#define IAX_ALLOWFWDOWNLOAD
Definition: chan_iax2.c:523
struct ast_format_cap * capabilities
Definition: channel.h:632
#define IAX_IE_MSGCOUNT
Definition: iax2.h:154
const ast_string_field ani
Definition: chan_iax2.c:861
unsigned char devname[16]
Definition: iax2.h:298
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
Definition: test_acl.c:111
unsigned int stop
Definition: res_smdi.c:217
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 defaultsoc...
Definition: chan_iax2.c:12866
const ast_string_field host
Definition: chan_iax2.c:861
uint64_t flags
Definition: chan_iax2.c:886
static int manager_iax2_show_peers(struct mansession *s, const struct message *m)
callback to display iax peers in manager
Definition: chan_iax2.c:7345
#define IAX_TRANSFERMEDIA
Definition: chan_iax2.c:520
void stasis_subscription_cb_noop(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Stasis subscription callback function that does nothing.
Definition: stasis.c:809
struct iax2_thread::@125 ffinfo
static void parse_dial_string(char *data, struct parsed_dial_string *pds)
Parses an IAX dial string into its component parts.
Definition: chan_iax2.c:5092
#define AST_MAX_CONTEXT
Definition: channel.h:135
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
Definition: utils.c:928
int retries
Definition: parser.h:108
union ast_frame::@224 data
#define IAX_IE_RR_OOO
Definition: iax2.h:182
#define AST_LIST_HEAD_INIT(head)
Initializes a list head structure.
Definition: linkedlists.h:626
char * command
Definition: cli.h:186
int datalen
Definition: parser.h:106
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define AST_OPTION_SECURE_SIGNALING
static int user_hash_cb(const void *obj, const int flags)
Definition: chan_iax2.c:2032
#define AST_CHANNEL_NAME
Definition: channel.h:171
struct ast_endpoint * endpoint
Definition: chan_iax2.c:635
unsigned int ast_format_get_default_ms(const struct ast_format *format)
Get the default framing size (in milliseconds) for a format.
Definition: format.c:359
unsigned char iseqno
Definition: iax2.h:235
#define AST_OPTION_AUDIO_MODE
int ast_str2cos(const char *value, unsigned int *cos)
Convert a string to the appropriate COS value.
Definition: acl.c:952
#define IAX_IE_CODEC_PREFS
Definition: iax2.h:175
int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if two capabilities structures are identical.
Definition: format_cap.c:687
int ast_register_switch(struct ast_switch *sw)
Register an alternative dialplan switch.
Definition: pbx_switch.c:58
static int set_config(const char *config_file, int reload, int forced)
Load configuration.
Definition: chan_iax2.c:13646
int retrytime
Definition: parser.h:112
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2541
#define IAX_RTIGNOREREGEXPIRE
Definition: chan_iax2.c:518
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5144
int maxms
Definition: chan_iax2.c:622
static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, const unsigned char *, int, int)
Definition: chan_iax2.c:7856
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1134
static void user_destructor(void *obj)
Free all memory associated with a user.
iax2_format chosenformat
Definition: chan_iax2.c:790
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr)
Determine if this is an IPv4-mapped IPv6 address.
Definition: netsock2.c:507
#define IAX_IMMEDIATE
Definition: chan_iax2.c:524
#define IAX_NOTRANSFER
Definition: chan_iax2.c:502
struct timeval trunkact
Definition: chan_iax2.c:647
const ast_string_field cid_num
Definition: chan_iax2.c:599
An API for managing task processing threads that can be shared across modules.
unsigned int inheritance
Definition: datastore.h:69
Definition: crypto.h:39
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:427
int retrans
Definition: parser.h:130
int ast_timer_fd(const struct ast_timer *handle)
Get a poll()-able file descriptor for a timer.
Definition: timing.c:161
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition: timing.c:166
int(* ast_sched_cb)(const void *data)
scheduler callback
Definition: sched.h:178
#define IAX_IE_OSPTOKEN
Definition: iax2.h:184
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
Definition: sched.c:567
int encmethods
Definition: parser.h:132
structure to hold users read from users.conf
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
Definition: stringfields.h:555
Structure used to handle boolean flags.
Definition: utils.h:199
struct iax_rr remote_rr
Definition: chan_iax2.c:923
Definition: md5.h:26
struct timeval delivery
void ast_channel_callid_set(struct ast_channel *chan, ast_callid value)
struct ast_sockaddr addr
Definition: chan_iax2.c:1029
static int user_cmp_cb(void *obj, void *arg, int flags)
Definition: chan_iax2.c:2043
const ast_string_field context
Definition: chan_iax2.c:599
int pokefreqnotok
Definition: chan_iax2.c:625
const char * usage
Definition: cli.h:177
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
int eff_auth_method
Definition: chan_iax2.c:867
int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service)
Get the IP address given a hostname and optional service.
Definition: acl.c:896
static const char * auth_method_labels[]
Name of effective auth method.
Definition: chan_iax2.c:399
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
void ast_endpoint_blob_publish(struct ast_endpoint *endpoint, struct stasis_message_type *type, struct ast_json *blob)
Creates and publishes a ast_endpoint_blob message.
struct ast_frame ast_null_frame
Definition: main/frame.c:79
static char * handle_cli_iax2_set_mtu(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Set trunk MTU from CLI.
Definition: chan_iax2.c:4007
#define IAX_IE_CALLINGTON
Definition: iax2.h:169
void ast_free_ha(struct ast_ha *ha)
Free a list of HAs.
Definition: acl.c:222
iax2_format capability
Definition: chan_iax2.c:616
#define IAX_IE_REFRESH
Definition: iax2.h:149
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
#define IAX_RTAUTOCLEAR
Definition: chan_iax2.c:517
struct ast_sockaddr addr
Definition: chan_iax2.c:776
char order[IAX2_CODEC_PREF_SIZE]
Definition: codec_pref.h:36
void * data
Definition: datastore.h:66
int transit_network_select
Transit Network Select.
Definition: channel.h:397
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681
char hold_signaling
Definition: chan_iax2.c:935
int maxauthreq
Definition: chan_iax2.c:569
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69
#define IAX_IE_CALLINGPRES
Definition: iax2.h:168
int ast_taskprocessor_push(struct ast_taskprocessor *tps, int(*task_exe)(void *datap), void *datap) attribute_warn_unused_result
Push a task into the specified taskprocessor queue and signal the taskprocessor thread.
struct iax2_thread::@126 full_frames
#define AST_OPTION_FAX_DETECT
static struct stasis_subscription * acl_change_sub
Definition: chan_iax2.c:328
#define IAX_ALREADYGONE
Definition: chan_iax2.c:507
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid, const char *application, void *data, void(*datad)(void *), const char *registrar)
Add and extension to an extension context.
Definition: pbx.c:6928
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
#define IAX_IE_RDNIS
Definition: iax2.h:158
Standard Command Line Interface.
int old_msgs
Definition: mwi.h:460
struct ast_sockaddr us
Definition: chan_iax2.c:694
int ast_sched_replace(int old_id, struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
replace a scheduler entry
Definition: sched.c:559
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:476
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag)
Add a rule to an ACL struct.
Definition: acl.c:429
int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
Allocate and initialize a DNS manager entry.
Definition: dnsmgr.c:191
#define IAX_IE_USERNAME
Definition: iax2.h:136
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235
ast_app: A registered application
Definition: pbx_app.c:45
unsigned char rseqno
Definition: chan_iax2.c:816
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"...
Definition: utils.c:2216
#define IAX_IE_RR_DROPPED
Definition: iax2.h:181
ast_callid ast_create_callid(void)
factory function to create a new uniquely identifying callid.
Definition: logger.c:2293
char secret[80]
Definition: chan_iax2.c:688
Channels have this property if they can accept input with jitter; i.e. most VoIP channels.
Definition: channel.h:960
#define IAX_KEYPOPULATED
Definition: chan_iax2.c:511
enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats)
get jitterbuf info: only "statistics" may be valid
Definition: jitterbuf.c:815
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7386
Asterisk MWI API.
struct ast_channel * owner
Definition: chan_iax2.c:808
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
struct ast_party_dialed::@206 number
Dialed/Called number.
const ast_string_field mailbox
Definition: chan_iax2.c:599
struct ast_sockaddr addr
Definition: chan_iax2.c:686
static int check_srcaddr(struct ast_sockaddr *addr)
Check if address can be used as packet source.
Definition: chan_iax2.c:12843
int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control, const void *data, size_t datalen)
Queue a control frame with payload.
Definition: channel.c:1238
#define DEFAULT_TRUNKDATA
Definition: chan_iax2.c:710
int stasis_subscription_accept_message_type(struct stasis_subscription *subscription, const struct stasis_message_type *type)
Indicate to a subscription that we are interested in a message type.
Definition: stasis.c:1023
struct ast_dnsmgr_entry * dnsmgr
Definition: chan_iax2.c:601
long min
Definition: jitterbuf.h:87
#define IAX_IE_RR_PKTS
Definition: iax2.h:179
uint64_t iax2_format_compatibility_best(uint64_t formats)
Pick the best format from the given bitfield formats.
Data structure associated with a single frame of data.
struct iax2_codec_pref prefs
Definition: chan_iax2.c:778
#define IAX_IE_CAUSE
Definition: iax2.h:152
Internal Asterisk hangup causes.
static int authenticate_request(int call_num)
Definition: chan_iax2.c:8157
#define IAX_MAXAUTHREQ
Definition: chan_iax2.c:521
static void delete_users(void)
Delete all users.
struct stasis_message * stasis_cache_get(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
Retrieve an item from the cache for the ast_eid_default entity.
Definition: stasis_cache.c:686
#define IAX_IE_RSA_RESULT
Definition: iax2.h:147
static char * auth_method_names(int authmethods, char *restrict buf)
Get names of all auth methods.
Definition: chan_iax2.c:415
Abstract JSON element (object, array, string, int, ...).
struct ast_key *AST_OPTIONAL_API_NAME() ast_key_get(const char *kname, int ktype)
return the ast_key structure for name
Definition: res_crypto.c:149
static int queue_signalling(struct chan_iax2_pvt *pvt, struct ast_frame *f)
All frames other than that of type AST_FRAME_IAX must be held until we have received a destination ca...
Definition: chan_iax2.c:2218
#define AST_OPTION_TONE_VERIFY
int oseqno
Definition: parser.h:126
int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
Check is see if a dnsmgr entry has changed.
Definition: dnsmgr.c:247
struct stasis_topic * ast_security_topic(void)
A stasis_topic which publishes messages for security related issues.
const ast_string_field cid_name
Definition: chan_iax2.c:599
struct ast_ha * ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
Add a new rule to a list of HAs.
Definition: acl.c:712
Definition: search.h:40
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:342
#define IAX_IE_CALLED_CONTEXT
Definition: iax2.h:135
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: extconf.c:2297
unsigned short zeros
Definition: iax2.h:271
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
enum ast_frame_type frametype
#define IAX_IE_APPARENT_ADDR
Definition: iax2.h:148
static struct ast_channel * ast_iax2_new(int callno, int state, iax2_format capability, struct iax2_codec_pref *prefs, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, unsigned int cachable)
Create new call, interface with the PBX core.
Definition: chan_iax2.c:5906
long jitter
Definition: jitterbuf.h:86
const ast_string_field parkinglot
Definition: chan_iax2.c:599
ast_aes_encrypt_key tdcx
Definition: chan_iax2.c:898
Generic container type.
static struct call_number_pool callno_pool
Definition: chan_iax2.c:966
static int __find_callno(unsigned short callno, unsigned short dcallno, struct ast_sockaddr *addr, int new, int sockfd, int return_locked, int check_dcallno)
Definition: chan_iax2.c:3155
iax2_format svideoformat
Definition: chan_iax2.c:754
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:279
void jb_destroy(jitterbuf *jb)
destroy jitterbuf
Definition: jitterbuf.c:99
Call Parking and Pickup API Includes code and algorithms from the Zapata library. ...
Media Format Bitfield Compatibility API.
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition: format_cap.c:400
unsigned short callno
Definition: chan_iax2.c:782
#define IAX_SENDCONNECTEDLINE
Definition: chan_iax2.c:525
struct ast_context * ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar)
Register a new context or find an existing one.
Definition: pbx.c:6149
#define AST_LIST_INSERT_BEFORE_CURRENT(elm, field)
Inserts a list entry before the current entry during a traversal.
Definition: linkedlists.h:599
struct ast_format * format
void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid)
copy a string representation of the callid into a target string
Definition: logger.c:2288
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.
Definition: chan_iax2.c:14432
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
static int iax2_prov_app(struct ast_channel *chan, const char *data)
Definition: chan_iax2.c:12417
const ast_string_field context
Definition: chan_iax2.c:861
unsigned int lastsent
Definition: chan_iax2.c:760
#define IAX_IE_TRANSFERID
Definition: iax2.h:157
void ast_shrink_phone_number(char *n)
Shrink a phone number in place to just digits (more accurately it just removes ()'s, .'s, and -'s...
Definition: callerid.c:1101
struct timeval rxtrunktime
Definition: chan_iax2.c:645
The structure that contains MWI state.
Definition: mwi.h:455
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:191
void ast_endpoint_shutdown(struct ast_endpoint *endpoint)
Shutsdown an ast_endpoint.
#define IAX_IE_FORMAT
Definition: iax2.h:139
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
unsigned short dcallno
Definition: parser.h:102
void iax2_codec_pref_convert(struct iax2_codec_pref *pref, char *buf, size_t size, int right)
Shift an audio codec preference list up or down 65 bytes so that it becomes an ASCII string...
Definition: codec_pref.c:44
Bridging API.
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition: channel.h:1258
static struct iax2_user * realtime_user(const char *username, struct ast_sockaddr *addr)
Definition: chan_iax2.c:4549
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
Asterisk module definitions.
struct ast_format * iax2_codec_pref_index(struct iax2_codec_pref *pref, int idx, struct ast_format **result)
Codec located at a particular place in the preference index.
Definition: codec_pref.c:77
static int iax2_queue_hold(int callno, const char *musicclass)
Queue a hold frame on the ast_channel owner.
Definition: chan_iax2.c:3323
MD5 digest functions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout)
unconditionally get frames from jitterbuf until empty
Definition: jitterbuf.c:801
struct ast_mwi_subscriber * ast_mwi_subscribe_pool(const char *mailbox, stasis_subscription_cb callback, void *data)
Add an MWI state subscriber, and stasis subscription to the mailbox.
Definition: mwi.c:235
int ast_format_cap_get_compatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2, struct ast_format_cap *result)
Find the compatible formats between two capabilities structures.
Definition: format_cap.c:628
static int trunk_nmaxmtu
Definition: chan_iax2.c:318
enum calltoken_peer_enum calltoken_required
Definition: chan_iax2.c:633
static struct ast_flags globalflags
Definition: app_minivm.c:699
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2385
Persistent data storage (akin to *doze registry)
int frames_received
Definition: chan_iax2.c:929
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
static struct ao2_container * callno_limits
Definition: chan_iax2.c:1009
long current
Definition: jitterbuf.h:88
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:297
Endpoint abstractions.
unsigned char metacmd
Definition: iax2.h:265
static int peer_cmp_cb(void *obj, void *arg, int flags)
Definition: chan_iax2.c:2020
void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
Sets the HANGUPCAUSE hash and optionally the SIP_CAUSE hash on the given channel. ...
Definition: channel.c:4346
struct ast_sockaddr transfer
Definition: chan_iax2.c:894
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
Definition: sched.c:271
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
#define IAX_RTUPDATE
Definition: chan_iax2.c:516
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:524
#define IAX_IE_CAPABILITY
Definition: iax2.h:138
long max_contig_interp
Definition: jitterbuf.h:71
#define AST_BRIDGE_DTMF_CHANNEL_0
Report DTMF on channel 0.
Definition: channel.h:2370
Timing source management.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
const ast_string_field peercontext
Definition: chan_iax2.c:599
Structure for mutex and tracking information.
Definition: lock.h:135
int64_t iax2_format
Definition: iax2.h:224
int iseqno
Definition: parser.h:128
Media Format Cache API.
int ast_app_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs)
Determine number of new/old messages in a mailbox.
Definition: main/app.c:604
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
Definition: netsock2.c:280
struct ast_sockaddr defaddr
Definition: chan_iax2.c:610
unsigned int refresh
void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
Copy the contents of one HA to another.
Definition: acl.c:255
unsigned int ts
Definition: iax2.h:233
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259
void jb_reset(jitterbuf *jb)
reset jitterbuf
Definition: jitterbuf.c:72
unsigned int outoforder
Definition: parser.h:114
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
#define IAX_TEMPONLY
Definition: chan_iax2.c:500
static struct ao2_container * peercnts
Definition: chan_iax2.c:1006
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
Definition: manager.c:3431
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:342
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
struct io_context * io_context_create(void)
Creates a context Create a context for I/O operations Basically mallocs an IO structure and sets up s...
Definition: io.c:81