Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8d065b09e | ||
|
|
2f2e64ae31 | ||
|
|
a4812dd596 | ||
|
|
2e420e921c | ||
|
|
99972f257a | ||
|
|
8e4ca75d36 | ||
|
|
360019d1a4 | ||
|
|
204c086a03 | ||
|
|
316834ffd0 | ||
|
|
aefbd2e062 | ||
|
|
ff6f60f079 | ||
|
|
b2a34fa18f | ||
|
|
984192c200 | ||
|
|
5747426565 | ||
|
|
0f78a61082 | ||
|
|
fa852f0b7d | ||
|
|
ff9ecd1b48 | ||
|
|
620c05b6bc | ||
|
|
5fffc80367 | ||
|
|
52ce3d69b4 | ||
|
|
259592e35c | ||
|
|
8866fc3a3a | ||
|
|
3dca97a235 | ||
|
|
9511680227 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -37,6 +37,7 @@ CMakeFiles/
|
||||
CMakeCache.txt
|
||||
Makefile
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
|
||||
# bin generated
|
||||
xfrpc
|
||||
|
||||
@@ -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);
|
||||
|
||||
15
config.c
15
config.c
@@ -71,6 +71,8 @@ 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)
|
||||
@@ -309,8 +311,13 @@ 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);
|
||||
}
|
||||
} else if (MATCH("common", "server_port")) {
|
||||
config->server_port = atoi(value);
|
||||
} else if (MATCH("common", "http_proxy")) {
|
||||
@@ -373,6 +380,7 @@ static void init_common_conf(struct common_conf *config)
|
||||
config->tcp_mux = 0;
|
||||
config->user = NULL;
|
||||
config->server_ip = NULL;
|
||||
config->is_router = 0;
|
||||
}
|
||||
|
||||
// it should be free after using
|
||||
@@ -423,3 +431,8 @@ void load_config(const char *confile)
|
||||
|
||||
dump_all_ps();
|
||||
}
|
||||
|
||||
int is_running_in_router()
|
||||
{
|
||||
return c_conf->is_router;
|
||||
}
|
||||
|
||||
16
config.h
16
config.h
@@ -45,20 +45,23 @@ struct base_conf{
|
||||
|
||||
// common config
|
||||
struct common_conf {
|
||||
char *server_addr; /* default 0.0.0.0 */
|
||||
char *server_addr; /* default 0.0.0.0 */
|
||||
char *server_ip;
|
||||
int server_port; /* default 7000 */
|
||||
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();
|
||||
@@ -73,5 +76,6 @@ 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_
|
||||
|
||||
69
control.c
69
control.c
@@ -317,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,
|
||||
@@ -800,7 +801,7 @@ static void keep_control_alive()
|
||||
static void server_dns_cb(int event_code, struct evutil_addrinfo *addr, void *ctx)
|
||||
{
|
||||
if (event_code) {
|
||||
set_common_server_ip(evutil_gai_strerror(event_code));
|
||||
set_common_server_ip((const char *)evutil_gai_strerror(event_code));
|
||||
} else {
|
||||
struct evutil_addrinfo *ai;
|
||||
if (addr->ai_canonname)
|
||||
@@ -815,10 +816,10 @@ static void server_dns_cb(int event_code, struct evutil_addrinfo *addr, void *ct
|
||||
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 (s) set_common_server_ip(s);
|
||||
}
|
||||
evutil_freeaddrinfo(addr);
|
||||
if (addr) evutil_freeaddrinfo(addr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1011,33 +1012,7 @@ void init_main_control()
|
||||
|
||||
main_ctl = calloc(sizeof(struct control), 1);
|
||||
assert(main_ctl);
|
||||
struct event_base *base = NULL;
|
||||
struct evdns_base *dnsbase = NULL;
|
||||
base = event_base_new();
|
||||
if (! base) {
|
||||
debug(LOG_ERR, "error: event base init failed!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
dnsbase = evdns_base_new(base, 1);
|
||||
if (! dnsbase) {
|
||||
debug(LOG_ERR, "error: evdns base init failed!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
evdns_base_set_option(dnsbase, "timeout", "1.0");
|
||||
|
||||
// thanks to the following article
|
||||
// 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.6.6.6"); //AliDNS
|
||||
evdns_base_nameserver_ip_add(dnsbase, "114.114.114.114"); //114DNS
|
||||
|
||||
main_ctl->connect_base = base;
|
||||
main_ctl->dnsbase = dnsbase;
|
||||
|
||||
struct common_conf *c_conf = get_common_config();
|
||||
if (c_conf->tcp_mux) {
|
||||
uint32_t *sid = init_sid_index();
|
||||
@@ -1047,11 +1022,39 @@ void init_main_control()
|
||||
debug(LOG_DEBUG, "Connect Frps with control session ID: %d", main_ctl->session_id);
|
||||
}
|
||||
|
||||
struct event_base *base = NULL;
|
||||
struct evdns_base *dnsbase = NULL;
|
||||
base = event_base_new();
|
||||
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) {
|
||||
debug(LOG_ERR, "error: evdns base init failed!");
|
||||
exit(0);
|
||||
}
|
||||
main_ctl->dnsbase = dnsbase;
|
||||
|
||||
evdns_base_set_option(dnsbase, "timeout", "1.0");
|
||||
|
||||
// thanks to the following article
|
||||
// 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.6.6.6"); //AliDNS
|
||||
evdns_base_nameserver_ip_add(dnsbase, "114.114.114.114"); //114DNS
|
||||
|
||||
// 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);
|
||||
|
||||
struct evutil_addrinfo hints;
|
||||
struct evdns_getaddrinfo_request *dns_req;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
@@ -1067,7 +1070,7 @@ void init_main_control()
|
||||
server_dns_cb,
|
||||
NULL);
|
||||
if (! dns_req) {
|
||||
debug(LOG_ERR, "error: can not analyse the dns of %s", c_conf->server_addr);
|
||||
debug(LOG_ERR, "error: can not analyse the dns of [%s]", c_conf->server_addr);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
17
msg.c
17
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));
|
||||
|
||||
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;
|
||||
}
|
||||
4
utils.h
4
utils.h
@@ -6,5 +6,9 @@ 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