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