Contiki 2.5
uiplib.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels and the Swedish Institute of
3  * Computer Science.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the uIP TCP/IP stack and the Contiki operating system.
31  *
32  * $Id: uiplib.c,v 1.3 2010/12/14 22:45:22 dak664 Exp $
33  *
34  */
35 
36 
37 #include "net/uip.h"
38 #include "net/uiplib.h"
39 #include <string.h>
40 
41 #define DEBUG DEBUG_NONE
42 #include "net/uip-debug.h"
43 
44 /*-----------------------------------------------------------------------------------*/
45 int
46 uiplib_ipaddrconv(const char *addrstr, uip_ipaddr_t *ipaddr)
47 {
48 #if UIP_CONF_IPV6
49  uint16_t value;
50  int tmp, zero;
51  unsigned int len;
52  char c = 0; //gcc warning if not initialized
53 
54  value = 0;
55  zero = -1;
56  if(*addrstr == '[') addrstr++;
57 
58  for(len = 0; len < sizeof(uip_ipaddr_t) - 1; addrstr++) {
59  c = *addrstr;
60  if(c == ':' || c == '\0' || c == ']') {
61  ipaddr->u8[len] = (value >> 8) & 0xff;
62  ipaddr->u8[len + 1] = value & 0xff;
63  len += 2;
64  value = 0;
65 
66  if(c == '\0' || c == ']') {
67  break;
68  }
69 
70  if(*(addrstr + 1) == ':') {
71  /* Zero compression */
72  if(zero < 0) {
73  zero = len;
74  }
75  addrstr++;
76  }
77  } else {
78  if(c >= '0' && c <= '9') {
79  tmp = c - '0';
80  } else if(c >= 'a' && c <= 'f') {
81  tmp = c - 'a' + 10;
82  } else if(c >= 'A' && c <= 'F') {
83  tmp = c - 'A' + 10;
84  } else {
85  PRINTF("uiplib: illegal char: '%c'\n", c);
86  return 0;
87  }
88  value = (value << 4) + (tmp & 0xf);
89  }
90  }
91  if(c != '\0' && c != ']') {
92  PRINTF("uiplib: too large address\n");
93  return 0;
94  }
95  if(len < sizeof(uip_ipaddr_t)) {
96  if(zero < 0) {
97  PRINTF("uiplib: too short address\n");
98  return 0;
99  }
100  memmove(&ipaddr->u8[zero + sizeof(uip_ipaddr_t) - len],
101  &ipaddr->u8[zero], len - zero);
102  memset(&ipaddr->u8[zero], 0, sizeof(uip_ipaddr_t) - len);
103  }
104 
105 #else /* UIP_CONF_IPV6 */
106 
107  unsigned char tmp;
108  char c;
109  unsigned char i, j;
110 
111  tmp = 0;
112 
113  for(i = 0; i < 4; ++i) {
114  j = 0;
115  do {
116  c = *addrstr;
117  ++j;
118  if(j > 4) {
119  return 0;
120  }
121  if(c == '.' || c == 0) {
122  ipaddr->u8[i] = tmp;
123  tmp = 0;
124  } else if(c >= '0' && c <= '9') {
125  tmp = (tmp * 10) + (c - '0');
126  } else {
127  return 0;
128  }
129  ++addrstr;
130  } while(c != '.' && c != 0);
131  }
132 #endif /* UIP_CONF_IPV6 */
133  return 1;
134 }
135 
136 /*-----------------------------------------------------------------------------------*/