Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:a548a085de55 1 /*
pehrhovey 0:a548a085de55 2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
pehrhovey 0:a548a085de55 3 * All rights reserved.
pehrhovey 0:a548a085de55 4 *
pehrhovey 0:a548a085de55 5 * Redistribution and use in source and binary forms, with or without modification,
pehrhovey 0:a548a085de55 6 * are permitted provided that the following conditions are met:
pehrhovey 0:a548a085de55 7 *
pehrhovey 0:a548a085de55 8 * 1. Redistributions of source code must retain the above copyright notice,
pehrhovey 0:a548a085de55 9 * this list of conditions and the following disclaimer.
pehrhovey 0:a548a085de55 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
pehrhovey 0:a548a085de55 11 * this list of conditions and the following disclaimer in the documentation
pehrhovey 0:a548a085de55 12 * and/or other materials provided with the distribution.
pehrhovey 0:a548a085de55 13 * 3. The name of the author may not be used to endorse or promote products
pehrhovey 0:a548a085de55 14 * derived from this software without specific prior written permission.
pehrhovey 0:a548a085de55 15 *
pehrhovey 0:a548a085de55 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
pehrhovey 0:a548a085de55 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
pehrhovey 0:a548a085de55 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
pehrhovey 0:a548a085de55 19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
pehrhovey 0:a548a085de55 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
pehrhovey 0:a548a085de55 21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
pehrhovey 0:a548a085de55 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
pehrhovey 0:a548a085de55 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
pehrhovey 0:a548a085de55 24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
pehrhovey 0:a548a085de55 25 * OF SUCH DAMAGE.
pehrhovey 0:a548a085de55 26 *
pehrhovey 0:a548a085de55 27 * This file is part of the lwIP TCP/IP stack.
pehrhovey 0:a548a085de55 28 *
pehrhovey 0:a548a085de55 29 * Author: Adam Dunkels <adam@sics.se>
pehrhovey 0:a548a085de55 30 *
pehrhovey 0:a548a085de55 31 */
pehrhovey 0:a548a085de55 32 #ifndef __LWIP_IP_H__
pehrhovey 0:a548a085de55 33 #define __LWIP_IP_H__
pehrhovey 0:a548a085de55 34
pehrhovey 0:a548a085de55 35 #include "lwip/opt.h"
pehrhovey 0:a548a085de55 36
pehrhovey 0:a548a085de55 37 #include "lwip/def.h"
pehrhovey 0:a548a085de55 38 #include "lwip/pbuf.h"
pehrhovey 0:a548a085de55 39 #include "lwip/ip_addr.h"
pehrhovey 0:a548a085de55 40 #include "lwip/err.h"
pehrhovey 0:a548a085de55 41 #include "lwip/netif.h"
pehrhovey 0:a548a085de55 42
pehrhovey 0:a548a085de55 43 #ifdef __cplusplus
pehrhovey 0:a548a085de55 44 extern "C" {
pehrhovey 0:a548a085de55 45 #endif
pehrhovey 0:a548a085de55 46
pehrhovey 0:a548a085de55 47 /** Currently, the function ip_output_if_opt() is only used with IGMP */
pehrhovey 0:a548a085de55 48 #define IP_OPTIONS_SEND LWIP_IGMP
pehrhovey 0:a548a085de55 49
pehrhovey 0:a548a085de55 50 #define IP_HLEN 20
pehrhovey 0:a548a085de55 51
pehrhovey 0:a548a085de55 52 #define IP_PROTO_ICMP 1
pehrhovey 0:a548a085de55 53 #define IP_PROTO_UDP 17
pehrhovey 0:a548a085de55 54 #define IP_PROTO_UDPLITE 136
pehrhovey 0:a548a085de55 55 #define IP_PROTO_TCP 6
pehrhovey 0:a548a085de55 56
pehrhovey 0:a548a085de55 57 /* This is passed as the destination address to ip_output_if (not
pehrhovey 0:a548a085de55 58 to ip_output), meaning that an IP header already is constructed
pehrhovey 0:a548a085de55 59 in the pbuf. This is used when TCP retransmits. */
pehrhovey 0:a548a085de55 60 #ifdef IP_HDRINCL
pehrhovey 0:a548a085de55 61 #undef IP_HDRINCL
pehrhovey 0:a548a085de55 62 #endif /* IP_HDRINCL */
pehrhovey 0:a548a085de55 63 #define IP_HDRINCL NULL
pehrhovey 0:a548a085de55 64
pehrhovey 0:a548a085de55 65 #if LWIP_NETIF_HWADDRHINT
pehrhovey 0:a548a085de55 66 #define IP_PCB_ADDRHINT ;u8_t addr_hint
pehrhovey 0:a548a085de55 67 #else
pehrhovey 0:a548a085de55 68 #define IP_PCB_ADDRHINT
pehrhovey 0:a548a085de55 69 #endif /* LWIP_NETIF_HWADDRHINT */
pehrhovey 0:a548a085de55 70
pehrhovey 0:a548a085de55 71 /* This is the common part of all PCB types. It needs to be at the
pehrhovey 0:a548a085de55 72 beginning of a PCB type definition. It is located here so that
pehrhovey 0:a548a085de55 73 changes to this common part are made in one location instead of
pehrhovey 0:a548a085de55 74 having to change all PCB structs. */
pehrhovey 0:a548a085de55 75 #define IP_PCB \
pehrhovey 0:a548a085de55 76 /* ip addresses in network byte order */ \
pehrhovey 0:a548a085de55 77 struct ip_addr local_ip; \
pehrhovey 0:a548a085de55 78 struct ip_addr remote_ip; \
pehrhovey 0:a548a085de55 79 /* Socket options */ \
pehrhovey 0:a548a085de55 80 u16_t so_options; \
pehrhovey 0:a548a085de55 81 /* Type Of Service */ \
pehrhovey 0:a548a085de55 82 u8_t tos; \
pehrhovey 0:a548a085de55 83 /* Time To Live */ \
pehrhovey 0:a548a085de55 84 u8_t ttl \
pehrhovey 0:a548a085de55 85 /* link layer address resolution hint */ \
pehrhovey 0:a548a085de55 86 IP_PCB_ADDRHINT
pehrhovey 0:a548a085de55 87
pehrhovey 0:a548a085de55 88 struct ip_pcb {
pehrhovey 0:a548a085de55 89 /* Common members of all PCB types */
pehrhovey 0:a548a085de55 90 IP_PCB;
pehrhovey 0:a548a085de55 91 };
pehrhovey 0:a548a085de55 92
pehrhovey 0:a548a085de55 93 /*
pehrhovey 0:a548a085de55 94 * Option flags per-socket. These are the same like SO_XXX.
pehrhovey 0:a548a085de55 95 */
pehrhovey 0:a548a085de55 96 #define SOF_DEBUG (u16_t)0x0001U /* turn on debugging info recording */
pehrhovey 0:a548a085de55 97 #define SOF_ACCEPTCONN (u16_t)0x0002U /* socket has had listen() */
pehrhovey 0:a548a085de55 98 #define SOF_REUSEADDR (u16_t)0x0004U /* allow local address reuse */
pehrhovey 0:a548a085de55 99 #define SOF_KEEPALIVE (u16_t)0x0008U /* keep connections alive */
pehrhovey 0:a548a085de55 100 #define SOF_DONTROUTE (u16_t)0x0010U /* just use interface addresses */
pehrhovey 0:a548a085de55 101 #define SOF_BROADCAST (u16_t)0x0020U /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
pehrhovey 0:a548a085de55 102 #define SOF_USELOOPBACK (u16_t)0x0040U /* bypass hardware when possible */
pehrhovey 0:a548a085de55 103 #define SOF_LINGER (u16_t)0x0080U /* linger on close if data present */
pehrhovey 0:a548a085de55 104 #define SOF_OOBINLINE (u16_t)0x0100U /* leave received OOB data in line */
pehrhovey 0:a548a085de55 105 #define SOF_REUSEPORT (u16_t)0x0200U /* allow local address & port reuse */
pehrhovey 0:a548a085de55 106
pehrhovey 0:a548a085de55 107
pehrhovey 0:a548a085de55 108 #ifdef PACK_STRUCT_USE_INCLUDES
pehrhovey 0:a548a085de55 109 # include "arch/bpstruct.h"
pehrhovey 0:a548a085de55 110 #endif
pehrhovey 0:a548a085de55 111 PACK_STRUCT_BEGIN
pehrhovey 0:a548a085de55 112 struct ip_hdr {
pehrhovey 0:a548a085de55 113 /* version / header length / type of service */
pehrhovey 0:a548a085de55 114 PACK_STRUCT_FIELD(u16_t _v_hl_tos);
pehrhovey 0:a548a085de55 115 /* total length */
pehrhovey 0:a548a085de55 116 PACK_STRUCT_FIELD(u16_t _len);
pehrhovey 0:a548a085de55 117 /* identification */
pehrhovey 0:a548a085de55 118 PACK_STRUCT_FIELD(u16_t _id);
pehrhovey 0:a548a085de55 119 /* fragment offset field */
pehrhovey 0:a548a085de55 120 PACK_STRUCT_FIELD(u16_t _offset);
pehrhovey 0:a548a085de55 121 #define IP_RF 0x8000 /* reserved fragment flag */
pehrhovey 0:a548a085de55 122 #define IP_DF 0x4000 /* dont fragment flag */
pehrhovey 0:a548a085de55 123 #define IP_MF 0x2000 /* more fragments flag */
pehrhovey 0:a548a085de55 124 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
pehrhovey 0:a548a085de55 125 /* time to live / protocol*/
pehrhovey 0:a548a085de55 126 PACK_STRUCT_FIELD(u16_t _ttl_proto);
pehrhovey 0:a548a085de55 127 /* checksum */
pehrhovey 0:a548a085de55 128 PACK_STRUCT_FIELD(u16_t _chksum);
pehrhovey 0:a548a085de55 129 /* source and destination IP addresses */
pehrhovey 0:a548a085de55 130 PACK_STRUCT_FIELD(struct ip_addr src);
pehrhovey 0:a548a085de55 131 PACK_STRUCT_FIELD(struct ip_addr dest);
pehrhovey 0:a548a085de55 132 } PACK_STRUCT_STRUCT;
pehrhovey 0:a548a085de55 133 PACK_STRUCT_END
pehrhovey 0:a548a085de55 134 #ifdef PACK_STRUCT_USE_INCLUDES
pehrhovey 0:a548a085de55 135 # include "arch/epstruct.h"
pehrhovey 0:a548a085de55 136 #endif
pehrhovey 0:a548a085de55 137
pehrhovey 0:a548a085de55 138 #define IPH_V(hdr) (ntohs((hdr)->_v_hl_tos) >> 12)
pehrhovey 0:a548a085de55 139 #define IPH_HL(hdr) ((ntohs((hdr)->_v_hl_tos) >> 8) & 0x0f)
pehrhovey 0:a548a085de55 140 #define IPH_TOS(hdr) (ntohs((hdr)->_v_hl_tos) & 0xff)
pehrhovey 0:a548a085de55 141 #define IPH_LEN(hdr) ((hdr)->_len)
pehrhovey 0:a548a085de55 142 #define IPH_ID(hdr) ((hdr)->_id)
pehrhovey 0:a548a085de55 143 #define IPH_OFFSET(hdr) ((hdr)->_offset)
pehrhovey 0:a548a085de55 144 #define IPH_TTL(hdr) (ntohs((hdr)->_ttl_proto) >> 8)
pehrhovey 0:a548a085de55 145 #define IPH_PROTO(hdr) (ntohs((hdr)->_ttl_proto) & 0xff)
pehrhovey 0:a548a085de55 146 #define IPH_CHKSUM(hdr) ((hdr)->_chksum)
pehrhovey 0:a548a085de55 147
pehrhovey 0:a548a085de55 148 #define IPH_VHLTOS_SET(hdr, v, hl, tos) (hdr)->_v_hl_tos = (htons(((v) << 12) | ((hl) << 8) | (tos)))
pehrhovey 0:a548a085de55 149 #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len)
pehrhovey 0:a548a085de55 150 #define IPH_ID_SET(hdr, id) (hdr)->_id = (id)
pehrhovey 0:a548a085de55 151 #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)
pehrhovey 0:a548a085de55 152 #define IPH_TTL_SET(hdr, ttl) (hdr)->_ttl_proto = (htons(IPH_PROTO(hdr) | ((u16_t)(ttl) << 8)))
pehrhovey 0:a548a085de55 153 #define IPH_PROTO_SET(hdr, proto) (hdr)->_ttl_proto = (htons((proto) | (IPH_TTL(hdr) << 8)))
pehrhovey 0:a548a085de55 154 #define IPH_CHKSUM_SET(hdr, chksum) (hdr)->_chksum = (chksum)
pehrhovey 0:a548a085de55 155
pehrhovey 0:a548a085de55 156 /** The interface that provided the packet for the current callback invocation. */
pehrhovey 0:a548a085de55 157 extern struct netif *current_netif;
pehrhovey 0:a548a085de55 158 /** Header of the input packet currently being processed. */
pehrhovey 0:a548a085de55 159 extern const struct ip_hdr *current_header;
pehrhovey 0:a548a085de55 160
pehrhovey 0:a548a085de55 161 #define ip_init() /* Compatibility define, not init needed. */
pehrhovey 0:a548a085de55 162 struct netif *ip_route(struct ip_addr *dest);
pehrhovey 0:a548a085de55 163 err_t ip_input(struct pbuf *p, struct netif *inp);
pehrhovey 0:a548a085de55 164 err_t ip_output(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
pehrhovey 0:a548a085de55 165 u8_t ttl, u8_t tos, u8_t proto);
pehrhovey 0:a548a085de55 166 err_t ip_output_if(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
pehrhovey 0:a548a085de55 167 u8_t ttl, u8_t tos, u8_t proto,
pehrhovey 0:a548a085de55 168 struct netif *netif);
pehrhovey 0:a548a085de55 169 #if LWIP_NETIF_HWADDRHINT
pehrhovey 0:a548a085de55 170 err_t ip_output_hinted(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
pehrhovey 0:a548a085de55 171 u8_t ttl, u8_t tos, u8_t proto, u8_t *addr_hint);
pehrhovey 0:a548a085de55 172 #endif /* LWIP_NETIF_HWADDRHINT */
pehrhovey 0:a548a085de55 173 #if IP_OPTIONS_SEND
pehrhovey 0:a548a085de55 174 err_t ip_output_if_opt(struct pbuf *p, struct ip_addr *src, struct ip_addr *dest,
pehrhovey 0:a548a085de55 175 u8_t ttl, u8_t tos, u8_t proto, struct netif *netif, void *ip_options,
pehrhovey 0:a548a085de55 176 u16_t optlen);
pehrhovey 0:a548a085de55 177 #endif /* IP_OPTIONS_SEND */
pehrhovey 0:a548a085de55 178 /** Get the interface that received the current packet.
pehrhovey 0:a548a085de55 179 * This function must only be called from a receive callback (udp_recv,
pehrhovey 0:a548a085de55 180 * raw_recv, tcp_accept). It will return NULL otherwise. */
pehrhovey 0:a548a085de55 181 #define ip_current_netif() (current_netif)
pehrhovey 0:a548a085de55 182 /** Get the IP header of the current packet.
pehrhovey 0:a548a085de55 183 * This function must only be called from a receive callback (udp_recv,
pehrhovey 0:a548a085de55 184 * raw_recv, tcp_accept). It will return NULL otherwise. */
pehrhovey 0:a548a085de55 185 #define ip_current_header() (current_header)
pehrhovey 0:a548a085de55 186 #if IP_DEBUG
pehrhovey 0:a548a085de55 187 void ip_debug_print(struct pbuf *p);
pehrhovey 0:a548a085de55 188 #else
pehrhovey 0:a548a085de55 189 #define ip_debug_print(p)
pehrhovey 0:a548a085de55 190 #endif /* IP_DEBUG */
pehrhovey 0:a548a085de55 191
pehrhovey 0:a548a085de55 192 #ifdef __cplusplus
pehrhovey 0:a548a085de55 193 }
pehrhovey 0:a548a085de55 194 #endif
pehrhovey 0:a548a085de55 195
pehrhovey 0:a548a085de55 196 #endif /* __LWIP_IP_H__ */
pehrhovey 0:a548a085de55 197
pehrhovey 0:a548a085de55 198