2017-07-29 20:32:26 +08:00
|
|
|
/*
|
|
|
|
* comm.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jul 29, 2017
|
|
|
|
* Author: wangyu
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "log.h"
|
2017-09-24 03:14:08 -05:00
|
|
|
#include "misc.h"
|
2017-07-29 20:32:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
static int random_number_fd=-1;
|
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
int force_socket_buf=0;
|
|
|
|
|
|
|
|
int address_t::from_str(char *str)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
char ip_addr_str[100];u32_t port;
|
|
|
|
mylog(log_info,"parsing address: %s\n",str);
|
|
|
|
int is_ipv6=0;
|
|
|
|
if(sscanf(str, "[%[^]]]:%u", ip_addr_str,&port)==2)
|
|
|
|
{
|
|
|
|
mylog(log_info,"its an ipv6 adress\n");
|
|
|
|
inner.ipv6.sin6_family=AF_INET6;
|
|
|
|
is_ipv6=1;
|
|
|
|
}
|
|
|
|
else if(sscanf(str, "%[^:]:%u", ip_addr_str,&port)==2)
|
|
|
|
{
|
|
|
|
mylog(log_info,"its an ipv4 adress\n");
|
|
|
|
inner.ipv4.sin_family=AF_INET;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_error,"failed to parse\n");
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mylog(log_info,"ip_address is {%s}, port is {%u}\n",ip_addr_str,port);
|
|
|
|
|
|
|
|
if(port>65535)
|
|
|
|
{
|
|
|
|
mylog(log_error,"invalid port: %d\n",port);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret=-100;
|
|
|
|
if(is_ipv6)
|
|
|
|
{
|
|
|
|
ret=inet_pton(AF_INET6, ip_addr_str,&(inner.ipv6.sin6_addr));
|
|
|
|
inner.ipv6.sin6_port=htons(port);
|
|
|
|
if(ret==0) // 0 if address type doesnt match
|
|
|
|
{
|
|
|
|
mylog(log_error,"ip_addr %s is not an ipv6 address, %d\n",ip_addr_str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
else if(ret==1) // inet_pton returns 1 on success
|
|
|
|
{
|
|
|
|
//okay
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_error,"ip_addr %s is invalid, %d\n",ip_addr_str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret=inet_pton(AF_INET, ip_addr_str,&(inner.ipv4.sin_addr));
|
|
|
|
inner.ipv4.sin_port=htons(port);
|
|
|
|
|
|
|
|
if(ret==0)
|
|
|
|
{
|
|
|
|
mylog(log_error,"ip_addr %s is not an ipv4 address, %d\n",ip_addr_str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
else if(ret==1)
|
|
|
|
{
|
|
|
|
//okay
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_error,"ip_addr %s is invalid, %d\n",ip_addr_str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-23 09:50:21 -05:00
|
|
|
int address_t::from_str_ip_only(char * str)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
u32_t type;
|
|
|
|
|
|
|
|
if(strchr(str,':')==NULL)
|
|
|
|
type=AF_INET;
|
|
|
|
else
|
|
|
|
type=AF_INET6;
|
|
|
|
|
|
|
|
((sockaddr*)&inner)->sa_family=type;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
if(type==AF_INET)
|
|
|
|
{
|
|
|
|
ret=inet_pton(type, str,&inner.ipv4.sin_addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret=inet_pton(type, str,&inner.ipv6.sin6_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret==0) // 0 if address type doesnt match
|
|
|
|
{
|
|
|
|
mylog(log_error,"confusion in parsing %s, %d\n",str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
else if(ret==1) // inet_pton returns 1 on success
|
|
|
|
{
|
|
|
|
//okay
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_error,"ip_addr %s is invalid, %d\n",str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
char * address_t::get_str()
|
|
|
|
{
|
|
|
|
static char res[max_addr_len];
|
|
|
|
to_str(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
void address_t::to_str(char * s)
|
|
|
|
{
|
|
|
|
//static char res[max_addr_len];
|
|
|
|
char ip_addr[max_addr_len];
|
|
|
|
u32_t port;
|
|
|
|
const char * ret=0;
|
|
|
|
if(get_type()==AF_INET6)
|
|
|
|
{
|
|
|
|
ret=inet_ntop(AF_INET6, &inner.ipv6.sin6_addr, ip_addr,max_addr_len);
|
|
|
|
port=inner.ipv6.sin6_port;
|
|
|
|
}
|
|
|
|
else if(get_type()==AF_INET)
|
|
|
|
{
|
|
|
|
ret=inet_ntop(AF_INET, &inner.ipv4.sin_addr, ip_addr,max_addr_len);
|
|
|
|
port=inner.ipv4.sin_port;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret==0) //NULL on failure
|
|
|
|
{
|
|
|
|
mylog(log_error,"inet_ntop failed\n");
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
port=ntohs(port);
|
|
|
|
|
|
|
|
ip_addr[max_addr_len-1]=0;
|
|
|
|
if(get_type()==AF_INET6)
|
|
|
|
{
|
|
|
|
sprintf(s,"[%s]:%u",ip_addr,(u32_t)port);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
sprintf(s,"%s:%u",ip_addr,(u32_t)port);
|
|
|
|
}
|
|
|
|
|
|
|
|
//return res;
|
|
|
|
}
|
|
|
|
|
2018-07-17 11:33:02 -05:00
|
|
|
char* address_t::get_ip()
|
|
|
|
{
|
|
|
|
char ip_addr[max_addr_len];
|
|
|
|
static char s[max_addr_len];
|
|
|
|
const char * ret=0;
|
|
|
|
if(get_type()==AF_INET6)
|
|
|
|
{
|
|
|
|
ret=inet_ntop(AF_INET6, &inner.ipv6.sin6_addr, ip_addr,max_addr_len);
|
|
|
|
}
|
|
|
|
else if(get_type()==AF_INET)
|
|
|
|
{
|
|
|
|
ret=inet_ntop(AF_INET, &inner.ipv4.sin_addr, ip_addr,max_addr_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret==0) //NULL on failure
|
|
|
|
{
|
|
|
|
mylog(log_error,"inet_ntop failed\n");
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ip_addr[max_addr_len-1]=0;
|
|
|
|
if(get_type()==AF_INET6)
|
|
|
|
{
|
2018-07-24 05:36:56 -05:00
|
|
|
sprintf(s,"%s",ip_addr);
|
2018-07-17 11:33:02 -05:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
sprintf(s,"%s",ip_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
int address_t::from_sockaddr(sockaddr * addr,socklen_t slen)
|
|
|
|
{
|
2018-07-20 04:35:25 -05:00
|
|
|
clear();
|
|
|
|
//memset(&inner,0,sizeof(inner));
|
2018-07-17 02:31:14 -05:00
|
|
|
if(addr->sa_family==AF_INET6)
|
|
|
|
{
|
|
|
|
assert(slen==sizeof(sockaddr_in6));
|
2018-07-20 04:35:25 -05:00
|
|
|
//inner.ipv6= *( (sockaddr_in6*) addr );
|
|
|
|
memcpy(&inner,addr,slen);
|
2018-07-17 02:31:14 -05:00
|
|
|
}
|
|
|
|
else if(addr->sa_family==AF_INET)
|
|
|
|
{
|
|
|
|
assert(slen==sizeof(sockaddr_in));
|
2018-07-20 04:35:25 -05:00
|
|
|
//inner.ipv4= *( (sockaddr_in*) addr );
|
|
|
|
memcpy(&inner,addr,slen);
|
2018-07-17 02:31:14 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int address_t::new_connected_udp_fd()
|
|
|
|
{
|
|
|
|
|
|
|
|
int new_udp_fd;
|
|
|
|
new_udp_fd = socket(get_type(), SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (new_udp_fd < 0) {
|
|
|
|
mylog(log_warn, "create udp_fd error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
setnonblocking(new_udp_fd);
|
|
|
|
set_buf_size(new_udp_fd,socket_buf_size);
|
|
|
|
|
|
|
|
mylog(log_debug, "created new udp_fd %d\n", new_udp_fd);
|
|
|
|
int ret = connect(new_udp_fd, (struct sockaddr *) &inner, get_len());
|
|
|
|
if (ret != 0) {
|
|
|
|
mylog(log_warn, "udp fd connect fail %d %s\n",ret,strerror(errno) );
|
|
|
|
//sock_close(new_udp_fd);
|
|
|
|
close(new_udp_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_udp_fd;
|
|
|
|
}
|
|
|
|
|
2018-07-23 09:50:21 -05:00
|
|
|
bool my_ip_t::equal (const my_ip_t &b) const
|
|
|
|
{
|
|
|
|
//extern int raw_ip_version;
|
|
|
|
if(raw_ip_version==AF_INET)
|
|
|
|
{
|
|
|
|
return v4==b.v4;
|
2018-07-24 05:36:56 -05:00
|
|
|
}else if(raw_ip_version==AF_INET6)
|
2018-07-23 09:50:21 -05:00
|
|
|
{
|
2018-07-24 05:36:56 -05:00
|
|
|
return memcmp(&v6,&b.v6,sizeof(v6))==0;
|
2018-07-23 09:50:21 -05:00
|
|
|
}
|
|
|
|
assert(0==1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char * my_ip_t::get_str1() const
|
|
|
|
{
|
|
|
|
static char res[max_addr_len];
|
|
|
|
if(raw_ip_version==AF_INET6)
|
|
|
|
{
|
|
|
|
assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0);
|
|
|
|
}
|
2018-08-18 12:55:02 -05:00
|
|
|
else
|
2018-07-23 09:50:21 -05:00
|
|
|
{
|
2018-08-18 12:55:02 -05:00
|
|
|
assert(raw_ip_version==AF_INET);
|
2018-07-23 09:50:21 -05:00
|
|
|
assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
char * my_ip_t::get_str2() const
|
|
|
|
{
|
|
|
|
static char res[max_addr_len];
|
|
|
|
if(raw_ip_version==AF_INET6)
|
|
|
|
{
|
|
|
|
assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0);
|
|
|
|
}
|
2018-08-18 12:55:02 -05:00
|
|
|
else
|
2018-07-23 09:50:21 -05:00
|
|
|
{
|
2018-08-18 12:55:02 -05:00
|
|
|
assert(raw_ip_version==AF_INET);
|
2018-07-23 09:50:21 -05:00
|
|
|
assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-07-24 05:55:59 -05:00
|
|
|
|
|
|
|
int my_ip_t::from_address_t(address_t tmp_addr)
|
|
|
|
{
|
|
|
|
if(tmp_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET)
|
|
|
|
{
|
|
|
|
v4=tmp_addr.inner.ipv4.sin_addr.s_addr;
|
|
|
|
}
|
|
|
|
else if(tmp_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET6)
|
|
|
|
{
|
|
|
|
v6=tmp_addr.inner.ipv6.sin6_addr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-23 09:50:21 -05:00
|
|
|
/*
|
|
|
|
int my_ip_t::from_str(char * str)
|
|
|
|
{
|
|
|
|
u32_t type;
|
|
|
|
|
|
|
|
if(strchr(str,':')==NULL)
|
|
|
|
type=AF_INET;
|
|
|
|
else
|
|
|
|
type=AF_INET6;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
ret=inet_pton(type, str,this);
|
2018-07-17 02:31:14 -05:00
|
|
|
|
2018-07-23 09:50:21 -05:00
|
|
|
if(ret==0) // 0 if address type doesnt match
|
|
|
|
{
|
|
|
|
mylog(log_error,"confusion in parsing %s, %d\n",str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
else if(ret==1) // inet_pton returns 1 on success
|
|
|
|
{
|
|
|
|
//okay
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_error,"ip_addr %s is invalid, %d\n",str,ret);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}*/
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t get_current_time()
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
|
|
|
timespec tmp_time;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &tmp_time);
|
2017-11-24 11:05:13 -06:00
|
|
|
return ((u64_t)tmp_time.tv_sec)*1000llu+((u64_t)tmp_time.tv_nsec)/(1000*1000llu);
|
2017-07-29 20:32:26 +08:00
|
|
|
}
|
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t pack_u64(u32_t a,u32_t b)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t ret=a;
|
2017-07-29 20:32:26 +08:00
|
|
|
ret<<=32u;
|
|
|
|
ret+=b;
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t get_u64_h(u64_t a)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
|
|
|
return a>>32u;
|
|
|
|
}
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t get_u64_l(u64_t a)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
|
|
|
return (a<<32u)>>32u;
|
|
|
|
}
|
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
char * my_ntoa(u32_t ip)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
|
|
|
in_addr a;
|
|
|
|
a.s_addr=ip;
|
|
|
|
return inet_ntoa(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_random_number_fd()
|
|
|
|
{
|
2017-08-04 04:22:16 +08:00
|
|
|
|
2017-07-29 20:32:26 +08:00
|
|
|
random_number_fd=open("/dev/urandom",O_RDONLY);
|
2017-08-04 04:22:16 +08:00
|
|
|
|
2017-07-29 20:32:26 +08:00
|
|
|
if(random_number_fd==-1)
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"error open /dev/urandom\n");
|
2017-08-04 11:51:39 +08:00
|
|
|
myexit(-1);
|
2017-07-29 20:32:26 +08:00
|
|
|
}
|
2017-08-04 04:22:16 +08:00
|
|
|
setnonblocking(random_number_fd);
|
2017-07-29 20:32:26 +08:00
|
|
|
}
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t get_true_random_number_64()
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t ret;
|
2017-08-04 04:22:16 +08:00
|
|
|
int size=read(random_number_fd,&ret,sizeof(ret));
|
|
|
|
if(size!=sizeof(ret))
|
|
|
|
{
|
2017-08-04 17:12:23 +08:00
|
|
|
mylog(log_fatal,"get random number failed %d\n",size);
|
2017-08-04 11:51:39 +08:00
|
|
|
myexit(-1);
|
2017-08-04 04:22:16 +08:00
|
|
|
}
|
|
|
|
|
2017-07-29 20:32:26 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t get_true_random_number()
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t ret;
|
2017-08-04 04:22:16 +08:00
|
|
|
int size=read(random_number_fd,&ret,sizeof(ret));
|
|
|
|
if(size!=sizeof(ret))
|
|
|
|
{
|
2017-08-04 17:12:23 +08:00
|
|
|
mylog(log_fatal,"get random number failed %d\n",size);
|
2017-08-04 11:51:39 +08:00
|
|
|
myexit(-1);
|
2017-08-04 04:22:16 +08:00
|
|
|
}
|
2017-07-29 20:32:26 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t get_true_random_number_nz() //nz for non-zero
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t ret=0;
|
2017-07-29 20:32:26 +08:00
|
|
|
while(ret==0)
|
|
|
|
{
|
2017-07-29 21:22:13 +08:00
|
|
|
ret=get_true_random_number();
|
2017-07-29 20:32:26 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2018-07-27 02:26:24 -05:00
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t ntoh64(u64_t a)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2018-07-27 02:26:24 -05:00
|
|
|
#ifdef UDP2RAW_LITTLE_ENDIAN
|
|
|
|
u32_t h=get_u64_h(a);
|
|
|
|
u32_t l=get_u64_l(a);
|
|
|
|
return pack_u64(ntohl(l),ntohl(h));
|
|
|
|
//return bswap_64( a);
|
|
|
|
#else
|
|
|
|
return a;
|
|
|
|
#endif
|
2017-07-29 20:32:26 +08:00
|
|
|
|
|
|
|
}
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t hton64(u64_t a)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2018-07-27 02:26:24 -05:00
|
|
|
return ntoh64(a);
|
2017-07-29 20:32:26 +08:00
|
|
|
}
|
|
|
|
|
2018-07-27 02:26:24 -05:00
|
|
|
|
2018-07-20 09:29:10 -05:00
|
|
|
void write_u16(char * p,u16_t w)
|
|
|
|
{
|
|
|
|
*(unsigned char*)(p + 1) = (w & 0xff);
|
|
|
|
*(unsigned char*)(p + 0) = (w >> 8);
|
|
|
|
}
|
|
|
|
u16_t read_u16(char * p)
|
|
|
|
{
|
|
|
|
u16_t res;
|
|
|
|
res = *(const unsigned char*)(p + 0);
|
|
|
|
res = *(const unsigned char*)(p + 1) + (res << 8);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u32(char * p,u32_t l)
|
|
|
|
{
|
|
|
|
*(unsigned char*)(p + 3) = (unsigned char)((l >> 0) & 0xff);
|
|
|
|
*(unsigned char*)(p + 2) = (unsigned char)((l >> 8) & 0xff);
|
|
|
|
*(unsigned char*)(p + 1) = (unsigned char)((l >> 16) & 0xff);
|
|
|
|
*(unsigned char*)(p + 0) = (unsigned char)((l >> 24) & 0xff);
|
|
|
|
}
|
|
|
|
u32_t read_u32(char * p)
|
|
|
|
{
|
|
|
|
u32_t res;
|
|
|
|
res = *(const unsigned char*)(p + 0);
|
|
|
|
res = *(const unsigned char*)(p + 1) + (res << 8);
|
|
|
|
res = *(const unsigned char*)(p + 2) + (res << 8);
|
|
|
|
res = *(const unsigned char*)(p + 3) + (res << 8);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u64(char * s,u64_t a)
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
u64_t read_u64(char * s)
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:32:26 +08:00
|
|
|
void setnonblocking(int sock) {
|
|
|
|
int opts;
|
|
|
|
opts = fcntl(sock, F_GETFL);
|
|
|
|
|
|
|
|
if (opts < 0) {
|
|
|
|
mylog(log_fatal,"fcntl(sock,GETFL)\n");
|
|
|
|
//perror("fcntl(sock,GETFL)");
|
2017-08-04 11:51:39 +08:00
|
|
|
myexit(1);
|
2017-07-29 20:32:26 +08:00
|
|
|
}
|
|
|
|
opts = opts | O_NONBLOCK;
|
|
|
|
if (fcntl(sock, F_SETFL, opts) < 0) {
|
|
|
|
mylog(log_fatal,"fcntl(sock,SETFL,opts)\n");
|
|
|
|
//perror("fcntl(sock,SETFL,opts)");
|
2017-08-04 11:51:39 +08:00
|
|
|
myexit(1);
|
2017-07-29 20:32:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Generic checksum calculation function
|
|
|
|
*/
|
2017-08-28 17:41:31 -05:00
|
|
|
unsigned short csum(const unsigned short *ptr,int nbytes) {//works both for big and little endian
|
2017-07-29 20:32:26 +08:00
|
|
|
register long sum;
|
|
|
|
unsigned short oddbyte;
|
|
|
|
register short answer;
|
|
|
|
|
|
|
|
sum=0;
|
|
|
|
while(nbytes>1) {
|
|
|
|
sum+=*ptr++;
|
|
|
|
nbytes-=2;
|
|
|
|
}
|
|
|
|
if(nbytes==1) {
|
|
|
|
oddbyte=0;
|
|
|
|
*((u_char*)&oddbyte)=*(u_char*)ptr;
|
|
|
|
sum+=oddbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = (sum>>16)+(sum & 0xffff);
|
|
|
|
sum = sum + (sum>>16);
|
|
|
|
answer=(short)~sum;
|
|
|
|
|
|
|
|
return(answer);
|
|
|
|
}
|
|
|
|
|
2018-07-20 09:29:10 -05:00
|
|
|
unsigned short csum_with_header(char* header,int hlen,const unsigned short *ptr,int nbytes) {//works both for big and little endian
|
|
|
|
|
|
|
|
long sum;
|
|
|
|
unsigned short oddbyte;
|
|
|
|
short answer;
|
|
|
|
|
|
|
|
assert(hlen%2==0);
|
|
|
|
|
|
|
|
sum=0;
|
|
|
|
unsigned short * tmp= (unsigned short *)header;
|
|
|
|
for(int i=0;i<hlen/2;i++)
|
|
|
|
{
|
|
|
|
sum+=*tmp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while(nbytes>1) {
|
|
|
|
sum+=*ptr++;
|
|
|
|
nbytes-=2;
|
|
|
|
}
|
|
|
|
if(nbytes==1) {
|
|
|
|
oddbyte=0;
|
|
|
|
*((u_char*)&oddbyte)=*(u_char*)ptr;
|
|
|
|
sum+=oddbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
sum = (sum>>16)+(sum & 0xffff);
|
|
|
|
sum = sum + (sum>>16);
|
|
|
|
answer=(short)~sum;
|
|
|
|
|
|
|
|
return(answer);
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
int set_buf_size(int fd,int socket_buf_size)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2017-09-04 03:21:34 -05:00
|
|
|
if(force_socket_buf)
|
|
|
|
{
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_SNDBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"SO_SNDBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
|
|
|
|
myexit(1);
|
|
|
|
}
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &socket_buf_size, sizeof(socket_buf_size))<0)
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"SO_RCVBUFFORCE fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
|
|
|
|
myexit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"SO_SNDBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
|
|
|
|
myexit(1);
|
|
|
|
}
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &socket_buf_size, sizeof(socket_buf_size))<0)
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"SO_RCVBUF fail socket_buf_size=%d errno=%s\n",socket_buf_size,strerror(errno));
|
|
|
|
myexit(1);
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 20:32:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-26 12:41:55 -05:00
|
|
|
int numbers_to_char(my_id_t id1,my_id_t id2,my_id_t id3,char * &data,int &len)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
|
|
|
static char buf[buf_len];
|
|
|
|
data=buf;
|
2018-07-26 12:41:55 -05:00
|
|
|
my_id_t tmp=htonl(id1);
|
2017-07-29 20:32:26 +08:00
|
|
|
memcpy(buf,&tmp,sizeof(tmp));
|
|
|
|
|
|
|
|
tmp=htonl(id2);
|
|
|
|
memcpy(buf+sizeof(tmp),&tmp,sizeof(tmp));
|
|
|
|
|
|
|
|
tmp=htonl(id3);
|
|
|
|
memcpy(buf+sizeof(tmp)*2,&tmp,sizeof(tmp));
|
|
|
|
|
2018-07-26 12:41:55 -05:00
|
|
|
len=sizeof(my_id_t)*3;
|
2017-07-29 20:32:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-26 12:41:55 -05:00
|
|
|
int char_to_numbers(const char * data,int len,my_id_t &id1,my_id_t &id2,my_id_t &id3)
|
2017-07-29 20:32:26 +08:00
|
|
|
{
|
2018-07-26 12:41:55 -05:00
|
|
|
if(len<int(sizeof(my_id_t)*3)) return -1;
|
2017-08-31 10:36:33 -05:00
|
|
|
//id1=ntohl( *((id_t*)(data+0)) );
|
|
|
|
memcpy(&id1,data+0,sizeof(id1));
|
|
|
|
id1=ntohl(id1);
|
|
|
|
//id2=ntohl( *((id_t*)(data+sizeof(id_t))) );
|
2018-07-26 12:41:55 -05:00
|
|
|
memcpy(&id2,data+sizeof(my_id_t),sizeof(id2));
|
2017-08-31 10:36:33 -05:00
|
|
|
id2=ntohl(id2);
|
|
|
|
//id3=ntohl( *((id_t*)(data+sizeof(id_t)*2)) );
|
2018-07-26 12:41:55 -05:00
|
|
|
memcpy(&id3,data+sizeof(my_id_t)*2,sizeof(id3));
|
2017-08-31 10:36:33 -05:00
|
|
|
id3=ntohl(id3);
|
2017-07-29 20:32:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-08-26 05:38:33 -05:00
|
|
|
int hex_to_u32(const string & a,u32_t &output)
|
|
|
|
{
|
|
|
|
//string b="0x";
|
|
|
|
//b+=a;
|
|
|
|
if(sscanf(a.c_str(),"%x",&output)==1)
|
|
|
|
{
|
|
|
|
//printf("%s %x\n",a.c_str(),output);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mylog(log_error,"<%s> doesnt contain a hex\n",a.c_str());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int hex_to_u32_with_endian(const string & a,u32_t &output)
|
|
|
|
{
|
|
|
|
//string b="0x";
|
|
|
|
//b+=a;
|
|
|
|
if(sscanf(a.c_str(),"%x",&output)==1)
|
|
|
|
{
|
|
|
|
output=htonl(output);
|
|
|
|
//printf("%s %x\n",a.c_str(),output);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mylog(log_error,"<%s> doesnt contain a hex\n",a.c_str());
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-04 17:12:23 +08:00
|
|
|
bool larger_than_u32(u32_t a,u32_t b)
|
2017-08-04 12:46:46 +08:00
|
|
|
{
|
2018-07-27 02:26:24 -05:00
|
|
|
return ((i32_t(a-b)) >0);
|
|
|
|
/*
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t smaller,bigger;
|
2017-08-04 12:46:46 +08:00
|
|
|
smaller=min(a,b);//smaller in normal sense
|
|
|
|
bigger=max(a,b);
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1));
|
2017-08-04 12:46:46 +08:00
|
|
|
if(distance==bigger-smaller)
|
|
|
|
{
|
|
|
|
if(bigger==a)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(smaller==b)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 02:26:24 -05:00
|
|
|
*/
|
2017-08-04 12:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool larger_than_u16(uint16_t a,uint16_t b)
|
|
|
|
{
|
2018-07-27 02:26:24 -05:00
|
|
|
return ((i16_t(a-b)) >0);
|
|
|
|
/*
|
2017-08-04 12:46:46 +08:00
|
|
|
uint16_t smaller,bigger;
|
|
|
|
smaller=min(a,b);//smaller in normal sense
|
|
|
|
bigger=max(a,b);
|
|
|
|
uint16_t distance=min(bigger-smaller,smaller+(0xffff-bigger+1));
|
|
|
|
if(distance==bigger-smaller)
|
|
|
|
{
|
|
|
|
if(bigger==a)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(smaller==b)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2018-07-27 02:26:24 -05:00
|
|
|
}*/
|
2017-08-04 12:46:46 +08:00
|
|
|
}
|
2017-09-24 03:14:08 -05:00
|
|
|
|
|
|
|
void myexit(int a)
|
|
|
|
{
|
|
|
|
if(enable_log_color)
|
|
|
|
printf("%s\n",RESET);
|
|
|
|
if(keep_thread_running)
|
|
|
|
{
|
|
|
|
if(pthread_cancel(keep_thread))
|
|
|
|
{
|
|
|
|
mylog(log_warn,"pthread_cancel failed\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_info,"pthread_cancel success\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clear_iptables_rule();
|
|
|
|
exit(a);
|
|
|
|
}
|
|
|
|
|
2017-08-17 23:40:17 +08:00
|
|
|
vector<string> string_to_vec(const char * s,const char * sp) {
|
|
|
|
vector<string> res;
|
|
|
|
string str=s;
|
|
|
|
char *p = strtok ((char *)str.c_str(),sp);
|
|
|
|
while (p != NULL)
|
|
|
|
{
|
|
|
|
res.push_back(p);
|
|
|
|
//printf ("%s\n",p);
|
2017-08-26 05:38:33 -05:00
|
|
|
p = strtok(NULL, sp);
|
2017-08-17 23:40:17 +08:00
|
|
|
}
|
2017-08-26 05:38:33 -05:00
|
|
|
|
|
|
|
/* for(int i=0;i<(int)res.size();i++)
|
|
|
|
{
|
|
|
|
printf("<<%s>>\n",res[i].c_str());
|
|
|
|
}*/
|
2017-08-17 23:40:17 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector< vector <string> > string_to_vec2(const char * s)
|
|
|
|
{
|
|
|
|
vector< vector <string> > res;
|
|
|
|
vector<string> lines=string_to_vec(s,"\n");
|
|
|
|
for(int i=0;i<int(lines.size());i++)
|
|
|
|
{
|
|
|
|
vector<string> tmp;
|
|
|
|
tmp=string_to_vec(lines[i].c_str(),"\t ");
|
|
|
|
res.push_back(tmp);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2017-08-26 05:38:33 -05:00
|
|
|
int read_file(const char * file,string &output)
|
2017-08-17 23:40:17 +08:00
|
|
|
{
|
2017-08-26 06:35:47 -05:00
|
|
|
const int max_len=3*1024*1024;
|
|
|
|
// static char buf[max_len+100];
|
|
|
|
string buf0;
|
|
|
|
buf0.reserve(max_len+200);
|
2017-08-26 06:56:30 -05:00
|
|
|
char * buf=(char *)buf0.c_str();
|
2017-08-26 06:35:47 -05:00
|
|
|
buf[max_len]=0;
|
|
|
|
//buf[sizeof(buf)-1]=0;
|
2017-08-17 23:40:17 +08:00
|
|
|
int fd=open(file,O_RDONLY);
|
|
|
|
if(fd==-1)
|
|
|
|
{
|
|
|
|
mylog(log_error,"read_file %s fail\n",file);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-26 05:38:33 -05:00
|
|
|
int len=read(fd,buf,max_len);
|
|
|
|
if(len==max_len)
|
2017-08-17 23:40:17 +08:00
|
|
|
{
|
|
|
|
buf[0]=0;
|
2017-08-26 05:38:33 -05:00
|
|
|
mylog(log_error,"%s too long,buf not large enough\n",file);
|
2017-08-17 23:40:17 +08:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else if(len<0)
|
|
|
|
{
|
|
|
|
buf[0]=0;
|
2017-08-26 05:38:33 -05:00
|
|
|
mylog(log_error,"%s read fail %d\n",file,len);
|
2017-08-17 23:40:17 +08:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[len]=0;
|
2017-08-26 05:38:33 -05:00
|
|
|
output=buf;
|
2017-08-17 23:40:17 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-20 16:28:23 +08:00
|
|
|
int run_command(string command0,char * &output,int flag) {
|
2017-08-17 23:40:17 +08:00
|
|
|
FILE *in;
|
2017-08-20 16:28:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
if((flag&show_log)==0) command0+=" 2>&1 ";
|
|
|
|
|
|
|
|
const char * command=command0.c_str();
|
|
|
|
|
|
|
|
int level= (flag&show_log)?log_warn:log_debug;
|
|
|
|
|
|
|
|
if(flag&show_command)
|
|
|
|
{
|
|
|
|
mylog(log_info,"run_command %s\n",command);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_debug,"run_command %s\n",command);
|
|
|
|
}
|
2017-08-21 20:26:55 +08:00
|
|
|
static __thread char buf[1024*1024+100];
|
2017-08-17 23:40:17 +08:00
|
|
|
buf[sizeof(buf)-1]=0;
|
|
|
|
if(!(in = popen(command, "r"))){
|
2017-08-20 16:28:23 +08:00
|
|
|
mylog(level,"command %s popen failed,errno %s\n",command,strerror(errno));
|
2017-08-17 23:40:17 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len =fread(buf, 1024*1024, 1, in);
|
|
|
|
if(len==1024*1024)
|
|
|
|
{
|
|
|
|
buf[0]=0;
|
2017-08-20 16:28:23 +08:00
|
|
|
mylog(level,"too long,buf not larger enough\n");
|
2017-08-17 23:40:17 +08:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[len]=0;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
if(( ret=ferror(in) ))
|
|
|
|
{
|
2017-08-20 16:28:23 +08:00
|
|
|
mylog(level,"command %s fread failed,ferror return value %d \n",command,ret);
|
|
|
|
return -3;
|
2017-08-17 23:40:17 +08:00
|
|
|
}
|
|
|
|
//if(output!=0)
|
|
|
|
output=buf;
|
|
|
|
ret= pclose(in);
|
|
|
|
|
|
|
|
int ret2=WEXITSTATUS(ret);
|
|
|
|
|
|
|
|
if(ret!=0||ret2!=0)
|
|
|
|
{
|
2017-08-20 16:28:23 +08:00
|
|
|
mylog(level,"commnad %s ,pclose returned %d ,WEXITSTATUS %d,errnor :%s \n",command,ret,ret2,strerror(errno));
|
|
|
|
return -4;
|
2017-08-17 23:40:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2017-08-20 16:28:23 +08:00
|
|
|
/*
|
|
|
|
int run_command_no_log(string command0,char * &output) {
|
|
|
|
FILE *in;
|
|
|
|
command0+=" 2>&1 ";
|
|
|
|
const char * command=command0.c_str();
|
|
|
|
mylog(log_debug,"run_command_no_log %s\n",command);
|
|
|
|
static char buf[1024*1024+100];
|
|
|
|
buf[sizeof(buf)-1]=0;
|
|
|
|
if(!(in = popen(command, "r"))){
|
|
|
|
mylog(log_debug,"command %s popen failed,errno %s\n",command,strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len =fread(buf, 1024*1024, 1, in);
|
|
|
|
if(len==1024*1024)
|
|
|
|
{
|
|
|
|
buf[0]=0;
|
|
|
|
mylog(log_debug,"too long,buf not larger enough\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[len]=0;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
if(( ret=ferror(in) ))
|
|
|
|
{
|
|
|
|
mylog(log_debug,"command %s fread failed,ferror return value %d \n",command,ret);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
//if(output!=0)
|
|
|
|
output=buf;
|
|
|
|
ret= pclose(in);
|
|
|
|
|
|
|
|
int ret2=WEXITSTATUS(ret);
|
|
|
|
|
|
|
|
if(ret!=0||ret2!=0)
|
|
|
|
{
|
|
|
|
mylog(log_debug,"commnad %s ,pclose returned %d ,WEXITSTATUS %d,errnor :%s \n",command,ret,ret2,strerror(errno));
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-08-17 23:40:17 +08:00
|
|
|
|
2017-08-20 16:28:23 +08:00
|
|
|
}*/
|
2017-08-23 10:38:55 +08:00
|
|
|
|
|
|
|
// Remove preceding and trailing characters
|
|
|
|
string trim(const string& str, char c) {
|
|
|
|
size_t first = str.find_first_not_of(c);
|
|
|
|
if(string::npos==first)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
size_t last = str.find_last_not_of(c);
|
|
|
|
return str.substr(first,(last-first+1));
|
|
|
|
}
|
2017-08-23 07:01:21 -05:00
|
|
|
|
2017-08-26 06:35:47 -05:00
|
|
|
vector<string> parse_conf_line(const string& s0)
|
2017-08-23 07:01:21 -05:00
|
|
|
{
|
2017-08-26 06:35:47 -05:00
|
|
|
string s=s0;
|
|
|
|
s.reserve(s.length()+200);
|
2017-08-26 06:56:30 -05:00
|
|
|
char *buf=(char *)s.c_str();
|
2017-08-26 06:35:47 -05:00
|
|
|
//char buf[s.length()+200];
|
2017-08-23 07:01:21 -05:00
|
|
|
char *p=buf;
|
|
|
|
int i=int(s.length())-1;
|
|
|
|
int j;
|
|
|
|
vector<string>res;
|
|
|
|
strcpy(buf,(char *)s.c_str());
|
|
|
|
while(i>=0)
|
|
|
|
{
|
|
|
|
if(buf[i]==' ' || buf[i]== '\t')
|
|
|
|
buf[i]=0;
|
|
|
|
else break;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
while(*p!=0)
|
|
|
|
{
|
|
|
|
if(*p==' ' || *p== '\t')
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
int new_len=strlen(p);
|
|
|
|
if(new_len==0)return res;
|
|
|
|
if(p[0]=='#') return res;
|
|
|
|
if(p[0]!='-')
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"line :<%s> not begin with '-' ",s.c_str());
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0;i<new_len;i++)
|
|
|
|
{
|
|
|
|
if(p[i]==' '||p[i]=='\t')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(i==new_len)
|
|
|
|
{
|
|
|
|
res.push_back(p);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
j=i;
|
|
|
|
while(p[j]==' '||p[j]=='\t')
|
|
|
|
j++;
|
|
|
|
p[i]=0;
|
|
|
|
res.push_back(p);
|
|
|
|
res.push_back(p+j);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-24 09:04:47 -05:00
|
|
|
int create_fifo(char * file)
|
|
|
|
{
|
|
|
|
if(mkfifo (file, 0666)!=0)
|
|
|
|
{
|
|
|
|
if(errno==EEXIST)
|
|
|
|
{
|
|
|
|
mylog(log_warn,"warning fifo file %s exist\n",file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"create fifo file %s failed\n",file);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int fifo_fd=open (file, O_RDWR);
|
|
|
|
if(fifo_fd<0)
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"create fifo file %s failed\n",file);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fifo_fd, &st)!=0)
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"fstat failed for fifo file %s\n",file);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
2017-08-23 07:01:21 -05:00
|
|
|
|
2017-10-24 09:04:47 -05:00
|
|
|
if(!S_ISFIFO(st.st_mode))
|
|
|
|
{
|
|
|
|
mylog(log_fatal,"%s is not a fifo\n",file);
|
|
|
|
myexit(-1);
|
|
|
|
}
|
2017-08-23 07:01:21 -05:00
|
|
|
|
2017-10-24 09:04:47 -05:00
|
|
|
setnonblocking(fifo_fd);
|
|
|
|
return fifo_fd;
|
|
|
|
}
|
2017-08-23 07:01:21 -05:00
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
/*
|
2017-10-30 01:57:27 -05:00
|
|
|
void ip_port_t::from_u64(u64_t u64)
|
|
|
|
{
|
|
|
|
ip=get_u64_h(u64);
|
|
|
|
port=get_u64_l(u64);
|
|
|
|
}
|
|
|
|
u64_t ip_port_t::to_u64()
|
|
|
|
{
|
|
|
|
return pack_u64(ip,port);
|
|
|
|
}
|
|
|
|
char * ip_port_t::to_s()
|
|
|
|
{
|
|
|
|
static char res[40];
|
|
|
|
sprintf(res,"%s:%d",my_ntoa(ip),port);
|
|
|
|
return res;
|
2018-07-17 02:31:14 -05:00
|
|
|
}*/
|
2017-08-23 07:01:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-22 22:28:19 -05:00
|
|
|
void print_binary_chars(const char * a,int len)
|
|
|
|
{
|
|
|
|
for(int i=0;i<len;i++)
|
|
|
|
{
|
|
|
|
unsigned char b=a[i];
|
|
|
|
log_bare(log_debug,"<%02x>",(int)b);
|
|
|
|
}
|
|
|
|
log_bare(log_debug,"\n");
|
|
|
|
}
|
2017-08-23 07:01:21 -05:00
|
|
|
|
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
u32_t djb2(unsigned char *str,int len)
|
|
|
|
{
|
|
|
|
u32_t hash = 5381;
|
|
|
|
int c;
|
|
|
|
int i=0;
|
|
|
|
while(c = *str++,i++!=len)
|
|
|
|
{
|
|
|
|
hash = ((hash << 5) + hash)^c; /* (hash * 33) ^ c */
|
|
|
|
}
|
|
|
|
|
|
|
|
hash=htonl(hash);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32_t sdbm(unsigned char *str,int len)
|
|
|
|
{
|
|
|
|
u32_t hash = 0;
|
|
|
|
int c;
|
|
|
|
int i=0;
|
|
|
|
while(c = *str++,i++!=len)
|
|
|
|
{
|
|
|
|
hash = c + (hash << 6) + (hash << 16) - hash;
|
|
|
|
}
|
|
|
|
//hash=htonl(hash);
|
|
|
|
return hash;
|
|
|
|
}
|
2017-08-23 07:01:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
|