source: rtems/cpukit/libnetworking/libc/linkaddr.c @ 39f13cb

4.104.115
Last change on this file since 39f13cb was 39f13cb, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/27/10 at 04:00:53

Add HAVE_STRINGS_H for better POSIX compliance.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*-
2 * Copyright (c) 1990, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the University of
16 *      California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *  $Id$
34 */
35
36#if HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <net/if_dl.h>
43#include <string.h>
44#ifdef HAVE_STRINGS_H
45#include <strings.h>
46#endif
47
48/* States*/
49#define NAMING  0
50#define GOTONE  1
51#define GOTTWO  2
52#define RESET   3
53/* Inputs */
54#define DIGIT   (4*0)
55#define END     (4*1)
56#define DELIM   (4*2)
57#define LETTER  (4*3)
58
59void
60link_addr(
61        const char *addr,
62        struct sockaddr_dl *sdl)
63{
64        char *cp = sdl->sdl_data;
65        char *cplim = sdl->sdl_len + (char *)sdl;
66        int byte = 0, state = NAMING,
67                new=0; /* new=0 to avoid warning */
68
69        bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1);
70        sdl->sdl_family = AF_LINK;
71        do {
72                state &= ~LETTER;
73                if ((*addr >= '0') && (*addr <= '9')) {
74                        new = *addr - '0';
75                } else if ((*addr >= 'a') && (*addr <= 'f')) {
76                        new = *addr - 'a' + 10;
77                } else if ((*addr >= 'A') && (*addr <= 'F')) {
78                        new = *addr - 'A' + 10;
79                } else if (*addr == 0) {
80                        state |= END;
81                } else if (state == NAMING &&
82                           (((*addr >= 'A') && (*addr <= 'Z')) ||
83                           ((*addr >= 'a') && (*addr <= 'z'))))
84                        state |= LETTER;
85                else
86                        state |= DELIM;
87                addr++;
88                switch (state /* | INPUT */) {
89                case NAMING | DIGIT:
90                case NAMING | LETTER:
91                        *cp++ = addr[-1];
92                        continue;
93                case NAMING | DELIM:
94                        state = RESET;
95                        sdl->sdl_nlen = cp - sdl->sdl_data;
96                        continue;
97                case GOTTWO | DIGIT:
98                        *cp++ = byte;
99                        /* FALLTHROUGH */
100                case RESET | DIGIT:
101                        state = GOTONE;
102                        byte = new;
103                        continue;
104                case GOTONE | DIGIT:
105                        state = GOTTWO;
106                        byte = new + (byte << 4);
107                        continue;
108                default: /* | DELIM */
109                        state = RESET;
110                        *cp++ = byte;
111                        byte = 0;
112                        continue;
113                case GOTONE | END:
114                case GOTTWO | END:
115                        *cp++ = byte;
116                        /* FALLTHROUGH */
117                case RESET | END:
118                        break;
119                }
120                break;
121        } while (cp < cplim);
122        sdl->sdl_alen = cp - LLADDR(sdl);
123        new = cp - (char *)sdl;
124        if (new > sizeof(*sdl))
125                sdl->sdl_len = new;
126        return;
127}
128
129static char hexlist[] = "0123456789abcdef";
130
131char *
132link_ntoa(
133        const struct sockaddr_dl *sdl)
134{
135        static char obuf[64];
136        char *out = obuf;
137        int i;
138        u_char *in = (u_char *)LLADDR(sdl);
139        u_char *inlim = in + sdl->sdl_alen;
140        int firsttime = 1;
141
142        if (sdl->sdl_nlen) {
143                memcpy(obuf, sdl->sdl_data, sdl->sdl_nlen);
144                out += sdl->sdl_nlen;
145                if (sdl->sdl_alen)
146                        *out++ = ':';
147        }
148        while (in < inlim) {
149                if (firsttime)
150                        firsttime = 0;
151                else
152                        *out++ = '.';
153                i = *in++;
154                if (i > 0xf) {
155                        out[1] = hexlist[i & 0xf];
156                        i >>= 4;
157                        out[0] = hexlist[i];
158                        out += 2;
159                } else
160                        *out++ = hexlist[i];
161        }
162        *out = 0;
163        return (obuf);
164}
Note: See TracBrowser for help on using the repository browser.