net: don't dynamically allocate hashmaps and lists?
This commit is contained in:
parent
e6721fc10f
commit
ac7c4cd2c5
@ -44,7 +44,6 @@ hashmap_t * net_arp_cache = NULL;
|
|||||||
|
|
||||||
void net_arp_cache_add(struct EthernetDevice * iface, uint32_t addr, uint8_t * hwaddr, uint16_t flags) {
|
void net_arp_cache_add(struct EthernetDevice * iface, uint32_t addr, uint8_t * hwaddr, uint16_t flags) {
|
||||||
spin_lock(net_arp_cache_lock);
|
spin_lock(net_arp_cache_lock);
|
||||||
if (!net_arp_cache) net_arp_cache = hashmap_create_int(10);
|
|
||||||
struct ArpCacheEntry * entry = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr);
|
struct ArpCacheEntry * entry = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr);
|
||||||
if (!entry) entry = malloc(sizeof(struct ArpCacheEntry));
|
if (!entry) entry = malloc(sizeof(struct ArpCacheEntry));
|
||||||
memcpy(entry->hwaddr, hwaddr, 6);
|
memcpy(entry->hwaddr, hwaddr, 6);
|
||||||
@ -55,7 +54,6 @@ void net_arp_cache_add(struct EthernetDevice * iface, uint32_t addr, uint8_t * h
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct ArpCacheEntry * net_arp_cache_get(uint32_t addr) {
|
struct ArpCacheEntry * net_arp_cache_get(uint32_t addr) {
|
||||||
if (!net_arp_cache) return NULL;
|
|
||||||
spin_lock(net_arp_cache_lock);
|
spin_lock(net_arp_cache_lock);
|
||||||
struct ArpCacheEntry * out = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr);
|
struct ArpCacheEntry * out = hashmap_get(net_arp_cache, (void*)(uintptr_t)addr);
|
||||||
spin_unlock(net_arp_cache_lock);
|
spin_unlock(net_arp_cache_lock);
|
||||||
|
@ -170,6 +170,11 @@ static void icmp_handle(struct ipv4_packet * packet, const char * src, const cha
|
|||||||
static hashmap_t * udp_sockets = NULL;
|
static hashmap_t * udp_sockets = NULL;
|
||||||
static hashmap_t * tcp_sockets = NULL;
|
static hashmap_t * tcp_sockets = NULL;
|
||||||
|
|
||||||
|
void ipv4_install(void) {
|
||||||
|
udp_sockets = hashmap_create_int(10);
|
||||||
|
tcp_sockets = hashmap_create_int(10);
|
||||||
|
}
|
||||||
|
|
||||||
#define TCP_FLAGS_FIN (1 << 0)
|
#define TCP_FLAGS_FIN (1 << 0)
|
||||||
#define TCP_FLAGS_SYN (1 << 1)
|
#define TCP_FLAGS_SYN (1 << 1)
|
||||||
#define TCP_FLAGS_RES (1 << 2)
|
#define TCP_FLAGS_RES (1 << 2)
|
||||||
@ -288,7 +293,7 @@ void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) {
|
|||||||
case IPV4_PROT_UDP: {
|
case IPV4_PROT_UDP: {
|
||||||
uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]);
|
uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]);
|
||||||
printf("net: ipv4: %s: %s -> %s udp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
|
printf("net: ipv4: %s: %s -> %s udp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
|
||||||
if (udp_sockets && hashmap_has(udp_sockets, (void*)(uintptr_t)dest_port)) {
|
if (hashmap_has(udp_sockets, (void*)(uintptr_t)dest_port)) {
|
||||||
printf("net: udp: received and have a waiting endpoint!\n");
|
printf("net: udp: received and have a waiting endpoint!\n");
|
||||||
sock_t * sock = hashmap_get(udp_sockets, (void*)(uintptr_t)dest_port);
|
sock_t * sock = hashmap_get(udp_sockets, (void*)(uintptr_t)dest_port);
|
||||||
net_sock_add(sock, packet, ntohs(packet->length));
|
net_sock_add(sock, packet, ntohs(packet->length));
|
||||||
@ -298,7 +303,7 @@ void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) {
|
|||||||
case IPV4_PROT_TCP: {
|
case IPV4_PROT_TCP: {
|
||||||
uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]);
|
uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]);
|
||||||
printf("net: ipv4: %s: %s -> %s tcp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
|
printf("net: ipv4: %s: %s -> %s tcp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
|
||||||
if (tcp_sockets && hashmap_has(tcp_sockets, (void*)(uintptr_t)dest_port)) {
|
if (hashmap_has(tcp_sockets, (void*)(uintptr_t)dest_port)) {
|
||||||
printf("net: tcp: received and have a waiting endpoint!\n");
|
printf("net: tcp: received and have a waiting endpoint!\n");
|
||||||
/* What kind of packet is this? Is it something we were expecting? */
|
/* What kind of packet is this? Is it something we were expecting? */
|
||||||
sock_t * sock = hashmap_get(tcp_sockets, (void*)(uintptr_t)dest_port);
|
sock_t * sock = hashmap_get(tcp_sockets, (void*)(uintptr_t)dest_port);
|
||||||
@ -339,9 +344,6 @@ static int next_port = 12345;
|
|||||||
static int udp_get_port(sock_t * sock) {
|
static int udp_get_port(sock_t * sock) {
|
||||||
spin_lock(udp_port_lock);
|
spin_lock(udp_port_lock);
|
||||||
int out = next_port++;
|
int out = next_port++;
|
||||||
if (!udp_sockets) {
|
|
||||||
udp_sockets = hashmap_create_int(10);
|
|
||||||
}
|
|
||||||
hashmap_set(udp_sockets, (void*)(uintptr_t)out, sock);
|
hashmap_set(udp_sockets, (void*)(uintptr_t)out, sock);
|
||||||
sock->priv[0] = out;
|
sock->priv[0] = out;
|
||||||
spin_unlock(udp_port_lock);
|
spin_unlock(udp_port_lock);
|
||||||
@ -429,7 +431,7 @@ static long sock_udp_recv(sock_t * sock, struct msghdr * msg, int flags) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void sock_udp_close(sock_t * sock) {
|
static void sock_udp_close(sock_t * sock) {
|
||||||
if (sock->priv[0] && udp_sockets) {
|
if (sock->priv[0]) {
|
||||||
printf("udp: removing port %d from bound map\n", sock->priv[0]);
|
printf("udp: removing port %d from bound map\n", sock->priv[0]);
|
||||||
spin_lock(udp_port_lock);
|
spin_lock(udp_port_lock);
|
||||||
hashmap_remove(udp_sockets, (void*)(uintptr_t)sock->priv[0]);
|
hashmap_remove(udp_sockets, (void*)(uintptr_t)sock->priv[0]);
|
||||||
@ -448,7 +450,7 @@ static int udp_socket(void) {
|
|||||||
|
|
||||||
static spin_lock_t tcp_port_lock = {0};
|
static spin_lock_t tcp_port_lock = {0};
|
||||||
static void sock_tcp_close(sock_t * sock) {
|
static void sock_tcp_close(sock_t * sock) {
|
||||||
if (sock->priv[0] && tcp_sockets) {
|
if (sock->priv[0]) {
|
||||||
printf("tcp: removing port %d from bound map\n", sock->priv[0]);
|
printf("tcp: removing port %d from bound map\n", sock->priv[0]);
|
||||||
spin_lock(tcp_port_lock);
|
spin_lock(tcp_port_lock);
|
||||||
hashmap_remove(tcp_sockets, (void*)(uintptr_t)sock->priv[0]);
|
hashmap_remove(tcp_sockets, (void*)(uintptr_t)sock->priv[0]);
|
||||||
@ -501,9 +503,6 @@ static int next_tcp_port = 49152;
|
|||||||
static int tcp_get_port(sock_t * sock) {
|
static int tcp_get_port(sock_t * sock) {
|
||||||
spin_lock(tcp_port_lock);
|
spin_lock(tcp_port_lock);
|
||||||
int out = next_tcp_port++;
|
int out = next_tcp_port++;
|
||||||
if (!tcp_sockets) {
|
|
||||||
tcp_sockets = hashmap_create_int(10);
|
|
||||||
}
|
|
||||||
hashmap_set(tcp_sockets, (void*)(uintptr_t)out, sock);
|
hashmap_set(tcp_sockets, (void*)(uintptr_t)out, sock);
|
||||||
sock->priv[0] = out;
|
sock->priv[0] = out;
|
||||||
spin_unlock(tcp_port_lock);
|
spin_unlock(tcp_port_lock);
|
||||||
|
@ -23,11 +23,16 @@ static hashmap_t * interfaces = NULL;
|
|||||||
extern list_t * net_raw_sockets_list;
|
extern list_t * net_raw_sockets_list;
|
||||||
static fs_node_t * _if_first = NULL;
|
static fs_node_t * _if_first = NULL;
|
||||||
|
|
||||||
|
extern void ipv4_install(void);
|
||||||
|
extern hashmap_t * net_arp_cache;
|
||||||
|
|
||||||
void net_install(void) {
|
void net_install(void) {
|
||||||
/* Set up virtual devices */
|
/* Set up virtual devices */
|
||||||
map_vfs_directory("/dev/net");
|
map_vfs_directory("/dev/net");
|
||||||
interfaces = hashmap_create(10);
|
interfaces = hashmap_create(10);
|
||||||
net_raw_sockets_list = list_create("raw sockets", NULL);
|
net_raw_sockets_list = list_create("raw sockets", NULL);
|
||||||
|
net_arp_cache = hashmap_create_int(10);
|
||||||
|
ipv4_install();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* kinda temporary for now */
|
/* kinda temporary for now */
|
||||||
|
Loading…
Reference in New Issue
Block a user