simpler socket api. Now the blocking of functions and error codes aren't function parameters but attributes of the socket object with their respective getters and setters
This commit is contained in:
+2
-57
@@ -5,8 +5,6 @@
|
||||
#include <tuntap.h>
|
||||
#include "xhttp.h"
|
||||
|
||||
static xh_handle handle;
|
||||
|
||||
static void callback(xh_request *req, xh_response *res, void *userp)
|
||||
{
|
||||
(void) req;
|
||||
@@ -16,63 +14,10 @@ static void callback(xh_request *req, xh_response *res, void *userp)
|
||||
res->body.str = "Hello, world!";
|
||||
xh_header_add(res, "Content-Type", "text/plain");
|
||||
}
|
||||
/*
|
||||
static void handle_sigterm(int signum)
|
||||
{
|
||||
(void) signum;
|
||||
xh_quit(handle);
|
||||
}
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/*
|
||||
signal(SIGTERM, handle_sigterm);
|
||||
signal(SIGINT, handle_sigterm);
|
||||
|
||||
#ifndef _WIN32
|
||||
signal(SIGQUIT, handle_sigterm);
|
||||
#endif
|
||||
*/
|
||||
char ip[] = "10.0.0.4";
|
||||
char mac[] = "00:01:00:01:00:00";
|
||||
|
||||
struct device *dev = tuntap_init();
|
||||
if (!dev) {
|
||||
fprintf(stderr, "Error: Couldn't initialize the TAP library\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// This must be set AFTER tuntap_init because
|
||||
// it sets the callback function to the default
|
||||
// callback which writes to stderr.
|
||||
//tuntap_log_set_cb(NULL);
|
||||
|
||||
int netmask = 24; // TODO: Make this configurable
|
||||
|
||||
if (tuntap_start(dev, TUNTAP_MODE_ETHERNET, TUNTAP_ID_ANY)) {
|
||||
fprintf(stderr, "Error: Couldn't set up the TAP device\n");
|
||||
tuntap_release(dev);
|
||||
return -1;
|
||||
}
|
||||
|
||||
tuntap_set_ip(dev, "10.0.0.5", netmask);
|
||||
tuntap_set_hwaddr(dev, mac);
|
||||
|
||||
if (tuntap_up(dev)) {
|
||||
fprintf(stderr, "Error: Couldn't activate the TAP device\n");
|
||||
tuntap_release(dev);
|
||||
}
|
||||
|
||||
microhttp_config_t config = {
|
||||
.userp = dev,
|
||||
.ip = ip,
|
||||
.mac = mac,
|
||||
.recv_frame = (int(*)(void*, void*, size_t)) tuntap_read,
|
||||
.send_frame = (int(*)(void*, const void*, size_t)) tuntap_write,
|
||||
};
|
||||
|
||||
const char *error = xhttp(80, callback, NULL, &handle, config);
|
||||
tuntap_release(dev);
|
||||
const char *error = xhttp(80, callback, NULL);
|
||||
if(error != NULL) {
|
||||
fprintf(stderr, "Error: %s\n", error);
|
||||
return 1;
|
||||
|
||||
+29
-44
@@ -512,11 +512,9 @@ static void req_deinit(xh_request *req)
|
||||
|
||||
static conn_t *accept_connection(context_t *ctx)
|
||||
{
|
||||
microtcp_errcode_t errcode;
|
||||
microtcp_socket_t *accepted_sock = microtcp_accept(ctx->sock, true, &errcode);
|
||||
|
||||
if(accepted_sock == NULL)
|
||||
return NULL; // Failed to accept.
|
||||
microtcp_socket_t *accepted_sock = microtcp_accept(ctx->sock);
|
||||
if(accepted_sock == NULL) return NULL;
|
||||
microtcp_set_blocking(accepted_sock, false);
|
||||
|
||||
if(ctx->freelist == NULL)
|
||||
{
|
||||
@@ -953,11 +951,12 @@ static bool upload(conn_t *conn)
|
||||
|
||||
while(sent < total)
|
||||
{
|
||||
microtcp_errcode_t errcode;
|
||||
size_t n = microtcp_send(conn->sock, conn->out.data + sent, total - sent, true, &errcode);
|
||||
|
||||
if(n == 0)
|
||||
int n = microtcp_send(conn->sock, conn->out.data + sent, total - sent);
|
||||
if(n <= 0)
|
||||
{
|
||||
microtcp_errcode_t errcode = microtcp_get_socket_error(conn->sock);
|
||||
microtcp_clear_socket_error(conn->sock);
|
||||
|
||||
if (errcode == MICROTCP_ERRCODE_WOULDBLOCK)
|
||||
break;
|
||||
|
||||
@@ -1254,26 +1253,24 @@ static void when_data_is_ready_to_be_read(context_t *ctx, conn_t *conn)
|
||||
|
||||
assert(b->size > b->used);
|
||||
|
||||
microtcp_errcode_t errcode;
|
||||
size_t n = microtcp_recv(conn->sock, b->data + b->used, b->size - b->used, true, &errcode);
|
||||
|
||||
if(n == 0)
|
||||
int n = microtcp_recv(conn->sock, b->data + b->used, b->size - b->used);
|
||||
if (n == 0) {
|
||||
// Peer disconnected.
|
||||
close_connection(ctx, conn);
|
||||
return;
|
||||
}
|
||||
|
||||
if(n < 0)
|
||||
{
|
||||
// Peer disconnected or an error occurred
|
||||
microtcp_errcode_t errcode = microtcp_get_socket_error(conn->sock);
|
||||
microtcp_clear_socket_error(conn->sock);
|
||||
|
||||
if(errcode == MICROTCP_ERRCODE_WOULDBLOCK)
|
||||
break; // Done downloading.
|
||||
if (errcode != MICROTCP_ERRCODE_NONE)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "XHTTP :: %s\n", microtcp_strerror(errcode));
|
||||
fprintf(stderr, "XHTTP :: %s\n", microtcp_strerror(errcode));
|
||||
#endif
|
||||
// An error occurred.
|
||||
close_connection(ctx, conn);
|
||||
return;
|
||||
}
|
||||
|
||||
// Peer disconnected.
|
||||
// An error occurred.
|
||||
close_connection(ctx, conn);
|
||||
return;
|
||||
}
|
||||
@@ -1407,31 +1404,23 @@ void xh_quit(xh_handle handle)
|
||||
ctx->exiting = 1;
|
||||
}
|
||||
|
||||
static const char *init(context_t *context, unsigned short port,
|
||||
microhttp_config_t config)
|
||||
static const char *init(context_t *context, unsigned short port)
|
||||
{
|
||||
microtcp_callbacks_t callbacks = {
|
||||
.data = config.userp,
|
||||
.free = NULL,
|
||||
.recv = config.recv_frame,
|
||||
.send = config.send_frame,
|
||||
};
|
||||
microtcp_t *mtcp = microtcp_create_using_callbacks(config.ip, config.mac, callbacks);
|
||||
microtcp_t *mtcp = microtcp_create("10.0.0.5", "10.0.0.4", NULL, NULL);
|
||||
if (!mtcp)
|
||||
return "Failed to initialize TCP";
|
||||
context->mtcp = mtcp;
|
||||
|
||||
microtcp_errcode_t errcode;
|
||||
|
||||
context->sock = microtcp_open(mtcp, port, &errcode);
|
||||
context->sock = microtcp_open(mtcp, port);
|
||||
if (!context->sock)
|
||||
return microtcp_strerror(errcode);
|
||||
return microtcp_strerror(microtcp_get_error(mtcp));
|
||||
microtcp_set_blocking(context->sock, false);
|
||||
|
||||
context->mux = microtcp_mux_create(mtcp);
|
||||
if (!context->mux)
|
||||
{
|
||||
microtcp_close(context->sock);
|
||||
return microtcp_strerror(errcode);
|
||||
return "Couldn't craete mux";
|
||||
}
|
||||
|
||||
if (!microtcp_mux_register(context->mux, context->sock, MICROTCP_MUX_ACCEPT, NULL))
|
||||
@@ -1454,27 +1443,23 @@ static const char *init(context_t *context, unsigned short port,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *xhttp(unsigned short port, xh_callback callback,
|
||||
void *userp, xh_handle *handle,
|
||||
microhttp_config_t config)
|
||||
const char *xhttp(unsigned short port, xh_callback callback, xh_handle *handle)
|
||||
{
|
||||
context_t context;
|
||||
|
||||
const char *error = init(&context, port, config);
|
||||
const char *error = init(&context, port);
|
||||
|
||||
if(error != NULL)
|
||||
return error;
|
||||
|
||||
context.callback = callback;
|
||||
context.userp = userp;
|
||||
context.userp = NULL;
|
||||
|
||||
if(handle)
|
||||
*handle = &context;
|
||||
|
||||
while(!context.exiting)
|
||||
{
|
||||
microtcp_step(context.mtcp);
|
||||
|
||||
microtcp_muxevent_t ev;
|
||||
if (!microtcp_mux_wait(context.mux, &ev))
|
||||
continue;
|
||||
|
||||
@@ -58,9 +58,7 @@ typedef struct {
|
||||
|
||||
typedef void (*xh_callback)(xh_request*, xh_response*, void*);
|
||||
|
||||
const char *xhttp(unsigned short port, xh_callback callback,
|
||||
void *userp, xh_handle *handle,
|
||||
microhttp_config_t config);
|
||||
const char *xhttp(unsigned short port, xh_callback callback, xh_handle *handle);
|
||||
void xh_quit(xh_handle handle);
|
||||
|
||||
void xh_header_add(xh_response *res, const char *name, const char *valfmt, ...);
|
||||
@@ -71,7 +69,6 @@ _Bool xh_header_cmp(const char *a, const char *b);
|
||||
int xh_urlcmp(const char *URL, const char *fmt, ...);
|
||||
int xh_vurlcmp(const char *URL, const char *fmt, va_list va);
|
||||
|
||||
|
||||
#define xh_string_new(s, l) \
|
||||
((xh_string) { (s), ((int) (l)) < 0 ? (int) strlen(s) : (int) (l) })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user