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