4 #include "configuration.h"
5 #include "networkInterfaces.h"
7 #include "gpsConversion.h"
19 /** The size of the buffer in which the received NMEA string is stored */
20 #define BUFFER_SIZE_FOR_OLSR 2048
22 /** The size of the buffer in which the transmit NMEA string is assembled */
23 #define BUFFER_SIZE_FROM_OLSR 512
25 /** The transmit socket address */
26 static union olsr_sockaddr * txAddress;
28 /** The de-duplication list */
29 static DeDupList deDupList;
32 Report a plugin error.
35 when true then errno is used in the error message; the error reason is also
38 a pointer to the format string
40 arguments to the format string
42 void pudError(bool useErrno, const char *format, ...) {
44 char *stringErr = NULL;
47 stringErr = strerror(errno);
50 if ((format == NULL) || (*format == '\0')) {
52 olsr_printf(0, "%s: %s\n", PUD_PLUGIN_ABBR, stringErr);
54 olsr_printf(0, "%s: Unknown error\n", PUD_PLUGIN_ABBR);
59 va_start(arglist, format);
60 vsnprintf(strDesc, sizeof(strDesc), format, arglist);
63 strDesc[sizeof(strDesc) - 1] = '\0'; /* Ensures null termination */
66 olsr_printf(0, "%s: %s: %s\n", PUD_PLUGIN_ABBR, strDesc, stringErr);
68 olsr_printf(0, "%s: %s\n", PUD_PLUGIN_ABBR, strDesc);
74 Sends a buffer out on all transmit interfaces
79 the number of bytes in the buffer
81 static void sendToAllTxInterfaces(unsigned char *buffer,
82 unsigned int bufferLength) {
83 TRxTxNetworkInterface *txNetworkInterfaces = getTxNetworkInterfaces();
84 while (txNetworkInterfaces != NULL) {
85 TRxTxNetworkInterface *networkInterface = txNetworkInterfaces;
87 #ifdef PUD_DUMP_GPS_PACKETS_TX_NON_OLSR
88 olsr_printf(0, "%s: packet sent to non-OLSR interface %s (%u bytes)\n",
89 PUD_PLUGIN_ABBR, &networkInterface->name[0], bufferLength);
90 dump_packet(&buffer[0], bufferLength);
94 if (sendto(networkInterface->socketFd, buffer, bufferLength, 0,
95 (struct sockaddr *) &txAddress->in, sizeof(txAddress->in)) < 0) {
96 pudError(true, "Transmit error on interface %s",
97 (char *) &networkInterface->name);
99 txNetworkInterfaces = networkInterface->next;
104 Called by OLSR core when a packet for the plugin is received from the OLSR
105 network. It converts the packet into an NMEA string and transmits it over all
106 transmit non-OLSR network interfaces.
109 a pointer to the received OLSR message
111 a pointer to the OLSR network interface on which the packet was received
113 a pointer to the IP address of the sender
116 - true when the packet was processed
119 bool packetReceivedFromOlsr(union olsr_message *olsrMessage,
120 struct interface *in_if __attribute__ ((unused)), union olsr_ip_addr *ipaddr __attribute__ ((unused))) {
121 const union olsr_ip_addr * originator;
122 unsigned int transmitStringLength;
123 unsigned char buffer[BUFFER_SIZE_FROM_OLSR];
125 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
126 unsigned int olsrMessageSize;
129 /* determine the originator of the messsage */
130 if (olsr_cnf->ip_version == AF_INET) {
131 originator = (const union olsr_ip_addr *) &olsrMessage->v4.originator;
132 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
133 olsrMessageSize = ntohs(olsrMessage->v4.olsr_msgsize);
136 originator = (const union olsr_ip_addr *) &olsrMessage->v6.originator;
137 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
138 olsrMessageSize = ntohs(olsrMessage->v6.olsr_msgsize);
142 /* when we do not loopback then check if the message originated from this
144 if (!getUseLoopback() && ipequal(originator, &olsr_cnf->main_addr)) {
148 /* do deduplication: when we have already seen this message from the same
149 * originator then just back off */
150 if (likely(getUseDeDup())) {
151 if (isInDeDupList(&deDupList, olsrMessage)) {
155 addToDeDup(&deDupList, olsrMessage);
158 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
159 olsr_printf(0, "\n%s: packet received from OLSR interface %s (%u bytes)\n",
160 PUD_PLUGIN_ABBR, in_if->int_name, olsrMessageSize);
161 dump_packet((unsigned char *) olsrMessage, olsrMessageSize);
164 transmitStringLength = gpsFromOlsr(olsrMessage, &buffer[0], sizeof(buffer));
165 if (unlikely(transmitStringLength == 0)) {
169 sendToAllTxInterfaces(&buffer[0], transmitStringLength);
175 Called by OLSR core when a packet for the plugin is received from the non-OLSR
176 network. It converts the packet into the internal OLSR wire format for a
177 position update and transmits it over all OLSR network interfaces.
180 the socket file descriptor on which the packet is received
182 a pointer to the network interface structure on which the packet was received
186 #ifdef PUD_DUMP_GPS_PACKETS_RX_NON_OLSR
187 static void packetReceivedForOlsr(int skfd, void *data, unsigned int flags __attribute__ ((unused))) {
189 static void packetReceivedForOlsr(int skfd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
192 unsigned char rxBuffer[BUFFER_SIZE_FOR_OLSR];
194 struct sockaddr sender;
195 socklen_t senderSize = sizeof(sender);
197 assert(data != NULL);
199 /* Receive the captured Ethernet frame */
200 memset(&sender, 0, senderSize);
202 rxCount = recvfrom(skfd, &rxBuffer[0], (sizeof(rxBuffer) - 1), 0,
203 &sender, &senderSize);
205 pudError(true, "Receive error in %s, ignoring message.", __func__);
209 /* make sure the string is null-terminated */
210 rxBuffer[rxCount] = '\0';
212 /* only accept messages from configured IP addresses */
213 if (!isRxAllowedSourceIpAddress(&sender)) {
217 #ifdef PUD_DUMP_GPS_PACKETS_RX_NON_OLSR
219 TRxTxNetworkInterface * networkInterface = data;
224 if (olsr_cnf->ip_version == AF_INET) {
225 src = &((struct sockaddr_in*) &sender)->sin_addr;
226 port = ntohs(((struct sockaddr_in*) &sender)->sin_port);
228 src = &((struct sockaddr_in6*) &sender)->sin6_addr;
229 port = ntohs(((struct sockaddr_in6*) &sender)->sin6_port);
232 inet_ntop(olsr_cnf->ip_version, src, &fromAddr[0], sizeof(fromAddr));
233 olsr_printf(0, "\n%s: packet received from %s, port %u on non-OLSR"
234 " interface %s (%lu bytes)\n", PUD_PLUGIN_ABBR, &fromAddr[0],
235 port, &networkInterface->name[0], (size_t) rxCount);
237 dump_packet(&rxBuffer[0], (size_t)rxCount);
241 /* we have the received string in the rxBuffer now */
243 /* hand the NMEA information to the receiver */
244 (void) receiverUpdateGpsInformation(&rxBuffer[0], rxCount);
249 Initialise the plugin: check the configuration, initialise the NMEA parser,
250 create network interface sockets, hookup the plugin to OLSR and setup data
251 that can be setup in advance.
258 if (!checkConfig()) {
259 pudError(false, "Invalid configuration");
263 initDeDupList(&deDupList, getDeDupDepth());
265 /* set global transmit socket config */
266 txAddress = getTxMcAddr();
268 if (!startReceiver()) {
269 pudError(false, "Could not start receiver");
274 * Creates receive and transmit sockets and register the receive sockets
275 * with the OLSR stack
277 if (!createNetworkInterfaces(&packetReceivedForOlsr)) {
278 pudError(false, "Could not create require network interfaces");
282 if (!checkRunSetup()) {
283 pudError(false, "Invalid configuration");
288 * Tell OLSR to launch olsr_parser when the packets for this plugin
289 * arrive from the OLSR network
291 olsr_parser_add_function(&packetReceivedFromOlsr, PUD_OLSR_MSG_TYPE);
300 Stop the plugin: shut down all created network interface sockets and destroy
303 void closePud(void) {
304 closeNetworkInterfaces();
306 destroyDeDupList(&deDupList);