~ubuntu-branches/ubuntu/wily/edk2/wily

« back to all changes in this revision

Viewing changes to StdLib/BsdSocketLib/ns_addr.c

  • Committer: Package Import Robot
  • Author(s): Steve Langasek
  • Date: 2013-02-10 13:11:25 UTC
  • Revision ID: package-import@ubuntu.com-20130210131125-0zwkb8f8m4ecia4m
Tags: upstream-0~20121205.edae8d2d
ImportĀ upstreamĀ versionĀ 0~20121205.edae8d2d

Show diffs side-by-side

added added

removed removed

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