Skip to content

Commit

Permalink
Message: Privatized alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Feb 23, 2021
1 parent 897bc02 commit cd188e8
Show file tree
Hide file tree
Showing 30 changed files with 108 additions and 115 deletions.
6 changes: 3 additions & 3 deletions admin/Admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static int sendMessage0(Dict* message, String* txid, struct Admin* adminPub, int

struct Allocator* alloc = NULL;
if (admin->currentRequest) {
alloc = admin->currentRequest->alloc;
alloc = Message_getAlloc(admin->currentRequest);
} else {
alloc = Allocator_child(admin->allocator);
}
Expand Down Expand Up @@ -395,8 +395,8 @@ static void handleRequest(Dict* messageDict,
if (String_equals(query, admin->functions[i].name)
&& (authed || !admin->functions[i].needsAuth))
{
if (checkArgs(args, &admin->functions[i], txid, message->alloc, admin)) {
admin->functions[i].call(args, admin->functions[i].context, txid, message->alloc);
if (checkArgs(args, &admin->functions[i], txid, Message_getAlloc(message), admin)) {
admin->functions[i].call(args, admin->functions[i].context, txid, Message_getAlloc(message));
}
noFunctionsCalled = false;
}
Expand Down
2 changes: 1 addition & 1 deletion benc/serialization/standard/BencMessageReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static Er_DEFUN(int64_t readInt(struct Message* msg, struct Allocator* alloc))
{
int64_t num = Er(Base10_read(msg));
if (Er(Message_epop8(msg)) != 'e') {
Er_raise(msg->alloc, "Int not terminated with 'e'");
Er_raise(Message_getAlloc(msg), "Int not terminated with 'e'");
}
Er_ret(num);
}
Expand Down
6 changes: 3 additions & 3 deletions client/AdminClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* addrIface)
Er_assert(Message_epop(msg, &source, ctx->targetAddr->addrLen));
if (Bits_memcmp(&source, ctx->targetAddr, ctx->targetAddr->addrLen)) {
Log_info(ctx->logger, "Got spurious message from [%s], expecting messages from [%s]",
Sockaddr_print(&source.addr, msg->alloc),
Sockaddr_print(ctx->targetAddr, msg->alloc));
Sockaddr_print(&source.addr, Message_getAlloc(msg)),
Sockaddr_print(ctx->targetAddr, Message_getAlloc(msg)));
// The UDP interface can't make use of an error but we'll inform anyway
return Error(INVALID);
}

// we don't yet know with which message this data belongs,
// the message alloc lives the length of the message reception.
struct Allocator* alloc = Allocator_child(msg->alloc);
struct Allocator* alloc = Allocator_child(Message_getAlloc(msg));

int origLen = msg->length;
Dict* d = NULL;
Expand Down
2 changes: 1 addition & 1 deletion crypto/Sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void Sign_signMsg(uint8_t keyPair[64], struct Message* msg, struct Random* rand)
int Sign_verifyMsg(uint8_t publicSigningKey[32], struct Message* msg)
{
if (msg->length < 64) { return -1; }
struct Allocator* alloc = Allocator_child(msg->alloc);
struct Allocator* alloc = Allocator_child(Message_getAlloc(msg));
uint8_t* buff = Allocator_malloc(alloc, msg->length);
unsigned long long ml = msg->length;
int ret = crypto_sign_ed25519_open(buff, &ml, msg->bytes, msg->length, publicSigningKey);
Expand Down
2 changes: 1 addition & 1 deletion crypto/test/CryptoAuthFuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static void delay(struct Context* ctx, struct Node* from, struct Message* msg, i
logNode(ctx, from, "DELAY %d packets (ptr:%p)", afterMsgs, (void*)msg);
struct Allocator* alloc = Allocator_child(ctx->alloc);
struct DelayedMsg* delayed = Allocator_calloc(alloc, sizeof(struct DelayedMsg), 1);
Allocator_adopt(alloc, msg->alloc);
Allocator_adopt(alloc, Message_getAlloc(msg));
delayed->msg = msg;
delayed->sendAfter = from->sendCounter + afterMsgs;
delayed->alloc = alloc;
Expand Down
4 changes: 2 additions & 2 deletions crypto/test/CryptoAuth_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ static void sendToIf1(struct Context* ctx, const char* x)
{
struct Message* msg = encryptMsg(ctx, &ctx->node2, x);
decryptMsg(ctx, msg, &ctx->node1, x, CryptoAuth_DecryptErr_NONE);
Allocator_free(msg->alloc);
Allocator_free(Message_getAlloc(msg));
}

static void sendToIf2(struct Context* ctx, const char* x)
{
struct Message* msg = encryptMsg(ctx, &ctx->node1, x);
decryptMsg(ctx, msg, &ctx->node2, x, CryptoAuth_DecryptErr_NONE);
Allocator_free(msg->alloc);
Allocator_free(Message_getAlloc(msg));
}

static void normal(enum TestCa_Config cfg)
Expand Down
8 changes: 4 additions & 4 deletions crypto/test/TestCa.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static Iface_DEFUN messagePlaintext(Message_t *msg, struct Iface* iface)
Message_t* m2 = NULL;
if (sess->s2) {
if (sess->s) {
m2 = Message_clone(msg, msg->alloc);
m2 = Message_clone(msg, Message_getAlloc(msg));
} else {
m2 = msg;
msg = NULL;
Expand Down Expand Up @@ -156,7 +156,7 @@ static Iface_DEFUN messageCiphertext(Message_t *msg, struct Iface* iface)
Message_t* m2 = NULL;
if (sess->s2) {
if (sess->s) {
m2 = Message_clone(msg, msg->alloc);
m2 = Message_clone(msg, Message_getAlloc(msg));
} else {
m2 = msg;
msg = NULL;
Expand Down Expand Up @@ -205,8 +205,8 @@ static bool check(Message_t *msg, TestCa_Session_pvt_t* sess)
msg->length, m2->length);
}
if (Bits_memcmp(msg->bytes, m2->bytes, msg->length)) {
const char* msgH = Hex_print(msg->bytes, msg->length, msg->alloc);
const char* m2H = Hex_print(m2->bytes, m2->length, m2->alloc);
const char* msgH = Hex_print(msg->bytes, msg->length, Message_getAlloc(msg));
const char* m2H = Hex_print(m2->bytes, m2->length, Message_getAlloc(m2));
Assert_failure("msg->bytes != m2->bytes:\n%s\n%s\n", msgH, m2H);
}
Assert_true(!Bits_memcmp(msg->bytes, m2->bytes, msg->length));
Expand Down
14 changes: 7 additions & 7 deletions dht/Pathfinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static Iface_DEFUN peer(struct Message* msg, struct Pathfinder_pvt* pf)
{
struct Address addr;
addressForNode(&addr, msg);
String* str = Address_toString(&addr, msg->alloc);
String* str = Address_toString(&addr, Message_getAlloc(msg));
Log_debug(pf->log, "Peer [%s]", str->bytes);

struct Node_Link* link = NodeStore_linkForPath(pf->nodeStore, addr.path);
Expand All @@ -327,7 +327,7 @@ static Iface_DEFUN peerGone(struct Message* msg, struct Pathfinder_pvt* pf)
{
struct Address addr;
addressForNode(&addr, msg);
String* str = Address_toString(&addr, msg->alloc);
String* str = Address_toString(&addr, Message_getAlloc(msg));
Log_debug(pf->log, "Peer gone [%s]", str->bytes);
NodeStore_disconnectedPeer(pf->nodeStore, addr.path);

Expand All @@ -339,7 +339,7 @@ static Iface_DEFUN session(struct Message* msg, struct Pathfinder_pvt* pf)
{
struct Address addr;
addressForNode(&addr, msg);
String* str = Address_toString(&addr, msg->alloc);
String* str = Address_toString(&addr, Message_getAlloc(msg));
Log_debug(pf->log, "Session [%s]", str->bytes);

/* This triggers for every little ping we send to some random node out there which
Expand All @@ -356,7 +356,7 @@ static Iface_DEFUN sessionEnded(struct Message* msg, struct Pathfinder_pvt* pf)
{
struct Address addr;
addressForNode(&addr, msg);
String* str = Address_toString(&addr, msg->alloc);
String* str = Address_toString(&addr, Message_getAlloc(msg));
Log_debug(pf->log, "Session ended [%s]", str->bytes);
return Error(NONE);
}
Expand All @@ -380,7 +380,7 @@ static Iface_DEFUN discoveredPath(struct Message* msg, struct Pathfinder_pvt* pf

addr.protocolVersion = nn->address.protocolVersion;

Log_debug(pf->log, "Discovered path [%s]", Address_toString(&addr, msg->alloc)->bytes);
Log_debug(pf->log, "Discovered path [%s]", Address_toString(&addr, Message_getAlloc(msg))->bytes);
RumorMill_addNode(pf->rumorMill, &addr);
return Error(NONE);
}
Expand Down Expand Up @@ -414,12 +414,12 @@ static Iface_DEFUN incomingMsg(struct Message* msg, struct Pathfinder_pvt* pf)
struct DHTMessage dht = {
.address = &addr,
.binMessage = msg,
.allocator = msg->alloc
.allocator = Message_getAlloc(msg)
};

DHTModuleRegistry_handleIncoming(&dht, pf->registry);

struct Message* nodeMsg = Message_new(0, 256, msg->alloc);
struct Message* nodeMsg = Message_new(0, 256, Message_getAlloc(msg));
Iface_CALL(sendNode, nodeMsg, &addr, Metric_DHT_INCOMING, pf);

if (dht.pleaseRespond) {
Expand Down
4 changes: 2 additions & 2 deletions interface/ASynchronizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static Iface_DEFUN fromA(struct Message* msg, struct Iface* ifA)
struct ASynchronizer_pvt* as = Identity_containerOf(ifA, struct ASynchronizer_pvt, pub.ifA);
if (!as->cycleAlloc) { as->cycleAlloc = Allocator_child(as->alloc); }
if (!as->msgsToB) { as->msgsToB = ArrayList_Messages_new(as->cycleAlloc); }
Allocator_adopt(as->cycleAlloc, msg->alloc);
Allocator_adopt(as->cycleAlloc, Message_getAlloc(msg));
ArrayList_Messages_add(as->msgsToB, msg);
checkTimeout(as);
return Error(NONE);
Expand All @@ -109,7 +109,7 @@ static Iface_DEFUN fromB(struct Message* msg, struct Iface* ifB)
struct ASynchronizer_pvt* as = Identity_containerOf(ifB, struct ASynchronizer_pvt, pub.ifB);
if (!as->cycleAlloc) { as->cycleAlloc = Allocator_child(as->alloc); }
if (!as->msgsToA) { as->msgsToA = ArrayList_Messages_new(as->cycleAlloc); }
Allocator_adopt(as->cycleAlloc, msg->alloc);
Allocator_adopt(as->cycleAlloc, Message_getAlloc(msg));
ArrayList_Messages_add(as->msgsToA, msg);
checkTimeout(as);
return Error(NONE);
Expand Down
10 changes: 3 additions & 7 deletions interface/FramingIface.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)
return Error(NONE);
}
fi->bytesRemaining -= msg->length;
Allocator_adopt(fi->frameAlloc, msg->alloc);
Allocator_adopt(fi->frameAlloc, Message_getAlloc(msg));
struct MessageList* parts = Allocator_calloc(fi->frameAlloc, sizeof(struct MessageList), 1);
parts->msg = msg;
parts->next = fi->frameParts;
Expand Down Expand Up @@ -129,7 +129,7 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)
return Iface_next(&fi->messageIf, msg);

} else if (fi->bytesRemaining < (uint32_t)msg->length) {
struct Allocator* alloc = Allocator_child(msg->alloc);
struct Allocator* alloc = Allocator_child(Message_getAlloc(msg));
struct Message* m = Message_new(fi->bytesRemaining, REQUIRED_PADDING, alloc);
Message_setAssociatedFd(m, Message_getAssociatedFd(msg));
Bits_memcpy(m->bytes, msg->bytes, fi->bytesRemaining);
Expand All @@ -141,12 +141,8 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)

} else {
fi->frameAlloc = Allocator_child(fi->alloc);
struct Message* m = Allocator_calloc(fi->frameAlloc, sizeof(struct Message), 1);
struct Message* m = Message_new(0, msg->length + 4, fi->frameAlloc);
Message_setAssociatedFd(m, Message_getAssociatedFd(msg));
m->capacity = m->length = msg->length + 4;
m->bytes = Allocator_calloc(fi->frameAlloc, m->length, 1);
m->alloc = fi->frameAlloc;
Er_assert(Message_eshift(m, -m->length));
Er_assert(Message_epush(m, msg->bytes, msg->length));
Er_assert(Message_epush(m, fi->header.bytes, 4));

Expand Down
2 changes: 1 addition & 1 deletion interface/addressable/PacketHeaderToUDPAddrIface.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static Iface_DEFUN incomingFromHeaderIf(struct Message* message, struct Iface* i
return Error(INVALID);
}

struct Allocator* alloc = Allocator_child(message->alloc);
struct Allocator* alloc = Allocator_child(Message_getAlloc(message));
struct Sockaddr* addr = Sockaddr_clone(context->pub.udpIf.addr, alloc);
uint8_t* addrPtr = NULL;
Assert_true(Sockaddr_getAddress(addr, &addrPtr) == 16);
Expand Down
4 changes: 2 additions & 2 deletions interface/tuntap/windows/TAPInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static void readCallbackB(struct TAPInterface_pvt* tap)
msg->length = bytesRead;
Log_debug(tap->log, "Read [%d] bytes", msg->length);
Iface_send(&tap->pub.generic, msg);
Allocator_free(msg->alloc);
Allocator_free(Message_getAlloc(msg));
postRead(tap);
}

Expand Down Expand Up @@ -235,7 +235,7 @@ static Iface_DEFUN sendMessage(struct Message* msg, struct Iface* iface)
tap->pendingWritesAlloc = Allocator_child(tap->alloc);
}
tap->writeMsgs[tap->writeMessageCount++] = msg;
Allocator_adopt(tap->pendingWritesAlloc, msg->alloc);
Allocator_adopt(tap->pendingWritesAlloc, Message_getAlloc(msg));
if (tap->writeMessageCount == 1) {
postWrite(tap);
}
Expand Down
4 changes: 2 additions & 2 deletions net/ControlHandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ static Iface_DEFUN changeSnode(struct Message* msg, struct Iface* eventIf)
Bits_memcpy(addr.key, node.publicKey, 32);

Log_debug(ch->log, "%s [%s] -> [%s]", log,
Address_toStringKey(&old, msg->alloc)->bytes,
Address_toStringKey(&addr, msg->alloc)->bytes);
Address_toStringKey(&old, Message_getAlloc(msg))->bytes,
Address_toStringKey(&addr, Message_getAlloc(msg))->bytes);

return Error(NONE);
}
Expand Down
16 changes: 8 additions & 8 deletions net/EventEmitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static Iface_DEFUN sendToPathfinder(struct Message* msg, struct Pathfinder* pf)
{
if (!pf || pf->state != Pathfinder_state_CONNECTED) { return Error(NONE); }
if (pf->bytesSinceLastPing < 8192 && pf->bytesSinceLastPing + msg->length >= 8192) {
struct Message* ping = Message_new(0, 512, msg->alloc);
struct Message* ping = Message_new(0, 512, Message_getAlloc(msg));
Er_assert(Message_epush32be(ping, pf->bytesSinceLastPing));
Er_assert(Message_epush32be(ping, PING_MAGIC));
Er_assert(Message_epush32be(ping, PFChan_Core_PING));
Expand Down Expand Up @@ -188,7 +188,7 @@ static Iface_DEFUN incomingFromCore(struct Message* msg, struct Iface* trickIf)
for (int i = 0; i < ee->pathfinders->length; i++) {
struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, i);
if (!pf || pf->state != Pathfinder_state_CONNECTED) { continue; }
struct Message* messageClone = Message_clone(msg, msg->alloc);
struct Message* messageClone = Message_clone(msg, Message_getAlloc(msg));
Iface_CALL(sendToPathfinder, messageClone, pf);
}
}
Expand Down Expand Up @@ -232,20 +232,20 @@ static int handleFromPathfinder(enum PFChan_Pathfinder ev,
Bits_memcpy(resp.publicKey, ee->publicKey, 32);
Er_assert(Message_epush(msg, &resp, PFChan_Core_Connect_SIZE));
Er_assert(Message_epush32be(msg, PFChan_Core_CONNECT));
struct Message* sendMsg = Message_clone(msg, msg->alloc);
struct Message* sendMsg = Message_clone(msg, Message_getAlloc(msg));
Iface_CALL(sendToPathfinder, sendMsg, pf);
break;
}
case PFChan_Pathfinder_SUPERIORITY: {
Er_assert(Message_eshift(msg, -8));
pf->superiority = Er_assert(Message_epop32be(msg));
struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, msg->alloc);
struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, Message_getAlloc(msg));
Iface_CALL(incomingFromCore, resp, &ee->trickIf);
break;
}

case PFChan_Pathfinder_PING: {
struct Message* sendMsg = Message_clone(msg, msg->alloc);
struct Message* sendMsg = Message_clone(msg, Message_getAlloc(msg));
Iface_send(&pf->iface, sendMsg);
break;
}
Expand All @@ -255,7 +255,7 @@ static int handleFromPathfinder(enum PFChan_Pathfinder ev,
uint32_t count = Er_assert(Message_epop32be(msg));
if (cookie != PING_MAGIC || count > pf->bytesSinceLastPing) {
pf->state = Pathfinder_state_ERROR;
struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER_GONE, pf, msg->alloc);
struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER_GONE, pf, Message_getAlloc(msg));
Iface_CALL(incomingFromCore, resp, &ee->trickIf);
} else {
pf->bytesSinceLastPing -= count;
Expand All @@ -266,7 +266,7 @@ static int handleFromPathfinder(enum PFChan_Pathfinder ev,
for (int i = 0; i < ee->pathfinders->length; i++) {
struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i);
if (!xpf || xpf->state != Pathfinder_state_CONNECTED) { continue; }
struct Allocator* alloc = Allocator_child(msg->alloc);
struct Allocator* alloc = Allocator_child(Message_getAlloc(msg));
struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, alloc);
Iface_CALL(sendToPathfinder, resp, pf);
Allocator_free(alloc);
Expand Down Expand Up @@ -312,7 +312,7 @@ static Iface_DEFUN incomingFromPathfinder(struct Message* msg, struct Iface* ifa
struct ArrayList_Ifaces* handlers = getHandlers(ee, ev, false);
if (!handlers) { return Error(NONE); }
for (int i = 0; i < handlers->length; i++) {
struct Message* messageClone = Message_clone(msg, msg->alloc);
struct Message* messageClone = Message_clone(msg, Message_getAlloc(msg));
struct Iface* iface = ArrayList_Ifaces_get(handlers, i);
// We have to call this manually because we don't have an interface handy which is
// actually plumbed with this one.
Expand Down
Loading

0 comments on commit cd188e8

Please sign in to comment.