2017-04-17 20:57:07 +02:00
|
|
|
//
|
2017-06-06 03:44:16 +02:00
|
|
|
// libtgvoip is free and unencumbered public domain software.
|
|
|
|
// For more information, see http://unlicense.org or the UNLICENSE file
|
|
|
|
// you should have received with this source code distribution.
|
2017-04-17 20:57:07 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBTGVOIP_NETWORKSOCKETPOSIX_H
|
|
|
|
#define LIBTGVOIP_NETWORKSOCKETPOSIX_H
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
#include "../../controller/net/NetworkSocket.h"
|
|
|
|
#include "../../tools/Buffers.h"
|
2017-06-06 03:44:16 +02:00
|
|
|
#include <vector>
|
2020-03-10 20:31:58 +01:00
|
|
|
#include <mutex>
|
2017-06-06 03:44:16 +02:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <pthread.h>
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2020-01-24 20:26:34 +01:00
|
|
|
namespace tgvoip
|
|
|
|
{
|
|
|
|
|
|
|
|
class SocketSelectCancellerPosix : public SocketSelectCanceller
|
|
|
|
{
|
|
|
|
friend class NetworkSocketPosix;
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
public:
|
|
|
|
SocketSelectCancellerPosix();
|
|
|
|
virtual ~SocketSelectCancellerPosix();
|
|
|
|
virtual void CancelSelect();
|
2020-01-24 20:26:34 +01:00
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
private:
|
|
|
|
int pipeRead;
|
|
|
|
int pipeWrite;
|
2017-06-06 03:44:16 +02:00
|
|
|
};
|
|
|
|
|
2020-01-24 20:26:34 +01:00
|
|
|
class NetworkSocketPosix : public NetworkSocket
|
|
|
|
{
|
2017-04-17 20:57:07 +02:00
|
|
|
public:
|
2017-07-03 03:42:49 +02:00
|
|
|
NetworkSocketPosix(NetworkProtocol protocol);
|
2020-03-10 20:31:58 +01:00
|
|
|
virtual ~NetworkSocketPosix() override;
|
2020-03-24 12:15:04 +01:00
|
|
|
virtual void Send(NetworkPacket &&packet) override;
|
2019-04-15 01:43:10 +02:00
|
|
|
virtual NetworkPacket Receive(size_t maxLen) override;
|
2018-11-09 16:44:01 +01:00
|
|
|
virtual void Open() override;
|
|
|
|
virtual void Close() override;
|
2019-04-15 01:43:10 +02:00
|
|
|
virtual void Connect(const NetworkAddress address, uint16_t port) override;
|
2020-01-24 20:26:34 +01:00
|
|
|
virtual std::string GetLocalInterfaceInfo(NetworkAddress *v4addr, NetworkAddress *v6addr) override;
|
2018-11-09 16:44:01 +01:00
|
|
|
virtual void OnActiveInterfaceChanged() override;
|
|
|
|
virtual uint16_t GetLocalPort() override;
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
static std::string V4AddressToString(uint32_t address);
|
2018-11-09 16:44:01 +01:00
|
|
|
static std::string V6AddressToString(const unsigned char address[16]);
|
2017-04-17 20:57:07 +02:00
|
|
|
static uint32_t StringToV4Address(std::string address);
|
2020-01-24 20:26:34 +01:00
|
|
|
static void StringToV6Address(std::string address, unsigned char *out);
|
2019-04-15 01:43:10 +02:00
|
|
|
static NetworkAddress ResolveDomainName(std::string name);
|
2020-01-24 20:26:34 +01:00
|
|
|
static bool Select(std::vector<std::shared_ptr<NetworkSocket>> &readFds, std::vector<std::shared_ptr<NetworkSocket>> &writeFds, std::vector<std::shared_ptr<NetworkSocket>> &errorFds, const std::unique_ptr<SocketSelectCanceller> &canceller);
|
2017-07-03 03:42:49 +02:00
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
virtual NetworkAddress GetConnectedAddress() override;
|
2017-07-03 03:42:49 +02:00
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
virtual uint16_t GetConnectedPort() override;
|
2017-07-03 03:42:49 +02:00
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
virtual void SetTimeouts(int sendTimeout, int recvTimeout) override;
|
|
|
|
virtual bool OnReadyToSend() override;
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
protected:
|
2018-11-09 16:44:01 +01:00
|
|
|
virtual void SetMaxPriority() override;
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
private:
|
2020-01-24 20:26:34 +01:00
|
|
|
static int GetDescriptorFromSocket(NetworkSocket *socket);
|
|
|
|
static int GetDescriptorFromSocket(const std::shared_ptr<NetworkSocket> &socket);
|
2019-04-15 01:43:10 +02:00
|
|
|
std::atomic<int> fd;
|
2020-03-10 20:31:58 +01:00
|
|
|
std::mutex m_fd;
|
2017-04-17 20:57:07 +02:00
|
|
|
bool needUpdateNat64Prefix;
|
|
|
|
bool nat64Present;
|
|
|
|
double switchToV6at;
|
2020-03-10 20:31:58 +01:00
|
|
|
std::atomic<bool> isV4Available;
|
2019-04-15 01:43:10 +02:00
|
|
|
std::atomic<bool> closing;
|
2020-01-24 20:26:34 +01:00
|
|
|
NetworkAddress tcpConnectedAddress = NetworkAddress::Empty();
|
2017-07-03 03:42:49 +02:00
|
|
|
uint16_t tcpConnectedPort;
|
2020-01-24 20:26:34 +01:00
|
|
|
NetworkPacket pendingOutgoingPacket = NetworkPacket::Empty();
|
|
|
|
Buffer recvBuffer = Buffer(2048);
|
2017-04-17 20:57:07 +02:00
|
|
|
};
|
|
|
|
|
2020-01-24 20:26:34 +01:00
|
|
|
} // namespace tgvoip
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
#endif //LIBTGVOIP_NETWORKSOCKETPOSIX_H
|