From 2115bd71dc5307cdcda1b7d7be3b9d9b0545531f Mon Sep 17 00:00:00 2001 From: attilamolnar Date: Mon, 9 Jul 2012 15:35:12 +0200 Subject: [PATCH] Call OnUserSetIP() whenever the IP of a local user changes, set ident,host,dhost in LocalUser constructor --- include/users.h | 6 ++++++ src/usermanager.cpp | 6 ------ src/users.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/include/users.h b/include/users.h index 57dea3fa5..5a8864cdd 100644 --- a/include/users.h +++ b/include/users.h @@ -395,6 +395,8 @@ class CoreExport User : public Extensible */ bool SetClientIP(const char* sip); + void SetClientIP(const irc::sockets::sockaddrs& sa); + /** Constructor * @throw CoreException if the UID allocated to the user already exists */ @@ -819,6 +821,10 @@ class CoreExport LocalUser : public User, public InviteBase */ void SetClass(const std::string &explicit_name = ""); + bool SetClientIP(const char* sip); + + void SetClientIP(const irc::sockets::sockaddrs& sa); + void SendText(const std::string& line); void Write(const std::string& text); void Write(const char*, ...) CUSTOM_PRINTF(2, 3); diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 785d23625..d65f47128 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -66,16 +66,10 @@ void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax); (*(this->clientlist))[New->nick] = New; - New->ident.assign("unknown"); - New->registered = REG_NONE; New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout; New->lastping = 1; - /* Smarter than your average bear^H^H^H^Hset of strlcpys. */ - New->dhost.assign(New->GetIPString(), 0, 64); - New->host.assign(New->GetIPString(), 0, 64); - ServerInstance->Users->AddLocalClone(New); ServerInstance->Users->AddGlobalClone(New); diff --git a/src/users.cpp b/src/users.cpp index 6e282f7c0..f211f6b49 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -222,10 +222,22 @@ LocalUser::LocalUser(int myfd, irc::sockets::sockaddrs* client, irc::sockets::so bytes_in(0), bytes_out(0), cmds_in(0), cmds_out(0), nping(0), CommandFloodPenalty(0), already_sent(0) { + ident = "unknown"; lastping = 0; eh.SetFd(myfd); - memcpy(&client_sa, client, sizeof(irc::sockets::sockaddrs)); memcpy(&server_sa, servaddr, sizeof(irc::sockets::sockaddrs)); + + /* + * Initialize host and dhost here to the user's IP. + * It is important to do this before calling SetClientIP() + * as that can pass execution to modules that expect these + * fields to be valid. + */ + + int port; + irc::sockets::satoap(*client, host, port); + dhost = host; + SetClientIP(*client); } User::~User() @@ -980,10 +992,36 @@ irc::sockets::cidr_mask User::GetCIDRMask() bool User::SetClientIP(const char* sip) { - this->cachedip = ""; + cachedip.clear(); return irc::sockets::aptosa(sip, 0, client_sa); } +void User::SetClientIP(const irc::sockets::sockaddrs& sa) +{ + cachedip.clear(); + memcpy(&client_sa, &sa, sizeof(irc::sockets::sockaddrs)); +} + +bool LocalUser::SetClientIP(const char* sip) +{ + irc::sockets::sockaddrs sa; + if (!irc::sockets::aptosa(sip, 0, sa)) + // Invalid + return false; + + LocalUser::SetClientIP(sa); + return true; +} + +void LocalUser::SetClientIP(const irc::sockets::sockaddrs& sa) +{ + if (sa != client_sa) + { + User::SetClientIP(sa); + FOREACH_MOD(I_OnSetUserIP,OnSetUserIP(this)); + } +} + static std::string wide_newline("\r\n"); void User::Write(const std::string& text) -- 2.39.5