#!/bin/sh
# Validate the fixed-width NLB SSL response address fields.
set -eu
cd "$(dirname "$0")/.."
CC="${CC:-cc}"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT

cat > "$TMP/test.c" <<'EOF'
#include <arpa/inet.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define IPV4_VIP_OFFSET 4
#define IPV4_GW_OFFSET 19
#define IPV4_FIELD_LEN 15
#define IPV6_RESPONSE_LEN 188
#define IPV6_VIP_OFFSET 98
#define IPV6_GW_OFFSET 137
#define IPV6_FIELD_LEN 39

struct nlb_config {
	char *vip;
	char *inner;
	char *vip6;
	char *inner6;
};

static int copy_ip(const char *line, int line_len, int offset, int field_len,
		   int af, char **out)
{
	char field[INET6_ADDRSTRLEN];
	char *end;
	unsigned char addr[sizeof(struct in6_addr)];

	if (offset > line_len || field_len > line_len - offset ||
	    field_len >= (int)sizeof(field))
		return -1;
	memcpy(field, line + offset, field_len);
	field[field_len] = '\0';
	end = field + field_len;
	while (end > field && isspace((unsigned char)end[-1]))
		*--end = '\0';
	if (inet_pton(af, field, addr) != 1)
		return -1;
	*out = strdup(field);
	return *out ? 0 : -1;
}

static int parse_response(const char *line, int len, struct nlb_config *nlb)
{
	if (len == 12 && !memcmp(line, "START_TUNNEL", 12))
		return 0;
	if (len < IPV4_GW_OFFSET + IPV4_FIELD_LEN ||
	    memcmp(line, "110 ", 4) || !strstr(line, "START_TUNNEL") ||
	    copy_ip(line, len, IPV4_VIP_OFFSET, IPV4_FIELD_LEN, AF_INET,
		    &nlb->vip) ||
	    copy_ip(line, len, IPV4_GW_OFFSET, IPV4_FIELD_LEN, AF_INET,
		    &nlb->inner))
		return -1;
	if (len == IPV6_RESPONSE_LEN &&
	    (copy_ip(line, len, IPV6_VIP_OFFSET, IPV6_FIELD_LEN, AF_INET6,
		     &nlb->vip6) ||
	     copy_ip(line, len, IPV6_GW_OFFSET, IPV6_FIELD_LEN, AF_INET6,
		     &nlb->inner6)))
		return -1;
	return 0;
}

static void free_config(struct nlb_config *nlb)
{
	free(nlb->vip);
	free(nlb->inner);
	free(nlb->vip6);
	free(nlb->inner6);
	memset(nlb, 0, sizeof(*nlb));
}

int main(void)
{
	static const char ipv4[] =
		"110 127.127.127.127192.168.255.43 "
		"c057fc7f7c6b74f9ac68b639bb056732404cbacf START_TUNNEL";
	char dual[IPV6_RESPONSE_LEN + 1];
	struct nlb_config nlb = { 0 };

	if (parse_response(ipv4, strlen(ipv4), &nlb) ||
	    strcmp(nlb.vip, "127.127.127.127") ||
	    strcmp(nlb.inner, "192.168.255.43") || nlb.vip6 || nlb.inner6)
		return 1;
	free_config(&nlb);

	memset(dual, ' ', IPV6_RESPONSE_LEN);
	dual[IPV6_RESPONSE_LEN] = '\0';
	memcpy(dual, "110 ", 4);
	memcpy(dual + IPV4_VIP_OFFSET, "127.127.127.127", 15);
	memcpy(dual + IPV4_GW_OFFSET, "192.168.255.43", 14);
	memcpy(dual + IPV6_VIP_OFFSET, "2001:db8::8", 11);
	memcpy(dual + IPV6_GW_OFFSET, "2001:db8::1", 11);
	memcpy(dual + IPV6_RESPONSE_LEN - 12, "START_TUNNEL", 12);
	if (parse_response(dual, IPV6_RESPONSE_LEN, &nlb) ||
	    strcmp(nlb.vip, "127.127.127.127") ||
	    strcmp(nlb.inner, "192.168.255.43") ||
	    strcmp(nlb.vip6, "2001:db8::8") ||
	    strcmp(nlb.inner6, "2001:db8::1"))
		return 1;
	free_config(&nlb);

	if (parse_response("START_TUNNEL", 12, &nlb) ||
	    !parse_response("HTTP/1.1 200 OK", 15, &nlb))
		return 1;
	puts("gp-nlb-ssl-parse: OK");
	return 0;
}
EOF

$CC -o "$TMP/test" "$TMP/test.c"
"$TMP/test"
