summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2009-09-01 15:07:52 +0000
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2009-09-01 15:07:52 +0000
commitb47228902d585dd16953b7f9bc395553890fc56e (patch)
treeffc4ed1d4177c2e2f0d10ea01f17c4ea8ee0409a /src
parent77d5c9f765702a06a1ad77f239d1cbe2d9b11662 (diff)
Move IP<->sockaddr conversions into irc::sockets:: namespace
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11580 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src')
-rw-r--r--src/inspsocket.cpp53
-rw-r--r--src/modules/m_cloaking.cpp5
-rw-r--r--src/socket.cpp53
-rw-r--r--src/users.cpp68
4 files changed, 35 insertions, 144 deletions
diff --git a/src/inspsocket.cpp b/src/inspsocket.cpp
index a23518148..e42990a74 100644
--- a/src/inspsocket.cpp
+++ b/src/inspsocket.cpp
@@ -53,22 +53,8 @@ BufferedSocket::BufferedSocket(InspIRCd* SI, const std::string &ipaddr, int apor
strlcpy(this->host,ipaddr.c_str(),MAXBUF);
this->port = aport;
- bool ipvalid = true;
-#ifdef IPV6
- if (strchr(host,':'))
- {
- in6_addr n;
- if (inet_pton(AF_INET6, host, &n) < 1)
- ipvalid = false;
- }
- else
-#endif
- {
- in_addr n;
- if (inet_aton(host,&n) < 1)
- ipvalid = false;
- }
- if (!ipvalid)
+ irc::sockets::sockaddrs testaddr;
+ if (!irc::sockets::aptosa(host, aport, &testaddr))
{
this->ServerInstance->Logs->Log("SOCKET", DEBUG,"BUG: Hostname passed to BufferedSocket, rather than an IP address!");
this->OnError(I_ERR_CONNECT);
@@ -106,40 +92,13 @@ void BufferedSocket::SetQueues()
bool BufferedSocket::DoBindMagic(const std::string &current_ip, bool v6)
{
irc::sockets::sockaddrs s;
- socklen_t size = sizeof(sockaddr_in);
-#ifdef IPV6
- if (v6)
+ if (!irc::sockets::aptosa(current_ip.c_str(), 0, &s))
{
- if (inet_pton(AF_INET6, current_ip.c_str(), &s.in6.sin6_addr) > 0)
- {
- s.in6.sin6_port = 0;
- s.in6.sin6_family = AF_INET6;
- size = sizeof(sockaddr_in6);
- }
- else
- {
- // Well, this is as good as it's gonna get.
- errno = EADDRNOTAVAIL;
- return false;
- }
- }
- else
-#endif
- {
- if (inet_aton(current_ip.c_str(), &s.in4.sin_addr) > 0)
- {
- s.in4.sin_port = 0;
- s.in4.sin_family = AF_INET;
- }
- else
- {
- // Well, this is as good as it's gonna get.
- errno = EADDRNOTAVAIL;
- return false;
- }
+ errno = EADDRNOTAVAIL;
+ return false;
}
- if (ServerInstance->SE->Bind(this->fd, &s.sa, size) < 0)
+ if (ServerInstance->SE->Bind(this->fd, &s.sa, sizeof(s)) < 0)
{
this->state = I_ERROR;
this->OnError(I_ERR_BIND);
diff --git a/src/modules/m_cloaking.cpp b/src/modules/m_cloaking.cpp
index a17577702..ae409ae93 100644
--- a/src/modules/m_cloaking.cpp
+++ b/src/modules/m_cloaking.cpp
@@ -410,8 +410,9 @@ class ModuleCloaking : public Module
* Their ISP shouldnt go to town on subdomains, or they shouldnt have a kiddie
* vhost.
*/
- in6_addr testaddr;
- if (inet_pton(dest->client_sa.sa.sa_family, dest->host.c_str(), &testaddr) < 1 && (hostcloak.length() <= 64))
+ std::string testaddr;
+ int testport;
+ if (!irc::sockets::satoap(&dest->client_sa, testaddr, testport) && (hostcloak.length() <= 64))
/* not a valid address, must have been a host, so cloak as a host */
b = hostcloak;
else if (dest->client_sa.sa.sa_family == AF_INET6)
diff --git a/src/socket.cpp b/src/socket.cpp
index 7d44b7e37..4d3509836 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -25,40 +25,18 @@ using irc::sockets::sockaddrs;
bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
{
sockaddrs servaddr;
- memset(&servaddr, 0, sizeof(servaddr));
-
- int ret, size;
+ int ret;
if (*addr == '*')
addr = "";
-#ifdef IPV6
if (*addr)
{
- /* There is an address here. Is it ipv6? */
- if (strchr(addr,':'))
- {
- if (inet_pton(AF_INET6, addr, &servaddr.in6.sin6_addr) < 1)
- {
- return false;
- }
- servaddr.in6.sin6_family = AF_INET6;
- servaddr.in6.sin6_port = htons(port);
- size = sizeof(sockaddr_in6);
- }
- else
- {
- if (inet_pton(AF_INET, addr, &servaddr.in4.sin_addr) < 1)
- {
- return false;
- }
- servaddr.in4.sin_family = AF_INET;
- servaddr.in4.sin_port = htons(port);
- size = sizeof(sockaddr_in);
- }
+ irc::sockets::aptosa(addr, port, &servaddr);
}
else
{
+#ifdef IPV6
if (port == -1)
{
/* Port -1: Means UDP IPV4 port binding - Special case
@@ -67,37 +45,22 @@ bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
servaddr.in4.sin_family = AF_INET;
servaddr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.in4.sin_port = 0;
- size = sizeof(sockaddr_in);
}
else
{
/* There's no address here, default to ipv6 bind to all */
servaddr.in6.sin6_family = AF_INET6;
servaddr.in6.sin6_port = htons(port);
- size = sizeof(sockaddr_in6);
+ memset(&servaddr.in6.sin6_addr, 0, sizeof(servaddr.in6.sin6_addr));
}
- }
#else
- /* If we aren't built with ipv6, the choice becomes simple */
- servaddr.in4.sin_family = AF_INET;
- if (*addr)
- {
- /* There is an address here. */
- if (inet_pton(AF_INET, addr, &servaddr.in4.sin_addr) < 1)
- {
- return false;
- }
- }
- else
- {
/* Bind ipv4 to all */
+ servaddr.in4.sin_family = AF_INET;
servaddr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
- }
- /* Bind ipv4 port number */
- servaddr.in4.sin_port = htons(port);
- size = sizeof(sockaddr_in);
+ servaddr.in4.sin_port = htons(port);
#endif
- ret = SE->Bind(sockfd, &servaddr.sa, size);
+ }
+ ret = SE->Bind(sockfd, &servaddr.sa, sizeof(servaddr));
if (ret < 0)
{
diff --git a/src/users.cpp b/src/users.cpp
index 7663a63b2..8e45d1772 100644
--- a/src/users.cpp
+++ b/src/users.cpp
@@ -1066,9 +1066,9 @@ int User::GetServerPort()
switch (this->server_sa.sa.sa_family)
{
case AF_INET6:
- return this->server_sa.in6.sin6_port;
+ return htons(this->server_sa.in6.sin6_port);
case AF_INET:
- return this->server_sa.in4.sin_port;
+ return htons(this->server_sa.in4.sin_port);
}
return 0;
}
@@ -1169,64 +1169,32 @@ const char* User::GetCIDRMask(int range)
return ""; // unused, but oh well
}
-const char* User::GetIPString()
+std::string User::GetServerIP()
{
- static char buf[40];
-
- if (!this->cachedip.empty())
- return this->cachedip.c_str();
+ int port;
+ std::string ip;
+ irc::sockets::satoap(&server_sa, ip, port);
+ return ip;
+}
- switch (this->client_sa.sa.sa_family)
+const char* User::GetIPString()
+{
+ int port;
+ if (cachedip.empty())
{
- case AF_INET6:
- {
- static char temp[41];
-
- inet_ntop(client_sa.in6.sin6_family, &client_sa.in6.sin6_addr, buf, sizeof(buf));
- /* IP addresses starting with a : on irc are a Bad Thing (tm) */
- if (*buf == ':')
- {
- strlcpy(&temp[1], buf, sizeof(temp) - 1);
- *temp = '0';
- this->cachedip = temp;
- return temp;
- }
-
- this->cachedip = buf;
- return buf;
- }
- break;
- case AF_INET:
- {
- inet_ntop(client_sa.in4.sin_family, &client_sa.in4.sin_addr, buf, sizeof(buf));
- this->cachedip = buf;
- return buf;
- }
- break;
- default:
- break;
+ irc::sockets::satoap(&client_sa, cachedip, port);
+ /* IP addresses starting with a : on irc are a Bad Thing (tm) */
+ if (cachedip.c_str()[0] == ':')
+ cachedip.insert(0,1,'0');
}
- // Unreachable, probably
- return "";
+ return cachedip.c_str();
}
bool User::SetClientIP(const char* sip)
{
this->cachedip = "";
- if (inet_pton(AF_INET, sip, &client_sa.in4.sin_addr))
- {
- client_sa.in4.sin_family = AF_INET;
- client_sa.in4.sin_port = 0;
- return true;
- }
- else if (inet_pton(AF_INET6, sip, &client_sa.in6.sin6_addr))
- {
- client_sa.in6.sin6_family = AF_INET6;
- client_sa.in6.sin6_port = 0;
- return true;
- }
- return false;
+ return irc::sockets::aptosa(sip, 0, &client_sa);
}
/** NOTE: We cannot pass a const reference to this method.