Call forwarding via IAX2 - prevent infinite loop


I’m looking for a possibility to prevent an infinite loop of call forwardings.


I have two users Alice and Bob. Alice has set up call forwarding to Bob. Bob has set up call forwarding to Alice. When someone calls either person then I have an infinite loop.

I would rather like to drop the call after a certain number of redirections.

Scenario 1 - Alice and Bob are both on the same Asterisk instance.

My current solution is to increment REDIRECTING(count) on each forward.
Set(REDIRECTING(count,i)=$[${REDIRECTING(count)} + 1])
If a certain limit is exceeded I drop the call.

Scenario 2 - Alice and Bob are on two separate Asterisk instances. Both Asterisk instances are connected via IAX2.

For this case my solution with REDIRECTING(count) doesn’t work.

  • Each time a call arrives in one Asterisk instance, REDIRECTING(count) is zero.
  • The number I store in REDIRECTING(from-num) on Asterisk instance A is accessible on Asterisk instance B at CALLERID(rdnis). But unfortunately I didn’t find a way to get the value of the redirecting counter.


  • How can I prevent infinite loops?
  • Am I doing it right in scenario 1?
  • What about scenario 2?

I tried to search on the internet on how to prevent infinite loops but unfortunately without success.

One idea/workaround/hack would be to introduce a waiting time (e.g. half a second) before processing each call. This would slow down the infinite loop considerably.

Best regards,

Good day

What about changing the caller ID each time it redirects and move the call to somewhere else when the pattern matches say 1111?

The caller id should identify the caller. Therefore I consider it to be fixed.

Your proposal is now to encode additional information in the caller id? This looks like a workaround/hack to me - similar to the “waiting time” I mentioned above. (If I didn’t understand your post correctly please explain a bit more.)

Thanks for you idea. I might consider it if there is no ‘real solution’.

It is a workaround/hack

The wait is great! You might also consider adding a prefix to the calls when you forward them. So instead of dialing 123, try muhfwd-123, and split that up on the receiving server.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.