| 1 |  | %%% eturnal STUN/TURN server module. | 
| 2 |  | %%% | 
| 3 |  | %%% Copyright (c) 2020 Holger Weiss <holger@zedat.fu-berlin.de>. | 
| 4 |  | %%% Copyright (c) 2020 ProcessOne, SARL. | 
| 5 |  | %%% All rights reserved. | 
| 6 |  | %%% | 
| 7 |  | %%% Licensed under the Apache License, Version 2.0 (the "License"); | 
| 8 |  | %%% you may not use this file except in compliance with the License. | 
| 9 |  | %%% You may obtain a copy of the License at | 
| 10 |  | %%% | 
| 11 |  | %%%     http://www.apache.org/licenses/LICENSE-2.0 | 
| 12 |  | %%% | 
| 13 |  | %%% Unless required by applicable law or agreed to in writing, software | 
| 14 |  | %%% distributed under the License is distributed on an "AS IS" BASIS, | 
| 15 |  | %%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 16 |  | %%% See the License for the specific language governing permissions and | 
| 17 |  | %%% limitations under the License. | 
| 18 |  |  | 
| 19 |  | %%% This module logs STUN queries (or rather, responses). | 
| 20 |  |  | 
| 21 |  | -module(mod_log_stun). | 
| 22 |  | -behaviour(eturnal_module). | 
| 23 |  | -export([start/0, | 
| 24 |  |          stop/0, | 
| 25 |  |          handle_event/2, | 
| 26 |  |          options/0]). | 
| 27 |  | -import(yval, [enum/1]). | 
| 28 |  |  | 
| 29 |  | -include_lib("kernel/include/logger.hrl"). | 
| 30 |  | -ifndef(LOG). % Erlang/OTP 21.0. | 
| 31 |  | -define(LOG(Level, Format, Args), logger:log(Level, Format, Args)). | 
| 32 |  | -endif. | 
| 33 |  |  | 
| 34 |  | %% API. | 
| 35 |  |  | 
| 36 |  | -spec start() -> {ok, eturnal_module:events()}. | 
| 37 |  | start() -> | 
| 38 | 3 |     ?LOG_DEBUG("Starting ~s", [?MODULE]), | 
| 39 | 3 |     {ok, [stun_query]}. | 
| 40 |  |  | 
| 41 |  | -spec handle_event(eturnal_module:event(), eturnal_module:info()) -> ok. | 
| 42 |  | handle_event(stun_query, #{version := Version}) -> | 
| 43 | 5 |     Level = eturnal_module:get_opt(?MODULE, level), | 
| 44 | 5 |     ?LOG(Level, "Responding to ~s request", [format_protocol(Version)]), | 
| 45 | 5 |     ok. | 
| 46 |  |  | 
| 47 |  | -spec stop() -> ok. | 
| 48 |  | stop() -> | 
| 49 | 3 |     ?LOG_DEBUG("Stopping ~s", [?MODULE]), | 
| 50 | 3 |     ok. | 
| 51 |  |  | 
| 52 |  | -spec options() -> eturnal_module:options(). | 
| 53 |  | options() -> | 
| 54 | 3 |     {#{level => enum([critical, error, warning, notice, info, debug])}, | 
| 55 |  |      [{defaults, | 
| 56 |  |        #{level => info}}]}. | 
| 57 |  |  | 
| 58 |  | %% Internal functions. | 
| 59 |  |  | 
| 60 |  | -spec format_protocol(old | new) -> binary(). | 
| 61 | :-( | format_protocol(old) -> <<"'classic' STUN">>; | 
| 62 | 5 | format_protocol(new) -> <<"STUN">>. |