2022-01-24 03:28:16 +00:00
|
|
|
#ifndef LAIKA_PEER_H
|
|
|
|
#define LAIKA_PEER_H
|
|
|
|
|
|
|
|
#include "laika.h"
|
|
|
|
#include "lsocket.h"
|
|
|
|
#include "lpacket.h"
|
|
|
|
#include "lpolllist.h"
|
2022-02-25 04:13:05 +00:00
|
|
|
#include "lsodium.h"
|
2022-01-24 03:28:16 +00:00
|
|
|
|
2022-01-27 19:36:36 +00:00
|
|
|
typedef enum {
|
2022-03-18 04:05:18 +00:00
|
|
|
PEER_UNKNWN,
|
2022-01-27 19:36:36 +00:00
|
|
|
PEER_BOT,
|
|
|
|
PEER_CNC, /* cnc 2 cnc communication */
|
2022-02-28 22:27:55 +00:00
|
|
|
PEER_AUTH /* authorized peers can send commands to cnc */
|
2022-01-27 19:36:36 +00:00
|
|
|
} PEERTYPE;
|
|
|
|
|
2022-03-18 04:05:18 +00:00
|
|
|
typedef enum {
|
|
|
|
OS_UNKNWN,
|
|
|
|
OS_WIN,
|
|
|
|
OS_LIN
|
|
|
|
} OSTYPE;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define LAIKA_OSTYPE OS_WIN
|
|
|
|
#else
|
|
|
|
#ifdef __linux__
|
|
|
|
#define LAIKA_OSTYPE OS_LIN
|
|
|
|
#else
|
|
|
|
#define LAIKA_OSTYPE OS_UNKNWN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
struct sLaika_peer;
|
|
|
|
typedef void (*PeerPktHandler)(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData);
|
|
|
|
|
2022-02-28 22:27:55 +00:00
|
|
|
struct sLaika_peerPacketInfo {
|
|
|
|
PeerPktHandler handler;
|
|
|
|
LAIKAPKT_SIZE size;
|
|
|
|
bool variadic;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-03-02 16:38:16 +00:00
|
|
|
#define LAIKA_CREATE_PACKET_INFO(ID, HANDLER, SIZE, ISVARIADIC) [ID] = {.handler = HANDLER, .size = SIZE, .variadic = ISVARIADIC}
|
2022-02-28 22:27:55 +00:00
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
struct sLaika_peer {
|
2022-01-25 02:50:14 +00:00
|
|
|
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 */
|
2022-02-03 22:25:49 +00:00
|
|
|
uint8_t peerPub[crypto_kx_PUBLICKEYBYTES]; /* connected peer's public key */
|
2022-02-04 02:51:32 +00:00
|
|
|
uint8_t inKey[crypto_kx_SESSIONKEYBYTES], outKey[crypto_kx_SESSIONKEYBYTES];
|
2022-03-05 02:17:03 +00:00
|
|
|
char hostname[LAIKA_HOSTNAME_LEN], inet[LAIKA_INET_LEN], ipv4[LAIKA_IPV4_LEN];
|
2022-02-10 22:56:40 +00:00
|
|
|
struct sLaika_pollList *pList; /* pollList we're activeList in */
|
2022-02-28 22:27:55 +00:00
|
|
|
struct sLaika_peerPacketInfo *packetTbl; /* const table to pull pkt data from */
|
2022-01-31 01:10:10 +00:00
|
|
|
void *uData; /* data to be passed to pktHandler */
|
2022-01-25 19:53:17 +00:00
|
|
|
LAIKAPKT_SIZE pktSize; /* current pkt size */
|
|
|
|
LAIKAPKT_ID pktID; /* current pkt ID */
|
2022-01-27 19:36:36 +00:00
|
|
|
PEERTYPE type;
|
2022-03-18 04:05:18 +00:00
|
|
|
OSTYPE osType;
|
2022-02-04 02:51:32 +00:00
|
|
|
int outStart; /* index of pktID for out packet */
|
|
|
|
int inStart; /* index of pktID for in packet */
|
|
|
|
bool useSecure; /* if true, peer will transmit/receive encrypted data using inKey & outKey */
|
2022-01-24 03:28:16 +00:00
|
|
|
};
|
|
|
|
|
2022-03-24 15:26:06 +00:00
|
|
|
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *packetTbl, struct sLaika_pollList *pList, pollFailEvent onPollFail, void *onPollFailUData, void *uData);
|
2022-01-24 03:28:16 +00:00
|
|
|
void laikaS_freePeer(struct sLaika_peer *peer);
|
|
|
|
|
2022-02-04 02:51:32 +00:00
|
|
|
void laikaS_setSecure(struct sLaika_peer *peer, bool flag);
|
2022-02-28 22:27:55 +00:00
|
|
|
void laikaS_emptyOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id); /* for sending packets with no body */
|
2022-02-19 01:32:16 +00:00
|
|
|
void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id);
|
2022-02-04 02:51:32 +00:00
|
|
|
int laikaS_endOutPacket(struct sLaika_peer *peer);
|
2022-02-19 01:32:16 +00:00
|
|
|
void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id);
|
|
|
|
int laikaS_endVarPacket(struct sLaika_peer *peer);
|
2022-03-24 15:26:06 +00:00
|
|
|
bool laikaS_handlePeerIn(struct sLaika_socket *sock);
|
|
|
|
bool laikaS_handlePeerOut(struct sLaika_socket *sock);
|
2022-01-24 03:28:16 +00:00
|
|
|
|
|
|
|
#endif
|