diff cmu200/socket.c @ 0:bd62be88259d

initial import of rfcal code and docs from freecalypso-tools repository
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 May 2017 18:49:35 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmu200/socket.c	Sat May 20 18:49:35 2017 +0000
@@ -0,0 +1,81 @@
+/*
+ * This module handles the local UNIX domain socket interface for fc-cmu200d.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int listener, activesock;
+
+extern char *bind_socket_pathname;
+
+create_listener_socket()
+{
+	/* local socket binding voodoo copied from osmocon */
+	struct sockaddr_un local;
+	unsigned int namelen;
+	int rc;
+
+	listener = socket(AF_UNIX, SOCK_STREAM, 0);
+	if (listener < 0) {
+		perror("socket(AF_UNIX, SOCK_STREAM, 0)");
+		exit(1);
+	}
+
+	local.sun_family = AF_UNIX;
+	strncpy(local.sun_path, bind_socket_pathname, sizeof(local.sun_path));
+	local.sun_path[sizeof(local.sun_path) - 1] = '\0';
+	unlink(local.sun_path);
+
+	/* we use the same magic that X11 uses in Xtranssock.c for
+	 * calculating the proper length of the sockaddr */
+#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
+	local.sun_len = strlen(local.sun_path);
+#endif
+#if defined(BSD44SOCKETS) || defined(SUN_LEN)
+	namelen = SUN_LEN(&local);
+#else
+	namelen = strlen(local.sun_path) +
+		  offsetof(struct sockaddr_un, sun_path) + 1;
+#endif
+
+	rc = bind(listener, (struct sockaddr *) &local, namelen);
+	if (rc != 0) {
+		perror("bind on local socket");
+		exit(1);
+	}
+	rc = listen(listener, 1);
+	if (rc != 0) {
+		perror("listen");
+		exit(1);
+	}
+	return(0);
+}
+
+get_socket_connection()
+{
+	struct sockaddr_un un_addr;
+	socklen_t len;
+
+	len = sizeof(un_addr);
+	activesock = accept(listener, (struct sockaddr *) &un_addr, &len);
+	if (activesock < 0) {
+		perror("socket accept");
+		exit(1);
+	}
+	printf("Accepted local socket connection\n");
+	return(0);
+}
+
+send_socket_response(str)
+	char *str;
+{
+	printf("Msg to client: %s", str);
+	write(activesock, str, strlen(str));
+}