Skip to content

Commit

Permalink
Rework unix sockets
Browse files Browse the repository at this point in the history
Add support for unix datagram sockets and unix seqpacket sockets.

This required adding new address types for these and new gensio types.

The seqpacket sockets are mostly like unix stream sockets.  Not much
there that's complicated.

The datagram sockets are much more like UDP sockets, so the udp code
was renamed dgram and the datagram sockets were fit into there.

There were also some issues around unix socket address handling that
needed to be fixes as part of this.

Signed-off-by: Corey Minyard <[email protected]>
  • Loading branch information
cminyard committed Aug 20, 2024
1 parent 7085558 commit 1b61f83
Show file tree
Hide file tree
Showing 13 changed files with 899 additions and 231 deletions.
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ sctp
tcp
Normal TCP communication. Out of bound data is supported.

unix
Unix stream domain socket.

unixdgram
Unix datagram domain socket. This is sort-of connection oriented.

unixseq
Unix seqpacket domain socket. Probably only works on Linux.

udp
Sort-of connection oriented UDP.

Expand Down
32 changes: 16 additions & 16 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -704,33 +704,33 @@ esac
AM_CONDITIONAL([BUILTIN_NET], [test ${BUILTIN_NET} = 1])
AC_SUBST(DYNAMIC_NET)

udp=$default_all
AC_ARG_WITH(udp,
[AS_HELP_STRING([--with-udp=yes|dynamic|no], [Enable tcp/unix gensio])],
dgram=$default_all
AC_ARG_WITH(dgram,
[AS_HELP_STRING([--with-dgram=yes|dynamic|no], [Enable tcp/unix gensio])],
if test "x$withval" = "xyes"; then
udp=yes
dgram=yes
elif test "x$withval" = "xdynamic"; then
udp=dynamic
dgram=dynamic
elif test "x$withval" = "xno"; then
udp=no
dgram=no
fi,
)
BUILTIN_UDP=0
DYNAMIC_UDP=
case $udp in
BUILTIN_DGRAM=0
DYNAMIC_DGRAM=
case $dgram in
yes)
BUILTIN_GENSIOS="$BUILTIN_GENSIOS udp"
BUILTIN_UDP=1
BUILTIN_GENSIOS="$BUILTIN_GENSIOS dgram"
BUILTIN_DGRAM=1
;;
dynamic)
DYNAMIC_GENSIOS="$DYNAMIC_GENSIOS udp"
DYNAMIC_UDP=libgensio_udp.la
DYNAMIC_GENSIOS="$DYNAMIC_GENSIOS dgram"
DYNAMIC_DGRAM=libgensio_dgram.la
;;
no)
;;
esac
AM_CONDITIONAL([BUILTIN_UDP], [test ${BUILTIN_UDP} = 1])
AC_SUBST(DYNAMIC_UDP)
AM_CONDITIONAL([BUILTIN_DGRAM], [test ${BUILTIN_DGRAM} = 1])
AC_SUBST(DYNAMIC_DGRAM)

trysctp=yes
AC_ARG_WITH(sctp,
Expand Down Expand Up @@ -2397,7 +2397,7 @@ echo
echo "**************************************************"
echo "Gensios:"
echo " net: " $net
echo " udp: " $udp
echo " dgram: " $dgram
echo " sctp: " $sctp
echo " stdio: " $stdio
echo " pty: " $pty
Expand Down
2 changes: 2 additions & 0 deletions include/gensio/gensio_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ struct gensio_addr {
#define GENSIO_NET_PROTOCOL_UDP 2
#define GENSIO_NET_PROTOCOL_SCTP 3
#define GENSIO_NET_PROTOCOL_UNIX 4
#define GENSIO_NET_PROTOCOL_UNIX_DGRAM 5
#define GENSIO_NET_PROTOCOL_UNIX_SEQPACKET 6

/*
* Dealing with iterators.
Expand Down
14 changes: 7 additions & 7 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ libgensio_net_la_SOURCES = gensio_net.c
libgensio_net_la_LDFLAGS = $(DYNAMIC_LDFLAGS)
libgensio_net_la_LIBADD = $(DYNAMIC_LIBS)

if BUILTIN_UDP
libgensio_la_SOURCES += gensio_udp.c
if BUILTIN_DGRAM
libgensio_la_SOURCES += gensio_dgram.c
else
EXTRA_LTLIBRARIES += libgensio_udp.la
EXTRA_LTLIBRARIES += libgensio_dgram.la
endif
gensiolibexec_LTLIBRARIES += $(DYNAMIC_UDP)
libgensio_udp_la_SOURCES = gensio_udp.c
libgensio_udp_la_LDFLAGS = $(DYNAMIC_LDFLAGS)
libgensio_udp_la_LIBADD = $(DYNAMIC_LIBS)
gensiolibexec_LTLIBRARIES += $(DYNAMIC_DGRAM)
libgensio_dgram_la_SOURCES = gensio_dgram.c
libgensio_dgram_la_LDFLAGS = $(DYNAMIC_LDFLAGS)
libgensio_dgram_la_LIBADD = $(DYNAMIC_LIBS)

if BUILTIN_SCTP
libgensio_la_SOURCES += gensio_sctp.c
Expand Down
6 changes: 4 additions & 2 deletions lib/gensio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1563,9 +1563,11 @@ gensio_loadlib(struct gensio_os_funcs *o, const char *str)
return false;
memcpy(name, str, len);
name[len] = '\0';
if (strcmp(name, "tcp") == 0 || strcmp(name, "unix") == 0)
if (strcmp(name, "udp") == 0 || strcmp(name, "unixdgram") == 0)
strncpy(name, "dgram", sizeof(name));
else if (strcmp(name, "tcp") == 0 || strncmp(name, "unix", 4) == 0)
strncpy(name, "net", sizeof(name));
if (strcmp(name, "dev") == 0 || strcmp(name, "sdev") == 0)
else if (strcmp(name, "dev") == 0 || strcmp(name, "sdev") == 0)
strncpy(name, "serialdev", sizeof(name));

return gensio_os_loadlib(o, name);
Expand Down
27 changes: 21 additions & 6 deletions lib/gensio_addrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include <sys/un.h>
#endif

#define SIZEOF_SOCKADDR_UN_HEADER \
(sizeof(struct sockaddr_un) - \
sizeof(((struct sockaddr_un *) 0)->sun_path))

#include <gensio/gensio.h>
#include <gensio/gensio_os_funcs.h>
Expand Down Expand Up @@ -404,7 +407,8 @@ sockaddr_equal(const struct sockaddr *a1, socklen_t l1,
{
struct sockaddr_un *s1 = (struct sockaddr_un *) a1;
struct sockaddr_un *s2 = (struct sockaddr_un *) a2;
if (strcmp(s1->sun_path, s2->sun_path) != 0)
if (strncmp(s1->sun_path, s2->sun_path,
sizeof(s1->sun_path)) != 0)
return false;
}
break;
Expand Down Expand Up @@ -539,7 +543,7 @@ gensio_addr_addrinfo_to_str_all(const struct gensio_addr *aaddr,
}

static int
gensio_scan_unixaddr(struct gensio_os_funcs *o, const char *str,
gensio_scan_unixaddr(struct gensio_os_funcs *o, int socktype, const char *str,
struct gensio_addr **raddr)
{
#if HAVE_UNIX
Expand All @@ -552,7 +556,8 @@ gensio_scan_unixaddr(struct gensio_os_funcs *o, const char *str,
if (len >= sizeof(saddr->sun_path) - 1)
return GE_TOOBIG;

addr = gensio_addr_addrinfo_make(o, sizeof(socklen_t) + len + 1, false);
addr = gensio_addr_addrinfo_make(o, SIZEOF_SOCKADDR_UN_HEADER + len + 1,
false);
if (!addr)
return GE_NOMEM;

Expand All @@ -561,8 +566,8 @@ gensio_scan_unixaddr(struct gensio_os_funcs *o, const char *str,
saddr->sun_family = AF_UNIX;
memcpy(saddr->sun_path, str, len);
ai->ai_family = AF_UNIX;
ai->ai_socktype = SOCK_STREAM;
ai->ai_addrlen = sizeof(socklen_t) + len + 1;
ai->ai_socktype = socktype;
ai->ai_addrlen = SIZEOF_SOCKADDR_UN_HEADER + len + 1;
ai->ai_addr = (struct sockaddr *) saddr;

*raddr = addr;
Expand Down Expand Up @@ -755,8 +760,18 @@ gensio_addr_addrinfo_scan_ips(struct gensio_os_funcs *o, const char *str,
return GE_NOTSUP;
#endif

case GENSIO_NET_PROTOCOL_UNIX_DGRAM:
socktype = SOCK_DGRAM;
goto do_unix;
#ifdef SOCK_SEQPACKET
case GENSIO_NET_PROTOCOL_UNIX_SEQPACKET:
socktype = SOCK_SEQPACKET;
goto do_unix;
#endif
case GENSIO_NET_PROTOCOL_UNIX:
rv = gensio_scan_unixaddr(o, str, raddr);
socktype = SOCK_STREAM;
do_unix:
rv = gensio_scan_unixaddr(o, socktype, str, raddr);
if (!rv && is_port_set)
*is_port_set = false;
return rv;
Expand Down
Loading

0 comments on commit 1b61f83

Please sign in to comment.