Skip to content

Commit

Permalink
Merge pull request #9 from gtortone/master
Browse files Browse the repository at this point in the history
set TCP keepalive and remove related sessions when client disconnects
  • Loading branch information
tschak909 authored Aug 9, 2024
2 parents 6907e1f + 0b13a70 commit 02c5eda
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/tnfsd
/tnfsd.exe
bin/tnfsd.exe
/data
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ifeq ($(OS),Windows_NT)
EXEC = tnfsd.exe
endif
ifeq ($(OS),BSD)
FLAGS = -Wall -DUNIX -DENABLE_CHROOT -DNEED_ERRTABLE
FLAGS = -Wall -DUNIX -DENABLE_CHROOT -DNEED_ERRTABLE -DBSD
EXOBJS = event_kqueue.o
LIBS =
EXEC = tnfsd
Expand Down
6 changes: 4 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
Expand All @@ -42,5 +41,8 @@
#define MAX_FILENAME_LEN 256 /* longest filename supported */
#define MAX_IOSZ 512 /* maximum size of an IO operation */
#define STATS_INTERVAL 60 /* how often the server stats should be logged. 0 to disable stats logging. */
#define TCP_KA_IDLE 30 /* the time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes */
#define TCP_KA_INTVL 1 /* the time (in seconds) between individual keepalive probes */
#define TCP_KA_COUNT 60 /* the maximum number of keepalive probes TCP should send before dropping the connection */

#endif
32 changes: 32 additions & 0 deletions src/datagram.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TNFS daemon datagram handler
#ifdef UNIX
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif

#ifdef WIN32
Expand Down Expand Up @@ -158,6 +159,37 @@ void tnfs_sockinit(int port)
{
die("setsockopt(SO_REUSEADDR) failed");
}

#ifndef WIN32
/* enables sending of keep-alive messages */
int ka_enable = 1;
if (setsockopt(tcplistenfd, SOL_SOCKET, SO_KEEPALIVE, &ka_enable, sizeof(ka_enable)) < 0)
{
die("setsockopt(SO_KEEPALIVE) failed");
}
int ka_idle = TCP_KA_IDLE;
#ifdef BSD
if (setsockopt(tcplistenfd, IPPROTO_TCP, TCP_KEEPALIVE, &ka_idle, sizeof(ka_idle)) < 0)
#else
if (setsockopt(tcplistenfd, IPPROTO_TCP, TCP_KEEPIDLE, &ka_idle, sizeof(ka_idle)) < 0)
#endif
{
die("setsockopt(TCP_KEEPIDLE) failed");
}
/* the time (in seconds) between individual keepalive probes */
int ka_interval = TCP_KA_INTVL;
if (setsockopt(tcplistenfd, IPPROTO_TCP, TCP_KEEPINTVL, &ka_interval, sizeof(ka_interval)) < 0)
{
die("setsockopt(TCP_KEEPINTVL) failed");
}
/* the maximum number of keepalive probes TCP should send before dropping the connection */
int ka_count = TCP_KA_COUNT;
if (setsockopt(tcplistenfd, IPPROTO_TCP, TCP_KEEPCNT, &ka_count, sizeof(ka_count)) < 0)
{
die("setsockopt(TCP_KEEPCNT) failed");
}
#endif

#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ void tnfs_reset_cli_fd_in_sessions(int cli_fd)
if (s->cli_fd == cli_fd)
{
LOG("Removing TCP connection handle from session 0x%02x\n", s->sid);
tnfs_freesession(s, i);
s->cli_fd = 0;
}
}
Expand Down Expand Up @@ -372,4 +373,4 @@ uint16_t tnfs_session_count()
}
}
return count;
}
}

0 comments on commit 02c5eda

Please sign in to comment.