9
0
Fork 0

accept() now supports non-blocking operations

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@2011 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2009-08-02 23:35:27 +00:00
parent 41a264fb84
commit 13af0f2c03
7 changed files with 147 additions and 106 deletions

View File

@ -828,9 +828,10 @@
* configs/ntosd-dm320/thttpd: A build configuration for THTTPD on the Neuros * configs/ntosd-dm320/thttpd: A build configuration for THTTPD on the Neuros
DM320 platform. DM320 platform.
* lib/: Added strstr() and strpbrk(). * lib/: Added strstr() and strpbrk().
* net/recvfrom.c: Sockets now support some non-blocking operations -- * net/recvfrom.c and net/accept(): Sockets now support some non-blocking
specifically only for TCP/IP read operations when read-ahead buffering operations, specifically for (1) TCP/IP read operations when read-ahead
is enabled. buffering is enabled, and (2) TCP/IP accept() operations when TCP/IP
connection backlog is enabled.
* fs/fs_fcntl.c and net/net_vfcntl.c: Minimal support provided for fcntl(). * fs/fs_fcntl.c and net/net_vfcntl.c: Minimal support provided for fcntl().
It can, at least, be used to mark sockets as blocking or non-blocking. It can, at least, be used to mark sockets as blocking or non-blocking.
* net/net_close.c: Fix bug in close(). If reference count not set to zero * net/net_close.c: Fix bug in close(). If reference count not set to zero

View File

@ -1501,9 +1501,10 @@ nuttx-0.4.10 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
* configs/ntosd-dm320/thttpd: A build configuration for THTTPD on the Neuros * configs/ntosd-dm320/thttpd: A build configuration for THTTPD on the Neuros
DM320 platform. DM320 platform.
* lib/: Added strstr() and strpbrk(). * lib/: Added strstr() and strpbrk().
* net/recvfrom.c: Sockets now support some non-blocking operations -- * net/recvfrom.c and net/accept(): Sockets now support some non-blocking
specifically only for TCP/IP read operations when read-ahead buffering operations, specifically for (1) TCP/IP read operations when read-ahead
is enabled. buffering is enabled, and (2) TCP/IP accept() operations when TCP/IP
connection backlog is enabled.
* fs/fs_fcntl.c and net/net_vfcntl.c: Minimal support provided for fcntl(). * fs/fs_fcntl.c and net/net_vfcntl.c: Minimal support provided for fcntl().
It can, at least, be used to mark sockets as blocking or non-blocking. It can, at least, be used to mark sockets as blocking or non-blocking.
* net/net_close.c: Fix bug in close(). If reference count not set to zero * net/net_close.c: Fix bug in close(). If reference count not set to zero

View File

@ -356,6 +356,18 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
nvdbg("Pending conn=%p\n", state.acpt_newconn); nvdbg("Pending conn=%p\n", state.acpt_newconn);
accept_tcpsender(state.acpt_newconn, inaddr); accept_tcpsender(state.acpt_newconn, inaddr);
} }
/* In general, this uIP-based implementation will not support non-blocking
* socket operations... except in a few cases: Here for TCP accept with backlog
* enabled. If this socket is configured as non-blocking then return EAGAIN
* if there is no pending connection in the backlog.
*/
else if (_SS_ISNONBLOCK(psock->s_flags))
{
err = EAGAIN;
goto errout_with_irq;
}
else else
#endif #endif
{ {

View File

@ -882,8 +882,8 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
recvfrom_readahead(&state); recvfrom_readahead(&state);
/* In general, this uI-based implementation will not support non-blocking /* In general, this uIP-based implementation will not support non-blocking
* socket operations... except in this one case: TCP receive with read-ahead * socket operations... except in a few cases: Here for TCP receive with read-ahead
* enabled. If this socket is configured as non-blocking then return EAGAIN * enabled. If this socket is configured as non-blocking then return EAGAIN
* if no data was obtained from the read-ahead buffers. * if no data was obtained from the read-ahead buffers.
*/ */

View File

@ -131,10 +131,20 @@
# define CONFIG_THTTPD_CHARSET "iso-8859-1" # define CONFIG_THTTPD_CHARSET "iso-8859-1"
# endif # endif
/* Initial buffer size, buffer reallocation increment, maximum buffer size */
# ifndef CONFIG_THTTPD_IOBUFFERSIZE # ifndef CONFIG_THTTPD_IOBUFFERSIZE
# define CONFIG_THTTPD_IOBUFFERSIZE 256 # define CONFIG_THTTPD_IOBUFFERSIZE 256
# endif # endif
# ifndef CONFIG_THTTPD_REALLOCINCR
# define CONFIG_THTTPD_REALLOCINCR 128
# endif
# ifndef CONFIG_THTTPD_MAXREALLOC
# define CONFIG_THTTPD_MAXREALLOC 4096
# endif
# if CONFIG_THTTPD_IOBUFFERSIZE > 65535 # if CONFIG_THTTPD_IOBUFFERSIZE > 65535
# error "Can't use uint16 for buffer" # error "Can't use uint16 for buffer"
# endif # endif

View File

@ -3469,7 +3469,7 @@ void httpd_realloc_str(char **strP, size_t * maxsizeP, size_t size)
{ {
if (*maxsizeP == 0) if (*maxsizeP == 0)
{ {
*maxsizeP = MAX(200, size + 100); *maxsizeP = MAX(CONFIG_THTTPD_IOBUFFERSIZE, size + CONFIG_THTTPD_REALLOCINCR);
*strP = NEW(char, *maxsizeP + 1); *strP = NEW(char, *maxsizeP + 1);
++str_alloc_count; ++str_alloc_count;
str_alloc_size += *maxsizeP; str_alloc_size += *maxsizeP;
@ -3559,7 +3559,7 @@ int httpd_get_conn(httpd_server *hs, int listen_fd, httpd_conn *hc)
if (!hc->initialized) if (!hc->initialized)
{ {
hc->read_size = 0; hc->read_size = 0;
httpd_realloc_str(&hc->read_buf, &hc->read_size, 500); httpd_realloc_str(&hc->read_buf, &hc->read_size, CONFIG_THTTPD_IOBUFFERSIZE);
hc->maxdecodedurl = hc->maxdecodedurl =
hc->maxorigfilename = hc->maxexpnfilename = hc->maxencodings = hc->maxorigfilename = hc->maxexpnfilename = hc->maxencodings =
hc->maxpathinfo = hc->maxquery = hc->maxaccept = hc->maxpathinfo = hc->maxquery = hc->maxaccept =
@ -3567,7 +3567,7 @@ int httpd_get_conn(httpd_server *hs, int listen_fd, httpd_conn *hc)
hc->maxremoteuser = 0; hc->maxremoteuser = 0;
#ifdef CONFIG_THTTPD_TILDE_MAP2 #ifdef CONFIG_THTTPD_TILDE_MAP2
hc->maxaltdir = 0; hc->maxaltdir = 0;
#endif /*CONFIG_THTTPD_TILDE_MAP2 */ #endif
httpd_realloc_str(&hc->decodedurl, &hc->maxdecodedurl, 1); httpd_realloc_str(&hc->decodedurl, &hc->maxdecodedurl, 1);
httpd_realloc_str(&hc->origfilename, &hc->maxorigfilename, 1); httpd_realloc_str(&hc->origfilename, &hc->maxorigfilename, 1);
httpd_realloc_str(&hc->expnfilename, &hc->maxexpnfilename, 0); httpd_realloc_str(&hc->expnfilename, &hc->maxexpnfilename, 0);
@ -3587,6 +3587,7 @@ int httpd_get_conn(httpd_server *hs, int listen_fd, httpd_conn *hc)
/* Accept the new connection. */ /* Accept the new connection. */
nvdbg("accept() new connection on listen_fd %d\n", listen_fd);
sz = sizeof(sa); sz = sizeof(sa);
hc->conn_fd = accept(listen_fd, (struct sockaddr*)&sa, &sz); hc->conn_fd = accept(listen_fd, (struct sockaddr*)&sa, &sz);
if (hc->conn_fd < 0) if (hc->conn_fd < 0)
@ -3613,54 +3614,56 @@ int httpd_get_conn(httpd_server *hs, int listen_fd, httpd_conn *hc)
hc->hs = hs; hc->hs = hs;
(void)memset(&hc->client_addr, 0, sizeof(hc->client_addr)); (void)memset(&hc->client_addr, 0, sizeof(hc->client_addr));
(void)memmove(&hc->client_addr, &sa, sockaddr_len(&sa)); (void)memmove(&hc->client_addr, &sa, sockaddr_len(&sa));
hc->read_idx = 0; hc->read_idx = 0;
hc->checked_idx = 0; hc->checked_idx = 0;
hc->checked_state = CHST_FIRSTWORD; hc->checked_state = CHST_FIRSTWORD;
hc->method = METHOD_UNKNOWN; hc->method = METHOD_UNKNOWN;
hc->status = 0; hc->status = 0;
hc->bytes_to_send = 0; hc->bytes_to_send = 0;
hc->bytes_sent = 0; hc->bytes_sent = 0;
hc->encodedurl = ""; hc->encodedurl = "";
hc->decodedurl[0] = '\0'; hc->decodedurl[0] = '\0';
hc->protocol = "UNKNOWN"; hc->protocol = "UNKNOWN";
hc->origfilename[0] = '\0'; hc->origfilename[0] = '\0';
hc->expnfilename[0] = '\0'; hc->expnfilename[0] = '\0';
hc->encodings[0] = '\0'; hc->encodings[0] = '\0';
hc->pathinfo[0] = '\0'; hc->pathinfo[0] = '\0';
hc->query[0] = '\0'; hc->query[0] = '\0';
hc->referer = ""; hc->referer = "";
hc->useragent = ""; hc->useragent = "";
hc->accept[0] = '\0'; hc->accept[0] = '\0';
hc->accepte[0] = '\0'; hc->accepte[0] = '\0';
hc->acceptl = ""; hc->acceptl = "";
hc->cookie = ""; hc->cookie = "";
hc->contenttype = ""; hc->contenttype = "";
hc->reqhost[0] = '\0'; hc->reqhost[0] = '\0';
hc->hdrhost = ""; hc->hdrhost = "";
hc->hostdir[0] = '\0'; hc->hostdir[0] = '\0';
hc->authorization = ""; hc->authorization = "";
hc->remoteuser[0] = '\0'; hc->remoteuser[0] = '\0';
hc->buffer[0] = '\0'; hc->buffer[0] = '\0';
#ifdef CONFIG_THTTPD_TILDE_MAP2 #ifdef CONFIG_THTTPD_TILDE_MAP2
hc->altdir[0] = '\0'; hc->altdir[0] = '\0';
#endif #endif
hc->buflen = 0; hc->buflen = 0;
hc->if_modified_since = (time_t) - 1; hc->if_modified_since = (time_t) - 1;
hc->range_if = (time_t) - 1; hc->range_if = (time_t)-1;
hc->contentlength = -1; hc->contentlength = -1;
hc->type = ""; hc->type = "";
#ifdef CONFIG_THTTPD_VHOST #ifdef CONFIG_THTTPD_VHOST
hc->vhostname = NULL; hc->vhostname = NULL;
#endif #endif
hc->mime_flag = TRUE; hc->mime_flag = TRUE;
hc->one_one = FALSE; hc->one_one = FALSE;
hc->got_range = FALSE; hc->got_range = FALSE;
hc->tildemapped = FALSE; hc->tildemapped = FALSE;
hc->range_start = 0; hc->range_start = 0;
hc->range_end = -1; hc->range_end = -1;
hc->keep_alive = FALSE; hc->keep_alive = FALSE;
hc->should_linger = FALSE; hc->should_linger = FALSE;
hc->file_fd = -1; hc->file_fd = -1;
nvdbg("New connection accepted on %d\n", hc->conn_fd);
return GC_OK; return GC_OK;
} }
@ -4067,14 +4070,13 @@ int httpd_parse_request(httpd_conn *hc)
cp += strspn(cp, " \t"); cp += strspn(cp, " \t");
if (hc->accept[0] != '\0') if (hc->accept[0] != '\0')
{ {
if (strlen(hc->accept) > 5000) if (strlen(hc->accept) > CONFIG_THTTPD_MAXREALLOC)
{ {
ndbg("%s way too much Accept: data\n", ndbg("%s way too much Accept: data\n",
httpd_ntoa(&hc->client_addr)); httpd_ntoa(&hc->client_addr));
continue; continue;
} }
httpd_realloc_str(&hc->accept, &hc->maxaccept, httpd_realloc_str(&hc->accept, &hc->maxaccept, strlen(hc->accept) + 2 + strlen(cp));
strlen(hc->accept) + 2 + strlen(cp));
(void)strcat(hc->accept, ", "); (void)strcat(hc->accept, ", ");
} }
else else
@ -4089,14 +4091,13 @@ int httpd_parse_request(httpd_conn *hc)
cp += strspn(cp, " \t"); cp += strspn(cp, " \t");
if (hc->accepte[0] != '\0') if (hc->accepte[0] != '\0')
{ {
if (strlen(hc->accepte) > 5000) if (strlen(hc->accepte) > CONFIG_THTTPD_MAXREALLOC)
{ {
ndbg("%s way too much Accept-Encoding: data\n", ndbg("%s way too much Accept-Encoding: data\n",
httpd_ntoa(&hc->client_addr)); httpd_ntoa(&hc->client_addr));
continue; continue;
} }
httpd_realloc_str(&hc->accepte, &hc->maxaccepte, httpd_realloc_str(&hc->accepte, &hc->maxaccepte, strlen(hc->accepte) + 2 + strlen(cp));
strlen(hc->accepte) + 2 + strlen(cp));
(void)strcat(hc->accepte, ", "); (void)strcat(hc->accepte, ", ");
} }
else else

View File

@ -195,37 +195,42 @@ static int handle_newconnect(struct timeval *tv, int listen_fd)
struct connect_s *conn; struct connect_s *conn;
ClientData client_data; ClientData client_data;
/* This loops until the accept() fails, trying to start new connections as /* This loops until the accept() fails, trying to start new connections as
* fast as possible so we don't overrun the listen queue. * fast as possible so we don't overrun the listen queue.
*/ */
nvdbg("New connection(s) on listen_fd %d\n", listen_fd);
for (;;) for (;;)
{ {
/* Is there room in the connection table? */ /* Is there room in the connection table? */
if (num_connects >= AVAILABLE_FDS) if (num_connects >= AVAILABLE_FDS)
{ {
/* Out of connection slots. Run the timers, then the existing /* Out of connection slots. Run the timers, then the existing
* connections, and maybe we'll free up a slot by the time we get * connections, and maybe we'll free up a slot by the time we get
* back here. */ * back here.
*/
ndbg("too many connections!\n"); ndbg("too many connections!\n");
tmr_run(tv); tmr_run(tv);
return 0; return -1;
} }
/* Get the first free connection entry off the free list */ /* Get the first free connection entry off the free list */
#ifdef CONFIG_DEBUG
if (first_free_connect == -1 || if (first_free_connect == -1 ||
connects[first_free_connect].conn_state != CNST_FREE) connects[first_free_connect].conn_state != CNST_FREE)
{ {
ndbg("the connects free list is messed up\n"); ndbg("the connects free list is messed up\n");
exit(1); exit(1);
} }
#endif
conn = &connects[first_free_connect]; conn = &connects[first_free_connect];
/* Make the httpd_conn if necessary */ /* Make the httpd_conn if necessary */
if (conn->hc == (httpd_conn *) 0) if (!conn->hc)
{ {
conn->hc = NEW(httpd_conn, 1); conn->hc = NEW(httpd_conn, 1);
if (conn->hc == (httpd_conn *) 0) if (conn->hc == (httpd_conn *) 0)
@ -233,8 +238,9 @@ static int handle_newconnect(struct timeval *tv, int listen_fd)
ndbg("out of memory allocating an httpd_conn\n"); ndbg("out of memory allocating an httpd_conn\n");
exit(1); exit(1);
} }
conn->hc->initialized = 0; conn->hc->initialized = 0;
++httpd_conn_count; httpd_conn_count++;
} }
/* Get the connection */ /* Get the connection */
@ -247,37 +253,43 @@ static int handle_newconnect(struct timeval *tv, int listen_fd)
case GC_FAIL: case GC_FAIL:
tmr_run(tv); tmr_run(tv);
return 0; return -1;
/* No more connections to accept for now */ /* No more connections to accept for now */
case GC_NO_MORE: case GC_NO_MORE:
return 1; return 0;
default:
break;
} }
nvdbg("New connection fd %d\n", conn->hc->conn_fd);
conn->conn_state = CNST_READING; conn->conn_state = CNST_READING;
/* Pop it off the free list */ /* Pop it off the free list */
first_free_connect = conn->next_free_connect; first_free_connect = conn->next_free_connect;
conn->next_free_connect = -1; conn->next_free_connect = -1;
++num_connects; num_connects++;
client_data.p = conn; client_data.p = conn;
conn->active_at = tv->tv_sec; conn->active_at = tv->tv_sec;
conn->wakeup_timer = (Timer *) 0; conn->wakeup_timer = NULL;
conn->linger_timer = (Timer *) 0; conn->linger_timer = NULL;
conn->offset = 0; conn->offset = 0;
/* Set the connection file descriptor to no-delay mode */ /* Set the connection file descriptor to no-delay mode */
httpd_set_ndelay(conn->hc->conn_fd); httpd_set_ndelay(conn->hc->conn_fd);
fdwatch_add_fd(fw, conn->hc->conn_fd, conn, FDW_READ); fdwatch_add_fd(fw, conn->hc->conn_fd, conn, FDW_READ);
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET) #if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
++stats_connections; ++stats_connections;
if (num_connects > stats_simultaneous) if (num_connects > stats_simultaneous)
stats_simultaneous = num_connects; {
stats_simultaneous = num_connects;
}
#endif #endif
} }
} }
@ -293,11 +305,11 @@ static void handle_read(struct connect_s *conn, struct timeval *tv)
if (hc->read_idx >= hc->read_size) if (hc->read_idx >= hc->read_size)
{ {
if (hc->read_size > 5000) if (hc->read_size > CONFIG_THTTPD_MAXREALLOC)
{ {
goto errout_with_400; goto errout_with_400;
} }
httpd_realloc_str(&hc->read_buf, &hc->read_size, hc->read_size + 1000); httpd_realloc_str(&hc->read_buf, &hc->read_size, hc->read_size + CONFIG_THTTPD_REALLOCINCR);
} }
/* Read some more bytes */ /* Read some more bytes */
@ -880,7 +892,12 @@ int thttpd_main(int argc, char **argv)
if (num_ready < 0) if (num_ready < 0)
{ {
if (errno == EINTR || errno == EAGAIN) if (errno == EINTR || errno == EAGAIN)
continue; /* try again */ {
/* Not errors... try again */
continue;
}
ndbg("fdwatch: %d\n", errno); ndbg("fdwatch: %d\n", errno);
exit(1); exit(1);
} }
@ -897,10 +914,9 @@ int thttpd_main(int argc, char **argv)
/* Is it a new connection? */ /* Is it a new connection? */
if (hs != (httpd_server *) 0 && hs->listen_fd != -1 && if (fdwatch_check_fd(fw, hs->listen_fd))
fdwatch_check_fd(fw, hs->listen_fd))
{ {
if (handle_newconnect(&tv, hs->listen_fd)) if (!handle_newconnect(&tv, hs->listen_fd))
{ {
/* Go around the loop and do another fdwatch, rather than /* Go around the loop and do another fdwatch, rather than
* dropping through and processing existing connections. New * dropping through and processing existing connections. New
@ -913,33 +929,33 @@ int thttpd_main(int argc, char **argv)
/* Find the connections that need servicing */ /* Find the connections that need servicing */
while ((conn = while ((conn = (struct connect_s*)fdwatch_get_next_client_data(fw)) != (struct connect_s*)-1)
(struct connect_s*)fdwatch_get_next_client_data(fw)) !=
(struct connect_s*)- 1)
{ {
if (conn == (struct connect_s *) 0) if (conn)
continue;
hc = conn->hc;
if (!fdwatch_check_fd(fw, hc->conn_fd))
{ {
/* Something went wrong */ hc = conn->hc;
if (!fdwatch_check_fd(fw, hc->conn_fd))
clear_connection(conn, &tv);
}
else
{
switch (conn->conn_state)
{ {
case CNST_READING: /* Something went wrong */
handle_read(conn, &tv);
break; nvdbg("Clearing connection\n");
case CNST_SENDING: clear_connection(conn, &tv);
handle_send(conn, &tv); }
break; else
case CNST_LINGERING: {
handle_linger(conn, &tv); nvdbg("Handle conn_state %d\n", conn->conn_state);
break; switch (conn->conn_state)
{
case CNST_READING:
handle_read(conn, &tv);
break;
case CNST_SENDING:
handle_send(conn, &tv);
break;
case CNST_LINGERING:
handle_linger(conn, &tv);
break;
}
} }
} }
} }