diff options
author | Diego Nehab <diego.nehab@gmail.com> | 2019-02-19 16:08:47 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-19 16:08:47 -0300 |
commit | 144fa01c2f204e3b1b13c834f2644d100dba701b (patch) | |
tree | b3e8b110d2e8055b60778423a4438238c73a24c9 | |
parent | 57e04f55dcf4435a9ac63e8b514ec9026b76a63a (diff) | |
parent | 024646de54dca99c65b705a6c01260f1d1d8ff91 (diff) | |
download | luasocket-144fa01c2f204e3b1b13c834f2644d100dba701b.tar.gz luasocket-144fa01c2f204e3b1b13c834f2644d100dba701b.tar.bz2 luasocket-144fa01c2f204e3b1b13c834f2644d100dba701b.zip |
Merge pull request #264 from ewestbrook/freebsd-unix-bind-connect
src/unixdgram.c: fix connect() and bind() on FreeBSD
-rw-r--r-- | src/unixdgram.c | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/unixdgram.c b/src/unixdgram.c index 4645892..0d6f18c 100644 --- a/src/unixdgram.c +++ b/src/unixdgram.c | |||
@@ -17,6 +17,12 @@ | |||
17 | 17 | ||
18 | #define UNIXDGRAM_DATAGRAMSIZE 8192 | 18 | #define UNIXDGRAM_DATAGRAMSIZE 8192 |
19 | 19 | ||
20 | // provide a SUN_LEN macro if sys/un.h doesn't (e.g. Android) | ||
21 | #ifndef SUN_LEN | ||
22 | #define SUN_LEN(ptr) \ | ||
23 | ((size_t) (((struct sockaddr_un *) 0)->sun_path) \ | ||
24 | + strlen ((ptr)->sun_path)) | ||
25 | #endif | ||
20 | /*=========================================================================*\ | 26 | /*=========================================================================*\ |
21 | * Internal function prototypes | 27 | * Internal function prototypes |
22 | \*=========================================================================*/ | 28 | \*=========================================================================*/ |
@@ -261,20 +267,15 @@ static int meth_dirty(lua_State *L) { | |||
261 | static const char *unixdgram_trybind(p_unix un, const char *path) { | 267 | static const char *unixdgram_trybind(p_unix un, const char *path) { |
262 | struct sockaddr_un local; | 268 | struct sockaddr_un local; |
263 | size_t len = strlen(path); | 269 | size_t len = strlen(path); |
264 | int err; | ||
265 | if (len >= sizeof(local.sun_path)) return "path too long"; | 270 | if (len >= sizeof(local.sun_path)) return "path too long"; |
266 | memset(&local, 0, sizeof(local)); | 271 | memset(&local, 0, sizeof(local)); |
267 | strcpy(local.sun_path, path); | 272 | strcpy(local.sun_path, path); |
268 | local.sun_family = AF_UNIX; | 273 | local.sun_family = AF_UNIX; |
274 | size_t addrlen = SUN_LEN(&local); | ||
269 | #ifdef UNIX_HAS_SUN_LEN | 275 | #ifdef UNIX_HAS_SUN_LEN |
270 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) | 276 | local.sun_len = addrlen + 1; |
271 | + len + 1; | ||
272 | err = socket_bind(&un->sock, (SA *) &local, local.sun_len); | ||
273 | |||
274 | #else | ||
275 | err = socket_bind(&un->sock, (SA *) &local, | ||
276 | sizeof(local.sun_family) + len); | ||
277 | #endif | 277 | #endif |
278 | int err = socket_bind(&un->sock, (SA *) &local, addrlen); | ||
278 | if (err != IO_DONE) socket_destroy(&un->sock); | 279 | if (err != IO_DONE) socket_destroy(&un->sock); |
279 | return socket_strerror(err); | 280 | return socket_strerror(err); |
280 | } | 281 | } |
@@ -315,21 +316,17 @@ static int meth_getsockname(lua_State *L) | |||
315 | static const char *unixdgram_tryconnect(p_unix un, const char *path) | 316 | static const char *unixdgram_tryconnect(p_unix un, const char *path) |
316 | { | 317 | { |
317 | struct sockaddr_un remote; | 318 | struct sockaddr_un remote; |
318 | int err; | ||
319 | size_t len = strlen(path); | 319 | size_t len = strlen(path); |
320 | if (len >= sizeof(remote.sun_path)) return "path too long"; | 320 | if (len >= sizeof(remote.sun_path)) return "path too long"; |
321 | memset(&remote, 0, sizeof(remote)); | 321 | memset(&remote, 0, sizeof(remote)); |
322 | strcpy(remote.sun_path, path); | 322 | strcpy(remote.sun_path, path); |
323 | remote.sun_family = AF_UNIX; | 323 | remote.sun_family = AF_UNIX; |
324 | timeout_markstart(&un->tm); | 324 | timeout_markstart(&un->tm); |
325 | size_t addrlen = SUN_LEN(&remote); | ||
325 | #ifdef UNIX_HAS_SUN_LEN | 326 | #ifdef UNIX_HAS_SUN_LEN |
326 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) | 327 | remote.sun_len = addrlen + 1; |
327 | + len + 1; | ||
328 | err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); | ||
329 | #else | ||
330 | err = socket_connect(&un->sock, (SA *) &remote, | ||
331 | sizeof(remote.sun_family) + len, &un->tm); | ||
332 | #endif | 328 | #endif |
329 | int err = socket_connect(&un->sock, (SA *) &remote, addrlen, &un->tm); | ||
333 | if (err != IO_DONE) socket_destroy(&un->sock); | 330 | if (err != IO_DONE) socket_destroy(&un->sock); |
334 | return socket_strerror(err); | 331 | return socket_strerror(err); |
335 | } | 332 | } |