view smpp-trx-sa/localsock.c @ 234:44f178901a46

libnumdb2: port refresh.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 13:17:07 -0800
parents 9d6e8d99d2b1
children
line wrap: on
line source

/*
 * This module implements the local socket part of smpp-trx-sa.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int localsock;

void
create_local_socket(pathname)
	char *pathname;
{
	struct sockaddr_un sa;
	unsigned sa_len;
	int rc;

	localsock = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (localsock < 0) {
		perror("socket(AF_UNIX, SOCK_DGRAM, 0)");
		exit(1);
	}
	unlink(pathname);
	fill_sockaddr_un(pathname, &sa, &sa_len);
	rc = bind(localsock, (struct sockaddr *) &sa, sa_len);
	if (rc < 0) {
		fprintf(stderr, "bind error on %s\n", pathname);
		perror("bind");
		exit(1);
	}
}

void
localsock_select_handler()
{
	u_char buf[0x10000];
	int cc;
	unsigned pdu_len;

	cc = recv(localsock, buf, sizeof buf, 0);
	if (cc <= 0) {
		perror("read from local socket");
		log_fatal_error("error reading from local socket");
		exit(1);
	}
	if (cc < 16) {
inv_pdu:	log_fatal_error("Received invalid PDU on local socket");
		return;
	}
	if (buf[0] || buf[1])
		goto inv_pdu;
	pdu_len = (buf[2] << 8) | buf[3];
	if (pdu_len != cc)
		goto inv_pdu;
	send_pdu_from_localsock(buf, pdu_len);
}