[net] add the socket interface, fix a bug with events in tcp
This commit is contained in:
@@ -39,6 +39,7 @@
|
|||||||
#define ERR_NOT_BLOCKED -12
|
#define ERR_NOT_BLOCKED -12
|
||||||
#define ERR_TIMED_OUT -13
|
#define ERR_TIMED_OUT -13
|
||||||
#define ERR_NOT_ALLOWED -14
|
#define ERR_NOT_ALLOWED -14
|
||||||
|
#define ERR_INVALID_HANDLE -15
|
||||||
|
|
||||||
#define ERR_NET_FAILED_ARP -1000
|
#define ERR_NET_FAILED_ARP -1000
|
||||||
#define ERR_NET_ARP_QUEUED -1001
|
#define ERR_NET_ARP_QUEUED -1001
|
||||||
@@ -49,6 +50,6 @@
|
|||||||
#define ERR_NET_ALREADY_CONNECTED -1006
|
#define ERR_NET_ALREADY_CONNECTED -1006
|
||||||
#define ERR_NET_CONNECTION_REFUSED -1007
|
#define ERR_NET_CONNECTION_REFUSED -1007
|
||||||
#define ERR_NET_NOT_LISTENING -1008
|
#define ERR_NET_NOT_LISTENING -1008
|
||||||
#define ERR_NET_NOT_CONNECTED -1008
|
#define ERR_NET_NOT_CONNECTED -1009
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
int net_init(void);
|
int net_init(void);
|
||||||
int net_init_postdev(void);
|
int net_init_postdev(void);
|
||||||
|
|
||||||
#define NET_CHATTY 1
|
#define NET_CHATTY 0
|
||||||
|
|
||||||
/* common net stuff */
|
/* common net stuff */
|
||||||
typedef struct netaddr {
|
typedef struct netaddr {
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ int net_init(void)
|
|||||||
loopback_init();
|
loopback_init();
|
||||||
udp_init();
|
udp_init();
|
||||||
tcp_init();
|
tcp_init();
|
||||||
// socket_init();
|
socket_init();
|
||||||
// net_control_init();
|
// net_control_init();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
LOCAL_DIR := $(GET_LOCAL_DIR)
|
LOCAL_DIR := $(GET_LOCAL_DIR)
|
||||||
|
|
||||||
OBJS += \
|
OBJS += \
|
||||||
$(LOCAL_DIR)/ethernet.o \
|
|
||||||
$(LOCAL_DIR)/arp.o \
|
$(LOCAL_DIR)/arp.o \
|
||||||
$(LOCAL_DIR)/ipv4.o \
|
$(LOCAL_DIR)/cbuf.o \
|
||||||
$(LOCAL_DIR)/udp.o \
|
$(LOCAL_DIR)/ethernet.o \
|
||||||
$(LOCAL_DIR)/tcp.o \
|
$(LOCAL_DIR)/hash.o \
|
||||||
$(LOCAL_DIR)/icmp.o \
|
$(LOCAL_DIR)/icmp.o \
|
||||||
$(LOCAL_DIR)/if.o \
|
$(LOCAL_DIR)/if.o \
|
||||||
|
$(LOCAL_DIR)/ipv4.o \
|
||||||
$(LOCAL_DIR)/loopback.o \
|
$(LOCAL_DIR)/loopback.o \
|
||||||
$(LOCAL_DIR)/misc.o \
|
$(LOCAL_DIR)/misc.o \
|
||||||
$(LOCAL_DIR)/net.o \
|
$(LOCAL_DIR)/net.o \
|
||||||
$(LOCAL_DIR)/net_timer.o \
|
$(LOCAL_DIR)/net_timer.o \
|
||||||
$(LOCAL_DIR)/cbuf.o \
|
|
||||||
$(LOCAL_DIR)/queue.o \
|
$(LOCAL_DIR)/queue.o \
|
||||||
$(LOCAL_DIR)/hash.o
|
$(LOCAL_DIR)/socket.o \
|
||||||
|
$(LOCAL_DIR)/tcp.o \
|
||||||
|
$(LOCAL_DIR)/udp.o
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
** Distributed under the terms of the NewOS License.
|
** 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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files
|
* 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
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include <kernel/kernel.h>
|
#include <debug.h>
|
||||||
#include <kernel/khash.h>
|
#include <err.h>
|
||||||
#include <kernel/lock.h>
|
#include <stdlib.h>
|
||||||
#include <kernel/heap.h>
|
#include <kernel/thread.h>
|
||||||
#include <kernel/arch/cpu.h>
|
#include <kernel/mutex.h>
|
||||||
#include <kernel/net/udp.h>
|
#include <kernel/event.h>
|
||||||
#include <kernel/net/tcp.h>
|
#include <lib/net/misc.h>
|
||||||
#include <kernel/net/socket.h>
|
#include <lib/net/socket.h>
|
||||||
|
#include <lib/net/hash.h>
|
||||||
|
#include <lib/net/udp.h>
|
||||||
|
#include <lib/net/tcp.h>
|
||||||
|
|
||||||
typedef struct netsocket {
|
typedef struct netsocket {
|
||||||
struct netsocket *next;
|
struct netsocket *next;
|
||||||
@@ -41,7 +44,7 @@ typedef struct netsocket {
|
|||||||
} netsocket;
|
} netsocket;
|
||||||
|
|
||||||
static netsocket *sock_table;
|
static netsocket *sock_table;
|
||||||
static mutex sock_mutex;
|
static mutex_t sock_mutex;
|
||||||
static sock_id next_sock_id;
|
static sock_id next_sock_id;
|
||||||
|
|
||||||
static int sock_compare_func(void *_s, const void *_key)
|
static int sock_compare_func(void *_s, const void *_key)
|
||||||
@@ -71,9 +74,9 @@ static netsocket *lookup_socket(sock_id id)
|
|||||||
netsocket *s;
|
netsocket *s;
|
||||||
|
|
||||||
// pull it from in the socket list
|
// pull it from in the socket list
|
||||||
mutex_lock(&sock_mutex);
|
mutex_acquire(&sock_mutex);
|
||||||
s = hash_lookup(sock_table, &id);
|
s = hash_lookup(sock_table, &id);
|
||||||
mutex_unlock(&sock_mutex);
|
mutex_release(&sock_mutex);
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
@@ -83,7 +86,7 @@ static netsocket *create_netsocket(int type, void *prot_data)
|
|||||||
netsocket *s;
|
netsocket *s;
|
||||||
|
|
||||||
// create the net socket
|
// create the net socket
|
||||||
s = kmalloc(sizeof(netsocket));
|
s = malloc(sizeof(netsocket));
|
||||||
if(!s)
|
if(!s)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -93,20 +96,20 @@ static netsocket *create_netsocket(int type, void *prot_data)
|
|||||||
s->type = type;
|
s->type = type;
|
||||||
|
|
||||||
// insert it in the socket list
|
// insert it in the socket list
|
||||||
mutex_lock(&sock_mutex);
|
mutex_acquire(&sock_mutex);
|
||||||
hash_insert(sock_table, s);
|
hash_insert(sock_table, s);
|
||||||
mutex_unlock(&sock_mutex);
|
mutex_release(&sock_mutex);
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int destroy_netsocket(netsocket *s)
|
static int destroy_netsocket(netsocket *s)
|
||||||
{
|
{
|
||||||
mutex_lock(&sock_mutex);
|
mutex_acquire(&sock_mutex);
|
||||||
hash_remove(sock_table, s);
|
hash_remove(sock_table, s);
|
||||||
mutex_unlock(&sock_mutex);
|
mutex_release(&sock_mutex);
|
||||||
|
|
||||||
kfree(s);
|
free(s);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -343,13 +346,13 @@ int socket_init(void)
|
|||||||
{
|
{
|
||||||
next_sock_id = 0;
|
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);
|
sock_table = hash_init(256, offsetof(netsocket, next), &sock_compare_func, &sock_hash_func);
|
||||||
if(!sock_table)
|
if(!sock_table)
|
||||||
return ERR_NO_MEMORY;
|
return ERR_NO_MEMORY;
|
||||||
|
|
||||||
socket_dev_init();
|
// socket_dev_init();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -927,13 +927,17 @@ retry:
|
|||||||
|
|
||||||
// pull something from the head of the accept queue
|
// pull something from the head of the accept queue
|
||||||
new_socket = queue_dequeue(&s->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
|
// see if that was the last accepted socket and unsignal the event
|
||||||
if (s->accept_queue.count == 0)
|
if (s->accept_queue.count == 0)
|
||||||
event_unsignal(&s->accept_event);
|
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
|
// we have the new socket, make sure it's ready to go
|
||||||
mutex_release(&s->lock);
|
mutex_release(&s->lock);
|
||||||
event_wait(&new_socket->read_event);
|
event_wait(&new_socket->read_event);
|
||||||
|
|||||||
Reference in New Issue
Block a user