aboutsummaryrefslogtreecommitdiffstats
path: root/lib/diameter/examples/code/sctp.erl
diff options
context:
space:
mode:
authorAnders Svensson <[email protected]>2011-12-20 14:51:56 +0100
committerAnders Svensson <[email protected]>2011-12-20 14:51:56 +0100
commit58336ee5c613de56f6d69562cd59b651eef734f0 (patch)
tree5a593244269992c86b9157229a2cb6d0242bbb79 /lib/diameter/examples/code/sctp.erl
parente986108d71a71d835c241d22cdb08f97584958c5 (diff)
parent84873533d8caf176a710135d15e3578168350eab (diff)
downloadotp-58336ee5c613de56f6d69562cd59b651eef734f0.tar.gz
otp-58336ee5c613de56f6d69562cd59b651eef734f0.tar.bz2
otp-58336ee5c613de56f6d69562cd59b651eef734f0.zip
Merge branch 'anders/diameter/testsuites/OTP-9829' into maint
* anders/diameter/testsuites/OTP-9829: (21 commits) Install example dictionaries Move example code to examples/code Move example dictionaries to examples/dict Set name/prefix at compilation, not in dictionaries Add RFC 4004 (MIP) dictionary Add RFC 4740 (SIP) dictionary Add RFC 4072 (EAP) dictionary Add RFC 4006 (CC) dictionary Add RFC 4005 (NAS) dictionary Add standards testcase to compiler suite Remove {init,end}_per_group workaround Use new syntax for specifying ct group properties Increase timetrap in compiler suite Minor capx suite tweaks Minor makefile tweak Remove trailing whitespace Update skip condition in gen_sctp suite Reintroduce gen_sctp suite Remove delay from connect in transport suite Add codec testcase for decode of unknown AVPs ...
Diffstat (limited to 'lib/diameter/examples/code/sctp.erl')
-rw-r--r--lib/diameter/examples/code/sctp.erl131
1 files changed, 131 insertions, 0 deletions
diff --git a/lib/diameter/examples/code/sctp.erl b/lib/diameter/examples/code/sctp.erl
new file mode 100644
index 0000000000..08de023571
--- /dev/null
+++ b/lib/diameter/examples/code/sctp.erl
@@ -0,0 +1,131 @@
+%%
+%% %CopyrightBegin%
+%%
+%% Copyright Ericsson AB 2010-2011. All Rights Reserved.
+%%
+%% The contents of this file are subject to the Erlang Public License,
+%% Version 1.1, (the "License"); you may not use this file except in
+%% compliance with the License. You should have received a copy of the
+%% Erlang Public License along with this software. If not, it can be
+%% retrieved online at http://www.erlang.org/.
+%%
+%% Software distributed under the License is distributed on an "AS IS"
+%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+%% the License for the specific language governing rights and limitations
+%% under the License.
+%%
+%% %CopyrightEnd%
+%%
+
+-module(sctp).
+
+%%
+%% A small example demonstrating the establishment of an SCTP
+%% association with gen_sctp.
+%%
+
+-export([assoc/0,
+ dbg/0]).
+
+-include_lib("kernel/include/inet_sctp.hrl").
+
+-define(ADDR, {127,0,0,1}).
+-define(SERVER_PORT, 3868).
+-define(CONNECT_TIMEOUT, 2000).
+-define(REQUEST, <<0:64>>).
+-define(REPLY, <<1:64>>).
+
+-record(server, {client,
+ socket,
+ assoc}).
+
+-record(client, {socket,
+ assoc}).
+
+%% assoc/0
+%%
+%% Return on a successfully established association, raise an
+%% exception otherwise.
+
+assoc() ->
+ {_, MRef} = spawn_monitor(fun server/0),
+ receive {'DOWN', MRef, process, _, Info} -> Info end.
+
+%% dbg/0
+
+dbg() ->
+ dbg:tracer(),
+ dbg:p(all,c),
+ dbg:tpl(?MODULE, [{'_',[],[{exception_trace}]}]),
+ dbg:tp(gen_sctp, [{'_',[],[{exception_trace}]}]),
+ ok.
+
+%% server/0
+
+server() ->
+ {ok, Sock} = gen_sctp:open([binary,
+ {reuseaddr, true},
+ {active, true},
+ {ip, ?ADDR},
+ {port, ?SERVER_PORT}]),
+ ok = gen_sctp:listen(Sock, true),
+ {_Pid, MRef} = spawn_monitor(fun client/0),
+ s(#server{client = MRef, socket = Sock}),
+ gen_sctp:close(Sock).
+
+%% s/1
+
+s(#server{} = S) ->
+ s(s(receive T -> T end, S));
+s(T) ->
+ T.
+
+%% s/2
+
+s({sctp, Sock, _RA, _RP, {[], #sctp_assoc_change{state = comm_up,
+ assoc_id = Id}}},
+ #server{socket = Sock, assoc = undefined}
+ = S) ->
+ S#server{assoc = Id};
+
+s({sctp, Sock, _RA, _RP, {[#sctp_sndrcvinfo{assoc_id = AId,
+ stream = SId}],
+ ?REQUEST}},
+ #server{socket = Sock, assoc = AId}
+ = S) ->
+ ok = gen_sctp:send(Sock, AId, SId, ?REPLY),
+ S;
+
+s({'DOWN', MRef, process, _, normal} = T, #server{client = MRef}) ->
+ T.
+
+%% client/0
+
+client() ->
+ {ok, Sock} = gen_sctp:open([binary,
+ {reuseaddr, true},
+ {active, true},
+ {ip, ?ADDR},
+ {port, 0}]),
+ ok = gen_sctp:connect_init(Sock, ?ADDR, ?SERVER_PORT, []),
+ c(#client{socket = Sock}),
+ gen_sctp:close(Sock).
+
+
+%% c/1
+
+c(#client{} = S) ->
+ c(c(receive T -> T end, S));
+c(T) ->
+ T.
+
+c({sctp, Sock, _RA, _RP, {[], #sctp_assoc_change{state = comm_up,
+ assoc_id = Id}}},
+ #client{socket = Sock, assoc = undefined}
+ = S) ->
+ ok = gen_sctp:send(Sock, Id, 0, ?REQUEST),
+ S#client{assoc = Id};
+
+c({sctp, Sock, _RA, _RP, {[#sctp_sndrcvinfo{assoc_id = AId}], ?REPLY}},
+ #client{socket = Sock, assoc = AId}) ->
+ ok.