Mobile roaming for yourself. Creating a GSM gate to asterisk + dongle from a mobile operator

  • Tutorial
It so 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 this at all and use roaming without bothering with expenses.
I’m not going to those countries and not so long to buy a SIM card of a local operator, but to those countries where Wi-Fi is almost everywhere.
It just so happened that lately I got to know the asterisk PBX programmatically and before the next trip I thought about how cool it would be to stick my SIM card as an incoming trunk in asterisk, standing in the snowy Moscow remaining behind, and cling to it myself customer over the internet. I don’t want to call it myself, would you have the Internet, and take calls to your own number, which is important for those who have a lot of contacts (you won’t notify everyone, and half will forget) - a cell phone with a SIM card is actually in your home region.

How is this implemented - under the cut.

The first thing that is necessary is:

from intangible

- At least some basic knowledge of Linux. Yes, you’ll have to collect a little from the source.
- The real IP address directly to Linux, well, or port forwarding to it. Still, you need to join the asterisk from the Internet somewhere. What if you put it behind a home router, which itself stands behind the masquerade of the provider, or even not one, it will not work.
Ports that the asterisk uses and on which you need to do the forwarding: udp 5000-31000. Here is such a whole pool.

from quite material

- A modem from a mobile provider. Almost all of them are huawey 3G E-series repainted in “corporate colors”. The megaphone does not even hesitate to call them exactly in accordance with the coding of the manufacturer. I turned out like this: . This is a Huawey 3G E173.
- Server on Linux

necessary software:

- asterisk:
I will tell you according to version 11 from the source, if your distribution has an asterisk package of any version - put the package, there is not much difference.
- usb-modeswitch:
both archives and -20121109.tar.bz2
If your distribution has such a package - put the package
- chan_dongle: for the 11th asterisk that will be discussed, or .com / p / asterisk-chan-dongle / downloads / listif you put an asterisk from packages and you have an earlier version. This is the same driver version, just in the one for which the patch is integrated for the 11th version that customizes it to the 11th asterisk with its changed dependencies.


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

Stick one into 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 it's not so simple.
These extremely user-friendly modems are designed so that after sticking them into usb it's a flash drive. Which automatically starts the installation of drivers, which in turn are either installed or skipped, if they are already installed, after which they switch the device to modem mode. Very convenient and user-oriented. Under Windows. Our case is not the same and this device sticks out in flash drive mode. What usb_modeswitch will help us with.
Run (after standard installation ./configure && make && make install if from source)
usb_modeswitch -v 0x12d1 -p 0x1446 -H -s 5 -M 555342430000000000000000000000110600000000000000000000000000000000

do not forget to exchange 12d1 and 1446 with your numbers from lsusb!
Run lsusb again and you will see there about the same 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 same option about the USB Serial Driver kernel should work, and several devices / dev / ttyUSB0 / dev / ttyUSB1, etc. should appear in / dev
These are the interfaces to the modem, some sound, some controls, as far as I understand which one of them depends on the modem.

A third of the work has been done, we have a digital interface to the cellular network, we turn to the 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 -dev is also needed, because we will collect the chan_ module for it.

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

Run asterisk directly without configuration.
We start asterisk -rv and we get to the console.
We write module load
The module will load and most likely will begin to swear about something.
But by running 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 xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxx


Open for editing the file /etc/asterisk/dongle.conf and at the very bottom where the device is described [dongle0] 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 add
imei = xxxxxxxxxxxxxxxx
context = dongle-incoming; context
group = 0
rxgain = 3; volume increase
txgain = 3; volume increase
resetdongle = yes; discard the card during initialization
u2diag = -1
usecallingpres = yes
callingpres = allowed_passed_screen

Now the chan_dongle module can be written into /etc/asterisk/modules.conf as the line load =>
so that it shakes when asterisk starts.
Restart asterisk.
Now, according to the correct imei, chan_dongle itself must find everything correctly - and which of the devices are command, and which audio, and so on.
We start asterisk -rv, we execute dongle show devices and we see our modem. Everything is good.

Now to setting up the asterisk.

All we need is in the sip.conf and extensions.conf files.
Let's start with sip.conf.
Find the [general] section and add it:
externip = x.x.x.x; the external aypishnik of the asterisk, it doesn’t matter whether he or the gate that forwards ports to it, is an external aypishnik, the address of which will be packets that go to the asterisk and he will consider them his
externaddr = x.x.x.x; the same
localnet = /; a network that the asterisk will consider internal and not arrange complex dances with a masquerade for it
nat = force_rport, comedia; necessary mode of operation if the asterisk is behind the nat and ports are thrown to it
context = public; The default context for calls. it is important that there be some context in which the call plan will not be completely defined, so that all calls by default do not get anywhere! the Internet is full of bots that scan day and night for poorly tuned asterisk to call through them for free
allowguest = no; do not receive calls is unknown from whom.

The default setting is finished, now in the same file we describe one client (our soft phone)
type = friend
host = dynamic
secret = password
context = default
canreinvite = yes
dtmfmode = rfc2833
permit = /
qualify = yes

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

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

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

What we need is to describe two contexts: [dongle-incoming] - what to do with calls coming into the modem and [default] - what to do with calls coming from software. phone.
We start with a simple one:
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 when the destination phone fits the 7-digit pattern, the second when + 7 digits, the third 8 digits and 4th 007 digits (in many soft phones there is 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 is called at the “hang up” event and just hangs up. However, you can’t even write it, this is a formality.

To make it easier to understand how this works, any incoming call from the phone registered in the asterisk under the me account will be processed in the [default] context, as indicated in the context option for [me]. In the context of [default], the destination number will be checked against 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 does not fall into the template, the call will be dropped as it is not known where it is being routed, you can try to call somewhere like +4 ... if you're interested. Also, there’s no way to protect yourself from telephone scammers, although they won’t call Uganda if they somehow get through.

Now the context [dongle-incoming], for calls arriving at a cellular number:
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 ()

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 ()

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

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

It is still simpler here - any incoming call is transmitted as a call to the client [me].
The upper 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 you can Then read (UTF-8 encoding).


That’s briefly all. But this is only the very foundation, if someone is really interested in how, or it won’t work out for himself (after all, those who can repeat all this, I’m 95% sure they can do it themselves, without instructions, and even supplement everything with everything not enough), I can continue developing this topic - how to configure sms so that they are received and sent to the soft client as well as calls; how to change dialplan so that the number is busy when the SIP client is not connected, or the caller gets into Voicemail.

PS. As a result of just one day of setup, I received my own phone number abroad on which I could not spend 200 rubles of money laid before leaving, although I used it constantly and called and received calls to relatives. An incomparable experience of using with roaming where money could be thrown like in a furnace of a diesel locomotive.

PPS WARNING, DISCLAIMER: the information arrived:
“The fact is that the use of any gateways is expressly prohibited by all mobile operators. Here is the “Terms and Conditions for the Provision of MTS Communication Services”:

Clause 1.3 MTS communication services cannot be used by the Subscriber without additional written agreement with the Operator for ... installing gateways for accessing the network
telecommunications and Internet telephony ... The

same points exist in the conditions of other mobile operators, because the margin on roaming is off scale, and operators will not refuse such a gain. For using such a gateway, they can simply block the SIM card. ”

Also popular now: