patch #7702 "Include ability to increase the socket number with defined offset"

This commit is contained in:
sg 2015-02-11 23:05:52 +01:00
parent 276e35ecfb
commit 8155b8cfb3
4 changed files with 46 additions and 20 deletions

View File

@ -6,12 +6,16 @@ HISTORY
++ New features: ++ New features:
2015-02-11: Nick van Ijzendoorn
* opt.h, sockets.h/c: patch #7702 "Include ability to increase the socket number
with defined offset"
2015-02-11: Frederick Baksik 2015-02-11: Frederick Baksik
opt.h, def.h, others: patch #8423 "arch/perf.h" should be made an optional item * opt.h, def.h, others: patch #8423 "arch/perf.h" should be made an optional item
2015-02-11: Simon Goldschmidt 2015-02-11: Simon Goldschmidt
* api_msg.c, opt.h: started to implement fullduplex sockets/netconns * api_msg.c, opt.h: started to implement fullduplex sockets/netconns
(note that this is highly unstable yet!) (note that this is highly unstable yet!)
2015-01-02: Simon Goldschmidt 2015-01-02: Simon Goldschmidt
* tcp.c: tcp_kill_prio(): prefer nearly-closed connections (waiting for the * tcp.c: tcp_kill_prio(): prefer nearly-closed connections (waiting for the

View File

@ -307,8 +307,10 @@ get_socket(int s)
{ {
struct lwip_sock *sock; struct lwip_sock *sock;
s -= LWIP_SOCKET_OFFSET;
if ((s < 0) || (s >= NUM_SOCKETS)) { if ((s < 0) || (s >= NUM_SOCKETS)) {
LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s)); LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s + LWIP_SOCKET_OFFSET));
set_errno(EBADF); set_errno(EBADF);
return NULL; return NULL;
} }
@ -316,7 +318,7 @@ get_socket(int s)
sock = &sockets[s]; sock = &sockets[s];
if (!sock->conn) { if (!sock->conn) {
LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s)); LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s + LWIP_SOCKET_OFFSET));
set_errno(EBADF); set_errno(EBADF);
return NULL; return NULL;
} }
@ -333,6 +335,7 @@ get_socket(int s)
static struct lwip_sock * static struct lwip_sock *
tryget_socket(int s) tryget_socket(int s)
{ {
s -= LWIP_SOCKET_OFFSET;
if ((s < 0) || (s >= NUM_SOCKETS)) { if ((s < 0) || (s >= NUM_SOCKETS)) {
return NULL; return NULL;
} }
@ -374,7 +377,7 @@ alloc_socket(struct netconn *newconn, int accepted)
sockets[i].errevent = 0; sockets[i].errevent = 0;
sockets[i].err = 0; sockets[i].err = 0;
sockets[i].select_waiting = 0; sockets[i].select_waiting = 0;
return i; return i + LWIP_SOCKET_OFFSET;
} }
SYS_ARCH_UNPROTECT(lev); SYS_ARCH_UNPROTECT(lev);
} }
@ -485,9 +488,9 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
sock_set_errno(sock, ENFILE); sock_set_errno(sock, ENFILE);
return -1; return -1;
} }
LWIP_ASSERT("invalid socket index", (newsock >= 0) && (newsock < NUM_SOCKETS)); LWIP_ASSERT("invalid socket index", (newsock >= LWIP_SOCKET_OFFSET) && (newsock < NUM_SOCKETS + LWIP_SOCKET_OFFSET));
LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback); LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback);
nsock = &sockets[newsock]; nsock = &sockets[newsock - LWIP_SOCKET_OFFSET];
/* See event_callback: If data comes in right away after an accept, even /* See event_callback: If data comes in right away after an accept, even
* though the server task might not have created a new socket yet. * though the server task might not have created a new socket yet.
@ -1157,7 +1160,7 @@ lwip_selscan(int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *excep
/* Go through each socket in each list to count number of sockets which /* Go through each socket in each list to count number of sockets which
currently match */ currently match */
for(i = 0; i < maxfdp1; i++) { for(i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) {
void* lastdata = NULL; void* lastdata = NULL;
s16_t rcvevent = 0; s16_t rcvevent = 0;
u16_t sendevent = 0; u16_t sendevent = 0;
@ -1270,7 +1273,7 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
/* Increase select_waiting for each socket we are interested in */ /* Increase select_waiting for each socket we are interested in */
maxfdp2 = maxfdp1; maxfdp2 = maxfdp1;
for (i = 0; i < maxfdp1; i++) { for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) {
if ((readset && FD_ISSET(i, readset)) || if ((readset && FD_ISSET(i, readset)) ||
(writeset && FD_ISSET(i, writeset)) || (writeset && FD_ISSET(i, writeset)) ||
(exceptset && FD_ISSET(i, exceptset))) { (exceptset && FD_ISSET(i, exceptset))) {
@ -1313,7 +1316,7 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
} }
/* Decrease select_waiting for each socket we are interested in */ /* Decrease select_waiting for each socket we are interested in */
for (i = 0; i < maxfdp2; i++) { for (i = LWIP_SOCKET_OFFSET; i < maxfdp2; i++) {
if ((readset && FD_ISSET(i, readset)) || if ((readset && FD_ISSET(i, readset)) ||
(writeset && FD_ISSET(i, writeset)) || (writeset && FD_ISSET(i, writeset)) ||
(exceptset && FD_ISSET(i, exceptset))) { (exceptset && FD_ISSET(i, exceptset))) {

View File

@ -1545,6 +1545,17 @@
#define LWIP_POSIX_SOCKETS_IO_NAMES 1 #define LWIP_POSIX_SOCKETS_IO_NAMES 1
#endif #endif
/**
* LWIP_SOCKET_OFFSET==n: Increases the file descriptor number created by LwIP with n.
* This can be useful when there are multiple APIs which create file descriptors.
* When they all start with a different offset and you won't make them overlap you can
* re implement read/write/close/ioctl/fnctl to send the requested action to the right
* library (sharing select will need more work though).
*/
#ifndef LWIP_SOCKET_OFFSET
#define LWIP_SOCKET_OFFSET 0
#endif
/** /**
* LWIP_TCP_KEEPALIVE==1: Enable TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT * LWIP_TCP_KEEPALIVE==1: Enable TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT
* options processing. Note that TCP_KEEPIDLE and TCP_KEEPINTVL have to be set * options processing. Note that TCP_KEEPIDLE and TCP_KEEPINTVL have to be set

View File

@ -378,18 +378,26 @@ typedef struct ip_mreq {
/* FD_SET used for lwip_select */ /* FD_SET used for lwip_select */
#ifndef FD_SET #ifndef FD_SET
#undef FD_SETSIZE #undef FD_SETSIZE
/* Make FD_SETSIZE match NUM_SOCKETS in socket.c */ /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
#define FD_SETSIZE MEMP_NUM_NETCONN #define FD_SETSIZE MEMP_NUM_NETCONN
#define FD_SET(n, p) ((p)->fd_bits[(n)/8] |= (1 << ((n) & 7))) #define FDSETSAFESET(n, p, code) do { \
#define FD_CLR(n, p) ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7))) if(((p) != NULL) && ((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0)) { \
#define FD_ISSET(n,p) ((p)->fd_bits[(n)/8] & (1 << ((n) & 7))) code; }} while(0)
#define FD_ZERO(p) memset((void*)(p),0,sizeof(*(p))) #define FDSETSAFEGET(n, p, code) (((p) != NULL) && ((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0) ?\
(code) : 0)
#define FD_SET(n, p) FDSETSAFESET(n, p, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] |= (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define FD_CLR(n, p) FDSETSAFESET(n, p, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] &= ~(1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define FD_ISSET(n,p) FDSETSAFEGET(n, p, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] & (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))
typedef struct fd_set { typedef struct fd_set
unsigned char fd_bits [(FD_SETSIZE+7)/8]; {
} fd_set; unsigned char fd_bits [(FD_SETSIZE+7)/8];
} fd_set;
#elif LWIP_SOCKET_OFFSET
#error LWIP_SOCKET_OFFSET does not work with external FD_SET!
#endif /* FD_SET */ #endif /* FD_SET */
/** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided /** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided