OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [route/] [lib/] [ether.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 simons
/*
2
 * lib/ether.c        This file contains an implementation of the "Ethernet"
3
 *              support functions.
4
 *
5
 * Version:     $Id: ether.c,v 1.1 2002-03-17 19:58:53 simons Exp $
6
 *
7
 * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
8
 *              Copyright 1993 MicroWalt Corporation
9
 *
10
 *              This program is free software; you can redistribute it
11
 *              and/or  modify it under  the terms of  the GNU General
12
 *              Public  License as  published  by  the  Free  Software
13
 *              Foundation;  either  version 2 of the License, or  (at
14
 *              your option) any later version.
15
 */
16
#include "config.h"
17
 
18
#if HAVE_HWETHER
19
#include <sys/types.h>
20
#include <sys/socket.h>
21
#include <net/if_arp.h>
22
#include <linux/if_ether.h>
23
#include <stdlib.h>
24
#include <stdio.h>
25
#include <errno.h>
26
#include <ctype.h>
27
#include <string.h>
28
#include <unistd.h>
29
#include "net-support.h"
30
#include "pathnames.h"
31
#include "intl.h"
32
#include "util.h"
33
 
34
extern struct hwtype ether_hwtype;
35
 
36
 
37
/* Display an Ethernet address in readable format. */
38
static char *pr_ether(unsigned char *ptr)
39
{
40
    static char buff[64];
41
 
42
#ifdef EMBED
43
    sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
44
             (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
45
             (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
46
        );
47
#else
48
    snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
49
             (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
50
             (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
51
        );
52
#endif
53
    return (buff);
54
}
55
 
56
 
57
/* Display an Ethernet socket address. */
58
static char *pr_sether(struct sockaddr *sap)
59
{
60
    static char buf[64];
61
 
62
    if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
63
        return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
64
    return (pr_ether(sap->sa_data));
65
}
66
 
67
 
68
/* Input an Ethernet address and convert to binary. */
69
static int in_ether(char *bufp, struct sockaddr *sap)
70
{
71
    unsigned char *ptr;
72
    char c, *orig;
73
    int i;
74
    unsigned val;
75
 
76
    sap->sa_family = ether_hwtype.type;
77
    ptr = sap->sa_data;
78
 
79
    i = 0;
80
    orig = bufp;
81
    while ((*bufp != '\0') && (i < ETH_ALEN)) {
82
        val = 0;
83
        c = *bufp++;
84
        if (isdigit(c))
85
            val = c - '0';
86
        else if (c >= 'a' && c <= 'f')
87
            val = c - 'a' + 10;
88
        else if (c >= 'A' && c <= 'F')
89
            val = c - 'A' + 10;
90
        else {
91
#ifdef DEBUG
92
            fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
93
#endif
94
            errno = EINVAL;
95
            return (-1);
96
        }
97
        val <<= 4;
98
        c = *bufp;
99
        if (isdigit(c))
100
            val |= c - '0';
101
        else if (c >= 'a' && c <= 'f')
102
            val |= c - 'a' + 10;
103
        else if (c >= 'A' && c <= 'F')
104
            val |= c - 'A' + 10;
105
        else if (c == ':' || c == 0)
106
            val >>= 4;
107
        else {
108
#ifdef DEBUG
109
            fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), orig);
110
#endif
111
            errno = EINVAL;
112
            return (-1);
113
        }
114
        if (c != 0)
115
            bufp++;
116
        *ptr++ = (unsigned char) (val & 0377);
117
        i++;
118
 
119
        /* We might get a semicolon here - not required. */
120
        if (*bufp == ':') {
121
            if (i == ETH_ALEN) {
122
#ifdef DEBUG
123
                fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"),
124
                        orig)
125
#endif
126
                    ;           /* nothing */
127
            }
128
            bufp++;
129
        }
130
    }
131
 
132
    /* That's it.  Any trailing junk? */
133
    if ((i == ETH_ALEN) && (*bufp != '\0')) {
134
#ifdef DEBUG
135
        fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig);
136
        errno = EINVAL;
137
        return (-1);
138
#endif
139
    }
140
#ifdef DEBUG
141
    fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
142
#endif
143
 
144
    return (0);
145
}
146
 
147
 
148
struct hwtype ether_hwtype =
149
{
150
    "ether", NULL, /*"10Mbps Ethernet", */ ARPHRD_ETHER, ETH_ALEN,
151
    pr_ether, pr_sether, in_ether, NULL
152
};
153
 
154
 
155
#endif                          /* HAVE_HWETHER */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.