29 #include <netinet/in.h>
31 using namespace dtn::data;
38 : _version(version), _flags(BEACON_NO_FLAGS), _canonical_eid(eid), _sn(0)
51 return (_flags & DiscoveryAnnouncement::BEACON_SHORT);
54 return !(_flags & DiscoveryAnnouncement::BEACON_CONTAINS_EID);
67 return _canonical_eid;
82 for (std::list<DiscoveryService>::const_iterator iter = _services.begin(); iter != _services.end(); ++iter)
84 if ((*iter).getName() == name)
95 _services.push_back(service);
105 const list<DiscoveryService> &services = announcement._services;
107 switch (announcement._version)
111 if (services.empty())
113 unsigned char flags = DiscoveryAnnouncement::BEACON_SHORT | announcement._flags;
125 for (list<DiscoveryService>::const_iterator iter = services.begin(); iter != services.end(); ++iter)
127 beacon_len += (*iter).getLength();
132 for (list<DiscoveryService>::const_iterator iter = services.begin(); iter != services.end(); ++iter)
142 unsigned char flags = 0;
146 if (announcement._canonical_eid !=
EID())
148 flags |= DiscoveryAnnouncement::BEACON_CONTAINS_EID;
151 if (!services.empty())
153 flags |= DiscoveryAnnouncement::BEACON_SERVICE_BLOCK;
159 uint16_t sn = htons(announcement._sn);
160 stream.write( (
char*)&sn, 2 );
162 if ( flags && DiscoveryAnnouncement::BEACON_CONTAINS_EID )
168 if ( flags && DiscoveryAnnouncement::BEACON_SERVICE_BLOCK )
172 for (list<DiscoveryService>::const_iterator iter = services.begin(); iter != services.end(); ++iter)
185 uint8_t interval = 10;
187 uint16_t inet_port = htons(4556);
188 std::string eid = announcement._canonical_eid.
getString();
189 uint16_t eid_len = htons((uint16_t)eid.length());
190 unsigned int add_zeros = (4 - (eid.length() % 4)) % 4;
191 uint16_t length = htons(static_cast<uint16_t>(12 + eid.length() + add_zeros));
194 stream << (
unsigned char)cl_type;
195 stream.write((
char*)&interval, 1);
196 stream.write((
char*)&length, 2);
207 stream.write(&zero, 1);
208 stream.write(&zero, 1);
209 stream.write(&zero, 1);
210 stream.write(&zero, 1);
212 stream.write((
char*)&inet_port, 2);
213 stream.write((
char*)&eid_len, 2);
216 for (
unsigned int i = 0; i < add_zeros; ++i)
218 stream.write((
char*)&zero, 1);
230 unsigned char version = 0;
241 version = (
unsigned char)stream.get();
253 stream.
get((
char&)announcement._flags);
256 if (DiscoveryAnnouncement::BEACON_SHORT == announcement._flags)
262 stream >> beacon_len;
263 int remain = beacon_len.
get<
int>();
267 remain -=
static_cast<int>(eid.getLength());
272 list<DiscoveryService> &services = announcement._services;
282 services.push_back(service);
283 remain -=
static_cast<int>(service.getLength());
292 stream.get((
char&)announcement._flags);
297 stream.read((
char*)&sn, 2);
300 uint16_t sequencenumber = ntohs(sn);
304 if (announcement._flags & DiscoveryAnnouncement::BEACON_CONTAINS_EID)
314 if (announcement._flags & DiscoveryAnnouncement::BEACON_SERVICE_BLOCK)
317 list<DiscoveryService> &services = announcement._services;
321 stream >> num_services;
328 for (
unsigned int i = 0; num_services > i; ++i)
333 services.push_back(service);
339 if (announcement._flags & DiscoveryAnnouncement::BEACON_BLOOMFILTER)
358 stream.read((
char*)&cl_type, 1);
359 stream.read((
char*)&interval, 1);
360 stream.read((
char*)&length, 2);
361 stream.read((
char*)&inet_addr, 4);
362 stream.read((
char*)&inet_port, 2);
363 stream.read((
char*)&eid_len, 2);
365 std::vector<char> eid(eid_len);
366 stream.read(&eid[0], eid.size());
369 announcement._canonical_eid =
EID(std::string(eid.begin(), eid.end()));
389 ss <<
"ANNOUNCE: " << _canonical_eid.
getString();