2017-04-17 20:57:07 +02:00
|
|
|
//
|
|
|
|
// Created by Grishka on 29.03.17.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "NetworkSocket.h"
|
|
|
|
#include <stdexcept>
|
2017-06-06 03:44:16 +02:00
|
|
|
#include <algorithm>
|
2017-04-17 20:57:07 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include "os/windows/NetworkSocketWinsock.h"
|
|
|
|
#else
|
|
|
|
#include "os/posix/NetworkSocketPosix.h"
|
|
|
|
#endif
|
|
|
|
#include "logging.h"
|
|
|
|
#include "VoIPServerConfig.h"
|
2017-06-06 03:44:16 +02:00
|
|
|
#include "VoIPController.h"
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
#define MIN_UDP_PORT 16384
|
|
|
|
#define MAX_UDP_PORT 32768
|
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
using namespace tgvoip;
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
NetworkSocket::NetworkSocket(){
|
|
|
|
ipv6Timeout=ServerConfig::GetSharedInstance()->GetDouble("nat64_fallback_timeout", 3);
|
|
|
|
failed=false;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
NetworkSocket::~NetworkSocket(){
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
std::string NetworkSocket::GetLocalInterfaceInfo(IPv4Address *inet4addr, IPv6Address *inet6addr){
|
|
|
|
std::string r="not implemented";
|
|
|
|
return r;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
uint16_t NetworkSocket::GenerateLocalPort(){
|
|
|
|
return (uint16_t) ((rand()%(MAX_UDP_PORT-MIN_UDP_PORT))+MIN_UDP_PORT);
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
void NetworkSocket::SetMaxPriority(){
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
bool NetworkSocket::IsFailed(){
|
|
|
|
return failed;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
NetworkSocket *NetworkSocket::Create(){
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2017-06-06 03:44:16 +02:00
|
|
|
return new NetworkSocketPosix();
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2017-06-06 03:44:16 +02:00
|
|
|
return new NetworkSocketWinsock();
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
void NetworkSocket::GenerateTCPO2States(unsigned char* buffer, TCPO2State* recvState, TCPO2State* sendState){
|
|
|
|
memset(recvState, 0, sizeof(TCPO2State));
|
|
|
|
memset(sendState, 0, sizeof(TCPO2State));
|
|
|
|
unsigned char nonce[64];
|
|
|
|
uint32_t *first = reinterpret_cast<uint32_t*>(nonce), *second = first + 1;
|
|
|
|
uint32_t first1 = 0x44414548U, first2 = 0x54534f50U, first3 = 0x20544547U, first4 = 0x20544547U, first5 = 0xeeeeeeeeU;
|
|
|
|
uint32_t second1 = 0;
|
|
|
|
do {
|
|
|
|
VoIPController::crypto.rand_bytes(nonce, sizeof(nonce));
|
|
|
|
} while (*first == first1 || *first == first2 || *first == first3 || *first == first4 || *first == first5 || *second == second1 || *reinterpret_cast<unsigned char*>(nonce) == 0xef);
|
|
|
|
|
|
|
|
// prepare encryption key/iv
|
|
|
|
memcpy(sendState->key, nonce + 8, 32);
|
|
|
|
memcpy(sendState->iv, nonce + 8 + 32, 16);
|
|
|
|
|
|
|
|
// prepare decryption key/iv
|
|
|
|
char reversed[48];
|
|
|
|
memcpy(reversed, nonce + 8, sizeof(reversed));
|
|
|
|
std::reverse(reversed, reversed + sizeof(reversed));
|
|
|
|
memcpy(recvState->key, reversed, 32);
|
|
|
|
memcpy(recvState->iv, reversed + 32, 16);
|
|
|
|
|
|
|
|
// write protocol identifier
|
|
|
|
*reinterpret_cast<uint32_t*>(nonce + 56) = 0xefefefefU;
|
|
|
|
memcpy(buffer, nonce, 56);
|
|
|
|
EncryptForTCPO2(nonce, sizeof(nonce), sendState);
|
|
|
|
memcpy(buffer+56, nonce+56, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkSocket::EncryptForTCPO2(unsigned char *buffer, size_t len, TCPO2State *state){
|
|
|
|
VoIPController::crypto.aes_ctr_encrypt(buffer, len, state->key, state->iv, state->ecount, &state->num);
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
bool NetworkAddress::operator==(const NetworkAddress &other){
|
|
|
|
IPv4Address* self4=dynamic_cast<IPv4Address*>(this);
|
|
|
|
IPv4Address* other4=dynamic_cast<IPv4Address*>((NetworkAddress*)&other);
|
|
|
|
if(self4 && other4){
|
|
|
|
return self4->GetAddress()==other4->GetAddress();
|
2017-04-17 20:57:07 +02:00
|
|
|
}
|
2017-06-06 03:44:16 +02:00
|
|
|
IPv6Address* self6=dynamic_cast<IPv6Address*>(this);
|
|
|
|
IPv6Address* other6=dynamic_cast<IPv6Address*>((NetworkAddress*)&other);
|
|
|
|
if(self6 && other6){
|
|
|
|
return memcmp(self6->GetAddress(), other6->GetAddress(), 16)==0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
bool NetworkAddress::operator!=(const NetworkAddress &other){
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
IPv4Address::IPv4Address(std::string addr){
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2017-06-06 03:44:16 +02:00
|
|
|
this->address=NetworkSocketPosix::StringToV4Address(addr);
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2017-06-06 03:44:16 +02:00
|
|
|
this->address=NetworkSocketWinsock::StringToV4Address(addr);
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
IPv4Address::IPv4Address(uint32_t addr){
|
|
|
|
this->address=addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Address::IPv4Address(){
|
|
|
|
this->address=0;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
std::string IPv4Address::ToString(){
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2017-06-06 03:44:16 +02:00
|
|
|
return NetworkSocketPosix::V4AddressToString(address);
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2017-06-06 03:44:16 +02:00
|
|
|
return NetworkSocketWinsock::V4AddressToString(address);
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
/*sockaddr &IPv4Address::ToSockAddr(uint16_t port){
|
|
|
|
sockaddr_in sa;
|
|
|
|
sa.sin_family=AF_INET;
|
|
|
|
sa.sin_addr=addr;
|
|
|
|
sa.sin_port=port;
|
|
|
|
return *((sockaddr *) &sa);
|
|
|
|
}*/
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
uint32_t IPv4Address::GetAddress(){
|
|
|
|
return address;
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
IPv6Address::IPv6Address(std::string addr){
|
2017-04-17 20:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2017-06-06 03:44:16 +02:00
|
|
|
NetworkSocketPosix::StringToV6Address(addr, this->address);
|
2017-04-17 20:57:07 +02:00
|
|
|
#else
|
2017-06-06 03:44:16 +02:00
|
|
|
NetworkSocketWinsock::StringToV6Address(addr, this->address);
|
2017-04-17 20:57:07 +02:00
|
|
|
#endif
|
2017-06-06 03:44:16 +02:00
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
IPv6Address::IPv6Address(uint8_t addr[16]){
|
|
|
|
memcpy(address, addr, 16);
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
IPv6Address::IPv6Address(){
|
|
|
|
memset(address, 0, 16);
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
std::string IPv6Address::ToString(){
|
|
|
|
return "";
|
|
|
|
}
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
/*sockaddr &IPv6Address::ToSockAddr(uint16_t port){
|
|
|
|
sockaddr_in6 sa;
|
|
|
|
sa.sin6_family=AF_INET6;
|
|
|
|
sa.sin6_addr=addr;
|
|
|
|
sa.sin6_port=port;
|
|
|
|
return *((sockaddr *) &sa);
|
|
|
|
}*/
|
2017-04-17 20:57:07 +02:00
|
|
|
|
2017-06-06 03:44:16 +02:00
|
|
|
const uint8_t *IPv6Address::GetAddress(){
|
|
|
|
return address;
|
2017-04-17 20:57:07 +02:00
|
|
|
}
|