cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Fwd: [PATCH] check ip callback

From: Alexey Pesternikov <paster_at_page2rss.com>
Date: Thu, 20 Sep 2007 10:44:33 -0700

On 9/20/07, Daniel Stenberg <daniel_at_haxx.se> wrote:
>
>
> >> + int* ai_family,
> >> + int* ai_socktype,
> >> + int* ai_protocol,
> >> + struct sockaddr *ai_addr,
> >> + socklen_t* ai_addrlen);
> >
> > Rather than passing these parameters individually, why not pass a
> pointer to
> > a Curl_addrinfo? It would also need to be made public and therefore
> placed
> > into curl/curl.h, which might be a bit tricky. In most cases it will
> end up
> > being a typedef of the system's struct addrinfo, though.
>
> I would prefer a struct pointer to prevent the number of arguments to be
> ridiculously many.

Agreed.

I think I would prefer a separate new struct, as then it could also feature
> a
> field or a bitmask for what struct members that have been modified and
> should
> be used. In a spirit similar to this:
>
> struct Curl_socketinfo {
> int* si_family;
> int* si_socktype;
> int* si_protocol;
> struct sockaddr *si_addr;
> socklen_t* si_addrlen;
> int si_setmask; /* bits as defined: */
> #define CURL_SI_FAMILY (1<<0)
> #define CURL_SI_SOCKTYPE (1<<1)
> /* etc */
> };

Two corrections. First, si_family etc. should not be a pointers. Second, it
would be better to use sockaddr_storage unless we want to deal with memory
ownership.That make it looks like this:
struct Curl_socketinfo {
   int si_family;
   int si_socktype;
   int si_protocol;
   socklen_t si_addrlen;
   struct sockaddr si_addr;
};
make sure si_addr is the last one and allocate some more space:
struct Curl_socketinfo_storage {
   int si_family;
   int si_socktype;
   int si_protocol;
   socklen_t si_addrlen;
   struct sockaddr_storage si_addr_storage;
};

   typedef int (*curl_sockopt_callback)(void *clientp,
> curl_socket_t curlfd,
> curlsocktype purpose,
> struct Curl_socketinfo *info);
>
> And then if you in the callback _only_ changes the family of the socket,
> it
> would look like this:
>
> int curl_sockopt_callback(void *clientp,
> curl_socket_t curlfd,
> curlsocktype purpose,
> struct Curl_socketinfo *info)
> {
> info->si_family = MY_PREFERRED_FAMILY;
> info->si_setmask = CURL_SI_FAMILY; /* family is modified */
>
> return 0;
> }

I think it would make the API more complicated and error-prone. It would not
take too much resources to save the old structure and compare 3 integers
after return.
Anyway most of the time the family will not be provided by callback directly
but obtained from resolver instead, so callback will have to detect the
changes by itself and set the flags.

-- 
Alexey,
http://page2rss.com      RSS feed from almost any page
Received on 2007-09-20