cbitcoin
 All Data Structures Files Functions Variables Typedefs
src/CBDependencies.h File Reference

File for weak linked functions for dependency injection. All these functions are unimplemented. The functions include the crytography functions which are key for the functioning of bitcoin. Sockets must be non-blocking and use an asynchronous onErrorReceived-type system. The use of the sockets is designed to be compatible with libevent. The random number functions should be cryptographically secure. See the dependecies folder for implementations. More...

#include <stdint.h>
#include <stdbool.h>
#include "CBConstants.h"

Go to the source code of this file.

Functions

void CBSha256 (uint8_t *data, uint16_t length, uint8_t *output)
 SHA-256 cryptographic hash function.
void CBRipemd160 (uint8_t *data, uint16_t length, uint8_t *output)
 RIPEMD-160 cryptographic hash function.
void CBSha160 (uint8_t *data, uint16_t length, uint8_t *output)
 SHA-1 cryptographic hash function.
bool CBEcdsaVerify (uint8_t *signature, uint8_t sigLen, uint8_t *hash, const uint8_t *pubKey, uint8_t keyLen)
 Verifies an ECDSA signature. This function must stick to the cryptography requirements in OpenSSL version 1.0.0 or any other compatible version. There may be compatibility problems when using libraries or code other than OpenSSL since OpenSSL does not adhere fully to the SEC1 ECDSA standards. This could cause security problems in your code. If in doubt, stick to OpenSSL.
CBSocketReturn CBNewSocket (uint64_t *socketID, bool IPv6)
 Creates a new TCP/IP socket. The socket should use a non-blocking mode.
bool CBSocketBind (uint64_t *socketID, bool IPv6, uint16_t port)
bool CBSocketConnect (uint64_t socketID, uint8_t *IP, bool IPv6, uint16_t port)
 Begin connecting to an external host with a socket. This should be non-blocking.
bool CBSocketListen (uint64_t socketID, uint16_t maxConnections)
 Begin listening for incomming connections on a bound socket. This should be non-blocking.
bool CBSocketAccept (uint64_t socketID, uint64_t *connectionSocketID)
 Accepts an incomming IPv4 connection on a bound socket. This should be non-blocking.
bool CBNewEventLoop (uint64_t *loopID, void(*onError)(void *), void(*onDidTimeout)(void *, void *, CBTimeOutType), void *communicator)
 Starts a event loop for socket onErrorReceived on a seperate thread. Access to the loop id should be thread safe.
bool CBSocketCanAcceptEvent (uint64_t *eventID, uint64_t loopID, uint64_t socketID, void(*onCanAccept)(void *, uint64_t))
 Creates an event where a listening socket is available for accepting a connection. The event should be persistent and not issue timeouts.
bool CBSocketDidConnectEvent (uint64_t *eventID, uint64_t loopID, uint64_t socketID, void(*onDidConnect)(void *, void *), void *peer)
 Sets a function pointer for the event where a socket has connected. The event only needs to fire once on the successful connection or timeout.
bool CBSocketCanSendEvent (uint64_t *eventID, uint64_t loopID, uint64_t socketID, void(*onCanSend)(void *, void *), void *peer)
 Sets a function pointer for the event where a socket is available for sending data. This should be persistent.
bool CBSocketCanReceiveEvent (uint64_t *eventID, uint64_t loopID, uint64_t socketID, void(*onCanReceive)(void *, void *), void *peer)
 Sets a function pointer for the event where a socket is available for receiving data. This should be persistent.
bool CBSocketAddEvent (uint64_t eventID, uint32_t timeout)
 Adds an event to be pending.
bool CBSocketRemoveEvent (uint64_t eventID)
 Removes an event so no more onErrorReceived are made.
void CBSocketFreeEvent (uint64_t eventID)
 Makes an event non-pending and frees it.
int32_t CBSocketSend (uint64_t socketID, uint8_t *data, uint32_t len)
 Sends data to a socket. This should be non-blocking.
int32_t CBSocketReceive (uint64_t socketID, uint8_t *data, uint32_t len)
 Receives data from a socket. This should be non-blocking.
bool CBStartTimer (uint64_t loopID, uint64_t *timer, uint16_t time, void(*callback)(void *), void *arg)
 Calls a callback every "time" seconds, until the timer is ended.
void CBEndTimer (uint64_t timer)
 Ends a timer.
void CBCloseSocket (uint64_t socketID)
 Closes a socket. The id should be freed, as well as any other data relating to this socket.
void CBExitEventLoop (uint64_t loopID)
 Exits an event loop and frees all data relating to it.
bool CBNewSecureRandomGenerator (uint64_t *gen)
 Returns an instance of a cryptographically secure random number generator.
void CBSecureRandomSeed (uint64_t gen)
 Seeds the random number generator securely.
void CBRandomSeed (uint64_t gen, uint64_t seed)
 Seeds the generator from a 64-bit integer.
uint64_t CBSecureRandomInteger (uint64_t gen)
 Generates a 64 bit integer.
void CBFreeSecureRandomGenerator (uint64_t gen)
 Frees the random number generator.

Detailed Description

File for weak linked functions for dependency injection. All these functions are unimplemented. The functions include the crytography functions which are key for the functioning of bitcoin. Sockets must be non-blocking and use an asynchronous onErrorReceived-type system. The use of the sockets is designed to be compatible with libevent. The random number functions should be cryptographically secure. See the dependecies folder for implementations.


Function Documentation

void CBCloseSocket ( uint64_t  socketID)

Closes a socket. The id should be freed, as well as any other data relating to this socket.

Parameters:
socketIDThe socket id to be closed.
bool CBEcdsaVerify ( uint8_t *  signature,
uint8_t  sigLen,
uint8_t *  hash,
const uint8_t *  pubKey,
uint8_t  keyLen 
)

Verifies an ECDSA signature. This function must stick to the cryptography requirements in OpenSSL version 1.0.0 or any other compatible version. There may be compatibility problems when using libraries or code other than OpenSSL since OpenSSL does not adhere fully to the SEC1 ECDSA standards. This could cause security problems in your code. If in doubt, stick to OpenSSL.

Parameters:
signatureBER encoded signature bytes.
sigLenThe length of the signature bytes.
hashA 32 byte hash for checking the signature against.
pubKeyPublic key bytes to check this signature with.
keyLenThe length of the public key bytes.
Returns:
true if the signature is valid and false if invalid.
void CBEndTimer ( uint64_t  timer)

Ends a timer.

Parameters:
timerThe timer sent by reference to be set.
void CBExitEventLoop ( uint64_t  loopID)

Exits an event loop and frees all data relating to it.

Parameters:
loopIDThe loop ID. If zero, do nothing.
void CBFreeSecureRandomGenerator ( uint64_t  gen)

Frees the random number generator.

Parameters:
genThe generator.
bool CBNewEventLoop ( uint64_t *  loopID,
void(*)(void *)  onError,
void(*)(void *, void *, CBTimeOutType)  onDidTimeout,
void *  communicator 
)

Starts a event loop for socket onErrorReceived on a seperate thread. Access to the loop id should be thread safe.

Parameters:
loopIDA uint64_t storing an integer or pointer representation of the new event loop.
onErrorIf the event loop fails during execution of the thread, this function should be called.
onDidTimeoutThe function to call for timeout onErrorReceived. The second argument is for the peer given by onErrorReceived. The third is for the timeout type. For receiving data, the timeout should be CB_TIMEOUT_RECEIVE. The CBNetworkCommunicator will determine if it should be changed to CB_TIMEOUT_RESPONSE.
communicatorA CBNetworkCommunicator to pass to all event functions (first parameter), including "onError" and "onDidTimeout"
Returns:
true on success, false on failure.
bool CBNewSecureRandomGenerator ( uint64_t *  gen)

Returns an instance of a cryptographically secure random number generator.

Parameters:
Thegenerator as a pointer or integer.
Returns:
true on success, false on failure.
CBSocketReturn CBNewSocket ( uint64_t *  socketID,
bool  IPv6 
)

Creates a new TCP/IP socket. The socket should use a non-blocking mode.

Parameters:
socketIDPointer to uint64_t. Can be pointer value.
IPv6true if the socket is used to connect to the IPv6 network.
Returns:
CB_SOCKET_OK if the socket was successfully created, CB_SOCKET_NO_SUPPORT and CB_SOCKET_BAD if the socket could not be created for any other reason.
void CBRandomSeed ( uint64_t  gen,
uint64_t  seed 
)

Seeds the generator from a 64-bit integer.

Parameters:
genThe generator.
seedThe 64-bit integer.
void CBRipemd160 ( uint8_t *  data,
uint16_t  length,
uint8_t *  output 
)

RIPEMD-160 cryptographic hash function.

Parameters:
dataA pointer to the byte data to hash.
lengthThe length of the data to hash.
outputA pointer to hold a 20-byte hash.
uint64_t CBSecureRandomInteger ( uint64_t  gen)

Generates a 64 bit integer.

Parameters:
genThe generator.
Returns:
The random 64-bit integer integer.
void CBSecureRandomSeed ( uint64_t  gen)

Seeds the random number generator securely.

Parameters:
genThe generator.
void CBSha160 ( uint8_t *  data,
uint16_t  length,
uint8_t *  output 
)

SHA-1 cryptographic hash function.

Parameters:
dataA pointer to the byte data to hash.
lengthThe length of the data to hash.
outputA pointer to hold a 10-byte hash.
void CBSha256 ( uint8_t *  data,
uint16_t  length,
uint8_t *  output 
)

SHA-256 cryptographic hash function.

Parameters:
dataA pointer to the byte data to hash.
lengthThe length of the data to hash.
outputA pointer to hold a 32-byte hash.
bool CBSocketAccept ( uint64_t  socketID,
uint64_t *  connectionSocketID 
)

Accepts an incomming IPv4 connection on a bound socket. This should be non-blocking.

Parameters:
socketIDThe socket id
connectionSocketIDA socket id for a new socket for the connection.
Returns:
true if function was sucessful and false otherwise.
bool CBSocketAddEvent ( uint64_t  eventID,
uint32_t  timeout 
)

Adds an event to be pending.

Parameters:
eventIDThe event ID to add.
timeoutThe time in milliseconds to issue a timeout for the event. 0 for no timeout.
Returns:
true if sucessful, false otherwise.
bool CBSocketCanAcceptEvent ( uint64_t *  eventID,
uint64_t  loopID,
uint64_t  socketID,
void(*)(void *, uint64_t)  onCanAccept 
)

Creates an event where a listening socket is available for accepting a connection. The event should be persistent and not issue timeouts.

Parameters:
loopIDThe loop id for socket onErrorReceived.
socketIDThe socket id
onCanAcceptThe function to call for the event. Accepts "onEventArg" and the socket ID.
Returns:
true on success, false on failure.
bool CBSocketCanReceiveEvent ( uint64_t *  eventID,
uint64_t  loopID,
uint64_t  socketID,
void(*)(void *, void *)  onCanReceive,
void *  peer 
)

Sets a function pointer for the event where a socket is available for receiving data. This should be persistent.

Parameters:
loopIDThe loop id for socket onErrorReceived.
socketIDThe socket id
onCanReceiveThe function to call for the event.
peerThe peer to send to the "onCanReceive" or "onDidTimeout" function.
Returns:
true on success, false on failure.
bool CBSocketCanSendEvent ( uint64_t *  eventID,
uint64_t  loopID,
uint64_t  socketID,
void(*)(void *, void *)  onCanSend,
void *  peer 
)

Sets a function pointer for the event where a socket is available for sending data. This should be persistent.

Parameters:
loopIDThe loop id for socket onErrorReceived.
socketIDThe socket id
onCanSendThe function to call for the event.
peerThe peer to send to the "onCanSend" or "onDidTimeout" function.
Returns:
true on success, false on failure.
bool CBSocketConnect ( uint64_t  socketID,
uint8_t *  IP,
bool  IPv6,
uint16_t  port 
)

Begin connecting to an external host with a socket. This should be non-blocking.

Parameters:
socketIDThe socket id
IP16 bytes for an IPv6 address to connect to.
IPv6True if IP address is for the IPv6 network. A IPv6 address can represent addresses for IPv4 too. To avoid the need to detect this, a boolean can be used.
portPort to connect to.
Returns:
true if the function was sucessful and false otherwise.
bool CBSocketDidConnectEvent ( uint64_t *  eventID,
uint64_t  loopID,
uint64_t  socketID,
void(*)(void *, void *)  onDidConnect,
void *  peer 
)

Sets a function pointer for the event where a socket has connected. The event only needs to fire once on the successful connection or timeout.

Parameters:
loopIDThe loop id for socket onErrorReceived.
socketIDThe socket id
onDidConnectThe function to call for the event.
peerThe peer to send to the "onDidConnect" or "onDidTimeout" function.
Returns:
true on success, false on failure.
void CBSocketFreeEvent ( uint64_t  eventID)

Makes an event non-pending and frees it.

Parameters:
eventIDThe event to free.
bool CBSocketListen ( uint64_t  socketID,
uint16_t  maxConnections 
)

Begin listening for incomming connections on a bound socket. This should be non-blocking.

Parameters:
socketIDThe socket id
maxConnectionsThe maximum incomming connections to allow.
Returns:
true if function was sucessful and false otherwise.
int32_t CBSocketReceive ( uint64_t  socketID,
uint8_t *  data,
uint32_t  len 
)

Receives data from a socket. This should be non-blocking.

Parameters:
socketIDThe socket id to receive data from.
dataThe data bytes to write the data to.
lenThe length of the data.
Returns:
The number of bytes actually written into "data", CB_SOCKET_CONNECTION_CLOSE on connection closure, 0 on no bytes received, and CB_SOCKET_FAILURE on failure.
bool CBSocketRemoveEvent ( uint64_t  eventID)

Removes an event so no more onErrorReceived are made.

Parameters:
eventIDThe event ID to remove
Returns:
true if sucessful, false otherwise.
int32_t CBSocketSend ( uint64_t  socketID,
uint8_t *  data,
uint32_t  len 
)

Sends data to a socket. This should be non-blocking.

Parameters:
socketIDThe socket id to send to.
dataThe data bytes to send.
lenThe length of the data to send.
Returns:
The number of bytes actually sent, and CB_SOCKET_FAILURE on failure that suggests further data cannot be sent.
bool CBStartTimer ( uint64_t  loopID,
uint64_t *  timer,
uint16_t  time,
void(*)(void *)  callback,
void *  arg 
)

Calls a callback every "time" seconds, until the timer is ended.

Parameters:
loopIDThe loop id for onErrorReceived.
timerThe timer sent by reference to be set.
timeThe number of milliseconds between each call of the callback.
callbackThe callback function.
argThe callback argument.