Skip to content

Commit

Permalink
Merge pull request #97 from nmav/tmp-idn2
Browse files Browse the repository at this point in the history
Simplify IDNA usage and switch to libidn2 when needed
  • Loading branch information
okias committed Oct 30, 2017
2 parents 67e7d0d + ca49ea3 commit 29fac00
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 48 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Expand Up @@ -3,17 +3,17 @@ dist: trusty
before_install:
- sudo apt-get update -qq
install:
- sudo apt-get install libcap-dev -qq
- sudo apt-get install nettle-dev -qq
- sudo apt-get install libcap-dev libidn2-0-dev -qq
- sudo apt-get install nettle-dev libidn2-0-dev -qq

compiler:
- gcc
- clang
env:
matrix:
- USE_CAP=true
- USE_CAP=false
- USE_IDN=true
- USE_IDN=false
script: make
- USE_CAP=yes
- USE_CAP=no
- USE_IDN=yes
- USE_IDN=no
script: make USE_CAP=${USE_CAP} USE_IDN=${USE_IDN}

2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -13,7 +13,7 @@ LDFLAG_CAP=-lcap
LDFLAG_GCRYPT=-lgcrypt -lgpg-error
LDFLAG_NETTLE=-lnettle
LDFLAG_CRYPTO=-lcrypto
LDFLAG_IDN=-lidn
LDFLAG_IDN=-lidn2
LDFLAG_RESOLV=-lresolv
LDFLAG_RT=-lrt
LDFLAG_M=-lm
Expand Down
1 change: 0 additions & 1 deletion arping.c
Expand Up @@ -43,7 +43,6 @@
#endif

#ifdef USE_IDN
#include <idna.h>
#include <locale.h>
#endif

Expand Down
3 changes: 1 addition & 2 deletions ping.h
Expand Up @@ -36,8 +36,7 @@

#ifdef USE_IDN
#include <locale.h>
#include <idna.h>
#include <stringprep.h>
#include <idn2.h>
#define getaddrinfo_flags (AI_CANONNAME | AI_IDN | AI_CANONIDN)
#define getnameinfo_flags NI_IDN
#else
Expand Down
40 changes: 17 additions & 23 deletions ping6_common.c
Expand Up @@ -430,14 +430,22 @@ static int niquery_option_subject_addr_handler(int index, const char *arg)
return 0;
}

static int niquery_option_subject_name_handler(int index, const char *arg)
#ifdef USE_IDN
# if IDN2_VERSION_NUMBER >= 0x02000000
# define IDN2_FLAGS IDN2_NONTRANSITIONAL
# else
# define IDN2_FLAGS 0
# endif
#endif

static int niquery_option_subject_name_handler(int index, const char *name)
{
#ifdef USE_CRYPTO
static char nigroup_buf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ];
unsigned char *dnptrs[2], **dpp, **lastdnptr;
int n;
int i;
char *name, *p;
char *p;
char *canonname = NULL, *idn = NULL;
unsigned char *buf = NULL;
size_t namelen;
Expand All @@ -453,18 +461,19 @@ static int niquery_option_subject_name_handler(int index, const char *arg)
return -1;

#ifdef USE_IDN
name = stringprep_locale_to_utf8(arg);
if (!name) {
fprintf(stderr, "ping6: IDN support failed.\n");
rc = idn2_lookup_ul(name, &idn, IDN2_FLAGS);
if (rc) {
fprintf(stderr, "ping6: IDN encoding error: %s\n",
idn2_strerror(rc));
exit(2);
}
#else
name = strdup(arg);
if (!name)
idn = strdup(name);
if (!idn)
goto oomexit;
#endif

p = strchr(name, SCOPE_DELIMITER);
p = strchr(idn, SCOPE_DELIMITER);
if (p) {
*p = '\0';
if (strlen(p + 1) >= IFNAMSIZ) {
Expand All @@ -473,19 +482,6 @@ static int niquery_option_subject_name_handler(int index, const char *arg)
}
}

#ifdef USE_IDN
rc = idna_to_ascii_8z(name, &idn, 0);
if (rc) {
fprintf(stderr, "ping6: IDN encoding error: %s\n",
idna_strerror(rc));
exit(2);
}
#else
idn = strdup(name);
if (!idn)
goto oomexit;
#endif

namelen = strlen(idn);
canonname = malloc(namelen + 1);
if (!canonname)
Expand Down Expand Up @@ -547,7 +543,6 @@ static int niquery_option_subject_name_handler(int index, const char *arg)

free(canonname);
free(idn);
free(name);

return 0;
oomexit:
Expand All @@ -556,7 +551,6 @@ static int niquery_option_subject_name_handler(int index, const char *arg)
free(buf);
free(canonname);
free(idn);
free(name);
exit(1);
#else
fprintf(stderr, "ping6: function not available; crypto disabled\n");
Expand Down
1 change: 0 additions & 1 deletion tracepath.c
Expand Up @@ -28,7 +28,6 @@
#include <arpa/inet.h>

#ifdef USE_IDN
#include <idna.h>
#include <locale.h>
#define getnameinfo_flags NI_IDN
#else
Expand Down
18 changes: 5 additions & 13 deletions traceroute6.c
Expand Up @@ -244,12 +244,13 @@
#endif

#ifdef USE_IDN
#include <idna.h>
#include <locale.h>

#define ADDRINFO_IDN_FLAGS AI_IDN
#define getnameinfo_flags NI_IDN
#else
#define getnameinfo_flags 0
#define ADDRINFO_IDN_FLAGS 0
#endif

#include <arpa/inet.h>
Expand Down Expand Up @@ -331,7 +332,8 @@ int main(int argc, char *argv[])
char pa[NI_MAXHOST];
extern char *optarg;
extern int optind;
struct addrinfo hints6 = { .ai_family = AF_INET6, .ai_socktype = SOCK_RAW, .ai_flags = AI_CANONNAME };
struct addrinfo hints6 = { .ai_family = AF_INET6, .ai_socktype = SOCK_RAW,
.ai_flags = AI_CANONNAME|ADDRINFO_IDN_FLAGS };
struct addrinfo *result;
int status;
struct sockaddr_in6 from, *to;
Expand Down Expand Up @@ -445,23 +447,13 @@ int main(int argc, char *argv[])
if (inet_pton(AF_INET6, *argv, &to->sin6_addr) > 0) {
hostname = *argv;
} else {
char *idn = NULL;
#ifdef USE_IDN
if (idna_to_ascii_lz(*argv, &idn, 0) != IDNA_SUCCESS)
idn = NULL;
#endif
status = getaddrinfo(idn ? idn : *argv, NULL, &hints6, &result);
status = getaddrinfo(*argv, NULL, &hints6, &result);
if (status) {
(void)fprintf(stderr,
"traceroute: %s: %s\n", *argv, gai_strerror(status));
exit(1);
}

if (idn != NULL) {
free(idn);
idn = NULL;
}

memcpy(to, result->ai_addr, sizeof *to);
resolved_hostname = strdup(result->ai_canonname);
if (resolved_hostname == NULL) {
Expand Down

0 comments on commit 29fac00

Please sign in to comment.