void tc_intercept_release_tunnel(int fd, tc_event_t *rev) { if (!srv_settings.tunnel[fd].fd_valid) { tc_log_info(LOG_WARN, 0, "already released, fd:%d", fd); return; } tc_log_info(LOG_NOTICE, 0, "release tunnel related resources, fd:%d", fd); tc_socket_close(fd); srv_settings.tunnel[fd].fd_valid = 0; #if (INTERCEPT_COMBINED) if (srv_settings.tunnel[fd].combined != NULL) { free(srv_settings.tunnel[fd].combined); srv_settings.tunnel[fd].combined = NULL; } else { tc_log_info(LOG_NOTICE, 0, "crazy here, combined is null, fd:%d", fd); } #endif if (rev == NULL) { if (srv_settings.tunnel[fd].ev == NULL) { tc_log_info(LOG_NOTICE, 0, "crazy here, ev is null, fd:%d", fd); } else { tc_event_del(srv_settings.tunnel[fd].ev->loop, srv_settings.tunnel[fd].ev, TC_EVENT_READ); tc_event_destroy(srv_settings.tunnel[fd].ev, 1); srv_settings.tunnel[fd].ev = NULL; } } else { tc_event_del(rev->loop, rev, TC_EVENT_READ); } }
int tc_event_proc_cycle(tc_event_loop_t *loop) { int ret; long timeout; tc_msec_t delta; tc_event_t *act_event, *act_next; tc_event_actions_t *actions; actions = loop->actions; for ( ;; ) { timeout = tc_event_find_timer(); if (timeout == 0 || timeout > 1000) { timeout = 500; } loop->active_events = NULL; delta = tc_current_time_msec; ret = actions->poll(loop, timeout); if (tc_over) { goto FINISH; } tc_time_update(); delta = tc_current_time_msec - delta; if (delta) { tc_event_expire_timers(); } if (ret == TC_EVENT_ERROR || ret == TC_EVENT_AGAIN) { continue; } for (act_event = loop->active_events; act_event; act_event = act_next) { act_next = act_event->next; if (act_event->events & TC_EVENT_READ) { if (act_event->read_handler(act_event) == TC_ERR_EXIT) { goto FINISH; } } if (act_event->events & TC_EVENT_WRITE) { if (act_event->write_handler(act_event) == TC_ERR_EXIT) { goto FINISH; } } if (act_event->reg_evs == TC_EVENT_NONE) { tc_event_destroy(act_event, 0); } } } FINISH: return TC_EVENT_OK; }
static int connect_to_server(tc_event_loop_t *ev_lp) { int i, j, fd; uint32_t target_ip; conns_t *conns; uint16_t target_port; for (i = 0; i < clt_settings.real_servers.num; i++) { conns = &(clt_settings.real_servers.conns[i]); target_ip = conns[i].ip; target_port = conns[i].port; if (target_port == 0) { target_port = clt_settings.srv_port; } if (conns[i].active != 0) { continue; } for (j = 0; j < conns->num; j++) { fd = conns->fds[j]; if (fd > 0) { tc_log_info(LOG_NOTICE, 0, "it close socket:%d", fd); tc_socket_close(fd); tc_event_del(clt_settings.ev[fd]->loop, clt_settings.ev[fd], TC_EVENT_READ); tc_event_destroy(clt_settings.ev[fd], 0); conns->fds[j] = -1; } } clt_settings.real_servers.conns[i].num = 0; clt_settings.real_servers.conns[i].remained_num = 0; for (j = 0; j < clt_settings.par_conns; j++) { fd = tc_message_init(ev_lp, target_ip, target_port); if (fd == TC_INVALID_SOCK) { return TC_ERR; } if (!send_version(fd)) { return TC_ERR; } if (j == 0) { clt_settings.real_servers.active_num++; conns[i].active = 1; } clt_settings.real_servers.conns[i].fds[j] = fd; clt_settings.real_servers.conns[i].num++; clt_settings.real_servers.conns[i].remained_num++; } } return TC_OK; }
static int connect_to_server(tc_event_loop_t *event_loop) { int i, j, fd; uint32_t target_ip; uint16_t target_port; connections_t *connections; /* * add connections to the real servers for sending router info * and receiving response packet */ for (i = 0; i < clt_settings.real_servers.num; i++) { target_ip = clt_settings.real_servers.ips[i]; target_port = clt_settings.real_servers.ports[i]; if (target_port == 0) { target_port = clt_settings.srv_port; } if (clt_settings.real_servers.active[i] != 0) { continue; } connections = &(clt_settings.real_servers.connections[i]); for (j = 0; j < connections->num; j++) { fd = connections->fds[j]; if (fd > 0) { tc_log_info(LOG_NOTICE, 0, "it close socket:%d", fd); tc_socket_close(fd); tc_event_del(clt_settings.ev[fd]->loop, clt_settings.ev[fd], TC_EVENT_READ); tc_event_destroy(clt_settings.ev[fd]); connections->fds[j] = -1; } } clt_settings.real_servers.connections[i].num = 0; clt_settings.real_servers.connections[i].remained_num = 0; for (j = 0; j < clt_settings.par_connections; j++) { fd = tc_message_init(event_loop, target_ip, target_port); if (fd == TC_INVALID_SOCKET) { return TC_ERROR; } if (!send_version(fd)) { return TC_ERROR; } if (j == 0) { clt_settings.real_servers.active_num++; clt_settings.real_servers.active[i] = 1; } clt_settings.real_servers.connections[i].fds[j] = fd; clt_settings.real_servers.connections[i].num++; clt_settings.real_servers.connections[i].remained_num++; } tc_log_info(LOG_NOTICE, 0, "add dr tunnels for exchanging info:%u:%u", target_ip, target_port); } return TC_OK; }