videolabs / libdsm

Defective SMb: A minimalist implementation of a client library for SMBv1 using Plain'Ol C

Home Page:http://videolabs.github.io/libdsm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dsm raises SIGPIPE after network disconnect

janboeye opened this issue · comments

open_socket_and_connect does not disable sigpipe, and when network disconnect, this will raise sigpipe to crash the app. Please review if my patch attached here is a good fix.

Thanks

From b53b1242323c715cee018ac189b9863eccd95240 Mon Sep 17 00:00:00 2001
From: Janboe Ye <janboe.ye@gmail.com>
Date: Fri, 21 Feb 2020 15:14:32 +0800
Subject: [PATCH] setsockopt to disable sigpipe

this signal will crash app when network goes wrong

Signed-off-by: Janboe Ye <janboe.ye@gmail.com>
---
 src/netbios_session.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/netbios_session.c b/src/netbios_session.c
index a77cbc7..7ae4dd0 100644
--- a/src/netbios_session.c
+++ b/src/netbios_session.c
@@ -54,8 +54,15 @@
 
 static int open_socket_and_connect(netbios_session *s)
 {
+    int optval = 0;
     if ((s->socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
         goto error;
+
+    //Never generate SIGPIPE on broken write
+    optval = 1;
+    if (setsockopt(s->socket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&optval, sizeof(int)))
+        goto error;
+
     if (connect(s->socket, (struct sockaddr *)&s->remote_addr, sizeof(s->remote_addr)) <0)
         goto error;
 
-- 
2.23.0