cbitcoin
 All Data Structures Files Functions Variables Typedefs
src/structures/CBObject/CBMessage/CBAddressManager/CBAddressManager.h File Reference

Stores addresses both unconnected and connected. It also proves serialisation and deserialisation so that addresses can be stored in binary format. Credit largely goes to the addrman.cpp code in the original client, although the code here has differences. Inherits CBMessage. The binary format contains a 32 bit integer for the cbitcoin version, followed by CB_BUCKET_NUM buckets. Each of these buckets is written as a 16 bit integer for the number of addresses and then the serialised CBNetworkAddresses one after the other. After the buckets is the 64 bit integer secret. More...

#include "CBPeer.h"
#include "CBAddressBroadcast.h"
#include "CBNetworkFunctions.h"
#include "CBDependencies.h"
#include <time.h>

Go to the source code of this file.

Data Structures

struct  CBNetworkAddressLocator
 Structure for storing a pointer to a CBNetworkAddress and describing it's location in the CBAddressManager. More...
struct  CBBucket
 Structure for placing IP addresses such that is is difficult to obtain IPs that fall into many buckets. More...
struct  CBAddressManager
 Structure for CBAddressManager objects. More...

Functions

CBAddressManagerCBNewAddressManager (void(*onErrorReceived)(CBError error, char *,...), void(*onBadTime)(void *))
 Creates a new CBAddressManager object.
CBAddressManagerCBNewAddressManagerFromData (CBByteArray *data, void(*onErrorReceived)(CBError error, char *,...), void(*onBadTime)(void *))
 Creates a new CBAddressManager object from serialised data.
CBAddressManagerCBGetAddressManager (void *self)
 Gets a CBAddressManager from another object. Use this to avoid casts.
bool CBInitAddressManager (CBAddressManager *self, void(*onErrorReceived)(CBError error, char *,...), void(*onBadTime)(void *))
 Initialises a CBAddressManager object.
bool CBInitAddressManagerFromData (CBAddressManager *self, CBByteArray *data, void(*onErrorReceived)(CBError error, char *,...), void(*onBadTime)(void *))
 Initialises a CBAddressManager object from serialised data.
void CBFreeAddressManager (void *vself)
 Frees a CBAddressManager object.
bool CBAddressManagerAddAddress (CBAddressManager *self, CBNetworkAddress *addr)
 Adds a CBPeer an places it into the CBAddressManager with a retain.
void CBAddressManagerAdjustTime (CBAddressManager *self)
 Adjust the network time offset with a peer's time.
uint32_t CBAddressManagerDeserialise (CBAddressManager *self)
 Deserialises a CBAddressManager so that it can be used as an object.
CBNetworkAddressLocatorCBAddressManagerGetAddresses (CBAddressManager *self, uint32_t num)
 Gets a number of addresses.
uint8_t CBAddressManagerGetBucketIndex (CBAddressManager *self, CBNetworkAddress *addr)
 Gets the bucket index for an address.
uint64_t CBAddressManagerGetGroup (CBAddressManager *self, CBNetworkAddress *addr)
 Gets the group number for an address. It is seperated such as to make it difficult to use an IP in many different groups. This is done such as the original bitcoin client.
uint64_t CBAddressManagerGetNumberOfAddresses (CBAddressManager *self)
 Gets the total amount of addresses, including connected ones. Not thread safe.
CBNetworkAddressCBAddressManagerGotNetworkAddress (CBAddressManager *self, CBNetworkAddress *addr)
 Determines if a CBNetworkAddress already exists in the CBAddressManager. Compares the IP address and port.
CBPeerCBAddressManagerGotNode (CBAddressManager *self, CBNetworkAddress *addr)
 Determines if a CBNetworkAddress is in the "peers" list. Compares the IP address and port.
bool CBAddressManagerIsReachable (CBAddressManager *self, CBIPType type)
 Determines if an IP type is reachable.
void CBAddressManagerRemoveAddress (CBAddressManager *self, CBNetworkAddress *addr)
 Removes a CBNetworkAddress if the CBAddressManager has it.
void CBAddressManagerRemoveNode (CBAddressManager *self, CBPeer *peer)
 Remove a CBPeer from the peers list.
uint32_t CBAddressManagerSerialise (CBAddressManager *self)
 Serialises a CBAddressManager to the byte data.
void CBAddressManagerSetReachability (CBAddressManager *self, CBIPType type, bool reachable)
 Sets the reachability of an IP type.
bool CBAddressManagerSetup (CBAddressManager *self)
 Setups the CBAddressMenager for both CBInitAddressManager and CBInitAddressManagerFromData.
bool CBAddressManagerTakeAddress (CBAddressManager *self, CBNetworkAddress *addr)
 Takes a CBPeer an places it into the CBAddressManager.
bool CBAddressManagerTakePeer (CBAddressManager *self, CBPeer *peer)
 Takes a CBPeer an places it into the peers list.

Detailed Description

Stores addresses both unconnected and connected. It also proves serialisation and deserialisation so that addresses can be stored in binary format. Credit largely goes to the addrman.cpp code in the original client, although the code here has differences. Inherits CBMessage. The binary format contains a 32 bit integer for the cbitcoin version, followed by CB_BUCKET_NUM buckets. Each of these buckets is written as a 16 bit integer for the number of addresses and then the serialised CBNetworkAddresses one after the other. After the buckets is the 64 bit integer secret.


Function Documentation

Adds a CBPeer an places it into the CBAddressManager with a retain.

Parameters:
selfThe CBAddressManager object.
peerThe CBNetworkAddress to take.
Returns:
true if the address was taken successfully, false if an error occured.

Adjust the network time offset with a peer's time.

Parameters:
selfThe CBAddressManager object.
timeTime to adjust network time with.

Deserialises a CBAddressManager so that it can be used as an object.

Parameters:
selfThe CBAddressManager object
Returns:
The length read on success, 0 on failure.

Gets a number of addresses.

Parameters:
selfThe CBAddressManager object.
numThe number of addresses to get.
Returns:
The addresses in a newly allocated memory block. This block is NULL terminated. The number of addresses returned may be less than "num". CBNetworkAddressLocator is used to locate addresses if needed. This will return NULL if malloc() fails.

Gets the bucket index for an address.

Parameters:
selfThe CBAddressManager object.
addrThe address.
Returns:
The bucket index.
uint64_t CBAddressManagerGetGroup ( CBAddressManager self,
CBNetworkAddress addr 
)

Gets the group number for an address. It is seperated such as to make it difficult to use an IP in many different groups. This is done such as the original bitcoin client.

Parameters:
selfThe CBAddressManager object.
addrThe address.
Returns:
The address group number.

Gets the total amount of addresses, including connected ones. Not thread safe.

Parameters:
selfThe CBAddressManager object.
Returns:
The total number of addresses.

Determines if a CBNetworkAddress already exists in the CBAddressManager. Compares the IP address and port.

Parameters:
selfThe CBAddressManager object.
addrThe address.
Returns:
If the address already exists, returns the existing object. Else returns NULL.

Determines if a CBNetworkAddress is in the "peers" list. Compares the IP address and port.

Parameters:
selfThe CBAddressManager object.
addrThe address.
Returns:
If the address already exists as a connected peer, returns the existing object. Else returns NULL.
bool CBAddressManagerIsReachable ( CBAddressManager self,
CBIPType  type 
)

Determines if an IP type is reachable.

Parameters:
selfThe CBAddressManager object.
typeThe type.
Returns:
true if reachable, false if not reachable.

Removes a CBNetworkAddress if the CBAddressManager has it.

Parameters:
selfThe CBAddressManager object.
addrThe CBNetworkAddress to remove
void CBAddressManagerRemoveNode ( CBAddressManager self,
CBPeer peer 
)

Remove a CBPeer from the peers list.

Parameters:
selfThe CBAddressManager object.
peerThe CBPeer to remove

Serialises a CBAddressManager to the byte data.

Parameters:
selfThe CBAddressManager object
Returns:
The length written on success, 0 on failure.
void CBAddressManagerSetReachability ( CBAddressManager self,
CBIPType  type,
bool  reachable 
)

Sets the reachability of an IP type.

Parameters:
selfThe CBAddressManager object
typeThe mask to set the types.
reachabletrue for setting as reachable, false if not.

Setups the CBAddressMenager for both CBInitAddressManager and CBInitAddressManagerFromData.

Parameters:
selfThe CBAddressManager object
Returns:
true if OK, false if not.

Takes a CBPeer an places it into the CBAddressManager.

Parameters:
selfThe CBAddressManager object.
peerThe CBNetworkAddress to take.
Returns:
true if the address was taken successfully, false if an error occured.
bool CBAddressManagerTakePeer ( CBAddressManager self,
CBPeer peer 
)

Takes a CBPeer an places it into the peers list.

Parameters:
selfThe CBAddressManager object.
peerThe CBPeer to take.
void CBFreeAddressManager ( void *  vself)

Frees a CBAddressManager object.

Parameters:
selfThe CBAddressManager object to free.

Gets a CBAddressManager from another object. Use this to avoid casts.

Parameters:
selfThe object to obtain the CBAddressManager from.
Returns:
The CBAddressManager object.
bool CBInitAddressManager ( CBAddressManager self,
void(*)(CBError error, char *,...)  onErrorReceived,
void(*)(void *)  onBadTime 
)

Initialises a CBAddressManager object.

Parameters:
selfThe CBAddressManager object to initialise
Returns:
true on success, false on failure.
bool CBInitAddressManagerFromData ( CBAddressManager self,
CBByteArray data,
void(*)(CBError error, char *,...)  onErrorReceived,
void(*)(void *)  onBadTime 
)

Initialises a CBAddressManager object from serialised data.

Parameters:
selfThe CBAddressManager object to initialise
dataThe serialised data.
Returns:
true on success, false on failure.
CBAddressManager* CBNewAddressManager ( void(*)(CBError error, char *,...)  onErrorReceived,
void(*)(void *)  onBadTime 
)

Creates a new CBAddressManager object.

Returns:
A new CBAddressManager object.
CBAddressManager* CBNewAddressManagerFromData ( CBByteArray data,
void(*)(CBError error, char *,...)  onErrorReceived,
void(*)(void *)  onBadTime 
)

Creates a new CBAddressManager object from serialised data.

Parameters:
dataSerialised CBAddressManager data.
Returns:
A new CBAddressManager object.