📜 ⬆️ ⬇️

Himself cellular roaming. Creating a GSM gate on asterisk + dongle from a cellular operator

It happened that I go on business trips and vacations not so often to use some kind of international telephony or virtual provider, but not so rarely that I don’t bother with it at all and use roaming without bothering about expenses.
I go not to those countries and not so long to buy a SIM card from a local operator, but to those countries where Wi-Fi is almost everywhere.
It just so happened that lately I had a close acquaintance with the asterisk software PBX and before the next trip I thought about how cool it would be to insert my SIM card as an incoming trunk in asterisk standing in the snowy Moscow that was left behind, and to cling to it sip client on the Internet. I don’t want to call it myself, there would be internet, and take calls to your own number, which is important for those who have many contacts (you don’t inform everyone, and half will forget) - the cellular phone with a SIM card is actually at home region.

How it is implemented - under the cut.

The first thing you need to do is:


from intangible

- At least some basic knowledge of Linux. yes, it is necessary to a little bit from sources.
- Real ip address right on Linux, well, or port forwarding to it. Still, to join from the Internet to an asterisk is necessary somewhere. What, if you put it behind a home router, which itself stands behind the provider’s masquerade, or even more than one, will not work.
Ports that uses an asterisk and which should be forwarded: udp 5000-31000. Here is a whole pool.

from quite material

- Modem from cellular provider. Almost all of them - huawey 3G E-series repainted in “corporate colors”. Megaphone does not even hesitate to call them exactly in accordance with the encoding of the manufacturer. I found myself like this: moscow.megafon.ru/devices/usbModem/e173 . This is a Huawey 3G E173.
- Server on Linux
')
necessary software:

- asterisk: www.asterisk.org/downloads
I will tell about version 11 of the sources, if there is an asterisk package of any version in your distribution kit - put the package, there is not much difference.
- usb-modeswitch: www.draisberghof.de/usb_modeswitch/#download
both archives www.draisberghof.de/usb_modeswitch/usb-modeswitch-1.2.5.tar.bz2 and www.draisberghof.de/usb_modeswitch/usb-modeswitch-data-20121109.tar.bz2
If there is such a package in your distribution kit - put the package
- chan_dongle: github.com/jstasiak/asterisk-chan-dongle/archive/asterisk11.zip for the 11th asterisk in question, or code.google.com/p/asterisk-chan-dongle/downloads/list if you put an asterisk from packages and you have an earlier version. This is the same version of the driver, just into the one that is integrated for the 11th version of the patch that drives it under the 11th asterisk with its modified dependencies.

So.


You have Linux, you have a dongle.
For those who have a self-assembly core - it needs an item
Device Drivers -> USB Support -> USB Serial Converter Support -> USB Generic Serial Driver


Vtykate one to another, do lsusb.
Find there a line similar to
Bus 001 Device 004: ID 12d1 : 1446 Huawei Technologies Co., Ltd.

12d1 is a vendor (huawey)
1446 is a device (E173 in my case)
But not everything is so simple.
These extremely user-friendly modems are designed so that after you plug them into usb, this is a flash drive. Which autorun installs drivers that, in turn, are either installed or skipped, if they already are, then switch the device to modem mode. Very user friendly and user friendly. Under Windows. We have the same case and this device sticks out in flash drive mode. With what usb_modeswitch will help us.
Run (after the standard installation ./configure && make && make install if from sources)
usb_modeswitch -v 0x12d1 -p 0x1446 -H -s 5 -M5553424300000000000000000000110600000000000000000000000000

don't forget to change 12d1 and 1446 to your lsusb numbers!
Run lsusb again and you'll see about the same thing as before, but the second 4 digits of your dongle will be different.
This means that everything went well and now your modem is a modem.
This is where the USB Serial Driver for the kernel should work and several devices / dev / ttyUSB0 / dev / ttyUSB1, etc. should appear in / dev.
These are interfaces to the modem, some sound, some controls, as far as I understood which one of them depends on the modem.

We have done a third of the case, we have a digital interface to the cellular network, we go to an asterisk.


I will not describe its installation, we will assume that it is installed (not configured, just installed). If from the source, then good. If the package is needed, you will also need -dev, because we will build a chan_ module for it.

Namely that chan_dongle.
Everything is standard, unpack, ./configure && make && make install
Copy the file etc / dongle.conf to / etc / asterisk /

Run asterisk directly without setting.
Run asterisk -rv and get into the console.
We write module load chan_dongle.so.
The module will load and most likely start swearing about something.
But when you run the dongle command, show devices will show something like

ID Group State RSSI Mode Submode Provider Name Model Firmware IMEI IMSI Number
dc_4823_7851 0 Free 11 0 0 MegaFon RUS E173 11.126.15.00.209 xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxx

we leave

We open for editing the file /etc/asterisk/dongle.conf and at the very bottom, where the device [dongle0] is described
We comment on everything except the imei line, where we enter the imei of your modem from the line shown by dongle show devices
and append
[dongle0]
imei = xxxxxxxxxxxxxxxx
context = dongle-incoming; context
group = 0
rxgain = 3; volume up
txgain = 3; volume up
resetdongle = yes; reset card upon initialization
u2diag = -1
usecallingpres = yes
callingpres = allowed_passed_screen

Now the chan_dongle module can be entered into /etc/asterisk/modules.conf as a load => chan_dongle.so line.
so that it loads itself when running asterisk.
Restart asterisk.
Now, with the correct imei, chan_dongle should correctly find everything itself - and which of the devices are command, and which audio, and so on.
Run asterisk -rv, run the dongle show devices and see your modem. Everything is good.

Now to set up an asterisk.


All we need is in the files sip.conf and extensions.conf
Let's start with sip.conf
Find the [general] section and add:
[general]
externip = x.xxx; an external IP address of an asterisk, no matter what it’s or the gate that passes ports to it, this is an external IP address whose address will contain packets going to the asterisk and it will consider them as its own
externaddr = xhhh; same
localnet = 192.168.0.0 / 255.255.0.0; a network that the asterisk will consider to be internal and not to arrange difficult dances with a masquerade for it
nat = force_rport, comedia; the necessary mode of operation if the asterisk is after nat and ports are prokibyvayutsya to it
context = public; The context in which calls arrive by default. it is important that there was some context in which the plan of calls will be completely undefined, so that all calls by default will not get anywhere! the internet is full of bots that scan day and night for badly tuned asterisks to call for free via them
allowguest = no; Do not take calls unknown from anyone.

With the default setting completed, now in the same file we describe one client (our softphone)
[me]
type = friend
host = dynamic
secret = password
context = default
canreinvite = yes
dtmfmode = rfc2833
permit = 0.0.0.0 / 0.0.0.0
qualify = yes

me - this will be the login, the password where to set is clear

We write down sip.conf. It's all here.

Open extensions.conf - this is a plan of calls, a description of where and how calls are transmitted.

What we need is to describe two contexts: [dongle-incoming] - what to do with calls coming to the modem and [default] - what to do with calls coming from the software. phone.
We start with a simple:
[default]
exten => _7X., 1, Dial (Dongle / dongle0 / holdother: + $ {FILTER (0-9, $ {EXTEN})})
exten => _ + 7X., 1, Dial (Dongle / dongle0 / holdother: + $ {FILTER (0-9, $ {EXTEN})})
exten => _8X., 1, Dial (Dongle / dongle0 / holdother: + $ 7 {FILTER (0-9, $ {EXTEN: 1})})
exten => _007X., 1, Dial (Dongle / dongle0 / holdother: + $ 7 {FILTER (0-9, $ {EXTEN: 3})})
exten => h, 1, Hangup ()


These are actually 4 lines that do the same thing, only the first one when the destination phone matches the 7 digit pattern, the second one when + 7 digits, third 8 digits and 4 th 007 digits (many soft phones have the option to send “+” as “00” ).
And each line does the same thing - it transfers the call to the dongle0 channel (our modem) in the form of + 7 digits.
Well, the last line will be called up at the “hang up” event and simply hangs up. But you can not even write it, this is a formality.

So that it is understandable how it works - any incoming call from the phone registered on an asterisk under the me account will be processed in the context of [default], as specified in the context option for [me]. In the context of [default], the destination number will be matched with the template and, if it falls under one of the templates, the action “Dial (Dongle ...)” will be executed. By the way, if the destination number in the template does not fall, the call will be dropped as it is not known where it is directed, you can try to call somewhere like +4 ... if you are interested. Also, there is no way to protect against telephone scams, though they will not call to Uganda if they somehow make it through.

Now the context [dongle-incoming], for calls arriving at the cellular number:
[dongle-incoming-sms]
exten => sms, 1, Noop (Incoming SMS from $ {CALLERID (num)} $ {BASE64_DECODE ($ {SMS_BASE64})})
exten => sms, n, System (echo '$ {STRFTIME ($ {EPOCH} ,,% Y-% m-% d% H:% M:% S)} - $ {DONGLENAME} - $ {CALLERID (num )}: $ {BASE64_DECODE ($ {SMS_BASE64})} '>> /var/log/asterisk/sms.txt)
exten => sms, n, hangup ()

[dongle-incoming-ussd]
exten => ussd, 1, Noop (Incoming USSD: $ {BASE64_DECODE ($ {USSD_BASE64})})
exten => ussd, n, System (echo '$ {STRFTIME ($ {EPOCH} ,,% Y-% m-% d% H:% M:% S)} - $ {DONGLENAME}: $ {BASE64_DECODE ($ {USSD_BASE64})} '>> /var/log/asterisk/ussd.txt)
exten => ussd, n, hangup ()

[dongle-incoming]
include => dongle-incoming-sms
include => dongle-incoming-ussd

exten => _X., 1, Dial (SIP / me)
exten => h, 1, Hangup ()


It is still easier here - any incoming call is transmitted as a call for the client [me].
The top two sections included as include => dongle-incoming-sms accept sms and ussd respectively, and write them to plain text files /var/log/asterisk/sms.txt and /var/log/asterisk/ussd.txt where they can be then read (UTF-8 encoding).

Epilogue


Here is a brief and all. But this is only the very basis, if someone really wonders how, or if it doesn’t work out for itself (after all, those who can repeat it all, I am 95% sure, they can do it all themselves, without instructions, and even add to everything not enough), I can continue to develop this topic - how to set up sms so that they are received and sent to the soft client as well as calls; how to change dialplan so that the number was busy when the SIP client is not connected, or the caller got into Voicemail.

Ps. As a result of just one day of setting up, I received my own phone number abroad where I could not spend 200 rubles of money put before departure, although I used it constantly, and I called and received calls from my relatives. Incomparable experience with roaming where money could be thrown into the locomotive's furnace.

Pps. WARNING, DISCLAIMER: Received information:
“The fact is that the use of any gateways is expressly prohibited by all mobile operators. Here are the "Terms of MTS communication services":
static.mts.ru/uploadmsk/contents/1656/uslovia_msk_06042012.pdf

Clause 1.3 MTS communication services cannot be used by the Subscriber without additional written approval from the Operator for ... installation of gateways for network access
telecommunications and Internet telephony ...

The same points are in terms of other mobile operators, because the margin on roaming is going off scale, and operators will not refuse such a lot of money. For the use of such a gateway can simply block the SIM card. "

Source: https://habr.com/ru/post/160299/


All Articles