Skip to content

Commit

Permalink
Iface_DEFUN return an error type
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Nov 17, 2020
1 parent 56aa2fc commit 3cf79da
Show file tree
Hide file tree
Showing 46 changed files with 328 additions and 320 deletions.
21 changes: 12 additions & 9 deletions admin/Admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,27 @@ struct Admin_pvt
Identity
};

static void sendMessage(struct Message* message, struct Sockaddr* dest, struct Admin_pvt* admin)
static struct Error_s sendMessage(
struct Message* message, struct Sockaddr* dest, struct Admin_pvt* admin)
{
// stack overflow when used with admin logger.
//Log_keys(admin->logger, "sending message to angel [%s]", message->bytes);
Er_assert(Message_epush(message, dest, dest->addrLen));
Iface_send(&admin->iface, message);
return Iface_send(&admin->iface, message);
}

static void sendBenc(Dict* message,
struct Sockaddr* dest,
struct Allocator* alloc,
struct Admin_pvt* admin,
int fd)
static struct Error_s sendBenc(Dict* message,
struct Sockaddr* dest,
struct Allocator* alloc,
struct Admin_pvt* admin,
int fd)
{
Message_reset(admin->tempSendMsg);
Er_assert(BencMessageWriter_write(message, admin->tempSendMsg));
struct Message* msg = Message_new(0, admin->tempSendMsg->length + 32, alloc);
Er_assert(Message_epush(msg, admin->tempSendMsg->bytes, admin->tempSendMsg->length));
Message_setAssociatedFd(msg, fd);
sendMessage(msg, dest, admin);
return sendMessage(msg, dest, admin);
}

/**
Expand Down Expand Up @@ -473,7 +474,9 @@ static Iface_DEFUN receiveMessage(struct Message* message, struct Iface* iface)

admin->currentRequest = NULL;
Allocator_free(alloc);
return NULL;
// We don't return errors here because the caller can't make use of them
// instead we reply with anything which went wrong.
return Error(NONE);
}

void Admin_registerFunctionWithArgCount(char* name,
Expand Down
4 changes: 2 additions & 2 deletions admin/angel/InterfaceWaiter.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ static void timeout(void* vcontext)
static Iface_DEFUN receiveMessage(struct Message* message, struct Iface* iface)
{
struct Context* ctx = Identity_check((struct Context*) iface);
if (ctx->messageReceived) { return 0; }
if (ctx->messageReceived) { return Error(NONE); }
ctx->message = Message_clone(message, ctx->alloc);

Timeout_clearTimeout(ctx->timeout);
EventBase_endLoop(ctx->eventBase);

return 0;
return Error(NONE);
}

struct Message* InterfaceWaiter_waitForData(struct Iface* iface,
Expand Down
11 changes: 6 additions & 5 deletions client/AdminClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* addrIface)
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));
return NULL;
// 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,
Expand All @@ -133,17 +134,17 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* addrIface)
int origLen = msg->length;
Dict* d = NULL;
const char* err = BencMessageReader_readNoExcept(msg, alloc, &d);
if (err) { return NULL; }
if (err) { return Error(INVALID); }
Er_assert(Message_eshift(msg, origLen));

String* txid = Dict_getStringC(d, "txid");
if (!txid || txid->len != 8) { return NULL; }
if (!txid || txid->len != 8) { return Error(INVALID); }

// look up the result
uint32_t handle = ~0u;
Hex_decode((uint8_t*)&handle, 4, txid->bytes, 8);
int idx = Map_OfRequestByHandle_indexForHandle(handle, &ctx->outstandingRequests);
if (idx < 0) { return NULL; }
if (idx < 0) { return Error(INVALID); }

struct Request* req = ctx->outstandingRequests.values[idx];

Expand All @@ -157,7 +158,7 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* addrIface)
Bits_memset(req->res.messageBytes, 0, AdminClient_MAX_MESSAGE_SIZE);
Bits_memcpy(req->res.messageBytes, msg->bytes, len);
done(req, AdminClient_Error_NONE);
return NULL;
return Error(NONE);
}

static int requestOnFree(struct Allocator_OnFreeJob* job)
Expand Down
28 changes: 14 additions & 14 deletions dht/Pathfinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ static Iface_DEFUN connected(struct Pathfinder_pvt* pf, struct Message* msg)

pf->state = Pathfinder_pvt_state_RUNNING;

return NULL;
return Error(NONE);
}

static void addressForNode(struct Address* addrOut, struct Message* msg)
Expand Down Expand Up @@ -277,7 +277,7 @@ static Iface_DEFUN switchErr(struct Message* msg, struct Pathfinder_pvt* pf)
SearchRunner_search(nodeAddr, 20, 3, pf->searchRunner, pf->alloc);
}

return NULL;
return Error(NONE);
}

static Iface_DEFUN searchReq(struct Message* msg, struct Pathfinder_pvt* pf)
Expand All @@ -286,7 +286,7 @@ static Iface_DEFUN searchReq(struct Message* msg, struct Pathfinder_pvt* pf)
Er_assert(Message_epop(msg, addr, 16));
Er_assert(Message_epop32be(msg));
uint32_t version = Er_assert(Message_epop32be(msg));
if (version && version >= 20) { return NULL; }
if (version && version >= 20) { return Error(NONE); }
Assert_true(!msg->length);
uint8_t printedAddr[40];
AddrTools_printIp(printedAddr, addr);
Expand All @@ -298,7 +298,7 @@ static Iface_DEFUN searchReq(struct Message* msg, struct Pathfinder_pvt* pf)
} else {
SearchRunner_search(addr, 20, 3, pf->searchRunner, pf->alloc);
}
return NULL;
return Error(NONE);
}

static Iface_DEFUN peer(struct Message* msg, struct Pathfinder_pvt* pf)
Expand All @@ -315,7 +315,7 @@ static Iface_DEFUN peer(struct Message* msg, struct Pathfinder_pvt* pf)
&& Node_getBestParent(link->child)->parent->address.path == 1
&& Node_getBestParent(link->child)->cannonicalLabel == addr.path)
{
return NULL;
return Error(NONE);
}
//RumorMill_addNode(pf->rumorMill, &addr);
Router_sendGetPeers(pf->router, &addr, 0, 0, pf->alloc);
Expand Down Expand Up @@ -349,7 +349,7 @@ static Iface_DEFUN session(struct Message* msg, struct Pathfinder_pvt* pf)
SearchRunner_search(addr.ip6.bytes, 20, 3, pf->searchRunner, pf->alloc);
}*/

return NULL;
return Error(NONE);
}

static Iface_DEFUN sessionEnded(struct Message* msg, struct Pathfinder_pvt* pf)
Expand All @@ -358,7 +358,7 @@ static Iface_DEFUN sessionEnded(struct Message* msg, struct Pathfinder_pvt* pf)
addressForNode(&addr, msg);
String* str = Address_toString(&addr, msg->alloc);
Log_debug(pf->log, "Session ended [%s]", str->bytes);
return NULL;
return Error(NONE);
}

static Iface_DEFUN discoveredPath(struct Message* msg, struct Pathfinder_pvt* pf)
Expand All @@ -367,22 +367,22 @@ static Iface_DEFUN discoveredPath(struct Message* msg, struct Pathfinder_pvt* pf
addressForNode(&addr, msg);

// We're somehow aware of this path (even if it's unused)
if (NodeStore_linkForPath(pf->nodeStore, addr.path)) { return NULL; }
if (NodeStore_linkForPath(pf->nodeStore, addr.path)) { return Error(NONE); }

// If we don't already care about the destination, then don't do anything.
struct Node_Two* nn = NodeStore_nodeForAddr(pf->nodeStore, addr.ip6.bytes);
if (!nn) { return NULL; }
if (!nn) { return Error(NONE); }

// Our best path is "shorter" (label bits which is somewhat representitive of hop count)
// basically this is just to dampen the flood to the RM because otherwise it prevents Janitor
// from getting any actual work done.
if (nn->address.path < addr.path) { return NULL; }
if (nn->address.path < addr.path) { return Error(NONE); }

addr.protocolVersion = nn->address.protocolVersion;

Log_debug(pf->log, "Discovered path [%s]", Address_toString(&addr, msg->alloc)->bytes);
RumorMill_addNode(pf->rumorMill, &addr);
return NULL;
return Error(NONE);
}

static Iface_DEFUN handlePing(struct Message* msg, struct Pathfinder_pvt* pf)
Expand All @@ -395,7 +395,7 @@ static Iface_DEFUN handlePing(struct Message* msg, struct Pathfinder_pvt* pf)
static Iface_DEFUN handlePong(struct Message* msg, struct Pathfinder_pvt* pf)
{
Log_debug(pf->log, "Received pong");
return NULL;
return Error(NONE);
}

static Iface_DEFUN incomingMsg(struct Message* msg, struct Pathfinder_pvt* pf)
Expand Down Expand Up @@ -427,7 +427,7 @@ static Iface_DEFUN incomingMsg(struct Message* msg, struct Pathfinder_pvt* pf)
return Iface_next(&pf->pub.eventIf, msg);
}

return NULL;
return Error(NONE);
}

static Iface_DEFUN incomingFromEventIf(struct Message* msg, struct Iface* eventIf)
Expand All @@ -453,7 +453,7 @@ static Iface_DEFUN incomingFromEventIf(struct Message* msg, struct Iface* eventI
case PFChan_Core_PONG: return handlePong(msg, pf);
case PFChan_Core_UNSETUP_SESSION:
case PFChan_Core_LINK_STATE:
case PFChan_Core_CTRL_MSG: return NULL;
case PFChan_Core_CTRL_MSG: return Error(NONE);
default:;
}
Assert_failure("unexpected event [%d]", ev);
Expand Down
4 changes: 2 additions & 2 deletions interface/ASynchronizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static Iface_DEFUN fromA(struct Message* msg, struct Iface* ifA)
Allocator_adopt(as->cycleAlloc, msg->alloc);
ArrayList_Messages_add(as->msgsToB, msg);
checkTimeout(as);
return NULL;
return Error(NONE);
}

static Iface_DEFUN fromB(struct Message* msg, struct Iface* ifB)
Expand All @@ -112,7 +112,7 @@ static Iface_DEFUN fromB(struct Message* msg, struct Iface* ifB)
Allocator_adopt(as->cycleAlloc, msg->alloc);
ArrayList_Messages_add(as->msgsToA, msg);
checkTimeout(as);
return NULL;
return Error(NONE);
}

struct ASynchronizer* ASynchronizer_new(struct Allocator* alloc,
Expand Down
3 changes: 1 addition & 2 deletions interface/ETHInterface_darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ static Iface_DEFUN sendMessage(struct Message* msg, struct Iface* iface)
if (msg->length != write(ctx->socket, msg->bytes, msg->length)) {
Log_debug(ctx->logger, "Error writing to eth device [%s]", strerror(errno));
}
Log_debug(ctx->logger, "message sent");
return NULL;
return Error(NONE);
}

static void handleEvent2(struct ETHInterface_pvt* context,
Expand Down
2 changes: 1 addition & 1 deletion interface/ETHInterface_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static Iface_DEFUN sendMessage(struct Message* msg, struct Iface* iface)
};
Er_assert(Message_epush(msg, &hdr, ETHInterface_Header_SIZE));
sendMessageInternal(msg, &addr, ctx);
return NULL;
return Error(NONE);
}

static void handleEvent2(struct ETHInterface_pvt* context, struct Allocator* messageAlloc)
Expand Down
12 changes: 6 additions & 6 deletions interface/FramingIface.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)
if (fi->bytesRemaining > fi->maxMessageSize) {
// Oversize message
Assert_ifTesting(0);
return NULL;
return Error(OVERSIZE_MESSAGE);
}

if (fi->frameParts) {
Expand All @@ -93,21 +93,21 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)
// Run the message through again since it's almost certainly not perfect size.
Iface_CALL(receiveMessage, wholeMessage, streamIf);
Allocator_free(frameAlloc);
return NULL;
return Error(NONE);
}
fi->bytesRemaining -= msg->length;
Allocator_adopt(fi->frameAlloc, msg->alloc);
struct MessageList* parts = Allocator_calloc(fi->frameAlloc, sizeof(struct MessageList), 1);
parts->msg = msg;
parts->next = fi->frameParts;
fi->frameParts = parts;
return NULL;
return Error(NONE);
}

for (;;) {
while (fi->headerIndex < 4) {
if (!msg->length) {
return NULL;
return Error(NONE);
}
fi->header.bytes[fi->headerIndex] = msg->bytes[0];
Er_assert(Message_eshift(msg, -1));
Expand All @@ -119,7 +119,7 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)
if (fi->bytesRemaining > fi->maxMessageSize) {
// oversize
Assert_ifTesting(0);
return NULL;
return Error(OVERSIZE_MESSAGE);
}

if (fi->bytesRemaining == (uint32_t)msg->length) {
Expand Down Expand Up @@ -153,7 +153,7 @@ static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)
fi->frameParts->msg = m;
fi->frameParts->next = NULL;
}
return NULL;
return Error(NONE);
}
}

Expand Down
Loading

0 comments on commit 3cf79da

Please sign in to comment.