2003-09-26 20:55:01 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : stringprep.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-09-26 20:55:01 +02:00
|
|
|
%%% Purpose : Interface to stringprep_drv
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 16 Feb 2003 by Alexey Shchepin <alexey@proces-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-09-26 20:55:01 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(stringprep).
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-09-26 20:55:01 +02:00
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
2003-09-28 20:27:55 +02:00
|
|
|
-export([start/0, start_link/0,
|
|
|
|
tolower/1,
|
|
|
|
nameprep/1,
|
|
|
|
nodeprep/1,
|
|
|
|
resourceprep/1]).
|
2003-09-26 20:55:01 +02:00
|
|
|
|
|
|
|
%% Internal exports, call-back functions.
|
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
handle_info/2,
|
|
|
|
code_change/3,
|
|
|
|
terminate/2]).
|
|
|
|
|
2004-12-01 23:48:53 +01:00
|
|
|
-define(STRINGPREP_PORT, stringprep_port).
|
|
|
|
|
2003-09-28 20:27:55 +02:00
|
|
|
-define(NAMEPREP_COMMAND, 1).
|
|
|
|
-define(NODEPREP_COMMAND, 2).
|
|
|
|
-define(RESOURCEPREP_COMMAND, 3).
|
2003-09-26 20:55:01 +02:00
|
|
|
|
|
|
|
start() ->
|
|
|
|
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
init([]) ->
|
2005-10-25 04:19:01 +02:00
|
|
|
case erl_ddll:load_driver(ejabberd:get_so_path(), stringprep_drv) of
|
|
|
|
ok -> ok;
|
|
|
|
{error, already_loaded} -> ok
|
|
|
|
end,
|
2003-09-26 20:55:01 +02:00
|
|
|
Port = open_port({spawn, stringprep_drv}, []),
|
2004-12-01 23:48:53 +01:00
|
|
|
register(?STRINGPREP_PORT, Port),
|
2003-09-26 20:55:01 +02:00
|
|
|
{ok, Port}.
|
|
|
|
|
|
|
|
|
|
|
|
%%% --------------------------------------------------------
|
|
|
|
%%% The call-back functions.
|
|
|
|
%%% --------------------------------------------------------
|
|
|
|
|
|
|
|
handle_call(_, _, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
handle_cast(_, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
2007-12-15 14:01:32 +01:00
|
|
|
handle_info({'EXIT', Port, Reason}, Port) ->
|
|
|
|
{stop, {port_died, Reason}, Port};
|
2007-12-07 01:48:11 +01:00
|
|
|
handle_info({'EXIT', _Pid, _Reason}, Port) ->
|
2003-09-26 20:55:01 +02:00
|
|
|
{noreply, Port};
|
|
|
|
handle_info(_, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
2007-12-07 01:48:11 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
2003-09-26 20:55:01 +02:00
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, Port) ->
|
|
|
|
Port ! {self, close},
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tolower(String) ->
|
2003-09-28 20:27:55 +02:00
|
|
|
control(0, String).
|
|
|
|
|
|
|
|
nameprep(String) ->
|
|
|
|
control(?NAMEPREP_COMMAND, String).
|
|
|
|
|
|
|
|
nodeprep(String) ->
|
|
|
|
control(?NODEPREP_COMMAND, String).
|
|
|
|
|
|
|
|
resourceprep(String) ->
|
|
|
|
control(?RESOURCEPREP_COMMAND, String).
|
|
|
|
|
|
|
|
control(Command, String) ->
|
2004-12-01 23:48:53 +01:00
|
|
|
case port_control(?STRINGPREP_PORT, Command, String) of
|
2003-09-28 20:27:55 +02:00
|
|
|
[0 | _] -> error;
|
|
|
|
[1 | Res] -> Res
|
|
|
|
end.
|
2003-09-26 20:55:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|