view mgw/ctrl_prot.c @ 34:7dae2bae56a1

themwi-mgw: implement MDCX parameter validation
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 Jul 2022 23:17:15 -0800
parents b3f74df7b808
children 020ba624bdd8
line wrap: on
line source

/*
 * In this module we implement our control socket protocol.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <unistd.h>
#include "../include/tmgw_ctrl.h"
#include "struct.h"
#include "select.h"

struct endpoint *
find_ep_by_id(conn, id)
	struct ctrl_conn *conn;
	unsigned id;
{
	struct endpoint *ep;

	for (ep = conn->endp_list; ep; ep = ep->next)
		if (ep->ep_id == id)
			return ep;
	return 0;
}

void
ctrl_message_handler(fd, conn)
	struct ctrl_conn *conn;
{
	struct tmgw_ctrl_req req;
	struct tmgw_ctrl_resp resp;
	int rc;

	rc = recv(fd, &req, sizeof req, 0);
	if (rc < sizeof req) {
		syslog(LOG_INFO, "ctrl connection closing %s active endpoints",
			conn->endp_list ? "with" : "without");
		close(fd);
		FD_CLR(fd, &select_for_read);
		dlcx_all_on_conn(conn);
		free(conn);
		return;
	}
	bzero(&resp, sizeof resp);
	resp.transact_ref = req.transact_ref;
	switch (req.opcode) {
	case TMGW_CTRL_OP_CRCX:
		process_crcx(conn, &req, &resp);
		break;
	case TMGW_CTRL_OP_MDCX:
		process_mdcx(conn, &req, &resp);
		break;
	case TMGW_CTRL_OP_DLCX:
		process_dlcx(conn, &req, &resp);
		break;
	default:
		resp.res = TMGW_RESP_ERR_OPCODE;
	}
	send(fd, &resp, sizeof resp, 0);
}