view rfcal/cmu200/socket.c @ 195:db9ee7745cdd

fc-cmu200d: socket handling skeleton added
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Apr 2017 01:43:02 +0000
parents
children 47d56330609d
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;
{
	write(activesock, str, strlen(str));
}