mirror of https://github.com/joan2937/pigpio
Teach "pigs" to use the Unix socket
This commit is contained in:
parent
9710fd9499
commit
88c18b120d
81
pigs.c
81
pigs.c
|
@ -37,6 +37,7 @@ This version is for pigpio version 69+
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/un.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
@ -109,40 +110,62 @@ static int initOpts(int argc, char *argv[])
|
||||||
|
|
||||||
static int openSocket(void)
|
static int openSocket(void)
|
||||||
{
|
{
|
||||||
int sock, err;
|
int sock;
|
||||||
struct addrinfo hints, *res, *rp;
|
const char *sockStr;
|
||||||
const char *addrStr, *portStr;
|
|
||||||
|
|
||||||
portStr = getenv(PI_ENVPORT);
|
sockStr = getenv(PI_ENVSOCK);
|
||||||
|
if (sockStr && *sockStr)
|
||||||
if (!portStr) portStr = PI_DEFAULT_SOCKET_PORT_STR;
|
|
||||||
|
|
||||||
addrStr = getenv(PI_ENVADDR);
|
|
||||||
|
|
||||||
if (!addrStr) addrStr = PI_DEFAULT_SOCKET_ADDR_STR;
|
|
||||||
|
|
||||||
memset (&hints, 0, sizeof (hints));
|
|
||||||
|
|
||||||
hints.ai_family = PF_UNSPEC;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_flags |= AI_CANONNAME;
|
|
||||||
|
|
||||||
err = getaddrinfo(addrStr, portStr, &hints, &res);
|
|
||||||
|
|
||||||
if (err) return SOCKET_OPEN_FAILED;
|
|
||||||
|
|
||||||
for (rp=res; rp!=NULL; rp=rp->ai_next)
|
|
||||||
{
|
{
|
||||||
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
struct sockaddr_un addr;
|
||||||
|
addr.sun_family = AF_LOCAL;
|
||||||
if (sock == -1) continue;
|
strncpy (addr.sun_path, sockStr, sizeof (addr.sun_path));
|
||||||
|
addr.sun_path[sizeof (addr.sun_path) - 1] = 0;
|
||||||
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break;
|
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;
|
||||||
|
|
||||||
freeaddrinfo(res);
|
portStr = getenv(PI_ENVPORT);
|
||||||
|
|
||||||
if (rp == NULL) return SOCKET_OPEN_FAILED;
|
if (!portStr) portStr = PI_DEFAULT_SOCKET_PORT_STR;
|
||||||
|
|
||||||
|
addrStr = getenv(PI_ENVADDR);
|
||||||
|
|
||||||
|
if (!addrStr) addrStr = PI_DEFAULT_SOCKET_ADDR_STR;
|
||||||
|
|
||||||
|
memset (&hints, 0, sizeof (hints));
|
||||||
|
|
||||||
|
hints.ai_family = PF_UNSPEC;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_flags |= AI_CANONNAME;
|
||||||
|
|
||||||
|
err = getaddrinfo(addrStr, portStr, &hints, &res);
|
||||||
|
|
||||||
|
if (err) return SOCKET_OPEN_FAILED;
|
||||||
|
|
||||||
|
for (rp=res; rp!=NULL; rp=rp->ai_next)
|
||||||
|
{
|
||||||
|
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
|
|
||||||
|
if (sock == -1) continue;
|
||||||
|
|
||||||
|
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
if (rp == NULL) return SOCKET_OPEN_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue