home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NET-TOOL.1 / NET-TOOL / net-tools / lib / arcnet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-11  |  2.6 KB  |  123 lines

  1. /*
  2.  * NET-2    This file contains an implementation of the "arcnet"
  3.  *        support functions for the NET-2 base distribution.
  4.  *
  5.  * Version:    @(#)Arcnet.c    1.10    10/07/93
  6.  *
  7.  * Author:    Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  8.  *        Copyright 1993 MicroWalt Corporation
  9.  *
  10.  *        Arcnet added by Alan Cox <iialan@iifeak.swan.ac.uk>
  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_HWARC
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/if_arp.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "support.h"
  32. #include "pathnames.h"
  33.  
  34.  
  35. extern struct hwtype arc_hwtype;
  36.  
  37.  
  38. /* Display an Ethernet address in readable format. */
  39. static char *
  40. pr_arcnet(unsigned char *ptr)
  41. {
  42.   static char buff[64];
  43.  
  44.   sprintf(buff, "%02X", ptr[0]);
  45.   return(buff);
  46. }
  47.  
  48.  
  49. /* Display an Ethernet socket address. */
  50. static char *
  51. pr_sarcnet(struct sockaddr *sap)
  52. {
  53.   if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return("[NONE SET]");
  54.   return(pr_arcnet(sap->sa_data));
  55. }
  56.  
  57.  
  58. /* Input an Ethernet address and convert to binary. */
  59. static int
  60. in_arcnet(char *bufp, struct sockaddr *sap)
  61. {
  62.   unsigned char *ptr;
  63.   char c, *orig;
  64.   int i, val;
  65.  
  66.   sap->sa_family = arc_hwtype.type;
  67.   ptr = sap->sa_data;
  68.  
  69.   i = 0;
  70.   orig = bufp;
  71.   val = 0;
  72.   c = *bufp++;
  73.   if (isdigit(c)) val = c - '0';
  74.     else if (c >= 'a' && c <= 'f') val = c - 'a' + 10;
  75.     else if (c >= 'A' && c <= 'F') val = c - 'A' + 10;
  76.     else {
  77. #ifdef DEBUG
  78.         fprintf(stderr, "in_arc(%s): invalid arcnet address!\n", orig);
  79. #endif
  80.         errno = EINVAL;
  81.         return(-1);
  82.     }
  83.     val <<= 4;
  84.     c = *bufp++;
  85.     if (isdigit(c)) val |= c - '0';
  86.       else if (c >= 'a' && c <= 'f') val |= c - 'a' + 10;
  87.       else if (c >= 'A' && c <= 'F') val |= c - 'A' + 10;
  88.       else {
  89. #ifdef DEBUG
  90.         fprintf(stderr, "in_arcnet(%s): invalid arcnet address!\n", orig);
  91. #endif
  92.         errno = EINVAL;
  93.         return(-1);
  94.     }
  95.     *ptr++ = (unsigned char) (val & 0377);
  96.     i++;
  97.     bufp++;
  98.  
  99.   /* That's it.  Any trailing junk? */
  100.   if ((i == 1) && (*bufp != '\0')) {
  101. #ifdef DEBUG
  102.     fprintf(stderr, "in_arc(%s): trailing junk!\n", orig);
  103.     errno = EINVAL;
  104.     return(-1);
  105. #endif
  106.   }
  107.  
  108. #ifdef DEBUG
  109.   fprintf(stderr, "in_arcnet(%s): %s\n", orig, pr_arcnet(sap->sa_data));
  110. #endif
  111.  
  112.   return(0);
  113. }
  114.  
  115.  
  116. struct hwtype arc_hwtype = {
  117.   "arcnet",    "ArcNET",        ARPHRD_ARCNET,    1,
  118.   pr_arcnet,    pr_sarcnet,    in_arcnet,    NULL
  119. };
  120.  
  121.  
  122. #endif    /* HAVE_HWARC */
  123.