Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#69: build for windows #597

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
22 changes: 11 additions & 11 deletions src/api/iptux-core/CoreThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <atomic>
#include <cstdint>
#include <memory>
#include <netinet/in.h>
#include <vector>

#include <gio/gio.h>
#include <sigc++/signal.h>

#include "iptux-core/Event.h"
Expand All @@ -32,14 +32,14 @@ class CoreThread {
uint16_t port() const;

std::shared_ptr<ProgramData> 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?
Expand All @@ -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;
Expand All @@ -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()));
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -183,8 +183,8 @@ class CoreThread {
protected:
std::shared_ptr<ProgramData> programData;
std::shared_ptr<IptuxConfig> config;
int tcpSock;
int udpSock;
GSocket* tcpSock;
GSocket* udpSock;
mutable std::mutex mutex; // 锁

private:
Expand Down
17 changes: 8 additions & 9 deletions src/api/iptux-core/Models.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
#ifndef IPTUX_MODELS_H
#define IPTUX_MODELS_H

#include <arpa/inet.h>
#include <cstdint>
#include <memory>
#include <netinet/in.h>
#include <string>

#include <json/json.h>
Expand Down Expand Up @@ -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;
};

Expand All @@ -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_); }
Expand Down Expand Up @@ -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; ///< 所在网段描述
Expand All @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion src/api/iptux-core/ProgramData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
39 changes: 33 additions & 6 deletions src/iptux-core/CoreThread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "config.h"
#include "iptux-core/CoreThread.h"
#include "Const.h"

#include <deque>
#include <fstream>
Expand Down Expand Up @@ -110,8 +111,8 @@ struct CoreThread::Impl {
CoreThread::CoreThread(shared_ptr<ProgramData> data)
: programData(data),
config(data->getConfig()),
tcpSock(-1),
udpSock(-1),
tcpSock(0),
udpSock(0),
started(false),
pImpl(std::make_unique<Impl>()) {
if (config->GetBool("debug_dont_broadcast")) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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};
Expand All @@ -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;
Comment on lines +253 to +254
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Handling zero-size UDP packets

The check for size == 0 is added, but it might be useful to log this event for debugging purposes, as receiving a zero-size packet could indicate an issue.

Suggested change
if (size == 0) {
continue;
if (size == 0) {
std::cerr << "Warning: Received a packet with size 0." << std::endl;
continue;

}

if (size != MAX_UDPLEN)
buf[size] = '\0';
auto port = ntohs(addr.sin_port);
Expand Down
2 changes: 1 addition & 1 deletion src/iptux-core/internal/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/iptux-core/internal/UdpDataService.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class UdpDataService {
public:
explicit UdpDataService(CoreThread& coreThread);

std::unique_ptr<UdpData> process(in_addr ipv4,
std::unique_ptr<UdpData> process(uint32_t ipv4,
int port,
const char buf[],
size_t size);

std::unique_ptr<UdpData> process(in_addr ipv4,
std::unique_ptr<UdpData> process(uint32_t ipv4,
int port,
const char buf[],
size_t size,
Expand Down
13 changes: 6 additions & 7 deletions src/iptux-utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#ifndef IPTUX_UTILS_H
#define IPTUX_UTILS_H

#include <cstdint>
#include <glib.h>
#include <memory>
#include <netinet/in.h>
#include <string>

namespace iptux {
Expand Down Expand Up @@ -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 <typename... Args>
std::string stringFormat(const char* format, ...) G_GNUC_PRINTF(1, 2);
Expand Down
Loading