~ubuntu-branches/ubuntu/warty/net-tools/warty

« back to all changes in this revision

Viewing changes to lib/eui64.c

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Eckenfels
  • Date: 2004-04-23 00:57:20 UTC
  • Revision ID: james.westby@ubuntu.com-20040423005720-q36dnihblq1oa5j3
Tags: 1.60-10
* typo fix in po/de.po for german arp command output (Closes: Bug #176151)
* added diagnostics messages to mii-tool.8 (Closes: Bug #239229)
* new version of nstrcmp (Closes: Bug #226503)
* enable EUI64 support
* stadanrds version 3.6.1 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * lib/eui64.c  This file contains support for generic EUI-64 hw addressing
 
3
 *
 
4
 * Version:     $Id: eui64.c,v 1.1 2001/11/12 02:12:05 ecki Exp $
 
5
 *
 
6
 * Author:      Daniel Stodden <stodden@in.tum.de>
 
7
 *              Copyright 2001 Daniel Stodden
 
8
 *
 
9
 *              blueprinted from ether.c 
 
10
 *              Copyright 1993 MicroWalt Corporation
 
11
 *
 
12
 *              This program is free software; you can redistribute it
 
13
 *              and/or  modify it under  the terms of  the GNU General
 
14
 *              Public  License as  published  by  the  Free  Software
 
15
 *              Foundation;  either  version 2 of the License, or  (at
 
16
 *              your option) any later version.
 
17
 */
 
18
#include "config.h"
 
19
 
 
20
#if HAVE_HWEUI64
 
21
 
 
22
#include <sys/types.h>
 
23
#include <sys/ioctl.h>
 
24
#include <sys/socket.h>
 
25
#include <net/if_arp.h>
 
26
#include <stdlib.h>
 
27
#include <stdio.h>
 
28
#include <ctype.h>
 
29
#include <errno.h>
 
30
#include <fcntl.h>
 
31
#include <string.h>
 
32
#include <termios.h>
 
33
#include <unistd.h>
 
34
#include "net-support.h"
 
35
#include "pathnames.h"
 
36
#include "intl.h"
 
37
 
 
38
/*
 
39
 * EUI-64 constants
 
40
 */
 
41
 
 
42
#define EUI64_ALEN      8
 
43
 
 
44
#ifndef ARPHRD_EUI64
 
45
#define ARPHRD_EUI64    27
 
46
#warning "ARPHRD_EUI64 not defined in <net/if_arp.h>. Using private value 27"
 
47
#endif
 
48
 
 
49
struct hwtype eui64_hwtype;
 
50
 
 
51
/* Display an EUI-64 address in readable format. */
 
52
static char *pr_eui64( unsigned char *ptr )
 
53
{
 
54
        static char buff[64];
 
55
 
 
56
        snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
 
57
                 (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377), (ptr[3] & 0377), 
 
58
                 (ptr[4] & 0377), (ptr[5] & 0377), (ptr[6] & 0377), (ptr[7] & 0377)
 
59
                );
 
60
        return (buff);
 
61
}
 
62
 
 
63
/* Start the PPP encapsulation on the file descriptor. */
 
64
static int in_eui64( char *bufp, struct sockaddr *sap )
 
65
{
 
66
        unsigned char *ptr;
 
67
        char c, *orig;
 
68
        int i;
 
69
        unsigned val;
 
70
 
 
71
        sap->sa_family = eui64_hwtype.type;
 
72
        ptr = sap->sa_data;
 
73
 
 
74
        i = 0;
 
75
        orig = bufp;
 
76
        
 
77
        while ((*bufp != '\0') && (i < EUI64_ALEN)) {
 
78
                val = 0;
 
79
                c = *bufp++;
 
80
                if (isdigit(c))
 
81
                        val = c - '0';
 
82
                else if (c >= 'a' && c <= 'f')
 
83
                        val = c - 'a' + 10;
 
84
                else if (c >= 'A' && c <= 'F')
 
85
                        val = c - 'A' + 10;
 
86
                else {
 
87
#ifdef DEBUG
 
88
                        fprintf( stderr, _("in_eui64(%s): invalid eui64 address!\n"), 
 
89
                                 orig );
 
90
#endif
 
91
                        errno = EINVAL;
 
92
                        return (-1);
 
93
                }
 
94
 
 
95
                val <<= 4;
 
96
                c = *bufp;
 
97
                if (isdigit(c))
 
98
                        val |= c - '0';
 
99
                else if (c >= 'a' && c <= 'f')
 
100
                        val |= c - 'a' + 10;
 
101
                else if (c >= 'A' && c <= 'F')
 
102
                        val |= c - 'A' + 10;
 
103
                else if (c == ':' || c == 0)
 
104
                        val >>= 4;
 
105
                else {
 
106
#ifdef DEBUG
 
107
                        fprintf( stderr, _("in_eui64(%s): invalid eui64 address!\n"), 
 
108
                                 orig );
 
109
#endif
 
110
                        errno = EINVAL;
 
111
                        return (-1);
 
112
                }
 
113
 
 
114
                if (c != 0)
 
115
                        bufp++;
 
116
 
 
117
                *ptr++ = (unsigned char) (val & 0377);
 
118
                i++;
 
119
                
 
120
                /* We might get a semicolon here - not required. */
 
121
                if (*bufp == ':') {
 
122
                        if (i == EUI64_ALEN) {
 
123
#ifdef DEBUG
 
124
                                fprintf(stderr, _("in_eui64(%s): trailing : ignored!\n"),
 
125
                                        orig)
 
126
#endif
 
127
                                        ; /* nothing */
 
128
                        }
 
129
                        bufp++;
 
130
                }
 
131
        }
 
132
 
 
133
        /* That's it.  Any trailing junk? */
 
134
        if ((i == EUI64_ALEN) && (*bufp != '\0')) {
 
135
#ifdef DEBUG
 
136
                fprintf(stderr, _("in_eui64(%s): trailing junk!\n"), orig);
 
137
                errno = EINVAL;
 
138
                return (-1);
 
139
#endif
 
140
        }
 
141
#ifdef DEBUG
 
142
        fprintf(stderr, "in_eui64(%s): %s\n", orig, pr_eui64(sap->sa_data));
 
143
#endif
 
144
 
 
145
    return (0);
 
146
}
 
147
 
 
148
struct hwtype eui64_hwtype =
 
149
{
 
150
        "eui64", NULL, /*"EUI-64 addressing", */ ARPHRD_EUI64, EUI64_ALEN,
 
151
        pr_eui64, in_eui64, NULL, 0
 
152
};
 
153
 
 
154
 
 
155
#endif                          /* HAVE_EUI64 */