From 99c8df01284adf562b70c854577e5e781edd7272 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Wed, 20 Aug 2014 16:01:16 +0200 Subject: [PATCH] Add 'auth-infos-clear' daemon command to flush auth infos --- daemon/Makefile.am | 3 +- daemon/commands/auth-infos-clear.cc | 52 +++++++++++++++++++++++++++++ daemon/commands/auth-infos-clear.h | 12 +++++++ daemon/daemon.cc | 14 ++++++++ daemon/daemon.h | 2 ++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 daemon/commands/auth-infos-clear.cc create mode 100644 daemon/commands/auth-infos-clear.h diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 4c39ff8e0..4c9f4136e 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -12,6 +12,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/audio-stream-start.cc \ commands/audio-stream-stop.cc \ commands/audio-stream-stats.cc \ + commands/auth-infos-clear.cc \ commands/call.cc \ commands/call-stats.cc \ commands/call-status.cc \ @@ -86,7 +87,7 @@ linphone_daemon_LDADD=$(top_builddir)/coreapi/liblinphone.la $(READLINE_LIBS) \ $(MEDIASTREAMER_LIBS) \ $(ORTP_LIBS) \ $(SPEEX_LIBS) \ - $(LIBXML2_LIBS) + $(LIBXML2_LIBS) AM_CFLAGS=$(READLINE_CFLAGS) -DIN_LINPHONE $(STRICT_OPTIONS) AM_CXXFLAGS=$(READLINE_CXXFLAGS) -DIN_LINPHONE $(STRICT_OPTIONS) diff --git a/daemon/commands/auth-infos-clear.cc b/daemon/commands/auth-infos-clear.cc new file mode 100644 index 000000000..b39fe51e9 --- /dev/null +++ b/daemon/commands/auth-infos-clear.cc @@ -0,0 +1,52 @@ +#include "auth-infos-clear.h" + +using namespace std; + +AuthInfosClearCommand::AuthInfosClearCommand() : + DaemonCommand("auth-infos-clear", "auth-infos-clear ", "Remove auth infos context for the given id, or all.") { + addExample(new DaemonCommandExample("auth-infos-clear 1", + "Status: Ok\n" + "Reason: Successfully cleared auth info 1.")); + addExample(new DaemonCommandExample("auth-infos-clear ALL", + "Status: Ok\n" + "Reason: Successfully cleared 5 auth infos.")); + addExample(new DaemonCommandExample("auth-infos-clear 3", + "Status: Error\n" + "Reason: No auth info with such id.")); +} + +void AuthInfosClearCommand::exec(Daemon *app, const char *args) { + string param; + int pid; + ostringstream ostr; + + istringstream ist(args); + ist >> param; + if (ist.fail()) { + app->sendResponse(Response("Missing parameter.", Response::Error)); + return; + } + if (param.compare("ALL") == 0) { + int previous_size = app->maxAuthInfoId(); + linphone_core_clear_all_auth_info(app->getCore()); + ostr << "Successfully cleared " << previous_size - app->maxAuthInfoId() << " auth infos." << endl; + app->sendResponse(Response(ostr.str().c_str(), Response::Ok)); + } else { + LinphoneAuthInfo *auth_info = NULL; + ist.clear(); + ist.str(param); + ist >> pid; + if (ist.fail()) { + app->sendResponse(Response("Incorrect parameter.", Response::Error)); + return; + } + auth_info = app->findAuthInfo(pid); + if (auth_info == NULL) { + app->sendResponse(Response("No auth info with such id.", Response::Error)); + return; + } + linphone_core_remove_auth_info(app->getCore(), auth_info); + ostr << "Successfully cleared auth info " << pid << "." << endl; + app->sendResponse(Response(ostr.str().c_str(), Response::Ok)); + } +} diff --git a/daemon/commands/auth-infos-clear.h b/daemon/commands/auth-infos-clear.h new file mode 100644 index 000000000..a15861cd7 --- /dev/null +++ b/daemon/commands/auth-infos-clear.h @@ -0,0 +1,12 @@ +#ifndef COMMAND_AUTH_INFOS_CLEAR_H_ +#define COMMAND_AUTH_INFOS_CLEAR_H_ + +#include "../daemon.h" + +class AuthInfosClearCommand: public DaemonCommand { +public: + AuthInfosClearCommand(); + virtual void exec(Daemon *app, const char *args); +}; + +#endif //COMMAND_AUTH_INFOS_CLEAR_H_ diff --git a/daemon/daemon.cc b/daemon/daemon.cc index f7a310798..ebac0fbaf 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -23,6 +23,7 @@ #include "commands/audio-stream-start.h" #include "commands/audio-stream-stop.h" #include "commands/audio-stream-stats.h" +#include "commands/auth-infos-clear.h" #include "commands/call.h" #include "commands/call-stats.h" #include "commands/call-status.h" @@ -374,6 +375,18 @@ LinphoneProxyConfig *Daemon::findProxy(int id) { return NULL; } +LinphoneAuthInfo *Daemon::findAuthInfo(int id) { + const MSList *elem = linphone_core_get_auth_info_list(mLc); + if (elem == NULL || id < 1 || id > ms_list_size(elem)) { + return NULL; + } + while (id > 1) { + elem = elem->next; + --id; + } + return (LinphoneAuthInfo *) elem->data; +} + int Daemon::updateAudioStreamId(AudioStream *audio_stream) { for (std::map::iterator it = mAudioStreams.begin(); it != mAudioStreams.end(); ++it) { if (it->second->stream == audio_stream) @@ -412,6 +425,7 @@ void Daemon::initCommands() { mCommands.push_back(new ContactCommand()); mCommands.push_back(new RegisterStatusCommand()); mCommands.push_back(new UnregisterCommand()); + mCommands.push_back(new AuthInfosClearCommand()); mCommands.push_back(new CallCommand()); mCommands.push_back(new TerminateCommand()); mCommands.push_back(new DtmfCommand()); diff --git a/daemon/daemon.h b/daemon/daemon.h index e1f7de095..0e83cb8bf 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -198,6 +198,7 @@ public: const std::list &getCommandList() const; LinphoneCall *findCall(int id); LinphoneProxyConfig *findProxy(int id); + LinphoneAuthInfo *findAuthInfo(int id); AudioStream *findAudioStream(int id); AudioStreamAndOther *findAudioStreamAndOther(int id); void removeAudioStream(int id); @@ -205,6 +206,7 @@ public: int updateCallId(LinphoneCall *call); int updateProxyId(LinphoneProxyConfig *proxy); inline int maxProxyId() { return mProxyIds; } + inline int maxAuthInfoId() { return ms_list_size(linphone_core_get_auth_info_list(mLc)); } int updateAudioStreamId(AudioStream *audio_stream); void dumpCommandsHelp(); void dumpCommandsHelpHtml();