|
|
|
@ -8,17 +8,6 @@
|
|
|
|
|
|
|
|
|
|
static int _LNSetup = 0;
|
|
|
|
|
|
|
|
|
|
bool laikaS_isBigEndian(void)
|
|
|
|
|
{
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint32_t i;
|
|
|
|
|
uint8_t c[4];
|
|
|
|
|
} _indxint = {0xDEADB33F};
|
|
|
|
|
|
|
|
|
|
return _indxint.c[0] == 0xDE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void laikaS_init(void)
|
|
|
|
|
{
|
|
|
|
|
if (_LNSetup++ > 0)
|
|
|
|
@ -42,6 +31,8 @@ void laikaS_cleanUp(void)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ======================================[[ Socket API ]]======================================= */
|
|
|
|
|
|
|
|
|
|
void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent onPollOut,
|
|
|
|
|
pollFailEvent onPollFail, void *uData)
|
|
|
|
|
{
|
|
|
|
@ -191,6 +182,8 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =====================================[[ Socket stream ]]===================================== */
|
|
|
|
|
|
|
|
|
|
void laikaS_consumeRead(struct sLaika_socket *sock, size_t sz)
|
|
|
|
|
{
|
|
|
|
|
laikaM_rmvVector(sock->inBuf, 0, sz);
|
|
|
|
@ -258,91 +251,50 @@ uint8_t laikaS_readByte(struct sLaika_socket *sock)
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz)
|
|
|
|
|
void laikaS_writeu16(struct sLaika_socket *sock, uint16_t i)
|
|
|
|
|
{
|
|
|
|
|
if (sock->flipEndian) {
|
|
|
|
|
VLA(uint8_t, tmp, sz); /* allocate tmp buffer to hold data while we switch endianness */
|
|
|
|
|
int k;
|
|
|
|
|
uint16_t tmp = i; /* copy int to buffer (which we can reverse if need-be) */
|
|
|
|
|
|
|
|
|
|
laikaS_read(sock, (void *)tmp, sz);
|
|
|
|
|
if (sock->flipEndian)
|
|
|
|
|
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
|
|
|
|
|
|
|
|
|
|
/* copy tmp buffer to user buffer, flipping endianness */
|
|
|
|
|
for (k = 0; k < sz; k++)
|
|
|
|
|
*((uint8_t *)buf + k) = tmp[sz - k - 1];
|
|
|
|
|
|
|
|
|
|
ENDVLA(tmp);
|
|
|
|
|
} else {
|
|
|
|
|
/* just a wrapper for laikaS_read */
|
|
|
|
|
laikaS_read(sock, buf, sz);
|
|
|
|
|
}
|
|
|
|
|
laikaS_write(sock, (void *)&tmp, sizeof(tmp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz)
|
|
|
|
|
uint16_t laikaS_readu16(struct sLaika_socket *sock)
|
|
|
|
|
{
|
|
|
|
|
if (sock->flipEndian) {
|
|
|
|
|
VLA(uint8_t, tmp, sz); /* allocate tmp buffer to hold data while we switch endianness */
|
|
|
|
|
int k;
|
|
|
|
|
|
|
|
|
|
/* copy user buffer to tmp buffer, flipping endianness */
|
|
|
|
|
for (k = 0; k < sz; k++)
|
|
|
|
|
tmp[k] = *((uint8_t *)buf + (sz - k - 1));
|
|
|
|
|
|
|
|
|
|
laikaS_write(sock, (void *)tmp, sz);
|
|
|
|
|
ENDVLA(tmp);
|
|
|
|
|
} else {
|
|
|
|
|
/* just a wrapper for laikaS_write */
|
|
|
|
|
laikaS_write(sock, buf, sz);
|
|
|
|
|
}
|
|
|
|
|
uint16_t tmp;
|
|
|
|
|
laikaS_read(sock, (void *)&tmp, sizeof(tmp));
|
|
|
|
|
|
|
|
|
|
if (sock->flipEndian)
|
|
|
|
|
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
|
|
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed)
|
|
|
|
|
void laikaS_writeu32(struct sLaika_socket *sock, uint32_t i)
|
|
|
|
|
{
|
|
|
|
|
RAWSOCKCODE errCode = RAWSOCK_OK;
|
|
|
|
|
int i, rcvd, start = laikaM_countVector(sock->inBuf);
|
|
|
|
|
uint32_t tmp = i; /* copy int to buffer (which we can reverse if need-be) */
|
|
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
|
if (sz == 0)
|
|
|
|
|
return RAWSOCK_OK;
|
|
|
|
|
if (sock->flipEndian)
|
|
|
|
|
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
|
|
|
|
|
|
|
|
|
|
/* make sure we have enough space to recv */
|
|
|
|
|
laikaM_growVector(uint8_t, sock->inBuf, sz);
|
|
|
|
|
rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[laikaM_countVector(sock->inBuf)], sz,
|
|
|
|
|
LN_MSG_NOSIGNAL);
|
|
|
|
|
laikaS_write(sock, (void *)&tmp, sizeof(tmp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rcvd == 0) {
|
|
|
|
|
errCode = RAWSOCK_CLOSED;
|
|
|
|
|
} else if (SOCKETERROR(rcvd) &&
|
|
|
|
|
LN_ERRNO != LN_EWOULD
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
/* if it's a posix system, also make sure its not a EAGAIN result (which is a
|
|
|
|
|
recoverable error, there's just nothing to read lol) */
|
|
|
|
|
&& LN_ERRNO != EAGAIN
|
|
|
|
|
#endif
|
|
|
|
|
) {
|
|
|
|
|
/* if the socket closed or an error occurred, return the error result */
|
|
|
|
|
errCode = RAWSOCK_ERROR;
|
|
|
|
|
} else if (rcvd > 0) {
|
|
|
|
|
#if 0
|
|
|
|
|
/* for debugging */
|
|
|
|
|
printf("---recv'd %d bytes---\n", rcvd);
|
|
|
|
|
for (i = 1; i <= rcvd; i++) {
|
|
|
|
|
printf("%.2x ", sock->inBuf[sock->inCount + (i-1)]);
|
|
|
|
|
if (i % 16 == 0) {
|
|
|
|
|
printf("\n");
|
|
|
|
|
} else if (i % 8 == 0) {
|
|
|
|
|
printf("\t");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
#endif
|
|
|
|
|
uint32_t laikaS_readu32(struct sLaika_socket *sock)
|
|
|
|
|
{
|
|
|
|
|
uint32_t tmp;
|
|
|
|
|
laikaS_read(sock, (void *)&tmp, sizeof(tmp));
|
|
|
|
|
|
|
|
|
|
/* recv() worked, add rcvd to inCount */
|
|
|
|
|
laikaM_countVector(sock->inBuf) += rcvd;
|
|
|
|
|
}
|
|
|
|
|
*processed = rcvd;
|
|
|
|
|
return errCode;
|
|
|
|
|
if (sock->flipEndian)
|
|
|
|
|
laikaM_reverse((uint8_t *)&tmp, sizeof(tmp));
|
|
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ===================================[[ Socket send/recv ]]==================================== */
|
|
|
|
|
|
|
|
|
|
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed)
|
|
|
|
|
{
|
|
|
|
|
RAWSOCKCODE errCode = RAWSOCK_OK;
|
|
|
|
@ -379,23 +331,43 @@ RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed
|
|
|
|
|
} while ((sentBytes += sent) < sz);
|
|
|
|
|
|
|
|
|
|
_rawWriteExit:
|
|
|
|
|
#if 0
|
|
|
|
|
/* for debugging */
|
|
|
|
|
printf("---sent %d bytes---\n", sent);
|
|
|
|
|
for (i = 1; i <= sentBytes; i++) {
|
|
|
|
|
printf("%.2x ", sock->outBuf[i-1]);
|
|
|
|
|
if (i % 16 == 0) {
|
|
|
|
|
printf("\n");
|
|
|
|
|
} else if (i % 8 == 0) {
|
|
|
|
|
printf("\t");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* trim sent data from outBuf */
|
|
|
|
|
laikaM_rmvVector(sock->outBuf, 0, sentBytes);
|
|
|
|
|
|
|
|
|
|
*processed = sentBytes;
|
|
|
|
|
return errCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed)
|
|
|
|
|
{
|
|
|
|
|
RAWSOCKCODE errCode = RAWSOCK_OK;
|
|
|
|
|
int i, rcvd, start = laikaM_countVector(sock->inBuf);
|
|
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
|
if (sz == 0)
|
|
|
|
|
return RAWSOCK_OK;
|
|
|
|
|
|
|
|
|
|
/* make sure we have enough space to recv */
|
|
|
|
|
laikaM_growVector(uint8_t, sock->inBuf, sz);
|
|
|
|
|
rcvd = recv(sock->sock, (buffer_t *)&sock->inBuf[laikaM_countVector(sock->inBuf)], sz,
|
|
|
|
|
LN_MSG_NOSIGNAL);
|
|
|
|
|
|
|
|
|
|
if (rcvd == 0) {
|
|
|
|
|
errCode = RAWSOCK_CLOSED;
|
|
|
|
|
} else if (SOCKETERROR(rcvd) &&
|
|
|
|
|
LN_ERRNO != LN_EWOULD
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
/* if it's a posix system, also make sure its not a EAGAIN result (which is a
|
|
|
|
|
recoverable error, there's just nothing to read lol) */
|
|
|
|
|
&& LN_ERRNO != EAGAIN
|
|
|
|
|
#endif
|
|
|
|
|
) {
|
|
|
|
|
/* if the socket closed or an error occurred, return the error result */
|
|
|
|
|
errCode = RAWSOCK_ERROR;
|
|
|
|
|
} else if (rcvd > 0) {
|
|
|
|
|
/* recv() worked, add rcvd to inCount */
|
|
|
|
|
laikaM_countVector(sock->inBuf) += rcvd;
|
|
|
|
|
}
|
|
|
|
|
*processed = rcvd;
|
|
|
|
|
return errCode;
|
|
|
|
|
}
|