Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.
Dependencies: mbed Socket lwip-eth lwip-sys lwip
EthernetInterface/lwip/api/netbuf.c@0:e7ca326e76ee, 2013-04-04 (annotated)
- Committer:
- mkersh3
- Date:
- Thu Apr 04 05:26:09 2013 +0000
- Revision:
- 0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mkersh3 | 0:e7ca326e76ee | 1 | /** |
mkersh3 | 0:e7ca326e76ee | 2 | * @file |
mkersh3 | 0:e7ca326e76ee | 3 | * Network buffer management |
mkersh3 | 0:e7ca326e76ee | 4 | * |
mkersh3 | 0:e7ca326e76ee | 5 | */ |
mkersh3 | 0:e7ca326e76ee | 6 | |
mkersh3 | 0:e7ca326e76ee | 7 | /* |
mkersh3 | 0:e7ca326e76ee | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
mkersh3 | 0:e7ca326e76ee | 9 | * All rights reserved. |
mkersh3 | 0:e7ca326e76ee | 10 | * |
mkersh3 | 0:e7ca326e76ee | 11 | * Redistribution and use in source and binary forms, with or without modification, |
mkersh3 | 0:e7ca326e76ee | 12 | * are permitted provided that the following conditions are met: |
mkersh3 | 0:e7ca326e76ee | 13 | * |
mkersh3 | 0:e7ca326e76ee | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
mkersh3 | 0:e7ca326e76ee | 15 | * this list of conditions and the following disclaimer. |
mkersh3 | 0:e7ca326e76ee | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mkersh3 | 0:e7ca326e76ee | 17 | * this list of conditions and the following disclaimer in the documentation |
mkersh3 | 0:e7ca326e76ee | 18 | * and/or other materials provided with the distribution. |
mkersh3 | 0:e7ca326e76ee | 19 | * 3. The name of the author may not be used to endorse or promote products |
mkersh3 | 0:e7ca326e76ee | 20 | * derived from this software without specific prior written permission. |
mkersh3 | 0:e7ca326e76ee | 21 | * |
mkersh3 | 0:e7ca326e76ee | 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
mkersh3 | 0:e7ca326e76ee | 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
mkersh3 | 0:e7ca326e76ee | 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
mkersh3 | 0:e7ca326e76ee | 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
mkersh3 | 0:e7ca326e76ee | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
mkersh3 | 0:e7ca326e76ee | 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
mkersh3 | 0:e7ca326e76ee | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
mkersh3 | 0:e7ca326e76ee | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
mkersh3 | 0:e7ca326e76ee | 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
mkersh3 | 0:e7ca326e76ee | 31 | * OF SUCH DAMAGE. |
mkersh3 | 0:e7ca326e76ee | 32 | * |
mkersh3 | 0:e7ca326e76ee | 33 | * This file is part of the lwIP TCP/IP stack. |
mkersh3 | 0:e7ca326e76ee | 34 | * |
mkersh3 | 0:e7ca326e76ee | 35 | * Author: Adam Dunkels <adam@sics.se> |
mkersh3 | 0:e7ca326e76ee | 36 | * |
mkersh3 | 0:e7ca326e76ee | 37 | */ |
mkersh3 | 0:e7ca326e76ee | 38 | |
mkersh3 | 0:e7ca326e76ee | 39 | #include "lwip/opt.h" |
mkersh3 | 0:e7ca326e76ee | 40 | |
mkersh3 | 0:e7ca326e76ee | 41 | #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */ |
mkersh3 | 0:e7ca326e76ee | 42 | |
mkersh3 | 0:e7ca326e76ee | 43 | #include "lwip/netbuf.h" |
mkersh3 | 0:e7ca326e76ee | 44 | #include "lwip/memp.h" |
mkersh3 | 0:e7ca326e76ee | 45 | |
mkersh3 | 0:e7ca326e76ee | 46 | #include <string.h> |
mkersh3 | 0:e7ca326e76ee | 47 | |
mkersh3 | 0:e7ca326e76ee | 48 | /** |
mkersh3 | 0:e7ca326e76ee | 49 | * Create (allocate) and initialize a new netbuf. |
mkersh3 | 0:e7ca326e76ee | 50 | * The netbuf doesn't yet contain a packet buffer! |
mkersh3 | 0:e7ca326e76ee | 51 | * |
mkersh3 | 0:e7ca326e76ee | 52 | * @return a pointer to a new netbuf |
mkersh3 | 0:e7ca326e76ee | 53 | * NULL on lack of memory |
mkersh3 | 0:e7ca326e76ee | 54 | */ |
mkersh3 | 0:e7ca326e76ee | 55 | struct |
mkersh3 | 0:e7ca326e76ee | 56 | netbuf *netbuf_new(void) |
mkersh3 | 0:e7ca326e76ee | 57 | { |
mkersh3 | 0:e7ca326e76ee | 58 | struct netbuf *buf; |
mkersh3 | 0:e7ca326e76ee | 59 | |
mkersh3 | 0:e7ca326e76ee | 60 | buf = (struct netbuf *)memp_malloc(MEMP_NETBUF); |
mkersh3 | 0:e7ca326e76ee | 61 | if (buf != NULL) { |
mkersh3 | 0:e7ca326e76ee | 62 | buf->p = NULL; |
mkersh3 | 0:e7ca326e76ee | 63 | buf->ptr = NULL; |
mkersh3 | 0:e7ca326e76ee | 64 | ip_addr_set_any(&buf->addr); |
mkersh3 | 0:e7ca326e76ee | 65 | buf->port = 0; |
mkersh3 | 0:e7ca326e76ee | 66 | #if LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY |
mkersh3 | 0:e7ca326e76ee | 67 | #if LWIP_CHECKSUM_ON_COPY |
mkersh3 | 0:e7ca326e76ee | 68 | buf->flags = 0; |
mkersh3 | 0:e7ca326e76ee | 69 | #endif /* LWIP_CHECKSUM_ON_COPY */ |
mkersh3 | 0:e7ca326e76ee | 70 | buf->toport_chksum = 0; |
mkersh3 | 0:e7ca326e76ee | 71 | #if LWIP_NETBUF_RECVINFO |
mkersh3 | 0:e7ca326e76ee | 72 | ip_addr_set_any(&buf->toaddr); |
mkersh3 | 0:e7ca326e76ee | 73 | #endif /* LWIP_NETBUF_RECVINFO */ |
mkersh3 | 0:e7ca326e76ee | 74 | #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */ |
mkersh3 | 0:e7ca326e76ee | 75 | return buf; |
mkersh3 | 0:e7ca326e76ee | 76 | } else { |
mkersh3 | 0:e7ca326e76ee | 77 | return NULL; |
mkersh3 | 0:e7ca326e76ee | 78 | } |
mkersh3 | 0:e7ca326e76ee | 79 | } |
mkersh3 | 0:e7ca326e76ee | 80 | |
mkersh3 | 0:e7ca326e76ee | 81 | /** |
mkersh3 | 0:e7ca326e76ee | 82 | * Deallocate a netbuf allocated by netbuf_new(). |
mkersh3 | 0:e7ca326e76ee | 83 | * |
mkersh3 | 0:e7ca326e76ee | 84 | * @param buf pointer to a netbuf allocated by netbuf_new() |
mkersh3 | 0:e7ca326e76ee | 85 | */ |
mkersh3 | 0:e7ca326e76ee | 86 | void |
mkersh3 | 0:e7ca326e76ee | 87 | netbuf_delete(struct netbuf *buf) |
mkersh3 | 0:e7ca326e76ee | 88 | { |
mkersh3 | 0:e7ca326e76ee | 89 | if (buf != NULL) { |
mkersh3 | 0:e7ca326e76ee | 90 | if (buf->p != NULL) { |
mkersh3 | 0:e7ca326e76ee | 91 | pbuf_free(buf->p); |
mkersh3 | 0:e7ca326e76ee | 92 | buf->p = buf->ptr = NULL; |
mkersh3 | 0:e7ca326e76ee | 93 | } |
mkersh3 | 0:e7ca326e76ee | 94 | memp_free(MEMP_NETBUF, buf); |
mkersh3 | 0:e7ca326e76ee | 95 | } |
mkersh3 | 0:e7ca326e76ee | 96 | } |
mkersh3 | 0:e7ca326e76ee | 97 | |
mkersh3 | 0:e7ca326e76ee | 98 | /** |
mkersh3 | 0:e7ca326e76ee | 99 | * Allocate memory for a packet buffer for a given netbuf. |
mkersh3 | 0:e7ca326e76ee | 100 | * |
mkersh3 | 0:e7ca326e76ee | 101 | * @param buf the netbuf for which to allocate a packet buffer |
mkersh3 | 0:e7ca326e76ee | 102 | * @param size the size of the packet buffer to allocate |
mkersh3 | 0:e7ca326e76ee | 103 | * @return pointer to the allocated memory |
mkersh3 | 0:e7ca326e76ee | 104 | * NULL if no memory could be allocated |
mkersh3 | 0:e7ca326e76ee | 105 | */ |
mkersh3 | 0:e7ca326e76ee | 106 | void * |
mkersh3 | 0:e7ca326e76ee | 107 | netbuf_alloc(struct netbuf *buf, u16_t size) |
mkersh3 | 0:e7ca326e76ee | 108 | { |
mkersh3 | 0:e7ca326e76ee | 109 | LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;); |
mkersh3 | 0:e7ca326e76ee | 110 | |
mkersh3 | 0:e7ca326e76ee | 111 | /* Deallocate any previously allocated memory. */ |
mkersh3 | 0:e7ca326e76ee | 112 | if (buf->p != NULL) { |
mkersh3 | 0:e7ca326e76ee | 113 | pbuf_free(buf->p); |
mkersh3 | 0:e7ca326e76ee | 114 | } |
mkersh3 | 0:e7ca326e76ee | 115 | buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM); |
mkersh3 | 0:e7ca326e76ee | 116 | if (buf->p == NULL) { |
mkersh3 | 0:e7ca326e76ee | 117 | return NULL; |
mkersh3 | 0:e7ca326e76ee | 118 | } |
mkersh3 | 0:e7ca326e76ee | 119 | LWIP_ASSERT("check that first pbuf can hold size", |
mkersh3 | 0:e7ca326e76ee | 120 | (buf->p->len >= size)); |
mkersh3 | 0:e7ca326e76ee | 121 | buf->ptr = buf->p; |
mkersh3 | 0:e7ca326e76ee | 122 | return buf->p->payload; |
mkersh3 | 0:e7ca326e76ee | 123 | } |
mkersh3 | 0:e7ca326e76ee | 124 | |
mkersh3 | 0:e7ca326e76ee | 125 | /** |
mkersh3 | 0:e7ca326e76ee | 126 | * Free the packet buffer included in a netbuf |
mkersh3 | 0:e7ca326e76ee | 127 | * |
mkersh3 | 0:e7ca326e76ee | 128 | * @param buf pointer to the netbuf which contains the packet buffer to free |
mkersh3 | 0:e7ca326e76ee | 129 | */ |
mkersh3 | 0:e7ca326e76ee | 130 | void |
mkersh3 | 0:e7ca326e76ee | 131 | netbuf_free(struct netbuf *buf) |
mkersh3 | 0:e7ca326e76ee | 132 | { |
mkersh3 | 0:e7ca326e76ee | 133 | LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;); |
mkersh3 | 0:e7ca326e76ee | 134 | if (buf->p != NULL) { |
mkersh3 | 0:e7ca326e76ee | 135 | pbuf_free(buf->p); |
mkersh3 | 0:e7ca326e76ee | 136 | } |
mkersh3 | 0:e7ca326e76ee | 137 | buf->p = buf->ptr = NULL; |
mkersh3 | 0:e7ca326e76ee | 138 | } |
mkersh3 | 0:e7ca326e76ee | 139 | |
mkersh3 | 0:e7ca326e76ee | 140 | /** |
mkersh3 | 0:e7ca326e76ee | 141 | * Let a netbuf reference existing (non-volatile) data. |
mkersh3 | 0:e7ca326e76ee | 142 | * |
mkersh3 | 0:e7ca326e76ee | 143 | * @param buf netbuf which should reference the data |
mkersh3 | 0:e7ca326e76ee | 144 | * @param dataptr pointer to the data to reference |
mkersh3 | 0:e7ca326e76ee | 145 | * @param size size of the data |
mkersh3 | 0:e7ca326e76ee | 146 | * @return ERR_OK if data is referenced |
mkersh3 | 0:e7ca326e76ee | 147 | * ERR_MEM if data couldn't be referenced due to lack of memory |
mkersh3 | 0:e7ca326e76ee | 148 | */ |
mkersh3 | 0:e7ca326e76ee | 149 | err_t |
mkersh3 | 0:e7ca326e76ee | 150 | netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size) |
mkersh3 | 0:e7ca326e76ee | 151 | { |
mkersh3 | 0:e7ca326e76ee | 152 | LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;); |
mkersh3 | 0:e7ca326e76ee | 153 | if (buf->p != NULL) { |
mkersh3 | 0:e7ca326e76ee | 154 | pbuf_free(buf->p); |
mkersh3 | 0:e7ca326e76ee | 155 | } |
mkersh3 | 0:e7ca326e76ee | 156 | buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF); |
mkersh3 | 0:e7ca326e76ee | 157 | if (buf->p == NULL) { |
mkersh3 | 0:e7ca326e76ee | 158 | buf->ptr = NULL; |
mkersh3 | 0:e7ca326e76ee | 159 | return ERR_MEM; |
mkersh3 | 0:e7ca326e76ee | 160 | } |
mkersh3 | 0:e7ca326e76ee | 161 | buf->p->payload = (void*)dataptr; |
mkersh3 | 0:e7ca326e76ee | 162 | buf->p->len = buf->p->tot_len = size; |
mkersh3 | 0:e7ca326e76ee | 163 | buf->ptr = buf->p; |
mkersh3 | 0:e7ca326e76ee | 164 | return ERR_OK; |
mkersh3 | 0:e7ca326e76ee | 165 | } |
mkersh3 | 0:e7ca326e76ee | 166 | |
mkersh3 | 0:e7ca326e76ee | 167 | /** |
mkersh3 | 0:e7ca326e76ee | 168 | * Chain one netbuf to another (@see pbuf_chain) |
mkersh3 | 0:e7ca326e76ee | 169 | * |
mkersh3 | 0:e7ca326e76ee | 170 | * @param head the first netbuf |
mkersh3 | 0:e7ca326e76ee | 171 | * @param tail netbuf to chain after head, freed by this function, may not be reference after returning |
mkersh3 | 0:e7ca326e76ee | 172 | */ |
mkersh3 | 0:e7ca326e76ee | 173 | void |
mkersh3 | 0:e7ca326e76ee | 174 | netbuf_chain(struct netbuf *head, struct netbuf *tail) |
mkersh3 | 0:e7ca326e76ee | 175 | { |
mkersh3 | 0:e7ca326e76ee | 176 | LWIP_ERROR("netbuf_ref: invalid head", (head != NULL), return;); |
mkersh3 | 0:e7ca326e76ee | 177 | LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;); |
mkersh3 | 0:e7ca326e76ee | 178 | pbuf_cat(head->p, tail->p); |
mkersh3 | 0:e7ca326e76ee | 179 | head->ptr = head->p; |
mkersh3 | 0:e7ca326e76ee | 180 | memp_free(MEMP_NETBUF, tail); |
mkersh3 | 0:e7ca326e76ee | 181 | } |
mkersh3 | 0:e7ca326e76ee | 182 | |
mkersh3 | 0:e7ca326e76ee | 183 | /** |
mkersh3 | 0:e7ca326e76ee | 184 | * Get the data pointer and length of the data inside a netbuf. |
mkersh3 | 0:e7ca326e76ee | 185 | * |
mkersh3 | 0:e7ca326e76ee | 186 | * @param buf netbuf to get the data from |
mkersh3 | 0:e7ca326e76ee | 187 | * @param dataptr pointer to a void pointer where to store the data pointer |
mkersh3 | 0:e7ca326e76ee | 188 | * @param len pointer to an u16_t where the length of the data is stored |
mkersh3 | 0:e7ca326e76ee | 189 | * @return ERR_OK if the information was retreived, |
mkersh3 | 0:e7ca326e76ee | 190 | * ERR_BUF on error. |
mkersh3 | 0:e7ca326e76ee | 191 | */ |
mkersh3 | 0:e7ca326e76ee | 192 | err_t |
mkersh3 | 0:e7ca326e76ee | 193 | netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len) |
mkersh3 | 0:e7ca326e76ee | 194 | { |
mkersh3 | 0:e7ca326e76ee | 195 | LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;); |
mkersh3 | 0:e7ca326e76ee | 196 | LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;); |
mkersh3 | 0:e7ca326e76ee | 197 | LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;); |
mkersh3 | 0:e7ca326e76ee | 198 | |
mkersh3 | 0:e7ca326e76ee | 199 | if (buf->ptr == NULL) { |
mkersh3 | 0:e7ca326e76ee | 200 | return ERR_BUF; |
mkersh3 | 0:e7ca326e76ee | 201 | } |
mkersh3 | 0:e7ca326e76ee | 202 | *dataptr = buf->ptr->payload; |
mkersh3 | 0:e7ca326e76ee | 203 | *len = buf->ptr->len; |
mkersh3 | 0:e7ca326e76ee | 204 | return ERR_OK; |
mkersh3 | 0:e7ca326e76ee | 205 | } |
mkersh3 | 0:e7ca326e76ee | 206 | |
mkersh3 | 0:e7ca326e76ee | 207 | /** |
mkersh3 | 0:e7ca326e76ee | 208 | * Move the current data pointer of a packet buffer contained in a netbuf |
mkersh3 | 0:e7ca326e76ee | 209 | * to the next part. |
mkersh3 | 0:e7ca326e76ee | 210 | * The packet buffer itself is not modified. |
mkersh3 | 0:e7ca326e76ee | 211 | * |
mkersh3 | 0:e7ca326e76ee | 212 | * @param buf the netbuf to modify |
mkersh3 | 0:e7ca326e76ee | 213 | * @return -1 if there is no next part |
mkersh3 | 0:e7ca326e76ee | 214 | * 1 if moved to the next part but now there is no next part |
mkersh3 | 0:e7ca326e76ee | 215 | * 0 if moved to the next part and there are still more parts |
mkersh3 | 0:e7ca326e76ee | 216 | */ |
mkersh3 | 0:e7ca326e76ee | 217 | s8_t |
mkersh3 | 0:e7ca326e76ee | 218 | netbuf_next(struct netbuf *buf) |
mkersh3 | 0:e7ca326e76ee | 219 | { |
mkersh3 | 0:e7ca326e76ee | 220 | LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return -1;); |
mkersh3 | 0:e7ca326e76ee | 221 | if (buf->ptr->next == NULL) { |
mkersh3 | 0:e7ca326e76ee | 222 | return -1; |
mkersh3 | 0:e7ca326e76ee | 223 | } |
mkersh3 | 0:e7ca326e76ee | 224 | buf->ptr = buf->ptr->next; |
mkersh3 | 0:e7ca326e76ee | 225 | if (buf->ptr->next == NULL) { |
mkersh3 | 0:e7ca326e76ee | 226 | return 1; |
mkersh3 | 0:e7ca326e76ee | 227 | } |
mkersh3 | 0:e7ca326e76ee | 228 | return 0; |
mkersh3 | 0:e7ca326e76ee | 229 | } |
mkersh3 | 0:e7ca326e76ee | 230 | |
mkersh3 | 0:e7ca326e76ee | 231 | /** |
mkersh3 | 0:e7ca326e76ee | 232 | * Move the current data pointer of a packet buffer contained in a netbuf |
mkersh3 | 0:e7ca326e76ee | 233 | * to the beginning of the packet. |
mkersh3 | 0:e7ca326e76ee | 234 | * The packet buffer itself is not modified. |
mkersh3 | 0:e7ca326e76ee | 235 | * |
mkersh3 | 0:e7ca326e76ee | 236 | * @param buf the netbuf to modify |
mkersh3 | 0:e7ca326e76ee | 237 | */ |
mkersh3 | 0:e7ca326e76ee | 238 | void |
mkersh3 | 0:e7ca326e76ee | 239 | netbuf_first(struct netbuf *buf) |
mkersh3 | 0:e7ca326e76ee | 240 | { |
mkersh3 | 0:e7ca326e76ee | 241 | LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;); |
mkersh3 | 0:e7ca326e76ee | 242 | buf->ptr = buf->p; |
mkersh3 | 0:e7ca326e76ee | 243 | } |
mkersh3 | 0:e7ca326e76ee | 244 | |
mkersh3 | 0:e7ca326e76ee | 245 | #endif /* LWIP_NETCONN */ |