Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket: persist connect and listen errors #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 71 additions & 57 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,10 @@ static lso_nargs_t lso_connect2(lua_State *L) {
if ((error = lso_prepsocket(S)))
goto error;

(void)so_connect(S->socket);
if ((error = so_connect(S->socket))) {
S->ibuf.error = error;
S->obuf.error = error;
}

return 1;
error:
Expand All @@ -965,25 +968,6 @@ static lso_nargs_t lso_connect2(lua_State *L) {
} /* lso_connect2() */


static lso_nargs_t lso_connect1(lua_State *L) {
struct luasocket *S = lso_checkself(L, 1);
int error;

so_clear(S->socket);

if (!(error = so_connect(S->socket))) {
lua_pushvalue(L, 1);

return 1;
} else {
lua_pushnil(L);
lua_pushinteger(L, error);

return 2;
}
} /* lso_connect1() */


static lso_nargs_t lso_listen2(lua_State *L) {
const char *host NOTUSED = NULL, *port NOTUSED = NULL;
const char *path = NULL;
Expand Down Expand Up @@ -1042,7 +1026,10 @@ static lso_nargs_t lso_listen2(lua_State *L) {
if ((error = lso_prepsocket(S)))
goto error;

(void)so_listen(S->socket);
if ((error = so_listen(S->socket))) {
S->ibuf.error = error;
S->obuf.error = error;
}

return 1;
error:
Expand All @@ -1053,25 +1040,6 @@ static lso_nargs_t lso_listen2(lua_State *L) {
} /* lso_listen2() */


static lso_nargs_t lso_listen1(lua_State *L) {
struct luasocket *S = lso_checkself(L, 1);
int error;

so_clear(S->socket);

if (!(error = so_listen(S->socket))) {
lua_pushvalue(L, 1);

return 1;
} else {
lua_pushnil(L);
lua_pushinteger(L, error);

return 2;
}
} /* lso_listen1() */


/* luasec compat */
#define LSEC_MODE_INVALID 0
#define LSEC_MODE_SERVER 1
Expand Down Expand Up @@ -1660,6 +1628,69 @@ static lso_nargs_t lso_clearerr(struct lua_State *L) {
} /* lso_clearerr() */


#define LSO_CHECKERRS(L, iobuf) do { \
if (!(iobuf).error) \
return 0; \
if (++(iobuf).numerrs > (iobuf).maxerrs) \
luaL_error((L), "exceeded unchecked error limit (%s)", cqs_strerror((iobuf).error)); \
return (iobuf).error; \
} while (0)

static lso_error_t lso_checkrcverrs(lua_State *L, struct luasocket *S) {
LSO_CHECKERRS(L, S->ibuf);
} /* lso_checkrcverrs() */

static lso_error_t lso_checksnderrs(lua_State *L, struct luasocket *S) {
LSO_CHECKERRS(L, S->obuf);
} /* lso_checksnderrs() */


static lso_nargs_t lso_connect1(lua_State *L) {
struct luasocket *S = lso_checkself(L, 1);
int error;

if ((error = lso_checkrcverrs(L, S)) || (error = lso_checksnderrs(L, S)))
goto error;

so_clear(S->socket);

if ((error = so_connect(S->socket)))
goto error;

lua_pushvalue(L, 1);

return 1;

error:
lua_pushnil(L);
lua_pushinteger(L, error);

return 2;
} /* lso_connect1() */


static lso_nargs_t lso_listen1(lua_State *L) {
struct luasocket *S = lso_checkself(L, 1);
int error;

if ((error = lso_checkrcverrs(L, S)) || (error = lso_checksnderrs(L, S)))
goto error;

so_clear(S->socket);

if ((error = so_listen(S->socket)))
goto error;

return 1;

error:
lua_pushnil(L);
lua_pushinteger(L, error);

return 2;
} /* lso_listen1() */


static lso_error_t lso_fill(struct luasocket *S, size_t limit) {
struct iovec iov;
size_t prepbuf, count;
Expand Down Expand Up @@ -1939,23 +1970,6 @@ static struct lso_rcvop lso_checkrcvop(lua_State *L, int index, int mode) {
} /* lso_checkrcvop() */


#define LSO_CHECKERRS(L, iobuf) do { \
if (!(iobuf).error) \
return 0; \
if (++(iobuf).numerrs > (iobuf).maxerrs) \
luaL_error((L), "exceeded unchecked error limit (%s)", cqs_strerror((iobuf).error)); \
return (iobuf).error; \
} while (0)

static lso_error_t lso_checkrcverrs(lua_State *L, struct luasocket *S) {
LSO_CHECKERRS(L, S->ibuf);
} /* lso_checkrcverrs() */

static lso_error_t lso_checksnderrs(lua_State *L, struct luasocket *S) {
LSO_CHECKERRS(L, S->obuf);
} /* lso_checksnderrs() */


static lso_error_t lso_preprcv(lua_State *L, struct luasocket *S) {
int error;

Expand Down