* @param ip The IP address of the user
* @return This function has no return value, but a call to AddClient may remove the user.
*/
- void AddUser(InspIRCd* Instance, int socket, int port, bool iscached, sockaddr* ip, const std::string &targetip);
+ void AddUser(InspIRCd* Instance, int socket, int port, bool iscached, irc::sockets::sockaddrs* ip, const std::string &targetip);
/** Disconnect a user gracefully
* @param user The user to remove
*/
bool quitting;
- /** IPV4 or IPV6 ip address. Use SetSockAddr to set this and GetProtocolFamily/
- * GetIPString/GetPort to obtain its values.
+ /** IPV4 or IPV6 ip address, binary format. Use SetSockAddr to set this and
+ * GetIPString/GetPort to obtain its value in a readable manner
*/
- sockaddr* ip;
+ irc::sockets::sockaddrs ip;
/** Initialize the clients sockaddr
- * @param protocol_family The protocol family of the IP address, AF_INET or AF_INET6
- * @param ip A human-readable IP address for this user matching the protcol_family
- * @param port The port number of this user or zero for a remote user
+ * @param ip A human-readable IP address for this user
+ * @param port The port number of this user (zero if unknown)
*/
- void SetSockAddr(int protocol_family, const char* ip, int port);
+ void SetSockAddr(const char* ip, int port);
/** Get port number from sockaddr
* @return The port number of this user.
*/
int GetPort();
- /** Get protocol family from sockaddr
- * @return The protocol family of this user, either AF_INET or AF_INET6
- */
- int GetProtocolFamily();
-
/** Get IP string from sockaddr, using static internal buffer
* @return The IP string
*/
void ClientListenSocket::OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
{
- ServerInstance->Users->AddUser(ServerInstance, nfd, bind_port, false, &client.sa, ipconnectedto);
+ ServerInstance->Users->AddUser(ServerInstance, nfd, bind_port, false, &client, ipconnectedto);
}
them->host.assign(result,0, 64);
them->dhost.assign(result, 0, 64);
if (querytype)
- them->SetSockAddr(them->GetProtocolFamily(), result.c_str(), them->GetPort());
+ them->SetSockAddr(result.c_str(), them->GetPort());
them->ident.assign("~cgiirc", 0, 8);
them->InvalidateCache();
them->CheckLines(true);
if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
{
ServerInstance->Users->RemoveCloneCounts(user);
- user->SetSockAddr(user->GetProtocolFamily(), webirc_ip->c_str(), user->GetPort());
+ user->SetSockAddr(webirc_ip->c_str(), user->GetPort());
delete webirc_ip;
user->InvalidateCache();
user->Shrink("cgiirc_webirc_ip");
bool valid = false;
ServerInstance->Users->RemoveCloneCounts(user);
#ifdef IPV6
- if (user->GetProtocolFamily() == AF_INET6)
- valid = (inet_pton(AF_INET6, user->password.c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
+ if (user->ip.sa.sa_family == AF_INET6)
+ valid = (inet_pton(AF_INET6, user->password.c_str(), &user->ip.in6.sin6_addr) > 0);
else
- valid = (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr));
+ valid = (inet_aton(user->password.c_str(), &user->ip.in4.sin_addr));
#else
if (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr))
valid = true;
user->Extend("cgiirc_realhost", new std::string(user->host));
user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
ServerInstance->Users->RemoveCloneCounts(user);
- user->SetSockAddr(user->GetProtocolFamily(), newip, user->GetPort());
+ user->SetSockAddr(newip, user->GetPort());
ServerInstance->Users->AddLocalClone(user);
ServerInstance->Users->AddGlobalClone(user);
user->CheckClass();
#ifdef IPV6
in6_addr testaddr;
in_addr testaddr2;
- if ((dest->GetProtocolFamily() == AF_INET6) && (inet_pton(AF_INET6,dest->host.c_str(),&testaddr) < 1) && (hostcloak.length() <= 64))
+ if ((dest->ip.sa.sa_family == AF_INET6) && (inet_pton(AF_INET6,dest->host.c_str(),&testaddr) < 1) && (hostcloak.length() <= 64))
/* Invalid ipv6 address, and ipv6 user (resolved host) */
b = hostcloak;
- else if ((dest->GetProtocolFamily() == AF_INET) && (inet_aton(dest->host.c_str(),&testaddr2) < 1) && (hostcloak.length() <= 64))
+ else if ((dest->ip.sa.sa_family == AF_INET) && (inet_aton(dest->host.c_str(),&testaddr2) < 1) && (hostcloak.length() <= 64))
/* Invalid ipv4 address, and ipv4 user (resolved host) */
b = hostcloak;
else
else
{
#ifdef IPV6
- if (dest->GetProtocolFamily() == AF_INET6)
+ if (dest->ip.sa.sa_family == AF_INET6)
b = cu->Cloak6(dest->GetIPString());
+ else
#endif
- if (dest->GetProtocolFamily() == AF_INET)
b = cu->Cloak4(dest->GetIPString());
}
int range = 32;
clonemap::iterator i;
- switch (u->GetProtocolFamily())
+ switch (u->ip.sa.sa_family)
{
case AF_INET6:
range = ipv6_cidr;
if (params[8][0] != '+')
params[8] = "+" + params[8];
- if (params[6].find_first_of(":") != std::string::npos)
- _new->SetSockAddr(AF_INET6, params[6].c_str(), 0);
- else
- _new->SetSockAddr(AF_INET, params[6].c_str(), 0);
+ _new->SetSockAddr(params[6].c_str(), 0);
ServerInstance->Users->AddGlobalClone(_new);
{
bool lcached = false;
#ifdef IPV6
- if (this->bound_user->GetProtocolFamily() == AF_INET6)
+ if (this->bound_user->ip.sa.sa_family == AF_INET6)
{
- /* IPV6 forward lookup (with possibility of 4in6) */
- const char* ip = this->bound_user->GetIPString();
- res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (!strncmp(ip, "0::ffff:", 8) ? DNS_QUERY_A : DNS_QUERY_AAAA), lcached);
+ /* IPV6 forward lookup */
+ res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_AAAA, lcached);
}
else
/* IPV4 lookup (mixed protocol mode) */
{
/* Both lookups completed */
- sockaddr* user_ip = this->bound_user->ip;
+ irc::sockets::sockaddrs* user_ip = &this->bound_user->ip;
bool rev_match = false;
#ifdef IPV6
- if (user_ip->sa_family == AF_INET6)
+ if (user_ip->sa.sa_family == AF_INET6)
{
struct in6_addr res_bin;
if (inet_pton(AF_INET6, result.c_str(), &res_bin))
{
- sockaddr_in6* user_ip6 = reinterpret_cast<sockaddr_in6*>(user_ip);
- rev_match = !memcmp(&user_ip6->sin6_addr, &res_bin, sizeof(res_bin));
+ rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
}
}
else
struct in_addr res_bin;
if (inet_pton(AF_INET, result.c_str(), &res_bin))
{
- sockaddr_in* user_ip4 = reinterpret_cast<sockaddr_in*>(user_ip);
- rev_match = !memcmp(&user_ip4->sin_addr, &res_bin, sizeof(res_bin));
+ rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
}
}
#include "bancache.h"
/* add a client connection to the sockets list */
-void UserManager::AddUser(InspIRCd* Instance, int socket, int port, bool iscached, sockaddr* ip, const std::string &targetip)
+void UserManager::AddUser(InspIRCd* Instance, int socket, int port, bool iscached, irc::sockets::sockaddrs* ip, const std::string &targetip)
{
/* NOTE: Calling this one parameter constructor for User automatically
* allocates a new UUID and places it in the hash_map.
return;
}
- char ipaddr[MAXBUF];
-#ifdef IPV6
- if (ip->sa_family == AF_INET6)
- inet_ntop(AF_INET6, &((const sockaddr_in6*)ip)->sin6_addr, ipaddr, sizeof(ipaddr));
- else
-#endif
- inet_ntop(AF_INET, &((const sockaddr_in*)ip)->sin_addr, ipaddr, sizeof(ipaddr));
-
New->SetFd(socket);
- New->SetSockAddr(ip->sa_family, ipaddr, port);
+ memcpy(&New->ip, ip, sizeof(*ip));
/* Give each of the modules an attempt to hook the user for I/O */
FOREACH_MOD_I(Instance, I_OnHookUserIO, OnHookUserIO(New, targetip));
{
try
{
- New->GetIOHook()->OnRawSocketAccept(socket, ipaddr, port);
+ New->GetIOHook()->OnRawSocketAccept(socket, New->GetIPString(), port);
}
catch (CoreException& modexcept)
{
{
int range = 32;
clonemap::iterator x;
- switch (user->GetProtocolFamily())
+ switch (user->ip.sa.sa_family)
{
case AF_INET6:
range = ServerInstance->Config->c_ipv6_range;
{
int range = 32;
clonemap::iterator x;
- switch (user->GetProtocolFamily())
+ switch (user->ip.sa.sa_family)
{
case AF_INET6:
range = ServerInstance->Config->c_ipv6_range;
void UserManager::RemoveCloneCounts(User *user)
{
int range = 32;
- switch (user->GetProtocolFamily())
+ switch (user->ip.sa.sa_family)
{
case AF_INET6:
range = ServerInstance->Config->c_ipv6_range;
unsigned long UserManager::GlobalCloneCount(User *user)
{
int range = 32;
- switch (user->GetProtocolFamily())
+ switch (user->ip.sa.sa_family)
{
case AF_INET6:
range = ServerInstance->Config->c_ipv6_range;
unsigned long UserManager::LocalCloneCount(User *user)
{
int range = 32;
- switch (user->GetProtocolFamily())
+ switch (user->ip.sa.sa_family)
{
case AF_INET6:
range = ServerInstance->Config->c_ipv6_range;
const char* sip = this->GetIPString();
UserResolver *res_reverse;
- QueryType resolvtype = strchr(sip, ':') ? DNS_QUERY_PTR6 : DNS_QUERY_PTR4;
- // when GetProtocolFamily() works correctly with 4in6, this can be replaced by
- // this->GetProtocolFamily() == AF_INET ? DNS_QUERY_PTR4 : DNS_QUERY_PTR6;
+ QueryType resolvtype = this->ip.sa.sa_family == AF_INET6 ? DNS_QUERY_PTR6 : DNS_QUERY_PTR4;
res_reverse = new UserResolver(this->ServerInstance, this, sip, resolvtype, cached);
this->ServerInstance->AddResolver(res_reverse, cached);
bytes_in = bytes_out = cmds_in = cmds_out = 0;
quietquit = quitting = exempt = haspassed = dns_done = false;
fd = -1;
+ ip.sa.sa_family = AF_UNSPEC;
recvq.clear();
sendq.clear();
Visibility = NULL;
- ip = NULL;
MyClass = NULL;
AllowedPrivs = AllowedOperCommands = NULL;
chans.clear();
this->InvalidateCache();
this->DecrementModes();
- if (ip)
- {
+ if (ip.sa.sa_family != AF_UNSPEC)
ServerInstance->Users->RemoveCloneCounts(this);
- if (this->GetProtocolFamily() == AF_INET)
- {
- delete (sockaddr_in*)ip;
- }
- else
- {
- delete (sockaddr_in6*)ip;
- }
- }
-
ServerInstance->Users->uuidlist->erase(uuid);
}
return false;
}
-void User::SetSockAddr(int protocol_family, const char* sip, int port)
+void User::SetSockAddr(const char* sip, int port)
{
this->cachedip = "";
- switch (protocol_family)
+ if (inet_pton(AF_INET, sip, &ip.in4.sin_addr))
{
- case AF_INET6:
- {
- sockaddr_in6* sin = new sockaddr_in6;
- sin->sin6_family = AF_INET6;
- sin->sin6_port = port;
- inet_pton(AF_INET6, sip, &sin->sin6_addr);
- this->ip = (sockaddr*)sin;
- }
- break;
- case AF_INET:
- {
- sockaddr_in* sin = new sockaddr_in;
- sin->sin_family = AF_INET;
- sin->sin_port = port;
- inet_pton(AF_INET, sip, &sin->sin_addr);
- this->ip = (sockaddr*)sin;
- }
- break;
- default:
- ServerInstance->Logs->Log("USERS",DEBUG,"Uh oh, I dont know protocol %d to be set on '%s'!", protocol_family, this->nick.c_str());
- break;
+ ip.in4.sin_family = AF_INET;
+ ip.in4.sin_port = port;
+ return;
+ }
+ else if (inet_pton(AF_INET6, sip, &ip.in6.sin6_addr))
+ {
+ ip.in6.sin6_family = AF_INET6;
+ ip.in6.sin6_port = port;
+ }
+ else
+ {
+ ServerInstance->Logs->Log("USERS",DEBUG,"Uh oh, I dont know how to read IP '%s' on '%s'!",
+ sip, this->nick.c_str());
}
}
int User::GetPort()
{
- if (this->ip == NULL)
- return 0;
-
- switch (this->GetProtocolFamily())
+ switch (this->ip.sa.sa_family)
{
case AF_INET6:
- {
- sockaddr_in6* sin = (sockaddr_in6*)this->ip;
- return sin->sin6_port;
- }
- break;
+ return this->ip.in6.sin6_port;
case AF_INET:
- {
- sockaddr_in* sin = (sockaddr_in*)this->ip;
- return sin->sin_port;
- }
- break;
- default:
- break;
+ return this->ip.in4.sin_port;
}
return 0;
}
-int User::GetProtocolFamily()
-{
- if (this->ip == NULL)
- return 0;
-
- sockaddr_in* sin = (sockaddr_in*)this->ip;
- return sin->sin_family;
-}
-
const char* User::GetCIDRMask(int range)
{
static char buf[44];
- if (this->ip == NULL)
- return "";
-
if (range < 0)
throw "Negative range, sorry, no.";
* Original code written by Oliver Lupton (Om).
* Integrated by me. Thanks. :) -- w00t
*/
- switch (this->GetProtocolFamily())
+ switch (this->ip.sa.sa_family)
{
case AF_INET6:
{
/* unsigned char s6_addr[16]; */
struct in6_addr v6;
- sockaddr_in6* sin;
int i, bytestozero, extrabits;
char buffer[40];
if(range > 128)
throw "CIDR mask width greater than address width (IPv6, 128 bit)";
- /* Access the user's IP structure directly */
- sin = (sockaddr_in6*)this->ip;
-
/* To create the CIDR mask we want to set all the bits after 'range' bits of the address
* to zero. This means the last (128 - range) bits of the address must be set to zero.
* Hence this number divided by 8 is the number of whole bytes from the end of the address
*/
for(i = 0; i < (16 - bytestozero); i++)
{
- v6.s6_addr[i] = sin->sin6_addr.s6_addr[i];
+ v6.s6_addr[i] = ip.in6.sin6_addr.s6_addr[i];
}
/* And zero all the remaining bytes in the IP. */
case AF_INET:
{
struct in_addr v4;
- sockaddr_in* sin;
char buffer[16];
if (range > 32)
throw "CIDR mask width greater than address width (IPv4, 32 bit)";
/* Users already have a sockaddr* pointer (User::ip) which contains either a v4 or v6 structure */
- sin = (sockaddr_in*)this->ip;
- v4.s_addr = sin->sin_addr.s_addr;
+ v4.s_addr = ip.in4.sin_addr.s_addr;
/* To create the CIDR mask we want to set all the bits after 'range' bits of the address
* to zero. This means the last (32 - range) bits of the address must be set to zero.
{
static char buf[40];
- if (this->ip == NULL)
- return "";
-
if (!this->cachedip.empty())
return this->cachedip.c_str();
- switch (this->GetProtocolFamily())
+ switch (this->ip.sa.sa_family)
{
case AF_INET6:
{
- static char temp[1024];
+ static char temp[41];
- sockaddr_in6* sin = (sockaddr_in6*)this->ip;
- inet_ntop(sin->sin6_family, &sin->sin6_addr, buf, sizeof(buf));
+ inet_ntop(ip.in6.sin6_family, &ip.in6.sin6_addr, buf, sizeof(buf));
/* IP addresses starting with a : on irc are a Bad Thing (tm) */
if (*buf == ':')
{
break;
case AF_INET:
{
- sockaddr_in* sin = (sockaddr_in*)this->ip;
- inet_ntop(sin->sin_family, &sin->sin_addr, buf, sizeof(buf));
+ inet_ntop(ip.in4.sin_family, &ip.in4.sin_addr, buf, sizeof(buf));
this->cachedip = buf;
return buf;
}