From bafb2d24704700b70ec981ab0de8711b705d331c Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Thu, 8 Dec 2016 21:22:55 +0100 Subject: [PATCH] Moved set_sock_timeout to own file --- Makefile | 1 + src/httplib_set_sock_timeout.c | 75 ++++++++++++++++++++++++++++++++++ src/libhttp.c | 40 ------------------ 3 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 src/httplib_set_sock_timeout.c diff --git a/Makefile b/Makefile index d1dedbf8..0f5e51d4 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_master_thread.c \ src/httplib_process_new_connection.c \ src/httplib_produce_socket.c \ + src/httplib_set_sock_timeout.c \ src/httplib_set_tcp_nodelay.c \ src/httplib_start.c \ src/httplib_stop.c \ diff --git a/src/httplib_set_sock_timeout.c b/src/httplib_set_sock_timeout.c new file mode 100644 index 00000000..811881d2 --- /dev/null +++ b/src/httplib_set_sock_timeout.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * 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. + */ + + + +#include "libhttp-private.h" + + + +/* + * int XX_httplib_set_sock_timeout( SOCKET sock, int milliseconds ); + * + * The function XX_httplib_set_sock_timeout() sets the timeout of a socket + * to the specified nummer of milliseconds. + */ + +int XX_httplib_set_sock_timeout( SOCKET sock, int milliseconds ) { + + int r0 = 0, r1, r2; + +#ifdef _WIN32 + /* Windows specific */ + + DWORD tv = (DWORD)milliseconds; + +#else + /* Linux, ... (not Windows) */ + + struct timeval tv; + +/* TCP_USER_TIMEOUT/RFC5482 (http://tools.ietf.org/html/rfc5482): + * max. time waiting for the acknowledged of TCP data before the connection + * will be forcefully closed and ETIMEDOUT is returned to the application. + * If this option is not set, the default timeout of 20-30 minutes is used. +*/ +/* #define TCP_USER_TIMEOUT (18) */ + +#if defined(TCP_USER_TIMEOUT) + unsigned int uto = (unsigned int)milliseconds; + r0 = setsockopt(sock, 6, TCP_USER_TIMEOUT, (const void *)&uto, sizeof(uto)); +#endif + + memset(&tv, 0, sizeof(tv)); + tv.tv_sec = milliseconds / 1000; + tv.tv_usec = (milliseconds * 1000) % 1000000; + +#endif /* _WIN32 */ + + r1 = setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, (SOCK_OPT_TYPE)&tv, sizeof(tv)); + r2 = setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, (SOCK_OPT_TYPE)&tv, sizeof(tv)); + + return r0 || r1 || r2; + +} /* XX_httplib_set_sock_timeout */ diff --git a/src/libhttp.c b/src/libhttp.c index 60efb11e..c0a1b99b 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -9001,43 +9001,3 @@ void XX_httplib_reset_per_request_attributes( struct mg_connection *conn ) { conn->internal_error = 0; } /* XX_httplib_reset_per_request_attributes */ - - -int XX_httplib_set_sock_timeout( SOCKET sock, int milliseconds ) { - - int r0 = 0, r1, r2; - -#ifdef _WIN32 - /* Windows specific */ - - DWORD tv = (DWORD)milliseconds; - -#else - /* Linux, ... (not Windows) */ - - struct timeval tv; - -/* TCP_USER_TIMEOUT/RFC5482 (http://tools.ietf.org/html/rfc5482): - * max. time waiting for the acknowledged of TCP data before the connection - * will be forcefully closed and ETIMEDOUT is returned to the application. - * If this option is not set, the default timeout of 20-30 minutes is used. -*/ -/* #define TCP_USER_TIMEOUT (18) */ - -#if defined(TCP_USER_TIMEOUT) - unsigned int uto = (unsigned int)milliseconds; - r0 = setsockopt(sock, 6, TCP_USER_TIMEOUT, (const void *)&uto, sizeof(uto)); -#endif - - memset(&tv, 0, sizeof(tv)); - tv.tv_sec = milliseconds / 1000; - tv.tv_usec = (milliseconds * 1000) % 1000000; - -#endif /* _WIN32 */ - - r1 = setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, (SOCK_OPT_TYPE)&tv, sizeof(tv)); - r2 = setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, (SOCK_OPT_TYPE)&tv, sizeof(tv)); - - return r0 || r1 || r2; - -} /* XX_httplib_set_sock_timeout */