Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 1 | % MGW Nat testing code |
| 2 | |
| 3 | % (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | % (C) 2011 OnWaves |
| 5 | % |
| 6 | % All Rights Reserved |
| 7 | % |
| 8 | % This program is free software; you can redistribute it and/or modify |
| 9 | % it under the terms of the GNU Affero General Public License as |
| 10 | % published by the Free Software Foundation; either version 3 of the |
| 11 | % License, or (at your option) any later version. |
| 12 | % |
| 13 | % This program is distributed in the hope that it will be useful, |
| 14 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | % GNU General Public License for more details. |
| 17 | % |
| 18 | % You should have received a copy of the GNU Affero General Public License |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 19 | % along with this program. If not, see <https://www.gnu.org/licenses/>. |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 20 | % |
| 21 | % Additional Permission under GNU AGPL version 3 section 7: |
| 22 | % |
| 23 | % If you modify this Program, or any covered work, by linking or |
| 24 | % combining it with runtime libraries of Erlang/OTP as released by |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 25 | % Ericsson on https://www.erlang.org (or a modified version of these |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 26 | % libraries), containing parts covered by the terms of the Erlang Public |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 27 | % License (https://www.erlang.org/EPLICENSE), the licensors of this |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 28 | % Program grant you additional permission to convey the resulting work |
| 29 | % without the need to license the runtime libraries of Erlang/OTP under |
| 30 | % the GNU Affero General Public License. Corresponding Source for a |
| 31 | % non-source form of such a combination shall include the source code |
| 32 | % for the parts of the runtime libraries of Erlang/OTP used as well as |
| 33 | % that of the covered work. |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 34 | |
| 35 | -module(osmo_ss7_pcap). |
| 36 | -author("Harald Welte <laforge@gnumonks.org>"). |
| 37 | -export([pcap_apply/3]). |
| 38 | |
| 39 | -define(NODEBUG, 1). |
| 40 | |
| 41 | -include_lib("eunit/include/eunit.hrl"). |
Harald Welte | c9f3178 | 2019-08-11 15:33:18 +0200 | [diff] [blame] | 42 | -include_lib("pkt/include/pkt.hrl"). |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 43 | |
Harald Welte | eba9e25 | 2011-03-11 18:21:53 +0100 | [diff] [blame] | 44 | -record(loop_data, { |
| 45 | args, |
| 46 | pkt_nr |
| 47 | }). |
| 48 | |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 49 | pcap_apply(File, Filter, Args) -> |
| 50 | epcap:start([{file, File}, {filter, Filter}]), |
Harald Welte | eba9e25 | 2011-03-11 18:21:53 +0100 | [diff] [blame] | 51 | loop(#loop_data{args = Args, pkt_nr = 1}). |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 52 | |
Harald Welte | eba9e25 | 2011-03-11 18:21:53 +0100 | [diff] [blame] | 53 | loop(L = #loop_data{args=Args, pkt_nr = PktNr}) -> |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 54 | receive |
| 55 | [{pkthdr, {_,_,_,{datalink,Datalink}}}, {packet, Packet}] -> |
| 56 | Decaps = epcap_net:decapsulate_dlt(Datalink, Packet), |
Harald Welte | eba9e25 | 2011-03-11 18:21:53 +0100 | [diff] [blame] | 57 | handle_pkt_cb(PktNr, Decaps, Args), |
| 58 | loop(L#loop_data{pkt_nr = PktNr+1}); |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 59 | {epcap, eof} -> |
| 60 | ?debugFmt("EOF from PCAP~n", []), |
Harald Welte | eba9e25 | 2011-03-11 18:21:53 +0100 | [diff] [blame] | 61 | epcap:stop(), |
| 62 | {ok, PktNr-1}; |
Harald Welte | 7629ced | 2011-04-03 01:14:41 +0200 | [diff] [blame] | 63 | Default -> |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 64 | ?debugFmt("Unknown ~p from PCAP~n", [Default]) |
| 65 | end. |
| 66 | |
Harald Welte | eba9e25 | 2011-03-11 18:21:53 +0100 | [diff] [blame] | 67 | handle_pkt_cb(PktNr, [Ether, IP, Hdr, Payload], Args) -> |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 68 | ?debugFmt("~p:~n ~p/~p~n", [IP, Hdr, Payload]), |
| 69 | case Hdr of |
| 70 | #sctp{chunks = Chunks} -> |
Harald Welte | eba9e25 | 2011-03-11 18:21:53 +0100 | [diff] [blame] | 71 | Path = [{epcap_pkt_nr, PktNr}, Ether, IP, Hdr], |
| 72 | handle_sctp_chunks(Chunks, Path, Args); |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 73 | _ -> |
| 74 | ok |
| 75 | end. |
| 76 | |
| 77 | handle_sctp_chunks([], _Path, _Args) -> |
| 78 | ok; |
| 79 | handle_sctp_chunks([Head|Tail], Path, Args) -> |
| 80 | RewriteFn = proplists:get_value(rewrite_fn, Args), |
| 81 | case Head of |
Harald Welte | 7629ced | 2011-04-03 01:14:41 +0200 | [diff] [blame] | 82 | #sctp_chunk{type = 0, payload=#sctp_chunk_data{ppi=Ppi, data=Data}} -> |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 83 | %mgw_nat:mangle_rx_data(l, from_stp, Data, fun handle_rewrite_cb/5); |
| 84 | put(rewrite_cb, RewriteFn), |
Harald Welte | 7629ced | 2011-04-03 01:14:41 +0200 | [diff] [blame] | 85 | shim_rw_actor(sctp, from_msc, Path, Ppi, Data); |
Harald Welte | 4821310 | 2011-03-11 15:50:13 +0100 | [diff] [blame] | 86 | _ -> |
| 87 | ok |
| 88 | end, |
| 89 | handle_sctp_chunks(Tail, Path, Args). |
| 90 | |
| 91 | shim_rw_actor(Proto, From, Path, MsgType, Msg) -> |
| 92 | ?debugFmt(" IN:~p:~p:~p~n", [Proto, From, Msg]), |
| 93 | Fn = get(rewrite_cb), |
| 94 | MsgOut = Fn(Proto, From, Path, MsgType, Msg), |
| 95 | case MsgOut of |
| 96 | Msg -> |
| 97 | MsgOut; |
| 98 | _ -> |
| 99 | %io:format("OUT:~p:~p:~p~n", [Proto, From, MsgOut]), |
| 100 | MsgOut |
| 101 | end. |