bool CBInitNetworkAddress(CBNetworkAddress * self, uint64_t lastSeen, CBByteArray * ip, uint16_t port, CBVersionServices services, bool isPublic){ self->lastSeen = lastSeen; self->penalty = 0; self->ip = ip; self->isPublic = isPublic; if (NOT ip) { ip = CBNewByteArrayOfSize(16); if (NOT ip) return false; memset(CBByteArrayGetData(ip), 0, 16); self->type = CB_IP_INVALID; }else{ // Determine IP type self->type = CBGetIPType(CBByteArrayGetData(ip)); CBRetainObject(ip); } self->port = port; self->services = services; self->bucketSet = false; if (NOT CBInitMessageByObject(CBGetMessage(self))){ CBReleaseObject(ip); return false; } return true; }
uint8_t CBNetworkAddressDeserialise(CBNetworkAddress * self, bool timestamp){ CBByteArray * bytes = CBGetMessage(self)->bytes; if (! bytes) { CBLogError("Attempting to deserialise a CBNetworkAddress with no bytes."); return 0; } if (bytes->length < 26 + timestamp * 4) { CBLogError("Attempting to deserialise a CBNetworkAddress with less bytes than required."); return 0; } uint8_t start; uint64_t twoHoursAgo = time(NULL) - 3600; if (timestamp) { // Make sure we do not set self->lastSeen later than one hour ago. self->lastSeen = CBByteArrayReadInt32(bytes, 0); if (self->lastSeen > twoHoursAgo) self->lastSeen = twoHoursAgo; start = 4; }else{ self->lastSeen = twoHoursAgo; start = 0; } self->services = (CBVersionServices) CBByteArrayReadInt64(bytes, start); self->ip = CBNewByteArraySubReference(bytes, start + 8, 16); // Determine IP type self->type = CBGetIPType(CBByteArrayGetData(self->ip)); self->port = CBByteArrayReadPort(bytes, start + 24); return start + 26; }
void CBRPCServerAcceptConnection(void * vself, CBDepObject socket){ CBRPCServer * self = vself; CBSocketAddress sockAddr; if (! CBSocketAccept(socket, &self->connSocket, &sockAddr)){ CBLogError("Could not accept RPC connection."); return; } // We want local connections. if (!(CBGetIPType(CBByteArrayGetData(sockAddr.ip)) & CB_IP_LOCAL) || !CBRPCServerRespond(self, NULL, CB_HTTP_FORBIDDEN, false, CBRPCServerDisconnect)){ CBRPCServerDisconnect(self); return; } }