Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
inet.c
00001 /** 00002 * @file 00003 * Functions common to all TCP/IPv4 modules, such as the byte order functions. 00004 * 00005 */ 00006 00007 /* 00008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00031 * OF SUCH DAMAGE. 00032 * 00033 * This file is part of the lwIP TCP/IP stack. 00034 * 00035 * Author: Adam Dunkels <adam@sics.se> 00036 * 00037 */ 00038 00039 #include "lwip/opt.h" 00040 00041 #include "lwip/inet.h" 00042 00043 /* Here for now until needed in other places in lwIP */ 00044 #ifndef isprint 00045 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up) 00046 #define isprint(c) in_range(c, 0x20, 0x7f) 00047 #define isdigit(c) in_range(c, '0', '9') 00048 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F')) 00049 #define islower(c) in_range(c, 'a', 'z') 00050 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') 00051 #endif 00052 00053 /** 00054 * Ascii internet address interpretation routine. 00055 * The value returned is in network order. 00056 * 00057 * @param cp IP address in ascii represenation (e.g. "127.0.0.1") 00058 * @return ip address in network order 00059 */ 00060 u32_t 00061 inet_addr(const char *cp) 00062 { 00063 struct in_addr val; 00064 00065 if (inet_aton(cp, &val)) { 00066 return (val.s_addr); 00067 } 00068 return (INADDR_NONE); 00069 } 00070 00071 /** 00072 * Check whether "cp" is a valid ascii representation 00073 * of an Internet address and convert to a binary address. 00074 * Returns 1 if the address is valid, 0 if not. 00075 * This replaces inet_addr, the return value from which 00076 * cannot distinguish between failure and a local broadcast address. 00077 * 00078 * @param cp IP address in ascii represenation (e.g. "127.0.0.1") 00079 * @param addr pointer to which to save the ip address in network order 00080 * @return 1 if cp could be converted to addr, 0 on failure 00081 */ 00082 int 00083 inet_aton(const char *cp, struct in_addr *addr) 00084 { 00085 u32_t val; 00086 u8_t base; 00087 char c; 00088 u32_t parts[4]; 00089 u32_t *pp = parts; 00090 00091 c = *cp; 00092 for (;;) { 00093 /* 00094 * Collect number up to ``.''. 00095 * Values are specified as for C: 00096 * 0x=hex, 0=octal, 1-9=decimal. 00097 */ 00098 if (!isdigit(c)) 00099 return (0); 00100 val = 0; 00101 base = 10; 00102 if (c == '0') { 00103 c = *++cp; 00104 if (c == 'x' || c == 'X') { 00105 base = 16; 00106 c = *++cp; 00107 } else 00108 base = 8; 00109 } 00110 for (;;) { 00111 if (isdigit(c)) { 00112 val = (val * base) + (int)(c - '0'); 00113 c = *++cp; 00114 } else if (base == 16 && isxdigit(c)) { 00115 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A')); 00116 c = *++cp; 00117 } else 00118 break; 00119 } 00120 if (c == '.') { 00121 /* 00122 * Internet format: 00123 * a.b.c.d 00124 * a.b.c (with c treated as 16 bits) 00125 * a.b (with b treated as 24 bits) 00126 */ 00127 if (pp >= parts + 3) 00128 return (0); 00129 *pp++ = val; 00130 c = *++cp; 00131 } else 00132 break; 00133 } 00134 /* 00135 * Check for trailing characters. 00136 */ 00137 if (c != '\0' && !isspace(c)) 00138 return (0); 00139 /* 00140 * Concoct the address according to 00141 * the number of parts specified. 00142 */ 00143 switch (pp - parts + 1) { 00144 00145 case 0: 00146 return (0); /* initial nondigit */ 00147 00148 case 1: /* a -- 32 bits */ 00149 break; 00150 00151 case 2: /* a.b -- 8.24 bits */ 00152 if (val > 0xffffffUL) 00153 return (0); 00154 val |= parts[0] << 24; 00155 break; 00156 00157 case 3: /* a.b.c -- 8.8.16 bits */ 00158 if (val > 0xffff) 00159 return (0); 00160 val |= (parts[0] << 24) | (parts[1] << 16); 00161 break; 00162 00163 case 4: /* a.b.c.d -- 8.8.8.8 bits */ 00164 if (val > 0xff) 00165 return (0); 00166 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 00167 break; 00168 } 00169 if (addr) 00170 addr->s_addr = htonl(val); 00171 return (1); 00172 } 00173 00174 /** 00175 * Convert numeric IP address into decimal dotted ASCII representation. 00176 * returns ptr to static buffer; not reentrant! 00177 * 00178 * @param addr ip address in network order to convert 00179 * @return pointer to a global static (!) buffer that holds the ASCII 00180 * represenation of addr 00181 */ 00182 char * 00183 inet_ntoa(struct in_addr addr) 00184 { 00185 static char str[16]; 00186 u32_t s_addr = addr.s_addr; 00187 char inv[3]; 00188 char *rp; 00189 u8_t *ap; 00190 u8_t rem; 00191 u8_t n; 00192 u8_t i; 00193 00194 rp = str; 00195 ap = (u8_t *)&s_addr; 00196 for(n = 0; n < 4; n++) { 00197 i = 0; 00198 do { 00199 rem = *ap % (u8_t)10; 00200 *ap /= (u8_t)10; 00201 inv[i++] = '0' + rem; 00202 } while(*ap); 00203 while(i--) 00204 *rp++ = inv[i]; 00205 *rp++ = '.'; 00206 ap++; 00207 } 00208 *--rp = 0; 00209 return str; 00210 } 00211 00212 /** 00213 * These are reference implementations of the byte swapping functions. 00214 * Again with the aim of being simple, correct and fully portable. 00215 * Byte swapping is the second thing you would want to optimize. You will 00216 * need to port it to your architecture and in your cc.h: 00217 * 00218 * #define LWIP_PLATFORM_BYTESWAP 1 00219 * #define LWIP_PLATFORM_HTONS(x) <your_htons> 00220 * #define LWIP_PLATFORM_HTONL(x) <your_htonl> 00221 * 00222 * Note ntohs() and ntohl() are merely references to the htonx counterparts. 00223 */ 00224 00225 #if (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) 00226 00227 /** 00228 * Convert an u16_t from host- to network byte order. 00229 * 00230 * @param n u16_t in host byte order 00231 * @return n in network byte order 00232 */ 00233 u16_t 00234 htons(u16_t n) 00235 { 00236 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8); 00237 } 00238 00239 /** 00240 * Convert an u16_t from network- to host byte order. 00241 * 00242 * @param n u16_t in network byte order 00243 * @return n in host byte order 00244 */ 00245 u16_t 00246 ntohs(u16_t n) 00247 { 00248 return htons(n); 00249 } 00250 00251 /** 00252 * Convert an u32_t from host- to network byte order. 00253 * 00254 * @param n u32_t in host byte order 00255 * @return n in network byte order 00256 */ 00257 u32_t 00258 htonl(u32_t n) 00259 { 00260 return ((n & 0xff) << 24) | 00261 ((n & 0xff00) << 8) | 00262 ((n & 0xff0000UL) >> 8) | 00263 ((n & 0xff000000UL) >> 24); 00264 } 00265 00266 /** 00267 * Convert an u32_t from network- to host byte order. 00268 * 00269 * @param n u32_t in network byte order 00270 * @return n in host byte order 00271 */ 00272 u32_t 00273 ntohl(u32_t n) 00274 { 00275 return htonl(n); 00276 } 00277 00278 #endif /* (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) */
Generated on Tue Jul 12 2022 19:17:23 by 1.7.2