added:
* win32_slim.h: includes the minimal version of windows.h modified: * cthread: added lock_t c++ class * os: added getUserName() * socket: modified socket_len_t to sk_len_t to better follow naming convetions in file * str: added operator std::string() to str_t
This commit is contained in:
parent
7fdc8b5301
commit
bce93361b1
16 changed files with 187 additions and 70 deletions
|
|
@ -6,8 +6,8 @@ typedef struct {
|
||||||
} _thr_internal_t;
|
} _thr_internal_t;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define VC_EXTRALEAN
|
#include "win32_slim.h"
|
||||||
#include <windows.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
// == THREAD ===========================================
|
// == THREAD ===========================================
|
||||||
|
|
||||||
|
|
|
||||||
25
cthreads.h
25
cthreads.h
|
|
@ -37,6 +37,31 @@ bool mtxLock(cmutex_t ctx);
|
||||||
bool mtxTryLock(cmutex_t ctx);
|
bool mtxTryLock(cmutex_t ctx);
|
||||||
bool mtxUnlock(cmutex_t ctx);
|
bool mtxUnlock(cmutex_t ctx);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
// small c++ class to make mutexes easier to use
|
||||||
|
struct lock_t {
|
||||||
|
inline lock_t(cmutex_t mutex)
|
||||||
|
: mutex(mutex) {
|
||||||
|
if (mtxValid(mutex)) {
|
||||||
|
mtxLock(mutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline ~lock_t() {
|
||||||
|
unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void unlock() {
|
||||||
|
if (mtxValid(mutex)) {
|
||||||
|
mtxUnlock(mutex);
|
||||||
|
}
|
||||||
|
mutex = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmutex_t mutex;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
#endif
|
#endif
|
||||||
9
dir.c
9
dir.c
|
|
@ -2,8 +2,7 @@
|
||||||
#include "tracelog.h"
|
#include "tracelog.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define VC_EXTRALEAN
|
#include "win32_slim.h"
|
||||||
#include <windows.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
|
@ -107,10 +106,10 @@ dir_entry_t *dirNext(dir_t ctx) {
|
||||||
// taken from https://sites.uclouvain.be/SystInfo/usr/include/dirent.h.html
|
// taken from https://sites.uclouvain.be/SystInfo/usr/include/dirent.h.html
|
||||||
// hopefully shouldn't be needed
|
// hopefully shouldn't be needed
|
||||||
#ifndef DT_DIR
|
#ifndef DT_DIR
|
||||||
#define DT_DIR 4
|
#define DT_DIR 4
|
||||||
#endif
|
#endif
|
||||||
#ifndef DT_REG
|
#ifndef DT_REG
|
||||||
#define DT_REG 8
|
#define DT_REG 8
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
@ -134,7 +133,7 @@ void dirClose(dir_t ctx) {
|
||||||
|
|
||||||
bool dirValid(dir_t ctx) {
|
bool dirValid(dir_t ctx) {
|
||||||
_dir_internal_t *dir = (_dir_internal_t*)ctx;
|
_dir_internal_t *dir = (_dir_internal_t*)ctx;
|
||||||
return dir->handle != NULL;
|
return dir->dir != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
dir_entry_t *dirNext(dir_t ctx) {
|
dir_entry_t *dirNext(dir_t ctx) {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
#include "dirwatch.h"
|
#include "dirwatch.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include "tracelog.h"
|
#include "tracelog.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define VC_EXTRALEAN
|
#include "win32_slim.h"
|
||||||
#include <windows.h>
|
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
@ -177,8 +177,6 @@ void stopWatchDir(dirwatch_t *ctx, bool immediately) {
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h> // malloc
|
|
||||||
#include <unistd.h> // read
|
#include <unistd.h> // read
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
|
||||||
6
file.c
6
file.c
|
|
@ -3,8 +3,8 @@
|
||||||
#include "tracelog.h"
|
#include "tracelog.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define VC_EXTRALEAN
|
#include "win32_slim.h"
|
||||||
#include <windows.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static DWORD _toWin32Access(int mode) {
|
static DWORD _toWin32Access(int mode) {
|
||||||
if(mode & FILE_READ) return GENERIC_READ;
|
if(mode & FILE_READ) return GENERIC_READ;
|
||||||
|
|
@ -209,7 +209,7 @@ fread_buf_t fileReadWholeFP(file_t *ctx) {
|
||||||
str_t fileReadWholeText(const char *fname) {
|
str_t fileReadWholeText(const char *fname) {
|
||||||
file_t fp = fileOpen(fname, FILE_READ);
|
file_t fp = fileOpen(fname, FILE_READ);
|
||||||
if(!fileIsValid(&fp)) {
|
if(!fileIsValid(&fp)) {
|
||||||
err("couldn't open file %s -> %d", fname);
|
err("couldn't open file %s", fname);
|
||||||
return strInit();
|
return strInit();
|
||||||
}
|
}
|
||||||
str_t contents = fileReadWholeFPText(&fp);
|
str_t contents = fileReadWholeFPText(&fp);
|
||||||
|
|
|
||||||
3
fs.c
3
fs.c
|
|
@ -8,8 +8,7 @@
|
||||||
#include "tracelog.h"
|
#include "tracelog.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define VC_EXTRALEAN
|
#include "win32_slim.h"
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
|
|
||||||
35
http.c
35
http.c
|
|
@ -85,7 +85,7 @@ void reqSetField(http_request_t *ctx, const char *key, const char *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void reqSetUri(http_request_t *ctx, const char *uri) {
|
void reqSetUri(http_request_t *ctx, const char *uri) {
|
||||||
if(uri == NULL) return;
|
if (uri == NULL) return;
|
||||||
size_t len = strlen(uri);
|
size_t len = strlen(uri);
|
||||||
if(uri[0] != '/') {
|
if(uri[0] != '/') {
|
||||||
len += 1;
|
len += 1;
|
||||||
|
|
@ -139,9 +139,7 @@ str_t reqString(http_request_t *ctx) {
|
||||||
// == HTTP RESPONSE ===========================================================
|
// == HTTP RESPONSE ===========================================================
|
||||||
|
|
||||||
http_response_t resInit() {
|
http_response_t resInit() {
|
||||||
http_response_t res;
|
return (http_response_t) {0};
|
||||||
memset(&res, 0, sizeof(res));
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void resFree(http_response_t *ctx) {
|
void resFree(http_response_t *ctx) {
|
||||||
|
|
@ -150,7 +148,7 @@ void resFree(http_response_t *ctx) {
|
||||||
free(ctx->fields.buf[i].value);
|
free(ctx->fields.buf[i].value);
|
||||||
}
|
}
|
||||||
field_vecFree(&ctx->fields);
|
field_vecFree(&ctx->fields);
|
||||||
free(ctx->body);
|
strFree(&ctx->body);
|
||||||
memset(ctx, 0, sizeof(http_response_t));
|
memset(ctx, 0, sizeof(http_response_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,8 +193,8 @@ void resParse(http_response_t *ctx, const char *data) {
|
||||||
const char *tran_encoding = resGetField(ctx, "transfer-encoding");
|
const char *tran_encoding = resGetField(ctx, "transfer-encoding");
|
||||||
if(tran_encoding == NULL || stricmp(tran_encoding, "chunked") != 0) {
|
if(tran_encoding == NULL || stricmp(tran_encoding, "chunked") != 0) {
|
||||||
strview_t body = istrGetviewLen(&in, 0, SIZE_MAX);
|
strview_t body = istrGetviewLen(&in, 0, SIZE_MAX);
|
||||||
free(ctx->body);
|
strFree(&ctx->body);
|
||||||
ctx->body = strvCopy(body).buf;
|
ctx->body = strvCopy(body);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fatal("chunked encoding not implemented yet");
|
fatal("chunked encoding not implemented yet");
|
||||||
|
|
@ -233,14 +231,13 @@ void resParseFields(http_response_t *ctx, str_istream_t *in) {
|
||||||
// == HTTP CLIENT =============================================================
|
// == HTTP CLIENT =============================================================
|
||||||
|
|
||||||
http_client_t hcliInit() {
|
http_client_t hcliInit() {
|
||||||
http_client_t client;
|
return (http_client_t) {
|
||||||
memset(&client, 0, sizeof(client));
|
.port = 80,
|
||||||
client.port = 80;
|
};
|
||||||
return client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void hcliFree(http_client_t *ctx) {
|
void hcliFree(http_client_t *ctx) {
|
||||||
free(ctx->host_name);
|
strFree(&ctx->host_name);
|
||||||
memset(ctx, 0, sizeof(http_client_t));
|
memset(ctx, 0, sizeof(http_client_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -248,7 +245,7 @@ void hcliSetHost(http_client_t *ctx, const char *hostname) {
|
||||||
strview_t hostview = strvInit(hostname);
|
strview_t hostview = strvInit(hostname);
|
||||||
// if the hostname starts with http:// (case insensitive)
|
// if the hostname starts with http:// (case insensitive)
|
||||||
if(strvICompare(strvSubstr(hostview, 0, 7), strvInit("http://")) == 0) {
|
if(strvICompare(strvSubstr(hostview, 0, 7), strvInit("http://")) == 0) {
|
||||||
ctx->host_name = strvCopy(strvSubstr(hostview, 7, SIZE_MAX)).buf;
|
ctx->host_name = strvCopy(strvSubstr(hostview, 7, SIZE_MAX));
|
||||||
}
|
}
|
||||||
else if(strvICompare(strvSubstr(hostview, 0, 8), strvInit("https://")) == 0) {
|
else if(strvICompare(strvSubstr(hostview, 0, 8), strvInit("https://")) == 0) {
|
||||||
err("HTTPS protocol not yet supported");
|
err("HTTPS protocol not yet supported");
|
||||||
|
|
@ -256,13 +253,16 @@ void hcliSetHost(http_client_t *ctx, const char *hostname) {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// undefined protocol, use HTTP
|
// undefined protocol, use HTTP
|
||||||
ctx->host_name = strvCopy(hostview).buf;
|
ctx->host_name = strvCopy(hostview);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
http_response_t hcliSendRequest(http_client_t *ctx, http_request_t *req) {
|
http_response_t hcliSendRequest(http_client_t *ctx, http_request_t *req) {
|
||||||
|
if (strBack(&ctx->host_name) == '/') {
|
||||||
|
strPop(&ctx->host_name);
|
||||||
|
}
|
||||||
if(!reqHasField(req, "Host")) {
|
if(!reqHasField(req, "Host")) {
|
||||||
reqSetField(req, "Host", ctx->host_name);
|
reqSetField(req, "Host", ctx->host_name.buf);
|
||||||
}
|
}
|
||||||
if(!reqHasField(req, "Content-Length")) {
|
if(!reqHasField(req, "Content-Length")) {
|
||||||
if(req->body) {
|
if(req->body) {
|
||||||
|
|
@ -297,7 +297,7 @@ http_response_t hcliSendRequest(http_client_t *ctx, http_request_t *req) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(skConnect(ctx->socket, ctx->host_name, ctx->port)) {
|
if(skConnect(ctx->socket, ctx->host_name.buf, ctx->port)) {
|
||||||
req_str = reqString(req);
|
req_str = reqString(req);
|
||||||
if(req_str.len == 0) {
|
if(req_str.len == 0) {
|
||||||
err("couldn't get string from request");
|
err("couldn't get string from request");
|
||||||
|
|
@ -328,6 +328,9 @@ http_response_t hcliSendRequest(http_client_t *ctx, http_request_t *req) {
|
||||||
|
|
||||||
resParse(&res, received.buf);
|
resParse(&res, received.buf);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
err("Couldn't connect to host %s -> %s", ctx->host_name, skGetErrorString());
|
||||||
|
}
|
||||||
|
|
||||||
if(!skClose(ctx->socket)) {
|
if(!skClose(ctx->socket)) {
|
||||||
err("Couldn't close socket");
|
err("Couldn't close socket");
|
||||||
|
|
|
||||||
4
http.h
4
http.h
|
|
@ -97,7 +97,7 @@ typedef struct {
|
||||||
int status_code;
|
int status_code;
|
||||||
field_vec_t fields;
|
field_vec_t fields;
|
||||||
http_version_t version;
|
http_version_t version;
|
||||||
char *body;
|
str_t body;
|
||||||
} http_response_t;
|
} http_response_t;
|
||||||
|
|
||||||
http_response_t resInit(void);
|
http_response_t resInit(void);
|
||||||
|
|
@ -112,7 +112,7 @@ void resParseFields(http_response_t *ctx, str_istream_t *in);
|
||||||
// == HTTP CLIENT =============================================================
|
// == HTTP CLIENT =============================================================
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *host_name;
|
str_t host_name;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
socket_t socket;
|
socket_t socket;
|
||||||
} http_client_t;
|
} http_client_t;
|
||||||
|
|
|
||||||
28
os.c
28
os.c
|
|
@ -7,6 +7,8 @@
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define _BUFSZ 128
|
#define _BUFSZ 128
|
||||||
|
|
||||||
|
#include <lmcons.h>
|
||||||
|
|
||||||
// modified from netbsd source http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/pkgtools/libnbcompat/files/getdelim.c?only_with_tag=MAIN
|
// modified from netbsd source http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/pkgtools/libnbcompat/files/getdelim.c?only_with_tag=MAIN
|
||||||
ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp) {
|
ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp) {
|
||||||
char *ptr, *eptr;
|
char *ptr, *eptr;
|
||||||
|
|
@ -64,4 +66,30 @@ ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp) {
|
||||||
ssize_t getline(char **line_ptr, size_t *n, FILE *stream) {
|
ssize_t getline(char **line_ptr, size_t *n, FILE *stream) {
|
||||||
return getdelim(line_ptr, n, '\n', stream);
|
return getdelim(line_ptr, n, '\n', stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
str_t getUserName() {
|
||||||
|
char buf[UNLEN + 1];
|
||||||
|
DWORD sz = sizeof(buf);
|
||||||
|
BOOL res = GetUserNameA(buf, &sz);
|
||||||
|
if(!res) {
|
||||||
|
return strInit();
|
||||||
|
}
|
||||||
|
return strInitBuf(buf, sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
str_t getUserName() {
|
||||||
|
char buf[255];
|
||||||
|
int res = getlogin_r(buf, sizeof(buf));
|
||||||
|
if(res) {
|
||||||
|
return strInit();
|
||||||
|
}
|
||||||
|
return strInitStr(buf);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
7
os.h
7
os.h
|
|
@ -6,21 +6,26 @@ extern "C" {
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "str.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <BaseTsd.h> // SSIZE_T
|
#include "win32_slim.h"
|
||||||
typedef SSIZE_T ssize_t;
|
typedef SSIZE_T ssize_t;
|
||||||
ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp);
|
ssize_t getdelim(char **buf, size_t *bufsz, int delimiter, FILE *fp);
|
||||||
ssize_t getline(char **line_ptr, size_t *n, FILE *stream);
|
ssize_t getline(char **line_ptr, size_t *n, FILE *stream);
|
||||||
#define stricmp _stricmp
|
#define stricmp _stricmp
|
||||||
#else
|
#else
|
||||||
#define stricmp strcasecmp
|
#define stricmp strcasecmp
|
||||||
|
#ifndef _GNU_SOURCE
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
|
#endif
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h> // ssize_t
|
#include <sys/types.h> // ssize_t
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
str_t getUserName();
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
#endif
|
#endif
|
||||||
28
socket.c
28
socket.c
|
|
@ -9,8 +9,6 @@ static int initialize_count = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if SOCK_WINDOWS
|
#if SOCK_WINDOWS
|
||||||
#include <ws2tcpip.h>
|
|
||||||
|
|
||||||
static bool _win_skInit();
|
static bool _win_skInit();
|
||||||
static bool _win_skCleanup();
|
static bool _win_skCleanup();
|
||||||
static int _win_skGetError();
|
static int _win_skGetError();
|
||||||
|
|
@ -113,7 +111,7 @@ bool skBind(socket_t sock, const char *ip, uint16_t port) {
|
||||||
return skBindPro(sock, (sk_addr_t *) &addr, sizeof(addr));
|
return skBindPro(sock, (sk_addr_t *) &addr, sizeof(addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool skBindPro(socket_t sock, const sk_addr_t *name, socket_len_t namelen) {
|
bool skBindPro(socket_t sock, const sk_addr_t *name, sk_len_t namelen) {
|
||||||
return bind(sock, name, namelen) != SOCKET_ERROR;
|
return bind(sock, name, namelen) != SOCKET_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,11 +125,11 @@ bool skListenPro(socket_t sock, int backlog) {
|
||||||
|
|
||||||
socket_t skAccept(socket_t sock) {
|
socket_t skAccept(socket_t sock) {
|
||||||
sk_addrin_t addr;
|
sk_addrin_t addr;
|
||||||
socket_len_t addr_size = (socket_len_t)sizeof(addr);
|
sk_len_t addr_size = (sk_len_t)sizeof(addr);
|
||||||
return skAcceptPro(sock, (sk_addr_t *) &addr, &addr_size);
|
return skAcceptPro(sock, (sk_addr_t *) &addr, &addr_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_t skAcceptPro(socket_t sock, sk_addr_t *addr, socket_len_t *addrlen) {
|
socket_t skAcceptPro(socket_t sock, sk_addr_t *addr, sk_len_t *addrlen) {
|
||||||
return accept(sock, addr, addrlen);
|
return accept(sock, addr, addrlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,40 +150,40 @@ bool skConnect(socket_t sock, const char *server, unsigned short server_port) {
|
||||||
return skConnectPro(sock, (sk_addr_t *) &addr, sizeof(addr));
|
return skConnectPro(sock, (sk_addr_t *) &addr, sizeof(addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool skConnectPro(socket_t sock, const sk_addr_t *name, socket_len_t namelen) {
|
bool skConnectPro(socket_t sock, const sk_addr_t *name, sk_len_t namelen) {
|
||||||
return connect(sock, name, namelen) != SOCKET_ERROR;
|
return connect(sock, name, namelen) != SOCKET_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int skSend(socket_t sock, const char *buf, int len) {
|
int skSend(socket_t sock, const void *buf, int len) {
|
||||||
return skSendPro(sock, buf, len, 0);
|
return skSendPro(sock, buf, len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int skSendPro(socket_t sock, const char *buf, int len, int flags) {
|
int skSendPro(socket_t sock, const void *buf, int len, int flags) {
|
||||||
return send(sock, buf, len, flags);
|
return send(sock, buf, len, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
int skSendTo(socket_t sock, const char *buf, int len, const sk_addrin_t *to) {
|
int skSendTo(socket_t sock, const void *buf, int len, const sk_addrin_t *to) {
|
||||||
return skSendToPro(sock, buf, len, 0, (sk_addr_t*) to, sizeof(sk_addrin_t));
|
return skSendToPro(sock, buf, len, 0, (sk_addr_t*) to, sizeof(sk_addrin_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
int skSendToPro(socket_t sock, const char *buf, int len, int flags, const sk_addr_t *to, int tolen) {
|
int skSendToPro(socket_t sock, const void *buf, int len, int flags, const sk_addr_t *to, int tolen) {
|
||||||
return sendto(sock, buf, len, flags, to, tolen);
|
return sendto(sock, buf, len, flags, to, tolen);
|
||||||
}
|
}
|
||||||
|
|
||||||
int skReceive(socket_t sock, char *buf, int len) {
|
int skReceive(socket_t sock, void *buf, int len) {
|
||||||
return skReceivePro(sock, buf, len, 0);
|
return skReceivePro(sock, buf, len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int skReceivePro(socket_t sock, char *buf, int len, int flags) {
|
int skReceivePro(socket_t sock, void *buf, int len, int flags) {
|
||||||
return recv(sock, buf, len, flags);
|
return recv(sock, buf, len, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
int skReceiveFrom(socket_t sock, char *buf, int len, sk_addrin_t *from) {
|
int skReceiveFrom(socket_t sock, void *buf, int len, sk_addrin_t *from) {
|
||||||
socket_len_t fromlen = sizeof(sk_addr_t);
|
sk_len_t fromlen = sizeof(sk_addr_t);
|
||||||
return skReceiveFromPro(sock, buf, len, 0, (sk_addr_t*)from, &fromlen);
|
return skReceiveFromPro(sock, buf, len, 0, (sk_addr_t*)from, &fromlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
int skReceiveFromPro(socket_t sock, char *buf, int len, int flags, sk_addr_t *from, socket_len_t *fromlen) {
|
int skReceiveFromPro(socket_t sock, void *buf, int len, int flags, sk_addr_t *from, sk_len_t *fromlen) {
|
||||||
return recvfrom(sock, buf, len, flags, from, fromlen);
|
return recvfrom(sock, buf, len, flags, from, fromlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
28
socket.h
28
socket.h
|
|
@ -15,15 +15,17 @@ extern "C" {
|
||||||
|
|
||||||
#if SOCK_WINDOWS
|
#if SOCK_WINDOWS
|
||||||
#pragma warning(disable:4996) // _WINSOCK_DEPRECATED_NO_WARNINGS
|
#pragma warning(disable:4996) // _WINSOCK_DEPRECATED_NO_WARNINGS
|
||||||
|
#include "win32_slim.h"
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
typedef SOCKET socket_t;
|
typedef SOCKET socket_t;
|
||||||
typedef int socket_len_t;
|
typedef int sk_len_t;
|
||||||
#elif SOCK_POSIX
|
#elif SOCK_POSIX
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
typedef int socket_t;
|
typedef int socket_t;
|
||||||
typedef uint32_t socket_len_t;
|
typedef uint32_t sk_len_t;
|
||||||
#define INVALID_SOCKET (-1)
|
#define INVALID_SOCKET (-1)
|
||||||
#define SOCKET_ERROR (-1)
|
#define SOCKET_ERROR (-1)
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -61,7 +63,7 @@ bool skClose(socket_t sock);
|
||||||
// Associate a local address with a socket
|
// Associate a local address with a socket
|
||||||
bool skBind(socket_t sock, const char *ip, uint16_t port);
|
bool skBind(socket_t sock, const char *ip, uint16_t port);
|
||||||
// Associate a local address with a socket
|
// Associate a local address with a socket
|
||||||
bool skBindPro(socket_t sock, const sk_addr_t *name, socket_len_t namelen);
|
bool skBindPro(socket_t sock, const sk_addr_t *name, sk_len_t namelen);
|
||||||
|
|
||||||
// Place a socket in a state in which it is listening for an incoming connection
|
// Place a socket in a state in which it is listening for an incoming connection
|
||||||
bool skListen(socket_t sock);
|
bool skListen(socket_t sock);
|
||||||
|
|
@ -71,29 +73,29 @@ bool skListenPro(socket_t sock, int backlog);
|
||||||
// Permits an incoming connection attempt on a socket
|
// Permits an incoming connection attempt on a socket
|
||||||
socket_t skAccept(socket_t sock);
|
socket_t skAccept(socket_t sock);
|
||||||
// Permits an incoming connection attempt on a socket
|
// Permits an incoming connection attempt on a socket
|
||||||
socket_t skAcceptPro(socket_t sock, sk_addr_t *addr, socket_len_t *addrlen);
|
socket_t skAcceptPro(socket_t sock, sk_addr_t *addr, sk_len_t *addrlen);
|
||||||
|
|
||||||
// Connects to a server (e.g. "127.0.0.1" or "google.com") with a port(e.g. 1234), returns true on success
|
// Connects to a server (e.g. "127.0.0.1" or "google.com") with a port(e.g. 1234), returns true on success
|
||||||
bool skConnect(socket_t sock, const char *server, unsigned short server_port);
|
bool skConnect(socket_t sock, const char *server, unsigned short server_port);
|
||||||
// Connects to a server, returns true on success
|
// Connects to a server, returns true on success
|
||||||
bool skConnectPro(socket_t sock, const sk_addr_t *name, socket_len_t namelen);
|
bool skConnectPro(socket_t sock, const sk_addr_t *name, sk_len_t namelen);
|
||||||
|
|
||||||
// Sends data on a socket, returns true on success
|
// Sends data on a socket, returns true on success
|
||||||
int skSend(socket_t sock, const char *buf, int len);
|
int skSend(socket_t sock, const void *buf, int len);
|
||||||
// Sends data on a socket, returns true on success
|
// Sends data on a socket, returns true on success
|
||||||
int skSendPro(socket_t sock, const char *buf, int len, int flags);
|
int skSendPro(socket_t sock, const void *buf, int len, int flags);
|
||||||
// Sends data to a specific destination
|
// Sends data to a specific destination
|
||||||
int skSendTo(socket_t sock, const char *buf, int len, const sk_addrin_t *to);
|
int skSendTo(socket_t sock, const void *buf, int len, const sk_addrin_t *to);
|
||||||
// Sends data to a specific destination
|
// Sends data to a specific destination
|
||||||
int skSendToPro(socket_t sock, const char *buf, int len, int flags, const sk_addr_t *to, int tolen);
|
int skSendToPro(socket_t sock, const void *buf, int len, int flags, const sk_addr_t *to, int tolen);
|
||||||
// Receives data from a socket, returns byte count on success, 0 on connection close or -1 on error
|
// Receives data from a socket, returns byte count on success, 0 on connection close or -1 on error
|
||||||
int skReceive(socket_t sock, char *buf, int len);
|
int skReceive(socket_t sock, void *buf, int len);
|
||||||
// Receives data from a socket, returns byte count on success, 0 on connection close or -1 on error
|
// Receives data from a socket, returns byte count on success, 0 on connection close or -1 on error
|
||||||
int skReceivePro(socket_t sock, char *buf, int len, int flags);
|
int skReceivePro(socket_t sock, void *buf, int len, int flags);
|
||||||
// Receives a datagram and stores the source address.
|
// Receives a datagram and stores the source address.
|
||||||
int skReceiveFrom(socket_t sock, char *buf, int len, sk_addrin_t *from);
|
int skReceiveFrom(socket_t sock, void *buf, int len, sk_addrin_t *from);
|
||||||
// Receives a datagram and stores the source address.
|
// Receives a datagram and stores the source address.
|
||||||
int skReceiveFromPro(socket_t sock, char *buf, int len, int flags, sk_addr_t *from, socket_len_t *fromlen);
|
int skReceiveFromPro(socket_t sock, void *buf, int len, int flags, sk_addr_t *from, sk_len_t *fromlen);
|
||||||
|
|
||||||
// Checks that a opened socket is valid, returns true on success
|
// Checks that a opened socket is valid, returns true on success
|
||||||
bool skIsValid(socket_t sock);
|
bool skIsValid(socket_t sock);
|
||||||
|
|
|
||||||
3
str.c
3
str.c
|
|
@ -10,8 +10,7 @@
|
||||||
#include "tracelog.h"
|
#include "tracelog.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define VC_EXTRALEAN
|
#include "win32_slim.h"
|
||||||
#include <windows.h>
|
|
||||||
#else
|
#else
|
||||||
#include <iconv.h>
|
#include <iconv.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
9
str.h
9
str.h
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
#include <string>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
@ -9,13 +10,17 @@ extern "C" {
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
// #include "strview.h"
|
|
||||||
|
|
||||||
#define STRV_NOT_FOUND SIZE_MAX
|
#define STRV_NOT_FOUND SIZE_MAX
|
||||||
|
|
||||||
typedef struct {
|
typedef struct str_t {
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
#ifdef __cplusplus
|
||||||
|
inline operator std::string() const {
|
||||||
|
return std::string(buf, len);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
} str_t;
|
} str_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,7 @@
|
||||||
#define TLOG_NO_COLOURS
|
#define TLOG_NO_COLOURS
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define VC_EXTRALEAN
|
#include "win32_slim.h"
|
||||||
#include <windows.h>
|
|
||||||
#else
|
#else
|
||||||
#error "can't use TLOG_VS if not on windows"
|
#error "can't use TLOG_VS if not on windows"
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
57
win32_slim.h
Normal file
57
win32_slim.h
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WIN32_EXTRA_LEAN
|
||||||
|
#define WIN32_EXTRA_LEAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define NOIME
|
||||||
|
#define NOWINRES
|
||||||
|
#define NOGDICAPMASKS
|
||||||
|
#define NOVIRTUALKEYCODES
|
||||||
|
#define NOWINMESSAGES
|
||||||
|
#define NOWINSTYLES
|
||||||
|
#define NOSYSMETRICS
|
||||||
|
#define NOMENUS
|
||||||
|
#define NOICONS
|
||||||
|
#define NOKEYSTATES
|
||||||
|
#define NOSYSCOMMANDS
|
||||||
|
#define NORASTEROPS
|
||||||
|
#define NOSHOWWINDOW
|
||||||
|
#define OEMRESOURCE
|
||||||
|
#define NOATOM
|
||||||
|
#define NOCLIPBOARD
|
||||||
|
#define NOCOLOR
|
||||||
|
#define NOCTLMGR
|
||||||
|
#define NODRAWTEXT
|
||||||
|
#define NOGDI
|
||||||
|
#define NOUSER
|
||||||
|
#define NOMB
|
||||||
|
#define NOMEMMGR
|
||||||
|
#define NOMETAFILE
|
||||||
|
#define NOMINMAX
|
||||||
|
#define NOMSG
|
||||||
|
#define NOOPENFILE
|
||||||
|
#define NOSCROLL
|
||||||
|
#define NOSERVICE
|
||||||
|
#define NOSOUND
|
||||||
|
#define NOTEXTMETRIC
|
||||||
|
#define NOWH
|
||||||
|
#define NOWINOFFSETS
|
||||||
|
#define NOCOMM
|
||||||
|
#define NOKANJI
|
||||||
|
#define NOHELP
|
||||||
|
#define NOPROFILER
|
||||||
|
#define NODEFERWINDOWPOS
|
||||||
|
#define NOMCX
|
||||||
|
#define NOIME
|
||||||
|
#define NOPROXYSTUB
|
||||||
|
#define NOIMAGE
|
||||||
|
#define NO
|
||||||
|
#define NOTAPE
|
||||||
|
#define ANSI_ONLY
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue