diff smpp-trx-sa/localsock.c @ 222:9d6e8d99d2b1

smpp-trx-sa: new program
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 03 Aug 2023 21:13:41 -0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smpp-trx-sa/localsock.c	Thu Aug 03 21:13:41 2023 -0800
@@ -0,0 +1,60 @@
+/*
+ * 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);
+}