Skip to content

Commit

Permalink
src/unixdgram.c: allow connect() and bind() on freebsd without dummy …
Browse files Browse the repository at this point in the history
…char, and simplify calculations
  • Loading branch information
ewestbrook committed Feb 19, 2019
1 parent 288219f commit 531012d
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/unixdgram.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,20 +261,15 @@ static int meth_dirty(lua_State *L) {
static const char *unixdgram_trybind(p_unix un, const char *path) {
struct sockaddr_un local;
size_t len = strlen(path);
int err;
if (len >= sizeof(local.sun_path)) return "path too long";
memset(&local, 0, sizeof(local));
strcpy(local.sun_path, path);
local.sun_family = AF_UNIX;
size_t addrlen = sizeof(local) - sizeof(local.sun_path) + len;
#ifdef UNIX_HAS_SUN_LEN
local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len)
+ len + 1;
err = socket_bind(&un->sock, (SA *) &local, local.sun_len);

#else
err = socket_bind(&un->sock, (SA *) &local,
sizeof(local.sun_family) + len);
local.sun_len = addrlen + 1;
#endif
int err = socket_bind(&un->sock, (SA *) &local, addrlen);
if (err != IO_DONE) socket_destroy(&un->sock);
return socket_strerror(err);
}
Expand Down Expand Up @@ -315,21 +310,17 @@ static int meth_getsockname(lua_State *L)
static const char *unixdgram_tryconnect(p_unix un, const char *path)
{
struct sockaddr_un remote;
int err;
size_t len = strlen(path);
if (len >= sizeof(remote.sun_path)) return "path too long";
memset(&remote, 0, sizeof(remote));
strcpy(remote.sun_path, path);
remote.sun_family = AF_UNIX;
timeout_markstart(&un->tm);
size_t addrlen = sizeof(remote) - sizeof(remote.sun_path) + len;
#ifdef UNIX_HAS_SUN_LEN
remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len)
+ len + 1;
err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm);
#else
err = socket_connect(&un->sock, (SA *) &remote,
sizeof(remote.sun_family) + len, &un->tm);
remote.sun_len = addrlen + 1;
#endif
int err = socket_connect(&un->sock, (SA *) &remote, addrlen, &un->tm);
if (err != IO_DONE) socket_destroy(&un->sock);
return socket_strerror(err);
}
Expand Down

0 comments on commit 531012d

Please sign in to comment.