mirror of https://github.com/joan2937/pigpio
Teach "pigs" to use the Unix socket
This commit is contained in:
parent
9710fd9499
commit
88c18b120d
27
pigs.c
27
pigs.c
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue