Index: EcnlProtoTool/trunk/ntshell/src/socket_stub.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/src/socket_stub.c	(revision 321)
+++ EcnlProtoTool/trunk/ntshell/src/socket_stub.c	(revision 331)
@@ -35,26 +35,10 @@
  *  @(#) $Id$
  */
-#include <sys/types.h>
-#include <stdint.h>
-#if 0
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#endif
-#include <fcntl.h>
-#include "sys/unistd.h"
-#include <stddef.h>
-#include <string.h>
-#include <stdlib.h>
+#include "shellif.h"
 #include <kernel.h>
 #include <t_syslog.h>
 #include <t_stdlib.h>
+#include <string.h>
 #include <sil.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
 #include <setjmp.h>
 #include "syssvc/syslog.h"
@@ -74,5 +58,6 @@
 #include "ff.h"
 #include "socket_stub.h"
-#define SO_REUSEADDR    2
+
+#define SOCKET_TIMEOUT 2000000
 
 typedef struct id_table_t {
@@ -107,5 +92,5 @@
 }
 
-	return -1;
+	return -ENOMEM;
 }
 
@@ -120,10 +105,10 @@
 		return 0;
 	}
-	return -1;
+	return -EINVAL;
 }
 
 int delete_tcp_rep(int repid)
 {
-	delete_tcp_fd(tmax_tcp_cepid + repid);
+	return delete_tcp_fd(tmax_tcp_cepid + repid);
 }
 
@@ -145,7 +130,8 @@
 typedef struct _IO_FILE SOCKET;
 
-int socket(int family, int type, int protocol)
+int shell_socket(int family, int type, int protocol)
 {
 	SOCKET *fp;
+	unsigned int flags;
 
 	switch (family) {
@@ -154,6 +140,9 @@
 		break;
 	default:
-		return -1;
-	}
+		return -EAFNOSUPPORT;
+	}
+
+	flags = type & (SOCK_CLOEXEC|SOCK_NONBLOCK);
+	type &= ~flags;
 
 	switch (type) {
@@ -165,9 +154,9 @@
 		break;
 	default:
-		return -1;
+		return -ENOPROTOOPT;
 	}
 
 	if (fp == NULL) {
-		return -1;
+		return -ENOMEM;
 	}
 
@@ -175,18 +164,16 @@
 	fp->socket.type = type;
 	fp->socket.protocol = protocol;
+	fp->socket.flags = flags;
 
 	return fp->fd;
 }
 
-int bind(int fd, const struct sockaddr *addr, socklen_t len)
+int shell_bind(int fd, const struct sockaddr *addr, socklen_t len)
 {
 	SOCKET *fp = fd_to_fp(fd);
-	if (fp == NULL) {
-		return -1;
-	}
-
-	if (fp->socket.family != addr->sa_family) {
-		return -1;
-	}
+	if (fp == NULL)
+		return -EBADF;
+	if (fp->socket.family != addr->sa_family)
+		return -EINVAL;
 
 	ER ret;
@@ -194,5 +181,5 @@
 	case AF_INET: {
 		if (len < 8) {
-			return -1;
+			return -EINVAL;
 		}
 		struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
@@ -202,13 +189,13 @@
 			ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
 			if (cepid < 0)
-				return -1;
+				return -ENOMEM;
 
 			fp->socket.buf_size = 512 + 512;
 			fp->socket.buf = id_to_buff(cepid);
-			T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, socket_tcp_callback };
+			T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
 			if (ret != E_OK) {
 				delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
-				return -1;
+				return -ENOMEM;
 			}
 			fp->handle = cepid;
@@ -219,11 +206,11 @@
 			ID cepid = new_id(udp_cepid_table, udp_cepid_table_count);
 			if (cepid < 0)
-				return -1;
-
-			T_UDP_CCEP ccep = { 0, {ntohl(addr_in->sin_addr.s_addr), ntohs(addr_in->sin_port)}, socket_udp_callback };
+				return -ENOMEM;
+
+			T_UDP_CCEP ccep = { 0, {ntohl(addr_in->sin_addr.s_addr), ntohs(addr_in->sin_port)}, (FP)socket_udp_callback };
 			ret = udp_cre_cep(cepid, &ccep);
 			if (ret != E_OK) {
 				delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
-				return -1;
+				return -ENOMEM;
 			}
 			fp->handle = cepid;
@@ -232,5 +219,5 @@
 		}
 		default:
-			return -1;
+			return -ENOPROTOOPT;
 		}
 		break;
@@ -238,5 +225,5 @@
 	case AF_INET6: {
 		if (len < 20) {
-			return -1;
+			return -EINVAL;
 		}
 		memcpy(&fp->socket.laddr4, addr, len);
@@ -248,10 +235,11 @@
 }
 
-int listen(int fd, int backlog)
+int shell_listen(int fd, int backlog)
 {
 	SOCKET *fp = fd_to_fp(fd);
-	if ((fp == NULL) || (fp->socket.type != SOCK_STREAM)) {
-		return -1;
-	}
+	if (fp == NULL)
+		return -EBADF;
+	if (fp->socket.type != SOCK_STREAM)
+		return -EINVAL;
 
 	fp->socket.backlog = backlog;
@@ -262,5 +250,5 @@
 		ID repid = new_id(tcp_repid_table, tcp_repid_table_count);
 		if (repid < 0)
-			return -1;
+			return -ENOMEM;
 
 		struct sockaddr_in *laddr = &fp->socket.laddr4;
@@ -269,5 +257,5 @@
 		if (ret != E_OK) {
 			delete_id(tcp_repid_table, tcp_repid_table_count, repid);
-			return -1;
+			return -ENOMEM;
 		}
 		fp->socket.repid = repid;
@@ -282,10 +270,11 @@
 }
 
-int connect(int fd, const struct sockaddr *addr, socklen_t len)
+int shell_connect(int fd, const struct sockaddr *addr, socklen_t len)
 {
 	SOCKET *fp = fd_to_fp(fd);
-	if ((fp == NULL) || (fp->socket.type != SOCK_STREAM)) {
-		return -1;
-	}
+	if (fp == NULL)
+		return -EBADF;
+	if (fp->socket.type != SOCK_STREAM)
+		return -EINVAL;
 
 	ER ret;
@@ -293,18 +282,18 @@
 	case AF_INET: {
 		if (len < 8) {
-			return -1;
+			return -EINVAL;
 		}
 		if (fp->socket.cepid == 0) {
 			ID cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
 			if (cepid < 0)
-				return -1;
+				return -ENOMEM;
 
 			fp->socket.buf_size = 512 + 512;
 			fp->socket.buf = id_to_buff(cepid);
-			T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, socket_tcp_callback };
+			T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
 			if (ret != E_OK) {
 				delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
-				return -1;
+				return -ENOMEM;
 			}
 			fp->handle = cepid;
@@ -317,7 +306,7 @@
 		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->socket.cepid, &lep, &rep, TMO_FEVR);
+		ret = tcp_con_cep(fp->socket.cepid, &lep, &rep, SOCKET_TIMEOUT);
 		if (ret < 0) {
-			return -1;
+			return -EHOSTUNREACH;
 		}
 		break;
@@ -331,14 +320,15 @@
 }
 
-int accept(int fd, struct sockaddr *addr, socklen_t *len)
+int shell_accept(int fd, struct sockaddr *__restrict addr, socklen_t *__restrict len)
 {
 	SOCKET *lfp = fd_to_fp(fd);
-	if ((lfp == NULL) || (lfp->socket.type != SOCK_STREAM)) {
-		return -1;
-	}
+	if (lfp == NULL)
+		return -EBADF;
+	if (lfp->socket.type != SOCK_STREAM)
+		return -EINVAL;
 
 	SOCKET *fp = new_tcp_fd(0);
 	if (fp == NULL) {
-		return -1;
+		return -ENOMEM;
 	}
 
@@ -352,13 +342,13 @@
 			cepid = new_id(tcp_cepid_table, tcp_cepid_table_count);
 			if (cepid < 0)
-				return -1;
+				return -ENOMEM;
 
 			fp->socket.buf_size = 512 + 512;
 			fp->socket.buf = id_to_buff(cepid);
-			T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, socket_tcp_callback };
+			T_TCP_CCEP ccep = { 0, fp->socket.buf, 512, &fp->socket.buf[512], 512, (FP)socket_tcp_callback };
 			ret = tcp_cre_cep(cepid, &ccep);
 			if (ret != E_OK) {
 				delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
-				return -1;
+				return -ENOMEM;
 			}
 			fp->handle = cepid;
@@ -375,5 +365,5 @@
 		ret = tcp_acp_cep(fp->socket.cepid, fp->socket.repid, &rep, TMO_FEVR);
 		if (ret < 0) {
-			return -1;
+			return -ENOMEM;
 		}
 		struct sockaddr_in *raddr = &fp->socket.raddr4;
@@ -385,5 +375,5 @@
 	}
 	case AF_INET6: {
-		break;
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -392,5 +382,5 @@
 		int sz = *len;
 		if (sz < 8) {
-			return -1;
+			return -EINVAL;
 		}
 		struct sockaddr_in *raddr = &fp->socket.raddr4;
@@ -404,9 +394,9 @@
 }
 
-ssize_t send(int fd, const void *buf, size_t len, int flags)
+ssize_t shell_send(int fd, const void *buf, size_t len, int flags)
 {
 	SOCKET *fp = fd_to_fp(fd);
 	if (fp == NULL) {
-		return -1;
+		return -EBADF;
 	}
 
@@ -417,13 +407,13 @@
 		case SOCK_STREAM: {
 			if (flags & MSG_OOB) {
-				ret = tcp_snd_oob(fp->socket.cepid, (void *)buf, len, TMO_FEVR);
+				ret = tcp_snd_oob(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
 			else {
-				ret = tcp_snd_dat(fp->socket.cepid, (void *)buf, len, TMO_FEVR);
+				ret = tcp_snd_dat(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
@@ -431,11 +421,11 @@
 		}
 		case SOCK_DGRAM: {
-			return -1;
-		}
-		}
-		break;
-	}
-	case AF_INET6: {
-		break;
+			return -EINVAL;
+		}
+		}
+		break;
+	}
+	case AF_INET6: {
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -444,9 +434,9 @@
 }
 
-ssize_t sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
+ssize_t shell_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t alen)
 {
 	SOCKET *fp = fd_to_fp(fd);
 	if (fp == NULL) {
-		return -1;
+		return -EBADF;
 	}
 
@@ -457,17 +447,17 @@
 		case SOCK_STREAM: {
 			if ((addr != NULL) && (alen != 0)) {
-				return -1;
+				return -EINVAL;
 			}
 
 			if (flags & MSG_OOB) {
-				ret = tcp_snd_oob(fp->socket.cepid, (void *)buf, len, TMO_FEVR);
+				ret = tcp_snd_oob(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
 			else {
-				ret = tcp_snd_dat(fp->socket.cepid, (void *)buf, len, TMO_FEVR);
+				ret = tcp_snd_dat(fp->socket.cepid, (void *)buf, len, SOCKET_TIMEOUT);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
@@ -477,5 +467,5 @@
 			int sz = alen;
 			if (sz < 8) {
-				return -1;
+				return -EINVAL;
 			}
 			struct sockaddr_in *raddr = &fp->socket.raddr4;
@@ -483,15 +473,16 @@
 			memcpy(raddr, addr, sz);
 			T_IPV4EP rep = { ntohl(raddr->sin_addr.s_addr), ntohs(raddr->sin_port) };
-			ret = udp_snd_dat(fp->socket.cepid, &rep, (void *)buf, len, TMO_FEVR);
+			ret = udp_snd_dat(fp->socket.cepid, &rep, (void *)buf, len,
+				(fp->socket.flags & O_NONBLOCK) ? 0 : SOCKET_TIMEOUT);
 			if (ret < 0) {
-				return -1;
-			}
-			break;
-		}
-		}
-		break;
-	}
-	case AF_INET6: {
-		break;
+				return -ECOMM;
+			}
+			break;
+		}
+		}
+		break;
+	}
+	case AF_INET6: {
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -500,9 +491,15 @@
 }
 
-ssize_t recv(int fd, void *buf, size_t len, int flags)
+ssize_t shell_sendmsg(int fd, const struct msghdr *msg, int flags)
+{
+	no_implement("sendmsg\n");
+	return -ENOSYS;
+}
+
+ssize_t shell_recv(int fd, void *buf, size_t len, int flags)
 {
 	SOCKET *fp = fd_to_fp(fd);
 	if (fp == NULL) {
-		return -1;
+		return -EBADF;
 	}
 
@@ -515,5 +512,5 @@
 				ret = tcp_rcv_oob(fp->socket.cepid, buf, len);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
@@ -521,5 +518,5 @@
 				ret = tcp_rcv_dat(fp->socket.cepid, buf, len, TMO_FEVR);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
@@ -528,14 +525,14 @@
 		case SOCK_DGRAM: {
 			T_IPV4EP rep = { 0, 0 };
-			ret = udp_rcv_dat(fp->socket.cepid, &rep, buf, len, TMO_FEVR);
+			ret = udp_rcv_dat(fp->socket.cepid, &rep, buf, len, (fp->socket.flags & O_NONBLOCK) ? 0 : TMO_FEVR);
 			if (ret < 0) {
-				return -1;
-			}
-		}
-		}
-		break;
-	}
-	case AF_INET6: {
-		break;
+				return -ECOMM;
+			}
+		}
+		}
+		break;
+	}
+	case AF_INET6: {
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -544,9 +541,9 @@
 }
 
-ssize_t recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *alen)
+ssize_t shell_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *__restrict addr, socklen_t *__restrict alen)
 {
 	SOCKET *fp = fd_to_fp(fd);
 	if (fp == NULL) {
-		return -1;
+		return -EBADF;
 	}
 
@@ -559,5 +556,5 @@
 				ret = tcp_rcv_oob(fp->socket.cepid, buf, len);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
@@ -565,5 +562,5 @@
 				ret = tcp_rcv_dat(fp->socket.cepid, buf, len, TMO_FEVR);
 				if (ret < 0) {
-					return -1;
+					return -ECOMM;
 				}
 			}
@@ -574,5 +571,5 @@
 			ret = udp_rcv_dat(fp->socket.cepid, &rep, buf, len, TMO_FEVR);
 			if (ret < 0) {
-				return -1;
+				return -ECOMM;
 			}
 			int sz = *alen;
@@ -590,5 +587,5 @@
 	}
 	case AF_INET6: {
-		break;
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -597,9 +594,15 @@
 }
 
-int shutdown(int fd, int how)
+ssize_t shell_recvmsg(int fd, struct msghdr *msg, int flags)
+{
+	no_implement("recvmsg\n");
+	return -ENOSYS;
+}
+
+int shell_shutdown(int fd, int how)
 {
 	SOCKET *fp = fd_to_fp(fd);
 	if (fp == NULL) {
-		return -1;
+		return -EBADF;
 	}
 
@@ -611,13 +614,13 @@
 			ret = tcp_sht_cep(fp->socket.cepid);
 			if (ret < 0) {
-				return -1;
-			}
-			break;
-		}
-		}
-		break;
-	}
-	case AF_INET6: {
-		break;
+				return -ECOMM;
+			}
+			break;
+		}
+		}
+		break;
+	}
+	case AF_INET6: {
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -626,9 +629,9 @@
 }
 
-int getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
+int shell_getsockopt(int fd, int level, int optname, void *optval, socklen_t *__restrict optlen)
 {
 	SOCKET *fp = fd_to_fp(fd);
 	if (fp == NULL) {
-		return -1;
+		return -EBADF;
 	}
 
@@ -650,5 +653,5 @@
 				ret = tcp_get_opt(fp->socket.cepid, optname, (void *)optval, *optlen);
 				if (ret < 0) {
-					return -1;
+					return -EINVAL;
 				}
 				*optlen = ret;
@@ -660,5 +663,5 @@
 			ret = udp_get_opt(fp->socket.cepid, optname, (void *)optval, *optlen);
 			if (ret < 0) {
-				return -1;
+				return -EINVAL;
 			}
 			*optlen = ret;
@@ -669,5 +672,5 @@
 	}
 	case AF_INET6: {
-		break;
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -676,9 +679,9 @@
 }
 
-int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
+int shell_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
 {
 	SOCKET *fp = fd_to_fp(fd);
 	if (fp == NULL) {
-		return -1;
+		return -EBADF;
 	}
 
@@ -700,5 +703,5 @@
 				ret = tcp_set_opt(fp->socket.cepid, optname, (void *)optval, optlen);
 				if (ret < 0) {
-					return -1;
+					return -EINVAL;
 				}
 				break;
@@ -709,13 +712,13 @@
 			ret = udp_set_opt(fp->socket.cepid, optname, (void *)optval, optlen);
 			if (ret < 0) {
-				return -1;
-			}
-			break;
-		}
-		}
-		break;
-	}
-	case AF_INET6: {
-		break;
+				return -EINVAL;
+			}
+			break;
+		}
+		}
+		break;
+	}
+	case AF_INET6: {
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -735,5 +738,5 @@
 				//return -1;
 			}
-			ret = tcp_cls_cep(cepid, TMO_FEVR);
+			ret = tcp_cls_cep(cepid, (fp->socket.repid != 0) ? 0 : SOCKET_TIMEOUT);
 			if (ret < 0) {
 				//return -1;
@@ -743,5 +746,5 @@
 			delete_id(tcp_cepid_table, tcp_cepid_table_count, cepid);
 			if (ret < 0) {
-				return -1;
+				return -EINVAL;
 			}
 		}
@@ -752,14 +755,14 @@
 			delete_id(tcp_repid_table, tcp_repid_table_count, repid);
 			if (ret < 0) {
-				return -1;
+				return -EINVAL;
 			}
 		}
 		else {
-			return -1;
-		}
-		break;
-	}
-	case AF_INET6: {
-		break;
+			return -EINVAL;
+		}
+		break;
+	}
+	case AF_INET6: {
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -770,15 +773,20 @@
 size_t tcp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
 {
-	return recv(fp->fd, dst, dstsz, 0);
+	return shell_recv(fp->fd, dst, dstsz, 0);
 }
 
 size_t tcp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
 {
-	return send(fp->fd, src, srcsz, 0);
+	return shell_send(fp->fd, src, srcsz, 0);
 }
 
 off_t tcp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org)
 {
-	return -1;
+	return -EPERM;
+}
+
+int tcp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg)
+{
+	return -EINVAL;
 }
 
@@ -794,10 +802,10 @@
 		delete_id(udp_cepid_table, udp_cepid_table_count, cepid);
 		if (ret < 0) {
-			return -1;
-		}
-		break;
-	}
-	case AF_INET6: {
-		break;
+			return -EINVAL;
+		}
+		break;
+	}
+	case AF_INET6: {
+		return -EAFNOSUPPORT;
 	}
 	}
@@ -808,15 +816,20 @@
 size_t udp_fd_read(struct _IO_FILE *fp, unsigned char *dst, size_t dstsz)
 {
-	return recv(fp->fd, dst, dstsz, 0);
+	return shell_recv(fp->fd, dst, dstsz, 0);
 }
 
 size_t udp_fd_write(struct _IO_FILE *fp, const unsigned char *src, size_t srcsz)
 {
-	return send(fp->fd, src, srcsz, 0);
+	return shell_send(fp->fd, src, srcsz, 0);
 }
 
 off_t udp_fd_seek(struct _IO_FILE *fp, off_t ofs, int org)
 {
-	return -1;
+	return -EPERM;
+}
+
+int udp_fd_ioctl(struct _IO_FILE *fp, int req, void *arg)
+{
+	return -EINVAL;
 }
 
