Checking if number exist or not on incoming calls


#1

I have RealTime asterisk setup. When the number is stored in database and I call the extension everything is okay.

The problem is when the number doesn’t exist. I want if number which is not existing to hear message like “number not exist” or some other action.

So, I have added this in func_odbc.conf

[HASVOICEMAIL]
dsn=asterisk
readsql=SELECT name FROM sipfriends WHERE name='${SQL_ESC(${ARG1})}'

and this in extensions.conf

[general]
static=yes
writeprotect=no
autofallthrough=no
clearglobalvars=no
priorityjumping=no

[lookupdundi]
switch => DUNDi/priv

[internal]

exten => 119,1,NoOp()
exten => 119,2,WaitExten(1)
exten => 119,3,Set(COUNT=${ODBC_HASVOICEMAIL(${CALLERID(num)})})
exten => 119,4,GotoIf($["${COUNT}" > "0"]?internal,5:no-voicemail,1)
exten => 119,5,VoicemailMain(${CALLERID(num)}@VoiceMail)

[no-voicemail]
exten => noVoiceMail,1,Handup()


include => dundiextens

include => lookupdundi

switch => Realtime

[incomingdundi]

exten => 119,1,NoOp(internal)
exten => 119,2,Playback(hello-world)

When I make a call in CLI I see this

[Dec 7 12:01:38] NOTICE[9133][C-00000031]: chan_sip.c:26449 handle_request_invite: Call from ‘’ (xxx.yyy.zzz.www:5060) to extension ‘119’ rejected because extension not found in context ‘default’.

parts of the /var/log/asterisk/full log

[Dec 7 12:00:53] DEBUG[9133] chan_sip.c: = Looking for Call ID: 2mdIf8lexOTBIMg2pPgKOBdDB3SowCcf (Checking From) --From tag PKcMBzp6yhIZQG-du1TsYkW1MPmX6L5V --To-tag
[Dec 7 12:00:53] DEBUG[9133] acl.c: For destination ‘xxx.yyy.zzz.www’, our source address is ‘xxx.yyy.zzz.www’.
[Dec 7 12:00:53] DEBUG[9133] chan_sip.c: Setting AST_TRANSPORT_UDP with address xxx.yyy.zzz.www:5060
[Dec 7 12:00:53] DEBUG[9133] netsock2.c: Splitting ‘xxx.yyy.zzz.www:5060’ into…
[Dec 7 12:00:53] DEBUG[9133] netsock2.c: …host ‘xxx.yyy.zzz.www’ and port ‘5060’.
[Dec 7 12:00:53] DEBUG[9133] chan_sip.c: Allocating new SIP dialog for 2mdIf8lexOTBIMg2pPgKOBdDB3SowCcf - INVITE (No RTP)
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: **** Received INVITE (5) - Command in SIP INVITE
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: Splitting ‘xxx.yyy.zzz.www:5060’ into…
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: …host ‘xxx.yyy.zzz.www’ and port ‘5060’.
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: Splitting ‘xxx.yyy.zzz.www’ into…
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: …host ‘xxx.yyy.zzz.www’ and port ‘’.
[Dec 7 12:00:53] DEBUG[9133][C-00000030] res_config_mysql.c: MySQL RealTime: Connection okay.
[Dec 7 12:00:53] DEBUG[9133][C-00000030] res_config_mysql.c: MySQL RealTime: Retrieve SQL: SELECT * FROM sipfriends WHERE name = ‘1002’ AND host = ‘dynamic’
[Dec 7 12:00:53] DEBUG[9133][C-00000030] res_config_mysql.c: MySQL RealTime: Connection okay.
[Dec 7 12:00:53] DEBUG[9133][C-00000030] res_config_mysql.c: MySQL RealTime: Retrieve SQL: SELECT * FROM sipfriends WHERE name = ‘1002’
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: We’re settling with these formats: (alaw)
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: Checking SIP call limits for device
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: Updating call counter for incoming call
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: Splitting ‘xxx.yyy.zzz.www:5060’ into…
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: …host ‘xxx.yyy.zzz.www’ and port ‘’.
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: Splitting ‘xxx.yyy.zzz.www’ into…
[Dec 7 12:00:53] DEBUG[9133][C-00000030] netsock2.c: …host ‘xxx.yyy.zzz.www’ and port ‘’.
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: Trying to put ‘SIP/2.0 404’ onto UDP socket destined for xxx.yyy.zzz.www:5060
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: Updating call counter for incoming call
[Dec 7 12:00:53] DEBUG[9133] chan_sip.c: = Looking for Call ID: 2mdIf8lexOTBIMg2pPgKOBdDB3SowCcf (Checking From) --From tag PKcMBzp6yhIZQG-du1TsYkW1MPmX6L5V --To-tag as2f1e4b11
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: **** Received ACK (6) - Command in SIP ACK
[Dec 7 12:00:53] DEBUG[9133][C-00000030] chan_sip.c: Stopping retransmission on ‘2mdIf8lexOTBIMg2pPgKOBdDB3SowCcf’ of Response 124: Match Found
[Dec 7 12:00:53] DEBUG[9133] chan_sip.c: Destroying SIP dialog 2mdIf8lexOTBIMg2pPgKOBdDB3SowCcf
[Dec 7 12:00:53] DEBUG[9133] rtp_engine.c: Destroyed RTP instance ‘0x7f7abc00e8a8’

The error in wireshark is 404 Not Found. Is this how the incoming calls are handled?


#2

There is nothing in your dialplan that at attempts to do what you say you want to do.

Sending number not found (404) is the expected behaviour.

Have you considered using the i extension? Note this is used for Goto and for the initial context, but not for Gosub.

Note that making a announcement will normally require answering the call, which will result in billing starting if the call is toll call for the originator. (Although early media would avoid this it is not generally available to external callers.)


#3

I’m sorry but I’m not sure I follow you. Is it possible to do this at all? To check the incoming number if exist or not?


#4

Asterisk always checks whether it exists. If it doesn’t, and the call arrived over SIP, it will return 404, unless there is an i extension, or an s extension, in which case it will run the first of these that it finds, trying both the original and default contexts.

You can also define patterns, such that all extensions exist, and then perform active checks on the number. I’m not sure why you would want to do an explicit check, given the automated process with i or s.

Note that the behaviour described in https://wiki.asterisk.org/wiki/display/AST/Special+Dialplan+Extensions differs. i certainly used to be more generally usable,but it is possible that it is now more restrictive, in which case you would need to use a pattern to catch all unknown extensions. Asterisk will always prefer a specific match to a pattern.


#5

It certainly looks to me that i will be used, in the initial context, although it is possible the fallback is only to e, and I didn’t check if it still falls back to the default context.


#6

Can you provide an example how can I use this “i” in dialplan? I can’t find anything on the net so far.


#7

https://wiki.asterisk.org/wiki/display/AST/Handling+Special+Extensions

http://the-asterisk-book.com/1.6/besondere-extensions.html

http://asteriskdocs.org/en/2nd_Edition/asterisk-book-html-chunk/asterisk-CHP-5-SECT-3.html#asterisk-CHP-5-SECT-3.2

https://wiki.asterisk.org/wiki/display/AST/Special+Dialplan+Extensions


#8

Just a FYI, In the dialplan you show the context you have created your 119 extension in is ‘internal’ not ‘default’ where that call is being sent to.


#9

@david551, those examples are IF there is already correct call placed or I don’t get them.

@johnkiniston, Yes, that is correct. 119 is in “internal” and accept all calls from numbers which are stored in database. Are you suggest that if I create [defaul] context and add my query which check db there will work?

I’ve tried that and still same outcome.

edit:
Actually this might work. I though that “default” context is exactly that - default and is used from backlogic in Asterisk. Didn’t tough that I can override it.

Will make some test and will write back.

edit2:

It is working with adding [default]

[default]
exten => 119,1,Playback(Not found)
exten => 119,2,WaitExten(2)
exten => 119,3,Hangup()