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
|
|
|
|
|
|
|
|
#include "../../NetworkSocket.h"
|
2018-11-09 16:44:01 +01:00
|
|
|
#include "../../Buffers.h"
|
2017-06-06 03:44:16 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <pthread.h>
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
namespace tgvoip {
|
|
|
|
|
2017-07-03 03:42:49 +02:00
|
|
|
class SocketSelectCancellerPosix : public SocketSelectCanceller{
|
|
|
|
friend class NetworkSocketPosix;
|
|
|
|
public:
|
|
|
|
SocketSelectCancellerPosix();
|
|
|
|
virtual ~SocketSelectCancellerPosix();
|
|
|
|
virtual void CancelSelect();
|
|
|
|
private:
|
|
|
|
int pipeRead;
|
|
|
|
int pipeWrite;
|
2017-06-06 03:44:16 +02:00
|
|
|
};
|
|
|
|
|
2017-04-17 20:57:07 +02:00
|
|
|
class NetworkSocketPosix : public NetworkSocket{
|
|
|
|
public:
|
2017-07-03 03:42:49 +02:00
|
|
|
NetworkSocketPosix(NetworkProtocol protocol);
|
2017-04-17 20:57:07 +02:00
|
|
|
virtual ~NetworkSocketPosix();
|
2019-04-15 01:43:10 +02:00
|
|
|
virtual void Send(NetworkPacket packet) override;
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
static void StringToV6Address(std::string address, unsigned char* out);
|
2019-04-15 01:43:10 +02:00
|
|
|
static NetworkAddress ResolveDomainName(std::string name);
|
2018-11-09 16:44:01 +01:00
|
|
|
static bool Select(std::vector<NetworkSocket*>& readFds, std::vector<NetworkSocket*>& writeFds, std::vector<NetworkSocket*>& errorFds, 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:
|
2017-07-03 03:42:49 +02:00
|
|
|
static int GetDescriptorFromSocket(NetworkSocket* socket);
|
2019-04-15 01:43:10 +02:00
|
|
|
std::atomic<int> fd;
|
2017-04-17 20:57:07 +02:00
|
|
|
bool needUpdateNat64Prefix;
|
|
|
|
bool nat64Present;
|
|
|
|
double switchToV6at;
|
|
|
|
bool isV4Available;
|
2019-04-15 01:43:10 +02:00
|
|
|
std::atomic<bool> closing;
|
|
|
|
NetworkAddress tcpConnectedAddress=NetworkAddress::Empty();
|
2017-07-03 03:42:49 +02:00
|
|
|
uint16_t tcpConnectedPort;
|
2019-04-15 01:43:10 +02:00
|
|
|
NetworkPacket pendingOutgoingPacket=NetworkPacket::Empty();
|
|
|
|
Buffer recvBuffer=Buffer(2048);
|
2017-04-17 20:57:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //LIBTGVOIP_NETWORKSOCKETPOSIX_H
|