1/* Stolen from BIND */ 2 3/* 4 * Copyright (c) 1985 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36/* 37 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 38 * 39 * Permission to use, copy, modify, and distribute this software for any 40 * purpose with or without fee is hereby granted, provided that the above 41 * copyright notice and this permission notice appear in all copies, and that 42 * the name of Digital Equipment Corporation not be used in advertising or 43 * publicity pertaining to distribution of the document or software without 44 * specific, written prior permission. 45 * 46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 53 * SOFTWARE. 54 */ 55 56/* 57 * Portions Copyright (c) 1995 by International Business Machines, Inc. 58 * 59 * International Business Machines, Inc. (hereinafter called IBM) grants 60 * permission under its copyrights to use, copy, modify, and distribute this 61 * Software with or without fee, provided that the above copyright notice and 62 * all paragraphs of this notice appear in all copies, and that the name of IBM 63 * not be used in connection with the marketing of any product incorporating 64 * the Software or modifications thereof, without specific, written prior 65 * permission. 66 * 67 * To the extent it has a right to do so, IBM grants an immunity from suit 68 * under its patents, if any, for the use, sale or manufacture of products to 69 * the extent that such products are used for performing Domain Name System 70 * dynamic updates in TCP/IP networks by means of the Software. No immunity is 71 * granted for any product per se or for any other function of any product. 72 * 73 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, 74 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 75 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, 76 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING 77 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN 78 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. 79 */ 80 81/* 82 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 83 * 84 * Permission to use, copy, modify, and distribute this software for any 85 * purpose with or without fee is hereby granted, provided that the above 86 * copyright notice and this permission notice appear in all copies. 87 * 88 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 89 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 90 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 91 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 92 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 93 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 94 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 95 * SOFTWARE. 96 */ 97 98#include <sys/types.h> 99#include <sys/param.h> 100#include <sys/socket.h> 101 102#include <netinet/in.h> 103#include <arpa/inet.h> 104#include <arpa/nameser.h> 105 106#include <ctype.h> 107#include <errno.h> 108#include <math.h> 109#include <netdb.h> 110#include <resolv.h> 111#include <stdio.h> 112#include <stdlib.h> 113#include <string.h> 114#include <time.h> 115 116#include "loc.h" 117 118const char *precsize_ntoa(); 119 120/* takes an on-the-wire LOC RR and formats it in a human readable format. */ 121const char * 122loc_ntoa(binary, ascii) 123 const u_char *binary; 124 char *ascii; 125{ 126 static char *error = "?"; 127 static char tmpbuf[sizeof 128"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"]; 129 const u_char *cp = binary; 130 131 int latdeg, latmin, latsec, latsecfrac; 132 int longdeg, longmin, longsec, longsecfrac; 133 char northsouth, eastwest; 134 int altmeters, altfrac, altsign; 135 136 const u_int32_t referencealt = 100000 * 100; 137 138 int32_t latval, longval, altval; 139 u_int32_t templ; 140 u_int8_t sizeval, hpval, vpval, versionval; 141 142 char *sizestr, *hpstr, *vpstr; 143 144 versionval = *cp++; 145 146 if (ascii == NULL) 147 ascii = tmpbuf; 148 149 if (versionval) { 150 (void) sprintf(ascii, "; error: unknown LOC RR version"); 151 return (ascii); 152 } 153 154 sizeval = *cp++; 155 156 hpval = *cp++; 157 vpval = *cp++; 158 159 GETLONG(templ, cp); 160 latval = (templ - ((unsigned)1<<31)); 161 162 GETLONG(templ, cp); 163 longval = (templ - ((unsigned)1<<31)); 164 165 GETLONG(templ, cp); 166 if (templ < referencealt) { /* below WGS 84 spheroid */ 167 altval = referencealt - templ; 168 altsign = -1; 169 } else { 170 altval = templ - referencealt; 171 altsign = 1; 172 } 173 174 if (latval < 0) { 175 northsouth = 'S'; 176 latval = -latval; 177 } else 178 northsouth = 'N'; 179 180 latsecfrac = latval % 1000; 181 latval = latval / 1000; 182 latsec = latval % 60; 183 latval = latval / 60; 184 latmin = latval % 60; 185 latval = latval / 60; 186 latdeg = latval; 187 188 if (longval < 0) { 189 eastwest = 'W'; 190 longval = -longval; 191 } else 192 eastwest = 'E'; 193 194 longsecfrac = longval % 1000; 195 longval = longval / 1000; 196 longsec = longval % 60; 197 longval = longval / 60; 198 longmin = longval % 60; 199 longval = longval / 60; 200 longdeg = longval; 201 202 altfrac = altval % 100; 203 altmeters = (altval / 100) * altsign; 204 205 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL) 206 sizestr = error; 207 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL) 208 hpstr = error; 209 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL) 210 vpstr = error; 211 212 sprintf(ascii, 213 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm", 214 latdeg, latmin, latsec, latsecfrac, northsouth, 215 longdeg, longmin, longsec, longsecfrac, eastwest, 216 altmeters, altfrac, sizestr, hpstr, vpstr); 217 218 if (sizestr != error) 219 free(sizestr); 220 if (hpstr != error) 221 free(hpstr); 222 if (vpstr != error) 223 free(vpstr); 224 225 return (ascii); 226} 227 228static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000, 229 1000000,10000000,100000000,1000000000}; 230 231/* takes an XeY precision/size value, returns a string representation. */ 232const char * 233precsize_ntoa(prec) 234 u_int8_t prec; 235{ 236 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */ 237 unsigned long val; 238 int mantissa, exponent; 239 240 mantissa = (int)((prec >> 4) & 0x0f) % 10; 241 exponent = (int)((prec >> 0) & 0x0f) % 10; 242 243 val = mantissa * poweroften[exponent]; 244 245 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100); 246 return (retbuf); 247} 248 249