ITS
Geonet.cpp
Go to the documentation of this file.
1 
2 #include <Geonet.h>
3 
4 quint16 GeoNet::sequenceNumber = 0;
5 
7 {
9  m_manager = 0;
10  m_paramGPS = 0;
11  m_paramCAN = 0;
12  m_paramAppli = 0;
13  m_paramConf = 0;
14  m_paramHybrid = 0;
15  m_RP = 0;
16  m_locationTable = 0;
17  m_HPB = 0;
18  m_BCFPB = 0;
19  m_UCFPB = 0;
20  m_CBFPB = 0;
21  m_RPB = 0;
22  m_LSPB = 0;
24  m_mutexLocT = new QMutex;
25  m_hybridFlag = false;
26 }
27 
28 GeoNet::GeoNet(Manager* m, bool geonetFlag, bool hybridFlag, QMutex* mutex) :
29  m_geonetFlag(geonetFlag),
30  m_securityManager(m)
31 {
33 
34  m_mutex = mutex;
35  QMutexLocker locker(m_mutex);
36  m_manager = m;
37  m_paramGPS = (FakeGPSProvider*) m->hashMap["GPS"];
38  m_paramCAN = (FakeCANProvider*) m->hashMap["CAN"];
39  m_paramAppli = (FakeApplicationProvider*) m->hashMap["Application"];
40  m_paramConf = (ParameterConfiguration*) m->hashMap["Configuration"];
41  m_paramUpperTester = (ParameterUpperTester*) m->hashMap["UpperTester"];
42  m_paramHybrid = (ParameterHybrid*) m->hashMap["Hybrid"];
43  m_gnAddr = (GnAddr*) m->hashMap["GnAddr"];
44  m_paramNetwork = (ParameterNetwork*) m->hashMap["Network"];
46  {
47  m_paramSecu = (ParameterSecurity*) m->hashMap["Security"];
48  m_signatureSocket = new QUdpSocket;
49  m_verifySocket = new QUdpSocket;
50  m_signatureSocket->open(QIODevice::ReadWrite);
51  m_verifySocket->open(QIODevice::ReadWrite);
52  }
53  else
54  m_paramSecu = 0;
56  m_mutexLocT = new QMutex;
57  m_hybridFlag = hybridFlag;
58  printf("Geonet constructor UNLOCKED\n");
59  if(m_hybridFlag)
61 
63  printf("RawSocketSend can't be initialized\n");
64 
65  gen2fac = new QUdpSocket(this);
66 
67  if(m_geonetFlag)
68  {
70  QObject::connect(m_RP, SIGNAL(packetReceived(QByteArray)), this, SLOT(treatPackets(QByteArray)));
71  QThread* threadReceive = new QThread;
72  m_RP->moveToThread(threadReceive);
73  QObject::connect(threadReceive, SIGNAL(started()), m_RP, SLOT(receive()));
74  threadReceive->start();
75  }
76 
77  m_locationTable = new QHash<QByteArray, LocationTableEntry>();
78 
79 
81 
83 
85 
87  QObject::connect(m_CBFPB, SIGNAL(lpvSent()), this, SLOT(lpvUpdate()));
88 
90  QObject::connect(m_RPB, SIGNAL(sendPacketToGn(gnDataRequest_t)), this, SLOT(sendPacketFromRPB(gnDataRequest_t)));
91 
93 
94  if(m_paramUpperTester != 0)
95  {
96  gen2ut = new QUdpSocket(this);
98  }
99  else
100  gen2ut = 0;
101 }
102 
104 {
106 
107  QObject::connect(this, SIGNAL(packetReceived(QByteArray)), this, SLOT(treatPackets(QByteArray)));
108  if(m_paramHybrid->sslUsed())
110  else
112  m_tcpSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
113  QObject::connect(this, SIGNAL(packetReceived(QByteArray)), this, SLOT(treatPackets(QByteArray)));
114 }
115 
117 {
118  m_sslSocket = new QSslSocket;
119 
120  QObject::connect(m_sslSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
121  QObject::connect(m_sslSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
122  QObject::connect(m_sslSocket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
123 
124  m_sslSocket->connectToHostEncrypted(m_paramHybrid->serverAddress(), m_paramHybrid->serverPort());
125  m_sslSocket->waitForConnected();
127 }
128 
129 void GeoNet::sslErrors(const QList<QSslError> &errors)
130 {
131  int i = 1;
132  for(const auto &error : errors)
133  qDebug() << "SSL Error" << i++ << error.errorString();
134 
135  if(m_tcpSocket->state() != QAbstractSocket::ConnectedState)
136  {
137  qDebug() << "Deactivation of hybridization due to error(s)";
138  m_hybridFlag = false;
139  }
140 }
141 
142 void GeoNet::socketError(QAbstractSocket::SocketError)
143 {
144  qDebug() << "Socket error" << m_tcpSocket->errorString();
145 }
146 
147 void GeoNet::socketReadyRead(){emit packetReceived(m_tcpSocket->readAll().prepend(14, 0));}
148 /*
149 {
150  QByteArray payload = m_tcpSocket->readAll();
151  char *btp = payload.data();
152  QByteArray payloadWithoutBTP = QByteArray(btp + 4, payload.length() - 4);
153  quint16 port = extractUInt(btp, 2);
154 
155  gen2fac->writeDatagram(payloadWithoutBTP, QHostAddress::LocalHost, port);
156 }
157 */
158 
160 {
161  m_tcpSocket = new QTcpSocket;
162 
163  QObject::connect(m_tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
164  QObject::connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
165 
166  qDebug() << "Connecting to" << QHostAddress(m_paramHybrid->serverAddress()).toString() << m_paramHybrid->serverAddress() << ":" << m_paramHybrid->serverPort();
167  m_tcpSocket->connectToHost(m_paramHybrid->serverAddress(), m_paramHybrid->serverPort(), QIODevice::ReadWrite);
168  m_tcpSocket->waitForConnected();
169  if(m_tcpSocket->state() == QAbstractSocket::ConnectedState)
170  qDebug() << "Connection success" ;
171  else
172  qDebug() << "Connection failed" << m_tcpSocket->errorString();
173 }
174 
176 {
177  close(m_paramNetwork->rawSocket);
178 }
179 
181 {
182  QMutexLocker locker(m_mutex);
183  QMutexLocker locker2(m_mutexLocT);
184  m_beacon = new QTimer;
185  QObject::connect(m_beacon, SIGNAL(timeout()), this, SLOT(sendBeacon()));
186  m_beacon->setSingleShot(true);
188  m_beacon->start();
189 }
190 
192 {
193  double aireCommunication = M_PI * pow(itsGnDefaultMaxCommunicationRange, 2);
194  int nbNeighbour = 0;
195  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();i != m_locationTable->end(); ++i)
196  {
197  if(i.value().isNeighbour() != 0 && *(i.value().isNeighbour()))
198  nbNeighbour++;
199  }
200 
201  double ratio = nbNeighbour/aireCommunication;
202 
203  if(ratio < 0.025)
204  m_angleThreshold = 30;
205  else if(ratio < 0.05)
206  m_angleThreshold = 45;
207  else
208  m_angleThreshold = 60;
209 
210 }
211 
213 {
214  QMutexLocker locker(m_mutexLocT);
215  int j = 0;
216  if(m_locationTable)
217  {
218  printf("m_locationTable size %d\n", m_locationTable->size());
219  if(!m_locationTable->isEmpty())
220  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();j < m_locationTable->size(); j++)
221  {
222  if(LocationTableEntry(i.value()).isPending())
223  {
224  i++;
225  continue;
226  }
227  quint64 last = LocationTableEntry(i.value()).lastTimestamp();
228  quint64 end = last + itsGnLifetimeLocTE * 1000;
229  quint64 now = m_paramGPS->timestamp() % 0x100000000;
230  if(end <= now)
231  {
232  i = m_locationTable->erase(i);
234  }
235  else
236  i++;
237  }
238  }
239 }
240 
242 {
243  if(m_geonetFlag)
244  m_RP->m_changeFilter = true;
245  QMutexLocker locker(m_mutexLocT);
246  m_locationTable->clear();
247  locker.unlock();
248 
249  m_HPB->clear();
250  m_RPB->clear();
251  m_BCFPB->clear();
252  m_CBFPB->clear();
253  m_CBFPBCounter.clear();
254  m_LSPB->clear();
255  m_UCFPB->clear();
256  sequenceNumber = 0;
257  m_beacon->start();
258 }
259 
261 {
262  m_beacon->start();
263 }
264 
266 {
267  geonetErrors[geonetError_success] = QString("Geonet Success\n");
268 
269  // Send Errors
270  // Common Header Errors
271  geonetErrors[geonetError_send_ch_headerType] = QString("CommonHeader Sending Error : Unknown HeaderType.\n");
272  // Anycast
273  geonetErrors[geonetError_send_ch_headerSubType_geoAnycast_unknown] = QString("CommonHeader Sending Error : HeaderSubType GeoAnycast unknown.\n");
274  // Broadcast
275  geonetErrors[geonetError_send_ch_headerSubType_geoBroadcast_unknown] = QString("CommonHeader Sending Error : HeaderSubType GeoBroadcast unknown.\n");
276  // Topological
277  geonetErrors[geonetError_send_ch_headerSubType_TSB_unknown] = QString("CommonHeader Sending Error : HeaderSubType TSB unknown.\n");
278 
279  // LSRequest Send Errors
280  geonetErrors[tmp_geonetError_send_LSRequest_securedPacket] = QString("TMP LSRequest Processing Error : Can't send secured packet.\n");
281  geonetErrors[geonetError_send_LSRequest_successfullyRespond] = QString("LSRequest Processing Error : Successfuly received a response.\n");
282  geonetErrors[geonetError_send_LSRequest_noResponse] = QString("LSRequest Processing Error : Maximum repetition time reached without response.\n");
283  geonetErrors[geonetError_send_LSRequest_buffered] = QString("LSRequest Sending : Packet buffered in LSPB.\n");
284 
285  // LSReply Send Errors
286  geonetErrors[geonetError_send_LSReply_destinationUnknown] = QString("LSReply Sending : Destination not found.\n");
287  geonetErrors[tmp_geonetError_send_LSReply_securedPacket] = QString("TMP LSRequest Processing Error : Can't send secured packet.\n");
288  geonetErrors[geonetError_send_LSReply_bufferedPacket] = QString("LSReply Sending : Packet buffered.\n");
289  geonetErrors[geonetError_send_LSReply_discardedPacket] = QString("LSReply Sending : Packet discarded.\n");
290  geonetErrors[geonetError_send_LSReply_destinationAddress] = QString("LSReply Sending Error : DestinationAddress isn't MAC.\n");
291  // LSReply Forward Errors
292  geonetErrors[geonetError_forward_LSReply_bufferedPacket] = QString("LsReply Forwarding : Packet buffered.\n");
293  geonetErrors[geonetError_forward_LSReply_discardedPacket] = QString("LsReply Forwarding : Packet discarded.\n");
294  geonetErrors[geonetError_forward_LSReply_destinationAddress] = QString("LsReply Forwarding Error : DestinationAddress isn't MAC.\n");
295 
296  // GAC Send Errors
297  geonetErrors[tmp_geonetError_send_gac_securedPacket] = QString("TMP GAC Processing Error : Can't send secured packet.\n");
298  geonetErrors[geonetError_send_gac_noDestinationArea] = QString("GAC Sending Error : No destination area.\n");
299  geonetErrors[geonetError_send_gac_areaSize] = QString("GAC Sending Error : Area size is too large.\n");
300  geonetErrors[geonetError_send_gac_unknownCommunicationProfile] = QString("GAC Sending Error : Communication Profile isn't G5A.\n");
301  geonetErrors[geonetError_send_gac_unicast_bufferedPacket] = QString("GAC Sending : Packet buffered in UCPB.\n");
302  geonetErrors[geonetError_send_gac_unicast_discardedPacket] = QString("GAC Sending : Packet discarded after unicast.\n");
303  geonetErrors[geonetError_send_gac_broadcast_bufferedPacket] = QString("GAC Sending : Packet buffered in BCPB.\n");
304  geonetErrors[geonetError_send_gac_broadcast_discardedPacket] = QString("GAC Sending : Packet discarded after broadcast.\n");
305  geonetErrors[geonetError_send_gac_destinationAddress] = QString("GAC Sending Error : DestinationAddress isn't MAC.\n");
306  geonetErrors[geonetError_send_gac_noAreaLineForwarding] = QString("GAC Sending Error : Area line forwarding is not enable, can't send.\n");
307  // GAC Forward Errors
308  geonetErrors[geonetError_forward_gac_areaSize] = QString("GAC Forwarding Error : Area size is too large.\n");
309  geonetErrors[geonetError_forward_gac_bufferedPacket] = QString("GAC Forwarding : Packet buffered.\n");
310  geonetErrors[geonetError_forward_gac_discardedPacket] = QString("GAC Forwarding : Packet discarded.\n");
311  geonetErrors[geonetError_forward_gac_destinationAddress] = QString("GAC Forwarding Error : DestinationAddress isn't MAC.\n");
312  geonetErrors[geonetError_forward_gac_noAreaLineForwarding] = QString("GAC Forwarding Error : Area line forwarding is not enable, can't forward.\n");
313 
314  // GBC Send Errors
315  geonetErrors[tmp_geonetError_send_gbc_securedPacket] = QString("TMP GBC Processing Error : Can't send secured packet.\n");
316  geonetErrors[geonetError_send_gbc_noDestinationArea] = QString("GBC Sending Error : No destination area.\n");
317  geonetErrors[geonetError_send_gbc_areaSize] = QString("GBC Sending Error : Area size is too large.\n");
318  geonetErrors[geonetError_send_gbc_unknownCommunicationProfile] = QString("GBC Sending Error : Communication Profile isn't G5A.\n");
319  geonetErrors[geonetError_send_gbc_bufferedPacket] = QString("GBC Sending : Packet buffered.\n");
320  geonetErrors[geonetError_send_gbc_discardedPacket] = QString("GBC Sending : Packet discarded.\n");
321  geonetErrors[geonetError_send_gbc_destinationAddress] = QString("GBC Sending Error : DestinationAddress isn't MAC.\n");
322  // GBC Forward Errors
323  geonetErrors[geonetError_forward_gbc_areaSize] = QString("GBC Forwarding Error : Area size is too large.\n");
324  geonetErrors[geonetError_forward_gbc_bufferedPacket] = QString("GBC Forwarding : Packet buffered.\n");
325  geonetErrors[geonetError_forward_gbc_discardedPacket] = QString("GBC Forwarding : Packet discarded.\n");
326  geonetErrors[geonetError_forward_gbc_destinationAddress] = QString("GBC Forwarding Error : DestinationAddress isn't MAC.\n");
327 
328  // GUC Send Errors
329  geonetErrors[geonetError_send_guc_noMID] = QString("GUC Sending Error : No destination address.\n");
330  geonetErrors[tmp_geonetError_send_guc_MIDNotFound] = QString("TMP GUC Sending Error : Destination not found in LocT.\n");
331  geonetErrors[tmp_geonetError_send_guc_securedPacket] = QString("TMP GUC Processing Error : Can't send secured packet.\n");
332  geonetErrors[geonetError_send_guc_unknownCommunicationProfile] = QString("GUC Sending Error : Communication Profile isn't G5A.\n");
333  geonetErrors[geonetError_send_guc_bufferedPacket] = QString("GUC Sending : Packet buffered.\n");
334  geonetErrors[geonetError_send_guc_discardedPacket] = QString("GUC Sending : Packet discarded.\n");
335  geonetErrors[geonetError_send_guc_destinationAddress] = QString("GUC Sending Error : DestinationAddress isn't MAC.\n");
336  // GUC Forward Errors
337  geonetErrors[geonetError_forward_guc_bufferedPacket] = QString("GUC Forwarding : Packet buffered.\n");
338  geonetErrors[geonetError_forward_guc_discardedPacket] = QString("GUC Forwarding : Packet discarded.\n");
339  geonetErrors[geonetError_forward_guc_destinationAddress] = QString("GUC Forwarding Error : DestinationAddress isn't MAC.\n");
340 
341  // SHB Send Errors
342  geonetErrors[tmp_geonetError_send_shb_securedPacket] = QString("TMP SHB Processing Error : Can't send secured packet.\n");
343  geonetErrors[geonetError_send_shb_unknownCommunicationProfile] = QString("SHB Sending Error : Communication Profile isn't G5A.\n");
344  geonetErrors[geonetError_send_shb_bufferedPacket] = QString("SHB Sending : Packet buffered.\n");
345 
346  // TSB Send Errors
347  geonetErrors[tmp_geonetError_send_tsb_securedPacket] = QString("TMP TSB Processing Error : Can't send secured packet.\n");
348  geonetErrors[geonetError_send_tsb_unknownCommunicationProfile] = QString("TSB Sending Error : Communication Profile isn't G5A.\n");
349  geonetErrors[geonetError_send_tsb_bufferedPacket] = QString("TSB Sending : Packet buffered.\n");
350  // TSB Forward Errors
351  geonetErrors[geonetError_forward_tsb_unknownCommunicationProfile] = QString("TSB Forward Error : Communication Profile isn't G5A.\n");
352  geonetErrors[geonetError_forward_tsb_bufferedPacket] = QString("TSB Forward : Packet buffered.\n");
353 
354  // Receive Errors
355  // Basic Header Errors
356  geonetErrors[geonetError_processing_bh_gnProtocolVersion] = QString("BasicHeader Processing Error : Protocol version is different from the current version (version ").append(QString::number(itsGnProtocolVersion)).append(").\n");
357  geonetErrors[geonetError_processing_bh_nextHeader] = QString("BasicHeader Processing Error : Next header invalide.\n");
358  geonetErrors[geonetError_processing_bh_securedHeader] = QString("BasicHeader Processing Error : Secured header.\n");
359 
360  // Common Header Errors
361  geonetErrors[geonetError_processing_ch_hopLimit] = QString("CommonHeader Processing Error : RemainingHopLimit is greater than MaximumHopLimit.\n");
362  geonetErrors[geonetError_processing_ch_headerType] = QString("CommonHeader Processing Error : HeaderType is equal to any or isn't known.\n");
363  geonetErrors[geonetError_processing_ch_headerSubType_TSB_unknown] = QString("CommonHeader Processing Error : HeaderSubType TSB is unknown\n");
364  geonetErrors[geonetError_processing_ch_headerSubType_LS_unknown] = QString("CommonHeader Processing Error : HeaderSubType Location Service is unknown.\n");
365 
366  // Duplicate Packet Address Errors
367  geonetErrors[geonetError_processing_beacon_DAD] = QString("Beacon Processing Error : Duplicate address detected.\n");
368  geonetErrors[geonetError_processing_LSRequest_DAD] = QString("LSRequest Processing Error : Duplicate address detected.\n");
369  geonetErrors[geonetError_processing_LSReply_DAD] = QString("LSReply Processing Error : Duplicate address detected.\n");
370  geonetErrors[geonetError_processing_gac_DAD] = QString("GAC Processing Error : Duplicate address detected.\n");
371  geonetErrors[geonetError_processing_gbc_DAD] = QString("GBC Processing Error : Duplicate address detected.\n");
372  geonetErrors[geonetError_processing_guc_DAD] = QString("GUC Processing Error : Duplicate address detected.\n");
373  geonetErrors[geonetError_processing_shb_DAD] = QString("SHB Processing Error : Duplicate address detected.\n");
374  geonetErrors[geonetError_processing_tsb_DAD] = QString("TSB Processing Error : Duplicate address detected.\n");
375 
376  // Duplicate Packet Timestamp &&/|| SN Errors
377  geonetErrors[geonetError_processing_beacon_DPD] = QString("Beacon Processing Error : Duplicate packet detected.\n");
378  geonetErrors[geonetError_processing_LSRequest_DPD] = QString("LSRequest Processing Error : Duplicate packet detected.\n");
379  geonetErrors[geonetError_processing_LSReply_DPD] = QString("LSReply Processing Error : Duplicate packet detected.\n");
380  geonetErrors[geonetError_processing_gac_DPD] = QString("GAC Processing Error : Duplicate packet detected.\n");
381  geonetErrors[geonetError_processing_gbc_DPD] = QString("GBC Processing Error : Duplicate packet detected.\n");
382  geonetErrors[geonetError_processing_guc_DPD] = QString("GUC Processing Error : Duplicate packet detected.\n");
383  geonetErrors[geonetError_processing_shb_DPD] = QString("SHB Processing Error : Duplicate packet detected.\n");
384  geonetErrors[geonetError_processing_tsb_DPD] = QString("TSB Processing Error : Duplicate packet detected.\n");
385 }
386 
388 {
389  int rawSocket;
390  struct ifreq if_idx;
391  struct sockaddr_ll socket_address;
392  char sendbuf[BUF_SIZ];
393  struct ether_header *eh = (struct ether_header *) sendbuf;
394  char ifName[IFNAMSIZ] = {'\0'};
395 
396  eh = (struct ether_header *) sendbuf;
397 
398  memset(&socket_address, 0, sizeof socket_address);
399 
400  /* Get interface name */
401  strcpy(ifName, m_paramConf->interface().toStdString().c_str());
402 
403  /* Open RAW socket to send on */
404  if((rawSocket = socket(AF_PACKET, SOCK_RAW, htons(0x8947))) == -1)
405  {
406  qDebug() << "socket" << strerror(errno);
408  }
409 
410  /* Get the index of the interface to send on */
411  memset(&if_idx, 0, sizeof(struct ifreq));
412  strncpy(if_idx.ifr_name, ifName, IFNAMSIZ - 1);
413  if(ioctl(rawSocket, SIOCGIFINDEX, &if_idx) < 0)
414  {
415  qDebug() << "SIOCGIFINDEX geonet" << strerror(errno) << ifName;
417  }
418 
419  /* Index of the network device */
420  socket_address.sll_ifindex = if_idx.ifr_ifindex;
421 
422  /* Address length */
423  socket_address.sll_halen = ETH_ALEN;
424 
425  socket_address.sll_family = AF_INET;
426 
427  /* Ethertype field */
428  eh->ether_type = htons(0x8947);
429 
430  m_paramNetwork->rawSocket = rawSocket;
431  m_paramNetwork->if_idx = if_idx;
432  m_paramNetwork->socket_address = socket_address;
433  memcpy(m_paramNetwork->sendbuf, sendbuf, BUF_SIZ);
434  m_paramNetwork->eh = *eh;
435  memcpy(m_paramNetwork->ifName, ifName, IFNAMSIZ);
436 
437  m_manager->hashMap["Network"] = m_paramNetwork;
438 
439  return networkError_success;
440 }
441 
442 void GeoNet::sendEventIndicationToUT(uchar* packetFull, int packetLen)
443 {
444  if(m_paramUpperTester != 0)
445  {
447 
448  QByteArray resUt;
449  QDataStream ds(&resUt, QIODevice::WriteOnly);
450  ds << mt;
451  ds << (quint16)packetLen;
452  ds.writeRawData((char*)packetFull, packetLen);
453 
454  if(gen2ut != 0)
455  gen2ut->writeDatagram(resUt, QHostAddress::LocalHost, m_paramUpperTester->upperTesterEventIndicationListenPort());
456  }
457 }
458 
459 int GeoNet::lsRetransmit(GnAddr destination, QByteArray lsRequestData, BasicHeader bh)
460 {
461  QMutexLocker mutexLocT(m_mutexLocT);
462  if(m_lsRetransmission[destination.toHex()] > 0 && !((*m_locationTable)[destination.toHex()].isPending()))
463  {
464  m_lsRetransmission.remove(destination.toHex());
466  }
467 
468  /* Construct the Ethernet header : destination addr */
469  for(int i = 0 ; i < 6 ; i++)
470  m_paramNetwork->eh.ether_dhost[i] = 0xFF;
471 
472  int ret = send(lsRequestData, true, bh);
473 
474  m_lsRetransmission[destination.toHex()]+=1;
475 
477  {
478  m_LSPB->erase(destination);
479  m_lsRetransmission.remove(destination.toHex());
480  m_locationTable->remove(destination.toHex());
482  }
483  QTimer::singleShot(itsGnLocationServiceRetransmitTimer, [this, destination, lsRequestData, bh]()
484  {lsRetransmit(destination, lsRequestData, bh);});
485 
486  return ret;
487 }
488 
489 /* -------------------------PACKET-------------------------SENDING------------------------- */
490 
492 {
493  send(rawPacket);
494 }
495 
496 /* CommonHeader verification */
498 {
499  QByteArray payload;
500  QDataStream dsPayload(&payload, QIODevice::WriteOnly);
501 
502  verifLocT();
503  if(gnDR.destinationPort != 0)
504  {
505  unsigned char btp[4] = {0x00};
506  format(btp, gnDR.destinationPort, 2);
507  format(&btp[2], gnDR.destinationPortInfo, 2);
508  dsPayload.writeRawData((const char*)btp, 4);
509  dsPayload.writeRawData(gnDR.payload.data(), gnDR.payload.length());
510  gnDR.payload = payload;
511  }
512 
513  switch(gnDR.packetTransportType)
514  {
516  switch(gnDR.packetTransportSubType)
517  {
521  return sendGAC(gnDR);
522  break;
523  default:
525  break;
526  }
527  break;
529  switch(gnDR.packetTransportSubType)
530  {
534  return sendGBC(gnDR);
535  break;
536  default:
538  break;
539  }
540  break;
542  return sendGUC(gnDR);
543  break;
545  switch(gnDR.packetTransportSubType)
546  {
547  /* SHB */
549  return sendSHB(gnDR);
550  break;
551  /* TSB */
553  return sendTSB(gnDR);
554  break;
555  default:
557  break;
558  }
559  break;
561  default:
563  break;
564  }
566 }
567 
568 /* Beacon Packet creation */
570 {
571  /* Creating a Beacon to send */
572  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
573  quint8 pai = m_paramGPS->data().referencePosition().positionConfidenceEllipse.semiMajorConfidence < itsGnPaiInterval / 2 ? true << 15 : false << 15;
575  BeaconPacket beacon(lpv);
576 
578 
580  BasicHeader bh;
581 
584  else
586 
587  QByteArray beaconData;
588  QDataStream ds(&beaconData, QIODevice::WriteOnly);
589 
590  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
591  ds.writeRawData(beacon.toHex().data(), beacon.toHex().length());
592 
593  for(int i = 0 ; i < 6 ; i++)
594  m_paramNetwork->eh.ether_dhost[i] = 0xFF;
595 
596  int ret = send(beaconData, true, bh);
597  if(ret == networkError_success)
598  {
599  m_HPB->addPacket(beacon);
600  printf("\n--> Beacon successfuly send!\n");
601  }
602  else
603  printf("\n--> Beacon sending failure. Error code : %s", geonetErrors[ret].toStdString().c_str());
604 
606  m_beacon->start();
607 }
608 
609 /* LsRequest creation */
611 {
612  if(m_locationTable->find(destination.toHex()) != m_locationTable->end() && (*m_locationTable)[destination.toHex()].isPending())
613  {
614  m_LSPB->addPacket(destination, gnDR, (m_paramGPS->timestamp() % 0x100000000));
616  }
617  else
618  {
619  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
622  LSRequestPacket lsR(sequenceNumber, lpv, destination);
623 
625 
626  BasicHeader bh;
629  else
631 
632  m_LSPB->addPacket(destination, gnDR, tmpstmp);
633 
634  m_lsRetransmission[destination.toHex()]=0;
635 
636  /* Construct the packet */
637  QByteArray lsRequestData;
638  QDataStream ds(&lsRequestData, QIODevice::WriteOnly);
639 
640  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
641  ds.writeRawData(lsR.toHex().data(), lsR.toHex().length());
642 
643 
644  int ret = lsRetransmit(destination.toHex(), lsRequestData, bh);
645  if(ret == networkError_success)
646  {
647  m_HPB->addPacket(lsR);
648  sequenceNumber++;
649  (*m_locationTable)[destination.toHex()] = LocationTableEntry();
650  (*m_locationTable)[destination.toHex()].setLsPending(true);
651  (*m_locationTable)[destination.toHex()].setNeverReceived(true);
652  //(*m_locationTable)[destination.toHex()].setLastTimestamp(m_paramGPS->timestamp() % 0x100000000);
653  (*m_locationTable)[destination.toHex()].setLastTimestamp(0);
654  return ret;
655  }
656  else
657  {
658  m_locationTable->remove(destination.toHex());
659  return ret;
660  }
661  }
662 }
663 
664 /* GACPacket creation */
666 {
669 
670  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
672 
675 
676  CommonHeader ch;
678 
680  int rhl = (gnDR.maximumHopLimit != 0)?*gnDR.maximumHopLimit:itsGnDefaultHopLimit;
681 
682  BasicHeader bh;
683 
686  else
688 
689  if(gnDR.repetitionInterval != NULL && *gnDR.repetitionInterval > 0 && gnDR.maximumRepetitionTime != NULL && *gnDR.maximumRepetitionTime > 0 && gnDR.maximumHopLimit != NULL && *gnDR.maximumHopLimit > 1)
690  {
691  m_RPB->addPacket(gnDR);
692  }
693 
694  e_shape shape;
695  switch(ch.headerSubType())
696  {
698  shape = shape_circle;
699  if((gac.destinationArea().distanceA() * gac.destinationArea().distanceA() * M_PI) > ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
701  break;
703  shape = shape_rectangle;
704  if((gac.destinationArea().distanceA() * gac.destinationArea().distanceB()) > ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
706  break;
708  shape = shape_ellipse;
709  if((gac.destinationArea().distanceA() * gac.destinationArea().distanceB() * M_PI) > ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
711  break;
712  default :
713  shape = shape_none;
714  break;
715  }
716 
717  double x, y;
718  x = diameter(m_paramGPS->data().referencePosition().longitude, gac.destinationArea().geoAreaPosLongitude(), circAtLat((m_paramGPS->data().referencePosition().latitude + gac.destinationArea().geoAreaPosLatitude())/2));
719  y = LAT2METER*((m_paramGPS->data().referencePosition().latitude - gac.destinationArea().geoAreaPosLatitude())/10000000.);
720  y *= y<0?-1:1;
721 
722  double inArea;
723  if(shape == shape_circle)
724  inArea = f(x, y, gac.destinationArea().distanceA(), gac.destinationArea().distanceB(), shape);
725  else
726  inArea = f(
727  x*cos(gac.destinationArea().angle())-y*sin(gac.destinationArea().angle()),
728  x*sin(gac.destinationArea().angle())+y*cos(gac.destinationArea().angle()),
729  gac.destinationArea().distanceA(), gac.destinationArea().distanceB(), shape);
730 
732 
733  if(inArea<0 && m_paramConf->itsGnGeoAreaLineForwarding())
734  {
736  {
737  default:
740  res = greedy(bh, ch, gac, gnDR.payload, QByteArray(), m_gnAddr->mid(), m_gnAddr->mid());
741  break;
743  uchar NH[MAC_ADDR_SIZE];
744  for(int i = 0; i < MAC_ADDR_SIZE; i++)
745  NH[i] = 0xFF;
746  res.setMacAddr(NH);
748  break;
749  }
750 
751  if(res.isInteger() && res.integer() == 0)
753  else if(res.isInteger() && res.integer() == -1)
755  }
756  else if(inArea >= 0)
757  {
759  {
760  default:
763  res = simpleForwardingAlgorithm(bh, ch, gac, gnDR.payload, shape, QByteArray(), true);
764  break;
766  uchar NH[MAC_ADDR_SIZE];
767  for(int i = 0; i < MAC_ADDR_SIZE; i++)
768  NH[i] = 0xFF;
769  res.setMacAddr(NH);
771  break;
773  res = advancedForwarding(bh, ch, gac, gnDR.payload, shape, QByteArray(), m_gnAddr->mid(), m_gnAddr->mid(), true);
774  break;
775  }
776 
777  if(res.isInteger() && res.integer() == 0)
779  else if(res.isInteger() && res.integer() == -1)
781  }
782  else
784 
785  /* If CommunicationProfile is not supported or unknown */
788 
789  /* Construct the packet */
790  QByteArray gacData;
791  QDataStream ds(&gacData, QIODevice::WriteOnly);
792 
793  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
794  ds.writeRawData(gac.toHex().data(), gac.toHex().length());
795  ds.writeRawData(gnDR.payload.data(), gnDR.payload.length());
796 
797  if(res.isMacAddr())
798  /* Construct the Ethernet header : destination addr */
799  for(int i = 0 ; i < 6 ; i++)
800  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
801  else
803 
804  int ret = send(gacData, true, bh);
805  if(ret == networkError_success)
806  {
807  m_HPB->addPacket(gac);
808  sequenceNumber++;
809  }
810 
811  return ret;
812 }
813 
814 /* GBCPacket creation */
816 {
817 
820 
821  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
823  true << 15 : false << 15;
824 
829 
830  CommonHeader ch;
832  m_paramAppli->data().itsGnIsMobile(), gnDR.payload.length(),
834 
836  int rhl = (gnDR.maximumHopLimit != 0)?*gnDR.maximumHopLimit:itsGnDefaultHopLimit;
837 
838  BasicHeader bh;
839 
842  else
844 
845  if(gnDR.repetitionInterval != NULL && *gnDR.repetitionInterval > 0 && gnDR.maximumRepetitionTime != NULL &&
846  *gnDR.maximumRepetitionTime > 0 && gnDR.maximumHopLimit != NULL && *gnDR.maximumHopLimit > 1)
847  {
848  m_RPB->addPacket(gnDR);
849  }
850 
851  e_shape shape;
852  switch(ch.headerSubType())
853  {
855  shape = shape_circle;
856  if((gbc.destinationArea().distanceA() * gbc.destinationArea().distanceA() * M_PI) >
857  ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
859  break;
861  shape = shape_rectangle;
862  if((gbc.destinationArea().distanceA() * gbc.destinationArea().distanceB()) >
863  ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
865  break;
867  shape = shape_ellipse;
868  if((gbc.destinationArea().distanceA() * gbc.destinationArea().distanceB() * M_PI) >
869  ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
871  break;
872  default :
873  shape = shape_none;
874  break;
875  }
876 
878 
880  {
881  default:
884  res = simpleForwardingAlgorithm(bh, ch, gbc, gnDR.payload, shape, QByteArray(), true);
885  break;
887  res = contentionBasedForwardingAlgorithm(bh, ch, gbc, gnDR.payload, shape, QByteArray(), true);
888  break;
890  res = advancedForwarding(bh, ch, gbc, gnDR.payload, shape, QByteArray(), m_gnAddr->mid(), m_gnAddr->mid(), true);
891  break;
892  }
893 
894  if(res.isInteger() && res.integer() == 0)
896  else if(res.isInteger() && res.integer() == -1)
898 
899  /* If CommunicationProfile is not supported or unknown */
902 
903  /* Construct the packet */
904  QByteArray gbcData;
905  QDataStream ds(&gbcData, QIODevice::WriteOnly);
906 
907  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
908  ds.writeRawData(gbc.toHex().data(), gbc.toHex().length());
909  ds.writeRawData(gnDR.payload.data(), gnDR.payload.length());
910 
911  if(res.isMacAddr())
912  /* Construct the Ethernet header : destination addr */
913  for(int i = 0 ; i < 6 ; i++)
914  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
915  else
917 
918  int ret = send(gbcData, true, bh);
919  if(ret == networkError_success)
920  {
921  m_HPB->addPacket(gbc);
922  sequenceNumber++;
923  }
924 
925  return ret;
926 }
927 
928 /* GUCPacket creation */
930 {
931  GUCPacket guc;
932 
935 
936  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(GnAddr(gnDR.destinationAddress.choice.gnAddr).toHex());
937 
938  if((pos == m_locationTable->end()) || LocationTableEntry(pos.value()).isPending() || (LocationTableEntry(pos.value()).longPositionVector().pai() == 0))
940 
941 
942  /*if(pos != m_locationTable->end() && !LocationTableEntry(pos.value()).isPending() && LocationTableEntry(pos.value()).longPositionVector().pai())
943  {*/
944  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
946 
948  guc = GUCPacket(sequenceNumber, lpv, ShortPositionVector(LocationTableEntry(pos.value()).longPositionVector().gnAddr(), LocationTableEntry(pos.value()).longPositionVector().timestamp(), LocationTableEntry(pos.value()).longPositionVector().latitude(), LocationTableEntry(pos.value()).longPositionVector().longitude()));
949  /*}
950  else
951  return sendLsRequest(GnAddr(gnDR.destinationAddress.choice.gnAddr), gnDR);*/
952 
953  CommonHeader ch;
955 
957  int rhl = (gnDR.maximumHopLimit != 0)?*gnDR.maximumHopLimit:itsGnDefaultHopLimit;
958 
959  BasicHeader bh;
960 
963  else
965 
966  if(gnDR.repetitionInterval != NULL && *gnDR.repetitionInterval > 0 && gnDR.maximumRepetitionTime != NULL && *gnDR.maximumRepetitionTime > 0 && gnDR.maximumHopLimit != NULL && *gnDR.maximumHopLimit > 1)
967  {
968  m_RPB->addPacket(gnDR);
969  }
970 
973  {
974  default:
977  res = greedy(bh, ch, guc, gnDR.payload, QByteArray());
978  break;
980  //res = CBFUnicast(bh, ch, guc, gnDR.payload);
981  uchar NH[MAC_ADDR_SIZE];
982  for(int i = 0; i < MAC_ADDR_SIZE; i++)
983  NH[i] = 0xFF;
984  res.setMacAddr(NH);
986  break;
987  }
988 
989  if(res.isInteger() && res.integer() == 0)
991  else if(res.isInteger() && res.integer() == -1)
993 
994  /* If CommunicationProfile is not supported or unknown */
997 
998  /* Construct the packet */
999  QByteArray gucData;
1000  QDataStream ds(&gucData, QIODevice::WriteOnly);
1001 
1002  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
1003  ds.writeRawData(guc.toHex().data(), guc.toHex().length());
1004  ds.writeRawData(gnDR.payload.data(), gnDR.payload.length());
1005 
1006 
1007  if(res.isMacAddr())
1008  /* Construct the Ethernet header : destination addr */
1009  for(int i = 0 ; i < 6 ; i++){
1010  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
1011  printf("PARAMNETWORK [%d] -> %d\n", i, m_paramNetwork->eh.ether_dhost[i]);}
1012  else
1014 
1015  int ret = send(gucData, true, bh);
1016  if(ret == networkError_success)
1017  {
1018  m_HPB->addPacket(guc);
1019  sequenceNumber++;
1020  }
1021 
1022  return ret;
1023 }
1024 
1025 /* SHBPacket creation */
1027 {
1028  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
1031  SHBPacket shb(lpv);
1032 
1034 
1036  quint8 rhl = (gnDR.maximumHopLimit != 0)?*gnDR.maximumHopLimit:1;
1037 
1038  BasicHeader bh;
1039 
1042  else
1044 
1045  bool noNeighbour = true;
1046  QMutexLocker locker(m_mutexLocT);
1047  if(m_locationTable && !m_locationTable->isEmpty())
1048  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();i != m_locationTable->end(); ++i)
1049  if(LocationTableEntry(i.value()).isNeighbour() != 0 && *(LocationTableEntry(i.value()).isNeighbour()))
1050  noNeighbour = false;
1051  locker.unlock();
1052 
1053  if(noNeighbour && gnDR.trafficClass.SCF() == 1)
1054  {
1055  m_BCFPB->addPacket(bh, ch, shb, gnDR.payload, QByteArray());
1057  }
1058  else
1059  {
1060  /* If CommunicationProfile is not supported or unknown */
1063 
1064  if(gnDR.repetitionInterval != NULL && *gnDR.repetitionInterval > 0 && gnDR.maximumRepetitionTime != NULL && *gnDR.maximumRepetitionTime > 0 && gnDR.maximumHopLimit != NULL && *gnDR.maximumHopLimit > 1)
1065  {
1066  m_RPB->addPacket(gnDR);
1067  }
1068 
1069  /* Construct the packet */
1070  QByteArray shbData;
1071  QDataStream ds(&shbData, QIODevice::WriteOnly);
1072 
1073  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
1074  ds.writeRawData(shb.toHex().data(), shb.toHex().length());
1075  ds.writeRawData(gnDR.payload.data(), gnDR.payload.length());
1076 
1077  /* Construct the Ethernet header : destination addr */
1078  for(int i = 0 ; i < 6 ; i++)
1079  m_paramNetwork->eh.ether_dhost[i] = 0xFF;
1080 
1081  int ret = send(shbData, true, bh);
1082  if(ret == networkError_success)
1083  m_HPB->addPacket(shb);
1084 
1085  return ret;
1086  }
1087 }
1088 
1089 /* TSBPacket creation */
1091 {
1092  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
1095  TSBPacket tsb(sequenceNumber, lpv);
1096 
1098 
1100  quint8 rhl = (gnDR.maximumHopLimit != 0)?*gnDR.maximumHopLimit:itsGnDefaultHopLimit;
1101 
1102  BasicHeader bh;
1103 
1106  else
1108 
1109  bool noNeighbour = true;
1110  QMutexLocker locker(m_mutexLocT);
1111  if(!m_locationTable->isEmpty())
1112  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();i != m_locationTable->end(); ++i)
1113  if(LocationTableEntry(i.value()).isNeighbour() != 0 && *(LocationTableEntry(i.value()).isNeighbour()))
1114  noNeighbour = false;
1115  locker.unlock();
1116 
1117  if(noNeighbour && gnDR.trafficClass.SCF() == 1)
1118  {
1119  m_BCFPB->addPacket(bh, ch, tsb, gnDR.payload, QByteArray());
1121  }
1122  else
1123  {
1124  /* If CommunicationProfile is not supported or unknown */
1127 
1128  if(gnDR.repetitionInterval != NULL && *gnDR.repetitionInterval > 0 && gnDR.maximumRepetitionTime != NULL && *gnDR.maximumRepetitionTime > 0 && gnDR.maximumHopLimit != NULL && *gnDR.maximumHopLimit > 1)
1129  {
1130  m_RPB->addPacket(gnDR);
1131  }
1132 
1133  /* Construct the packet */
1134  QByteArray tsbData;
1135  QDataStream ds(&tsbData, QIODevice::WriteOnly);
1136 
1137  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
1138  ds.writeRawData(tsb.toHex().data(), tsb.toHex().length());
1139  ds.writeRawData(gnDR.payload.data(), gnDR.payload.length());
1140 
1141  /* Construct the Ethernet header : destination addr */
1142  for(int i = 0 ; i < 6 ; i++)
1143  m_paramNetwork->eh.ether_dhost[i] = 0xFF;
1144 
1145  int ret = send(tsbData, true, bh);
1146  if(ret == networkError_success)
1147  {
1148  m_HPB->addPacket(tsb);
1149  sequenceNumber++;
1150  }
1151 
1152  return ret;
1153  }
1154 }
1155 
1156 /* Sending the packet */
1157 int GeoNet::send(QByteArray payloadToSend, bool containLPV, BasicHeader bh)
1158 {
1159  QByteArray payload;
1160  size_t tx_len = sizeof(struct ether_header);
1161  int error;
1162 
1163  /* Security Header */
1165  payload = getSecuredPayload(payloadToSend, bh);
1166  else
1167  payload = bh.toHex() + payloadToSend;
1168 
1169  /* Construct the Ethernet header : my mac addr */
1170  for(int i = 0 ; i < 6 ; i++)
1171  m_paramNetwork->eh.ether_shost[i] = m_paramConf->mid(i);
1172 
1173  memcpy(m_paramNetwork->sendbuf, &m_paramNetwork->eh, sizeof(struct ether_header));
1174  for(int i = 0 ; i < payload.length(); i++)
1175  m_paramNetwork->sendbuf[tx_len++] = payload.data()[i];
1176 
1177  /* Send packet */
1178  if(m_geonetFlag)
1179  {
1180  if(sendto(m_paramNetwork->rawSocket, m_paramNetwork->sendbuf, tx_len, 0, (struct sockaddr*)(&(m_paramNetwork->socket_address)), sizeof(struct sockaddr_ll)) < 0)
1181  {
1182  qDebug() << "sendto" << strerror(errno);
1184  }
1185  }
1186  if(m_hybridFlag)
1187  {
1188  error = hybridSend(payload);
1189  if(error != hybridError_success)
1190  {
1191  qDebug() << "hybridSend" << m_tcpSocket->errorString();
1192  return error;
1193  }
1194  else
1195  qInfo() << "HybridSend success";
1196  }
1197 
1198  if(containLPV)
1199  QTimer::singleShot(0, this, SLOT(lpvUpdate()));
1200 
1201  return networkError_success;
1202 }
1203 
1204 int GeoNet::hybridSend(QByteArray payload)
1205 {
1206  int p = -1;
1207  qDebug() << /*"Sending" << payload.toHex() << "of" << payload.size() << "bytes to"
1208  << m_tcpSocket->peerName() << "(" << m_tcpSocket->peerAddress().toString() << ") :" << m_tcpSocket->peerPort()
1209  << */"connection state" << m_tcpSocket->state();
1210  if(m_tcpSocket->state() == QAbstractSocket::ConnectedState)
1211  {
1212  p = m_tcpSocket->write(payload.data(), payload.length());
1213  m_tcpSocket->flush();
1214  m_tcpSocket->waitForBytesWritten();
1215  }
1216  else
1217  {
1218  if(!m_paramHybrid->sslUsed())
1219  {
1220  m_tcpSocket->connectToHost(m_paramHybrid->serverAddress(), m_paramHybrid->serverPort(), QIODevice::ReadWrite);
1221  m_tcpSocket->waitForConnected();
1222  }
1223  else
1224  {
1225  m_sslSocket->connectToHostEncrypted(m_paramHybrid->serverAddress(), m_paramHybrid->serverPort());
1226  m_sslSocket->waitForConnected();
1228  }
1229  if(m_tcpSocket->state() == QAbstractSocket::ConnectedState)
1230  {
1231  p = m_tcpSocket->write(payload.data(), payload.length());
1232  m_tcpSocket->flush();
1233  m_tcpSocket->waitForBytesWritten();
1234  }
1235  }
1236 
1237  if(p < 0)
1238  return hybridError_send;
1239  qDebug() << p << "bytes sent";
1240 
1241 
1242  return hybridError_success;
1243 }
1244 
1245 /* -------------------------PACKET-------------------------READING------------------------- */
1246 
1247 /* EthernetHeader Processing */
1248 void GeoNet::treatPackets(QByteArray rawPacket)
1249 {
1250  uchar* packet = (uchar*) rawPacket.data();
1251  sniff_ethernet_t* eth = (sniff_ethernet_t*) packet;
1252  int len = rawPacket.length() - SIZE_ETHERNET;
1253  int res = 0;
1254  res = basicHeaderProcessing(packet + SIZE_ETHERNET, len, eth);
1255 
1256  if(res == networkError_success)
1257  printf("\n--> Successfuly received and treated a packet.\n");
1258  else
1259  printf("\n--> Failure while treating a packet. Error code : %s", geonetErrors[res].toStdString().c_str());
1260 
1261 }
1262 
1263 int GeoNet::basicHeaderProcessing(uchar* packet, int len, sniff_ethernet_t* eth)
1264 {
1265 
1266  qDebug() << "BH processing";
1267  sniff_basicHeader_t* tmpbh = (sniff_basicHeader_t*) packet;
1268  BasicHeader bh(*tmpbh);
1269  bool *verifySecuredPacket = 0;
1270  QByteArray securedPacket;
1271 
1272  if(bh.version() != itsGnProtocolVersion)
1273  {
1275  }
1276  else
1277  {
1278  switch(bh.nextHeader())
1279  {
1282  /* CommonHeader processing */
1283  return commonHeaderProcessing(packet + BasicHeader::helper_size, eth, bh);
1284  break;
1286  /* SecuredPacket processing */
1287  if(m_paramConf->itsGnSecurity())
1288  {
1289  verifySecuredPacket = (bool*)calloc(1, sizeof *verifySecuredPacket);
1290  securedPacket = verifySecuredPayload(QByteArray((const char*)packet, len), verifySecuredPacket);
1291  if(verifySecuredPacket)
1292  {
1293  free(verifySecuredPacket);
1294  return commonHeaderProcessing((unsigned char*)securedPacket.data(), eth, bh);
1295  }
1296  free(verifySecuredPacket);
1297  }
1299  break;
1300  default:
1302  break;
1303  }
1304  }
1305 }
1306 
1308 {
1309  sniff_commonHeader_t* tmpch = (sniff_commonHeader_t*) packet;
1310  CommonHeader ch(*tmpch);
1311 
1312  if(ch.maximumHopLimit() < bh.rhl())
1313  {
1315  }
1316  else
1317  {
1318  bh.setRHL(bh.rhl() - 1);
1319  switch(ch.headerType())
1320  {
1321  default:
1324  break;
1326  return beaconPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1327  break;
1329  return gacPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1330  break;
1332  return gbcPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1333  break;
1335  return gucPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1336  break;
1338  switch(ch.headerSubType())
1339  {
1341  return shbPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1342  break;
1344  return tsbPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1345  break;
1346  default :
1348  break;
1349  }
1350  break;
1352  switch(ch.headerSubType())
1353  {
1355  return LSRequestPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1356  break;
1358  return LSReplyPacketProcessing(packet + CommonHeader::helper_size, eth, bh, ch);
1359  break;
1360  default :
1362  break;
1363  }
1364  break;
1365  }
1366  }
1367 }
1368 
1370 {
1371  Q_UNUSED(ch);
1372  BeaconPacket beac;
1373  beac.extract(packet);
1374 
1375  verifLocT();
1376 
1377  QByteArray packetIndex = QByteArray((const char*)beac.soPv().gnAddr().toHex(), GnAddr::helper_size);
1378 
1379  /* DuplicatePacketDetection */
1380  if(m_locationTable->find(packetIndex) != m_locationTable->end())
1381  if(m_HPB->isDuplicateTimestamp(beac) || isDuplicatePacketDetectionTimestamp((*m_locationTable)[packetIndex].lastTimestamp(), beac.soPv().timestamp()))
1383 
1384  /* DuplicateAddressDetection */
1386  {
1387  bool a = true;
1388  bool b = true;
1389 
1390  for(int i=0; i<6; i++)
1391  if(beac.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
1392  a = false;
1394 
1395  if(a && b)
1396  {
1397  emit m_manager->changeMID();
1398  m_RP->m_changeFilter = true;
1400  }
1401  }
1402 
1403  QMutexLocker locker(m_mutexLocT);
1404  (*m_locationTable)[packetIndex].setLongPositionVector(beac.soPv());
1405  (*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1406  (*m_locationTable)[packetIndex].setLastTimestamp(beac.soPv().timestamp());
1407  (*m_locationTable)[packetIndex].setVersion(bh.version());
1408  (*m_locationTable)[packetIndex].setStationType(beac.soPv().gnAddr().stationType());
1409  (*m_locationTable)[packetIndex].setIsNeighbour(true);
1410 
1412 
1413  /* TODO update the PDR
1414  itsGnMaxPacketDataRateEmaBeta
1415  itsGnMaxPacketDataRate
1416  (*m_locationTable)[packetIndex].setPDR(...) */
1417  locker.unlock();
1418  //m_HPB->addPacket(bh, ch, beac);
1419 
1420  checkBCFPB();
1421  checkUCFPB();
1422 
1423  return geonetError_success;
1424 }
1425 
1427 {
1428  Q_UNUSED(eth);
1429  LSRequestPacket LSRequest;
1430  LSRequest.extract(packet);
1431 
1432  verifLocT();
1433 
1434  QByteArray packetIndex = QByteArray((const char*)LSRequest.soPv().gnAddr().toHex(), GnAddr::helper_size);
1435 
1436  /* DuplicatePacketDetection */
1437  if(m_locationTable->find(packetIndex) != m_locationTable->end())
1438  if(m_HPB->isDuplicateTimestamp(LSRequest) || isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].lastTimestamp(), LSRequest.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), LSRequest.sequenceNumber()))
1440 
1441  /* DuplicateAddressDetection */
1443  {
1444  bool a = true;
1445  bool b = true;
1446 
1447  for(int i=0; i<6; i++)
1448  if(LSRequest.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
1449  a = false;
1451 
1452  if(a && b)
1453  {
1454  emit m_manager->changeMID();
1455  m_RP->m_changeFilter = true;
1457  }
1458  }
1459 
1460  QMutexLocker locker(m_mutexLocT);
1461  /* The entry does not exist and will be created */
1462  if((*m_locationTable).find(packetIndex) == (*m_locationTable).end())
1463  {
1464  (*m_locationTable)[packetIndex].setLongPositionVector(LSRequest.soPv());
1465  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1466  (*m_locationTable)[packetIndex].setVersion(bh.version());
1467  (*m_locationTable)[packetIndex].setStationType(LSRequest.soPv().gnAddr().stationType());
1468  (*m_locationTable)[packetIndex].setIsNeighbour(false);
1469  (*m_locationTable)[packetIndex].setLastTimestamp(LSRequest.soPv().timestamp());
1470  (*m_locationTable)[packetIndex].setSequenceNumber(LSRequest.sequenceNumber());
1471 
1472  /* TODO update the PDR
1473  itsGnMaxPacketDataRateEmaBeta
1474  itsGnMaxPacketDataRate
1475  (*m_locationTable)[packetIndex].setPDR(...) */
1476 
1477  }
1478  else /* The entry exist and will be updated */
1479  {
1480  /* If the packet is more recent the entry will be update */
1481  if(!m_HPB->isDuplicateTimestamp(LSRequest) && !isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].longPositionVector().timestamp(), LSRequest.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), LSRequest.sequenceNumber()))
1482  {
1483  (*m_locationTable)[packetIndex].setLongPositionVector(LSRequest.soPv());
1484  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1485  (*m_locationTable)[packetIndex].setVersion(bh.version());
1486  (*m_locationTable)[packetIndex].setStationType(LSRequest.soPv().gnAddr().stationType());
1487  (*m_locationTable)[packetIndex].setLastTimestamp(LSRequest.soPv().timestamp());
1488  (*m_locationTable)[packetIndex].setSequenceNumber(LSRequest.sequenceNumber());
1489 
1490  /* TODO update the PDR
1491  itsGnMaxPacketDataRateEmaBeta
1492  itsGnMaxPacketDataRate
1493  (*m_locationTable)[packetIndex].setPDR(...) */
1494 
1495  }
1496  }
1497  locker.unlock();
1498 
1499  flushPB(LSRequest.soPv().gnAddr());
1500 
1501  if(LSRequest.destination() == *m_gnAddr)
1502  {
1503  LSReplyPacket LSReply;
1504 
1505  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(LSRequest.soPv().gnAddr().toHex());
1506  if(pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai())
1507  {
1508  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
1510 
1512  LSReply = LSReplyPacket(sequenceNumber, lpv, ShortPositionVector(LocationTableEntry(pos.value()).longPositionVector().gnAddr(), LocationTableEntry(pos.value()).longPositionVector().timestamp(), LocationTableEntry(pos.value()).longPositionVector().latitude(), LocationTableEntry(pos.value()).longPositionVector().longitude()));
1513  }
1514  else
1516 
1517 
1518  CommonHeader ch;
1520 
1521  BasicHeader bh;
1522 
1525  else
1527 
1528 
1531  {
1532  default:
1535  res = greedy(bh, ch, LSReply, QByteArray(), QByteArray());
1536  break;
1538  //res = CBFUnicast(bh, ch, LSReply, QByteArray());
1539  uchar NH[MAC_ADDR_SIZE];
1540  for(int i = 0; i < MAC_ADDR_SIZE; i++)
1541  NH[i] = 0xFF;
1542  res.setMacAddr(NH);
1544  break;
1545  }
1546 
1547  if(res.isInteger() && res.integer() == 0)
1549  else if(res.isInteger() && res.integer() == -1)
1551 
1552  /* Construct the packet */
1553  QByteArray LSReplyData;
1554  QDataStream ds(&LSReplyData, QIODevice::WriteOnly);
1555 
1556  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
1557  ds.writeRawData(LSReply.toHex().data(), LSReply.toHex().length());
1558 
1559  if(res.isMacAddr())
1560  /* Construct the Ethernet header : destination addr */
1561  for(int i = 0 ; i < 6 ; i++)
1562  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
1563  else
1565 
1566  int ret = send(LSReplyData, true, bh);
1567  if(ret == networkError_success)
1568  {
1569  //m_HPB->addPacket(bh, ch, LSReply);
1570  sequenceNumber++;
1571  }
1572 
1573  return ret;
1574  }
1575  else
1576  {
1577  /* Forwarding LSRequest packet */
1578 
1579  if(bh.rhl() < 1)
1580  return geonetError_success;
1581 
1582  quint16 port, portInfo;
1583  get_UINT16(packet + LSRequestPacket::helper_size, &port);
1584  get_UINT16(packet + LSRequestPacket::helper_size + 2, &portInfo);
1585 
1586  gen2fac->writeDatagram(QByteArray((const char*)packet + LSRequestPacket::helper_size + 4, ch.payloadLength() - 4),
1587  QHostAddress::LocalHost, m_paramNetwork->internalBTPPort(port, portInfo));
1588  qDebug() << "Sending packet to" << port << portInfo << m_paramNetwork->internalBTPPort(port, portInfo);
1589 
1591 
1592  /* Construct the packet */
1593  QByteArray LSRequestData;
1594  QDataStream ds(&LSRequestData, QIODevice::WriteOnly);
1595 
1596  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
1597  ds.writeRawData(LSRequest.toHex().data(), LSRequest.toHex().length());
1598 
1599  /* Construct the Ethernet header : destination addr */
1600  for(int i = 0 ; i < 6 ; i++)
1601  m_paramNetwork->eh.ether_dhost[i] = 0xFF;
1602 
1603  return send(LSRequestData, false, bh);
1604  }
1605 }
1606 
1608 {
1609  Q_UNUSED(eth);
1610  LSReplyPacket lsReply;
1611  lsReply.extract(packet);
1612 
1613  verifLocT();
1614 
1615  QByteArray packetIndex = QByteArray((const char*)lsReply.soPv().gnAddr().toHex(), GnAddr::helper_size);
1616 
1617  /* DuplicatePacketDetection */
1618  if(m_locationTable->find(packetIndex) != m_locationTable->end() && !(*m_locationTable)[packetIndex].neverReceived())
1619  if(m_HPB->isDuplicateTimestamp(lsReply) || isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].lastTimestamp(), lsReply.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), lsReply.sequenceNumber()))
1621 
1622  /* DuplicateAddressDetection */
1624  {
1625  bool a = true;
1626  bool b = true;
1627 
1628  for(int i=0; i<6; i++)
1629  if(lsReply.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
1630  a = false;
1632 
1633  if(a && b)
1634  {
1635  emit m_manager->changeMID();
1636  m_RP->m_changeFilter = true;
1638  }
1639  }
1640 
1641  QMutexLocker locker(m_mutexLocT);
1642  /* The entry does not exist and will be created */
1643  if((*m_locationTable).find(packetIndex) == (*m_locationTable).end())
1644  {
1645  (*m_locationTable)[packetIndex].setLongPositionVector(lsReply.soPv());
1646  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1647  (*m_locationTable)[packetIndex].setVersion(bh.version());
1648  (*m_locationTable)[packetIndex].setLastTimestamp(lsReply.soPv().timestamp());
1649  (*m_locationTable)[packetIndex].setSequenceNumber(lsReply.sequenceNumber());
1650  (*m_locationTable)[packetIndex].setStationType(lsReply.soPv().gnAddr().stationType());
1651  (*m_locationTable)[packetIndex].setIsNeighbour(false);
1652 
1653  /* TODO update the PDR
1654  itsGnMaxPacketDataRateEmaBeta
1655  itsGnMaxPacketDataRate
1656  (*m_locationTable)[packetIndex].setPDR(...) */
1657 
1658  }
1659  else /* The entry exist and will be updated */
1660  {
1661  /* If the packet is more recent the entry will be update */
1662  if(!m_HPB->isDuplicateTimestamp(lsReply) && !isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].longPositionVector().timestamp(), lsReply.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), lsReply.sequenceNumber()))
1663  {
1664  (*m_locationTable)[packetIndex].setLongPositionVector(lsReply.soPv());
1665  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1666  (*m_locationTable)[packetIndex].setVersion(bh.version());
1667  (*m_locationTable)[packetIndex].setStationType(lsReply.soPv().gnAddr().stationType());
1668  (*m_locationTable)[packetIndex].setLastTimestamp(lsReply.soPv().timestamp());
1669  (*m_locationTable)[packetIndex].setSequenceNumber(lsReply.sequenceNumber());
1670 
1671  /* TODO update the PDR
1672  itsGnMaxPacketDataRateEmaBeta
1673  itsGnMaxPacketDataRate
1674  (*m_locationTable)[packetIndex].setPDR(...) */
1675  }
1676  }
1677 
1678 
1679  if(lsReply.dePv().gnAddr() == *m_gnAddr)
1680  {/*
1681  (*m_locationTable)[packetIndex].setLastTimestamp(lsReply.soPv().timestamp());
1682  (*m_locationTable)[packetIndex].setSequenceNumber(lsReply.sequenceNumber());
1683  (*m_locationTable)[packetIndex].setVersion(bh.version());
1684  (*m_locationTable)[packetIndex].setStationType(lsReply.soPv().gnAddr().stationType());
1685  (*m_locationTable)[packetIndex].setLongPositionVector(lsReply.soPv());
1686  (*m_locationTable)[packetIndex].setIsNeighbour(false);*/
1687 
1688  (*m_locationTable)[packetIndex].setLsPending(false);
1689  (*m_locationTable)[packetIndex].setNeverReceived(false);
1690 
1691  locker.unlock();
1692  flushPB(lsReply.soPv().gnAddr());
1693 
1694  /* TODO update the PDR
1695  itsGnMaxPacketDataRateEmaBeta
1696  itsGnMaxPacketDataRate
1697  (*m_locationTable)[packetIndex].setPDR(...) */
1698 
1699 
1700  return geonetError_success;
1701  }
1702  else
1703  {
1704  locker.unlock();
1705  flushPB(lsReply.soPv().gnAddr());
1706  if(bh.rhl() < 1)
1707  return geonetError_success;
1708 
1710 
1712  {
1713  default:
1716  res = greedy(bh, ch, lsReply, packetIndex);
1717  break;
1719  //res = CBFUnicast(bh, ch, lsReply, QByteArray());
1720  uchar NH[MAC_ADDR_SIZE];
1721  for(int i = 0; i < MAC_ADDR_SIZE; i++)
1722  NH[i] = 0xFF;
1723  res.setMacAddr(NH);
1725  break;
1726  }
1727 
1728  if(res.isInteger() && res.integer() == 0)
1730  else if(res.isInteger() && res.integer() == -1)
1732 
1733  /* Construct the Ethernet header : destination addr */
1734  if(res.isMacAddr())
1735  for(int i = 0 ; i < 6 ; i++)
1736  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
1737  else
1739 
1740  /* Construct the packet */
1741  QByteArray basicHeaderData;
1742  QDataStream ds(&basicHeaderData, QIODevice::WriteOnly);
1743 
1744  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
1745  ds.writeRawData(lsReply.toHex().data(), lsReply.toHex().length());
1746 
1747  int ret = send(basicHeaderData, false, bh);
1748  if(ret == geonetError_success)
1749  printf("\nSuccesfully forwarded LSReply");
1750 
1751  return ret;
1752  }
1753 }
1754 
1756 {
1757  QByteArray payload((char*)packet + GACPacket::helper_size, ch.payloadLength());
1758  GACPacket gac;
1759  gac.extract(packet);
1760 
1761  verifLocT();
1762 
1763  QByteArray packetIndex = QByteArray((const char*)gac.soPv().gnAddr().toHex(), GnAddr::helper_size);
1764 
1765  /* DuplicatePacketDetection */
1766 
1767  if(m_locationTable->find(packetIndex) != m_locationTable->end())
1768  if(m_HPB->isDuplicateTimestamp(gac) || isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].lastTimestamp(), gac.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), gac.sequenceNumber()))
1770 
1771  /* DuplicateAddressDetection */
1773  {
1774  bool a = true;
1775  bool b = true;
1776 
1777  for(int i=0; i<6; i++)
1778  if(gac.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
1779  a = false;
1780  b = (gac.soPv().gnAddr().MStationTypeCountryCode() == m_gnAddr->MStationTypeCountryCode());
1781 
1782  if(a && b)
1783  {
1784  emit m_manager->changeMID();
1785  m_RP->m_changeFilter = true;
1787  }
1788  }
1789 
1790  QMutexLocker locker(m_mutexLocT);
1791  /* The entry does not exist and will be created */
1792  if((*m_locationTable).find(packetIndex) == (*m_locationTable).end())
1793  {
1794  (*m_locationTable)[packetIndex].setLongPositionVector(gac.soPv());
1795  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1796  (*m_locationTable)[packetIndex].setVersion(bh.version());
1797  (*m_locationTable)[packetIndex].setStationType(gac.soPv().gnAddr().stationType());
1798  (*m_locationTable)[packetIndex].setIsNeighbour(false);
1799  (*m_locationTable)[packetIndex].setLastTimestamp(gac.soPv().timestamp());
1800  (*m_locationTable)[packetIndex].setSequenceNumber(gac.sequenceNumber());
1801 
1802  /* TODO update the PDR
1803  itsGnMaxPacketDataRateEmaBeta
1804  itsGnMaxPacketDataRate
1805  (*m_locationTable)[packetIndex].setPDR(...) */
1806 
1807  }
1808  else /* The entry exist and will be updated */
1809  {
1810  /* If the packet is more recent the entry will be update */
1811  if(isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].longPositionVector().timestamp(), gac.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), gac.sequenceNumber()))
1812  {
1813  (*m_locationTable)[packetIndex].setLongPositionVector(gac.soPv());
1814  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1815  (*m_locationTable)[packetIndex].setVersion(bh.version());
1816  (*m_locationTable)[packetIndex].setStationType(gac.soPv().gnAddr().stationType());
1817  (*m_locationTable)[packetIndex].setLastTimestamp(gac.soPv().timestamp());
1818  (*m_locationTable)[packetIndex].setSequenceNumber(gac.sequenceNumber());
1819 
1820  /* TODO update the PDR
1821  itsGnMaxPacketDataRateEmaBeta
1822  itsGnMaxPacketDataRate
1823  (*m_locationTable)[packetIndex].setPDR(...) */
1824  }
1825  }
1826  locker.unlock();
1827 
1828  flushPB(gac.soPv().gnAddr());
1829 
1830  if(bh.rhl() < 1)
1831  return geonetError_success;
1832 
1833 
1834  e_shape shape;
1835  switch(ch.headerSubType())
1836  {
1838  shape = shape_circle;
1839  if(((gac.destinationArea().distanceA() * gac.destinationArea().distanceA()) * M_PI) > (itsGnMaxGeoAreaSize * 1000 * 1000))
1841  break;
1843  shape = shape_rectangle;
1844  if((gac.destinationArea().distanceA() * gac.destinationArea().distanceB()) > (itsGnMaxGeoAreaSize * 1000 * 1000))
1846  break;
1848  shape = shape_ellipse;
1849  if((gac.destinationArea().distanceA() * gac.destinationArea().distanceB() * M_PI) > (itsGnMaxGeoAreaSize * 1000 * 1000))
1851  break;
1852  default :
1853  shape = shape_none;
1854  break;
1855  }
1856 
1857  double x, y;
1858  x = diameter(m_paramGPS->data().referencePosition().longitude, gac.destinationArea().geoAreaPosLongitude(), circAtLat((m_paramGPS->data().referencePosition().latitude + gac.destinationArea().geoAreaPosLatitude())/2)) / 10000000.;
1859  y = LAT2METER*((m_paramGPS->data().referencePosition().latitude - gac.destinationArea().geoAreaPosLatitude())/10000000.);
1860  y *= y < 0 ? -1 : 1;
1861 
1862  double inArea;
1863  if(shape == shape_circle)
1864  inArea = f(x, y, gac.destinationArea().distanceA(), gac.destinationArea().distanceB(), shape);
1865  else
1866  inArea = f(
1867  x*cos(gac.destinationArea().angle())-y*sin(gac.destinationArea().angle()),
1868  x*sin(gac.destinationArea().angle())+y*cos(gac.destinationArea().angle()),
1869  gac.destinationArea().distanceA(), gac.destinationArea().distanceB(), shape);
1870  /* Router is in the destination area */
1871  if(inArea >= 0)
1872  {
1873  quint16 port, portInfo;
1874  get_UINT16(packet + GACPacket::helper_size, &port);
1875  get_UINT16(packet + GACPacket::helper_size + 2, &portInfo);
1876 
1877  gen2fac->writeDatagram(QByteArray((const char*)packet + GACPacket::helper_size + 4, ch.payloadLength() - 4),
1878  QHostAddress::LocalHost, m_paramNetwork->internalBTPPort(port, portInfo));
1879  qDebug() << "Sending packet to" << port << portInfo << m_paramNetwork->internalBTPPort(port, portInfo);
1880 
1881  sendEventIndicationToUT((uchar*)payload.data(), payload.length());
1882 
1883  return geonetError_success;
1884  }
1885  else
1886  {
1888  {
1890 
1892  {
1893  default:
1896  res = greedy(bh, ch, gac, payload, packetIndex, eth->ether_dhost, eth->ether_shost);
1897  break;
1899  res = CBFUnicast(bh, ch, gac, payload);
1900  break;
1901  }
1902 
1903  if(res.isInteger() && res.integer() == 0)
1905  else if(res.isInteger() && res.integer() == -1)
1907 
1908  /* Construct the Ethernet header : destination addr */
1909  if(res.isMacAddr())
1910  for(int i = 0 ; i < 6 ; i++)
1911  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
1912  else
1914 
1915  /* Construct the packet */
1916  QByteArray basicHeaderData;
1917  QDataStream ds(&basicHeaderData, QIODevice::WriteOnly);
1918 
1919  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
1920  ds.writeRawData(gac.toHex().data(), gac.toHex().length());
1921  ds.writeRawData(payload, payload.length());
1922 
1923  int ret = send(basicHeaderData, false, bh);
1924  if(ret == geonetError_success)
1925  printf("\nSuccesfully forwarded GAC");
1926 
1927  return ret;
1928  }
1929  else
1931  }
1932 }
1933 
1935 {
1936  QByteArray payload((char*)packet + GBCPacket::helper_size, ch.payloadLength());
1937  GBCPacket gbc;
1938  gbc.extract(packet);
1939 
1940  verifLocT();
1941 
1942  QByteArray packetIndex = QByteArray((const char*)gbc.soPv().gnAddr().toHex(), GnAddr::helper_size);
1943 
1944  /* If itsGnGeoBroadcastForwardingAlgorithm is Unspecified or Simple */
1947  {
1948  /* DuplicatePacketDetection */
1949  if(m_locationTable->find(packetIndex) != m_locationTable->end())
1950  if(m_HPB->isDuplicateTimestamp(gbc) || isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].lastTimestamp(), gbc.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), gbc.sequenceNumber()))
1951  {
1952  printf("DPD\n");
1954  }
1955  }
1956 
1957  /* DuplicateAddressDetection */
1959  {
1960  bool a = true;
1961  bool b = true;
1962 
1963  for(int i=0; i<6; i++)
1964  if(gbc.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
1965  a = false;
1966  b = (gbc.soPv().gnAddr().MStationTypeCountryCode() == m_gnAddr->MStationTypeCountryCode());
1967 
1968  if(a && b)
1969  {
1970  emit m_manager->changeMID();
1971  m_RP->m_changeFilter = true;
1972  printf("DAD\n");
1974  }
1975  }
1976 
1977  QMutexLocker locker(m_mutexLocT);
1978  /* The entry does not exist and will be created */
1979  if((*m_locationTable).find(packetIndex) == (*m_locationTable).end())
1980  {
1981  (*m_locationTable)[packetIndex].setLongPositionVector(gbc.soPv());
1982  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
1983  (*m_locationTable)[packetIndex].setVersion(bh.version());
1984  (*m_locationTable)[packetIndex].setStationType(gbc.soPv().gnAddr().stationType());
1985  (*m_locationTable)[packetIndex].setIsNeighbour(false);
1986  (*m_locationTable)[packetIndex].setLastTimestamp(gbc.soPv().timestamp());
1987  (*m_locationTable)[packetIndex].setSequenceNumber(gbc.sequenceNumber());
1988 
1989  /* TODO update the PDR
1990  itsGnMaxPacketDataRateEmaBeta
1991  itsGnMaxPacketDataRate
1992  (*m_locationTable)[packetIndex].setPDR(...) */
1993 
1994  }
1995  else /* The entry exist and will be updated */
1996  {
1997  /* If the packet is more recent the entry will be update */
1998  if(!m_HPB->isDuplicateTimestamp(gbc) && !isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].longPositionVector().timestamp(), gbc.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), gbc.sequenceNumber()))
1999  {
2000  (*m_locationTable)[packetIndex].setLongPositionVector(gbc.soPv());
2001  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
2002  (*m_locationTable)[packetIndex].setVersion(bh.version());
2003  (*m_locationTable)[packetIndex].setStationType(gbc.soPv().gnAddr().stationType());
2004  (*m_locationTable)[packetIndex].setLastTimestamp(gbc.soPv().timestamp());
2005  (*m_locationTable)[packetIndex].setSequenceNumber(gbc.sequenceNumber());
2006  /* TODO update the PDR
2007  itsGnMaxPacketDataRateEmaBeta
2008  itsGnMaxPacketDataRate
2009  (*m_locationTable)[packetIndex].setPDR(...) */
2010 
2011  }
2012  }
2013  locker.unlock();
2014  //m_HPB->addPacket(bh, ch, gbc, payload);
2015  flushPB(gbc.soPv().gnAddr());
2016 
2017  e_shape shape;
2018  switch(ch.headerSubType())
2019  {
2021  shape = shape_circle;
2022  if(((gbc.destinationArea().distanceA() * gbc.destinationArea().distanceA()) * M_PI) > (itsGnMaxGeoAreaSize * 1000 * 1000))
2023  {
2024  printf("GBC_AREA_SIZE_C\n");
2026  }
2027  break;
2029  shape = shape_rectangle;
2030  if((gbc.destinationArea().distanceA() * gbc.destinationArea().distanceB()) > (itsGnMaxGeoAreaSize * 1000 * 1000))
2031  {
2032  printf("GBC_AREA_SIZE_R\n");
2034  }
2035  break;
2037  shape = shape_ellipse;
2038  if((gbc.destinationArea().distanceA() * gbc.destinationArea().distanceB() * M_PI) > (itsGnMaxGeoAreaSize * 1000 * 1000))
2039  {
2040  printf("GBC_AREA_SIZE_E\n");
2042  }
2043  break;
2044  default :
2045  shape = shape_none;
2046  break;
2047  }
2048 
2049  double x, y;
2050  x = diameter(m_paramGPS->data().referencePosition().longitude, gbc.destinationArea().geoAreaPosLongitude(),
2051  circAtLat((m_paramGPS->data().referencePosition().latitude + gbc.destinationArea().geoAreaPosLatitude())/2)) / 10000000.;
2052  y = LAT2METER*((m_paramGPS->data().referencePosition().latitude - gbc.destinationArea().geoAreaPosLatitude())/10000000.);
2053  y *= y < 0 ? -1 : 1;
2054 
2055  double inArea;
2056  if(shape == shape_circle)
2057  inArea = f(x, y, gbc.destinationArea().distanceA(), gbc.destinationArea().distanceB(), shape);
2058  else
2059  inArea = f(
2060  x*cos(gbc.destinationArea().angle())-y*sin(gbc.destinationArea().angle()),
2061  x*sin(gbc.destinationArea().angle())+y*cos(gbc.destinationArea().angle()),
2062  gbc.destinationArea().distanceA(), gbc.destinationArea().distanceB(), shape);
2063  /* Router is in the destination area */
2064  if(inArea>=0)
2065  {
2066  quint16 port, portInfo;
2067  get_UINT16(packet + GBCPacket::helper_size, &port);
2068  get_UINT16(packet + GBCPacket::helper_size + 2, &portInfo);
2069 
2070  printf("PACKET send to FAC\n");
2071  gen2fac->writeDatagram(QByteArray((const char*)packet + GBCPacket::helper_size + 4, ch.payloadLength() - 4),
2072  QHostAddress::LocalHost, m_paramNetwork->internalBTPPort(port, portInfo));
2073  qDebug() << "Sending packet to" << port << portInfo << m_paramNetwork->internalBTPPort(port, portInfo);
2074 
2075  sendEventIndicationToUT((uchar*)payload.data(), payload.length());
2076  }
2077  else
2078  printf("Not in area\n");
2079 
2080  /* Forwarding GBC packet */
2081 
2082  if(bh.rhl() < 1)
2083  return geonetError_success;
2084 
2087  {
2088  default:
2091  res = simpleForwardingAlgorithm(bh, ch, gbc, payload, shape, packetIndex, false);
2092  break;
2094  res = contentionBasedForwardingAlgorithm(bh, ch, gbc, payload, shape, packetIndex, false);
2095  break;
2097  res = advancedForwarding(bh, ch, gbc, payload, shape, packetIndex, eth->ether_dhost, eth->ether_shost, false);
2098  break;
2099  }
2100 
2101  if(res.isInteger() && res.integer() == 0)
2103  else if(res.isInteger() && res.integer() == -1)
2105 
2106  /* Construct the Ethernet header : destination addr */
2107  if(res.isMacAddr())
2108  for(int i = 0 ; i < 6 ; i++)
2109  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
2110  else
2112 
2113  /* Construct the packet */
2114  QByteArray basicHeaderData;
2115  QDataStream ds(&basicHeaderData, QIODevice::WriteOnly);
2116 
2117  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
2118  ds.writeRawData(gbc.toHex().data(), gbc.toHex().length());
2119  ds.writeRawData(payload, payload.length());
2120 
2121  int ret = send(basicHeaderData, false, bh);
2122  if(ret == geonetError_success)
2123  printf("\nSuccesfully forwarded GBC");
2124 
2125  return ret;
2126 }
2127 
2129 {
2130  Q_UNUSED(eth);
2131  QByteArray payload((char*)packet + GUCPacket::helper_size, ch.payloadLength());
2132  GUCPacket guc;
2133  guc.extract(packet);
2134 
2135  verifLocT();
2136 
2137  QByteArray packetIndex = QByteArray((const char*)guc.soPv().gnAddr().toHex(), GnAddr::helper_size);
2138 
2139  /* If itsGnGeoBroadcastForwardingAlgorithm is Unspecified or Simple */
2142  {
2143  /* DuplicatePacketDetection */
2144  if(m_locationTable->find(packetIndex) != m_locationTable->end() && !(*m_locationTable)[packetIndex].isPending())
2145  if(m_HPB->isDuplicateTimestamp(guc) || isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].lastTimestamp(), guc.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), guc.sequenceNumber()))
2147  }
2148 
2149  /* DuplicateAddressDetection */
2151  {
2152  bool a = true;
2153  bool b = true;
2154 
2155  for(int i=0; i<6; i++)
2156  if(guc.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
2157  a = false;
2158  b = (guc.soPv().gnAddr().MStationTypeCountryCode() == m_gnAddr->MStationTypeCountryCode());
2159 
2160  if(a && b)
2161  {
2162  emit m_manager->changeMID();
2163  m_RP->m_changeFilter = true;
2165  }
2166  }
2167 
2168  QMutexLocker locker(m_mutexLocT);
2169  /* The entry does not exist and will be created */
2170  if((*m_locationTable).find(packetIndex) == (*m_locationTable).end())
2171  {
2172  (*m_locationTable)[packetIndex].setLongPositionVector(guc.soPv());
2173  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
2174  (*m_locationTable)[packetIndex].setVersion(bh.version());
2175  (*m_locationTable)[packetIndex].setStationType(guc.soPv().gnAddr().stationType());
2176  (*m_locationTable)[packetIndex].setIsNeighbour(false);
2177  (*m_locationTable)[packetIndex].setLastTimestamp(guc.soPv().timestamp());
2178  (*m_locationTable)[packetIndex].setSequenceNumber(guc.sequenceNumber());
2179 
2180  /* TODO update the PDR
2181  itsGnMaxPacketDataRateEmaBeta
2182  itsGnMaxPacketDataRate
2183  (*m_locationTable)[packetIndex].setPDR(...) */
2184 
2185  }
2186  else /* The entry exist and will be updated */
2187  {
2188  /* If the packet is more recent the entry will be update */
2189  if((!m_HPB->isDuplicateTimestamp(guc) &&
2190  !isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].longPositionVector().timestamp(), guc.soPv().timestamp(),
2191  (*m_locationTable)[packetIndex].sequenceNumber(), guc.sequenceNumber()))
2192  || (*m_locationTable)[packetIndex].isPending())
2193  {
2194  (*m_locationTable)[packetIndex].setLongPositionVector(guc.soPv());
2195  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
2196  (*m_locationTable)[packetIndex].setVersion(bh.version());
2197  (*m_locationTable)[packetIndex].setStationType(guc.soPv().gnAddr().stationType());
2198  (*m_locationTable)[packetIndex].setLastTimestamp(guc.soPv().timestamp());
2199  (*m_locationTable)[packetIndex].setSequenceNumber(guc.sequenceNumber());
2200  if((*m_locationTable)[packetIndex].isPending())
2201  {
2202  (*m_locationTable)[packetIndex].setNeverReceived(false);
2203  (*m_locationTable)[packetIndex].setLsPending(false);
2204  }
2205 
2206  /* TODO update the PDR
2207  itsGnMaxPacketDataRateEmaBeta
2208  itsGnMaxPacketDataRate
2209  (*m_locationTable)[packetIndex].setPDR(...) */
2210 
2211  }
2212  }
2213 
2214  locker.unlock();
2215 
2216  flushPB(guc.soPv().gnAddr());
2217 
2218  /* If this routeur is the destination */
2219  if(guc.dePv().gnAddr() == *m_gnAddr)
2220  {
2221  quint16 port, portInfo;
2222  get_UINT16(packet + GUCPacket::helper_size, &port);
2223  get_UINT16(packet + GUCPacket::helper_size + 2, &portInfo);
2224 
2225  gen2fac->writeDatagram(QByteArray((const char*)packet + GUCPacket::helper_size + 4, ch.payloadLength() - 4),
2226  QHostAddress::LocalHost, m_paramNetwork->internalBTPPort(port, portInfo));
2227  qDebug() << "Sending packet to" << port << portInfo << m_paramNetwork->internalBTPPort(port, portInfo);
2228 
2229  sendEventIndicationToUT((uchar*)payload.data(), payload.length());
2230 
2231  return geonetError_success;
2232  }
2233 
2234  locker.relock();
2235 
2236  QByteArray destIndex = QByteArray((const char*)guc.dePv().gnAddr().toHex(), GnAddr::helper_size);
2237 
2238  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(destIndex);
2239  if(pos == m_locationTable->end() || LocationTableEntry(pos.value()).isNeighbour() == 0)
2240  if(bh.nextHeader()==0 || bh.nextHeader()==1)
2241  (*m_locationTable)[destIndex].setLongPositionVector(LongPositionVector(guc.dePv().gnAddr(), guc.dePv().timestamp(), guc.dePv().latitude(), guc.dePv().longitude(), 0, 0, 0));
2242 
2243 
2244  /* If the destination position vector is different than our we update it */
2245  if((*m_locationTable)[destIndex].lastTimestamp() < guc.dePv().timestamp())
2246  {
2247  (*m_locationTable)[destIndex].setLastTimestamp(guc.dePv().timestamp());
2248  (*m_locationTable)[destIndex].setLongPositionVector(guc.dePv());
2249  }
2250  else
2251  {
2252  guc.setDePv((*m_locationTable)[destIndex].longPositionVector());
2253  }
2254 
2255 
2256  locker.unlock();
2257 
2258  if(bh.rhl() < 1)
2259  return geonetError_success;
2260 
2262 
2264  {
2265  default:
2268  res = greedy(bh, ch, guc, payload, packetIndex);
2269  break;
2271  res = CBFUnicast(bh, ch, guc, payload);
2272  break;
2273  }
2274 
2275  if(res.isInteger() && res.integer() == 0)
2277  else if(res.isInteger() && res.integer() == -1)
2279 
2280  /* Construct the Ethernet header : destination addr */
2281  if(res.isMacAddr())
2282  for(int i = 0 ; i < 6 ; i++)
2283  m_paramNetwork->eh.ether_dhost[i] = res.macAddr()[i];
2284  else
2286 
2287  /* Construct the packet */
2288  QByteArray basicHeaderData;
2289  QDataStream ds(&basicHeaderData, QIODevice::WriteOnly);
2290 
2291  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
2292  ds.writeRawData(guc.toHex().data(), guc.toHex().length());
2293  ds.writeRawData(payload, payload.length());
2294 
2295  int ret = send(basicHeaderData, false, bh);
2296  if(ret == geonetError_success)
2297  printf("\nSuccesfully forwarded GUC");
2298 
2299  return ret;
2300 }
2301 
2303 {
2304  SHBPacket shb;
2305  shb.extract(packet);
2306  verifLocT();
2307 
2308  QByteArray packetIndex = QByteArray((const char*)shb.soPv().gnAddr().toHex(), GnAddr::helper_size);
2309 
2310  /* DuplicatePacketDetection */
2311  if(m_locationTable->find(packetIndex) != m_locationTable->end())
2312  if(m_HPB->isDuplicateTimestamp(shb) || isDuplicatePacketDetectionTimestamp((*m_locationTable)[packetIndex].lastTimestamp(), shb.soPv().timestamp()))
2314  /* DuplicateAddressDetection */
2316  {
2317  bool a = true;
2318  bool b = true;
2319 
2320  for(int i=0; i<6; i++)
2321  if(shb.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
2322  a = false;
2324 
2325  if(a && b)
2326  {
2327  emit m_manager->changeMID();
2328  m_RP->m_changeFilter = true;
2330  }
2331  }
2332 
2333  QMutexLocker locker(m_mutexLocT);
2334  (*m_locationTable)[packetIndex].setLongPositionVector(shb.soPv());
2335  (*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
2336  (*m_locationTable)[packetIndex].setLastTimestamp(shb.soPv().timestamp());
2337  (*m_locationTable)[packetIndex].setVersion(bh.version());
2338  (*m_locationTable)[packetIndex].setStationType(shb.soPv().gnAddr().stationType());
2339  (*m_locationTable)[packetIndex].setIsNeighbour(true);
2340 
2342 
2343  /* TODO update the PDR
2344  itsGnMaxPacketDataRateEmaBeta
2345  itsGnMaxPacketDataRate
2346  (*m_locationTable)[packetIndex].setPDR(...) */
2347  locker.unlock();
2348 
2349  checkBCFPB();
2350  checkUCFPB();
2351 
2352  quint16 port, portInfo;
2353  get_UINT16(packet + SHBPacket::helper_size, &port);
2354  get_UINT16(packet + SHBPacket::helper_size + 2, &portInfo);
2355 
2356  gen2fac->writeDatagram(QByteArray((const char*)packet + SHBPacket::helper_size + 4, ch.payloadLength() - 4),
2357  QHostAddress::LocalHost, m_paramNetwork->internalBTPPort(port, portInfo));
2358  qDebug() << "Sending packet to" << port << portInfo << m_paramNetwork->internalBTPPort(port, portInfo);
2359 
2361  return geonetError_success;
2362 }
2363 
2365 {
2366  Q_UNUSED(eth);
2367  QByteArray payload((const char*)packet + TSBPacket::helper_size, ch.payloadLength());
2368  TSBPacket tsb;
2369  tsb.extract(packet);
2370 
2371  verifLocT();
2372 
2373  QByteArray packetIndex = QByteArray((const char*)tsb.soPv().gnAddr().toHex(), GnAddr::helper_size);
2374 
2375  /* DuplicatePacketDetection */
2376  if(m_locationTable->find(packetIndex) != m_locationTable->end())
2377  if(m_HPB->isDuplicateTimestamp(tsb) || isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].lastTimestamp(), tsb.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), tsb.sequenceNumber()))
2379  /* DuplicateAddressDetection */
2381  {
2382  bool a = true;
2383  bool b = true;
2384 
2385  for(int i=0; i<6; i++)
2386  if(tsb.soPv().gnAddr().mid()[i] != m_gnAddr->mid(i))
2387  a = false;
2388  b = (tsb.soPv().gnAddr().MStationTypeCountryCode() == m_gnAddr->MStationTypeCountryCode());
2389 
2390  if(a && b)
2391  {
2392  emit m_manager->changeMID();
2393  m_RP->m_changeFilter = true;
2395  }
2396  }
2397 
2398 
2399  QMutexLocker locker(m_mutexLocT);
2400  /* The entry does not exist and will be created */
2401  if((*m_locationTable).find(packetIndex) == (*m_locationTable).end())
2402  {
2403  (*m_locationTable)[packetIndex].setLongPositionVector(tsb.soPv());
2404  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
2405  (*m_locationTable)[packetIndex].setVersion(bh.version());
2406  (*m_locationTable)[packetIndex].setStationType(tsb.soPv().gnAddr().stationType());
2407  (*m_locationTable)[packetIndex].setIsNeighbour(false);
2408  (*m_locationTable)[packetIndex].setLastTimestamp(tsb.soPv().timestamp());
2409  (*m_locationTable)[packetIndex].setSequenceNumber(tsb.sequenceNumber());
2410 
2411  /* TODO update the PDR
2412  itsGnMaxPacketDataRateEmaBeta
2413  itsGnMaxPacketDataRate
2414  (*m_locationTable)[packetIndex].setPDR(...) */
2415 
2416  }
2417  else /* The entry exist and will be updated */
2418  {
2419  /* If the packet is more recent the entry will be update */
2420  if(!m_HPB->isDuplicateTimestamp(tsb) && !isDuplicatePacketDetectionTimestampSN((*m_locationTable)[packetIndex].longPositionVector().timestamp(), tsb.soPv().timestamp(), (*m_locationTable)[packetIndex].sequenceNumber(), tsb.sequenceNumber()))
2421  {
2422  (*m_locationTable)[packetIndex].setLongPositionVector(tsb.soPv());
2423  //(*m_locationTable)[packetIndex].setLlAddr(eth->ether_shost);
2424  (*m_locationTable)[packetIndex].setVersion(bh.version());
2425  (*m_locationTable)[packetIndex].setStationType(tsb.soPv().gnAddr().stationType());
2426  (*m_locationTable)[packetIndex].setLastTimestamp(tsb.soPv().timestamp());
2427  (*m_locationTable)[packetIndex].setSequenceNumber(tsb.sequenceNumber());
2428 
2429  /* TODO update the PDR
2430  itsGnMaxPacketDataRateEmaBeta
2431  itsGnMaxPacketDataRate
2432  (*m_locationTable)[packetIndex].setPDR(...) */
2433 
2434  }
2435  }
2436 
2437 
2438  /* TODO update the PDR
2439  itsGnMaxPacketDataRateEmaBeta
2440  itsGnMaxPacketDataRate
2441  (*m_locationTable)[packetIndex].setPDR(...) */
2442  locker.unlock();
2443  flushPB(tsb.soPv().gnAddr());
2444 
2445  quint16 port, portInfo;
2446  get_UINT16(packet + TSBPacket::helper_size, &port);
2447  get_UINT16(packet + TSBPacket::helper_size + 2, &portInfo);
2448 
2449  gen2fac->writeDatagram(QByteArray((const char*)packet + TSBPacket::helper_size + 4, ch.payloadLength() - 4),
2450  QHostAddress::LocalHost, m_paramNetwork->internalBTPPort(port, portInfo));
2451  qDebug() << "Sending packet to" << port << portInfo << m_paramNetwork->internalBTPPort(port, portInfo);
2452 
2454 
2455  /* Forwarding TSB packet */
2456 
2457  if(bh.rhl() < 1)
2458  return geonetError_success;
2459 
2460  bool noNeighbour = true;
2461  locker.relock();
2462  if(!m_locationTable->isEmpty())
2463  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();i != m_locationTable->end(); ++i)
2464  if(LocationTableEntry(i.value()).isNeighbour())
2465  noNeighbour = false;
2466  locker.unlock();
2467 
2468  if(noNeighbour && ch.trafficClass().SCF() == 1)
2469  {
2470  m_BCFPB->addPacket(bh, ch, tsb, payload, packetIndex);
2472  }
2473  else
2474  {
2475  /* Construct the packet */
2476  QByteArray tsbData;
2477  QDataStream ds(&tsbData, QIODevice::WriteOnly);
2478 
2479  ds.writeRawData(ch.toHex().data(), ch.toHex().length());
2480  ds.writeRawData(tsb.toHex().data(), tsb.toHex().length());
2481  ds.writeRawData(payload.data(), payload.length());
2482 
2483  /* Construct the Ethernet header : destination addr */
2484  for(int i = 0 ; i < 6 ; i++)
2485  m_paramNetwork->eh.ether_dhost[i] = 0xFF;
2486 
2487  int ret = send(tsbData, false, bh);
2488 
2489  return ret;
2490  }
2491  return geonetError_success;
2492 }
2493 
2494 /* -------------------------FORWARDING-------------------------ALGORITHM------------------------- */
2495 
2496 int GeoNet::TO_CBF_GBC(int distance)
2497 {
2498  if(distance > itsGnDefaultMaxCommunicationRange)
2500  else
2502 }
2503 
2504 int GeoNet::TO_CBF_GUC(int progress)
2505 {
2506  if(progress > itsGnDefaultMaxCommunicationRange)
2508  else
2510 }
2511 
2512 ForwardingAlgorithmResponse GeoNet::greedy(BasicHeader bh, CommonHeader ch, GACPacket packet, QByteArray payload, QByteArray sender, uchar* destination, uchar* forwarder)
2513 {return greedy(PacketBuffersEntry(bh, ch, packet, payload, destination, forwarder), sender);}
2514 
2515 ForwardingAlgorithmResponse GeoNet::greedy(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray payload, QByteArray sender, uchar* destination, uchar* forwarder)
2516 {return greedy(PacketBuffersEntry(bh, ch, packet, payload, destination, forwarder), sender);}
2517 
2518 ForwardingAlgorithmResponse GeoNet::greedy(BasicHeader bh, CommonHeader ch, GUCPacket packet, QByteArray payload, QByteArray sender)
2519 {return greedy(PacketBuffersEntry(bh, ch, packet, payload), sender);}
2520 
2522 {return greedy(PacketBuffersEntry (bh, ch, packet), sender);}
2523 
2525 {
2527  uchar NH[6];
2528  double distDest;
2529  qint32 packetLat, packetLong;
2530  switch(packet.commonHeader().headerType())
2531  {
2533  packetLat = packet.gacPacket().destinationArea().geoAreaPosLatitude();
2534  packetLong = packet.gacPacket().destinationArea().geoAreaPosLongitude();
2535  break;
2537  packetLat = packet.gbcPacket().destinationArea().geoAreaPosLatitude();
2538  packetLong = packet.gbcPacket().destinationArea().geoAreaPosLongitude();
2539  break;
2541  packetLat = packet.gucPacket().dePv().latitude();
2542  packetLong = packet.gucPacket().dePv().longitude();
2543  break;
2546  {
2547  packetLat = packet.lsReplyPacket().dePv().latitude();
2548  packetLong = packet.lsReplyPacket().dePv().longitude();
2549  }
2550  else
2551  {
2552  retour.setInteger(-1);
2553  return retour;
2554  }
2555  break;
2557  default:
2558  retour.setInteger(-1);
2559  return retour;
2560  break;
2561  }
2562 
2563  distDest = dist(packetLat, packetLong, m_paramGPS->data().referencePosition().latitude,
2565 
2566  double MFR = distDest;
2567  bool hasNeighbour = false;
2568 
2569  int cpt = 0;
2570 
2571  QMutexLocker locker(m_mutexLocT);
2572  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();i != m_locationTable->end(); ++i, cpt++)
2573  {
2574  LocationTableEntry tmp(i.value());
2575  if(i.key() != sender && tmp.isNeighbour() != 0 && *tmp.isNeighbour())
2576  {
2577  hasNeighbour = true;
2578  double distNeighbour = dist(packetLat, packetLong, tmp.longPositionVector().latitude(),
2579  tmp.longPositionVector().longitude());
2580  if(distNeighbour<MFR)
2581  {
2582  for(int i = 0; i < MAC_ADDR_SIZE; i++)
2583  NH[i] = tmp.llAddr()[i];
2584  MFR = distNeighbour;
2585  }
2586  }
2587  }
2588  locker.unlock();
2589 
2590  if(MFR<distDest)
2591  {
2592  retour.setMacAddr(NH);
2593 
2595  return retour;
2596  }
2597  else
2598  {
2599  if(!hasNeighbour && packet.commonHeader().trafficClass().SCF())
2600  {
2601  /* Ajouter le packet au buffer (bc ou uc) */
2603  {
2604  m_UCFPB->addPacket(packet.basicHeader(), packet.commonHeader(), packet.gucPacket(), packet.payload(), sender);
2605  printf("Packet stocké dans le UnicastPacketBuffer\n");
2606  }
2608  {
2609  m_BCFPB->addPacket(packet.basicHeader(), packet.commonHeader(), packet.gbcPacket(), packet.payload(), sender, packet.destination(), packet.forwarder());
2610  printf("Packet stocké dans le BroadcastPacketBuffer\n");
2611  }
2613  retour.setInteger(0);
2614  return retour;
2615  }
2616  else
2617  {
2618  for(int i = 0; i < MAC_ADDR_SIZE; i++)
2619  NH[i] = 0xFF;
2620  retour.setMacAddr(NH);
2622  return retour;
2623  }
2624  }
2625 }
2626 
2627 ForwardingAlgorithmResponse GeoNet::simpleForwardingAlgorithm(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray packetPayload, e_shape shape, QByteArray sender, bool flagSrc)
2628 {
2630  double x, y;
2631 
2634  y = LAT2METER * ((m_paramGPS->data().referencePosition().latitude - packet.destinationArea().geoAreaPosLatitude()) / 10000000.);
2635  y *= y < 0 ? -1 : 1;
2636 
2637  double inArea;
2638  if(shape == shape_circle)
2639  inArea = f(x, y, packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2640  else
2641  inArea = f(
2642  x*cos(packet.destinationArea().angle())-y*sin(packet.destinationArea().angle()),
2643  x*sin(packet.destinationArea().angle())+y*cos(packet.destinationArea().angle()),
2644  packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2645  if(inArea>=0)
2646  {
2647  uchar NH[MAC_ADDR_SIZE];
2648  for(int i = 0; i < MAC_ADDR_SIZE; i++)
2649  NH[i] = 0xFF;
2650  addr.setMacAddr(NH);
2652  return addr;
2653  }
2654  else
2655  {
2656  if(flagSrc)
2657  {
2658  return greedy(bh, ch, packet, packetPayload, sender, m_gnAddr->mid(), m_gnAddr->mid());
2659  }
2660  else
2661  {
2662  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(sender);
2663  if(pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai())
2664  {
2665  LocationTableEntry lte = LocationTableEntry(pos.value());
2666 
2669  y = LAT2METER*((lte.longPositionVector().latitude() - packet.destinationArea().geoAreaPosLatitude())/10000000.);
2670  y *= y<0?-1:1;
2671 
2672  double inAreaExp;
2673  if(shape == shape_circle)
2674  inAreaExp = f(x, y, packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2675  else
2676  inAreaExp = f(
2677  x*cos(packet.destinationArea().angle())-y*sin(packet.destinationArea().angle()),
2678  x*sin(packet.destinationArea().angle())+y*cos(packet.destinationArea().angle()),
2679  packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2680 
2681  if(inAreaExp<0)
2682  {
2683  return greedy(bh, ch, packet, packetPayload, sender, m_gnAddr->mid(), m_gnAddr->mid());
2684  }
2685  else
2686  {
2688  addr.setInteger(-1);
2689  return addr;
2690  }
2691  }
2692  else
2693  {
2694  uchar NH[MAC_ADDR_SIZE];
2695  for(int i = 0; i < MAC_ADDR_SIZE; i++)
2696  NH[i] = 0xFF;
2697  addr.setMacAddr(NH);
2699  return addr;
2700  }
2701  }
2702  }
2703 }
2704 
2705 ForwardingAlgorithmResponse GeoNet::contentionBasedForwardingAlgorithm(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray packetPayload, e_shape shape, QByteArray sender, bool flagSrc)
2706 {
2708  double x, y;
2709 
2710  if(m_CBFPB->isDuplicateTimestamp(packet))
2711  {
2712  m_CBFPB->erase(packet);
2714  addr.setInteger(-1);
2715  return addr;
2716  }
2717  else
2718  {
2719 
2722  y *= y<0?-1:1;
2723 
2724  double inArea;
2725  if(shape == shape_circle)
2726  inArea = f(x, y, packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2727  else
2728  inArea = f(
2729  x*cos(packet.destinationArea().angle())-y*sin(packet.destinationArea().angle()),
2730  x*sin(packet.destinationArea().angle())+y*cos(packet.destinationArea().angle()),
2731  packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2732 
2733  if(inArea>=0)
2734  {
2735  QMutexLocker locker(m_mutexLocT);
2736  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(packet.soPv().gnAddr().toHex());
2737  int timer_CBF_GBC;
2738  if(pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai())
2739  {
2740  double distance = dist(LocationTableEntry(pos.value()).longPositionVector().latitude(), LocationTableEntry(pos.value()).longPositionVector().longitude(), m_paramGPS->data().referencePosition().latitude, m_paramGPS->data().referencePosition().longitude);
2741  timer_CBF_GBC = TO_CBF_GBC(distance);
2742  }
2743  else
2744  timer_CBF_GBC = itsGnGeoBroadcastCbfMaxTime;
2745  locker.unlock();
2746 
2747  if(m_CBFPB->addPacket(bh, ch, packet, packetPayload, timer_CBF_GBC) > 0)
2748  printf("Succès de l'ajout du packet au cbfpb!\n");
2749  else
2750  printf("Echec de l'ajout du packet au cbfpb : DuplicatePacket (Ceci n'est pas censé arrivé)\n");
2751 
2753  addr.setInteger(0);
2754  return addr;
2755  }
2756  else
2757  {
2758  if(flagSrc)
2759  {
2760  return greedy(bh, ch, packet, packetPayload, sender, m_gnAddr->mid(), m_gnAddr->mid());
2761  }
2762  else
2763  {
2764  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(sender);
2765  if(pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai())
2766  {
2767  LocationTableEntry lte = LocationTableEntry(pos.value());
2768 
2770  y = LAT2METER*((lte.longPositionVector().latitude() - packet.destinationArea().geoAreaPosLatitude())/10000000.);
2771  y *= y<0?-1:1;
2772 
2773  double inAreaExp;
2774  if(shape == shape_circle)
2775  inAreaExp = f(x, y, packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2776  else
2777  inAreaExp = f(
2778  x*cos(packet.destinationArea().angle())-y*sin(packet.destinationArea().angle()),
2779  x*sin(packet.destinationArea().angle())+y*cos(packet.destinationArea().angle()),
2780  packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2781 
2782  if(inAreaExp<0)
2783  return greedy(bh, ch, packet, packetPayload, sender, m_gnAddr->mid(), m_gnAddr->mid());
2784  else
2785  {
2787  addr.setInteger(-1);
2788  return addr;
2789  }
2790  }
2791  else
2792  {
2793  uchar NH[MAC_ADDR_SIZE];
2794  for(int i = 0; i < MAC_ADDR_SIZE; i++)
2795  NH[i] = 0xFF;
2796  addr.setMacAddr(NH);
2798  return addr;
2799  }
2800  }
2801  }
2802  }
2803 }
2804 
2805 ForwardingAlgorithmResponse GeoNet::advancedForwarding(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray packetPayload, e_shape shape, QByteArray sender, uchar* destEth, uchar* forwarderEth, bool flagSrc)
2806 {
2808 
2809  double x, y;
2812  y *= y<0?-1:1;
2813 
2814  double xSender, ySender;
2815  if(flagSrc)
2816  {
2817  xSender = x;
2818  ySender = y;
2819  }
2820  else
2821  {
2822  xSender = diameter(packet.soPv().longitude(), packet.destinationArea().geoAreaPosLongitude(), circAtLat((packet.soPv().latitude() + packet.destinationArea().geoAreaPosLatitude())/2));
2823  ySender = LAT2METER*((packet.soPv().latitude() - packet.destinationArea().geoAreaPosLatitude())/10000000.);
2824  ySender *= ySender<0?-1:1;
2825  }
2826 
2827  double xFrwrd = 0, yFrwrd = 0;
2828  if(flagSrc)
2829  {
2830  xFrwrd = x;
2831  yFrwrd = y;
2832  }
2833  else
2834  {
2835  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();i != m_locationTable->end(); ++i)
2836  {
2837  bool isForwarder = true;
2838  for(int j = 0; j < ETHER_ADDR_LEN; j++)
2839  if(i->llAddr(j) != forwarderEth[j])
2840  isForwarder = false;
2841  if(isForwarder)
2842  {
2843  xFrwrd = diameter(i->longPositionVector().longitude(), packet.destinationArea().geoAreaPosLongitude(), circAtLat((i->longPositionVector().latitude() + packet.destinationArea().geoAreaPosLatitude())/2));
2844  yFrwrd = LAT2METER*((i->longPositionVector().longitude() - packet.destinationArea().geoAreaPosLatitude())/10000000.);
2845  yFrwrd *= yFrwrd<0?-1:1;
2846  }
2847  }
2848  }
2849 
2850  double inout1;
2851  if(shape == shape_circle)
2852  inout1 = f(x, y, packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2853  else
2854  inout1 = f(
2855  x*cos(packet.destinationArea().angle())-y*sin(packet.destinationArea().angle()),
2856  x*sin(packet.destinationArea().angle())+y*cos(packet.destinationArea().angle()),
2857  packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2858 
2859  QByteArray packetCPT = QByteArray(QByteArray(packet.soPv().gnAddr().toHex(),GnAddr::helper_size)+QByteArray(packet.soPv().timestamp(),32)+QByteArray(packet.sequenceNumber(),16), GnAddr::helper_size + 32 + 16);
2860 
2861  if(inout1 >= 0)
2862  {
2863  if(m_CBFPB->isDuplicateTimestamp(packet))
2864  {
2866  {
2867  m_CBFPB->erase(packet);
2868  m_CBFPBCounter.erase(m_CBFPBCounter.find(packetCPT));
2870  addr.setInteger(-1);
2871  return addr;
2872  }
2873  else
2874  {
2875 // double dist_r = dist(m_paramGPS->data().referencePosition().latitude, m_paramGPS->data().referencePosition().longitude, packet.soPv().latitude(), packet.soPv().longitude());
2876 // double dist_f = dist(frwrdLat, frwrdLong, senderLat, senderLong);
2877 // double dist_fr = dist(m_paramGPS->data().referencePosition().latitude, m_paramGPS->data().referencePosition().longitude, frwrdLat, frwrdLong);
2878  double dist_r = dist(x, y, xSender, ySender);
2879  double dist_f = dist(xFrwrd, yFrwrd, xSender, ySender);
2880  double dist_fr = dist(x, y, xFrwrd, yFrwrd);
2881  double inout2 = g(dist_r, dist_f, dist_fr, m_angleThreshold);
2882 
2883  if(inout2 >=0)
2884  {
2885  m_CBFPB->erase(packet);
2886  m_CBFPBCounter.erase(m_CBFPBCounter.find(packetCPT));
2888  addr.setInteger(-1);
2889  return addr;
2890  }
2891  else
2892  {
2893  m_CBFPBCounter[packetCPT] += 1;
2894  QMutexLocker locker(m_mutexLocT);
2895  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(packet.soPv().gnAddr().toHex());
2896  int timer_CBF_GBC;
2897  if(pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai())
2898  {
2899  double distance = dist(LocationTableEntry(pos.value()).longPositionVector().latitude(), LocationTableEntry(pos.value()).longPositionVector().longitude(), m_paramGPS->data().referencePosition().latitude, m_paramGPS->data().referencePosition().longitude);
2900  timer_CBF_GBC = TO_CBF_GBC(distance);
2901  }
2902  else
2903  timer_CBF_GBC = itsGnGeoBroadcastCbfMaxTime;
2904  locker.unlock();
2905 
2906  if(m_CBFPB->update(packet, timer_CBF_GBC))
2907  printf("\n\nTIMER PROLONGER\n\n");
2908  else
2909  printf("Echec de l'update du timer du packet dans le cbfpb : PacketNotFound (Ceci n'est pas censé arrivé)\n");
2910 
2912  addr.setInteger(0);
2913  return addr;
2914  }
2915  }
2916  }
2917  else
2918  {
2919  bool iAmDest = true, destIsBC = true;
2920  for(int j = 0; j < ETHER_ADDR_LEN; j++)
2921  if(iAmDest && m_gnAddr->mid(j) != destEth[j])
2922  iAmDest = false;
2923  if(*destEth != 0xff)
2924  destIsBC = false;
2925 
2926 
2927  if(iAmDest || destIsBC)
2928  {
2929  m_CBFPBCounter[packetCPT] = 1;
2930 
2931  addr = greedy(bh, ch, packet, packetPayload, sender, destEth, forwarderEth);
2932 
2933  int timer_CBF_GBC;
2934  timer_CBF_GBC = itsGnGeoBroadcastCbfMaxTime;
2935 
2936  if(m_CBFPB->addPacket(bh, ch, packet, packetPayload, timer_CBF_GBC) > 0)
2937  printf("Succès de l'ajout du packet au cbfpb!\n");
2938  else
2939  printf("Echec de l'ajout du packet au cbfpb : DuplicatePacket (Ceci n'est pas censé arrivé)\n");
2940 
2942  addr.setInteger(0);
2943  return addr;
2944  }
2945  else
2946  {
2947  QMutexLocker locker(m_mutexLocT);
2948  int timer_CBF_GBC;
2949  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(packet.soPv().gnAddr().toHex());
2950  if(pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai())
2951  {
2952  double distance = dist(LocationTableEntry(pos.value()).longPositionVector().latitude(), LocationTableEntry(pos.value()).longPositionVector().longitude(), m_paramGPS->data().referencePosition().latitude, m_paramGPS->data().referencePosition().longitude);
2953  timer_CBF_GBC = TO_CBF_GBC(distance);
2954  }
2955  else
2956  timer_CBF_GBC = itsGnGeoBroadcastCbfMaxTime;
2957  locker.unlock();
2958 
2959  if(m_CBFPB->addPacket(bh, ch, packet, packetPayload, timer_CBF_GBC) > 0)
2960  printf("Succès de l'ajout du packet au cbfpb!\n");
2961  else
2962  printf("Echec de l'ajout du packet au cbfpb : DuplicatePacket (Ceci n'est pas censé arrivé)\n");
2963 
2965  addr.setInteger(0);
2966  return addr;
2967  }
2968  }
2969  }
2970  else
2971  {
2972  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(packet.soPv().gnAddr().toHex());
2973  if(flagSrc || (pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai()))
2974  {
2975  double inout3;
2976 
2977  if(shape == shape_circle)
2978  inout3 = f(xSender, ySender, packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2979  else
2980  inout3 = f(
2981  x*cos(packet.destinationArea().angle())-y*sin(packet.destinationArea().angle()),
2982  x*sin(packet.destinationArea().angle())+y*cos(packet.destinationArea().angle()),
2983  packet.destinationArea().distanceA(), packet.destinationArea().distanceB(), shape);
2984  if(inout3<0)
2985  {
2986  return greedy(bh, ch, packet, packetPayload, sender, destEth, forwarderEth);
2987  }
2988  else
2989  {
2991  addr.setInteger(-1);
2992  return addr;
2993  }
2994  }
2995  else
2996  {
2997  uchar NH[MAC_ADDR_SIZE];
2998  for(int i = 0; i < MAC_ADDR_SIZE; i++)
2999  NH[i] = 0xFF;
3000  addr.setMacAddr(NH);
3002  return addr;
3003  }
3004  }
3005 
3006 }
3007 
3009 {
3010  return CBFUnicast(bh, ch, GUCPacket(packet.sequenceNumber(), packet.soPv(), ShortPositionVector(GnAddr(), 0, packet.destinationArea().geoAreaPosLatitude(), packet.destinationArea().geoAreaPosLongitude())), packetPayload);
3011 }
3012 
3014 {
3016 
3017  QMutexLocker mutexCBFPB(m_CBFPB->m_mutex);
3018  if(m_CBFPB->isDuplicateTimestamp(packet))
3019  {
3020  m_CBFPB->erase(packet);
3021  qDebug() << "CBFUnicast erase";
3023  addr.setInteger(-1);
3024  return addr;
3025  }
3026  else
3027  {
3028  QMutexLocker locker(m_mutexLocT);
3029  QHash<QByteArray, LocationTableEntry>::Iterator pos = m_locationTable->find(packet.soPv().gnAddr().toHex());
3030  locker.unlock();
3031  if(pos != m_locationTable->end() && LocationTableEntry(pos.value()).longPositionVector().pai())
3032  {
3033  double progress = dist(packet.dePv().latitude(), packet.dePv().longitude(), packet.soPv().latitude(), packet.soPv().longitude()) - dist(packet.dePv().latitude(), packet.dePv().longitude(), m_paramGPS->data().referencePosition().latitude, m_paramGPS->data().referencePosition().longitude);
3034  if(progress > 0)
3035  {
3036  int timer_CBF_GUC = TO_CBF_GUC(progress);
3037 
3038  if(m_CBFPB->addPacket(bh, ch, packet, packetPayload, timer_CBF_GUC) > 0)
3039  printf("Succès de l'ajout du packet au cbfpb!\n");
3040  else
3041  printf("Echec de l'ajout du packet au cbfpb : DuplicatePacket (Ceci n'est pas censé arrivé)\n");
3042 
3044  addr.setInteger(0);
3045  return addr;
3046  }
3047  else
3048  {
3050  addr.setInteger(-1);
3051  return addr;
3052  }
3053  }
3054  else
3055  {
3056  int timer_CBF_GUC = itsGnGeoBroadcastCbfMaxTime;
3057  if(m_CBFPB->addPacket(bh, ch, packet, packetPayload, timer_CBF_GUC) > 0)
3058  printf("Succès de l'ajout du packet au cbfpb!\n");
3059  else
3060  printf("Echec de l'ajout du packet au cbfpb : DuplicatePacket (Ceci n'est pas censé arrivé)\n");
3061 
3063  addr.setInteger(0);
3064  return addr;
3065  }
3066  }
3067 }
3068 
3069 /* Packet Buffer Management */
3070 
3071 void GeoNet::flushPB(GnAddr destination)
3072 {
3073  flushLSPB(destination);
3074  flushUCFPB(destination);
3075  checkBCFPB();
3076 }
3077 
3078 void GeoNet::flushLSPB(GnAddr destination)
3079 {
3081  {
3082  if (i.destination() == destination)
3083  {
3084  //bool ltIsOver = false;
3085  /*if(i.packet().maximumPacketLifeTime != 0 && (nowTimestamp - i.timestamp() > i.packet().maximumPacketLifeTime->lifeTime() || ((i.packet().maximumPacketLifeTime->lifeTime() - (m_paramGPS->timestamp() % 0x100000000) - i.timestamp())/50) < 1))
3086  ltIsOver = true;*/
3087 
3088  gnDataRequest_t tmp = i.packet();
3089  quint32 tmpTimestamp = i.timestamp();
3090  m_LSPB->remove(i);
3092 
3093  quint32 nowTimestamp = (m_paramGPS->timestamp() % 0x100000000);
3094 
3095  //i.packet().maximumPacketLifeTime == 0;
3096  //||
3097 
3098  //quint32 LTTEST = i.packet().maximumPacketLifeTime->lifeTime();
3099  if(i.packet().maximumPacketLifeTime == 0 || (nowTimestamp - tmpTimestamp < tmp.maximumPacketLifeTime->lifeTime() && (((tmp.maximumPacketLifeTime->lifeTime() - nowTimestamp - tmpTimestamp)/50) > 0)))
3100  //if(!ltIsOver)
3101  {
3102  tmp.maximumPacketLifeTime->setMultiplierBase(tmp.maximumPacketLifeTime->lifeTime() - nowTimestamp - tmpTimestamp);
3103  emit sendGUC(tmp);
3104  }
3105  }
3106  //else ++i;
3107  }
3108 }
3109 
3111 {
3112  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
3115 
3116  QMutexLocker locker(m_mutex);
3117  for(QVector<UnicastForwardingPacketEntry>::Iterator i = m_UCFPB->ucForwardPacketBuffer().begin();i != m_UCFPB->ucForwardPacketBuffer().end(); ++i)
3118  {
3120  tmp.updateSourcePacketSoPv(lpv);
3121 
3122  quint64 lt;
3123  if(tmp.basicHeader().lifeTime().lifeTime() - tmp.m_elapsed.elapsed() > 0)
3124  {
3125  lt = tmp.basicHeader().lifeTime().lifeTime() - tmp.m_elapsed.elapsed();
3126  if(lt % 50 != 0)
3127  lt = (lt/50)*50;
3128  }
3129  else
3130  {
3131  printf("LifeTime outdated\n");
3132  m_UCFPB->erase(tmp.gucPacket());
3133  return;
3134  }
3135 
3136 
3137  tmp.basicHeader().setLifeTime(LifeTime(lt));
3138 
3139  QByteArray packet;
3140  QDataStream ds(&packet, QIODevice::WriteOnly);
3141  ds.writeRawData(tmp.basicHeader().toHex().data(), tmp.basicHeader().toHex().length());
3142  ds.writeRawData(tmp.commonHeader().toHex().data(), tmp.commonHeader().toHex().length());
3143 
3146  {
3147  default:
3150  res = greedy(tmp.basicHeader(), tmp.commonHeader(), tmp.gucPacket(), tmp.payload(), tmp.sender());
3151  break;
3153  res = CBFUnicast(tmp.basicHeader(), tmp.commonHeader(), tmp.gucPacket(), tmp.payload());
3154  break;
3155  }
3156 
3157  if(res.isInteger() && res.integer() == 0)
3158  {
3159  printf("geonetError_checkUC_guc_bufferedPacket\n");
3160  return;
3161  }
3162  else if(res.isInteger() && res.integer() == -1)
3163  {
3164  printf("geonetError_checkUC_guc_discardedPacket\n");
3165  return;
3166  }
3167 
3168  ds.writeRawData(tmp.gucPacket().toHex().data(), tmp.gucPacket().toHex().length());
3169  ds.writeRawData(tmp.payload().data(), tmp.payload().length());
3170 
3171  if(res.isMacAddr())
3172  for(int k = 0 ; k < 6 ; k++)
3173  m_paramNetwork->eh.ether_dhost[k] = res.macAddr()[k];
3174 
3175  /* Construct the Ethernet header : my mac addr */
3176  for(int k = 0 ; k < 6 ; k++)
3177  m_paramNetwork->eh.ether_shost[k] = m_paramConf->mid(k);
3178 
3179  size_t tx_len = sizeof(struct ether_header);
3180 
3181  memcpy(m_paramNetwork->sendbuf, &m_paramNetwork->eh, sizeof(struct ether_header));
3182  for(int k = 0 ; k < packet.length(); k++)
3183  m_paramNetwork->sendbuf[tx_len++] = packet.data()[k];
3184 
3185  /* Send packet */
3186  if(sendto(m_paramNetwork->rawSocket, m_paramNetwork->sendbuf, tx_len, 0, (struct sockaddr*)(&(m_paramNetwork->socket_address)), sizeof(struct sockaddr_ll)) < 0)
3187  qDebug() << "sendto" << strerror(errno);
3188  else
3189  m_UCFPB->erase(tmp.gucPacket());
3190  }
3191 }
3192 void GeoNet::flushUCFPB(GnAddr destination)
3193 {
3194  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
3197 
3198  QMutexLocker locker(m_mutex);
3199  for(int priority = 0; priority < 4; priority++)
3200  {
3201  for(QVector<UnicastForwardingPacketEntry>::Iterator i = m_UCFPB->ucForwardPacketBuffer().begin();i != m_UCFPB->ucForwardPacketBuffer().end(); ++i)
3202  {
3203  if(i->gucPacket().dePv().gnAddr() == destination && i->commonHeader().trafficClass().TCID() == priority)
3204  {
3205  i->updateSourcePacketSoPv(lpv);
3206  quint64 lt = i->basicHeader().lifeTime().lifeTime() - i->m_elapsed.elapsed();
3207  if(lt % 50 != 0)
3208  lt = (lt/50)*50;
3209  i->basicHeader().setLifeTime(LifeTime(lt));
3210 
3211  QByteArray packet;
3212  QDataStream ds(&packet, QIODevice::WriteOnly);
3213  ds.writeRawData(i->basicHeader().toHex().data(), i->basicHeader().toHex().length());
3214  ds.writeRawData(i->commonHeader().toHex().data(), i->commonHeader().toHex().length());
3215 
3218  {
3219  default:
3222  res = greedy(i->basicHeader(), i->commonHeader(), i->gucPacket(), i->payload(), i->sender());
3223  break;
3225  res = CBFUnicast(i->basicHeader(), i->commonHeader(), i->gucPacket(), i->payload());
3226  break;
3227  }
3228 
3229  if(res.isInteger() && res.integer() == 0)
3230  {
3231  printf("geonetError_flushUC_guc_bufferedPacket\n");
3232  return;
3233  }
3234  else if(res.isInteger() && res.integer() == -1)
3235  {
3236  printf("geonetError_flushUC_guc_discardedPacket\n");
3237  return;
3238  }
3239 
3240  ds.writeRawData(i->gucPacket().toHex().data(), i->gucPacket().toHex().length());
3241  ds.writeRawData(i->payload().data(), i->payload().length());
3242 
3243  if(res.isMacAddr())
3244  for(int k = 0 ; k < 6 ; k++)
3245  m_paramNetwork->eh.ether_dhost[k] = res.macAddr()[k];
3246 
3247  /* Construct the Ethernet header : my mac addr */
3248  for(int k = 0 ; k < 6 ; k++)
3249  m_paramNetwork->eh.ether_shost[k] = m_paramConf->mid(k);
3250 
3251  size_t tx_len = sizeof(struct ether_header);
3252 
3253  memcpy(m_paramNetwork->sendbuf, &m_paramNetwork->eh, sizeof(struct ether_header));
3254  for(int k = 0 ; k < packet.length(); k++)
3255  m_paramNetwork->sendbuf[tx_len++] = packet.data()[k];
3256 
3257  /* Send packet */
3258  if(sendto(m_paramNetwork->rawSocket, m_paramNetwork->sendbuf, tx_len, 0, (struct sockaddr*)(&(m_paramNetwork->socket_address)), sizeof(struct sockaddr_ll)) < 0)
3259  qDebug() << "sendto" << strerror(errno);
3260  else
3261  m_UCFPB->erase(i->gucPacket());
3262  }
3263  }
3264  }
3265 }
3266 
3268 {
3269  printf("\n\nCHECK BCFPB\n\n");
3270  quint32 tmpstmp = m_paramGPS->timestamp() % 0x100000000;
3273 
3274  int j = 0;
3275  for(QVector<BroadcastForwardingPacketEntry>::Iterator i = m_BCFPB->bcForwardPacketBuffer().begin(); j < m_BCFPB->bcForwardPacketBuffer().size(); ++i, j++)
3276  {
3277  QMutexLocker locker(m_mutex);
3278  i->updateSourcePacketSoPv(lpv);
3279  if(i->basicHeader().lifeTime().lifeTime() - i->m_elapsed.elapsed() < 0)
3280  {
3281  m_BCFPB->bcForwardPacketBuffer().erase(i);
3282  continue;
3283  }
3284 /* printf("\n"
3285  " ------------------------------\n"
3286  "| |\n"
3287  "| |\n"
3288  "| |\n"
3289  "| BCFPB LT IS OVER |\n"
3290  "| |\n"
3291  "| |\n"
3292  "| |\n"
3293  " ------------------------------\n");
3294  else
3295  printf("\n"
3296  " ------------------------------\n"
3297  "| |\n"
3298  "| |\n"
3299  "| |\n"
3300  "| %lld |\n"
3301  "| |\n"
3302  "| |\n"
3303  "| |\n"
3304  " ------------------------------\n", i->basicHeader().lifeTime().lifeTime() - i->m_elapsed.elapsed());
3305 */
3306  quint64 lt = i->basicHeader().lifeTime().lifeTime() - i->m_elapsed.elapsed();
3307  if(lt % 50 != 0)
3308  lt = (lt/50)*50;
3309  i->basicHeader().setLifeTime(LifeTime(lt));
3310 
3311  switch(i->commonHeader().headerType())
3312  {
3315  {
3316  printf("\n\nCHECK BCFPB\n\n");
3317  DestinationArea da;
3318  GBCPacket geoPacket;
3319  if(i->commonHeader().headerType() == CommonHeader::headerType_geoAnycast)
3320  geoPacket = i->gacPacket();
3321  else if(i->commonHeader().headerType() == CommonHeader::headerType_geoBroadcast)
3322  geoPacket = i->gbcPacket();
3323  da = geoPacket.destinationArea();
3324 
3326  e_shape shape;
3327  switch(i->commonHeader().headerSubType())
3328  {
3330  shape = shape_circle;
3331  if((da.distanceA() * da.distanceA() * M_PI) > ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
3332  {
3333  printf("GBC Sending Error : Area size is too large.\n");
3334  return;
3335  }
3336  break;
3338  shape = shape_rectangle;
3339  if((da.distanceA() * da.distanceB()) > ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
3340  {
3341  printf("GBC Sending Error : Area size is too large.\n");
3342  return;
3343  }
3344  break;
3346  shape = shape_ellipse;
3347  if((da.distanceA() * da.distanceB() * M_PI) > ((itsGnMaxGeoAreaSize * 1000)*(itsGnMaxGeoAreaSize * 1000)))
3348  {
3349  printf("GBC Sending Error : Area size is too large.\n");
3350  return;
3351  }
3352  break;
3353  default :
3354  shape = shape_none;
3355  break;
3356  }
3357 
3358  bool iamsender = (i->sender() == QByteArray());
3360  {
3361  default:
3364  if(iamsender)
3365  res = simpleForwardingAlgorithm(i->basicHeader(), i->commonHeader(), geoPacket, i->payload(), shape, QByteArray(), true);
3366  else
3367  res = simpleForwardingAlgorithm(i->basicHeader(), i->commonHeader(), geoPacket, i->payload(), shape, i->sender(), false);
3368  break;
3370  if(iamsender)
3371  res = contentionBasedForwardingAlgorithm(i->basicHeader(), i->commonHeader(), geoPacket, i->payload(), shape, QByteArray(), true);
3372  else
3373  res = contentionBasedForwardingAlgorithm(i->basicHeader(), i->commonHeader(), geoPacket, i->payload(), shape, i->sender(), true);
3374  break;
3376  if(iamsender)
3377  res = advancedForwarding(i->basicHeader(), i->commonHeader(), geoPacket, i->payload(), shape, QByteArray(), i->destination(), i->forwarder(), true);
3378  else
3379  res = advancedForwarding(i->basicHeader(), i->commonHeader(), geoPacket, i->payload(), shape, i->sender(), i->destination(), i->forwarder(), true);
3380  break;
3381  }
3382 
3383  if(res.isInteger() && res.integer() == 0)
3384  {
3385  printf("checkBCFPB packet buffered GBC/GAC\n");
3386  return;
3387  }
3388  //m_BCFPB->addPacket(i->basicHeader(), i->commonHeader(), (i->commonHeader().headerType() == CommonHeader::headerType_geoAnycast)?i->gacPacket():i->gbcPacket(), i->payload(), i->sender());
3389 
3390  if(res.isMacAddr())
3391  for(int k = 0 ; k < 6 ; k++)
3392  m_paramNetwork->eh.ether_dhost[k] = res.macAddr()[k];
3393 
3394  /* Construct the packet */
3395  QByteArray packet;
3396  QDataStream ds(&packet, QIODevice::WriteOnly);
3397 
3398  ds.writeRawData(i->commonHeader().toHex().data(), i->commonHeader().toHex().length());
3399  ds.writeRawData(geoPacket.toHex().data(), geoPacket.toHex().length());
3400  ds.writeRawData(i->payload().data(), i->payload().length());
3401 
3402  send(packet, true, i->basicHeader());
3403  }
3404 
3405  break;
3406 
3408  {
3409  bool noNeighbour = true;
3410  if(!m_locationTable->isEmpty())
3411  for(QHash<QByteArray, LocationTableEntry>::Iterator i = m_locationTable->begin();i != m_locationTable->end(); ++i)
3412  if(LocationTableEntry(i.value()).isNeighbour() != 0 && *LocationTableEntry(i.value()).isNeighbour())
3413  noNeighbour = false;
3414 
3415  if(noNeighbour && i->commonHeader().trafficClass().SCF() == 1)
3416  {
3417  locker.unlock();
3418  if(i->commonHeader().headerSubType()==CommonHeader::headerSubTypeTopologicallyScopedBroadcast_singleHop)
3419  m_BCFPB->addPacket(i->basicHeader(), i->commonHeader(), i->shbPacket(), i->payload(), i->sender());
3420  else
3421  m_BCFPB->addPacket(i->basicHeader(), i->commonHeader(), i->tsbPacket(), i->payload(), i->sender());
3422  locker.relock();
3423  }
3424  else
3425  {
3426  /* Construct the packet */
3427  QByteArray shbData;
3428  QDataStream ds(&shbData, QIODevice::WriteOnly);
3429 
3430  ds.writeRawData(i->commonHeader().toHex().data(), i->commonHeader().toHex().length());
3431  if(i->commonHeader().headerSubType()==CommonHeader::headerSubTypeTopologicallyScopedBroadcast_singleHop)
3432  ds.writeRawData(i->shbPacket().toHex().data(), i->shbPacket().toHex().length());
3433  else
3434  ds.writeRawData(i->tsbPacket().toHex().data(), i->tsbPacket().toHex().length());
3435  ds.writeRawData(i->payload().data(), i->payload().length());
3436 
3437  /* Construct the Ethernet header : destination addr */
3438  for(int i = 0 ; i < 6 ; i++)
3439  m_paramNetwork->eh.ether_dhost[i] = 0xFF;
3440 
3441  /*int ret = */
3442  /*if(*/send(shbData, true, i->basicHeader());/* == networkError_success)
3443  i = m_BCFPB->bcForwardPacketBuffer().erase(i);*/
3444  /*
3445  if(ret == networkError_success)
3446  {
3447  if(i->commonHeader().headerSubType()==CommonHeader::headerSubTypeTopologicallyScopedBroadcast_singleHop)
3448  m_HPB->addPacket(i->basicHeader(), i->commonHeader(), i->shbPacket(), i->payload());
3449  else
3450  m_HPB->addPacket(i->basicHeader(), i->commonHeader(), i->tsbPacket(), i->payload())
3451  }*/
3452  }
3453  break;
3454  }
3455  }
3456  }
3457  //m_BCFPB->clear();
3458 }
3459 
3460 /* Security layer */
3461 QByteArray GeoNet::getSecuredPayload(QByteArray payload, BasicHeader bh)
3462 {
3463  /*QNetworkDatagram response;
3464 
3465  //qDebug() << "sending" << payload.toHex() << "to security server";
3466  m_signatureSocket->writeDatagram(payload, QHostAddress(m_paramSecu->serverAddress()), m_paramSecu->signaturePort());
3467 
3468  if(m_signatureSocket->waitForReadyRead(m_paramSecu->timeout() * 1000))
3469  {
3470  response = m_signatureSocket->receiveDatagram();
3471  //qDebug() << response.data().toHex() << "received from security server";
3472  return bh.toHex() + response.data();
3473  }
3474 
3475  qDebug() << "no response from security server";
3476  bh.setNextHeader(BasicHeader::nextHeader_commonHeader);
3477  return bh.toHex() + payload;*/
3478 #include <SecurityProfile_CAM.h>
3479 #include <decodeSecurity.h>
3480 
3481  Certificate_t certif;
3482  decode_Certificate((unsigned char*)m_securityManager.certificate().data(), &certif);
3483  QByteArray signedPayload = m_securityManager.signPayload(payload, SecurityProfile::securityProfile_cam);
3484 
3485  return bh.toHex() + signedPayload;
3486 }
3487 
3488 QByteArray GeoNet::verifySecuredPayload(QByteArray payload, bool *success)
3489 {
3490  QNetworkDatagram response;
3491 
3492  m_verifySocket->writeDatagram(payload, QHostAddress(m_paramSecu->serverAddress()), m_paramSecu->verifyPort());
3493 
3494  if(m_verifySocket->waitForReadyRead(m_paramSecu->timeout()))
3495  {
3496  response = m_verifySocket->receiveDatagram();
3497  qDebug() << response.data().toHex() << "received from verify";
3498  *success = (bool)response.data()[0];
3499  qDebug() << (int)response.data()[0] << *success;
3500  if(*success)
3501  {
3502  qDebug() << response.data().right(response.data().length() - 1).toHex();
3503  return response.data().right(response.data().length() - 1);
3504  }
3505  }
3506  *success = false;
3507 
3508  return payload;
3509 }
#define itsGnLifetimeLocTE
Definition: constantes.h:97
void setPresent(ForwardingAlgorithmResponse_PR p)
setPresent Set the type of response
int sendGUC(gnDataRequest_t gnDR)
sendGUC Send a GUC.
Definition: Geonet.cpp:929
ForwardingAlgorithmResponse simpleForwardingAlgorithm(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray packetPayload, e_shape shape, QByteArray sender, bool flagSrc)
simpleForwardingAlgorithm Simple algorithm.
Definition: Geonet.cpp:2627
FakeGPSProvider * m_paramGPS
Definition: Geonet.h:610
quint32 timestamp() const
timestamp Entry timestamp getter.
void checkBCFPB()
checkBCFPB Check to flush the broadcast forawarding packet buffer.
Definition: Geonet.cpp:3267
gnDataRequest_t structure is the GN data request definition.
Definition: GNDataRequest.h:46
void addPacket(BasicHeader bh, CommonHeader ch, GUCPacket packet, QByteArray payload, QByteArray sender)
addPacket Add packet to the buffer.
void flushLSPB(GnAddr destination)
flushLSPB Flush LS packet buffer.
Definition: Geonet.cpp:3078
int sendGBC(gnDataRequest_t gnDR)
sendGBC Send an LSRequest.
Definition: Geonet.cpp:815
LifeTime * maximumPacketLifeTime
Definition: GNDataRequest.h:55
quint16 * repetitionInterval
Definition: GNDataRequest.h:56
CBFPacketBuffer * m_CBFPB
Definition: Geonet.h:623
GeoNet()
GeoNet GeoNet constructor.
Definition: Geonet.cpp:6
u_char ether_dhost[ETHER_ADDR_LEN]
quint16 timeout() const
timeout Timeout to wait for server response getter.
void sendPacketFromRPB(gnDataRequest_t rawPacket)
sendPacketFromRPB Slot used to send packet from repetition packet buffer.
Definition: Geonet.cpp:491
ForwardingAlgorithmResponse contentionBasedForwardingAlgorithm(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray packetPayload, e_shape shape, QByteArray sender, bool flagSrc)
contentionBasedForwardingAlgorithm CBF algorithm.
Definition: Geonet.cpp:2705
void verifLocT()
verifLocT Checks if the LocT has expired packet.
Definition: Geonet.cpp:212
ReceivePackets * m_RP
Definition: Geonet.h:629
int upperTesterEventIndicationListenPort() const
upperTesterEventIndicationListenPort Port used to listen for event indication getter.
int gbcPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
gbcPacketProcessing GBC Header decoding.
Definition: Geonet.cpp:1934
LSReplyPacket lsReplyPacket()
beaconPacket LSReply entry creation.
struct ether_header eh
Security layer decoder.
bool itsGnGeoAreaLineForwarding() const
itsGnGeoAreaLineForwarding Is the GN geo area line forwarding used?
GnAddr * m_gnAddr
Definition: Geonet.h:617
QByteArray toHex()
toHex Create a QByteArray containing the hexadecimal format of the CommonHeader.
void addPacket(gnDataRequest_t gnDR)
addPacket Add packet to the buffer.
quint64 extract(uchar *packet)
extract Extract GBC from packet.
Definition: TSBPacket.cpp:26
int integer() const
integer Integer getter.
int lsRetransmit(GnAddr destination, QByteArray lsRequestData, BasicHeader bh)
lsRetransmit Retransmission of an LSRequest.
Definition: Geonet.cpp:459
int TO_CBF_GUC(int distance)
TO_CBF_GUC TO_CBF_GUC computation.
Definition: Geonet.cpp:2504
sniff_destinationArea_t destinationArea
Definition: GNDataRequest.h:39
The sniff_basicHeader_t is a C version of the class.
Definition: BasicHeader.h:25
#define itsGnBeaconServiceRetransmitTimer
Definition: constantes.h:108
BasicHeader basicHeader() const
basicHeader Entry Basic Header getter.
quint16 * maximumRepetitionTime
Definition: GNDataRequest.h:57
void clear()
clear Clear the buffer.
Security profile for CAM purpose.
void clear()
clear Clear the buffer.
SpeedValue_t speedValue
Definition: Speed.h:24
TrafficClass trafficClass() const
trafficClass ITS-S traffic class getter.
void clear()
clear Clear the buffer.
~GeoNet()
~GeoNet GeoNet destructor.
Definition: Geonet.cpp:175
bool isDuplicateTimestamp(BeaconPacket packet)
isDuplicateTimestamp Check is the packet is duplicate.
void treatPackets(QByteArray)
treatPackets Slot used to treat received packets.
Definition: Geonet.cpp:1248
QSslSocket * m_sslSocket
Definition: Geonet.h:577
quint64 extract(uchar *packet)
extract Extract GUCPacket from packet.
Definition: GUCPacket.cpp:29
CommonHeader commonHeader() const
basicHeader Entry Common Header getter.
int m_angleThreshold
Definition: Geonet.h:681
ParameterSecurity * m_paramSecu
Definition: Geonet.h:616
double dist(qint32 x1, qint32 y1, qint32 x2, qint32 y2)
dist This function calculate the euclidian distance between two points ([x1, y1], [x2...
Definition: api.cpp:228
char sendbuf[BUF_SIZ]
quint64 extract(uchar *packet)
extract Extract GBC from packet.
Definition: GBCPacket.cpp:43
int commonHeaderProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh)
commonHeaderProcessing Common Header decoding.
Definition: Geonet.cpp:1307
LongPositionVector soPv() const
soPv TSB SO PV getter.
Definition: TSBPacket.h:73
void sslErrors(const QList< QSslError > &errors)
sslErrors Print SSL errors.
Definition: Geonet.cpp:129
QMutex * m_mutexLocT
Definition: Geonet.h:634
LongPositionVector soPv() const
soPv SO PV getter.
Definition: GBCPacket.h:95
BasicHeader basicHeader() const
basicHeader Entry Basic Header getter.
QByteArray getSecuredPayload(QByteArray payload, BasicHeader bh)
getSecuredPayload Send payload to secure to the HSM.jar.
Definition: Geonet.cpp:3461
QVector< BroadcastForwardingPacketEntry > bcForwardPacketBuffer() const
bcForwardPacketBuffer Buffer getter.
void sendBeacon()
sendBeacon Slot used to send Beacon.
Definition: Geonet.cpp:569
GUCPacket gucPacket()
beaconPacket GUC entry creation.
quint32 timestamp() const
timestamp Timestamp field getter
#define itsGnBeaconServiceMaxJitter
Definition: constantes.h:109
QHash< QByteArray, LocationTableEntry > * m_locationTable
Definition: Geonet.h:627
void changeMID()
changeMID Slot used when need to change the MID.
Definition: Manager.cpp:57
ParameterUpperTester * m_paramUpperTester
Definition: Geonet.h:614
QHash< QString, void * > hashMap
Definition: Manager.h:53
void setRHL(quint8 r)
setRHL Router hop limit field setter.
Definition: BasicHeader.h:137
quint64 timestamp()
timestamp Timestamp getter.
QUdpSocket * m_signatureSocket
Definition: Geonet.h:706
quint8 nextHeader() const
nextHeader Next header field getter.
Definition: BasicHeader.h:100
QElapsedTimer m_elapsed
uchar * mid()
mid MID getter.
quint16 sequenceNumber() const
sequenceNumber Sequence number getter.
Definition: GBCPacket.h:90
bool m_hybridFlag
Definition: Geonet.h:580
u_char ether_shost[ETHER_ADDR_LEN]
quint16 payloadLength() const
payloadLength Payload length field getter.
Definition: CommonHeader.h:218
N&T layer.
QByteArray payload() const
payload Entry payload getter.
#define itsGnLocalAddrConfMethod_auto
Definition: constantes.h:67
Manager * m_manager
Definition: Geonet.h:618
quint16 distanceB() const
geoAreaPosLatitude Distance B field getter.
QHash< int, QString > geonetErrors
Definition: Geonet.h:344
int sendLsRequest(GnAddr dest, gnDataRequest_t gnDR)
sendLsRequest Send an LSRequest.
Definition: Geonet.cpp:610
void flushPB(GnAddr destination)
flushPB Flush buffer.
Definition: Geonet.cpp:3071
ForwardingAlgorithmResponse advancedForwarding(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray packetPayload, e_shape shape, QByteArray sender, uchar *destEth, uchar *forwarderEth, bool flagSrc)
advancedForwarding Advanced algorithm.
Definition: Geonet.cpp:2805
quint8 version() const
version Version field getter.
Definition: BasicHeader.h:95
The sniff_ethernet_t structure is a C style of the Ethernet header.
GnAddr destination() const
destination LS destination getter.
static quint16 sequenceNumber
Definition: Geonet.h:237
QUdpSocket * gen2ut
Definition: Geonet.h:575
GnAddr gnAddr() const
gnAddr GnAddr field getter
void socketReadyRead()
socketReadyRead Slot used when socket is ready to read.
Definition: Geonet.cpp:147
#define itsGnPaiInterval
Definition: constantes.h:94
#define itsGnLocationServiceRetransmitTimer
Definition: constantes.h:106
char ifName[IFNAMSIZ]
bool isDuplicatePacketDetectionTimestampSN(quint32 lastReceivedTimestamp, quint32 nowReceivedTimestamp, quint16 lastReceivedSN, quint16 nowReceivedSN)
isDuplicatePacketDetectionTimestamp Calculate if the packet is a duplicate using the timestamp and th...
Definition: apiNetwork.cpp:104
GBCPacket gbcPacket()
beaconPacket GBC entry creation.
#define itsGnBroadcastCBFDefSectorAngle
Definition: constantes.h:122
GACPacket gacPacket()
beaconPacket GAC entry creation.
#define MAC_ADDR_SIZE
HeadingValue_t headingValue
Definition: Heading.h:24
QMutex * m_mutex
Definition: Geonet.h:633
#define itsGnSecurity_enabled
Definition: constantes.h:100
SemiAxisLength_t semiMajorConfidence
int upperTesterEventIndicationWritePort() const
upperTesterEventIndicationWritePort Port used to write for event indication getter.
ParameterConfiguration * m_paramConf
Definition: Geonet.h:613
QByteArray payload() const
basicHeader Entry payload getter.
ParameterHybrid * m_paramHybrid
Definition: Geonet.h:579
QByteArray sender() const
sender Entry sender getter.
#define LAT2METER
Definition: api.h:23
FakeCANProvider * m_paramCAN
Definition: Geonet.h:611
QTimer * m_beacon
Definition: Geonet.h:572
QList< LocationServicePacketEntry > lsPacketBuffer()
lsPacketBuffer The LS buffer getter.
quint32 lifeTime() const
lifeTime Life time value computation.
Definition: LifeTime.h:120
bool m_geonetFlag
Definition: Geonet.h:581
PosConfidenceEllipse_t positionConfidenceEllipse
ParameterGPS & data()
data GPS data getter.
CommonHeader::e_upperProtocolEntity upperProtocolEntity
Definition: GNDataRequest.h:48
struct ifreq if_idx
QHash< QByteArray, int > m_lsRetransmission
Definition: Geonet.h:631
int basicHeaderProcessing(uchar *packet, int len, sniff_ethernet_t *eth)
basicHeaderProcessing Basic Header decoding.
Definition: Geonet.cpp:1263
HistoryPacketBuffer * m_HPB
Definition: Geonet.h:620
void packetReceived(QByteArray)
packetReceived Signal used when a packet is received.
QByteArray certificate() const
certificate Certificate getter.
int shbPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
shbPacketProcessing SHB Header decoding.
Definition: Geonet.cpp:2302
int maxRepetitionAdvancedContention() const
maxRepetitionAdvancedContention Counter used for the Advance GBC forwarding algorithm getter...
uchar * destination()
destination Entry destination getter.
quint8 * mid()
mid MID field getter.
Definition: GnAddr.h:121
void clear()
clear Clear the buffer.
void initHybridSocket()
initHybridSocket Set the hybridization sockets.
Definition: Geonet.cpp:103
QHash< QByteArray, int > m_CBFPBCounter
Definition: Geonet.h:636
quint16 MStationTypeCountryCode() const
MStationTypeCountryCode Manually StationType CountryCode field getter.
Definition: GnAddr.h:116
quint64 extract(uchar *packet)
extract Extract GBC from packet.
Definition: SHBPacket.cpp:25
int tsbPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
tsbPacketProcessing TSB Header decoding.
Definition: Geonet.cpp:2364
quint16 serverPort() const
serverPort ITS-C port getter.
QUdpSocket * gen2fac
Definition: Geonet.h:574
void addPacket(BasicHeader bh, CommonHeader ch, GACPacket packet, QByteArray payload, QByteArray sender, uchar *destination, uchar *forwarder)
addPacket Add packet to the buffer.
#define itsGnGeoBroadcastCbfMaxTime
Definition: constantes.h:120
QTcpSocket * m_tcpSocket
Definition: Geonet.h:578
quint16 internalBTPPort(quint16 port, quint16 portInfo)
internalBTPPort Get the internal BTP port.
int gucPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
gucPacketProcessing GUC Header decoding.
Definition: Geonet.cpp:2128
quint64 extract(uchar *packet)
extract Extract GBC from packet.
LocationServicePacketBuffer * m_LSPB
Definition: Geonet.h:625
bool update(GBCPacket packet, int timeToBuffer)
update Update the packet of the buffer.
#define itsGnLocationServiceMaxRetrans
Definition: constantes.h:105
destinationAddress_t destinationAddress
Definition: GNDataRequest.h:53
bool erase(GBCPacket packet)
erase Remove packet from the buffer.
quint8 headerType() const
headerType Header field getter.
Definition: CommonHeader.h:193
qint32 geoAreaPosLatitude() const
geoAreaPosLatitude Geo area position latitude field getter.
int g(double dist_r, double dist_f, double dist_fr, int angleThreshold)
g Compute the g function.
Definition: api.cpp:272
qint32 latitude() const
latitude Latitude field getter
quint8 packetTransportSubType
Definition: GNDataRequest.h:50
#define itsGnDefaultHopLimit
Definition: constantes.h:110
QByteArray toHex()
toHex Byte array translator
Definition: BasicHeader.cpp:43
QString serverAddress() const
serverAddress IP address of the security server getter.
int beaconPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
beaconPacketProcessing Beacon Header decoding.
Definition: Geonet.cpp:1369
quint8 * maximumHopLimit
Definition: GNDataRequest.h:58
quint8 rhl() const
rhl Router hop limit field getter.
Definition: BasicHeader.h:110
e_shape
The e_shape enum defines the shapes type.
Definition: constantes.h:54
ForwardingAlgorithmResponse greedy(BasicHeader bh, CommonHeader ch, GACPacket packet, QByteArray payload, QByteArray sender, uchar *destination, uchar *forwarder)
greedy Greedy algorithm.
Definition: Geonet.cpp:2512
int sendTSB(gnDataRequest_t gnDR)
sendTSB Send an LSRequest.
Definition: Geonet.cpp:1090
bool erase(GUCPacket packet)
erase Remove packet from the buffer.
#define itsGnMaxGeoAreaSize
Definition: constantes.h:115
qint32 latitude() const
latitude Latitude field getter
void sendEventIndicationToUT(uchar *packetFull, int packetLen)
sendEventIndicationToUT Send decoding success notification to the UpperTester.
Definition: Geonet.cpp:442
void setInteger(int n)
setInteger Integer setter.
quint16 angle() const
geoAreaPosLatitude Angle field getter.
QUdpSocket * m_verifySocket
Definition: Geonet.h:707
void setupUnsecureSocket()
setupUnsecureSocket Set TCP socket for hybridization.
Definition: Geonet.cpp:159
ShortPositionVector dePv() const
dePv GUC DE PV getter.
Definition: GUCPacket.h:82
int TO_CBF_GBC(int distance)
TO_CBF_GBC TO_CBF_GBC computation.
Definition: Geonet.cpp:2496
bool isMacAddr()
isMacAddr Look if the ForwardingAlgorithmResponse is a MAC address.
uchar * forwarder()
forwarder Entry forwarder getter.
Speed_t speed() const
speed ITS-S speed getter.
Definition: ParameterGPS.h:86
bool sslUsed() const
sslUsed Is SSL used?
quint8 SCF() const
SCF Store-carry-forward field getter.
Definition: TrafficClass.h:78
quint64 circAtLat(long l)
circAtLat Compute the circumference at a latitude.
Definition: api.cpp:211
sniff_gnAddr_t gnAddr
Definition: GNDataRequest.h:38
quint16 destinationPort
Definition: GNDataRequest.h:51
LifeTime lifeTime() const
lifeTime Life time field getter.
Definition: BasicHeader.h:105
ReferencePosition_t referencePosition() const
referencePosition ITS-S position getter.
Definition: ParameterGPS.h:81
TrafficClass trafficClass() const
trafficClass Traffic class field getter.
Definition: CommonHeader.h:203
quint64 extract(uchar *packet)
extract Extract Beacon from packet.
int sendSHB(gnDataRequest_t gnDR)
sendSHB Send an LSRequest.
Definition: Geonet.cpp:1026
QByteArray toHex()
toHex Create a QByteArray containing the hexadecimal format of the GnAddr.
Definition: GnAddr.cpp:69
LongPositionVector longPositionVector() const
longPositionVector LPV getter.
quint16 sequenceNumber() const
sequenceNumber TSB sequence number getter.
Definition: TSBPacket.h:68
quint64 get_UINT16(u_char *field, quint16 *uint16)
get_INT64 Extract an integer encoded in 64 bits from a byte array.
Definition: apiNetwork.cpp:69
int sendGAC(gnDataRequest_t gnDR)
sendGAC Send an LSRequest.
Definition: Geonet.cpp:665
QByteArray signPayload(QByteArray payload, SecurityProfile::e_securityProfileType profile)
signPayload Sign a payload.
quint8 headerSubType() const
headerSubType Header sub-type field getter.
Definition: CommonHeader.h:198
uchar * macAddr()
macAddr MAC address getter.
BroadcastForwardingPacketBuffer * m_BCFPB
Definition: Geonet.h:621
bool itsGnLocalAddrConfMethod() const
itsGnLocalAddrConfMethod ITS GN-ADDR configuration method getter.
qint32 geoAreaPosLongitude() const
geoAreaPosLatitude Geo area position longitude field getter.
double f(long x, long y, double lengthA, double lengthB, e_shape shape)
f Compute the f function.
Definition: api.cpp:236
int addPacket(BasicHeader bh, CommonHeader ch, GBCPacket packet, QByteArray payload, int timeToBuffer)
addPacket Add packet to the buffer.
quint8 itsGnGeoUnicastForwardingAlgorithm() const
itsGnGeoUnicastForwardingAlgorithm GUC forwarding algorithm getter.
quint8 itsGnGeoBroadcastForwardingAlgorithm() const
itsGnGeoBroadcastForwardingAlgorithm GBC forwarding algorithm getter.
quint64 decode_Certificate(uchar *field, Certificate_t *certificate)
decode_Certificate Decode a Certificate.
void initGeoNetErrors()
initGeoNetErrors Set the debugging print.
Definition: Geonet.cpp:265
void remove(LocationServicePacketEntry lspe)
find Remove packet in the buffer.
DestinationArea destinationArea() const
destinationArea Destination area getter.
Definition: GBCPacket.h:100
void flushUCFPB(GnAddr destination)
flushUCFPB Flush unicast forawarding packet buffer.
Definition: Geonet.cpp:3192
#define itsGnProtocolVersion
Definition: constantes.h:71
ParameterApplication & data()
data Returns the vehicle status.
CommonHeader::e_headerType packetTransportType
Definition: GNDataRequest.h:49
bool isDuplicateTimestamp(GBCPacket packet)
isDuplicateTimestamp Check is the packet is duplicate.
QString serverAddress() const
serverAddress ITS-C IP address getter.
e_destinationAddress_PR_present present
Definition: GNDataRequest.h:32
bool itsGnIsMobile() const
itsGnIsMobile Is the ITS-S mobile?.
UnicastForwardingPacketBuffer * m_UCFPB
Definition: Geonet.h:622
bool isInteger()
isInteger Look if the ForwardingAlgorithmResponse is an integer.
ParameterNetwork * m_paramNetwork
Definition: Geonet.h:615
quint8 stationType() const
stationType Station Type field getter (5 bits).
Definition: GnAddr.h:106
void erase(GnAddr destination)
find Remove packet in the buffer.
int gacPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
gacPacketProcessing GAC Header decoding.
Definition: Geonet.cpp:1755
void socketError(QAbstractSocket::SocketError)
socketError Print TCP socket error.
Definition: Geonet.cpp:142
Definition: GnAddr.h:28
#define itsGnDefaultMaxCommunicationRange
Definition: constantes.h:121
The sniff_commonHeader_t is a C version of the class.
Definition: CommonHeader.h:29
void execute()
execute Slot used to execute the Beacon creation and sending.
Definition: Geonet.cpp:180
CommonHeader commonHeader() const
commonHeader Entry Common Header getter.
quint16 destinationPortInfo
Definition: GNDataRequest.h:52
FakeApplicationProvider * m_paramAppli
Definition: Geonet.h:612
void utInitialize()
utInitialize Slot used to initialize the N&T layer.
Definition: Geonet.cpp:241
void setMultiplierBase(quint8 m)
setMultiplierBase Multiplier + base setter.
Definition: LifeTime.h:155
int LSReplyPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
LSReplyProcessing LSReply Header decoding.
Definition: Geonet.cpp:1607
int InitRawSocket_send()
InitRawSocket_send Set the ITS-G5 socket.
Definition: Geonet.cpp:387
int send(gnDataRequest_t gnDR)
send Creates the BTP and then select the correct packet send option.
Definition: Geonet.cpp:497
GnAddr destination()
destination Request GN_ADDR getter.
QByteArray toHex()
toHex Create a QByteArray containing the hexadecimal format of the GBCPacket.
Definition: GUCPacket.cpp:41
quint8 maximumHopLimit() const
maximumHopLimit Maximum hop limit field getter.
Definition: CommonHeader.h:223
union destinationAddress_s::@2 choice
The choice union is composed by a GN_ADDR or a destination area.
ForwardingAlgorithmResponse CBFUnicast(BasicHeader bh, CommonHeader ch, GACPacket packet, QByteArray packetPayload)
CBFUnicast Unicast CBF algoritm.
Definition: Geonet.cpp:3008
#define itsGnGeoBroadcastCbfMinTime
Definition: constantes.h:119
#define SIZE_ETHERNET
struct sockaddr_ll socket_address
void setLifeTime(LifeTime l)
setLifeTime Life time field setter.
Definition: BasicHeader.h:132
void setupSecureSocket()
setupSecureSocket Set SSL socket for hybridization.
Definition: Geonet.cpp:116
bool itsGnSecurity() const
itsGnSecurity Is the security layer used?
bool isDuplicatePacketDetectionTimestamp(quint32 lastReceivedTimestamp, quint32 nowReceivedTimestamp)
isDuplicatePacketDetectionTimestamp Calculate if the packet is a duplicate using the timestamp of the...
Definition: apiNetwork.cpp:95
RepetitionPacketBuffer * m_RPB
Definition: Geonet.h:624
Heading_t heading() const
heading ITS-S heading getter.
Definition: ParameterGPS.h:71
gnDataRequest_t packet() const
packet Entry GN Data Request getter.
quint16 sequenceNumber() const
sequenceNumber GUC sequence number getter.
Definition: GUCPacket.h:72
QByteArray payload
Definition: GNDataRequest.h:60
e_gnCommunicationProfile gnCommunicationProfile
Definition: GNDataRequest.h:54
qint32 longitude() const
longitude Longitude field getter
quint16 verifyPort() const
verifyPort Port of the verification service getter.
#define BUF_SIZ
unsigned int diameter(long begin, long end, quint64 circ)
diameter Compute the distance between two point on the same latitude.
Definition: api.cpp:220
void addPacket(BeaconPacket packet)
addPacket Add packet to the buffer.
QString interface() const
interface ITS-G5 interface name getter.
void checkUCFPB()
checkUCFPB Check to flush the unicast forawarding packet buffer.
Definition: Geonet.cpp:3110
QByteArray verifySecuredPayload(QByteArray payload, bool *success)
verifySecuredPayload Send payload to verify to the HSM.jar.
Definition: Geonet.cpp:3488
QVector< UnicastForwardingPacketEntry > ucForwardPacketBuffer() const
ucForwardPacketBuffer Buffer getter.
int LSRequestPacketProcessing(uchar *packet, sniff_ethernet_t *eth, BasicHeader bh, CommonHeader ch)
LSRequestPacketProcessing LSRequest Header decoding.
Definition: Geonet.cpp:1426
void setMacAddr(uchar *m)
setMacAddr MAC address setter.
void updateSourcePacketSoPv(LongPositionVector lpv)
updateSourcePacketSoPv Update entry SO PV.
void lpvUpdate()
lpvUpdate Slot used when a packet containing an LPV is sent.
Definition: Geonet.cpp:260
LongPositionVector soPv() const
soPv SHB SO PV getter.
Definition: SHBPacket.h:66
QByteArray toHex()
toHex Create a QByteArray containing the hexadecimal format of the GBCPacket.
int hybridSend(QByteArray payload)
hybridSend Send packet with the hybridization.
Definition: Geonet.cpp:1204
void format(unsigned char *msg, unsigned long v, int l)
format Format a number in byte array.
Definition: api.cpp:35
SecurityManager m_securityManager
Definition: Geonet.h:708
qint32 longitude() const
longitude Longitude field getter
GnAddr gnAddr() const
gnAddr GnAddr field getter
TrafficClass trafficClass
Definition: GNDataRequest.h:59
LongPositionVector soPv() const
soPv GUC SO PV getter.
Definition: GUCPacket.h:77
void calculAngleThreshold()
calculAngleThreshold Compute the ANGLE_TH for the advanced forwarding.
Definition: Geonet.cpp:191
void addPacket(GnAddr destination, gnDataRequest_t packet, quint32 timestamp)
addPacket Add packet to the buffer.
QByteArray toHex()
toHex Create a QByteArray containing the hexadecimal format of the GBCPacket.
Definition: GBCPacket.cpp:55
void lpvSent()
lpvSent Signal used when a packet containing an LPV is sent.
void clear()
clear Clear the buffer.
LongPositionVector soPv() const
soPv SO PV getter.
Definition: BeaconPacket.h:63
quint16 distanceA() const
geoAreaPosLatitude Distance A field getter.
GUCPacket gucPacket() const
gucPacket Entry packet getter.