Commit bdd54648 authored by Yury Popov's avatar Yury Popov Committed by Marcel Stör
Browse files

Upgrade to SDK 2.0.0 (#1435)

* Update LWIP from SDK
* mbedTLS integration
* Fix argument type in dbg_printf (#1624)
* Migrate to espressif’s download center (#1604)
* Fixed BBS links to firmware
* Adjust net module docs with mbedTLS info
* Remove unrelevant axTLS notice
parent 017b4637
#include "lwip/app/espconn_buf.h"
\ No newline at end of file
/*
* ESPRSSIF MIT License
*
* Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef ESP_SOCKET_H_
#define ESP_SOCKET_H_
#include "lwip/inet.h"
#include "lwip/opt.h"
#include "sys/ringbuf.h"
#if 0
#define ESP_LOG os_printf
#else
#define ESP_LOG(...)
#endif
#define NUM_SOCKETS 3
#define ESP_OK 0 /* No error, everything OK. */
#define ESP_MEM -1 /* Out of memory error. */
#define ESP_TIMEOUT -3 /* Timeout. */
#define ESP_RTE -4 /* Routing problem. */
#define ESP_INPROGRESS -5 /* Operation in progress */
#define ESP_MAXNUM -7 /* Total number exceeds the set maximum*/
#define ESP_ABRT -8 /* Connection aborted. */
#define ESP_RST -9 /* Connection reset. */
#define ESP_CLSD -10 /* Connection closed. */
#define ESP_CONN -11 /* Not connected. */
#define ESP_ARG -12 /* Illegal argument. */
#define ESP_IF -14 /* Low_level error */
#define ESP_ISCONN -15 /* Already connected. */
typedef enum{
NETCONN_STATE_NONE = 0,
NETCONN_STATE_ESTABLISHED,
NETCONN_STATE_WRITE,
NETCONN_STATE_READ,
NETCONN_STATE_CLOSED,
NETCONN_STATE_ERROR,
NETCONN_STATE_SUMNUM
}netconn_state;
#if (!defined(lwIP_unlikely))
#define lwIP_unlikely(Expression) !!(Expression)
#endif
#define lwIP_ASSERT(Expression) do{if (!(Expression)) ESP_LOG("%d\n", __LINE__);}while(0)
#define lwIP_REQUIRE_ACTION(Expression,Label,Action) \
do{\
if (lwIP_unlikely(!(Expression))) \
{\
ESP_LOG("%d\n", __LINE__);\
{Action;}\
goto Label;\
}\
}while(0)
#define lwIP_REQUIRE_NOERROR(Expression,Label) \
do{\
int LocalError;\
LocalError = (int)Expression;\
if (lwIP_unlikely(LocalError != 0)) \
{\
ESP_LOG("%d 0x%x\n", __LINE__, LocalError);\
goto Label;\
}\
}while(0)
#define lwIP_REQUIRE_NOERROR_ACTION(Expression,Label,Action) \
do{\
int LocalError;\
LocalError = (int)Expression;\
if (lwIP_unlikely(LocalError != 0)) \
{\
ESP_LOG("%d\n", __LINE__);\
{Action;}\
goto Label;\
}\
}while(0)
#define lwIP_EVENT_PARSE(s, error) \
do { \
mbedtls_parse_internal(s, error); \
} while (0)
#define lwIP_EVENT_THREAD(s, event, error) \
do { \
mbedtls_parse_thread(s, event, error); \
}while(0)
typedef enum{
ENTCONN_EVENT_NONE = 0,
NETCONN_EVENT_ESTABLISHED = 1,
ENTCONN_EVENT_RECV = 2,
NETCONN_EVENT_SEND = 3,
NETCONN_EVENT_ERROR = 4,
NETCONN_EVENT_CLOSE = 5,
NETCONN_EVENT_SUMNUM = 6
}netconn_event;
typedef enum _netconn_type {
NETCONN_INVALID = 0,
/* ESPCONN_TCP Group */
NETCONN_TCP = 0x10,
/* ESPCONN_UDP Group */
NETCONN_UDP = 0x20,
} netconn_type;
/* members are in network byte order */
struct sockaddr_in {
u8_t sin_len;
u8_t sin_family;
u16_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
/** A netconn descriptor */
typedef struct _lwIP_netconn{
/** flags blocking or nonblocking defines */
uint8 flags;
/** type of the lwIP_netconn (TCP, UDP) */
netconn_type type;
/** current state of the lwIP_netconn */
netconn_state state;
/** the lwIP internal protocol control block */
struct tcp_pcb *tcp;
/**the new socket is unknown and sync*/
void *acceptmbox;
/**the lwIP internal buffer control block*/
ringbuf *readbuf;
/** only used for socket layer */
int socket;
}lwIP_netconn, *lwIPNetconn;
/** Contains all internal pointers and states used for a socket */
typedef struct _lwIP_sock{
/** sockets currently are built on lwIP_netconn, each socket has one lwIP_netconn */
lwIP_netconn *conn;
/** data that was left from the previous read */
u32_t recv_index;
u32_t send_index;
u32_t recv_data_len;
void *send_buffer;
}lwIP_sock;
typedef uint8 sa_family_t;
struct sockaddr {
uint8 sa_len;
sa_family_t sa_family;
char sa_data[14];
};
typedef uint32 socklen_t;
#define NETCONNTYPE_GROUP(t) ((t)&0xF0)
#define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
#define AF_UNSPEC 0
#define AF_INET 2
/* Socket protocol types (TCP/UDP/RAW) */
#define SOCK_STREAM 1
#define SOCK_DGRAM 2
#define SOCK_RAW 3
#define lwIPThreadPrio 25
#define lwIPThreadQueueLen 15
/*
* Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
*/
#define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
#define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
#define SO_REUSEADDR 0x0004 /* Allow local address reuse */
#define SO_KEEPALIVE 0x0008 /* keep connections alive */
#define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
#define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
#define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
#define SO_LINGER 0x0080 /* linger on close if data present */
#define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
#define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
/*
* Additional options, not kept in so_options.
*/
#define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
#define SO_RCVBUF 0x1002 /* receive buffer size */
#define SO_TYPE 0x1008 /* get socket type */
#define IPPROTO_IP 0
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
#define SOL_SOCKET 0xfff /* options for socket level */
#if LWIP_TCP
/*
* Options for level IPPROTO_TCP
*/
#define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
#define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
#define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
#define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
#define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
#endif /* LWIP_TCP */
/* commands for fnctl */
#ifndef F_GETFL
#define F_GETFL 3
#endif
#ifndef F_SETFL
#define F_SETFL 4
#endif
/* File status flags and file access modes for fnctl,
these are bits in an int. */
#ifndef O_NONBLOCK
#define O_NONBLOCK 1 /* nonblocking I/O */
#endif
#ifndef O_NDELAY
#define O_NDELAY 1 /* same as O_NONBLOCK, for compatibility */
#endif
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
/* Flags for struct netconn.flags (u8_t) */
/** TCP: when data passed to netconn_write doesn't fit into the send buffer,
this temporarily stores whether to wake up the original application task
if data couldn't be sent in the first try. */
#define NETCONN_FLAG_WRITE_DELAYED 0x01
/** Should this netconn avoid blocking? */
#define NETCONN_FLAG_NON_BLOCKING 0x02
/** Was the last connect action a non-blocking one? */
#define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
/** If this is set, a TCP netconn must call netconn_recved() to update
the TCP receive window (done automatically if not set). */
#define NETCONN_FLAG_NO_AUTO_RECVED 0x08
/** If a nonblocking write has been rejected before, poll_tcp needs to
check if the netconn is writable again */
#define NETCONN_FLAG_CHECK_WRITESPACE 0x10
/** Set the blocking status of netconn calls (@todo: write/send is missing) */
#define netconn_set_nonblocking(conn, val) do { if(val) { \
(conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \
} else { \
(conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
/** Get the blocking status of netconn calls (@todo: write/send is missing) */
#define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
/* FD_SET used for lwip_select */
#ifndef FD_SET
#undef FD_SETSIZE
/* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
#define FD_SETSIZE NUM_SOCKETS
#define FD_SET(n, p) ((p)->fd_bits[(n)/8] |= (1 << ((n) & 7)))
#define FD_CLR(n, p) ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
#define FD_ISSET(n,p) ((p)->fd_bits[(n)/8] & (1 << ((n) & 7)))
#define FD_ZERO(p) os_memset((void*)(p),0,sizeof(*(p)))
typedef struct fd_set {
unsigned char fd_bits[(FD_SETSIZE + 7) / 8];
} fd_set;
#endif /* FD_SET */
void lwip_socket_init(void);
int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
int lwip_shutdown(int s, int how);
int lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen);
int lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen);
int lwip_getsockopt(int s, int level, int optname, void *optval,socklen_t *optlen);
int lwip_setsockopt(int s, int level, int optname, const void *optval,socklen_t optlen);
int lwip_close(int s);
int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
int lwip_listen(int s, int backlog);
int lwip_recv(int s, void *mem, size_t len, int flags);
int lwip_read(int s, void *mem, size_t len);
int lwip_recvfrom(int s, void *mem, size_t len, int flags,struct sockaddr *from, socklen_t *fromlen);
int lwip_send(int s, const void *dataptr, size_t size, int flags);
int lwip_sendto(int s, const void *dataptr, size_t size, int flags,const struct sockaddr *to, socklen_t tolen);
int lwip_socket(int domain, int type, int protocol);
int lwip_write(int s, const void *dataptr, size_t size);
int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset,fd_set *exceptset, struct timeval *timeout);
int lwip_ioctl(int s, long cmd, void *argp);
int lwip_fcntl(int s, int cmd, int val);
uint32_t lwip_getul(char *str);
#define accept(a,b,c) lwip_accept(a,b,c)
#define bind(a,b,c) lwip_bind(a,b,c)
#define shutdown(a,b) lwip_shutdown(a,b)
#define closesocket(s) lwip_close(s)
#define connect(a,b,c) lwip_connect(a,b,c)
#define getsockname(a,b,c) lwip_getsockname(a,b,c)
#define getpeername(a,b,c) lwip_getpeername(a,b,c)
#define setsockopt(a,b,c,d,e) lwip_setsockopt(a,b,c,d,e)
#define getsockopt(a,b,c,d,e) lwip_getsockopt(a,b,c,d,e)
#define listen(a,b) lwip_listen(a,b)
#define recv(a,b,c,d) lwip_recv(a,b,c,d)
#define recvfrom(a,b,c,d,e,f) lwip_recvfrom(a,b,c,d,e,f)
#define send(a,b,c,d) lwip_send(a,b,c,d)
#define sendto(a,b,c,d,e,f) lwip_sendto(a,b,c,d,e,f)
#define socket(a,b,c) lwip_socket(a,b,c)
#define select(a,b,c,d,e) lwip_select(a,b,c,d,e)
#define ioctlsocket(a,b,c) lwip_ioctl(a,b,c)
#define read(a,b,c) lwip_read(a,b,c)
#define write(a,b,c) lwip_write(a,b,c)
#define close(s) lwip_close(s)
#define getul(s) lwip_getul(s)
#endif /* ESPCONN_SOCKT_H_ */
#include "c_types.h"
#undef MBEDTLS_HAVE_ASM
#undef MBEDTLS_HAVE_SSE2
#define MBEDTLS_HAVE_TIME
#define MBEDTLS_HAVE_TIME_DATE
#undef MBEDTLS_PLATFORM_MEMORY
#undef MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
#undef MBEDTLS_PLATFORM_EXIT_ALT
#undef MBEDTLS_PLATFORM_TIME_ALT
#undef MBEDTLS_PLATFORM_FPRINTF_ALT
#undef MBEDTLS_PLATFORM_PRINTF_ALT
#undef MBEDTLS_PLATFORM_SNPRINTF_ALT
#undef MBEDTLS_PLATFORM_NV_SEED_ALT
#undef MBEDTLS_DEPRECATED_WARNING
#define MBEDTLS_DEPRECATED_REMOVED
#undef MBEDTLS_TIMING_ALT
#undef MBEDTLS_AES_ALT
#undef MBEDTLS_ARC4_ALT
#undef MBEDTLS_BLOWFISH_ALT
#undef MBEDTLS_CAMELLIA_ALT
#undef MBEDTLS_DES_ALT
#undef MBEDTLS_XTEA_ALT
#undef MBEDTLS_MD2_ALT
#undef MBEDTLS_MD4_ALT
#undef MBEDTLS_MD5_ALT
#undef MBEDTLS_RIPEMD160_ALT
#undef MBEDTLS_SHA1_ALT
#undef MBEDTLS_SHA256_ALT
#undef MBEDTLS_SHA512_ALT
#undef MBEDTLS_MD2_PROCESS_ALT
#undef MBEDTLS_MD4_PROCESS_ALT
#undef MBEDTLS_MD5_PROCESS_ALT
#undef MBEDTLS_RIPEMD160_PROCESS_ALT
#undef MBEDTLS_SHA1_PROCESS_ALT
#undef MBEDTLS_SHA256_PROCESS_ALT
#undef MBEDTLS_SHA512_PROCESS_ALT
#undef MBEDTLS_DES_SETKEY_ALT
#undef MBEDTLS_DES_CRYPT_ECB_ALT
#undef MBEDTLS_DES3_CRYPT_ECB_ALT
#undef MBEDTLS_AES_SETKEY_ENC_ALT
#undef MBEDTLS_AES_SETKEY_DEC_ALT
#undef MBEDTLS_AES_ENCRYPT_ALT
#undef MBEDTLS_AES_DECRYPT_ALT
#undef MBEDTLS_TEST_NULL_ENTROPY
#define MBEDTLS_ENTROPY_HARDWARE_ALT
#define MBEDTLS_AES_ROM_TABLES
#define MBEDTLS_CAMELLIA_SMALL_MEMORY
#define MBEDTLS_CIPHER_MODE_CBC
#define MBEDTLS_CIPHER_MODE_CFB
#define MBEDTLS_CIPHER_MODE_CTR
#undef MBEDTLS_CIPHER_NULL_CIPHER
#define MBEDTLS_CIPHER_PADDING_PKCS7
#define MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS
#define MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN
#define MBEDTLS_CIPHER_PADDING_ZEROS
#undef MBEDTLS_ENABLE_WEAK_CIPHERSUITES
#define MBEDTLS_REMOVE_ARC4_CIPHERSUITES
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
#define MBEDTLS_ECP_DP_SECP192K1_ENABLED
#define MBEDTLS_ECP_DP_SECP224K1_ENABLED
#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
#define MBEDTLS_ECP_DP_BP256R1_ENABLED
#define MBEDTLS_ECP_DP_BP384R1_ENABLED
#define MBEDTLS_ECP_DP_BP512R1_ENABLED
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
#define MBEDTLS_ECP_NIST_OPTIM
#undef MBEDTLS_ECDSA_DETERMINISTIC
#undef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
#undef MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
#undef MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
#undef MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
#undef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
#undef MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
#undef MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
#define MBEDTLS_PK_PARSE_EC_EXTENDED
#undef MBEDTLS_ERROR_STRERROR_DUMMY
#define MBEDTLS_GENPRIME
#undef MBEDTLS_FS_IO
#undef MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
#define MBEDTLS_NO_PLATFORM_ENTROPY
#define MBEDTLS_ENTROPY_FORCE_SHA256
#undef MBEDTLS_ENTROPY_NV_SEED
#undef MBEDTLS_MEMORY_DEBUG
#undef MBEDTLS_MEMORY_BACKTRACE
#define MBEDTLS_PK_RSA_ALT_SUPPORT
#define MBEDTLS_PKCS1_V15
#define MBEDTLS_PKCS1_V21
#undef MBEDTLS_RSA_NO_CRT
#undef MBEDTLS_SELF_TEST
#define MBEDTLS_SHA256_SMALLER
#undef MBEDTLS_SSL_AEAD_RANDOM_IV
#define MBEDTLS_SSL_ALL_ALERT_MESSAGES
#undef MBEDTLS_SSL_DEBUG_ALL
#define MBEDTLS_SSL_ENCRYPT_THEN_MAC
#define MBEDTLS_SSL_EXTENDED_MASTER_SECRET
#define MBEDTLS_SSL_FALLBACK_SCSV
#undef MBEDTLS_SSL_HW_RECORD_ACCEL
#define MBEDTLS_SSL_CBC_RECORD_SPLITTING
#define MBEDTLS_SSL_RENEGOTIATION
#undef MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO
#undef MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE
#define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
#undef MBEDTLS_SSL_PROTO_SSL3
#define MBEDTLS_SSL_PROTO_TLS1
#define MBEDTLS_SSL_PROTO_TLS1_1
#define MBEDTLS_SSL_PROTO_TLS1_2
#define MBEDTLS_SSL_PROTO_DTLS
#define MBEDTLS_SSL_ALPN
#define MBEDTLS_SSL_DTLS_ANTI_REPLAY
#define MBEDTLS_SSL_DTLS_HELLO_VERIFY
#define MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE
#define MBEDTLS_SSL_DTLS_BADMAC_LIMIT
#define MBEDTLS_SSL_SESSION_TICKETS
#define MBEDTLS_SSL_EXPORT_KEYS
#define MBEDTLS_SSL_SERVER_NAME_INDICATION
#define MBEDTLS_SSL_TRUNCATED_HMAC
#undef MBEDTLS_THREADING_ALT
#undef MBEDTLS_THREADING_PTHREAD
#define MBEDTLS_VERSION_FEATURES
#undef MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
#undef MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
#define MBEDTLS_X509_CHECK_KEY_USAGE
#define MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE
#define MBEDTLS_X509_RSASSA_PSS_SUPPORT
#undef MBEDTLS_ZLIB_SUPPORT
#undef MBEDTLS_AESNI_C
#define MBEDTLS_AES_C
#define MBEDTLS_ARC4_C
#define MBEDTLS_ASN1_PARSE_C
#define MBEDTLS_ASN1_WRITE_C
#define MBEDTLS_BASE64_C
#define MBEDTLS_BIGNUM_C
#define MBEDTLS_BLOWFISH_C
#define MBEDTLS_CAMELLIA_C
#define MBEDTLS_CCM_C
#undef MBEDTLS_CERTS_C
#define MBEDTLS_CIPHER_C
#define MBEDTLS_CTR_DRBG_C
#undef MBEDTLS_DEBUG_C
#define MBEDTLS_DES_C
#define MBEDTLS_DHM_C
#define MBEDTLS_ECDH_C
#undef MBEDTLS_ECDSA_C
#undef MBEDTLS_ECJPAKE_C
#define MBEDTLS_ECP_C
#define MBEDTLS_ENTROPY_C
#define MBEDTLS_ERROR_C
#define MBEDTLS_GCM_C
#undef MBEDTLS_HAVEGE_C
#define MBEDTLS_HMAC_DRBG_C
#define MBEDTLS_MD_C
#undef MBEDTLS_MD2_C
#undef MBEDTLS_MD4_C
#define MBEDTLS_MD5_C
#undef MBEDTLS_MEMORY_BUFFER_ALLOC_C
#define MBEDTLS_NET_C
#define MBEDTLS_OID_C
#undef MBEDTLS_PADLOCK_C
#define MBEDTLS_PEM_PARSE_C
#define MBEDTLS_PEM_WRITE_C
#define MBEDTLS_PK_C
#define MBEDTLS_PK_PARSE_C
#define MBEDTLS_PK_WRITE_C
#define MBEDTLS_PKCS5_C
#undef MBEDTLS_PKCS11_C
#define MBEDTLS_PKCS12_C
#define MBEDTLS_PLATFORM_C
#define MBEDTLS_RIPEMD160_C
#define MBEDTLS_RSA_C
#define MBEDTLS_SHA1_C
#define MBEDTLS_SHA256_C
#define MBEDTLS_SHA512_C
#define MBEDTLS_SSL_CACHE_C
#define MBEDTLS_SSL_COOKIE_C
#define MBEDTLS_SSL_TICKET_C
#define MBEDTLS_SSL_CLI_C
#define MBEDTLS_SSL_SRV_C
#define MBEDTLS_SSL_TLS_C
#undef MBEDTLS_THREADING_C
#undef MBEDTLS_TIMING_C
#define MBEDTLS_VERSION_C
#define MBEDTLS_X509_USE_C
#define MBEDTLS_X509_CRT_PARSE_C
#define MBEDTLS_X509_CRL_PARSE_C
#define MBEDTLS_X509_CSR_PARSE_C
#define MBEDTLS_X509_CREATE_C
#define MBEDTLS_X509_CRT_WRITE_C
#define MBEDTLS_X509_CSR_WRITE_C
#define MBEDTLS_XTEA_C
#define MBEDTLS_MPI_WINDOW_SIZE 1 /**< Maximum windows size used. */
#define MBEDTLS_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
//#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 1000 /**< Interval before reseed is performed by default */
//#define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
//#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
//#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 1000 /**< Interval before reseed is performed by default */
//#define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
//#define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
//#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
//#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
#define MBEDTLS_ECP_WINDOW_SIZE 2 /**< Maximum window size used */
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */
//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
//#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
//#define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
//#define MBEDTLS_PLATFORM_STD_MEM_HDR <stdlib.h> /**< Header to include if MBEDTLS_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */
//#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS 0 /**< Default exit value to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE 1 /**< Default exit value to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read /**< Default nv_seed_read function to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write /**< Default nv_seed_write function to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile" /**< Seed file to read/write with default implementation */
//#define MBEDTLS_PLATFORM_CALLOC_MACRO calloc /**< Default allocator macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_FREE_MACRO free /**< Default free macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_EXIT_MACRO exit /**< Default exit macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_TIME_MACRO time /**< Default time macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_TIME_TYPE_MACRO time_t /**< Default time macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_PRINTF_MACRO printf /**< Default printf macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf /**< Default snprintf macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_NV_SEED_READ_MACRO mbedtls_platform_std_nv_seed_read /**< Default nv_seed_read function to use, can be undefined */
//#define MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO mbedtls_platform_std_nv_seed_write /**< Default nv_seed_write function to use, can be undefined */
//#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */
//#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */
#define MBEDTLS_SSL_MAX_CONTENT_LEN 4096 /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */
//#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
//#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */
//#define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */
//#define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
#define MBEDTLS_X509_MAX_INTERMEDIATE_CA 3 /**< Maximum number of intermediate CAs in a verification chain. */
#ifndef __USER_VERSION_H__
#define __USER_VERSION_H__
#define NODE_VERSION_MAJOR 1U
#define NODE_VERSION_MINOR 5U
#define NODE_VERSION_REVISION 4U
#define NODE_VERSION_INTERNAL 1U
#define NODE_VERSION_MAJOR 2U
#define NODE_VERSION_MINOR 0U
#define NODE_VERSION_REVISION 0U
#define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMCU 1.5.4.1"
#define NODE_VERSION "NodeMCU 2.0.0"
#ifndef BUILD_DATE
#define BUILD_DATE "unspecified"
#endif
......
......@@ -144,7 +144,7 @@ reswitch: switch (ch = *fmt++) {
}
static void
kprintn(void (*put)(const char), unsigned long ul, int base, int width, char padchar)
kprintn(void (*put)(const char), uint32_t ul, int base, int width, char padchar)
{
/* hold a long in base 8 */
char *p, buf[(sizeof(long) * 8 / 3) + 2];
......
#include "lwip/inet.h"
#include "lwip/err.h"
#include "lwip/err.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/mem.h"
......@@ -25,7 +25,6 @@ static struct udp_pcb *pcb_dhcps = NULL;
static struct ip_addr broadcast_dhcps;
static struct ip_addr server_address;
static struct ip_addr client_address;//added
static struct ip_addr client_address_plus;
static struct dhcps_lease dhcps_lease;
//static bool dhcps_lease_flag = true;
......@@ -34,6 +33,10 @@ static uint8 offer = 0xFF;
static bool renew = false;
#define DHCPS_LEASE_TIME_DEF (120)
uint32 dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute
void wifi_softap_dhcps_client_leave(u8 *bssid, struct ip_addr *ip,bool force);
uint32 wifi_softap_dhcps_client_update(u8 *bssid, struct ip_addr *ip);
/******************************************************************************
* FunctionName : node_insert_to_list
* Description : insert the node to the list
......@@ -90,10 +93,12 @@ void ICACHE_FLASH_ATTR node_remove_from_list(list_node **phead, list_node* pdele
} else {
if (plist == pdelete){
*phead = plist->pnext;
pdelete->pnext = NULL;
} else {
while (plist != NULL) {
if (plist->pnext == pdelete){
plist->pnext = pdelete->pnext;
pdelete->pnext = NULL;
}
plist = plist->pnext;
}
......@@ -248,7 +253,7 @@ static void ICACHE_FLASH_ATTR create_msg(struct dhcps_msg *m)
{
struct ip_addr client;
client.addr = *( (uint32_t *) &client_address);
client.addr = client_address.addr;
m->op = DHCP_REPLY;
m->htype = DHCP_HTYPE_ETHERNET;
......@@ -267,7 +272,10 @@ static void ICACHE_FLASH_ATTR create_msg(struct dhcps_msg *m)
os_memset((char *) m->file, 0, sizeof(m->file));
os_memset((char *) m->options, 0, sizeof(m->options));
os_memcpy((char *) m->options, &magic_cookie, sizeof(magic_cookie));
//For xiaomi crash bug
uint32 magic_cookie1 = magic_cookie;
os_memcpy((char *) m->options, &magic_cookie1, sizeof(magic_cookie1));
}
///////////////////////////////////////////////////////////////////////////////////
/*
......@@ -307,12 +315,6 @@ static void ICACHE_FLASH_ATTR send_offer(struct dhcps_msg *m)
for(i=0; i<q->len; i++)
{
data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG
os_printf("%02x ",data[i]);
if((i+1)%16 == 0){
os_printf("\n");
}
#endif
}
q = q->next;
......@@ -373,12 +375,6 @@ static void ICACHE_FLASH_ATTR send_nak(struct dhcps_msg *m)
for(i=0; i<q->len; i++)
{
data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG
os_printf("%02x ",data[i]);
if((i+1)%16 == 0){
os_printf("\n");
}
#endif
}
q = q->next;
......@@ -440,12 +436,6 @@ static void ICACHE_FLASH_ATTR send_ack(struct dhcps_msg *m)
for(i=0; i<q->len; i++)
{
data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG
os_printf("%02x ",data[i]);
if((i+1)%16 == 0){
os_printf("\n");
}
#endif
}
q = q->next;
......@@ -503,6 +493,7 @@ static uint8_t ICACHE_FLASH_ATTR parse_options(uint8_t *optptr, sint16_t len)
break;
case DHCP_OPTION_REQ_IPADDR://50
//os_printf("dhcps:0x%08x,0x%08x\n",client.addr,*(uint32*)(optptr+2));
if( os_memcmp( (char *) &client.addr, (char *) optptr+2,4)==0 ) {
#if DHCPS_DEBUG
os_printf("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n");
......@@ -573,158 +564,24 @@ static uint8_t ICACHE_FLASH_ATTR parse_options(uint8_t *optptr, sint16_t len)
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
static sint16_t ICACHE_FLASH_ATTR parse_msg(struct dhcps_msg *m, u16_t len)
{
if(os_memcmp((char *)m->options,
&magic_cookie,
sizeof(magic_cookie)) == 0){
#if DHCPS_DEBUG
os_printf("dhcps: len = %d\n", len);
#endif
/*
* ��¼��ǰ��xid���ﴦ���?
* �˺�ΪDHCP�ͻ����������û�ͳһ��ȡIPʱ��
*/
// if((old_xid[0] == 0) &&
// (old_xid[1] == 0) &&
// (old_xid[2] == 0) &&
// (old_xid[3] == 0)){
// /*
// * old_xidδ��¼�κ����?
// * �϶��ǵ�һ��ʹ��
// */
// os_memcpy((char *)old_xid, (char *)m->xid, sizeof(m->xid));
// }else{
// /*
// * ���δ����DHCP msg��Я���xid���ϴμ�¼�IJ�ͬ��
// * �϶�Ϊ��ͬ��DHCP�ͻ��˷��ͣ���ʱ����Ҫ����Ŀͻ���IP
// * ���� 192.168.4.100(0x6404A8C0) <--> 192.168.4.200(0xC804A8C0)
// *
// */
// if(os_memcmp((char *)old_xid, (char *)m->xid, sizeof(m->xid)) != 0){
/*
* ��¼���ε�xid�ţ�ͬʱ�����IP����
*/
struct ip_addr addr_tmp;
// os_memcpy((char *)old_xid, (char *)m->xid, sizeof(m->xid));
// {
struct dhcps_pool *pdhcps_pool = NULL;
list_node *pnode = NULL;
list_node *pback_node = NULL;
struct ip_addr first_address;
bool flag = false;
// POOL_START:
first_address.addr = dhcps_lease.start_ip.addr;
client_address.addr = client_address_plus.addr;
renew = false;
// addr_tmp.addr = htonl(client_address_plus.addr);
// addr_tmp.addr++;
// client_address_plus.addr = htonl(addr_tmp.addr);
for (pback_node = plist; pback_node != NULL;pback_node = pback_node->pnext) {
pdhcps_pool = pback_node->pnode;
if (os_memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0){
// os_printf("the same device request ip\n");
if (os_memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) {
renew = true;
}
client_address.addr = pdhcps_pool->ip.addr;
pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER;
pnode = pback_node;
goto POOL_CHECK;
} else if (pdhcps_pool->ip.addr == client_address_plus.addr){
// client_address.addr = client_address_plus.addr;
// os_printf("the ip addr has been request\n");
addr_tmp.addr = htonl(client_address_plus.addr);
addr_tmp.addr++;
client_address_plus.addr = htonl(addr_tmp.addr);
client_address.addr = client_address_plus.addr;
}
if(flag == false) { // search the fisrt unused ip
if(first_address.addr < pdhcps_pool->ip.addr) {
flag = true;
} else {
addr_tmp.addr = htonl(first_address.addr);
addr_tmp.addr++;
first_address.addr = htonl(addr_tmp.addr);
}
}
}
if (client_address_plus.addr > dhcps_lease.end_ip.addr) {
client_address.addr = first_address.addr;
}
if (client_address.addr > dhcps_lease.end_ip.addr) {
client_address_plus.addr = dhcps_lease.start_ip.addr;
pdhcps_pool = NULL;
pnode = NULL;
} else {
pdhcps_pool = (struct dhcps_pool *)os_zalloc(sizeof(struct dhcps_pool));
pdhcps_pool->ip.addr = client_address.addr;
os_memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac));
pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER;
pnode = (list_node *)os_zalloc(sizeof(list_node ));
pnode->pnode = pdhcps_pool;
pnode->pnext = NULL;
node_insert_to_list(&plist,pnode);
if (client_address.addr == dhcps_lease.end_ip.addr) {
client_address_plus.addr = dhcps_lease.start_ip.addr;
} else {
addr_tmp.addr = htonl(client_address.addr);
addr_tmp.addr++;
client_address_plus.addr = htonl(addr_tmp.addr);
}
}
POOL_CHECK:
if ((client_address.addr > dhcps_lease.end_ip.addr) || (ip_addr_isany(&client_address))){
os_printf("client_address_plus.addr %x %d\n", client_address_plus.addr, system_get_free_heap_size());
if(pnode != NULL) {
node_remove_from_list(&plist,pnode);
os_free(pnode);
pnode = NULL;
}
if (pdhcps_pool != NULL) {
os_free(pdhcps_pool);
pdhcps_pool = NULL;
}
// client_address_plus.addr = dhcps_lease.start_ip.addr;
return 4;
}
sint16_t ret = parse_options(&m->options[4], len);;
if(ret == DHCPS_STATE_RELEASE) {
if(pnode != NULL) {
node_remove_from_list(&plist,pnode);
os_free(pnode);
pnode = NULL;
}
if (pdhcps_pool != NULL) {
os_free(pdhcps_pool);
pdhcps_pool = NULL;
}
os_memset(&client_address,0x0,sizeof(client_address));
}
if (wifi_softap_set_station_info(m->chaddr, &client_address) == false) {
return 0;
}
// }
{
if(os_memcmp((char *)m->options,
&magic_cookie,
sizeof(magic_cookie)) == 0){
struct ip_addr ip;
os_memcpy(&ip.addr,m->ciaddr,sizeof(ip.addr));
client_address.addr = wifi_softap_dhcps_client_update(m->chaddr,&ip);
sint16_t ret = parse_options(&m->options[4], len);
if(ret == DHCPS_STATE_RELEASE) {
wifi_softap_dhcps_client_leave(m->chaddr,&ip,TRUE); // force to delete
client_address.addr = ip.addr;
}
#if DHCPS_DEBUG
os_printf("dhcps: xid changed\n");
os_printf("dhcps: client_address.addr = %x\n", client_address.addr);
#endif
// }
// }
return ret;
}
return 0;
return ret;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
/*
......@@ -772,12 +629,6 @@ static void ICACHE_FLASH_ATTR handle_dhcp(void *arg,
for(i=0; i<p->len; i++){
p_dhcps_msg[dhcps_msg_cnt++] = data[i];
#if DHCPS_DEBUG
os_printf("%02x ",data[i]);
if((i+1)%16 == 0){
os_printf("\n");
}
#endif
}
if(p->next != NULL) {
......@@ -790,12 +641,6 @@ static void ICACHE_FLASH_ATTR handle_dhcp(void *arg,
data = p->next->payload;
for(i=0; i<p->next->len; i++){
p_dhcps_msg[dhcps_msg_cnt++] = data[i];
#if DHCPS_DEBUG
os_printf("%02x ",data[i]);
if((i+1)%16 == 0){
os_printf("\n");
}
#endif
}
}
......@@ -899,7 +744,6 @@ void ICACHE_FLASH_ATTR dhcps_start(struct ip_info *info)
server_address = info->ip;
wifi_softap_init_dhcps_lease(server_address.addr);
client_address_plus.addr = dhcps_lease.start_ip.addr;
udp_bind(pcb_dhcps, IP_ADDR_ANY, DHCPS_SERVER_PORT);
udp_recv(pcb_dhcps, handle_dhcp, NULL);
......@@ -923,11 +767,18 @@ void ICACHE_FLASH_ATTR dhcps_stop(void)
//udp_remove(pcb_dhcps);
list_node *pnode = NULL;
list_node *pback_node = NULL;
struct dhcps_pool* dhcp_node = NULL;
struct ip_addr ip_zero;
os_memset(&ip_zero,0x0,sizeof(ip_zero));
pnode = plist;
while (pnode != NULL) {
pback_node = pnode;
pnode = pback_node->pnext;
node_remove_from_list(&plist, pback_node);
dhcp_node = (struct dhcps_pool*)pback_node->pnode;
//wifi_softap_dhcps_client_leave(dhcp_node->mac,&dhcp_node->ip,TRUE); // force to delete
wifi_softap_set_station_info(dhcp_node->mac, &ip_zero);
os_free(pback_node->pnode);
pback_node->pnode = NULL;
os_free(pback_node);
......@@ -1065,7 +916,9 @@ void ICACHE_FLASH_ATTR dhcps_coarse_tmr(void)
pnode = plist;
while (pnode != NULL) {
pdhcps_pool = pnode->pnode;
pdhcps_pool->lease_timer --;
if ( pdhcps_pool->type == DHCPS_TYPE_DYNAMIC) {
pdhcps_pool->lease_timer --;
}
if (pdhcps_pool->lease_timer == 0){
pback_node = pnode;
pnode = pback_node->pnext;
......@@ -1144,3 +997,184 @@ uint32 ICACHE_FLASH_ATTR wifi_softap_get_dhcps_lease_time(void) // minute
{
return dhcps_lease_time;
}
void ICACHE_FLASH_ATTR wifi_softap_dhcps_client_leave(u8 *bssid, struct ip_addr *ip,bool force)
{
struct dhcps_pool *pdhcps_pool = NULL;
list_node *pback_node = NULL;
if ((bssid == NULL) || (ip == NULL)) {
return;
}
for (pback_node = plist; pback_node != NULL;pback_node = pback_node->pnext) {
pdhcps_pool = pback_node->pnode;
if (os_memcmp(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)) == 0){
if (os_memcmp(&pdhcps_pool->ip.addr, &ip->addr, sizeof(pdhcps_pool->ip.addr)) == 0) {
if ((pdhcps_pool->type == DHCPS_TYPE_STATIC) || (force)) {
if(pback_node != NULL) {
node_remove_from_list(&plist,pback_node);
os_free(pback_node);
pback_node = NULL;
}
if (pdhcps_pool != NULL) {
os_free(pdhcps_pool);
pdhcps_pool = NULL;
}
} else {
pdhcps_pool->state = DHCPS_STATE_OFFLINE;
}
struct ip_addr ip_zero;
os_memset(&ip_zero,0x0,sizeof(ip_zero));
wifi_softap_set_station_info(bssid, &ip_zero);
break;
}
}
}
}
uint32 ICACHE_FLASH_ATTR wifi_softap_dhcps_client_update(u8 *bssid, struct ip_addr *ip)
{
struct dhcps_pool *pdhcps_pool = NULL;
list_node *pback_node = NULL;
list_node *pmac_node = NULL;
list_node *pip_node = NULL;
bool flag = FALSE;
uint32 start_ip = dhcps_lease.start_ip.addr;
uint32 end_ip = dhcps_lease.end_ip.addr;
dhcps_type_t type = DHCPS_TYPE_DYNAMIC;
if (bssid == NULL) {
return IPADDR_ANY;
}
if (ip) {
if (IPADDR_BROADCAST == ip->addr) {
return IPADDR_ANY;
} else if (IPADDR_ANY == ip->addr) {
ip = NULL;
} else {
type = DHCPS_TYPE_STATIC;
}
}
renew = FALSE;
for (pback_node = plist; pback_node != NULL;pback_node = pback_node->pnext) {
pdhcps_pool = pback_node->pnode;
//os_printf("mac:"MACSTR"bssid:"MACSTR"\r\n",MAC2STR(pdhcps_pool->mac),MAC2STR(bssid));
if (os_memcmp(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac)) == 0){
pmac_node = pback_node;
if (ip == NULL) {
flag = TRUE;
break;
}
}
if (ip != NULL) {
if (os_memcmp(&pdhcps_pool->ip.addr, &ip->addr, sizeof(pdhcps_pool->ip.addr)) == 0) {
pip_node = pback_node;
}
} else if (flag == FALSE){
if (os_memcmp(&pdhcps_pool->ip.addr, &start_ip, sizeof(pdhcps_pool->ip.addr)) != 0) {
flag = TRUE;
} else {
start_ip = htonl((ntohl(start_ip) + 1));
}
}
}
if ((ip == NULL) && (flag == FALSE)) {
if (plist == NULL) {
if (start_ip <= end_ip) {
flag = TRUE;
} else {
return IPADDR_ANY;
}
} else {
if (start_ip > end_ip) {
return IPADDR_ANY;
}
//start_ip = htonl((ntohl(start_ip) + 1));
flag = TRUE;
}
}
if (pmac_node != NULL) { // update new ip
if (pip_node != NULL){
pdhcps_pool = pip_node->pnode;
if (pip_node != pmac_node) {
if(pdhcps_pool->state != DHCPS_STATE_OFFLINE) { // ip is used
return IPADDR_ANY;
}
// mac exists and ip exists in other node,delete mac
node_remove_from_list(&plist,pmac_node);
os_free(pmac_node->pnode);
pmac_node->pnode = NULL;
os_free(pmac_node);
pmac_node = pip_node;
os_memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac));
} else {
renew = true;
type = DHCPS_TYPE_DYNAMIC;
}
pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER;
pdhcps_pool->type = type;
pdhcps_pool->state = DHCPS_STATE_ONLINE;
} else {
pdhcps_pool = pmac_node->pnode;
if (ip != NULL) {
pdhcps_pool->ip.addr = ip->addr;
} else if (flag == TRUE) {
pdhcps_pool->ip.addr = start_ip;
} else { // no ip to distribute
return IPADDR_ANY;
}
node_remove_from_list(&plist,pmac_node);
pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER;
pdhcps_pool->type = type;
pdhcps_pool->state = DHCPS_STATE_ONLINE;
node_insert_to_list(&plist,pmac_node);
}
} else { // new station
if (pip_node != NULL) { // maybe ip has used
pdhcps_pool = pip_node->pnode;
if (pdhcps_pool->state != DHCPS_STATE_OFFLINE) {
return IPADDR_ANY;
}
os_memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac));
pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER;
pdhcps_pool->type = type;
pdhcps_pool->state = DHCPS_STATE_ONLINE;
} else {
pdhcps_pool = (struct dhcps_pool *)os_zalloc(sizeof(struct dhcps_pool));
if (ip != NULL) {
pdhcps_pool->ip.addr = ip->addr;
} else if (flag == TRUE) {
pdhcps_pool->ip.addr = start_ip;
} else { // no ip to distribute
os_free(pdhcps_pool);
return IPADDR_ANY;
}
if (pdhcps_pool->ip.addr > end_ip) {
os_free(pdhcps_pool);
return IPADDR_ANY;
}
os_memcpy(pdhcps_pool->mac, bssid, sizeof(pdhcps_pool->mac));
pdhcps_pool->lease_timer = DHCPS_LEASE_TIMER;
pdhcps_pool->type = type;
pdhcps_pool->state = DHCPS_STATE_ONLINE;
pback_node = (list_node *)os_zalloc(sizeof(list_node ));
pback_node->pnode = pdhcps_pool;
pback_node->pnext = NULL;
node_insert_to_list(&plist,pback_node);
}
}
wifi_softap_set_station_info(bssid, &pdhcps_pool->ip);
return pdhcps_pool->ip.addr;
}
......@@ -436,6 +436,39 @@ espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length)
return ESPCONN_ARG;
}
sint16 ICACHE_FLASH_ATTR espconn_recv(struct espconn *espconn, void *mem, size_t len)
{
espconn_msg *pnode = NULL;
bool value = false;
int bytes_used = 0;
if (espconn == NULL || mem == NULL || len == 0)
return ESPCONN_ARG;
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value && espconn->type == ESPCONN_TCP){
if (pnode->readbuf != NULL){
bytes_used = ringbuf_bytes_used(pnode->readbuf);
if (bytes_used != 0) {
if (len > bytes_used) {
len = bytes_used;
}
ringbuf_memcpy_from(mem, pnode->readbuf, len);
espconn_recv_unhold(pnode->pespconn);
return len;
} else {
return ESPCONN_OK;
}
} else{
return ESPCONN_OK;
}
} else{
return ESPCONN_ARG;
}
return ESPCONN_ARG;
}
/******************************************************************************
* FunctionName : espconn_sendto
* Description : send data for UDP
......@@ -954,7 +987,7 @@ espconn_disconnect(struct espconn *espconn)
if (value){
/*protect for redisconnection*/
if (espconn->state == ESPCONN_CLOSE)
if (pnode->preverse == NULL && espconn->state == ESPCONN_CLOSE)
return ESPCONN_INPROGRESS;
espconn_tcp_disconnect(pnode,0); //1 force, 0 normal
return ESPCONN_OK;
......
/*
* espconn_buf.c
*
* Created on: May 25, 2016
* Author: liuhan
*/
#include "lwip/memp.h"
#include "lwip/def.h"
#include "ets_sys.h"
#include "os_type.h"
#include "lwip/app/espconn_buf.h"
#ifdef MEMLEAK_DEBUG
static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
#endif
#if (!defined(lwIP_unlikely))
#define lwIP_unlikely(Expression) !!(Expression)
#endif
#define lwIP_ASSERT(Expression) do{if(!(Expression)) {os_printf("%s %d\n", __func__, __LINE__);return;}}while(0)
ringbuf_t ringbuf_new(size_t capacity)
{
ringbuf_t rb = (ringbuf_t)os_zalloc(sizeof(struct ringbuf_t));
if (rb){
rb->size = capacity + 1;
rb->buf = (uint8*)os_zalloc(rb->size);
if (rb->buf){
ringbuf_reset(rb);
}else{
os_free(rb);
return NULL;
}
}
return rb;
}
size_t ringbuf_buffer_size(const struct ringbuf_t *rb)
{
return rb->size;
}
void ringbuf_reset(ringbuf_t rb)
{
rb ->head = rb->tail = rb->buf;
}
void ringbuf_free(ringbuf_t *rb)
{
lwIP_ASSERT(rb && *rb);
os_free((*rb)->buf);
os_free(*rb);
*rb = NULL;
}
size_t ringbuf_capacity(const struct ringbuf_t *rb)
{
return ringbuf_buffer_size(rb) - 1;
}
static const uint8_t* ringbuf_end(const struct ringbuf_t *rb)
{
return rb->buf + ringbuf_buffer_size(rb);
}
size_t ringbuf_bytes_free(const struct ringbuf_t *rb)
{
if (rb->head >= rb->tail){
return ringbuf_capacity(rb) - (rb->head - rb->tail);
}else{
return rb->tail - rb->head -1;
}
}
size_t ringbuf_bytes_used(const struct ringbuf_t *rb)
{
return ringbuf_capacity(rb) - ringbuf_bytes_free(rb);
}
int ringbuf_is_full(const struct ringbuf_t *rb)
{
return ringbuf_bytes_free(rb) == 0;
}
int ringbuf_is_empty(const struct ringbuf_t *rb)
{
return ringbuf_bytes_free(rb) == ringbuf_capacity(rb);
}
const void* ringbuf_tail(const struct ringbuf_t *rb)
{
return rb->tail;
}
const void* ringbuf_head(const struct ringbuf_t *rb)
{
return rb->head;
}
static uint8_t *ringbuf_nextp(ringbuf_t rb, const uint8_t *p)
{
lwIP_ASSERT((p >= rb->buf) && (p < ringbuf_end(rb)));
return rb->buf + ((++p -rb->buf) % ringbuf_buffer_size(rb));
}
size_t ringbuf_findchr(const struct ringbuf_t *rb, int c, size_t offset)
{
const uint8_t *bufend = ringbuf_end(rb);
size_t bytes_used = ringbuf_bytes_used(rb);
if (offset >= bytes_used)
return bytes_used;
const uint8_t *start = rb ->buf + (((rb->tail - rb->buf) + offset) % ringbuf_buffer_size(rb));
lwIP_ASSERT(bufend > start);
size_t n = LWIP_MIN(bufend - start, bytes_used - offset);
const uint8_t *found = (const uint8_t *)memchr(start, c, n);
if (found)
return offset + (found - start);
else
return ringbuf_findchr(rb, c, offset + n);
}
size_t ringbuf_memset(ringbuf_t dst, int c, size_t len)
{
const uint8_t *bufend = ringbuf_end(dst);
size_t nwritten = 0;
size_t count = LWIP_MIN(len, ringbuf_buffer_size(dst));
int overflow = count > ringbuf_bytes_free(dst);
while (nwritten != count){
lwIP_ASSERT(bufend > dst->head);
size_t n = LWIP_MIN(bufend - dst->head, count - nwritten);
os_memset(dst->head, c, n);
dst->head += n;
nwritten += n;
if (dst->head == bufend)
dst->head = dst->buf;
}
if (overflow){
dst->tail = ringbuf_nextp(dst, dst->head);
lwIP_ASSERT(ringbuf_is_full(dst));
}
return nwritten;
}
void *ringbuf_memcpy_into(ringbuf_t dst,const void *src, size_t count)
{
const uint8_t *u8src = src;
const uint8_t *bufend = ringbuf_end(dst);
int overflow = count > ringbuf_bytes_free(dst);
size_t nread = 0;
while (nread != count){
lwIP_ASSERT(bufend > dst->head);
size_t n = LWIP_MIN(bufend - dst->head, count - nread);
os_memcpy(dst->head, u8src + nread, n);
dst->head += n;
nread += n;
if (dst->head == bufend)
dst->head = dst->buf;
}
if (overflow) {
dst->tail = ringbuf_nextp(dst, dst->head);
lwIP_ASSERT(ringbuf_is_full(dst));
}
return dst->head;
}
void *ringbuf_memcpy_from(void *dst,ringbuf_t src, size_t count)
{
size_t bytes_used = ringbuf_bytes_used(src);
if (count > bytes_used)
return NULL;
const uint8_t *u8dst = dst;
const uint8_t *bufend = ringbuf_end(src);
size_t nwritten = 0;
while (nwritten != count){
lwIP_ASSERT(bufend > src->tail);
size_t n = LWIP_MIN(bufend - src->tail, count - nwritten);
os_memcpy((uint8_t*)u8dst + nwritten, src->tail, n);
src->tail += n;
nwritten += n;
if (src->tail == bufend)
src->tail = src->buf;
}
lwIP_ASSERT(count + ringbuf_bytes_used(src) == bytes_used);
return src->tail;
}
......@@ -219,6 +219,34 @@ struct tcp_pcb *ICACHE_FLASH_ATTR espconn_find_current_pcb(espconn_msg *pcurrent
return NULL;
}
/******************************************************************************
* FunctionName : espconn_tcp_memp_free
* Description : frees the connection memory in the server mode
* Parameters : arg -- Additional argument to pass to the function
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR espconn_tcp_memp_free(espconn_msg *pmemp)
{
struct espconn *espconn = NULL;
if (pmemp == NULL)
return;
if (pmemp->espconn_mode == ESPCONN_TCPSERVER_MODE){
if (pmemp->pespconn != NULL && pmemp->pespconn->proto.tcp != NULL)
os_free(pmemp->pespconn->proto.tcp);
pmemp->pespconn->proto.tcp = NULL;
os_free(pmemp->pespconn);
pmemp->pespconn = NULL;
}
if (pmemp->readbuf != NULL){
ringbuf_free(&pmemp->readbuf);
}
os_free(pmemp);
pmemp = NULL;
}
/******************************************************************************
* FunctionName : espconn_tcp_reconnect
* Description : reconnect with host
......@@ -235,24 +263,15 @@ espconn_tcp_reconnect(void *arg)
espconn_kill_oldest_pcb();
if (precon_cb != NULL) {
struct espconn *espconn = precon_cb->preverse;
re_err = precon_cb->pcommon.err;
if (precon_cb->pespconn != NULL){
if (espconn != NULL){/*Process the server's message block*/
if (precon_cb->pespconn->proto.tcp != NULL){
espconn_copy_partial(espconn, precon_cb->pespconn);
espconn_printf("server: %d.%d.%d.%d : %d reconnection\n", espconn->proto.tcp->remote_ip[0],
espconn->proto.tcp->remote_ip[1],espconn->proto.tcp->remote_ip[2],
espconn->proto.tcp->remote_ip[3],espconn->proto.tcp->remote_port);
os_free(precon_cb->pespconn->proto.tcp);
precon_cb->pespconn->proto.tcp = NULL;
}
os_free(precon_cb->pespconn);
precon_cb->pespconn = NULL;
} else {/*Process the client's message block*/
espconn = precon_cb->pespconn;
espconn_printf("client: %d.%d.%d.%d : %d reconnection\n", espconn->proto.tcp->local_ip[0],
espconn->proto.tcp->local_ip[1],espconn->proto.tcp->local_ip[2],
espconn->proto.tcp->local_ip[3],espconn->proto.tcp->local_port);
}
}
......@@ -267,11 +286,13 @@ espconn_tcp_reconnect(void *arg)
}
os_bzero(&pktinfo[1], sizeof(struct espconn_packet));
os_memcpy(&pktinfo[1], (void*)&precon_cb->pcommon.packet_info, sizeof(struct espconn_packet));
os_free(precon_cb);
precon_cb = NULL;
if (espconn && espconn->proto.tcp && espconn->proto.tcp->reconnect_callback != NULL) {
espconn->proto.tcp->reconnect_callback(espconn, re_err);
}
/*frees the connection memory*/
espconn_tcp_memp_free(precon_cb);
} else {
espconn_printf("espconn_tcp_reconnect err\n");
}
......@@ -300,20 +321,11 @@ espconn_tcp_disconnect_successful(void *arg)
if (espconn != NULL){/*Process the server's message block*/
if (pdiscon_cb->pespconn->proto.tcp != NULL && espconn->proto.tcp){
espconn_copy_partial(espconn, pdiscon_cb->pespconn);
espconn_printf("server: %d.%d.%d.%d : %d disconnect\n", espconn->proto.tcp->remote_ip[0],
espconn->proto.tcp->remote_ip[1],espconn->proto.tcp->remote_ip[2],
espconn->proto.tcp->remote_ip[3],espconn->proto.tcp->remote_port);
os_free(pdiscon_cb->pespconn->proto.tcp);
pdiscon_cb->pespconn->proto.tcp = NULL;
}
os_free(pdiscon_cb->pespconn);
pdiscon_cb->pespconn = NULL;
} else {/*Process the client's message block*/
espconn = pdiscon_cb->pespconn;
espconn_printf("client: %d.%d.%d.%d : %d disconnect\n", espconn->proto.tcp->local_ip[0],
espconn->proto.tcp->local_ip[1],espconn->proto.tcp->local_ip[2],
espconn->proto.tcp->local_ip[3],espconn->proto.tcp->local_port);
}
/*process the current TCP block*/
pcb = espconn_find_current_pcb(pdiscon_cb);
if (pcb != NULL){
......@@ -368,11 +380,13 @@ espconn_tcp_disconnect_successful(void *arg)
}
os_bzero(&pktinfo[0], sizeof(struct espconn_packet));
os_memcpy(&pktinfo[0], (void*)&pdiscon_cb->pcommon.packet_info, sizeof(struct espconn_packet));
os_free(pdiscon_cb);
pdiscon_cb = NULL;
if (espconn->proto.tcp && espconn->proto.tcp->disconnect_callback != NULL) {
espconn->proto.tcp->disconnect_callback(espconn);
}
/*frees the connection memory*/
espconn_tcp_memp_free(pdiscon_cb);
} else {
espconn_printf("espconn_tcp_disconnect err\n");
}
......@@ -387,34 +401,46 @@ espconn_tcp_disconnect_successful(void *arg)
static void ICACHE_FLASH_ATTR
espconn_Task(os_event_t *events)
{
espconn_msg *plist = NULL;
bool active_flag = false;
espconn_msg *task_msg = NULL;
struct espconn *pespconn = NULL;
task_msg = (espconn_msg *) events->par;
switch (events->sig) {
case SIG_ESPCONN_WRITE: {
pespconn = task_msg->pespconn;
if (pespconn == NULL) {
return;
}
/*find the active connection node*/
for (plist = plink_active; plist != NULL; plist = plist->pnext){
if (task_msg == plist) {
active_flag = true;
break;
}
}
if (pespconn->proto.tcp->write_finish_fn != NULL) {
pespconn->proto.tcp->write_finish_fn(pespconn);
if (active_flag){
switch (events->sig) {
case SIG_ESPCONN_WRITE: {
pespconn = task_msg->pespconn;
if (pespconn == NULL) {
return;
}
if (pespconn->proto.tcp->write_finish_fn != NULL) {
pespconn->proto.tcp->write_finish_fn(pespconn);
}
}
break;
case SIG_ESPCONN_ERRER:
/*remove the node from the client's active connection list*/
espconn_list_delete(&plink_active, task_msg);
espconn_tcp_reconnect(task_msg);
break;
case SIG_ESPCONN_CLOSE:
/*remove the node from the client's active connection list*/
espconn_list_delete(&plink_active, task_msg);
espconn_tcp_disconnect_successful(task_msg);
break;
default:
break;
}
break;
case SIG_ESPCONN_ERRER:
/*remove the node from the client's active connection list*/
espconn_list_delete(&plink_active, task_msg);
espconn_tcp_reconnect(task_msg);
break;
case SIG_ESPCONN_CLOSE:
/*remove the node from the client's active connection list*/
espconn_list_delete(&plink_active, task_msg);
espconn_tcp_disconnect_successful(task_msg);
break;
default:
break;
}
}
......@@ -468,9 +494,13 @@ espconn_tcp_sent(void *arg, uint8 *psent, uint16 length)
err = tcp_write(pcb, psent, len, 0);
if (err == ERR_MEM) {
len /= 2;
if(len < 3)
len--;
else
len /= 2;
}
} while (err == ERR_MEM && len > 1);
} while (err == ERR_MEM && len > 0);
/*Find out what we can send and send it, offset the buffer point for next send*/
if (err == ERR_OK) {
......@@ -528,16 +558,22 @@ espconn_client_close(void *arg, struct tcp_pcb *pcb, u8 type)
if(type == 0)
err = tcp_close(pcb);
else
{tcp_abort(pcb); err = ERR_OK;}
else {
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
tcp_abort(pcb);
err = ERR_OK;
}
if (err != ERR_OK) {
/* closing failed, try again later */
tcp_recv(pcb, espconn_client_recv);
} else {
/* closing succeeded */
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
if (type == 0) {
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
}
/*switch the state of espconn for application process*/
pclose->pespconn->state = ESPCONN_CLOSE;
ets_post(espconn_TaskPrio, SIG_ESPCONN_CLOSE, (uint32_t)pclose);
......@@ -599,7 +635,37 @@ espconn_recv_unhold(struct espconn *pespconn)
}
//***********Code for WIFI_BLOCK from upper**************
sint8 ICACHE_FLASH_ATTR
espconn_lock_recv(espconn_msg *plockmsg)
{
if (plockmsg == NULL || plockmsg->pespconn == NULL) {
return ESPCONN_ARG;
}
if (plockmsg->pespconn->recv_callback == NULL){
if (plockmsg->readbuf == NULL){
plockmsg->readbuf = ringbuf_new(TCP_WND);
if (plockmsg->readbuf == NULL)
return ESPCONN_MEM;
}
return espconn_recv_hold(plockmsg->pespconn);
}
return ESPCONN_OK;
}
sint8 ICACHE_FLASH_ATTR
espconn_unlock_recv(espconn_msg *punlockmsg)
{
if (punlockmsg == NULL || punlockmsg->pespconn == NULL) {
return ESPCONN_ARG;
}
if (punlockmsg->pespconn->recv_callback != NULL)
return espconn_recv_unhold(punlockmsg->pespconn);
return ESPCONN_OK;
}
/******************************************************************************
* FunctionName : espconn_client_recv
* Description : Data has been received on this pcb.
......@@ -615,6 +681,8 @@ espconn_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
espconn_msg *precv_cb = arg;
tcp_arg(pcb, arg);
/*lock the window because of application layer don't need the data*/
espconn_lock_recv(precv_cb);
if (p != NULL) {
/*To update and advertise a larger window*/
......@@ -624,30 +692,38 @@ espconn_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
precv_cb->recv_holded_buf_Len += p->tot_len;
}
if (err == ERR_OK && p != NULL) {
char *pdata = NULL;
u16_t length = 0;
/*Copy the contents of a packet buffer to an application buffer.
*to prevent memory leaks, ensure that each allocated is deleted*/
pdata = (char *)os_zalloc(p ->tot_len + 1);
length = pbuf_copy_partial(p, pdata, p ->tot_len, 0);
pbuf_free(p);
if (length != 0) {
/*switch the state of espconn for application process*/
precv_cb->pespconn ->state = ESPCONN_READ;
precv_cb->pcommon.pcb = pcb;
if (precv_cb->pespconn->recv_callback != NULL) {
precv_cb->pespconn->recv_callback(precv_cb->pespconn, pdata, length);
}
/*switch the state of espconn for next packet copy*/
if (pcb->state == ESTABLISHED)
precv_cb->pespconn ->state = ESPCONN_CONNECT;
}
if (precv_cb->pespconn->recv_callback != NULL){
if (err == ERR_OK && p != NULL) {
char *pdata = NULL;
u16_t length = 0;
/*Copy the contents of a packet buffer to an application buffer.
*to prevent memory leaks, ensure that each allocated is deleted*/
pdata = (char *)os_zalloc(p ->tot_len + 1);
length = pbuf_copy_partial(p, pdata, p ->tot_len, 0);
pbuf_free(p);
if (length != 0) {
/*switch the state of espconn for application process*/
precv_cb->pespconn ->state = ESPCONN_READ;
precv_cb->pcommon.pcb = pcb;
precv_cb->pespconn->recv_callback(precv_cb->pespconn, pdata, length);
/*switch the state of espconn for next packet copy*/
if (pcb->state == ESTABLISHED)
precv_cb->pespconn ->state = ESPCONN_CONNECT;
}
/*to prevent memory leaks, ensure that each allocated is deleted*/
os_free(pdata);
pdata = NULL;
/*to prevent memory leaks, ensure that each allocated is deleted*/
os_free(pdata);
pdata = NULL;
}
} else{
/*unregister receive function*/
struct pbuf *pthis = NULL;
for (pthis = p; pthis != NULL; pthis = pthis->next) {
ringbuf_memcpy_into(precv_cb->readbuf, pthis->payload, pthis->len);
pbuf_free(pthis);
}
}
if (err == ERR_OK && p == NULL) {
......@@ -861,6 +937,8 @@ espconn_client_connect(void *arg, struct tcp_pcb *tpcb, err_t err)
if (espconn_keepalive_disabled(pcon))
espconn_keepalive_enable(tpcb);
// /*lock the window because of application layer don't need the data*/
// espconn_lock_recv(pcon);
} else{
os_printf("err in host connected (%s)\n",lwip_strerr(err));
}
......@@ -924,6 +1002,7 @@ espconn_tcp_client(struct espconn *espconn)
}
#endif
/*Establish the connection*/
pclient->espconn_mode = ESPCONN_TCPCLIENT_MODE;
pclient->pcommon.err = tcp_connect(pcb, &ipaddr,
pclient->pespconn->proto.tcp->remote_port, espconn_client_connect);
if (pclient->pcommon.err == ERR_RTE){
......@@ -958,17 +1037,24 @@ espconn_server_close(void *arg, struct tcp_pcb *pcb,u8 type)
if(type ==0)
err = tcp_close(pcb);
else
{tcp_abort(pcb); err = ERR_OK;}
else {
tcp_poll(pcb, NULL, 0);
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
tcp_abort(pcb);
err = ERR_OK;
}
if (err != ERR_OK) {
/* closing failed, try again later */
tcp_recv(pcb, espconn_server_recv);
} else {
/* closing succeeded */
tcp_poll(pcb, NULL, 0);
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
if (type == 0) {
tcp_poll(pcb, NULL, 0);
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
}
/*switch the state of espconn for application process*/
psclose->pespconn->state = ESPCONN_CLOSE;
ets_post(espconn_TaskPrio, SIG_ESPCONN_CLOSE, (uint32_t)psclose);
......@@ -991,6 +1077,9 @@ espconn_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
tcp_arg(pcb, arg);
espconn_printf("server has application data received: %d\n", system_get_free_heap_size());
/*lock the window because of application layer don't need the data*/
espconn_lock_recv(precv_cb);
if (p != NULL) {
/*To update and advertise a larger window*/
if(precv_cb->recv_hold_flag == 0)
......@@ -999,42 +1088,47 @@ espconn_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
precv_cb->recv_holded_buf_Len += p->tot_len;
}
if (err == ERR_OK && p != NULL) {
u8_t *data_ptr = NULL;
u32_t data_cntr = 0;
/*clear the count for connection timeout*/
precv_cb->pcommon.recv_check = 0;
/*Copy the contents of a packet buffer to an application buffer.
*to prevent memory leaks, ensure that each allocated is deleted*/
data_ptr = (u8_t *)os_zalloc(p ->tot_len + 1);
data_cntr = pbuf_copy_partial(p, data_ptr, p ->tot_len, 0);
pbuf_free(p);
if (data_cntr != 0) {
/*switch the state of espconn for application process*/
precv_cb->pespconn ->state = ESPCONN_READ;
precv_cb->pcommon.pcb = pcb;
if (precv_cb->pespconn->recv_callback != NULL) {
precv_cb->pespconn->recv_callback(precv_cb->pespconn, data_ptr, data_cntr);
}
/*switch the state of espconn for next packet copy*/
if (pcb->state == ESTABLISHED)
precv_cb->pespconn ->state = ESPCONN_CONNECT;
}
/*to prevent memory leaks, ensure that each allocated is deleted*/
os_free(data_ptr);
data_ptr = NULL;
espconn_printf("server's application data has been processed: %d\n", system_get_free_heap_size());
} else {
if (p != NULL) {
pbuf_free(p);
}
/*register receive function*/
if (precv_cb->pespconn->recv_callback != NULL) {
if (err == ERR_OK && p != NULL) {
u8_t *data_ptr = NULL;
u32_t data_cntr = 0;
/*clear the count for connection timeout*/
precv_cb->pcommon.recv_check = 0;
/*Copy the contents of a packet buffer to an application buffer.
*to prevent memory leaks, ensure that each allocated is deleted*/
data_ptr = (u8_t *) os_zalloc(p ->tot_len + 1);
data_cntr = pbuf_copy_partial(p, data_ptr, p->tot_len, 0);
pbuf_free(p);
if (data_cntr != 0) {
/*switch the state of espconn for application process*/
precv_cb->pespconn->state = ESPCONN_READ;
precv_cb->pcommon.pcb = pcb;
precv_cb->pespconn->recv_callback(precv_cb->pespconn, data_ptr, data_cntr);
/*switch the state of espconn for next packet copy*/
if (pcb->state == ESTABLISHED)
precv_cb->pespconn->state = ESPCONN_CONNECT;
}
espconn_server_close(precv_cb, pcb,0);
}
/*to prevent memory leaks, ensure that each allocated is deleted*/
os_free(data_ptr);
data_ptr = NULL;
espconn_printf("server's application data has been processed: %d\n", system_get_free_heap_size());
}
} else {
/*unregister receive function*/
struct pbuf *pthis = NULL;
for (pthis = p; pthis != NULL; pthis = pthis->next) {
ringbuf_memcpy_into(precv_cb->readbuf, pthis->payload, pthis->len);
pbuf_free(pthis);
}
}
if (err == ERR_OK && p == NULL) {
espconn_server_close(precv_cb, pcb, 0);
}
return ERR_OK;
}
......@@ -1225,6 +1319,7 @@ espconn_tcp_accept(void *arg, struct tcp_pcb *pcb, err_t err)
espconn_list_creat(&plink_active, paccept);
paccept->preverse = espconn;
paccept->espconn_mode = ESPCONN_TCPSERVER_MODE;
paccept->pespconn = (struct espconn *)os_zalloc(sizeof(struct espconn));
if (paccept->pespconn == NULL)
return ERR_MEM;
......@@ -1253,7 +1348,7 @@ espconn_tcp_accept(void *arg, struct tcp_pcb *pcb, err_t err)
* or periodically from active connection*/
tcp_sent(pcb, espconn_server_sent);
tcp_recv(pcb, espconn_server_recv);
tcp_poll(pcb, espconn_server_poll, 8); /* every 1 seconds */
tcp_poll(pcb, espconn_server_poll, 4); /* every 1 seconds */
/*Disable Nagle algorithm default*/
tcp_nagle_disable(pcb);
/*Default set the total number of espconn_buf on the unsent lists for one*/
......@@ -1267,6 +1362,8 @@ espconn_tcp_accept(void *arg, struct tcp_pcb *pcb, err_t err)
if (espconn_keepalive_disabled(paccept))
espconn_keepalive_enable(pcb);
// /*lock the window because of application layer don't need the data*/
// espconn_lock_recv(paccept);
return ERR_OK;
}
......
......@@ -54,8 +54,8 @@ static void ICACHE_FLASH_ATTR espconn_data_sent(void *arg, enum send_opt opt)
if (psent->pcommon.cntr == 0) {
psent->pespconn->state = ESPCONN_CONNECT;
// sys_timeout(10, espconn_data_sentcb, psent->pespconn);
espconn_data_sentcb(psent->pespconn);
if (psent->pcommon.err == 0)
espconn_data_sentcb(psent->pespconn);
} else {
if (opt == ESPCONN_SEND){
espconn_udp_sent(arg, psent->pcommon.ptrbuf, psent->pcommon.cntr);
......@@ -94,8 +94,8 @@ espconn_udp_sent(void *arg, uint8 *psent, uint16 length)
return ESPCONN_ARG;
}
if (1470 < length) {
datalen = 1470;
if ((IP_FRAG_MAX_MTU - 20 - 8) < length) {
datalen = IP_FRAG_MAX_MTU - 20 - 8;
} else {
datalen = length;
}
......@@ -157,6 +157,7 @@ espconn_udp_sent(void *arg, uint8 *psent, uint16 length)
pbuf_free(p);
pudp_sent->pcommon.ptrbuf = psent + datalen;
pudp_sent->pcommon.cntr = length - datalen;
pudp_sent->pcommon.err = err;
espconn_data_sent(pudp_sent, ESPCONN_SEND);
if (err > 0)
return ESPCONN_IF;
......@@ -199,8 +200,8 @@ espconn_udp_sendto(void *arg, uint8 *psent, uint16 length)
return ESPCONN_ARG;
}
if (1470 < length) {
datalen = 1470;
if ((IP_FRAG_MAX_MTU - 20 - 8) < length) {
datalen = IP_FRAG_MAX_MTU - 20 - 8;
} else {
datalen = length;
}
......@@ -257,8 +258,8 @@ espconn_udp_sendto(void *arg, uint8 *psent, uint16 length)
pbuf_free(p);
pudp_sent->pcommon.ptrbuf = psent + datalen;
pudp_sent->pcommon.cntr = length - datalen;
if (err == ERR_OK)
espconn_data_sent(pudp_sent, ESPCONN_SENDTO);
pudp_sent->pcommon.err = err;
espconn_data_sent(pudp_sent, ESPCONN_SENDTO);
if (err > 0)
return ESPCONN_IF;
......
......@@ -1096,12 +1096,12 @@ dhcp_renew(struct netif *netif)
}
#endif /* LWIP_NETIF_HOSTNAME */
#if 0
#if 1
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
#endif
#if 0
#if 1
dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
#endif
......@@ -1159,7 +1159,7 @@ dhcp_rebind(struct netif *netif)
}
#endif /* LWIP_NETIF_HOSTNAME */
#if 0
#if 1
dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
......
......@@ -578,7 +578,7 @@ dns_send(u8_t numdns, const char* name, u8_t id)
char *query, *nptr;
const char *pHostname;
u8_t n;
dns_random = (u16_t)os_random();
dns_random = os_random()>>16;
LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n",
(u16_t)(numdns), name));
LWIP_ASSERT("dns server out of array", numdns < DNS_MAX_SERVERS);
......@@ -706,7 +706,7 @@ dns_check_entry(u8_t i)
case DNS_STATE_DONE: {
/* if the time to live is nul */
if (--pEntry->ttl == 0) {
if ((pEntry->ttl == 0) || (--pEntry->ttl == 0)) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name));
/* flush this entry */
pEntry->state = DNS_STATE_UNUSED;
......@@ -782,8 +782,6 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
if (i < DNS_TABLE_SIZE) {
pEntry = &dns_table[i];
if(pEntry->state == DNS_STATE_ASKING) {
/* This entry is now completed. */
pEntry->state = DNS_STATE_DONE;
pEntry->err = hdr->flags2 & DNS_FLAG2_ERR_MASK;
/* We only care about the question(s) and the answers. The authrr
......@@ -795,8 +793,11 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
if (((hdr->flags1 & DNS_FLAG1_RESPONSE) == 0) || (pEntry->err != 0) || (nquestions != 1)) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", pEntry->name));
/* call callback to indicate error, clean up memory and return */
goto responseerr;
//goto responseerr;
goto memerr;
}
/* This entry is now completed. */
pEntry->state = DNS_STATE_DONE;
#if DNS_DOES_NAME_CHECK
/* Check if the name in the "question" part match with the name in the entry. */
......@@ -832,6 +833,13 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
if (pEntry->found) {
(*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg);
}
if (pEntry->ttl == 0) {
/* RFC 883, page 29: "Zero values are
interpreted to mean that the RR can only be used for the
transaction in progress, and should not be cached."
-> flush this entry now */
goto flushentry;
}
/* deallocate memory and return */
goto memerr;
} else {
......@@ -854,6 +862,7 @@ responseerr:
if (pEntry->found) {
(*pEntry->found)(pEntry->name, NULL, pEntry->arg);
}
flushentry:
/* flush this entry */
pEntry->state = DNS_STATE_UNUSED;
pEntry->found = NULL;
......
......@@ -99,6 +99,13 @@ Steve Reynolds
static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
#endif
//#define DYC_IGMP_DEBUG
#ifdef DYC_IGMP_DEBUG
#define IGMP_LOG os_printf
#else
#define IGMP_LOG //os_printf
#endif
/*
* IGMP constants
*/
......@@ -149,7 +156,7 @@ static err_t igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest,
static void igmp_send(struct igmp_group *group, u8_t type)ICACHE_FLASH_ATTR;
static struct igmp_group* igmp_group_list;
static struct igmp_group* igmp_group_list = NULL;
static ip_addr_t allsystems;
static ip_addr_t allrouters;
......@@ -166,22 +173,29 @@ igmp_init(void)
IP4_ADDR(&allrouters, 224, 0, 0, 2);
}
#ifdef LWIP_DEBUG
//#ifdef LWIP_DEBUG
#ifdef DYC_IGMP_DEBUG
/**
* Dump global IGMP groups list
*/
void
igmp_dump_group_list()
{
struct igmp_group *group = igmp_group_list;
while (group != NULL) {
LWIP_DEBUGF(IGMP_DEBUG, ("igmp_dump_group_list: [%"U32_F"] ", (u32_t)(group->group_state)));
ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
group = group->next;
}
LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
struct igmp_group *group = igmp_group_list;
IGMP_LOG("igmp_dump:\n");
while (group != NULL) {
LWIP_DEBUGF(IGMP_DEBUG, ("igmp_dump_group_list: [%"U32_F"] ", (u32_t)(group->group_state)));
ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
if(group!=NULL)
IGMP_LOG("group:%p,netif:%p\n",group,group->netif);
group = group->next;
}
LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
IGMP_LOG("\n");
}
#else
#define igmp_dump_group_list()
......@@ -236,6 +250,9 @@ igmp_stop(struct netif *netif)
next = group->next;
/* is it a group joined on this interface? */
if (group->netif == netif) {
IGMP_LOG("stop igmp:%p,%p,",group,group->netif);
/* is it the first group of the list? */
if (group == igmp_group_list) {
igmp_group_list = next;
......@@ -253,6 +270,9 @@ igmp_stop(struct netif *netif)
}
/* free group */
memp_free(MEMP_IGMP_GROUP, group);
igmp_dump_group_list();
} else {
/* change the "previous" */
prev = group;
......@@ -339,14 +359,14 @@ igmp_lookup_group(struct netif *ifp, ip_addr_t *addr)
group->last_reporter_flag = 0;
group->use = 0;
group->next = igmp_group_list;
igmp_group_list = group;
}
IGMP_LOG("add igmp:%p,%p,",group,group->netif);
LWIP_DEBUGF(IGMP_DEBUG, ("igmp_lookup_group: %sallocated a new group with address ", (group?"":"impossible to ")));
ip_addr_debug_print(IGMP_DEBUG, addr);
LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", ifp));
igmp_dump_group_list();
return group;
}
......@@ -360,7 +380,7 @@ static err_t
igmp_remove_group(struct igmp_group *group)
{
err_t err = ERR_OK;
IGMP_LOG("rmv igmp:%p,%p,",group,group->netif);
/* Is it the first group? */
if (igmp_group_list == group) {
igmp_group_list = group->next;
......@@ -380,6 +400,7 @@ igmp_remove_group(struct igmp_group *group)
/* free group */
memp_free(MEMP_IGMP_GROUP, group);
igmp_dump_group_list();
return err;
}
......@@ -707,6 +728,9 @@ igmp_start_timer(struct igmp_group *group, u8_t max_time)
max_time = 1;
}
/* ensure the random value is > 0 */
if(max_time == 1)
group->timer = 1;
else
group->timer = (LWIP_RAND() % (max_time - 1)) + 1;
}
......
......@@ -900,12 +900,28 @@ mdns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
void ICACHE_FLASH_ATTR
mdns_close(void)
{
if (mdns_pcb != NULL && ms_info != NULL)
uint8 text_index = 0;
if (mdns_pcb != NULL && ms_info != NULL) {
udp_remove(mdns_pcb);
for(text_index = 0;text_index < 10;text_index++) {
if(ms_info->txt_data[text_index] != NULL) {
os_free(ms_info->txt_data[text_index]);
ms_info->txt_data[text_index] = NULL;
}
}
if (ms_info->host_name != NULL) {
os_free(ms_info->host_name);
ms_info->host_name = NULL;
}
if (ms_info->server_name != NULL) {
os_free(ms_info->server_name);
ms_info->server_name = NULL;
}
os_free(ms_info);
mdns_pcb = NULL;
ms_info = NULL;
}
}
void ICACHE_FLASH_ATTR
......@@ -1034,9 +1050,23 @@ mdns_init(struct mdns_info *info) {
multicast_addr.addr = DNS_MULTICAST_ADDRESS;
struct ip_addr ap_host_addr;
struct ip_info ipconfig;
uint8 text_index = 0;
ms_info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info));
if (ms_info != NULL) {
os_memcpy(ms_info,info,sizeof(struct mdns_info));
ms_info->host_name = (char *)os_zalloc(os_strlen(info->host_name)+1);
os_memcpy(ms_info->host_name,info->host_name,os_strlen(info->host_name));
ms_info->server_name = (char *)os_zalloc(os_strlen(info->server_name)+1);
os_memcpy(ms_info->server_name,info->server_name,os_strlen(info->server_name));
for(text_index = 0;text_index < 10;text_index++) {
if(info->txt_data[text_index] != NULL) {
ms_info->txt_data[text_index] = (char *)os_zalloc(os_strlen(info->txt_data[text_index])+1);
os_memcpy(ms_info->txt_data[text_index],info->txt_data[text_index],os_strlen(info->txt_data[text_index]));
} else {
break;
}
}
} else {
os_printf("ms_info alloc failed\n");
return;
......
......@@ -486,6 +486,10 @@ void netif_set_up(struct netif *netif)
*/
void netif_set_down(struct netif *netif)
{
if (netif == NULL) {
return;
}
if (netif->flags & NETIF_FLAG_UP) {
netif->flags &= ~NETIF_FLAG_UP;
#if LWIP_SNMP
......
......@@ -51,7 +51,7 @@
#include "lwip/dns.h"
#include "lwip/ip_addr.h"
#include "lwip/pbuf.h"
#include "lwip/app/time.h"
//#include <string.h>
#if LWIP_UDP
......@@ -148,14 +148,18 @@
#endif
/** SNTP macro to change system time including microseconds */
#ifdef SNTP_SET_SYSTEM_TIME_US
#define SNTP_CALC_TIME_US 1
#define SNTP_RECEIVE_TIME_SIZE 2
#else
#define SNTP_SET_SYSTEM_TIME_US(sec, us)
#define SNTP_CALC_TIME_US 0
#define SNTP_RECEIVE_TIME_SIZE 1
#endif
uint8 sntp_receive_time_size = 1;
#define SNTP_RECEIVE_TIME_SIZE sntp_receive_time_size
#define SNTP_SET_SYSTEM_TIME_US(sec, us) sntp_update_rtc(sec, us)
//#ifdef SNTP_SET_SYSTEM_TIME_US
//#define SNTP_SET_SYSTEM_TIME_US(sec, us) sntp_update_rtc(sec, us)
//#define SNTP_CALC_TIME_US 1
//#define SNTP_RECEIVE_TIME_SIZE 2
//#else
//#define SNTP_SET_SYSTEM_TIME_US(sec, us)
//#define SNTP_CALC_TIME_US 0
//#define SNTP_RECEIVE_TIME_SIZE sntp_receive_time_size
//#endif
/** SNTP macro to get system time, used with SNTP_CHECK_RESPONSE >= 2
* to send in request and compare in response.
......@@ -295,10 +299,12 @@ static ip_addr_t sntp_last_server_address;
* to compare against in response */
static u32_t sntp_last_timestamp_sent[2];
#endif /* SNTP_CHECK_RESPONSE >= 2 */
typedef long time_t;
//uint32 current_stamp_1 = 0;
//uint32 current_stamp_2 = 0;
uint32 realtime_stamp = 0;
static bool sntp_time_flag = false;
static uint32 sntp_update_delay = SNTP_UPDATE_DELAY;
static uint32 realtime_stamp = 0;
LOCAL os_timer_t sntp_timer;
/*****************************************/
#define SECSPERMIN 60L
......@@ -643,13 +649,24 @@ bool ICACHE_FLASH_ATTR
sntp_set_timezone(sint8 timezone)
{
if(timezone >= -11 || timezone <= 13) {
time_zone = timezone;
if (sntp_get_timetype()){
RTC_TZ_SET(time_zone);
} else
time_zone = timezone;
return true;
} else {
return false;
}
}
void ICACHE_FLASH_ATTR sntp_set_daylight(int daylight)
{
if (sntp_get_timetype()){
RTC_DST_SET(daylight);
}
}
void ICACHE_FLASH_ATTR
sntp_time_inc(void)
{
......@@ -665,7 +682,22 @@ sntp_process(u32_t *receive_timestamp)
* @todo: if MSB is 1, SNTP time is 2036-based!
*/
time_t t = (ntohl(receive_timestamp[0]) - DIFF_SEC_1900_1970);
if (sntp_get_timetype()){
u32_t us = ntohl(receive_timestamp[1]) / 4295;
SNTP_SET_SYSTEM_TIME_US(t, us);
/* display local time from GMT time */
LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s, %"U32_F" us", ctime(&t), us));
} else{
/* change system time and/or the update the RTC clock */
SNTP_SET_SYSTEM_TIME(t);
/* display local time from GMT time */
t += time_zone * 60 * 60;// format GMT + time_zone TIME ZONE
realtime_stamp = t;
os_timer_disarm(&sntp_timer);
os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL);
os_timer_arm(&sntp_timer, 1000, 1);
}
#if 0
#if SNTP_CALC_TIME_US
u32_t us = ntohl(receive_timestamp[1]) / 4295;
SNTP_SET_SYSTEM_TIME_US(t, us);
......@@ -682,10 +714,8 @@ sntp_process(u32_t *receive_timestamp)
os_timer_disarm(&sntp_timer);
os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL);
os_timer_arm(&sntp_timer, 1000, 1);
os_printf("%s\n",sntp_asctime(sntp_localtime (&t)));
// os_printf("%s\n",ctime(&t));
// LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s", ctime(&t)));
#endif /* SNTP_CALC_TIME_US */
#endif
}
/**
......@@ -856,9 +886,9 @@ sntp_recv(void *arg, struct udp_pcb* pcb, struct pbuf *p, ip_addr_t *addr, u16_t
sntp_process(receive_timestamp);
/* Set up timeout for next request */
sys_timeout((u32_t)SNTP_UPDATE_DELAY, sntp_request, NULL);
sys_timeout((u32_t)sntp_update_delay, sntp_request, NULL);
LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Scheduled next time request: %"U32_F" ms\n",
(u32_t)SNTP_UPDATE_DELAY));
(u32_t)sntp_update_delay));
} else if (err == SNTP_ERR_KOD) {
/* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */
sntp_try_next_server(NULL);
......@@ -1125,4 +1155,31 @@ sntp_getservername(u8_t idx)
}
#endif /* SNTP_SERVER_DNS */
void ICACHE_FLASH_ATTR
sntp_set_update_delay(uint32 ms)
{
sntp_update_delay = ms > 15000?ms:15000;
}
void ICACHE_FLASH_ATTR
sntp_set_timetype(bool type)
{
sntp_time_flag = type;
}
bool sntp_get_timetype(void)
{
return sntp_time_flag;
}
void ICACHE_FLASH_ATTR
sntp_set_receive_time_size(void)
{
if (sntp_get_timetype()){
sntp_receive_time_size = 2;
} else{
sntp_receive_time_size = 1;
}
}
#endif /* LWIP_UDP */
......@@ -511,8 +511,11 @@ tcp_listen_input(struct tcp_pcb_listen *pcb)
return ERR_ABRT;
}
#endif /* TCP_LISTEN_BACKLOG */
for(pactive_pcb = tcp_active_pcbs; pactive_pcb != NULL; pactive_pcb = pactive_pcb->next)
active_pcb_num ++;
for(pactive_pcb = tcp_active_pcbs; pactive_pcb != NULL; pactive_pcb = pactive_pcb->next){
if (pactive_pcb->state == ESTABLISHED){
active_pcb_num ++;
}
}
if (active_pcb_num == MEMP_NUM_TCP_PCB){
LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: exceed the number of active TCP connections\n"));
TCP_STATS_INC(tcp.memerr);
......@@ -717,8 +720,9 @@ tcp_process(struct tcp_pcb *pcb)
pcb->rtime = -1;
else {
pcb->rtime = 0;
pcb->nrtx = 0;
// pcb->nrtx = 0;
}
pcb->nrtx = 0;
tcp_seg_free(rseg);
......
......@@ -231,7 +231,7 @@ tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
LWIP_UNUSED_ARG(apiflags);
LWIP_UNUSED_ARG(first_seg);
/* always create MSS-sized pbufs */
alloc = TCP_MSS;
alloc = pcb->mss; //TCP_MSS;
#else /* LWIP_NETIF_TX_SINGLE_PBUF */
if (length < max_length) {
/* Should we allocate an oversized pbuf, or just the minimum
......@@ -420,8 +420,8 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
/* Find the tail of the unsent queue. */
if (pcb->unsent != NULL) {
u16_t space;
u16_t unsent_optlen;
u16_t space = 0;
u16_t unsent_optlen = 0;
/* @todo: this could be sped up by keeping last_unsent in the pcb */
for (last_unsent = pcb->unsent; last_unsent->next != NULL;
......@@ -457,7 +457,11 @@ tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len));
#endif /* TCP_OVERSIZE */
/*
if (pos > len) {
return ERR_MEM;
}
/*
* Phase 2: Chain a new pbuf to the end of pcb->unsent.
*
* We don't extend segments containing SYN/FIN flags or options
......@@ -955,7 +959,8 @@ tcp_output(struct tcp_pcb *pcb)
/* data available and window allows it to be sent?
*��ǰ��Ч�������?�ķ��ͣ�ѭ�����ͱ��ģ�ֱ�������*/
while (seg != NULL &&
ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd
&& (seg->p->ref<2) ) {
LWIP_ASSERT("RST not expected here!",
(TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
/* Stop sending if the nagle algorithm would prevent it
......@@ -1290,6 +1295,7 @@ tcp_rexmit_rto(struct tcp_pcb *pcb)
/* unacked queue is now empty */
pcb->unacked = NULL;
#endif
/* last unsent hasn't changed, no need to reset unsent_oversize */
/* increment number of retransmissions */
++pcb->nrtx;
......
......@@ -152,13 +152,13 @@ udp_input(struct pbuf *p, struct netif *inp)
pcb = inp->dhcp->pcb;
}
}
} else if (dest == DHCP_SERVER_PORT) {
if (src == DHCP_CLIENT_PORT) {
if ( inp->dhcps_pcb != NULL ) {
if ((ip_addr_isany(&inp->dhcps_pcb->local_ip) ||
ip_addr_cmp(&(inp->dhcps_pcb->local_ip), &current_iphdr_dest))) {
pcb = inp->dhcps_pcb;
}
}
} else if (dest == DHCP_SERVER_PORT) {
if (src == DHCP_CLIENT_PORT) {
if ( inp->dhcps_pcb != NULL ) {
if ((ip_addr_isany(&inp->dhcps_pcb->local_ip) ||
ip_addr_cmp(&(inp->dhcps_pcb->local_ip), &current_iphdr_dest))) {
pcb = inp->dhcps_pcb;
}
}
}
......
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
ifndef PDIR
UP_EXTRACT_DIR = ..
GEN_LIBS = libmbedtls.a
COMPONENTS_libmbedtls = library/liblibrary.a platform/libplatform.a app/libapp.a
endif
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
DEFINES +=
#CCFLAGS += --rename-section .text=.irom0.text --rename-section .literal=.irom0.literal
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment