From 5acadc9cd7a1ff40ffa8d57214c90d8c788b2b03 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 29 Aug 2006 14:39:33 +0000 Subject: [PATCH] David McCreedy added CURLOPT_SOCKOPTFUNCTION and CURLOPT_SOCKOPTDATA to allow applications to set their own socket options. --- CHANGES | 4 +++ RELEASE-NOTES | 5 ++-- docs/libcurl/curl_easy_setopt.3 | 18 ++++++++++- include/curl/curl.h | 53 +++++++++++++++++++++++++++++++++ include/curl/multi.h | 40 ------------------------- lib/connect.c | 11 +++++++ lib/setup.h | 13 -------- lib/url.c | 14 +++++++++ lib/urldata.h | 2 ++ 9 files changed, 104 insertions(+), 56 deletions(-) diff --git a/CHANGES b/CHANGES index ab0a8fd3e..f58c00ae7 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,10 @@ Changelog +Daniel (29 August 2006) +- David McCreedy added CURLOPT_SOCKOPTFUNCTION and CURLOPT_SOCKOPTDATA to + allow applications to set their own socket options. + Daniel (25 August 2006) - Armel Asselin reported that the 'running_handles' counter wasn't updated properly if you removed a "live" handle from a multi handle with diff --git a/RELEASE-NOTES b/RELEASE-NOTES index cc014a2b3..70689cee6 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -3,7 +3,7 @@ Curl and libcurl 7.15.6 Public curl release number: 96 Releases counted from the very beginning: 123 Available command line options: 114 - Available curl_easy_setopt() options: 133 + Available curl_easy_setopt() options: 135 Number of public functions in libcurl: 54 Amount of public web site mirrors: 33 Number of known libcurl bindings: 32 @@ -11,12 +11,13 @@ Curl and libcurl 7.15.6 This release includes the following changes: + o CURLOPT_SOCKOPTFUNCTION and CURLOPT_SOCKOPTDATA were added o (FTP) libcurl avoids sending TYPE if the desired type was already set o (FTP) CURLOPT_PREQUOTE works even when CURLOPT_NOBODY is set true This release includes the following bugfixes: - o running_handles' counter wasn't always updated properly when + o the 'running_handles' counter wasn't always updated properly when curl_multi_remove_handle() was used o (FTP) EPRT transfers with IPv6 didn't work properly o (FTP) SINGLECWD mode and using files in the root dir diff --git a/docs/libcurl/curl_easy_setopt.3 b/docs/libcurl/curl_easy_setopt.3 index 0fce61a98..98abd417d 100644 --- a/docs/libcurl/curl_easy_setopt.3 +++ b/docs/libcurl/curl_easy_setopt.3 @@ -160,12 +160,28 @@ found in \fI\fP. This function gets called by libcurl when something special I/O-related needs to be done that the library can't do by itself. For now, rewinding the read data stream is the only action it can request. The rewinding of the read data stream may be necessary when doing a -HTTP PUT or POST with a multi-pass authentication method. (Opion added in +HTTP PUT or POST with a multi-pass authentication method. (Option added in 7.12.3) .IP CURLOPT_IOCTLDATA Pass a pointer that will be untouched by libcurl and passed as the 3rd argument in the ioctl callback set with \fICURLOPT_IOCTLFUNCTION\fP. (Option added in 7.12.3) +.IP CURLOPT_SOCKOPTFUNCTION +Function pointer that should match the \fIcurl_sockopt_callback\fP prototype +found in \fI\fP. This function gets called by libcurl after the +socket() call but before the connect() call. The callback's \fIpurpose\fP +argument identifies the exact purpose for this particular socket, and +currently only one value is supported: \fICURLSOCKTYPE_IPCXN\fP for the +primary connection (meaning the control connection in the FTP case). Future +versions of libcurl may support more purposes. It passes the newly created +socket descriptor so additional setsockopt() calls can be done at the user's +discretion. A non-zero return code from the callback function will signal an +unrecoverable error to the library and it will close the socket and return +\fICURLE_COULDNT_CONNECT\fP. (Option added in 7.15.6.) +.IP CURLOPT_SOCKOPTDATA +Pass a pointer that will be untouched by libcurl and passed as the first +argument in the sockopt callback set with \fICURLOPT_SOCKOPTFUNCTION\fP. +(Option added in 7.15.6.) .IP CURLOPT_PROGRESSFUNCTION Function pointer that should match the \fIcurl_progress_callback\fP prototype found in \fI\fP. This function gets called by libcurl instead of diff --git a/include/curl/curl.h b/include/curl/curl.h index f4166cb58..6f0be2c9f 100644 --- a/include/curl/curl.h +++ b/include/curl/curl.h @@ -133,6 +133,47 @@ extern "C" { #undef FILESIZEBITS #endif +#if defined(_WIN32) && !defined(WIN32) +/* Chris Lewis mentioned that he doesn't get WIN32 defined, only _WIN32 so we + make this adjustment to catch this. */ +#define WIN32 1 +#endif + +#if defined(WIN32) && !defined(_WIN32_WCE) && !defined(__GNUC__) && \ + !defined(__CYGWIN__) || defined(__MINGW32__) +#if !(defined(_WINSOCKAPI_) || defined(_WINSOCK_H)) +/* The check above prevents the winsock2 inclusion if winsock.h already was + included, since they can't co-exist without problems */ +#include +#endif +#else + +/* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish + libc5-based Linux systems. Only include it on system that are known to + require it! */ +#if defined(_AIX) || defined(NETWARE) || defined(__NetBSD__) || defined(__minix) +#include +#endif + +#ifndef _WIN32_WCE +#include +#endif +#include +#include +#endif + +#ifndef curl_socket_typedef +/* socket typedef */ +#ifdef WIN32 +typedef SOCKET curl_socket_t; +#define CURL_SOCKET_BAD INVALID_SOCKET +#else +typedef int curl_socket_t; +#define CURL_SOCKET_BAD -1 +#endif +#define curl_socket_typedef +#endif /* curl_socket_typedef */ + struct curl_httppost { struct curl_httppost *next; /* next entry in the list */ char *name; /* pointer to allocated name */ @@ -184,6 +225,14 @@ typedef size_t (*curl_read_callback)(char *buffer, size_t nitems, void *instream); +typedef enum { + CURLSOCKTYPE_IPCXN, /* socket created for a specific IP connection */ + CURLSOCKTYPE_LAST /* never use */ +} curlsocktype; + +typedef int (*curl_sockopt_callback)(void *clientp, + curl_socket_t curlfd, + curlsocktype purpose); #ifndef CURL_NO_OLDIES /* not used since 7.10.8, will be removed in a future release */ @@ -982,6 +1031,10 @@ typedef enum { /* Pointer to command string to send if USER/PASS fails. */ CINIT(FTP_ALTERNATIVE_TO_USER, OBJECTPOINT, 147), + /* callback function for setting socket options */ + CINIT(SOCKOPTFUNCTION, FUNCTIONPOINT, 148), + CINIT(SOCKOPTDATA, OBJECTPOINT, 149), + CURLOPT_LASTENTRY /* the last unused */ } CURLoption; diff --git a/include/curl/multi.h b/include/curl/multi.h index 6811ce798..1beda8a92 100644 --- a/include/curl/multi.h +++ b/include/curl/multi.h @@ -37,34 +37,6 @@ file descriptors simultaneous easily. */ -#if defined(_WIN32) && !defined(WIN32) -/* Chris Lewis mentioned that he doesn't get WIN32 defined, only _WIN32 so we - make this adjustment to catch this. */ -#define WIN32 1 -#endif - -#if defined(WIN32) && !defined(_WIN32_WCE) && !defined(__GNUC__) && \ - !defined(__CYGWIN__) || defined(__MINGW32__) -#if !(defined(_WINSOCKAPI_) || defined(_WINSOCK_H)) -/* The check above prevents the winsock2 inclusion if winsock.h already was - included, since they can't co-exist without problems */ -#include -#endif -#else - -/* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish - libc5-based Linux systems. Only include it on system that are known to - require it! */ -#if defined(_AIX) || defined(NETWARE) || defined(__NetBSD__) || defined(__minix) -#include -#endif - -#ifndef _WIN32_WCE -#include -#endif -#include -#include -#endif /* * This header file should not really need to include "curl.h" since curl.h @@ -83,18 +55,6 @@ extern "C" { typedef void CURLM; -#ifndef curl_socket_typedef -/* Public socket typedef */ -#ifdef WIN32 -typedef SOCKET curl_socket_t; -#define CURL_SOCKET_BAD INVALID_SOCKET -#else -typedef int curl_socket_t; -#define CURL_SOCKET_BAD -1 -#endif -#define curl_socket_typedef -#endif /* curl_socket_typedef */ - typedef enum { CURLM_CALL_MULTI_PERFORM = -1, /* please call curl_multi_perform() or curl_multi_socket*() soon */ diff --git a/lib/connect.c b/lib/connect.c index 95fdf8119..4e7f4f8ea 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -702,6 +702,17 @@ singleipconnect(struct connectdata *conn, nosigpipe(conn, sockfd); + if(data->set.fsockopt) { + /* activate callback for setting socket options */ + error = data->set.fsockopt(data->set.sockopt_client, + sockfd, + CURLSOCKTYPE_IPCXN); + if (error) { + sclose(sockfd); /* close the socket and bail out */ + return CURL_SOCKET_BAD; + } + } + /* possibly bind the local end to an IP, interface or port */ res = bindlocal(conn, sockfd); if(res) { diff --git a/lib/setup.h b/lib/setup.h index 3538256c4..1e64c928a 100644 --- a/lib/setup.h +++ b/lib/setup.h @@ -278,19 +278,6 @@ int fileno( FILE *stream); #endif /* WIN32 */ -#ifndef curl_socket_typedef -/* now typedef our socket type */ -#ifdef WIN32 -typedef SOCKET curl_socket_t; -#define CURL_SOCKET_BAD INVALID_SOCKET -#else -typedef int curl_socket_t; -#define CURL_SOCKET_BAD -1 -#endif -#define curl_socket_typedef -#endif /* curl_socket_typedef */ - - #if defined(WIN32) && !defined(__CYGWIN__) && !defined(USE_ARES) && \ !defined(__LCC__) /* lcc-win32 doesn't have _beginthreadex() */ #ifdef ENABLE_IPV6 diff --git a/lib/url.c b/lib/url.c index c0641c78b..17f543615 100644 --- a/lib/url.c +++ b/lib/url.c @@ -1551,6 +1551,20 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, data->set.ftp_alternative_to_user = va_arg(param, char *); break; + case CURLOPT_SOCKOPTFUNCTION: + /* + * socket callback function: called after socket() but before connect() + */ + data->set.fsockopt = va_arg(param, curl_sockopt_callback); + break; + + case CURLOPT_SOCKOPTDATA: + /* + * socket callback data pointer. Might be NULL. + */ + data->set.sockopt_client = va_arg(param, void *); + break; + default: /* unknown tag and its companion, just ignore: */ result = CURLE_FAILED_INIT; /* correct this */ diff --git a/lib/urldata.h b/lib/urldata.h index 33f7ee860..5acac2c53 100644 --- a/lib/urldata.h +++ b/lib/urldata.h @@ -1029,6 +1029,8 @@ struct UserDefined { curl_progress_callback fprogress; /* function for progress information */ curl_debug_callback fdebug; /* function that write informational data */ curl_ioctl_callback ioctl; /* function for I/O control */ + curl_sockopt_callback fsockopt; /* function for setting socket options */ + void *sockopt_client; /* pointer to pass to the socket options callback */ /* the 3 curl_conv_callback functions below are used on non-ASCII hosts */ /* function to convert from the network encoding: */