/*
 * ws protocol handler plugin for "lws-minimal"
 *
 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 *
 * This version holds a single message at a time, which may be lost if a new
 * message comes.  See the minimal-ws-server-ring sample for the same thing
 * but using an lws_ring ringbuffer to hold up to 8 messages at a time.
 */

#if !defined (LWS_PLUGIN_STATIC)
#define LWS_DLL
#define LWS_INTERNAL
#include <libwebsockets.h>
#endif

#include <string.h>

/*
 * This came from...
 *
 * cat /dev/urandom | hexdump -C -n 1024 | tr -s ' ' | cut -d' ' -f 2-17 | head -n-1 | sed "s/\ /, 0x/g" | sed "s/^/0x/g" | sed "s/\$/,/g"
 *
 * ...then the length tuned by hand to get the ciphertext sizes that we want to
 * confirm are OK.
 *
 * We can only pass in a maximum of one compression buffer of input at a time,
 * which is 1024 by default.
 */

unsigned char uncompressible[] = {
	0xfe, 0xcc, 0x47, 0xcb, 0x10, 0xf4, 0x3c, 0x85,
	0x8e, 0xd4, 0xe2, 0xf6, 0xd1, 0xd1, 0xdb, 0x64,
	0x94, 0x50, 0xf6, 0x14, 0x25, 0x03, 0x09, 0x3a,
	0xb1, 0x47, 0x86, 0xa8, 0x3c, 0x4f, 0x3b, 0x98,
	0x7b, 0x3e, 0x67, 0x3e, 0x22, 0xc5, 0x4c, 0x45,
	0xf4, 0xf7, 0xb5, 0x79, 0xc0, 0x26, 0x6e, 0x5c,
	0xf4, 0x10, 0x04, 0xa9, 0x3c, 0x4f, 0xed, 0xc5,
	0x3d, 0xd4, 0x9f, 0x9f, 0xa3, 0xdb, 0x29, 0xeb,
	0x1e, 0xe1, 0x52, 0xab, 0xb5, 0x75, 0x25, 0x86,
	0x86, 0x02, 0x2c, 0x9d, 0x9c, 0x86, 0x46, 0x92,
	0xe9, 0x04, 0xd8, 0x2c, 0x7d, 0x8a, 0x56, 0xe1,
	0xe1, 0xb6, 0x84, 0x4d, 0x17, 0x30, 0x01, 0x60,
	0xa6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, 0x19, 0x70,
	0x5e, 0xcb, 0xf5, 0x87, 0xcc, 0x45, 0xf7, 0x60,
	0xd7, 0x9d, 0x1e, 0x2e, /* 1012 */

	/* up to here, this generates a 1022-byte single packet of compressed
	 * data that is well-formed and produces 1012 bytes of plaintext.
	 *
	 * The compressed packet ends
	 *
	 *  03F0: 70 5E CB F5 87 CC 45 F7 60 D7 9D 1E 2E 00
	 */

	0x54, /* 1013 */

	/* up to here, this generates a 1023-byte single packet of compressed
	 * data that is well-formed and produces 1013 bytes of plaintext.
	 *
	 * The compressed packet ends
	 *
	 *  03F0: 70 5E CB F5 87 CC 45 F7 60 D7 9D 1E 2E 54 00
	 */

	0x83, /* 1014 */

	/* up to here, a 1023-byte + 3-byte (1 byte payload) packet
	 * of uncompressed length 1014 */

	0x09, 0x99, 0xf9, 0x71, 0x9f, 0x15, 0x49, 0xda, 0xa8, 0x99, /* 1024 */

	/* up to here, a 1023-byte (1020 payload) + 3-byte (1 payload) packet
	 * of uncompressed length 1019 */

	0xf5, 0xe6, 0xa1, 0x71, 0x64, 0x9a, 0x95, 0xed,


};

/* generates ciphertext:        1022  1023  1023 + 3 1023 + 3 */
static int corner_lengths[] = {
/* bytes plaintext,	ciphertext */
	1012,	/*	1019 */
	1013,	/*	1020 */
	1014,	/*	1021 */
	1019,	/*	1021 */
	1024,	/*	1021*/
};


/* one of these is created for each client connecting to us */

struct per_session_data__minimal {
	int which;
	int last; /* 0 no test, else test number in corner_lengths[] + 1 */
};

static int
callback_minimal(struct lws *wsi, enum lws_callback_reasons reason,
			void *user, void *in, size_t len)
{
	struct per_session_data__minimal *pss =
			(struct per_session_data__minimal *)user;
	unsigned char buf[LWS_PRE + 2048];
	int m;

	switch (reason) {
	case LWS_CALLBACK_ESTABLISHED:
		if (lws_hdr_copy(wsi, (char *)buf, sizeof(buf),
				 WSI_TOKEN_GET_URI) < 0)
			return -1;

		pss->last = atoi((char *)buf + 1);

		if (pss->last > (int)LWS_ARRAY_SIZE(corner_lengths))
			pss->last = 0;
		lws_callback_on_writable(wsi);
		break;

	case LWS_CALLBACK_SERVER_WRITEABLE:
		if (!pss->last)
			break;

		lwsl_err("%s: writable %d, %d\n", __func__, pss->last,
				corner_lengths[pss->last - 1]);

		memcpy(buf + LWS_PRE, uncompressible,
		       corner_lengths[pss->last - 1]);

		/* notice we allowed for LWS_PRE in the payload already */
		m = lws_write(wsi, buf + LWS_PRE, corner_lengths[pss->last - 1],
				LWS_WRITE_BINARY);
		if (m < corner_lengths[pss->last - 1]) {
			lwsl_err("ERROR %d writing to ws socket\n", m);
			return -1;
		}

		pss->last = 0;
		break;

	default:
		break;
	}

	return 0;
}

#define LWS_PLUGIN_PROTOCOL_MINIMAL \
	{ \
		"lws-minimal", \
		callback_minimal, \
		sizeof(struct per_session_data__minimal), \
		2048, \
		0, NULL, 0 \
	}

#if !defined (LWS_PLUGIN_STATIC)

/* boilerplate needed if we are built as a dynamic plugin */

static const struct lws_protocols protocols[] = {
	LWS_PLUGIN_PROTOCOL_MINIMAL
};

int
init_protocol_minimal(struct lws_context *context,
		      struct lws_plugin_capability *c)
{
	if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
		lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
			 c->api_magic);
		return 1;
	}

	c->protocols = protocols;
	c->count_protocols = LWS_ARRAY_SIZE(protocols);
	c->extensions = NULL;
	c->count_extensions = 0;

	return 0;
}

int
destroy_protocol_minimal(struct lws_context *context)
{
	return 0;
}
#endif
