1226031Sstas/* 2226031Sstas * Copyright (c) 2004 Kungliga Tekniska H��gskolan 3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4226031Sstas * All rights reserved. 5226031Sstas * 6226031Sstas * Redistribution and use in source and binary forms, with or without 7226031Sstas * modification, are permitted provided that the following conditions 8226031Sstas * are met: 9226031Sstas * 10226031Sstas * 1. Redistributions of source code must retain the above copyright 11226031Sstas * notice, this list of conditions and the following disclaimer. 12226031Sstas * 13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright 14226031Sstas * notice, this list of conditions and the following disclaimer in the 15226031Sstas * documentation and/or other materials provided with the distribution. 16226031Sstas * 17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors 18226031Sstas * may be used to endorse or promote products derived from this software 19226031Sstas * without specific prior written permission. 20226031Sstas * 21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24226031Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31226031Sstas * SUCH DAMAGE. 32226031Sstas */ 33226031Sstas 34226031Sstas#ifdef HAVE_CONFIG_H 35226031Sstas#include <config.h> 36226031Sstas#endif 37226031Sstas#include <assert.h> 38226031Sstas#include <err.h> 39226031Sstas#include <netdb.h> 40226031Sstas#include <stdio.h> 41226031Sstas#include <stdlib.h> 42226031Sstas#include <string.h> 43226031Sstas#include <sys/socket.h> 44226031Sstas#include <sys/types.h> 45226031Sstas 46226031Sstas#include <getarg.h> 47226031Sstas#include <roken.h> 48226031Sstas 49226031Sstas#include "windlocl.h" 50226031Sstas 51226031Sstasstatic int version_flag = 0; 52226031Sstasstatic int help_flag = 0; 53226031Sstas 54226031Sstas 55226031Sstasstatic int 56226031Sstasis_separator(uint32_t u) 57226031Sstas{ 58226031Sstas return u == 0x002E || u == 0x3002; 59226031Sstas} 60226031Sstas 61226031Sstasstatic void 62226031Sstaslookup(const char *name) 63226031Sstas{ 64226031Sstas unsigned i; 65226031Sstas char encoded[1024]; 66226031Sstas char *ep; 67226031Sstas int ret; 68226031Sstas struct addrinfo hints; 69226031Sstas struct addrinfo *ai; 70226031Sstas 71226031Sstas size_t u_len = strlen(name); 72226031Sstas uint32_t *u = malloc(u_len * sizeof(uint32_t)); 73226031Sstas size_t norm_len = u_len * 2; 74226031Sstas uint32_t *norm = malloc(norm_len * sizeof(uint32_t)); 75226031Sstas 76226031Sstas if (u == NULL && u_len != 0) 77226031Sstas errx(1, "malloc failed"); 78226031Sstas if (norm == NULL && norm_len != 0) 79226031Sstas errx(1, "malloc failed"); 80226031Sstas 81226031Sstas ret = wind_utf8ucs4(name, u, &u_len); 82226031Sstas if (ret) 83226031Sstas errx(1, "utf8 conversion failed"); 84226031Sstas ret = wind_stringprep(u, u_len, norm, &norm_len, WIND_PROFILE_NAME); 85226031Sstas if (ret) 86226031Sstas errx(1, "stringprep failed"); 87226031Sstas free(u); 88226031Sstas 89226031Sstas ep = encoded; 90226031Sstas for (i = 0; i < norm_len; ++i) { 91226031Sstas unsigned j; 92226031Sstas size_t len; 93226031Sstas 94226031Sstas for (j = i; j < norm_len && !is_separator(norm[j]); ++j) 95226031Sstas ; 96226031Sstas len = sizeof(encoded) - (ep - encoded); 97226031Sstas ret = wind_punycode_label_toascii(norm + i, j - i, ep, &len); 98226031Sstas if (ret) 99226031Sstas errx(1, "punycode failed"); 100226031Sstas 101226031Sstas ep += len; 102226031Sstas *ep++ = '.'; 103226031Sstas i = j; 104226031Sstas } 105226031Sstas *ep = '\0'; 106226031Sstas free(norm); 107226031Sstas 108226031Sstas printf("Converted \"%s\" into \"%s\"\n", name, encoded); 109226031Sstas 110226031Sstas memset(&hints, 0, sizeof(hints)); 111226031Sstas hints.ai_flags = AI_CANONNAME; 112226031Sstas ret = getaddrinfo(encoded, NULL, &hints, &ai); 113226031Sstas if(ret) 114226031Sstas errx(1, "getaddrinfo failed: %s", gai_strerror(ret)); 115226031Sstas printf("canonical-name: %s\n", ai->ai_canonname); 116226031Sstas freeaddrinfo(ai); 117226031Sstas} 118226031Sstas 119226031Sstasstatic struct getargs args[] = { 120226031Sstas {"version", 0, arg_flag, &version_flag, 121226031Sstas "print version", NULL }, 122226031Sstas {"help", 0, arg_flag, &help_flag, 123226031Sstas NULL, NULL } 124226031Sstas}; 125226031Sstas 126226031Sstasstatic void 127226031Sstasusage (int ret) 128226031Sstas{ 129226031Sstas arg_printusage(args, sizeof(args)/sizeof(args[0]), NULL, 130226031Sstas "dns-names ..."); 131226031Sstas exit (ret); 132226031Sstas} 133226031Sstas 134226031Sstasint 135226031Sstasmain(int argc, char **argv) 136226031Sstas{ 137226031Sstas int optidx = 0; 138226031Sstas unsigned i; 139226031Sstas 140226031Sstas setprogname (argv[0]); 141226031Sstas 142226031Sstas if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) 143226031Sstas usage(1); 144226031Sstas 145226031Sstas if (help_flag) 146226031Sstas usage (0); 147226031Sstas 148226031Sstas if(version_flag){ 149226031Sstas print_version(NULL); 150226031Sstas exit(0); 151226031Sstas } 152226031Sstas 153226031Sstas argc -= optidx; 154226031Sstas argv += optidx; 155226031Sstas 156226031Sstas if (argc == 0) 157226031Sstas usage(1); 158226031Sstas 159226031Sstas for (i = 0; i < argc; ++i) 160226031Sstas lookup(argv[i]); 161226031Sstas return 0; 162226031Sstas} 163