Merge "Modified license using SPDX license identifier" into tizen
[platform/upstream/net-tools.git] / lib / fddi.c
1 /*
2  * lib/fddi.c This file contains an implementation of the "FDDI"
3  *              support functions.
4  *
5  * Version:     $Id: fddi.c,v 1.7 2000/03/05 11:26:02 philip Exp $
6  *
7  * Author:      Lawrence V. Stefani, <stefani@lkg.dec.com>
8  *
9  * 1998-07-01 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> GNU gettext
10  *
11  *              This program is free software; you can redistribute it
12  *              and/or  modify it under  the terms of  the GNU General
13  *              Public  License as  published  by  the  Free  Software
14  *              Foundation;  either  version 2 of the License, or  (at
15  *              your option) any later version.
16  */
17 #include "config.h"
18
19 #include <features.h>
20
21 #if HAVE_HWFDDI
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <net/if_arp.h>
25 #ifndef ARPHRD_FDDI
26 #error "No FDDI Support in your current Kernelsource Tree."
27 #error "Disable HW Type FDDI"
28 #endif
29 #if __GLIBC__ >= 2
30 #include <netinet/if_fddi.h>
31 #else
32 #include <linux/if_fddi.h>
33 #endif
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include "net-support.h"
41 #include "pathnames.h"
42 #include "intl.h"
43 #include "util.h"
44
45 extern struct hwtype fddi_hwtype;
46
47
48 /* Display an FDDI address in readable format. */
49 static const char *pr_fddi(const char *ptr)
50 {
51     static char buff[64];
52
53     snprintf(buff, sizeof(buff), "%02X-%02X-%02X-%02X-%02X-%02X",
54              (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
55              (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
56         );
57     return (buff);
58 }
59
60 #ifdef DEBUG
61 #define _DEBUG 1
62 #else
63 #define _DEBUG 0
64 #endif
65
66 /* Input an FDDI address and convert to binary. */
67 static int in_fddi(char *bufp, struct sockaddr *sap)
68 {
69     char *ptr;
70     char c, *orig;
71     int i, val;
72
73     sap->sa_family = fddi_hwtype.type;
74     ptr = sap->sa_data;
75
76     i = 0;
77     orig = bufp;
78     while ((*bufp != '\0') && (i < FDDI_K_ALEN)) {
79         val = 0;
80         c = *bufp++;
81         if (isdigit(c))
82             val = c - '0';
83         else if (c >= 'a' && c <= 'f')
84             val = c - 'a' + 10;
85         else if (c >= 'A' && c <= 'F')
86             val = c - 'A' + 10;
87         else {
88             if (_DEBUG)
89                 fprintf(stderr, _("in_fddi(%s): invalid fddi address!\n"), orig);
90             errno = EINVAL;
91             return (-1);
92         }
93         val <<= 4;
94         c = *bufp++;
95         if (isdigit(c))
96             val |= c - '0';
97         else if (c >= 'a' && c <= 'f')
98             val |= c - 'a' + 10;
99         else if (c >= 'A' && c <= 'F')
100             val |= c - 'A' + 10;
101         else {
102             if (_DEBUG)
103                 fprintf(stderr, _("in_fddi(%s): invalid fddi address!\n"), orig);
104             errno = EINVAL;
105             return (-1);
106         }
107         *ptr++ = (unsigned char) (val & 0377);
108         i++;
109
110         /* We might get a semicolon here - not required. */
111         if (*bufp == ':') {
112             if (_DEBUG && i == FDDI_K_ALEN)
113                 fprintf(stderr, _("in_fddi(%s): trailing : ignored!\n"),
114                         orig);
115             bufp++;
116         }
117     }
118
119     /* That's it.  Any trailing junk? */
120     if (_DEBUG && (i == FDDI_K_ALEN) && (*bufp != '\0')) {
121         fprintf(stderr, _("in_fddi(%s): trailing junk!\n"), orig);
122         errno = EINVAL;
123         return (-1);
124     }
125     if (_DEBUG)
126         fprintf(stderr, "in_fddi(%s): %s\n", orig, pr_fddi(sap->sa_data));
127
128     return (0);
129 }
130
131
132 struct hwtype fddi_hwtype =
133 {
134     "fddi", NULL, /*"Fiber Distributed Data Interface (FDDI)", */ ARPHRD_FDDI, FDDI_K_ALEN,
135     pr_fddi, in_fddi, NULL
136 };
137
138
139 #endif                          /* HAVE_HWFDDI */