1 /* 2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 3 * All rights reserved. 4 * Copyright (c) <2013-2015>, <Huawei Technologies Co., Ltd> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without modification, 8 * are permitted provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 27 * OF SUCH DAMAGE. 28 * 29 * This file is part of the lwIP TCP/IP stack. 30 * 31 * Author: Adam Dunkels <adam@sics.se> 32 * 33 */ 34 #ifndef LWIP_HDR_DEF_H 35 #define LWIP_HDR_DEF_H 36 37 /* arch.h might define NULL already */ 38 #include "lwip/arch.h" 39 #include "lwip/opt.h" 40 41 #if defined (__cplusplus) && __cplusplus 42 extern "C" { 43 #endif 44 45 /* time unit */ 46 #define MS_PER_SECOND 1000 47 #define US_PER_MSECOND 1000 48 #define NS_PER_USECOND 1000 49 50 #define LWIP_MAX(x, y) (((x) > (y)) ? (x) : (y)) 51 #define LWIP_MIN(x, y) (((x) < (y)) ? (x) : (y)) 52 53 /* Get the number of entries in an array ('x' must NOT be a pointer!) */ 54 #define LWIP_ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0])) 55 56 /** Create u32_t value from bytes */ 57 #define LWIP_MAKEU32(a, b, c, d) (((u32_t)((a) & 0xff) << 24) | \ 58 ((u32_t)((b) & 0xff) << 16) | \ 59 ((u32_t)((c) & 0xff) << 8) | \ 60 (u32_t)((d) & 0xff)) 61 62 63 /* Endianess-optimized shifting of two u8_t to create one u16_t */ 64 #if BYTE_ORDER == LITTLE_ENDIAN 65 #define LWIP_MAKE_U16(a, b) (((a) << 8) | (b)) 66 #else 67 #define LWIP_MAKE_U16(a, b) (((b) << 8) | (a)) 68 #endif 69 70 #ifndef LWIP_PLATFORM_BYTESWAP 71 #define LWIP_PLATFORM_BYTESWAP 0 72 #endif 73 74 /* workaround for naming collisions on some platforms */ 75 #ifdef htons 76 #define lwip_htons htons 77 #else 78 #if (BYTE_ORDER == BIG_ENDIAN) 79 #define lwip_htons(x) ((u16_t)(x)) 80 #else 81 u16_t lwip_htons(u16_t x); 82 #endif 83 #define htons lwip_htons 84 #endif /* htons */ 85 #ifdef htonl 86 #define lwip_htonl htonl 87 #else 88 #if (BYTE_ORDER == BIG_ENDIAN) 89 #define lwip_htonl(x) ((u32_t)(x)) 90 #else 91 u32_t lwip_htonl(u32_t x); 92 #endif 93 #define htonl lwip_htonl 94 #endif /* htonl */ 95 #ifdef ntohs 96 #define lwip_ntohs ntohs 97 #else 98 #if (BYTE_ORDER == BIG_ENDIAN) 99 #define lwip_ntohs(x) ((u16_t)(x)) 100 #else 101 u16_t lwip_ntohs(u16_t x); 102 #endif 103 #define ntohs lwip_ntohs 104 #endif /* ntohs */ 105 106 #ifdef ntohl 107 #define lwip_ntohl ntohl 108 #else 109 #if (BYTE_ORDER == BIG_ENDIAN) 110 #define lwip_ntohl(x) ((u32_t)(x)) 111 #else 112 u32_t lwip_ntohl(u32_t x); 113 #endif 114 #define ntohl lwip_ntohl 115 #endif /* ntohl */ 116 117 #if (BYTE_ORDER == BIG_ENDIAN) 118 #define PP_HTONS(x) ((u16_t)(x)) 119 #define PP_NTOHS(x) ((u16_t)(x)) 120 #define PP_HTONL(x) ((u32_t)(x)) 121 #define PP_NTOHL(x) ((u32_t)(x)) 122 #else /* BYTE_ORDER != BIG_ENDIAN */ 123 124 125 /* These macros should be calculated by the preprocessor and are used 126 with compile-time constants only (so that there is no little-endian 127 overhead at runtime). */ 128 #define PP_HTONS(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8)) 129 #define PP_NTOHS(x) PP_HTONS(x) 130 #define PP_HTONL(x) ((((x) & 0x000000ffUL) << 24) | \ 131 (((x) & 0x0000ff00UL) << 8) | \ 132 (((x) & 0x00ff0000UL) >> 8) | \ 133 (((x) & 0xff000000UL) >> 24)) 134 #define PP_NTOHL(x) PP_HTONL(x) 135 136 #endif /* BYTE_ORDER == BIG_ENDIAN */ 137 138 /* Functions that are not available as standard implementations. 139 * In cc.h, you can #define these to implementations available on 140 * your platform to save some code bytes if you use these functions 141 * in your application, too. 142 */ 143 144 #ifndef lwip_itoa 145 /* This can be #defined to itoa() or snprintf(result, bufsize, "%d", number) depending on your platform */ 146 void lwip_itoa(char *result, size_t bufsize, int number); 147 #endif 148 #ifndef lwip_strnicmp 149 /* This can be #defined to strnicmp() or strncasecmp() depending on your platform */ 150 int lwip_strnicmp(const char *str1, const char *str2, size_t len); 151 #endif 152 #ifndef lwip_stricmp 153 /* This can be #defined to stricmp() or strcasecmp() depending on your platform */ 154 int lwip_stricmp(const char *str1, const char *str2); 155 #endif 156 #ifndef lwip_strnstr 157 /* This can be #defined to strnstr() depending on your platform */ 158 char *lwip_strnstr(const char *buffer, const char *token, size_t n); 159 #endif 160 #if LWIP_SO_DONTROUTE 161 /* route entry scope, Actually it is not scope, but distance to the destination. */ 162 typedef enum rt_scope { 163 RT_SCOPE_UNIVERSAL, /* everywhere in the Universe */ 164 RT_SCOPE_LINK, /* destinations located on directly attached link, maybe not same IP network */ 165 RT_SCOPE_HOST /* our local address */ 166 } rt_scope_t; 167 #endif 168 169 #if defined (__cplusplus) && __cplusplus 170 } 171 #endif 172 173 #endif /* LWIP_HDR_DEF_H */ 174