-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_api.h
More file actions
152 lines (106 loc) · 4.63 KB
/
common_api.h
File metadata and controls
152 lines (106 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef __COMMON__API__H__
#define __COMMON__API__H__
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <set>
#include "jsoncpp/json.h"
using namespace std;
typedef map<string, string> MapParam;
unsigned get_ip_by_if(const char* ifname);
uint64_t CalcTimeCost_MS(const struct timeval& start, const struct timeval& end);
uint64_t CalcTimeCost_US(const struct timeval& start, const struct timeval& end);
#if 0
bool HitMask(uint64_t mask, uint64_t value);
#endif
int NetDataType(char * data, unsigned data_len);
char char2hex(char first,char second);
void hex2char(unsigned char what,char *sDest);
void hex2str(const char *what,int len, char *sDest);
void str2hex(const char* what,int len, char *dest);
void LittleStr2hex(const char* what,int len, char *dest);
char LittleChar2hex(char first,char second);
void LittleHex2char(unsigned char what,char *sDest);
void LittleHex2str(const char *what,int len,char *sDest);
string bin2str(const char *sBinData,int iLen);
string GetTime2(const int iType);
string GetFormatTime(const timeval& tvl);
int MySplitTag(char* pContentLine, const char* pSplitTag, vector<string>& vecContent);
int Split2Set(char* pContentLine, char* pSplitTag, set<string>& setContent);
unsigned INET_aton(const char* ip_str);
string INET_ntoa( unsigned ip );
void string_replace_reverse(string& in);
void string_replace(string& in);
int send_udp_data(const char *ip, short port, const char *data, int data_len);
int get_eth_ip(const char *eth, char ip[16]);
signed str2int(const signed char* what,int len);
unsigned str2unsign(const unsigned char* what,int len);
int GetMd5random(char * value, unsigned value_len, unsigned mod_base);
/*
* make_flow64(): Get a 64 bit flow number from IP, PORT and Index.
* @ip: IP.
* @port: PORT.
* @index: Internal index.
* Returns: Always return a flow number.
*/
static inline unsigned long long
make_flow64(unsigned ip, unsigned short port, unsigned short index = 0)
{
unsigned long long flow = 0, flow_low = 0;
flow = ip;
flow = flow << 32;
flow_low = port;
flow_low = flow_low << 16;
flow = flow | flow_low;
flow_low = index;
flow = flow | flow_low;
return flow;
}
inline uint32_t CalcTimeCost_MS(timeval& start, timeval& end) {
return (end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec) / 1000;
}
inline uint32_t CalcTimeCost_US(timeval& start, timeval& end) {
return (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
}
string get_default(MapParam& mparams, const string& key, const string& def_val);
time_t str2time (const char* date, int defval, const char* format="%Y-%m-%d %H:%M:%S");
uint64_t str2timetick(const char* date, uint64_t defval, const char* format="%Y-%m-%d %H:%M:%S");
uint64_t curr_timetick();
string utf8_2_gbk(const string& u);
void add_docid(string& docids, const string& docid);
void add_doc_question(string& doc_qs, const string& doc_q);
string trim_left(const string &s,const string& filt=" ");
string trim_right(const string &s,const string& filt=" ");
string Trim(const string& s);
int GetKv(const char* pszFilePath, map<string, string>& mConf, const string& split="=");
void set_value(Json::Value& item, const string& keyi, Json::Value& abs, const string& keya, bool convert);
void set_value_uint(Json::Value& item, const string& keyi, Json::Value& abs, const string& keya, uint32_t defval);
void set_value_uint64(Json::Value& item, const string& keyi, Json::Value& abs, const string& keya, uint64_t defval);
int CvtGb2313ToGbk (const string &src_str, string &dst_str);
string urldecode(const string &str_source);
string gbk_2_utf8(const string& g);
string i2str (int n);
string ui2str (unsigned int n);
string l2str (long n);
string ul2str (unsigned long n);
bool IsFileExist(const char* filename);
string getappID(const string& key);
string delappID(string key);
string getDomainIp(const string domainName);
int cancelUserFromString(const string& userID, string& queueString);
string get_value_str(Json::Value &jv, const string &key, const string def_val = "");
int get_value_int(Json::Value &jv, const string &key, const int def_val = 0);
unsigned int get_value_uint(Json::Value &jv, const string &key, const unsigned int def_val = 0);
long long get_value_int64(Json::Value &jv, const string &key, const long long def_val = 0);
unsigned long long get_value_uint64(Json::Value &jv, const string &key, const unsigned long long def_val = 0);
long long GetCurTimeStamp();
#endif