diff options
author | E. Westbrook <github@westbrook.io> | 2019-02-19 04:51:23 -0700 |
---|---|---|
committer | E. Westbrook <github@westbrook.io> | 2019-02-19 04:51:23 -0700 |
commit | 531012df1ad65d9b075a1644a930a09ac584f106 (patch) | |
tree | f36620bda675e7ab8b9eed5c5cc6618e788d3822 | |
parent | 288219fd6b53ce2e709745c9918aa4c4b7f715c9 (diff) | |
download | luasocket-531012df1ad65d9b075a1644a930a09ac584f106.tar.gz luasocket-531012df1ad65d9b075a1644a930a09ac584f106.tar.bz2 luasocket-531012df1ad65d9b075a1644a930a09ac584f106.zip |
src/unixdgram.c: allow connect() and bind() on freebsd without dummy char, and simplify calculations
-rw-r--r-- | src/unixdgram.c | 21 |
1 files changed, 6 insertions, 15 deletions
diff --git a/src/unixdgram.c b/src/unixdgram.c index 4645892..07c394c 100644 --- a/src/unixdgram.c +++ b/src/unixdgram.c | |||
@@ -261,20 +261,15 @@ static int meth_dirty(lua_State *L) { | |||
261 | static const char *unixdgram_trybind(p_unix un, const char *path) { | 261 | static const char *unixdgram_trybind(p_unix un, const char *path) { |
262 | struct sockaddr_un local; | 262 | struct sockaddr_un local; |
263 | size_t len = strlen(path); | 263 | size_t len = strlen(path); |
264 | int err; | ||
265 | if (len >= sizeof(local.sun_path)) return "path too long"; | 264 | if (len >= sizeof(local.sun_path)) return "path too long"; |
266 | memset(&local, 0, sizeof(local)); | 265 | memset(&local, 0, sizeof(local)); |
267 | strcpy(local.sun_path, path); | 266 | strcpy(local.sun_path, path); |
268 | local.sun_family = AF_UNIX; | 267 | local.sun_family = AF_UNIX; |
268 | size_t addrlen = sizeof(local) - sizeof(local.sun_path) + len; | ||
269 | #ifdef UNIX_HAS_SUN_LEN | 269 | #ifdef UNIX_HAS_SUN_LEN |
270 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) | 270 | 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 | 271 | #endif |
272 | int err = socket_bind(&un->sock, (SA *) &local, addrlen); | ||
278 | if (err != IO_DONE) socket_destroy(&un->sock); | 273 | if (err != IO_DONE) socket_destroy(&un->sock); |
279 | return socket_strerror(err); | 274 | return socket_strerror(err); |
280 | } | 275 | } |
@@ -315,21 +310,17 @@ static int meth_getsockname(lua_State *L) | |||
315 | static const char *unixdgram_tryconnect(p_unix un, const char *path) | 310 | static const char *unixdgram_tryconnect(p_unix un, const char *path) |
316 | { | 311 | { |
317 | struct sockaddr_un remote; | 312 | struct sockaddr_un remote; |
318 | int err; | ||
319 | size_t len = strlen(path); | 313 | size_t len = strlen(path); |
320 | if (len >= sizeof(remote.sun_path)) return "path too long"; | 314 | if (len >= sizeof(remote.sun_path)) return "path too long"; |
321 | memset(&remote, 0, sizeof(remote)); | 315 | memset(&remote, 0, sizeof(remote)); |
322 | strcpy(remote.sun_path, path); | 316 | strcpy(remote.sun_path, path); |
323 | remote.sun_family = AF_UNIX; | 317 | remote.sun_family = AF_UNIX; |
324 | timeout_markstart(&un->tm); | 318 | timeout_markstart(&un->tm); |
319 | size_t addrlen = sizeof(remote) - sizeof(remote.sun_path) + len; | ||
325 | #ifdef UNIX_HAS_SUN_LEN | 320 | #ifdef UNIX_HAS_SUN_LEN |
326 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) | 321 | 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 | 322 | #endif |
323 | int err = socket_connect(&un->sock, (SA *) &remote, addrlen, &un->tm); | ||
333 | if (err != IO_DONE) socket_destroy(&un->sock); | 324 | if (err != IO_DONE) socket_destroy(&un->sock); |
334 | return socket_strerror(err); | 325 | return socket_strerror(err); |
335 | } | 326 | } |