diff --git a/.gitignore b/.gitignore index e28e4f8..a0d4874 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /tnfsd /tnfsd.exe bin/tnfsd.exe +/data diff --git a/src/Makefile b/src/Makefile index a80d787..48c54cd 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 diff --git a/src/config.h b/src/config.h index 0cecdbe..a91108f 100644 --- a/src/config.h +++ b/src/config.h @@ -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. * @@ -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 diff --git a/src/datagram.c b/src/datagram.c index 29c892c..342b8d1 100644 --- a/src/datagram.c +++ b/src/datagram.c @@ -36,6 +36,7 @@ TNFS daemon datagram handler #ifdef UNIX #include #include +#include #endif #ifdef WIN32 @@ -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 diff --git a/src/session.c b/src/session.c index 641ea71..bc18ae4 100644 --- a/src/session.c +++ b/src/session.c @@ -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; } } @@ -372,4 +373,4 @@ uint16_t tnfs_session_count() } } return count; -} \ No newline at end of file +}