Change post payload and padding to ptr + size and some small improvements

This commit is contained in:
kdeme 2019-11-04 13:03:28 +01:00 committed by zah
parent e841c08b23
commit a8ca7d1d7f
3 changed files with 74 additions and 48 deletions

View File

@ -2,6 +2,7 @@
#define __LIBNIMBUS_H__
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
@ -9,10 +10,10 @@ extern "C" {
#endif
typedef struct {
int8_t* decoded;
uint8_t* decoded;
size_t decodedLen;
uint8_t source[64];
uint8_t recipientPublicKey[64];
uint8_t source[64]; /* TODO: change to ptr so they can be checked on nil? */
uint8_t recipientPublicKey[64]; /* TODO: change to ptr so they can be checked on nil? */
uint32_t timestamp;
uint32_t ttl;
uint8_t topic[4];
@ -23,20 +24,22 @@ typedef struct {
typedef struct {
const char* symKeyID;
const char* privateKeyID;
uint8_t* source; // 64 bytes public key
uint8_t* source; /* 64 bytes public key */
double minPow;
uint8_t topic[4];
int allowP2P;
} filter_options;
typedef struct {
const char* symKeyID;
uint8_t* pubKey; // 64 bytes public key
const char* sourceID;
const char* symKeyID; /* Identifier for symmetric key, set to nil if none */
uint8_t* pubKey; /* 64 bytes public key, set to nil if none */
const char* sourceID; /* Identifier for asymmetric key, set to nil if none */
uint32_t ttl;
uint8_t topic[4]; // default 0 is OK
const char* payload; // could also provide uint8_t* + size_t
const char* padding; // could also provide uint8_t* + size_t
uint8_t topic[4]; /* default of 0 is OK */
uint8_t* payload; /* payload to be send, can be len=0 but can not be nil */
size_t payloadLen;
uint8_t* padding; /* custom padding, can be set to nil */
size_t paddingLen;
double powTime;
double powTarget;
} post_message;
@ -73,8 +76,8 @@ void nimbus_poll();
* doing any other API calls. */
const char* nimbus_new_keypair();
const char* nimbus_add_keypair(const uint8_t* privkey);
int nimbus_delete_keypair(const char* id);
int nimbus_get_private_key(const char* id, uint8_t* privkey);
bool nimbus_delete_keypair(const char* id);
bool nimbus_get_private_key(const char* id, uint8_t* privkey);
/** Symmetric Keys API */
@ -82,8 +85,8 @@ int nimbus_get_private_key(const char* id, uint8_t* privkey);
* doing any other API calls. */
const char* nimbus_add_symkey(const uint8_t* symkey);
const char* nimbus_add_symkey_from_password(const char* password);
int nimbus_delete_symkey(const char* id);
int nimbus_get_symkey(const char* id, uint8_t* symkey);
bool nimbus_delete_symkey(const char* id);
bool nimbus_get_symkey(const char* id, uint8_t* symkey);
/** Whisper message posting and receiving API */
@ -92,9 +95,9 @@ int nimbus_get_symkey(const char* id, uint8_t* symkey);
*/
const char* nimbus_subscribe_filter(filter_options* filter_options,
received_msg_handler msg, void* udata);
int nimbus_unsubscribe_filter(const char* id);
bool nimbus_unsubscribe_filter(const char* id);
/* Post Whisper message to the queue */
int nimbus_post(post_message* msg);
bool nimbus_post(post_message* msg);
/** TODO: why are following two getters needed? */
@ -107,7 +110,7 @@ double nimbus_get_min_pow();
void nimbus_get_bloom_filter(uint8_t* bloomfilter);
/** Example helper, can be removed */
topic nimbus_string_to_topic(const char* s);
topic nimbus_channel_to_topic(const char* channel);
/** Very limited Status chat API */
void nimbus_post_public(const char* channel, const char* payload);

View File

@ -9,10 +9,9 @@
import
chronos, chronicles, nimcrypto/[utils, hmac, pbkdf2, hash], tables,
eth/[keys, rlp, p2p, async_utils], eth/p2p/rlpx_protocols/whisper_protocol,
eth/p2p/[discovery, enode, peer_pool, bootnodes, whispernodes]
from stew/byteutils import hexToSeqByte, hexToByteArray
stew/ranges/ptr_arith, eth/[keys, rlp, p2p, async_utils],
eth/p2p/rlpx_protocols/whisper_protocol,
eth/p2p/[enode, peer_pool, bootnodes, whispernodes]
# TODO: If we really want/need this type of API for the keys, put it somewhere
# seperate as it is the same code for Whisper RPC
@ -21,8 +20,6 @@ type
asymKeys*: Table[string, KeyPair]
symKeys*: Table[string, SymKey]
KeyGenerationError = object of CatchableError
proc newWhisperKeys*(): WhisperKeys =
new(result)
result.asymKeys = initTable[string, KeyPair]()
@ -56,8 +53,10 @@ type
sourceID*: cstring
ttl*: uint32
topic*: Topic
payload*: cstring
padding*: cstring
payload*: ptr byte
payloadLen*: csize
padding*: ptr byte
paddingLen*: csize
powTime*: float64
powTarget*: float64
@ -195,8 +194,8 @@ proc nimbus_add_peer(nodeId: cstring) {.exportc.} =
# Whisper API (Similar to Whisper RPC API)
# Mostly an example for now, lots of things to fix if continued like this.
proc nimbus_string_to_topic(s: cstring): CTopic {.exportc.} =
let hash = digest(keccak256, $s)
proc nimbus_channel_to_topic(channel: cstring): CTopic {.exportc.} =
let hash = digest(keccak256, $channel)
for i in 0..<4:
result.topic[i] = hash.data[i]
@ -268,9 +267,9 @@ proc nimbus_get_symkey(id: cstring, symKey: ptr SymKey):
# Whisper message posting and receiving API
proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} =
## Encryption is not mandatory.
## A symKey, an asymKey, or nothing can be provided. asymKey has precedence.
## Providing a payload is mandatory.
## Encryption is mandatory.
## A symmetric key or an asymmetric key can be provided. Both is not allowed.
## Providing a payload is mandatory, it cannot be nil, but can be of length 0.
var
sigPrivKey: Option[PrivateKey]
asymKey: Option[PublicKey]
@ -278,6 +277,14 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} =
padding: Option[Bytes]
payload: Bytes
if not message.pubKey.isNil() and not message.symKeyID.isNil():
warn "Both symmetric and asymmetric keys are provided, choose one."
return false
if message.pubKey.isNil() and message.symKeyID.isNil():
warn "Both symmetric and asymmetric keys are nil, provide one."
return false
if not message.pubKey.isNil():
asymKey = some(message.pubKey[])
@ -287,16 +294,19 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} =
if not message.sourceID.isNil():
sigPrivKey = some(whisperKeys.asymKeys[$message.sourceID].seckey)
except KeyError:
warn "No key found with provided key ID."
return false
if not message.payload.isNil():
# TODO: Is this cast OK?
payload = cast[Bytes]($message.payload)
# This will make a copy
payload = @(makeOpenArray(message.payload, message.payloadLen))
else:
warn "Message payload was nil, post aborted."
return false
if not message.padding.isNil():
padding = some(cast[Bytes]($message.padding))
# This will make a copy
padding = some(@(makeOpenArray(message.padding, message.paddingLen)))
result = node.postMessage(asymKey,
symKey,
@ -350,7 +360,7 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
if msg.decoded.src.isSome():
cmsg.source = msg.decoded.src.get()
if msg.dst.isSome():
cmsg.recipientPublicKey = msg.decoded.src.get()
cmsg.recipientPublicKey = msg.dst.get()
handler(addr cmsg, udata)

View File

@ -8,6 +8,9 @@ import (
)
/*
#include <stdlib.h>
#include <stdbool.h>
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
#include "libnimbus.h"
@ -31,8 +34,8 @@ func poll() {
//export receiveHandler
func receiveHandler(msg *C.received_message, udata unsafe.Pointer) {
fmt.Printf("[nim-status] received message %s\n",
C.GoStringN((*C.char)(msg.decoded), (C.int)(msg.decodedLen)) )
receivedMsg := C.GoBytes(unsafe.Pointer(msg.decoded), C.int(msg.decodedLen))
fmt.Printf("[nim-status] received message %s\n", string(receivedMsg))
fmt.Printf("[nim-status] source public key %x\n", msg.source)
msgCount := (*int)(udata)
*msgCount += 1
@ -48,25 +51,31 @@ func Start() {
func StatusListenAndPost(channel string) {
fmt.Println("[nim-status] Status Public ListenAndPost")
// TODO: free the CStrings?
// TODO: Is this doing a copy or not? If not, shouldn't we see issues when the
// nim GC kicks in?
symKeyId := C.GoString(C.nimbus_add_symkey_from_password(C.CString(channel)))
channelC := C.CString(channel)
defer C.free(unsafe.Pointer(channelC))
symKeyId := C.GoString(C.nimbus_add_symkey_from_password(channelC))
asymKeyId := C.GoString(C.nimbus_new_keypair())
msgCount := 0
var msgCount int = 0
options := C.filter_options{symKeyID: C.CString(symKeyId),
minPow: 0.002,
topic: C.nimbus_string_to_topic(C.CString(channel)).topic}
topic: C.nimbus_channel_to_topic(channelC).topic}
filterId := C.GoString(C.nimbus_subscribe_filter(&options,
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)),
unsafe.Pointer(&msgCount)))
fmt.Printf("[nim-status] filter subscribed, id: %s\n", filterId)
postMessage := C.post_message{symKeyID: C.CString(symKeyId),
sourceID: C.CString(asymKeyId),
symKeyIdC := C.CString(symKeyId)
defer C.free(unsafe.Pointer(symKeyIdC))
asymKeyIdC := C.CString(asymKeyId)
defer C.free(unsafe.Pointer(asymKeyIdC))
postMessage := C.post_message{symKeyID: symKeyIdC,
sourceID: asymKeyIdC,
ttl: 20,
topic: C.nimbus_string_to_topic(C.CString(channel)).topic,
topic: C.nimbus_channel_to_topic(channelC).topic,
powTarget: 0.002,
powTime: 1.0}
@ -78,9 +87,13 @@ func StatusListenAndPost(channel string) {
time.Sleep(1 * time.Microsecond)
message := fmt.Sprintf("[\"~#c4\",[\"Message:%d\",\"text/plain\",\"~:public-group-user-message\",%d,%d,[\"^ \",\"~:chat-id\",\"%s\",\"~:text\",\"Message:%d\"]]]", i, t*100, t, channel, i)
if i%1000 == 0 {
fmt.Println("[nim-status] posting", message)
postMessage.payload = (C.CString(message))
C.nimbus_post(&postMessage)
fmt.Printf("[nim-status] posting msg number %d: %s\n", msgCount, message)
postMessage.payload = (*C.uint8_t)(C.CBytes([]byte(message)))
postMessage.payloadLen = (C.size_t)(len([]byte(message)))
defer C.free(unsafe.Pointer(postMessage.payload))
if C.nimbus_post(&postMessage) == false {
fmt.Println("[nim-status] message could not be added to queue")
}
}
}
}