unsigned char* DomainServer::addNodeToBroadcastPacket(unsigned char* currentPosition, Node* nodeToAdd) { *currentPosition++ = nodeToAdd->getType(); currentPosition += packNodeId(currentPosition, nodeToAdd->getNodeID()); currentPosition += packSocket(currentPosition, nodeToAdd->getPublicSocket()); currentPosition += packSocket(currentPosition, nodeToAdd->getLocalSocket()); // return the new unsigned char * for broadcast packet return currentPosition; }
unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) { *currentPosition++ = agentToAdd->getType(); currentPosition += packAgentId(currentPosition, agentToAdd->getAgentId()); currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket()); currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket()); // return the new unsigned char * for broadcast packet return currentPosition; }
unsigned char* DomainServer::addNodeToBroadcastPacket(unsigned char* currentPosition, Node* nodeToAdd) { *currentPosition++ = nodeToAdd->getType(); QByteArray rfcUUID = nodeToAdd->getUUID().toRfc4122(); memcpy(currentPosition, rfcUUID.constData(), rfcUUID.size()); currentPosition += rfcUUID.size(); currentPosition += packSocket(currentPosition, nodeToAdd->getPublicSocket()); currentPosition += packSocket(currentPosition, nodeToAdd->getLocalSocket()); // return the new unsigned char * for broadcast packet return currentPosition; }
void AgentList::sendDomainServerCheckIn() { static bool printedDomainServerIP = false; // Lookup the IP address of the domain server if we need to if (atoi(DOMAIN_IP) == 0) { struct hostent* pHostInfo; if ((pHostInfo = gethostbyname(DOMAIN_HOSTNAME)) != NULL) { sockaddr_in tempAddress; memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length); strcpy(DOMAIN_IP, inet_ntoa(tempAddress.sin_addr)); printLog("Domain Server: %s \n", DOMAIN_HOSTNAME); } else { printLog("Failed domain server lookup\n"); } } else if (!printedDomainServerIP) { printLog("Domain Server IP: %s\n", DOMAIN_IP); printedDomainServerIP = true; } // construct the DS check in packet if we need to static unsigned char* checkInPacket = NULL; static int checkInPacketSize; if (!checkInPacket) { int numBytesAgentsOfInterest = _agentTypesOfInterest ? strlen((char*) _agentTypesOfInterest) : 0; // check in packet has header, agent type, port, IP, agent types of interest, null termination int numPacketBytes = sizeof(PACKET_HEADER) + sizeof(AGENT_TYPE) + sizeof(uint16_t) + (sizeof(char) * 4) + numBytesAgentsOfInterest + sizeof(unsigned char); checkInPacket = new unsigned char[numPacketBytes]; unsigned char* packetPosition = checkInPacket; *(packetPosition++) = (memchr(SOLO_AGENT_TYPES, _ownerType, sizeof(SOLO_AGENT_TYPES))) ? PACKET_HEADER_DOMAIN_REPORT_FOR_DUTY : PACKET_HEADER_DOMAIN_LIST_REQUEST; *(packetPosition++) = _ownerType; packetPosition += packSocket(checkInPacket + sizeof(PACKET_HEADER) + sizeof(AGENT_TYPE), getLocalAddress(), htons(_agentSocket.getListeningPort())); // add the number of bytes for agent types of interest *(packetPosition++) = numBytesAgentsOfInterest; // copy over the bytes for agent types of interest, if required if (numBytesAgentsOfInterest > 0) { memcpy(packetPosition, _agentTypesOfInterest, numBytesAgentsOfInterest); packetPosition += numBytesAgentsOfInterest; } checkInPacketSize = packetPosition - checkInPacket; } _agentSocket.send(DOMAIN_IP, DOMAINSERVER_PORT, checkInPacket, checkInPacketSize); }
int Assignment::packToBuffer(unsigned char* buffer) { int numPackedBytes = 0; // pack the UUID for this assignment, if this is an assignment create or deploy if (_command != Assignment::RequestCommand) { memcpy(buffer, _uuid.toRfc4122().constData(), NUM_BYTES_RFC4122_UUID); numPackedBytes += NUM_BYTES_RFC4122_UUID; } memcpy(buffer + numPackedBytes, &_type, sizeof(_type)); numPackedBytes += sizeof(_type); if (_attachedPublicSocket || _attachedLocalSocket) { 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; numPackedBytes += packSocket(buffer + numPackedBytes, socketToPack); } return numPackedBytes; }
void NodeList::sendDomainServerCheckIn(const char* assignmentUUID) { static bool printedDomainServerIP = false; // Lookup the IP address of the domain server if we need to if (_domainIP.isNull()) { qDebug("Looking up DS hostname %s.\n", _domainHostname.toStdString().c_str()); QHostInfo domainServerHostInfo = QHostInfo::fromName(_domainHostname); for (int i = 0; i < domainServerHostInfo.addresses().size(); i++) { if (domainServerHostInfo.addresses()[i].protocol() == QAbstractSocket::IPv4Protocol) { _domainIP = domainServerHostInfo.addresses()[i]; qDebug("DS at %s is at %s\n", _domainHostname.toStdString().c_str(), _domainIP.toString().toStdString().c_str()); printedDomainServerIP = true; break; } // if we got here without a break out of the for loop then we failed to lookup the address if (i == domainServerHostInfo.addresses().size() - 1) { qDebug("Failed domain server lookup\n"); } } } else if (!printedDomainServerIP) { qDebug("Domain Server IP: %s\n", _domainIP.toString().toStdString().c_str()); printedDomainServerIP = true; } // construct the DS check in packet if we need to if (!_checkInPacket) { int numBytesNodesOfInterest = _nodeTypesOfInterest ? strlen((char*) _nodeTypesOfInterest) : 0; const int IP_ADDRESS_BYTES = 4; // check in packet has header, optional UUID, node type, port, IP, node types of interest, null termination int numPacketBytes = sizeof(PACKET_TYPE) + sizeof(PACKET_VERSION) + sizeof(NODE_TYPE) + NUM_BYTES_RFC4122_UUID + sizeof(uint16_t) + IP_ADDRESS_BYTES + numBytesNodesOfInterest + sizeof(unsigned char); _checkInPacket = new unsigned char[numPacketBytes]; unsigned char* packetPosition = _checkInPacket; PACKET_TYPE nodePacketType = (memchr(SOLO_NODE_TYPES, _ownerType, sizeof(SOLO_NODE_TYPES))) ? PACKET_TYPE_DOMAIN_REPORT_FOR_DUTY : PACKET_TYPE_DOMAIN_LIST_REQUEST; int numHeaderBytes = populateTypeAndVersion(packetPosition, nodePacketType); packetPosition += numHeaderBytes; *(packetPosition++) = _ownerType; if (assignmentUUID) { // if we've got an assignment UUID to send add that here memcpy(packetPosition, assignmentUUID, NUM_BYTES_RFC4122_UUID); packetPosition += NUM_BYTES_RFC4122_UUID; } packetPosition += packSocket(_checkInPacket + (packetPosition - _checkInPacket), getLocalAddress(), htons(_nodeSocket.getListeningPort())); // add the number of bytes for node types of interest *(packetPosition++) = numBytesNodesOfInterest; // copy over the bytes for node types of interest, if required if (numBytesNodesOfInterest > 0) { memcpy(packetPosition, _nodeTypesOfInterest, numBytesNodesOfInterest); packetPosition += numBytesNodesOfInterest; } _numBytesCheckInPacket = packetPosition - _checkInPacket; } _nodeSocket.send(_domainIP.toString().toStdString().c_str(), _domainPort, _checkInPacket, _numBytesCheckInPacket); // increment the count of un-replied check-ins _numNoReplyDomainCheckIns++; }