2017-07-29 20:32:26 +08:00
|
|
|
/*
|
|
|
|
* common.h
|
|
|
|
*
|
|
|
|
* Created on: Jul 29, 2017
|
|
|
|
* Author: wangyu
|
|
|
|
*/
|
|
|
|
|
2017-09-01 17:40:55 -05:00
|
|
|
#ifndef UDP2RAW_COMMON_H_
|
|
|
|
#define UDP2RAW_COMMON_H_
|
2017-08-04 17:12:23 +08:00
|
|
|
#define __STDC_FORMAT_MACROS 1
|
2017-08-04 03:29:53 +08:00
|
|
|
#include <inttypes.h>
|
2017-08-04 17:12:23 +08:00
|
|
|
|
2017-07-29 20:32:26 +08:00
|
|
|
#include<stdio.h>
|
|
|
|
#include<string.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
#include<getopt.h>
|
2017-08-04 17:12:23 +08:00
|
|
|
|
2017-07-30 16:57:24 +08:00
|
|
|
#include<unistd.h>
|
2017-07-29 20:32:26 +08:00
|
|
|
#include<errno.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h> //for socket ofcourse
|
|
|
|
#include <sys/types.h>
|
2017-10-23 11:38:22 -05:00
|
|
|
#include <sys/stat.h>
|
2017-07-29 20:32:26 +08:00
|
|
|
#include <stdlib.h> //for exit(0);
|
|
|
|
#include <errno.h> //For errno - the error number
|
2017-10-24 22:58:58 +08:00
|
|
|
#include <netdb.h> // for gethostbyname()
|
2017-07-29 20:32:26 +08:00
|
|
|
#include <netinet/tcp.h> //Provides declarations for tcp header
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/ip.h> //Provides declarations for ip header
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <stdarg.h>
|
2017-07-30 16:37:49 +08:00
|
|
|
#include <assert.h>
|
2017-08-07 13:58:42 +08:00
|
|
|
#include <linux/if_packet.h>
|
2017-08-16 19:44:19 +08:00
|
|
|
#include <byteswap.h>
|
2017-08-20 16:28:23 +08:00
|
|
|
#include <pthread.h>
|
2017-07-30 16:57:24 +08:00
|
|
|
|
|
|
|
#include<unordered_map>
|
2017-09-23 03:35:28 -05:00
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2018-07-19 03:18:07 -05:00
|
|
|
#include <list>
|
2017-07-29 20:32:26 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
|
|
|
|
typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
|
|
|
|
typedef long long i64_t;
|
|
|
|
|
|
|
|
typedef unsigned int u32_t;
|
|
|
|
typedef int i32_t;
|
|
|
|
|
|
|
|
typedef u32_t id_t;
|
2017-07-29 20:32:26 +08:00
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
typedef u64_t iv_t;
|
2017-07-29 20:32:26 +08:00
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
typedef u64_t padding_t;
|
2017-08-01 20:03:29 +08:00
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
typedef u64_t anti_replay_seq_t;
|
2017-07-29 20:32:26 +08:00
|
|
|
|
2018-07-19 03:18:07 -05:00
|
|
|
typedef u64_t my_time_t;
|
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
const int max_addr_len=100;
|
2017-10-30 01:57:27 -05:00
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
extern int force_socket_buf;
|
|
|
|
|
|
|
|
/*
|
2017-10-30 01:57:27 -05:00
|
|
|
struct ip_port_t
|
|
|
|
{
|
|
|
|
u32_t ip;
|
|
|
|
int port;
|
|
|
|
void from_u64(u64_t u64);
|
|
|
|
u64_t to_u64();
|
|
|
|
char * to_s();
|
2018-07-17 02:31:14 -05:00
|
|
|
};*/
|
2017-11-01 08:25:50 -05:00
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
typedef u64_t fd64_t;
|
2017-10-30 01:57:27 -05:00
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
u32_t djb2(unsigned char *str,int len);
|
|
|
|
u32_t sdbm(unsigned char *str,int len);
|
2017-10-30 01:57:27 -05:00
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
struct address_t //TODO scope id
|
|
|
|
{
|
|
|
|
struct hash_function
|
|
|
|
{
|
|
|
|
u32_t operator()(const address_t &key) const
|
|
|
|
{
|
|
|
|
return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
union storage_t //sockaddr_storage is too huge, we dont use it.
|
|
|
|
{
|
|
|
|
sockaddr_in ipv4;
|
|
|
|
sockaddr_in6 ipv6;
|
|
|
|
};
|
|
|
|
storage_t inner;
|
|
|
|
|
|
|
|
address_t()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
memset(&inner,0,sizeof(inner));
|
|
|
|
}
|
2018-07-20 03:44:53 -05:00
|
|
|
int from_ip_port(u32_t ip, int port)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
inner.ipv4.sin_family=AF_INET;
|
|
|
|
inner.ipv4.sin_port=htons(port);
|
|
|
|
inner.ipv4.sin_addr.s_addr=ip;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-17 02:31:14 -05:00
|
|
|
int from_str(char * str);
|
|
|
|
|
|
|
|
int from_sockaddr(sockaddr *,socklen_t);
|
|
|
|
|
|
|
|
char* get_str();
|
|
|
|
void to_str(char *);
|
|
|
|
|
|
|
|
inline u32_t get_type()
|
|
|
|
{
|
|
|
|
return ((sockaddr*)&inner)->sa_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline u32_t get_len()
|
|
|
|
{
|
|
|
|
u32_t type=get_type();
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
return sizeof(sockaddr_in);
|
|
|
|
case AF_INET6:
|
|
|
|
return sizeof(sockaddr_in6);
|
|
|
|
default:
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-17 11:33:02 -05:00
|
|
|
inline u32_t get_port()
|
|
|
|
{
|
|
|
|
u32_t type=get_type();
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
return ntohs(inner.ipv4.sin_port);
|
|
|
|
case AF_INET6:
|
|
|
|
return ntohs(inner.ipv6.sin6_port);
|
|
|
|
default:
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void set_port(int port)
|
|
|
|
{
|
|
|
|
u32_t type=get_type();
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
inner.ipv4.sin_port=htons(port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
inner.ipv6.sin6_port=htons(port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:31:14 -05:00
|
|
|
bool operator == (const address_t &b) const
|
|
|
|
{
|
|
|
|
//return this->data==b.data;
|
|
|
|
return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int new_connected_udp_fd();
|
2018-07-17 11:33:02 -05:00
|
|
|
|
|
|
|
char* get_ip();
|
2018-07-17 02:31:14 -05:00
|
|
|
};
|
2017-10-30 01:57:27 -05:00
|
|
|
|
2018-07-19 01:23:25 -05:00
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
struct hash<address_t>
|
|
|
|
{
|
|
|
|
std::size_t operator()(const address_t& key) const
|
|
|
|
{
|
|
|
|
|
|
|
|
//return address_t::hash_function(k);
|
|
|
|
return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct not_copy_able_t
|
|
|
|
{
|
|
|
|
not_copy_able_t()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
not_copy_able_t(const not_copy_able_t &other)
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
}
|
|
|
|
const not_copy_able_t & operator=(const not_copy_able_t &other)
|
|
|
|
{
|
|
|
|
assert(0==1);
|
|
|
|
return other;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-19 03:18:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-20 06:10:18 -06:00
|
|
|
const int max_data_len=1800;
|
2017-09-24 03:14:08 -05:00
|
|
|
const int buf_len=max_data_len+400;
|
|
|
|
|
2018-06-05 11:26:55 -05:00
|
|
|
const int max_address_len=512;
|
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t get_current_time();
|
|
|
|
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
|
|
|
u32_t get_u64_h(u64_t a);
|
2017-07-29 20:32:26 +08:00
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
u32_t get_u64_l(u64_t a);
|
2017-07-29 20:32:26 +08:00
|
|
|
|
2017-08-04 17:12:23 +08:00
|
|
|
char * my_ntoa(u32_t ip);
|
2017-07-29 20:32:26 +08:00
|
|
|
|
|
|
|
void init_random_number_fd();
|
2017-08-04 17:12:23 +08:00
|
|
|
u64_t get_true_random_number_64();
|
|
|
|
u32_t get_true_random_number();
|
|
|
|
u32_t get_true_random_number_nz();
|
|
|
|
u64_t ntoh64(u64_t a);
|
|
|
|
u64_t hton64(u64_t a);
|
2017-08-04 12:46:46 +08:00
|
|
|
bool larger_than_u16(uint16_t a,uint16_t b);
|
2017-08-04 17:12:23 +08:00
|
|
|
bool larger_than_u32(u32_t a,u32_t b);
|
2017-07-29 20:32:26 +08:00
|
|
|
void setnonblocking(int sock);
|
2018-07-17 02:31:14 -05:00
|
|
|
int set_buf_size(int fd,int socket_buf_size);
|
2017-09-24 03:14:08 -05:00
|
|
|
|
|
|
|
void myexit(int a);
|
2017-07-29 20:32:26 +08:00
|
|
|
|
|
|
|
unsigned short csum(const unsigned short *ptr,int nbytes);
|
|
|
|
|
|
|
|
int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
|
|
|
|
int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3);
|
|
|
|
|
2017-08-20 16:28:23 +08:00
|
|
|
const int show_none=0;
|
|
|
|
const int show_command=0x1;
|
|
|
|
const int show_log=0x2;
|
|
|
|
const int show_all=show_command|show_log;
|
2017-09-24 03:14:08 -05:00
|
|
|
|
2017-08-20 16:28:23 +08:00
|
|
|
int run_command(string command,char * &output,int flag=show_all);
|
|
|
|
//int run_command_no_log(string command,char * &output);
|
2017-08-26 05:38:33 -05:00
|
|
|
int read_file(const char * file,string &output);
|
2017-08-17 23:40:17 +08:00
|
|
|
|
|
|
|
vector<string> string_to_vec(const char * s,const char * sp);
|
|
|
|
vector< vector <string> > string_to_vec2(const char * s);
|
|
|
|
|
2017-08-23 10:38:55 +08:00
|
|
|
string trim(const string& str, char c);
|
|
|
|
|
2017-08-23 07:01:21 -05:00
|
|
|
string trim_conf_line(const string& str);
|
|
|
|
|
|
|
|
vector<string> parse_conf_line(const string& s);
|
|
|
|
|
2017-08-26 05:38:33 -05:00
|
|
|
int hex_to_u32_with_endian(const string & a,u32_t &output);
|
|
|
|
int hex_to_u32(const string & a,u32_t &output);
|
2017-08-20 16:28:23 +08:00
|
|
|
//extern string iptables_pattern;
|
|
|
|
|
2017-10-24 09:04:47 -05:00
|
|
|
int create_fifo(char * file);
|
|
|
|
|
2018-06-22 22:28:19 -05:00
|
|
|
void print_binary_chars(const char * a,int len);
|
|
|
|
|
|
|
|
|
2018-07-19 03:18:07 -05:00
|
|
|
template <class key_t>
|
|
|
|
struct lru_collector_t:not_copy_able_t
|
|
|
|
{
|
|
|
|
//typedef void* key_t;
|
|
|
|
//#define key_t void*
|
|
|
|
struct lru_pair_t
|
|
|
|
{
|
|
|
|
key_t key;
|
|
|
|
my_time_t ts;
|
|
|
|
};
|
|
|
|
|
|
|
|
unordered_map<key_t,typename list<lru_pair_t>::iterator> mp;
|
|
|
|
|
|
|
|
list<lru_pair_t> q;
|
|
|
|
int update(key_t key)
|
|
|
|
{
|
|
|
|
assert(mp.find(key)!=mp.end());
|
|
|
|
auto it=mp[key];
|
|
|
|
q.erase(it);
|
|
|
|
|
|
|
|
my_time_t value=get_current_time();
|
|
|
|
if(!q.empty())
|
|
|
|
{
|
|
|
|
assert(value >=q.front().ts);
|
|
|
|
}
|
|
|
|
lru_pair_t tmp; tmp.key=key; tmp.ts=value;
|
|
|
|
q.push_front( tmp);
|
|
|
|
mp[key]=q.begin();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int new_key(key_t key)
|
|
|
|
{
|
|
|
|
assert(mp.find(key)==mp.end());
|
|
|
|
|
|
|
|
my_time_t value=get_current_time();
|
|
|
|
if(!q.empty())
|
|
|
|
{
|
|
|
|
assert(value >=q.front().ts);
|
|
|
|
}
|
|
|
|
lru_pair_t tmp; tmp.key=key; tmp.ts=value;
|
|
|
|
q.push_front( tmp);
|
|
|
|
mp[key]=q.begin();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int size()
|
|
|
|
{
|
|
|
|
return q.size();
|
|
|
|
}
|
|
|
|
int empty()
|
|
|
|
{
|
|
|
|
return q.empty();
|
|
|
|
}
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
mp.clear(); q.clear();
|
|
|
|
}
|
|
|
|
my_time_t ts_of(key_t key)
|
|
|
|
{
|
|
|
|
assert(mp.find(key)!=mp.end());
|
|
|
|
return mp[key]->ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
my_time_t peek_back(key_t &key)
|
|
|
|
{
|
|
|
|
assert(!q.empty());
|
|
|
|
auto it=q.end(); it--;
|
|
|
|
key=it->key;
|
|
|
|
return it->ts;
|
|
|
|
}
|
|
|
|
void erase(key_t key)
|
|
|
|
{
|
|
|
|
assert(mp.find(key)!=mp.end());
|
|
|
|
q.erase(mp[key]);
|
|
|
|
mp.erase(key);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
void erase_back()
|
|
|
|
{
|
|
|
|
assert(!q.empty());
|
|
|
|
auto it=q.end(); it--;
|
|
|
|
key_t key=it->key;
|
|
|
|
erase(key);
|
|
|
|
}*/
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-29 20:32:26 +08:00
|
|
|
#endif /* COMMON_H_ */
|