arcnet.c 2.95 KB
Newer Older
Phil Blundell's avatar
Phil Blundell committed
1
/*
2 3
 * lib/arcnet.c       This file contains an implementation of the "ARCnet"
 *              support functions for the NET-2 base distribution.
Phil Blundell's avatar
Phil Blundell committed
4
 *
5
 * Version:     $Id: arcnet.c,v 1.6 2000/03/05 11:26:02 philip Exp $
Phil Blundell's avatar
Phil Blundell committed
6
 *
7 8
 * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
 *              Copyright 1993 MicroWalt Corporation
Phil Blundell's avatar
Phil Blundell committed
9
 *
10 11 12 13 14
 *              This program is free software; you can redistribute it
 *              and/or  modify it under  the terms of  the GNU General
 *              Public  License as  published  by  the  Free  Software
 *              Foundation;  either  version 2 of the License, or  (at
 *              your option) any later version.
Phil Blundell's avatar
Phil Blundell committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 */
#include "config.h"

#if HAVE_HWARC
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <linux/if_ether.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include "net-support.h"
#include "pathnames.h"
31
#include "intl.h"
32
#include "util.h"
Phil Blundell's avatar
Phil Blundell committed
33 34 35 36 37

extern struct hwtype arcnet_hwtype;


/* Display an ARCnet address in readable format. */
38
static const char *pr_arcnet(const char *ptr)
Phil Blundell's avatar
Phil Blundell committed
39
{
40
    static char buff[64];
Phil Blundell's avatar
Phil Blundell committed
41

42 43
    snprintf(buff, sizeof(buff), "%02X", (ptr[0] & 0377));
    return (buff);
Phil Blundell's avatar
Phil Blundell committed
44 45
}

46 47 48 49 50
#ifdef DEBUG
#define _DEBUG 1
#else
#define _DEBUG 0
#endif
Phil Blundell's avatar
Phil Blundell committed
51 52

/* Input an ARCnet address and convert to binary. */
53
static int in_arcnet(char *bufp, struct sockaddr_storage *sasp)
Phil Blundell's avatar
Phil Blundell committed
54
{
55
    struct sockaddr *sap = (struct sockaddr *)sasp;
56
    char *ptr;
57 58
    char c, *orig;
    int i, val;
Phil Blundell's avatar
Phil Blundell committed
59

60 61
    sap->sa_family = arcnet_hwtype.type;
    ptr = sap->sa_data;
Phil Blundell's avatar
Phil Blundell committed
62

63 64 65
    i = 0;
    orig = bufp;
    while ((*bufp != '\0') && (i < 1)) {
Phil Blundell's avatar
Phil Blundell committed
66 67
	val = 0;
	c = *bufp++;
68 69 70 71 72 73 74
	if (isdigit(c))
	    val = c - '0';
	else if (c >= 'a' && c <= 'f')
	    val = c - 'a' + 10;
	else if (c >= 'A' && c <= 'F')
	    val = c - 'A' + 10;
	else {
75 76
	    if (_DEBUG)
		fprintf(stderr, _("in_arcnet(%s): invalid arcnet address!\n"), orig);
77 78
	    errno = EINVAL;
	    return (-1);
Phil Blundell's avatar
Phil Blundell committed
79 80 81
	}
	val <<= 4;
	c = *bufp++;
82 83 84 85 86 87 88
	if (isdigit(c))
	    val |= c - '0';
	else if (c >= 'a' && c <= 'f')
	    val |= c - 'a' + 10;
	else if (c >= 'A' && c <= 'F')
	    val |= c - 'A' + 10;
	else {
89 90
	    if (_DEBUG)
		fprintf(stderr, _("in_arcnet(%s): invalid arcnet address!\n"), orig);
91 92
	    errno = EINVAL;
	    return (-1);
Phil Blundell's avatar
Phil Blundell committed
93 94 95 96 97 98
	}
	*ptr++ = (unsigned char) (val & 0377);
	i++;

	/* We might get a semicolon here - not required. */
	if (*bufp == ':') {
99
	    if (_DEBUG && i == ETH_ALEN)
100
		fprintf(stderr, _("in_arcnet(%s): trailing : ignored!\n"),
101
			orig);
102
	    bufp++;
Phil Blundell's avatar
Phil Blundell committed
103
	}
104
    }
Phil Blundell's avatar
Phil Blundell committed
105

106
    /* That's it.  Any trailing junk? */
107
    if (_DEBUG && (i == ETH_ALEN) && (*bufp != '\0')) {
108
	fprintf(stderr, _("in_arcnet(%s): trailing junk!\n"), orig);
Phil Blundell's avatar
Phil Blundell committed
109
	errno = EINVAL;
110 111
	return (-1);
    }
112 113
    if (_DEBUG)
	fprintf(stderr, "in_arcnet(%s): %s\n", orig, pr_arcnet(sap->sa_data));
Phil Blundell's avatar
Phil Blundell committed
114

115
    return (0);
Phil Blundell's avatar
Phil Blundell committed
116 117 118
}


119 120 121
struct hwtype arcnet_hwtype =
{
    "arcnet", NULL, /*"2.5Mbps ARCnet", */ ARPHRD_ARCNET, 1,
122
    pr_arcnet, in_arcnet, NULL
Phil Blundell's avatar
Phil Blundell committed
123 124 125
};


126
#endif				/* HAVE_HWARC */