EVE 1.0
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  *
33  */
34 
35 
36 #include "net/uip.h"
37 #include "net/uiplib.h"
38 #include <string.h>
39 
40 #ifndef DEBUG
41  #define DEBUG DEBUG_NONE
42 #endif
43 #include "net/uip-debug.h"
44 
45 /*-----------------------------------------------------------------------------------*/
46 #if UIP_CONF_IPV6
47 int
48 uiplib_ip6addrconv(const char *addrstr, uip_ip6addr_t *ipaddr)
49 {
50  uint16_t value;
51  int tmp, zero;
52  unsigned int len;
53  char c = 0; //gcc warning if not initialized
54 
55  value = 0;
56  zero = -1;
57  if(*addrstr == '[') addrstr++;
58 
59  for(len = 0; len < sizeof(uip_ip6addr_t) - 1; addrstr++) {
60  c = *addrstr;
61  if(c == ':' || c == '\0' || c == ']' || c == '/') {
62  ipaddr->u8[len] = (value >> 8) & 0xff;
63  ipaddr->u8[len + 1] = value & 0xff;
64  len += 2;
65  value = 0;
66 
67  if(c == '\0' || c == ']' || c == '/') {
68  break;
69  }
70 
71  if(*(addrstr + 1) == ':') {
72  /* Zero compression */
73  if(zero < 0) {
74  zero = len;
75  }
76  addrstr++;
77  }
78  } else {
79  if(c >= '0' && c <= '9') {
80  tmp = c - '0';
81  } else if(c >= 'a' && c <= 'f') {
82  tmp = c - 'a' + 10;
83  } else if(c >= 'A' && c <= 'F') {
84  tmp = c - 'A' + 10;
85  } else {
86  PRINTF("uiplib: illegal char: '%c'\n", c);
87  return 0;
88  }
89  value = (value << 4) + (tmp & 0xf);
90  }
91  }
92  if(c != '\0' && c != ']' && c != '/') {
93  PRINTF("uiplib: too large address\n");
94  return 0;
95  }
96  if(len < sizeof(uip_ip6addr_t)) {
97  if(zero < 0) {
98  PRINTF("uiplib: too short address\n");
99  return 0;
100  }
101  memmove(&ipaddr->u8[zero + sizeof(uip_ip6addr_t) - len],
102  &ipaddr->u8[zero], len - zero);
103  memset(&ipaddr->u8[zero], 0, sizeof(uip_ip6addr_t) - len);
104  }
105 
106  return 1;
107 }
108 #endif /* UIP_CONF_IPV6 */
109 /*-----------------------------------------------------------------------------------*/
110 /* Parse a IPv4-address from a string. Returns the number of characters read
111  * for the address. */
112 int
113 uiplib_ip4addrconv(const char *addrstr, uip_ip4addr_t *ipaddr)
114 {
115  unsigned char tmp;
116  char c;
117  unsigned char i, j;
118  uint8_t charsread = 0;
119 
120  tmp = 0;
121 
122  for(i = 0; i < 4; ++i) {
123  j = 0;
124  do {
125  c = *addrstr;
126  ++j;
127  if(j > 4) {
128  return 0;
129  }
130  if(c == '.' || c == 0 || c == ' ') {
131  ipaddr->u8[i] = tmp;
132  tmp = 0;
133  } else if(c >= '0' && c <= '9') {
134  tmp = (tmp * 10) + (c - '0');
135  } else {
136  return 0;
137  }
138  ++addrstr;
139  ++charsread;
140  } while(c != '.' && c != 0 && c != ' ');
141 
142  }
143  return charsread-1;
144 }
145 /*-----------------------------------------------------------------------------------*/