added EUI64 Hardware Address Family
[oss-qm-packages.git] / lib / eui64.c
blob64c3614e82f5dd688f1698c91b8e4d20b8e7d0af
1 /*
2 * lib/eui64.c This file contains support for generic EUI-64 hw addressing
4 * Version: $Id: eui64.c,v 1.1 2001/11/12 02:12:05 ecki Exp $
6 * Author: Daniel Stodden <stodden@in.tum.de>
7 * Copyright 2001 Daniel Stodden
9 * blueprinted from ether.c
10 * Copyright 1993 MicroWalt Corporation
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.
18 #include "config.h"
20 #if HAVE_HWEUI64
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"
39 * EUI-64 constants
42 #define EUI64_ALEN 8
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
49 struct hwtype eui64_hwtype;
51 /* Display an EUI-64 address in readable format. */
52 static char *pr_eui64( unsigned char *ptr )
54 static char buff[64];
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)
60 return (buff);
63 /* Start the PPP encapsulation on the file descriptor. */
64 static int in_eui64( char *bufp, struct sockaddr *sap )
66 unsigned char *ptr;
67 char c, *orig;
68 int i;
69 unsigned val;
71 sap->sa_family = eui64_hwtype.type;
72 ptr = sap->sa_data;
74 i = 0;
75 orig = bufp;
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);
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);
114 if (c != 0)
115 bufp++;
117 *ptr++ = (unsigned char) (val & 0377);
118 i++;
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 */
129 bufp++;
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
141 #ifdef DEBUG
142 fprintf(stderr, "in_eui64(%s): %s\n", orig, pr_eui64(sap->sa_data));
143 #endif
145 return (0);
148 struct hwtype eui64_hwtype =
150 "eui64", NULL, /*"EUI-64 addressing", */ ARPHRD_EUI64, EUI64_ALEN,
151 pr_eui64, in_eui64, NULL, 0
155 #endif /* HAVE_EUI64 */