Move output parameters to end and add defines for buffer sizes

This commit is contained in:
kdeme 2019-11-13 18:34:05 +01:00 committed by zah
parent 278e230761
commit d781a25f30
3 changed files with 33 additions and 27 deletions

View File

@ -50,6 +50,12 @@ typedef struct {
typedef void (*received_msg_handler)(received_message* msg, void* udata);
/** Buffer lengths, can be used in go for convenience */
#define ID_LEN 32
#define SYMKEY_LEN 32
#define PRIVKEY_LEN 32
#define BLOOM_LEN 64
/** Initialize Nim and the Status library. Must be called before anything else
* of the API. Also, all following calls must come from the same thread as from
* which this call was done.
@ -77,19 +83,20 @@ void nimbus_poll();
/** Raw 32 byte arrays are passed as IDs. The caller needs to provide a pointer
* to 32 bytes allocation for this. */
bool nimbus_new_keypair(uint8_t id[32]);
bool nimbus_add_keypair(uint8_t id[32], const uint8_t privkey[32]);
bool nimbus_delete_keypair(const uint8_t id[32]);
bool nimbus_get_private_key(const uint8_t id[32], uint8_t privkey[32]);
bool nimbus_new_keypair(uint8_t id[ID_LEN]);
bool nimbus_add_keypair(const uint8_t privkey[PRIVKEY_LEN], uint8_t id[ID_LEN]);
bool nimbus_delete_keypair(const uint8_t id[ID_LEN]);
bool nimbus_get_private_key(const uint8_t id[ID_LEN],
uint8_t privkey[PRIVKEY_LEN]);
/** Symmetric Keys API */
/** Raw 32 byte arrays are passed as IDs. The caller needs to provide a pointer
* to 32 bytes allocation for this. */
bool nimbus_add_symkey(uint8_t id[32], const uint8_t symkey[32]);
bool nimbus_add_symkey_from_password(uint8_t id[32], const char* password);
bool nimbus_delete_symkey(const uint8_t id[32]);
bool nimbus_get_symkey(const uint8_t id[32], uint8_t symkey[32]);
bool nimbus_add_symkey(const uint8_t symkey[SYMKEY_LEN], uint8_t id[ID_LEN]);
bool nimbus_add_symkey_from_password(const char* password, uint8_t id[ID_LEN]);
bool nimbus_delete_symkey(const uint8_t id[ID_LEN]);
bool nimbus_get_symkey(const uint8_t id[ID_LEN], uint8_t symkey[SYMKEY_LEN]);
/** Whisper message posting and receiving API */
@ -98,9 +105,9 @@ bool nimbus_post(post_message* msg);
/** Subscribe to given filter. The void pointer udata will be passed to the
* received_msg_handler callback.
*/
bool nimbus_subscribe_filter(uint8_t id[32], filter_options* filter_options,
received_msg_handler msg, void* udata);
bool nimbus_unsubscribe_filter(const uint8_t id[32]);
bool nimbus_subscribe_filter(filter_options* filter_options,
received_msg_handler msg, void* udata, uint8_t id[ID_LEN]);
bool nimbus_unsubscribe_filter(const uint8_t id[ID_LEN]);
/** Get the minimum required PoW of this node */
double nimbus_get_min_pow();
@ -108,7 +115,7 @@ double nimbus_get_min_pow();
/** Get the currently set bloom filter of this node. This will automatically
*update for each filter subsribed to.
*/
void nimbus_get_bloom_filter(uint8_t bloomfilter[64]);
void nimbus_get_bloom_filter(uint8_t bloomfilter[BLOOM_LEN]);
/** Example helper, can be removed */
topic nimbus_channel_to_topic(const char* channel);

View File

@ -176,7 +176,7 @@ proc nimbus_new_keypair(id: var Identifier): bool {.exportc, raises: [].} =
# in `newKeyPair`
discard
proc nimbus_add_keypair(id: var Identifier, privateKey: ptr byte):
proc nimbus_add_keypair(privateKey: ptr byte, id: var Identifier):
bool {.exportc, raises: [OSError, IOError, ValueError].} =
## Caller needs to provide as id a pointer to 32 bytes allocation.
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
@ -213,7 +213,7 @@ proc nimbus_get_private_key(id: Identifier, privateKey: var PrivateKey):
# Symmetric Keys
proc nimbus_add_symkey(id: var Identifier, symKey: ptr SymKey): bool
proc nimbus_add_symkey(symKey: ptr SymKey, id: var Identifier): bool
{.exportc, raises: [].} =
## Caller needs to provide as id a pointer to 32 bytes allocation.
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
@ -225,7 +225,7 @@ proc nimbus_add_symkey(id: var Identifier, symKey: ptr SymKey): bool
# Copy of key happens at add
whisperKeys.symKeys.add(id.toHex, symKey[])
proc nimbus_add_symkey_from_password(id: var Identifier, password: cstring):
proc nimbus_add_symkey_from_password(password: cstring, id: var Identifier):
bool {.exportc, raises: [].} =
## Caller needs to provide as id a pointer to 32 bytes allocation.
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
@ -321,9 +321,9 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} =
powTime = message.powTime,
powTarget = message.powTarget)
proc nimbus_subscribe_filter(id: var Identifier, options: ptr CFilterOptions,
proc nimbus_subscribe_filter(options: ptr CFilterOptions,
handler: proc (msg: ptr CReceivedMessage, udata: pointer) {.gcsafe, cdecl.},
udata: pointer = nil): bool {.exportc.} =
udata: pointer = nil, id: var Identifier): bool {.exportc.} =
## Encryption is mandatory.
## A symmetric key or an asymmetric key must be provided. Both is not allowed.
## In case of a passed handler, the received msg needs to be copied before the

View File

@ -60,25 +60,24 @@ func StatusListenAndPost(channel string) {
channelC := C.CString(channel)
defer C.free(unsafe.Pointer(channelC))
tmp := C.malloc(C.size_t(32))
if C.nimbus_add_symkey_from_password((*C.uint8_t)(tmp), channelC) == false {
tmp := C.malloc(C.size_t(C.ID_LEN))
if C.nimbus_add_symkey_from_password(channelC, (*C.uint8_t)(tmp)) == false {
panic("Cannot create symmetric key")
}
// No need to do this back and forth GO <-> C, just showing how it might work
// in implementations (when wrapped in calls passing Go Bytes or Strings).
symKeyId := C.GoBytes(tmp, 32)
symKeyId := C.GoBytes(tmp, C.ID_LEN)
C.free(unsafe.Pointer(tmp))
symKeyIdC := (*C.uint8_t)(C.CBytes(symKeyId))
defer C.free(unsafe.Pointer(symKeyIdC))
tmp = C.malloc(C.size_t(32))
tmp = C.malloc(C.size_t(C.ID_LEN))
if C.nimbus_new_keypair((*C.uint8_t)(tmp)) == false {
panic("Cannot create asymmetric keypair")
}
// No need to do this back and forth GO <-> C, just showing how it might work
// in implementations (when wrapped in calls passing Go Bytes or Strings).
asymKeyId := C.GoBytes(tmp, 32)
asymKeyId := C.GoBytes(tmp, C.ID_LEN)
C.free(unsafe.Pointer(tmp))
asymKeyIdC := (*C.uint8_t)(C.CBytes(asymKeyId))
defer C.free(unsafe.Pointer(asymKeyIdC))
@ -89,13 +88,13 @@ func StatusListenAndPost(channel string) {
minPow: 0.002,
topic: C.nimbus_channel_to_topic(channelC).topic}
tmp = C.malloc(C.size_t(32))
if C.nimbus_subscribe_filter((*C.uint8_t)(tmp), &options,
tmp = C.malloc(C.size_t(C.ID_LEN))
if C.nimbus_subscribe_filter(&options,
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)),
unsafe.Pointer(&msgCount)) == false {
unsafe.Pointer(&msgCount), (*C.uint8_t)(tmp)) == false {
panic("Cannot subscribe filter")
}
filterId := C.GoBytes(tmp, 32)
filterId := C.GoBytes(tmp, C.ID_LEN)
C.free(unsafe.Pointer(tmp))
fmt.Printf("[nim-status] filter subscribed, id: %s\n",
hex.EncodeToString(filterId))