Contiki 2.6

uiplib.c

00001 /*
00002  * Copyright (c) 2004, Adam Dunkels and the Swedish Institute of
00003  * Computer Science.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions 
00008  * are met: 
00009  * 1. Redistributions of source code must retain the above copyright 
00010  *    notice, this list of conditions and the following disclaimer. 
00011  * 2. Redistributions in binary form must reproduce the above copyright 
00012  *    notice, this list of conditions and the following disclaimer in the 
00013  *    documentation and/or other materials provided with the distribution. 
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.  
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00029  *
00030  * This file is part of the uIP TCP/IP stack and the Contiki operating system.
00031  *
00032  * $Id: uiplib.c,v 1.3 2010/12/14 22:45:22 dak664 Exp $
00033  *
00034  */
00035 
00036 
00037 #include "net/uip.h"
00038 #include "net/uiplib.h"
00039 #include <string.h>
00040 
00041 #define DEBUG DEBUG_NONE
00042 #include "net/uip-debug.h"
00043 
00044 /*-----------------------------------------------------------------------------------*/
00045 int
00046 uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
00047 {
00048 #if UIP_CONF_IPV6
00049   uint16_t value;
00050   int tmp, zero;
00051   unsigned int len;
00052   char c = 0;  //gcc warning if not initialized
00053 
00054   value = 0;
00055   zero = -1;
00056   if(*addrstr == '[') addrstr++;
00057 
00058   for(len = 0; len < sizeof(uip_ipaddr_t) - 1; addrstr++) {
00059     c = *addrstr;
00060     if(c == ':' || c == '\0' || c == ']' || c == '/') {
00061       ipaddr->u8[len] = (value >> 8) & 0xff;
00062       ipaddr->u8[len + 1] = value & 0xff;
00063       len += 2;
00064       value = 0;
00065 
00066       if(c == '\0' || c == ']' || c == '/') {
00067         break;
00068       }
00069 
00070       if(*(addrstr + 1) == ':') {
00071         /* Zero compression */
00072         if(zero < 0) {
00073           zero = len;
00074         }
00075         addrstr++;
00076       }
00077     } else {
00078       if(c >= '0' && c <= '9') {
00079         tmp = c - '0';
00080       } else if(c >= 'a' && c <= 'f') {
00081         tmp = c - 'a' + 10;
00082       } else if(c >= 'A' && c <= 'F') {
00083         tmp = c - 'A' + 10;
00084       } else {
00085         PRINTF("uiplib: illegal char: '%c'\n", c);
00086         return 0;
00087       }
00088       value = (value << 4) + (tmp & 0xf);
00089     }
00090   }
00091   if(c != '\0' && c != ']' && c != '/') {
00092     PRINTF("uiplib: too large address\n");
00093     return 0;
00094   }
00095   if(len < sizeof(uip_ipaddr_t)) {
00096     if(zero < 0) {
00097       PRINTF("uiplib: too short address\n");
00098       return 0;
00099     }
00100     memmove(&ipaddr->u8[zero + sizeof(uip_ipaddr_t) - len],
00101             &ipaddr->u8[zero], len - zero);
00102     memset(&ipaddr->u8[zero], 0, sizeof(uip_ipaddr_t) - len);
00103   }
00104 
00105 #else /* UIP_CONF_IPV6 */
00106 
00107   unsigned char tmp;
00108   char c;
00109   unsigned char i, j;
00110 
00111   tmp = 0;
00112   
00113   for(i = 0; i < 4; ++i) {
00114     j = 0;
00115     do {
00116       c = *addrstr;
00117       ++j;
00118       if(j > 4) {
00119         return 0;
00120       }
00121       if(c == '.' || c == 0) {
00122         ipaddr->u8[i] = tmp;
00123         tmp = 0;
00124       } else if(c >= '0' && c <= '9') {
00125         tmp = (tmp * 10) + (c - '0');
00126       } else {
00127         return 0;
00128       }
00129       ++addrstr;
00130     } while(c != '.' && c != 0);
00131   }
00132 #endif /* UIP_CONF_IPV6 */
00133   return 1;
00134 }
00135 
00136 /*-----------------------------------------------------------------------------------*/