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