mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 11:37:58 +02:00
store an attached local and public socket with assignment
This commit is contained in:
parent
93ca5278da
commit
e53807ca82
6 changed files with 58 additions and 29 deletions
|
@ -70,8 +70,8 @@ void childClient() {
|
||||||
qDebug() << "Received an assignment -" << deployedAssignment << "\n";
|
qDebug() << "Received an assignment -" << deployedAssignment << "\n";
|
||||||
|
|
||||||
// switch our nodelist DOMAIN_IP to the ip receieved in the assignment
|
// switch our nodelist DOMAIN_IP to the ip receieved in the assignment
|
||||||
if (deployedAssignment.getDestinationSocket()->sa_family == AF_INET) {
|
if (deployedAssignment.getAttachedPublicSocket()->sa_family == AF_INET) {
|
||||||
in_addr domainSocketAddr = ((sockaddr_in*) deployedAssignment.getDestinationSocket())->sin_addr;
|
in_addr domainSocketAddr = ((sockaddr_in*) deployedAssignment.getAttachedPublicSocket())->sin_addr;
|
||||||
nodeList->setDomainIP(inet_ntoa(domainSocketAddr));
|
nodeList->setDomainIP(inet_ntoa(domainSocketAddr));
|
||||||
|
|
||||||
qDebug("Destination IP for assignment is %s\n", inet_ntoa(domainSocketAddr));
|
qDebug("Destination IP for assignment is %s\n", inet_ntoa(domainSocketAddr));
|
||||||
|
|
|
@ -96,10 +96,10 @@ int main(int argc, const char* argv[]) {
|
||||||
qDebug() << "Received a created assignment:" << *createdAssignment;
|
qDebug() << "Received a created assignment:" << *createdAssignment;
|
||||||
qDebug() << "Current queue size is" << assignmentQueue.size();
|
qDebug() << "Current queue size is" << assignmentQueue.size();
|
||||||
|
|
||||||
// assignment server is on a public server
|
// assignment server is likely 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 if necessary
|
||||||
createdAssignment->setDestinationSocket((sockaddr*) &senderSocket);
|
createdAssignment->setAttachedPublicSocket((sockaddr*) &senderSocket);
|
||||||
|
|
||||||
// add this assignment to the queue
|
// add this assignment to the queue
|
||||||
assignmentQueue.push_back(createdAssignment);
|
assignmentQueue.push_back(createdAssignment);
|
||||||
|
|
|
@ -17,7 +17,8 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
|
||||||
_direction(direction),
|
_direction(direction),
|
||||||
_type(type),
|
_type(type),
|
||||||
_pool(NULL),
|
_pool(NULL),
|
||||||
_destinationSocket(NULL)
|
_attachedPublicSocket(NULL),
|
||||||
|
_attachedLocalSocket(NULL)
|
||||||
{
|
{
|
||||||
// set the create time on this assignment
|
// set the create time on this assignment
|
||||||
gettimeofday(&_time, NULL);
|
gettimeofday(&_time, NULL);
|
||||||
|
@ -34,7 +35,8 @@ 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),
|
||||||
_destinationSocket(NULL)
|
_attachedPublicSocket(NULL),
|
||||||
|
_attachedLocalSocket(NULL)
|
||||||
{
|
{
|
||||||
// set the create time on this assignment
|
// set the create time on this assignment
|
||||||
gettimeofday(&_time, NULL);
|
gettimeofday(&_time, NULL);
|
||||||
|
@ -65,11 +67,15 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
|
||||||
|
|
||||||
if (numBytes > numBytesRead) {
|
if (numBytes > numBytesRead) {
|
||||||
|
|
||||||
|
sockaddr* socketDestination = (_direction == Assignment::Create)
|
||||||
|
? _attachedLocalSocket
|
||||||
|
: _attachedPublicSocket;
|
||||||
|
|
||||||
if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
|
if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
|
||||||
// IPv4 address
|
// IPv4 address
|
||||||
delete _destinationSocket;
|
delete socketDestination;
|
||||||
_destinationSocket = (sockaddr*) new sockaddr_in;
|
socketDestination = (sockaddr*) new sockaddr_in;
|
||||||
unpackSocket(dataBuffer + numBytesRead, _destinationSocket);
|
unpackSocket(dataBuffer + numBytesRead, socketDestination);
|
||||||
} else {
|
} else {
|
||||||
// IPv6 address, or bad designator
|
// IPv6 address, or bad designator
|
||||||
qDebug("Received a socket that cannot be unpacked!\n");
|
qDebug("Received a socket that cannot be unpacked!\n");
|
||||||
|
@ -78,26 +84,30 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
|
||||||
}
|
}
|
||||||
|
|
||||||
Assignment::~Assignment() {
|
Assignment::~Assignment() {
|
||||||
delete _destinationSocket;
|
delete _attachedPublicSocket;
|
||||||
|
delete _attachedLocalSocket;
|
||||||
delete _pool;
|
delete _pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Assignment::setDestinationSocket(const sockaddr* destinationSocket) {
|
void Assignment::setAttachedPublicSocket(const sockaddr* attachedPublicSocket) {
|
||||||
|
|
||||||
if (_destinationSocket) {
|
if (_attachedPublicSocket) {
|
||||||
// delete the old _domainSocket if it exists
|
// delete the old socket if it exists
|
||||||
delete _destinationSocket;
|
delete _attachedPublicSocket;
|
||||||
_destinationSocket = NULL;
|
_attachedPublicSocket = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new sockaddr or sockaddr_in depending on what type of address this is
|
copySocketToEmptySocketPointer(_attachedPublicSocket, attachedPublicSocket);
|
||||||
if (destinationSocket->sa_family == AF_INET) {
|
|
||||||
_destinationSocket = (sockaddr*) new sockaddr_in;
|
|
||||||
memcpy(_destinationSocket, destinationSocket, sizeof(sockaddr_in));
|
|
||||||
} else {
|
|
||||||
_destinationSocket = (sockaddr*) new sockaddr_in6;
|
|
||||||
memcpy(_destinationSocket, destinationSocket, sizeof(sockaddr_in6));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Assignment::setAttachedLocalSocket(const sockaddr* attachedLocalSocket) {
|
||||||
|
if (_attachedLocalSocket) {
|
||||||
|
// delete the old socket if it exists
|
||||||
|
delete _attachedLocalSocket;
|
||||||
|
_attachedLocalSocket = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
copySocketToEmptySocketPointer(_attachedLocalSocket, attachedLocalSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Assignment::packToBuffer(unsigned char* buffer) {
|
int Assignment::packToBuffer(unsigned char* buffer) {
|
||||||
|
@ -116,11 +126,14 @@ int Assignment::packToBuffer(unsigned char* buffer) {
|
||||||
numPackedBytes += sizeof(char);
|
numPackedBytes += sizeof(char);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_destinationSocket) {
|
if (_attachedPublicSocket || _attachedLocalSocket) {
|
||||||
buffer[numPackedBytes++] = (_destinationSocket->sa_family == AF_INET)
|
sockaddr* socketToPack = (_attachedPublicSocket) ? _attachedPublicSocket : _attachedLocalSocket;
|
||||||
|
|
||||||
|
// we have a socket to pack, add the designator
|
||||||
|
buffer[numPackedBytes++] = (socketToPack->sa_family == AF_INET)
|
||||||
? IPv4_ADDRESS_DESIGNATOR : IPv6_ADDRESS_DESIGNATOR;
|
? IPv4_ADDRESS_DESIGNATOR : IPv6_ADDRESS_DESIGNATOR;
|
||||||
|
|
||||||
numPackedBytes += packSocket(buffer + numPackedBytes, _destinationSocket);
|
numPackedBytes += packSocket(buffer + numPackedBytes, socketToPack);
|
||||||
}
|
}
|
||||||
|
|
||||||
return numPackedBytes;
|
return numPackedBytes;
|
||||||
|
|
|
@ -43,8 +43,11 @@ 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* getDestinationSocket() { return _destinationSocket; }
|
const sockaddr* getAttachedPublicSocket() { return _attachedPublicSocket; }
|
||||||
void setDestinationSocket(const sockaddr* destinationSocket);
|
void setAttachedPublicSocket(const sockaddr* attachedPublicSocket);
|
||||||
|
|
||||||
|
const sockaddr* getAttachedLocalSocket() { return _attachedLocalSocket; }
|
||||||
|
void setAttachedLocalSocket(const sockaddr* attachedLocalSocket);
|
||||||
|
|
||||||
/// 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 +61,8 @@ 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* _destinationSocket; /// pointer to destination socket for assignment
|
sockaddr* _attachedPublicSocket; /// pointer to a public socket that relates to assignment, depends on direction
|
||||||
|
sockaddr* _attachedLocalSocket; /// pointer to a local socket that relates to assignment, depends on direction
|
||||||
timeval _time; /// time the assignment was created (set in constructor)
|
timeval _time; /// time the assignment was created (set in constructor)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,17 @@ int unpackSocket(const unsigned char* packedData, sockaddr* unpackDestSocket) {
|
||||||
return 6; // this could be more if we ever need IPv6
|
return 6; // this could be more if we ever need IPv6
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void copySocketToEmptySocketPointer(sockaddr* destination, const sockaddr* source) {
|
||||||
|
// create a new sockaddr or sockaddr_in depending on what type of address this is
|
||||||
|
if (source->sa_family == AF_INET) {
|
||||||
|
destination = (sockaddr*) new sockaddr_in;
|
||||||
|
memcpy(destination, source, sizeof(sockaddr_in));
|
||||||
|
} else {
|
||||||
|
destination = (sockaddr*) new sockaddr_in6;
|
||||||
|
memcpy(destination, source, sizeof(sockaddr_in6));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int getLocalAddress() {
|
int getLocalAddress() {
|
||||||
// get this node's local address so we can pass that to DS
|
// get this node's local address so we can pass that to DS
|
||||||
struct ifaddrs* ifAddrStruct = NULL;
|
struct ifaddrs* ifAddrStruct = NULL;
|
||||||
|
|
|
@ -45,6 +45,7 @@ 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(const unsigned char* packedData, sockaddr* unpackDestSocket);
|
int unpackSocket(const unsigned char* packedData, sockaddr* unpackDestSocket);
|
||||||
|
void copySocketToEmptySocketPointer(sockaddr* destination, const sockaddr* source);
|
||||||
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);
|
||||||
|
|
Loading…
Reference in a new issue