Skip to content

Commit

Permalink
setsockopt to disable sigpipe
Browse files Browse the repository at this point in the history
this signal will crash app when network goes wrong

Signed-off-by: Janboe Ye <[email protected]>
  • Loading branch information
janboeye authored and tguillem committed Feb 28, 2020
1 parent a056a57 commit 685fb21
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/netbios_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,19 @@
#include "netbios_session.h"
#include "netbios_utils.h"

#if !defined(MSG_NOSIGNAL)
# define MSG_NOSIGNAL 0
#endif

static int open_socket_and_connect(netbios_session *s)
{
if ((s->socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
goto error;
#ifdef SO_NOSIGPIPE
//Never generate SIGPIPE on broken write
if (setsockopt(s->socket, SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof(int)))
goto error;
#endif
if (connect(s->socket, (struct sockaddr *)&s->remote_addr, sizeof(s->remote_addr)) <0)
goto error;

Expand Down Expand Up @@ -229,7 +238,7 @@ int netbios_session_packet_send(netbios_session *s)

s->packet->length = htons(s->packet_cursor);
to_send = sizeof(netbios_session_packet) + s->packet_cursor;
sent = send(s->socket, (void *)s->packet, to_send, 0);
sent = send(s->socket, (void *)s->packet, to_send, MSG_NOSIGNAL);

if (sent != to_send)
{
Expand Down

0 comments on commit 685fb21

Please sign in to comment.