diff --git a/src/api/iptux-core/CoreThread.h b/src/api/iptux-core/CoreThread.h index a476031cf..bd9dda847 100644 --- a/src/api/iptux-core/CoreThread.h +++ b/src/api/iptux-core/CoreThread.h @@ -4,9 +4,9 @@ #include #include #include -#include #include +#include #include #include "iptux-core/Event.h" @@ -32,14 +32,14 @@ class CoreThread { uint16_t port() const; std::shared_ptr getProgramData(); - bool BlacklistContainItem(in_addr ipv4) const; + bool BlacklistContainItem(uint32_t ipv4) const; /** * @brief add ipaddress to block list * * @param ipv4 the ip address */ - void AddBlockIp(in_addr ipv4); + void AddBlockIp(uint32_t ipv4); /** * @brief whether the ipv4 address is blocked? @@ -48,7 +48,7 @@ class CoreThread { * @return true if blocked * @return false if not blocked */ - bool IsBlocked(in_addr ipv4) const; + bool IsBlocked(uint32_t ipv4) const; void Lock() const; void Unlock() const; @@ -58,17 +58,17 @@ class CoreThread { CPPalInfo GetPal(PalKey palKey) const; PPalInfo GetPal(PalKey palKey); - CPPalInfo GetPal(in_addr ipv4) const { return GetPal(PalKey(ipv4, port())); } - PPalInfo GetPal(in_addr ipv4) { return GetPal(PalKey(ipv4, port())); } + CPPalInfo GetPal(uint32_t ipv4) const { return GetPal(PalKey(ipv4, port())); } + PPalInfo GetPal(uint32_t ipv4) { return GetPal(PalKey(ipv4, port())); } CPPalInfo GetPal(const std::string& ipv4) const; PPalInfo GetPal(const std::string& ipv4); virtual void DelPalFromList(PalKey palKey); - virtual void DelPalFromList(in_addr palKey) { + virtual void DelPalFromList(uint32_t palKey) { DelPalFromList(PalKey(palKey, port())); } virtual void UpdatePalToList(PalKey palKey); - virtual void UpdatePalToList(in_addr palKey) { + virtual void UpdatePalToList(uint32_t palKey) { UpdatePalToList(PalKey(palKey, port())); } @@ -138,7 +138,7 @@ class CoreThread { const std::string& password); void SendDetectPacket(const std::string& ipv4); - void SendDetectPacket(in_addr ipv4); + void SendDetectPacket(uint32_t ipv4); void SendExit(PPalInfo pal); void SendMyIcon(PPalInfo pal, std::istream& iss); void SendSharedFiles(PPalInfo pal); @@ -183,8 +183,8 @@ class CoreThread { protected: std::shared_ptr programData; std::shared_ptr config; - int tcpSock; - int udpSock; + GSocket* tcpSock; + GSocket* udpSock; mutable std::mutex mutex; // 锁 private: diff --git a/src/api/iptux-core/Models.h b/src/api/iptux-core/Models.h index b4fe1d101..7e0124960 100644 --- a/src/api/iptux-core/Models.h +++ b/src/api/iptux-core/Models.h @@ -12,9 +12,8 @@ #ifndef IPTUX_MODELS_H #define IPTUX_MODELS_H -#include +#include #include -#include #include #include @@ -55,17 +54,17 @@ typedef enum { class PalKey { public: - PalKey(in_addr ipv4, int port); + PalKey(uint32_t ipv4, int port); bool operator==(const PalKey& rhs) const; - in_addr GetIpv4() const { return ipv4; } + uint32_t GetIpv4() const { return ipv4; } std::string GetIpv4String() const; int GetPort() const { return port; } std::string ToString() const; private: - in_addr ipv4; + uint32_t ipv4; int port; }; @@ -80,7 +79,7 @@ class PalKey { class PalInfo { public: PalInfo(const std::string& ipv4, uint16_t port); - PalInfo(in_addr ipv4, uint16_t port); + PalInfo(uint32_t ipv4, uint16_t port); ~PalInfo(); PalKey GetKey() const { return PalKey(ipv4(), port_); } @@ -117,7 +116,7 @@ class PalInfo { } std::string toString() const; - in_addr ipv4() const { return ipv4_; } + uint32_t ipv4() const { return ipv4_; } uint16_t port() const { return port_; } char* segdes; ///< 所在网段描述 @@ -137,7 +136,7 @@ class PalInfo { PalInfo& setInBlacklistl(bool value); private: - in_addr ipv4_; ///< 好友IP + uint32_t ipv4_; ///< 好友IP uint16_t port_; ///< 好友端口 std::string icon_file_; ///< 好友头像 * std::string user; @@ -239,7 +238,7 @@ class NetSegment { NetSegment(); ~NetSegment(); - bool ContainIP(in_addr ipv4) const; + bool ContainIP(uint32_t ipv4) const; /** * @brief return the ip count in this segment * diff --git a/src/api/iptux-core/ProgramData.h b/src/api/iptux-core/ProgramData.h index de90e84a8..1ce116fa8 100644 --- a/src/api/iptux-core/ProgramData.h +++ b/src/api/iptux-core/ProgramData.h @@ -48,7 +48,7 @@ class ProgramData { FileInfo* GetShareFileInfo(uint32_t fileId); FileInfo* GetShareFileInfo(uint32_t packetn, uint32_t filenum); - std::string FindNetSegDescription(in_addr ipv4) const; + std::string FindNetSegDescription(uint32_t ipv4) const; void Lock(); void Unlock(); diff --git a/src/iptux-core/CoreThread.cpp b/src/iptux-core/CoreThread.cpp index 6a31946a7..eb0ef99b6 100644 --- a/src/iptux-core/CoreThread.cpp +++ b/src/iptux-core/CoreThread.cpp @@ -1,5 +1,6 @@ #include "config.h" #include "iptux-core/CoreThread.h" +#include "Const.h" #include #include @@ -110,8 +111,8 @@ struct CoreThread::Impl { CoreThread::CoreThread(shared_ptr data) : programData(data), config(data->getConfig()), - tcpSock(-1), - udpSock(-1), + tcpSock(0), + udpSock(0), started(false), pImpl(std::make_unique()) { if (config->GetBool("debug_dont_broadcast")) { @@ -158,7 +159,23 @@ void CoreThread::bind_iptux_port() { struct sockaddr_in addr; tcpSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); socket_enable_reuse(tcpSock); - udpSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + + GError* error = nullptr; + udpSock = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, + G_SOCKET_PROTOCOL_UDP, &error); + if (error) { + LOG_ERROR("create udp socket failed: %s", error->message); + g_clear_error(&error); + throw Exception(SOCKET_CREATE_FAILED); + } + + tcpSock = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, + G_SOCKET_PROTOCOL_TCP, &error); + if (error) { + LOG_ERROR("create tcp socket failed: %s", error->message); + g_clear_error(&error); + throw Exception(SOCKET_CREATE_FAILED); + } socket_enable_reuse(udpSock); socket_enable_broadcast(udpSock); if ((tcpSock == -1) || (udpSock == -1)) { @@ -210,7 +227,6 @@ void CoreThread::RecvUdpData(CoreThread* self) { struct sockaddr_in addr; socklen_t len; char buf[MAX_UDPLEN]; - ssize_t size; while (self->started) { struct pollfd pfd = {self->udpSock, POLLIN, 0}; @@ -224,9 +240,20 @@ void CoreThread::RecvUdpData(CoreThread* self) { } CHECK(ret == 1); len = sizeof(addr); - if ((size = recvfrom(self->udpSock, buf, MAX_UDPLEN, 0, - (struct sockaddr*)&addr, &len)) == -1) + + GError* error = nullptr; + gssize size = + g_socket_receive_from(self->udpSock, buf, MAX_UDPLEN, nullptr, &error); + if (size < 0) { + LOG_ERROR("recvfrom udp socket failed: %s", error->message); + g_clear_error(&error); continue; + } + + if (size == 0) { + continue; + } + if (size != MAX_UDPLEN) buf[size] = '\0'; auto port = ntohs(addr.sin_port); diff --git a/src/iptux-core/internal/Command.h b/src/iptux-core/internal/Command.h index 2af3482d6..c69d7bfd1 100644 --- a/src/iptux-core/internal/Command.h +++ b/src/iptux-core/internal/Command.h @@ -37,7 +37,7 @@ class Command { void SendAnsentry(int sock, CPPalInfo pal); void SendExit(int sock, CPPalInfo pal); void SendAbsence(int sock, CPPalInfo pal); - void SendDetectPacket(int sock, in_addr ipv4, uint16_t port); + void SendDetectPacket(int sock, uint32_t ipv4, uint16_t port); void SendMessage(int sock, CPPalInfo pal, const char* msg); void SendReply(int sock, CPPalInfo pal, uint32_t packetno); void SendReply(int sock, const PalKey& pal, uint32_t packetno); diff --git a/src/iptux-core/internal/UdpDataService.h b/src/iptux-core/internal/UdpDataService.h index ccc199466..d09995451 100644 --- a/src/iptux-core/internal/UdpDataService.h +++ b/src/iptux-core/internal/UdpDataService.h @@ -10,12 +10,12 @@ class UdpDataService { public: explicit UdpDataService(CoreThread& coreThread); - std::unique_ptr process(in_addr ipv4, + std::unique_ptr process(uint32_t ipv4, int port, const char buf[], size_t size); - std::unique_ptr process(in_addr ipv4, + std::unique_ptr process(uint32_t ipv4, int port, const char buf[], size_t size, diff --git a/src/iptux-core/internal/support.cpp b/src/iptux-core/internal/support.cpp index 18ae1ad1a..e71cd8728 100644 --- a/src/iptux-core/internal/support.cpp +++ b/src/iptux-core/internal/support.cpp @@ -26,40 +26,29 @@ using namespace std; namespace iptux { +static void socket_enable(GSocket* sock, const char* optname, int opt) { + GError* error = NULL; + if (!g_socket_set_option(sock, SOL_SOCKET, opt, TRUE, &error)) { + LOG_WARN("g_socket_set_option for %d, %s failed: %s", g_socket_get_fd(sock), + optname, error->message); + g_error_free(error); + } +} + /** * 让套接口支持广播. * @param sock socket */ -void socket_enable_broadcast(int sock) { - socklen_t len; - int optval; - - optval = 1; - len = sizeof(optval); - if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, len) != 0) { - LOG_WARN("setsockopt for SO_BROADCAST failed: %s", strerror(errno)); - } +void socket_enable_broadcast(GSocket* sock) { + socket_enable(sock, "SO_BROADCAST", SO_BROADCAST); } /** * 让套接口监听端口可重用. * @param sock socket */ -void socket_enable_reuse(int sock) { - socklen_t len; - int optval; - - optval = 1; - len = sizeof(optval); -#ifndef __CYGWIN__ - if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval, len) != 0) { - LOG_WARN("setsockopt for SO_REUSEPORT failed: %s", strerror(errno)); - } -#else - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, len) != 0) { - LOG_WARN("setsockopt for SO_REUSEADDR failed: %s", strerror(errno)); - } -#endif +void socket_enable_reuse(GSocket* sock) { + socket_enable(sock, "SO_REUSEPORT", SO_REUSEPORT); } /** @@ -69,7 +58,7 @@ void socket_enable_reuse(int sock) { * @note 链表数据不是指针而是实际的IP */ vector get_sys_broadcast_addr(int sock) { - const uint8_t amount = 5; //支持5个IP地址 + const uint8_t amount = 5; // 支持5个IP地址 uint8_t count, sum; struct ifconf ifc; struct ifreq* ifr; diff --git a/src/iptux-core/internal/support.h b/src/iptux-core/internal/support.h index 22f660260..91f218c85 100644 --- a/src/iptux-core/internal/support.h +++ b/src/iptux-core/internal/support.h @@ -12,14 +12,15 @@ #ifndef IPTUX_SUPPORT_H #define IPTUX_SUPPORT_H +#include #include #include namespace iptux { -void socket_enable_broadcast(int sock); -void socket_enable_reuse(int sock); -std::vector get_sys_broadcast_addr(int sock); +void socket_enable_broadcast(GSocket* sock); +void socket_enable_reuse(GSocket* sock); +std::vector get_sys_broadcast_addr(GSocket* sock); } // namespace iptux diff --git a/src/iptux-utils/utils.h b/src/iptux-utils/utils.h index 2cf70182b..798f5d151 100644 --- a/src/iptux-utils/utils.h +++ b/src/iptux-utils/utils.h @@ -12,9 +12,9 @@ #ifndef IPTUX_UTILS_H #define IPTUX_UTILS_H +#include #include #include -#include #include namespace iptux { @@ -71,13 +71,12 @@ char* ipmsg_get_filename_me(const char* pathname, char** path); char* iptux_erase_filename_suffix(const char* filename); char* ipmsg_get_pathname_full(const char* path, const char* name); -bool ipv4Equal(const in_addr& ip1, const in_addr& ip2); -int ipv4Compare(const in_addr& ip1, const in_addr& ip2); +bool ipv4Equal(uint32_t ip1, uint32_t ip2); +int ipv4Compare(uint32_t ip1, uint32_t ip2); -std::string inAddrToString(in_addr ipv4); -in_addr inAddrFromString(const std::string& s); -uint32_t inAddrToUint32(in_addr ipv4); -in_addr inAddrFromUint32(uint32_t value); +std::string inAddrToString(uint32_t ipv4); +uint32_t inAddrFromString(const std::string& s); +uint32_t inAddrToUint32(uint32_t ipv4); template std::string stringFormat(const char* format, ...) G_GNUC_PRINTF(1, 2);