Path: blob/main/crypto/heimdal/lib/wind/idn-lookup.c
34907 views
/*1* Copyright (c) 2004 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#ifdef HAVE_CONFIG_H34#include <config.h>35#endif36#include <assert.h>37#include <err.h>38#include <netdb.h>39#include <stdio.h>40#include <stdlib.h>41#include <string.h>42#include <sys/socket.h>43#include <sys/types.h>4445#include <getarg.h>46#include <roken.h>4748#include "windlocl.h"4950static int version_flag = 0;51static int help_flag = 0;525354static int55is_separator(uint32_t u)56{57return u == 0x002E || u == 0x3002;58}5960static void61lookup(const char *name)62{63unsigned i;64char encoded[1024];65char *ep;66int ret;67struct addrinfo hints;68struct addrinfo *ai;6970size_t u_len = strlen(name);71uint32_t *u = malloc(u_len * sizeof(uint32_t));72size_t norm_len = u_len * 2;73uint32_t *norm = malloc(norm_len * sizeof(uint32_t));7475if (u == NULL && u_len != 0)76errx(1, "malloc failed");77if (norm == NULL && norm_len != 0)78errx(1, "malloc failed");7980ret = wind_utf8ucs4(name, u, &u_len);81if (ret)82errx(1, "utf8 conversion failed");83ret = wind_stringprep(u, u_len, norm, &norm_len, WIND_PROFILE_NAME);84if (ret)85errx(1, "stringprep failed");86free(u);8788ep = encoded;89for (i = 0; i < norm_len; ++i) {90unsigned j;91size_t len;9293for (j = i; j < norm_len && !is_separator(norm[j]); ++j)94;95len = sizeof(encoded) - (ep - encoded);96ret = wind_punycode_label_toascii(norm + i, j - i, ep, &len);97if (ret)98errx(1, "punycode failed");99100ep += len;101*ep++ = '.';102i = j;103}104*ep = '\0';105free(norm);106107printf("Converted \"%s\" into \"%s\"\n", name, encoded);108109memset(&hints, 0, sizeof(hints));110hints.ai_flags = AI_CANONNAME;111ret = getaddrinfo(encoded, NULL, &hints, &ai);112if(ret)113errx(1, "getaddrinfo failed: %s", gai_strerror(ret));114printf("canonical-name: %s\n", ai->ai_canonname);115freeaddrinfo(ai);116}117118static struct getargs args[] = {119{"version", 0, arg_flag, &version_flag,120"print version", NULL },121{"help", 0, arg_flag, &help_flag,122NULL, NULL }123};124125static void126usage (int ret)127{128arg_printusage(args, sizeof(args)/sizeof(args[0]), NULL,129"dns-names ...");130exit (ret);131}132133int134main(int argc, char **argv)135{136int optidx = 0;137unsigned i;138139setprogname (argv[0]);140141if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))142usage(1);143144if (help_flag)145usage (0);146147if(version_flag){148print_version(NULL);149exit(0);150}151152argc -= optidx;153argv += optidx;154155if (argc == 0)156usage(1);157158for (i = 0; i < argc; ++i) {159if (argv[i][0]) /* Quiet lint */160lookup(argv[i]);161}162return 0;163}164165166