summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/ip.hpp13
-rw-r--r--src/common/md5calc.cpp21
-rw-r--r--src/common/md5calc.hpp5
-rw-r--r--src/common/socket.cpp6
-rw-r--r--src/common/socket.hpp30
-rw-r--r--src/common/utils.cpp20
-rw-r--r--src/common/utils.hpp4
7 files changed, 57 insertions, 42 deletions
diff --git a/src/common/ip.hpp b/src/common/ip.hpp
index e67056c..aceb765 100644
--- a/src/common/ip.hpp
+++ b/src/common/ip.hpp
@@ -21,6 +21,8 @@
#include "sanity.hpp"
+#include <netinet/in.h>
+
#include "extract.hpp"
#include "strings.hpp"
@@ -53,6 +55,17 @@ public:
IP4Address(const uint8_t (&a)[4])
: _addr{a[0], a[1], a[2], a[3]}
{}
+ explicit
+ IP4Address(struct in_addr addr)
+ {
+ static_assert(sizeof(addr) == sizeof(_addr), "4 bytes");
+ *this = IP4Address(reinterpret_cast<const uint8_t (&)[4]>(addr));
+ }
+ explicit
+ operator struct in_addr() const
+ {
+ return reinterpret_cast<const struct in_addr&>(_addr);
+ }
constexpr friend
IP4Address operator & (IP4Address l, IP4Address r)
diff --git a/src/common/md5calc.cpp b/src/common/md5calc.cpp
index ae134b7..1c48a24 100644
--- a/src/common/md5calc.cpp
+++ b/src/common/md5calc.cpp
@@ -327,26 +327,21 @@ bool pass_ok(AccountPass password, AccountCrypt crypted)
// [M|h]ashes up an IP address and a secret key
// to return a hopefully unique masked IP.
-struct in_addr MD5_ip(struct in_addr ip)
+IP4Address MD5_ip(IP4Address ip)
{
static SaltString secret = make_salt();
- union
- {
- uint8_t bytes[4];
- struct in_addr ip;
- } conv;
// MD5sum a secret + the IP address
VString<31> ipbuf;
- SNPRINTF(ipbuf, 32, "%u%s", ip.s_addr, secret);
+ SNPRINTF(ipbuf, 32, "%s %s", ip, secret);
md5_binary obuf;
MD5_to_bin(MD5_from_string(ipbuf), obuf);
// Fold the md5sum to 32 bits, pack the bytes to an in_addr
- conv.bytes[0] = obuf[0] ^ obuf[1] ^ obuf[8] ^ obuf[9];
- conv.bytes[1] = obuf[2] ^ obuf[3] ^ obuf[10] ^ obuf[11];
- conv.bytes[2] = obuf[4] ^ obuf[5] ^ obuf[12] ^ obuf[13];
- conv.bytes[3] = obuf[6] ^ obuf[7] ^ obuf[14] ^ obuf[15];
-
- return conv.ip;
+ return IP4Address({
+ static_cast<uint8_t>(obuf[0] ^ obuf[1] ^ obuf[8] ^ obuf[9]),
+ static_cast<uint8_t>(obuf[2] ^ obuf[3] ^ obuf[10] ^ obuf[11]),
+ static_cast<uint8_t>(obuf[4] ^ obuf[5] ^ obuf[12] ^ obuf[13]),
+ static_cast<uint8_t>(obuf[6] ^ obuf[7] ^ obuf[14] ^ obuf[15]),
+ });
}
diff --git a/src/common/md5calc.hpp b/src/common/md5calc.hpp
index 98f44d6..70a996e 100644
--- a/src/common/md5calc.hpp
+++ b/src/common/md5calc.hpp
@@ -11,6 +11,7 @@
#include <array>
+#include "ip.hpp"
#include "mmo.hpp"
#include "strings.hpp"
@@ -53,7 +54,7 @@ SaltString make_salt(void);
/// check plaintext password against saved saltcrypt
bool pass_ok(AccountPass password, AccountCrypt crypted);
-/// This returns an in_addr because it is configurable whether it gets called at all
-struct in_addr MD5_ip(struct in_addr ip);
+/// This returns an IP4Address because it is configurable whether it gets called at all
+IP4Address MD5_ip(IP4Address ip);
#endif // MD5CALC_HPP
diff --git a/src/common/socket.cpp b/src/common/socket.cpp
index 4655d1d..c7e6ed2 100644
--- a/src/common/socket.cpp
+++ b/src/common/socket.cpp
@@ -174,7 +174,7 @@ void connect_client(int listen_fd)
session[fd]->func_recv = recv_to_fifo;
session[fd]->func_send = send_from_fifo;
session[fd]->func_parse = default_func_parse;
- session[fd]->client_addr = client_address;
+ session[fd]->client_ip = IP4Address(client_address.sin_addr);
session[fd]->created = TimeT::now();
session[fd]->connected = 0;
}
@@ -240,7 +240,7 @@ int make_listen_port(uint16_t port)
return fd;
}
-int make_connection(uint32_t ip, uint16_t port)
+int make_connection(IP4Address ip, uint16_t port)
{
struct sockaddr_in server_address;
int fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -264,7 +264,7 @@ int make_connection(uint32_t ip, uint16_t port)
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof yes);
server_address.sin_family = AF_INET;
- server_address.sin_addr.s_addr = ip;
+ server_address.sin_addr = in_addr(ip);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#if __GNUC__ > 4 || __GNUC_MINOR__ >= 8
diff --git a/src/common/socket.hpp b/src/common/socket.hpp
index dd1c872..91a8c49 100644
--- a/src/common/socket.hpp
+++ b/src/common/socket.hpp
@@ -10,6 +10,7 @@
# include <array>
# include "dumb_ptr.hpp"
+# include "ip.hpp"
# include "utils.hpp"
# include "timer.t.hpp"
@@ -43,7 +44,7 @@ struct socket_data
/// Note that there is no need for a wdata_pos
size_t rdata_pos;
- struct sockaddr_in client_addr;
+ IP4Address client_ip;
/// Send or recieve
/// Only called when select() indicates the socket is ready
@@ -78,7 +79,7 @@ extern int fd_max;
int make_listen_port(uint16_t port);
/// Connect to an address, return a connected socket or -1
// FIXME - this is IPv4 only!
-int make_connection(uint32_t ip, uint16_t port);
+int make_connection(IP4Address ip, uint16_t port);
/// free() the structure and close() the fd
void delete_session(int);
/// Make a the internal queues bigger
@@ -143,6 +144,13 @@ void RFIFO_STRUCT(int fd, size_t pos, T& structure)
{
really_memcpy(pod_addressof_m(structure), static_cast<const uint8_t *>(RFIFOP(fd, pos)), sizeof(T));
}
+inline
+IP4Address RFIFOIP(int fd, size_t pos)
+{
+ IP4Address o;
+ RFIFO_STRUCT(fd, pos, o);
+ return o;
+}
template<uint8_t len>
inline
VString<len-1> RFIFO_STRING(int fd, size_t pos)
@@ -195,6 +203,13 @@ void RBUF_STRUCT(const uint8_t *p, size_t pos, T& structure)
{
really_memcpy(pod_addressof_m(structure), p + pos, sizeof(T));
}
+inline
+IP4Address RBUFIP(const uint8_t *p, size_t pos)
+{
+ IP4Address o;
+ RBUF_STRUCT(p, pos, o);
+ return o;
+}
template<uint8_t len>
inline
VString<len-1> RBUF_STRING(const uint8_t *p, size_t pos)
@@ -248,6 +263,12 @@ void WFIFO_STRUCT(int fd, size_t pos, T& structure)
really_memcpy(static_cast<uint8_t *>(WFIFOP(fd, pos)), pod_addressof_c(structure), sizeof(T));
}
inline
+IP4Address& WFIFOIP(int fd, size_t pos)
+{
+ static_assert(is_trivially_copyable<IP4Address>::value, "That was the whole point");
+ return *static_cast<IP4Address *>(WFIFOP(fd, pos));
+}
+inline
void WFIFO_STRING(int fd, size_t pos, XString s, size_t len)
{
char *const begin = static_cast<char *>(WFIFOP(fd, pos));
@@ -298,6 +319,11 @@ void WBUF_STRUCT(uint8_t *p, size_t pos, T& structure)
really_memcpy(p + pos, pod_addressof_c(structure), sizeof(T));
}
inline
+IP4Address& WBUFIP(uint8_t *p, size_t pos)
+{
+ return *static_cast<IP4Address *>(WBUFP(p, pos));
+}
+inline
void WBUF_STRING(uint8_t *p, size_t pos, XString s, size_t len)
{
char *const begin = static_cast<char *>(WBUFP(p, pos));
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
index c9c22b9..9ab470b 100644
--- a/src/common/utils.cpp
+++ b/src/common/utils.cpp
@@ -44,11 +44,11 @@ bool e_mail_check(XString email)
//-------------------------------------------------
int config_switch (ZString str)
{
- if (str == "on" || str == "yes"
+ if (str == "true" || str == "on" || str == "yes"
|| str == "oui" || str == "ja"
|| str == "si")
return 1;
- if (str == "off" || str == "no"
+ if (str == "false" || str == "off" || str == "no"
|| str == "non" || str == "nein")
return 0;
@@ -59,22 +59,6 @@ int config_switch (ZString str)
abort();
}
-IP_String ip2str(struct in_addr ip)
-{
- const uint8_t *p = reinterpret_cast<const uint8_t *>(&ip);
-
- IP_String out;
- SNPRINTF(out, 16, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
- return out;
-}
-VString<16> ip2str_extradot(struct in_addr ip)
-{
- const uint8_t *p = reinterpret_cast<const uint8_t *>(&ip);
- VString<16> out;
- SNPRINTF(out, 17, "%d.%d.%d.%d.", p[0], p[1], p[2], p[3]);
- return out;
-}
-
bool split_key_value(const FString& line, SString *w1, TString *w2)
{
FString::iterator begin = line.begin(), end = line.end();
diff --git a/src/common/utils.hpp b/src/common/utils.hpp
index 196bb3e..4f6190a 100644
--- a/src/common/utils.hpp
+++ b/src/common/utils.hpp
@@ -13,8 +13,6 @@
#include "strings.hpp"
#include "utils2.hpp"
-struct IP_String : VString<15> {};
-
template<class T>
struct is_trivially_copyable
: std::integral_constant<bool,
@@ -26,8 +24,6 @@ struct is_trivially_copyable
bool e_mail_check(XString email);
int config_switch (ZString str);
-IP_String ip2str(struct in_addr ip);
-VString<15 + 1> ip2str_extradot(struct in_addr ip);
bool split_key_value(const FString& line, SString *w1, TString *w2);