助手程序移除了对 inet_ntop() 函数的使用,使之能够在WinXP上运行。

pull/105/head
Apex Liu 2017-12-11 14:50:01 +08:00
parent d6bcdeb42d
commit 9f673138c4
4 changed files with 319 additions and 149 deletions

View File

@ -477,10 +477,10 @@ void TsHttpRpc::_process_js_request(const ex_astr& func_cmd, const ex_astr& func
{
_rpc_func_run_client(func_args, buf);
}
else if (func_cmd == "check")
{
_rpc_func_check(func_args, buf);
}
// else if (func_cmd == "check")
// {
// _rpc_func_check(func_args, buf);
// }
else if (func_cmd == "rdp_play")
{
_rpc_func_rdp_play(func_args, buf);
@ -1030,137 +1030,137 @@ void TsHttpRpc::_rpc_func_run_client(const ex_astr& func_args, ex_astr& buf)
_create_json_ret(buf, root_ret);
}
bool isIPAddress(const char *s)
{
const char *pChar;
bool rv = true;
int tmp1, tmp2, tmp3, tmp4, i;
while (1)
{
i = sscanf_s(s, "%d.%d.%d.%d", &tmp1, &tmp2, &tmp3, &tmp4);
if (i != 4)
{
rv = false;
break;
}
if ((tmp1 > 255) || (tmp2 > 255) || (tmp3 > 255) || (tmp4 > 255))
{
rv = false;
break;
}
for (pChar = s; *pChar != 0; pChar++)
{
if ((*pChar != '.')
&& ((*pChar < '0') || (*pChar > '9')))
{
rv = false;
break;
}
}
break;
}
return rv;
}
void TsHttpRpc::_rpc_func_check(const ex_astr& func_args, ex_astr& buf)
{
// 入参:{"ip":"192.168.5.11","port":22,"uname":"root","uauth":"abcdefg","authmode":1,"protocol":2}
// authmode: 1=password, 2=private-key
// protocol: 1=rdp, 2=ssh
// SSH返回 {"code":0, "data":{"sid":"0123abcde"}}
// RDP返回 {"code":0, "data":{"sid":"0123abcde0A"}}
Json::Reader jreader;
Json::Value jsRoot;
if (!jreader.parse(func_args.c_str(), jsRoot))
{
_create_json_ret(buf, TPE_JSON_FORMAT);
return;
}
if (jsRoot.isArray())
{
_create_json_ret(buf, TPE_PARAM);
return;
}
int windows_size = 2;
// 判断参数是否正确
if (!jsRoot["server_ip"].isString() || !jsRoot["ssh_port"].isNumeric()
|| !jsRoot["rdp_port"].isNumeric()
)
{
_create_json_ret(buf, TPE_PARAM);
return;
}
std::string host = jsRoot["server_ip"].asCString();
int rdp_port = jsRoot["rdp_port"].asUInt();
int ssh_port = jsRoot["rdp_port"].asUInt();
std::string server_ip;
if (isIPAddress(host.c_str()))
{
server_ip = host;
}
else
{
char *ptr, **pptr;
struct hostent *hptr;
char IP[128] = { 0 };
/* 取得命令后第一个参数,即要解析的域名或主机名 */
ptr = (char*)host.c_str();
/* 调用gethostbyname()。调用结果都存在hptr中 */
if ((hptr = gethostbyname(ptr)) == NULL)
{
//printf("gethostbyname error for host:%s/n", ptr);
_create_json_ret(buf, TPE_PARAM);
return;
}
char szbuf[1204] = { 0 };
switch (hptr->h_addrtype)
{
case AF_INET:
case AF_INET6:
pptr = hptr->h_addr_list;
for (; *pptr != NULL; pptr++)
inet_ntop(hptr->h_addrtype, *pptr, IP, sizeof(IP));
server_ip = IP;
break;
default:
printf("unknown address type/n");
break;
}
}
if (!isIPAddress(server_ip.c_str()))
{
_create_json_ret(buf, TPE_PARAM);
return;
}
if (TestTCPPort(server_ip, rdp_port) && TestTCPPort(server_ip, ssh_port))
{
_create_json_ret(buf, TPE_OK);
return;
}
ICMPheaderRet temp = { 0 };
int b_ok = ICMPSendTo(&temp, (char*)server_ip.c_str(), 16, 8);
if (b_ok == 0)
{
_create_json_ret(buf, TPE_OK);
return;
}
else
{
_create_json_ret(buf, TPE_NETWORK);
}
return;
}
// bool isIPAddress(const char *s)
// {
// const char *pChar;
// bool rv = true;
// int tmp1, tmp2, tmp3, tmp4, i;
// while (1)
// {
// i = sscanf_s(s, "%d.%d.%d.%d", &tmp1, &tmp2, &tmp3, &tmp4);
// if (i != 4)
// {
// rv = false;
// break;
// }
//
// if ((tmp1 > 255) || (tmp2 > 255) || (tmp3 > 255) || (tmp4 > 255))
// {
// rv = false;
// break;
// }
//
// for (pChar = s; *pChar != 0; pChar++)
// {
// if ((*pChar != '.')
// && ((*pChar < '0') || (*pChar > '9')))
// {
// rv = false;
// break;
// }
// }
// break;
// }
//
// return rv;
// }
//
// void TsHttpRpc::_rpc_func_check(const ex_astr& func_args, ex_astr& buf)
// {
// // 入参:{"ip":"192.168.5.11","port":22,"uname":"root","uauth":"abcdefg","authmode":1,"protocol":2}
// // authmode: 1=password, 2=private-key
// // protocol: 1=rdp, 2=ssh
// // SSH返回 {"code":0, "data":{"sid":"0123abcde"}}
// // RDP返回 {"code":0, "data":{"sid":"0123abcde0A"}}
//
// Json::Reader jreader;
// Json::Value jsRoot;
//
// if (!jreader.parse(func_args.c_str(), jsRoot))
// {
// _create_json_ret(buf, TPE_JSON_FORMAT);
// return;
// }
// if (jsRoot.isArray())
// {
// _create_json_ret(buf, TPE_PARAM);
// return;
// }
// int windows_size = 2;
//
//
//
// // 判断参数是否正确
// if (!jsRoot["server_ip"].isString() || !jsRoot["ssh_port"].isNumeric()
// || !jsRoot["rdp_port"].isNumeric()
// )
// {
// _create_json_ret(buf, TPE_PARAM);
// return;
// }
//
// std::string host = jsRoot["server_ip"].asCString();
// int rdp_port = jsRoot["rdp_port"].asUInt();
// int ssh_port = jsRoot["rdp_port"].asUInt();
// std::string server_ip;
// if (isIPAddress(host.c_str()))
// {
// server_ip = host;
// }
// else
// {
// char *ptr, **pptr;
// struct hostent *hptr;
// char IP[128] = { 0 };
// /* 取得命令后第一个参数,即要解析的域名或主机名 */
// ptr = (char*)host.c_str();
// /* 调用gethostbyname()。调用结果都存在hptr中 */
// if ((hptr = gethostbyname(ptr)) == NULL)
// {
// //printf("gethostbyname error for host:%s/n", ptr);
// _create_json_ret(buf, TPE_PARAM);
// return;
// }
//
// char szbuf[1204] = { 0 };
// switch (hptr->h_addrtype)
// {
// case AF_INET:
// case AF_INET6:
// pptr = hptr->h_addr_list;
// for (; *pptr != NULL; pptr++)
// ex_inet_ntop(hptr->h_addrtype, *pptr, IP, sizeof(IP));
// server_ip = IP;
// break;
// default:
// printf("unknown address type/n");
// break;
// }
// }
// if (!isIPAddress(server_ip.c_str()))
// {
// _create_json_ret(buf, TPE_PARAM);
// return;
// }
// if (TestTCPPort(server_ip, rdp_port) && TestTCPPort(server_ip, ssh_port))
// {
// _create_json_ret(buf, TPE_OK);
// return;
// }
// ICMPheaderRet temp = { 0 };
// int b_ok = ICMPSendTo(&temp, (char*)server_ip.c_str(), 16, 8);
// if (b_ok == 0)
// {
// _create_json_ret(buf, TPE_OK);
// return;
// }
// else
// {
// _create_json_ret(buf, TPE_NETWORK);
// }
//
// return;
// }
void TsHttpRpc::_rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf)
{

View File

@ -75,7 +75,7 @@ private:
void _create_json_ret(ex_astr& buf, Json::Value& jr_root);
void _rpc_func_run_client(const ex_astr& func_args, ex_astr& buf);
void _rpc_func_check(const ex_astr& func_args, ex_astr& buf);
// void _rpc_func_check(const ex_astr& func_args, ex_astr& buf);
void _rpc_func_rdp_play(const ex_astr& func_args, ex_astr& buf);
void _rpc_func_get_config(const ex_astr& func_args, ex_astr& buf);
void _rpc_func_set_config(const ex_astr& func_args, ex_astr& buf);

View File

@ -47,4 +47,8 @@ void ex_dlclose(EX_DYLIB_HANDLE dylib);
// inet...
int ex_ip4_name(const struct sockaddr_in* src, char* dst, size_t size);
#define EX_IPV4_NAME_LEN 16
#define EX_IPV6_NAME_LEN 46
const char* ex_inet_ntop(int af, const void *src, char *dst, size_t size);
#endif // __LIB_EX_UTIL_H__

View File

@ -290,21 +290,187 @@ void ex_dlclose(EX_DYLIB_HANDLE dylib) {
#endif
}
static int _inet_ntop4(const unsigned char *src, char *dst, size_t size) {
static const char fmt[] = "%u.%u.%u.%u";
char tmp[32];
int l;
l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
if (l <= 0 || (size_t) l >= size) {
return -1;
}
ex_strcpy(dst, size, tmp);
dst[size - 1] = '\0';
return 0;
// static int _inet_ntop4(const unsigned char *src, char *dst, size_t size) {
// static const char fmt[] = "%u.%u.%u.%u";
// char tmp[32];
// int l;
//
// l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
// if (l <= 0 || (size_t) l >= size) {
// return -1;
// }
// ex_strcpy(dst, size, tmp);
// dst[size - 1] = '\0';
// return 0;
// }
//
// int ex_ip4_name(const struct sockaddr_in *src, char *dst, size_t size) {
// return _inet_ntop4((const unsigned char *) &(src->sin_addr), dst, size);
// }
//
static const char * _inet_ntop_v4(const void *src, char *dst, size_t size)
{
const char digits[] = "0123456789";
int i;
struct in_addr *addr = (struct in_addr *)src;
u_long a = ntohl(addr->s_addr);
const char *orig_dst = dst;
if (size < EX_IPV4_NAME_LEN) {
//errno = ENOSPC;
return NULL;
}
for (i = 0; i < 4; ++i) {
int n = (a >> (24 - i * 8)) & 0xFF;
int non_zerop = 0;
if (non_zerop || n / 100 > 0) {
*dst++ = digits[n / 100];
n %= 100;
non_zerop = 1;
}
if (non_zerop || n / 10 > 0) {
*dst++ = digits[n / 10];
n %= 10;
non_zerop = 1;
}
*dst++ = digits[n];
if (i != 3)
*dst++ = '.';
}
*dst++ = '\0';
return orig_dst;
}
#define IN6ADDRSZ 16
#define INT16SZ 2
static const char * _inet_ntop_v6(const ex_u8 *src, char *dst, size_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
* to contain a value of the specified size. On some systems, like
* Crays, there is no such thing as an integer variable with 16 bits.
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
char tmp[EX_IPV6_NAME_LEN];
char *tp;
struct {
long base;
long len;
} best, cur;
u_long words[IN6ADDRSZ / INT16SZ];
int i;
/* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
memset(words, 0, sizeof(words));
for (i = 0; i < IN6ADDRSZ; i++)
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
best.base = -1;
cur.base = -1;
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
{
if (words[i] == 0)
{
if (cur.base == -1)
cur.base = i, cur.len = 1;
else cur.len++;
}
else if (cur.base != -1)
{
if (best.base == -1 || cur.len > best.len)
best = cur;
cur.base = -1;
}
}
if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
best = cur;
if (best.base != -1 && best.len < 2)
best.base = -1;
/* Format the result.
*/
tp = tmp;
size_t tmp_size = 0;
size_t offset = 0;
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
{
/* Are we inside the best run of 0x00's?
*/
if (best.base != -1 && i >= best.base && i < (best.base + best.len))
{
if (i == best.base) {
*tp++ = ':';
offset += 1;
}
continue;
}
/* Are we following an initial run of 0x00s or any real hex?
*/
if (i != 0) {
*tp++ = ':';
offset += 1;
}
/* Is this address an encapsulated IPv4?
*/
if (i == 6 && best.base == 0 &&
(best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
{
if (!_inet_ntop_v4(src + 12, tp, sizeof(tmp) - (tp - tmp)))
{
//errno = ENOSPC;
return (NULL);
}
tmp_size = strlen(tp);
tp += tmp_size;
offset += tmp_size;
break;
}
//tp += ex_strformat(tp, "%lX", words[i]);
tmp_size = ex_strformat(tp, EX_IPV6_NAME_LEN-offset, "%lX", words[i]);
tp += tmp_size;
offset += tmp_size;
}
/* Was it a trailing run of 0x00's?
*/
if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
*tp++ = ':';
*tp++ = '\0';
/* Check for overflow, copy, and we're done.
*/
if ((size_t)(tp - tmp) > size)
{
//errno = ENOSPC;
return (NULL);
}
//return strcpy(dst, tmp);
return ex_strcpy(dst, size, tmp);
//return (NULL);
}
const char* ex_inet_ntop(int af, const void *src, char *dst, size_t size) {
switch (af) {
case AF_INET:
return _inet_ntop_v4(src, dst, size);
case AF_INET6:
return _inet_ntop_v6((const ex_u8*)src, dst, size);
default:
errno = EAFNOSUPPORT;
return NULL;
}
}
int ex_ip4_name(const struct sockaddr_in *src, char *dst, size_t size) {
return _inet_ntop4((const unsigned char *) &(src->sin_addr), dst, size);
if (NULL == _inet_ntop_v4((const unsigned char *)&(src->sin_addr), dst, size))
return -1;
return 0;
}