eui64.c 3.37 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * lib/eui64.c  This file contains support for generic EUI-64 hw addressing
 *
 * Version:     $Id: eui64.c,v 1.1 2001/11/12 02:12:05 ecki Exp $
 *
 * Author:      Daniel Stodden <stodden@in.tum.de>
 *              Copyright 2001 Daniel Stodden
 *
9
 *              blueprinted from ether.c
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 *              Copyright 1993 MicroWalt Corporation
 *
 *              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.
 */
#include "config.h"

#if HAVE_HWEUI64

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "net-support.h"
#include "pathnames.h"
#include "intl.h"

/*
 * EUI-64 constants
 */

#define EUI64_ALEN	8

#ifndef ARPHRD_EUI64
#define ARPHRD_EUI64	27
#warning "ARPHRD_EUI64 not defined in <net/if_arp.h>. Using private value 27"
#endif

struct hwtype eui64_hwtype;

/* Display an EUI-64 address in readable format. */
52
static const char *pr_eui64(const char *ptr)
53 54 55 56
{
	static char buff[64];

	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 59 60 61 62
		 (ptr[4] & 0377), (ptr[5] & 0377), (ptr[6] & 0377), (ptr[7] & 0377)
		);
	return (buff);
}

63 64 65 66 67 68
#ifdef DEBUG
#define _DEBUG 1
#else
#define _DEBUG 0
#endif

69
/* Start the PPP encapsulation on the file descriptor. */
70
static int in_eui64(char *bufp, struct sockaddr_storage *sasp)
71
{
72
    struct sockaddr *sap = (struct sockaddr *)sasp;
73
	char *ptr;
74 75 76 77 78 79 80 81 82
	char c, *orig;
	int i;
	unsigned val;

	sap->sa_family = eui64_hwtype.type;
	ptr = sap->sa_data;

	i = 0;
	orig = bufp;
83

84 85 86 87 88 89 90 91 92 93
	while ((*bufp != '\0') && (i < EUI64_ALEN)) {
		val = 0;
		c = *bufp++;
		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 {
94 95 96
			if (_DEBUG)
				fprintf( stderr, _("in_eui64(%s): invalid eui64 address!\n"),
					 orig );
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
			errno = EINVAL;
			return (-1);
		}

		val <<= 4;
		c = *bufp;
		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 if (c == ':' || c == 0)
			val >>= 4;
		else {
112 113 114
			if (_DEBUG)
				fprintf( stderr, _("in_eui64(%s): invalid eui64 address!\n"),
					 orig );
115 116 117 118 119 120 121 122 123
			errno = EINVAL;
			return (-1);
		}

		if (c != 0)
			bufp++;

		*ptr++ = (unsigned char) (val & 0377);
		i++;
124

125 126
		/* We might get a semicolon here - not required. */
		if (*bufp == ':') {
127
			if (_DEBUG && i == EUI64_ALEN)
128
				fprintf(stderr, _("in_eui64(%s): trailing : ignored!\n"),
129
					orig);
130 131 132 133 134
			bufp++;
		}
	}

	/* That's it.  Any trailing junk? */
135
	if (_DEBUG && (i == EUI64_ALEN) && (*bufp != '\0')) {
136 137 138 139
		fprintf(stderr, _("in_eui64(%s): trailing junk!\n"), orig);
		errno = EINVAL;
		return (-1);
	}
140 141
	if (_DEBUG)
		fprintf(stderr, "in_eui64(%s): %s\n", orig, pr_eui64(sap->sa_data));
142 143 144 145 146 147 148 149 150 151 152 153

    return (0);
}

struct hwtype eui64_hwtype =
{
	"eui64", NULL, /*"EUI-64 addressing", */ ARPHRD_EUI64, EUI64_ALEN,
	pr_eui64, in_eui64, NULL, 0
};


#endif				/* HAVE_EUI64 */