1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2010-2020. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21-module(relay_cb).
22
23-include_lib("diameter/include/diameter.hrl").
24
25%% diameter callbacks
26-export([peer_up/3,
27         peer_down/3,
28         pick_peer/4,
29         prepare_request/3,
30         prepare_retransmit/3,
31         handle_answer/4,
32         handle_error/4,
33         handle_request/3]).
34
35%% peer_up/3
36
37peer_up(_SvcName, _Peer, State) ->
38    State.
39
40%% peer_down/3
41
42peer_down(_SvcName, _Peer, State) ->
43    State.
44
45%% handle_request/3
46
47%% Assume the destination is directly connected; filter
48%% correspondingly; don't relay to the sender.
49handle_request(_Pkt, _SvcName, {_, Caps}) ->
50    #diameter_caps{origin_host = {_, OH}}
51        = Caps,
52    {relay, [{timeout, 2000},
53             {filter, {all, [host, realm, {neg, {host, OH}}]}}]}.
54
55%% pick_peer/4
56
57pick_peer([Peer | _], _, _SvcName, _State) ->
58    {ok, Peer}.
59
60%% prepare_request/3
61
62prepare_request(Pkt, _SvcName, _Peer) ->
63    {send, Pkt}.
64
65%% prepare_request/3
66
67prepare_retransmit(Pkt, _SvcName, _Peer) ->
68    {send, Pkt}.
69
70%% handle_answer/4
71
72%% Relay an answer by returning the first argument.
73handle_answer(Pkt, _Request, _SvcName, _Peer) ->
74    Pkt.
75
76%% handle_error/4
77
78handle_error(Reason, _Request, _SvcName, _Peer) ->
79    {error, Reason}.
80