wangyu-udp2raw/common.h

210 lines
4.5 KiB
C
Raw Normal View History

2017-07-29 20:32:26 +08:00
/*
* common.h
*
* Created on: Jul 29, 2017
* Author: wangyu
*/
#ifndef UDP2RAW_COMMON_H_
#define UDP2RAW_COMMON_H_
#define __STDC_FORMAT_MACROS 1
2017-08-04 03:29:53 +08:00
#include <inttypes.h>
2017-07-29 20:32:26 +08:00
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<getopt.h>
2017-07-30 16:57:24 +08:00
#include<unistd.h>
2017-07-29 20:32:26 +08:00
#include<errno.h>
2018-06-14 06:46:43 -07:00
//#include <sys/epoll.h>
2017-07-29 20:32:26 +08:00
#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
#include <netdb.h> // for gethostbyname()
2018-06-14 09:54:33 -05:00
//#include <netinet/tcp.h> //Provides declarations for tcp header
//#include <netinet/udp.h>
//#include <netinet/ip.h> //Provides declarations for ip header
2017-07-29 20:32:26 +08:00
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <fcntl.h>
2018-06-14 06:46:43 -07:00
//#include <byteswap.h>
2017-07-29 20:32:26 +08:00
#include <arpa/inet.h>
2018-06-14 06:46:43 -07:00
//#include <linux/if_ether.h>
//#include <linux/filter.h>
2017-07-29 20:32:26 +08:00
#include <sys/time.h>
#include <time.h>
2018-06-14 06:46:43 -07:00
//#include <sys/timerfd.h>
2017-07-29 20:32:26 +08:00
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <assert.h>
2018-06-14 06:46:43 -07:00
//#include <linux/if_packet.h>
//#include <byteswap.h>
2017-08-20 16:28:23 +08:00
#include <pthread.h>
2018-06-09 09:04:00 -05:00
#include <pcap.h>
#include <libnet.h>
2017-07-30 16:57:24 +08:00
2018-06-06 20:53:02 -05:00
#include <my_ev.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>
2017-07-29 20:32:26 +08:00
using namespace std;
2018-06-14 08:46:37 -07:00
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
defined(__BIG_ENDIAN__) || \
defined(__ARMEB__) || \
defined(__THUMBEB__) || \
defined(__AARCH64EB__) || \
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
#define UDP2RAW_BIG_ENDIAN 1
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
defined(__LITTLE_ENDIAN__) || \
defined(__ARMEL__) || \
defined(__THUMBEL__) || \
defined(__AARCH64EL__) || \
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
#define UDP2RAW_LITTLE_ENDIAN 1
// It's a little-endian target architecture
#else
#error "I don't know what architecture this is!"
#endif
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
typedef u64_t iv_t;
2017-07-29 20:32:26 +08:00
typedef u64_t padding_t;
2017-08-01 20:03:29 +08:00
typedef u64_t anti_replay_seq_t;
2017-07-29 20:32:26 +08:00
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();
};
2017-10-30 01:57:27 -05:00
typedef u64_t fd64_t;
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;
const int max_address_len=512;
2018-06-14 08:01:03 -05:00
const int queue_len=2000;
struct queue_t
{
char data[queue_len][buf_len];
int data_len[queue_len];
int head=0;
int tail=0;
void clear()
{
head=tail=0;
}
int empty()
{
if(head==tail) return 1;
else return 0;
}
int full()
{
if( (tail+1)%queue_len==head ) return 1;
else return 0;
}
void peek_front(char * & p,int &len)
{
assert(!empty());
p=data[head];
len=data_len[head];
}
void pop_front()
{
assert(!empty());
head++;head%=queue_len;
}
void push_back(char * p,int len)
{
assert(!full());
memcpy(data[tail],p,len);
data_len[tail]=len;
tail++;tail%=queue_len;
}
};
2018-06-14 09:54:33 -05:00
u64_t get_current_time();
u64_t pack_u64(u32_t a,u32_t b);
2017-07-29 20:32:26 +08:00
u32_t get_u64_h(u64_t a);
2017-07-29 20:32:26 +08:00
u32_t get_u64_l(u64_t a);
2017-07-29 20:32:26 +08:00
char * my_ntoa(u32_t ip);
2017-07-29 20:32:26 +08:00
void init_random_number_fd();
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);
bool larger_than_u32(u32_t a,u32_t b);
2017-07-29 20:32:26 +08:00
void setnonblocking(int sock);
2017-09-24 03:14:08 -05:00
int set_buf_size(int fd,int socket_buf_size,int force_socket_buf);
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);
int read_file(const char * file,string &output);
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);
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);
2017-07-29 20:32:26 +08:00
#endif /* COMMON_H_ */