Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c31106689a | ||
|
|
ce522f5675 | ||
|
|
6bd859747a | ||
|
|
7d5f53aebb | ||
|
|
f28efdf2b1 | ||
|
|
a9a75c6554 | ||
|
|
26f49d26db | ||
|
|
508ddd60e0 | ||
|
|
08b908273a | ||
|
|
8402967a0e | ||
|
|
a2e4264b01 | ||
|
|
ce057a05a7 | ||
|
|
7515c15123 | ||
|
|
60871860ae | ||
|
|
f8d065b09e | ||
|
|
2f2e64ae31 | ||
|
|
a4812dd596 | ||
|
|
2e420e921c | ||
|
|
99972f257a | ||
|
|
8e4ca75d36 | ||
|
|
360019d1a4 | ||
|
|
204c086a03 | ||
|
|
316834ffd0 | ||
|
|
aefbd2e062 | ||
|
|
ff6f60f079 | ||
|
|
b2a34fa18f | ||
|
|
984192c200 | ||
|
|
5747426565 | ||
|
|
0f78a61082 | ||
|
|
fa852f0b7d | ||
|
|
ff9ecd1b48 | ||
|
|
620c05b6bc | ||
|
|
5fffc80367 | ||
|
|
52ce3d69b4 | ||
|
|
259592e35c | ||
|
|
8866fc3a3a | ||
|
|
3dca97a235 | ||
|
|
9511680227 | ||
|
|
acb79bcf1f | ||
|
|
75adc0a669 | ||
|
|
2ef0200276 | ||
|
|
7940a4f949 | ||
|
|
81779637ba | ||
|
|
91d6129426 | ||
|
|
d333533639 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -37,6 +37,7 @@ CMakeFiles/
|
||||
CMakeCache.txt
|
||||
Makefile
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
|
||||
# bin generated
|
||||
xfrpc
|
||||
|
||||
34
README.md
34
README.md
@@ -25,9 +25,10 @@
|
||||
|
||||
## What is xfrp and why start xfrp
|
||||
|
||||
xfrp was [xfrps](https://github.com/KunTengRom/xfrp) client implemented by c for OpenWRT system
|
||||
`xfrp` is [xfrps](https://github.com/KunTengRom/xfrp) client implemented by c for [OpenWRT](https://github.com/openwrt/openwrt) and [LEDE](https://github.com/lede-project/source) system
|
||||
|
||||
The motivation to start xfrp project is that we are OpenWRTer, and openwrt usually ran in device which has little ROM and RAM space, however golang always need more space and memory; therefore we start xfrp project.
|
||||
|
||||
The motivation to start xfrp project is that we are OpenWRTer, and openwrt usually ran in device which has little ROM and RAM space, however golang always need more space and memory; therefore we start xfrp project
|
||||
|
||||
## Compile
|
||||
|
||||
@@ -44,6 +45,7 @@ make
|
||||
|
||||
## Quick start
|
||||
|
||||
**before using xfrp, you should have built the proxy server: [xfrps](https://github.com/KunTengRom/xfrp), It's no difference with frp at usage, but support `FTP` and more embedded-client-friendly for linux.**
|
||||
|
||||
Run in debug mode :
|
||||
|
||||
@@ -57,6 +59,34 @@ Run in release mode :
|
||||
xfrpc -c frpc_mini.ini -d 0
|
||||
```
|
||||
|
||||
## FTP support
|
||||
|
||||
xfrp support ftp proxy after version [0.07.451](https://github.com/KunTengRom/xfrp/tree/0.07.451). **Hypothesize you have built [xfrps](https://github.com/KunTengRom/xfrp) succeed!**
|
||||
|
||||
Configure ftp in frpc.ini
|
||||
|
||||
```
|
||||
[common]
|
||||
server_addr = 111.112.113.114
|
||||
server_port = 7001
|
||||
|
||||
[router_ftp_example]
|
||||
type = ftp
|
||||
local_port = 21
|
||||
remote_port = 30621
|
||||
remote_data_port = 30622
|
||||
```
|
||||
|
||||
`remote_port` is the reporxy port of FTP command tunnel, and `remote_data_port` is FTP-DATA port reporxy.
|
||||
|
||||
Use `-p` (PASV Mode) of `ftp` command to connect to proxy ftp server:
|
||||
|
||||
```
|
||||
ftp -p 111.112.113.114 30621
|
||||
```
|
||||
|
||||
In [Master](https://github.com/KunTengRom/xfrp) version `server_addr` can use domain name intead of IP address in FTP proxy. [Issue #4](https://github.com/KunTengRom/xfrp/issues/4) and [Issue #5](https://github.com/KunTengRom/xfrp/issues/5).
|
||||
|
||||
----
|
||||
|
||||
## Todo list
|
||||
|
||||
45
client.c
45
client.c
@@ -54,14 +54,14 @@
|
||||
#include "zip.h"
|
||||
#include "common.h"
|
||||
#include "proxy.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define MAX_OUTPUT (512*1024)
|
||||
|
||||
static void drained_writecb(struct bufferevent *bev, void *ctx);
|
||||
static void xfrp_event_cb(struct bufferevent *bev, short what, void *ctx);
|
||||
|
||||
static void
|
||||
xfrp_read_cb(struct bufferevent *bev, void *ctx)
|
||||
static void xfrp_read_cb(struct bufferevent *bev, void *ctx)
|
||||
{
|
||||
struct proxy *p = (struct proxy *)ctx;
|
||||
struct bufferevent *partner = p?p->bev:NULL;
|
||||
@@ -89,8 +89,7 @@ xfrp_read_cb(struct bufferevent *bev, void *ctx)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
drained_writecb(struct bufferevent *bev, void *ctx)
|
||||
static void drained_writecb(struct bufferevent *bev, void *ctx)
|
||||
{
|
||||
struct proxy *p = (struct proxy *)ctx;
|
||||
struct bufferevent *partner = p?p->bev:NULL;
|
||||
@@ -103,8 +102,7 @@ drained_writecb(struct bufferevent *bev, void *ctx)
|
||||
bufferevent_enable(partner, EV_READ);
|
||||
}
|
||||
|
||||
static void
|
||||
close_on_finished_writecb(struct bufferevent *bev, void *ctx)
|
||||
static void close_on_finished_writecb(struct bufferevent *bev, void *ctx)
|
||||
{
|
||||
struct evbuffer *b = bufferevent_get_output(bev);
|
||||
|
||||
@@ -113,8 +111,7 @@ close_on_finished_writecb(struct bufferevent *bev, void *ctx)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
xfrp_event_cb(struct bufferevent *bev, short what, void *ctx)
|
||||
static void xfrp_event_cb(struct bufferevent *bev, short what, void *ctx)
|
||||
{
|
||||
struct proxy *p = (struct proxy *)ctx;
|
||||
struct bufferevent *partner = p?p->bev:NULL;
|
||||
@@ -145,6 +142,38 @@ xfrp_event_cb(struct bufferevent *bev, short what, void *ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// return: 0: init succeed; 1: init failed
|
||||
// static int init_ftp_data_port(struct proxy_service *ps)
|
||||
// {
|
||||
// struct mycurl_string ret_buf;
|
||||
// if ( ! mycurl_string_init(&ret_buf)) {
|
||||
// debug(LOG_ERR, "error: ftp data port buffer init failed!");
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// char url[1024] = {0};
|
||||
// struct common_conf *c_conf = get_common_config();
|
||||
|
||||
|
||||
// int state_code = 0;
|
||||
// double down_size = 0;
|
||||
// int ret = net_visit(url,
|
||||
// &ret_buf,
|
||||
// HTTP_GET,
|
||||
// NULL,
|
||||
// 60l,
|
||||
// &state_code,
|
||||
// &down_size);
|
||||
|
||||
// if (ret) {
|
||||
// debug(LOG_ERR, "error: ftp remote data port init failed by HTTP GET");
|
||||
// mycurl_string_free(&ret_buf);
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
int is_ftp_proxy(const struct proxy_service *ps)
|
||||
{
|
||||
if (! ps || ! ps->proxy_type)
|
||||
|
||||
1
client.h
1
client.h
@@ -63,6 +63,7 @@ struct proxy_client {
|
||||
struct proxy_service {
|
||||
char *proxy_name;
|
||||
char *proxy_type;
|
||||
char *ftp_cfg_proxy_name;
|
||||
int use_encryption;
|
||||
int use_compression;
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "commandline.h"
|
||||
#include "debug.h"
|
||||
#include "version.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef void signal_func (int);
|
||||
|
||||
@@ -122,6 +123,7 @@ usage(const char *appname)
|
||||
fprintf(stdout, " -d <level> Debug level\n");
|
||||
fprintf(stdout, " -h Print usage\n");
|
||||
fprintf(stdout, " -v Print version information\n");
|
||||
fprintf(stdout, " -r Print run id of client\n");
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
|
||||
@@ -134,7 +136,7 @@ parse_commandline(int argc, char **argv)
|
||||
int c;
|
||||
int flag = 0;
|
||||
|
||||
while (-1 != (c = getopt(argc, argv, "c:hfd:sw:vx:i:a:"))) {
|
||||
while (-1 != (c = getopt(argc, argv, "c:hfd:sw:vrx:i:a:"))) {
|
||||
|
||||
|
||||
switch (c) {
|
||||
@@ -165,10 +167,28 @@ parse_commandline(int argc, char **argv)
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
fprintf(stdout, "This is %s version " VERSION "\n", argv[0]);
|
||||
fprintf(stdout, "version: " VERSION "\n");
|
||||
exit(1);
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
{
|
||||
char ifname[16] = {0};
|
||||
if(get_net_ifname(ifname, 16)){
|
||||
debug(LOG_ERR, "error: get device sign ifname failed!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
char if_mac[64] = {0};
|
||||
if(get_net_mac(ifname, if_mac, sizeof(if_mac))) {
|
||||
debug(LOG_ERR, "error: Hard ware MAC address of [%s] get failed!", ifname);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
fprintf(stdout, "run ID:%s\n", if_mac);
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
|
||||
46
config.c
46
config.c
@@ -63,8 +63,18 @@ void free_common_config()
|
||||
if (c_conf->log_level) free(c_conf->log_level);
|
||||
if (c_conf->auth_token) free(c_conf->auth_token);
|
||||
if (c_conf->privilege_token) free(c_conf->privilege_token);
|
||||
SAFE_FREE(c_conf->server_ip);
|
||||
};
|
||||
|
||||
void set_common_server_ip(const char *ip)
|
||||
{
|
||||
struct common_conf *c_conf = get_common_config();
|
||||
c_conf->server_ip = strdup(ip);
|
||||
assert(c_conf->server_ip);
|
||||
|
||||
debug(LOG_DEBUG, "server IP address: [%s]", c_conf->server_ip);
|
||||
}
|
||||
|
||||
void free_base_config(struct base_conf *bconf)
|
||||
{
|
||||
if (bconf->name) free(bconf->name);
|
||||
@@ -112,7 +122,7 @@ static const char *get_valid_type(const char *val)
|
||||
|
||||
static void dump_common_conf()
|
||||
{
|
||||
if(!c_conf) {
|
||||
if(! c_conf) {
|
||||
debug(LOG_ERR, "Error: c_conf is NULL");
|
||||
return;
|
||||
}
|
||||
@@ -135,18 +145,6 @@ static void dump_proxy_service(const int index, struct proxy_service *ps)
|
||||
ps->proxy_type = strdup("tcp");
|
||||
assert(ps->proxy_type);
|
||||
} else if (strcmp(ps->proxy_type, "ftp") == 0) {
|
||||
if (ps->remote_data_port <= 0) {
|
||||
debug(LOG_ERR,
|
||||
"Proxy [%s] error: remote_data_port must be exist when type is ftp",
|
||||
ps->proxy_name);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (!is_valid_ip_address((const char *)c_conf->server_addr)){
|
||||
debug(LOG_ERR, "now ftp proxy only support ip address in [server_addr]");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
new_ftp_data_proxy_service(ps);
|
||||
}
|
||||
|
||||
@@ -178,6 +176,7 @@ static struct proxy_service *new_proxy_service(const char *name)
|
||||
assert(c_conf);
|
||||
|
||||
ps->proxy_name = strdup(name);
|
||||
ps->ftp_cfg_proxy_name = NULL;
|
||||
assert(ps->proxy_name);
|
||||
|
||||
ps->proxy_type = NULL;
|
||||
@@ -212,6 +211,9 @@ static void new_ftp_data_proxy_service(struct proxy_service *ftp_ps)
|
||||
"cannot create ftp data proxy service, it should not happenned!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
ps->ftp_cfg_proxy_name = strdup(ftp_ps->proxy_name);
|
||||
assert(ps->ftp_cfg_proxy_name);
|
||||
|
||||
ps->proxy_type = strdup("tcp");
|
||||
ps->remote_port = ftp_ps->remote_data_port;
|
||||
@@ -306,8 +308,15 @@ static int common_handler(void *user, const char *section, const char *name, con
|
||||
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
||||
if (MATCH("common", "server_addr")) {
|
||||
SAFE_FREE(config->server_addr);
|
||||
config->server_addr = strdup(value);
|
||||
int addr_len = strlen(value) + 1;
|
||||
config->server_addr = (char *)calloc(1, addr_len);
|
||||
assert(config->server_addr);
|
||||
if(dns_unified(value, config->server_addr, addr_len)) {
|
||||
debug(LOG_ERR, "error: server_addr [%s] is invalid!", value);
|
||||
exit(0);
|
||||
}
|
||||
if (is_valid_ip_address(value))
|
||||
set_common_server_ip(value);
|
||||
} else if (MATCH("common", "server_port")) {
|
||||
config->server_port = atoi(value);
|
||||
} else if (MATCH("common", "http_proxy")) {
|
||||
@@ -369,13 +378,15 @@ static void init_common_conf(struct common_conf *config)
|
||||
config->heartbeat_timeout = 60;
|
||||
config->tcp_mux = 0;
|
||||
config->user = NULL;
|
||||
config->server_ip = NULL;
|
||||
config->is_router = 0;
|
||||
}
|
||||
|
||||
// it should be free after using
|
||||
// because of assert it will never return NULL
|
||||
char *get_ftp_data_proxy_name(const char *ftp_proxy_name)
|
||||
{
|
||||
char *ftp_tail_data_name = FTP_DATA_PROXY_SUFFIX;
|
||||
char *ftp_tail_data_name = FTP_RMT_CTL_PROXY_SUFFIX;
|
||||
char *ftp_data_proxy_name = (char *)calloc(1,
|
||||
strlen(ftp_proxy_name)+strlen(ftp_tail_data_name)+1);
|
||||
assert(ftp_data_proxy_name);
|
||||
@@ -419,3 +430,8 @@ void load_config(const char *confile)
|
||||
|
||||
dump_all_ps();
|
||||
}
|
||||
|
||||
int is_running_in_router()
|
||||
{
|
||||
return c_conf->is_router;
|
||||
}
|
||||
|
||||
20
config.h
20
config.h
@@ -29,7 +29,7 @@
|
||||
#include "client.h"
|
||||
#include "common.h"
|
||||
|
||||
#define FTP_DATA_PROXY_SUFFIX "_ftp_data_proxy"
|
||||
#define FTP_RMT_CTL_PROXY_SUFFIX "_ftp_remote_ctl_proxy"
|
||||
|
||||
struct base_conf{
|
||||
char *name;
|
||||
@@ -45,19 +45,23 @@ struct base_conf{
|
||||
|
||||
// common config
|
||||
struct common_conf {
|
||||
char *server_addr; /* default 0.0.0.0 */
|
||||
int server_port; /* default 7000 */
|
||||
char *server_addr; /* default 0.0.0.0 */
|
||||
char *server_ip;
|
||||
int server_port; /* default 7000 */
|
||||
char *http_proxy;
|
||||
char *log_file; /* default consol */
|
||||
char *log_way; /* default console */
|
||||
char *log_level; /* default info */
|
||||
char *log_file; /* default consol */
|
||||
char *log_way; /* default console */
|
||||
char *log_level; /* default info */
|
||||
int log_max_days; /* default 3 */
|
||||
char *privilege_token;
|
||||
char *auth_token;
|
||||
int heartbeat_interval; /* default 10 */
|
||||
int heartbeat_timeout; /* default 30 */
|
||||
int tcp_mux; /* default 0 */
|
||||
int tcp_mux; /* default 0 */
|
||||
char *user;
|
||||
|
||||
/* private fields */
|
||||
int is_router; // to sign router (Openwrt/LEDE) or not
|
||||
};
|
||||
|
||||
struct common_conf *get_common_config();
|
||||
@@ -71,5 +75,7 @@ struct proxy_client *get_all_pc();
|
||||
|
||||
void load_config(const char *confile);
|
||||
char *get_ftp_data_proxy_name(const char *ftp_proxy_name);
|
||||
void set_common_server_ip(const char *ip);
|
||||
int is_running_in_router();
|
||||
|
||||
#endif //_CONFIG_H_
|
||||
|
||||
252
control.c
252
control.c
@@ -29,10 +29,9 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <json-c/json.h>
|
||||
#include <syslog.h>
|
||||
|
||||
@@ -60,7 +59,6 @@
|
||||
#include "login.h"
|
||||
|
||||
static struct control *main_ctl;
|
||||
static char *request_buf;
|
||||
static int clients_conn_signel = 0;
|
||||
|
||||
static void sync_new_work_connection(struct bufferevent *bev);
|
||||
@@ -71,13 +69,6 @@ static int is_client_connected()
|
||||
return clients_conn_signel;
|
||||
}
|
||||
|
||||
static void init_request_buffer()
|
||||
{
|
||||
size_t len = (1<<16) + get_header_size();
|
||||
request_buf = calloc(1, len);
|
||||
assert(request_buf);
|
||||
}
|
||||
|
||||
static int client_connected(int is_connected)
|
||||
{
|
||||
if (is_connected)
|
||||
@@ -131,7 +122,7 @@ static void new_client_connect()
|
||||
struct proxy_client *client = new_proxy_client();
|
||||
struct common_conf *c_conf = get_common_config();
|
||||
assert(c_conf);
|
||||
client->base = main_ctl->connect_base;;
|
||||
client->base = main_ctl->connect_base;
|
||||
struct bufferevent *bev = connect_server(client->base, c_conf->server_addr, c_conf->server_port);
|
||||
if (!bev) {
|
||||
debug(LOG_DEBUG, "Connect server [%s:%d] failed", c_conf->server_addr, c_conf->server_port);
|
||||
@@ -195,21 +186,18 @@ static size_t request(struct bufferevent *bev, struct frame *f)
|
||||
bout = main_ctl->connect_bev;
|
||||
}
|
||||
|
||||
if ( ! bout) {
|
||||
goto REQ_END;
|
||||
}
|
||||
if ( ! bout)
|
||||
return 0;
|
||||
|
||||
struct common_conf *c = get_common_config();
|
||||
if ( ! c)
|
||||
goto REQ_END;
|
||||
return 0;
|
||||
|
||||
write_len = (size_t)f->len;
|
||||
if ( 0 == write_len)
|
||||
goto REQ_END;;
|
||||
return 0;
|
||||
|
||||
bufferevent_write(bout, f->data, write_len);
|
||||
|
||||
REQ_END:
|
||||
return write_len;
|
||||
}
|
||||
|
||||
@@ -255,6 +243,7 @@ static void ping(struct bufferevent *bev)
|
||||
struct frame *f = new_frame(cmdNOP, 0); //ping sid is 0
|
||||
assert(f);
|
||||
request(bout, f);
|
||||
free_frame(f);
|
||||
}
|
||||
|
||||
uint32_t sid = get_main_control()->session_id;
|
||||
@@ -328,11 +317,12 @@ static void sync_new_work_connection(struct bufferevent *bev)
|
||||
SAFE_FREE(work_c);
|
||||
}
|
||||
|
||||
struct bufferevent *connect_server(struct event_base *base, const char *name, const int port)
|
||||
struct bufferevent *
|
||||
connect_server(struct event_base *base, const char *name, const int port)
|
||||
{
|
||||
struct bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
|
||||
assert(bev);
|
||||
|
||||
|
||||
if (bufferevent_socket_connect_hostname(bev,
|
||||
main_ctl->dnsbase,
|
||||
AF_INET,
|
||||
@@ -364,6 +354,50 @@ static void hb_sender_cb(evutil_socket_t fd, short event, void *arg)
|
||||
set_ticker_ping_timer(main_ctl->ticker_ping);
|
||||
}
|
||||
|
||||
// return: 0: raw succeed 1: raw failed
|
||||
static int proxy_service_resp_raw(struct new_proxy_response *npr)
|
||||
{
|
||||
if (npr->error && strlen(npr->error) > 2) {
|
||||
debug(LOG_ERR, "error: new proxy response error_field:%s", npr->error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((! npr->proxy_name) || (strlen(npr->proxy_name) <= 0)) {
|
||||
debug(LOG_ERR, "error: new proxy response proxy name unmarshal failed!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct proxy_service *ps = NULL;
|
||||
ps = get_proxy_service(npr->proxy_name);
|
||||
if (! ps) {
|
||||
debug(LOG_ERR, "error: proxy_name responsed by TypeNewProxyResp not found!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (! ps->proxy_type) {
|
||||
debug(LOG_ERR, "error: proxy_type is NULL, it should be never happend!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ps->ftp_cfg_proxy_name) {
|
||||
struct proxy_service *main_ps = get_proxy_service(ps->ftp_cfg_proxy_name);
|
||||
if (main_ps) {
|
||||
debug(LOG_DEBUG, "find main ftp proxy service name [%s]", main_ps->proxy_name);
|
||||
} else {
|
||||
debug(LOG_ERR, "error: cannot find main ftp proxy service!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (npr->remote_port <= 0) {
|
||||
debug(LOG_ERR, "error: ftp remote_data_port [%d] that request from server is invalid!", npr->remote_port);
|
||||
return 1;
|
||||
}
|
||||
main_ps->remote_data_port = npr->remote_port;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
raw_message(struct message *msg, struct bufferevent *bev, struct proxy_client *client)
|
||||
{
|
||||
@@ -416,7 +450,24 @@ raw_message(struct message *msg, struct bufferevent *bev, struct proxy_client *c
|
||||
break;
|
||||
|
||||
case TypeNewProxyResp:
|
||||
break;
|
||||
{
|
||||
if (msg->data_p == NULL) {
|
||||
debug(LOG_ERR,
|
||||
"recved TypeNewProxyResp but no data, it should be never happend!");
|
||||
break;
|
||||
}
|
||||
|
||||
struct new_proxy_response *npr = new_proxy_resp_unmarshal(msg->data_p);
|
||||
if (npr == NULL) {
|
||||
debug(LOG_ERR, "new proxy response buffer unmarshal faild!");
|
||||
return;
|
||||
}
|
||||
|
||||
proxy_service_resp_raw(npr);
|
||||
|
||||
SAFE_FREE(npr);
|
||||
break;
|
||||
}
|
||||
|
||||
case TypeStartWorkConn:
|
||||
sr = start_work_conn_resp_unmarshal(msg->data_p);
|
||||
@@ -756,7 +807,9 @@ static void open_connection_session(struct bufferevent *bev)
|
||||
{
|
||||
struct frame *f = new_frame(cmdSYN, main_ctl->session_id);
|
||||
assert(f);
|
||||
|
||||
request(bev, f);
|
||||
free_frame(f);
|
||||
}
|
||||
|
||||
static void connect_event_cb (struct bufferevent *bev, short what, void *ctx)
|
||||
@@ -764,15 +817,16 @@ static void connect_event_cb (struct bufferevent *bev, short what, void *ctx)
|
||||
struct common_conf *c_conf = get_common_config();
|
||||
static int retry_times = 0;
|
||||
if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
|
||||
if (retry_times >= 10) {
|
||||
debug(LOG_INFO,
|
||||
if (retry_times >= 10) { // only try 10 times consecutively
|
||||
debug(LOG_ERR,
|
||||
"have retry connect to xfrp server for %d times, exit!",
|
||||
retry_times);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
retry_times++;
|
||||
debug(LOG_ERR, "connect server [%s:%d] failed",
|
||||
debug(LOG_ERR, "error: connect server [%s:%d] failed",
|
||||
c_conf->server_addr,
|
||||
c_conf->server_port);
|
||||
free_control();
|
||||
@@ -781,11 +835,14 @@ static void connect_event_cb (struct bufferevent *bev, short what, void *ctx)
|
||||
close_main_control();
|
||||
} else if (what & BEV_EVENT_CONNECTED) {
|
||||
retry_times = 0;
|
||||
|
||||
// recv login-response message before recving othfer fprs messages,
|
||||
bufferevent_setcb(bev, recv_cb, NULL, connect_event_cb, NULL);
|
||||
bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST);
|
||||
|
||||
open_connection_session(bev);
|
||||
if (get_common_config()->tcp_mux)
|
||||
open_connection_session(bev);
|
||||
|
||||
login();
|
||||
}
|
||||
}
|
||||
@@ -802,17 +859,40 @@ static void keep_control_alive()
|
||||
set_ticker_ping_timer(main_ctl->ticker_ping);
|
||||
}
|
||||
|
||||
static void server_dns_cb(int event_code, struct evutil_addrinfo *addr, void *ctx)
|
||||
{
|
||||
if (event_code) {
|
||||
set_common_server_ip((const char *)evutil_gai_strerror(event_code));
|
||||
} else {
|
||||
struct evutil_addrinfo *ai;
|
||||
if (addr->ai_canonname)
|
||||
debug(LOG_DEBUG, "addr->ai_canonname [%s]", addr->ai_canonname);
|
||||
for (ai = addr; ai; ai = ai->ai_next) {
|
||||
char buf[128];
|
||||
const char *s = NULL;
|
||||
if (ai->ai_family == AF_INET) {
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
|
||||
s = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, 128);
|
||||
} else if (ai->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
|
||||
s = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, 128);
|
||||
}
|
||||
|
||||
if (s) set_common_server_ip(s);
|
||||
}
|
||||
if (addr) evutil_freeaddrinfo(addr);
|
||||
}
|
||||
}
|
||||
|
||||
void start_base_connect()
|
||||
{
|
||||
struct common_conf *c_conf = get_common_config();
|
||||
assert(c_conf);
|
||||
|
||||
main_ctl->connect_bev = connect_server(main_ctl->connect_base,
|
||||
c_conf->server_addr,
|
||||
c_conf->server_port);
|
||||
c_conf->server_addr,
|
||||
c_conf->server_port);
|
||||
if ( ! main_ctl->connect_bev) {
|
||||
debug(LOG_ERR,
|
||||
"Connect server [%s:%d] failed",
|
||||
"error: connect server [%s:%d] failed",
|
||||
c_conf->server_addr,
|
||||
c_conf->server_port);
|
||||
exit(0);
|
||||
@@ -827,6 +907,7 @@ void sync_iv(unsigned char *iv)
|
||||
{
|
||||
struct frame *f = new_frame(cmdPSH, main_ctl->session_id);
|
||||
assert(f);
|
||||
|
||||
f->len = (ushort) get_encrypt_block_size();
|
||||
f->data = calloc(f->len, 1);
|
||||
memcpy(f->data, iv, f->len);
|
||||
@@ -846,14 +927,15 @@ void login()
|
||||
char *lg_msg = NULL;
|
||||
int len = login_request_marshal(&lg_msg); //marshal login request
|
||||
if ( ! lg_msg || ! len) {
|
||||
debug(LOG_ERR, "login_request_marshal failed");
|
||||
assert(lg_msg);
|
||||
debug(LOG_ERR,
|
||||
"error: login_request_marshal failed, it should never be happenned");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (get_common_config()->tcp_mux) {
|
||||
// using sid = 3 is only for matching fprs, it will change after using tcp-mux
|
||||
// using sid = 3 is only for matching fprs, it will change after using tcp-mux
|
||||
if (get_common_config()->tcp_mux)
|
||||
sync_session_id(3);
|
||||
}
|
||||
|
||||
send_msg_frp_server(NULL, TypeLogin, lg_msg, len, main_ctl->session_id);
|
||||
SAFE_FREE(lg_msg);
|
||||
}
|
||||
@@ -880,10 +962,8 @@ void send_msg_frp_server(struct bufferevent *bev,
|
||||
} else {
|
||||
bout = main_ctl->connect_bev;
|
||||
}
|
||||
assert(bout);
|
||||
|
||||
if ( ! bout) {
|
||||
return;
|
||||
}
|
||||
debug(LOG_DEBUG, "send ----> [%c: %s]", type, msg);
|
||||
|
||||
struct message req_msg;
|
||||
@@ -906,34 +986,12 @@ void send_msg_frp_server(struct bufferevent *bev,
|
||||
unsigned char *pack_buf = NULL;
|
||||
size_t pack_buf_len = pack(&req_msg, &pack_buf);
|
||||
if ( ! pack_buf_len || ! pack_buf) {
|
||||
debug(LOG_ERR, "send buffer pack failed!");
|
||||
debug(LOG_ERR, "error: send buffer pack failed!");
|
||||
goto S_M_F_END;
|
||||
}
|
||||
|
||||
#ifdef ENCRYPTO
|
||||
debug(LOG_DEBUG, "start encode message ...");
|
||||
unsigned char *encode_ret;
|
||||
unsigned char *encode_ret_test;
|
||||
unsigned char *decode_ret_test;
|
||||
struct frp_coder *encoder = get_main_encoder();
|
||||
|
||||
if (encoder) {
|
||||
size_t encode_ret_len = encrypt_data(pack_buf, pack_buf_len, encoder, &encode_ret);
|
||||
debug(LOG_DEBUG, "encode len:[%lu]", encode_ret_len);
|
||||
|
||||
if (encode_ret_len > 0) {
|
||||
f->data = encode_ret;
|
||||
set_frame_len(f, (ushort) encode_ret_len);
|
||||
}
|
||||
|
||||
set_frame_len(f, (ushort) pack_buf_len);
|
||||
}
|
||||
|
||||
#endif //ENCRYPTO
|
||||
if (! f->data) {
|
||||
set_frame_len(f, (ushort) pack_buf_len);
|
||||
f->data = pack_buf;
|
||||
}
|
||||
set_frame_len(f, (ushort) pack_buf_len);
|
||||
f->data = pack_buf;
|
||||
|
||||
if (get_common_config()->tcp_mux) {
|
||||
switch (type)
|
||||
@@ -957,7 +1015,8 @@ void send_msg_frp_server(struct bufferevent *bev,
|
||||
|
||||
S_M_F_END:
|
||||
SAFE_FREE(req_msg.data_p);
|
||||
SAFE_FREE(f->data);
|
||||
SAFE_FREE(pack_buf);
|
||||
f->data = NULL;
|
||||
free_frame(f);
|
||||
}
|
||||
|
||||
@@ -1007,20 +1066,38 @@ void send_new_proxy(struct proxy_service *ps)
|
||||
|
||||
void init_main_control()
|
||||
{
|
||||
if (main_ctl && main_ctl->connect_base)
|
||||
if (main_ctl && main_ctl->connect_base) {
|
||||
event_base_loopbreak(main_ctl->connect_base);
|
||||
free(main_ctl);
|
||||
}
|
||||
|
||||
main_ctl = calloc(sizeof(struct control), 1);
|
||||
assert(main_ctl);
|
||||
|
||||
struct common_conf *c_conf = get_common_config();
|
||||
if (c_conf->tcp_mux) {
|
||||
uint32_t *sid = init_sid_index();
|
||||
assert(sid);
|
||||
main_ctl->session_id = *sid;
|
||||
|
||||
debug(LOG_DEBUG, "Connect Frps with control session ID: %d", main_ctl->session_id);
|
||||
}
|
||||
|
||||
struct event_base *base = NULL;
|
||||
struct evdns_base *dnsbase = NULL;
|
||||
struct evdns_base *dnsbase = NULL;
|
||||
base = event_base_new();
|
||||
if (!base)
|
||||
return;
|
||||
if (! base) {
|
||||
debug(LOG_ERR, "error: event base init failed!");
|
||||
exit(0);
|
||||
}
|
||||
main_ctl->connect_base = base;
|
||||
|
||||
dnsbase = evdns_base_new(base, 1);
|
||||
if (!dnsbase)
|
||||
return;
|
||||
if (! dnsbase) {
|
||||
debug(LOG_ERR, "error: evdns base init failed!");
|
||||
exit(0);
|
||||
}
|
||||
main_ctl->dnsbase = dnsbase;
|
||||
|
||||
evdns_base_set_option(dnsbase, "timeout", "1.0");
|
||||
|
||||
@@ -1028,20 +1105,34 @@ void init_main_control()
|
||||
// http://www.wuqiong.info/archives/13/
|
||||
evdns_base_set_option(dnsbase, "randomize-case:", "0"); //TurnOff DNS-0x20 encoding
|
||||
evdns_base_nameserver_ip_add(dnsbase, "180.76.76.76"); //BaiduDNS
|
||||
evdns_base_nameserver_ip_add(dnsbase, "223.5.5.5"); //AliDNS
|
||||
evdns_base_nameserver_ip_add(dnsbase, "223.5.5.5"); //AliDNS
|
||||
evdns_base_nameserver_ip_add(dnsbase, "223.6.6.6"); //AliDNS
|
||||
evdns_base_nameserver_ip_add(dnsbase, "114.114.114.114"); //114DNS
|
||||
evdns_base_nameserver_ip_add(dnsbase, "114.114.114.114"); //114DNS
|
||||
|
||||
main_ctl->connect_base = base;
|
||||
main_ctl->dnsbase = dnsbase;
|
||||
init_request_buffer();
|
||||
// if server_addr is ip, done control init.
|
||||
if (is_valid_ip_address((const char *)c_conf->server_addr))
|
||||
return;
|
||||
|
||||
// if server_addr is domain, analyze it to ip for server_ip
|
||||
debug(LOG_DEBUG, "Get ip address of [%s] from DNServer", c_conf->server_addr);
|
||||
|
||||
if (get_common_config()->tcp_mux) {
|
||||
uint32_t *sid = init_sid_index();
|
||||
assert(sid);
|
||||
main_ctl->session_id = *sid;
|
||||
struct evutil_addrinfo hints;
|
||||
struct evdns_getaddrinfo_request *dns_req;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_flags = EVUTIL_AI_CANONNAME;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
debug(LOG_DEBUG, "Connect Frps with control session ID: %d", main_ctl->session_id);
|
||||
dns_req = evdns_getaddrinfo(dnsbase,
|
||||
c_conf->server_addr,
|
||||
NULL /* no service name given */,
|
||||
&hints,
|
||||
server_dns_cb,
|
||||
NULL);
|
||||
if (! dns_req) {
|
||||
debug(LOG_ERR, "error: can not analyse the dns of [%s]", c_conf->server_addr);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1064,6 +1155,5 @@ void free_control()
|
||||
if (!main_ctl)
|
||||
return;
|
||||
|
||||
SAFE_FREE(request_buf);
|
||||
SAFE_FREE(main_ctl);
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
/usr/bin/xfrpc
|
||||
/usr/bin/xfrp_test_server
|
||||
26
login.c
26
login.c
@@ -15,6 +15,7 @@
|
||||
#include "msg.h"
|
||||
#include "version.h"
|
||||
#include "login.h"
|
||||
#include "utils.h"
|
||||
|
||||
static struct login *c_login;
|
||||
|
||||
@@ -37,12 +38,12 @@ void init_login()
|
||||
{
|
||||
if (! c_login)
|
||||
c_login = calloc(sizeof(struct login), 1);
|
||||
|
||||
|
||||
assert(c_login);
|
||||
|
||||
struct common_conf *c_conf = get_common_config();
|
||||
assert(c_conf);
|
||||
|
||||
|
||||
struct utsname uname_buf;
|
||||
if (uname(&uname_buf)) {
|
||||
return;
|
||||
@@ -64,6 +65,27 @@ void init_login()
|
||||
c_login->user = c_conf->user;
|
||||
|
||||
c_login->logged = 0;
|
||||
|
||||
/* start to init login->run_id */
|
||||
char ifname[16] = {0};
|
||||
if(get_net_ifname(ifname, 16)){
|
||||
debug(LOG_ERR, "error: get device sign ifname failed!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (strcmp(ifname, "br-lan") == 0) {
|
||||
c_conf->is_router = 1;
|
||||
debug(LOG_DEBUG, "working in router");
|
||||
}
|
||||
|
||||
char if_mac[64] = {0};
|
||||
if(get_net_mac(ifname, if_mac, sizeof(if_mac))) {
|
||||
debug(LOG_ERR, "error: Hard ware MAC address of [%s] get failed!", ifname);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
c_login->run_id = strdup(if_mac);
|
||||
assert(c_login->run_id);
|
||||
}
|
||||
|
||||
int login_resp_check(struct login_resp *lr)
|
||||
|
||||
55
msg.c
55
msg.c
@@ -42,6 +42,7 @@
|
||||
#include "common.h"
|
||||
#include "login.h"
|
||||
#include "client.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define JSON_MARSHAL_TYPE(jobj,key,jtype,item) \
|
||||
json_object_object_add(jobj, key, json_object_new_##jtype((item)));
|
||||
@@ -80,7 +81,14 @@ static void fill_custom_domains(struct json_object *j_ctl_req, const char *custo
|
||||
char *tok = tmp, *end = tmp;
|
||||
while (tok != NULL) {
|
||||
strsep(&end, ",");
|
||||
json_object_array_add(jarray_cdomains, json_object_new_string(tok));
|
||||
|
||||
int dname_len = strlen(tok) + 1;
|
||||
char *dname_buf = (char *)calloc(1, dname_len);
|
||||
assert(dname_buf);
|
||||
dns_unified(tok, dname_buf, dname_len);
|
||||
json_object_array_add(jarray_cdomains, json_object_new_string(dname_buf));
|
||||
|
||||
free(dname_buf);
|
||||
tok = end;
|
||||
}
|
||||
SAFE_FREE(tmp);
|
||||
@@ -172,7 +180,6 @@ int new_proxy_service_marshal(const struct proxy_service *np_req, char **msg)
|
||||
JSON_MARSHAL_TYPE(j_np_req, "proxy_type", string, np_req->proxy_type);
|
||||
JSON_MARSHAL_TYPE(j_np_req, "use_encryption", boolean, np_req->use_encryption);
|
||||
JSON_MARSHAL_TYPE(j_np_req, "use_compression", boolean, np_req->use_compression);
|
||||
JSON_MARSHAL_TYPE(j_np_req, "remote_port", int, np_req->remote_port);
|
||||
|
||||
if (is_ftp_proxy(np_req)) {
|
||||
JSON_MARSHAL_TYPE(j_np_req, "remote_data_port", int, np_req->remote_data_port);
|
||||
@@ -180,8 +187,14 @@ int new_proxy_service_marshal(const struct proxy_service *np_req, char **msg)
|
||||
|
||||
if (np_req->custom_domains) {
|
||||
fill_custom_domains(j_np_req, np_req->custom_domains);
|
||||
json_object_object_add(j_np_req, "remote_port", NULL);
|
||||
} else {
|
||||
json_object_object_add(j_np_req, "custom_domains", NULL);
|
||||
if (np_req->remote_port != -1) {
|
||||
JSON_MARSHAL_TYPE(j_np_req, "remote_port", int, np_req->remote_port);
|
||||
} else {
|
||||
json_object_object_add(j_np_req, "remote_port", NULL);
|
||||
}
|
||||
}
|
||||
|
||||
JSON_MARSHAL_TYPE(j_np_req, "subdomain", string, SAFE_JSON_STRING(np_req->subdomain));
|
||||
@@ -229,6 +242,44 @@ int new_work_conn_marshal(const struct work_conn *work_c, char **msg)
|
||||
return nret;
|
||||
}
|
||||
|
||||
// result returned of this func need be free
|
||||
struct new_proxy_response *new_proxy_resp_unmarshal(const char *jres)
|
||||
{
|
||||
struct json_object *j_np_res = json_tokener_parse(jres);
|
||||
if (is_error(j_np_res))
|
||||
return NULL;
|
||||
|
||||
struct new_proxy_response *npr = calloc(1, sizeof(struct new_proxy_response));
|
||||
assert(npr);
|
||||
|
||||
struct json_object *npr_run_id = NULL;
|
||||
if (! json_object_object_get_ex(j_np_res, "run_id", &npr_run_id))
|
||||
goto END_ERROR;
|
||||
npr->run_id = strdup(json_object_get_string(npr_run_id));
|
||||
assert(npr->run_id);
|
||||
|
||||
struct json_object *npr_proxy_remote_port = NULL;
|
||||
if (! json_object_object_get_ex(j_np_res, "remote_port", &npr_proxy_remote_port))
|
||||
goto END_ERROR;
|
||||
npr->remote_port = json_object_get_int(npr_proxy_remote_port);
|
||||
|
||||
struct json_object *npr_proxy_name = NULL;
|
||||
if (! json_object_object_get_ex(j_np_res, "proxy_name", &npr_proxy_name))
|
||||
goto END_ERROR;
|
||||
npr->proxy_name = strdup(json_object_get_string(npr_proxy_name));
|
||||
assert(npr->proxy_name);
|
||||
|
||||
struct json_object *npr_error = NULL;
|
||||
if(! json_object_object_get_ex(j_np_res, "error", &npr_error))
|
||||
goto END_ERROR;
|
||||
npr->error = strdup(json_object_get_string(npr_error));
|
||||
assert(npr->error);
|
||||
|
||||
END_ERROR:
|
||||
json_object_put(j_np_res);
|
||||
return npr;
|
||||
}
|
||||
|
||||
// login_resp_unmarshal NEED FREE
|
||||
struct login_resp *login_resp_unmarshal(const char *jres)
|
||||
{
|
||||
|
||||
8
msg.h
8
msg.h
@@ -67,6 +67,13 @@ struct control_response {
|
||||
char *msg;
|
||||
};
|
||||
|
||||
struct new_proxy_response {
|
||||
char *run_id;
|
||||
char *proxy_name;
|
||||
char *error;
|
||||
int remote_port;
|
||||
};
|
||||
|
||||
struct work_conn {
|
||||
char *run_id;
|
||||
};
|
||||
@@ -91,6 +98,7 @@ size_t pack(struct message *req_msg, unsigned char **ret_buf);
|
||||
struct message *unpack(unsigned char *recv_msg, const ushort len);
|
||||
|
||||
// tranlate control request to json string
|
||||
struct new_proxy_response *new_proxy_resp_unmarshal(const char *jres);
|
||||
struct login_resp *login_resp_unmarshal(const char *jres);
|
||||
struct start_work_conn_resp *start_work_conn_resp_unmarshal(const char *resp_msg);
|
||||
|
||||
|
||||
11
proxy_ftp.c
11
proxy_ftp.c
@@ -101,7 +101,12 @@ void ftp_proxy_c2s_cb(struct bufferevent *bev, void *ctx)
|
||||
struct ftp_pasv *r_fp = new_ftp_pasv();
|
||||
r_fp->code = local_fp->code;
|
||||
|
||||
strncpy(r_fp->ftp_server_ip, c_conf->server_addr, IP_LEN);
|
||||
if (! c_conf->server_ip) {
|
||||
debug(LOG_ERR, "error: FTP proxy without server ip!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
strncpy(r_fp->ftp_server_ip, c_conf->server_ip, IP_LEN);
|
||||
r_fp->ftp_server_port = p->remote_data_port;
|
||||
|
||||
if (r_fp->ftp_server_port <= 0) {
|
||||
@@ -117,6 +122,10 @@ void ftp_proxy_c2s_cb(struct bufferevent *bev, void *ctx)
|
||||
goto FTP_C2S_CB_END;
|
||||
}
|
||||
|
||||
#ifdef FTP_P_DEBUG
|
||||
debug(LOG_DEBUG, "ftp pack result:%s", pasv_msg);
|
||||
#endif //FTP_P_DEBUG
|
||||
|
||||
set_ftp_data_proxy_tunnel(p->proxy_name, local_fp, r_fp);
|
||||
evbuffer_add(dst, pasv_msg, pack_len);
|
||||
SAFE_FREE(pasv_msg);
|
||||
|
||||
179
utils.c
179
utils.c
@@ -7,6 +7,15 @@
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <linux/if_link.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
@@ -28,4 +37,174 @@ int is_valid_ip_address(const char *ip_address)
|
||||
struct sockaddr_in sa;
|
||||
int result = inet_pton(AF_INET, ip_address, &(sa.sin_addr));
|
||||
return result;
|
||||
}
|
||||
|
||||
// net_if_name: name of network interface, e.g. br-lan
|
||||
// return: 1: error 0:get succeed
|
||||
int get_net_mac(char *net_if_name, char *mac, int mac_len) {
|
||||
int ret = 1;
|
||||
int i = 0;
|
||||
int sock = 0;
|
||||
|
||||
if (mac_len < 12 || net_if_name == NULL) {
|
||||
return 1;
|
||||
}
|
||||
struct ifreq ifreq;
|
||||
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if( sock < 0 ) {
|
||||
perror("error sock");
|
||||
goto OUT;
|
||||
}
|
||||
|
||||
strncpy(ifreq.ifr_name, net_if_name, IFNAMSIZ);
|
||||
if( ioctl(sock, SIOCGIFHWADDR,&ifreq) < 0 ) {
|
||||
perror("error ioctl");
|
||||
goto OUT;
|
||||
}
|
||||
|
||||
for( i = 0; i < 6; i++ ){
|
||||
snprintf(mac+2*i, mac_len - 2*i, "%02X",
|
||||
(unsigned char)ifreq.ifr_hwaddr.sa_data[i]);
|
||||
}
|
||||
mac[strlen(mac)] = 0;
|
||||
ret = 0;
|
||||
|
||||
OUT:
|
||||
close(sock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// return: -1: network interface check failed; other: ifname numbers
|
||||
int show_net_ifname()
|
||||
{
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
int family, s, n;
|
||||
char host[NI_MAXHOST];
|
||||
|
||||
if (getifaddrs(&ifaddr) == -1) {
|
||||
perror("getifaddrs");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Walk through linked list, maintaining head pointer so we
|
||||
can free list later */
|
||||
|
||||
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
|
||||
if (ifa->ifa_addr == NULL) continue;
|
||||
|
||||
family = ifa->ifa_addr->sa_family;
|
||||
|
||||
/* Display interface name and family (including symbolic
|
||||
form of the latter for the common families) */
|
||||
|
||||
printf("%-8s %s (%d)\n",
|
||||
ifa->ifa_name,
|
||||
(family == AF_PACKET) ? "AF_PACKET" :
|
||||
(family == AF_INET) ? "AF_INET" :
|
||||
(family == AF_INET6) ? "AF_INET6" : "???",
|
||||
family);
|
||||
|
||||
/* For an AF_INET* interface address, display the address */
|
||||
|
||||
if (family == AF_INET || family == AF_INET6) {
|
||||
s = getnameinfo(ifa->ifa_addr,
|
||||
(family == AF_INET) ? sizeof(struct sockaddr_in) :
|
||||
sizeof(struct sockaddr_in6),
|
||||
host, NI_MAXHOST,
|
||||
NULL, 0, NI_NUMERICHOST);
|
||||
if (s != 0) {
|
||||
printf("getnameinfo() failed: %s\n", gai_strerror(s));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("\t\taddress: <%s>\n", host);
|
||||
|
||||
} else if (family == AF_PACKET && ifa->ifa_data != NULL) {
|
||||
struct rtnl_link_stats *stats = (struct rtnl_link_stats *)ifa->ifa_data;
|
||||
|
||||
printf("\t\ttx_packets = %10u; rx_packets = %10u\n"
|
||||
"\t\ttx_bytes = %10u; rx_bytes = %10u\n",
|
||||
stats->tx_packets, stats->rx_packets,
|
||||
stats->tx_bytes, stats->rx_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
freeifaddrs(ifaddr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return: 0: network interface get succeed
|
||||
int get_net_ifname(char *if_buf, int blen)
|
||||
{
|
||||
if (NULL == if_buf || blen < 8) return -1;
|
||||
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
int family, n;
|
||||
int ret = 1;
|
||||
if (getifaddrs(&ifaddr) == -1) {
|
||||
perror("getifaddrs");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int found = 0;
|
||||
char tmp_if_buf[16];
|
||||
memset(tmp_if_buf, 0, sizeof(tmp_if_buf));
|
||||
/* Walk through linked list, maintaining head pointer so we
|
||||
can free list later */
|
||||
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
|
||||
if (ifa->ifa_addr == NULL) continue;
|
||||
|
||||
family = ifa->ifa_addr->sa_family;
|
||||
|
||||
if (family == AF_INET) {
|
||||
// for LEDE/OpenWRT embedded router os
|
||||
if (strcmp(ifa->ifa_name, "br-lan") == 0) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
} else if (family == AF_PACKET &&
|
||||
ifa->ifa_data != NULL &&
|
||||
strcmp(ifa->ifa_name, "lo") != 0) { // skip local loop interface
|
||||
|
||||
strncpy(tmp_if_buf, ifa->ifa_name, 16);
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
strncpy(if_buf, ifa->ifa_name, blen);
|
||||
ret = 0;
|
||||
} else if (tmp_if_buf[0] != 0) {
|
||||
strncpy(if_buf, tmp_if_buf, blen);
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
freeifaddrs(ifaddr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// e.g. wWw.Baidu.com/China will be trans into www.baidu.com/China
|
||||
// return: 0:check and trant succeed, 1:failed or domain name is invalid
|
||||
int dns_unified(const char *dname, char *udname_buf, int udname_buf_len)
|
||||
{
|
||||
if (! dname || ! udname_buf || udname_buf_len < strlen(dname)+1)
|
||||
return 1;
|
||||
|
||||
int has_dot = 0;
|
||||
int dlen = strlen(dname);
|
||||
int i = 0;
|
||||
for(i=0; i<dlen; i++) {
|
||||
if(dname[i] == '/')
|
||||
break;
|
||||
|
||||
if (dname[i] == '.' && i != dlen-1)
|
||||
has_dot = 1;
|
||||
|
||||
udname_buf[i] = tolower(dname[i]);
|
||||
}
|
||||
|
||||
if (! has_dot) //domain name should have 1 dot leastly
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
utils.h
9
utils.h
@@ -1,10 +1,19 @@
|
||||
#ifndef _UTILS_H_
|
||||
#define _UTILS_H_
|
||||
|
||||
struct mycurl_string {
|
||||
char *ptr;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
void s_sleep(unsigned int s, unsigned int u);
|
||||
|
||||
// is_valid_ip_address:
|
||||
// return 0:ipaddress unlegal
|
||||
int is_valid_ip_address(const char *ip_address);
|
||||
int show_net_ifname();
|
||||
int get_net_ifname(char *if_buf, int blen);
|
||||
int get_net_mac(char *net_if_name, char *mac, int mac_len);
|
||||
int dns_unified(const char *dname, char *udname_buf, int udname_buf_len);
|
||||
|
||||
#endif //_UTILS_H_
|
||||
|
||||
Reference in New Issue
Block a user