1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Address #463 - prevent sigpipe on client connections.

We erroneously did not set SO_NOSIGPIPE on connections aquired with
net/connect, only those quired thorugh net/server. This meant that
failed writes by a client could send sigpipe.
This commit is contained in:
Calvin Rose 2020-08-10 18:45:44 -05:00
parent 6f2f3fdb68
commit ca75f8dc20

View File

@ -497,6 +497,18 @@ static struct addrinfo *janet_get_addrinfo(Janet *argv, int32_t offset) {
* C Funs * C Funs
*/ */
static void nosigpipe(JSock s) {
#ifdef SO_NOSIGPIPE
int enable = 1;
if (setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int)) < 0) {
JSOCKCLOSE(s);
janet_panic("setsockopt(SO_NOSIGPIPE) failed");
}
#else
(void) s;
#endif
}
static Janet cfun_net_connect(int32_t argc, Janet *argv) { static Janet cfun_net_connect(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2); janet_fixarity(argc, 2);
@ -517,6 +529,8 @@ static Janet cfun_net_connect(int32_t argc, Janet *argv) {
janet_panic("could not connect to socket"); janet_panic("could not connect to socket");
} }
nosigpipe(sock);
/* Wrap socket in abstract type JanetStream */ /* Wrap socket in abstract type JanetStream */
JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE); JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
return janet_wrap_abstract(stream); return janet_wrap_abstract(stream);
@ -542,12 +556,7 @@ static Janet cfun_net_server(int32_t argc, Janet *argv) {
JSOCKCLOSE(sfd); JSOCKCLOSE(sfd);
janet_panic("setsockopt(SO_REUSEADDR) failed"); janet_panic("setsockopt(SO_REUSEADDR) failed");
} }
#ifdef SO_NOSIGPIPE nosigpipe(sfd);
if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int)) < 0) {
JSOCKCLOSE(sfd);
janet_panic("setsockopt(SO_NOSIGPIPE) failed");
}
#endif
#ifdef SO_REUSEPORT #ifdef SO_REUSEPORT
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) { if (setsockopt(sfd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) {
JSOCKCLOSE(sfd); JSOCKCLOSE(sfd);