home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NET-TOOL.1 / NET-TOOL / net-tools / lib / ether.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-10  |  3.0 KB  |  137 lines

  1. /*
  2.  * NET-2    This file contains an implementation of the "Ethernet"
  3.  *        support functions for the NET-2 base distribution.
  4.  *
  5.  * Version:    @(#)ether.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.  *        This program is free software; you can redistribute it
  11.  *        and/or  modify it under  the terms of  the GNU General
  12.  *        Public  License as  published  by  the  Free  Software
  13.  *        Foundation;  either  version 2 of the License, or  (at
  14.  *        your option) any later version.
  15.  */
  16. #include "config.h"
  17.  
  18. #if HAVE_HWETHER
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/if_arp.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "support.h"
  30. #include "pathnames.h"
  31.  
  32.  
  33. extern struct hwtype ether_hwtype;
  34.  
  35.  
  36. /* Display an Ethernet address in readable format. */
  37. static char *
  38. pr_ether(unsigned char *ptr)
  39. {
  40.   static char buff[64];
  41.  
  42.   sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
  43.     (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
  44.     (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
  45.   );
  46.   return(buff);
  47. }
  48.  
  49.  
  50. /* Display an Ethernet socket address. */
  51. static char *
  52. pr_sether(struct sockaddr *sap)
  53. {
  54.   if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return("[NONE SET]");
  55.   return(pr_ether(sap->sa_data));
  56. }
  57.  
  58.  
  59. /* Input an Ethernet address and convert to binary. */
  60. static int
  61. in_ether(char *bufp, struct sockaddr *sap)
  62. {
  63.   unsigned char *ptr;
  64.   char c, *orig;
  65.   int i, val;
  66.  
  67.   sap->sa_family = ether_hwtype.type;
  68.   ptr = sap->sa_data;
  69.  
  70.   i = 0;
  71.   orig = bufp;
  72.   while((*bufp != '\0') && (i < ETH_ALEN)) {
  73.     val = 0;
  74.     c = *bufp++;
  75.     if (isdigit(c)) val = c - '0';
  76.       else if (c >= 'a' && c <= 'f') val = c - 'a' + 10;
  77.       else if (c >= 'A' && c <= 'F') val = c - 'A' + 10;
  78.       else {
  79. #ifdef DEBUG
  80.         fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
  81. #endif
  82.         errno = EINVAL;
  83.         return(-1);
  84.     }
  85.     val <<= 4;
  86.     c = *bufp++;
  87.     if (isdigit(c)) val |= c - '0';
  88.       else if (c >= 'a' && c <= 'f') val |= c - 'a' + 10;
  89.       else if (c >= 'A' && c <= 'F') val |= c - 'A' + 10;
  90.       else {
  91. #ifdef DEBUG
  92.         fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
  93. #endif
  94.         errno = EINVAL;
  95.         return(-1);
  96.     }
  97.     *ptr++ = (unsigned char) (val & 0377);
  98.     i++;
  99.  
  100.     /* We might get a semicolon here - not required. */
  101.     if (*bufp == ':') {
  102.         if (i == ETH_ALEN) {
  103. #ifdef DEBUG
  104.             fprintf(stderr, "in_ether(%s): trailing : ignored!\n",
  105.                                     orig)
  106. #endif
  107.                         ; /* nothing */
  108.         }
  109.         bufp++;
  110.     }
  111.   }
  112.  
  113.   /* That's it.  Any trailing junk? */
  114.   if ((i == ETH_ALEN) && (*bufp != '\0')) {
  115. #ifdef DEBUG
  116.     fprintf(stderr, "in_ether(%s): trailing junk!\n", orig);
  117.     errno = EINVAL;
  118.     return(-1);
  119. #endif
  120.   }
  121.  
  122. #ifdef DEBUG
  123.   fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
  124. #endif
  125.  
  126.   return(0);
  127. }
  128.  
  129.  
  130. struct hwtype ether_hwtype = {
  131.   "ether",    "10Mbps Ethernet",        ARPHRD_ETHER,    ETH_ALEN,
  132.   pr_ether,    pr_sether,    in_ether,    NULL
  133. };
  134.  
  135.  
  136. #endif    /* HAVE_HWETHER */
  137.