chg: SOCKET: connections (@xianling1998)

This commit is contained in:
iceman1001 2020-04-16 16:46:45 +02:00
parent 1bf32aad90
commit b0854bb3aa

View file

@ -48,6 +48,8 @@
#include <fcntl.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include "sys/socket.h"
#include "sys/un.h"
#include "comms.h"
@ -156,6 +158,51 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
return sp;
}
// The socket for abstract namespace implement.
// Is local socket buffer, not a TCP or any net connection!
// so, you can't connect with address like: 127.0.0.1, or any IP
// see http://man7.org/linux/man-pages/man7/unix.7.html
if (memcmp(pcPortName, "socket:", 7) == 0) {
if (strlen(pcPortName) <= 7) {
free(sp);
return INVALID_SERIAL_PORT;
}
// we must use max timeout!
timeout.tv_usec = UART_TCP_CLIENT_RX_TIMEOUT_MS * 1000;
size_t servernameLen = (strlen(pcPortName) - 7) + 1;
char serverNameBuf[servernameLen];
memset(serverNameBuf, '\0', servernameLen);
for (int i = 7, j = 0; j < servernameLen; ++i, ++j) {
serverNameBuf[j] = pcPortName[i];
}
serverNameBuf[servernameLen - 1] = '\0';
int localsocket, len;
struct sockaddr_un remote;
remote.sun_path[0] = '\0'; // abstract namespace
strcpy(remote.sun_path + 1, serverNameBuf);
remote.sun_family = AF_LOCAL;
int nameLen = strlen(serverNameBuf);
len = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
if ((localsocket = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1) {
free(sp);
return INVALID_SERIAL_PORT;
}
if (connect(localsocket, (struct sockaddr *) &remote, len) == -1) {
free(sp);
return INVALID_SERIAL_PORT;
}
sp->fd = localsocket;
return sp;
}
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (sp->fd == -1) {
uart_close(sp);