%ents; ]>
20112012 Ericsson AB. 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. diameter_app(3) Anders Svensson %REV% diameter_app.xml
diameter_app Callback module of a Diameter application.

A diameter service as started by &mod_start_service; configures one of more Diameter applications, each of whose configuration specifies a callback that handles messages specific to the application. The messages and AVPs of the application are defined in a dictionary file whose format is documented in &man_dict; while the callback module is documented here. The callback module implements the Diameter application-specific functionality of a service.

A callback module must export all of the functions documented below. The functions themselves are of three distinct flavours:

&peer_up; and &peer_down; signal the attainment or loss of connectivity with a Diameter peer.

&pick_peer;, &prepare_request;, &prepare_retransmit;, &handle_answer; and &handle_error; are (or may be) called as a consequence of a call to &mod_call; to send an outgoing Diameter request message.

&handle_request; is called in response to an incoming Diameter request message.

The arities given for the the callback functions here assume no extra arguments. All functions will also be passed any extra arguments configured with the callback module itself when calling &mod_start_service; and, for the call-specific callbacks, any extra arguments passed to &mod_call;.

DATA TYPES capabilities() = #diameter_caps{}

A record containing the identities of the local Diameter node and the remote Diameter peer having an established transport connection, as well as the capabilities as determined by capabilities exchange. Each field of the record is a 2-tuple consisting of values for the (local) host and (remote) peer. Optional or possibly multiple values are encoded as lists of values, mandatory values as the bare value.

message() = record() | list()

The representation of a Diameter message as passed to &mod_call;. The record representation is as outlined in diameter_dict(4): a message as defined in a dictionary file is encoded as a record with one field for each component AVP. Equivalently, a message can also be encoded as a list whose head is the atom-valued message name (the record name minus any prefix specified in the relevant dictionary file) and whose tail is a list of {FieldName, FieldValue} pairs.

A third representation allows a message to be specified as a list whose head is a #diameter_header{} record and whose tail is a list of #diameter_avp{} records. This representation is used by diameter itself when relaying requests as directed by the return value of a &handle_request; callback. It differs from the other other two in that it bypasses the checks for messages that do not agree with their definitions in the dictionary in question (since relays agents must handle arbitrary request): messages are sent exactly as specified.

packet() = #diameter_packet{}

A container for incoming and outgoing Diameter messages that's passed through encode/decode and transport. Fields should not be set in return values except as documented.

peer_ref() = term()

A term identifying a transport connection with a Diameter peer.

peer() = {&peer_ref;, &capabilities;}

A tuple representing a Diameter peer connection.

state() = term()

The state maintained by the application callback functions &peer_up;, &peer_down; and (optionally) &pick_peer;. The initial state is configured in the call to &mod_start_service; that configures the application on a service. Callback functions returning a state are evaluated in a common service-specific process while those not returning state are evaluated in a request-specific process.

Mod:peer_up(SvcName, Peer, State) -> NewState Invoked when a transport connection has been established SvcName = &mod_service_name; Peer = &peer; State = NewState = &state;

Invoked to signal the availability of a peer connection. In particular, capabilities exchange with the peer has indicated support for the application in question, the RFC 3539 watchdog state machine for the connection has reached state OKAY and Diameter messages can be both sent and received.

A watchdog state machine can reach state OKAY from state SUSPECT without a new capabilities exchange taking place. A new transport connection (and capabilities exchange) results in a new peer_ref().

There is no requirement that a callback return before incoming requests are received: &handle_request; callbacks must be handled independently of &peer_up; and &peer_down;.

Mod:peer_down(SvcName, Peer, State) -> NewState Invoked when a transport connection has been lost. SvcName = &mod_service_name; Peer = &peer; State = NewState = &state;

Invoked to signal that a peer connection is no longer available following a previous call to &peer_up;. In particular, that the RFC 3539 watchdog state machine for the connection has left state OKAY and the peer will no longer be a candidate in &pick_peer; callbacks.

Mod:pick_peer(Candidates, _Reserved, SvcName, State) -> Selection | false Select a target peer for an outgoing request. Candidates = [&peer;] SvcName = &mod_service_name; State = NewState = &state; Selection = {ok, Peer} | {Peer, NewState} Peer = &peer; | false

Invoked as a consequence of a call to &mod_call; to select a destination peer for an outgoing request. The return value indicates the selected peer.

The candidate list contains only those peers that have advertised support for the Diameter application in question during capabilities exchange, that have not be excluded by a filter option in the call to &mod_call; and whose watchdog state machine is in the OKAY state. The order of the elements is unspecified except that any peers whose Origin-Host and Origin-Realm matches that of the outgoing request (in the sense of a {filter, {all, [host, realm]}} option to &mod_call;) will be placed at the head of the list.

A callback that returns a peer() will be followed by a &prepare_request; callback and, if the latter indicates that the request should be sent, by either &handle_answer; or &handle_error; depending on whether or not an answer message is received from the peer. If the transport becomes unavailable after &prepare_request; then a new &pick_peer; callback may take place to failover to an alternate peer, after which &prepare_retransmit; takes the place of &prepare_request; in resending the request. There is no guarantee that a &pick_peer; callback to select an alternate peer will be followed by any additional callbacks since a retransmission to an alternate peer is abandoned if an answer is received from a previously selected peer.

Returning false or {false, NewState} causes {error, no_connection} to be returned from &mod_call;.

The return values false and {false, State} (that is, NewState = State) are equivalent, as are {ok, Peer} and {Peer, State}.

The return value {Peer, NewState} is only allowed if the Diameter application in question was configured with the &mod_application_opt; {call_mutates_state, true}. Otherwise, the State argument is always the intial value as configured on the application, not any subsequent value returned by a &peer_up; or &peer_down; callback.

Mod:prepare_request(Packet, SvcName, Peer) -> Action Return a request for encoding and transport. Packet = &packet; SvcName = &mod_service_name; Peer = &peer; Action = Send | Discard | {eval_packet, Action, PostF} Send = {send, &packet; | &message;} Discard = {discard, Reason} | discard PostF = &mod_evaluable;}

Invoked to return a request for encoding and transport. Allows the sender to use the selected peer's capabilities to modify the outgoing request. Many implementations may simply want to return {send, Packet}

A returned &packet; should set the request to be encoded in its msg field and can set the transport_data field in order to pass information to the transport process. Extra arguments passed to &mod_call; can be used to communicate transport (or any other) data to the callback.

A returned &packet; can set the header field to a #diameter_header{} to specify values that should be preserved in the outgoing request, values otherwise being those in the header record contained in Packet. A returned length, cmd_code or application_id is ignored.

A returned PostF will be evaluated on any encoded #diameter_packet{} prior to transmission, the bin field containing the encoded binary. The return value is ignored.

Returning {discard, Reason} causes the request to be aborted and the &mod_call; for which the callback has taken place to return {error, Reason}. Returning discard is equivalent to returning {discard, discarded}.

Mod:prepare_retransmit(Packet, SvcName, Peer) -> Action Return a request for encoding and retransmission. Packet = &packet; SvcName = &mod_service_name; Peer = &peer; Action = Send | Discard | {eval_packet, Action, PostF} Send = {send, &packet; | &message;} Discard = {discard, Reason} | discard PostF = &mod_evaluable;}

Invoked to return a request for encoding and retransmission. Has the same role as &prepare_request; in the case that a peer connection is lost an an alternate peer selected but the argument &packet; is as returned by the initial &prepare_request;.

Returning {discard, Reason} causes the request to be aborted and a &handle_error; callback to take place with Reason as initial argument. Returning discard is equivalent to returning {discard, discarded}.

Mod:handle_answer(Packet, Request, SvcName, Peer) -> Result Receive an answer message from a peer. Packet = &packet; Request = &message; SvcName = &mod_service_name; Peer = &peer; Result = term()

Invoked when an answer message is received from a peer. The return value is returned from &mod_call; unless the detach option was specified.

The decoded answer record and undecoded binary are in the msg and bin fields of the argument &packet; respectively. Request is the outgoing request message as was returned from &prepare_request; or &prepare_retransmit;.

For any given call to &mod_call; there is at most one &handle_answer; callback: any duplicate answer (due to retransmission or otherwise) is discarded. Similarly, only one of &handle_answer; or &handle_error; is called.

By default, an incoming answer message that cannot be successfully decoded causes the request process to fail, causing &mod_call; to return {error, failure} unless the detach option was specified. In particular, there is no &handle_error; callback in this case. The &mod_application_opt; answer_errors can be set to change this behaviour.

Mod:handle_error(Reason, Request, SvcName, Peer) -> Result Return an error from a outgoing request. Reason = timeout | failover | term() Request = &message; SvcName = &mod_service_name; Peer = &peer; Result = term()

Invoked when an error occurs before an answer message is received in response to an outgoing request. The return value is returned from &mod_call; unless the detach option was specified.

Reason timeout indicates that an answer message has not been received within the time specified with the corresponding &mod_call_opt;. Reason failover indicates that the transport connection to the peer to which the request has been sent has become unavailable and that not alternate peer was not selected.

Mod:handle_request(Packet, SvcName, Peer) -> Action Receive an incoming request. Packet = &packet; SvcName = term() Peer = &peer; Action = Reply | {relay, [Opt]} | discard | {eval|eval_packet, Action, PostF} Reply = {reply, &packet; | &message;} | {protocol_error, 3000..3999} Opt = &mod_call_opt; PostF = &mod_evaluable;

Invoked when a request message is received from a peer. The application in which the callback takes place (that is, the callback module as configured with &mod_start_service;) is determined by the Application Identifier in the header of the incoming request message, the selected module being the one whose corresponding dictionary declares itself as defining either the application in question or the Relay application.

The argument &packet; has the following signature.

#diameter_packet{header = #diameter_header{},
                 avps   = [#diameter_avp{}],
                 msg    = record() | undefined,
                 errors = [&dict_Unsigned32; | {&dict_Unsigned32;, #diameter_avp{}}],
                 bin    = binary(),
                 transport_data = term()}

The msg field will be undefined in case the request has been received in the relay application. Otherwise it contains the record representing the request as outlined in diameter_dict(4).

The errors field specifies any Result-Code's identifying errors that were encountered in decoding the request. In this case diameter will set both Result-Code and Failed-AVP AVP's in a returned answer &message; before sending it to the peer: the returned &message; need only set any other required AVP's. Note that the errors detected by diameter are all of the 5xxx series (Permanent Failures). The errors list is empty if the request has been received in the relay application.

The transport_data field contains an arbitrary term passed into diameter from the transport module in question, or the atom undefined if the transport specified no data. The term is preserved if a &message; is returned but must be set explicitly in a returned &packet;.

The semantics of each of the possible return values are as follows.

{reply, &packet; | &message;}

Send the specified answer message to the peer. In the case of a &packet;, the message to be sent must be set in the msg field and the header field can be set to a #diameter_header{} to specify values that should be preserved in the outgoing answer, appropriate values otherwise being set by diameter.

{protocol_error, 3000..3999}

Send an answer message to the peer containing the specified protocol error. Equivalent to

{reply, ['answer-message' | Avps]

where Avps sets the Origin-Host, Origin-Realm, the specified Result-Code and (if the request sent one) Session-Id AVP's.

Note that RFC 3588 mandates that only answers with a 3xxx series Result-Code (protocol errors) may set the E bit. Returning a non-3xxx value in a protocol_error tuple will cause the request process in question to fail.

{relay, Opts}

Relay a request to another peer in the role of a Diameter relay agent. If a routing loop is detected then the request is answered with 3005 (DIAMETER_LOOP_DETECTED). Otherwise a Route-Record AVP (containing the sending peer's Origin-Host) is added to the request and &pick_peer; and subsequent callbacks take place just as if &mod_call; had been called explicitly. The End-to-End Identifier of the incoming request is preserved in the header of the relayed request.

The returned Opts should not specify detach. A subsequent &handle_answer; callback for the relayed request must return its first argument, the #diameter_packet{} record containing the answer message. Note that the extra option can be specified to supply arguments that can distinguish the relay case from others if so desired. Any other return value (for example, from a &handle_error; callback) causes the request to be answered with 3002 (DIAMETER_UNABLE_TO_DELIVER).

discard

Discard the request. No answer message is sent to the peer.

{eval, Action, PostF}

Handle the request as if Action has been returned and then evaluate PostF in the request process. The return value is ignored.

{eval_packet, Action, PostF}

Like eval but evaluate PostF on any encoded #diameter_packet{} prior to transmission, the bin field containing the encoded binary. The return value is ignored.

Note that protocol errors detected by diameter will result in an answer message without handle_request/3 being invoked.