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 * Ethernet Interface Skeleton
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 /*
tax 0:66300c77c6e9 40 * This file is a skeleton for developing Ethernet network interface
tax 0:66300c77c6e9 41 * drivers for lwIP. Add code to the low_level functions and do a
tax 0:66300c77c6e9 42 * search-and-replace for the word "ethernetif" to replace it with
tax 0:66300c77c6e9 43 * something that better describes your network interface.
tax 0:66300c77c6e9 44 */
tax 0:66300c77c6e9 45
tax 0:66300c77c6e9 46 #include "lwip/opt.h"
tax 0:66300c77c6e9 47
tax 0:66300c77c6e9 48 #if 0 /* don't build, this is only a skeleton, see previous comment */
tax 0:66300c77c6e9 49
tax 0:66300c77c6e9 50 #include "lwip/def.h"
tax 0:66300c77c6e9 51 #include "lwip/mem.h"
tax 0:66300c77c6e9 52 #include "lwip/pbuf.h"
tax 0:66300c77c6e9 53 #include "lwip/sys.h"
tax 0:66300c77c6e9 54 #include <lwip/stats.h>
tax 0:66300c77c6e9 55 #include <lwip/snmp.h>
tax 0:66300c77c6e9 56 #include "netif/etharp.h"
tax 0:66300c77c6e9 57 #include "netif/ppp_oe.h"
tax 0:66300c77c6e9 58
tax 0:66300c77c6e9 59 /* Define those to better describe your network interface. */
tax 0:66300c77c6e9 60 #define IFNAME0 'e'
tax 0:66300c77c6e9 61 #define IFNAME1 'n'
tax 0:66300c77c6e9 62
tax 0:66300c77c6e9 63 /**
tax 0:66300c77c6e9 64 * Helper struct to hold private data used to operate your ethernet interface.
tax 0:66300c77c6e9 65 * Keeping the ethernet address of the MAC in this struct is not necessary
tax 0:66300c77c6e9 66 * as it is already kept in the struct netif.
tax 0:66300c77c6e9 67 * But this is only an example, anyway...
tax 0:66300c77c6e9 68 */
tax 0:66300c77c6e9 69 struct ethernetif {
tax 0:66300c77c6e9 70 struct eth_addr *ethaddr;
tax 0:66300c77c6e9 71 /* Add whatever per-interface state that is needed here. */
tax 0:66300c77c6e9 72 };
tax 0:66300c77c6e9 73
tax 0:66300c77c6e9 74 /* Forward declarations. */
tax 0:66300c77c6e9 75 static void ethernetif_input(struct netif *netif);
tax 0:66300c77c6e9 76
tax 0:66300c77c6e9 77 /**
tax 0:66300c77c6e9 78 * In this function, the hardware should be initialized.
tax 0:66300c77c6e9 79 * Called from ethernetif_init().
tax 0:66300c77c6e9 80 *
tax 0:66300c77c6e9 81 * @param netif the already initialized lwip network interface structure
tax 0:66300c77c6e9 82 * for this ethernetif
tax 0:66300c77c6e9 83 */
tax 0:66300c77c6e9 84 static void
tax 0:66300c77c6e9 85 low_level_init(struct netif *netif)
tax 0:66300c77c6e9 86 {
tax 0:66300c77c6e9 87 struct ethernetif *ethernetif = netif->state;
tax 0:66300c77c6e9 88
tax 0:66300c77c6e9 89 /* set MAC hardware address length */
tax 0:66300c77c6e9 90 netif->hwaddr_len = ETHARP_HWADDR_LEN;
tax 0:66300c77c6e9 91
tax 0:66300c77c6e9 92 /* set MAC hardware address */
tax 0:66300c77c6e9 93 netif->hwaddr[0] = ;
tax 0:66300c77c6e9 94 ...
tax 0:66300c77c6e9 95 netif->hwaddr[5] = ;
tax 0:66300c77c6e9 96
tax 0:66300c77c6e9 97 /* maximum transfer unit */
tax 0:66300c77c6e9 98 netif->mtu = 1500;
tax 0:66300c77c6e9 99
tax 0:66300c77c6e9 100 /* device capabilities */
tax 0:66300c77c6e9 101 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
tax 0:66300c77c6e9 102 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
tax 0:66300c77c6e9 103
tax 0:66300c77c6e9 104 /* Do whatever else is needed to initialize interface. */
tax 0:66300c77c6e9 105 }
tax 0:66300c77c6e9 106
tax 0:66300c77c6e9 107 /**
tax 0:66300c77c6e9 108 * This function should do the actual transmission of the packet. The packet is
tax 0:66300c77c6e9 109 * contained in the pbuf that is passed to the function. This pbuf
tax 0:66300c77c6e9 110 * might be chained.
tax 0:66300c77c6e9 111 *
tax 0:66300c77c6e9 112 * @param netif the lwip network interface structure for this ethernetif
tax 0:66300c77c6e9 113 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
tax 0:66300c77c6e9 114 * @return ERR_OK if the packet could be sent
tax 0:66300c77c6e9 115 * an err_t value if the packet couldn't be sent
tax 0:66300c77c6e9 116 *
tax 0:66300c77c6e9 117 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
tax 0:66300c77c6e9 118 * strange results. You might consider waiting for space in the DMA queue
tax 0:66300c77c6e9 119 * to become availale since the stack doesn't retry to send a packet
tax 0:66300c77c6e9 120 * dropped because of memory failure (except for the TCP timers).
tax 0:66300c77c6e9 121 */
tax 0:66300c77c6e9 122
tax 0:66300c77c6e9 123 static err_t
tax 0:66300c77c6e9 124 low_level_output(struct netif *netif, struct pbuf *p)
tax 0:66300c77c6e9 125 {
tax 0:66300c77c6e9 126 struct ethernetif *ethernetif = netif->state;
tax 0:66300c77c6e9 127 struct pbuf *q;
tax 0:66300c77c6e9 128
tax 0:66300c77c6e9 129 initiate transfer();
tax 0:66300c77c6e9 130
tax 0:66300c77c6e9 131 #if ETH_PAD_SIZE
tax 0:66300c77c6e9 132 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
tax 0:66300c77c6e9 133 #endif
tax 0:66300c77c6e9 134
tax 0:66300c77c6e9 135 for(q = p; q != NULL; q = q->next) {
tax 0:66300c77c6e9 136 /* Send the data from the pbuf to the interface, one pbuf at a
tax 0:66300c77c6e9 137 time. The size of the data in each pbuf is kept in the ->len
tax 0:66300c77c6e9 138 variable. */
tax 0:66300c77c6e9 139 send data from(q->payload, q->len);
tax 0:66300c77c6e9 140 }
tax 0:66300c77c6e9 141
tax 0:66300c77c6e9 142 signal that packet should be sent();
tax 0:66300c77c6e9 143
tax 0:66300c77c6e9 144 #if ETH_PAD_SIZE
tax 0:66300c77c6e9 145 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
tax 0:66300c77c6e9 146 #endif
tax 0:66300c77c6e9 147
tax 0:66300c77c6e9 148 LINK_STATS_INC(link.xmit);
tax 0:66300c77c6e9 149
tax 0:66300c77c6e9 150 return ERR_OK;
tax 0:66300c77c6e9 151 }
tax 0:66300c77c6e9 152
tax 0:66300c77c6e9 153 /**
tax 0:66300c77c6e9 154 * Should allocate a pbuf and transfer the bytes of the incoming
tax 0:66300c77c6e9 155 * packet from the interface into the pbuf.
tax 0:66300c77c6e9 156 *
tax 0:66300c77c6e9 157 * @param netif the lwip network interface structure for this ethernetif
tax 0:66300c77c6e9 158 * @return a pbuf filled with the received packet (including MAC header)
tax 0:66300c77c6e9 159 * NULL on memory error
tax 0:66300c77c6e9 160 */
tax 0:66300c77c6e9 161 static struct pbuf *
tax 0:66300c77c6e9 162 low_level_input(struct netif *netif)
tax 0:66300c77c6e9 163 {
tax 0:66300c77c6e9 164 struct ethernetif *ethernetif = netif->state;
tax 0:66300c77c6e9 165 struct pbuf *p, *q;
tax 0:66300c77c6e9 166 u16_t len;
tax 0:66300c77c6e9 167
tax 0:66300c77c6e9 168 /* Obtain the size of the packet and put it into the "len"
tax 0:66300c77c6e9 169 variable. */
tax 0:66300c77c6e9 170 len = ;
tax 0:66300c77c6e9 171
tax 0:66300c77c6e9 172 #if ETH_PAD_SIZE
tax 0:66300c77c6e9 173 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
tax 0:66300c77c6e9 174 #endif
tax 0:66300c77c6e9 175
tax 0:66300c77c6e9 176 /* We allocate a pbuf chain of pbufs from the pool. */
tax 0:66300c77c6e9 177 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
tax 0:66300c77c6e9 178
tax 0:66300c77c6e9 179 if (p != NULL) {
tax 0:66300c77c6e9 180
tax 0:66300c77c6e9 181 #if ETH_PAD_SIZE
tax 0:66300c77c6e9 182 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
tax 0:66300c77c6e9 183 #endif
tax 0:66300c77c6e9 184
tax 0:66300c77c6e9 185 /* We iterate over the pbuf chain until we have read the entire
tax 0:66300c77c6e9 186 * packet into the pbuf. */
tax 0:66300c77c6e9 187 for(q = p; q != NULL; q = q->next) {
tax 0:66300c77c6e9 188 /* Read enough bytes to fill this pbuf in the chain. The
tax 0:66300c77c6e9 189 * available data in the pbuf is given by the q->len
tax 0:66300c77c6e9 190 * variable.
tax 0:66300c77c6e9 191 * This does not necessarily have to be a memcpy, you can also preallocate
tax 0:66300c77c6e9 192 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
tax 0:66300c77c6e9 193 * actually received size. In this case, ensure the tot_len member of the
tax 0:66300c77c6e9 194 * pbuf is the sum of the chained pbuf len members.
tax 0:66300c77c6e9 195 */
tax 0:66300c77c6e9 196 read data into(q->payload, q->len);
tax 0:66300c77c6e9 197 }
tax 0:66300c77c6e9 198 acknowledge that packet has been read();
tax 0:66300c77c6e9 199
tax 0:66300c77c6e9 200 #if ETH_PAD_SIZE
tax 0:66300c77c6e9 201 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
tax 0:66300c77c6e9 202 #endif
tax 0:66300c77c6e9 203
tax 0:66300c77c6e9 204 LINK_STATS_INC(link.recv);
tax 0:66300c77c6e9 205 } else {
tax 0:66300c77c6e9 206 drop packet();
tax 0:66300c77c6e9 207 LINK_STATS_INC(link.memerr);
tax 0:66300c77c6e9 208 LINK_STATS_INC(link.drop);
tax 0:66300c77c6e9 209 }
tax 0:66300c77c6e9 210
tax 0:66300c77c6e9 211 return p;
tax 0:66300c77c6e9 212 }
tax 0:66300c77c6e9 213
tax 0:66300c77c6e9 214 /**
tax 0:66300c77c6e9 215 * This function should be called when a packet is ready to be read
tax 0:66300c77c6e9 216 * from the interface. It uses the function low_level_input() that
tax 0:66300c77c6e9 217 * should handle the actual reception of bytes from the network
tax 0:66300c77c6e9 218 * interface. Then the type of the received packet is determined and
tax 0:66300c77c6e9 219 * the appropriate input function is called.
tax 0:66300c77c6e9 220 *
tax 0:66300c77c6e9 221 * @param netif the lwip network interface structure for this ethernetif
tax 0:66300c77c6e9 222 */
tax 0:66300c77c6e9 223 static void
tax 0:66300c77c6e9 224 ethernetif_input(struct netif *netif)
tax 0:66300c77c6e9 225 {
tax 0:66300c77c6e9 226 struct ethernetif *ethernetif;
tax 0:66300c77c6e9 227 struct eth_hdr *ethhdr;
tax 0:66300c77c6e9 228 struct pbuf *p;
tax 0:66300c77c6e9 229
tax 0:66300c77c6e9 230 ethernetif = netif->state;
tax 0:66300c77c6e9 231
tax 0:66300c77c6e9 232 /* move received packet into a new pbuf */
tax 0:66300c77c6e9 233 p = low_level_input(netif);
tax 0:66300c77c6e9 234 /* no packet could be read, silently ignore this */
tax 0:66300c77c6e9 235 if (p == NULL) return;
tax 0:66300c77c6e9 236 /* points to packet payload, which starts with an Ethernet header */
tax 0:66300c77c6e9 237 ethhdr = p->payload;
tax 0:66300c77c6e9 238
tax 0:66300c77c6e9 239 switch (htons(ethhdr->type)) {
tax 0:66300c77c6e9 240 /* IP or ARP packet? */
tax 0:66300c77c6e9 241 case ETHTYPE_IP:
tax 0:66300c77c6e9 242 case ETHTYPE_ARP:
tax 0:66300c77c6e9 243 #if PPPOE_SUPPORT
tax 0:66300c77c6e9 244 /* PPPoE packet? */
tax 0:66300c77c6e9 245 case ETHTYPE_PPPOEDISC:
tax 0:66300c77c6e9 246 case ETHTYPE_PPPOE:
tax 0:66300c77c6e9 247 #endif /* PPPOE_SUPPORT */
tax 0:66300c77c6e9 248 /* full packet send to tcpip_thread to process */
tax 0:66300c77c6e9 249 if (netif->input(p, netif)!=ERR_OK)
tax 0:66300c77c6e9 250 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
tax 0:66300c77c6e9 251 pbuf_free(p);
tax 0:66300c77c6e9 252 p = NULL;
tax 0:66300c77c6e9 253 }
tax 0:66300c77c6e9 254 break;
tax 0:66300c77c6e9 255
tax 0:66300c77c6e9 256 default:
tax 0:66300c77c6e9 257 pbuf_free(p);
tax 0:66300c77c6e9 258 p = NULL;
tax 0:66300c77c6e9 259 break;
tax 0:66300c77c6e9 260 }
tax 0:66300c77c6e9 261 }
tax 0:66300c77c6e9 262
tax 0:66300c77c6e9 263 /**
tax 0:66300c77c6e9 264 * Should be called at the beginning of the program to set up the
tax 0:66300c77c6e9 265 * network interface. It calls the function low_level_init() to do the
tax 0:66300c77c6e9 266 * actual setup of the hardware.
tax 0:66300c77c6e9 267 *
tax 0:66300c77c6e9 268 * This function should be passed as a parameter to netif_add().
tax 0:66300c77c6e9 269 *
tax 0:66300c77c6e9 270 * @param netif the lwip network interface structure for this ethernetif
tax 0:66300c77c6e9 271 * @return ERR_OK if the loopif is initialized
tax 0:66300c77c6e9 272 * ERR_MEM if private data couldn't be allocated
tax 0:66300c77c6e9 273 * any other err_t on error
tax 0:66300c77c6e9 274 */
tax 0:66300c77c6e9 275 err_t
tax 0:66300c77c6e9 276 ethernetif_init(struct netif *netif)
tax 0:66300c77c6e9 277 {
tax 0:66300c77c6e9 278 struct ethernetif *ethernetif;
tax 0:66300c77c6e9 279
tax 0:66300c77c6e9 280 LWIP_ASSERT("netif != NULL", (netif != NULL));
tax 0:66300c77c6e9 281
tax 0:66300c77c6e9 282 ethernetif = mem_malloc(sizeof(struct ethernetif));
tax 0:66300c77c6e9 283 if (ethernetif == NULL) {
tax 0:66300c77c6e9 284 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
tax 0:66300c77c6e9 285 return ERR_MEM;
tax 0:66300c77c6e9 286 }
tax 0:66300c77c6e9 287
tax 0:66300c77c6e9 288 #if LWIP_NETIF_HOSTNAME
tax 0:66300c77c6e9 289 /* Initialize interface hostname */
tax 0:66300c77c6e9 290 netif->hostname = "lwip";
tax 0:66300c77c6e9 291 #endif /* LWIP_NETIF_HOSTNAME */
tax 0:66300c77c6e9 292
tax 0:66300c77c6e9 293 /*
tax 0:66300c77c6e9 294 * Initialize the snmp variables and counters inside the struct netif.
tax 0:66300c77c6e9 295 * The last argument should be replaced with your link speed, in units
tax 0:66300c77c6e9 296 * of bits per second.
tax 0:66300c77c6e9 297 */
tax 0:66300c77c6e9 298 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
tax 0:66300c77c6e9 299
tax 0:66300c77c6e9 300 netif->state = ethernetif;
tax 0:66300c77c6e9 301 netif->name[0] = IFNAME0;
tax 0:66300c77c6e9 302 netif->name[1] = IFNAME1;
tax 0:66300c77c6e9 303 /* We directly use etharp_output() here to save a function call.
tax 0:66300c77c6e9 304 * You can instead declare your own function an call etharp_output()
tax 0:66300c77c6e9 305 * from it if you have to do some checks before sending (e.g. if link
tax 0:66300c77c6e9 306 * is available...) */
tax 0:66300c77c6e9 307 netif->output = etharp_output;
tax 0:66300c77c6e9 308 netif->linkoutput = low_level_output;
tax 0:66300c77c6e9 309
tax 0:66300c77c6e9 310 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
tax 0:66300c77c6e9 311
tax 0:66300c77c6e9 312 /* initialize the hardware */
tax 0:66300c77c6e9 313 low_level_init(netif);
tax 0:66300c77c6e9 314
tax 0:66300c77c6e9 315 return ERR_OK;
tax 0:66300c77c6e9 316 }
tax 0:66300c77c6e9 317
tax 0:66300c77c6e9 318 #endif /* 0 */