Index: asp3_tinet_ecnl_arm/trunk/ntshell/src/socket_stub.c
===================================================================
--- asp3_tinet_ecnl_arm/trunk/ntshell/src/socket_stub.c	(revision 364)
+++ asp3_tinet_ecnl_arm/trunk/ntshell/src/socket_stub.c	(revision 374)
@@ -57,5 +57,8 @@
 #include <netinet/udp_var.h>
 #include <net/net_buf.h>
-//#include <netinet/tcp_var.h>
+#include <netinet/udp.h>
+#include <netinet/udp_var.h>
+#include <netinet/tcp.h>
+#include <netinet/tcp_var.h>
 #include <netapp/resolver.h>
 extern const ID tmax_tcp_cepid;
@@ -64,5 +67,28 @@
 #include "kernel_cfg.h"
 
+#ifdef _DEBUG
+static const char THIS_FILE[] = __FILE__;
+#endif
+
 #define SOCKET_TIMEOUT 2000000
+
+static int tcp_fd_close(struct SHELL_FILE *fp);
+static size_t tcp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
+static size_t tcp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
+static off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org);
+static int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
+static bool_t tcp_fd_readable(struct SHELL_FILE *fp);
+static void tcp_fd_delete(struct SHELL_FILE *fp);
+
+static int udp_fd_close(struct SHELL_FILE *fp);
+static size_t udp_fd_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
+static size_t udp_fd_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
+static off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org);
+static int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg);
+static bool_t udp_fd_readable(struct SHELL_FILE *fp);
+static void udp_fd_delete(struct SHELL_FILE *fp);
+
+IO_TYPE IO_TYPE_TCP = { tcp_fd_close, tcp_fd_read, tcp_fd_write, tcp_fd_seek, tcp_fd_ioctl, tcp_fd_readable, tcp_fd_delete };
+IO_TYPE IO_TYPE_UDP = { udp_fd_close, udp_fd_read, udp_fd_write, udp_fd_seek, udp_fd_ioctl, udp_fd_readable, udp_fd_delete };
 
 typedef struct id_table_t {
@@ -116,10 +142,5 @@
 }
 
-int delete_tcp_rep(int repid)
-{
-	return delete_tcp_fd(tmax_tcp_cepid + repid);
-}
-
-typedef struct _IO_FILE SOCKET;
+typedef struct SHELL_FILE SOCKET;
 
 int shell_socket(int family, int type, int protocol)
@@ -141,8 +162,18 @@
 	switch (type) {
 	case SOCK_STREAM:
-		fp = new_tcp_fd(0);
+		fp = new_fp(&IO_TYPE_TCP, 0, 0);
+		if (fp == NULL)
+			return -ENOMEM;
+
+		fp->exinf = malloc(sizeof(socket_t));
+		memset(fp->exinf, 0, sizeof(socket_t));
 		break;
 	case SOCK_DGRAM:
-		fp = new_udp_fd(0);
+		fp = new_fp(&IO_TYPE_UDP, 0, 1);
+		if (fp == NULL)
+			return -ENOMEM;
+
+		fp->exinf = malloc(sizeof(socket_t));
+		memset(fp->exinf, 0, sizeof(socket_t));
 		break;
 	default:
@@ -150,12 +181,9 @@
 	}
 
-	if (fp == NULL) {
-		return -ENOMEM;
-	}
-
-	fp->psock->family = family;
-	fp->psock->type = type;
-	fp->psock->protocol = protocol;
-	fp->psock->flags = flags;
+	socket_t *socket = (socket_t *)fp->exinf;
+	socket->family = family;
+	socket->type = type;
+	socket->protocol = protocol;
+	socket->flags = flags;
 
 	return fp->fd;
@@ -167,5 +195,6 @@
 	if (fp == NULL)
 		return -EBADF;
-	if (fp->psock->family != addr->sa_family)
+	socket_t *socket = (socket_t *)fp->exinf;
+	if (socket->family != addr->sa_family)
 		return -EINVAL;
 
@@ -177,6 +206,6 @@
 		}
 		struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
-		memcpy(&fp->psock->laddr4, addr, len);
-		switch (fp->psock->type) {
+		memcpy(&socket->laddr4, addr, len);
+		switch (socket->type) {
 		case SOCK_STREAM: {
 			ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
@@ -184,10 +213,10 @@
 				return -ENOMEM;
 
-			fp->psock->buf_size = 512 + 512;
-			fp->psock->buf = malloc(fp->psock->buf_size);
+			socket->buf_size = 512 + 512;
+			socket->buf = malloc(socket->buf_size);
 #ifdef _DEBUG
-			memset(fp->psock->buf, 0, fp->psock->buf_size);
+			memset(socket->buf, 0, socket->buf_size);
 #endif
-			T_TCP_CCEP ccep = { 0, fp->psock->buf, 512, &fp->psock->buf[512], 512, (FP)socket_tcp_callback };
+			T_TCP_CCEP ccep = { 0, socket->buf, 512, &socket->buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
 			if (ret != E_OK) {
@@ -196,5 +225,5 @@
 			}
 			fp->handle = cepid;
-			fp->psock->cepid = cepid;
+			socket->cepid = cepid;
 			break;
 		}
@@ -211,5 +240,5 @@
 			}
 			fp->handle = cepid;
-			fp->psock->cepid = cepid;
+			socket->cepid = cepid;
 			break;
 		}
@@ -223,5 +252,5 @@
 			return -EINVAL;
 		}
-		memcpy(&fp->psock->laddr4, addr, len);
+		memcpy(&socket->laddr4, addr, len);
 		break;
 	}
@@ -236,11 +265,12 @@
 	if (fp == NULL)
 		return -EBADF;
-	if (fp->psock->type != SOCK_STREAM)
+	socket_t *socket = (socket_t *)fp->exinf;
+	if (socket->type != SOCK_STREAM)
 		return -EINVAL;
 
-	fp->psock->backlog = backlog;
+	socket->backlog = backlog;
 
 	ER ret;
-	switch (fp->psock->family) {
+	switch (socket->family) {
 	case AF_INET: {
 		ID repid = new_id(tcp_repid_table, tcp_repid_table_count);
@@ -248,5 +278,5 @@
 			return -ENOMEM;
 
-		struct sockaddr_in *laddr = &fp->psock->laddr4;
+		struct sockaddr_in *laddr = &socket->laddr4;
 		T_TCP_CREP crep = { 0, {ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port)} };
 		ret = tcp_cre_rep(repid, &crep);
@@ -255,5 +285,5 @@
 			return -ENOMEM;
 		}
-		fp->psock->repid = repid;
+		socket->repid = repid;
 		break;
 	}
@@ -271,24 +301,25 @@
 	if (fp == NULL)
 		return -EBADF;
-	if (fp->psock->type != SOCK_STREAM)
+	socket_t *socket = (socket_t *)fp->exinf;
+	if (socket->type != SOCK_STREAM)
 		return -EINVAL;
 
 	ER ret;
-	switch (fp->psock->family) {
+	switch (socket->family) {
 	case AF_INET: {
 		if (len < 8) {
 			return -EINVAL;
 		}
-		if (fp->psock->cepid == 0) {
+		if (socket->cepid == 0) {
 			ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
 			if (cepid < 0)
 				return -ENOMEM;
 
-			fp->psock->buf_size = 512 + 512;
-			fp->psock->buf = malloc(fp->psock->buf_size);
+			socket->buf_size = 512 + 512;
+			socket->buf = malloc(socket->buf_size);
 #ifdef _DEBUG
-			memset(fp->psock->buf, 0, fp->psock->buf_size);
+			memset(socket->buf, 0, socket->buf_size);
 #endif
-			T_TCP_CCEP ccep = { 0, fp->psock->buf, 512, &fp->psock->buf[512], 512, (FP)socket_tcp_callback };
+			T_TCP_CCEP ccep = { 0, socket->buf, 512, &socket->buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
 			if (ret != E_OK) {
@@ -297,13 +328,13 @@
 			}
 			fp->handle = cepid;
-			fp->psock->cepid = cepid;
-		}
-		struct sockaddr_in *laddr = &fp->psock->laddr4;
-		struct sockaddr_in *raddr = &fp->psock->raddr4;
+			socket->cepid = cepid;
+		}
+		struct sockaddr_in *laddr = &socket->laddr4;
+		struct sockaddr_in *raddr = &socket->raddr4;
 		memset(raddr, 0, sizeof(*raddr));
 		memcpy(raddr, addr, len);
 		T_IPV4EP lep = { ntohl(laddr->sin_addr.s_addr), ntohs(laddr->sin_port) };
 		T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
-		ret = tcp_con_cep(fp->psock->cepid, &lep, &rep, SOCKET_TIMEOUT);
+		ret = tcp_con_cep(socket->cepid, &lep, &rep, SOCKET_TIMEOUT);
 		if (ret < 0) {
 			return -EHOSTUNREACH;
@@ -324,29 +355,32 @@
 	if (lfp == NULL)
 		return -EBADF;
-	if (lfp->psock->type != SOCK_STREAM)
+	if (((socket_t *)lfp->exinf)->type != SOCK_STREAM)
 		return -EINVAL;
 
-	SOCKET *fp = new_tcp_fd(0);
-	if (fp == NULL) {
+	SOCKET *fp = new_fp(&IO_TYPE_TCP, 0, 0);
+	if (fp == NULL)
 		return -ENOMEM;
-	}
-
-	memcpy(fp->psock, lfp->psock, offsetof(socket_t, buf_size));
+
+	fp->exinf = malloc(sizeof(socket_t));
+	memset(fp->exinf, 0, sizeof(socket_t));
+
+	memcpy(fp->exinf, lfp->exinf, offsetof(socket_t, buf_size));
 
 	ER ret;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
 		ID cepid;
-		if (fp->psock->cepid == 0) {
+		if (socket->cepid == 0) {
 			cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
 			if (cepid < 0)
 				return -ENOMEM;
 
-			fp->psock->buf_size = 512 + 512;
-			fp->psock->buf = malloc(fp->psock->buf_size);
+			socket->buf_size = 512 + 512;
+			socket->buf = malloc(socket->buf_size);
 #ifdef _DEBUG
-			memset(fp->psock->buf, 0, fp->psock->buf_size);
+			memset(socket->buf, 0, socket->buf_size);
 #endif
-			T_TCP_CCEP ccep = { 0, fp->psock->buf, 512, &fp->psock->buf[512], 512, (FP)socket_tcp_callback };
+			T_TCP_CCEP ccep = { 0, socket->buf, 512, &socket->buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
 			if (ret != E_OK) {
@@ -355,20 +389,20 @@
 			}
 			fp->handle = cepid;
-			fp->psock->cepid = cepid;
+			socket->cepid = cepid;
 		}
 		else {
-			cepid = lfp->psock->cepid;
+			cepid = ((socket_t *)lfp->exinf)->cepid;
 			fp->handle = cepid;
-			lfp->handle = tmax_tcp_cepid + lfp->psock->repid;
-			lfp->psock->cepid = 0;
-			lfp->psock->buf_size = 0;
-			lfp->psock->buf = 0;
+			lfp->handle = tmax_tcp_cepid + ((socket_t *)lfp->exinf)->repid;
+			((socket_t *)lfp->exinf)->cepid = 0;
+			((socket_t *)lfp->exinf)->buf_size = 0;
+			((socket_t *)lfp->exinf)->buf = 0;
 		}
 		T_IPV4EP rep = { 0, 0 };
-		ret = tcp_acp_cep(fp->psock->cepid, fp->psock->repid, &rep, TMO_FEVR);
+		ret = tcp_acp_cep(socket->cepid, socket->repid, &rep, TMO_FEVR);
 		if (ret < 0) {
 			return -ENOMEM;
 		}
-		struct sockaddr_in *raddr = &fp->psock->raddr4;
+		struct sockaddr_in *raddr = &socket->raddr4;
 		memset(raddr, 0, sizeof(*raddr));
 		raddr->sin_family = AF_INET;
@@ -387,5 +421,5 @@
 			return -EINVAL;
 		}
-		struct sockaddr_in *raddr = &fp->psock->raddr4;
+		struct sockaddr_in *raddr = &socket->raddr4;
 		if (sz > sizeof(*raddr))
 			sz = sizeof(*raddr);
@@ -405,7 +439,8 @@
 
 	int ret = 0;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		switch (fp->psock->type) {
+		switch (socket->type) {
 		case SOCK_STREAM: {
 			if ((addr != NULL) || (alen != 0)) {
@@ -414,5 +449,5 @@
 
 			if (flags & MSG_OOB) {
-				ret = tcp_snd_oob(fp->psock->cepid, (void *)buf, len, SOCKET_TIMEOUT);
+				ret = tcp_snd_oob(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
 				if (ret < 0) {
 					return -ECOMM;
@@ -421,5 +456,5 @@
 			else {
 				for (;;) {
-					ret = tcp_snd_dat(fp->psock->cepid, (void *)buf, len, SOCKET_TIMEOUT);
+					ret = tcp_snd_dat(socket->cepid, (void *)buf, len, SOCKET_TIMEOUT);
 					if (ret < 0) {
 						if (ret == E_TMOUT)
@@ -440,10 +475,10 @@
 				return -EINVAL;
 			}
-			struct sockaddr_in *raddr = &fp->psock->raddr4;
+			struct sockaddr_in *raddr = &socket->raddr4;
 			memset(raddr, 0, sizeof(*raddr));
 			memcpy(raddr, addr, sz);
 			T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
-			ret = udp_snd_dat(fp->psock->cepid, &rep, (void *)buf, len,
-				(fp->psock->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
+			ret = udp_snd_dat(socket->cepid, &rep, (void *)buf, len,
+				(socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
 			if (ret < 0) {
 				return (ret == E_TMOUT) ? -ETIME : -ECOMM;
@@ -476,10 +511,11 @@
 
 	int ret = 0;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		switch (fp->psock->type) {
+		switch (socket->type) {
 		case SOCK_STREAM: {
 			if (flags & MSG_OOB) {
-				ret = tcp_rcv_oob(fp->psock->cepid, buf, len);
+				ret = tcp_rcv_oob(socket->cepid, buf, len);
 				if (ret < 0) {
 					syslog(LOG_ERROR, "tcp_rcv_oob => %d", ret);
@@ -489,15 +525,15 @@
 			else {
 				int rsz, tmp;
-				if (fp->psock->input == NULL) {
+				if (socket->input == NULL) {
 					ret = wai_sem(SEM_FILEDESC);
 					if (ret < 0) {
 						syslog(LOG_ERROR, "wai_sem => %d", ret);
 					}
-					fp->psock->len = 0;
+					socket->len = 0;
 					ret = sig_sem(SEM_FILEDESC);
 					if (ret < 0) {
 						syslog(LOG_ERROR, "sig_sem => %d", ret);
 					}
-					ret = tcp_rcv_buf(fp->psock->cepid, &fp->psock->input, TMO_FEVR);
+					ret = tcp_rcv_buf(socket->cepid, &socket->input, TMO_FEVR);
 					if (ret < 0) {
 						syslog(LOG_ERROR, "tcp_rcv_buf => %d", ret);
@@ -507,15 +543,15 @@
 				}
 				else
-					rsz = fp->psock->len;
+					rsz = socket->len;
 				tmp = rsz;
 				if (rsz > len)
 					rsz = len;
 				if (rsz >= 0) {
-					memcpy(buf, fp->psock->input, rsz);
+					memcpy(buf, socket->input, rsz);
 					ret = wai_sem(SEM_FILEDESC);
 					if (ret < 0) {
 						syslog(LOG_ERROR, "wai_sem => %d", ret);
 					}
-					fp->psock->len = tmp - rsz;
+					socket->len = tmp - rsz;
 					ret = sig_sem(SEM_FILEDESC);
 					if (ret < 0) {
@@ -523,9 +559,9 @@
 					}
 					if (tmp - rsz == 0) {
-						fp->psock->input = NULL;
+						socket->input = NULL;
 					}
 					else
-						fp->psock->input = (void *)&((uint8_t *)fp->psock->input)[rsz];
-					ret = tcp_rel_buf(fp->psock->cepid, rsz);
+						socket->input = (void *)&((uint8_t *)socket->input)[rsz];
+					ret = tcp_rel_buf(socket->cepid, rsz);
 					if ((ret != E_OBJ) && (ret < 0)) {
 						syslog(LOG_ERROR, "tcp_rel_buf => %d", ret);
@@ -538,5 +574,5 @@
 		}
 		case SOCK_DGRAM: {
-			struct sockaddr_in *raddr = &fp->psock->raddr4;
+			struct sockaddr_in *raddr = &socket->raddr4;
 			int rsz;
 			ret = wai_sem(SEM_FILEDESC);
@@ -544,5 +580,5 @@
 				syslog(LOG_ERROR, "wai_sem => %d", ret);
 			}
-			T_NET_BUF *input = fp->psock->input;
+			T_NET_BUF *input = socket->input;
 			if (input == NULL) {
 				ret = sig_sem(SEM_FILEDESC);
@@ -552,8 +588,9 @@
 
 				T_IPV4EP rep = { 0, 0 };
-				ret = udp_rcv_dat(fp->psock->cepid, &rep, buf, len,
-					(fp->psock->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
+				ret = udp_rcv_dat(socket->cepid, &rep, buf, len,
+					(socket->flags & O_NONBLOCK) ? TMO_POL : SOCKET_TIMEOUT);
 				if (ret < 0) {
-					syslog(LOG_ERROR, "udp_rcv_buf => %d", ret);
+					if ((socket->flags & O_NONBLOCK) == 0)
+						syslog(LOG_ERROR, "udp_rcv_buf => %d", ret);
 					return (ret == E_TMOUT) ? -ETIME : -ECOMM;
 				}
@@ -565,10 +602,10 @@
 					}
 					int sz = *alen;
-					memset(raddr, 0, sizeof(fp->psock->raddr4));
+					memset(raddr, 0, sizeof(socket->raddr4));
 					raddr->sin_family = AF_INET;
 					raddr->sin_port = htons(rep.portno);
 					raddr->sin_addr.s_addr = htonl(rep.ipaddr);
-					if (sz > sizeof(fp->psock->raddr4))
-						sz = sizeof(fp->psock->raddr4);
+					if (sz > sizeof(socket->raddr4))
+						sz = sizeof(socket->raddr4);
 					memcpy(addr, raddr, sz);
 					*alen = sz;
@@ -580,13 +617,13 @@
 			}
 			else {
-				rsz = fp->psock->len;
-				void *pbuf = fp->psock->buf;
-				fp->psock->input = NULL;
-				fp->psock->len = 0;
-				fp->psock->buf = NULL;
+				rsz = socket->len;
+				void *pbuf = socket->buf;
+				socket->input = NULL;
+				socket->len = 0;
+				socket->buf = NULL;
 				if ((addr != NULL) && (alen != NULL)) {
 					int sz = *alen;
-					if (sz > sizeof(fp->psock->raddr4))
-						sz = sizeof(fp->psock->raddr4);
+					if (sz > sizeof(socket->raddr4))
+						sz = sizeof(socket->raddr4);
 					memcpy(addr, raddr, sz);
 					*alen = sz;
@@ -632,9 +669,10 @@
 
 	ER ret;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		switch (fp->psock->type) {
+		switch (socket->type) {
 		case SOCK_STREAM: {
-			ret = tcp_sht_cep(fp->psock->cepid);
+			ret = tcp_sht_cep(socket->cepid);
 			if (ret < 0) {
 				return -ECOMM;
@@ -661,7 +699,8 @@
 
 	ER ret;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		switch (fp->psock->type) {
+		switch (socket->type) {
 		case SOCK_STREAM: {
 			switch (level) {
@@ -669,5 +708,5 @@
 				switch (optname) {
 				case SO_REUSEADDR:
-					if (fp->psock->flags & SO_REUSEADDR) {
+					if (socket->flags & SO_REUSEADDR) {
 						*(bool *)optval = true;
 					}
@@ -677,5 +716,5 @@
 					break;
 				case SO_KEEPALIVE:
-					if (fp->psock->flags & SO_KEEPALIVE) {
+					if (socket->flags & SO_KEEPALIVE) {
 						*(bool *)optval = true;
 					}
@@ -692,5 +731,5 @@
 				break;
 			case IPPROTO_TCP:
-				ret = tcp_get_opt(fp->psock->cepid, optname, (void *)optval, *optlen);
+				ret = tcp_get_opt(socket->cepid, optname, (void *)optval, *optlen);
 				if (ret < 0) {
 					return -EINVAL;
@@ -706,5 +745,5 @@
 			switch (level) {
 			case IPPROTO_UDP:
-				ret = udp_get_opt(fp->psock->cepid, optname, (void *)optval, *optlen);
+				ret = udp_get_opt(socket->cepid, optname, (void *)optval, *optlen);
 				if (ret < 0) {
 					return -EINVAL;
@@ -736,7 +775,8 @@
 
 	ER ret;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		switch (fp->psock->type) {
+		switch (socket->type) {
 		case SOCK_STREAM: {
 			switch (level){
@@ -745,16 +785,16 @@
 				case SO_REUSEADDR:
 					if (*(bool *)optval) {
-						fp->psock->flags |= SO_REUSEADDR;
+						socket->flags |= SO_REUSEADDR;
 					}
 					else {
-						fp->psock->flags &= ~SO_REUSEADDR;
+						socket->flags &= ~SO_REUSEADDR;
 					}
 					break;
 				case SO_KEEPALIVE:
 					if (*(bool *)optval) {
-						fp->psock->flags |= SO_KEEPALIVE;
+						socket->flags |= SO_KEEPALIVE;
 					}
 					else {
-						fp->psock->flags &= ~SO_KEEPALIVE;
+						socket->flags &= ~SO_KEEPALIVE;
 					}
 					break;
@@ -764,5 +804,5 @@
 				break;
 			case IPPROTO_TCP:
-				ret = tcp_set_opt(fp->psock->cepid, optname, (void *)optval, optlen);
+				ret = tcp_set_opt(socket->cepid, optname, (void *)optval, optlen);
 				if (ret < 0) {
 					return -EINVAL;
@@ -777,5 +817,5 @@
 			switch (level){
 			case IPPROTO_UDP:
-				ret = udp_set_opt(fp->psock->cepid, optname, (void *)optval, optlen);
+				ret = udp_set_opt(socket->cepid, optname, (void *)optval, optlen);
 				if (ret < 0) {
 					return -EINVAL;
@@ -809,7 +849,8 @@
 
 	socklen_t size = *len;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		struct sockaddr_in *raddr = &fp->psock->raddr4;
+		struct sockaddr_in *raddr = &socket->raddr4;
 		*len = sizeof(struct sockaddr_in);
 		if (size > sizeof(struct sockaddr_in))
@@ -837,5 +878,6 @@
 
 	socklen_t size = *len;
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
 		const T_IN4_ADDR *laddr4 = in4_get_ifaddr(0);
@@ -843,5 +885,5 @@
 		laddr.sin_family = AF_INET;
 		laddr.sin_addr.s_addr = htonl(*laddr4);
-		laddr.sin_port = fp->psock->laddr4.sin_port;
+		laddr.sin_port = socket->laddr4.sin_port;
 		memset(&laddr.sin_zero, 0, sizeof(laddr.sin_zero));
 		*len = sizeof(struct sockaddr_in);
@@ -859,21 +901,20 @@
 }
 
-int tcp_fd_close(struct _IO_FILE *fp)
+int tcp_fd_close(struct SHELL_FILE *fp)
 {
 	ER ret, ret2;
 
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		if (fp->psock->cepid != 0) {
-			ID cepid = fp->psock->cepid;
+		if (socket->cepid != 0) {
+			ID cepid = socket->cepid;
 			ret = tcp_sht_cep(cepid);
 			if (ret < 0) {
 				//return -1;
 			}
-			ret = tcp_cls_cep(cepid, (fp->psock->repid != 0) ? 0 : SOCKET_TIMEOUT);
+			ret = tcp_cls_cep(cepid, (socket->repid != 0) ? 0 : SOCKET_TIMEOUT);
 			ret2 = tcp_del_cep(cepid);
-			free(fp->psock->buf);
-			fp->psock->buf = NULL;
-			delete_tcp_fd(cepid);
+			//delete_fd_by_id(&IO_TYPE_TCP, cepid);
 			delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
 			if ((ret < 0) || (ret2 < 0)) {
@@ -881,10 +922,8 @@
 			}
 		}
-		else if (fp->psock->repid != 0) {
-			ID repid = fp->psock->repid;
+		else if (socket->repid != 0) {
+			ID repid = socket->repid;
 			ret = tcp_del_rep(repid);
-			free(fp->psock->buf);
-			fp->psock->buf = NULL;
-			delete_tcp_fd(tmax_tcp_cepid + repid);
+			//delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + repid);
 			delete_id(tcp_repid_table, tcp_repid_table_count, repid);
 			if (ret < 0) {
@@ -893,6 +932,4 @@
 		}
 		else {
-			free(fp->psock->buf);
-			fp->psock->buf = NULL;
 			return -EINVAL;
 		}
@@ -907,36 +944,157 @@
 }
 
-size_t tcp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
+size_t tcp_fd_read(struct SHELL_FILE *fp, unsigned char *dst, size_t dstsz)
 {
 	return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
 }
 
-size_t tcp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
+size_t tcp_fd_write(struct SHELL_FILE *fp, const unsigned char *src, size_t srcsz)
 {
 	return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
 }
 
-off_t tcp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org)
+off_t tcp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org)
 {
 	return -EPERM;
 }
 
-int tcp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg)
+int tcp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg)
 {
 	return -EINVAL;
 }
 
-int udp_fd_close(struct _IO_FILE *fp)
+bool_t tcp_fd_readable(struct SHELL_FILE *fp)
+{
+	ER ret;
+
+	socket_t *socket = (socket_t *)fp->exinf;
+	if (socket->cepid != 0) {
+		if (socket->len == 0) {
+			ret = tcp_rcv_buf(socket->cepid, &socket->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);
+				}
+				socket->len += ret;
+				ret = sig_sem(SEM_FILEDESC);
+				if (ret < 0) {
+					syslog(LOG_ERROR, "sig_sem => %d", ret);
+				}
+			}
+		}
+		else ret = 1;
+		if (ret > 0) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
+void tcp_fd_delete(struct SHELL_FILE *fp)
+{
+	socket_t *socket = (socket_t *)fp->exinf;
+	free(socket->buf);
+	socket->buf = NULL;
+	free(fp->exinf);
+	fp->exinf = NULL;
+}
+
+ER socket_tcp_callback(ID cepid, FN fncd, void *p_parblk)
+{
+	struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_TCP, cepid);
+	FLGPTN flgptn = 0;
+	ER ret;
+	int len;
+
+	if (fp == NULL)
+		return E_PAR;
+
+	int fd = fp->fd;
+	FD_SET(fd, (fd_set *)&flgptn);
+
+	switch (fncd) {
+	case TFN_TCP_RCV_BUF:
+		len = *(int *)p_parblk;
+		if ((len <= 0) || (fp->exinf == NULL))
+			return E_OK;
+
+		ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		socket_t *socket = (socket_t *)fp->exinf;
+		socket->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) || (fp->exinf == NULL))
+			return E_OK;
+
+		ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		socket->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_SND_DAT:
+		if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+
+	case TFN_TCP_CAN_CEP:
+		if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+
+	case TFN_TCP_DEL_REP:
+		delete_fd_by_id(&IO_TYPE_TCP, tmax_tcp_cepid + cepid);
+		return E_OK;
+
+	case TFN_TCP_DEL_CEP:
+		delete_fd_by_id(&IO_TYPE_TCP, cepid);
+		return E_OK;
+
+	default:
+		return E_OK;
+	}
+}
+
+int udp_fd_close(struct SHELL_FILE *fp)
 {
 	ER ret;
 	ID cepid;
 
-	switch (fp->psock->family) {
+	socket_t *socket = (socket_t *)fp->exinf;
+	switch (socket->family) {
 	case AF_INET: {
-		cepid = fp->psock->cepid;
+		cepid = socket->cepid;
 		ret = udp_del_cep(cepid);
-		//free(fp->psock->buf);
-		//fp->psock->buf = NULL;
-		delete_udp_fd(cepid);
+		//delete_fd_by_id(&IO_TYPE_UDP, cepid);
 		delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
 		if (ret < 0) {
@@ -953,22 +1111,126 @@
 }
 
-size_t udp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
+size_t udp_fd_read(struct SHELL_FILE *fp, unsigned char *dst, size_t dstsz)
 {
 	return shell_recvfrom(fp->fd, dst, dstsz, 0, NULL, NULL);
 }
 
-size_t udp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
+size_t udp_fd_write(struct SHELL_FILE *fp, const unsigned char *src, size_t srcsz)
 {
 	return shell_sendto(fp->fd, src, srcsz, 0, NULL, 0);
 }
 
-off_t udp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org)
+off_t udp_fd_seek(struct SHELL_FILE *fp, off_t ofs, int org)
 {
 	return -EPERM;
 }
 
-int udp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg)
+int udp_fd_ioctl(struct SHELL_FILE *fp, int req, void *arg)
 {
 	return -EINVAL;
+}
+
+bool_t udp_fd_readable(struct SHELL_FILE *fp)
+{
+	socket_t *socket = (socket_t *)fp->exinf;
+	if (socket->cepid != 0) {
+		if (socket->input != NULL) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
+void udp_fd_delete(struct SHELL_FILE *fp)
+{
+	//socket_t *socket = (socket_t *)fp->exinf;
+	//free(socket->buf);
+	//socket->buf = NULL;
+	free(fp->exinf);
+	fp->exinf = NULL;
+}
+
+ER socket_udp_callback(ID cepid, FN fncd, void *p_parblk)
+{
+	struct SHELL_FILE *fp = id_to_fd(&IO_TYPE_UDP, cepid);
+	FLGPTN flgptn = 0;
+	int len;
+
+	if (fp == NULL)
+		return E_PAR;
+
+	int fd = fp->fd;
+	FD_SET(fd, (fd_set *)&flgptn);
+
+	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) || (fp->exinf == NULL))
+			return E_OK;
+
+		ER ret = wai_sem(SEM_FILEDESC);
+		if (ret < 0) {
+			syslog(LOG_ERROR, "wai_sem => %d", ret);
+		}
+		socket_t *socket = (socket_t *)fp->exinf;
+		socket->len = len;
+		if (socket->input != NULL) {
+			ret = rel_net_buf(socket->input);
+			if (ret < 0) {
+				syslog(LOG_ERROR, "rel_net_buf => %d", ret);
+			}
+		}
+		socket->input = udppara->input;
+		socket->buf = GET_UDP_SDU(udppara->input, udppara->off);
+		memset(&socket->raddr4, 0, sizeof(socket->raddr4));
+		socket->raddr4.sin_family = AF_INET;
+		socket->raddr4.sin_port = htons(udppara->rep4.portno);
+		socket->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) || (fp->exinf == NULL))
+			return E_OK;
+
+		if (fp->readevt_w == fp->readevt_r) fp->readevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+
+	case TFN_UDP_SND_DAT:
+		if (fp->writeevt_w == fp->writeevt_r) fp->writeevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+
+	case TFN_UDP_CAN_CEP:
+		if (fp->errorevt_w == fp->errorevt_r) fp->errorevt_w++;
+
+		set_flg(FLG_SELECT_WAIT, flgptn);
+		return E_OK;
+
+	case TFN_UDP_DEL_CEP:
+		delete_fd_by_id(&IO_TYPE_UDP, cepid);
+		return E_OK;
+
+	default:
+		return E_OK;
+	}
 }
 
