diff --git a/include/err.h b/include/err.h index d642dc7e..72cf04f8 100644 --- a/include/err.h +++ b/include/err.h @@ -39,6 +39,7 @@ #define ERR_NOT_BLOCKED -12 #define ERR_TIMED_OUT -13 #define ERR_NOT_ALLOWED -14 +#define ERR_INVALID_HANDLE -15 #define ERR_NET_FAILED_ARP -1000 #define ERR_NET_ARP_QUEUED -1001 @@ -49,6 +50,6 @@ #define ERR_NET_ALREADY_CONNECTED -1006 #define ERR_NET_CONNECTION_REFUSED -1007 #define ERR_NET_NOT_LISTENING -1008 -#define ERR_NET_NOT_CONNECTED -1008 +#define ERR_NET_NOT_CONNECTED -1009 #endif diff --git a/include/lib/net.h b/include/lib/net.h index d95c1bc1..01cc28aa 100644 --- a/include/lib/net.h +++ b/include/lib/net.h @@ -32,7 +32,7 @@ int net_init(void); int net_init_postdev(void); -#define NET_CHATTY 1 +#define NET_CHATTY 0 /* common net stuff */ typedef struct netaddr { diff --git a/lib/net/net.c b/lib/net/net.c index 6ad2dfcb..7c9c8683 100644 --- a/lib/net/net.c +++ b/lib/net/net.c @@ -227,7 +227,7 @@ int net_init(void) loopback_init(); udp_init(); tcp_init(); -// socket_init(); + socket_init(); // net_control_init(); return 0; diff --git a/lib/net/rules.mk b/lib/net/rules.mk index 94b9a85e..593595e1 100644 --- a/lib/net/rules.mk +++ b/lib/net/rules.mk @@ -1,17 +1,19 @@ LOCAL_DIR := $(GET_LOCAL_DIR) OBJS += \ - $(LOCAL_DIR)/ethernet.o \ $(LOCAL_DIR)/arp.o \ - $(LOCAL_DIR)/ipv4.o \ - $(LOCAL_DIR)/udp.o \ - $(LOCAL_DIR)/tcp.o \ + $(LOCAL_DIR)/cbuf.o \ + $(LOCAL_DIR)/ethernet.o \ + $(LOCAL_DIR)/hash.o \ $(LOCAL_DIR)/icmp.o \ $(LOCAL_DIR)/if.o \ + $(LOCAL_DIR)/ipv4.o \ $(LOCAL_DIR)/loopback.o \ $(LOCAL_DIR)/misc.o \ $(LOCAL_DIR)/net.o \ $(LOCAL_DIR)/net_timer.o \ - $(LOCAL_DIR)/cbuf.o \ $(LOCAL_DIR)/queue.o \ - $(LOCAL_DIR)/hash.o + $(LOCAL_DIR)/socket.o \ + $(LOCAL_DIR)/tcp.o \ + $(LOCAL_DIR)/udp.o + diff --git a/lib/net/socket.c b/lib/net/socket.c index 5d1e106f..029d4dc7 100644 --- a/lib/net/socket.c +++ b/lib/net/socket.c @@ -3,7 +3,7 @@ ** Distributed under the terms of the NewOS License. */ /* - * Copyright (c) 2008 Travis Geiselbrecht + * Copyright (c) 2008-2009 Travis Geiselbrecht * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files @@ -24,14 +24,17 @@ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include typedef struct netsocket { struct netsocket *next; @@ -41,7 +44,7 @@ typedef struct netsocket { } netsocket; static netsocket *sock_table; -static mutex sock_mutex; +static mutex_t sock_mutex; static sock_id next_sock_id; static int sock_compare_func(void *_s, const void *_key) @@ -71,9 +74,9 @@ static netsocket *lookup_socket(sock_id id) netsocket *s; // pull it from in the socket list - mutex_lock(&sock_mutex); + mutex_acquire(&sock_mutex); s = hash_lookup(sock_table, &id); - mutex_unlock(&sock_mutex); + mutex_release(&sock_mutex); return s; } @@ -83,7 +86,7 @@ static netsocket *create_netsocket(int type, void *prot_data) netsocket *s; // create the net socket - s = kmalloc(sizeof(netsocket)); + s = malloc(sizeof(netsocket)); if(!s) return NULL; @@ -93,20 +96,20 @@ static netsocket *create_netsocket(int type, void *prot_data) s->type = type; // insert it in the socket list - mutex_lock(&sock_mutex); + mutex_acquire(&sock_mutex); hash_insert(sock_table, s); - mutex_unlock(&sock_mutex); + mutex_release(&sock_mutex); return s; } static int destroy_netsocket(netsocket *s) { - mutex_lock(&sock_mutex); + mutex_acquire(&sock_mutex); hash_remove(sock_table, s); - mutex_unlock(&sock_mutex); + mutex_release(&sock_mutex); - kfree(s); + free(s); return 0; } @@ -343,13 +346,13 @@ int socket_init(void) { next_sock_id = 0; - mutex_init(&sock_mutex, "socket list mutex"); + mutex_init(&sock_mutex); sock_table = hash_init(256, offsetof(netsocket, next), &sock_compare_func, &sock_hash_func); if(!sock_table) return ERR_NO_MEMORY; - socket_dev_init(); +// socket_dev_init(); return 0; } diff --git a/lib/net/tcp.c b/lib/net/tcp.c index 142dc5d2..8df4b7ba 100644 --- a/lib/net/tcp.c +++ b/lib/net/tcp.c @@ -927,13 +927,17 @@ retry: // pull something from the head of the accept queue new_socket = queue_dequeue(&s->accept_queue); - ASSERT(new_socket != NULL); - ASSERT(new_socket->ref_count > 0); // see if that was the last accepted socket and unsignal the event if (s->accept_queue.count == 0) event_unsignal(&s->accept_event); + // see if we may have raced another thread into here and ended up with an empty queue + if (!new_socket) + goto retry; + + ASSERT(new_socket->ref_count > 0); + // we have the new socket, make sure it's ready to go mutex_release(&s->lock); event_wait(&new_socket->read_event);