Compare commits

...

8 Commits

Author SHA1 Message Date
CPunch d015eec5f1 updated CONTRIB 2022-09-01 20:15:35 -05:00
CPunch cf01657cc2 fixes for winblows 2022-09-01 20:07:29 -05:00
CPunch 587d9a26e5 lpolllist.c: fix poll ifdef paths 2022-09-01 20:04:54 -05:00
CPunch b23057b219 Refactoring: reorganized files 2022-09-01 20:00:37 -05:00
CPunch 169313ee39 shell: migrated to new vector API
- removed array API from lmem.h
- sterm.c: basically left as-is. see source for notes as to why
2022-09-01 19:35:52 -05:00
CPunch 44086f563b cnc: migrate to new vector API 2022-09-01 19:12:13 -05:00
CPunch 13398dbdf6 lsocket.[ch] & lpeer.c: migrated to new vector API 2022-09-01 19:05:56 -05:00
CPunch af09e74263 lmem.h: new laikaM_*Vector macros
- these will slowly replace laikaM_*array
- lpeer.[ch] has been migrated
2022-09-01 18:47:29 -05:00
53 changed files with 842 additions and 742 deletions

View File

@ -16,8 +16,8 @@ Looking for some simple tasks that need to get done for that sweet 'contributor'
- Change `lib/lin/linshell.c` to use openpty() instead of forkpty() for BSD support - Change `lib/lin/linshell.c` to use openpty() instead of forkpty() for BSD support
- Fix address sanitizer for CMake DEBUG builds - Fix address sanitizer for CMake DEBUG builds
- Change laikaT_getTime in `lib/src/ltask.c` to not use C11 features - Change laikaT_getTime in `lib/src/core/ltask.c` to not use C11 features
- Implement more LAIKA_BOX_* VMs in `lib/include/lbox.h` - Implement more LAIKA_BOX_* VMs in `lib/include/core/lbox.h`
- Import more WinAPI manually using the method listed below - Import more WinAPI manually using the method listed below
## Bot: Windows API Imports Obfuscation ## Bot: Windows API Imports Obfuscation
@ -63,7 +63,7 @@ If the `real` & `hashed` match, that means our manual runtime import and the imp
Now just replace all of the calls to the raw WinAPI (in our case, ShellExecuteA) with our new manually imported oShellExecuteA function pointer. Format & commit your changes, and open a PR and I'll merge your changes. Thanks! Now just replace all of the calls to the raw WinAPI (in our case, ShellExecuteA) with our new manually imported oShellExecuteA function pointer. Format & commit your changes, and open a PR and I'll merge your changes. Thanks!
## Lib: Error Handling ## Lib: Error Handling
Error handling in Laika is done via the 'lerror.h' header library. It's a small and simple error handling solution written for laika, however can be stripped and used as a simple error handling library. Error handling in Laika is used similarly to other languages, implementing a try & catch block and is achieved using setjmp(). The LAIKA_ERROR(...) is used to throw errors. Error handling in Laika is done via the 'lib/core/lerror.h' header library. It's a small and simple error handling solution written for laika, however can be stripped and used as a simple error handling library. Error handling in Laika is used similarly to other languages, implementing a try & catch block and is achieved using setjmp() & longjmp(). The LAIKA_ERROR(...) macro is used to throw errors.
Example: Example:
```C ```C
@ -84,15 +84,13 @@ Some minor inconveniences include:
- not thread safe. - not thread safe.
## Lib: Packet Handlers ## Lib: Packet Handlers
Laika has a simple binary protocol & a small backend (see `lib/src/net/lpeer.c`) to handle packets to/from peers. `lib/include/net/lpacket.h` includes descriptions for each packet type. For an example of proper packet handler definitions see `bot/src/bot.c`. It boils down to passing a sLaika_peerPacketInfo table to laikaS_newPeer. To add packet handlers to the bot, add your handler info to laikaB_pktTbl in `bot/src/bot.c`. To add packet handlers to the shell, add your handler info to shellC_pktTbl in `shell/src/sclient.c`. For adding packet handlers to cnc, make sure you add them to the corresponding table in `cnc/src/cnc.c`, laikaC_botPktTbl for packets being received from a bot peer, laikaC_authPktTbl for packets being received from an auth peer (shell), or DEFAULT_PKT_TBL if it's received by all peer types (things like handshakes, keep-alive, etc.)
Laika has a simple binary protocol & a small backend (see `lib/src/lpeer.c`) to handle packets to/from peers. `lib/include/lpacket.h` includes descriptions for each packet type. For an example of proper packet handler definitions see `bot/src/bot.c`. It boils down to passing a sLaika_peerPacketInfo table to laikaS_newPeer. To add packet handlers to the bot, add your handler info to laikaB_pktTbl in `bot/src/bot.c`. To add packet handlers to the shell, add your handler info to shellC_pktTbl in `shell/src/sclient.c`. For adding packet handlers to cnc, make sure you add them to the corresponding table in `cnc/src/cnc.c`, laikaC_botPktTbl for packets being received from a bot peer, laikaC_authPktTbl for packets being received from an auth peer (shell), or DEFAULT_PKT_TBL if it's received by all peer types (things like handshakes, keep-alive, etc.)
## Lib: Task Service ## Lib: Task Service
Tasks can be scheduled on a delta-period (call X function every approximate N seconds). laikaT_pollTasks() is used to check & run any currently queued tasks. This is useful for sending keep-alive packets, polling shell pipes, or other repeatably scheduled tasks. Most laikaT_pollTasks() calls are done in the peerHandler for each client/server. Tasks can be scheduled on a delta-period (call X function every approximate N seconds). laikaT_pollTasks() is used to check & run any currently queued tasks. This is useful for sending keep-alive packets, polling shell pipes, or other repeatably scheduled tasks. Most laikaT_pollTasks() calls are done in the peerHandler for each client/server.
## Lib: VM Boxes ## Lib: VM Boxes
Laika has a tiny VM for decrypting sensitive information. For details on the ISA read `lib/include/lvm.h`, for information on how to use them read `lib/include/lbox.h`. Feel free to write your own boxes and contribute them :D Laika has a tiny VM for decrypting sensitive information. For details on the ISA read `lib/include/core/lvm.h`, for information on how to use them read `lib/include/core/lbox.h`. Feel free to write your own boxes and contribute them :D
## Bot: Platform-specific backends ## Bot: Platform-specific backends
`bot/win` and `bot/lin` include code for platform-specific code that can't be quickly "ifdef"d away. These mainly include stuff like persistence or opening pseudo-ttys. `bot/win` and `bot/lin` include code for platform-specific code that can't be quickly "ifdef"d away. These mainly include stuff like persistence or opening pseudo-ttys.

View File

@ -1,13 +1,13 @@
#ifndef LAIKA_BOT_H #ifndef LAIKA_BOT_H
#define LAIKA_BOT_H #define LAIKA_BOT_H
#include "core/lsodium.h"
#include "core/ltask.h"
#include "laika.h" #include "laika.h"
#include "lpacket.h" #include "net/lpacket.h"
#include "lpeer.h" #include "net/lpeer.h"
#include "lpolllist.h" #include "net/lpolllist.h"
#include "lsocket.h" #include "net/lsocket.h"
#include "lsodium.h"
#include "ltask.h"
struct sLaika_shell; struct sLaika_shell;
struct sLaika_bot struct sLaika_bot

View File

@ -2,7 +2,7 @@
#define LAIKA_SHELL_H #define LAIKA_SHELL_H
#include "laika.h" #include "laika.h"
#include "lpacket.h" #include "net/lpacket.h"
#include <time.h> #include <time.h>

View File

@ -1,10 +1,10 @@
/* platform specific code for achieving persistence on linux */ /* platform specific code for achieving persistence on linux */
#include "lbox.h" #include "core/lbox.h"
#include "core/lerror.h"
#include "core/lmem.h"
#include "lconfig.h" #include "lconfig.h"
#include "lerror.h" #include "net/lsocket.h"
#include "lmem.h"
#include "lsocket.h"
#include "persist.h" #include "persist.h"
#include <pwd.h> #include <pwd.h>

View File

@ -1,9 +1,9 @@
/* platform specific code for opening shells in linux */ /* platform specific code for opening shells in linux */
#include "bot.h" #include "bot.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
#include "ltask.h" #include "core/ltask.h"
#include "shell.h" #include "shell.h"
#include <pty.h> #include <pty.h>

View File

@ -1,9 +1,9 @@
#include "bot.h" #include "bot.h"
#include "lbox.h" #include "core/lbox.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
#include "lsodium.h" #include "core/lsodium.h"
#include "shell.h" #include "shell.h"
void laikaB_handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) void laikaB_handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData)

View File

@ -1,8 +1,8 @@
#include "bot.h" #include "bot.h"
#include "lbox.h" #include "core/lbox.h"
#include "core/lerror.h"
#include "core/ltask.h"
#include "lconfig.h" #include "lconfig.h"
#include "lerror.h"
#include "ltask.h"
#include "lobf.h" #include "lobf.h"
#include "persist.h" #include "persist.h"
#include "shell.h" #include "shell.h"

View File

@ -1,8 +1,8 @@
#include "shell.h" #include "shell.h"
#include "bot.h" #include "bot.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>

View File

@ -6,11 +6,11 @@
#pragma comment(lib, "Shlwapi.lib") #pragma comment(lib, "Shlwapi.lib")
#include "lbox.h" #include "core/lbox.h"
#include "core/lerror.h"
#include "core/lmem.h"
#include "core/lvm.h"
#include "lconfig.h" #include "lconfig.h"
#include "lerror.h"
#include "lmem.h"
#include "lvm.h"
#include "lobf.h" #include "lobf.h"
#include "persist.h" #include "persist.h"

View File

@ -1,8 +1,8 @@
/* platform specific code for opening shells (pseudo consoles) on windows */ /* platform specific code for opening shells (pseudo consoles) on windows */
#include "bot.h" #include "bot.h"
#include "core/lerror.h"
#include "core/lmem.h"
#include "lobf.h" #include "lobf.h"
#include "lerror.h"
#include "lmem.h"
#include "shell.h" #include "shell.h"
#include <process.h> #include <process.h>

View File

@ -1,13 +1,14 @@
#ifndef LAIKA_CNC_H #ifndef LAIKA_CNC_H
#define LAIKA_CNC_H #define LAIKA_CNC_H
#include "hashmap.h" #include "core/hashmap.h"
#include "core/lmem.h"
#include "core/ltask.h"
#include "laika.h" #include "laika.h"
#include "lpacket.h" #include "net/lpacket.h"
#include "lpeer.h" #include "net/lpeer.h"
#include "lpolllist.h" #include "net/lpolllist.h"
#include "lsocket.h" #include "net/lsocket.h"
#include "ltask.h"
/* kill peers if they haven't ping'd within a minute */ /* kill peers if they haven't ping'd within a minute */
#define LAIKA_PEER_TIMEOUT 60 * 1000 #define LAIKA_PEER_TIMEOUT 60 * 1000
@ -20,12 +21,8 @@ struct sLaika_cnc
struct sLaika_socket sock; struct sLaika_socket sock;
struct sLaika_pollList pList; struct sLaika_pollList pList;
struct hashmap *peers; /* holds all peers, lookup using pubkey */ struct hashmap *peers; /* holds all peers, lookup using pubkey */
struct sLaika_peer **authPeers; /* holds connected panel peers */ laikaM_newVector(struct sLaika_peer *, authPeers); /* holds connected panel peers */
uint8_t **authKeys; laikaM_newVector(uint8_t *, authKeys);
int authKeysCount;
int authKeysCap;
int authPeersCount;
int authPeersCap;
uint16_t port; uint16_t port;
}; };

View File

@ -2,7 +2,7 @@
#define LAIKA_CNC_PANEL_H #define LAIKA_CNC_PANEL_H
#include "cnc.h" #include "cnc.h"
#include "lpeer.h" #include "net/lpeer.h"
void laikaC_sendPeerList(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer); void laikaC_sendPeerList(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer);
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot); void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *bot);

View File

@ -2,10 +2,10 @@
#define LAIKA_CNC_PEER_H #define LAIKA_CNC_PEER_H
#include "laika.h" #include "laika.h"
#include "lpacket.h" #include "net/lpacket.h"
#include "lpeer.h" #include "net/lpeer.h"
#include "lpolllist.h" #include "net/lpolllist.h"
#include "lsocket.h" #include "net/lsocket.h"
struct sLaika_peerInfo struct sLaika_peerInfo
{ {

View File

@ -1,12 +1,12 @@
#include "cnc.h" #include "cnc.h"
#include "core/lerror.h"
#include "core/lmem.h"
#include "core/lsodium.h"
#include "core/ltask.h"
#include "cpanel.h" #include "cpanel.h"
#include "cpeer.h" #include "cpeer.h"
#include "lerror.h" #include "net/lsocket.h"
#include "lmem.h"
#include "lsocket.h"
#include "lsodium.h"
#include "ltask.h"
/* ======================================[[ PeerHashMap ]]======================================= */ /* ======================================[[ PeerHashMap ]]======================================= */
@ -161,12 +161,8 @@ struct sLaika_cnc *laikaC_newCNC(uint16_t port)
/* init peer hashmap & panel list */ /* init peer hashmap & panel list */
cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash, cnc->peers = hashmap_new(sizeof(tCNC_PeerHashElem), 8, 0, 0, cnc_PeerElemHash,
cnc_PeerElemCompare, NULL, NULL); cnc_PeerElemCompare, NULL, NULL);
cnc->authPeers = NULL; laikaM_initVector(cnc->authPeers, 4);
cnc->authKeys = NULL; laikaM_initVector(cnc->authKeys, 4);
cnc->authKeysCount = 0;
cnc->authKeysCap = 4;
cnc->authPeersCap = 4;
cnc->authPeersCount = 0;
cnc->port = port; cnc->port = port;
/* init socket (we just need it for the raw socket fd and abstracted API :P) & pollList */ /* init socket (we just need it for the raw socket fd and abstracted API :P) & pollList */
@ -207,7 +203,7 @@ void laikaC_freeCNC(struct sLaika_cnc *cnc)
hashmap_free(cnc->peers); hashmap_free(cnc->peers);
/* free auth keys */ /* free auth keys */
for (i = 0; i < cnc->authKeysCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authKeys); i++) {
laikaM_free(cnc->authKeys[i]); laikaM_free(cnc->authKeys[i]);
} }
laikaM_free(cnc->authKeys); laikaM_free(cnc->authKeys);
@ -222,7 +218,7 @@ void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
hashmap_set(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer}); hashmap_set(cnc->peers, &(tCNC_PeerHashElem){.pub = peer->peerPub, .peer = peer});
/* notify connected panels of the newly connected peer */ /* notify connected panels of the newly connected peer */
for (i = 0; i < cnc->authPeersCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authPeers); i++) {
laikaC_sendNewPeer(cnc->authPeers[i], peer); laikaC_sendNewPeer(cnc->authPeers[i], peer);
} }
@ -273,7 +269,7 @@ void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer)
} }
/* notify connected panels of the disconnected peer */ /* notify connected panels of the disconnected peer */
for (i = 0; i < cnc->authPeersCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authPeers); i++) {
laikaC_sendRmvPeer(cnc->authPeers[i], peer); laikaC_sendRmvPeer(cnc->authPeers[i], peer);
} }
@ -316,11 +312,10 @@ void laikaC_setPeerType(struct sLaika_cnc *cnc, struct sLaika_peer *peer, PEERTY
void laikaC_addAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer) void laikaC_addAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
{ {
/* grow array if we need to */ /* grow array if we need to */
laikaM_growarray(struct sLaika_peer *, cnc->authPeers, 1, cnc->authPeersCount, laikaM_growVector(struct sLaika_peer *, cnc->authPeers, 1);
cnc->authPeersCap);
/* insert into authenticated peer table */ /* insert into authenticated peer table */
cnc->authPeers[cnc->authPeersCount++] = authPeer; cnc->authPeers[laikaM_countVector(cnc->authPeers)++] = authPeer;
LAIKA_DEBUG("added panel %p!\n", authPeer); LAIKA_DEBUG("added panel %p!\n", authPeer);
} }
@ -329,9 +324,9 @@ void laikaC_rmvAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
{ {
int i; int i;
for (i = 0; i < cnc->authPeersCount; i++) { for (i = 0; i < laikaM_countVector(cnc->authPeers); i++) {
if (cnc->authPeers[i] == authPeer) { /* we found the index for our panel! */ if (cnc->authPeers[i] == authPeer) { /* we found the index for our panel! */
laikaM_rmvarray(cnc->authPeers, cnc->authPeersCount, i, 1); laikaM_rmvVector(cnc->authPeers, i, 1);
return; return;
} }
} }
@ -340,14 +335,14 @@ void laikaC_rmvAuth(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
void laikaC_addAuthKey(struct sLaika_cnc *cnc, const char *key) void laikaC_addAuthKey(struct sLaika_cnc *cnc, const char *key)
{ {
uint8_t *buf; uint8_t *buf;
laikaM_growarray(uint8_t *, cnc->authKeys, 1, cnc->authKeysCount, cnc->authKeysCap); laikaM_growVector(uint8_t *, cnc->authKeys, 1);
buf = laikaM_malloc(crypto_kx_PUBLICKEYBYTES); buf = laikaM_malloc(crypto_kx_PUBLICKEYBYTES);
if (!laikaK_loadKeys(buf, NULL, key, NULL)) if (!laikaK_loadKeys(buf, NULL, key, NULL))
LAIKA_ERROR("Failed to load key '%s'\n", key); LAIKA_ERROR("Failed to load key '%s'\n", key);
/* insert key */ /* insert key */
cnc->authKeys[cnc->authKeysCount++] = buf; cnc->authKeys[laikaM_countVector(cnc->authKeys)++] = buf;
printf("[~] Added authenticated public key '%s'\n", key); printf("[~] Added authenticated public key '%s'\n", key);
} }
@ -455,7 +450,7 @@ bool laikaC_iterPeersNext(struct sLaika_cnc *cnc, size_t *i, struct sLaika_peer
{ {
tCNC_PeerHashElem *elem; tCNC_PeerHashElem *elem;
if (hashmap_iter(cnc->peers, i, (void *)&elem)) { if (hashmap_iter(cnc->peers, i, (void **)&elem)) {
*peer = elem->peer; *peer = elem->peer;
return true; return true;
} }

View File

@ -1,9 +1,9 @@
#include "cpanel.h" #include "cpanel.h"
#include "cnc.h" #include "cnc.h"
#include "core/lerror.h"
#include "core/lmem.h"
#include "cpeer.h" #include "cpeer.h"
#include "lerror.h"
#include "lmem.h"
void laikaC_sendPeerList(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer) void laikaC_sendPeerList(struct sLaika_cnc *cnc, struct sLaika_peer *authPeer)
{ {

View File

@ -1,8 +1,8 @@
#include "cpeer.h" #include "cpeer.h"
#include "cnc.h" #include "cnc.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
/* =======================================[[ Peer Info ]]======================================= */ /* =======================================[[ Peer Info ]]======================================= */
@ -155,7 +155,7 @@ void laikaC_handlePeerLoginReq(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void
break; break;
case PEER_AUTH: case PEER_AUTH:
/* check that peer's pubkey is authenticated */ /* check that peer's pubkey is authenticated */
if (!laikaK_checkAuth(peer->peerPub, cnc->authKeys, cnc->authKeysCount)) if (!laikaK_checkAuth(peer->peerPub, cnc->authKeys, laikaM_countVector(cnc->authKeys)))
LAIKA_ERROR("laikaC_handlePeerHandshake: Unauthorized panel!\n"); LAIKA_ERROR("laikaC_handlePeerHandshake: Unauthorized panel!\n");
LAIKA_DEBUG("Accepted authenticated panel %p\n", peer); LAIKA_DEBUG("Accepted authenticated panel %p\n", peer);

View File

@ -1,7 +1,7 @@
#include "cnc.h" #include "cnc.h"
#include "ini.h" #include "core/ini.h"
#include "core/ltask.h"
#include "lconfig.h" #include "lconfig.h"
#include "ltask.h"
#include <stdio.h> #include <stdio.h>

View File

@ -8,7 +8,7 @@ project(LaikaLib VERSION ${LAIKA_VERSION_MAJOR}.${LAIKA_VERSION_MINOR})
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# compile LaikaLib library # compile LaikaLib library
file(GLOB_RECURSE LIBSOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/**.c ${CMAKE_CURRENT_SOURCE_DIR}/vendor/**.c) file(GLOB_RECURSE LIBSOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/**.c)
file(GLOB_RECURSE LIBHEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/**.h) file(GLOB_RECURSE LIBHEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/**.h)
# include platform specific backends # include platform specific backends

View File

@ -16,129 +16,127 @@ https://github.com/benhoyt/inih
/* Make this header file easier to include in C++ code */ /* Make this header file easier to include in C++ code */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
#include <stdio.h> #include <stdio.h>
/* Nonzero if ini_handler callback should accept lineno parameter. */ /* Nonzero if ini_handler callback should accept lineno parameter. */
#ifndef INI_HANDLER_LINENO #ifndef INI_HANDLER_LINENO
#define INI_HANDLER_LINENO 0 # define INI_HANDLER_LINENO 0
#endif #endif
/* Typedef for prototype of handler function. */ /* Typedef for prototype of handler function. */
#if INI_HANDLER_LINENO #if INI_HANDLER_LINENO
typedef int (*ini_handler)(void* user, const char* section, typedef int (*ini_handler)(void *user, const char *section, const char *name, const char *value,
const char* name, const char* value, int lineno);
int lineno);
#else #else
typedef int (*ini_handler)(void* user, const char* section, typedef int (*ini_handler)(void *user, const char *section, const char *name, const char *value);
const char* name, const char* value);
#endif #endif
/* Typedef for prototype of fgets-style reader function. */ /* Typedef for prototype of fgets-style reader function. */
typedef char* (*ini_reader)(char* str, int num, void* stream); typedef char *(*ini_reader)(char *str, int num, void *stream);
/* Parse given INI-style file. May have [section]s, name=value pairs /* Parse given INI-style file. May have [section]s, name=value pairs
(whitespace stripped), and comments starting with ';' (semicolon). Section (whitespace stripped), and comments starting with ';' (semicolon). Section
is "" if name=value pair parsed before any section heading. name:value is "" if name=value pair parsed before any section heading. name:value
pairs are also supported as a concession to Python's configparser. pairs are also supported as a concession to Python's configparser.
For each name=value pair parsed, call handler function with given user For each name=value pair parsed, call handler function with given user
pointer as well as section, name, and value (data only valid for duration pointer as well as section, name, and value (data only valid for duration
of handler call). Handler should return nonzero on success, zero on error. of handler call). Handler should return nonzero on success, zero on error.
Returns 0 on success, line number of first error on parse error (doesn't Returns 0 on success, line number of first error on parse error (doesn't
stop on first error), -1 on file open error, or -2 on memory allocation stop on first error), -1 on file open error, or -2 on memory allocation
error (only when INI_USE_STACK is zero). error (only when INI_USE_STACK is zero).
*/ */
int ini_parse(const char* filename, ini_handler handler, void* user); int ini_parse(const char *filename, ini_handler handler, void *user);
/* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't /* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't
close the file when it's finished -- the caller must do that. */ close the file when it's finished -- the caller must do that. */
int ini_parse_file(FILE* file, ini_handler handler, void* user); int ini_parse_file(FILE *file, ini_handler handler, void *user);
/* Same as ini_parse(), but takes an ini_reader function pointer instead of /* Same as ini_parse(), but takes an ini_reader function pointer instead of
filename. Used for implementing custom or string-based I/O (see also filename. Used for implementing custom or string-based I/O (see also
ini_parse_string). */ ini_parse_string). */
int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, int ini_parse_stream(ini_reader reader, void *stream, ini_handler handler, void *user);
void* user);
/* Same as ini_parse(), but takes a zero-terminated string with the INI data /* Same as ini_parse(), but takes a zero-terminated string with the INI data
instead of a file. Useful for parsing INI data from a network socket or instead of a file. Useful for parsing INI data from a network socket or
already in memory. */ already in memory. */
int ini_parse_string(const char* string, ini_handler handler, void* user); int ini_parse_string(const char *string, ini_handler handler, void *user);
/* Nonzero to allow multi-line value parsing, in the style of Python's /* Nonzero to allow multi-line value parsing, in the style of Python's
configparser. If allowed, ini_parse() will call the handler with the same configparser. If allowed, ini_parse() will call the handler with the same
name for each subsequent line parsed. */ name for each subsequent line parsed. */
#ifndef INI_ALLOW_MULTILINE #ifndef INI_ALLOW_MULTILINE
#define INI_ALLOW_MULTILINE 1 # define INI_ALLOW_MULTILINE 1
#endif #endif
/* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of /* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of
the file. See https://github.com/benhoyt/inih/issues/21 */ the file. See https://github.com/benhoyt/inih/issues/21 */
#ifndef INI_ALLOW_BOM #ifndef INI_ALLOW_BOM
#define INI_ALLOW_BOM 1 # define INI_ALLOW_BOM 1
#endif #endif
/* Chars that begin a start-of-line comment. Per Python configparser, allow /* Chars that begin a start-of-line comment. Per Python configparser, allow
both ; and # comments at the start of a line by default. */ both ; and # comments at the start of a line by default. */
#ifndef INI_START_COMMENT_PREFIXES #ifndef INI_START_COMMENT_PREFIXES
#define INI_START_COMMENT_PREFIXES ";#" # define INI_START_COMMENT_PREFIXES ";#"
#endif #endif
/* Nonzero to allow inline comments (with valid inline comment characters /* Nonzero to allow inline comments (with valid inline comment characters
specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match
Python 3.2+ configparser behaviour. */ Python 3.2+ configparser behaviour. */
#ifndef INI_ALLOW_INLINE_COMMENTS #ifndef INI_ALLOW_INLINE_COMMENTS
#define INI_ALLOW_INLINE_COMMENTS 1 # define INI_ALLOW_INLINE_COMMENTS 1
#endif #endif
#ifndef INI_INLINE_COMMENT_PREFIXES #ifndef INI_INLINE_COMMENT_PREFIXES
#define INI_INLINE_COMMENT_PREFIXES ";" # define INI_INLINE_COMMENT_PREFIXES ";"
#endif #endif
/* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */ /* Nonzero to use stack for line buffer, zero to use heap (malloc/free). */
#ifndef INI_USE_STACK #ifndef INI_USE_STACK
#define INI_USE_STACK 1 # define INI_USE_STACK 1
#endif #endif
/* Maximum line length for any line in INI file (stack or heap). Note that /* Maximum line length for any line in INI file (stack or heap). Note that
this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */ this must be 3 more than the longest line (due to '\r', '\n', and '\0'). */
#ifndef INI_MAX_LINE #ifndef INI_MAX_LINE
#define INI_MAX_LINE 200 # define INI_MAX_LINE 200
#endif #endif
/* Nonzero to allow heap line buffer to grow via realloc(), zero for a /* Nonzero to allow heap line buffer to grow via realloc(), zero for a
fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is fixed-size buffer of INI_MAX_LINE bytes. Only applies if INI_USE_STACK is
zero. */ zero. */
#ifndef INI_ALLOW_REALLOC #ifndef INI_ALLOW_REALLOC
#define INI_ALLOW_REALLOC 0 # define INI_ALLOW_REALLOC 0
#endif #endif
/* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK /* Initial size in bytes for heap line buffer. Only applies if INI_USE_STACK
is zero. */ is zero. */
#ifndef INI_INITIAL_ALLOC #ifndef INI_INITIAL_ALLOC
#define INI_INITIAL_ALLOC 200 # define INI_INITIAL_ALLOC 200
#endif #endif
/* Stop parsing on first error (default is to keep parsing). */ /* Stop parsing on first error (default is to keep parsing). */
#ifndef INI_STOP_ON_FIRST_ERROR #ifndef INI_STOP_ON_FIRST_ERROR
#define INI_STOP_ON_FIRST_ERROR 0 # define INI_STOP_ON_FIRST_ERROR 0
#endif #endif
/* Nonzero to call the handler at the start of each new section (with /* Nonzero to call the handler at the start of each new section (with
name and value NULL). Default is to only call the handler on name and value NULL). Default is to only call the handler on
each name=value pair. */ each name=value pair. */
#ifndef INI_CALL_HANDLER_ON_NEW_SECTION #ifndef INI_CALL_HANDLER_ON_NEW_SECTION
#define INI_CALL_HANDLER_ON_NEW_SECTION 0 # define INI_CALL_HANDLER_ON_NEW_SECTION 0
#endif #endif
/* Nonzero to allow a name without a value (no '=' or ':' on the line) and /* Nonzero to allow a name without a value (no '=' or ':' on the line) and
call the handler with value NULL in this case. Default is to treat call the handler with value NULL in this case. Default is to treat
no-value lines as an error. */ no-value lines as an error. */
#ifndef INI_ALLOW_NO_VALUE #ifndef INI_ALLOW_NO_VALUE
#define INI_ALLOW_NO_VALUE 0 # define INI_ALLOW_NO_VALUE 0
#endif #endif
/* Nonzero to use custom ini_malloc, ini_free, and ini_realloc memory /* Nonzero to use custom ini_malloc, ini_free, and ini_realloc memory
@ -146,10 +144,9 @@ int ini_parse_string(const char* string, ini_handler handler, void* user);
have the same signatures as malloc/free/realloc and behave in a similar have the same signatures as malloc/free/realloc and behave in a similar
way. ini_realloc is only needed if INI_ALLOW_REALLOC is set. */ way. ini_realloc is only needed if INI_ALLOW_REALLOC is set. */
#ifndef INI_CUSTOM_ALLOCATOR #ifndef INI_CUSTOM_ALLOCATOR
#define INI_CUSTOM_ALLOCATOR 0 # define INI_CUSTOM_ALLOCATOR 0
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1,10 +1,10 @@
#ifndef LAIKA_BOX_H #ifndef LAIKA_BOX_H
#define LAIKA_BOX_H #define LAIKA_BOX_H
#include "core/lmem.h"
#include "core/lsodium.h"
#include "core/lvm.h"
#include "laika.h" #include "laika.h"
#include "lmem.h"
#include "lsodium.h"
#include "lvm.h"
#include <inttypes.h> #include <inttypes.h>

60
lib/include/core/lmem.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef LAIKA_MEM_H
#define LAIKA_MEM_H
#include "laika.h"
#define GROW_FACTOR 2
/* microsoft strikes again with their lack of support for VLAs */
#if _MSC_VER
# define VLA(type, var, sz) type *var = laikaM_malloc(sizeof(type) * sz);
# define ENDVLA(var) laikaM_free(var);
#else
# define VLA(type, var, sz) type var[sz];
# define ENDVLA(var) ((void)0) /* no op */
#endif
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
#define laikaM_free(buf) laikaM_realloc(buf, 0)
/* ========================================[[ Vectors ]]======================================== */
#define laikaM_countVector(name) name##_COUNT
#define laikaM_capVector(name) name##_CAP
#define laikaM_newVector(type, name) \
type *name; \
int name##_COUNT; \
int name##_CAP
#define laikaM_initVector(name, startCap) \
name = NULL; \
name##_COUNT = 0; \
name##_CAP = startCap
#define laikaM_growVector(type, name, needed) \
if (name##_COUNT + needed >= name##_CAP || name == NULL) { \
name##_CAP = (name##_CAP + needed) * GROW_FACTOR; \
name = (type *)laikaM_realloc(name, sizeof(type) * name##_CAP); \
}
/* moves vector elements above indx down by numElem, removing numElem elements at indx */
#define laikaM_rmvVector(name, indx, numElem) \
do { \
int _i, _sz = ((name##_COUNT - indx) - numElem); \
for (_i = 0; _i < _sz; _i++) \
name[indx + _i] = name[indx + numElem + _i]; \
name##_COUNT -= numElem; \
} while (0);
/* moves vector elements above indx up by numElem, inserting numElem elements at indx */
#define laikaM_insertVector(name, indx, numElem) \
do { \
int _i; \
for (_i = name##_COUNT; _i > indx; _i--) \
name[_i] = name[_i - 1]; \
name##_COUNT += numElem; \
} while (0);
void *laikaM_realloc(void *buf, size_t sz);
#endif

View File

@ -9,8 +9,8 @@
fit this specific use case. fit this specific use case.
*/ */
#include "core/lerror.h"
#include "laika.h" #include "laika.h"
#include "lerror.h"
#include <inttypes.h> #include <inttypes.h>

View File

@ -1,46 +0,0 @@
#ifndef LAIKA_MEM_H
#define LAIKA_MEM_H
#include "laika.h"
#define GROW_FACTOR 2
/* microsoft strikes again with their lack of support for VLAs */
#if _MSC_VER
# define VLA(type, var, sz) type *var = laikaM_malloc(sizeof(type) * sz);
# define ENDVLA(var) laikaM_free(var);
#else
# define VLA(type, var, sz) type var[sz];
# define ENDVLA(var) ((void)0) /* no op */
#endif
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
#define laikaM_free(buf) laikaM_realloc(buf, 0)
#define laikaM_growarray(type, buf, needed, count, capacity) \
if (count + needed >= capacity || buf == NULL) { \
capacity = (capacity + needed) * GROW_FACTOR; \
buf = (type *)laikaM_realloc(buf, sizeof(type) * capacity); \
}
/* moves array elements above indx down by numElem, removing numElem elements at indx */
#define laikaM_rmvarray(buf, count, indx, numElem) \
do { \
int _i, _sz = ((count - indx) - numElem); \
for (_i = 0; _i < _sz; _i++) \
buf[indx + _i] = buf[indx + numElem + _i]; \
count -= numElem; \
} while (0);
/* moves array elements above indx up by numElem, inserting numElem elements at indx */
#define laikaM_insertarray(buf, count, indx, numElem) \
do { \
int _i; \
for (_i = count; _i > indx; _i--) \
buf[_i] = buf[_i - 1]; \
count += numElem; \
} while (0);
void *laikaM_realloc(void *buf, size_t sz);
#endif

View File

@ -1,11 +1,11 @@
#ifndef LAIKA_PEER_H #ifndef LAIKA_PEER_H
#define LAIKA_PEER_H #define LAIKA_PEER_H
#include "core/lsodium.h"
#include "laika.h" #include "laika.h"
#include "lpacket.h" #include "net/lpacket.h"
#include "lpolllist.h" #include "net/lpolllist.h"
#include "lsocket.h" #include "net/lsocket.h"
#include "lsodium.h"
typedef enum typedef enum
{ {

View File

@ -1,9 +1,10 @@
#ifndef LAIKA_POLLLIST_H #ifndef LAIKA_POLLLIST_H
#define LAIKA_POLLLIST_H #define LAIKA_POLLLIST_H
#include "hashmap.h" #include "core/hashmap.h"
#include "core/lmem.h"
#include "laika.h" #include "laika.h"
#include "lsocket.h" #include "net/lsocket.h"
#include <stdbool.h> #include <stdbool.h>
@ -20,22 +21,17 @@ struct sLaika_pollEvent
struct sLaika_pollList struct sLaika_pollList
{ {
struct hashmap *sockets; struct hashmap *sockets;
struct sLaika_socket **outQueue; /* holds sockets which have data needed to be sent */ /* holds sockets which have data needed to be sent */
struct sLaika_pollEvent *revents; laikaM_newVector(struct sLaika_socket *, outQueue);
laikaM_newVector(struct sLaika_pollEvent, revents);
#ifdef LAIKA_USE_EPOLL #ifdef LAIKA_USE_EPOLL
/* epoll */ /* epoll */
struct epoll_event ev, ep_events[MAX_EPOLL_EVENTS]; struct epoll_event ev, ep_events[MAX_EPOLL_EVENTS];
SOCKET epollfd; SOCKET epollfd;
#else #else
/* raw poll descriptor */ /* raw poll descriptor */
PollFD *fds; laikaM_newVector(PollFD, fds);
int fdCapacity;
int fdCount;
#endif #endif
int reventCap;
int reventCount;
int outCap;
int outCount;
}; };
void laikaP_initPList(struct sLaika_pollList *pList); void laikaP_initPList(struct sLaika_pollList *pList);

View File

@ -3,7 +3,7 @@
/* clang-format will change the order of the included windows headers, this BREAKS THINGS. /* clang-format will change the order of the included windows headers, this BREAKS THINGS.
for now, make clang ignore this section */ for now, make clang ignore this section */
/* clang-format off */ /* clang-format off */
/* socket/winsock headers */ /* socket/winsock headers */
@ -54,7 +54,8 @@ typedef void buffer_t;
# define SOCKETERROR(x) (x == -1) # define SOCKETERROR(x) (x == -1)
#endif #endif
#include "laika.h" #include "laika.h"
#include "lsodium.h" #include "core/lsodium.h"
#include "core/lmem.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
@ -78,13 +79,9 @@ struct sLaika_socket
pollFailEvent onPollFail; pollFailEvent onPollFail;
pollEvent onPollIn; pollEvent onPollIn;
pollEvent onPollOut; pollEvent onPollOut;
void *uData; /* passed to onPollFail */ void *uData; /* passed to onPollFail */
uint8_t *outBuf; /* raw data to be sent() */ laikaM_newVector(uint8_t, outBuf); /* raw data to be sent() */
uint8_t *inBuf; /* raw data we recv()'d */ laikaM_newVector(uint8_t, inBuf); /* raw data we recv()'d */
int outCount;
int inCount;
int outCap;
int inCap;
bool flipEndian; bool flipEndian;
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */ bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
}; };

File diff suppressed because it is too large Load Diff

View File

@ -12,59 +12,60 @@ https://github.com/benhoyt/inih
*/ */
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS # define _CRT_SECURE_NO_WARNINGS
#endif #endif
#include <stdio.h> #include "core/ini.h"
#include <ctype.h> #include <ctype.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include "ini.h"
#if !INI_USE_STACK #if !INI_USE_STACK
#if INI_CUSTOM_ALLOCATOR # if INI_CUSTOM_ALLOCATOR
#include <stddef.h> # include <stddef.h>
void* ini_malloc(size_t size); void *ini_malloc(size_t size);
void ini_free(void* ptr); void ini_free(void *ptr);
void* ini_realloc(void* ptr, size_t size); void *ini_realloc(void *ptr, size_t size);
#else # else
#include <stdlib.h> # include <stdlib.h>
#define ini_malloc malloc # define ini_malloc malloc
#define ini_free free # define ini_free free
#define ini_realloc realloc # define ini_realloc realloc
#endif # endif
#endif #endif
#define MAX_SECTION 50 #define MAX_SECTION 50
#define MAX_NAME 50 #define MAX_NAME 50
/* Used by ini_parse_string() to keep track of string parsing state. */ /* Used by ini_parse_string() to keep track of string parsing state. */
typedef struct { typedef struct
const char* ptr; {
const char *ptr;
size_t num_left; size_t num_left;
} ini_parse_string_ctx; } ini_parse_string_ctx;
/* Strip whitespace chars off end of given string, in place. Return s. */ /* Strip whitespace chars off end of given string, in place. Return s. */
static char* rstrip(char* s) static char *rstrip(char *s)
{ {
char* p = s + strlen(s); char *p = s + strlen(s);
while (p > s && isspace((unsigned char)(*--p))) while (p > s && isspace((unsigned char)(*--p)))
*p = '\0'; *p = '\0';
return s; return s;
} }
/* Return pointer to first non-whitespace char in given string. */ /* Return pointer to first non-whitespace char in given string. */
static char* lskip(const char* s) static char *lskip(const char *s)
{ {
while (*s && isspace((unsigned char)(*s))) while (*s && isspace((unsigned char)(*s)))
s++; s++;
return (char*)s; return (char *)s;
} }
/* Return pointer to first char (of chars) or inline comment in given string, /* Return pointer to first char (of chars) or inline comment in given string,
or pointer to NUL at end of string if neither found. Inline comment must or pointer to NUL at end of string if neither found. Inline comment must
be prefixed by a whitespace character to register as a comment. */ be prefixed by a whitespace character to register as a comment. */
static char* find_chars_or_comment(const char* s, const char* chars) static char *find_chars_or_comment(const char *s, const char *chars)
{ {
#if INI_ALLOW_INLINE_COMMENTS #if INI_ALLOW_INLINE_COMMENTS
int was_space = 0; int was_space = 0;
@ -78,12 +79,12 @@ static char* find_chars_or_comment(const char* s, const char* chars)
s++; s++;
} }
#endif #endif
return (char*)s; return (char *)s;
} }
/* Similar to strncpy, but ensures dest (size bytes) is /* Similar to strncpy, but ensures dest (size bytes) is
NUL-terminated, and doesn't pad with NULs. */ NUL-terminated, and doesn't pad with NULs. */
static char* strncpy0(char* dest, const char* src, size_t size) static char *strncpy0(char *dest, const char *src, size_t size)
{ {
/* Could use strncpy internally, but it causes gcc warnings (see issue #91) */ /* Could use strncpy internally, but it causes gcc warnings (see issue #91) */
size_t i; size_t i;
@ -94,42 +95,41 @@ static char* strncpy0(char* dest, const char* src, size_t size)
} }
/* See documentation in header file. */ /* See documentation in header file. */
int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, int ini_parse_stream(ini_reader reader, void *stream, ini_handler handler, void *user)
void* user)
{ {
/* Uses a fair bit of stack (use heap instead if you need to) */ /* Uses a fair bit of stack (use heap instead if you need to) */
#if INI_USE_STACK #if INI_USE_STACK
char line[INI_MAX_LINE]; char line[INI_MAX_LINE];
int max_line = INI_MAX_LINE; int max_line = INI_MAX_LINE;
#else #else
char* line; char *line;
size_t max_line = INI_INITIAL_ALLOC; size_t max_line = INI_INITIAL_ALLOC;
#endif #endif
#if INI_ALLOW_REALLOC && !INI_USE_STACK #if INI_ALLOW_REALLOC && !INI_USE_STACK
char* new_line; char *new_line;
size_t offset; size_t offset;
#endif #endif
char section[MAX_SECTION] = ""; char section[MAX_SECTION] = "";
char prev_name[MAX_NAME] = ""; char prev_name[MAX_NAME] = "";
char* start; char *start;
char* end; char *end;
char* name; char *name;
char* value; char *value;
int lineno = 0; int lineno = 0;
int error = 0; int error = 0;
#if !INI_USE_STACK #if !INI_USE_STACK
line = (char*)ini_malloc(INI_INITIAL_ALLOC); line = (char *)ini_malloc(INI_INITIAL_ALLOC);
if (!line) { if (!line) {
return -2; return -2;
} }
#endif #endif
#if INI_HANDLER_LINENO #if INI_HANDLER_LINENO
#define HANDLER(u, s, n, v) handler(u, s, n, v, lineno) # define HANDLER(u, s, n, v) handler(u, s, n, v, lineno)
#else #else
#define HANDLER(u, s, n, v) handler(u, s, n, v) # define HANDLER(u, s, n, v) handler(u, s, n, v)
#endif #endif
/* Scan through stream line by line */ /* Scan through stream line by line */
@ -158,9 +158,8 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
start = line; start = line;
#if INI_ALLOW_BOM #if INI_ALLOW_BOM
if (lineno == 1 && (unsigned char)start[0] == 0xEF && if (lineno == 1 && (unsigned char)start[0] == 0xEF && (unsigned char)start[1] == 0xBB &&
(unsigned char)start[1] == 0xBB && (unsigned char)start[2] == 0xBF) {
(unsigned char)start[2] == 0xBF) {
start += 3; start += 3;
} }
#endif #endif
@ -188,13 +187,11 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
if (!HANDLER(user, section, NULL, NULL) && !error) if (!HANDLER(user, section, NULL, NULL) && !error)
error = lineno; error = lineno;
#endif #endif
} } else if (!error) {
else if (!error) {
/* No ']' found on section line */ /* No ']' found on section line */
error = lineno; error = lineno;
} }
} } else if (*start) {
else if (*start) {
/* Not a comment, must be a name[=:]value pair */ /* Not a comment, must be a name[=:]value pair */
end = find_chars_or_comment(start, "=:"); end = find_chars_or_comment(start, "=:");
if (*end == '=' || *end == ':') { if (*end == '=' || *end == ':') {
@ -213,8 +210,7 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
strncpy0(prev_name, name, sizeof(prev_name)); strncpy0(prev_name, name, sizeof(prev_name));
if (!HANDLER(user, section, name, value) && !error) if (!HANDLER(user, section, name, value) && !error)
error = lineno; error = lineno;
} } else if (!error) {
else if (!error) {
/* No '=' or ':' found on name[=:]value line */ /* No '=' or ':' found on name[=:]value line */
#if INI_ALLOW_NO_VALUE #if INI_ALLOW_NO_VALUE
*end = '\0'; *end = '\0';
@ -241,15 +237,15 @@ int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
} }
/* See documentation in header file. */ /* See documentation in header file. */
int ini_parse_file(FILE* file, ini_handler handler, void* user) int ini_parse_file(FILE *file, ini_handler handler, void *user)
{ {
return ini_parse_stream((ini_reader)fgets, file, handler, user); return ini_parse_stream((ini_reader)fgets, file, handler, user);
} }
/* See documentation in header file. */ /* See documentation in header file. */
int ini_parse(const char* filename, ini_handler handler, void* user) int ini_parse(const char *filename, ini_handler handler, void *user)
{ {
FILE* file; FILE *file;
int error; int error;
file = fopen(filename, "r"); file = fopen(filename, "r");
@ -262,11 +258,12 @@ int ini_parse(const char* filename, ini_handler handler, void* user)
/* An ini_reader function to read the next line from a string buffer. This /* An ini_reader function to read the next line from a string buffer. This
is the fgets() equivalent used by ini_parse_string(). */ is the fgets() equivalent used by ini_parse_string(). */
static char* ini_reader_string(char* str, int num, void* stream) { static char *ini_reader_string(char *str, int num, void *stream)
ini_parse_string_ctx* ctx = (ini_parse_string_ctx*)stream; {
const char* ctx_ptr = ctx->ptr; ini_parse_string_ctx *ctx = (ini_parse_string_ctx *)stream;
const char *ctx_ptr = ctx->ptr;
size_t ctx_num_left = ctx->num_left; size_t ctx_num_left = ctx->num_left;
char* strp = str; char *strp = str;
char c; char c;
if (ctx_num_left == 0 || num < 2) if (ctx_num_left == 0 || num < 2)
@ -288,11 +285,11 @@ static char* ini_reader_string(char* str, int num, void* stream) {
} }
/* See documentation in header file. */ /* See documentation in header file. */
int ini_parse_string(const char* string, ini_handler handler, void* user) { int ini_parse_string(const char *string, ini_handler handler, void *user)
{
ini_parse_string_ctx ctx; ini_parse_string_ctx ctx;
ctx.ptr = string; ctx.ptr = string;
ctx.num_left = strlen(string); ctx.num_left = strlen(string);
return ini_parse_stream((ini_reader)ini_reader_string, &ctx, handler, return ini_parse_stream((ini_reader)ini_reader_string, &ctx, handler, user);
user);
} }

4
lib/src/core/lerror.c Normal file
View File

@ -0,0 +1,4 @@
#include "core/lerror.h"
jmp_buf eLaika_errStack[LAIKA_MAXERRORS];
int eLaika_errIndx = -1;

View File

@ -1,6 +1,6 @@
#include "lmem.h" #include "core/lmem.h"
#include "lerror.h" #include "core/lerror.h"
void *laikaM_realloc(void *buf, size_t sz) void *laikaM_realloc(void *buf, size_t sz)
{ {

View File

@ -1,4 +1,4 @@
#include "lsodium.h" #include "core/lsodium.h"
#include <string.h> #include <string.h>

View File

@ -1,6 +1,6 @@
#include "ltask.h" #include "core/ltask.h"
#include "lmem.h" #include "core/lmem.h"
/* this is the only reason C11 support is needed, i cba to write windows/linux specific stuff to get /* this is the only reason C11 support is needed, i cba to write windows/linux specific stuff to get
the current time in ms also side note: microsoft? more like micropenis */ the current time in ms also side note: microsoft? more like micropenis */

1
lib/src/core/lvm.c Normal file
View File

@ -0,0 +1 @@
#include "core/lvm.h"

View File

@ -1,4 +0,0 @@
#include "lerror.h"
jmp_buf eLaika_errStack[LAIKA_MAXERRORS];
int eLaika_errIndx = -1;

View File

@ -1 +0,0 @@
#include "lvm.h"

View File

@ -1,4 +1,4 @@
#include "lpacket.h" #include "net/lpacket.h"
#ifdef DEBUG #ifdef DEBUG
const char *laikaD_getPacketName(LAIKAPKT_ID id) const char *laikaD_getPacketName(LAIKAPKT_ID id)

View File

@ -1,7 +1,7 @@
#include "lpeer.h" #include "net/lpeer.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl,
struct sLaika_pollList *pList, pollFailEvent onPollFail, struct sLaika_pollList *pList, pollFailEvent onPollFail,
@ -71,7 +71,7 @@ void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id)
laikaS_writeByte(sock, id); laikaS_writeByte(sock, id);
peer->outStart = sock->outCount; peer->outStart = laikaM_countVector(sock->outBuf);
if (peer->useSecure) { /* if we're encrypting this packet, append the nonce right after the if (peer->useSecure) { /* if we're encrypting this packet, append the nonce right after the
packet ID */ packet ID */
uint8_t nonce[crypto_secretbox_NONCEBYTES]; uint8_t nonce[crypto_secretbox_NONCEBYTES];
@ -88,26 +88,26 @@ int laikaS_endOutPacket(struct sLaika_peer *peer)
if (peer->useSecure) { if (peer->useSecure) {
/* make sure we have enough space */ /* make sure we have enough space */
laikaM_growarray(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES, sock->outCount, laikaM_growVector(uint8_t, sock->outBuf, crypto_secretbox_MACBYTES);
sock->outCap);
/* packet body starts after the id & nonce */ /* packet body starts after the id & nonce */
body = &sock->outBuf[peer->outStart + crypto_secretbox_NONCEBYTES]; body = &sock->outBuf[peer->outStart + crypto_secretbox_NONCEBYTES];
/* encrypt packet body in-place */ /* encrypt packet body in-place */
if (crypto_secretbox_easy(body, body, if (crypto_secretbox_easy(body, body,
(sock->outCount - peer->outStart) - crypto_secretbox_NONCEBYTES, (laikaM_countVector(sock->outBuf) - peer->outStart) -
crypto_secretbox_NONCEBYTES,
&sock->outBuf[peer->outStart], peer->outKey) != 0) { &sock->outBuf[peer->outStart], peer->outKey) != 0) {
LAIKA_ERROR("Failed to encrypt packet!\n"); LAIKA_ERROR("Failed to encrypt packet!\n");
} }
sock->outCount += crypto_secretbox_MACBYTES; laikaM_countVector(sock->outBuf) += crypto_secretbox_MACBYTES;
} }
/* add to pollList's out queue */ /* add to pollList's out queue */
laikaP_pushOutQueue(peer->pList, &peer->sock); laikaP_pushOutQueue(peer->pList, &peer->sock);
/* return packet size and prepare for next outPacket */ /* return packet size and prepare for next outPacket */
sz = sock->outCount - peer->outStart; sz = laikaM_countVector(sock->outBuf) - peer->outStart;
peer->outStart = -1; peer->outStart = -1;
return sz; return sz;
} }
@ -148,30 +148,31 @@ void laikaS_startInPacket(struct sLaika_peer *peer, bool variadic)
if (peer->useSecure && !variadic && peer->pktSize != 0) if (peer->useSecure && !variadic && peer->pktSize != 0)
peer->pktSize += crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES; peer->pktSize += crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
peer->inStart = sock->inCount; peer->inStart = laikaM_countVector(sock->inBuf);
} }
int laikaS_endInPacket(struct sLaika_peer *peer) int laikaS_endInPacket(struct sLaika_peer *peer)
{ {
struct sLaika_socket *sock = &peer->sock; struct sLaika_socket *sock = &peer->sock;
uint8_t *body; uint8_t *body;
size_t sz = sock->inCount - peer->inStart; size_t sz = laikaM_countVector(sock->inBuf) - peer->inStart;
if (peer->useSecure && sz > crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES) { if (peer->useSecure && sz > crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES) {
body = &sock->inBuf[peer->inStart + crypto_secretbox_NONCEBYTES]; body = &sock->inBuf[peer->inStart + crypto_secretbox_NONCEBYTES];
/* decrypt packet body in-place */ /* decrypt packet body in-place */
if (crypto_secretbox_open_easy( if (crypto_secretbox_open_easy(body, body,
body, body, (sock->inCount - peer->inStart) - crypto_secretbox_NONCEBYTES, (laikaM_countVector(sock->inBuf) - peer->inStart) -
&sock->inBuf[peer->inStart], peer->inKey) != 0) { crypto_secretbox_NONCEBYTES,
&sock->inBuf[peer->inStart], peer->inKey) != 0) {
LAIKA_ERROR("Failed to decrypt packet!\n"); LAIKA_ERROR("Failed to decrypt packet!\n");
} }
/* decrypted message is smaller now */ /* decrypted message is smaller now */
sock->inCount -= crypto_secretbox_MACBYTES; laikaM_countVector(sock->inBuf) -= crypto_secretbox_MACBYTES;
/* remove nonce */ /* remove nonce */
laikaM_rmvarray(sock->inBuf, sock->inCount, peer->inStart, crypto_secretbox_NONCEBYTES); laikaM_rmvVector(sock->inBuf, peer->inStart, crypto_secretbox_NONCEBYTES);
sz -= crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES; sz -= crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
} }
@ -254,18 +255,19 @@ bool laikaS_handlePeerIn(struct sLaika_socket *sock)
default: default:
_HandlePacketBody: _HandlePacketBody:
/* try grabbing the rest of the packet */ /* try grabbing the rest of the packet */
if (laikaS_rawRecv(&peer->sock, peer->pktSize - peer->sock.inCount, &recvd) != RAWSOCK_OK) if (laikaS_rawRecv(&peer->sock, peer->pktSize - laikaM_countVector(peer->sock.inBuf),
&recvd) != RAWSOCK_OK)
return false; return false;
/* have we received the full packet? */ /* have we received the full packet? */
if (peer->pktSize == peer->sock.inCount) { if (peer->pktSize == laikaM_countVector(peer->sock.inBuf)) {
peer->pktSize = laikaS_endInPacket(peer); peer->pktSize = laikaS_endInPacket(peer);
/* dispatch to packet handler */ /* dispatch to packet handler */
peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData); peer->packetTbl[peer->pktID].handler(peer, peer->pktSize, peer->uData);
/* reset */ /* reset */
peer->sock.inCount = 0; laikaM_countVector(peer->sock.inBuf) = 0;
peer->pktID = LAIKAPKT_MAXNONE; peer->pktID = LAIKAPKT_MAXNONE;
} }
@ -280,10 +282,10 @@ bool laikaS_handlePeerOut(struct sLaika_socket *sock)
struct sLaika_peer *peer = (struct sLaika_peer *)sock; struct sLaika_peer *peer = (struct sLaika_peer *)sock;
int sent; int sent;
if (peer->sock.outCount == 0) /* sanity check */ if (laikaM_countVector(peer->sock.outBuf) == 0) /* sanity check */
return true; return true;
switch (laikaS_rawSend(&peer->sock, peer->sock.outCount, &sent)) { switch (laikaS_rawSend(&peer->sock, laikaM_countVector(peer->sock.outBuf), &sent)) {
case RAWSOCK_OK: /* we're ok! */ case RAWSOCK_OK: /* we're ok! */
/* if POLLOUT was set, unset it */ /* if POLLOUT was set, unset it */
laikaP_rmvPollOut(peer->pList, &peer->sock); laikaP_rmvPollOut(peer->pList, &peer->sock);

View File

@ -1,7 +1,7 @@
#include "lpolllist.h" #include "net/lpolllist.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
/* ===================================[[ Helper Functions ]]==================================== */ /* ===================================[[ Helper Functions ]]==================================== */
@ -34,12 +34,10 @@ void laikaP_initPList(struct sLaika_pollList *pList)
/* setup hashmap */ /* setup hashmap */
pList->sockets = hashmap_new(sizeof(tLaika_hashMapElem), POLLSTARTCAP, 0, 0, elem_hash, pList->sockets = hashmap_new(sizeof(tLaika_hashMapElem), POLLSTARTCAP, 0, 0, elem_hash,
elem_compare, NULL, NULL); elem_compare, NULL, NULL);
pList->revents = NULL; /* laikaP_pollList() will allocate the buffer */
pList->reventCap = POLLSTARTCAP / GROW_FACTOR; /* laikaP_pollList() will allocate these buffer */
pList->reventCount = 0; laikaM_initVector(pList->revents, POLLSTARTCAP / GROW_FACTOR);
pList->outQueue = NULL; laikaM_initVector(pList->outQueue, POLLSTARTCAP / GROW_FACTOR);
pList->outCap = POLLSTARTCAP / GROW_FACTOR;
pList->outCount = 0;
#ifdef LAIKA_USE_EPOLL #ifdef LAIKA_USE_EPOLL
/* setup our epoll */ /* setup our epoll */
@ -48,11 +46,8 @@ void laikaP_initPList(struct sLaika_pollList *pList)
LAIKA_ERROR("epoll_create() failed!\n"); LAIKA_ERROR("epoll_create() failed!\n");
#else #else
pList->fds = NULL; /* laikaP_addSock will allocate the buffer */ /* laikaP_addSock will allocate this buffer */
pList->fdCapacity = laikaM_initVector(pList->fds, POLLSTARTCAP / GROW_FACTOR);
POLLSTARTCAP /
GROW_FACTOR; /* div by GROW_FACTOR since laikaM_growarray multiplies by GROW_FACTOR */
pList->fdCount = 0;
#endif #endif
} }
@ -85,8 +80,8 @@ void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
#else #else
/* allocate space in array & add PollFD */ /* allocate space in array & add PollFD */
laikaM_growarray(PollFD, pList->fds, 1, pList->fdCount, pList->fdCapacity); laikaM_growVector(PollFD, pList->fds, 1);
pList->fds[pList->fdCount++] = (PollFD){sock->sock, POLLIN}; pList->fds[laikaM_countVector(pList->fds)++] = (PollFD){sock->sock, POLLIN};
#endif #endif
} }
@ -98,9 +93,9 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
hashmap_delete(pList->sockets, &(tLaika_hashMapElem){.fd = sock->sock, .sock = sock}); hashmap_delete(pList->sockets, &(tLaika_hashMapElem){.fd = sock->sock, .sock = sock});
/* make sure peer isn't in outQueue */ /* make sure peer isn't in outQueue */
for (i = 0; i < pList->outCount; i++) { for (i = 0; i < laikaM_countVector(pList->outQueue); i++) {
if ((void *)pList->outQueue[i] == (void *)sock) { if ((void *)pList->outQueue[i] == (void *)sock) {
laikaM_rmvarray(pList->outQueue, pList->outCount, i, 1); laikaM_rmvVector(pList->outQueue, i, 1);
} }
} }
@ -114,10 +109,10 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock)
#else #else
/* search fds for socket, remove it and shrink array */ /* search fds for socket, remove it and shrink array */
for (i = 0; i < pList->fdCount; i++) { for (i = 0; i < laikaM_countVector(pList->fds); i++) {
if (pList->fds[i].fd == sock->sock) { if (pList->fds[i].fd == sock->sock) {
/* remove from array */ /* remove from array */
laikaM_rmvarray(pList->fds, pList->fdCount, i, 1); laikaM_rmvVector(pList->fds, i, 1);
break; break;
} }
} }
@ -140,7 +135,7 @@ void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
int i; int i;
/* search fds for socket, add POLLOUT flag */ /* search fds for socket, add POLLOUT flag */
for (i = 0; i < pList->fdCount; i++) { for (i = 0; i < laikaM_countVector(pList->fds); i++) {
if (pList->fds[i].fd == sock->sock) { if (pList->fds[i].fd == sock->sock) {
pList->fds[i].events = POLLIN | POLLOUT; pList->fds[i].events = POLLIN | POLLOUT;
break; break;
@ -167,7 +162,7 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
int i; int i;
/* search fds for socket, remove POLLOUT flag */ /* search fds for socket, remove POLLOUT flag */
for (i = 0; i < pList->fdCount; i++) { for (i = 0; i < laikaM_countVector(pList->fds); i++) {
if (pList->fds[i].fd == sock->sock) { if (pList->fds[i].fd == sock->sock) {
pList->fds[i].events = POLLIN; pList->fds[i].events = POLLIN;
break; break;
@ -183,18 +178,18 @@ void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_socket *so
int i; int i;
/* first, check that we don't have this peer in the queue already */ /* first, check that we don't have this peer in the queue already */
for (i = 0; i < pList->outCount; i++) { for (i = 0; i < laikaM_countVector(pList->outQueue); i++) {
if (pList->outQueue[i] == sock) if (pList->outQueue[i] == sock)
return; /* found it :) */ return; /* found it :) */
} }
laikaM_growarray(struct sLaika_socket *, pList->outQueue, 1, pList->outCount, pList->outCap); laikaM_growVector(struct sLaika_socket *, pList->outQueue, 1);
pList->outQueue[pList->outCount++] = sock; pList->outQueue[laikaM_countVector(pList->outQueue)++] = sock;
} }
void laikaP_resetOutQueue(struct sLaika_pollList *pList) void laikaP_resetOutQueue(struct sLaika_pollList *pList)
{ {
pList->outCount = 0; /* ez lol */ laikaM_countVector(pList->outQueue) = 0; /* ez lol */
} }
void laikaP_flushOutQueue(struct sLaika_pollList *pList) void laikaP_flushOutQueue(struct sLaika_pollList *pList)
@ -203,7 +198,7 @@ void laikaP_flushOutQueue(struct sLaika_pollList *pList)
int i; int i;
/* flush pList's outQueue */ /* flush pList's outQueue */
for (i = 0; i < pList->outCount; i++) { for (i = 0; i < laikaM_countVector(pList->outQueue); i++) {
sock = pList->outQueue[i]; sock = pList->outQueue[i];
LAIKA_DEBUG("sending OUT to %p\n", sock); LAIKA_DEBUG("sending OUT to %p\n", sock);
if (sock->onPollOut && !sock->onPollOut(sock) && sock->onPollFail) if (sock->onPollOut && !sock->onPollOut(sock) && sock->onPollFail)
@ -216,7 +211,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
{ {
int nEvents, i; int nEvents, i;
pList->reventCount = 0; /* reset revent array */ laikaM_countVector(pList->revents) = 0; /* reset revent array */
#ifdef LAIKA_USE_EPOLL #ifdef LAIKA_USE_EPOLL
/* fastpath: we store the sLaika_socket* pointer directly in the epoll_data_t, saving us a /* fastpath: we store the sLaika_socket* pointer directly in the epoll_data_t, saving us a
lookup into our socket hashmap not to mention the various improvements epoll() has over lookup into our socket hashmap not to mention the various improvements epoll() has over
@ -229,22 +224,21 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
for (i = 0; i < nEvents; i++) { for (i = 0; i < nEvents; i++) {
/* add event to revent array */ /* add event to revent array */
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, laikaM_growVector(struct sLaika_pollEvent, pList->revents, 1);
pList->reventCap); pList->revents[laikaM_countVector(pList->revents)++] =
pList->revents[pList->reventCount++] =
(struct sLaika_pollEvent){.sock = pList->ep_events[i].data.ptr, (struct sLaika_pollEvent){.sock = pList->ep_events[i].data.ptr,
.pollIn = pList->ep_events[i].events & EPOLLIN, .pollIn = pList->ep_events[i].events & EPOLLIN,
.pollOut = pList->ep_events[i].events & EPOLLOUT}; .pollOut = pList->ep_events[i].events & EPOLLOUT};
} }
#else #else
nEvents = poll(pList->fds, pList->fdCount, /* poll returns -1 for error, or the number of events */
timeout); /* poll returns -1 for error, or the number of events */ nEvents = poll(pList->fds, laikaM_countVector(pList->fds), timeout);
if (SOCKETERROR(nEvents)) if (SOCKETERROR(nEvents))
LAIKA_ERROR("poll() failed!\n"); LAIKA_ERROR("poll() failed!\n");
/* walk through the returned poll fds, if they have an event, add it to our revents array */ /* walk through the returned poll fds, if they have an event, add it to our revents array */
for (i = 0; i < pList->fdCount && nEvents > 0; i++) { for (i = 0; i < laikaM_countVector(pList->fds) && nEvents > 0; i++) {
PollFD pfd = pList->fds[i]; PollFD pfd = pList->fds[i];
if (pList->fds[i].revents != 0) { if (pList->fds[i].revents != 0) {
/* grab socket from hashmap */ /* grab socket from hashmap */
@ -252,9 +246,8 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
pList->sockets, &(tLaika_hashMapElem){.fd = (SOCKET)pfd.fd}); pList->sockets, &(tLaika_hashMapElem){.fd = (SOCKET)pfd.fd});
/* insert event into revents array */ /* insert event into revents array */
laikaM_growarray(struct sLaika_pollEvent, pList->revents, 1, pList->reventCount, laikaM_growVector(struct sLaika_pollEvent, pList->revents, 1);
pList->reventCap); pList->revents[laikaM_countVector(pList->revents)++] =
pList->revents[pList->reventCount++] =
(struct sLaika_pollEvent){.sock = elem->sock, (struct sLaika_pollEvent){.sock = elem->sock,
.pollIn = pfd.revents & POLLIN, .pollIn = pfd.revents & POLLIN,
.pollOut = pfd.revents & POLLOUT}; .pollOut = pfd.revents & POLLOUT};
@ -264,7 +257,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
} }
#endif #endif
*_nevents = pList->reventCount; *_nevents = laikaM_countVector(pList->revents);
/* return revents array */ /* return revents array */
return pList->revents; return pList->revents;

View File

@ -1,10 +1,10 @@
#include "lsocket.h" #include "net/lsocket.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
#include "lpacket.h" #include "core/lsodium.h"
#include "lpolllist.h" #include "net/lpacket.h"
#include "lsodium.h" #include "net/lpolllist.h"
static int _LNSetup = 0; static int _LNSetup = 0;
@ -50,12 +50,8 @@ void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent
sock->onPollIn = onPollIn; sock->onPollIn = onPollIn;
sock->onPollOut = onPollOut; sock->onPollOut = onPollOut;
sock->uData = uData; sock->uData = uData;
sock->inBuf = NULL; laikaM_initVector(sock->inBuf, 8);
sock->inCap = 8; laikaM_initVector(sock->outBuf, 8);
sock->inCount = 0;
sock->outBuf = NULL;
sock->outCap = 8;
sock->outCount = 0;
sock->flipEndian = false; sock->flipEndian = false;
sock->setPollOut = false; sock->setPollOut = false;
@ -197,44 +193,44 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock)
void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz) void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz)
{ {
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, sz); laikaM_rmvVector(sock->inBuf, 0, sz);
} }
void laikaS_zeroWrite(struct sLaika_socket *sock, size_t sz) void laikaS_zeroWrite(struct sLaika_socket *sock, size_t sz)
{ {
laikaM_growarray(uint8_t, sock->outBuf, sz, sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, sz);
/* set NULL bytes */ /* set NULL bytes */
memset(&sock->outBuf[sock->outCount], 0, sz); memset(&sock->outBuf[laikaM_countVector(sock->outBuf)], 0, sz);
sock->outCount += sz; laikaM_countVector(sock->outBuf) += sz;
} }
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz) void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz)
{ {
memcpy(buf, sock->inBuf, sz); memcpy(buf, sock->inBuf, sz);
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, sz); laikaM_rmvVector(sock->inBuf, 0, sz);
} }
void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz) void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz)
{ {
/* make sure we have enough space to copy the buffer */ /* make sure we have enough space to copy the buffer */
laikaM_growarray(uint8_t, sock->outBuf, sz, sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, sz);
/* copy the buffer, then increment outCount */ /* copy the buffer, then increment outCount */
memcpy(&sock->outBuf[sock->outCount], buf, sz); memcpy(&sock->outBuf[laikaM_countVector(sock->outBuf)], buf, sz);
sock->outCount += sz; laikaM_countVector(sock->outBuf) += sz;
} }
void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub) void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub)
{ {
/* make sure we have enough space to encrypt the buffer */ /* make sure we have enough space to encrypt the buffer */
laikaM_growarray(uint8_t, sock->outBuf, LAIKAENC_SIZE(sz), sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, LAIKAENC_SIZE(sz));
/* encrypt the buffer into outBuf */ /* encrypt the buffer into outBuf */
if (crypto_box_seal(&sock->outBuf[sock->outCount], buf, sz, pub) != 0) if (crypto_box_seal(&sock->outBuf[laikaM_countVector(sock->outBuf)], buf, sz, pub) != 0)
LAIKA_ERROR("Failed to encrypt!\n"); LAIKA_ERROR("Failed to encrypt!\n");
sock->outCount += LAIKAENC_SIZE(sz); laikaM_countVector(sock->outBuf) += LAIKAENC_SIZE(sz);
} }
void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub, void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub,
@ -244,21 +240,21 @@ void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uin
if (crypto_box_seal_open(buf, sock->inBuf, LAIKAENC_SIZE(sz), pub, priv) != 0) if (crypto_box_seal_open(buf, sock->inBuf, LAIKAENC_SIZE(sz), pub, priv) != 0)
LAIKA_ERROR("Failed to decrypt!\n"); LAIKA_ERROR("Failed to decrypt!\n");
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, LAIKAENC_SIZE(sz)); laikaM_rmvVector(sock->inBuf, 0, LAIKAENC_SIZE(sz));
} }
void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data) void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data)
{ {
laikaM_growarray(uint8_t, sock->outBuf, 1, sock->outCount, sock->outCap); laikaM_growVector(uint8_t, sock->outBuf, 1);
sock->outBuf[sock->outCount++] = data; sock->outBuf[laikaM_countVector(sock->outBuf)++] = data;
} }
uint8_t laikaS_readByte(struct sLaika_socket *sock) uint8_t laikaS_readByte(struct sLaika_socket *sock)
{ {
uint8_t tmp = *sock->inBuf; uint8_t tmp = *sock->inBuf;
/* pop 1 byte */ /* consume 1 byte */
laikaM_rmvarray(sock->inBuf, sock->inCount, 0, 1); laikaM_rmvVector(sock->inBuf, 0, 1);
return tmp; return tmp;
} }
@ -302,15 +298,16 @@ void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz)
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed) RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed)
{ {
RAWSOCKCODE errCode = RAWSOCK_OK; RAWSOCKCODE errCode = RAWSOCK_OK;
int i, rcvd, start = sock->inCount; int i, rcvd, start = laikaM_countVector(sock->inBuf);
/* sanity check */ /* sanity check */
if (sz == 0) if (sz == 0)
return RAWSOCK_OK; return RAWSOCK_OK;
/* make sure we have enough space to recv */ /* make sure we have enough space to recv */
laikaM_growarray(uint8_t, sock->inBuf, sz, sock->inCount, sock->inCap); laikaM_growVector(uint8_t, sock->inBuf, sz);
rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[sock->inCount], sz, LN_MSG_NOSIGNAL); rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[laikaM_countVector(sock->inBuf)], sz,
LN_MSG_NOSIGNAL);
if (rcvd == 0) { if (rcvd == 0) {
errCode = RAWSOCK_CLOSED; errCode = RAWSOCK_CLOSED;
@ -340,7 +337,7 @@ RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed
#endif #endif
/* recv() worked, add rcvd to inCount */ /* recv() worked, add rcvd to inCount */
sock->inCount += rcvd; laikaM_countVector(sock->inBuf) += rcvd;
} }
*processed = rcvd; *processed = rcvd;
return errCode; return errCode;
@ -397,7 +394,7 @@ _rawWriteExit:
#endif #endif
/* trim sent data from outBuf */ /* trim sent data from outBuf */
laikaM_rmvarray(sock->outBuf, sock->outCount, 0, sentBytes); laikaM_rmvVector(sock->outBuf, 0, sentBytes);
*processed = sentBytes; *processed = sentBytes;
return errCode; return errCode;

View File

@ -1,10 +1,11 @@
#ifndef SHELLCLIENT_H #ifndef SHELLCLIENT_H
#define SHELLCLIENT_H #define SHELLCLIENT_H
#include "hashmap.h" #include "core/hashmap.h"
#include "lpeer.h" #include "core/lmem.h"
#include "lsodium.h" #include "core/lsodium.h"
#include "ltask.h" #include "core/ltask.h"
#include "net/lpeer.h"
#include "speer.h" #include "speer.h"
typedef struct sShell_client typedef struct sShell_client
@ -15,9 +16,7 @@ typedef struct sShell_client
struct sLaika_peer *peer; struct sLaika_peer *peer;
tShell_peer *openShell; /* if not NULL, shell is open on peer */ tShell_peer *openShell; /* if not NULL, shell is open on peer */
struct hashmap *peers; struct hashmap *peers;
tShell_peer **peerTbl; laikaM_newVector(tShell_peer *, peerTbl);
int peerTblCount;
int peerTblCap;
} tShell_client; } tShell_client;
#define shellC_isShellOpen(x) (x->openShell != NULL) #define shellC_isShellOpen(x) (x->openShell != NULL)

View File

@ -1,8 +1,8 @@
#ifndef SHELLPEER_H #ifndef SHELLPEER_H
#define SHELLPEER_H #define SHELLPEER_H
#include "lpeer.h" #include "core/lsodium.h"
#include "lsodium.h" #include "net/lpeer.h"
typedef struct sShell_peer typedef struct sShell_peer
{ {

View File

@ -1,4 +1,4 @@
#include "ini.h" #include "core/ini.h"
#include "sclient.h" #include "sclient.h"
#include "sterm.h" #include "sterm.h"

View File

@ -1,9 +1,9 @@
#include "sclient.h" #include "sclient.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
#include "lpacket.h" #include "core/lsodium.h"
#include "lsodium.h" #include "net/lpacket.h"
#include "sterm.h" #include "sterm.h"
void shell_pingTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void shell_pingTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick,
@ -217,9 +217,7 @@ void shellC_init(tShell_client *client)
client->peers = hashmap_new(sizeof(tShell_hashMapElem), 8, 0, 0, shell_ElemHash, client->peers = hashmap_new(sizeof(tShell_hashMapElem), 8, 0, 0, shell_ElemHash,
shell_ElemCompare, NULL, NULL); shell_ElemCompare, NULL, NULL);
client->openShell = NULL; client->openShell = NULL;
client->peerTbl = NULL; laikaM_initVector(client->peerTbl, 4);
client->peerTblCap = 4;
client->peerTblCount = 0;
laikaT_initTaskService(&client->tService); laikaT_initTaskService(&client->tService);
laikaT_newTask(&client->tService, LAIKA_PING_INTERVAL, shell_pingTask, client); laikaT_newTask(&client->tService, LAIKA_PING_INTERVAL, shell_pingTask, client);
@ -254,7 +252,7 @@ void shellC_cleanup(tShell_client *client)
laikaT_cleanTaskService(&client->tService); laikaT_cleanTaskService(&client->tService);
/* free peers */ /* free peers */
for (i = 0; i < client->peerTblCount; i++) { for (i = 0; i < laikaM_countVector(client->peerTbl); i++) {
if (client->peerTbl[i]) if (client->peerTbl[i])
shellP_freePeer(client->peerTbl[i]); shellP_freePeer(client->peerTbl[i]);
} }
@ -346,16 +344,15 @@ int shellC_addPeer(tShell_client *client, tShell_peer *newPeer)
{ {
/* find empty ID */ /* find empty ID */
int id; int id;
for (id = 0; id < client->peerTblCount; id++) { for (id = 0; id < laikaM_countVector(client->peerTbl); id++) {
if (client->peerTbl[id] == NULL) /* it's empty! */ if (client->peerTbl[id] == NULL) /* it's empty! */
break; break;
} }
/* if we didn't find an empty id, grow the array */ /* if we didn't find an empty id, grow the array (ID is already set to the correct index) */
if (id == client->peerTblCount) { if (id == laikaM_countVector(client->peerTbl)) {
laikaM_growarray(tShell_peer *, client->peerTbl, 1, client->peerTblCount, laikaM_growVector(tShell_peer *, client->peerTbl, 1);
client->peerTblCap); laikaM_countVector(client->peerTbl)++;
client->peerTblCount++;
} }
/* add to peer lookup table */ /* add to peer lookup table */

View File

@ -1,7 +1,7 @@
#include "scmd.h" #include "scmd.h"
#include "lerror.h" #include "core/lerror.h"
#include "lmem.h" #include "core/lmem.h"
#include "sclient.h" #include "sclient.h"
#include "speer.h" #include "speer.h"
#include "sterm.h" #include "sterm.h"
@ -23,7 +23,7 @@ tShell_cmdDef *shellS_findCmd(char *cmd);
tShell_peer *shellS_getPeer(tShell_client *client, int id) tShell_peer *shellS_getPeer(tShell_client *client, int id)
{ {
if (id < 0 || id >= client->peerTblCount || client->peerTbl[id] == NULL) if (id < 0 || id >= laikaM_countVector(client->peerTbl) || client->peerTbl[id] == NULL)
CMD_ERROR("Not a valid peer ID! [%d]\n", id); CMD_ERROR("Not a valid peer ID! [%d]\n", id);
return client->peerTbl[id]; return client->peerTbl[id];
@ -48,7 +48,7 @@ void listPeersCMD(tShell_client *client, int argc, char *argv[])
{ {
int i; int i;
for (i = 0; i < client->peerTblCount; i++) { for (i = 0; i < laikaM_countVector(client->peerTbl); i++) {
if (client->peerTbl[i]) { if (client->peerTbl[i]) {
shellT_printf("%04d ", i); shellT_printf("%04d ", i);
shellP_printInfo(client->peerTbl[i]); shellP_printInfo(client->peerTbl[i]);
@ -172,11 +172,10 @@ void shellS_cleanupCmds(void)
char **shellS_splitCmd(char *cmd, int *argSize) char **shellS_splitCmd(char *cmd, int *argSize)
{ {
int argCount = 0;
int argCap = 4;
char *temp; char *temp;
char **args = NULL;
char *arg = cmd; char *arg = cmd;
laikaM_newVector(char *, args);
laikaM_initVector(args, 4);
do { do {
/* replace space with NULL terminator */ /* replace space with NULL terminator */
@ -192,12 +191,12 @@ char **shellS_splitCmd(char *cmd, int *argSize)
*arg++ = '\0'; *arg++ = '\0';
} }
/* insert into our 'args' array */ /* insert into our 'args' vector */
laikaM_growarray(char *, args, 1, argCount, argCap); laikaM_growVector(char *, args, 1);
args[argCount++] = arg; args[laikaM_countVector(args)++] = arg;
} while ((arg = strchr(arg, ' ')) != NULL); /* while we still have a delimiter */ } while ((arg = strchr(arg, ' ')) != NULL); /* while we still have a delimiter */
*argSize = argCount; *argSize = laikaM_countVector(args);
return args; return args;
} }

View File

@ -1,7 +1,7 @@
#include "speer.h" #include "speer.h"
#include "lmem.h" #include "core/lmem.h"
#include "lpacket.h" #include "net/lpacket.h"
#include "sterm.h" #include "sterm.h"
tShell_peer *shellP_newPeer(PEERTYPE type, OSTYPE osType, uint8_t *pubKey, char *hostname, tShell_peer *shellP_newPeer(PEERTYPE type, OSTYPE osType, uint8_t *pubKey, char *hostname,

View File

@ -1,6 +1,6 @@
#include "sterm.h" #include "sterm.h"
#include "lmem.h" #include "core/lmem.h"
#include "scmd.h" #include "scmd.h"
#define KEY_ESCAPE 0x001b #define KEY_ESCAPE 0x001b
@ -15,8 +15,42 @@
#define cursorBackward(x) printf("\033[%dD", (x)) #define cursorBackward(x) printf("\033[%dD", (x))
#define clearLine() printf("\033[2K") #define clearLine() printf("\033[2K")
/* =================================[[ DEPRECATED ARRAY API ]]================================== */
/*
this whole target needs to be rewritten, so these macros have been embedded here until a
rewrite can be done. this is the only section of the entire codebase that relies too heavily
on these to quickly exchange into the vector api equivalent. there's technically a memory leak
here since the array is never free'd, but since the array is expected to live the entire
lifetime of the program it's safe to leave as-is for now.
*/
#define laikaM_growarray(type, buf, needed, count, capacity) \
if (count + needed >= capacity || buf == NULL) { \
capacity = (capacity + needed) * GROW_FACTOR; \
buf = (type *)laikaM_realloc(buf, sizeof(type) * capacity); \
}
/* moves array elements above indx down by numElem, removing numElem elements at indx */
#define laikaM_rmvarray(buf, count, indx, numElem) \
do { \
int _i, _sz = ((count - indx) - numElem); \
for (_i = 0; _i < _sz; _i++) \
buf[indx + _i] = buf[indx + numElem + _i]; \
count -= numElem; \
} while (0);
/* moves array elements above indx up by numElem, inserting numElem elements at indx */
#define laikaM_insertarray(buf, count, indx, numElem) \
do { \
int _i; \
for (_i = count; _i > indx; _i--) \
buf[_i] = buf[_i - 1]; \
count += numElem; \
} while (0);
struct termios orig_termios; struct termios orig_termios;
char *cmd, *prompt = "$> "; char *cmd = NULL, *prompt = "$> ";
int cmdCount = 0, cmdCap = 4, cmdCursor = 0; int cmdCount = 0, cmdCap = 4, cmdCursor = 0;
void shellT_conioTerm(void) void shellT_conioTerm(void)

View File

@ -1,5 +1,5 @@
#include "lerror.h" #include "core/lerror.h"
#include "lsodium.h" #include "core/lsodium.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>

View File

@ -1,5 +1,5 @@
#include "lbox.h" #include "core/lbox.h"
#include "lvm.h" #include "core/lvm.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>