mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 16:55:07 +02:00
pack destination socket for assignment as tightly as possible
This commit is contained in:
parent
e31ba3b75b
commit
97803b827e
5 changed files with 28 additions and 32 deletions
|
@ -99,7 +99,7 @@ int main(int argc, const char* argv[]) {
|
|||
// assignment server is on a public server
|
||||
// assume that the address we now have for the sender is the public address/port
|
||||
// and store that with the assignment so it can be given to the requestor later
|
||||
createdAssignment->setDomainSocket((sockaddr*) &senderSocket);
|
||||
createdAssignment->setDestinationSocket((sockaddr*) &senderSocket);
|
||||
|
||||
// add this assignment to the queue
|
||||
assignmentQueue.push_back(createdAssignment);
|
||||
|
|
|
@ -17,7 +17,7 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
|
|||
_direction(direction),
|
||||
_type(type),
|
||||
_pool(NULL),
|
||||
_domainSocket(NULL)
|
||||
_destinationSocket(NULL)
|
||||
{
|
||||
// set the create time on this assignment
|
||||
gettimeofday(&_time, NULL);
|
||||
|
@ -34,7 +34,7 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
|
|||
|
||||
Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
|
||||
_pool(NULL),
|
||||
_domainSocket(NULL)
|
||||
_destinationSocket(NULL)
|
||||
{
|
||||
// set the create time on this assignment
|
||||
gettimeofday(&_time, NULL);
|
||||
|
@ -64,41 +64,39 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
|
|||
}
|
||||
|
||||
if (numBytes > numBytesRead) {
|
||||
|
||||
if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
|
||||
// IPv4 address
|
||||
sockaddr_in destinationSocket = {};
|
||||
memcpy(&destinationSocket, dataBuffer + numBytesRead, sizeof(sockaddr_in));
|
||||
destinationSocket.sin_family = AF_INET;
|
||||
setDomainSocket((sockaddr*) &destinationSocket);
|
||||
delete _destinationSocket;
|
||||
_destinationSocket = (sockaddr*) new sockaddr_in;
|
||||
unpackSocket(dataBuffer + numBytesRead, _destinationSocket);
|
||||
} else {
|
||||
// IPv6 address
|
||||
sockaddr_in6 destinationSocket = {};
|
||||
memcpy(&destinationSocket, dataBuffer + numBytesRead, sizeof(sockaddr_in6));
|
||||
setDomainSocket((sockaddr*) &destinationSocket);
|
||||
// IPv6 address, or bad designator
|
||||
qDebug("Received a socket that cannot be unpacked!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assignment::~Assignment() {
|
||||
delete _domainSocket;
|
||||
delete _destinationSocket;
|
||||
delete _pool;
|
||||
}
|
||||
|
||||
void Assignment::setDomainSocket(const sockaddr* domainSocket) {
|
||||
void Assignment::setDestinationSocket(const sockaddr* destinationSocket) {
|
||||
|
||||
if (_domainSocket) {
|
||||
if (_destinationSocket) {
|
||||
// delete the old _domainSocket if it exists
|
||||
delete _domainSocket;
|
||||
_domainSocket = NULL;
|
||||
delete _destinationSocket;
|
||||
_destinationSocket = NULL;
|
||||
}
|
||||
|
||||
// create a new sockaddr or sockaddr_in depending on what type of address this is
|
||||
if (domainSocket->sa_family == AF_INET) {
|
||||
_domainSocket = (sockaddr*) new sockaddr_in;
|
||||
memcpy(_domainSocket, domainSocket, sizeof(sockaddr_in));
|
||||
if (destinationSocket->sa_family == AF_INET) {
|
||||
_destinationSocket = (sockaddr*) new sockaddr_in;
|
||||
memcpy(_destinationSocket, destinationSocket, sizeof(sockaddr_in));
|
||||
} else {
|
||||
_domainSocket = (sockaddr*) new sockaddr_in6;
|
||||
memcpy(_domainSocket, domainSocket, sizeof(sockaddr_in6));
|
||||
_destinationSocket = (sockaddr*) new sockaddr_in6;
|
||||
memcpy(_destinationSocket, destinationSocket, sizeof(sockaddr_in6));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,13 +116,11 @@ int Assignment::packToBuffer(unsigned char* buffer) {
|
|||
numPackedBytes += sizeof(char);
|
||||
}
|
||||
|
||||
if (_domainSocket) {
|
||||
buffer[numPackedBytes++] = (_domainSocket->sa_family == AF_INET) ? IPv4_ADDRESS_DESIGNATOR : IPv6_ADDRESS_DESIGNATOR;
|
||||
if (_destinationSocket) {
|
||||
buffer[numPackedBytes++] = (_destinationSocket->sa_family == AF_INET)
|
||||
? IPv4_ADDRESS_DESIGNATOR : IPv6_ADDRESS_DESIGNATOR;
|
||||
|
||||
int numSocketBytes = (_domainSocket->sa_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
|
||||
|
||||
memcpy(buffer + numPackedBytes, _domainSocket, numSocketBytes);
|
||||
numPackedBytes += numSocketBytes;
|
||||
numPackedBytes += packSocket(buffer + numPackedBytes, _destinationSocket);
|
||||
}
|
||||
|
||||
return numPackedBytes;
|
||||
|
|
|
@ -43,8 +43,8 @@ public:
|
|||
const char* getPool() const { return _pool; }
|
||||
const timeval& getTime() const { return _time; }
|
||||
|
||||
const sockaddr* getDomainSocket() { return _domainSocket; }
|
||||
void setDomainSocket(const sockaddr* domainSocket);
|
||||
const sockaddr* getDestinationSocket() { return _destinationSocket; }
|
||||
void setDestinationSocket(const sockaddr* destinationSocket);
|
||||
|
||||
/// Packs the assignment to the passed buffer
|
||||
/// \param buffer the buffer in which to pack the assignment
|
||||
|
@ -58,7 +58,7 @@ private:
|
|||
Assignment::Direction _direction; /// the direction of the assignment (Create, Deploy, Request)
|
||||
Assignment::Type _type; /// the type of the assignment, defines what the assignee will do
|
||||
char* _pool; /// the pool this assignment is for/from
|
||||
sockaddr* _domainSocket; /// pointer to socket for domain server that created assignment
|
||||
sockaddr* _destinationSocket; /// pointer to destination socket for assignment
|
||||
timeval _time; /// time the assignment was created (set in constructor)
|
||||
};
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ int packSocket(unsigned char* packStore, sockaddr* socketToPack) {
|
|||
return packSocket(packStore, ((sockaddr_in*) socketToPack)->sin_addr.s_addr, ((sockaddr_in*) socketToPack)->sin_port);
|
||||
}
|
||||
|
||||
int unpackSocket(unsigned char* packedData, sockaddr* unpackDestSocket) {
|
||||
int unpackSocket(const unsigned char* packedData, sockaddr* unpackDestSocket) {
|
||||
sockaddr_in* destinationSocket = (sockaddr_in*) unpackDestSocket;
|
||||
destinationSocket->sin_addr.s_addr = (packedData[0] << 24) + (packedData[1] << 16) + (packedData[2] << 8) + packedData[3];
|
||||
destinationSocket->sin_port = (packedData[4] << 8) + packedData[5];
|
||||
|
|
|
@ -44,7 +44,7 @@ private:
|
|||
bool socketMatch(const sockaddr* first, const sockaddr* second);
|
||||
int packSocket(unsigned char* packStore, in_addr_t inAddress, in_port_t networkOrderPort);
|
||||
int packSocket(unsigned char* packStore, sockaddr* socketToPack);
|
||||
int unpackSocket(unsigned char* packedData, sockaddr* unpackDestSocket);
|
||||
int unpackSocket(const unsigned char* packedData, sockaddr* unpackDestSocket);
|
||||
int getLocalAddress();
|
||||
unsigned short loadBufferWithSocketInfo(char* addressBuffer, sockaddr* socket);
|
||||
sockaddr_in socketForHostnameAndHostOrderPort(const char* hostname, unsigned short port = 0);
|
||||
|
|
Loading…
Reference in a new issue