Let patty_kiss_tnc_new() use Unix domain sockets

Modify patty_kiss_tnc_new() to connect to a Unix domain socket
This commit is contained in:
XANTRONIX Development 2020-07-19 23:36:35 -04:00 committed by XANTRONIX Industrial
parent 67f66d5570
commit 216bd91050

View file

@ -5,7 +5,8 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <patty/kiss.h>
@ -66,9 +67,36 @@ error_malloc_tnc:
patty_kiss_tnc *patty_kiss_tnc_new(const char *device) {
patty_kiss_tnc *tnc;
int fd;
struct stat st;
if ((fd = open(device, O_RDWR)) < 0) {
goto error_open;
if (stat(device, &st) < 0) {
goto error_stat;
}
if (st.st_mode & S_IFSOCK) {
struct sockaddr_un addr;
if (strlen(device) > sizeof(addr.sun_path)) {
errno = EOVERFLOW;
goto error_overflow;
}
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
goto error_socket;
}
memset(&addr, '\0', sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, device, sizeof(addr.sun_path));
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
goto error_connect;
}
} else {
if ((fd = open(device, O_RDWR)) < 0) {
goto error_open;
}
}
if ((tnc = patty_kiss_tnc_new_fd(fd)) == NULL) {
@ -80,9 +108,13 @@ patty_kiss_tnc *patty_kiss_tnc_new(const char *device) {
return tnc;
error_tnc_new_fd:
error_connect:
close(fd);
error_socket:
error_overflow:
error_open:
error_stat:
return NULL;
}