From a8a700b6f2e442530345db4a987f2742c0796c9a Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Mon, 19 Sep 2011 00:43:16 +0000 Subject: [PATCH] Added option to change router control port. --- src/common.c | 5 +++++ src/common.h | 1 + src/config.h | 1 + src/ipxconfig.cpp | 51 +++++++++++++++++++++++++++++++++++++++-------- src/router.c | 3 +-- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/common.c b/src/common.c index 69611a7..85d387d 100644 --- a/src/common.c +++ b/src/common.c @@ -98,6 +98,11 @@ DWORD reg_get_bin(const char *val_name, void *buf, DWORD size) { return size; } +DWORD reg_get_dword(const char *val_name, DWORD default_val) { + DWORD buf; + return reg_get_bin(val_name, &buf, sizeof(buf)) == sizeof(buf) ? buf : default_val; +} + void load_dll(unsigned int dllnum) { char path[512]; diff --git a/src/common.h b/src/common.h index 345dce2..183fd2f 100644 --- a/src/common.h +++ b/src/common.h @@ -55,6 +55,7 @@ void reg_close(void); char reg_get_char(const char *val_name, char default_val); DWORD reg_get_bin(const char *val_name, void *buf, DWORD size); +DWORD reg_get_dword(const char *val_name, DWORD default_val); void load_dll(unsigned int dllnum); void unload_dlls(void); diff --git a/src/config.h b/src/config.h index c1f14cb..cd8b251 100644 --- a/src/config.h +++ b/src/config.h @@ -19,6 +19,7 @@ #define IPX_CONFIG_H #define DEFAULT_PORT 54792 +#define DEFAULT_CONTROL_PORT 54793 #define TTL 60 struct reg_value { diff --git a/src/ipxconfig.cpp b/src/ipxconfig.cpp index 14dc528..94634b7 100644 --- a/src/ipxconfig.cpp +++ b/src/ipxconfig.cpp @@ -39,6 +39,7 @@ #define ID_OPT_BCAST 23 #define ID_OPT_FILTER 24 #define ID_OPT_LOG 25 +#define ID_OPT_CPORT 26 #define ID_OK 31 #define ID_CANCEL 32 @@ -61,7 +62,7 @@ struct iface { typedef std::vector iface_list; static void get_nics(); -static bool reg_write(const char *name, void *value, size_t size); +static bool reg_write(const char *name, void *value, size_t size, DWORD type = REG_BINARY); static size_t reg_read(const char *name, void *buf, size_t max_size); static bool save_config(); static bool store_nic(); @@ -110,6 +111,8 @@ static struct { HWND opt_log; HWND opt_port_lbl; HWND opt_port; + HWND opt_cport_lbl; + HWND opt_cport; HWND ok_btn; HWND can_btn; @@ -221,10 +224,10 @@ static LRESULT CALLBACK main_wproc(HWND window, UINT msg, WPARAM wp, LPARAM lp) /* Options groupbox */ - int lbl_w = get_text_width(windows.nic_net_lbl, "UDP port number"); + int lbl_w = get_text_width(windows.nic_net_lbl, "Control port number"); int edit_w = get_text_width(windows.nic_node, "000000"); - int opt_h = 5 * text_h + edit_h + 18; + int opt_h = 5 * text_h + 2 * edit_h + 18; MoveWindow(windows.opt_group, 0, height - opt_h - btn_h - 12, width, opt_h, TRUE); @@ -233,6 +236,11 @@ static LRESULT CALLBACK main_wproc(HWND window, UINT msg, WPARAM wp, LPARAM lp) MoveWindow(windows.opt_port_lbl, 10, y + edge, lbl_w, text_h, TRUE); MoveWindow(windows.opt_port, 15 + lbl_w, y, edit_w, edit_h, TRUE); + y += edit_h + 2; + + MoveWindow(windows.opt_cport_lbl, 10, y + edge, lbl_w, text_h, TRUE); + MoveWindow(windows.opt_cport, 15 + lbl_w, y, edit_w, edit_h, TRUE); + MoveWindow(windows.opt_filter, 10, y += edit_h + 2, width - 20, text_h, TRUE); MoveWindow(windows.opt_bcast, 10, y += text_h + 2, width - 20, text_h, TRUE); MoveWindow(windows.opt_w95, 10, y += text_h + 2, width - 20, text_h, TRUE); @@ -394,7 +402,7 @@ static void get_nics() { delete buf; } -static bool reg_write(const char *name, void *value, size_t size) { +static bool reg_write(const char *name, void *value, size_t size, DWORD type) { if(!regkey) { int err = RegCreateKeyEx( HKEY_CURRENT_USER, @@ -418,7 +426,7 @@ static bool reg_write(const char *name, void *value, size_t size) { } } - int err = RegSetValueEx(regkey, name, 0, REG_BINARY, (BYTE*)value, size); + int err = RegSetValueEx(regkey, name, 0, type, (BYTE*)value, size); if(err != ERROR_SUCCESS) { std::string msg = "Error writing value to registry: " + w32_errmsg(err); MessageBox(NULL, msg.c_str(), "Error", MB_OK | MB_TASKMODAL | MB_ICONERROR); @@ -455,8 +463,8 @@ static bool save_config() { } char port_s[32], *endptr; - GetWindowText(windows.opt_port, port_s, 32); + GetWindowText(windows.opt_port, port_s, 32); int port = strtol(port_s, &endptr, 10); if(port < 1 || port > 65535 || *endptr) { @@ -468,6 +476,18 @@ static bool save_config() { return false; } + GetWindowText(windows.opt_cport, port_s, 32); + int control_port = strtol(port_s, &endptr, 10); + + if(port < 1 || port > 65535 || *endptr) { + MessageBox(windows.main, "Invalid port number.\nPort number must be an integer in the range 1 - 65535", "Error", MB_OK); + + SetFocus(windows.opt_cport); + Edit_SetSel(windows.opt_cport, 0, Edit_GetTextLength(windows.opt_cport)); + + return false; + } + global_conf.udp_port = port; global_conf.w95_bug = Button_GetCheck(windows.opt_w95) == BST_CHECKED; global_conf.bcast_all = Button_GetCheck(windows.opt_bcast) == BST_CHECKED; @@ -491,6 +511,10 @@ static bool save_config() { return false; } + if(!reg_write("control_port", &control_port, sizeof(control_port), REG_DWORD)) { + return false; + } + return true; } @@ -657,11 +681,22 @@ static void init_windows() { windows.opt_port_lbl = create_child(windows.opt_group, 0, 0, 0, 0, "STATIC", "UDP port number", SS_RIGHT); windows.opt_port = create_child(windows.opt_group, 0, 0, 0, 0, "EDIT", "", WS_TABSTOP, WS_EX_CLIENTEDGE, ID_OPT_PORT); - char port_s[8]; - sprintf(port_s, "%hu", global_conf.udp_port); + windows.opt_cport_lbl = create_child(windows.opt_group, 0, 0, 0, 0, "STATIC", "Control port number", SS_RIGHT); + windows.opt_cport = create_child(windows.opt_group, 0, 0, 0, 0, "EDIT", "", WS_TABSTOP, WS_EX_CLIENTEDGE, ID_OPT_CPORT); + char port_s[8]; + + sprintf(port_s, "%hu", global_conf.udp_port); SetWindowText(windows.opt_port, port_s); + DWORD port_buf; + if(reg_read("control_port", &port_buf, sizeof(DWORD)) != sizeof(DWORD) || port_buf > 65535) { + port_buf = DEFAULT_CONTROL_PORT; + } + + sprintf(port_s, "%hu", (uint16_t)port_buf); + SetWindowText(windows.opt_cport, port_s); + windows.opt_w95 = create_child(windows.opt_group, 0, 0, 0, 0, "BUTTON", "Enable Windows 95 SO_BROADCAST bug", BS_AUTOCHECKBOX | WS_TABSTOP, 0, ID_OPT_W95); windows.opt_bcast = create_child(windows.opt_group, 0, 0, 0, 0, "BUTTON", "Send broadcast packets to all subnets", BS_AUTOCHECKBOX | WS_TABSTOP, 0, ID_OPT_BCAST); windows.opt_filter = create_child(windows.opt_group, 0, 0, 0, 0, "BUTTON", "Filter receieved packets by subnet", BS_AUTOCHECKBOX | WS_TABSTOP, 0, ID_OPT_FILTER); diff --git a/src/router.c b/src/router.c index f8c5124..b47c5cc 100644 --- a/src/router.c +++ b/src/router.c @@ -113,9 +113,8 @@ struct router_vars *router_init(BOOL global) { return NULL; } - /* TODO: Use different port number for control socket? */ - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + addr.sin_port = htons(reg_get_dword("control_port", DEFAULT_CONTROL_PORT)); if(bind(router->listener, (struct sockaddr*)&addr, sizeof(addr)) == -1) { log_printf("Failed to bind TCP socket: %s", w32_error(WSAGetLastError()));