Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ip.h Source File

ip.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * IP API
00004  */
00005 
00006 /*
00007  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Adam Dunkels <adam@sics.se>
00035  *
00036  */
00037 #ifndef LWIP_HDR_IP_H
00038 #define LWIP_HDR_IP_H
00039 
00040 #include "lwip/opt.h"
00041 
00042 #include "lwip/def.h"
00043 #include "lwip/pbuf.h"
00044 #include "lwip/ip_addr.h"
00045 #include "lwip/err.h"
00046 #include "lwip/netif.h"
00047 #include "lwip/ip4.h"
00048 #include "lwip/ip6.h"
00049 #include "lwip/prot/ip.h"
00050 
00051 #ifdef __cplusplus
00052 extern "C" {
00053 #endif
00054 
00055 /* This is passed as the destination address to ip_output_if (not
00056    to ip_output), meaning that an IP header already is constructed
00057    in the pbuf. This is used when TCP retransmits. */
00058 #define LWIP_IP_HDRINCL  NULL
00059 
00060 /** pbufs passed to IP must have a ref-count of 1 as their payload pointer
00061     gets altered as the packet is passed down the stack */
00062 #ifndef LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX
00063 #define LWIP_IP_CHECK_PBUF_REF_COUNT_FOR_TX(p) LWIP_ASSERT("p->ref == 1", (p)->ref == 1)
00064 #endif
00065 
00066 #if LWIP_NETIF_HWADDRHINT
00067 #define IP_PCB_ADDRHINT ;u8_t addr_hint
00068 #else
00069 #define IP_PCB_ADDRHINT
00070 #endif /* LWIP_NETIF_HWADDRHINT */
00071 
00072 /** This is the common part of all PCB types. It needs to be at the
00073    beginning of a PCB type definition. It is located here so that
00074    changes to this common part are made in one location instead of
00075    having to change all PCB structs. */
00076 #define IP_PCB \
00077   /* ip addresses in network byte order */ \
00078   ip_addr_t local_ip; \
00079   ip_addr_t remote_ip; \
00080    /* Socket options */  \
00081   u8_t so_options;      \
00082    /* Type Of Service */ \
00083   u8_t tos;              \
00084   /* Time To Live */     \
00085   u8_t ttl               \
00086   /* link layer address resolution hint */ \
00087   IP_PCB_ADDRHINT
00088 
00089 struct ip_pcb {
00090 /* Common members of all PCB types */
00091   IP_PCB;
00092 };
00093 
00094 /*
00095  * Option flags per-socket. These are the same like SO_XXX in sockets.h
00096  */
00097 #define SOF_REUSEADDR     0x04U  /* allow local address reuse */
00098 #define SOF_KEEPALIVE     0x08U  /* keep connections alive */
00099 #define SOF_BROADCAST     0x20U  /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
00100 
00101 /* These flags are inherited (e.g. from a listen-pcb to a connection-pcb): */
00102 #define SOF_INHERITED   (SOF_REUSEADDR|SOF_KEEPALIVE)
00103 
00104 /** Global variables of this module, kept in a struct for efficient access using base+index. */
00105 struct ip_globals
00106 {
00107   /** The interface that accepted the packet for the current callback invocation. */
00108   struct netif *current_netif;
00109   /** The interface that received the packet for the current callback invocation. */
00110   struct netif *current_input_netif;
00111 #if LWIP_IPV4
00112   /** Header of the input packet currently being processed. */
00113   struct ip_hdr *current_ip4_header;
00114 #endif /* LWIP_IPV4 */
00115 #if LWIP_IPV6
00116   /** Header of the input IPv6 packet currently being processed. */
00117   struct ip6_hdr *current_ip6_header;
00118 #endif /* LWIP_IPV6 */
00119   /** Total header length of current_ip4/6_header (i.e. after this, the UDP/TCP header starts) */
00120   u16_t current_ip_header_tot_len;
00121   /** Source IP address of current_header */
00122   ip_addr_t current_iphdr_src;
00123   /** Destination IP address of current_header */
00124   ip_addr_t current_iphdr_dest;
00125 };
00126 extern struct ip_globals ip_data;
00127 
00128 
00129 /** Get the interface that accepted the current packet.
00130  * This may or may not be the receiving netif, depending on your netif/network setup.
00131  * This function must only be called from a receive callback (udp_recv,
00132  * raw_recv, tcp_accept). It will return NULL otherwise. */
00133 #define ip_current_netif()      (ip_data.current_netif)
00134 /** Get the interface that received the current packet.
00135  * This function must only be called from a receive callback (udp_recv,
00136  * raw_recv, tcp_accept). It will return NULL otherwise. */
00137 #define ip_current_input_netif() (ip_data.current_input_netif)
00138 /** Total header length of ip(6)_current_header() (i.e. after this, the UDP/TCP header starts) */
00139 #define ip_current_header_tot_len() (ip_data.current_ip_header_tot_len)
00140 /** Source IP address of current_header */
00141 #define ip_current_src_addr()   (&ip_data.current_iphdr_src)
00142 /** Destination IP address of current_header */
00143 #define ip_current_dest_addr()  (&ip_data.current_iphdr_dest)
00144 
00145 #if LWIP_IPV4 && LWIP_IPV6
00146 /** Get the IPv4 header of the current packet.
00147  * This function must only be called from a receive callback (udp_recv,
00148  * raw_recv, tcp_accept). It will return NULL otherwise. */
00149 #define ip4_current_header()     ((const struct ip_hdr*)(ip_data.current_ip4_header))
00150 /** Get the IPv6 header of the current packet.
00151  * This function must only be called from a receive callback (udp_recv,
00152  * raw_recv, tcp_accept). It will return NULL otherwise. */
00153 #define ip6_current_header()      ((const struct ip6_hdr*)(ip_data.current_ip6_header))
00154 /** Returns TRUE if the current IP input packet is IPv6, FALSE if it is IPv4 */
00155 #define ip_current_is_v6()        (ip6_current_header() != NULL)
00156 /** Source IPv6 address of current_header */
00157 #define ip6_current_src_addr()    (ip_2_ip6(&ip_data.current_iphdr_src))
00158 /** Destination IPv6 address of current_header */
00159 #define ip6_current_dest_addr()   (ip_2_ip6(&ip_data.current_iphdr_dest))
00160 /** Get the transport layer protocol */
00161 #define ip_current_header_proto() (ip_current_is_v6() ? \
00162                                    IP6H_NEXTH(ip6_current_header()) :\
00163                                    IPH_PROTO(ip4_current_header()))
00164 /** Get the transport layer header */
00165 #define ip_next_header_ptr()     ((const void*)((ip_current_is_v6() ? \
00166   (const u8_t*)ip6_current_header() : (const u8_t*)ip4_current_header())  + ip_current_header_tot_len()))
00167 
00168 /** Source IP4 address of current_header */
00169 #define ip4_current_src_addr()     (ip_2_ip4(&ip_data.current_iphdr_src))
00170 /** Destination IP4 address of current_header */
00171 #define ip4_current_dest_addr()    (ip_2_ip4(&ip_data.current_iphdr_dest))
00172 
00173 #elif LWIP_IPV4 /* LWIP_IPV4 && LWIP_IPV6 */
00174 
00175 /** Get the IPv4 header of the current packet.
00176  * This function must only be called from a receive callback (udp_recv,
00177  * raw_recv, tcp_accept). It will return NULL otherwise. */
00178 #define ip4_current_header()     ((const struct ip_hdr*)(ip_data.current_ip4_header))
00179 /** Always returns FALSE when only supporting IPv4 only */
00180 #define ip_current_is_v6()        0
00181 /** Get the transport layer protocol */
00182 #define ip_current_header_proto() IPH_PROTO(ip4_current_header())
00183 /** Get the transport layer header */
00184 #define ip_next_header_ptr()     ((const void*)((const u8_t*)ip4_current_header() + ip_current_header_tot_len()))
00185 /** Source IP4 address of current_header */
00186 #define ip4_current_src_addr()     (&ip_data.current_iphdr_src)
00187 /** Destination IP4 address of current_header */
00188 #define ip4_current_dest_addr()    (&ip_data.current_iphdr_dest)
00189 
00190 #elif LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */
00191 
00192 /** Get the IPv6 header of the current packet.
00193  * This function must only be called from a receive callback (udp_recv,
00194  * raw_recv, tcp_accept). It will return NULL otherwise. */
00195 #define ip6_current_header()      ((const struct ip6_hdr*)(ip_data.current_ip6_header))
00196 /** Always returns TRUE when only supporting IPv6 only */
00197 #define ip_current_is_v6()        1
00198 /** Get the transport layer protocol */
00199 #define ip_current_header_proto() IP6H_NEXTH(ip6_current_header())
00200 /** Get the transport layer header */
00201 #define ip_next_header_ptr()     ((const void*)((const u8_t*)ip6_current_header()))
00202 /** Source IP6 address of current_header */
00203 #define ip6_current_src_addr()    (&ip_data.current_iphdr_src)
00204 /** Destination IP6 address of current_header */
00205 #define ip6_current_dest_addr()   (&ip_data.current_iphdr_dest)
00206 
00207 #endif /* LWIP_IPV6 */
00208 
00209 /** Union source address of current_header */
00210 #define ip_current_src_addr()    (&ip_data.current_iphdr_src)
00211 /** Union destination address of current_header */
00212 #define ip_current_dest_addr()   (&ip_data.current_iphdr_dest)
00213 
00214 /** Gets an IP pcb option (SOF_* flags) */
00215 #define ip_get_option(pcb, opt)   ((pcb)->so_options & (opt))
00216 /** Sets an IP pcb option (SOF_* flags) */
00217 #define ip_set_option(pcb, opt)   ((pcb)->so_options |= (opt))
00218 /** Resets an IP pcb option (SOF_* flags) */
00219 #define ip_reset_option(pcb, opt) ((pcb)->so_options &= ~(opt))
00220 
00221 #if LWIP_IPV4 && LWIP_IPV6
00222 /**
00223  * @ingroup ip
00224  * Output IP packet, netif is selected by source address
00225  */
00226 #define ip_output(p, src, dest, ttl, tos, proto) \
00227         (IP_IS_V6(dest) ? \
00228         ip6_output(p, ip_2_ip6(src), ip_2_ip6(dest), ttl, tos, proto) : \
00229         ip4_output(p, ip_2_ip4(src), ip_2_ip4(dest), ttl, tos, proto))
00230 /**
00231  * @ingroup ip
00232  * Output IP packet to specified interface
00233  */
00234 #define ip_output_if(p, src, dest, ttl, tos, proto, netif) \
00235         (IP_IS_V6(dest) ? \
00236         ip6_output_if(p, ip_2_ip6(src), ip_2_ip6(dest), ttl, tos, proto, netif) : \
00237         ip4_output_if(p, ip_2_ip4(src), ip_2_ip4(dest), ttl, tos, proto, netif))
00238 /**
00239  * @ingroup ip
00240  * Output IP packet to interface specifying source address
00241  */
00242 #define ip_output_if_src(p, src, dest, ttl, tos, proto, netif) \
00243         (IP_IS_V6(dest) ? \
00244         ip6_output_if_src(p, ip_2_ip6(src), ip_2_ip6(dest), ttl, tos, proto, netif) : \
00245         ip4_output_if_src(p, ip_2_ip4(src), ip_2_ip4(dest), ttl, tos, proto, netif))
00246 /** Output IP packet with addr_hint */
00247 #define ip_output_hinted(p, src, dest, ttl, tos, proto, addr_hint) \
00248         (IP_IS_V6(dest) ? \
00249         ip6_output_hinted(p, ip_2_ip6(src), ip_2_ip6(dest), ttl, tos, proto, addr_hint) : \
00250         ip4_output_hinted(p, ip_2_ip4(src), ip_2_ip4(dest), ttl, tos, proto, addr_hint))
00251 /**
00252  * @ingroup ip
00253  * Get netif for address combination. See \ref ip6_route and \ref ip4_route
00254  */
00255 #define ip_route(src, dest) \
00256         (IP_IS_V6(dest) ? \
00257         ip6_route(ip_2_ip6(src), ip_2_ip6(dest)) : \
00258         ip4_route_src(ip_2_ip4(dest), ip_2_ip4(src)))
00259 /**
00260  * @ingroup ip
00261  * Get netif for IP.
00262  */
00263 #define ip_netif_get_local_ip(netif, dest) (IP_IS_V6(dest) ? \
00264         ip6_netif_get_local_ip(netif, ip_2_ip6(dest)) : \
00265         ip4_netif_get_local_ip(netif))
00266 #define ip_debug_print(is_ipv6, p) ((is_ipv6) ? ip6_debug_print(p) : ip4_debug_print(p))
00267 
00268 err_t ip_input(struct pbuf *p, struct netif *inp);
00269 
00270 #elif LWIP_IPV4 /* LWIP_IPV4 && LWIP_IPV6 */
00271 
00272 #define ip_output(p, src, dest, ttl, tos, proto) \
00273         ip4_output(p, src, dest, ttl, tos, proto)
00274 #define ip_output_if(p, src, dest, ttl, tos, proto, netif) \
00275         ip4_output_if(p, src, dest, ttl, tos, proto, netif)
00276 #define ip_output_if_src(p, src, dest, ttl, tos, proto, netif) \
00277         ip4_output_if_src(p, src, dest, ttl, tos, proto, netif)
00278 #define ip_output_hinted(p, src, dest, ttl, tos, proto, addr_hint) \
00279         ip4_output_hinted(p, src, dest, ttl, tos, proto, addr_hint)
00280 #define ip_route(src, dest) \
00281         ip4_route_src(dest, src)
00282 #define ip_netif_get_local_ip(netif, dest) \
00283         ip4_netif_get_local_ip(netif)
00284 #define ip_debug_print(is_ipv6, p) ip4_debug_print(p)
00285 
00286 #define ip_input ip4_input
00287 
00288 #elif LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */
00289 
00290 #define ip_output(p, src, dest, ttl, tos, proto) \
00291         ip6_output(p, src, dest, ttl, tos, proto)
00292 #define ip_output_if(p, src, dest, ttl, tos, proto, netif) \
00293         ip6_output_if(p, src, dest, ttl, tos, proto, netif)
00294 #define ip_output_if_src(p, src, dest, ttl, tos, proto, netif) \
00295         ip6_output_if_src(p, src, dest, ttl, tos, proto, netif)
00296 #define ip_output_hinted(p, src, dest, ttl, tos, proto, addr_hint) \
00297         ip6_output_hinted(p, src, dest, ttl, tos, proto, addr_hint)
00298 #define ip_route(src, dest) \
00299         ip6_route(src, dest)
00300 #define ip_netif_get_local_ip(netif, dest) \
00301         ip6_netif_get_local_ip(netif, dest)
00302 #define ip_debug_print(is_ipv6, p) ip6_debug_print(p)
00303 
00304 #define ip_input ip6_input
00305 
00306 #endif /* LWIP_IPV6 */
00307 
00308 #define ip_route_get_local_ip(src, dest, netif, ipaddr) do { \
00309   (netif) = ip_route(src, dest); \
00310   (ipaddr) = ip_netif_get_local_ip(netif, dest); \
00311 }while(0)
00312 
00313 #ifdef __cplusplus
00314 }
00315 #endif
00316 
00317 #endif /* LWIP_HDR_IP_H */
00318 
00319