Index: asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c
===================================================================
--- asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c	(revision 359)
+++ asp3_tinet_ecnl_arm/trunk/ntshell/src/fdtable.c	(revision 364)
@@ -45,5 +45,8 @@
 #include "target_syssvc.h"
 #ifndef NTSHELL_NO_SOCKET
+#include <tinet_defs.h>
 #include <tinet_config.h>
+#include <net/net.h>
+#include <net/net_endian.h>
 #include <netinet/in.h>
 #include <netinet/in_itron.h>
@@ -55,7 +58,9 @@
 #include <net/net.h>
 #include <net/if_var.h>
+#include <netinet/udp.h>
 #include <netinet/udp_var.h>
 #include <netinet/tcp.h>
 #include <netinet/tcp_var.h>
+#include <net/net_buf.h>
 #endif
 #include "ff.h"
@@ -69,5 +74,5 @@
 #define IO_TYPE_DIR		3
 #define IO_TYPE_TCP		4
-#define IO_TYPE_UDP		5
+#define IO_TYPE_UDP	5
 
 static struct _IO_FILE fd_table[8 * sizeof(FLGPTN)] = {
@@ -85,4 +90,5 @@
 			continue;
 
+		memset(fp, 0, sizeof(struct _IO_FILE));
 		fp->fd = fd;
 		fp->type = type;
@@ -280,4 +286,15 @@
 #endif
 
+void memor(void *dst, void *src, size_t len)
+{
+	uint8_t *d = (uint8_t *)dst;
+	uint8_t *s = (uint8_t *)src;
+	uint8_t *e = &s[len];
+
+	while (s < e) {
+		*d++ |= *s++;
+	}
+}
+
 struct fd_events {
 	int count;
@@ -304,20 +321,35 @@
 	}
 
-	memcpy(&evts.readfds, rfds, sizeof(fd_set));
-	memcpy(&evts.writefds, wfds, sizeof(fd_set));
-	memcpy(&evts.errorfds, efds, sizeof(fd_set));
+	if (rfds != NULL)
+		memcpy(&evts.readfds, rfds, sizeof(fd_set));
+	else
+		memset(&evts.readfds, 0, sizeof(fd_set));
+	if (wfds != NULL)
+		memcpy(&evts.writefds, wfds, sizeof(fd_set));
+	else
+		memset(&evts.writefds, 0, sizeof(fd_set));
+	if (efds != NULL)
+		memcpy(&evts.errorfds, efds, sizeof(fd_set));
+	else
+		memset(&evts.errorfds, 0, sizeof(fd_set));
 	evts.count = 0;
 
 	ret = shell_get_evts(&evts, tmout);
+	if (rfds != NULL)
+		memset(rfds, 0, sizeof(fd_set));
+	if (wfds != NULL)
+		memset(wfds, 0, sizeof(fd_set));
+	if (efds != NULL)
+		memset(efds, 0, sizeof(fd_set));
 	if (ret == E_OK) {
-		memcpy(rfds, &evts.readfds, sizeof(fd_set));
-		memcpy(wfds, &evts.writefds, sizeof(fd_set));
-		memcpy(efds, &evts.errorfds, sizeof(fd_set));
+		if (rfds != NULL)
+			memor(rfds, &evts.readfds, sizeof(fd_set));
+		if (wfds != NULL)
+			memor(wfds, &evts.writefds, sizeof(fd_set));
+		if (efds != NULL)
+			memor(efds, &evts.errorfds, sizeof(fd_set));
 		return evts.count;
 	}
 	if (ret == E_TMOUT) {
-		memset(rfds, 0, sizeof(fd_set));
-		memset(wfds, 0, sizeof(fd_set));
-		memset(efds, 0, sizeof(fd_set));
 		return 0;
 	}
@@ -441,4 +473,6 @@
 	struct _IO_FILE *fp = tcpid_to_fd(cepid);
 	FLGPTN flgptn = 0;
+	ER ret;
+	int len;
 
 	if (fp == NULL)
@@ -449,5 +483,39 @@
 
 	switch (fncd) {
+	case TFN_TCP_RCV_BUF:
+		len = *(int *)p_parblk;
+		if (len <= 0)
+			return E_OK;
+
+		ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		fp->psock->len += len;
+		ret = sig_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "sig_sem => %d", ret);
+		}
+
+		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+
 	case TFN_TCP_RCV_DAT:
+		len = *(int *)p_parblk;
+		if (len <= 0)
+			return E_OK;
+
+		ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		fp->psock->len += len;
+		ret = sig_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "sig_sem => %d", ret);
+		}
+
 		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
 
@@ -484,4 +552,5 @@
 	struct _IO_FILE *fp = udpid_to_fd(cepid);
 	FLGPTN flgptn = 0;
+	int len;
 
 	if (fp == NULL)
@@ -492,6 +561,47 @@
 
 	switch (fncd) {
+	case TEV_UDP_RCV_DAT:
+	{
+		T_UDP_RCV_DAT_PARA *udppara = (T_UDP_RCV_DAT_PARA *)p_parblk;
+		len = udppara->len;
+		if (len <= 0)
+			return E_OK;
+
+		ER ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		fp->psock->len = len;
+		if (fp->psock->input != NULL) {
+			ret = rel_net_buf(fp->psock->input);
+			if (ret < 0) {
+				syslog(LOG_ERROR, "rel_net_buf => %d", ret);
+			}
+		}
+		fp->psock->input = udppara->input;
+		fp->psock->buf = GET_UDP_SDU(udppara->input, udppara->off);
+		memset(&fp->psock->raddr4, 0, sizeof(fp->psock->raddr4));
+		fp->psock->raddr4.sin_family = AF_INET;
+		fp->psock->raddr4.sin_port = htons(udppara->rep4.portno);
+		fp->psock->raddr4.sin_addr.s_addr = htonl(udppara->rep4.ipaddr);
+		udppara->input->flags |= NB_FLG_NOREL_IFOUT;
+		ret = sig_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "sig_sem => %d", ret);
+		}
+
+		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+	}
 	case TFN_UDP_CRE_CEP:
+		return E_OK;
+
 	case TFN_UDP_RCV_DAT:
+		len = *(int *)p_parblk;
+		if (len <= 0)
+			return E_OK;
+
 		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
 
@@ -524,101 +634,149 @@
 ER shell_get_evts(struct fd_events *evts, TMO tmout)
 {
-	ER ret;
-	FLGPTN waitptn, flgptn = 0, readfds = 0, writefds = 0;
-	struct _IO_FILE *fp = NULL;
 	int count = 0;
-
-	stdio_update_evts();
+	SYSTIM prev, now;
+
+	get_tim(&prev);
+
+	for (;;) {
+		ER ret;
+		FLGPTN waitptn, flgptn, readfds = 0, writefds = 0;
+		struct _IO_FILE *fp = NULL;
+
+		stdio_update_evts();
 
 #ifndef NTSHELL_NO_SOCKET
-	waitptn = *((FLGPTN *)&evts->errorfds);
+		waitptn = *((FLGPTN *)&evts->errorfds);
 #else
-	waitptn = *((FLGPTN *)&evts->readfds) | *((FLGPTN *)&evts->errorfds);
+		waitptn = *((FLGPTN *)&evts->readfds) | *((FLGPTN *)&evts->errorfds);
 #endif
-	for (int fd = 0; fd < fd_table_count; fd++) {
-		fp = &fd_table[fd];
+		for (int fd = 0; fd < fd_table_count; fd++) {
+			fp = &fd_table[fd];
 
 #ifndef NTSHELL_NO_SOCKET
-		if (FD_ISSET(fd, &evts->readfds)) {
-			if ((fp->type == IO_TYPE_TCP)
-				&& (GET_TCP_CEP(fp->psock->cepid)->rwbuf_count > 0)) {
-				FD_SET(fd, (fd_set *)&readfds);
-				count++;
-				if (fp->readevt_w == fp->readevt_r) fp->readevt_r--;
+			if (FD_ISSET(fd, &evts->readfds)) {
+				if ((fp->type == IO_TYPE_TCP) && (fp->psock->cepid != 0)) {
+					if (fp->psock->len == 0) {
+						ret = tcp_rcv_buf(fp->psock->cepid, &fp->psock->input, TMO_NBLK);
+						if ((ret != E_WBLK) && (ret != E_OBJ) && (ret < 0)) {
+							syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
+							//return ret;
+						}
+						if (ret > 0) {
+							ret = wai_sem(SEM_FILEDESC);
+							if (ret < 0) {
+								syslog(LOG_ERROR, "wai_sem => %d", ret);
+							}
+							fp->psock->len += ret;
+							ret = sig_sem(SEM_FILEDESC);
+							if (ret < 0) {
+								syslog(LOG_ERROR, "sig_sem => %d", ret);
+							}
+						}
+					}
+					else ret = 1;
+					if (ret > 0) {
+						FD_SET(fd, (fd_set *)&readfds);
+						count++;
+						if (fp->readevt_w == fp->readevt_r) fp->readevt_r--;
+					}
+				}
+				else if ((fp->type == IO_TYPE_UDP) && (fp->psock->cepid != 0)) {
+					if (fp->psock->input != NULL) {
+						FD_SET(fd, (fd_set *)&readfds);
+						count++;
+						if (fp->readevt_w == fp->readevt_r) fp->readevt_r--;
+					}
+				}
+				else {
+					FD_SET(fd, (fd_set *)&waitptn);
+				}
 			}
-			else {
-				FD_SET(fd, (fd_set *)&waitptn);
+#endif
+			if (FD_ISSET(fd, &evts->writefds)) {
+				if (fp->writeevt_w == fp->writeevt_r) {
+					FD_SET(fd, (fd_set *)&writefds);
+					count++;
+					if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_r--;
+				}
+				else {
+					FD_SET(fd, (fd_set *)&waitptn);
+				}
 			}
 		}
-#endif
-		if (FD_ISSET(fd, &evts->writefds)) {
-			if (fp->writable) {
-				FD_SET(fd, (fd_set *)&writefds);
+		memset(evts, 0, sizeof(*evts));
+
+		if (waitptn == 0) {
+			memcpy(&evts->readfds, &readfds, sizeof(evts->readfds));
+			memcpy(&evts->writefds, &writefds, sizeof(evts->writefds));
+			evts->count = count;
+			return E_OK;
+		}
+		else if ((readfds | writefds) != 0) {
+			set_flg(FLG_SELECT_WAIT, (readfds | writefds));
+		}
+
+		/* イベント待ち */
+		flgptn = 0;
+		ret = twai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn, tmout);
+		if (ret != E_OK) {
+			if (ret != E_TMOUT) {
+				syslog(LOG_ERROR, "twai_flg => %d", ret);
+				return ret;
+			}
+
+			stdio_flgptn(&flgptn);
+
+			if (flgptn == 0)
+				return E_TMOUT;
+		}
+		flgptn &= waitptn;
+
+		/* 受け取ったフラグのみクリア */
+		ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
+		if (ret != E_OK) {
+			syslog(LOG_ERROR, "clr_flg => %d", ret);
+		}
+
+		count = 0;
+		for (int fd = 0; fd < fd_table_count; fd++) {
+			if (!FD_ISSET(fd, (fd_set *)&waitptn))
+				continue;
+
+			fp = &fd_table[fd];
+
+			if (fp->readevt_w != fp->readevt_r) {
+				fp->readevt_r++;
+				FD_SET(fd, &evts->readfds);
 				count++;
 			}
-			else {
-				FD_SET(fd, (fd_set *)&waitptn);
+			if (fp->writeevt_w != fp->writeevt_r) {
+				fp->writeevt_r++;
+				fp->writable = 1;
 			}
-		}
-	}
-	memset(evts, 0, sizeof(*evts));
-
-	if (waitptn == 0) {
-		memcpy(&evts->readfds, &readfds, sizeof(evts->readfds));
-		memcpy(&evts->writefds, &writefds, sizeof(evts->writefds));
-		evts->count = count;
-		return E_OK;
-	}
-	else if ((readfds | writefds) != 0){
-		set_flg(FLG_SELECT_WAIT, (readfds | writefds));
-	}
-
-	/* イベント待ち */
-	ret = twai_flg(FLG_SELECT_WAIT, waitptn, TWF_ORW, &flgptn, tmout);
-	if (ret != E_OK) {
-		if (ret != E_TMOUT) {
-			syslog(LOG_ERROR, "twai_flg => %d", ret);
-			return ret;
-		}
-
-		stdio_flgptn(&flgptn);
-
-		if (flgptn == 0)
+			if (fp->writable) {
+				FD_SET(fd, &evts->writefds);
+				count++;
+			}
+			if (fp->errorevt_w != fp->errorevt_r) {
+				fp->errorevt_r++;
+				FD_SET(fd, &evts->errorfds);
+				count++;
+			}
+		}
+
+		if (count > 0)
+			break;
+
+		get_tim(&now);
+
+		SYSTIM elapse = now - prev;
+		if (elapse > tmout)
 			return E_TMOUT;
-	}
-	flgptn &= waitptn;
-
-	/* 受け取ったフラグのみクリア */
-	ret = clr_flg(FLG_SELECT_WAIT, ~flgptn);
-	if (ret != E_OK) {
-		syslog(LOG_ERROR, "clr_flg => %d", ret);
-	}
-
-	count = 0;
-	for (int fd = 0; fd < fd_table_count; fd++) {
-		if (!FD_ISSET(fd, (fd_set *)&waitptn))
-			continue;
-
-		fp = &fd_table[fd];
-
-		if (fp->readevt_w != fp->readevt_r) {
-			fp->readevt_r++;
-			FD_SET(fd, &evts->readfds);
-			count++;
-		}
-		if (fp->writeevt_w != fp->writeevt_r) {
-			fp->writeevt_r++;
-			fp->writable = 1;
-		}
-		if (fp->writable) {
-			FD_SET(fd, &evts->writefds);
-			count++;
-		}
-		if (fp->errorevt_w != fp->errorevt_r) {
-			fp->errorevt_r++;
-			FD_SET(fd, &evts->errorfds);
-			count++;
-		}
-	}
+
+		prev = now;
+		tmout -= elapse;
+	}
+
 	evts->count = count;
 
@@ -676,9 +834,4 @@
 }
 
-ssize_t shell_send(int fd, const void *buf, size_t len, int flags)
-{
-	return -ENOMEM;
-}
-
 ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
 {
@@ -691,10 +844,5 @@
 }
 
-ssize_t shell_recv(int fd, void *buf, size_t len, int flags)
-{
-	return -ENOMEM;
-}
-
-ssize_t shell_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
+ssize_t shell_recvfrom(int fd, void *__restrict buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
 {
 	return -ENOMEM;
