cnping

Minimal Graphical Ping Tool
git clone https://git.sinitax.com/cnlohr/cnping
Log | Files | Refs | Submodules | README | LICENSE | sfeed.txt

commit 23ffbf3a02eb3d4c1835d4158bbf6a3c527ecb01
parent 9af3b2d01aa25ee92f0e0aa9606e0ee65c2164b8
Author: mrbesen <y.g.2@gmx.de>
Date:   Thu, 22 Sep 2022 17:33:27 +0200

add possible hint to resolve (ipv4 for windows ICMPping only)

Diffstat:
Mhttping.c | 2+-
Mping.c | 6+++---
Mresolve.c | 39+++++++++++++++++++++++++--------------
Mresolve.h | 3++-
4 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/httping.c b/httping.c @@ -80,7 +80,7 @@ void DoHTTPing( const char * addy, double minperiod, int * seqnoptr, volatile do /* gethostbyname: get the server's DNS entry */ serveraddr_len = sizeof(serveraddr); *getting_host_by_name = 1; - serverresolve = resolveName((struct sockaddr*) &serveraddr, &serveraddr_len, hostname); + serverresolve = resolveName((struct sockaddr*) &serveraddr, &serveraddr_len, hostname, AF_UNSPEC); *getting_host_by_name = 0; if (serverresolve != 1) { diff --git a/ping.c b/ping.c @@ -54,7 +54,7 @@ void ping_setup(const char * strhost, const char * device) { // resolve host psaddr_len = sizeof(psaddr); - resolveName((struct sockaddr*) &psaddr, &psaddr_len, strhost); + resolveName((struct sockaddr*) &psaddr, &psaddr_len, strhost, AF_INET); // only resolve ipv4 on windows s_disp = OGCreateSema(); s_ping = OGCreateSema(); @@ -133,7 +133,7 @@ void ping(struct sockaddr *addr, socklen_t addr_len ) { // ipv6 ICMP Ping is not supported on windows ERRM( "ERROR: ipv6 ICMP Ping is not supported on windows\n" ); - return; + exit( -1 ); } //Launch pinger threads @@ -463,7 +463,7 @@ void ping_setup(const char * strhost, const char * device) #else // resolve host psaddr_len = sizeof(psaddr); - resolveName((struct sockaddr*) &psaddr, &psaddr_len, strhost); + resolveName((struct sockaddr*) &psaddr, &psaddr_len, strhost, AF_UNSPEC); sd = createSocket(); diff --git a/resolve.c b/resolve.c @@ -15,7 +15,7 @@ #include <netdb.h> #endif -int resolveName(struct sockaddr* addr, socklen_t* addr_len, const char* hostname) +int resolveName(struct sockaddr* addr, socklen_t* addr_len, const char* hostname, int family) { int result; struct addrinfo* res = NULL; @@ -24,25 +24,36 @@ int resolveName(struct sockaddr* addr, socklen_t* addr_len, const char* hostname memset(addr, 0, *addr_len); // try to parse ipv4 - result = inet_pton(AF_INET, hostname, &((struct sockaddr_in*) addr)->sin_addr); - if(result == 1) - { - addr->sa_family = AF_INET; - *addr_len = sizeof(struct sockaddr_in); - return 1; + if(family == AF_UNSPEC || family == AF_INET) { + result = inet_pton(AF_INET, hostname, &((struct sockaddr_in*) addr)->sin_addr); + if(result == 1) + { + addr->sa_family = AF_INET; + *addr_len = sizeof(struct sockaddr_in); + return 1; + } } // try to parse ipv6 - result = inet_pton(AF_INET6, hostname, &((struct sockaddr_in6*) addr)->sin6_addr); - if(result == 1) - { - addr->sa_family = AF_INET6; - *addr_len = sizeof(struct sockaddr_in6); - return 1; + if(family == AF_UNSPEC || family == AF_INET6) { + result = inet_pton(AF_INET6, hostname, &((struct sockaddr_in6*) addr)->sin6_addr); + if(result == 1) + { + addr->sa_family = AF_INET6; + *addr_len = sizeof(struct sockaddr_in6); + return 1; + } } // try to resolve DNS - result = getaddrinfo(hostname, NULL, NULL, &res); + struct addrinfo hints; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = family; // AF_UNSPEC, AF_INET, AF_INET6 + hints.ai_socktype = 0; // 0 = any, SOCK_STREAM, SOCK_DGRAM + hints.ai_protocol = 0; // 0 = any + hints.ai_flags = 0; // no flags + + result = getaddrinfo(hostname, NULL, &hints, &res); if( result != 0) { ERRM( "Error: cannot resolve hostname %s: %s\n", hostname, gai_strerror(result) ); diff --git a/resolve.h b/resolve.h @@ -13,6 +13,7 @@ // * as ipv6 notation (abcd:ef00::1) // * as hostname (resolve DNS) // returns 1 on success -int resolveName(struct sockaddr* addr, socklen_t* addr_len, const char* hostname); +// family can be used to force ipv4 or ipv6. Use AF_UNSPEC (allow both), AF_INET or AF_INET6 to force ipv4 or ipv6 +int resolveName(struct sockaddr* addr, socklen_t* addr_len, const char* hostname, int family); #endif