pack destination socket for assignment as tightly as possible

This commit is contained in:
Stephen Birarda 2013-09-09 16:45:44 -07:00
parent e31ba3b75b
commit 97803b827e
5 changed files with 28 additions and 32 deletions

View file

@ -99,7 +99,7 @@ int main(int argc, const char* argv[]) {
// assignment server is on a public server // assignment server is on a public server
// assume that the address we now have for the sender is the public address/port // 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 // 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 // add this assignment to the queue
assignmentQueue.push_back(createdAssignment); assignmentQueue.push_back(createdAssignment);

View file

@ -17,7 +17,7 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
_direction(direction), _direction(direction),
_type(type), _type(type),
_pool(NULL), _pool(NULL),
_domainSocket(NULL) _destinationSocket(NULL)
{ {
// set the create time on this assignment // set the create time on this assignment
gettimeofday(&_time, NULL); gettimeofday(&_time, NULL);
@ -34,7 +34,7 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) : Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
_pool(NULL), _pool(NULL),
_domainSocket(NULL) _destinationSocket(NULL)
{ {
// set the create time on this assignment // set the create time on this assignment
gettimeofday(&_time, NULL); gettimeofday(&_time, NULL);
@ -64,41 +64,39 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
} }
if (numBytes > numBytesRead) { if (numBytes > numBytesRead) {
if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) { if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
// IPv4 address // IPv4 address
sockaddr_in destinationSocket = {}; delete _destinationSocket;
memcpy(&destinationSocket, dataBuffer + numBytesRead, sizeof(sockaddr_in)); _destinationSocket = (sockaddr*) new sockaddr_in;
destinationSocket.sin_family = AF_INET; unpackSocket(dataBuffer + numBytesRead, _destinationSocket);
setDomainSocket((sockaddr*) &destinationSocket);
} else { } else {
// IPv6 address // IPv6 address, or bad designator
sockaddr_in6 destinationSocket = {}; qDebug("Received a socket that cannot be unpacked!\n");
memcpy(&destinationSocket, dataBuffer + numBytesRead, sizeof(sockaddr_in6));
setDomainSocket((sockaddr*) &destinationSocket);
} }
} }
} }
Assignment::~Assignment() { Assignment::~Assignment() {
delete _domainSocket; delete _destinationSocket;
delete _pool; 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 the old _domainSocket if it exists
delete _domainSocket; delete _destinationSocket;
_domainSocket = NULL; _destinationSocket = NULL;
} }
// create a new sockaddr or sockaddr_in depending on what type of address this is // create a new sockaddr or sockaddr_in depending on what type of address this is
if (domainSocket->sa_family == AF_INET) { if (destinationSocket->sa_family == AF_INET) {
_domainSocket = (sockaddr*) new sockaddr_in; _destinationSocket = (sockaddr*) new sockaddr_in;
memcpy(_domainSocket, domainSocket, sizeof(sockaddr_in)); memcpy(_destinationSocket, destinationSocket, sizeof(sockaddr_in));
} else { } else {
_domainSocket = (sockaddr*) new sockaddr_in6; _destinationSocket = (sockaddr*) new sockaddr_in6;
memcpy(_domainSocket, domainSocket, sizeof(sockaddr_in6)); memcpy(_destinationSocket, destinationSocket, sizeof(sockaddr_in6));
} }
} }
@ -118,13 +116,11 @@ int Assignment::packToBuffer(unsigned char* buffer) {
numPackedBytes += sizeof(char); numPackedBytes += sizeof(char);
} }
if (_domainSocket) { if (_destinationSocket) {
buffer[numPackedBytes++] = (_domainSocket->sa_family == AF_INET) ? IPv4_ADDRESS_DESIGNATOR : IPv6_ADDRESS_DESIGNATOR; 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); numPackedBytes += packSocket(buffer + numPackedBytes, _destinationSocket);
memcpy(buffer + numPackedBytes, _domainSocket, numSocketBytes);
numPackedBytes += numSocketBytes;
} }
return numPackedBytes; return numPackedBytes;

View file

@ -43,8 +43,8 @@ public:
const char* getPool() const { return _pool; } const char* getPool() const { return _pool; }
const timeval& getTime() const { return _time; } const timeval& getTime() const { return _time; }
const sockaddr* getDomainSocket() { return _domainSocket; } const sockaddr* getDestinationSocket() { return _destinationSocket; }
void setDomainSocket(const sockaddr* domainSocket); void setDestinationSocket(const sockaddr* destinationSocket);
/// Packs the assignment to the passed buffer /// Packs the assignment to the passed buffer
/// \param buffer the buffer in which to pack the assignment /// \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::Direction _direction; /// the direction of the assignment (Create, Deploy, Request)
Assignment::Type _type; /// the type of the assignment, defines what the assignee will do Assignment::Type _type; /// the type of the assignment, defines what the assignee will do
char* _pool; /// the pool this assignment is for/from 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) timeval _time; /// time the assignment was created (set in constructor)
}; };

View file

@ -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); 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; sockaddr_in* destinationSocket = (sockaddr_in*) unpackDestSocket;
destinationSocket->sin_addr.s_addr = (packedData[0] << 24) + (packedData[1] << 16) + (packedData[2] << 8) + packedData[3]; destinationSocket->sin_addr.s_addr = (packedData[0] << 24) + (packedData[1] << 16) + (packedData[2] << 8) + packedData[3];
destinationSocket->sin_port = (packedData[4] << 8) + packedData[5]; destinationSocket->sin_port = (packedData[4] << 8) + packedData[5];

View file

@ -44,7 +44,7 @@ private:
bool socketMatch(const sockaddr* first, const sockaddr* second); 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, in_addr_t inAddress, in_port_t networkOrderPort);
int packSocket(unsigned char* packStore, sockaddr* socketToPack); int packSocket(unsigned char* packStore, sockaddr* socketToPack);
int unpackSocket(unsigned char* packedData, sockaddr* unpackDestSocket); int unpackSocket(const unsigned char* packedData, sockaddr* unpackDestSocket);
int getLocalAddress(); int getLocalAddress();
unsigned short loadBufferWithSocketInfo(char* addressBuffer, sockaddr* socket); unsigned short loadBufferWithSocketInfo(char* addressBuffer, sockaddr* socket);
sockaddr_in socketForHostnameAndHostOrderPort(const char* hostname, unsigned short port = 0); sockaddr_in socketForHostnameAndHostOrderPort(const char* hostname, unsigned short port = 0);