Skip to content

Commit

Permalink
Add ipify_connect1() to select IPv4 or IPv6 address
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Jul 20, 2022
1 parent 4e3e974 commit 4250f0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions ipify.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
"User-Agent: " AGENT_NAME "\r\n\r\n";

/*
* Connect to api.ipify.org using either address protocol supported. We
* want to connect using TCP, so ask getaddrinfo() for a TCP connection
* over either IPv4 or IPv6, then use the first successful connection.
* Same as ipify_connect() but you can limit the call to the given
* address family. Only AF_INET and IF_INET6 makes sense here, the
* default used by ipipfy_connect() is AF_UNSPEC with tries both IPv4
* and IPv6 in the order the name server provides them.
*/
int ipify_connect(void)
int ipify_connect1(int family)
{
struct addrinfo *info, *ai;
struct addrinfo hints;
int rc, sd;

memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;

rc = getaddrinfo(IPIFY_HOST, "http", &hints, &info);
Expand All @@ -68,6 +69,16 @@ int ipify_connect(void)
return sd;
}

/*
* Connect to api.ipify.org using either address protocol supported. We
* want to connect using TCP, so ask getaddrinfo() for a TCP connection
* over either IPv4 or IPv6, then use the first successful connection.
*/
int ipify_connect(void)
{
return ipify_connect1(AF_UNSPEC);
}

int ipify_query(int sd, char *addr, size_t len)
{
const char *req = HTTP_REQUEST;
Expand Down
1 change: 1 addition & 0 deletions ipify.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdio.h>

int ipify_connect (void);
int ipify_connect1 (int family);
int ipify_query (int sd, char *addr, size_t len);
int ipify_disconnect (int sd);

Expand Down

0 comments on commit 4250f0f

Please sign in to comment.