#include #define OPEN_PORT_FUNCTION print_open #define CLOSE_PORT_FUNCTION print_close #define CONNECT_PORT_FUNCTION print_connect void handle_open_port(unsigned int x, void *(*open_port)(unsigned int port)); void handle_close_port(unsigned int x, void *(*close_port)(unsigned int port)); void handle_connect_to_port(unsigned int x, void *(*connect_to_port)(unsigned int port)); void print_open(unsigned int x); void print_close(unsigned int x); void print_connect(unsigned int x); #define PREVIOUS_FRAME(x, y) *(unsigned int *)(x + y) long gap_between_x = 0; void kernel_recursion(unsigned int x) { long current_offset = gap_between_x - (long)(char *)&x; gap_between_x = (long)(char *)&x; unsigned int previous_x = PREVIOUS_FRAME((char *)&x, current_offset); if (x < previous_x) { handle_open_port(x, (void*(*)(unsigned int))OPEN_PORT_FUNCTION); ++x; kernel_recursion(previous_x); --x; handle_close_port(x, (void*(*)(unsigned int))CLOSE_PORT_FUNCTION); } else if (previous_x < x) { handle_open_port(previous_x, (void*(*)(unsigned int))OPEN_PORT_FUNCTION); kernel_recursion(previous_x + 1); --x; handle_close_port(previous_x, (void*(*)(unsigned int))CLOSE_PORT_FUNCTION); } else { handle_open_port(previous_x, (void*(*)(unsigned int))OPEN_PORT_FUNCTION); handle_close_port(previous_x, (void*(*)(unsigned int))CLOSE_PORT_FUNCTION); } } void kernel(unsigned int x) { gap_between_x = (long)&x; kernel_recursion(0); } int main() { kernel(22000); } void handle_open_port(unsigned int x, void *(*open_port)(unsigned int port)) { open_port(x); } void handle_close_port(unsigned int x, void *(*close_port)(unsigned int port)) { handle_connect_to_port(x, (void*(*)(unsigned int))CONNECT_PORT_FUNCTION); close_port(x); } void handle_connect_to_port(unsigned int x, void *(*connect_to_port)(unsigned int port)) { connect_to_port(x); } void print_open(unsigned int x) { printf("Opened port %u\n", x); } void print_close(unsigned int x) { printf("Closed port %u\n", x); } void print_connect(unsigned int x) { printf("Connected port %u\n", x); }