# Re-writing MyNetFone destination SIP headers

- URL: https://blog.jourdant.me/post/freeswitch-rewriting-mynetfone-sip-headers-to-show-correct-destination
- Author: Jourdan Templeton
- Published: 2014-12-11T12:11:59.382Z
- Updated: 2015-01-31T22:07:21.960Z
- Category: FreeSWITCH
- Tags: FreeSWITCH, VoIP
- Summary: How to pull the dialled number out of MyNetFone’s Diversion header in FreeSWITCH so those calls follow the same dialplan rules as my other providers.

Internal numbering, or internal extensions are common practice among SIP providers for having multiple DID numbers on a single SIP registration.

[MyNetFone](https://www.mynetfone.com.au/) is a good example. You register to MyNetFone using your assigned internal extension, eg, 0946xxxx. In my case, I have two regional DID numbers assigned to my account.
</br>
### Problem

Inbound calls to these regional numbers show the following in the SIP INVITE packet:
```
To: <sip:0946xxxx@sip01.mynetfone.com.au>
```
How can I find which number was dialled and re-write the **To:** header? I want FreeSWITCH to route these calls through the same rules as my numbers from other providers. I want to route these numbers to different destinations if required.
</br>

### Solution
After some investigation I discovered MyNetFone places the "dialled" number in the **Diversion:** header shown below:
```
Diversion: <sip:6186102xxxx@xxx.xxx.xxx.xxx>;reason=unconditional
```

As mentioned, I have multiple numbers routing through a common set of dialplan rules; requiring me to normalise ([E.164](http://en.wikipedia.org/wiki/E.164)) all incoming calls. This structure makes it very easy to re-write the header.

The first XML dialplan in my public SIP profile is "00_normalisation.xml". An excerpt is shown below that re-writes the Diversion header:

```markup
<include>
	<extension name="mynetfone_normalisation" continue="true">
		<condition field="destination_number" expression="0946xxxx" require-nested="true" break="on-false">
			<condition field="${sip_h_Diversion}" expression="^(?:<sip:)?(\d+)(?:@.+)?$">
				<action application="set" data="sip_h_Diversion=null"/>
				<action application="transfer" data="$1 XML public"/>
			</condition>
		</condition>
	</extension>
	...
    
</include>
```

**Note: Place this rule at the top of the file for normalisation to occur after the transfer.**

Also for anyone interested, I have included my SIP provider configuration I use for MyNetFone; it's nice and simple:

```markup
<include>
	<gateway name="MyNetFone">
    	<param name="username" value="0946xxxx"/>
        <param name="password" value="xxxxxxxxxxxxxxxxxxx"/>
		<param name="realm" value="sip01.mynetfone.com.au"/>
		<param name="proxy" value="sip01.mynetfone.com.au"/>
		<param name="register" value="true"/>
		<param name="expire-seconds" value="120"/>
	</gateway>
</include>
```

</br>
Jourdan

