#include <sys_defs.h>
#include <string.h>
#include <ctype.h>
#ifndef NO_EAI
#include <unicode/uidna.h>
#include <mymalloc.h>
#include <msg.h>
#include <ctable.h>
#include <stringops.h>
#include <valid_hostname.h>
#include <name_mask.h>
#include <midna_domain.h>
#define DEF_MIDNA_CACHE_SIZE 256
int midna_domain_cache_size = DEF_MIDNA_CACHE_SIZE;
int midna_domain_transitional = 0;
static VSTRING *midna_domain_buf;
#define STR(x) vstring_str(x)
static const char *midna_domain_strerror(UErrorCode error, int info_errors)
{
static LONG_NAME_MASK uidna_errors[] = {
"UIDNA_ERROR_EMPTY_LABEL", UIDNA_ERROR_EMPTY_LABEL,
"UIDNA_ERROR_LABEL_TOO_LONG", UIDNA_ERROR_LABEL_TOO_LONG,
"UIDNA_ERROR_DOMAIN_NAME_TOO_LONG", UIDNA_ERROR_DOMAIN_NAME_TOO_LONG,
"UIDNA_ERROR_LEADING_HYPHEN", UIDNA_ERROR_LEADING_HYPHEN,
"UIDNA_ERROR_TRAILING_HYPHEN", UIDNA_ERROR_TRAILING_HYPHEN,
"UIDNA_ERROR_HYPHEN_3_4", UIDNA_ERROR_HYPHEN_3_4,
"UIDNA_ERROR_LEADING_COMBINING_MARK", UIDNA_ERROR_LEADING_COMBINING_MARK,
"UIDNA_ERROR_DISALLOWED", UIDNA_ERROR_DISALLOWED,
"UIDNA_ERROR_PUNYCODE", UIDNA_ERROR_PUNYCODE,
"UIDNA_ERROR_LABEL_HAS_DOT", UIDNA_ERROR_LABEL_HAS_DOT,
"UIDNA_ERROR_INVALID_ACE_LABEL", UIDNA_ERROR_INVALID_ACE_LABEL,
"UIDNA_ERROR_BIDI", UIDNA_ERROR_BIDI,
"UIDNA_ERROR_CONTEXTJ", UIDNA_ERROR_CONTEXTJ,
0,
};
if (info_errors) {
return (str_long_name_mask_opt((VSTRING *) 0, "idna error",
uidna_errors, info_errors,
NAME_MASK_NUMBER | NAME_MASK_COMMA));
} else {
return u_errorName(error);
}
}
static void *midna_domain_to_ascii_create(const char *name, void *unused_context)
{
static const char myname[] = "midna_domain_to_ascii_create";
char buf[1024];
UErrorCode error = U_ZERO_ERROR;
UIDNAInfo info = UIDNA_INFO_INITIALIZER;
UIDNA *idna;
int anl;
if (allascii(name) == 0 && valid_utf8_string(name, strlen(name)) == 0) {
msg_warn("%s: Problem translating domain \"%.100s\" to ASCII form: %s",
myname, name, "malformed UTF-8");
return (0);
}
idna = uidna_openUTS46(midna_domain_transitional ? UIDNA_DEFAULT
: UIDNA_NONTRANSITIONAL_TO_ASCII, &error);
anl = uidna_nameToASCII_UTF8(idna,
name, strlen(name),
buf, sizeof(buf) - 1,
&info,
&error);
uidna_close(idna);
if (U_SUCCESS(error) && info.errors == 0 && anl > 0) {
buf[anl] = 0;
if (!valid_hostname(buf, DONT_GRIPE)) {
msg_warn("%s: Problem translating domain \"%.100s\" to ASCII form: %s",
myname, name, "malformed ASCII label(s)");
return (0);
}
return (mystrndup(buf, anl));
} else {
msg_warn("%s: Problem translating domain \"%.100s\" to ASCII form: %s",
myname, name, midna_domain_strerror(error, info.errors));
return (0);
}
}
static void *midna_domain_to_utf8_create(const char *name, void *unused_context)
{
static const char myname[] = "midna_domain_to_utf8_create";
char buf[1024];
UErrorCode error = U_ZERO_ERROR;
UIDNAInfo info = UIDNA_INFO_INITIALIZER;
UIDNA *idna;
int anl;
if (allascii(name) == 0 && valid_utf8_string(name, strlen(name)) == 0) {
msg_warn("%s: Problem translating domain \"%.100s\" to UTF-8 form: %s",
myname, name, "malformed UTF-8");
return (0);
}
idna = uidna_openUTS46(midna_domain_transitional ? UIDNA_DEFAULT
: UIDNA_NONTRANSITIONAL_TO_UNICODE, &error);
anl = uidna_nameToUnicodeUTF8(idna,
name, strlen(name),
buf, sizeof(buf) - 1,
&info,
&error);
uidna_close(idna);
if (U_SUCCESS(error) && info.errors == 0 && anl > 0) {
buf[anl] = 0;
if (midna_domain_to_ascii(buf) == 0)
return (0);
return (mystrndup(buf, anl));
} else {
msg_warn("%s: Problem translating domain \"%.100s\" to UTF8 form: %s",
myname, name, midna_domain_strerror(error, info.errors));
return (0);
}
}
static void midna_domain_cache_free(void *value, void *unused_context)
{
if (value)
myfree(value);
}
const char *midna_domain_to_ascii(const char *name)
{
static CTABLE *midna_domain_to_ascii_cache = 0;
if (midna_domain_to_ascii_cache == 0)
midna_domain_to_ascii_cache = ctable_create(midna_domain_cache_size,
midna_domain_to_ascii_create,
midna_domain_cache_free,
(void *) 0);
return (ctable_locate(midna_domain_to_ascii_cache, name));
}
const char *midna_domain_to_utf8(const char *name)
{
static CTABLE *midna_domain_to_utf8_cache = 0;
if (midna_domain_to_utf8_cache == 0)
midna_domain_to_utf8_cache = ctable_create(midna_domain_cache_size,
midna_domain_to_utf8_create,
midna_domain_cache_free,
(void *) 0);
return (ctable_locate(midna_domain_to_utf8_cache, name));
}
const char *midna_domain_suffix_to_ascii(const char *suffix)
{
const char *cache_res;
if (midna_domain_buf == 0)
midna_domain_buf = vstring_alloc(100);
vstring_sprintf(midna_domain_buf, "x%s", suffix);
if ((cache_res = midna_domain_to_ascii(STR(midna_domain_buf))) == 0)
return (0);
else
return (cache_res + 1);
}
const char *midna_domain_suffix_to_utf8(const char *name)
{
const char *cache_res;
if (midna_domain_buf == 0)
midna_domain_buf = vstring_alloc(100);
vstring_sprintf(midna_domain_buf, "x%s", name);
if ((cache_res = midna_domain_to_utf8(STR(midna_domain_buf))) == 0)
return (0);
else
return (cache_res + 1);
}
#ifdef TEST
#include <stdlib.h>
#include <locale.h>
#include <stringops.h>
#include <vstring.h>
#include <vstream.h>
#include <vstring_vstream.h>
#include <msg_vstream.h>
int main(int argc, char **argv)
{
VSTRING *buffer = vstring_alloc(1);
const char *bp;
const char *ascii;
const char *utf8;
if (setlocale(LC_ALL, "C") == 0)
msg_fatal("setlocale(LC_ALL, C) failed: %m");
msg_vstream_init(argv[0], VSTREAM_ERR);
util_utf8_enable = 1;
while (vstring_fgets_nonl(buffer, VSTREAM_IN)) {
bp = STR(buffer);
msg_info("> %s", bp);
while (ISSPACE(*bp))
bp++;
if (*bp == '#' || *bp == 0)
continue;
msg_info("unconditional conversions:");
utf8 = midna_domain_to_utf8(bp);
msg_info("\"%s\" ->utf8 \"%s\"", bp, utf8 ? utf8 : "(error)");
ascii = midna_domain_to_ascii(bp);
msg_info("\"%s\" ->ascii \"%s\"", bp, ascii ? ascii : "(error)");
msg_info("conditional conversions:");
if (!allascii(bp)) {
if (ascii != 0) {
utf8 = midna_domain_to_utf8(ascii);
msg_info("\"%s\" ->ascii \"%s\" ->utf8 \"%s\"",
bp, ascii, utf8 ? utf8 : "(error)");
if (utf8 != 0) {
if (strcmp(utf8, bp) != 0)
msg_warn("\"%s\" != \"%s\"", bp, utf8);
}
}
} else {
if (utf8 != 0) {
ascii = midna_domain_to_ascii(utf8);
msg_info("\"%s\" ->utf8 \"%s\" ->ascii \"%s\"",
bp, utf8, ascii ? ascii : "(error)");
if (ascii != 0) {
if (strcmp(ascii, bp) != 0)
msg_warn("\"%s\" != \"%s\"", bp, ascii);
}
}
}
}
exit(0);
}
#endif
#endif