Teach "pigs" to use the Unix socket

This commit is contained in:
root 2023-12-05 19:13:54 +01:00 committed by Matthias Urlichs
parent 9710fd9499
commit 88c18b120d
1 changed files with 52 additions and 29 deletions

27
pigs.c
View File

@ -37,6 +37,7 @@ This version is for pigpio version 69+
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netdb.h>
#include <arpa/inet.h>
@ -109,7 +110,29 @@ static int initOpts(int argc, char *argv[])
static int openSocket(void)
{
int sock, err;
int sock;
const char *sockStr;
sockStr = getenv(PI_ENVSOCK);
if (sockStr && *sockStr)
{
struct sockaddr_un addr;
addr.sun_family = AF_LOCAL;
strncpy (addr.sun_path, sockStr, sizeof (addr.sun_path));
addr.sun_path[sizeof (addr.sun_path) - 1] = 0;
sock = socket (AF_LOCAL, SOCK_STREAM, 0);
if (sock > -1)
{
if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) == -1)
{
close(sock);
sock = -1;
}
}
}
else
{
int err;
struct addrinfo hints, *res, *rp;
const char *addrStr, *portStr;
@ -141,8 +164,8 @@ static int openSocket(void)
}
freeaddrinfo(res);
if (rp == NULL) return SOCKET_OPEN_FAILED;
}
return sock;
}