PUD: shift knowledge of nodeId to wireformat library
authorFerry Huberts <ferry.huberts@pelagic.nl>
Fri, 3 Feb 2012 17:00:04 +0000 (18:00 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 14 Feb 2012 13:32:59 +0000 (14:32 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/pud/src/configuration.c
lib/pud/wireformat/include/OlsrdPudWireFormat/nodeIdConversion.h
lib/pud/wireformat/include/OlsrdPudWireFormat/wireFormat.h
lib/pud/wireformat/src/nodeIdConversion.c

index 6f5a3b8..3520e35 100644 (file)
@@ -10,6 +10,7 @@
 #include "olsr_cfg.h"
 
 /* System includes */
+#include <OlsrdPudWireFormat/nodeIdConversion.h>
 #include <assert.h>
 #include <errno.h>
 #include <stdlib.h>
@@ -190,16 +191,6 @@ int setNodeIdType(const char *value, void *data __attribute__ ((unused)),
  * nodeId
  */
 
-/**
- The type that is used to store the nodeId as a binary representation
- */
-typedef union _nodeIdBinaryType {
-               unsigned long long longValue;
-               unsigned char stringValue[PUD_TX_NODEID_BUFFERSIZE + 1];
-               union olsr_ip_addr ip;
-               unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES];
-} nodeIdBinaryType;
-
 /** The nodeId buffer */
 static unsigned char nodeId[PUD_TX_NODEID_BUFFERSIZE + 1];
 
@@ -317,16 +308,14 @@ int setNodeId(const char *value, void *data __attribute__ ((unused)), set_plugin
  - true when ok
  - false on failure
  */
-static bool setupNodeIdBinaryMAC(void) {
+static bool intSetupNodeIdBinaryMAC(void) {
        unsigned char * mac = getMainIpMacAddress();
        if (!mac) {
                return false;
        }
 
-       memcpy(&nodeIdBinary.mac, mac, PUD_NODEIDTYPE_MAC_BYTES);
-       nodeIdBinaryLength = PUD_NODEIDTYPE_MAC_BYTES;
-       nodeIdBinarySet = true;
-       return true;
+       return setupNodeIdBinaryMAC(&nodeIdBinary, &nodeIdBinaryLength,
+                       &nodeIdBinarySet, mac);
 }
 
 /**
@@ -344,11 +333,9 @@ static bool setupNodeIdBinaryMAC(void) {
  - true when ok
  - false on failure
  */
-static bool setupNodeIdBinaryLongLong(unsigned long long min,
+static bool intSetupNodeIdBinaryLongLong(unsigned long long min,
                unsigned long long max, unsigned int bytes) {
        unsigned long long longValue = 0;
-       int i = bytes - 1;
-
        if (!readULL(PUD_NODE_ID_NAME, (char *) getNodeId(), &longValue)) {
                return false;
        }
@@ -359,17 +346,8 @@ static bool setupNodeIdBinaryLongLong(unsigned long long min,
                return false;
        }
 
-       while (i >= 0) {
-               ((unsigned char *)&nodeIdBinary.longValue)[i] = longValue & 0xff;
-               longValue >>= 8;
-               i--;
-       }
-
-       assert(longValue == 0);
-
-       nodeIdBinaryLength = bytes;
-       nodeIdBinarySet = true;
-       return true;
+       return setupNodeIdBinaryLongLong(&nodeIdBinary, &nodeIdBinaryLength,
+                               &nodeIdBinarySet, longValue, bytes);
 }
 
 /**
@@ -380,7 +358,7 @@ static bool setupNodeIdBinaryLongLong(unsigned long long min,
  - true when ok
  - false on failure
  */
-static bool setupNodeIdBinaryString(void) {
+static bool intSetupNodeIdBinaryString(void) {
        bool invalidChars;
        char report[256];
        size_t nodeidlength;
@@ -398,11 +376,8 @@ static bool setupNodeIdBinaryString(void) {
                return false;
        }
 
-       /* including trailing \0 */
-       memcpy(&nodeIdBinary.stringValue[0], &nodeid[0], nodeidlength + 1);
-       nodeIdBinaryLength = nodeIdLength + 1;
-       nodeIdBinarySet = true;
-       return true;
+       return setupNodeIdBinaryString(&nodeIdBinary, &nodeIdBinaryLength,
+                       &nodeIdBinarySet, nodeid, nodeidlength);
 }
 
 /**
@@ -413,7 +388,7 @@ static bool setupNodeIdBinaryString(void) {
  - true when ok
  - false on failure
  */
-static bool setupNodeIdBinaryIp(void) {
+static bool intSetupNodeIdBinaryIp(void) {
        void * src;
        size_t length;
        if (olsr_cnf->ip_version == AF_INET) {
@@ -424,10 +399,8 @@ static bool setupNodeIdBinaryIp(void) {
                length = sizeof(struct in6_addr);
        }
 
-       memcpy(&nodeIdBinary.ip, src, length);
-       nodeIdBinaryLength = length;
-       nodeIdBinarySet = true;
-       return true;
+       return setupNodeIdBinaryIp(&nodeIdBinary, &nodeIdBinaryLength,
+                       &nodeIdBinarySet, src, length);
 }
 
 /**
@@ -441,43 +414,43 @@ static bool setupNodeIdBinaryIp(void) {
 static bool setupNodeIdBinaryAndValidate(NodeIdType nodeIdTypeNumber) {
        switch (nodeIdTypeNumber) {
                case PUD_NODEIDTYPE_MAC: /* hardware address */
-                       return setupNodeIdBinaryMAC();
+                       return intSetupNodeIdBinaryMAC();
 
                case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
-                       return setupNodeIdBinaryLongLong(PUD_NODEIDTYPE_MSISDN_MIN,
+                       return intSetupNodeIdBinaryLongLong(PUD_NODEIDTYPE_MSISDN_MIN,
                                PUD_NODEIDTYPE_MSISDN_MAX, PUD_NODEIDTYPE_MSISDN_BYTES);
 
                case PUD_NODEIDTYPE_TETRA: /* a Tetra number */
-                       return setupNodeIdBinaryLongLong(PUD_NODEIDTYPE_TETRA_MIN,
+                       return intSetupNodeIdBinaryLongLong(PUD_NODEIDTYPE_TETRA_MIN,
                                PUD_NODEIDTYPE_TETRA_MAX, PUD_NODEIDTYPE_TETRA_BYTES);
 
                case PUD_NODEIDTYPE_DNS: /* DNS name */
-                       return setupNodeIdBinaryString();
+                       return intSetupNodeIdBinaryString();
 
                case PUD_NODEIDTYPE_MMSI: /* an AIS MMSI number */
-                       return setupNodeIdBinaryLongLong(PUD_NODEIDTYPE_MMSI_MIN,
+                       return intSetupNodeIdBinaryLongLong(PUD_NODEIDTYPE_MMSI_MIN,
                                PUD_NODEIDTYPE_MMSI_MAX, PUD_NODEIDTYPE_MMSI_BYTES);
 
                case PUD_NODEIDTYPE_URN: /* a URN number */
-                       return setupNodeIdBinaryLongLong(PUD_NODEIDTYPE_URN_MIN,
+                       return intSetupNodeIdBinaryLongLong(PUD_NODEIDTYPE_URN_MIN,
                                PUD_NODEIDTYPE_URN_MAX, PUD_NODEIDTYPE_URN_BYTES);
 
                case PUD_NODEIDTYPE_192:
-                       return setupNodeIdBinaryLongLong(PUD_NODEIDTYPE_192_MIN,
+                       return intSetupNodeIdBinaryLongLong(PUD_NODEIDTYPE_192_MIN,
                                PUD_NODEIDTYPE_192_MAX, PUD_NODEIDTYPE_192_BYTES);
 
                case PUD_NODEIDTYPE_193:
-                       return setupNodeIdBinaryLongLong(PUD_NODEIDTYPE_193_MIN,
+                       return intSetupNodeIdBinaryLongLong(PUD_NODEIDTYPE_193_MIN,
                                PUD_NODEIDTYPE_193_MAX, PUD_NODEIDTYPE_193_BYTES);
 
                case PUD_NODEIDTYPE_194:
-                       return setupNodeIdBinaryLongLong(PUD_NODEIDTYPE_194_MIN,
+                       return intSetupNodeIdBinaryLongLong(PUD_NODEIDTYPE_194_MIN,
                                PUD_NODEIDTYPE_194_MAX, PUD_NODEIDTYPE_194_BYTES);
 
                case PUD_NODEIDTYPE_IPV4: /* IPv4 address */
                case PUD_NODEIDTYPE_IPV6: /* IPv6 address */
                default: /* unsupported */
-                       return setupNodeIdBinaryIp();
+                       return intSetupNodeIdBinaryIp();
        }
 
        return false;
index dac8a38..95c143d 100644 (file)
@@ -14,4 +14,20 @@ void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
                const char **nodeIdStr, char *nodeIdStrBuffer,
                unsigned int nodeIdStrBufferSize);
 
+bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
+               unsigned char * mac);
+
+bool setupNodeIdBinaryLongLong(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
+               unsigned long long longValue, size_t bytes);
+
+bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
+               char * nodeId, size_t nodeIdLength);
+
+bool setupNodeIdBinaryIp(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet, void * ip,
+               size_t ipLength);
+
 #endif /* _PUD_NODEIDCONVERSION_H_ */
index 018a4d8..f5d72ed 100644 (file)
@@ -230,6 +230,16 @@ typedef enum _NodeIdType {
 /** The maximum size of the string representation of the nodeId */
 #define PUD_TX_NODEID_BUFFERSIZE               1023
 
+/**
+ The type that is used to store the nodeId as a binary representation
+ */
+typedef union _nodeIdBinaryType {
+               unsigned long long longValue;
+               unsigned char stringValue[PUD_TX_NODEID_BUFFERSIZE + 1];
+               union olsr_ip_addr ip;
+               unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES];
+} nodeIdBinaryType;
+
 /*
  * Wire Format Structures
  */
index aa7f710..a9027cb 100644 (file)
@@ -170,3 +170,125 @@ void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
 
        return;
 }
+
+/**
+ Convert a given MAC address to the binary/wireformat representation of it.
+
+ @param nodeIdBinary
+ a pointer to the buffer in which to store the binary/wireformat representation
+ @param nodeIdBinaryLength
+ a pointer to the variable in which to store the number of bytes used by the
+ binary/wireformat representation
+ @param nodeIdBinarySet
+ a pointer to a variable in which to store the result of whether the binary/wireformat
+ representation was stored
+ @param mac
+ a pointer to a buffer in which the MAC address is stored (in network byte-order)
+ @return
+ - true when ok
+ - false on failure
+ */
+bool setupNodeIdBinaryMAC(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
+               unsigned char * mac) {
+       memcpy(&nodeIdBinary->mac, mac, PUD_NODEIDTYPE_MAC_BYTES);
+       *nodeIdBinaryLength = PUD_NODEIDTYPE_MAC_BYTES;
+       *nodeIdBinarySet = true;
+       return true;
+}
+
+/**
+ Convert a given unsigned long long to the binary/wireformat representation of it.
+
+ @param nodeIdBinary
+ a pointer to the buffer in which to store the binary/wireformat representation
+ @param nodeIdBinaryLength
+ a pointer to the variable in which to store the number of bytes used by the
+ binary/wireformat representation
+ @param nodeIdBinarySet
+ a pointer to a variable in which to store the result of whether the binary/wireformat
+ representation was stored
+ @param value
+ the value to convert (in machine byte-order)
+ @param bytes
+ the number of bytes used by the value
+
+ @return
+ - true when ok
+ - false on failure
+ */
+bool setupNodeIdBinaryLongLong(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
+               unsigned long long value, size_t bytes) {
+       unsigned long long longValue = value;
+       int i = bytes - 1;
+
+       while (i >= 0) {
+               ((unsigned char *)&nodeIdBinary->longValue)[i] = longValue & 0xff;
+               longValue >>= 8;
+               i--;
+       }
+
+       assert(longValue == 0);
+
+       *nodeIdBinaryLength = bytes;
+       *nodeIdBinarySet = true;
+       return true;
+}
+
+/**
+ Convert a given string to the binary/wireformat representation of it.
+
+ @param nodeIdBinary
+ a pointer to the buffer in which to store the binary/wireformat representation
+ @param nodeIdBinaryLength
+ a pointer to the variable in which to store the number of bytes used by the
+ binary/wireformat representation
+ @param nodeIdBinarySet
+ a pointer to a variable in which to store the result of whether the binary/wireformat
+ representation was stored
+ @param nodeId
+ a pointer to the nodeId string
+ @param nodeIdLength
+ the length of the nodeId string
+ @return
+ - true when ok
+ - false on failure
+ */
+bool setupNodeIdBinaryString(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet,
+               char * nodeId, size_t nodeIdLength) {
+       /* including trailing \0 */
+       memcpy(&nodeIdBinary->stringValue[0], &nodeId[0], nodeIdLength + 1);
+       *nodeIdBinaryLength = nodeIdLength + 1;
+       *nodeIdBinarySet = true;
+       return true;
+}
+
+/**
+ Convert a given IP address to the binary/wireformat representation of it.
+
+ @param nodeIdBinary
+ a pointer to the buffer in which to store the binary/wireformat representation
+ @param nodeIdBinaryLength
+ a pointer to the variable in which to store the number of bytes used by the
+ binary/wireformat representation
+ @param nodeIdBinarySet
+ a pointer to a variable in which to store the result of whether the binary/wireformat
+ representation was stored
+ @param ip
+ a pointer to a buffer in which the IP address is stored (in network byte-order)
+ @param ipLength
+ the number of bytes used by the IP address
+ @return
+ - true when ok
+ - false on failure
+ */
+bool setupNodeIdBinaryIp(nodeIdBinaryType * nodeIdBinary,
+               size_t * nodeIdBinaryLength, bool * nodeIdBinarySet, void * ip,
+               size_t ipLength) {
+       memcpy(&nodeIdBinary->ip, ip, ipLength);
+       *nodeIdBinaryLength = ipLength;
+       *nodeIdBinarySet = true;
+       return true;
+}