加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
xkcp_spy.c 4.13 KB
一键复制 编辑 原始数据 按行查看 历史
Dengfeng Liu 提交于 2017-04-21 16:10 . Update xkcp_spy.c
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA gnu@gnu.org *
* *
\********************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/bufferevent_ssl.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
static void timeoutcb(evutil_socket_t fd, short what, void *arg)
{
struct event_base *base = arg;
printf("timeout\n");
event_base_loopexit(base, NULL);
}
static void eventcb(struct bufferevent *bev, short what, void *ctx)
{
struct event_base *base = ctx;
if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
event_base_loopexit(base, NULL);
}
}
static void readcb(struct bufferevent *bev, void *ctx)
{
struct evbuffer *input = bufferevent_get_input(bev);
int len = evbuffer_get_length(input);
if (len > 0) {
char *buf = malloc(len+1);
memset(buf, 0, len+1);
evbuffer_remove(input, buf, len);
printf("%s", buf);
}
}
static void usage()
{
printf("xkcp_spy -h address -s|c -t cmd [ -m param]\n");
}
int main(int argc, char **argv)
{
struct event *evtimeout;
struct timeval timeout;
struct event_base *base;
struct bufferevent *bev;
char *cmd = NULL, *addr = NULL, *param = NULL;
int port = 0, opt, flag = 1;
while((opt = getopt(argc, argv, "h:sct:m:")) != -1) {
flag = 0;
switch(opt) {
case 'h':
addr = strdup(optarg);
break;
case 's':
port = 9087;
break;
case 'c':
port = 9086;
break;
case 't':
cmd = strdup(optarg);
break;
case 'm':
param = strdup(optarg);
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
if (flag || (!cmd || !addr || !port)) {
usage();
exit(EXIT_FAILURE);
}
base = event_base_new();
if (!base) {
puts("Couldn't open event base");
exit(EXIT_FAILURE);
}
timeout.tv_sec = 2;
timeout.tv_usec = 0;
evtimeout = evtimer_new(base, timeoutcb, base);
evtimer_add(evtimeout, &timeout);
bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, readcb, NULL, eventcb, base);
bufferevent_enable(bev, EV_READ|EV_WRITE);
if (bufferevent_socket_connect_hostname(bev, NULL, AF_INET, addr, port) < 0) {
bufferevent_free(bev);
printf("bufferevent_socket_connect failed [%s]", strerror(errno));
exit(EXIT_FAILURE);
}
free(addr);
if (!param) {
bufferevent_write(bev, cmd, strlen(cmd));
free(cmd);
} else {
int size = strlen(cmd) + strlen(param) + 2;
char *input = malloc(size);
memset(input, 0, size);
snprintf(input, size, "%s %s", cmd, param);
bufferevent_write(bev, cmd, strlen(cmd));
free(input);
free(cmd);
free(param);
}
event_base_dispatch(base);
bufferevent_free(bev);
event_free(evtimeout);
event_base_free(base);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化