1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLINFO_PRIMARY_IP
5Section: 3
6Source: libcurl
7See-also:
8  - CURLINFO_LOCAL_IP (3)
9  - CURLINFO_LOCAL_PORT (3)
10  - CURLINFO_PRIMARY_PORT (3)
11  - curl_easy_getinfo (3)
12  - curl_easy_setopt (3)
13Protocol:
14  - All
15---
16
17# NAME
18
19CURLINFO_PRIMARY_IP - get IP address of last connection
20
21# SYNOPSIS
22
23~~~c
24#include <curl/curl.h>
25
26CURLcode curl_easy_getinfo(CURL *handle, CURLINFO_PRIMARY_IP, char **ip);
27~~~
28
29# DESCRIPTION
30
31Pass a pointer to a char pointer to receive the pointer to a null-terminated
32string holding the IP address of the most recent connection done with this
33**curl** handle. This string may be IPv6 when that is enabled. Note that you
34get a pointer to a memory area that is reused at next request so you need to
35copy the string if you want to keep the information.
36
37The **ip** pointer is NULL or points to private memory. You MUST NOT free -
38it gets freed when you call curl_easy_cleanup(3) on the corresponding
39CURL handle.
40
41# EXAMPLE
42
43~~~c
44int main(void)
45{
46  char *ip;
47  CURLcode res;
48  CURL *curl = curl_easy_init();
49
50  curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
51
52  /* Perform the transfer */
53  res = curl_easy_perform(curl);
54  /* Check for errors */
55  if((res == CURLE_OK) &&
56     !curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip) && ip) {
57    printf("IP: %s\n", ip);
58  }
59
60  /* always cleanup */
61  curl_easy_cleanup(curl);
62}
63~~~
64
65# AVAILABILITY
66
67Added in 7.19.0
68
69# RETURN VALUE
70
71Returns CURLE_OK if the option is supported, and CURLE_UNKNOWN_OPTION if not.
72