bc5dde
diff -up bind-9.7.0b1/bin/dig/dighost.c.libidn bind-9.7.0b1/bin/dig/dighost.c
bc5dde
--- bind-9.7.0b1/bin/dig/dighost.c.libidn	2009-09-16 01:48:09.000000000 +0200
bc5dde
+++ bind-9.7.0b1/bin/dig/dighost.c	2009-10-20 10:49:26.719056220 +0200
bc5dde
@@ -44,6 +44,11 @@
bc5dde
 #include <idn/api.h>
bc5dde
 #endif
bc5dde
 
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+#include <stringprep.h>
bc5dde
+#include <idna.h>
bc5dde
+#endif
bc5dde
+
bc5dde
 #include <dns/byaddr.h>
bc5dde
 #ifdef DIG_SIGCHASE
bc5dde
 #include <dns/callbacks.h>
bc5dde
@@ -153,6 +158,14 @@ static void		idn_check_result(idn_result
bc5dde
 int  idnoptions	= 0;
bc5dde
 #endif
bc5dde
 
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+static isc_result_t	libidn_locale_to_utf8 (const char* from, char **to);
bc5dde
+static isc_result_t	libidn_utf8_to_ascii (const char* from, char *to);
bc5dde
+static isc_result_t	output_filter (isc_buffer_t *buffer,
bc5dde
+				       unsigned int used_org,
bc5dde
+				       isc_boolean_t absolute);
bc5dde
+#endif
bc5dde
+
bc5dde
 /*%
bc5dde
  * Exit Codes:
bc5dde
  *
bc5dde
@@ -1184,6 +1197,9 @@ setup_system(void) {
bc5dde
 	dig_searchlist_t *domain = NULL;
bc5dde
 	lwres_result_t lwresult;
bc5dde
 	unsigned int lwresflags;
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+	isc_result_t result;
bc5dde
+#endif
bc5dde
 
bc5dde
 	debug("setup_system()");
bc5dde
 
bc5dde
@@ -1242,8 +1258,15 @@ setup_system(void) {
bc5dde
 
bc5dde
 #ifdef WITH_IDN
bc5dde
 	initialize_idn();
bc5dde
+	
bc5dde
+#endif
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+	result = dns_name_settotextfilter(output_filter);
bc5dde
+	check_result(result, "dns_name_settotextfilter");
bc5dde
+#ifdef HAVE_SETLOCALE
bc5dde
+	setlocale (LC_ALL, "");
bc5dde
+#endif
bc5dde
 #endif
bc5dde
-
bc5dde
 	if (keyfile[0] != 0)
bc5dde
 		setup_file_key();
bc5dde
 	else if (keysecret[0] != 0)
bc5dde
@@ -1957,12 +1980,18 @@ setup_lookup(dig_lookup_t *lookup) {
bc5dde
 	idn_result_t mr;
bc5dde
 	char utf8_textname[MXNAME], utf8_origin[MXNAME], idn_textname[MXNAME];
bc5dde
 #endif
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+	char *utf8_str = NULL, utf8_name[MXNAME], ascii_name[MXNAME];
bc5dde
+#endif
bc5dde
 
bc5dde
 #ifdef WITH_IDN
bc5dde
 	result = dns_name_settotextfilter(output_filter);
bc5dde
 	check_result(result, "dns_name_settotextfilter");
bc5dde
 #endif
bc5dde
-
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+	result = dns_name_settotextfilter (output_filter);
bc5dde
+	check_result(result, "dns_name_settotextfilter");
bc5dde
+#endif
bc5dde
 	REQUIRE(lookup != NULL);
bc5dde
 	INSIST(!free_now);
bc5dde
 
bc5dde
@@ -1999,6 +2028,16 @@ setup_lookup(dig_lookup_t *lookup) {
bc5dde
 	mr = idn_encodename(IDN_LOCALCONV | IDN_DELIMMAP, lookup->textname,
bc5dde
 			    utf8_textname, sizeof(utf8_textname));
bc5dde
 	idn_check_result(mr, "convert textname to UTF-8");
bc5dde
+#elif defined (WITH_LIBIDN)
bc5dde
+	result = libidn_locale_to_utf8 (lookup->textname, &utf8_str);
bc5dde
+	check_result (result, "converting textname to UTF-8");
bc5dde
+	len = strlen (utf8_str);
bc5dde
+	if (len < MXNAME) {
bc5dde
+		(void) strcpy (utf8_name, utf8_str);
bc5dde
+	} else {
bc5dde
+		fatal ("Too long name");
bc5dde
+	}
bc5dde
+	isc_mem_free (mctx, utf8_str);
bc5dde
 #endif
bc5dde
 
bc5dde
 	/*
bc5dde
@@ -2018,6 +2057,15 @@ setup_lookup(dig_lookup_t *lookup) {
bc5dde
 			lookup->origin = ISC_LIST_HEAD(search_list);
bc5dde
 			lookup->need_search = ISC_FALSE;
bc5dde
 		}
bc5dde
+#elif defined (WITH_LIBIDN)
bc5dde
+		if ((count_dots(utf8_name) >= ndots) || !usesearch) {
bc5dde
+			lookup->origin = NULL; /* Force abs lookup */
bc5dde
+			lookup->done_as_is = ISC_TRUE;
bc5dde
+			lookup->need_search = usesearch;
bc5dde
+		} else if (lookup->origin == NULL && usesearch) {
bc5dde
+			lookup->origin = ISC_LIST_HEAD(search_list);
bc5dde
+			lookup->need_search = ISC_FALSE;
bc5dde
+		}
bc5dde
 #else
bc5dde
 		if ((count_dots(lookup->textname) >= ndots) || !usesearch) {
bc5dde
 			lookup->origin = NULL; /* Force abs lookup */
bc5dde
@@ -2044,6 +2092,20 @@ setup_lookup(dig_lookup_t *lookup) {
bc5dde
 			    IDN_IDNCONV | IDN_LENCHECK, utf8_textname,
bc5dde
 			    idn_textname, sizeof(idn_textname));
bc5dde
 	idn_check_result(mr, "convert UTF-8 textname to IDN encoding");
bc5dde
+#elif defined (WITH_LIBIDN)
bc5dde
+	if (lookup->origin != NULL) {
bc5dde
+		result = libidn_locale_to_utf8 (lookup->origin->origin, &utf8_str);
bc5dde
+		check_result (result, "convert origin to UTF-8");
bc5dde
+		if (len + strlen (utf8_str) + 1 < MXNAME) {
bc5dde
+			utf8_name[len++] = '.';
bc5dde
+			(void) strcpy (utf8_name + len, utf8_str);
bc5dde
+		} else {
bc5dde
+			fatal ("Too long name + origin");
bc5dde
+		}
bc5dde
+		isc_mem_free (mctx, utf8_str);
bc5dde
+	}
bc5dde
+
bc5dde
+	result = libidn_utf8_to_ascii (utf8_name, ascii_name);
bc5dde
 #else
bc5dde
 	if (lookup->origin != NULL) {
bc5dde
 		debug("trying origin %s", lookup->origin->origin);
bc5dde
@@ -2099,6 +2161,13 @@ setup_lookup(dig_lookup_t *lookup) {
bc5dde
 			result = dns_name_fromtext(lookup->name, &b,
bc5dde
 						   dns_rootname, 0,
bc5dde
 						   &lookup->namebuf);
bc5dde
+#elif defined (WITH_LIBIDN)
bc5dde
+			len = strlen (ascii_name);
bc5dde
+			isc_buffer_init(&b, ascii_name, len);
bc5dde
+			isc_buffer_add(&b, len);
bc5dde
+			result = dns_name_fromtext(lookup->name, &b,
bc5dde
+						   dns_rootname, 0,
bc5dde
+						   &lookup->namebuf);
bc5dde
 #else
bc5dde
 			len = strlen(lookup->textname);
bc5dde
 			isc_buffer_init(&b, lookup->textname, len);
bc5dde
@@ -3617,7 +3686,7 @@ destroy_libs(void) {
bc5dde
 	void * ptr;
bc5dde
 	dig_message_t *chase_msg;
bc5dde
 #endif
bc5dde
-#ifdef WITH_IDN
bc5dde
+#if defined (WITH_IDN) || defined (WITH_LIBIDN)
bc5dde
 	isc_result_t result;
bc5dde
 #endif
bc5dde
 
bc5dde
@@ -3656,6 +3725,10 @@ destroy_libs(void) {
bc5dde
 	result = dns_name_settotextfilter(NULL);
bc5dde
 	check_result(result, "dns_name_settotextfilter");
bc5dde
 #endif
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+	result = dns_name_settotextfilter (NULL);
bc5dde
+	check_result(result, "clearing dns_name_settotextfilter");
bc5dde
+#endif
bc5dde
 	dns_name_destroy();
bc5dde
 
bc5dde
 	if (commctx != NULL) {
bc5dde
@@ -3834,6 +3907,79 @@ idn_check_result(idn_result_t r, const c
bc5dde
 	}
bc5dde
 }
bc5dde
 #endif /* WITH_IDN */
bc5dde
+#ifdef WITH_LIBIDN
bc5dde
+/* If stringprep_locale_to_utf8 fails simple copy string */
bc5dde
+static isc_result_t
bc5dde
+libidn_locale_to_utf8 (const char *from, char **to) {
bc5dde
+	char *utf8_str;
bc5dde
+
bc5dde
+	utf8_str = stringprep_locale_to_utf8 (from);
bc5dde
+	if (utf8_str == NULL) {
bc5dde
+		*to = isc_mem_allocate (mctx, strlen (from) + 1);
bc5dde
+		if (*to == NULL)
bc5dde
+			return (ISC_R_NOMEMORY);
bc5dde
+		(void) strcpy (*to, from);
bc5dde
+	} else {
bc5dde
+		*to = isc_mem_allocate (mctx, strlen (utf8_str) + 1);
bc5dde
+		if (*to == NULL)
bc5dde
+			return (ISC_R_NOMEMORY);
bc5dde
+		(void) strcpy (*to, utf8_str);
bc5dde
+		free (utf8_str);
bc5dde
+	}
bc5dde
+	return (ISC_R_SUCCESS);
bc5dde
+}
bc5dde
+static isc_result_t
bc5dde
+libidn_utf8_to_ascii (const char *from, char *to) {
bc5dde
+	char *ascii;
bc5dde
+
bc5dde
+	if (idna_to_ascii_8z (from, &ascii, 0) != IDNA_SUCCESS)
bc5dde
+		return (ISC_R_FAILURE);
bc5dde
+
bc5dde
+	(void) strcpy (to, ascii);
bc5dde
+	free (ascii);
bc5dde
+	return (ISC_R_SUCCESS);
bc5dde
+}
bc5dde
+/* based on idnkit's code*/
bc5dde
+static isc_result_t
bc5dde
+output_filter (isc_buffer_t *buffer, unsigned int used_org,
bc5dde
+	       isc_boolean_t absolute) {
bc5dde
+	char tmp1[MXNAME], *tmp2;
bc5dde
+        size_t fromlen, tolen;
bc5dde
+        isc_boolean_t end_with_dot;
bc5dde
+
bc5dde
+        fromlen = isc_buffer_usedlength(buffer) - used_org;
bc5dde
+	if (fromlen >= MXNAME)
bc5dde
+		return (ISC_R_SUCCESS);
bc5dde
+        memcpy(tmp1, (char *)isc_buffer_base(buffer) + used_org, fromlen);
bc5dde
+        end_with_dot = (tmp1[fromlen - 1] == '.') ? ISC_TRUE : ISC_FALSE;
bc5dde
+        if (absolute && !end_with_dot) {
bc5dde
+                fromlen++;
bc5dde
+		if (fromlen >= MXNAME)
bc5dde
+			return (ISC_R_SUCCESS);
bc5dde
+                tmp1[fromlen - 1] = '.';
bc5dde
+        }
bc5dde
+        tmp1[fromlen] = '\0';
bc5dde
+
bc5dde
+	if (idna_to_unicode_lzlz (tmp1, &tmp2, 0) != IDNA_SUCCESS)
bc5dde
+		return (ISC_R_SUCCESS);
bc5dde
+
bc5dde
+	(void) strcpy (tmp1, tmp2);
bc5dde
+	free (tmp2);
bc5dde
+
bc5dde
+        tolen = strlen(tmp1);
bc5dde
+        if (absolute && !end_with_dot && tmp1[tolen - 1] == '.')
bc5dde
+                tolen--;
bc5dde
+
bc5dde
+        if (isc_buffer_length(buffer) < used_org + tolen)
bc5dde
+                return (ISC_R_NOSPACE);
bc5dde
+
bc5dde
+        isc_buffer_subtract(buffer, isc_buffer_usedlength(buffer) - used_org);
bc5dde
+        memcpy(isc_buffer_used(buffer), tmp1, tolen);
bc5dde
+        isc_buffer_add(buffer, tolen);
bc5dde
+
bc5dde
+        return (ISC_R_SUCCESS);
bc5dde
+}
bc5dde
+#endif /* WITH_LIBIDN*/
bc5dde
 
bc5dde
 #ifdef DIG_SIGCHASE
bc5dde
 void
bc5dde
diff -up bind-9.7.0b1/bin/dig/Makefile.in.libidn bind-9.7.0b1/bin/dig/Makefile.in
bc5dde
--- bind-9.7.0b1/bin/dig/Makefile.in.libidn	2009-09-22 10:47:55.000000000 +0200
bc5dde
+++ bind-9.7.0b1/bin/dig/Makefile.in	2009-10-20 10:50:06.201543709 +0200
bc5dde
@@ -46,10 +46,10 @@ DEPLIBS =	${DNSDEPLIBS} ${BIND9DEPLIBS} 
bc5dde
 		${LWRESDEPLIBS}
bc5dde
 
bc5dde
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
bc5dde
-		${ISCLIBS} @IDNLIBS@ @LIBS@
bc5dde
+		${ISCLIBS} @IDNLIBS@ @LIBS@ -lidn
bc5dde
 
bc5dde
 NOSYMLIBS =	${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} ${ISCCFGLIBS} \
bc5dde
-		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@
bc5dde
+		${ISCNOSYMLIBS} @IDNLIBS@ @LIBS@ -lidn
bc5dde
 
bc5dde
 SUBDIRS =
bc5dde
 
bc5dde
@@ -67,6 +67,8 @@ HTMLPAGES =	dig.html host.html nslookup.
bc5dde
 
bc5dde
 MANOBJS =	${MANPAGES} ${HTMLPAGES}
bc5dde
 
bc5dde
+EXT_CFLAGS = -DWITH_LIBIDN
bc5dde
+
bc5dde
 @BIND9_MAKE_RULES@
bc5dde
 
bc5dde
 dig@EXEEXT@: dig.@O@ dighost.@O@ ${UOBJS} ${DEPLIBS}