1 /* $NetBSD: compat_ns_addr.c,v 1.5 2025/12/16 12:39:01 nia Exp $ */ 2 3 /* 4 * Copyright (c) 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * J.Q. Johnson. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93"; 39 #else 40 __RCSID("$NetBSD: compat_ns_addr.c,v 1.5 2025/12/16 12:39:01 nia Exp $"); 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include "namespace.h" 45 #include <sys/param.h> 46 #include <sys/endian.h> 47 #include <compat/include/ns.h> 48 49 #include <assert.h> 50 #include <stdio.h> 51 #include <string.h> 52 53 static void Field(char *, uint8_t *, int); 54 static void cvtbase(long, int, int[], int, uint8_t [], int); 55 56 struct ns_addr 57 ns_addr(const char *name) 58 { 59 char separator; 60 char *hostname, *socketname, *cp; 61 char buf[50]; 62 static struct ns_addr addr; 63 64 _DIAGASSERT(name != NULL); 65 66 (void)strlcpy(buf, name, sizeof(buf)); 67 68 /* 69 * First, figure out what he intends as a field separator. 70 * Despite the way this routine is written, the preferred 71 * form 2-272.AA001234H.01777, i.e. XDE standard. 72 * Great efforts are made to ensure backward compatibility. 73 */ 74 if ((hostname = strchr(buf, '#')) != NULL) 75 separator = '#'; 76 else { 77 hostname = strchr(buf, '.'); 78 if ((cp = strchr(buf, ':')) && 79 ((hostname && cp < hostname) || (hostname == 0))) { 80 hostname = cp; 81 separator = ':'; 82 } else 83 separator = '.'; 84 } 85 if (hostname) 86 *hostname++ = 0; 87 88 memset(&addr, '\0', sizeof(addr)); 89 Field(buf, addr.x_net.c_net, 4); 90 if (hostname == 0) 91 return (addr); /* No separator means net only */ 92 93 socketname = strchr(hostname, separator); 94 if (socketname) { 95 *socketname++ = 0; 96 Field(socketname, (uint8_t *)(void *)&addr.x_port, 2); 97 } 98 99 Field(hostname, addr.x_host.c_host, 6); 100 101 return (addr); 102 } 103 104 static void 105 Field(char *buf, uint8_t *out, int len) 106 { 107 register char *bp = buf; 108 int i, ibase, base16 = 0, base10 = 0; 109 unsigned int clen = 0; 110 int hb[6], *hp; 111 112 _DIAGASSERT(buf != NULL); 113 _DIAGASSERT(out != NULL); 114 115 /* 116 * first try 2-273#2-852-151-014#socket 117 */ 118 if ((*buf != '-') && 119 (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", 120 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { 121 cvtbase(1000L, 256, hb, i, out, len); 122 return; 123 } 124 /* 125 * try form 8E1#0.0.AA.0.5E.E6#socket 126 */ 127 if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", 128 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { 129 cvtbase(256L, 256, hb, i, out, len); 130 return; 131 } 132 /* 133 * try form 8E1#0:0:AA:0:5E:E6#socket 134 */ 135 if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", 136 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { 137 cvtbase(256L, 256, hb, i, out, len); 138 return; 139 } 140 /* 141 * This is REALLY stretching it but there was a 142 * comma notation separating shorts -- definitely non standard 143 */ 144 if (1 < (i = sscanf(buf,"%x,%x,%x", 145 &hb[0], &hb[1], &hb[2]))) { 146 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); 147 hb[2] = htons(hb[2]); 148 cvtbase(65536L, 256, hb, i, out, len); 149 return; 150 } 151 152 /* Need to decide if base 10, 16 or 8 */ 153 while (*bp) switch (*bp++) { 154 155 case '0': case '1': case '2': case '3': case '4': case '5': 156 case '6': case '7': case '-': 157 break; 158 159 case '8': case '9': 160 base10 = 1; 161 break; 162 163 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 164 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 165 base16 = 1; 166 break; 167 168 case 'x': case 'X': 169 *--bp = '0'; 170 base16 = 1; 171 break; 172 173 case 'h': case 'H': 174 base16 = 1; 175 /* FALLTHROUGH */ 176 177 default: 178 *--bp = 0; /* Ends Loop */ 179 } 180 if (base16) { 181 ibase = 4096; 182 } else if (base10 == 0 && *buf == '0') { 183 ibase = 512; 184 } else { 185 base10 = 1; 186 ibase = 1000; 187 } 188 189 for (bp = buf; *bp++; ) clen++; 190 if (clen == 0) clen++; 191 if (clen > 18) clen = 18; 192 i = ((clen - 1) / 3) + 1; 193 bp = clen + buf - 3; 194 hp = hb + i - 1; 195 196 while (hp > hb) { 197 if (base16) 198 (void)sscanf(bp, "%3x", hp); 199 else if (base10) 200 (void)sscanf(bp, "%3d", hp); 201 else 202 (void)sscanf(bp, "%3o", hp); 203 204 bp[0] = 0; 205 hp--; 206 bp -= 3; 207 } 208 if (base16) 209 (void)sscanf(buf, "%3x", hp); 210 else if (base10) 211 (void)sscanf(buf, "%3d", hp); 212 else 213 (void)sscanf(buf, "%3o", hp); 214 215 cvtbase((long)ibase, 256, hb, i, out, len); 216 } 217 218 static void 219 cvtbase(long oldbase, int newbase, int input[], int inlen, 220 uint8_t result[], int reslen) 221 { 222 int d, e; 223 long sum; 224 225 _DIAGASSERT(input != NULL); 226 _DIAGASSERT(result != NULL); 227 _DIAGASSERT(inlen > 0); 228 229 e = 1; 230 while (e > 0 && reslen > 0) { 231 d = 0; e = 0; sum = 0; 232 /* long division: input=input/newbase */ 233 while (d < inlen) { 234 sum = sum*oldbase + (long) input[d]; 235 e += (sum > 0); 236 input[d++] = (int) (sum / newbase); 237 sum %= newbase; 238 } 239 /* accumulate remainder */ 240 result[--reslen] = (unsigned char)sum; 241 } 242 for (d=0; d < reslen; d++) 243 result[d] = 0; 244 } 245