From 2c85fb5c8c37972d9ab60b41dc88a159b4e114b0 Mon Sep 17 00:00:00 2001 From: abhijitm Date: Sat, 30 Jul 2016 23:03:38 +0530 Subject: [PATCH 1/2] Sat Jul 30 23:03:38 IST 2016: traslated most of the chinese to english --- .cproject | 55 ++++++++++++++++++++++++++ .project | 27 +++++++++++++ client.c | 104 ++++++++++++++++++++++++------------------------ mptunnel.c | 114 ++++++++++++++++++++++++++--------------------------- mptunnel.h | 18 ++++----- net.c | 28 ++++++------- server.c | 66 +++++++++++++++---------------- server.h | 4 +- 8 files changed, 249 insertions(+), 167 deletions(-) create mode 100644 .cproject create mode 100644 .project diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..4f721a7 --- /dev/null +++ b/.cproject @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..2ca3130 --- /dev/null +++ b/.project @@ -0,0 +1,27 @@ + + + mptunnel + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/client.c b/client.c index 0e7f0d4..3f2b078 100644 --- a/client.c +++ b/client.c @@ -42,15 +42,15 @@ extern int g_config_encrypt; /** - * 用于保护 ev_io 的锁 - * 在调用 reconnect_to_server 时,我们需要销毁旧的 ev_io,但此时对应的 ev_io 可能正在其回调处理函数(recv_remote_callback)中, - * 如果这时候 free 掉 ev_io,就会出现内存访问错误,因此,我们使用此锁来保护,不要在 recv_remote_callback 正在执行的时候删除 ev_io - */ +* For the protection of ev_io lock +* In call reconnect_to_server, we need to destroy the old ev_io, but in this case the corresponding ev_io may be its callback processing function(recv_remote_callback), +* If this time free off ev_io, there will be memory access errors, and therefore, we use this lock to protect, not in the recv_remote_callback being executed when the Delete ev_io +*/ static pthread_mutex_t g_ev_io_w_mutex = PTHREAD_MUTEX_INITIALIZER; /** - * ev 处理线程 - */ +* ev processing thread +*/ void* ev_thread(void* ptr) { LOGD(_("Starting libev thread\n")); @@ -109,8 +109,8 @@ int main(int argc, char** argv) { /** - * 收到远程桥发来的数据时的处理函数 - */ +* Receive remote Bridge is sent to the data processing function +*/ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { char* buf; int buflen = 65536; @@ -119,9 +119,9 @@ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { buf = malloc(buflen); - //memset(buf, 0x00, buflen); /// 为了提升效率,不再初始化接收缓存 + //memset(buf, 0x00, buflen); /// in order to enhance the efficiency, no longer initialize the receive buffer - /// 查找对应的连接对象 + /// Find the corresponding connection object struct list_head *pos; list_for_each(pos, &g_connections) { conn = list_entry(pos, connections_t, list); @@ -149,26 +149,26 @@ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { LOGW(_("Error while receive data, remote bridge %s:%d (fd=%d) may close the connection(errno=%d): %s\n"), conn->host, conn->port, w->fd, errno, strerror(errno)); free(buf); - /// 实验性修改:EWOULDBLOCK 也断开,因为长期没有收到数据包时,我们需要主动断开链接,这时另一个线程会给这个回调喂一个事件,但此时系统认为连接还是正常的,所以会返回 EWOULDBLOCK + /// Experimental modifications: EWOULDBLOCK is also turned off, because long-term did not receive a packet, we need to take the initiative to disconnect the link, then another thread will give this a callback feed of an event, but in this case the system considers the connection is still normal, it will return EWOULDBLOCK //if ((errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) || conn->broken == 1) { if (errno != EINTR) { struct list_head *pos; - /// 仅当链接断开的时候才会发生 EWOULDBLOCK 事件 + /// Only when the link is disconnected when it will happen EWOULDBLOCK event if ((errno == EWOULDBLOCK || errno == EAGAIN) && conn->broken == 1) { LOGI(_("The connection is marked broken, try restart connection to %s:%d(fd=%d),errno=%d: %s\n"), conn->host, conn->port, w->fd, errno, strerror(errno)); - //LOGI("因为连接被标记为断开的,尝试重新启动到远程桥 %s:%d (fd=%d)的连接, errno=%d: %s\n", conn->host, conn->port, w->fd, errno, strerror(errno)); + //LOGI("because the connection is marked as disconnected, try to restart to the remote Bridge %s:%d (fd=%d), errno=%d: %s\n", conn->host, conn->port, w->fd, errno, strerror(errno)); } else { LOGI(_("Network broken, try to restart connection to %s:%d(fd=%d), errno=%d: %s\n"), conn->host, conn->port, w->fd, errno, strerror(errno)); - //LOGI("因为网络中断,尝试重新启动到远程桥 %s:%d (fd=%d)的连接, errno=%d: %s\n", conn->host, conn->port, w->fd, errno, strerror(errno)); + //LOGI("because the network interruption, try to restart to the remote Bridge %s:%d (fd=%d), errno=%d: %s\n", conn->host, conn->port, w->fd, errno, strerror(errno)); } list_for_each(pos, &g_connections) { conn = list_entry(pos, connections_t, list); if (conn->fd == w->fd) { LOGI("About to re-establish connection to %s:%d\n", conn->host, conn->port); - //LOGI("将要重新启动到远程桥 %s:%d 的连接\n", conn->host, conn->port); + //LOGI("will restart to the remote Bridge %s:%d connected\n", conn->host, conn->port); reconnect_to_server(conn); break; } @@ -178,13 +178,13 @@ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { goto cleanup_return; } else if (readb == 0) { - //LOGW("无法从远程桥(%d)接收数据(readb=0),远程桥可能已经断开了连接: %s\n", w->fd, strerror(errno)); + //LOGW("unable to remote from Bridge%d receive data(readb=0), the remote Bridge may have been disconnected: %s\n", w->fd, strerror(errno)); LOGW(_("Can't received data from remote bridge(%d), remote bridge may close the connection (readb=0): %s\n"), w->fd, strerror(errno)); free(buf); goto cleanup_return; } else { - //LOGD("从远程桥(%d)收取了 %d 字节数据\n", w->fd, readb); + //LOGD("from a remote Bridge%d received %d bytes of data\n", w->fd, readb); struct list_head *pos; @@ -206,13 +206,13 @@ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { if (c->type == PKT_TYPE_CTL) { - //LOGD("收到来自 %s:%d 的控制类型数据包(fd=%d),丢弃,数据包编号为 %d\n", conn->host, conn->port, w->fd, c->id); + //LOGD("received from %s:%d the control type data packet(fd=%d), discarding that data packet number is %d\n", conn->host, conn->port, w->fd, c->id); LOGD(_("Receive control packet from %s:%d (fd=%d), packet ID is %d, drop it.\n"), conn->host, conn->port, w->fd, c->id); free(c); goto cleanup_return; } else if (c->type == PKT_TYPE_NONE) { - //LOGD("收到来自 %s:%d 的无类型数据包,这应该是网络出现了问题(fd=%d),丢弃,数据包编号为 %d\n", conn->host, conn->port, w->fd, c->id); + //LOGD("received from %s:%d non-type data packet, which should be a network problem(fd=%d), discarding that data packet number is %d\n", conn->host, conn->port, w->fd, c->id); LOGD(_("Received unknown type packet from %s:%d (fd=%d), may cause by bad network, packet ID is %d, drop it."), conn->host, conn->port, w->fd, c->id); free(c); goto cleanup_return; @@ -225,17 +225,17 @@ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { - /// 简单地丢弃已经收过的包 + /// Simply discard already received packet if (received_is_received(received, c->id) != 0) { - /// 已经收过包了 - //LOGD("从远程桥 %s:%d (fd=%d)收到长度为 %d 的曾经收取过的编号为 %d 的包,丢弃之\n", conn->host, conn->port, w->fd, c->buflen, c->id); + /// Has been received through the packet. + //LOGD("from a remote Bridge %s:%d fd=%d, received length is %d ever charged over the number is %d packet, discarding it\n", conn->host, conn->port, w->fd, c->buflen, c->id); LOGD(_("Received packet from remote bridge %s:%d (fd=%d) of %d bytes, packet ID is %d, drop it.\n"), conn->host, conn->port, w->fd, c->buflen, c->id); free(c); goto cleanup_return; } else { received_add(received, c->id); - //LOGD("从远程桥 %s:%d (fd=%d)收到 %d 字节的数据包,包编号 %d,原始包长度 %d, 载荷长度 %d\n", conn->host, conn->port, w->fd, c->buflen, c->id, readb, c->buflen); + //LOGD("from a remote Bridge %s:%d fd=%d, received %d bytes of packet, packet number %d, the original packet length %d payload length %d\n", conn->host, conn->port, w->fd, c->buflen, c->id, readb, c->buflen); LOGD(_("Received packet from remote bridge %s:%d (fd=%d) of %d bytes, packet ID is %d, raw lenth %d bytes, payload length %d bytes\n"), conn->host, conn->port, w->fd, c->buflen, c->id, readb, c->buflen); } @@ -243,19 +243,19 @@ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { - /// 将收到的数据包转发到客户端 + /// The received data packet is forwarded to the client int sendb; sendb = sendto(g_listen_fd, buf, c->buflen, MSG_DONTWAIT, &g_client_addr, g_client_addrlen); if (sendb < 0) { - //LOGW("无法向客户端转发编号为 %d 的数据包:%s\n", c->id, strerror(errno)); + //LOGW("unable to client forwarding number is %d packet:%s\n", c->id, strerror(errno)); LOGW(_("Can not forward packet #%d to client: %s\n"), c->id, strerror(errno)); } else if (sendb == 0) { - //LOGW("客户端可能已经断开了连接,无法转发编号为 %d 的数据包\n", c->id); + //LOGW("the client may have been disconnected, not the forwarding number is %d packet\n", c->id); LOGW(_("Client might close the connection, packet #%d can not be forwarded\n"), c->id); } else { - //LOGD("向客户端(端口 %u)转发了 %d 字节长度的编号为 %d 的数据包\n", ntohs(((struct sockaddr_in*)&g_client_addr)->sin_port), sendb, c->id); + //LOGD("to client(port %u), forwarding %d bytes to the length of the number is %d packet\n", ntohs(((struct sockaddr_in*)&g_client_addr)->sin_port), sendb, c->id); LOGD(_("Forwarded packet to client(port %u) of %d bytes, ID is %d\n"), ntohs(((struct sockaddr_in*)&g_client_addr)->sin_port), sendb, c->id); } @@ -269,8 +269,8 @@ void recv_remote_callback(struct ev_loop* reactor, ev_io* w, int events) { /** - * 用于异线程发送 EV 事件,通知 client_thread 有 fd 事件(连接断开)发生的函数 - */ +* For ISO-thread to send an EV event, the notification client_thread have the fd event that disconnect occurs the function +*/ void restart_conn_signal(struct ev_loop *reactor, ev_async *w, int events) { ev_feed_fd_event(g_ev_reactor, ev_async_reset_conn.fd, EV_READ); } @@ -279,8 +279,8 @@ void restart_conn_signal(struct ev_loop *reactor, ev_async *w, int events) { /** - * 初始化一个接收器 ev,用来处理收到的数据 - */ +* Initialize a receiver ev, is used to process the received data +*/ ev_io* init_recv_ev(int fd) { if (g_ev_reactor == NULL) { g_ev_reactor = ev_loop_new(EVFLAG_AUTO); @@ -300,8 +300,8 @@ ev_io* init_recv_ev(int fd) { /** - * 删除一个已经初始化好的接收器 ev - */ +* To delete an already initialized the good of the receiver ev +*/ int destroy_recv_ev(ev_io *watcher) { assert(g_ev_reactor != NULL); @@ -388,8 +388,8 @@ int connect_to_servers(struct list_head *list, char* server_list_path) { /** - * 重新连接到链表中指定的服务器 - */ +* Re-connection to the list specified in the server +*/ int reconnect_to_server(connections_t *c) { connections_t* newc = NULL; @@ -403,7 +403,7 @@ int reconnect_to_server(connections_t *c) { LOGD(_("Reconnected to %s:%d, fd %d -> %d"), c->host, c->port, c->fd, newc->fd); - /// host 和 port 是相同的,所以不用复制 + /// host and port are the same, so don't copy c->fd = newc->fd; c->watcher = newc->watcher; c->st_time = 0; @@ -419,10 +419,10 @@ int reconnect_to_server(connections_t *c) { /** - * 将本地数据转发到桥的线程 - * - * @param void* 目标服务器配置文件路径 - */ +* The local data is forwarded to the bridge thread +* +* @param void* target server configuration file path +*/ void* client_thread(void* ptr) { int readb, sendb, buflen; char* buf; @@ -435,7 +435,7 @@ void* client_thread(void* ptr) { buf = malloc(buflen); - /// 连接到服务器 + /// Connect to the server connect_to_servers(&g_connections, server_list_path); LOGD(_("Initializing libev thread\n")); @@ -465,7 +465,7 @@ void* client_thread(void* ptr) { break; } else { - /// 收到了数据,将数据转发给桥 + /// Received a data, the data is forwarded to the bridge struct list_head *pos; connections_t *c, *c_broken = NULL; @@ -479,26 +479,26 @@ void* client_thread(void* ptr) { list_for_each(pos, &g_connections) { c = list_entry(pos, connections_t, list); - /// 1. 检查连接是否超时 - /// 1.1 如果最后一次收到服务器包的时间已经初始化,则检查连接是否超时 + /// 1. Check whether the connection is timeout + /// 1.1 if the last received server time has been initialized, it is checked whether the connection timeout if (c->rc_time != 0 && c->st_time - c->rc_time > CLIENT_BRIDGE_TIMEOUT) { - //LOGD("到 %s:%d 的连接在最后一次发包后已经超过 %d 秒没有收到桥端的数据了,认为连接断开,即将重连\n", c->host, c->port, c->st_time - c->rc_time); + //LOGD("to %s:%d connection in the last contract has more than %d seconds have not received the bridge end of the data, that the connection is disconnected, the upcoming re-connect\n", c->host, c->port, c->st_time - c->rc_time); LOGD(_("No packet received from %s:%d since last packet sent to it (%d seconds), assume connection broken, about to reconnect\n"), c->host, c->port, c->st_time - c->rc_time); //reconnect_to_server(c); c_broken = c; } c->st_time = time(NULL); - /// 1.2 初始化最后一次收到服务器包的时间,以便进行超时判断 + /// 1.2 initialize the last time the server receives packets time, for timeout judgment if (c->rc_time == 0) { c->rc_time = time(NULL); } - /// 2. 发送数据 + /// 2. Send data sendb = packet_send(c->fd, buf, readb, *g_packet_id); if (sendb < 0) { - //LOGW("无法向 %s:%d(%d) 发送 %d 字节数据: %s\n", c->host, c->port, c->fd, readb, strerror(errno)); + //LOGW("无法向 %s:%d(%d) 发送 %of 字节数据: %s\n", c->host, c->port, c->fd, read, strerror(errno)); LOGW(_("Error while sending data to %s:%d(%d) with %d bytes: %s\n"), c->host, c->port, c->fd, readb, strerror(errno)); if (errno == EINVAL || errno == ECONNREFUSED) { @@ -511,17 +511,17 @@ void* client_thread(void* ptr) { LOGW(_("%s:%d(%d) may close the connection\n"), c->host, c->port, c->fd); } else { - //LOGD("向 %s:%d 发送了 %d 字节消息“%s”\n", c->host, c->port, sendb, buf); + //LOGD("向 %s:%d 发送了 %of 字节消息“%s”\n", c->host, c->port, sendb, buf); LOGD(_("Packet sent to %s:%d(%d) of %d bytes.\n"), c->host, c->port, c->fd, sendb); } } - /// 检查是否有失效的连接 + /// Check whether fail a connection if (c_broken != NULL) { LOGI(_("Connection to %s:%d is broken, try reconnect\n"), c_broken->host, c_broken->port); - //LOGD("不会在该线程中进行重连操作, 才怪,不在这里重连的话似乎一直不会重连"); - //LOGD("不会在该线程(%s)中进行重连操作,但会调用 ev_async_send(c->fd=%d) 以便通知另一线程调用 ev_feed_fd_event,以执行此操作\n", __FUNCTION__, c_broken->fd); + //LOGD("not in the thread to re-connect operation, No, is not here to re-connect the words seem to have been not re-connect"); + //LOGD("not in this thread(%s)to re-connect to operate, but would call ev_async_send(c->fd=%d) to notify another thread to call ev_feed_fd_event to perform this operation\n", __FUNCTION__, c_broken->fd); LOGD(_("I will not perform reconnect in current thread(%s), I will call ev_async_send(c->fd=%d) to ask another thread to perform the reconnect\n"), __FUNCTION__, c_broken->fd); c_broken->broken = 1; ev_async_reset_conn.fd = c_broken->fd; diff --git a/mptunnel.c b/mptunnel.c index 83bb808..6e573cb 100644 --- a/mptunnel.c +++ b/mptunnel.c @@ -21,13 +21,13 @@ int g_config_encrypt = 1; /** - * 已经收到的包的红黑树根 - */ +* Have received a package of red-black tree root +*/ static struct rb_root g_received_rbtree = RB_ROOT; /** - * 组装一个数据包,组装出来的包需要调用 packet_free 进行释放 - */ +* Assemble a data packet, assembled it out of the pack need to call packet_free release +*/ packet_t* packet_make(enum packet_type type, const char* buf, int buflen, int id) { packet_t* p; @@ -66,7 +66,7 @@ int packet_send(int fd, char* buf, int buflen, int id) { LOGW("fd=%d may close the connection\n", fd); } else { - //LOGD("向 %d 发送了 %d 字节消息“%s”\n", fd, sendb, (char*)(p + 1)); + //LOGD ("%d sending %d byte message '%s '\n", fd, sendb, (char*)(p + 1)); LOGD(_("Sent %d bytes to %d, message id is %d\n"), sendb, fd, id); } @@ -89,8 +89,8 @@ int packet_received(int _id) { */ /** - * 判断一个包是否曾经接收过,如果接收过返回 1,否则返回 0 - */ +* Determine whether a packet was received, if received before return 1,otherwise return 0 +*/ /* int packet_is_received(int _id) { if (g_received_id == NULL) { @@ -133,12 +133,12 @@ int received_destroy(received_t* r) { } /** - * 收包管理器,从列表中删除一个 id - */ +* Received the package Manager, and remove from the list an id +*/ int received_list_del(received_t* r, int id) { received_list_t* c; - /// 从“已收到包列表”中删除这个 id + /// From the“Receive packet list”delete this id pthread_mutex_lock(&r->rlist_mutex); c = received_rbtree_get(&g_received_rbtree, id); @@ -157,8 +157,8 @@ int received_list_del(received_t* r, int id) { /** - * 收包管理器,往列表中增加一个 id - */ +* Received the package Manager, to the list add an id +*/ int received_list_add(received_t* r, int id) { pthread_mutex_lock(&r->rlist_mutex); @@ -181,10 +181,10 @@ int received_list_add(received_t* r, int id) { /** - * 收包管理器:判断一个数据包是否已经收过了 - * - * @param int 如果一个数据包已经收到过了就返回 1,如果没有收到过就返回 0 - */ +* Received the package Manager: determining a whether the data packet has been received. +* +* @param int if a data packet has been received. returns 1,if not received returns 0 +*/ int received_is_received(received_t* r, int id) { if (id <= r->min_con_id) { return 1; @@ -195,7 +195,7 @@ int received_is_received(received_t* r, int id) { else { int ret = 0; - /// 在列表中查找这个 id + /// In the list to find the id pthread_mutex_lock(&r->rlist_mutex); struct received_list_t *n; @@ -216,22 +216,22 @@ int received_is_received(received_t* r, int id) { /** - * 收包管理器,记录收到了一个数据包 - * - * @param received_t* 收包管理器 - * @param int 刚刚收到的数据包的 id - */ +* Received the package Manager, the record received is a data packet +* +* @param received_t* received the package Manager +* @param int the just received data packet the id of the +*/ int received_add(received_t* r, int id) { - /// 收到的数据包正好比“连续收到的最小数据包编号”大 1 的情况 + /// The data packet received just more than“the continuous receipt of a minimum data packet number”large-1 case if (id == r->min_con_id + 1) { r->min_con_id = id; received_list_del(r, id); } else if (id <= r->min_con_id) { - /// 这个包已经收过了,无视 + /// This packet has been received before, ignore } else { - /// 这个包跳过了几个包,将其添加到列表中 + /// This package to skip a few packets, add it to the list received_list_add(r, id); } @@ -243,11 +243,11 @@ int received_add(received_t* r, int id) { } /** - * 收包管理器,丢弃超时的数据包,并更新“连续收到的最小数据包” - * - * @param received_t* 丢包管理器 - * @param int 丢弃多少秒内未收到的包。 - */ +* Received the package Manager, the discard-timeout for the data packet, and updates the“continuous receipt of the minimum data package” +* +* @param received_t* throw the package Manager +* @param int discarded number of seconds is not received within the package. +*/ int received_try_dropdead(received_t* r, int ttl) { long ts = time(NULL); if (r->last_dropdead_time + ttl <= ts) { @@ -264,18 +264,18 @@ int received_try_dropdead(received_t* r, int ttl) { do { received_list_t *minn = NULL; struct rb_node* rbnode = rbnode; - /// 找列表中最小的 id,判断其 ctime 是否距离现在已经超过了 ttl 秒,如果是,则丢弃该 id 之前的所有数据包 - /// 如果其 ctime 距离现在没有超过 ttl 秒,则退出清理过程 + /// Find the list of the smallest id, to determine its ctime whether the distance now has more than ttl seconds, if it is, then discard the id before all the data packets + /// If its ctime is now no more than ttl seconds, then exit the Clean-up process rbnode = rb_first(&g_received_rbtree); minn = container_of(rbnode, received_list_t, rbnode); if (minn != NULL) { if (minn->ctime + ttl <= time(NULL)) { - /// 丢弃该 id 之前的所有数据包 - /// 由于这个数据包是列表中最小的数据包,所以不需要清理列表,只需要将 minn 从列表中删除即可 + /// Discard the id before all the data packets + /// Since this packet is a list of the minimum data package, so no need to clean up the list, just to minn from the list can be deleted - //LOGD("数据包“%d”收到的时间已经过去了 %ld 秒,认为之前所有的数据包都已经收到了,当前最小已收到连续包 id 是 %d\n", minn->id, time(NULL) - minn->ctime, r->min_con_id); + //LOGD("data package“%d”received time has passed for %ld seconds, think before all data packets have been received, the current minimum has received a continuous packet id is %d\n", minn->id, time(NULL) - minn->ctime, r->min_con_id); LOGD(_("Packet #%d was received and time elapsed %ld seconds, assume packets which ID is smaller than it are all received. The smallest ID of received packet is %d\n"), minn->id, time(NULL) - minn->ctime, r->min_con_id); rb_erase(&minn->rbnode, &g_received_rbtree); @@ -283,7 +283,7 @@ int received_try_dropdead(received_t* r, int ttl) { free(minn); } else { - /// 退出循环 + /// Exit the loop minn = NULL; break; } @@ -360,12 +360,12 @@ received_list_t* received_rbtree_get(struct rb_root* root, int id) { /** - * 加密和解密内容 - * - * @param char* 要加密的内容 - * @param int 要加密内容的长度 - * @param uint32_t 初始化向量 - */ +* Encrypt and decrypt the content +* +* @param char* content to be encrypted +* @param int to the encrypted Content Length +* @param a uint32_t that the initialization vector +*/ void encrypt_lfsr(char* _buf, int _size, uint32_t *iv) { int i; unsigned char *buf = (unsigned char*)_buf; @@ -383,8 +383,8 @@ void decrypt_lfsr(char* _buf, int _size, uint32_t* iv) { } /** - * 对一个完整的 mptunnel 数据包进行加密和解密 - */ +* For a complete mptunnel packet encryption and decryption +*/ void mpdecrypt(char* _buf) { packet_t *p = (packet_t*)_buf; uint32_t iv; @@ -393,14 +393,14 @@ void mpdecrypt(char* _buf) { return; } - /// FIXME: 由于数据通过 UDP 协议传输,故此处的 p->buflen 不可信,应该同时传入 buflen 并做校验,以防止内存错误 + /// FIXME: since the data by the UDP Protocol to transmit, so the p->buflen not credible, it should be at the same time the incoming buflen and do check, to prevent memory errors - /// 首先解密 packet_t + /// First decrypt the packet_t iv = p->iv; decrypt_lfsr(_buf + sizeof(p->iv), sizeof(packet_t) - sizeof(p->iv), &iv); - /// 接着解密内容 + /// Then decrypt the content decrypt_lfsr(_buf + sizeof(packet_t), p->buflen, &iv); } @@ -422,22 +422,22 @@ void mpencrypt(char* _buf, int _buflen) { /** - * 一个简单的线性反馈移位寄存器随机数发生器 - * - * @param uint32_t 随机数种子(状态),该状态的值会被改变 - * @return uint32_t 一个 32 位无符号整型的随机数 - */ +* A simple linear feedback shift register random number generator +* +* @param a uint32_t that the random number seed status, the status value will be changed +* @return a uint32_t that a 32-bit unsigned integer random number +*/ uint32_t lfsr_rand(uint32_t *st) { unsigned char b32, b30, b26, b25, b; uint32_t r = 0x00; int i; /// - /** 在我们的应用中不需要高强度的随机数 - if (*st == 0) { - *st = 1; - } - */ + /** In our application does not require high-strength random number +if (*st == 0) { +*st = 1; +} +*/ for (i = 0; i < 32; i++) { b32 = *st & 0x00000001; diff --git a/mptunnel.h b/mptunnel.h index 310286c..35ab0c2 100644 --- a/mptunnel.h +++ b/mptunnel.h @@ -20,10 +20,10 @@ #define _(STR) gettext(STR) -/// 转发时最大的包长度 +/// Forwarded when the maximum packet length #define MAX_PACKET_SIZE 8000 -/// 客户端检测的到桥端的连接超时时间 +/// Client detection to the bridge end of the connection timeout time #define CLIENT_BRIDGE_TIMEOUT 60 @@ -63,10 +63,10 @@ enum packet_type { }; typedef struct packet_t { - uint32_t iv; /** 加密使用的 IV */ + uint32_t iv; /** Encryption IV */ enum packet_type type; int id; - int buflen; /** 数据长度(不包括 packet_t 自身) */ + int buflen; /** Data length does not include packet_t itself */ } packet_t; @@ -78,8 +78,8 @@ typedef struct received_list_t { typedef struct received_t { - int min_con_id; /// 连续收到的最小包编号 - int max_id; /// 目前已经收到的最大包编号 + int min_con_id; /// Continuously received the smallest number + int max_id; /// Currently have received maximum packet number time_t last_dropdead_time; struct list_head rlist; pthread_mutex_t rlist_mutex; @@ -92,9 +92,9 @@ typedef struct connections_t { char* host; int port; ev_io *watcher; - int rc_time; /// 最后一次收到服务器端数据的时间 - int st_time; /// 最后一次向服务器发送数据包的时间 - unsigned char broken; /// 连接是否已经中断并需要重连,该记号由人工进行标记 + int rc_time; /// The last received server-side data time + int st_time; /// The last time the server sent data packet of the time + unsigned char broken; /// Whether the connection has been interrupted and need to re-connect, the sign by artificial marking } connections_t; diff --git a/net.c b/net.c index de60d12..cb8f3c4 100644 --- a/net.c +++ b/net.c @@ -15,13 +15,13 @@ #include "net.h" /** - * 在指定地址上开始一个监听 - * - * @param const char* 监听地址,暂时只支持 IPv4,使用点分数字格式表示 - * @param int 监听端口 - * @param type 连接类型,可选 SOCK_STREAM 和 SOCK_DGRAM - * @return int 成功时返回监听 fd,失败时返回其他值,并设置 errno - */ +* At the specified address on the start a listener +* +* @param const char* listen address, temporarily only supports IPv4,using the point score Word format +* @param int listening port +* @param type The type of connection, the optional SOCK_STREAM and SOCK_DGRAM +* @return int successfully returned when the listening fd,fails to return other value, and set errno +*/ int net_bind(const char* bind_ip, int port, int type) { int ret, n, c[4]; int fd; @@ -99,13 +99,13 @@ int net_accept(int bind_fd, void* client_ip) { /** - * 连接到一个服务器 - * - * @param const char* 服务器地址 - * @param int 服务器端口 - * @param int 连接类型,可以是 SOCK_STREAM 或 SOCK_DGRAM - * @return int 连接成功返回 fd,失败返回负数,并设置 errno - */ +* Connect to a server +* +* @param const char* server address +* @param int server port +* @param int the type of connection, which can be SOCK_STREAM or SOCK_DGRAM +* @return int the connection is successful return fd, failure to return a negative number and set errno +*/ int net_connect(const char* host, int port, int type) { int ret, fd; struct sockaddr_in server_addr; diff --git a/server.c b/server.c index 6ebf32e..f029735 100644 --- a/server.c +++ b/server.c @@ -24,8 +24,8 @@ #define UDP_KEEP_ALIVE 300 /** - * 一个 UDP 连接上,最后收到包的时间与最后发送包的时间的最大时间差 - */ +* A UDP connection, the last received packet time and the last transmitted packet time The maximum time difference +*/ #define UDP_INTERACTIVE_TIMEOUT 60 @@ -48,8 +48,8 @@ extern int g_config_encrypt; /** - * 收到远程桥发来的数据时的处理函数 - */ +* Receive remote Bridge is sent to the data processing function +*/ void recv_bridge_callback(struct ev_loop* reactor, ev_io* w, int events) { char* buf; int buflen = 65536; @@ -71,7 +71,7 @@ void recv_bridge_callback(struct ev_loop* reactor, ev_io* w, int events) { b->addrlen = sizeof(b->addr); baddr = *(struct sockaddr_in*)&b->addr; - //LOGD("收到从桥端(fd=%d)发来的数据\n", w->fd); + //LOGD("received from the bridge end fd=%d sent data\n", w->fd); readb = recvfrom(w->fd, buf, buflen, 0, &b->addr, &b->addrlen); if (readb < 0) { @@ -107,7 +107,7 @@ void recv_bridge_callback(struct ev_loop* reactor, ev_io* w, int events) { b->rc_time = time(NULL); if (exists != 1) { - /// 这是一个新客户端,将其添加到客户端列表中 + /// This is a new client, add it to the client list LOGI(_("Got a new client, add it to Client List\n")); list_add(&b->list, &g_bridge_list); } @@ -116,33 +116,33 @@ void recv_bridge_callback(struct ev_loop* reactor, ev_io* w, int events) { } - /// 解包,然后发送给目标服务器 + /// Unpack, and then sent to the target server packet_t* p; mpdecrypt(buf); p = (packet_t*)buf; if (p->type == PKT_TYPE_CTL) { - //LOGD("从桥端(:%u)收取了 %d 字节数据编号为 %d 的数据包,但这是一个控制包,丢弃之\n", htons(baddr.sin_port), readb, p->id); + //LOGD("from the bridge end(:%u)received %d bytes of data number of %d data packets, but this is a control packet, discarding it\n", htons(baddr. sin_port), readb, p->id); LOGD(_("Received control packet from bridge (:%u) of %d bytes, packet ID is %d, drop it\n"), htons(baddr.sin_port), readb, p->id); free(buf); return; } else if (p->type != PKT_TYPE_DATA) { - //LOGD("从桥端(:%u)收取了 %d 字节编号为 %d 的数据包,但这是一个未知类型的数据包,丢弃之\n", htons(baddr.sin_port), readb, p->id); + //LOGD("from the bridge end(:%u)received %d byte number %d of data packet, but this is an unknown type of packet, discard it\n", htons(baddr. sin_port), readb, p->id); LOGD(_("Received packet from bridge (:%u) of %d bytes, packet ID is %d, but packet type is unknown, drop it.\n"), htons(baddr.sin_port), readb, p->id); free(buf); return; } else { - //LOGD("从桥端(:%u)收取了 %d 字节编号为 %d 的数据包\n", htons(baddr.sin_port), readb, p->id); + //LOGD("from the bridge end(:%u)received %d byte number is %d packet\n", htons(baddr. sin_port), readb, p->id); } buflen = p->buflen; buf = (char*)buf + sizeof(*p); if (received_is_received(received, p->id) == 1) { - //LOGD("从桥端(:%u)收取了 %d 字节编号为 %d 的曾经收取过的数据包,丢弃之\n", htons(baddr.sin_port), readb, p->id); + //LOGD("from the bridge end(:%u)received %d byte number %d had charge of the data packet, discarding it\n", htons(baddr. sin_port), readb, p->id); LOGD(_("Received packet from bridge (:%u) of %d bytes which was received, packet ID is %d, drop it\n"), htons(baddr.sin_port), readb, p->id); free(p); @@ -153,7 +153,7 @@ void recv_bridge_callback(struct ev_loop* reactor, ev_io* w, int events) { return; } else { - //LOGD("从桥端(:%u)收取了 %d 字节编号为 %d 的数据包,转发该包\n", htons(baddr.sin_port), readb, p->id); + //LOGD("from the bridge end(:%u)received %d byte number %d of data packet, forwarding the packet\n", htons(baddr. sin_port), readb, p->id); LOGD(_("Received packet from bridge (:%u) of %d bytes, ID is %d, forward it\n"), htons(baddr.sin_port), readb, p->id); received_add(received, p->id); } @@ -164,19 +164,19 @@ void recv_bridge_callback(struct ev_loop* reactor, ev_io* w, int events) { } - /// 发送给目标服务器 + /// Sent to the target server int sendb; sendb = send(g_target_fd, buf, buflen, MSG_DONTWAIT); if (sendb < 0) { - ///LOGW("无法向目标服务器发送编号为 %d 的数据包:%s\n", p->id, strerror(errno)); + ///LOGW("unable to the target server to send the number is %d packet:%s\n", p->id, strerror(errno)); LOGW(_("Can't send packet #%d to target server: %s\n"), p->id, strerror(errno)); } else if (sendb == 0) { - //LOGW("目标服务器可能已经断开了连接,无法转发 %d 号数据包\n", p->id); + //LOGW("target server may have been disconnected, unable to forward %d number of data packet\n", p->id); LOGW(_("Connection to target server seems closed, can't forward packet #%d\n"), p->id); } else { - //LOGD("成功向目标服务器发送了 %d 字节数据:%s\n", buflen, buf); + //LOGD("successfully to the target server to send %d bytes data:%s\n", buflen, buf); } free(p); @@ -185,8 +185,8 @@ void recv_bridge_callback(struct ev_loop* reactor, ev_io* w, int events) { /** - * ev 处理线程 - */ +* ev processing thread +*/ void* ev_thread(void* ptr) { int port = 3002; @@ -211,8 +211,8 @@ void* ev_thread(void* ptr) { /** - * 向桥们发送数据 - */ +* To bridge who sent the data +*/ int send_to_servers(char* buf, int buflen) { struct sockaddr* addr; struct sockaddr_in *baddr; @@ -225,7 +225,7 @@ int send_to_servers(char* buf, int buflen) { int ret = 0; int split = buflen / 2; - //LOGI("要发送的数据大小为 %d 字节,超过最大包大小(%d 字节),将该包拆分为两个小包后再尝试发送\n", buflen, MAX_PACKET_SIZE); + //LOGI("sent data size is %d bytes, exceeds the maximum packet size(%d bytes), the packet is split into two packets and then try to send\n", buflen, MAX_PACKET_SIZE); LOGI(_("Packet is %d bytes, which excees max packet size limit (%d bytes), spilt the packet into two smaller packets before send.\n"), buflen, MAX_PACKET_SIZE); ret += send_to_servers(buf, split); @@ -253,9 +253,9 @@ int send_to_servers(char* buf, int buflen) { b = list_entry(l, bridge_t, list); baddr = (struct sockaddr_in*)&b->addr; - /// 1. 检查连接是否超时 + /// 1. Check whether the connection is timeout if (ts - b->rc_time > UDP_KEEP_ALIVE) { - //LOGD("桥(%s:%u)空闲了 %d 秒,认为此桥已经断开,不向其转发数据包 %d\n", ipstr, ntohs(baddr->sin_port), ts - b->rc_time, p->id); + //LOGD("Bridge%s:%u idle %d seconds, that this bridge has been disconnected, not to forward the data packet of %d\n", ipstr, ntohs(baddr->sin_port), ts - b->rc_time, p->id); LOGD(_("No packet received from bridge (%s:%u) for %d seconds, assume the connection is closed, stop forward packet to it %d\n"), ipstr, ntohs(baddr->sin_port), ts - b->rc_time, p->id); list_del(l); free(l); @@ -263,7 +263,7 @@ int send_to_servers(char* buf, int buflen) { } if (abs(b->rc_time - b->st_time) > UDP_INTERACTIVE_TIMEOUT) { - //LOGD("桥(%s:%u)最后发包与收包时间之差超过了 %d 秒(实际:%d),认为此桥已经断开,不向其转发数据包 %d\n", ipstr, ntohs(baddr->sin_port), UDP_INTERACTIVE_TIMEOUT, b->rc_time - b->st_time, p->id); + //LOGD("Bridge%s:%u last contract and the packet receiving time difference of more than %d seconds, actual:%d, that this bridge has been disconnected, not to forward the data packet of %d\n", ipstr, ntohs(baddr->sin_port), UDP_INTERACTIVE_TIMEOUT, b->rc_time - b->st_time, p->id); LOGD(_("The time difference between packet received and packet sent of bridge %s:%u is larger than %d seconds (Actually %d seconds), assume the connection is broken, stop forward packet to it %d\n"), ipstr, ntohs(baddr->sin_port), UDP_INTERACTIVE_TIMEOUT, b->rc_time - b->st_time, p->id); list_del(l); free(l); @@ -273,7 +273,7 @@ int send_to_servers(char* buf, int buflen) { b->st_time = time(NULL); - /// 2. 发送数据包 + /// 2. Transmitting the data packet sendb = sendto(g_listen_fd, p, buflen + sizeof(*p), 0, &b->addr, b->addrlen); if (sendb < 0) { //LOGW("无法向桥(%s:%d)发送 %d 字节数据,包编号 %d: %s\n", ipstr, ntohs(baddr->sin_port), buflen, rawp.id, strerror(errno)); @@ -283,7 +283,7 @@ int send_to_servers(char* buf, int buflen) { LOGW("Can't send packet to bridge, bridge may close the connection\n"); } else { - //LOGD("向桥(端口:%u)发送了 %d 字节数据,包编号 %d\n", ntohs(baddr->sin_port), sendb, rawp.id); + //LOGD("to bridge, port:%u, sent %d bytes of data, packet number %d\n", ntohs(baddr->sin_port), sendb, rawp. id); LOGD(_("Forward packet to bridge(port %u) of %d bytes, packet ID is %d\n"), ntohs(baddr->sin_port), sendb, rawp.id); } } @@ -297,8 +297,8 @@ int send_to_servers(char* buf, int buflen) { /** - * 用于转发服务器消息到客户端的线程 - */ +* For the forwarding server message to client thread +*/ void* server_thread(void* ptr) { int readb, sendb, buflen; char* buf; @@ -334,7 +334,7 @@ void* server_thread(void* ptr) { continue; } else { - /// 收到了数据,将数据转发给桥 + /// Received a data, the data is forwarded to the bridge send_to_servers(buf, readb); } } @@ -362,7 +362,7 @@ int main(int argc, char** argv) { exit(-1); } else { - /// 载入配置信息 + /// Load the configuration information g_listen_port = atoi(argv[1]); g_target_host = strdup(argv[2]); g_target_port = atoi(argv[3]); @@ -401,7 +401,7 @@ int main(int argc, char** argv) { - /// 创建转发数据到目标服务器的线程 + /// Create a forwards the data to the destination server thread int* ptr = malloc(sizeof(int)); *ptr = clientfd; pthread_create(&tid, NULL, server_thread, NULL); @@ -419,8 +419,8 @@ int main(int argc, char** argv) { /** - * 初始化一个接收器 ev,用来处理收到的数据 - */ +* Initialize a receiver ev, is used to process the received data +*/ ev_io* init_recv_ev(int fd) { ev_io *watcher = (ev_io*)malloc(sizeof(ev_io)); memset(watcher, 0x00, sizeof(*watcher)); diff --git a/server.h b/server.h index caa72fb..9a3d643 100644 --- a/server.h +++ b/server.h @@ -20,8 +20,8 @@ typedef struct bridge_t { struct list_head list; struct sockaddr addr; socklen_t addrlen; - int rc_time; /// 最后一次收到客户端数据包的时间 - int st_time; /// 最后一次向客户端发送端数据包的时间 + int rc_time; /// The last time the client receives data packets the time + int st_time; /// The last time the client sends the end data packet of the time } bridge_t; #endif From 3dccebffea16fa768f721c5968233a3f09f592f4 Mon Sep 17 00:00:00 2001 From: abhijitm Date: Sat, 30 Jul 2016 23:06:35 +0530 Subject: [PATCH 2/2] Sat Jul 30 23:06:35 IST 2016: traslated most of the chinese to english --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ff9d786..c05c49a 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ callgrind* *valgrind* *core* *.mo +*.swp