-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.h
142 lines (121 loc) · 3.35 KB
/
Common.h
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
#ifndef COMMON_H_
#define COMMON_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <signal.h>
#include <execinfo.h>
#include <algorithm>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <net/if.h>
#include <netdb.h>
#include <fcntl.h>
#include <strings.h>
#include <sstream>
#include <ifaddrs.h>
using std::string;
using std::cout;
using std::endl;
using std::vector;
using std::set;
using std::map;
#define MCAST_BUFF_LEN (1024) // message length
// print out error message to stderr
#define LOG_ERROR(msg) \
do {if (!Common::isDebugMode()) std::cerr << "[ERROR] " << msg << endl;\
else std::cerr << __FILE__ << ":" << __LINE__ << "-(" \
<<__func__ << ") [ERROR] " << msg << endl;\
} while(0)
#define LOG_DEBUG(msg) \
do {if (Common::isDebugMode()) std::cerr << __FILE__ << ":" \
<< __LINE__ << "-(" <<__func__ << ") [DEBUG] " << msg << endl;\
} while(0)
// Set of all mcast addresses
typedef set<string> McastAddressSet;
/**
* Struct that contains interface name and its associated socket
*/
struct IfaceData
{
int sockFd;
string ifaceName;
vector<string> ifaceAddresses;
IfaceData(): sockFd(-1), ifaceName(""), ifaceAddresses(vector<string>()) {}
IfaceData(const string& name, const vector<string>& addresses, int fd = -1):
sockFd(fd), ifaceName(name), ifaceAddresses(addresses) {}
const string& getReadableName() const;
const string& getReadableAddress() const;
const string& toString() const;
private:
friend std::ostream & operator<<(std::ostream &os, const IfaceData& iface);
};
namespace Common
{
/**
* Get ip addresses for ifaceName
*
* @param ifaceName - [IN] has to be nonzero length
* @param ifaceIpAdresses - [OUT] with primary address as the first entry
* @param isIpV6 - true if create ipv6 socket
* @return 0 on success, -1 if error
*/
int getIfaceIPFromIfaceName(const string& ifaceName, vector<string>& ifaceIpAdresses, bool isIpV6 = false);
/**
* Get a set of all interface names that are up (have ip address)
* @param useIpV6
* @return set of up interfaces
*/
const vector<string>& getAllIfaceNames(bool useIpV6 = false);
/**
* Create udp socket
*
* @param isIpV6
* @return socket fd, -1 if error, check errno for more info
*/
int createSocket(bool isIpV6 = false);
/**
* Enable reuse address and port on socket
* @param sock - input socket
* @return 0 on success, <0 on error
*/
int setReuseSocket(int sock);
/**
* Clear up internal data from Common.cpp
*/
void cleanupCommon();
/**
* Getter/setter for debug mode
*/
void setDebugMode(bool enable = true);
bool isDebugMode();
/**
* Encode/decode ack message
* @param message [IN] message to be processed
* @param resultMsg [OUT] result message, = message if failed to de/encode
* @return true on success
*/
bool encodeAckMessage(const string& message, string& resultMsg);
bool decodeAckMessage(const string& message, string& resultMsg);
/**
* Send unicast message to target
* @param sock
* @param target
* @param msg
* @return true if success
*/
bool unicastMessage(int sock, struct sockaddr_storage& target, const string& msg);
}
#endif /*COMMON_H_*/