|
1 | 1 | #include <kernel.h> |
2 | 2 |
|
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] = {}; |
4 | 9 | spinlock_t udp_lock = 0; |
5 | 10 | #define UDP_MAX_PACKET (65536 + sizeof(udp_packet_t)) |
6 | 11 |
|
@@ -42,29 +47,30 @@ void udp_handle_packet([[maybe_unused]] ip_packet_t* encap_packet, udp_packet_t* |
42 | 47 | void * data_ptr = (void*)packet + sizeof(udp_packet_t); |
43 | 48 | uint32_t data_len = length; |
44 | 49 |
|
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); |
47 | 52 | } |
48 | 53 | } |
49 | 54 |
|
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) { |
51 | 56 |
|
52 | 57 | if (dst_port == 0) { |
53 | 58 | /* Let the OS allocate a port |
54 | 59 | * Walk up the port table, looking for one that doesn't have |
55 | 60 | * any daemon bound to it. If we wrap back around to 0, then |
56 | 61 | * there are no free ports remaining to bind to. |
57 | 62 | */ |
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); |
59 | 64 | if (dst_port == 0) { |
60 | 65 | return 0; |
61 | 66 | } |
62 | 67 | } |
63 | 68 |
|
64 | | - if (daemons[dst_port] != NULL && daemons[dst_port] != handler) { |
| 69 | + if (daemons[dst_port].func != NULL && daemons[dst_port].func != handler) { |
65 | 70 | dprintf("*** BUG *** udp_register_daemon(%d) called twice!\n", dst_port); |
66 | 71 | return 0; |
67 | 72 | } |
68 | | - daemons[dst_port] = handler; |
| 73 | + daemons[dst_port].func = handler; |
| 74 | + daemons[dst_port].opaque = opaque; |
69 | 75 | return dst_port; |
70 | 76 | } |
0 commit comments