Back to index

glibc  2.9
ether_line.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996,1999,2002,2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <ctype.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <netinet/ether.h>
00023 #include <netinet/if_ether.h>
00024 
00025 
00026 int
00027 ether_line (const char *line, struct ether_addr *addr, char *hostname)
00028 {
00029   for (size_t cnt = 0; cnt < 6; ++cnt)
00030     {
00031       unsigned int number;
00032       char ch;
00033 
00034       ch = _tolower (*line++);
00035       if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
00036        return -1;
00037       number = isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
00038 
00039       ch = _tolower (*line);
00040       if ((cnt < 5 && ch != ':') || (cnt == 5 && ch != '\0' && !isspace (ch)))
00041        {
00042          ++line;
00043          if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f'))
00044            return -1;
00045          number <<= 4;
00046          number += isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
00047 
00048          ch = *line;
00049          if (cnt < 5 && ch != ':')
00050            return -1;
00051        }
00052 
00053       /* Store result.  */
00054       addr->ether_addr_octet[cnt] = (unsigned char) number;
00055 
00056       /* Skip ':'.  */
00057       if (ch != '\0')
00058        ++line;
00059     }
00060 
00061   /* Skip initial whitespace.  */
00062   while (isspace (*line))
00063     ++line;
00064 
00065   if (*line == '#' || *line == '\0')
00066     /* No hostname.  */
00067     return -1;
00068 
00069   /* The hostname is up to the next non-space character.  */
00070   /* XXX This can cause trouble because the hostname might be too long
00071      but we have no possibility to check it here.  */
00072   while (*line != '\0' && *line != '#' && !isspace (*line))
00073     *hostname++ = *line++;
00074   *hostname = '\0';
00075 
00076   return 0;
00077 }