From f866af912dd5a28206343c5eb092bcb9f9a336d2 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sat, 12 Jan 2008 22:10:53 +0000 Subject: Eric Landes provided the patch (edited by me) that introduces the --keepalive-time to curl to set the keepalive probe interval. I also took the opportunity to rename the recently added no-keep-alive option to no-keepalive to keep a consistent naming and to avoid getting two dashes in these option names. Eric also provided an update to the man page for the new option. --- src/main.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 7801c345f..55a75bb3f 100644 --- a/src/main.c +++ b/src/main.c @@ -104,6 +104,13 @@ #endif #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */ +#ifdef HAVE_NETINET_IN_H +#include /* for IPPROTO_TCP */ +#endif +#ifdef HAVE_NETINET_TCP_H +#include /* for TCP_KEEPIDLE, TCP_KEEPINTVL */ +#endif + /* The last #include file should be: */ #ifdef CURLDEBUG #ifndef CURLTOOLDEBUG @@ -486,7 +493,9 @@ struct Configurable { char *libcurl; /* output libcurl code to this file name */ bool raw; bool post301; - bool nokeepalive; + bool nokeepalive; /* for keepalive needs */ + long alivetime; + struct OutStruct *outs; }; @@ -679,6 +688,7 @@ static void help(void) " -I/--head Show document info only", " -j/--junk-session-cookies Ignore session cookies read from file (H)", " --interface Specify network interface/address to use", + " --keepalive-time Interval between keepalive probes", " --krb Enable kerberos with specified security level (F)", " -k/--insecure Allow connections to SSL sites without certs (H)", " -K/--config Specify which config file to read", @@ -697,7 +707,7 @@ static void help(void) " --netrc-optional Use either .netrc or URL; overrides -n", " --ntlm Use HTTP NTLM authentication (H)", " -N/--no-buffer Disable buffering of the output stream", - " --no-keep-alive Disable keep-alive use on the connection", + " --no-keepalive Disable keepalive use on the connection", " --no-sessionid Disable SSL session-ID reusing (SSL)", " -o/--output Write output to instead of stdout", " -O/--remote-name Write output to a file named as the remote file", @@ -1444,11 +1454,13 @@ static int ftpcccmethod(struct Configurable *config, char *str) } -static int set_so_keepalive(void *clientp, curl_socket_t curlfd, - curlsocktype purpose) +static int sockoptcallback(void *clientp, curl_socket_t curlfd, + curlsocktype purpose) { struct Configurable *config = (struct Configurable *)clientp; - int onoff = config->nokeepalive ? 0 : 1; + int onoff = 1; /* this callback is only used if we ask for keepalives on the + connection */ + long keepidle = config->alivetime; switch (purpose) { case CURLSOCKTYPE_IPCXN: @@ -1459,6 +1471,28 @@ static int set_so_keepalive(void *clientp, curl_socket_t curlfd, warnf(clientp, "Could not set SO_KEEPALIVE!\n"); return 0; } + else { + if (config->alivetime) { +#ifdef TCP_KEEPIDLE + if(setsockopt(curlfd, IPPROTO_TCP, TCP_KEEPIDLE, (void *)&keepidle, + sizeof(keepidle)) < 0) { + /* don't abort operation, just issue a warning */ + SET_SOCKERRNO(0); + warnf(clientp, "Could not set TCP_KEEPIDLE!\n"); + return 0; + } +#endif +#ifdef TCP_KEEPINTVL + if(setsockopt(curlfd, IPPROTO_TCP, TCP_KEEPINTVL, (void *)&keepidle, + sizeof(keepidle)) < 0) { + /* don't abort operation, just issue a warning */ + SET_SOCKERRNO(0); + warnf(clientp, "Could not set TCP_KEEPINTVL!\n"); + return 0; + } +#endif + } + } break; default: break; @@ -1555,8 +1589,9 @@ static ParameterError getparameter(char *flag, /* f or -long-flag */ {"$z", "libcurl", TRUE}, {"$#", "raw", FALSE}, {"$0", "post301", FALSE}, - {"$1", "no-keep-alive", FALSE}, - {"$2", "socks5-hostname", TRUE}, + {"$1", "no-keepalive", FALSE}, + {"$2", "socks5-hostname", TRUE}, + {"$3", "keepalive-time", TRUE}, {"0", "http1.0", FALSE}, {"1", "tlsv1", FALSE}, @@ -2024,9 +2059,13 @@ static ParameterError getparameter(char *flag, /* f or -long-flag */ case '0': /* --post301 */ config->post301 ^= TRUE; break; - case '1': /* --no-keep-alive */ + case '1': /* --no-keepalive */ config->nokeepalive ^= TRUE; break; + case '3': /* --keepalive-time */ + if(str2num(&config->alivetime, nextarg)) + return PARAM_BAD_NUMERIC; + break; } break; case '#': /* --progress-bar */ @@ -4590,7 +4629,7 @@ operate(struct Configurable *config, int argc, argv_item_t argv[]) /* curl 7.17.1 */ my_setopt(curl, CURLOPT_POST301, config->post301); if (!config->nokeepalive) { - my_setopt(curl, CURLOPT_SOCKOPTFUNCTION, set_so_keepalive); + my_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockoptcallback); my_setopt(curl, CURLOPT_SOCKOPTDATA, config); } -- cgit v1.2.3