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

refactored laikaS_*Packet functions, added debugging output

This commit is contained in:
2022-02-03 20:51:32 -06:00
parent 5fe72e9eb0
commit 85d6cdcba1
7 changed files with 137 additions and 101 deletions

View File

@@ -27,7 +27,7 @@ enum {
/* layout of LAIKAPKT_HANDSHAKE_RES:
* uint8_t endian;
*/
//LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ,
LAIKAPKT_AUTHENTICATED_HANDSHAKE_REQ,
/* layout of LAIKAPKT_STAGE2_HANDSHAKE_REQ
* uint8_t peerType;
*/

View File

@@ -20,6 +20,7 @@ typedef void (*PeerPktHandler)(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void
struct sLaika_peer {
struct sLaika_socket sock; /* DO NOT MOVE THIS. this member HAS TO BE FIRST so that typecasting sLaika_peer* to sLaika_sock* works as intended */
uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */
uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES];
struct sLaika_pollList *pList; /* pollList we're active in */
PeerPktHandler *handlers;
LAIKAPKT_SIZE *pktSizeTable; /* const table to pull pkt size data from */
@@ -27,12 +28,20 @@ struct sLaika_peer {
LAIKAPKT_SIZE pktSize; /* current pkt size */
LAIKAPKT_ID pktID; /* current pkt ID */
PEERTYPE type;
int outStart; /* index of pktID for out packet */
int inStart; /* index of pktID for in packet */
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
bool useSecure; /* if true, peer will transmit/receive encrypted data using inKey & outKey */
};
struct sLaika_peer *laikaS_newPeer(PeerPktHandler *handlers, LAIKAPKT_SIZE *pktSizeTable, struct sLaika_pollList *pList, void *uData);
void laikaS_freePeer(struct sLaika_peer *peer);
void laikaS_setSecure(struct sLaika_peer *peer, bool flag);
void laikaS_startOutPacket(struct sLaika_peer *peer, uint8_t id);
int laikaS_endOutPacket(struct sLaika_peer *peer);
void laikaS_startInPacket(struct sLaika_peer *peer);
int laikaS_endInPacket(struct sLaika_peer *peer);
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
bool laikaS_handlePeerOut(struct sLaika_peer *peer);

View File

@@ -63,16 +63,12 @@ typedef enum {
struct sLaika_socket {
uint8_t *outBuf; /* raw data to be sent() */
uint8_t *inBuf; /* raw data we recv()'d */
uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES];
SOCKET sock; /* raw socket fd */
int outCount;
int inCount;
int outCap;
int inCap;
int outStart; /* index of pktID for out packet */
int inStart; /* index of pktID for in packet */
bool flipEndian;
bool useSecure; /* if true, sock will transmit/receive encrypted data using inKey & outKey */
};
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
@@ -90,11 +86,6 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port); /* bind sock to por
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from);
bool laikaS_setNonBlock(struct sLaika_socket *sock);
void laikaS_startOutPacket(struct sLaika_socket *sock, uint8_t id);
int laikaS_endOutPacket(struct sLaika_socket *sock);
void laikaS_startInPacket(struct sLaika_socket *sock);
int laikaS_endInPacket(struct sLaika_socket *sock);
void laikaS_setSecure(struct sLaika_socket *sock, bool flag);
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz); /* reads from inBuf */
void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz); /* writes to outBuf */
void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub); /* encrypts & writes from buf using pub key */