Use getaddrinfo() to fill struct sockaddr_un. It now supports

SOCK_DGRAM and SOCK_SEQPACKET in addition to SOCK_STREAM.
This commit is contained in:
Hiroki Sato 2015-10-03 12:49:05 +00:00
parent 4c60a05d25
commit 1c9fbb5a26
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=288602

View File

@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$");
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <errno.h> #include <errno.h>
#include <netdb.h>
#endif #endif
#include <ctype.h> #include <ctype.h>
@ -302,31 +303,39 @@ raw_cat(int rfd)
static int static int
udom_open(const char *path, int flags) udom_open(const char *path, int flags)
{ {
struct sockaddr_un sou; struct addrinfo hints, *res, *res0;
int fd; char rpath[PATH_MAX];
unsigned int len; int fd, error;
bzero(&sou, sizeof(sou));
/* /*
* Construct the unix domain socket address and attempt to connect * Construct the unix domain socket address and attempt to connect.
*/ */
fd = socket(AF_UNIX, SOCK_STREAM, 0); bzero(&hints, sizeof(hints));
if (fd >= 0) { hints.ai_family = AF_LOCAL;
sou.sun_family = AF_UNIX; if (realpath(path, rpath) == NULL)
if ((len = strlcpy(sou.sun_path, path, return (-1);
sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) { error = getaddrinfo(rpath, NULL, &hints, &res0);
close(fd); if (error) {
errno = ENAMETOOLONG; warn("%s", gai_strerror(error));
errno = EINVAL;
return (-1);
}
for (res = res0; res != NULL; res = res->ai_next) {
fd = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (fd < 0) {
freeaddrinfo(res0);
return (-1); return (-1);
} }
len = offsetof(struct sockaddr_un, sun_path[len+1]); error = connect(fd, res->ai_addr, res->ai_addrlen);
if (error == 0)
if (connect(fd, (void *)&sou, len) < 0) { break;
else {
close(fd); close(fd);
fd = -1; fd = -1;
} }
} }
freeaddrinfo(res0);
/* /*
* handle the open flags by shutting down appropriate directions * handle the open flags by shutting down appropriate directions