libkeepalive-0.3/0000755000175000017500000000000013010322161012717 5ustar fabiofabiolibkeepalive-0.3/ChangeLog0000644000175000017500000000034113010322001014460 0ustar fabiofabiolibkeepalive-0.3: - support for IPv6 - code cleanup and fix - license changed to MIT libkeepalive-0.2: - support for runtime tuning using environment variables libkeepalive-0.1: - first version of libkeepalive libkeepalive-0.3/LICENSE0000644000175000017500000000211513010321755013733 0ustar fabiofabioMIT License Copyright (c) 2005-2016 Fabio Busatto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. libkeepalive-0.3/AUTHORS0000644000175000017500000000005013007476051014000 0ustar fabiofabioFabio Busatto libkeepalive-0.3/test/0000755000175000017500000000000013010322147013702 5ustar fabiofabiolibkeepalive-0.3/test/test.c0000644000175000017500000000530613010321571015031 0ustar fabiofabio/* _ _ _ _ _ _ | (_) |__ | | _____ ___ _ __ __ _| (_)_ _____ | | | '_ \| |/ / _ \/ _ \ '_ \ / _` | | \ \ / / _ \ | | | |_) | < __/ __/ |_) | (_| | | |\ V / __/ |_|_|_.__/|_|\_\___|\___| .__/ \__,_|_|_| \_/ \___| |_| MIT License Copyright (c) 2005-2016 Fabio Busatto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define _GNU_SOURCE #include #include #include #include #include #include #include int main(void); int main() { int s; int optval; socklen_t optlen = sizeof(optval); if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("socket()"); exit(EXIT_FAILURE); } if(getsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) { perror("getsockopt()"); close(s); exit(EXIT_FAILURE); } printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF")); if(optval) { #ifdef TCP_KEEPCNT if(getsockopt(s, SOL_TCP, TCP_KEEPCNT, &optval, &optlen) < 0) { perror("getsockopt()"); close(s); exit(EXIT_FAILURE); } printf("TCP_KEEPCNT = %d\n", optval); #endif #ifdef TCP_KEEPIDLE if(getsockopt(s, SOL_TCP, TCP_KEEPIDLE, &optval, &optlen) < 0) { perror("getsockopt()"); close(s); exit(EXIT_FAILURE); } printf("TCP_KEEPIDLE = %d\n", optval); #endif #ifdef TCP_KEEPINTVL if(getsockopt(s, SOL_TCP, TCP_KEEPINTVL, &optval, &optlen) < 0) { perror("getsockopt()"); close(s); exit(EXIT_FAILURE); } printf("TCP_KEEPINTVL = %d\n", optval); #endif } close(s); exit(EXIT_SUCCESS); } libkeepalive-0.3/test/Makefile0000644000175000017500000000060213007476243015355 0ustar fabiofabio# _ _ _ _ _ _ # | (_) |__ | | _____ ___ _ __ __ _| (_)_ _____ # | | | '_ \| |/ / _ \/ _ \ '_ \ / _` | | \ \ / / _ \ # | | | |_) | < __/ __/ |_) | (_| | | |\ V / __/ # |_|_|_.__/|_|\_\___|\___| .__/ \__,_|_|_| \_/ \___| # |_| # # (C) Fabio Busatto CC=gcc default: test clean: rm -f test libkeepalive-0.3/src/0000755000175000017500000000000013010322147013512 5ustar fabiofabiolibkeepalive-0.3/src/libkeepalive.c0000644000175000017500000000647313010321612016320 0ustar fabiofabio/* _ _ _ _ _ _ | (_) |__ | | _____ ___ _ __ __ _| (_)_ _____ | | | '_ \| |/ / _ \/ _ \ '_ \ / _` | | \ \ / / _ \ | | | |_) | < __/ __/ |_) | (_| | | |\ V / __/ |_|_|_.__/|_|\_\___|\___| .__/ \__,_|_|_| \_/ \___| |_| MIT License Copyright (c) 2005-2016 Fabio Busatto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define _GNU_SOURCE #include #include #include #include #include #include #include int socket(int domain, int type, int protocol); int socket(int domain, int type, int protocol) { static int (*orig_socket)(int, int, int) = NULL; int sockfd = -1, optval = 1; char *env; do { /* if the original function is NULL, try to resolve it or break */ if(!orig_socket && !(*(void **)(&orig_socket) = dlsym(RTLD_NEXT, "socket"))) { errno = EACCES; break; } /* call original function with parameters */ if((sockfd = (*orig_socket)(domain, type, protocol)) == -1) break; /* socket must be IPv4 or IPv6 */ if((domain != AF_INET) && (domain != AF_INET6)) break; /* socket must be TCP */ if(!(type & SOCK_STREAM)) break; /* if environment variable KEEPALIVE is set to "off", break */ if((env = getenv("KEEPALIVE")) && !strcasecmp(env, "off")) break; /* if setting keepalive fails, break */ if(setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) break; #ifdef TCP_KEEPCNT /* if environment variable KEEPCNT is set, override the default option value */ if((env = getenv("KEEPCNT"))) { optval = atoi(env); setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, &optval, sizeof(optval)); } #endif #ifdef TCP_KEEPIDLE /* if environment variable KEEPIDLE is set, override the default option value */ if((env = getenv("KEEPIDLE"))) { optval = atoi(env); setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval)); } #endif #ifdef TCP_KEEPINTVL /* if environment variable KEEPINTVL is set, override the default option value */ if((env = getenv("KEEPINTVL"))) { optval = atoi(env); setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval)); } #endif } while(0); return sockfd; } libkeepalive-0.3/src/Makefile0000644000175000017500000000103413007476232015163 0ustar fabiofabio# _ _ _ _ _ _ # | (_) |__ | | _____ ___ _ __ __ _| (_)_ _____ # | | | '_ \| |/ / _ \/ _ \ '_ \ / _` | | \ \ / / _ \ # | | | |_) | < __/ __/ |_) | (_| | | |\ V / __/ # |_|_|_.__/|_|\_\___|\___| .__/ \__,_|_|_| \_/ \___| # |_| # # (C) Fabio Busatto CC=gcc CFLAGS=-fPIC -ansi -pedantic -Wall LDFLAGS=-shared -Wl,-soname,libkeepalive.so LDLIBS=-ldl default: libkeepalive.so %.so: %.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) clean: rm -f *.so *.o libkeepalive-0.3/FEEDBACK0000644000175000017500000000061513007476124014007 0ustar fabiofabioIf you downloaded libkeepalive and are interested to tell me something about, please send me an email at fabio.busatto@gmail.com. If you've successfully used libkeepalive on your system, write me what your os is and what you did to make it work. If you are unsure if you've to mail me or not, don't be afraid: mail me. I'm happy to receive real mails sometimes, that's better than reading spam. libkeepalive-0.3/Makefile0000644000175000017500000000101013007476157014374 0ustar fabiofabio# _ _ _ _ _ _ # | (_) |__ | | _____ ___ _ __ __ _| (_)_ _____ # | | | '_ \| |/ / _ \/ _ \ '_ \ / _` | | \ \ / / _ \ # | | | |_) | < __/ __/ |_) | (_| | | |\ V / __/ # |_|_|_.__/|_|\_\___|\___| .__/ \__,_|_|_| \_/ \___| # |_| # # (C) Fabio Busatto default: make -C src/ make -C test/ cp src/libkeepalive.so libkeepalive.so strip -s libkeepalive.so clean: make -C src/ clean make -C test/ clean rm -f libkeepalive.so libkeepalive-0.3/README0000600000175000017500000000644613007476202013615 0ustar fabiofabio _ _ _ _ _ _ | (_) |__ | | _____ ___ _ __ __ _| (_)_ _____ | | | '_ \| |/ / _ \/ _ \ '_ \ / _` | | \ \ / / _ \ | | | |_) | < __/ __/ |_) | (_| | | |\ V / __/ |_|_|_.__/|_|\_\___|\___| .__/ \__,_|_|_| \_/ \___| |_| (C) Fabio Busatto 1. OVERVIEW Many systems provide the ability to keep tcp connections alive, so they aren't reset by peers or by routers because of inactivity. Even if this feature is present, only a few programs correctly implement the code to use it. If your favourite foo-client is not written with support for tcp keepalive, you'll continue to see your connection reset. libkeepalive library provides a way to enable tcp keepalive support in any program that uses shared libraries (e.g.: glibc shared object) to perform network operations. Using the preload method, you will be able to intercept normal program execution and to inject the code needed to enable the keepalive routines, everything done without modifying the original binary file and with no need to gain root privileges. 2. OBTAINING SOFTWARE Latest version of libkeepalive is available on http://libkeepalive.sourceforge.net/#download. 3. BUILDING BINARIES Just run make (use gmake if your default make is another one). 4. INSTALLING Simply copy libkeepalive.so in some cool directory (/usr/lib, or ~/lib if you're not so elite to have root privileges). If you want to enable keepalive in suid programs too, you have to copy libkeepalive.so in /lib or /usr/lib and suid it (according to ld.so manpage). In any other scenarios the library will not work with suid binaries. 5. USER CONFIGURATION libkeepalive will do its job if you set the environment variable LD_PRELOAD to the path of libkeepalive.so shared library before executing the target program. You must specify the full path of the library if ld.so cannot is not able to find it. You can also set the variable in your startup script (i.e.: ~/.profile), depending from your shell. For informations about setting environment variables, refer to your shell documentation. 6. SYSTEM-WIDE CONFIGURATION Host administrators may want to impose the use of libkeepalive to all their users and all daemons loaded by the system startup process. Adding the full path of the libkeepalive.so dynamic library to /etc/ld.so.preload you obtain the same result as the LD_PRELOAD variable is always set, forcing the use indipendently from the environment. 7. DISABLING FOR A SPECIFIC EXECUTION If you need to disable the action of libkeepalive for a single job, or if your admin set the ld.so.preload, you can set the KEEPALIVE environment variable to "off", inhibiting the library even if it will be loaded. 8. TUNING KEEPALIVE PARAMETERS You can change the keepalive parameters using environment variables or via sysctl/procfs. env sysctl KEEPCNT <=> net.ipv4.tcp_keepalive_probes KEEPIDLE <=> net.ipv4.tcp_keepalive_time KEEPINTVL <=> net.ipv4.tcp_keepalive_intvl DON'T FORGET to configure your system-specific parameters for keepalive! Many times, defaults are not what you need and you have to change your settings (see your system documentation for this). libkeepalive-0.3/VERSION0000644000175000017500000000000413007475645014007 0ustar fabiofabio0.3