2022-01-25 03:46:29 +00:00
|
|
|
#ifndef LAIKA_SOCKET_H
|
|
|
|
#define LAIKA_SOCKET_H
|
|
|
|
|
2022-06-28 00:48:43 +00:00
|
|
|
/* clang-format will change the order of the included windows headers, this BREAKS THINGS.
|
|
|
|
for now, make clang ignore this section */
|
2022-09-02 01:00:37 +00:00
|
|
|
|
2022-06-28 00:11:43 +00:00
|
|
|
/* clang-format off */
|
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
/* socket/winsock headers */
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* windows */
|
2022-06-27 23:57:00 +00:00
|
|
|
# ifndef NOMINMAX
|
|
|
|
# define NOMINMAX
|
|
|
|
# endif
|
|
|
|
# define _WINSOCK_DEPRECATED_NO_WARNINGS
|
|
|
|
# include <winsock2.h>
|
2022-06-28 00:11:43 +00:00
|
|
|
# include <windows.h>
|
2022-06-27 23:57:00 +00:00
|
|
|
# include <ws2tcpip.h>
|
|
|
|
# pragma comment(lib, "Ws2_32.lib")
|
2022-01-24 03:28:16 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
typedef char buffer_t;
|
|
|
|
# define PollFD WSAPOLLFD
|
|
|
|
# define poll WSAPoll
|
|
|
|
# define LN_ERRNO WSAGetLastError()
|
|
|
|
# define LN_EWOULD WSAEWOULDBLOCK
|
|
|
|
# define LN_MSG_NOSIGNAL 0
|
|
|
|
# define SOCKETINVALID(x) (x == INVALID_SOCKET)
|
|
|
|
# define SOCKETERROR(x) (x == SOCKET_ERROR)
|
2022-01-24 03:28:16 +00:00
|
|
|
#else
|
|
|
|
/* posix platform */
|
2022-06-27 23:57:00 +00:00
|
|
|
# include <arpa/inet.h>
|
|
|
|
# include <netdb.h>
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <poll.h>
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/types.h>
|
|
|
|
# ifdef __linux__
|
|
|
|
# include <sys/epoll.h>
|
|
|
|
/* max events for epoll() */
|
|
|
|
# define MAX_EPOLL_EVENTS 128
|
2022-09-02 01:07:29 +00:00
|
|
|
# define LAIKA_USE_EPOLL
|
2022-06-27 23:57:00 +00:00
|
|
|
# endif
|
|
|
|
# include <errno.h>
|
|
|
|
# include <unistd.h>
|
2022-01-24 03:28:16 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
typedef int SOCKET;
|
|
|
|
typedef void buffer_t;
|
|
|
|
# define PollFD struct pollfd
|
|
|
|
# define LN_ERRNO errno
|
|
|
|
# define LN_EWOULD EWOULDBLOCK
|
|
|
|
# define LN_MSG_NOSIGNAL MSG_NOSIGNAL
|
|
|
|
# define INVALID_SOCKET -1
|
|
|
|
# define SOCKETINVALID(x) (x < 0)
|
|
|
|
# define SOCKETERROR(x) (x == -1)
|
2022-01-24 03:28:16 +00:00
|
|
|
#endif
|
2022-05-17 15:37:58 +00:00
|
|
|
#include "laika.h"
|
2022-09-02 01:00:37 +00:00
|
|
|
#include "core/lsodium.h"
|
|
|
|
#include "core/lmem.h"
|
2022-02-03 22:25:49 +00:00
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2022-06-28 00:11:43 +00:00
|
|
|
/* clang-format on */
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2022-01-24 03:28:16 +00:00
|
|
|
RAWSOCK_OK,
|
|
|
|
RAWSOCK_ERROR,
|
|
|
|
RAWSOCK_CLOSED,
|
|
|
|
RAWSOCK_POLL
|
|
|
|
} RAWSOCKCODE;
|
|
|
|
|
2022-03-24 15:26:06 +00:00
|
|
|
typedef bool (*pollEvent)(struct sLaika_socket *sock);
|
|
|
|
typedef void (*pollFailEvent)(struct sLaika_socket *sock, void *uData);
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
struct sLaika_socket
|
|
|
|
{
|
2022-03-05 02:17:03 +00:00
|
|
|
SOCKET sock; /* raw socket fd */
|
2022-03-24 15:26:06 +00:00
|
|
|
pollFailEvent onPollFail;
|
|
|
|
pollEvent onPollIn;
|
|
|
|
pollEvent onPollOut;
|
2022-09-02 01:00:37 +00:00
|
|
|
void *uData; /* passed to onPollFail */
|
2022-09-02 00:05:56 +00:00
|
|
|
laikaM_newVector(uint8_t, outBuf); /* raw data to be sent() */
|
|
|
|
laikaM_newVector(uint8_t, inBuf); /* raw data we recv()'d */
|
2022-01-25 03:46:29 +00:00
|
|
|
bool flipEndian;
|
2022-03-27 22:32:32 +00:00
|
|
|
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
|
2022-01-24 03:28:16 +00:00
|
|
|
};
|
|
|
|
|
2022-01-24 16:34:30 +00:00
|
|
|
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
|
2022-01-24 03:28:16 +00:00
|
|
|
|
2022-01-25 18:13:04 +00:00
|
|
|
bool laikaS_isBigEndian(void);
|
2022-01-25 02:50:14 +00:00
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
void laikaS_init(void);
|
|
|
|
void laikaS_cleanUp(void);
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent onPollOut,
|
|
|
|
pollFailEvent onPollFail, void *uData);
|
2022-01-24 03:28:16 +00:00
|
|
|
void laikaS_cleanSocket(struct sLaika_socket *sock);
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_kill(struct sLaika_socket *sock); /* kills a socket */
|
2022-01-24 03:28:16 +00:00
|
|
|
void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port); /* connect to ip & port */
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_bind(struct sLaika_socket *sock, uint16_t port); /* bind sock to port */
|
2022-06-04 15:11:28 +00:00
|
|
|
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from, char *ipStr);
|
2022-01-24 03:28:16 +00:00
|
|
|
bool laikaS_setNonBlock(struct sLaika_socket *sock);
|
|
|
|
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_consumeRead(struct sLaika_socket *sock,
|
|
|
|
size_t sz); /* throws sz bytes away from the inBuf */
|
|
|
|
void laikaS_zeroWrite(struct sLaika_socket *sock,
|
|
|
|
size_t sz); /* writes sz NULL bytes to the outBuf */
|
|
|
|
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz); /* reads from inBuf */
|
2022-01-24 03:28:16 +00:00
|
|
|
void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz); /* writes to outBuf */
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_writeKeyEncrypt(struct sLaika_socket *sock, void *buf, size_t sz,
|
|
|
|
uint8_t *pub); /* encrypts & writes from buf using pub key */
|
|
|
|
void laikaS_readKeyDecrypt(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub,
|
|
|
|
uint8_t *priv); /* decrypts & reads to buf using pub & priv key*/
|
2022-01-24 03:28:16 +00:00
|
|
|
void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data);
|
|
|
|
uint8_t laikaS_readByte(struct sLaika_socket *sock);
|
2022-06-27 23:57:00 +00:00
|
|
|
void laikaS_readInt(struct sLaika_socket *sock, void *buf,
|
|
|
|
size_t sz); /* reads INT, respecting endianness */
|
|
|
|
void laikaS_writeInt(struct sLaika_socket *sock, void *buf,
|
|
|
|
size_t sz); /* writes INT, respecting endianness */
|
2022-01-24 03:28:16 +00:00
|
|
|
|
|
|
|
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed);
|
2022-01-25 03:46:29 +00:00
|
|
|
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed);
|
|
|
|
|
|
|
|
#endif
|