Merge "Modified license using SPDX license identifier" into tizen
[platform/upstream/net-tools.git] / lib / hippi.c
1 /*
2  * lib/hippi.c        This file contains an implementation of the "HIPPI"
3  *              support functions for the NET-2 base distribution.
4  *
5  * Version:     $Id$
6  *
7  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
8  *              Copyright 1993 MicroWalt Corporation
9  *
10  *              Modified for HIPPI by Jes Sorensen, <Jes.Sorensen@cern.ch>
11  *
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.
17  */
18 #include "config.h"
19
20 #if HAVE_HWHIPPI
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <net/if_arp.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include "net-support.h"
31 #include "pathnames.h"
32 #include "intl.h"
33 #include "util.h"
34
35 /*
36  *    HIPPI magic constants.
37  */
38
39 #define HIPPI_ALEN      6       /* Bytes in one HIPPI hw-addr        */
40 #ifndef ARPHRD_HIPPI
41 #define ARPHRD_HIPPI    780
42 #warning "ARPHRD_HIPPI is not defined in <net/if_arp.h>. Using private value 708"
43 #endif
44
45 extern struct hwtype hippi_hwtype;
46
47
48 /* Display an HIPPI address in readable format. */
49 static const char *pr_hippi(const char *ptr)
50 {
51     static char buff[64];
52
53     sprintf(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 HIPPI address and convert to binary. */
67 static int in_hippi(char *bufp, struct sockaddr *sap)
68 {
69     char *ptr;
70     char c, *orig;
71     int i, val;
72
73     sap->sa_family = hippi_hwtype.type;
74     ptr = sap->sa_data;
75
76     i = 0;
77     orig = bufp;
78     while ((*bufp != '\0') && (i < HIPPI_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_hippi(%s): invalid hippi 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_hippi(%s): invalid hippi 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 == HIPPI_ALEN)
113                 fprintf(stderr, _("in_hippi(%s): trailing : ignored!\n"), orig);
114             bufp++;
115         }
116     }
117
118     /* That's it.  Any trailing junk? */
119     if (_DEBUG && (i == HIPPI_ALEN) && (*bufp != '\0')) {
120         fprintf(stderr, _("in_hippi(%s): trailing junk!\n"), orig);
121         errno = EINVAL;
122         return (-1);
123     }
124     if (_DEBUG)
125         fprintf(stderr, "in_hippi(%s): %s\n", orig, pr_hippi(sap->sa_data));
126
127     return (0);
128 }
129
130
131 struct hwtype hippi_hwtype =
132 {
133     "hippi", NULL, /*"HIPPI", */ ARPHRD_HIPPI, HIPPI_ALEN,
134     pr_hippi, in_hippi, NULL, 0
135 };
136
137
138 #endif                          /* HAVE_HWHIPPI */