reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
HepAgent.hxx
Go to the documentation of this file.
1#if !defined(RESIP_HEPAGENT_HXX)
2#define RESIP_HEPAGENT_HXX
3
4#include "rutil/Data.hxx"
6#include "rutil/Socket.hxx"
10#include "rutil/Subsystem.hxx"
11#include "rutil/Logger.hxx"
12
13#define RESIPROCATE_SUBSYSTEM resip::Subsystem::EEP
14
15namespace resip
16{
17
19{
20 public:
21 typedef enum
22 {
23 SIP = 1,
24 RTCP_JSON = 5
26
27 HepAgent(const Data &captureHost, int capturePort, int captureAgentID);
28 virtual ~HepAgent();
29 virtual Data convertRTCPtoJSON(const Data& rtcpRaw);
30 void sendRTCP(const TransportType type, const GenericIPAddress& source, const GenericIPAddress& destination, const Data& rtcpRaw, const Data& correlationId);
31 template <class T>
32 void sendToHOMER(const TransportType type, const GenericIPAddress& source, const GenericIPAddress& destination, const HEPEventType eventType, const T& msg, const Data& correlationId)
33 {
34 struct hep_generic *hg;
37
38#ifdef USE_IPV6
40#endif
41
42 char *data = new char[sizeof(struct hep_generic)];
43 Data buf(Data::Take, data, sizeof(struct hep_generic));
44 hg = (struct hep_generic *)buf.data();
45 DebugLog(<< "buf.size() == " << buf.size());
46 DataStream stream(buf);
47
48 memset(hg, 0, sizeof(struct hep_generic));
49
50 /* header set */
51 memcpy(hg->header.id, "\x48\x45\x50\x33", 4);
52
53 Data chunk;
54
55 /* IP proto */
56 hg->ip_family.chunk.vendor_id = htons(0x0000);
57 hg->ip_family.chunk.type_id = htons(0x0001);
58 hg->ip_family.chunk.length = htons(sizeof(hg->ip_family));
59 unsigned int sourcePort = 0;
60 unsigned int destinationPort = 0;
61 switch(source.address.sa_family)
62 {
63 case AF_INET:
64 {
65 hg->ip_family.data = AF_INET;
66 src_ip4.chunk.vendor_id = htons(0x0000);
67 src_ip4.chunk.type_id = htons(0x0003);
68 const struct sockaddr_in& src_sa = source.v4Address;
69 memcpy(&src_ip4.data, &src_sa.sin_addr.s_addr, sizeof(src_sa.sin_addr.s_addr));
70 src_ip4.chunk.length = htons(sizeof(src_ip4));
71 chunk = Data(Data::Borrow, (char *)&src_ip4, sizeof(hep_chunk_ip4_t));
72 stream << chunk;
73 sourcePort = ntohs(src_sa.sin_port);
74
75 dst_ip4.chunk.vendor_id = htons(0x0000);
76 dst_ip4.chunk.type_id = htons(0x0004);
77 const struct sockaddr_in& dst_sa = destination.v4Address;
78 memcpy(&dst_ip4.data, &dst_sa.sin_addr.s_addr, sizeof(dst_sa.sin_addr.s_addr));
79 dst_ip4.chunk.length = htons(sizeof(dst_ip4));
80 chunk = Data(Data::Borrow, (char *)&dst_ip4, sizeof(hep_chunk_ip4_t));
81 stream << chunk;
82 destinationPort = ntohs(dst_sa.sin_port);
83
84 break;
85 }
86#ifdef USE_IPV6
87 case AF_INET6:
88 {
89 hg->ip_family.data = AF_INET6;
90 src_ip6.chunk.vendor_id = htons(0x0000);
91 src_ip6.chunk.type_id = htons(0x0005);
92 const struct sockaddr_in6& src_sa6 = source.v6Address;
93 memcpy(&src_ip6.data, &src_sa6.sin6_addr, sizeof(src_sa6.sin6_addr));
94 src_ip6.chunk.length = htons(sizeof(src_ip6));
95 chunk = Data(Data::Borrow, (char *)&src_ip6, sizeof(hep_chunk_ip6_t));
96 stream << chunk;
97 sourcePort = ntohs(src_sa6.sin6_port);
98
99 dst_ip6.chunk.vendor_id = htons(0x0000);
100 dst_ip6.chunk.type_id = htons(0x0006);
101 const struct sockaddr_in6& dst_sa6 = destination.v6Address;
102 memcpy(&dst_ip6.data, &dst_sa6.sin6_addr, sizeof(dst_sa6.sin6_addr));
103 dst_ip6.chunk.length = htons(sizeof(dst_ip6));
104 chunk = Data(Data::Borrow, (char *)&dst_ip6, sizeof(hep_chunk_ip6_t));
105 stream << chunk;
106 destinationPort = ntohs(dst_sa6.sin6_port);
107 break;
108 }
109#endif
110 {
111 default:
112 ErrLog(<<"unhandled address family");
113 return;
114 }
115 }
116 stream.flush();
117 DebugLog(<< "buf.size() == " << buf.size());
118 hg = (struct hep_generic *)buf.data();
119
120 /* PROTOCOL */
121 switch(type)
122 {
123 case TLS:
124 hg->ip_proto.data = IPPROTO_IDP; // FIXME
125 break;
126 case TCP:
127 hg->ip_proto.data = IPPROTO_TCP;
128 break;
129 case UDP:
130 hg->ip_proto.data = IPPROTO_UDP;
131 break;
132#if !defined(WIN32) || (defined(WIN32) && (_WIN32_WINNT >= 0x0600))
133 case SCTP:
134 hg->ip_proto.data = IPPROTO_SCTP;
135 break;
136#endif
137 case WS:
138 case WSS:
139 hg->ip_proto.data = IPPROTO_TCP; // FIXME
140 break;
141 default:
142 ErrLog(<<"unhandled TransportType");
143 return;
144 }
145 /* Proto ID */
146 hg->ip_proto.chunk.vendor_id = htons(0x0000);
147 hg->ip_proto.chunk.type_id = htons(0x0002);
148 hg->ip_proto.chunk.length = htons(sizeof(hg->ip_proto));
149
150 /* SRC PORT */
151 hg->src_port.chunk.vendor_id = htons(0x0000);
152 hg->src_port.chunk.type_id = htons(0x0007);
153 hg->src_port.data = htons(sourcePort);
154 hg->src_port.chunk.length = htons(sizeof(hg->src_port));
155
156 /* DST PORT */
157 hg->dst_port.chunk.vendor_id = htons(0x0000);
158 hg->dst_port.chunk.type_id = htons(0x0008);
159 hg->dst_port.data = htons(destinationPort);
160 hg->dst_port.chunk.length = htons(sizeof(hg->dst_port));
161
163
164 /* TIMESTAMP SEC */
165 hg->time_sec.chunk.vendor_id = htons(0x0000);
166 hg->time_sec.chunk.type_id = htons(0x0009);
167 hg->time_sec.chunk.length = htons(sizeof(hg->time_sec));
168 hg->time_sec.data = htonl((u_long)(now / 1000000LL));
169
170 /* TIMESTAMP USEC */
171 hg->time_usec.chunk.vendor_id = htons(0x0000);
172 hg->time_usec.chunk.type_id = htons(0x000a);
173 hg->time_usec.data = htonl(now % 1000000LL);
174 hg->time_usec.chunk.length = htons(sizeof(hg->time_usec));
175
176 /* Protocol TYPE */
177 hg->proto_t.chunk.vendor_id = htons(0x0000);
178 hg->proto_t.chunk.type_id = htons(0x000b);
179 hg->proto_t.data = eventType;
180 hg->proto_t.chunk.length = htons(sizeof(hg->proto_t));
181
182 /* Capture ID */
183 hg->capt_id.chunk.vendor_id = htons(0x0000);
184 hg->capt_id.chunk.type_id = htons(0x000c);
185 hg->capt_id.data = htons(mCaptureAgentID);
186 hg->capt_id.chunk.length = htons(sizeof(hg->capt_id));
187
188 stream.flush();
189
190 /* Correlation ID */
191 if(!correlationId.empty())
192 {
193 StackLog(<<"adding correlation ID: " << correlationId);
195 correlation_chunk.vendor_id = htons(0x0000);
196 correlation_chunk.type_id = htons(0x0011);
199 stream << chunk;
200 stream << correlationId;
201 stream.flush();
202 }
203
205 payload_chunk.vendor_id = htons(0x0000);
206 payload_chunk.type_id = htons(0x000f);
207 chunk = Data(Data::Borrow, (char *)&payload_chunk, sizeof(payload_chunk));
208 stream << chunk;
209 stream.flush();
210 Data::size_type beforePayload = buf.size();
211 DebugLog(<< "buf.size() == " << buf.size());
212 stream << msg;
213 stream.flush();
214 Data::size_type afterPayload = buf.size();
215 DebugLog(<< "Final buf.size() == " << buf.size());
218 hg = (struct hep_generic *)buf.data();
219 hg->header.length = htons((uint16_t)afterPayload);
220
221 if(sendToWire(buf))
222 {
223 int e = getErrno();
224#if defined(WIN32)
225 ErrLog(<< "sending to HOMER " << mDestination << " failed (" << e << ")");
226#else
227 ErrLog(<< "sending to HOMER " << mDestination << " failed (" << e << "): " << strError(e));
228#endif
229 }
230 else
231 {
232 DebugLog(<< "packet sent to HOMER " << mDestination);
233 }
234 }
235
236 protected:
237 virtual bool sendToWire(const Data& buf) const;
238 private:
239 Data mCaptureHost;
240 int mCapturePort;
241 int mCaptureAgentID;
242 GenericIPAddress mDestination;
243 Socket mSocket;
244};
245
246
247}
248
249#undef RESIPROCATE_SUBSYSTEM
250
251#endif
252
253
254/* ====================================================================
255 *
256 * Copyright (c) 2022, Software Freedom Institute https://softwarefreedom.institute
257 * Copyright (c) 2021-2022, Daniel Pocock https://danielpocock.com
258 *
259 * Redistribution and use in source and binary forms, with or without
260 * modification, are permitted provided that the following conditions
261 * are met:
262 *
263 * 1. Redistributions of source code must retain the above copyright
264 * notice, this list of conditions and the following disclaimer.
265 *
266 * 2. Redistributions in binary form must reproduce the above copyright
267 * notice, this list of conditions and the following disclaimer in
268 * the documentation and/or other materials provided with the
269 * distribution.
270 *
271 * 3. Neither the name of the author(s) nor the names of any contributors
272 * may be used to endorse or promote products derived from this software
273 * without specific prior written permission.
274 *
275 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS "AS IS" AND
276 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
277 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
278 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
279 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
280 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
282 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
283 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
285 * SUCH DAMAGE.
286 *
287 * ====================================================================
288 *
289 *
290 */
#define DebugLog(args_)
Definition Logger.hxx:128
#define StackLog(args_)
Definition Logger.hxx:125
#define ErrLog(args_)
Definition Logger.hxx:137
uint64_t hepUnixTimestamp()
Definition ResipHep.cxx:18
struct hep_chunk hep_chunk_t
Definition ResipHep.hxx:36
struct hep_chunk_ip6 hep_chunk_ip6_t
Definition ResipHep.hxx:79
struct hep_chunk_ip4 hep_chunk_ip4_t
Definition ResipHep.hxx:71
Handles cross-platform sockets compatibility.
An iostream that operates on an existing Data.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
@ Borrow
The Data instance is borrowing the memory from the passed in buffer.
Definition Data.hxx:260
@ Take
The Data instance takes complete ownership of the buffer.
Definition Data.hxx:272
size_t size_type
Definition Data.hxx:94
virtual Data convertRTCPtoJSON(const Data &rtcpRaw)
Definition HepAgent.cxx:161
void sendToHOMER(const TransportType type, const GenericIPAddress &source, const GenericIPAddress &destination, const HEPEventType eventType, const T &msg, const Data &correlationId)
Definition HepAgent.hxx:32
virtual ~HepAgent()
Definition HepAgent.cxx:119
void sendRTCP(const TransportType type, const GenericIPAddress &source, const GenericIPAddress &destination, const Data &rtcpRaw, const Data &correlationId)
Definition HepAgent.cxx:244
virtual bool sendToWire(const Data &buf) const
Definition HepAgent.cxx:259
#define IPPROTO_SCTP
Definition compat.hxx:322
dcm! – add UnusedChecking(_enum) below;
unsigned short uint16_t
Definition Stun.hxx:30
int Socket
Definition Socket.hxx:94
unsigned long long uint64_t
Definition Stun.hxx:39
TransportType
An enumeration of transport protocols.
int getErrno()
Definition Socket.hxx:97
std::string strError(int err)
Definition compat.hxx:274
Represents an IP-address and port (V4 or V6).
void list()
Definition userAdmin.cxx:66