view cmu200/socket.c @ 132:94e8a410d6bd

fc-rfcal-txcheck: pass PCL to tester (txpwr-cal-pcl)
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 20 Dec 2021 03:52:01 +0000
parents bd62be88259d
children
line wrap: on
line source

/*
 * 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));
}