~ubuntu-branches/ubuntu/feisty/net-tools/feisty

« back to all changes in this revision

Viewing changes to lib/tr.c

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Eckenfels
  • Date: 2001-11-24 06:26:37 UTC
  • Revision ID: james.westby@ubuntu.com-20011124062637-1y96kzx03e8dbi55
Tags: upstream-1.60
ImportĀ upstreamĀ versionĀ 1.60

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * lib/tr.c   This file contains an implementation of the "Tokenring"
 
3
 *              support functions.
 
4
 *
 
5
 * Version:     $Id: tr.c,v 1.8 2000/02/02 08:56:30 freitag 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_HWTR
 
19
#include <asm/types.h>
 
20
#include <sys/types.h>
 
21
#include <sys/socket.h>
 
22
#include <net/if_arp.h>
 
23
#include <linux/if_tr.h>
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <errno.h>
 
27
#include <ctype.h>
 
28
#include <string.h>
 
29
#include <unistd.h>
 
30
#include "net-support.h"
 
31
#include "pathnames.h"
 
32
#include "intl.h"
 
33
 
 
34
extern struct hwtype tr_hwtype;
 
35
 
 
36
static char *pr_tr(unsigned char *ptr)
 
37
{
 
38
    static char buff[64];
 
39
 
 
40
    snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
 
41
             (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
 
42
             (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
 
43
        );
 
44
    return (buff);
 
45
}
 
46
 
 
47
 
 
48
static int in_tr(char *bufp, struct sockaddr *sap)
 
49
{
 
50
    unsigned char *ptr;
 
51
    char c, *orig;
 
52
    int i, val;
 
53
 
 
54
    sap->sa_family = tr_hwtype.type;
 
55
    ptr = sap->sa_data;
 
56
 
 
57
    i = 0;
 
58
    orig = bufp;
 
59
    while ((*bufp != '\0') && (i < TR_ALEN)) {
 
60
        val = 0;
 
61
        c = *bufp++;
 
62
        if (isdigit(c))
 
63
            val = c - '0';
 
64
        else if (c >= 'a' && c <= 'f')
 
65
            val = c - 'a' + 10;
 
66
        else if (c >= 'A' && c <= 'F')
 
67
            val = c - 'A' + 10;
 
68
        else {
 
69
#ifdef DEBUG
 
70
            fprintf(stderr, _("in_tr(%s): invalid token ring address!\n"), orig);
 
71
#endif
 
72
            errno = EINVAL;
 
73
            return (-1);
 
74
        }
 
75
        val <<= 4;
 
76
        c = *bufp++;
 
77
        if (isdigit(c))
 
78
            val |= c - '0';
 
79
        else if (c >= 'a' && c <= 'f')
 
80
            val |= c - 'a' + 10;
 
81
        else if (c >= 'A' && c <= 'F')
 
82
            val |= c - 'A' + 10;
 
83
        else {
 
84
#ifdef DEBUG
 
85
            fprintf(stderr, _("in_tr(%s): invalid token ring address!\n"), orig);
 
86
#endif
 
87
            errno = EINVAL;
 
88
            return (-1);
 
89
        }
 
90
        *ptr++ = (unsigned char) (val & 0377);
 
91
        i++;
 
92
 
 
93
        /* We might get a semicolon here - not required. */
 
94
        if (*bufp == ':') {
 
95
            if (i == TR_ALEN) {
 
96
#ifdef DEBUG
 
97
                fprintf(stderr, _("in_tr(%s): trailing : ignored!\n"),
 
98
                        orig)
 
99
#endif
 
100
                    ;           /* nothing */
 
101
            }
 
102
            bufp++;
 
103
        }
 
104
    }
 
105
 
 
106
    /* That's it.  Any trailing junk? */
 
107
    if ((i == TR_ALEN) && (*bufp != '\0')) {
 
108
#ifdef DEBUG
 
109
        fprintf(stderr, _("in_tr(%s): trailing junk!\n"), orig);
 
110
        errno = EINVAL;
 
111
        return (-1);
 
112
#endif
 
113
    }
 
114
#ifdef DEBUG
 
115
    fprintf(stderr, "in_tr(%s): %s\n", orig, pr_tr(sap->sa_data));
 
116
#endif
 
117
 
 
118
    return (0);
 
119
}
 
120
 
 
121
 
 
122
struct hwtype tr_hwtype =
 
123
{
 
124
    "tr", NULL /* "16/4 Mbps Token Ring" */, ARPHRD_IEEE802, TR_ALEN,
 
125
    pr_tr, in_tr, NULL
 
126
};
 
127
#ifdef ARPHRD_IEEE802_TR
 
128
struct hwtype tr_hwtype1 =
 
129
{
 
130
    "tr", NULL /* "16/4 Mbps Token Ring" */, ARPHRD_IEEE802_TR, TR_ALEN,
 
131
    pr_tr, in_tr, NULL
 
132
};
 
133
#endif
 
134
 
 
135
 
 
136
#endif                          /* HAVE_HWTR */