Skip to content

Commit

Permalink
fix: refactor server connection initialization with improved paramete…
Browse files Browse the repository at this point in the history
…r validation and error handling

Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
  • Loading branch information
liudf0716 committed Nov 14, 2024
1 parent 5c36eca commit 73a8e58
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
70 changes: 56 additions & 14 deletions control.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,25 +682,67 @@ keep_control_alive()
set_ticker_ping_timer(main_ctl->ticker_ping);
}

static void
start_base_connect()
static int init_server_connection(struct bufferevent **bev_out,
struct event_base *base,
const char *server_addr,
int server_port)
{
if (!bev_out || !base || !server_addr) {
debug(LOG_ERR, "Invalid parameters for server connection");
return -1;
}

// Free existing connection if any
if (*bev_out) {
bufferevent_free(*bev_out);
*bev_out = NULL;
}

// Create new connection
*bev_out = connect_server(base, server_addr, server_port);
if (!*bev_out) {
debug(LOG_ERR, "Failed to connect to server [%s:%d]: [%d: %s]",
server_addr, server_port, errno, strerror(errno));
return -1;
}

debug(LOG_INFO, "Connecting to server [%s:%d]...", server_addr, server_port);
return 0;
}

static int setup_server_callbacks(struct bufferevent *bev)
{
if (!bev) {
debug(LOG_ERR, "Invalid bufferevent for callback setup");
return -1;
}

bufferevent_enable(bev, EV_WRITE|EV_READ);
bufferevent_setcb(bev, recv_cb, NULL, connect_event_cb, NULL);
return 0;
}

static void start_base_connect()
{
struct common_conf *c_conf = get_common_config();
if (main_ctl->connect_bev)
bufferevent_free(main_ctl->connect_bev);
if (!c_conf) {
debug(LOG_ERR, "Failed to get common config");
exit(1);
}

main_ctl->connect_bev = connect_server(main_ctl->connect_base,
c_conf->server_addr,
c_conf->server_port);
if ( ! main_ctl->connect_bev) {
debug(LOG_ERR, "error: connect server [%s:%d] failed: [%d: %s]",
c_conf->server_addr, c_conf->server_port, errno, strerror(errno));
exit(0);
// Initialize server connection
if (init_server_connection(&main_ctl->connect_bev,
main_ctl->connect_base,
c_conf->server_addr,
c_conf->server_port) != 0) {
exit(1);
}

debug(LOG_INFO, "connect server [%s:%d]...", c_conf->server_addr, c_conf->server_port);
bufferevent_enable(main_ctl->connect_bev, EV_WRITE|EV_READ);
bufferevent_setcb(main_ctl->connect_bev, recv_cb, NULL, connect_event_cb, NULL);
// Setup callbacks for the connection
if (setup_server_callbacks(main_ctl->connect_bev) != 0) {
bufferevent_free(main_ctl->connect_bev);
exit(1);
}
}

static int prepare_login_message(char **msg_out, int *len_out) {
Expand Down
10 changes: 10 additions & 0 deletions xfrpc.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[common]
server_addr = 82.157.174.2
server_port = 7000
token=abc123

[ssh]
type = tcp
local_ip = 192.168.67.1
local_port = 22
remote_port = 6000

0 comments on commit 73a8e58

Please sign in to comment.