Outgoing calls work after new start but do fail later Deutsche Telekom

Hello,
I run Asterisk on a RaspberryPi2 and I am quite happy with it. I have however one problem…
May provider ist Deutsche Telekom. I have several Computers, cellphones and tablets that connect to the raspberrypi running asterisk. They can all phone each other. They can also do outgoing calls via Deutsche Telekom, but only after a new start of Asterisk. (I am currently not interested in incomming calls)

After several hours outgoing calls do not work any longer. A restart of Asterisk solves the problem for several hours again.

I did a tcpdump and wireshark trace and found the reason (but I dont know the solution)

Asterisk performs a registration every 8 minutes with Deutsche Telekom.
Before each registration asterisk does a DNS query for tel.t-online.de
That is answered with two entries such as b-epp-002.isp.t-ipnet.de and s-epp-002.isp.t-ipnet.de.

The entry with the lower priority is used for the registration. OK. In this case s-epp-002.isp.t-ipnet.de -> 217.0.23.5

So it registers succesfully to 217.0.23.5. Fine.

sip show peers shows alo this address (217.0.23.5)

If I do an outgoing call then it goes also to this address. (217.0.23.5), but without an additional DNS query.

After some hours Deutsche Telekom changes the answer to the periodical register DNS query.
That means, I am now registered with a new, different server of Deutsche Telekom.

If I now try an outgoing call, asterisk continues to use the old IP-adress (217.0.23.5) It seems to have stored/remembered this old adress. Deutsche Telekom says: “No, you can only call the servers you are registered with. You are (no longer registered here” Forbidden!

If I restart Asterisk both addresses (for registration and for outgoing calls) are the same again and I can do outgoing calls until Deutsche Telekom changes its servers (and the DNS-answer) again.

Is there anything I can configure to request DNS queries for each outgoing call? Or is there another solution for my problem?

Reagards Alexander

The easiest solution would be to hard code one of the IP addresses.

ok, thanks. I will try with a fixed-set IP address instead of the URL.
But is that the official solution? I can not know why my provider changed the IP adresses, so if I use an IP address insead of an URL I could try to connect to a no longer existing address…

At the moment I have a special extension, when I call that it forces a restart of Asterisk to refresh the (remembered, now invalid) ip address. That works but is also not very handy.

Is there a technical reason for not doing DNS-queries for outgoing calls? (Only for registrations?)

Regards Alexander

The chan_sip module was never written to do so, it’s purely that.

OK, thanks.
This is my solution for the problem now. Maybe it can help others with the same issue?
I run th following script periodically in a cronjob at the same interval as the registration occurs.

#############################################################################################################
#                                                                                                           #
# Alexander Fritsch, 12.Apr.2017                                                                            #
#                                                                                                           #
# Deutsche Telekom changes the answer to DNS queries to tel-t-online.de from time to time (load-balancing?) #
# Asterisk registrations go always the new IP-address from that DNS query.                                  #
# Asterisk uses the very first learned IP-address for ALL outgoing calls however.                           #
# That leads to a mismatch between (periodicalla updated) registration address and outgoing call address    #
# Deutsche Telekom refuses outgoing calls to the old adress then.                                           #
#                                                                                                           #
# This script (run periodically as cron job) checks for this condition performs a "core restart"            #
# to sync again if necessary.                                                                               #
#                                                                                                           #
#############################################################################################################


URL=tel.t-online.de


REGISTER_URL=$(dig +short +noshort +identify SRV _sip._udp.$URL | sort -k 5 | awk '/_sip/{print $8}' | head -n1)
REGISTER_IP=$(dig +short $REGISTER_URL)

OUTGOING_IP=$(sudo asterisk -rx "sip show peers" | grep ext-sip-account | awk '{print $2}')

#echo Register_URL $REGISTER_URL Register_IP $REGISTER_IP Outgoiung_IP $OUTGOING_IP

if [ $REGISTER_IP != $OUTGOING_IP ]; then
   logger Asterisk Outgoing-IP-Check: Register_IP $REGISTER_IP != Outgoing_IP $OUTGOING_IP, performing core-restart to sync addresses again
   sudo asterisk -rx "core restart when convenient"
fi