More #include "util.h"
[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.6 1998/11/26 10:16:39 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 char *pr_fddi(unsigned 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
61 /* Display an FDDI socket address. */
62 static char *pr_sfddi(struct sockaddr *sap)
63 {
64     static char buf[64];
65
66     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
67         return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
68     return (pr_fddi(sap->sa_data));
69 }
70
71
72 /* Input an FDDI address and convert to binary. */
73 static int in_fddi(char *bufp, struct sockaddr *sap)
74 {
75     unsigned char *ptr;
76     char c, *orig;
77     int i, val;
78
79     sap->sa_family = fddi_hwtype.type;
80     ptr = sap->sa_data;
81
82     i = 0;
83     orig = bufp;
84     while ((*bufp != '\0') && (i < FDDI_K_ALEN)) {
85         val = 0;
86         c = *bufp++;
87         if (isdigit(c))
88             val = c - '0';
89         else if (c >= 'a' && c <= 'f')
90             val = c - 'a' + 10;
91         else if (c >= 'A' && c <= 'F')
92             val = c - 'A' + 10;
93         else {
94 #ifdef DEBUG
95             fprintf(stderr, _("in_fddi(%s): invalid fddi address!\n"), orig);
96 #endif
97             errno = EINVAL;
98             return (-1);
99         }
100         val <<= 4;
101         c = *bufp++;
102         if (isdigit(c))
103             val |= c - '0';
104         else if (c >= 'a' && c <= 'f')
105             val |= c - 'a' + 10;
106         else if (c >= 'A' && c <= 'F')
107             val |= c - 'A' + 10;
108         else {
109 #ifdef DEBUG
110             fprintf(stderr, _("in_fddi(%s): invalid fddi address!\n"), orig);
111 #endif
112             errno = EINVAL;
113             return (-1);
114         }
115         *ptr++ = (unsigned char) (val & 0377);
116         i++;
117
118         /* We might get a semicolon here - not required. */
119         if (*bufp == ':') {
120             if (i == FDDI_K_ALEN) {
121 #ifdef DEBUG
122                 fprintf(stderr, _("in_fddi(%s): trailing : ignored!\n"),
123                         orig)
124 #endif
125                     ;           /* nothing */
126             }
127             bufp++;
128         }
129     }
130
131     /* That's it.  Any trailing junk? */
132     if ((i == FDDI_K_ALEN) && (*bufp != '\0')) {
133 #ifdef DEBUG
134         fprintf(stderr, _("in_fddi(%s): trailing junk!\n"), orig);
135         errno = EINVAL;
136         return (-1);
137 #endif
138     }
139 #ifdef DEBUG
140     fprintf(stderr, "in_fddi(%s): %s\n", orig, pr_fddi(sap->sa_data));
141 #endif
142
143     return (0);
144 }
145
146
147 struct hwtype fddi_hwtype =
148 {
149     "fddi", NULL, /*"Fiber Distributed Data Interface (FDDI)", */ ARPHRD_FDDI, FDDI_K_ALEN,
150     pr_fddi, pr_sfddi, in_fddi, NULL
151 };
152
153
154 #endif                          /* HAVE_HWFDDI */