|
||||
Warning, cross-references for /kernel/lwip/core/ipv4/inet.c need to be fixed.
0001 /** 0002 * @file 0003 * Functions common to all TCP/IPv4 modules, such as the byte order functions. 0004 * 0005 */ 0006 0007 /* 0008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 0009 * All rights reserved. 0010 * 0011 * Redistribution and use in source and binary forms, with or without modification, 0012 * are permitted provided that the following conditions are met: 0013 * 0014 * 1. Redistributions of source code must retain the above copyright notice, 0015 * this list of conditions and the following disclaimer. 0016 * 2. Redistributions in binary form must reproduce the above copyright notice, 0017 * this list of conditions and the following disclaimer in the documentation 0018 * and/or other materials provided with the distribution. 0019 * 3. The name of the author may not be used to endorse or promote products 0020 * derived from this software without specific prior written permission. 0021 * 0022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 0023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 0024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 0025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 0026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 0027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 0028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 0029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 0030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 0031 * OF SUCH DAMAGE. 0032 * 0033 * This file is part of the lwIP TCP/IP stack. 0034 * 0035 * Author: Adam Dunkels <adam@sics.se> 0036 * 0037 */ 0038 0039 #include "lwip/opt.h" 0040 0041 #include "lwip/inet.h" 0042 0043 /* Here for now until needed in other places in lwIP */ 0044 #ifndef isprint 0045 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up) 0046 #define isprint(c) in_range(c, 0x20, 0x7f) 0047 #define isdigit(c) in_range(c, '0', '9') 0048 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F')) 0049 #define islower(c) in_range(c, 'a', 'z') 0050 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') 0051 #endif 0052 0053 /** 0054 * Ascii internet address interpretation routine. 0055 * The value returned is in network order. 0056 * 0057 * @param cp IP address in ascii represenation (e.g. "127.0.0.1") 0058 * @return ip address in network order 0059 */ 0060 u32_t 0061 inet_addr(const char *cp) 0062 { 0063 struct in_addr val; 0064 0065 if (inet_aton(cp, &val)) { 0066 return (val.s_addr); 0067 } 0068 return (INADDR_NONE); 0069 } 0070 0071 /** 0072 * Check whether "cp" is a valid ascii representation 0073 * of an Internet address and convert to a binary address. 0074 * Returns 1 if the address is valid, 0 if not. 0075 * This replaces inet_addr, the return value from which 0076 * cannot distinguish between failure and a local broadcast address. 0077 * 0078 * @param cp IP address in ascii represenation (e.g. "127.0.0.1") 0079 * @param addr pointer to which to save the ip address in network order 0080 * @return 1 if cp could be converted to addr, 0 on failure 0081 */ 0082 int 0083 inet_aton(const char *cp, struct in_addr *addr) 0084 { 0085 u32_t val; 0086 u8_t base; 0087 char c; 0088 u32_t parts[4]; 0089 u32_t *pp = parts; 0090 0091 c = *cp; 0092 for (;;) { 0093 /* 0094 * Collect number up to ``.''. 0095 * Values are specified as for C: 0096 * 0x=hex, 0=octal, 1-9=decimal. 0097 */ 0098 if (!isdigit(c)) 0099 return (0); 0100 val = 0; 0101 base = 10; 0102 if (c == '0') { 0103 c = *++cp; 0104 if (c == 'x' || c == 'X') { 0105 base = 16; 0106 c = *++cp; 0107 } else 0108 base = 8; 0109 } 0110 for (;;) { 0111 if (isdigit(c)) { 0112 val = (val * base) + (int)(c - '0'); 0113 c = *++cp; 0114 } else if (base == 16 && isxdigit(c)) { 0115 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A')); 0116 c = *++cp; 0117 } else 0118 break; 0119 } 0120 if (c == '.') { 0121 /* 0122 * Internet format: 0123 * a.b.c.d 0124 * a.b.c (with c treated as 16 bits) 0125 * a.b (with b treated as 24 bits) 0126 */ 0127 if (pp >= parts + 3) 0128 return (0); 0129 *pp++ = val; 0130 c = *++cp; 0131 } else 0132 break; 0133 } 0134 /* 0135 * Check for trailing characters. 0136 */ 0137 if (c != '\0' && !isspace(c)) 0138 return (0); 0139 /* 0140 * Concoct the address according to 0141 * the number of parts specified. 0142 */ 0143 switch (pp - parts + 1) { 0144 0145 case 0: 0146 return (0); /* initial nondigit */ 0147 0148 case 1: /* a -- 32 bits */ 0149 break; 0150 0151 case 2: /* a.b -- 8.24 bits */ 0152 if (val > 0xffffffUL) 0153 return (0); 0154 val |= parts[0] << 24; 0155 break; 0156 0157 case 3: /* a.b.c -- 8.8.16 bits */ 0158 if (val > 0xffff) 0159 return (0); 0160 val |= (parts[0] << 24) | (parts[1] << 16); 0161 break; 0162 0163 case 4: /* a.b.c.d -- 8.8.8.8 bits */ 0164 if (val > 0xff) 0165 return (0); 0166 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 0167 break; 0168 } 0169 if (addr) 0170 addr->s_addr = htonl(val); 0171 return (1); 0172 } 0173 0174 /** 0175 * Convert numeric IP address into decimal dotted ASCII representation. 0176 * returns ptr to static buffer; not reentrant! 0177 * 0178 * @param addr ip address in network order to convert 0179 * @return pointer to a global static (!) buffer that holds the ASCII 0180 * represenation of addr 0181 */ 0182 char * 0183 inet_ntoa(struct in_addr addr) 0184 { 0185 static char str[16]; 0186 u32_t s_addr = addr.s_addr; 0187 char inv[3]; 0188 char *rp; 0189 u8_t *ap; 0190 u8_t rem; 0191 u8_t n; 0192 u8_t i; 0193 0194 rp = str; 0195 ap = (u8_t *)&s_addr; 0196 for(n = 0; n < 4; n++) { 0197 i = 0; 0198 do { 0199 rem = *ap % (u8_t)10; 0200 *ap /= (u8_t)10; 0201 inv[i++] = '0' + rem; 0202 } while(*ap); 0203 while(i--) 0204 *rp++ = inv[i]; 0205 *rp++ = '.'; 0206 ap++; 0207 } 0208 *--rp = 0; 0209 return str; 0210 } 0211 0212 /** 0213 * These are reference implementations of the byte swapping functions. 0214 * Again with the aim of being simple, correct and fully portable. 0215 * Byte swapping is the second thing you would want to optimize. You will 0216 * need to port it to your architecture and in your cc.h: 0217 * 0218 * #define LWIP_PLATFORM_BYTESWAP 1 0219 * #define LWIP_PLATFORM_HTONS(x) <your_htons> 0220 * #define LWIP_PLATFORM_HTONL(x) <your_htonl> 0221 * 0222 * Note ntohs() and ntohl() are merely references to the htonx counterparts. 0223 */ 0224 0225 #if (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) 0226 0227 /** 0228 * Convert an u16_t from host- to network byte order. 0229 * 0230 * @param n u16_t in host byte order 0231 * @return n in network byte order 0232 */ 0233 u16_t 0234 htons(u16_t n) 0235 { 0236 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8); 0237 } 0238 0239 /** 0240 * Convert an u16_t from network- to host byte order. 0241 * 0242 * @param n u16_t in network byte order 0243 * @return n in host byte order 0244 */ 0245 u16_t 0246 ntohs(u16_t n) 0247 { 0248 return htons(n); 0249 } 0250 0251 /** 0252 * Convert an u32_t from host- to network byte order. 0253 * 0254 * @param n u32_t in host byte order 0255 * @return n in network byte order 0256 */ 0257 u32_t 0258 htonl(u32_t n) 0259 { 0260 return ((n & 0xff) << 24) | 0261 ((n & 0xff00) << 8) | 0262 ((n & 0xff0000UL) >> 8) | 0263 ((n & 0xff000000UL) >> 24); 0264 } 0265 0266 /** 0267 * Convert an u32_t from network- to host byte order. 0268 * 0269 * @param n u32_t in network byte order 0270 * @return n in host byte order 0271 */ 0272 u32_t 0273 ntohl(u32_t n) 0274 { 0275 return htonl(n); 0276 } 0277 0278 #endif /* (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 1.2.0 LXR engine. |