PUD: move nodeId validation into configuration
authorFerry Huberts <f.huberts@mindef.nl>
Thu, 21 Jul 2011 10:05:40 +0000 (12:05 +0200)
committerFerry Huberts <f.huberts@mindef.nl>
Mon, 25 Jul 2011 09:45:50 +0000 (11:45 +0200)
And move the nodeId number cache in there as well

Signed-off-by: Ferry Huberts <f.huberts@mindef.nl>
lib/pud/src/configuration.c
lib/pud/src/configuration.h
lib/pud/src/nodeIdConversion.c
lib/pud/src/nodeIdConversion.h

index 5252888..dc23570 100644 (file)
@@ -3,7 +3,6 @@
 /* Plugin includes */
 #include "pud.h"
 #include "netTools.h"
-#include "nodeIdConversion.h"
 #include "networkInterfaces.h"
 
 /* OLSR includes */
@@ -268,6 +267,135 @@ int setNodeId(const char *value, void *data __attribute__ ((unused)), set_plugin
        return false;
 }
 
+/*
+ * nodeId Cache
+ */
+
+/** The size of the cached nodeId buffer */
+#define PUD_CACHED_NODEID_BUFFER_SIZE 16
+
+/** The cached nodeId buffer: contains a pre-processed version of the nodeId
+ in order to improve performance. It is currently used for nodeIdTypes
+ PUD_NODEIDTYPE_MSISDN, PUD_NODEIDTYPE_TETRA, PUD_NODEIDTYPE_192,
+ PUD_NODEIDTYPE_193 (so basically for numbers that will not change) */
+static unsigned char cachedNodeIdBuffer[PUD_CACHED_NODEID_BUFFER_SIZE];
+
+/** The number of bytes stored in cachedNodeIdBuffer */
+static unsigned int cachedNodeIdBufferLength = 0;
+
+/**
+ @param buffer
+ A pointer to the location in which to store a pointer to the nodeId cache
+ buffer
+ @param length
+ A pointer to the location in which to store the number of bytes in the buffer
+ */
+void getNodeIdNumberForOlsrCache(unsigned char ** buffer, unsigned int * length) {
+       *buffer = &cachedNodeIdBuffer[0];
+       *length = cachedNodeIdBufferLength;
+}
+
+/**
+ Check a nodeId number for validity and if valid set it up in the
+ cachedNodeIdBuffer. The valid range for the number is [min, max].
+
+ @param min
+ The lower bound for a valid number
+ @param max
+ The upper bound for a valid number
+ @param bytes
+ The number of bytes used by the number in the wire format
+
+ @return
+ - true when the number is valid
+ - false otherwise
+ */
+static bool setupNodeIdNumberForOlsrCache(unsigned long long min,
+               unsigned long long max, unsigned int bytes) {
+       unsigned long long val;
+
+       assert (bytes <= PUD_CACHED_NODEID_BUFFER_SIZE);
+
+       if (!getNodeIdAsNumber(&val)) {
+               return false;
+       }
+
+       if ((val >= min) && (val <= max)) {
+               int i = bytes - 1;
+               while (i >= 0) {
+                       cachedNodeIdBuffer[i] = val & 0xff;
+                       val >>= 8;
+                       i--;
+               }
+
+               assert(val == 0);
+
+               cachedNodeIdBufferLength = bytes;
+               return true;
+       }
+
+       pudError(false, "%s value %llu is out of range [%llu,%llu]",
+                       PUD_NODE_ID_NAME, val, min, max);
+       return false;
+}
+
+/*
+ * nodeId Validation
+ */
+
+/**
+ Validate whether the configured nodeId is valid w.r.t. the configured
+ nodeIdType
+
+ @return
+ - true when ok
+ - false on failure
+ */
+static bool setupNodeIdNumberForOlsrCacheAndValidate(NodeIdType nodeIdTypeNumber) {
+       switch (nodeIdTypeNumber) {
+               case PUD_NODEIDTYPE_IPV4: /* IPv4 address */
+               case PUD_NODEIDTYPE_IPV6: /* IPv6 address */
+               case PUD_NODEIDTYPE_MAC: /* hardware address */
+                       /* explicit return: configured nodeId is not relevant */
+                       return true;
+
+               case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
+                       return setupNodeIdNumberForOlsrCache(0LL, 999999999999999LL, 7);
+
+               case PUD_NODEIDTYPE_TETRA: /* a Tetra number */
+                       return setupNodeIdNumberForOlsrCache(0LL, 99999999999999999LL, 8);
+
+               case PUD_NODEIDTYPE_DNS: /* DNS name */
+               {
+                       bool invalidChars;
+                       char report[256];
+
+                       invalidChars = nmea_string_has_invalid_chars((char *) getNodeId(),
+                                       PUD_NODE_ID_NAME, &report[0], sizeof(report));
+                       if (invalidChars) {
+                               pudError(false, &report[0]);
+                       }
+                       return !invalidChars;
+               }
+
+               case PUD_NODEIDTYPE_192:
+                       return setupNodeIdNumberForOlsrCache(0LL, 9999999LL, 3);
+
+               case PUD_NODEIDTYPE_193:
+                       return setupNodeIdNumberForOlsrCache(0LL, 999999LL, 3);
+
+               case PUD_NODEIDTYPE_194:
+                       return setupNodeIdNumberForOlsrCache(1LL, 8191LL, 2);
+
+               default: /* unsupported */
+                       /* explicit return: configured nodeId is not relevant, will
+                        * fallback to IP addresses */
+                       return true;
+       }
+
+       return false;
+}
+
 /*
  * rxNonOlsrIf
  */
@@ -1663,7 +1791,7 @@ unsigned int checkConfig(void) {
                }
        }
 
-       if (!validateNodeId(nodeIdType)) {
+       if (!setupNodeIdNumberForOlsrCacheAndValidate(nodeIdType)) {
                retval = false;
        }
 
index ee1eb50..709532e 100644 (file)
@@ -36,6 +36,8 @@ bool getNodeIdAsNumber(unsigned long long * value);
 unsigned char * getNodeIdWithLength(size_t *length);
 int setNodeId(const char *value, void *data, set_plugin_parameter_addon addon);
 
+void getNodeIdNumberForOlsrCache(unsigned char ** buffer, unsigned int * length);
+
 /*
  * RX Parameters
  */
index 0bc60d5..64163a5 100644 (file)
  * Node Information
  * ************************************************************************ */
 
-/** The size of the cached nodeId buffer */
-#define PUD_CACHED_NODEID_BUFFER_SIZE 16
-
-/** The cached nodeId buffer: contains a pre-processed version of the nodeId
- in order to improve performance. It is currently used for nodeIdTypes
- PUD_NODEIDTYPE_MSISDN, PUD_NODEIDTYPE_TETRA, PUD_NODEIDTYPE_192,
- PUD_NODEIDTYPE_193 (so basically for numbers that will not change) */
-static unsigned char cachedNodeIdBuffer[PUD_CACHED_NODEID_BUFFER_SIZE];
-
-/** The number of bytes stored in cachedNodeIdBuffer */
-static unsigned char cachedNodeIdBufferLength = 0;
-
-/**
- Check a nodeId number for validity and if valid set it up in the
- cachedNodeIdBuffer. The valid range for the number is [min, max].
-
- @param min
- The lower bound for a valid number
- @param max
- The upper bound for a valid number
- @param bytes
- The number of bytes used by the number in the wire format
-
- @return
- - true when the number is valid
- - false otherwise
- */
-static bool setupNodeIdNumberForOlsr(unsigned long long min,
-               unsigned long long max, unsigned int bytes) {
-       unsigned long long val;
-
-       assert (bytes <= PUD_CACHED_NODEID_BUFFER_SIZE);
-
-       if (!getNodeIdAsNumber(&val)) {
-               return false;
-       }
-
-       if ((val >= min) && (val <= max)) {
-               int i = bytes - 1;
-               while (i >= 0) {
-                       cachedNodeIdBuffer[i] = val & 0xff;
-                       val >>= 8;
-                       i--;
-               }
-
-               assert(val == 0);
-
-               cachedNodeIdBufferLength = bytes;
-               return true;
-       }
-
-       pudError(false, "%s value %llu is out of range [%llu,%llu]",
-                       PUD_NODE_ID_NAME, val, min, max);
-       return false;
-}
-
-/**
- Validate whether the configured nodeId is valid w.r.t. the configured
- nodeIdType
-
- @return
- - true when ok
- - false on failure
- */
-bool validateNodeId(NodeIdType nodeIdTypeNumber) {
-       switch (nodeIdTypeNumber) {
-               case PUD_NODEIDTYPE_IPV4: /* IPv4 address */
-               case PUD_NODEIDTYPE_IPV6: /* IPv6 address */
-               case PUD_NODEIDTYPE_MAC: /* hardware address */
-                       /* explicit return: configured nodeId is not relevant */
-                       return true;
-
-               case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
-                       return setupNodeIdNumberForOlsr(0LL, 999999999999999LL, 7);
-
-               case PUD_NODEIDTYPE_TETRA: /* a Tetra number */
-                       return setupNodeIdNumberForOlsr(0LL, 99999999999999999LL, 8);
-
-               case PUD_NODEIDTYPE_DNS: /* DNS name */
-               {
-                       bool invalidChars;
-                       char report[256];
-
-                       invalidChars = nmea_string_has_invalid_chars((char *) getNodeId(),
-                                       PUD_NODE_ID_NAME, &report[0], sizeof(report));
-                       if (invalidChars) {
-                               pudError(false, &report[0]);
-                       }
-                       return !invalidChars;
-               }
-
-               case PUD_NODEIDTYPE_192:
-                       return setupNodeIdNumberForOlsr(0LL, 9999999LL, 3);
-
-               case PUD_NODEIDTYPE_193:
-                       return setupNodeIdNumberForOlsr(0LL, 999999LL, 3);
-
-               case PUD_NODEIDTYPE_194:
-                       return setupNodeIdNumberForOlsr(1LL, 8191LL, 2);
-
-               default: /* unsupported */
-                       /* explicit return: configured nodeId is not relevant, will
-                        * fallback to IP addresses */
-                       return true;
-       }
-
-       return false;
-}
-
 /**
  Convert the node information to the node information for an OLSR message and
  put it in the PUD message in the OLSR message. Also updates the PUD message
@@ -143,7 +34,8 @@ bool validateNodeId(NodeIdType nodeIdTypeNumber) {
 size_t setupNodeInfoForOlsr(PudOlsrWireFormat * olsrGpsMessage,
                unsigned int olsrMessageSize) {
        NodeIdType nodeIdTypeNumber = getNodeIdTypeNumber();
-       size_t length = 0;
+       unsigned char * buffer;
+       unsigned int length = 0;
 
        olsrGpsMessage->nodeInfo.nodeIdType = nodeIdTypeNumber;
        switch (nodeIdTypeNumber) {
@@ -158,9 +50,8 @@ size_t setupNodeInfoForOlsr(PudOlsrWireFormat * olsrGpsMessage,
                case PUD_NODEIDTYPE_192:
                case PUD_NODEIDTYPE_193:
                case PUD_NODEIDTYPE_194:
-                       length = cachedNodeIdBufferLength;
-                       memcpy(&olsrGpsMessage->nodeInfo.nodeId, &cachedNodeIdBuffer[0],
-                                       length);
+                       getNodeIdNumberForOlsrCache(&buffer, &length);
+                       memcpy(&olsrGpsMessage->nodeInfo.nodeId, buffer, length);
                        break;
 
                case PUD_NODEIDTYPE_DNS: /* DNS name */
index aaf3237..6afe2c0 100644 (file)
@@ -11,8 +11,6 @@
 #include <stdbool.h>
 #include <stddef.h>
 
-bool validateNodeId(NodeIdType nodeIdTypeNumber);
-
 size_t setupNodeInfoForOlsr(PudOlsrWireFormat * olsrGpsMessage,
                unsigned int olsrMessageSize);