[net] add the socket interface, fix a bug with events in tcp

This commit is contained in:
Travis Geiselbrecht
2009-07-04 14:38:35 -07:00
parent 0badd12210
commit 25b09037a9
6 changed files with 41 additions and 31 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -227,7 +227,7 @@ int net_init(void)
loopback_init();
udp_init();
tcp_init();
// socket_init();
socket_init();
// net_control_init();
return 0;

View File

@@ -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

View File

@@ -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 <kernel/kernel.h>
#include <kernel/khash.h>
#include <kernel/lock.h>
#include <kernel/heap.h>
#include <kernel/arch/cpu.h>
#include <kernel/net/udp.h>
#include <kernel/net/tcp.h>
#include <kernel/net/socket.h>
#include <debug.h>
#include <err.h>
#include <stdlib.h>
#include <kernel/thread.h>
#include <kernel/mutex.h>
#include <kernel/event.h>
#include <lib/net/misc.h>
#include <lib/net/socket.h>
#include <lib/net/hash.h>
#include <lib/net/udp.h>
#include <lib/net/tcp.h>
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;
}

View File

@@ -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);