]> granicus.if.org Git - curl/commitdiff
- Added CURLINFO_PRIMARY_IP as a new information retrievable with
authorDaniel Stenberg <daniel@haxx.se>
Fri, 6 Jun 2008 17:33:35 +0000 (17:33 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 6 Jun 2008 17:33:35 +0000 (17:33 +0000)
  curl_easy_getinfo. It returns a pointer to a string with the most recently
  used IP address. Modified test case 500 to also verify this feature. The
  implementing of this feature was sponsored by Lenny Rachitsky at NeuStar.

CHANGES
RELEASE-NOTES
docs/libcurl/curl_easy_getinfo.3
include/curl/curl.h
lib/connect.c
lib/getinfo.c
lib/urldata.h
tests/data/test500
tests/libtest/lib500.c

diff --git a/CHANGES b/CHANGES
index d6ea8d6a67251d85f793d56a22f681ada491cf67..3726effe3280aa66e9bd1cb8bea190cc88a3a7b2 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,12 @@
                                   Changelog
 
 
+Daniel Stenberg (6 Jun 2008)
+- Added CURLINFO_PRIMARY_IP as a new information retrievable with
+  curl_easy_getinfo. It returns a pointer to a string with the most recently
+  used IP address. Modified test case 500 to also verify this feature. The
+  implementing of this feature was sponsored by Lenny Rachitsky at NeuStar.
+
 Version 7.18.2 (4 June 2008)
 
 Daniel Fandrich (3 Jun 2008)
index ed5b6d65522518826390cb91abec2d59cc1306d2..573acce9bab446f1c3b3b933ed0e5d426eff8a64 100644 (file)
@@ -10,7 +10,7 @@ Curl and libcurl 7.18.3
 
 This release includes the following changes:
  
- o 
+ o Added CURLINFO_PRIMARY_IP
 
 This release includes the following bugfixes:
 
@@ -31,6 +31,6 @@ New curl mirrors:
 This release would not have looked like this without help, code, reports and
 advice from friends like these:
 
-
+ Lenny Rachitsky
 
         Thanks! (and sorry if I forgot to mention someone)
index be0f060d47f0b996f676f7cdffebf25cb178ef3b..9837d417d4fb8893e77a278ad03116eb0222e6f0 100644 (file)
@@ -159,6 +159,12 @@ counted).  Combined with \fICURLINFO_REDIRECT_COUNT\fP you are able to know
 how many times libcurl successfully reused existing connection(s) or not.  See
 the Connection Options of \fIcurl_easy_setopt(3)\fP to see how libcurl tries
 to make persistent connections to save time.  (Added in 7.12.3)
+.IP CURLINFO_PRIMARY_IP
+Pass a pointer to a char pointer to receive the pointer to a zero-terminated
+string holding the IP address of the most recent connection done with this
+\fBcurl\fP handle. This string may be IPv6 if that's enabled. Note that you
+get a pointer to a memory area that will be re-used at next request so you
+need to copy the string if you want to keep the information. (Added in 7.18.3)
 .IP CURLINFO_COOKIELIST
 Pass a pointer to a 'struct curl_slist *' to receive a linked-list of all
 cookies cURL knows (expired ones, too). Don't forget to
index d87942376c0b7ddaf5ffcd67334f40b3d8a3d04d..b42f0b31b11637f03f316a35ed80a0027922d3e8 100644 (file)
@@ -1596,9 +1596,10 @@ typedef enum {
   CURLINFO_LASTSOCKET       = CURLINFO_LONG   + 29,
   CURLINFO_FTP_ENTRY_PATH   = CURLINFO_STRING + 30,
   CURLINFO_REDIRECT_URL     = CURLINFO_STRING + 31,
+  CURLINFO_PRIMARY_IP       = CURLINFO_STRING + 32,
   /* Fill in new entries below here! */
 
-  CURLINFO_LASTONE          = 31
+  CURLINFO_LASTONE          = 32
 } CURLINFO;
 
 /* CURLINFO_RESPONSE_CODE is the new name for the option previously known as
index b3928ef1ecff690671b258578064f82dfc496afe..e398f61fb6ed25f6d53c0bed3a9ae1d31a61ec92 100644 (file)
@@ -775,9 +775,12 @@ singleipconnect(struct connectdata *conn,
   /* FIXME: do we have Curl_printable_address-like with struct sockaddr* as
      argument? */
 #if defined(HAVE_SYS_UN_H) && defined(AF_UNIX)
-  if(addr->family==AF_UNIX)
+  if(addr->family==AF_UNIX) {
     infof(data, "  Trying %s... ",
           ((const struct sockaddr_un*)(&addr->addr))->sun_path);
+    snprintf(data->info.ip, MAX_IPADR_LEN, "%s",
+             ((const struct sockaddr_un*)(&addr->addr))->sun_path);
+  }
   else
 #endif
   {
@@ -789,8 +792,10 @@ singleipconnect(struct connectdata *conn,
       iptoprint = &((const struct sockaddr_in*)(&addr->addr))->sin_addr;
 
     if(Curl_inet_ntop(addr->family, iptoprint, addr_buf,
-                      sizeof(addr_buf)) != NULL)
+                      sizeof(addr_buf)) != NULL) {
       infof(data, "  Trying %s... ", addr_buf);
+      snprintf(data->info.ip, MAX_IPADR_LEN, "%s", addr_buf);
+    }
   }
 
   if(data->set.tcp_nodelay)
index 078154246f7fec9e07f4dfc5d74e92b35dbcdd60..bef2ebac44289c38f4e67f6514f7596f742caedb 100644 (file)
@@ -210,6 +210,10 @@ CURLcode Curl_getinfo(struct SessionHandle *data, CURLINFO info, ...)
        option had been enabled! */
     *param_charp = data->info.wouldredirect;
     break;
+  case CURLINFO_PRIMARY_IP:
+    /* Return the ip address of the most recent (primary) connection */
+    *param_charp = data->info.ip;
+    break;
   default:
     return CURLE_BAD_FUNCTION_ARGUMENT;
   }
index fa93a6454c92987a5c6b5e07c18624bba9f0945e..1f9d3ebd28245496d24672c4c68a5e32bc1efe33 100644 (file)
@@ -48,6 +48,8 @@
 #define CURL_DEFAULT_USER "anonymous"
 #define CURL_DEFAULT_PASSWORD "ftp@example.com"
 
+#define MAX_IPADR_LEN (4*9) /* should be enough to hold the longest ipv6 one */
+
 #include "cookie.h"
 #include "formdata.h"
 
@@ -1036,6 +1038,9 @@ struct PureInfo {
   long numconnects; /* how many new connection did libcurl created */
   char *contenttype; /* the content type of the object */
   char *wouldredirect; /* URL this would've been redirected to if asked to */
+  char ip[MAX_IPADR_LEN]; /* this buffer gets the numerical ip version stored
+                             at the connect *attempt* so it will get the last
+                             tried connect IP even on failures */
 };
 
 
index bf4407fea840abd5b5a5bba40381b39400401b63..33a3a7959061f881fe0e7c5cf8d7195f0f7959e8 100644 (file)
@@ -32,13 +32,16 @@ lib500
 simple libcurl HTTP GET tool
  </name>
  <command>
-http://%HOSTIP:%HTTPPORT/500
+http://%HOSTIP:%HTTPPORT/500 log/ip500
 </command>
 </client>
 
 #
 # Verify data after the test has been "shot"
 <verify>
+<file name="log/ip500" mode="text">
+IP: %HOSTIP
+</file>
 <protocol>
 GET /500 HTTP/1.1\r
 Host: %HOSTIP:%HTTPPORT\r
index d22aecdbcddca96202437df2a34e82fac87dfc93..c28f5d0c94854f247ebb9cccf558f2d9a52ed558 100644 (file)
@@ -14,6 +14,7 @@ int test(char *URL)
 {
   CURLcode res;
   CURL *curl;
+  char *ipstr=NULL;
 
   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     fprintf(stderr, "curl_global_init() failed\n");
@@ -31,7 +32,17 @@ int test(char *URL)
 
   res = curl_easy_perform(curl);
 
-  curl_easy_cleanup(curl);  
+  if(!res) {
+    FILE *moo;
+    res = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ipstr);
+    moo = fopen(libtest_arg2, "wb");
+    if(moo) {
+      fprintf(moo, "IP: %s\n", ipstr);
+      fclose(moo);
+    }
+  }
+
+  curl_easy_cleanup(curl);
   curl_global_cleanup();
 
   return (int)res;