4 #include "configuration.h"
5 #include "networkInterfaces.h"
7 #include "gpsConversion.h"
20 /** The size of the buffer in which the received NMEA string is stored */
21 #define BUFFER_SIZE_FOR_OLSR 2048
23 /** The size of the buffer in which the transmit NMEA string is assembled */
24 #define BUFFER_SIZE_FROM_OLSR 512
26 /** The transmit socket address */
27 static union olsr_sockaddr * txAddress;
29 /** The de-duplication list */
30 static DeDupList deDupList;
33 Report a plugin error.
36 when true then errno is used in the error message; the error reason is also
39 a pointer to the format string
41 arguments to the format string
43 void pudError(bool useErrno, const char *format, ...) {
45 char *stringErr = NULL;
48 stringErr = strerror(errno);
51 if ((format == NULL) || (*format == '\0')) {
53 olsr_printf(0, "%s: %s\n", PUD_PLUGIN_ABBR, stringErr);
55 olsr_printf(0, "%s: Unknown error\n", PUD_PLUGIN_ABBR);
60 va_start(arglist, format);
61 vsnprintf(strDesc, sizeof(strDesc), format, arglist);
64 strDesc[sizeof(strDesc) - 1] = '\0'; /* Ensures null termination */
67 olsr_printf(0, "%s: %s: %s\n", PUD_PLUGIN_ABBR, strDesc, stringErr);
69 olsr_printf(0, "%s: %s\n", PUD_PLUGIN_ABBR, strDesc);
75 Sends a buffer out on all transmit interfaces
80 the number of bytes in the buffer
82 static void sendToAllTxInterfaces(unsigned char *buffer,
83 unsigned int bufferLength) {
84 TRxTxNetworkInterface *txNetworkInterfaces = getTxNetworkInterfaces();
85 while (txNetworkInterfaces != NULL) {
86 TRxTxNetworkInterface *networkInterface = txNetworkInterfaces;
88 #ifdef PUD_DUMP_GPS_PACKETS_TX_NON_OLSR
89 olsr_printf(0, "%s: packet sent to non-OLSR interface %s (%u bytes)\n",
90 PUD_PLUGIN_ABBR, &networkInterface->name[0], bufferLength);
91 dump_packet(&buffer[0], bufferLength);
95 if (sendto(networkInterface->socketFd, buffer, bufferLength, 0,
96 (struct sockaddr *) &txAddress->in, sizeof(txAddress->in)) < 0) {
97 pudError(true, "Transmit error on interface %s",
98 (char *) &networkInterface->name);
100 txNetworkInterfaces = networkInterface->next;
105 Called by OLSR core when a packet for the plugin is received from the OLSR
106 network. It converts the packet into an NMEA string and transmits it over all
107 transmit non-OLSR network interfaces.
110 a pointer to the received OLSR message
112 a pointer to the OLSR network interface on which the packet was received
114 a pointer to the IP address of the sender
117 - true when the packet was processed
120 bool packetReceivedFromOlsr(union olsr_message *olsrMessage,
121 struct interface *in_if __attribute__ ((unused)), union olsr_ip_addr *ipaddr __attribute__ ((unused))) {
122 const union olsr_ip_addr * originator = getOlsrMessageOriginator(
123 olsr_cnf->ip_version, olsrMessage);
124 unsigned int transmitStringLength;
125 unsigned char buffer[BUFFER_SIZE_FROM_OLSR];
127 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
128 unsigned short olsrMessageSize =
129 getOlsrMessageSize(olsr_cnf->ip_version, olsrMessage);
132 /* when we do not loopback then check if the message originated from this
134 if (!getUseLoopback() && ipequal(originator, &olsr_cnf->main_addr)) {
138 /* do deduplication: when we have already seen this message from the same
139 * originator then just back off */
140 if (likely(getUseDeDup())) {
141 if (isInDeDupList(&deDupList, olsrMessage)) {
145 addToDeDup(&deDupList, olsrMessage);
148 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
149 olsr_printf(0, "\n%s: packet received from OLSR interface %s (%u bytes)\n",
150 PUD_PLUGIN_ABBR, in_if->int_name, olsrMessageSize);
151 dump_packet((unsigned char *) olsrMessage, olsrMessageSize);
154 transmitStringLength = gpsFromOlsr(olsrMessage, &buffer[0], sizeof(buffer));
155 if (unlikely(transmitStringLength == 0)) {
159 sendToAllTxInterfaces(&buffer[0], transmitStringLength);
165 Called by OLSR core when a packet for the plugin is received from the non-OLSR
166 network. It converts the packet into the internal OLSR wire format for a
167 position update and transmits it over all OLSR network interfaces.
170 the socket file descriptor on which the packet is received
172 a pointer to the network interface structure on which the packet was received
176 #ifdef PUD_DUMP_GPS_PACKETS_RX_NON_OLSR
177 static void packetReceivedForOlsr(int skfd, void *data, unsigned int flags __attribute__ ((unused))) {
179 static void packetReceivedForOlsr(int skfd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
182 unsigned char rxBuffer[BUFFER_SIZE_FOR_OLSR];
184 struct sockaddr sender;
185 socklen_t senderSize = sizeof(sender);
187 assert(data != NULL);
189 /* Receive the captured Ethernet frame */
190 memset(&sender, 0, senderSize);
192 rxCount = recvfrom(skfd, &rxBuffer[0], (sizeof(rxBuffer) - 1), 0,
193 &sender, &senderSize);
195 pudError(true, "Receive error in %s, ignoring message.", __func__);
199 /* make sure the string is null-terminated */
200 rxBuffer[rxCount] = '\0';
202 /* only accept messages from configured IP addresses */
203 if (!isRxAllowedSourceIpAddress(&sender)) {
207 #ifdef PUD_DUMP_GPS_PACKETS_RX_NON_OLSR
209 TRxTxNetworkInterface * networkInterface = data;
214 if (olsr_cnf->ip_version == AF_INET) {
215 src = &((struct sockaddr_in*) &sender)->sin_addr;
216 port = ntohs(((struct sockaddr_in*) &sender)->sin_port);
218 src = &((struct sockaddr_in6*) &sender)->sin6_addr;
219 port = ntohs(((struct sockaddr_in6*) &sender)->sin6_port);
222 inet_ntop(olsr_cnf->ip_version, src, &fromAddr[0], sizeof(fromAddr));
223 olsr_printf(0, "\n%s: packet received from %s, port %u on non-OLSR"
224 " interface %s (%lu bytes)\n", PUD_PLUGIN_ABBR, &fromAddr[0],
225 port, &networkInterface->name[0], (size_t) rxCount);
227 dump_packet(&rxBuffer[0], (size_t)rxCount);
231 /* we have the received string in the rxBuffer now */
233 /* hand the NMEA information to the receiver */
234 (void) receiverUpdateGpsInformation(&rxBuffer[0], rxCount);
239 Initialise the plugin: check the configuration, initialise the NMEA parser,
240 create network interface sockets, hookup the plugin to OLSR and setup data
241 that can be setup in advance.
248 if (!checkConfig()) {
249 pudError(false, "Invalid configuration");
253 initDeDupList(&deDupList, getDeDupDepth());
255 /* set global transmit socket config */
256 txAddress = getTxMcAddr();
258 if (!startReceiver()) {
259 pudError(false, "Could not start receiver");
264 * Creates receive and transmit sockets and register the receive sockets
265 * with the OLSR stack
267 if (!createNetworkInterfaces(&packetReceivedForOlsr)) {
268 pudError(false, "Could not create require network interfaces");
272 if (!checkRunSetup()) {
273 pudError(false, "Invalid configuration");
278 * Tell OLSR to launch olsr_parser when the packets for this plugin
279 * arrive from the OLSR network
281 olsr_parser_add_function(&packetReceivedFromOlsr, PUD_OLSR_MSG_TYPE);
290 Stop the plugin: shut down all created network interface sockets and destroy
293 void closePud(void) {
294 closeNetworkInterfaces();
296 destroyDeDupList(&deDupList);