Skip to content

Commit 7633c99

Browse files
allow passing void opaque into a udp server handler
1 parent ed1d689 commit 7633c99

7 files changed

Lines changed: 25 additions & 16 deletions

File tree

include/udp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "kernel.h"
99

1010
// Interrupt handler definition
11-
typedef void (*udp_daemon_handler)(uint32_t, uint16_t, uint16_t, void*, uint32_t);
11+
typedef void (*udp_daemon_handler)(uint32_t, uint16_t, uint16_t, void*, uint32_t, void* opaque);
1212

1313
/**
1414
* @brief Raw structure for UDP
@@ -55,6 +55,7 @@ void udp_handle_packet([[maybe_unused]] ip_packet_t* encap_packet, udp_packet_t*
5555
* @param dst_port destination port to listen on. If 0 is passed, a random port above or equal to 1024
5656
* is allocated for use and will be returned as the return value.
5757
* @param handler handler for incoming packets
58+
* @param opaque Opaque pointer passed to the handler
5859
* @return port number that was allocated
5960
*/
60-
uint16_t udp_register_daemon(uint16_t dst_port, udp_daemon_handler handler);
61+
uint16_t udp_register_daemon(uint16_t dst_port, udp_daemon_handler handler, void* opaque);

src/basic/sockets.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,5 @@ void sockwrite_statement(struct basic_ctx* ctx)
241241
}
242242
}
243243

244+
static void basic_udp_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, void* data, uint32_t length, void* opaque) {
245+
}

src/debugger.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void gdb_data(uint32_t src_ip, uint16_t src_port, uint8_t* data, uint32_t length
198198
}
199199
}
200200

201-
void debug_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, void* data, uint32_t length)
201+
void debug_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, void* data, uint32_t length, void* opaque)
202202
{
203203
uint8_t identifier = *(uint8_t*)data;
204204
switch (identifier) {
@@ -359,7 +359,7 @@ void init_debug()
359359
setforeground(COLOUR_WHITE);
360360
kprintf("(%ld bytes)\n", filesize);
361361

362-
udp_register_daemon(DEBUG_DST_PORT, &debug_handle_packet);
362+
udp_register_daemon(DEBUG_DST_PORT, &debug_handle_packet, NULL);
363363
}
364364

365365
const char* findsymbol(uint64_t address, uint64_t* offset) {

src/net/dhcp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <kernel.h>
22

3-
void dhcp_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, void* data, [[maybe_unused]] uint32_t length) {
3+
void dhcp_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, void* data, uint32_t length, void* opaque) {
44
dhcp_packet_t* packet = (dhcp_packet_t*)data;
55
if(packet->op == DHCP_REPLY) {
66
uint8_t* type = get_dhcp_options(packet, OPT_TYPE);
@@ -48,7 +48,7 @@ void dhcp_discover() {
4848
uint8_t request_ip[4] = { 0, 0, 0, 0 };
4949
uint8_t dst_ip[4] = { 0xff, 0xff, 0xff, 0xff };
5050
dhcp_packet_t packet;
51-
udp_register_daemon(DHCP_DST_PORT, &dhcp_handle_packet);
51+
udp_register_daemon(DHCP_DST_PORT, &dhcp_handle_packet, NULL);
5252
memset(&packet, 0, sizeof(dhcp_packet_t));
5353
uint16_t optsize = make_dhcp_packet(&packet, DHCPDISCOVER, request_ip, DHCP_TRANSACTION_IDENTIFIER, 0);
5454
udp_send_packet(dst_ip, DHCP_DST_PORT, DHCP_SRC_PORT, &packet, optsize);

src/net/dns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static int dns_make_payload(const char * const name, const uint8_t rr, const uns
168168
* @param data raw packet data
169169
* @param length length of packet
170170
*/
171-
void dns_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, void* data, uint32_t length) {
171+
void dns_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, void* data, uint32_t length, void* opaque) {
172172
dns_header_t* packet = (dns_header_t*)data;
173173
uint16_t inbound_id = ntohs(packet->id);
174174
dns_request_t findrequest = { .id = inbound_id };
@@ -534,7 +534,7 @@ void init_dns()
534534
dns_replies = hashmap_new(sizeof(dns_request_t), 0, 6453563734, 7645356235, dns_request_hash, dns_request_compare, NULL, NULL);
535535
dns_cache = hashmap_new(sizeof(dns_cache_entry_t), 0, 6453563734, 7645356235, dns_cache_hash, dns_cache_compare, NULL, NULL);
536536
/* Let the IP stack decide on the port number to use */
537-
dns_query_port = udp_register_daemon(0, &dns_handle_packet);
537+
dns_query_port = udp_register_daemon(0, &dns_handle_packet, NULL);
538538
if (dns_query_port == 0) {
539539
kprintf("Could not bind DNS port! DNS queries will not resolve.\n");
540540
}

src/net/net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ uint32_t ntohl(uint32_t netlong) {
9595

9696
void network_up()
9797
{
98-
kprintf(" arp,");
98+
kprintf(" ARP,");
9999
arp_init();
100100
kprintf(" IP,");
101101
ip_init();

src/net/udp.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include <kernel.h>
22

3-
static udp_daemon_handler daemons[USHRT_MAX] = { 0 };
3+
typedef struct {
4+
udp_daemon_handler func;
5+
void* opaque;
6+
} udp_daemon_registration;
7+
8+
static udp_daemon_registration daemons[USHRT_MAX] = {};
49
spinlock_t udp_lock = 0;
510
#define UDP_MAX_PACKET (65536 + sizeof(udp_packet_t))
611

@@ -42,29 +47,30 @@ void udp_handle_packet([[maybe_unused]] ip_packet_t* encap_packet, udp_packet_t*
4247
void * data_ptr = (void*)packet + sizeof(udp_packet_t);
4348
uint32_t data_len = length;
4449

45-
if (daemons[dst_port] != NULL) {
46-
daemons[dst_port](src_ip, src_port, dst_port, data_ptr, data_len);
50+
if (daemons[dst_port].func != NULL) {
51+
daemons[dst_port].func(src_ip, src_port, dst_port, data_ptr, data_len, daemons[dst_port].opaque);
4752
}
4853
}
4954

50-
uint16_t udp_register_daemon(uint16_t dst_port, udp_daemon_handler handler) {
55+
uint16_t udp_register_daemon(uint16_t dst_port, udp_daemon_handler handler, void* opaque) {
5156

5257
if (dst_port == 0) {
5358
/* Let the OS allocate a port
5459
* Walk up the port table, looking for one that doesn't have
5560
* any daemon bound to it. If we wrap back around to 0, then
5661
* there are no free ports remaining to bind to.
5762
*/
58-
for(dst_port = 1024; daemons[dst_port] != NULL && dst_port != 0; ++dst_port);
63+
for(dst_port = 1024; daemons[dst_port].func != NULL && dst_port != 0; ++dst_port);
5964
if (dst_port == 0) {
6065
return 0;
6166
}
6267
}
6368

64-
if (daemons[dst_port] != NULL && daemons[dst_port] != handler) {
69+
if (daemons[dst_port].func != NULL && daemons[dst_port].func != handler) {
6570
dprintf("*** BUG *** udp_register_daemon(%d) called twice!\n", dst_port);
6671
return 0;
6772
}
68-
daemons[dst_port] = handler;
73+
daemons[dst_port].func = handler;
74+
daemons[dst_port].opaque = opaque;
6975
return dst_port;
7076
}

0 commit comments

Comments
 (0)