Nut/OS  4.10.3
API Reference
ether_addr.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  */
00032 
00033 #include <string.h>
00034 #include <netinet/if_ether.h>
00035 
00036 static INLINE int hex2bin(char c)
00037 {
00038     if (c >= '0' && c <= '9')
00039         return c - '0';
00040     if (c >= 'a' && c <= 'f')
00041         return c - 'a' + 10;
00042     if (c >= 'A' && c <= 'F')
00043         return c - 'A' + 10;
00044     return 0;
00045 }
00046 
00047 static char hexdigit[] = "0123456789ABCDEF";
00048 
00060 uint8_t *ether_aton(CONST char *str)
00061 {
00062     static uint8_t mac[6];
00063     CONST char *cp = str;
00064     int n = 0;
00065 
00066     memset(mac, 0, sizeof(mac));
00067     for (cp = str; *cp;) {
00068         if (*cp < '0' || (*cp > '9' && *cp < 'A') || (*cp > 'F' && *cp < 'a') || *cp > 'f') {
00069             return NULL;
00070         }
00071         if (mac[n] > 0x0F) {
00072             return NULL;
00073         }
00074         mac[n] <<= 4;
00075         mac[n] |= hex2bin(*cp);
00076         cp++;
00077         if (*cp == ':') {
00078             n++;
00079             if (n > 5) {
00080                 return NULL;
00081             }
00082             cp++;
00083         }
00084     }
00085     return mac;
00086 }
00087 
00099 char *ether_ntoa(CONST uint8_t *mac)
00100 {
00101     static char str[18];
00102     uint_fast8_t i;
00103     char *cp = str;
00104 
00105     for (i = 0; i < 6; i++) {
00106         *cp++ = hexdigit[(mac[i] >> 4) & 0x0F];
00107         *cp++ = hexdigit[mac[i] & 0x0F];
00108         *cp++ = ':';
00109     }
00110     *--cp = 0;
00111 
00112     return str;
00113 }