-
Notifications
You must be signed in to change notification settings - Fork 42
/
ocs_diameter_base_application_cb.erl
161 lines (146 loc) · 5.76 KB
/
ocs_diameter_base_application_cb.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
%%% ocs_diameter_base_application_cb.erl
%%% vim: ts=3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @copyright 2016 - 2017 SigScale Global Inc.
%%% @end
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @doc This {@link //stdlib/gen_server. gen_server} behaviour callback
%%% module receives {@link //diameter. diameter} messages on a port assigned
%%% for authentication in the {@link //ocs. ocs} application.
%%%
%%% @reference <a href="https://tools.ietf.org/pdf/rfc6733.pdf">
%%% RFC6733 - DIAMETER base protocol</a>
%%%
-module(ocs_diameter_base_application_cb).
-copyright('Copyright (c) 2016 - 2017 SigScale Global Inc.').
-export([peer_up/3, peer_down/3, pick_peer/4, prepare_request/3,
prepare_retransmit/3, handle_answer/4, handle_error/4,
handle_request/3]).
-include_lib("diameter/include/diameter.hrl").
-include_lib("diameter/include/diameter_gen_base_rfc6733.hrl").
-record(state, {}).
-type state() :: #state{}.
-type capabilities() :: #diameter_caps{}.
-type packet() :: #diameter_packet{}.
-type message() :: tuple() | list().
-type peer() :: {Peer_Ref :: term(), Capabilities :: capabilities()}.
%%----------------------------------------------------------------------
%% The DIAMETER application callbacks
%%----------------------------------------------------------------------
-spec peer_up(SvcName, Peer, State) -> NewState
when
SvcName :: diameter:service_name(),
Peer :: peer(),
State :: state(),
NewState :: state().
%% @doc Invoked when the peer connection is available
peer_up(_SvcName, _Peer, State) ->
State.
-spec peer_down(SvcName, Peer, State) -> NewState
when
SvcName :: diameter:service_name(),
Peer :: peer(),
State :: state(),
NewState :: state().
%% @doc Invoked when the peer connection is not available
peer_down(_SvcName, _Peer, State) ->
State.
-spec pick_peer(LocalCandidates, RemoteCandidates, SvcName, State) -> Result
when
LocalCandidates :: [peer()],
RemoteCandidates :: [peer()],
SvcName :: diameter:service_name(),
State :: state(),
NewState :: state(),
Selection :: {ok, Peer} | {Peer, NewState},
Peer :: peer() | false,
Result :: Selection | false.
%% @doc Invoked as a consequence of a call to diameter:call/4 to select
%% a destination peer for an outgoing request.
pick_peer([Peer | _], _, _SvcName, _State) ->
{ok, Peer}.
-spec prepare_request(Packet, SvcName, Peer) -> Action
when
Packet :: packet(),
SvcName :: diameter:service_name(),
Peer :: peer(),
Action :: Send | Discard | {eval_packet, Action, PostF},
Send :: {send, packet() | message()},
Discard :: {discard, Reason} | discard,
Reason :: term(),
PostF :: diameter:evaluable().
%% @doc Invoked to return a request for encoding and transport
prepare_request(#diameter_packet{msg = ['RAR' = T | Avps]}, _, {_, Caps}) ->
#diameter_caps{origin_host = {OH, DH}, origin_realm = {OR, DR}} = Caps,
{send, [T, {'Origin-Host', OH}, {'Origin-Realm', OR},
{'Destination-Host', DH}, {'Destination-Realm', DR} | Avps]};
prepare_request(#diameter_packet{msg = Record}, _, {_, Caps}) ->
#diameter_caps{origin_host = {OH, DH}, origin_realm = {OR, DR}} = Caps,
ASR = Record#diameter_base_ASR{'Origin-Host' = OH, 'Origin-Realm' = OR,
'Destination-Host' = DH, 'Destination-Realm' = DR},
{send, ASR}.
-spec prepare_retransmit(Packet, SvcName, Peer) -> Action
when
Packet :: packet(),
SvcName :: diameter:service_name(),
Peer :: peer(),
Action :: Send | Discard | {eval_packet, Action, PostF},
Send :: {send, packet() | message()},
Discard :: {discard, Reason} | discard,
Reason :: term(),
PostF :: diameter:evaluable().
%% @doc Invoked to return a request for encoding and retransmission.
%% In case of peer connection is lost alternate peer is selected.
prepare_retransmit(_Packet, _SvcName, _Peer) ->
discard.
-spec handle_answer(Packet, Request, SvcName, Peer) -> Result
when
Packet :: packet(),
Request :: message(),
SvcName :: diameter:service_name(),
Peer :: peer(),
Result :: term().
%% @doc Invoked when an answer message is received from a peer.
handle_answer(Packet, _Request, _SvcName, _Peer) ->
{ok, Packet}.
-spec handle_error(Reason, Request, SvcName, Peer) -> Result
when
Reason :: timeout | failover | term(),
Request :: message(),
SvcName :: diameter:service_name(),
Peer :: peer(),
Result :: term().
%% @doc Invoked when an error occurs before an answer message is received
%% in response to an outgoing request.
handle_error(_Reason, _Request, _SvcName, _Peer) ->
not_implemented.
-spec handle_request(Packet, SvcName, Peer) -> Action
when
Packet :: packet(),
SvcName :: term(),
Peer :: peer(),
Action :: Reply | {relay, [Opt]} | discard
| {eval|eval_packet, Action, PostF},
Reply :: {reply, packet() | message()}
| {answer_message, 3000..3999|5000..5999}
| {protocol_error, 3000..3999},
Opt :: diameter:call_opt(),
PostF :: diameter:evaluable().
%% @doc Invoked when a request messge is received from the peer.
handle_request(#diameter_packet{msg = _Req, errors = []},
_SvcName, {_Peer, _Caps}) ->
discard.
%%----------------------------------------------------------------------
%% internal functions
%%----------------------------------------------------------------------