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;
123 unsigned int transmitStringLength;
124 unsigned char buffer[BUFFER_SIZE_FROM_OLSR];
126 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
127 unsigned int olsrMessageSize;
130 /* determine the originator of the messsage */
131 if (olsr_cnf->ip_version == AF_INET) {
132 originator = (const union olsr_ip_addr *) &olsrMessage->v4.originator;
133 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
134 olsrMessageSize = ntohs(olsrMessage->v4.olsr_msgsize);
137 originator = (const union olsr_ip_addr *) &olsrMessage->v6.originator;
138 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
139 olsrMessageSize = ntohs(olsrMessage->v6.olsr_msgsize);
143 /* when we do not loopback then check if the message originated from this
145 if (!getUseLoopback() && ipequal(originator, &olsr_cnf->main_addr)) {
149 /* do deduplication: when we have already seen this message from the same
150 * originator then just back off */
151 if (likely(getUseDeDup())) {
152 if (isInDeDupList(&deDupList, olsrMessage)) {
156 addToDeDup(&deDupList, olsrMessage);
159 #ifdef PUD_DUMP_GPS_PACKETS_RX_OLSR
160 olsr_printf(0, "\n%s: packet received from OLSR interface %s (%u bytes)\n",
161 PUD_PLUGIN_ABBR, in_if->int_name, olsrMessageSize);
162 dump_packet((unsigned char *) olsrMessage, olsrMessageSize);
165 transmitStringLength = gpsFromOlsr(olsrMessage, &buffer[0], sizeof(buffer));
166 if (unlikely(transmitStringLength == 0)) {
170 sendToAllTxInterfaces(&buffer[0], transmitStringLength);
176 Called by OLSR core when a packet for the plugin is received from the non-OLSR
177 network. It converts the packet into the internal OLSR wire format for a
178 position update and transmits it over all OLSR network interfaces.
181 the socket file descriptor on which the packet is received
183 a pointer to the network interface structure on which the packet was received
187 #ifdef PUD_DUMP_GPS_PACKETS_RX_NON_OLSR
188 static void packetReceivedForOlsr(int skfd, void *data, unsigned int flags __attribute__ ((unused))) {
190 static void packetReceivedForOlsr(int skfd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
193 unsigned char rxBuffer[BUFFER_SIZE_FOR_OLSR];
195 struct sockaddr sender;
196 socklen_t senderSize = sizeof(sender);
198 assert(data != NULL);
200 /* Receive the captured Ethernet frame */
201 memset(&sender, 0, senderSize);
203 rxCount = recvfrom(skfd, &rxBuffer[0], (sizeof(rxBuffer) - 1), 0,
204 &sender, &senderSize);
206 pudError(true, "Receive error in %s, ignoring message.", __func__);
210 /* make sure the string is null-terminated */
211 rxBuffer[rxCount] = '\0';
213 /* only accept messages from configured IP addresses */
214 if (!isRxAllowedSourceIpAddress(&sender)) {
218 #ifdef PUD_DUMP_GPS_PACKETS_RX_NON_OLSR
220 TRxTxNetworkInterface * networkInterface = data;
225 if (olsr_cnf->ip_version == AF_INET) {
226 src = &((struct sockaddr_in*) &sender)->sin_addr;
227 port = ntohs(((struct sockaddr_in*) &sender)->sin_port);
229 src = &((struct sockaddr_in6*) &sender)->sin6_addr;
230 port = ntohs(((struct sockaddr_in6*) &sender)->sin6_port);
233 inet_ntop(olsr_cnf->ip_version, src, &fromAddr[0], sizeof(fromAddr));
234 olsr_printf(0, "\n%s: packet received from %s, port %u on non-OLSR"
235 " interface %s (%lu bytes)\n", PUD_PLUGIN_ABBR, &fromAddr[0],
236 port, &networkInterface->name[0], (size_t) rxCount);
238 dump_packet(&rxBuffer[0], (size_t)rxCount);
242 /* we have the received string in the rxBuffer now */
244 /* hand the NMEA information to the receiver */
245 (void) receiverUpdateGpsInformation(&rxBuffer[0], rxCount);
250 Initialise the plugin: check the configuration, initialise the NMEA parser,
251 create network interface sockets, hookup the plugin to OLSR and setup data
252 that can be setup in advance.
259 if (!checkConfig()) {
260 pudError(false, "Invalid configuration");
264 initDeDupList(&deDupList, getDeDupDepth());
266 /* set global transmit socket config */
267 txAddress = getTxMcAddr();
269 if (!startReceiver()) {
270 pudError(false, "Could not start receiver");
275 * Creates receive and transmit sockets and register the receive sockets
276 * with the OLSR stack
278 if (!createNetworkInterfaces(&packetReceivedForOlsr)) {
279 pudError(false, "Could not create require network interfaces");
283 if (!checkRunSetup()) {
284 pudError(false, "Invalid configuration");
289 * Tell OLSR to launch olsr_parser when the packets for this plugin
290 * arrive from the OLSR network
292 olsr_parser_add_function(&packetReceivedFromOlsr, PUD_OLSR_MSG_TYPE);
301 Stop the plugin: shut down all created network interface sockets and destroy
304 void closePud(void) {
305 closeNetworkInterfaces();
307 destroyDeDupList(&deDupList);