I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1 /**
tax 0:66300c77c6e9 2 * @file
tax 0:66300c77c6e9 3 * This is the IPv4 address tools implementation.
tax 0:66300c77c6e9 4 *
tax 0:66300c77c6e9 5 */
tax 0:66300c77c6e9 6
tax 0:66300c77c6e9 7 /*
tax 0:66300c77c6e9 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
tax 0:66300c77c6e9 9 * All rights reserved.
tax 0:66300c77c6e9 10 *
tax 0:66300c77c6e9 11 * Redistribution and use in source and binary forms, with or without modification,
tax 0:66300c77c6e9 12 * are permitted provided that the following conditions are met:
tax 0:66300c77c6e9 13 *
tax 0:66300c77c6e9 14 * 1. Redistributions of source code must retain the above copyright notice,
tax 0:66300c77c6e9 15 * this list of conditions and the following disclaimer.
tax 0:66300c77c6e9 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
tax 0:66300c77c6e9 17 * this list of conditions and the following disclaimer in the documentation
tax 0:66300c77c6e9 18 * and/or other materials provided with the distribution.
tax 0:66300c77c6e9 19 * 3. The name of the author may not be used to endorse or promote products
tax 0:66300c77c6e9 20 * derived from this software without specific prior written permission.
tax 0:66300c77c6e9 21 *
tax 0:66300c77c6e9 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
tax 0:66300c77c6e9 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
tax 0:66300c77c6e9 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
tax 0:66300c77c6e9 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
tax 0:66300c77c6e9 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
tax 0:66300c77c6e9 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
tax 0:66300c77c6e9 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
tax 0:66300c77c6e9 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
tax 0:66300c77c6e9 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
tax 0:66300c77c6e9 31 * OF SUCH DAMAGE.
tax 0:66300c77c6e9 32 *
tax 0:66300c77c6e9 33 * This file is part of the lwIP TCP/IP stack.
tax 0:66300c77c6e9 34 *
tax 0:66300c77c6e9 35 * Author: Adam Dunkels <adam@sics.se>
tax 0:66300c77c6e9 36 *
tax 0:66300c77c6e9 37 */
tax 0:66300c77c6e9 38
tax 0:66300c77c6e9 39 #include "lwip/opt.h"
tax 0:66300c77c6e9 40 #include "lwip/ip_addr.h"
tax 0:66300c77c6e9 41 #include "lwip/netif.h"
tax 0:66300c77c6e9 42
tax 0:66300c77c6e9 43 /* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
tax 0:66300c77c6e9 44 const ip_addr_t ip_addr_any = { IPADDR_ANY };
tax 0:66300c77c6e9 45 const ip_addr_t ip_addr_broadcast = { IPADDR_BROADCAST };
tax 0:66300c77c6e9 46
tax 0:66300c77c6e9 47 /**
tax 0:66300c77c6e9 48 * Determine if an address is a broadcast address on a network interface
tax 0:66300c77c6e9 49 *
tax 0:66300c77c6e9 50 * @param addr address to be checked
tax 0:66300c77c6e9 51 * @param netif the network interface against which the address is checked
tax 0:66300c77c6e9 52 * @return returns non-zero if the address is a broadcast address
tax 0:66300c77c6e9 53 */
tax 0:66300c77c6e9 54 u8_t
tax 0:66300c77c6e9 55 ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)
tax 0:66300c77c6e9 56 {
tax 0:66300c77c6e9 57 ip_addr_t ipaddr;
tax 0:66300c77c6e9 58 ip4_addr_set_u32(&ipaddr, addr);
tax 0:66300c77c6e9 59
tax 0:66300c77c6e9 60 /* all ones (broadcast) or all zeroes (old skool broadcast) */
tax 0:66300c77c6e9 61 if ((~addr == IPADDR_ANY) ||
tax 0:66300c77c6e9 62 (addr == IPADDR_ANY)) {
tax 0:66300c77c6e9 63 return 1;
tax 0:66300c77c6e9 64 /* no broadcast support on this network interface? */
tax 0:66300c77c6e9 65 } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
tax 0:66300c77c6e9 66 /* the given address cannot be a broadcast address
tax 0:66300c77c6e9 67 * nor can we check against any broadcast addresses */
tax 0:66300c77c6e9 68 return 0;
tax 0:66300c77c6e9 69 /* address matches network interface address exactly? => no broadcast */
tax 0:66300c77c6e9 70 } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) {
tax 0:66300c77c6e9 71 return 0;
tax 0:66300c77c6e9 72 /* on the same (sub) network... */
tax 0:66300c77c6e9 73 } else if (ip_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask))
tax 0:66300c77c6e9 74 /* ...and host identifier bits are all ones? =>... */
tax 0:66300c77c6e9 75 && ((addr & ~ip4_addr_get_u32(&netif->netmask)) ==
tax 0:66300c77c6e9 76 (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) {
tax 0:66300c77c6e9 77 /* => network broadcast address */
tax 0:66300c77c6e9 78 return 1;
tax 0:66300c77c6e9 79 } else {
tax 0:66300c77c6e9 80 return 0;
tax 0:66300c77c6e9 81 }
tax 0:66300c77c6e9 82 }
tax 0:66300c77c6e9 83
tax 0:66300c77c6e9 84 /** Checks if a netmask is valid (starting with ones, then only zeros)
tax 0:66300c77c6e9 85 *
tax 0:66300c77c6e9 86 * @param netmask the IPv4 netmask to check (in network byte order!)
tax 0:66300c77c6e9 87 * @return 1 if the netmask is valid, 0 if it is not
tax 0:66300c77c6e9 88 */
tax 0:66300c77c6e9 89 u8_t
tax 0:66300c77c6e9 90 ip4_addr_netmask_valid(u32_t netmask)
tax 0:66300c77c6e9 91 {
tax 0:66300c77c6e9 92 u32_t mask;
tax 0:66300c77c6e9 93 u32_t nm_hostorder = lwip_htonl(netmask);
tax 0:66300c77c6e9 94
tax 0:66300c77c6e9 95 /* first, check for the first zero */
tax 0:66300c77c6e9 96 for (mask = 1U << 31 ; mask != 0; mask >>= 1) {
tax 0:66300c77c6e9 97 if ((nm_hostorder & mask) == 0) {
tax 0:66300c77c6e9 98 break;
tax 0:66300c77c6e9 99 }
tax 0:66300c77c6e9 100 }
tax 0:66300c77c6e9 101 /* then check that there is no one */
tax 0:66300c77c6e9 102 for (; mask != 0; mask >>= 1) {
tax 0:66300c77c6e9 103 if ((nm_hostorder & mask) != 0) {
tax 0:66300c77c6e9 104 /* there is a one after the first zero -> invalid */
tax 0:66300c77c6e9 105 return 0;
tax 0:66300c77c6e9 106 }
tax 0:66300c77c6e9 107 }
tax 0:66300c77c6e9 108 /* no one after the first zero -> valid */
tax 0:66300c77c6e9 109 return 1;
tax 0:66300c77c6e9 110 }
tax 0:66300c77c6e9 111
tax 0:66300c77c6e9 112 /* Here for now until needed in other places in lwIP */
tax 0:66300c77c6e9 113 #ifndef isprint
tax 0:66300c77c6e9 114 #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
tax 0:66300c77c6e9 115 #define isprint(c) in_range(c, 0x20, 0x7f)
tax 0:66300c77c6e9 116 #define isdigit(c) in_range(c, '0', '9')
tax 0:66300c77c6e9 117 #define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
tax 0:66300c77c6e9 118 #define islower(c) in_range(c, 'a', 'z')
tax 0:66300c77c6e9 119 #define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
tax 0:66300c77c6e9 120 #endif
tax 0:66300c77c6e9 121
tax 0:66300c77c6e9 122 /**
tax 0:66300c77c6e9 123 * Ascii internet address interpretation routine.
tax 0:66300c77c6e9 124 * The value returned is in network order.
tax 0:66300c77c6e9 125 *
tax 0:66300c77c6e9 126 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
tax 0:66300c77c6e9 127 * @return ip address in network order
tax 0:66300c77c6e9 128 */
tax 0:66300c77c6e9 129 u32_t
tax 0:66300c77c6e9 130 ipaddr_addr(const char *cp)
tax 0:66300c77c6e9 131 {
tax 0:66300c77c6e9 132 ip_addr_t val;
tax 0:66300c77c6e9 133
tax 0:66300c77c6e9 134 if (ipaddr_aton(cp, &val)) {
tax 0:66300c77c6e9 135 return ip4_addr_get_u32(&val);
tax 0:66300c77c6e9 136 }
tax 0:66300c77c6e9 137 return (IPADDR_NONE);
tax 0:66300c77c6e9 138 }
tax 0:66300c77c6e9 139
tax 0:66300c77c6e9 140 /**
tax 0:66300c77c6e9 141 * Check whether "cp" is a valid ascii representation
tax 0:66300c77c6e9 142 * of an Internet address and convert to a binary address.
tax 0:66300c77c6e9 143 * Returns 1 if the address is valid, 0 if not.
tax 0:66300c77c6e9 144 * This replaces inet_addr, the return value from which
tax 0:66300c77c6e9 145 * cannot distinguish between failure and a local broadcast address.
tax 0:66300c77c6e9 146 *
tax 0:66300c77c6e9 147 * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
tax 0:66300c77c6e9 148 * @param addr pointer to which to save the ip address in network order
tax 0:66300c77c6e9 149 * @return 1 if cp could be converted to addr, 0 on failure
tax 0:66300c77c6e9 150 */
tax 0:66300c77c6e9 151 int
tax 0:66300c77c6e9 152 ipaddr_aton(const char *cp, ip_addr_t *addr)
tax 0:66300c77c6e9 153 {
tax 0:66300c77c6e9 154 u32_t val;
tax 0:66300c77c6e9 155 u8_t base;
tax 0:66300c77c6e9 156 char c;
tax 0:66300c77c6e9 157 u32_t parts[4];
tax 0:66300c77c6e9 158 u32_t *pp = parts;
tax 0:66300c77c6e9 159
tax 0:66300c77c6e9 160 c = *cp;
tax 0:66300c77c6e9 161 for (;;) {
tax 0:66300c77c6e9 162 /*
tax 0:66300c77c6e9 163 * Collect number up to ``.''.
tax 0:66300c77c6e9 164 * Values are specified as for C:
tax 0:66300c77c6e9 165 * 0x=hex, 0=octal, 1-9=decimal.
tax 0:66300c77c6e9 166 */
tax 0:66300c77c6e9 167 if (!isdigit(c))
tax 0:66300c77c6e9 168 return (0);
tax 0:66300c77c6e9 169 val = 0;
tax 0:66300c77c6e9 170 base = 10;
tax 0:66300c77c6e9 171 if (c == '0') {
tax 0:66300c77c6e9 172 c = *++cp;
tax 0:66300c77c6e9 173 if (c == 'x' || c == 'X') {
tax 0:66300c77c6e9 174 base = 16;
tax 0:66300c77c6e9 175 c = *++cp;
tax 0:66300c77c6e9 176 } else
tax 0:66300c77c6e9 177 base = 8;
tax 0:66300c77c6e9 178 }
tax 0:66300c77c6e9 179 for (;;) {
tax 0:66300c77c6e9 180 if (isdigit(c)) {
tax 0:66300c77c6e9 181 val = (val * base) + (int)(c - '0');
tax 0:66300c77c6e9 182 c = *++cp;
tax 0:66300c77c6e9 183 } else if (base == 16 && isxdigit(c)) {
tax 0:66300c77c6e9 184 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
tax 0:66300c77c6e9 185 c = *++cp;
tax 0:66300c77c6e9 186 } else
tax 0:66300c77c6e9 187 break;
tax 0:66300c77c6e9 188 }
tax 0:66300c77c6e9 189 if (c == '.') {
tax 0:66300c77c6e9 190 /*
tax 0:66300c77c6e9 191 * Internet format:
tax 0:66300c77c6e9 192 * a.b.c.d
tax 0:66300c77c6e9 193 * a.b.c (with c treated as 16 bits)
tax 0:66300c77c6e9 194 * a.b (with b treated as 24 bits)
tax 0:66300c77c6e9 195 */
tax 0:66300c77c6e9 196 if (pp >= parts + 3) {
tax 0:66300c77c6e9 197 return (0);
tax 0:66300c77c6e9 198 }
tax 0:66300c77c6e9 199 *pp++ = val;
tax 0:66300c77c6e9 200 c = *++cp;
tax 0:66300c77c6e9 201 } else
tax 0:66300c77c6e9 202 break;
tax 0:66300c77c6e9 203 }
tax 0:66300c77c6e9 204 /*
tax 0:66300c77c6e9 205 * Check for trailing characters.
tax 0:66300c77c6e9 206 */
tax 0:66300c77c6e9 207 if (c != '\0' && !isspace(c)) {
tax 0:66300c77c6e9 208 return (0);
tax 0:66300c77c6e9 209 }
tax 0:66300c77c6e9 210 /*
tax 0:66300c77c6e9 211 * Concoct the address according to
tax 0:66300c77c6e9 212 * the number of parts specified.
tax 0:66300c77c6e9 213 */
tax 0:66300c77c6e9 214 switch (pp - parts + 1) {
tax 0:66300c77c6e9 215
tax 0:66300c77c6e9 216 case 0:
tax 0:66300c77c6e9 217 return (0); /* initial nondigit */
tax 0:66300c77c6e9 218
tax 0:66300c77c6e9 219 case 1: /* a -- 32 bits */
tax 0:66300c77c6e9 220 break;
tax 0:66300c77c6e9 221
tax 0:66300c77c6e9 222 case 2: /* a.b -- 8.24 bits */
tax 0:66300c77c6e9 223 if (val > 0xffffffUL) {
tax 0:66300c77c6e9 224 return (0);
tax 0:66300c77c6e9 225 }
tax 0:66300c77c6e9 226 val |= parts[0] << 24;
tax 0:66300c77c6e9 227 break;
tax 0:66300c77c6e9 228
tax 0:66300c77c6e9 229 case 3: /* a.b.c -- 8.8.16 bits */
tax 0:66300c77c6e9 230 if (val > 0xffff) {
tax 0:66300c77c6e9 231 return (0);
tax 0:66300c77c6e9 232 }
tax 0:66300c77c6e9 233 val |= (parts[0] << 24) | (parts[1] << 16);
tax 0:66300c77c6e9 234 break;
tax 0:66300c77c6e9 235
tax 0:66300c77c6e9 236 case 4: /* a.b.c.d -- 8.8.8.8 bits */
tax 0:66300c77c6e9 237 if (val > 0xff) {
tax 0:66300c77c6e9 238 return (0);
tax 0:66300c77c6e9 239 }
tax 0:66300c77c6e9 240 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
tax 0:66300c77c6e9 241 break;
tax 0:66300c77c6e9 242 default:
tax 0:66300c77c6e9 243 LWIP_ASSERT("unhandled", 0);
tax 0:66300c77c6e9 244 break;
tax 0:66300c77c6e9 245 }
tax 0:66300c77c6e9 246 if (addr) {
tax 0:66300c77c6e9 247 ip4_addr_set_u32(addr, htonl(val));
tax 0:66300c77c6e9 248 }
tax 0:66300c77c6e9 249 return (1);
tax 0:66300c77c6e9 250 }
tax 0:66300c77c6e9 251
tax 0:66300c77c6e9 252 /**
tax 0:66300c77c6e9 253 * Convert numeric IP address into decimal dotted ASCII representation.
tax 0:66300c77c6e9 254 * returns ptr to static buffer; not reentrant!
tax 0:66300c77c6e9 255 *
tax 0:66300c77c6e9 256 * @param addr ip address in network order to convert
tax 0:66300c77c6e9 257 * @return pointer to a global static (!) buffer that holds the ASCII
tax 0:66300c77c6e9 258 * represenation of addr
tax 0:66300c77c6e9 259 */
tax 0:66300c77c6e9 260 char *
tax 0:66300c77c6e9 261 ipaddr_ntoa(const ip_addr_t *addr)
tax 0:66300c77c6e9 262 {
tax 0:66300c77c6e9 263 static char str[16];
tax 0:66300c77c6e9 264 return ipaddr_ntoa_r(addr, str, 16);
tax 0:66300c77c6e9 265 }
tax 0:66300c77c6e9 266
tax 0:66300c77c6e9 267 /**
tax 0:66300c77c6e9 268 * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
tax 0:66300c77c6e9 269 *
tax 0:66300c77c6e9 270 * @param addr ip address in network order to convert
tax 0:66300c77c6e9 271 * @param buf target buffer where the string is stored
tax 0:66300c77c6e9 272 * @param buflen length of buf
tax 0:66300c77c6e9 273 * @return either pointer to buf which now holds the ASCII
tax 0:66300c77c6e9 274 * representation of addr or NULL if buf was too small
tax 0:66300c77c6e9 275 */
tax 0:66300c77c6e9 276 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
tax 0:66300c77c6e9 277 {
tax 0:66300c77c6e9 278 u32_t s_addr;
tax 0:66300c77c6e9 279 char inv[3];
tax 0:66300c77c6e9 280 char *rp;
tax 0:66300c77c6e9 281 u8_t *ap;
tax 0:66300c77c6e9 282 u8_t rem;
tax 0:66300c77c6e9 283 u8_t n;
tax 0:66300c77c6e9 284 u8_t i;
tax 0:66300c77c6e9 285 int len = 0;
tax 0:66300c77c6e9 286
tax 0:66300c77c6e9 287 s_addr = ip4_addr_get_u32(addr);
tax 0:66300c77c6e9 288
tax 0:66300c77c6e9 289 rp = buf;
tax 0:66300c77c6e9 290 ap = (u8_t *)&s_addr;
tax 0:66300c77c6e9 291 for(n = 0; n < 4; n++) {
tax 0:66300c77c6e9 292 i = 0;
tax 0:66300c77c6e9 293 do {
tax 0:66300c77c6e9 294 rem = *ap % (u8_t)10;
tax 0:66300c77c6e9 295 *ap /= (u8_t)10;
tax 0:66300c77c6e9 296 inv[i++] = '0' + rem;
tax 0:66300c77c6e9 297 } while(*ap);
tax 0:66300c77c6e9 298 while(i--) {
tax 0:66300c77c6e9 299 if (len++ >= buflen) {
tax 0:66300c77c6e9 300 return NULL;
tax 0:66300c77c6e9 301 }
tax 0:66300c77c6e9 302 *rp++ = inv[i];
tax 0:66300c77c6e9 303 }
tax 0:66300c77c6e9 304 if (len++ >= buflen) {
tax 0:66300c77c6e9 305 return NULL;
tax 0:66300c77c6e9 306 }
tax 0:66300c77c6e9 307 *rp++ = '.';
tax 0:66300c77c6e9 308 ap++;
tax 0:66300c77c6e9 309 }
tax 0:66300c77c6e9 310 *--rp = 0;
tax 0:66300c77c6e9 311 return buf;
tax 0:66300c77c6e9 312 }