0
0
mirror of https://github.com/cjdelisle/cjdns synced 2025-10-06 00:32:50 +02:00

Renamed some more struct Message to Message_t and got rid of Message_foreign which creates a Message from an outside data buffer.

This commit is contained in:
Caleb James DeLisle
2024-04-25 23:32:34 +02:00
parent bc5f04a15b
commit e60257daa0
25 changed files with 126 additions and 101 deletions

View File

@@ -31,7 +31,7 @@ Linker_require("admin/angel/InterfaceWaiter.c")
* @param eh an exception handler in case something goes wrong.
* @return the message.
*/
struct Message* InterfaceWaiter_waitForData(struct Iface* iface,
Message_t* InterfaceWaiter_waitForData(struct Iface* iface,
EventBase_t* eventBase,
struct Allocator* alloc,
struct Except* eh);

View File

@@ -25,11 +25,11 @@ Linker_require("benc/serialization/json/JsonBencMessageReader.c")
#include <stdbool.h>
Er_DEFUN(Dict* JsonBencMessageReader_read(
struct Message* msg,
Message_t* msg,
struct Allocator* alloc,
bool lax
));
const char* JsonBencMessageReader_readNoExcept(
struct Message* msg, struct Allocator* alloc, Dict** outPtr, bool lax);
Message_t* msg, struct Allocator* alloc, Dict** outPtr, bool lax);
#endif

View File

@@ -22,9 +22,9 @@
#include "util/Linker.h"
Linker_require("benc/serialization/standard/BencMessageReader.c")
Er_DEFUN(Dict* BencMessageReader_read(struct Message* msg, struct Allocator* alloc));
Er_DEFUN(Dict* BencMessageReader_read(Message_t* msg, struct Allocator* alloc));
const char* BencMessageReader_readNoExcept(
struct Message* msg, struct Allocator* alloc, Dict** outPtr);
Message_t* msg, struct Allocator* alloc, Dict** outPtr);
#endif

View File

@@ -21,6 +21,6 @@
#include "util/Linker.h"
Linker_require("benc/serialization/standard/BencMessageWriter.c")
Er_DEFUN(void BencMessageWriter_write(Dict* toWrite, struct Message* msg));
Er_DEFUN(void BencMessageWriter_write(Dict* toWrite, Message_t* msg));
#endif

View File

@@ -133,7 +133,7 @@ struct CryptoAuth_Session* CryptoAuth_newSession(struct CryptoAuth* ca,
bool useNoise);
/** @return 0 on success, -1 otherwise. */ // Now only used in unit tests on Rust side
int CryptoAuth_encrypt(struct CryptoAuth_Session* session, struct Message* msg);
int CryptoAuth_encrypt(struct CryptoAuth_Session* session, Message_t* msg);
enum CryptoAuth_DecryptErr {
CryptoAuth_DecryptErr_NONE = 0,
@@ -185,7 +185,7 @@ enum CryptoAuth_DecryptErr {
// returns 0 if everything if ok, otherwise an encryption error.
// If there is an error, the content of the message MIGHT already be decrypted !
// Now only used in unit tests on Rust side
enum CryptoAuth_DecryptErr CryptoAuth_decrypt(struct CryptoAuth_Session* sess, struct Message* msg);
enum CryptoAuth_DecryptErr CryptoAuth_decrypt(struct CryptoAuth_Session* sess, Message_t* msg);
/**
* Choose the authentication credentials to use.

View File

@@ -135,14 +135,14 @@ struct CryptoAuth_Session_pvt
};
//uint8_t CryptoAuth_receiveMessage(struct Message* received, struct Iface* interface);
//uint8_t CryptoAuth_receiveMessage(Message_t* received, struct Iface* interface);
//uint8_t CryptoAuth_encryptHandshake(struct Message* message,
//uint8_t CryptoAuth_encryptHandshake(Message_t* message,
// struct CryptoAuth_Wrapper* wrapper,
// int setupMessage);
int CryptoAuth_decryptRndNonce(const uint8_t nonce[24], struct Message* msg, const uint8_t secret[32]);
int CryptoAuth_decryptRndNonce(const uint8_t nonce[24], Message_t* msg, const uint8_t secret[32]);
void CryptoAuth_encryptRndNonce(const uint8_t nonce[24], struct Message* msg, const uint8_t secret[32]);
void CryptoAuth_encryptRndNonce(const uint8_t nonce[24], Message_t* msg, const uint8_t secret[32]);
#endif

View File

@@ -23,10 +23,10 @@ Linker_require("crypto/Sign.c")
void Sign_signingKeyPairFromCurve25519(uint8_t keypairOut[64], uint8_t secretCryptoKey[32]);
/** Pushes 64 bit sig to message. */
void Sign_signMsg(uint8_t keyPair[64], struct Message* msg, struct Random* rand);
void Sign_signMsg(uint8_t keyPair[64], Message_t* msg, struct Random* rand);
/** returns 0 and pops sig if signature check passes, zeros message content if it fails! */
int Sign_verifyMsg(uint8_t publicSigningKey[32], struct Message* msg);
int Sign_verifyMsg(uint8_t publicSigningKey[32], Message_t* msg);
int Sign_publicSigningKeyToCurve25519(uint8_t curve25519keyOut[32], uint8_t publicSigningKey[32]);

View File

@@ -23,6 +23,6 @@
Linker_require("crypto/test/CryptoAuthFuzz.c")
void* CryptoAuthFuzz_init(struct Allocator* alloc, struct Random* rand, enum TestCa_Config cfg);
void CryptoAuthFuzz_main(void* vctx, struct Message* fuzz);
void CryptoAuthFuzz_main(void* vctx, Message_t* fuzz);
#endif

View File

@@ -13,6 +13,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "crypto/CryptoAuth_pvt.h"
#include "memory/Allocator.h"
#include "util/Hex.h"
#include "util/Bits.h"
@@ -23,31 +24,32 @@
static void encryptRndNonceTest()
{
uint8_t buff[44];
Bits_memset(buff, 0, 44);
uint8_t nonce[24];
Bits_memset(nonce, 0, 24);
uint8_t secret[32];
Bits_memset(secret, 0, 32);
struct Message m = Message_foreign(44, buff);
Er_assert(Message_epop(&m, NULL, 44));
Er_assert(Message_epush(&m, HELLOWORLDLOWER, CString_strlen(HELLOWORLDLOWER)+1));
Allocator_t* alloc = Allocator_new(1<<14);
Message_t* m = Message_new(0, 512, alloc);
Er_assert(Message_epush(m, NULL, 44));
Er_assert(Message_epop(m, NULL, 44));
Er_assert(Message_epush(m, HELLOWORLDLOWER, CString_strlen(HELLOWORLDLOWER)+1));
CryptoAuth_encryptRndNonce(nonce, &m, secret);
CryptoAuth_encryptRndNonce(nonce, m, secret);
uint8_t* expected = (uint8_t*) "1391ac5d03ba9f7099bffbb6e6c69d67ae5bd79391a5b94399b293dc";
uint8_t output[57];
Hex_encode(output, 57, Message_bytes(&m), Message_getLength(&m));
Hex_encode(output, 57, Message_bytes(m), Message_getLength(m));
printf("\n%s\n%s\n", (char*) expected, (char*) output);
Assert_true(!Bits_memcmp(expected, output, 56));
Assert_true(!CryptoAuth_decryptRndNonce(nonce, &m, secret));
Assert_true(Message_getLength(&m) == HELLOWORLDLEN
&& !Bits_memcmp(Message_bytes(&m), HELLOWORLDLOWER, Message_getLength(&m)));
Assert_true(!CryptoAuth_decryptRndNonce(nonce, m, secret));
Assert_true(Message_getLength(m) == HELLOWORLDLEN
&& !Bits_memcmp(Message_bytes(m), HELLOWORLDLOWER, Message_getLength(m)));
Allocator_free(alloc);
}
int main()

View File

@@ -20,6 +20,7 @@
#endif
#include "benc/Dict.h"
#include "wire/Message.h"
/**
* Maximum number of bytes in a message.
@@ -39,7 +40,7 @@ struct DHTMessage
{
struct Address* address;
struct Message* binMessage;
Message_t* binMessage;
/** The message as a bencoded dictionary. */
Dict* asDict;

View File

@@ -30,7 +30,7 @@ struct Iface;
* @param thisInterface the interface which contains the sendMessage function pointer.
* @param message the message
*/
typedef struct RTypes_Error_t* (* Iface_Callback)(struct Message* message, struct Iface* thisInterface);
typedef struct RTypes_Error_t* (* Iface_Callback)(Message_t* message, struct Iface* thisInterface);
#define Iface_DEFUN __attribute__ ((warn_unused_result)) struct RTypes_Error_t*
@@ -41,7 +41,7 @@ struct Iface
#ifdef PARANOIA
/** This is for checking currentMsg Iface_next() has not been called incorrectly. */
struct Message* currentMsg;
Message_t* currentMsg;
#endif
/** Interface to which this one is connected (if connected) */
@@ -51,7 +51,7 @@ struct Iface
} /* Iface_t defined in RffiPrefix.h */;
// This needs to be in a C file in order to be accessible from Rust
Iface_DEFUN Iface_incomingFromRust(struct Message* message, struct Iface* thisInterface);
Iface_DEFUN Iface_incomingFromRust(Message_t* message, struct Iface* thisInterface);
void Iface_setIdentity(struct Iface* iface);
void Iface_checkIdentity(struct Iface* iface);
@@ -64,7 +64,7 @@ void Iface_checkIdentity(struct Iface* iface);
* assertion error, in order to forward the message which you received, you must use Iface_next()
* and it must be a tail-call.
*/
static inline struct RTypes_Error_t* Iface_send(struct Iface* iface, struct Message* msg)
static inline struct RTypes_Error_t* Iface_send(struct Iface* iface, Message_t* msg)
{
Iface_checkIdentity(iface);
struct Iface* conn = iface->connectedIf;
@@ -74,7 +74,7 @@ static inline struct RTypes_Error_t* Iface_send(struct Iface* iface, struct Mess
Assert_true(conn);
Assert_true(conn->send);
Assert_true(msg);
struct Message* currentMsg = conn->currentMsg;
Message_t* currentMsg = conn->currentMsg;
msg->currentIface = conn;
conn->currentMsg = msg;
#endif
@@ -92,11 +92,11 @@ static inline struct RTypes_Error_t* Iface_send(struct Iface* iface, struct Mess
/**
* Forward a message from inside of an Iface_Callback function.
*/
static inline Iface_DEFUN Iface_next(struct Iface* iface, struct Message* msg)
static inline Iface_DEFUN Iface_next(struct Iface* iface, Message_t* msg)
{
#ifdef PARANOIA
struct Iface* conn = iface->connectedIf;
struct Message* currentMsg = conn->currentMsg;
Message_t* currentMsg = conn->currentMsg;
Assert_true(msg->currentIface);
Assert_true(msg->currentIface->currentMsg == msg);
msg->currentIface->currentMsg = NULL;

View File

@@ -40,13 +40,13 @@ typedef struct AddrIface
struct Allocator* alloc;
} AddrIface_t;
static inline Er_DEFUN(void AddrIface_pushAddr(struct Message* msg, const struct Sockaddr* addr))
static inline Er_DEFUN(void AddrIface_pushAddr(Message_t* msg, const struct Sockaddr* addr))
{
Er(Message_epush(msg, addr, addr->addrLen));
Er_ret();
}
static inline Er_DEFUN(struct Sockaddr* AddrIface_popAddr(struct Message* msg))
static inline Er_DEFUN(struct Sockaddr* AddrIface_popAddr(Message_t* msg))
{
struct Sockaddr* out = (struct Sockaddr*) Message_bytes(msg);
uint16_t len = Er(Message_epop16h(msg));

View File

@@ -18,7 +18,7 @@
#include "util/Defined.h"
#include "wire/Message.h"
static inline Er_DEFUN(void TUNMessageType_push(struct Message* message,
static inline Er_DEFUN(void TUNMessageType_push(Message_t* message,
uint16_t ethertype))
{
Er(Message_eshift(message, 4));
@@ -27,7 +27,7 @@ static inline Er_DEFUN(void TUNMessageType_push(struct Message* message,
Er_ret();
}
static inline Er_DEFUN(uint16_t TUNMessageType_pop(struct Message* message))
static inline Er_DEFUN(uint16_t TUNMessageType_pop(Message_t* message))
{
Er(Message_eshift(message, -4));
Er_ret( ((uint16_t*) Message_bytes(message))[-1] );

View File

@@ -25,9 +25,9 @@ Linker_require("interface/tuntap/test/TUNTools.c")
struct TUNTools;
typedef Iface_DEFUN (* TUNTools_Callback)(struct Message* msg, struct TUNTools* tt);
typedef Iface_DEFUN (* TUNTools_Callback)(Message_t* msg, struct TUNTools* tt);
Iface_DEFUN TUNTools_genericIP6Echo(struct Message* msg, struct TUNTools* tt);
Iface_DEFUN TUNTools_genericIP6Echo(Message_t* msg, struct TUNTools* tt);
extern const uint8_t* TUNTools_testIP6AddrA;
extern const uint8_t* TUNTools_testIP6AddrB;

View File

@@ -224,6 +224,8 @@ void Rffi_glock(void);
void Rffi_gunlock(void);
int Rffi_parseBase10(const uint8_t *buf, uint32_t max_len, int64_t *num_out, uint32_t *bytes);
/**
* Create a root level allocator.
*/

View File

@@ -0,0 +1,48 @@
#[no_mangle]
pub extern "C" fn Rffi_parseBase10(
buf: *const u8,
max_len: u32,
num_out: *mut i64,
bytes: *mut u32,
) -> libc::c_int {
// Safety: Ensure that buf is valid and not null
if buf.is_null() {
return -1; // Indicates error: null pointer
}
// Safety: Ensure that num_out is valid and not null
if num_out.is_null() {
return -2; // Indicates error: null pointer
}
// Convert the buffer into a slice
let buf_slice = unsafe { std::slice::from_raw_parts(buf, max_len as usize) };
let mut limit = buf_slice.len();
for (i, &c) in buf_slice.iter().enumerate() {
if c != b'-' && (c < b'0' || c > b'9') {
limit = i;
break;
}
}
// Convert the slice into a string
let buf_str = match std::str::from_utf8(&buf_slice[0..limit]) {
Ok(s) => s,
Err(_) => return -3, // Indicates error: invalid UTF-8 sequence
};
// Parse the string into an i64
let parsed_num = match buf_str.parse::<i64>() {
Ok(n) => n,
Err(_) => return -4, // Indicates error: unable to parse as i64
};
// Assign the parsed number to num_out
unsafe {
*num_out = parsed_num;
*bytes = limit as _;
};
0 // Indicates success
}

View File

@@ -11,6 +11,7 @@ mod network;
mod time;
mod util;
mod glock;
mod base10;
pub mod allocator;
use crate::bytestring::ByteString;

View File

@@ -35,7 +35,7 @@ struct LinkState {
};
static inline int LinkState_encode(
struct Message* msg,
Message_t* msg,
struct LinkState* ls,
uint32_t lastSamples)
{
@@ -86,7 +86,7 @@ static inline int LinkState_encode(
return 0;
}
static inline int LinkState_mkDecoder(struct Message* msg, struct VarInt_Iter* it)
static inline int LinkState_mkDecoder(Message_t* msg, struct VarInt_Iter* it)
{
if (!Message_getLength(msg)) { return 1; }
uint8_t len = Message_bytes(msg)[0];

View File

@@ -13,6 +13,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "crypto/random/Random.h"
#include "exception/Er.h"
#include "memory/Allocator.h"
#include "subnode/LinkState.h"
#include "util/Assert.h"
@@ -110,7 +111,7 @@ static void assertSame(struct LinkState* beforeState,
Assert_true(!Bits_memcmp(&local, updated, sizeof(struct LinkState)));
}
static void testStatic()
static void testStatic(Allocator_t* alloc)
{
uint8_t* hex =
"\x20\x03\x06\x00\x00\x00\x00\x00\x00"
@@ -124,8 +125,9 @@ static void testStatic()
"\x13\x00\x01";
struct LinkState ls = { .nodeId = 0 };
struct VarInt_Iter it = { .start = 0 };
struct Message msg = Message_foreign(32, hex);
Assert_true(!LinkState_mkDecoder(&msg, &it));
Message_t* msg = Message_new(0, 512, alloc);
Er_assert(Message_epush(msg, hex, 32));
Assert_true(!LinkState_mkDecoder(msg, &it));
Assert_true(!LinkState_decode(&it, &ls));
// printf("%u %u\n", ls.nodeId, ls.samples);
// for (int i = 0; i < LinkState_SLOTS; i++) {
@@ -144,8 +146,8 @@ static void testStatic()
int main(int argc, char* argv[])
{
testStatic();
struct Allocator* mainAlloc = Allocator_new(1<<18);
testStatic(mainAlloc);
struct Random* rand = Random_new(mainAlloc, NULL, NULL);
int cycles = CYCLES;

View File

@@ -19,7 +19,7 @@
#include "crypto/random/Random.h"
#include "wire/Message.h"
void CJDNS_FUZZ_MAIN(void* vctx, struct Message* fuzz);
void CJDNS_FUZZ_MAIN(void* vctx, Message_t* fuzz);
void* CJDNS_FUZZ_INIT(struct Allocator* alloc, struct Random* rand);
#endif

View File

@@ -37,13 +37,13 @@ struct TestFramework
struct EncodingScheme* scheme;
/** The last message which this node sent. */
struct Message* lastMsg;
Message_t* lastMsg;
/**
* A backup of the last message which this node sent.
* Used to check if the framework alters the message after sending it.
*/
struct Message* lastMsgBackup;
Message_t* lastMsgBackup;
uint8_t* publicKey;
uint8_t* ip;
@@ -65,7 +65,7 @@ void TestFramework_linkNodes(struct TestFramework* client,
struct TestFramework* server,
bool beacon);
void TestFramework_craftIPHeader(struct Message* msg, uint8_t srcAddr[16], uint8_t destAddr[16]);
void TestFramework_craftIPHeader(Message_t* msg, uint8_t srcAddr[16], uint8_t destAddr[16]);
/** Check if the last message sent was altered after having been sent. */
void TestFramework_assertLastMessageUnaltered(struct TestFramework* tf);

View File

@@ -17,6 +17,7 @@
#include "wire/Message.h"
#include "exception/Except.h"
#include "util/CString.h"
#include "rust/cjdns_sys/Rffi.h"
#include <stdbool.h>
@@ -41,45 +42,23 @@ Er_DEFUN(void Base10_write(Message_t* msg, int64_t num))
Er_DEFUN(int64_t Base10_read(Message_t* msg))
{
int64_t out = 0;
bool negative = false;
uint8_t chr = Er(Message_epop8(msg));
if (chr == '-') {
negative = true;
chr = Er(Message_epop8(msg));
}
if (chr >= '0' && chr <= '9') {
while (chr >= '0' && chr <= '9') {
out *= 10;
out += chr - '0';
if (Message_getLength(msg) == 0) {
if (negative) { out = -out; }
Er_ret(out);
}
chr = Er(Message_epop8(msg));
}
Er(Message_epush8(msg, chr));
if (negative) { out = -out; }
Er_ret(out);
} else {
Er(Message_epush8(msg, chr));
Er_raise(Message_getAlloc(msg), "No base10 characters found");
int64_t numOut = 0;
uint32_t bytes = 0;
int out = Rffi_parseBase10(
Message_bytes(msg), Message_getLength(msg), &numOut, &bytes);
if (out != 0) {
Er_raise(Message_getAlloc(msg), "Error reading base10: %d", out);
}
Er(Message_eshift(msg, -bytes));
Er_ret(numOut);
}
int Base10_fromString(uint8_t* str, int64_t* numOut)
{
int len = CString_strlen(str);
int len = CString_strlen((char*)str);
if (len < 1) {
return -1;
} else if (str[0] == '-') {
if (len < 2 || str[1] < '0' || str[1] > '9') {
return -1;
}
} else if (str[0] < '0' || str[0] > '9') {
return -1;
}
struct Message msg = Message_foreign(len, str);
*numOut = Er_assert(Base10_read(&msg));
return 0;
uint32_t bytes = 0;
return Rffi_parseBase10(str, len, numOut, &bytes);
}

View File

@@ -22,8 +22,8 @@ Linker_require("util/Base10.c")
#include <stdint.h>
Er_DEFUN(void Base10_write(struct Message* msg, int64_t num));
Er_DEFUN(int64_t Base10_read(struct Message* msg));
Er_DEFUN(void Base10_write(Message_t* msg, int64_t num));
Er_DEFUN(int64_t Base10_read(Message_t* msg));
int Base10_fromString(uint8_t* str, int64_t* numOut);
#endif

View File

@@ -71,7 +71,7 @@ struct Announce_EncodingScheme
uint8_t scheme[2];
};
static inline void Announce_EncodingScheme_push(struct Message* pushTo, String* compressedScheme)
static inline void Announce_EncodingScheme_push(Message_t* pushTo, String* compressedScheme)
{
Assert_true(compressedScheme->len + 2 < 256);
Er_assert(Message_epush(pushTo, compressedScheme->bytes, compressedScheme->len));
@@ -173,7 +173,7 @@ struct Announce_LinkState
uint8_t linkState[1];
};
static inline void Announce_LinkState_applyHeader(struct Message* pushTo)
static inline void Announce_LinkState_applyHeader(Message_t* pushTo)
{
Assert_failure("todo implement");
}
@@ -327,7 +327,7 @@ static inline void Announce_Header_setVersion(struct Announce_Header* hdr, int v
Bits_memcpy(hdr->timeStampVersionFlags_be, &ts_be, sizeof(uint64_t));
}
static inline struct Announce_ItemHeader* Announce_ItemHeader_next(struct Message* msg, void* last)
static inline struct Announce_ItemHeader* Announce_ItemHeader_next(Message_t* msg, void* last)
{
struct Announce_ItemHeader* ih = (struct Announce_ItemHeader*) last;
if (ih) {
@@ -395,7 +395,7 @@ static inline bool Announce_ItemHeader_doesReplace(
}
static inline struct Announce_ItemHeader* Announce_itemInMessage(
struct Message* msg,
Message_t* msg,
struct Announce_ItemHeader* ref)
{
struct Announce_ItemHeader* ih = NULL;
@@ -405,7 +405,7 @@ static inline struct Announce_ItemHeader* Announce_itemInMessage(
return ih;
}
static inline bool Announce_isValid(struct Message* msg)
static inline bool Announce_isValid(Message_t* msg)
{
struct Announce_ItemHeader* ih = NULL;
for (;;) {
@@ -415,7 +415,7 @@ static inline bool Announce_isValid(struct Message* msg)
}
}
static inline struct Announce_Peer* Announce_Peer_next(struct Message* msg, void* last)
static inline struct Announce_Peer* Announce_Peer_next(Message_t* msg, void* last)
{
struct Announce_ItemHeader* ih = (struct Announce_ItemHeader*) last;
do {

View File

@@ -94,16 +94,6 @@ static inline int32_t Message_getCapacity(struct Message* msg)
return msg->_capacity;
}
static inline Message_t Message_foreign(uint32_t len, uint8_t* bytes)
{
return (Message_t){ ._length = len, ._msgbytes = bytes, ._capacity = len };
}
// static inline Er_DEFUN(void Message_ecopy(struct Message* to, struct Message* from, uint32_t amt))
// {
// }
struct Message* Message_new(uint32_t messageLength,
uint32_t amountOfPadding,
struct Allocator* alloc);