[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r7055: added accept() framework, still an assertion failure I can't (bsockets/trunk)



Author: chiussi
Date: 2006-08-14 07:45:44 -0400 (Mon, 14 Aug 2006)
New Revision: 7055

Removed:
   bsockets/trunk/bsocket.c
Modified:
   bsockets/trunk/LICENSE
   bsockets/trunk/Makefile
   bsockets/trunk/bsocket.h
   bsockets/trunk/callback.c
   bsockets/trunk/event.c
   bsockets/trunk/event.h
   bsockets/trunk/io.c
   bsockets/trunk/io.h
   bsockets/trunk/notes
   bsockets/trunk/select.c
   bsockets/trunk/select.h
   bsockets/trunk/socket.c
   bsockets/trunk/socket.h
   bsockets/trunk/sync.c
   bsockets/trunk/test.c
   bsockets/trunk/unix.c
   bsockets/trunk/unix.h
Log:
added accept() framework, still an assertion failure I can't nail 


Modified: bsockets/trunk/LICENSE
===================================================================
--- bsockets/trunk/LICENSE	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/LICENSE	2006-08-14 11:45:44 UTC (rev 7055)
@@ -1,5 +1,5 @@
 ==============================================================================
-BSOCK is distributed under this license
+BSOCKETS is distributed under this license
 
 Copyright (c) 2006 Mike Chiussi
 
@@ -30,4 +30,4 @@
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-==============================================================================
\ No newline at end of file
+==============================================================================

Modified: bsockets/trunk/Makefile
===================================================================
--- bsockets/trunk/Makefile	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/Makefile	2006-08-14 11:45:44 UTC (rev 7055)
@@ -1,7 +1,7 @@
 test_HEADERS= test.h
 bsock_HEADERS = list.h test.h socket.h bsocket.h misc.h
 
-sock_OBJS = list.o socket.o unix.o event.o sync.o select.o wait.o misc.o io.o callback.o bsocket.o
+sock_OBJS = list.o socket.o unix.o event.o sync.o select.o wait.o misc.o io.o callback.o accept.o
 test_OBJS = test.o ${sock_OBJS}
 
 BIN_SUFFIX = .exe

Deleted: bsockets/trunk/bsocket.c
===================================================================
--- bsockets/trunk/bsocket.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/bsocket.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -1,24 +0,0 @@
-#include <stdio.h>
-
-#include "bsocket.h"
-
-
-#ifdef USE_WIN32
-
-int bsocketpair(int domain, int type, int protocol, int fd[2]) {
-
-	return socketpair_win32(domain,type,protocol,fd,__GLOBAL_BSOCKET_ENV_);
-}
-
-#else
-
-int bsocketpair(int domain, int type, int protocol, int fd[2]) {
-
-	return socketpair(domain,type,protocol,fd);
-
-}
-
-#endif
-
-
-

Modified: bsockets/trunk/bsocket.h
===================================================================
--- bsockets/trunk/bsocket.h	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/bsocket.h	2006-08-14 11:45:44 UTC (rev 7055)
@@ -5,10 +5,26 @@
 
 #define USE_WIN32
 
-#ifdef USE_WIN32
+/**************/
+/*user options*/
+/**************/
 
+/*these should be throttled carefully, as too many open half-open sockets
+	or listening sockets wreak havoc on non-server kernels*/
+
 #define MAX_BSOCKETS 2048
 
+#define MAX_SIMUL_CONNECT 		9
+#define MAX_SIMUL_LISTEN  		5
+
+#if ((MAX_SIMUL_CONNECT + MAX_SIMUL_LISTEN + 2 )> WSA_MAXIMUM_WAIT_EVENTS )
+#error "Reduce MAX_SIMUL_CONNECT or MAX_SIMUL_LISTEN"
+#endif
+/*end user options*/
+
+
+#ifdef USE_WIN32
+
 typedef struct _BFD_SET {
 	int hash[MAX_BSOCKETS];
 	int list[MAX_BSOCKETS];
@@ -41,64 +57,65 @@
 						}\
 
 struct socket_env;
-struct socket_env *__GLOBAL_BSOCKET_ENV_;
+extern struct socket_env *__GLOBAL_BSOCKET_ENV_;
 
 int winsock_start();
 
-int socket_win32(int, int, int, struct socket_env *);
-int close_win32(int, struct socket_env *);
-int connect_win32(int, struct sockaddr*,size_t,struct socket_env*);
-int socket_init_win32(struct socket_env*);
-int socket_cleanup_win32(struct socket_env*);
-int fcntl_win32(int,int,long, struct socket_env*);
-int select_win32(int, bfd_set*, bfd_set*, bfd_set*, struct timeval*, struct socket_env*);
-int getsockopt_win32(int, int, int, void*, int *, struct socket_env*);
-int send_win32(int, void*, size_t, int, struct socket_env*);
-int recv_win32(int, void*, size_t, int, struct socket_env*);
-int socketpair_win32(int, int, int, int*, struct socket_env*);
-
-
 #define bsocket_init(X)			socket_init_win32(X)
 #define bsocket_shutdown(X)		socket_cleanup_win32(X)
 
-#define bsocket(A,B,C)			socket_win32(A,B,C,__GLOBAL_BSOCKET_ENV_)
-#define bconnect(A,B,C)			connect_win32(A,B,C,__GLOBAL_BSOCKET_ENV_)
-#define bclose(X)				close_win32(X,__GLOBAL_BSOCKET_ENV_)
+#define bsocket(...)			socket_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define bconnect(...)			connect_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define bclose(...)				close_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
 
-#define bfcntl(X,Y,Z)			fcntl_win32(X,Y,Z,__GLOBAL_BSOCKET_ENV_)
+#define bselect(...)			select_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define bfcntl(...)				fcntl_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
 
-#define bselect(W,X,Y,Z,A)		select_win32(W,X,Y,Z,A,__GLOBAL_BSOCKET_ENV_)
+#define brecv(...)				recv_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define bsend(...)				send_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define bsocketpair(...)		socketpair_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define bgetsockopt(...)		getsockopt_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
 
-#define bgetsockopt(A,B,C,D,E)	getsockopt_win32(A,B,C,D,E,__GLOBAL_BSOCKET_ENV_)
+#define blisten(...)			listen_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define bbind(...)				bind_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
+#define baccept(...)			accept_win32(__VA_ARGS__,__GLOBAL_BSOCKET_ENV_)
 
-#define bsend(A,B,C,D)			send_win32(A,B,C,D,__GLOBAL_BSOCKET_ENV_)
-#define brecv(A,B,C,D)			recv_win32(A,B,C,D,__GLOBAL_BSOCKET_ENV_);
-
-
-int bsocketpair(int,int,int,int*);
-
+#ifndef F_SETFL
 #define F_SETFL					1
+#endif
 
+#ifndef O_NONBLOCK
 #define	O_NONBLOCK				1
+#endif
+
+#ifndef O_BLOCK
 #define O_BLOCK					2
+#endif
 
-/**************/
-/*user options*/
-/**************/
+#else
 
-/*these should be throttled carefully, as too many open half-open sockets
-	or listening sockets wreak havoc on non-server kernels*/
+/*non-win32 stuff*/
+typedef fd_set		bfd_set;
 
-#define MAX_SIMUL_CONNECT 		9
-#define MAX_SIMUL_LISTEN  		5
+#define BFD_CLR		FD_CLR
+#define BFD_SET		FD_SET
+#define BFD_ISSET	FD_ISSET
+#define BFD_ZERO	FD_ZERO
 
-#if ((MAX_SIMUL_CONNECT + MAX_SIMUL_LISTEN + 2 )> WSA_MAXIMUM_WAIT_EVENTS )
-#error "Reduce MAX_SIMUL_CONNECT or MAX_SIMUL_LISTEN"
-#endif
+#define bsocket		socket
+#define bsend		send
+#define brecv		recv
+#define blisten		listen
 
+#define bfcntl		fcntl
+#define bsockgetopt	socketgetopt
 
-#else
+//todo define the rest
+
 #endif
 
+
+
+
 #endif
 

Modified: bsockets/trunk/callback.c
===================================================================
--- bsockets/trunk/callback.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/callback.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -54,9 +54,7 @@
 			if (r == SOCKET_ERROR) {
 
 				if ((err2 = WSAGetLastError()) != WSA_IO_PENDING) {
-
 					socket_exception(msg->fd,unixify_wsaerr(err2),msg->env);
-
 				}
 
 			}
@@ -65,7 +63,9 @@
 
 	} else {
 
-		socket_exception(msg->fd,unixify_wsaerr(err),msg->env);
+		if (err != WSAEINVAL) {
+			socket_exception(msg->fd,unixify_wsaerr(err),msg->env);
+		}
 
 	}
 
@@ -82,10 +82,8 @@
 
 	int r;
 
-	//todo -- do we need to free wo when we're done?
 	msg = (struct _msg*) wo->hEvent;
 
-
 	if (err == 0) {
 
 		ASSERT(len >= 0);
@@ -95,6 +93,8 @@
 		if (len > 0) {
 
 			b = bsocket_get(msg->fd,AS_READ,msg->env);
+			printf("%d %d\n",msg->fd,len);
+			fflush(stdout);
 
 			ASSERT(b != NULL);
 			ASSERT(b->fd == msg->fd);
@@ -128,7 +128,12 @@
 
 	} else {
 
-		socket_exception(msg->fd,unixify_wsaerr(err),msg->env);
+		//if err is WSAINVAL it means that the OVERLAPPED argument has become invalid.
+		//this happens when there is an outstanding read operation when the socket is
+		//deallocated. the best thing to do is nothing.
+		if (err != WSAEINVAL) {
+			socket_exception(msg->fd,unixify_wsaerr(err),msg->env);
+		}
 	}
 
 }
@@ -175,3 +180,4 @@
 
 
 }
+

Modified: bsockets/trunk/event.c
===================================================================
--- bsockets/trunk/event.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/event.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -8,6 +8,7 @@
 #include "select.h"
 #include "callback.h"
 #include "io.h"
+#include "accept.h"
 
 struct _connect_data *connect_data_new(struct sockaddr *name, size_t len) {
 
@@ -71,6 +72,10 @@
 			//nothing to do
 		break;
 
+		case EV_LISTEN:
+			//nothing to do
+		break;
+
 		default:
 			ASSERT(FALSE);
 		break;
@@ -113,8 +118,6 @@
 
 }
 
-//if a bsocket is passed, it is the users responsibility to ensure it remains locked
-//during the event processing
 int event_post(struct bsocket *b, int type, void *data, int *ret, int *err, int wait, struct socket_env *env) {
 
 	HANDLE w;
@@ -207,7 +210,6 @@
 
 }
 
-
 void event_ping(struct socket_env *env) {
 
 	ASSERT( event_post(NULL,EV_PING,NULL,NULL,NULL,TRUE,env) == 0);
@@ -246,8 +248,6 @@
 int post_write(struct bsocket *b, int *ret, int *err, struct socket_env *env) {
 
 	//todo assert is writable
-
-
 	return  event_post(b,EV_WRITE,&b->w_wo,ret,err,TRUE,env);
 
 }
@@ -270,7 +270,12 @@
 
 }
 
+int post_listen(struct bsocket *b, int backlog, int *res, int *err, struct socket_env *env) {
 
+	return event_post (b,EV_LISTEN,(void*) backlog, res,err,TRUE,env);
+
+}
+
 struct _event *event_next(struct socket_env *env) {
 
 	struct _event *e;
@@ -290,9 +295,6 @@
 
 }
 
-//todo -- make this less hideous
-//todo -- is it possible for there to be events related to a socket after closure?
-
 #define E_NOTIFY	(0+WAIT_OBJECT_0)
 #define E_CONNECT	(1+WAIT_OBJECT_0)
 #define E_ACCEPT	(2+WAIT_OBJECT_0)
@@ -341,8 +343,6 @@
 	WSAEVENT we[WSA_MAXIMUM_WAIT_EVENTS];
 	struct __ev ev[WSA_MAXIMUM_WAIT_EVENTS];
 
-	WSABUF *wb;
-
 	WSANETWORKEVENTS ne;
 	WSAOVERLAPPED *wo;
 
@@ -361,10 +361,9 @@
 	int out;
 	int resume;
 	int r,z;
-	int type;
 
-	int x;
 	int connect_count;
+	int listen_count;
 
 	connect_q = NULL;
 
@@ -372,8 +371,8 @@
 
 	resume = TRUE;
 	connect_count = 0;
+	listen_count  = 0;
 
-
 	connect_q = list_new();
 
 	CHECK(connect_q != NULL, 0);
@@ -387,8 +386,6 @@
 
 	}
 
-	x = 0;
-
 	for (i=0; i<MAX_BSOCKETS; i++) {
 		connect_lookup[i] = (SOCKET) -1;
 		listen_lookup[i]  = (SOCKET) -1;
@@ -438,8 +435,6 @@
 						if (e->p_fd == -1)
 							ASSERT(closesocket(e->s) == 0);
 
-
-
 						//todo -- this is a major sync violation, but it feels safe, why?
 						SetEvent(env->b[e->fd]->exception_e);
 
@@ -589,26 +584,38 @@
 
 								}
 
-
-
 							} else {
 
 								//todo -- what is a better errno?
 								event_respond(e,-1,errno);
 
 							}
+						}
 
+					break;
 
+					case EV_LISTEN:
 
+						if (listen_count <= MAX_SIMUL_LISTEN) {
+
+							z = listen(e->s,(int) e->data);
+
+							if (z == 0) {
+
+								socket_raise(e->fd,IS_READABLE,FALSE,env);
+								E_INSERT(e->s,e->fd,FD_ACCEPT);
+								event_respond(e,0,0);
+
+							} else {
+								event_respond(e,-1,unixify_wsaerr(WSAGetLastError()));
+							}
+
+						} else {
+							event_respond(e,-1,ETOOMANY);
 						}
 
 					break;
 
-					case EV_LISTEN:
-						ASSERT(FALSE);
-					break;
-
-
 					default:
 						ASSERT(FALSE);
 					break;
@@ -635,12 +642,32 @@
 
 						case FD_CONNECT:
 							err 	= ne.iErrorCode[FD_CONNECT_BIT];
-							type	= IS_WRITABLE;
+
+							if (err) {
+								complete_connect(ev[i].fd,unixify_wsaerr(err),env);
+							} else {
+								complete_connect(ev[i].fd,0,env);
+							}
+
+							E_DELETE(i);
+							connect_count--;
+
+							ASSERT(event_post(NULL,EV_CONNECT,NULL,NULL,NULL,FALSE,env) == 0);
+
 						break;
 
 						case FD_ACCEPT:
 							err		= ne.iErrorCode[FD_ACCEPT_BIT];
-							type 	= IS_READABLE;
+
+							if (err) {
+								socket_exception(ev[i].fd,unixify_wsaerr(err),env);
+							} else {
+								socket_client_in(ev[i].fd,env);
+							}
+
+							E_DELETE(i);
+							listen_count--;
+
 						break;
 
 						default:
@@ -648,34 +675,17 @@
 						break;
 					}
 
-					if (err) {
 
-						complete_connect(ev[i].fd,unixify_wsaerr(err),env);
-
-					} else {
-
-						complete_connect(ev[i].fd,0,env);
-					}
-
 				} else {
 
+					//complete_connect(ev[i].fd,unixify_wsaerr(err),env);
+					//socket_exception(ev[i].fd,unix);
+					//todo -- rethink this part
+					ASSERT(FALSE);
 
-					complete_connect(ev[i].fd,unixify_wsaerr(err),env);
-
 				}
 
-				if (ev[i].type == FD_CONNECT) {
 
-					//todo -- figure out a way to test the element is being deleted
-					E_DELETE(i);
-
-					//if (connect_count >= MAX_SIMUL_CONNECT) {
-					connect_count--;
-
-					ASSERT(event_post(NULL,EV_CONNECT,NULL,NULL,NULL,FALSE,env) == 0);
-
-				}
-
 			break;
 
 			case WSA_WAIT_FAILED:
@@ -699,14 +709,10 @@
 		ASSERT(WSACloseEvent(we[i]));
 	}
 
-
 	fail:
 
 	if (connect_q != NULL)
 		list_free(connect_q);
 
-
 }
 
-
-

Modified: bsockets/trunk/event.h
===================================================================
--- bsockets/trunk/event.h	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/event.h	2006-08-14 11:45:44 UTC (rev 7055)
@@ -51,5 +51,6 @@
 int post_read(struct bsocket *, struct socket_env *);
 int post_write(struct bsocket *, int*, int*, struct socket_env*);
 int post_close(struct bsocket *, struct socket_env*);
+int post_listen(struct bsocket *, int, int*, int*, struct socket_env*);
 
 #endif

Modified: bsockets/trunk/io.c
===================================================================
--- bsockets/trunk/io.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/io.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -53,7 +53,6 @@
 
 	}
 
-
 	return out;
 
 }
@@ -65,7 +64,6 @@
 
 }
 
-
 void invoke_read(SOCKET s, int fd, WSAOVERLAPPED *wo, struct socket_env *env) {
 
 	struct _msg *msg;
@@ -126,7 +124,6 @@
 
 }
 
-//todo -- check is connected
 int recv_win32(int fd, void *buf, size_t len, int flags, struct socket_env *env) {
 
 	struct bsocket *b;
@@ -156,6 +153,7 @@
 		bsocket_release(fd,AS_READ,env);
 
 		r = wait_until(b,IS_READABLE,env);
+		CHECK(r == 0, 0)
 
 		b = bsocket_get(fd,AS_READ,env);
 
@@ -166,7 +164,6 @@
 	pos			= buf;
 	data_read	= 0;
 
-
 	while (space_left) {
 
 		r = list_queuepeek(&msg,b->in_q);

Modified: bsockets/trunk/io.h
===================================================================
--- bsockets/trunk/io.h	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/io.h	2006-08-14 11:45:44 UTC (rev 7055)
@@ -25,4 +25,7 @@
 struct _msg *msg_new(int, int, void*, struct socket_env*);
 void msg_free(struct _msg *);
 
+int send_win32(int, void*, size_t, int, struct socket_env*);
+int recv_win32(int, void*, size_t, int, struct socket_env*);
+
 #endif

Modified: bsockets/trunk/notes
===================================================================
--- bsockets/trunk/notes	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/notes	2006-08-14 11:45:44 UTC (rev 7055)
@@ -41,5 +41,10 @@
 - OpenSSL uses SO_*TIMEO flags. What should we do about this? Winsock doesn't 
   handle these options, nor can bsocket or any wrapper I can think of. Is this
   even a portability issue? I don't want to lose my dollar to nick over this :)
+- Make sure that private functions are declared as static so they don't cause
+  any collisions
 
 
+
+
+

Modified: bsockets/trunk/select.c
===================================================================
--- bsockets/trunk/select.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/select.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -53,6 +53,7 @@
 
 	l = bsocket_list_get(env);
 
+	//todo -- assert is not null
 	if (l != NULL) {
 
 		if (l[fd] != NULL) {

Modified: bsockets/trunk/select.h
===================================================================
--- bsockets/trunk/select.h	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/select.h	2006-08-14 11:45:44 UTC (rev 7055)
@@ -19,7 +19,10 @@
 void socket_raise(int, int, int, struct socket_env*);
 void socket_exception(int, int, struct socket_env*);
 
+int select_win32(int, bfd_set*, bfd_set*, bfd_set*, struct timeval*, struct socket_env*);
 
+
+
 #define IS_READABLE		(1)
 #define IS_WRITABLE		(2)
 #define IS_EXCEPTED		(4)

Modified: bsockets/trunk/socket.c
===================================================================
--- bsockets/trunk/socket.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/socket.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -15,6 +15,8 @@
 #include "misc.h"
 #include "callback.h"
 
+struct socket_env *__GLOBAL_BSOCKET_ENV_;
+
 //support only turning off and on socket blocking, will add more features as needed
 int fcntl_win32(int fd, int cmd, long args, struct socket_env *env ) {
 
@@ -254,12 +256,10 @@
 	bsocket_free(b);
 
 	//todo out = last error
-
 	fail:
 
 	return out;
 
-
 }
 
 struct bsocket *bsocket_new(SOCKET s) {
@@ -291,6 +291,9 @@
 	b->partner 		= -1;
 	b->excepted     = FALSE;
 
+	b->bound		= FALSE;
+	b->listening	= FALSE;
+
 	b->read_m = CreateMutex(NULL,FALSE,NULL);
 	CHECK_(b->read_m != NULL);
 

Modified: bsockets/trunk/socket.h
===================================================================
--- bsockets/trunk/socket.h	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/socket.h	2006-08-14 11:45:44 UTC (rev 7055)
@@ -60,6 +60,9 @@
 	void *write_pointer;
 	int write_len;
 
+	int bound;
+	int listening;
+
 	/*reading business*/
 	WSAOVERLAPPED r_wo;
 	struct _list *in_q;
@@ -89,5 +92,17 @@
 
 };
 
+int socket_win32(int, int, int, struct socket_env *);
+int close_win32(int, struct socket_env *);
+int connect_win32(int, struct sockaddr*,size_t,struct socket_env*);
+int socket_init_win32(struct socket_env*);
+int socket_cleanup_win32(struct socket_env*);
+int socketpair_win32(int, int, int, int*, struct socket_env*);
+int fcntl_win32(int,int,long, struct socket_env*);
+int getsockopt_win32(int, int, int, void*, int *, struct socket_env*);
 
+struct bsocket *bsocket_new(SOCKET);
+void bsocket_free(struct bsocket *);
+
+
 #endif

Modified: bsockets/trunk/sync.c
===================================================================
--- bsockets/trunk/sync.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/sync.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -27,14 +27,11 @@
 
 void release_fd(int fd, struct socket_env *env) {
 
-	struct bsocket **l;
-
 	ASSERT(fd >= 0);
 	ASSERT(fd < MAX_BSOCKETS);
 
-	l[fd] = NULL;
+	env->b[fd] = NULL;
 
-
 }
 
 

Modified: bsockets/trunk/test.c
===================================================================
--- bsockets/trunk/test.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/test.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -14,6 +14,8 @@
 #include "sync.h"
 #include "wait.h"
 #include "misc.h"
+#include "io.h"
+#include "accept.h"
 
 #define TEST_MSG "hello!"
 
@@ -1625,8 +1627,6 @@
 
 	int mega_tests = 10;
 
-
-
 	while(mega_tests--) {
 
 		TEST(bsocket_init(NULL) == 0);
@@ -1760,10 +1760,82 @@
 	return -1;
 }
 
+int test_accept_helper(int num) {
+
+	struct sockaddr_in in;
+	int r;
+
+	int msg;
+
+	memcpy(&in,&localhost,sizeof(in));
+
+	int fd = bsocket(AF_INET,SOCK_STREAM,0);
+	SILENT_TEST(fd >= 0);
+
+	in.sin_port = htons(82);
+
+	r = bconnect(fd,(struct sockaddr*) &in, sizeof(in));
+	SILENT_TEST(r == 0);
+
+	msg = 666;
+	r = bsend(fd,&msg,sizeof(msg),0);
+	SILENT_TEST(r == sizeof(msg));
+
+	r = brecv(fd,&msg,sizeof(msg),0);
+	SILENT_TEST(r == sizeof(msg));
+	SILENT_TEST(msg == 667);
+
+	SILENT_TEST(bclose(fd) == 0);
+
+	return 0;
+
+}
+
 int test_accept() {
 
-	TEST(FALSE);
+	HANDLE h;
 
+	int fd;
+	int c;
+	int r;
+
+	int msg;
+
+	struct sockaddr_in lh;
+	memcpy(&lh,&localhost,sizeof(lh));
+
+	TEST(bsocket_init(NULL) == 0);
+
+	fd = bsocket(AF_INET,SOCK_STREAM,0);
+	TEST(fd >= 0);
+
+	lh.sin_port = htons(82);
+
+	r = bbind(fd,(struct sockaddr*) &lh,sizeof(lh));
+	TEST(r == 0);
+
+	r = blisten(fd,10);
+	TEST(r == 0);
+
+	h = new_thread(test_accept_helper,NULL);
+	TEST(h != NULL);
+
+	c = baccept(fd,NULL,0);
+	TEST(c >= 0);
+
+	r = brecv(c,&msg,sizeof(msg),0);
+	TEST(r == sizeof(msg));
+	TEST(msg == 666);
+
+	msg = 667;
+	r = bsend(c,&msg,sizeof(msg),0);
+	TEST(r == sizeof(msg));
+
+	r = WaitForSingleObject(h, INFINITE);
+	TEST(r == WAIT_OBJECT_0);
+
+	TEST(bsocket_shutdown(NULL) == 0);
+
 	return 0;
 }
 
@@ -1825,7 +1897,6 @@
 	winsock_start();
 
 	//lookup googles address before starting (we want something that should be available)
-
 	host = gethostbyname("google.com");
 	ASSERT(host != NULL);
 

Modified: bsockets/trunk/unix.c
===================================================================
--- bsockets/trunk/unix.c	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/unix.c	2006-08-14 11:45:44 UTC (rev 7055)
@@ -30,6 +30,7 @@
 		break;
 
 		case WSAEWOULDBLOCK:
+		case WSAEINPROGRESS:
 			out = EINPROGRESS;
 		break;
 
@@ -37,6 +38,18 @@
 			out = ECONNREFUSED;
 		break;
 
+		case WSAEADDRINUSE:
+			out = EINVAL;
+		break;
+
+		case WSAEFAULT:
+			out = EFAULT;
+		break;
+
+		case WSAEISCONN:
+			out = EISCONN;
+		break;
+
 		default:
 			out = err;
 		break;

Modified: bsockets/trunk/unix.h
===================================================================
--- bsockets/trunk/unix.h	2006-08-14 10:16:42 UTC (rev 7054)
+++ bsockets/trunk/unix.h	2006-08-14 11:45:44 UTC (rev 7055)
@@ -69,5 +69,17 @@
 #define	ENOTCONN		(BSOCK_PRIVATE_ERRNO+15)
 #endif
 
+#ifndef ETOOMANY
+#define ETOOMANY		(BSOCK_PRIVATE_ERRNO+16)
+#endif
 
+#ifndef EFAULT
+#define EFAULT			(BSOCK_PRIVATE_ERRNO+17)
 #endif
+
+#ifndef ENFILE
+#define ENFILE			(BSOCK_PRIVATE_ERRNO+18)
+#endif
+
+
+#endif