Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/api/iptux-core/Models.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define IPTUX_MODELS_H

#include <arpa/inet.h>
#include <gio/gio.h>
#include <memory>
#include <netinet/in.h>
#include <string>
Expand Down Expand Up @@ -56,17 +57,33 @@ typedef enum {
class PalKey {
public:
PalKey(in_addr ipv4, int port);
explicit PalKey(GSocketAddress* address);
~PalKey();

// Copy constructor
PalKey(const PalKey& other);

// Copy assignment operator
PalKey& operator=(const PalKey& other);

// Move constructor
PalKey(PalKey&& other) noexcept;

// Move assignment operator
PalKey& operator=(PalKey&& other) noexcept;

bool operator==(const PalKey& rhs) const;

in_addr GetIpv4() const { return ipv4; }
in_addr GetIpv4() const;
std::string GetIpv4String() const;
int GetPort() const { return port; }
int GetPort() const;
std::string ToString() const;

// Get the underlying GSocketAddress (increases reference count)
GSocketAddress* GetSocketAddress() const;

private:
in_addr ipv4;
int port;
GSocketAddress* address_;
};

/**
Expand Down
94 changes: 90 additions & 4 deletions src/iptux-core/Models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <cstring>
#include <netinet/in.h>
#include <sstream>
#include <unistd.h>
Expand Down Expand Up @@ -254,18 +255,103 @@ string ChipData::getSummary() const {
return "";
}

PalKey::PalKey(in_addr ipv4, int port) : ipv4(ipv4), port(port) {}
PalKey::PalKey(in_addr ipv4, int port) {
GInetAddress* inet_addr = g_inet_address_new_from_bytes(
reinterpret_cast<const guint8*>(&ipv4), G_SOCKET_FAMILY_IPV4);
address_ = g_inet_socket_address_new(inet_addr, port);
g_object_unref(inet_addr);
}

PalKey::PalKey(GSocketAddress* address) {
address_ = G_SOCKET_ADDRESS(g_object_ref(address));
}

PalKey::~PalKey() {
if (address_) {
g_object_unref(address_);
}
}

PalKey::PalKey(const PalKey& other) {
address_ = G_SOCKET_ADDRESS(g_object_ref(other.address_));
}

PalKey& PalKey::operator=(const PalKey& other) {
if (this != &other) {
if (address_) {
g_object_unref(address_);
}
address_ = G_SOCKET_ADDRESS(g_object_ref(other.address_));
}
return *this;
}

PalKey::PalKey(PalKey&& other) noexcept : address_(other.address_) {
other.address_ = nullptr;
}

PalKey& PalKey::operator=(PalKey&& other) noexcept {
if (this != &other) {
if (address_) {
g_object_unref(address_);
}
address_ = other.address_;
other.address_ = nullptr;
}
return *this;
}

in_addr PalKey::GetIpv4() const {
GInetAddress* inet_addr = g_inet_socket_address_get_address(
G_INET_SOCKET_ADDRESS(address_));

in_addr result;
gsize size = g_inet_address_get_native_size(inet_addr);
if (size == sizeof(in_addr)) {
memcpy(&result, g_inet_address_to_bytes(inet_addr), sizeof(in_addr));
} else {
// Should not happen for IPv4
memset(&result, 0, sizeof(in_addr));
}
return result;
}

string PalKey::GetIpv4String() const {
return inAddrToString(ipv4);
GInetAddress* inet_addr = g_inet_socket_address_get_address(
G_INET_SOCKET_ADDRESS(address_));
gchar* str = g_inet_address_to_string(inet_addr);
string result(str);
g_free(str);
return result;
}

int PalKey::GetPort() const {
return g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(address_));
}

bool PalKey::operator==(const PalKey& rhs) const {
return ipv4Equal(this->ipv4, rhs.ipv4) && this->port == rhs.port;
GInetSocketAddress* this_addr = G_INET_SOCKET_ADDRESS(this->address_);
GInetSocketAddress* other_addr = G_INET_SOCKET_ADDRESS(rhs.address_);

// Compare ports
if (g_inet_socket_address_get_port(this_addr) !=
g_inet_socket_address_get_port(other_addr)) {
return false;
}

// Compare addresses
GInetAddress* this_inet = g_inet_socket_address_get_address(this_addr);
GInetAddress* other_inet = g_inet_socket_address_get_address(other_addr);

return g_inet_address_equal(this_inet, other_inet);
}

string PalKey::ToString() const {
return stringFormat("%s:%d", inAddrToString(ipv4).c_str(), port);
return stringFormat("%s:%d", GetIpv4String().c_str(), GetPort());
}

GSocketAddress* PalKey::GetSocketAddress() const {
return G_SOCKET_ADDRESS(g_object_ref(address_));
}

bool FileInfo::operator==(const FileInfo& rhs) const {
Expand Down
39 changes: 39 additions & 0 deletions src/iptux-core/ModelsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@ TEST(PalKey, GetIpv4String) {
ASSERT_EQ(key1.GetIpv4String(), "1.2.3.4");
}

TEST(PalKey, GSocketAddressConstructor) {
// Test creating PalKey from GSocketAddress
GInetAddress* inet_addr = g_inet_address_new_from_string("192.168.1.100");
ASSERT_NE(inet_addr, nullptr);

GSocketAddress* socket_addr = g_inet_socket_address_new(inet_addr, 8080);
ASSERT_NE(socket_addr, nullptr);

PalKey key(socket_addr);

// Test that the values are correctly extracted
ASSERT_EQ(key.GetIpv4String(), "192.168.1.100");
ASSERT_EQ(key.GetPort(), 8080);
ASSERT_EQ(key.ToString(), "192.168.1.100:8080");

// Test GetSocketAddress method
GSocketAddress* retrieved_addr = key.GetSocketAddress();
ASSERT_NE(retrieved_addr, nullptr);

// Verify the retrieved address matches
GInetSocketAddress* inet_socket_addr = G_INET_SOCKET_ADDRESS(retrieved_addr);
ASSERT_EQ(g_inet_socket_address_get_port(inet_socket_addr), 8080);

GInetAddress* retrieved_inet =
g_inet_socket_address_get_address(inet_socket_addr);
gchar* ip_str = g_inet_address_to_string(retrieved_inet);
ASSERT_STREQ(ip_str, "192.168.1.100");
g_free(ip_str);

// Test equality with another PalKey created from in_addr
PalKey key2(inAddrFromString("192.168.1.100"), 8080);
ASSERT_EQ(key, key2);

// Cleanup
g_object_unref(retrieved_addr);
g_object_unref(socket_addr);
g_object_unref(inet_addr);
}

TEST(NetSegment, ContainIP) {
NetSegment netSegment("1.2.3.4", "1.2.4.5", "");

Expand Down
Loading