re-structure SS7 code and MGW NAT code into separate modules

This repository now only contains the SS7 code
diff --git a/src/bssmap_codec.erl b/src/bssmap_codec.erl
new file mode 100644
index 0000000..61cf85e
--- /dev/null
+++ b/src/bssmap_codec.erl
@@ -0,0 +1,197 @@
+% GSM TS 08.08 / 3GPP TS 48.008 BSSMAP
+
+% (C) 2010 by Harald Welte <laforge@gnumonks.org>
+%
+% All Rights Reserved
+%
+% This program is free software; you can redistribute it and/or modify
+% it under the terms of the GNU Affero General Public License as
+% published by the Free Software Foundation; either version 3 of the
+% License, or (at your option) any later version.
+%
+% This program is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU Affero General Public License
+% along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+-module(bssmap_codec).
+-author('Harald Welte <laforge@gnumonks.org>').
+-include("bssmap.hrl").
+
+-export([parse_bssmap_msg/1, encode_bssmap_msg/1]).
+
+parse_bssmap_msg(<<MsgType:8, Remain/binary>>) ->
+	parse_bssmap_msgt(MsgType, Remain).
+
+parse_bssmap_msgt(MsgType, Msg) when is_integer(MsgType), is_binary(Msg) ->
+	IeList = parse_ies(Msg, []),
+	{bssmap_msg, MsgType, IeList}.
+
+parse_ies(<<>>, ParsedIeList) ->
+	ParsedIeList;
+parse_ies(Msg, ParsedIeList) when is_binary(Msg) ->
+	CurIe = binary:first(Msg),
+	% Parse current IE and append it to list of Parsed IEs
+	case is_tv_ie(CurIe) of
+		true ->
+			Res = parse_ie_tv(CurIe, Msg);
+		false ->
+			Res = parse_ie(CurIe, Msg)
+	end,
+	{ok, BytesConsumed, ParsedIe} = Res,
+	{CurIe, Payload} = ParsedIe,
+	DecodedIe = decode_ie(CurIe, Payload),
+	ParsedIeList1 = ParsedIeList ++ [DecodedIe],
+	%ParsedIeList1 = ParsedIeList ++ [ParsedIe],
+	RemainMsg = binary:part(Msg, BytesConsumed, byte_size(Msg)-BytesConsumed),
+	parse_ies(RemainMsg, ParsedIeList1).
+
+% check if this element is of TV type
+is_tv_ie(T) when 
+	T == ?BSSMAP_IE_NUMBER_OF_MSS;
+	T == ?BSSMAP_IE_PERIODICITY;
+	T == ?BSSMAP_IE_EXTD_RES_IND;
+	T == ?BSSMAP_IE_INTERF_BAND_TO_USE;
+	T == ?BSSMAP_IE_RR_CAUSE;
+	T == ?BSSMAP_IE_DLCI;
+	T == ?BSSMAP_IE_DOWNLINK_DTX_FLAG;
+	T == ?BSSMAP_IE_RESPONSE_RQST;
+	T == ?BSSMAP_IE_RES_IND_METHOD;
+	T == ?BSSMAP_IE_CM_INFO_T1;
+	T == ?BSSMAP_IE_CHOSEN_CHANNEL;
+	T == ?BSSMAP_IE_CIPH_RESP_MODE;
+	T == ?BSSMAP_IE_TRACE_TYPE;
+	T == ?BSSMAP_IE_TRACE_REFERENCE;
+	T == ?BSSMAP_IE_FORWARD_INDICATOR;
+	T == ?BSSMAP_IE_CHOSEN_ENCR_ALG;
+	T == ?BSSMAP_IE_CIRCUIT_POOL;
+	T == ?BSSMAP_IE_TIME_INDICATION;
+	T == ?BSSMAP_IE_CUR_CHAN_TYPE_1;
+	T == ?BSSMAP_IE_QUEUEING_IND;
+	T == ?BSSMAP_IE_SPEECH_VERSION;
+	T == ?BSSMAP_IE_ASS_REQUIREMENT;
+	T == ?BSSMAP_IE_EMLPP_PRIORITY;
+	T == ?BSSMAP_IE_CONFIG_EVO_INDI;
+	T == ?BSSMAP_IE_LSA_ACCESS_CTRL_SUPPR ->
+		true;
+is_tv_ie(_T) ->
+		false.
+
+% Parser for any non-TLV and non-TV IEs
+parse_ie(?BSSMAP_IE_CIRC_ID_CODE, Msg) ->
+	<<?BSSMAP_IE_CIRC_ID_CODE:8, Cic:16/big>> = Msg,
+	{ok, 3, {?BSSMAP_IE_CIRC_ID_CODE, Cic}};
+parse_ie(?BSSMAP_IE_CONN_REL_RQSTED, Msg) ->
+	<<?BSSMAP_IE_CONN_REL_RQSTED:8>> = Msg,
+	{ok, 1, {?BSSMAP_IE_CONN_REL_RQSTED, 1}};
+parse_ie(?BSSMAP_IE_RES_AVAIL, Msg) ->
+	<<?BSSMAP_IE_RES_AVAIL:8, ResAvail:8/binary>> = Msg,
+	{ok, 9, {?BSSMAP_IE_RES_AVAIL, ResAvail}};
+parse_ie(?BSSMAP_IE_TOT_RES_ACCESS, Msg) ->
+	<<?BSSMAP_IE_TOT_RES_ACCESS:8, ResAvail:4/binary>> = Msg,
+	{ok, 5, {?BSSMAP_IE_TOT_RES_ACCESS, ResAvail}};
+parse_ie(?BSSMAP_IE_TALKER_FLAG, Msg) ->
+	<<?BSSMAP_IE_TALKER_FLAG:8>> = Msg,
+	{ok, 1, {?BSSMAP_IE_TALKER_FLAG, 1}};
+% Default: Parser for TLV IE
+parse_ie(MsgType, Msg) ->
+	<<MsgType:8, Length:8, Value:Length/binary, _/binary>> = Msg,
+	{ok, 2+Length, {MsgType, Value}}.
+
+% Parser for simple Tag-Value IE
+parse_ie_tv(IeType, Msg) ->
+	<<IeType:8, Par:8>> = Msg,
+	{ok, 2, {IeType, Par}}.
+
+
+% FIXME
+encode_bssmap_msg(_) ->
+	ok.
+
+
+
+
+
+decode_ie(?BSSMAP_IE_CIRC_ID_CODE, <<Pcm:11, Ts:5>>) ->
+	{circuit_id, Pcm, Ts};
+decode_ie(?BSSMAP_IE_IMSI, Remain) ->
+	{imsi, bin_bcd2str(Remain)};
+decode_ie(?BSSMAP_IE_TMSI, <<Tmsi:32>>) ->
+	{tmsi, Tmsi};
+decode_ie(?BSSMAP_IE_L3_HDR_INFO, <<Pdisc:8, Tid:8>>) ->
+	{l3_hdr_info, Pdisc, Tid};
+decode_ie(?BSSMAP_IE_ENCR_INFO, <<Algos:8, Key/binary>>) ->
+	{encr_info, Algos, Key};
+decode_ie(?BSSMAP_IE_CHANNEL_TYPE, <<_:4, Spdi:4, RateType:8, Remain/binary>>) ->
+	{chan_type, Spdi, RateType, Remain};
+decode_ie(?BSSMAP_IE_EXTD_RES_IND, Ri) ->
+	<<_:6, Sm:1, Tarr:1>> = <<Ri>>,
+	{extended_ri, Sm, Tarr};
+decode_ie(?BSSMAP_IE_TOT_RES_ACCESS, <<NumFr:16/big, NumHr:16/big>>) ->
+	{tot_res_access, NumFr, NumHr};
+decode_ie(?BSSMAP_IE_CELL_ID, <<_Spare:4, Discr:4, Remain/binary>>) ->
+	{cell_id, decode_cid_ie(Discr, Remain)};
+decode_ie(?BSSMAP_IE_PRIORITY, <<_:1, Pci:1, Prio:4, Qa:1, Pvi:1>>) ->
+	{priority, Pci, Prio, Qa, Pvi};
+decode_ie(?BSSMAP_IE_CELL_ID_LIST, <<_Spare:4, Discr:4, Remain/binary>>) ->
+	{cell_id_list, decode_cid_list(Discr, Remain, [])};
+decode_ie(?BSSMAP_IE_DIAGNOSTIC, <<ErrPtr:8, _:4, BitPtr:4, MsgRecv/binary>>) ->
+	{diagnostic, ErrPtr, BitPtr, MsgRecv};
+decode_ie(?BSSMAP_IE_CHOSEN_CHANNEL, Int) ->
+	<<Mode:4, Chan:4>> = <<Int:8>>,
+	{chosen_channel, Mode, Chan};
+decode_ie(?BSSMAP_IE_MOBILE_IDENTITY, Data) ->
+	% FIXME
+	fixme;
+% Default: don't decode
+decode_ie(IeI, Data) ->
+	{IeI, Data}.
+
+decode_cid_ie(?CELL_ID_WHOLE_GLOBAL, Remain) ->
+	<<Mcc2:4, Mcc1:4, Mnc3:4, Mcc3:4, Mnc2:4, Mnc1:4, Lac:16/big, Ci:16/big>> = Remain,
+	[{mcc, [Mcc1, Mcc2, Mcc3]}, {mnc, [Mnc1, Mnc2, Mnc3]}, {lac, Lac}, {cid, Ci}];
+decode_cid_ie(?CELL_ID_LAC_AND_CI, Remain) ->
+	<<Lac:16/big, Ci:16/big>> = Remain,
+	[{lac, Lac}, {cid, Ci}];
+decode_cid_ie(?CELL_ID_CI, Remain) ->
+	<<Ci:16/big>> = Remain,
+	[{cid, Ci}];
+decode_cid_ie(?CELL_ID_NO_CELL, _Remain) ->
+	[];
+decode_cid_ie(?CELL_ID_UTRAN_PLMN_LAC_RNC, Remain) ->
+	<<Mcc2:4, Mcc1:4, Mnc3:4, Mcc3:4, Mnc2:4, Mnc1:4, Lac:16/big, Rnc:16/big>> = Remain,
+	[{mcc, [Mcc1, Mcc2, Mcc3]}, {mnc, [Mnc1, Mnc2, Mnc3]}, {lac, Lac}, {rnc_id, Rnc}];
+decode_cid_ie(?CELL_ID_UTRAN_RNC, Remain) ->
+	<<Rnc:16/big>> = Remain,
+	[{rnc_id, Rnc}];
+decode_cid_ie(?CELL_ID_UTRAN_LAC_RNC, Remain) ->
+	<<Lac:16/big, Rnc:16/big>> = Remain,
+	[{lac, Lac}, {rnc_id, Rnc}].
+
+decode_cid_list(Discr, Data, List) ->
+	case Discr of
+		?CELL_ID_WHOLE_GLOBAL -> Len = 7;
+		?CELL_ID_LAC_AND_CI ->	 Len = 4;
+		?CELL_ID_CI ->		 Len = 2;
+		?CELL_ID_NO_CELL ->	 Len = 0;
+		?CELL_ID_UTRAN_PLMN_LAC_RNC -> Len = 7;
+		?CELL_ID_UTRAN_RNC ->	Len = 2;
+		?CELL_ID_UTRAN_LAC_RNC -> Len = 4
+	end,
+	<<Subset:Len/binary, Remain/binary>> = Data,
+	Elem = {cell_id, decode_cid_ie(Discr, Subset)},
+	decode_cid_list(Discr, Remain, List ++ [Elem]).
+
+
+
+bin_bcd2str(BcdBin) when is_binary(BcdBin) ->
+	bin_bcd2str(BcdBin, []).
+bin_bcd2str(<<>>, List) ->
+	List;
+bin_bcd2str(BcdBin, List) ->
+	<<Nibble:4, Remain/bitstring>> = BcdBin,
+	Char = "0" + Nibble,
+	bin_bcd2str(Remain, List ++ [Char]).
diff --git a/src/isup.hrl b/src/isup.hrl
deleted file mode 100644
index 55750ea..0000000
--- a/src/isup.hrl
+++ /dev/null
@@ -1,111 +0,0 @@
-% Table 1 / Q.762 - ISDN user part message acronyms
-% Table C-3 / Q.762
--define(ISUP_MSGT_ACM, 2#00000110).	%  Address complete
--define(ISUP_MSGT_ANM, 2#00001001).	%  Answer
-%-define(ISUP_MSGT_APM).	%  Application transport
--define(ISUP_MSGT_BLA, 2#00010101).	%  Blocking acknowledgement
--define(ISUP_MSGT_BLO, 2#00010011).	%  Blocking
--define(ISUP_MSGT_CCR, 2#00010001).	%  Continuity check request
--define(ISUP_MSGT_CFN, 2#00101111).	%  Confusion
--define(ISUP_MSGT_CGB, 2#00011000).	%  Circuit group blocking
--define(ISUP_MSGT_CGBA,2#00011010).%  Circuit group blocking acknowledgement
--define(ISUP_MSGT_CGU, 2#00011001).	%  Circuit group unblocking
--define(ISUP_MSGT_CGUA,2#00011011).%  Circuit group unblocking acknowledgement
--define(ISUP_MSGT_CON, 2#00000111).	%  Connect
--define(ISUP_MSGT_COT, 2#00000101).	%  Continuity
--define(ISUP_MSGT_CPG, 2#00101100).	%  Call progress
--define(ISUP_MSGT_CRG, 2#00110001).	%  Charge information
--define(ISUP_MSGT_CQM, 2#00101010).	%  Circuit group query
--define(ISUP_MSGT_CQR, 2#00101011).	%  Circuit group query response
--define(ISUP_MSGT_DRS, 2#00100111).	%  Delayed release (reserved – used in 1988 version)
--define(ISUP_MSGT_FAA, 2#00100000).	%  Facility accepted
-%-define(ISUP_MSGT_FAC).	%  Facility
--define(ISUP_MSGT_FAR, 2#00011111).	%  Facility request
--define(ISUP_MSGT_FOT, 2#00001000).	%  Forward transfer
--define(ISUP_MSGT_FRJ, 2#00100001).	%  Facility reject
--define(ISUP_MSGT_GRA, 2#00101001).	%  Circuit group reset acknowledgement
--define(ISUP_MSGT_GRS, 2#00010111).	%  Circuit group reset
--define(ISUP_MSGT_IAM, 2#00000001).	%  Initial address
-%-define(ISUP_MSGT_IDR).	%  Identification request
-%-define(ISUP_MSGT_IRS).	%  Identification response
--define(ISUP_MSGT_INF, 2#00000100).	%  Information
--define(ISUP_MSGT_INR, 2#00000011).	%  Information request
--define(ISUP_MSGT_LPA, 2#00100100).	%  Loop back acknowledgement
-%-define(ISUP_MSGT_LOP).	%  Loop prevention
-%-define(ISUP_MSGT_NRM).	%  Network resource management
--define(ISUP_MSGT_OLM, 2#00110000).	%  Overload
--define(ISUP_MSGT_PAM, 2#00101000).	%  Pass-along
-%-define(ISUP_MSGT_PRI).	%  Pre-release information
--define(ISUP_MSGT_REL, 2#00001100).	%  Release
--define(ISUP_MSGT_RES, 2#00001110).	%  Resume
--define(ISUP_MSGT_RLC, 2#00010000).	%  Release complete
--define(ISUP_MSGT_RSC, 2#00010010).	%  Reset circuit
--define(ISUP_MSGT_SAM, 2#00000010).	%  Subsequent address
-%-define(ISUP_MSGT_SDM).	%  Subsequent directory number
-%-define(ISUP_MSGT_SGM).	%  Segmentation
--define(ISUP_MSGT_SUS, 2#00001101).	%  Suspend
--define(ISUP_MSGT_UBL, 2#00010100).	%  Unblocking
--define(ISUP_MSGT_UBA, 2#00010110).	%  Unblocking acknowledgement
--define(ISUP_MSGT_UCIC,2#00101110).%  Unequipped circuit identification code
-%-define(ISUP_MSGT_UPA).	%  User part available
-%-define(ISUP_MSGT_UPT).	%  User part test
--define(ISUP_MSGT_USR, 2#00101101).	%  User-to-user information
-
-
-% TABLE C-4/Q.767
--define(ISUP_PAR_ACC_TRANSP,		2#00000011).	% Access transport
--define(ISUP_PAR_AUT_CONG_LVL,		2#00100111).	% Automatic congestion level
--define(ISUP_PAR_BACKW_CALL_IND,	2#00010001).	% Backward call indicators
--define(ISUP_PAR_CALL_MOD_IND,		2#00010111).	% Call modification indicators
--define(ISUP_PAR_CALL_REF,		2#00000001).	% Call reference
--define(ISUP_PAR_CALLED_P_NUM,		2#00000100).	% Called party number
--define(ISUP_PAR_CALLING_P_NUM,		2#00001010).	% Calling party number
--define(ISUP_PAR_CALLING_P_CAT,		2#00001001).	% Calling party category
--define(ISUP_PAR_CAUSE_IND,		2#00010010).	% Cause indicators
--define(ISUP_PAR_CG_SUB_MSGT_IND,	2#00010101).	% Circuit group supervision message type indicator
--define(ISUP_PAR_CIRC_STATE_IND,	2#00100110).	% Circuit state indicator
--define(ISUP_PAR_CLSD_U_INTERL_CODE,	2#00011010).	% Closed user interlock code
--define(ISUP_PAR_CONNECTED_NUM,		2#00100001).	% Connected number
--define(ISUP_PAR_CONN_REQ,		2#00001101).	% Connection request
--define(ISUP_PAR_CONTINUITY_IND,	2#00010000).	% Continuity idnicators
--define(ISUP_PAR_END_OF_OPT,		2#00000000).	% End of optional parameters
--define(ISUP_PAR_EVENT_INFO,		2#00100100).	% Event information
--define(ISUP_PAR_FACILITY_IND,		2#00011000).	% Facility indicators
--define(ISUP_PAR_FW_CALL_IND,		2#00000111).	% Forward call indicators
--define(ISUP_PAR_INFO_IND,		2#00001111).	% Information indicators
--define(ISUP_PAR_INFO_REQ_IND,		2#00001110).	% Information request indicatos
--define(ISUP_PAR_NAT_OF_CONN_IND,	2#00000110).	% Nature of connection indicators
--define(ISUP_PAR_OPT_BW_CALL_IND,	2#00101001).	% Optional backward call indicators
--define(ISUP_PAR_OPT_FW_CALL_IND,	2#00001000).	% Optional forward call indicators
--define(ISUP_PAR_ORIG_CALLED_NUM,	2#00101000).	% Original called number
--define(ISUP_PAR_RANGE_AND_STATUS,	2#00010110).	% Range and status
--define(ISUP_PAR_REDIR_NUM,		2#00001011).	% Redirecting number
--define(ISUP_PAR_REDIR_INFO,		2#00010011).	% Redirection information
--define(ISUP_PAR_REDIRECTION_NUM,	2#00001100).	% Redirection number
--define(ISUP_PAR_SIGN_POINT_CODE,	2#00011110).	% Signalling point code
--define(ISUP_PAR_SUBSEQ_NUM,		2#00000101).	% Subsequent number
--define(ISUP_PAR_SUSP_RES_IND,		2#00100010).	% Suspend/resume indicators
--define(ISUP_PAR_XMIT_NET_SEL,		2#00100011).	% Transmit network selection
--define(ISUP_PAR_TRANSM_MED_REQ,	2#00000010).	% Transmission medium requirement
--define(ISUP_PAR_USER_SERV_INFO,	2#00011101).	% User service information
--define(ISUP_PAR_USER_USER_IND,		2#00101010).	% User-to-user indicators
--define(ISUP_PAR_USER_USER_INFO,	2#00100000).	% User-to-user information
-
--define(ISUP_ADDR_NAT_NATIONAL,		2#0000011).	% National (significant) number
--define(ISUP_ADDR_NAT_INTERNATIONAL,	2#0000100).	% International number
-
--record(party_number, {
-	nature_of_addr_ind,
-	internal_net_num,	% only in called party
-	number_incompl_ind,	% only in calling party
-	numbering_plan,
-	present_restrict,	% only in calling party
-	screening_ind,		% only in calling party
-	phone_number}
-	).
-
--record(isup_msg, {
-	msg_type,
-	cic,
-	parameters
-	}).
diff --git a/src/m2ua.hrl b/src/m2ua.hrl
deleted file mode 100644
index 26f10fc..0000000
--- a/src/m2ua.hrl
+++ /dev/null
@@ -1,59 +0,0 @@
-
-% RFC 3331 Section 3.1.3 Message Class
--define(M2UA_MSGC_MGMT,		0).	% Management Messages [IUA/M2UA/M3UA/SUA]
--define(M2UA_MSGC_TRANSFER,	1).	% Transfer Messages [M3UA]
--define(M2UA_MSGC_SSNM,		2).	% SS7 Signalling Network Management [M3UA/SUA]
--define(M2UA_MSGC_ASPSM,	3).	% ASP State Maintenance [IUA/M2UA/M3UA/SUA]
--define(M2UA_MSGC_ASPTM,	4).	% ASP Traffic Maintenance [IUA/M2UA/M3UA/SUA]
--define(M2UA_MSGC_QPTM,		5).	% Q.921/Q.931 Boundary Primitives Transport [IUA]
--define(M2UA_MSGC_MAUP,		6).	% MTP2 User Adaption [M2UA]
--define(M2UA_MSGC_CONNLESS,	7).	% Connectionless Messages [SUA]
--define(M2UA_MSGC_CONN,		8).	% Connection oriented messages [SUA]
--define(M2UA_MSGC_RKM,		9).	% Routing Key Management [M3UA]
--define(M2UA_MSGC_IIM,		10).	% Interface Identifier Management (M2UA)
-
-% RFC 3331 Section 3.1.4 Message Type
--define(M2UA_MAUP_MSGT_RESERVED,	0).
--define(M2UA_MAUP_MSGT_DATA,		1).
--define(M2UA_MAUP_MSGT_EST_REQ,		2).
--define(M2UA_MAUP_MSGT_EST_CONF,	3).
--define(M2UA_MAUP_MSGT_REL_REQ,		4).
--define(M2UA_MAUP_MSGT_REL_CONF,	5).
--define(M2UA_MAUP_MSGT_REL_IND,		6).
--define(M2UA_MAUP_MSGT_STATE_REQ,	7).
--define(M2UA_MAUP_MSGT_STATE_CONF,	8).
--define(M2UA_MAUP_MSGT_STATE_IND,	9).
--define(M2UA_MAUP_MSGT_DATA_RETR_REQ,	10).
--define(M2UA_MAUP_MSGT_DATA_RETR_CONF,	11).
--define(M2UA_MAUP_MSGT_DATA_RETR_IND,	12).
--define(M2UA_MAUP_MSGT_DATA_RETR_COMPL_IND,	13).
--define(M2UA_MAUP_MSGT_CONG_IND,	14).
--define(M2UA_MAUP_MSGT_DATA_ACK,	15).
-
-
--define(M2UA_ASPSM_MSGT_UP,		0).
--define(M2UA_ASPSM_MSGT_DOWN,		1).
--define(M2UA_ASPSM_MSGT_BEAT,		2).
--define(M2UA_ASPSM_MSGT_UP_ACK,		3).
--define(M2UA_ASPSM_MSGT_DOWN_ACK,	5).
--define(M2UA_ASPSM_MSGT_BEAT_ACK,	6).
-
--define(M2UA_ASPTM_MSGT_ACTIVE,		1).
--define(M2UA_ASPTM_MSGT_INACTIVE,	2).
--define(M2UA_ASPTM_MSGT_ACTIVE_ACK,	3).
--define(M2UA_ASPTM_MSGT_INACTIVE_ACK,	4).
-
--define(M2UA_MGMT_MSGT_ERROR,		0).
--define(M2UA_MGMT_MSGT_NOTIFY,		1).
-
--define(M2UA_MGMT_IIM_REG_REQ,		1).
--define(M2UA_MGMT_IIM_REG_RSP,		2).
--define(M2UA_MGMT_IIM_DEREG_REQ,	3).
--define(M2UA_MGMT_IIM_DEREG_RSP,	4).
-
--record(m2ua_msg, {
-	msg_class,
-	msg_type,
-	parameters
-	}).
-
diff --git a/src/mgw_nat.erl b/src/mgw_nat.erl
deleted file mode 100644
index 39c8abe..0000000
--- a/src/mgw_nat.erl
+++ /dev/null
@@ -1,294 +0,0 @@
-% 
-
-% (C) 2011 by Harald Welte <laforge@gnumonks.org>
-% (C) 2011 OnWaves
-%
-% All Rights Reserved
-%
-% This program is free software; you can redistribute it and/or modify
-% it under the terms of the GNU Affero General Public License as
-% published by the Free Software Foundation; either version 3 of the
-% License, or (at your option) any later version.
-%
-% This program is distributed in the hope that it will be useful,
-% but WITHOUT ANY WARRANTY; without even the implied warranty of
-% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-% GNU General Public License for more details.
-%
-% You should have received a copy of the GNU Affero General Public License
-% along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
--module(mgw_nat).
--author("Harald Welte <laforge@gnumonks.org>").
--export([mangle_rx_data/3]).
-
-%-include_lib("kernel/include/inet.hrl").
-%-include_lib("kernel/include/inet_sctp.hrl").
-
--include("m2ua.hrl").
--include("mtp3.hrl").
--include("isup.hrl").
--include("sccp.hrl").
-
-% mangle the received data
-mangle_rx_data(L, From, Data) when is_binary(Data) ->
-	{ok, M2ua} = m2ua_codec:parse_m2ua_msg(Data),
-	%io:format("M2UA Decode: ~p~n", [M2ua]),
-	case M2ua of
-		#m2ua_msg{msg_class = ?M2UA_MSGC_MAUP,
-			  msg_type = ?M2UA_MAUP_MSGT_DATA} ->
-			M2ua_out = mangle_rx_m2ua_maup(L, From, M2ua);
-		#m2ua_msg{} ->
-			% simply pass it along unmodified
-			M2ua_out = M2ua
-	end,
-	% re-encode the data
-	%io:format("M2UA Encode: ~p~n", [M2ua_out]),
-	m2ua_codec:encode_m2ua_msg(M2ua_out).
-
-% mangle the received M2UA
-mangle_rx_m2ua_maup(L, From, M2ua = #m2ua_msg{parameters = Params}) ->
-	{_Len, M2uaPayload} = proplists:get_value(16#300, Params),
-	Mtp3 = mtp3_codec:parse_mtp3_msg(M2uaPayload),
-	%io:format("MTP3 Decode: ~p~n", [Mtp3]),
-	Mtp3_out = mangle_rx_mtp3(L, From, Mtp3),
-	%io:format("MTP3 Encode: ~p~n", [Mtp3_out]),
-	Mtp3OutBin = mtp3_codec:encode_mtp3_msg(Mtp3_out),
-	Params2 = proplists:delete(16#300, Params),
-	ParamsNew = Params2 ++ [{16#300, {byte_size(Mtp3OutBin), Mtp3OutBin}}],
-	% return mangled parsed m2ua msg
-	M2ua#m2ua_msg{parameters = ParamsNew}.
-
-% mangle the MTP3 payload
-mangle_rx_mtp3(L, From, Mtp3 = #mtp3_msg{service_ind = Service}) ->
-	mangle_rx_mtp3_serv(L, From, Service, Mtp3).
-
-% mangle the ISUP content
-mangle_rx_mtp3_serv(_L, From, ?MTP3_SERV_ISUP, Mtp3 = #mtp3_msg{payload = Payload}) ->
-	io:format("ISUP In: ~p~n", [Payload]),
-	Isup = isup_codec:parse_isup_msg(Payload),
-	io:format("ISUP Decode: ~p~n", [Isup]),
-	% FIXME
-	IsupMangled = mangle_rx_isup(From, Isup#isup_msg.msg_type, Isup),
-	if IsupMangled == Isup ->
-		Mtp3;
-	   true ->
-		io:format("ISUP Encode In: ~p~n", [IsupMangled]),
-		Payload_out = isup_codec:encode_isup_msg(IsupMangled),
-		io:format("ISUP Encode Out: ~p~n", [Payload_out]),
-		% return modified MTP3 payload
-		Mtp3#mtp3_msg{payload = Payload_out}
-	end;
-% mangle the SCCP content
-mangle_rx_mtp3_serv(_L, From, ?MTP3_SERV_SCCP, Mtp3 = #mtp3_msg{payload = Payload}) ->
-	io:format("SCCP In: ~p~n", [Payload]),
-	{ok, Sccp} = sccp_codec:parse_sccp_msg(Payload),
-	io:format("SCCP Decode: ~p~n", [Sccp]),
-	SccpMangled = mangle_rx_sccp(From, Sccp#sccp_msg.msg_type, Sccp),
-	SccpMasqued = sccp_masq:sccp_masq_msg(From, SccpMangled#sccp_msg.msg_type,
-					      SccpMangled),
-	if SccpMasqued == Sccp ->
-		Mtp3;
-	   true ->
-		io:format("SCCP Encode In: ~p~n", [SccpMasqued]),
-		Payload_out = sccp_codec:encode_sccp_msg(SccpMasqued),
-		io:format("SCCP Encode Out: ~p~n", [Payload_out]),
-		% return modified MTP3 payload
-		Mtp3#mtp3_msg{payload = Payload_out}
-	end;
-% default: do nothing
-mangle_rx_mtp3_serv(_L, _From, _, Mtp3) ->
-	Mtp3.
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Actual mangling of the decoded SCCP messages
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-% iterate over list of rewrite tuples and apply translation if there is a match
-do_sccp_gt_rewrite(GT, _From, []) ->
-	GT;
-do_sccp_gt_rewrite(GT = #global_title{phone_number = PhoneNum}, from_stp, [Head|List]) ->
-	{MscSide, StpSide, Comment} = Head,
-	if PhoneNum == StpSide ->
-		NewPhoneNum = MscSide,
-		io:format("SCCP STP->MSC rewrite (~p) ~p -> ~p~n",
-			  [Comment, PhoneNum, NewPhoneNum]),
-		GT#global_title{phone_number = NewPhoneNum};
-	   true ->
-		do_sccp_gt_rewrite(GT, from_stp, List)
-	end;
-do_sccp_gt_rewrite(GT = #global_title{phone_number = PhoneNum}, from_msc, [Head|List]) ->
-	{MscSide, StpSide, Comment} = Head,
-	if PhoneNum == MscSide ->
-		NewPhoneNum = StpSide,
-		io:format("SCCP MSC->STP rewrite (~p) ~p -> ~p~n",
-			  [Comment, PhoneNum, NewPhoneNum]),
-		GT#global_title{phone_number = NewPhoneNum};
-	   true ->
-		do_sccp_gt_rewrite(GT, from_msc, List)
-	end.
-
-% mangle called address
-mangle_rx_called(from_stp, Addr = #sccp_addr{global_title = GT}) ->
-	{ok, RewriteTbl} = application:get_env(sccp_rewrite_tbl),
-	GTout = do_sccp_gt_rewrite(GT, from_stp, RewriteTbl),
-	Addr#sccp_addr{global_title = GTout};
-mangle_rx_called(_From, Addr) ->
-	Addr.
-
-% mangle calling address
-mangle_rx_calling(from_msc, Addr = #sccp_addr{global_title = GT}) ->
-	{ok, RewriteTbl} = application:get_env(sccp_rewrite_tbl),
-	GTout = do_sccp_gt_rewrite(GT, from_msc, RewriteTbl),
-	Addr#sccp_addr{global_title = GTout};
-mangle_rx_calling(_From, Addr) ->
-	Addr.
-
-mangle_rx_sccp(From, ?SCCP_MSGT_UDT, Msg = #sccp_msg{parameters = Opts}) ->
-	CalledParty = proplists:get_value(called_party_addr, Opts),
-	CalledPartyNew = mangle_rx_called(From, CalledParty),
-	CallingParty = proplists:get_value(calling_party_addr, Opts),
-	CallingPartyNew = mangle_rx_calling(From, CallingParty),
-	Opts1 = lists:keyreplace(called_party_addr, 1, Opts,
-				 {called_party_addr, CalledPartyNew}),
-	Opts2 = lists:keyreplace(calling_party_addr, 1, Opts1,
-				 {calling_party_addr, CallingPartyNew}),
-	Msg#sccp_msg{parameters = Opts2};
-mangle_rx_sccp(_From, _MsgType, Msg) ->
-	Msg.
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Actual mangling of the decoded ISUP messages 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-% iterate over list of parameters and call mangle_rx_isup_par() for each one
-mangle_rx_isup_params(_From, _MsgType, _Msg, ParListOut, []) ->
-	ParListOut;
-mangle_rx_isup_params(From, MsgType, Msg, ParListOut, [Par|ParList]) ->
-	ParOut = mangle_rx_isup_par(From, MsgType, Msg, Par),
-	mangle_rx_isup_params(From, MsgType, Msg, ParListOut++[ParOut], ParList).
-
-% manipulate phone numbers
-mangle_rx_isup_par(From, MsgType, _Msg, {ParType, ParBody}) when
-					ParType == ?ISUP_PAR_CALLED_P_NUM;
-					ParType == ?ISUP_PAR_CONNECTED_NUM;
-					ParType == ?ISUP_PAR_CALLING_P_NUM ->
-	NewParBody = mangle_isup_number(From, MsgType, ParType, ParBody),
-	{ParType, NewParBody};
-% defauly case: do not mangle this parameter
-mangle_rx_isup_par(_From, _MsgType, _Msg, Par) ->
-	Par.
-
-% mangle an incoming ISUP message
-mangle_rx_isup(From, MsgType, Msg = #isup_msg{parameters = Params}) ->
-	ParamsOut = mangle_rx_isup_params(From, MsgType, Msg, [], Params),
-	% return message with modified parameter list
-	Msg#isup_msg{parameters = ParamsOut}.
-
-% STP->MSC: Mangle a Party Number in IAM
-mangle_isup_number(from_stp, ?ISUP_MSGT_IAM, NumType, PartyNum) ->
-	case NumType of
-		?ISUP_PAR_CALLED_P_NUM ->
-			% First convert to international number, if it is national
-			Num1 = isup_party_internationalize(PartyNum,
-						application:get_env(intern_pfx)),
-			io:format("IAM MSRN rewrite (STP->MSC): "),
-			isup_party_replace_prefix(Num1,
-						application:get_env(msrn_pfx_stp),
-						application:get_env(msrn_pfx_msc));
-		_ ->
-			PartyNum
-	end;
-% MSC->STP: Mangle connected number in response to IAM
-mangle_isup_number(from_msc, MsgT, NumType, PartyNum) when MsgT == ?ISUP_MSGT_CON;
-							   MsgT == ?ISUP_MSGT_ANM ->
-	case NumType of
-		?ISUP_PAR_CONNECTED_NUM ->
-			io:format("CON MSRN rewrite (MSC->STP): "),
-			Num1 = isup_party_replace_prefix(PartyNum,
-						application:get_env(msrn_pfx_msc),
-						application:get_env(msrn_pfx_stp)),
-			% Second: convert to national number, if it is international
-			isup_party_nationalize(Num1,
-						application:get_env(intern_pfx));
-		_ ->
-			PartyNum
-	end;
-% MAC->STP: Mangle IAM international -> national
-mangle_isup_number(from_msc, ?ISUP_MSGT_IAM, NumType, PartyNum) ->
-	case NumType of
-		?ISUP_PAR_CALLED_P_NUM ->
-			isup_party_nationalize(PartyNum,
-						applicaiton:get_env(intern_pfx));
-		_ ->
-			PartyNum
-	end;
-% STP->MSC: Mangle connected number in response to IAM (national->international)
-mangle_isup_number(from_stp, MsgT, NumType, PartyNum) when MsgT == ?ISUP_MSGT_CON;
-							   MsgT == ?ISUP_MSGT_ANM ->
-	case NumType of
-		?ISUP_PAR_CONNECTED_NUM ->
-			isup_party_internationalize(PartyNum,
-						application:get_env(intern_pfx));
-		_ ->
-			PartyNum
-	end;
-% default case: no rewrite
-mangle_isup_number(from_msc, _, _, PartyNum) ->
-	PartyNum.
-
-% replace the prefix of PartyNum with NewPfx _if_ the current prefix matches MatchPfx
-isup_party_replace_prefix(PartyNum, MatchPfx, NewPfxInt) ->
-	IntIn = PartyNum#party_number.phone_number,
-	DigitsIn = osmo_util:int2digit_list(IntIn),
-	NewPfx = osmo_util:int2digit_list(NewPfxInt),
-	MatchPfxLen = length(MatchPfx),
-	Pfx = lists:sublist(DigitsIn, 1, MatchPfxLen),
-	if Pfx == MatchPfx ->
-		Trailer = lists:sublist(DigitsIn, MatchPfxLen+1, length(DigitsIn)-MatchPfxLen),
-		DigitsOut = NewPfx ++ Trailer,
-		io:format("Prefix rewrite: ~p -> ~p~n", [DigitsIn, DigitsOut]);
-	   true ->
-		io:format("Prefix rewrite: NO MATCH (~p != ~p)~n", [Pfx, MatchPfx]),
-		DigitsOut = DigitsIn
-	end,
-	IntOut = osmo_util:digit_list2int(DigitsOut),
-	PartyNum#party_number{phone_number = IntOut}.
-
-isup_party_internationalize(PartyNum, CountryCode) ->
-	#party_number{phone_number = IntIn, nature_of_addr_ind = Nature} = PartyNum,
-	DigitsIn = osmo_util:int2digit_list(IntIn),
-	case Nature of
-		?ISUP_ADDR_NAT_NATIONAL ->
-			DigitsOut = CountryCode ++ DigitsIn,
-			NatureOut = ?ISUP_ADDR_NAT_INTERNATIONAL,
-			io:format("Internationalize: ~p -> ~p~n", [DigitsIn, DigitsOut]);
-		_ ->
-			DigitsOut = DigitsIn,
-			NatureOut = Nature
-	end,
-	IntOut = osmo_util:digit_list2int(DigitsOut),
-	PartyNum#party_number{phone_number = IntOut, nature_of_addr_ind = NatureOut}.
-
-isup_party_nationalize(PartyNum, CountryCode) ->
-	#party_number{phone_number = IntIn, nature_of_addr_ind = Nature} = PartyNum,
-	DigitsIn = osmo_util:int2digit_list(IntIn),
-	CountryCodeLen = length(CountryCode),
-	case Nature of
-		?ISUP_ADDR_NAT_INTERNATIONAL ->
-			Pfx = lists:sublist(DigitsIn, CountryCodeLen),
-			if Pfx == CountryCode ->
-				DigitsOut = lists:sublist(DigitsIn, CountryCodeLen+1,
-							  length(DigitsIn)-CountryCodeLen),
-				NatureOut = ?ISUP_ADDR_NAT_NATIONAL,
-				io:format("Nationalize: ~p -> ~p~n", [DigitsIn, DigitsOut]);
-			   true ->
-				DigitsOut = DigitsIn,
-				NatureOut = Nature
-			end;
-		_ ->
-			DigitsOut = DigitsIn,
-			NatureOut = Nature
-	end,
-	IntOut = osmo_util:digit_list2int(DigitsOut),
-	PartyNum#party_number{phone_number = IntOut, nature_of_addr_ind = NatureOut}.
diff --git a/src/mgw_nat_app.erl b/src/mgw_nat_app.erl
deleted file mode 100644
index c4e32ea..0000000
--- a/src/mgw_nat_app.erl
+++ /dev/null
@@ -1,16 +0,0 @@
--module(mgw_nat_app).
--behavior(application).
--export([start/2, stop/1]).
-
--export([reload_config/0]).
-
-start(_Type, _Args) ->
-	Sup = mgw_nat_sup:start_link(),
-	io:format("Sup ~p~n", [Sup]),
-	Sup.
-
-stop(_State) ->
-	ok.
-
-reload_config() ->
-	osmo_util:reload_config().
diff --git a/src/mgw_nat_sup.erl b/src/mgw_nat_sup.erl
deleted file mode 100644
index 2dfe245..0000000
--- a/src/mgw_nat_sup.erl
+++ /dev/null
@@ -1,40 +0,0 @@
-% OTP Supervisor for MGW NAT
-
-% (C) 2011 by Harald Welte <laforge@gnumonks.org>
-% (C) 2011 OnWaves
-%
-% All Rights Reserved
-%
-% This program is free software; you can redistribute it and/or modify
-% it under the terms of the GNU Affero General Public License as
-% published by the Free Software Foundation; either version 3 of the
-% License, or (at your option) any later version.
-%
-% This program is distributed in the hope that it will be useful,
-% but WITHOUT ANY WARRANTY; without even the implied warranty of
-% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-% GNU General Public License for more details.
-%
-% You should have received a copy of the GNU Affero General Public License
-% along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
--module(mgw_nat_sup).
--behavior(supervisor).
-
--export([start_link/0]).
--export([init/1]).
-
-start_link() ->
-	supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-
-init(_Arg) ->
-	{ok, MscLocalIp} = application:get_env(msc_local_ip),
-	{ok, MscLocalPort} = application:get_env(msc_local_port),
-	{ok, MscRemoteIp} = application:get_env(msc_remote_ip),
-	{ok, StpRemoteIp} = application:get_env(stp_remote_ip),
-	{ok, StpRemotePort} = application:get_env(stp_remote_port),
-	SctpHdlrArgs =	[MscLocalIp, MscLocalPort, MscRemoteIp,
-			 StpRemoteIp, StpRemotePort],
-	MgwChild = {mgw_nat_usr, {mgw_nat_usr, start_link, [SctpHdlrArgs]},
-		    permanent, 2000, worker, [mgw_nat_usr, sctp_handler, mgw_nat]},
-	{ok,{{one_for_all,1,1}, [MgwChild]}}.
diff --git a/src/mgw_nat_usr.erl b/src/mgw_nat_usr.erl
deleted file mode 100644
index f738f7e..0000000
--- a/src/mgw_nat_usr.erl
+++ /dev/null
@@ -1,59 +0,0 @@
-% Wrapper code, wrapping sctp_handler.erl into OTP gen_server
-
-% (C) 2011 by Harald Welte <laforge@gnumonks.org>
-% (C) 2011 OnWaves
-%
-% All Rights Reserved
-%
-% This program is free software; you can redistribute it and/or modify
-% it under the terms of the GNU Affero General Public License as
-% published by the Free Software Foundation; either version 3 of the
-% License, or (at your option) any later version.
-%
-% This program is distributed in the hope that it will be useful,
-% but WITHOUT ANY WARRANTY; without even the implied warranty of
-% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-% GNU General Public License for more details.
-%
-% You should have received a copy of the GNU Affero General Public License
-% along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
--module(mgw_nat_usr).
--author("Harald Welte <laforge@gnumonks.org>").
-
--behavior(gen_server).
-
--export([start_link/1, stop/0, sccp_masq_reset/0]).
--export([init/1, handle_cast/2, handle_info/2, terminate/2]).
-
-
-start_link(Params) ->
-	gen_server:start_link({local, ?MODULE}, ?MODULE, Params, []).
-
-stop() ->
-	gen_server:cast(?MODULE, stop).
-
-sccp_masq_reset() ->
-	gen_server:cast(?MODULE, sccp_masq_reset).
-
-
-%% Callback functions of the OTP behavior
-
-init(Params) ->
-	sccp_masq:init(),
-	apply(sctp_handler, init, Params).
-
-handle_cast(stop, LoopData) ->
-	{stop, normal, LoopData};
-
-handle_cast(sccp_masq_reset, LoopData) ->
-	sccp_masq:reset(),
-	{noreply, LoopData}.
-
-terminate(_Reason, _LoopData) ->
-	ok.
-
-% callback for other events like incoming SCTP message
-handle_info({sctp, Sock, Ip, Port, Data}, LoopData) ->
-	NewL = sctp_handler:handle_sctp(LoopData, {sctp, Sock, Ip, Port, Data}),
-	{noreply, NewL}.
diff --git a/src/mtp3.hrl b/src/mtp3.hrl
deleted file mode 100644
index 53025a7..0000000
--- a/src/mtp3.hrl
+++ /dev/null
@@ -1,21 +0,0 @@
-
--define(MTP3_SERV_MGMT,		0).
--define(MTP3_SERV_MTN,		1).
--define(MTP3_SERV_SCCP,		3).
--define(MTP3_SERV_TUP,		4).
--define(MTP3_SERV_ISUP,		5).
-
--record(mtp3_routing_label, {
-	sig_link_sel,
-	origin_pc,
-	dest_pc
-	}).
-
--record(mtp3_msg, {
-	network_ind,
-	service_ind,
-	routing_label,
-	payload
-	}).
-
-
diff --git a/src/sccp.hrl b/src/sccp.hrl
deleted file mode 100644
index b445495..0000000
--- a/src/sccp.hrl
+++ /dev/null
@@ -1,103 +0,0 @@
-
-% Table 1 / Q.713 - SCCP Message Types
--define(SCCP_MSGT_CR,		1).	% Connection request
--define(SCCP_MSGT_CC,		2).	% Connection confirm
--define(SCCP_MSGT_CREF,		3).	% Connection refused
--define(SCCP_MSGT_RLSD,		4).	% Released
--define(SCCP_MSGT_RLC,		5).	% Release complete
--define(SCCP_MSGT_DT1,		6).	% Data form 1
--define(SCCP_MSGT_DT2,		7).	% Data form 2
--define(SCCP_MSGT_AK,		8).	% Data acknowledgement
--define(SCCP_MSGT_UDT,		9).	% Unitdata
--define(SCCP_MSGT_UDTS,		10).	% Unitdata service
--define(SCCP_MSGT_ED,		11).	% Expedited data
--define(SCCP_MSGT_EA,		12).	% Expedited data ack
--define(SCCP_MSGT_RSR,		13).	% Reset Request
--define(SCCP_MSGT_RSC,		14).	% Reset Confirmation
--define(SCCP_MSGT_ERR,		15).	% Protocol data unit error
--define(SCCP_MSGT_IT,		16).	% Inactivity test
--define(SCCP_MSGT_XUDT,		17).	% Extended unitdata
--define(SCCP_MSGT_XUDTS,	18).	% Extended unitdata service
--define(SCCP_MSGT_LUDT,		19).	% Long unitdata
--define(SCCP_MSGT_LUDTS,	20).	% Long unitdata service
-
-% Table 2 / Q.713 - SCCP parameter name codes
--define(SCCP_PNC_END_OF_OPTIONAL,		0).
--define(SCCP_PNC_DESTINATION_LOCAL_REFERENCE,	1).
--define(SCCP_PNC_SOURCE_LOCAL_REFERENCE,	2).
--define(SCCP_PNC_CALLED_PARTY_ADDRESS,		3).
--define(SCCP_PNC_CALLING_PARTY_ADDRESS,		4).
--define(SCCP_PNC_PROTOCOL_CLASS,		5).
--define(SCCP_PNC_SEGMENTING,			6).
--define(SCCP_PNC_RECEIVE_SEQ_NUMBER,		7).
--define(SCCP_PNC_SEQUENCING,			8).
--define(SCCP_PNC_CREDIT,			9).
--define(SCCP_PNC_RELEASE_CAUSE,			10).
--define(SCCP_PNC_RETURN_CAUSE,			11).
--define(SCCP_PNC_RESET_CAUSE,			12).
--define(SCCP_PNC_ERROR_CAUSE,			13).
--define(SCCP_PNC_REFUSAL_CAUSE,			14).
--define(SCCP_PNC_DATA,				15).
--define(SCCP_PNC_SEGMENTATION,			16).
--define(SCCP_PNC_HOP_COUNTER,			17).
--define(SCCP_PNC_IMPORTANCE,			18).
--define(SCCP_PNC_LONG_DATA,			19).
-
-% According to Q.713 Section 3.4.1
--define(SCCP_GTI_NO_GT,		2#0000).
--define(SCCP_GTI_NAT_ONLY,	2#0001).
--define(SCCP_GTI_TT_ONLY,	2#0010).
--define(SCCP_GTI_TT_NP_ENC,	2#0011).
--define(SCCP_GTI_TT_NP_ENC_NAT,	2#0100).
-
-% According to Q.731 Section 3.4.2.2
--define(SCCP_SSN_UNKNOWN,	2#000000000).
--define(SCCP_SSN_SCCP_MGMT,	2#000000001).
--define(SCCP_SSN_ITU_T,		2#000000010).
--define(SCCP_SSN_ISUP,		2#000000011).
--define(SCCP_SSN_OAM,		2#000000100).
--define(SCCP_SSN_MAP,		2#000000101).
--define(SCCP_SSN_HLR,		2#000000110).
--define(SCCP_SSN_VLR,		2#000000111).
--define(SCCP_SSN_MSC,		2#000001000).
--define(SCCP_SSN_EIR,		2#000001001).
--define(SCCP_SSN_AUC,		2#000001010).
--define(SCCP_SSN_ISDN_SS,	2#000001011).
--define(SCCP_SSN_RES_NAT,	2#000001100).
--define(SCCP_SSN_BISDN,		2#000001101).
--define(SCCP_SSN_TC_TEST,	2#000001110).
-
-
-
-% a single parsed SCCP message
--record(sccp_msg, {
-	msg_type,
-	parameters
-	}).
-
-
-% a primitive how it is used inside the SCCP stack and to the user
--record(primitive, {
-	  subsystem,
-	  gen_name,
-	  spec_name,
-	  parameters
-	}).
-
--record(global_title, {
-	  gti,
-	  nature_of_addr_ind,
-	  trans_type,
-	  encoding,
-	  numbering_plan,
-	  phone_number
-	}).
-
--record(sccp_addr, {
-	  res_nat_use,
-	  route_on_ssn,
-	  point_code,		% optional
-	  ssn,			% optional
-	  global_title		% optional
-	}).
-
diff --git a/src/sccp_masq.erl b/src/sccp_masq.erl
deleted file mode 100644
index 2739307..0000000
--- a/src/sccp_masq.erl
+++ /dev/null
@@ -1,132 +0,0 @@
-% ITU-T Q.71x SCCP UDT stateful masquerading
-
-% (C) 2011 by Harald Welte <laforge@gnumonks.org>
-%
-% All Rights Reserved
-%
-% This program is free software; you can redistribute it and/or modify
-% it under the terms of the GNU Affero General Public License as
-% published by the Free Software Foundation; either version 3 of the
-% License, or (at your option) any later version.
-%
-% This program is distributed in the hope that it will be useful,
-% but WITHOUT ANY WARRANTY; without even the implied warranty of
-% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-% GNU General Public License for more details.
-%
-% You should have received a copy of the GNU Affero General Public License
-% along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
--module(sccp_masq).
--author('Harald Welte <laforge@gnumonks.org>').
--include("sccp.hrl").
-
--export([sccp_masq_msg/3, init/0, reset/0]).
-
--compile([export_all]).
-
--record(sccp_masq_rec, {
-	  digits_in,	% list of GT digits
-	  digits_out,	% list of GT digits
-	  last_access	% timestamp of last usage
-	}).
-
-% alloc + insert a new masquerade state record in our tables
-masq_alloc(DigitsOrig) ->
-	{ok, Base} = application:get_env(sccp_masq_gt_base),
-	{ok, Max} = application:get_env(sccp_masq_gt_max),
-	masq_try_alloc(DigitsOrig, Base, Max, 0).
-masq_try_alloc(_DigitsOrig, _Base, Max, Offset) when Offset > Max ->
-	undef;
-masq_try_alloc(DigitsOrig, Base, Max, Offset) ->
-	Try = Base + Offset,
-	EtsRet = ets:insert_new(get(sccp_masq_orig),
-				#sccp_masq_rec{digits_in = DigitsOrig,
-					       digits_out = Try}),
-	case EtsRet of
-		false ->
-			masq_try_alloc(DigitsOrig, Base, Max, Offset+1);
-		_ ->
-			ets:insert(get(sccp_masq_rev),
-				   #sccp_masq_rec{digits_in = Try,
-						  digits_out = DigitsOrig}),
-			Try
-	end.
-
-% lookup a masqerade state record
-lookup_masq_addr(orig, GtDigits) ->
-	case ets:lookup(get(sccp_masq_orig), GtDigits) of
-		[#sccp_masq_rec{digits_out = DigitsOut}] ->
-			DigitsOut;
-		_ ->
-			% allocate a new masq GT
-			masq_alloc(GtDigits)
-	end;
-lookup_masq_addr(rev, GtDigits) ->
-	case ets:lookup(get(sccp_masq_rev), GtDigits) of
-		[#sccp_masq_rec{digits_out = DigitsOut}] ->
-			DigitsOut;
-		_ ->
-			% we do not allocate entries in the reverse direction
-			undef
-	end.
-
-
-% Masquerade the CALLING address in first STP(G-MSC) -> HLR/VLR/MSC dir
-mangle_rx_calling(from_stp, Addr = #sccp_addr{global_title = GT}) ->
-	GtOrig = GT#global_title.phone_number,
-	GtReplace = lookup_masq_addr(orig, GtOrig),
-	case GtReplace of
-		undef ->
-			io:format("SCCP MASQ: Unable to rewrite in original direction (out of GT addrs?)~n"),
-			Addr;
-		_ ->
-			io:format("SCCP MASQ (STP->MSC) rewrite ~p->~p~n", [GtOrig, GtReplace]),
-			GTout = GT#global_title{phone_number = GtReplace},
-			Addr#sccp_addr{global_title = GTout}
-	end;
-mangle_rx_calling(_From, Addr) ->
-	Addr.
-
-mangle_rx_called(from_msc, Addr = #sccp_addr{global_title = GT}) ->
-	GtOrig = GT#global_title.phone_number,
-	GtReplace = lookup_masq_addr(rev, GtOrig),
-	case GtReplace of
-		undef ->
-			io:format("SCCP MASQ: Unable to rewrite in original direction (unknown GT ~p)~n", [GT]),
-			Addr;
-		_ ->
-			io:format("SCCP MASQ (MSC->STP) rewrite ~p->~p~n", [GtOrig, GtReplace]),
-			GTout = GT#global_title{phone_number = GtReplace},
-			Addr#sccp_addr{global_title = GTout}
-	end;
-mangle_rx_called(_From, Addr) ->
-	Addr.
-
-
-sccp_masq_msg(From, ?SCCP_MSGT_UDT, Msg = #sccp_msg{parameters = Opts}) ->
-	CalledParty = proplists:get_value(called_party_addr, Opts),
-	CalledPartyNew = mangle_rx_called(From, CalledParty),
-	CallingParty = proplists:get_value(calling_party_addr, Opts),
-	CallingPartyNew = mangle_rx_calling(From, CallingParty),
-	Opts1 = lists:keyreplace(called_party_addr, 1, Opts,
-				 {called_party_addr, CalledPartyNew}),
-	Opts2 = lists:keyreplace(calling_party_addr, 1, Opts1,
-				 {calling_party_addr, CallingPartyNew}),
-	Msg#sccp_msg{parameters = Opts2};
-sccp_masq_msg(_From, _MsgType, Msg) ->
-	Msg.
-
-init() ->
-	Orig = ets:new(sccp_masq_orig, [ordered_set,
-					{keypos, #sccp_masq_rec.digits_in}]),
-	Rev  = ets:new(sccp_masq_rev, [ordered_set,
-					{keypos, #sccp_masq_rec.digits_in}]),
-	put(sccp_masq_orig, Orig),
-	put(sccp_masq_rev, Rev),
-	ok.
-
-reset() ->
-	io:format("SCCP MASQ: Deleting all MASQ state records~n"),
-	ets:delete_all_objects(get(sccp_masq_orig)),
-	ets:delete_all_objects(get(sccp_masq_rev)).
diff --git a/src/sccp_user.erl b/src/sccp_user.erl
deleted file mode 100644
index 0871b98..0000000
--- a/src/sccp_user.erl
+++ /dev/null
@@ -1,80 +0,0 @@
-
-% (C) 2010 by Harald Welte <laforge@gnumonks.org>
-%
-% All Rights Reserved
-%
-% This program is free software; you can redistribute it and/or modify
-% it under the terms of the GNU Affero General Public License as
-% published by the Free Software Foundation; either version 3 of the
-% License, or (at your option) any later version.
-%
-% This program is distributed in the hope that it will be useful,
-% but WITHOUT ANY WARRANTY; without even the implied warranty of
-% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-% GNU General Public License for more details.
-%
-% You should have received a copy of the GNU Affero General Public License
-% along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
--module(sccp_user).
--author('Harald Welte <laforge@gnumonks.org>').
--export([init/3]).
-
--include("sccp.hrl").
-
--define(IPA_STREAM_ID_SCCP, 253).
-
--record(loop_data, {
-		ipa_stream_id
-	}).
-
-init(TcpServerPort, IpaStreamId, Opts) ->
-	ipa_proto:init(),
-	% Create listening IPA socket
-	ipa_proto:start_listen(TcpServerPort, 1, Opts),
-	loop(#loop_data{ipa_stream_id = IpaStreamId}).
-
-% callback function to be called by IPA socket handler if it receives some data
-sccp_ipa_adapter_cb(S, IpaStreamID, DataBin, [ScrcPid]) ->
-	io:format("sccp_ipa_adapter_cb (Socket ~p, Stream ~p), passing data to SCRP~n", [S, IpaStreamID]),
-	% hand any incoming IPA message off into the SCCP stacks SCRC
-	gen_fsm:send_event(ScrcPid, sccp_scoc:make_prim('MTP', 'TRANSFER', indication, DataBin)).
-
-% callback function to be called by SCCP if it wants to transmit some data
-sccp_to_ipa_cb(#primitive{subsystem = 'MTP', gen_name = 'TRANSFER',
-			  spec_name = request, parameters = DataBin}, [IpaPid, S, IpaStreamID]) ->
-	%ipa_proto:send(S, IpaStreamID, DataBin).
-	io:format("sccp_to_ipa_cb: Sending to ~p ~p/~p: ~p~n", [IpaPid, S,IpaStreamID, DataBin]),
-	IpaPid ! {ipa_send, S, IpaStreamID, DataBin}.
-
-loop(LoopData) ->
-	receive
-		{ipa_tcp_accept, S} ->
-			io:format("sccp_ipa_adapter: ipa_tcp_accept from ~p~n", [inet:peername(S)]),
-			IpaStreamId = LoopData#loop_data.ipa_stream_id,
-			% hand over the socket into the IPA stack
-			{ok, IpaPid} = ipa_proto:register_socket(S),
-			% Start the SCRC FSM for this virtual MTP link
-			ScrcMtpCb = {callback_fn, fun sccp_to_ipa_cb/2, [IpaPid, S, IpaStreamId]},
-			{ok, ScrcPid} = sccp_scrc:start_link([{mtp_tx_action, ScrcMtpCb}]),
-			% Register an IPA stream for SCCP
-			ipa_proto:register_stream(S, IpaStreamId,
-						  {callback_fn, fun sccp_ipa_adapter_cb/4, [ScrcPid]}),
-			ipa_proto:unblock(S),
-			loop(LoopData);
-		% this code should later be moved into the actual MSC
-		{sccp, Prim} ->
-			io:format("sccp_user has received primitive ~p~n", [Prim]),
-			handle_sccp_prim(Prim),
-			loop(LoopData)
-	end.
-
-
-handle_sccp_prim(#primitive{subsystem = 'N', gen_name = 'CONNECT',
-			    spec_name = indication, parameters = Params}) ->
-	%RespPrim = Prim#primitive{spec_name = response},
-	RespPrim = sccp_scoc:make_prim('N', 'CONNECT', response, []),
-	ScocPid = proplists:get_value(scoc_pid, Params),
-	gen_fsm:send_event(ScocPid, RespPrim);
-handle_sccp_prim(#primitive{}) ->
-	ok.