PUD: add getNodeIdPointers function
authorFerry Huberts <f.huberts@mindef.nl>
Thu, 4 Aug 2011 13:24:12 +0000 (15:24 +0200)
committerFerry Huberts <f.huberts@mindef.nl>
Thu, 18 Aug 2011 13:25:41 +0000 (15:25 +0200)
Signed-off-by: Ferry Huberts <f.huberts@mindef.nl>
lib/pud/src/configuration.c
lib/pud/src/nodeIdConversion.c
lib/pud/src/receiver.c
lib/pud/src/wireFormat.c
lib/pud/src/wireFormat.h

index 12c85b5..931683d 100644 (file)
@@ -400,10 +400,12 @@ static bool setupNodeIdNumberForOlsrCacheAndValidate(NodeIdType nodeIdTypeNumber
                        return true;
 
                case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
-                       return setupNodeIdNumberForOlsrCache(0LL, 999999999999999LL, 7);
+                       return setupNodeIdNumberForOlsrCache(0LL, 999999999999999LL,
+                                       PUD_NODEIDTYPE_MSISDN_BYTES);
 
                case PUD_NODEIDTYPE_TETRA: /* a Tetra number */
-                       return setupNodeIdNumberForOlsrCache(0LL, 99999999999999999LL, 8);
+                       return setupNodeIdNumberForOlsrCache(0LL, 99999999999999999LL,
+                                       PUD_NODEIDTYPE_TETRA_BYTES);
 
                case PUD_NODEIDTYPE_DNS: /* DNS name */
                {
@@ -419,13 +421,16 @@ static bool setupNodeIdNumberForOlsrCacheAndValidate(NodeIdType nodeIdTypeNumber
                }
 
                case PUD_NODEIDTYPE_192:
-                       return setupNodeIdNumberForOlsrCache(0LL, 9999999LL, 3);
+                       return setupNodeIdNumberForOlsrCache(0LL, 9999999LL,
+                                       PUD_NODEIDTYPE_192_BYTES);
 
                case PUD_NODEIDTYPE_193:
-                       return setupNodeIdNumberForOlsrCache(0LL, 999999LL, 3);
+                       return setupNodeIdNumberForOlsrCache(0LL, 999999LL,
+                                       PUD_NODEIDTYPE_193_BYTES);
 
                case PUD_NODEIDTYPE_194:
-                       return setupNodeIdNumberForOlsrCache(1LL, 8191LL, 2);
+                       return setupNodeIdNumberForOlsrCache(1LL, 8191LL,
+                                       PUD_NODEIDTYPE_194_BYTES);
 
                default: /* unsupported */
                        /* explicit return: configured nodeId is not relevant, will
index 7956464..fce6d90 100644 (file)
  * Node Information
  * ************************************************************************ */
 
+/**
+ Convert the nodeIdType of an OLSR message into a string.
+
+ @param ipVersion
+ The ip version, either AF_INET or AF_INET6
+ @param olsrMessage
+ A pointer to the OLSR message. Used to be able to retrieve the IP address of
+ the sender.
+ @param nodeIdTypeBuffer
+ A pointer to the buffer in which the nodeIdType string representation is
+ written (the buffer needs to be at least PUD_TX_NODEIDTYPE_DIGITS + 1 bytes).
+ When NULL then the nodeIdType string is not written.
+ @param nodeIdTypeBufferSize
+ The size of the nodeIdTypeBuffer
+ */
+void getNodeTypeStringFromOlsr(int ipVersion, union olsr_message * olsrMessage,
+               char * nodeIdTypeBuffer, int nodeIdTypeBufferSize) {
+       int chars;
+
+       if (unlikely(!nodeIdTypeBuffer || (nodeIdTypeBufferSize == 0))) {
+               return;
+       }
+
+       assert(nodeIdTypeBufferSize >= (PUD_TX_NODEIDTYPE_DIGITS + 1));
+
+       /* message has NO nodeId information */
+       chars = snprintf(&nodeIdTypeBuffer[0], nodeIdTypeBufferSize, "%u",
+                       getNodeIdType(ipVersion, olsrMessage));
+       if (likely(chars < nodeIdTypeBufferSize)) {
+               nodeIdTypeBuffer[chars] = '\0';
+       } else {
+               nodeIdTypeBuffer[nodeIdTypeBufferSize] = '\0';
+       }
+
+       return;
+}
+
 /**
  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
@@ -43,7 +80,7 @@ size_t setupNodeInfoForOlsr(PudOlsrWireFormat * olsrGpsMessage,
                case PUD_NODEIDTYPE_MAC: /* hardware address */
                        /* handled when the message is actually sent into OLSR, in the
                         * pre-transmit hook */
-                       length = IFHWADDRLEN;
+                       length = PUD_NODEIDTYPE_MAC_BYTES;
                        break;
 
                case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
@@ -104,28 +141,27 @@ size_t setupNodeInfoForOlsr(PudOlsrWireFormat * olsrGpsMessage,
  Get a nodeId number (in string representation), using a certain number of
  bytes, from the message of an OLSR message.
 
- @param olsrGpsMessage
- A pointer to the OLSR message
- @param bytes
- The number of bytes used by the number
+ @param buffer
+ A pointer to the buffer that holds the nodeId
+ @param bufferSize
+ The number of bytes used by the number in the buffer
  @param nodeIdBuffer
  The buffer in which to place the nodeId number in string representation
  @param nodeIdBufferSize
- The size of the buffer
+ The size of the nodeIdbuffer
 
  @return
  A pointer to the nodeId string representation (&nodeIdBuffer[0])
  */
-static char *getNodeIdNumberFromOlsr(PudOlsrWireFormat * olsrGpsMessage,
-               unsigned int bytes, char *nodeIdBuffer, socklen_t nodeIdBufferSize) {
-       unsigned char * nodeId = &(olsrGpsMessage->nodeInfo.nodeId);
+static char *getNodeIdNumberFromOlsr(unsigned char * buffer,
+               unsigned int bufferSize, char *nodeIdBuffer, socklen_t nodeIdBufferSize) {
        unsigned long long val = 0;
        unsigned int i = 0;
        int chars;
 
-       while (i < bytes) {
+       while (i < bufferSize) {
                val <<= 8;
-               val += nodeId[i];
+               val += buffer[i];
                i++;
        }
 
@@ -163,7 +199,8 @@ static char *getNodeIdNumberFromOlsr(PudOlsrWireFormat * olsrGpsMessage,
 void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
                const char **nodeId, char *nodeIdBuffer, unsigned int nodeIdBufferSize) {
        PudOlsrWireFormat * olsrGpsMessage;
-       int chars;
+       unsigned char * buffer;
+       unsigned int bufferSize;
 
        if (unlikely(!nodeIdBuffer || (nodeIdBufferSize == 0) || !nodeId)) {
                return;
@@ -173,17 +210,15 @@ void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
 
        olsrGpsMessage = getOlsrMessagePayload(ipVersion, olsrMessage);
 
+       getNodeIdPointers(ipVersion, olsrMessage, &buffer, &bufferSize);
+
        if (olsrGpsMessage->smask & PUD_FLAGS_ID) {
                switch (olsrGpsMessage->nodeInfo.nodeIdType) {
                        case PUD_NODEIDTYPE_MAC: /* hardware address */
                        {
-                               unsigned char * hwAddr = &olsrGpsMessage->nodeInfo.nodeId;
-
-                               assert (IFHWADDRLEN == 6);
-
-                               chars = snprintf(nodeIdBuffer, nodeIdBufferSize,
-                                               "%02x:%02x:%02x:%02x:%02x:%02x", hwAddr[0], hwAddr[1],
-                                               hwAddr[2], hwAddr[3], hwAddr[4], hwAddr[5]);
+                               int chars = snprintf(nodeIdBuffer, nodeIdBufferSize,
+                                               "%02x:%02x:%02x:%02x:%02x:%02x", buffer[0], buffer[1],
+                                               buffer[2], buffer[3], buffer[4], buffer[5]);
                                if (likely(chars < (int) nodeIdBufferSize)) {
                                        nodeIdBuffer[chars] = '\0';
                                } else {
@@ -193,40 +228,22 @@ void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
                        }
                                break;
 
-                       case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
-                               *nodeId = getNodeIdNumberFromOlsr(olsrGpsMessage, 7,
-                                               nodeIdBuffer, nodeIdBufferSize);
-                               break;
-
-                       case PUD_NODEIDTYPE_TETRA: /* a Tetra number */
-                               *nodeId = getNodeIdNumberFromOlsr(olsrGpsMessage, 8,
-                                               nodeIdBuffer, nodeIdBufferSize);
-                               break;
-
                        case PUD_NODEIDTYPE_DNS: /* DNS name */
                                *nodeId = (char *) &olsrGpsMessage->nodeInfo.nodeId;
                                break;
 
+                       case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
+                       case PUD_NODEIDTYPE_TETRA: /* a Tetra number */
                        case PUD_NODEIDTYPE_192:
                        case PUD_NODEIDTYPE_193:
-                               *nodeId = getNodeIdNumberFromOlsr(olsrGpsMessage, 3,
-                                               nodeIdBuffer, nodeIdBufferSize);
-                               break;
-
                        case PUD_NODEIDTYPE_194:
-                               *nodeId = getNodeIdNumberFromOlsr(olsrGpsMessage, 2,
+                               *nodeId = getNodeIdNumberFromOlsr(buffer, bufferSize,
                                                nodeIdBuffer, nodeIdBufferSize);
                                break;
 
                        case PUD_NODEIDTYPE_IPV4: /* IPv4 address */
                        case PUD_NODEIDTYPE_IPV6: /* IPv6 address */
-                               goto noId;
-
                        default: /* unsupported */
-                               pudError(false, "Reception of unsupported %s %u,"
-                                               " falling back to IP address", PUD_NODE_ID_TYPE_NAME,
-                                               olsrGpsMessage->nodeInfo.nodeIdType);
-                               olsrGpsMessage->smask &= ~PUD_FLAGS_ID;
                                goto noId;
                }
 
@@ -241,40 +258,3 @@ void getNodeIdStringFromOlsr(int ipVersion, union olsr_message *olsrMessage,
 
        return;
 }
-
-/**
- Convert the nodeIdType of an OLSR message into a string.
-
- @param ipVersion
- The ip version, either AF_INET or AF_INET6
- @param olsrMessage
- A pointer to the OLSR message. Used to be able to retrieve the IP address of
- the sender.
- @param nodeIdTypeBuffer
- A pointer to the buffer in which the nodeIdType string representation is
- written (the buffer needs to be at least PUD_TX_NODEIDTYPE_DIGITS + 1 bytes).
- When NULL then the nodeIdType string is not written.
- @param nodeIdTypeBufferSize
- The size of the nodeIdTypeBuffer
- */
-void getNodeTypeStringFromOlsr(int ipVersion, union olsr_message * olsrMessage,
-               char * nodeIdTypeBuffer, int nodeIdTypeBufferSize) {
-       int chars;
-
-       if (unlikely(!nodeIdTypeBuffer || (nodeIdTypeBufferSize == 0))) {
-               return;
-       }
-
-       assert(nodeIdTypeBufferSize >= (PUD_TX_NODEIDTYPE_DIGITS + 1));
-
-       /* message has NO nodeId information */
-       chars = snprintf(&nodeIdTypeBuffer[0], nodeIdTypeBufferSize, "%u",
-                       getNodeIdType(ipVersion, olsrMessage));
-       if (likely(chars < nodeIdTypeBufferSize)) {
-               nodeIdTypeBuffer[chars] = '\0';
-       } else {
-               nodeIdTypeBuffer[nodeIdTypeBufferSize] = '\0';
-       }
-
-       return;
-}
index a2950b7..eedefde 100644 (file)
@@ -150,11 +150,11 @@ static void nodeIdPreTransmitHook(union olsr_message *olsrMessage,
 
                if (likely(olsrIf != NULL)) {
                        memcpy(&olsrGpsMessage->nodeInfo.nodeId, &olsrIf->hwAddress[0],
-                                       IFHWADDRLEN);
+                                       PUD_NODEIDTYPE_MAC_BYTES);
                } else {
                        pudError(false, "Could not find OLSR interface %s, cleared its"
                                " MAC address in the OLSR message\n", ifn->int_name);
-                       memset(&olsrGpsMessage->nodeInfo.nodeId, 0, IFHWADDRLEN);
+                       memset(&olsrGpsMessage->nodeInfo.nodeId, 0, PUD_NODEIDTYPE_MAC_BYTES);
                }
        }
 }
index 67362b0..115effd 100644 (file)
@@ -8,7 +8,6 @@
 #include <stdbool.h>
 #include <math.h>
 #include <assert.h>
-#include <netinet/in.h>
 
 /*
  * GPS Information Conversion Functions For OLSR GPS Wire Format
@@ -526,3 +525,75 @@ NodeIdType getNodeIdType(int ipVersion, union olsr_message * olsrMessage) {
 
        return ((ipVersion == AF_INET) ? PUD_NODEIDTYPE_IPV4 : PUD_NODEIDTYPE_IPV6);
 }
+
+/**
+ Get the nodeId and its size, accounting for nodeId presence
+
+ @param ipVersion
+ The ip version, either AF_INET or AF_INET6
+ @param olsrMessage
+ A pointer to the OLSR message
+ @param buffer
+ A pointer to the location where a pointer to the nodeId (as contained in the
+ olsrMessage) can be stored
+ @param bufferSize
+ A pointer to the location where the number of bytes in the buffer can be
+ stored
+ */
+void getNodeIdPointers(int ipVersion, union olsr_message * olsrMessage,
+               unsigned char ** buffer, unsigned int * bufferSize) {
+       PudOlsrWireFormat * olsrGpsMessage = getOlsrMessagePayload(ipVersion, olsrMessage);
+
+       if (olsrGpsMessage->smask & PUD_FLAGS_ID) {
+               *buffer = &olsrGpsMessage->nodeInfo.nodeId;
+
+               switch (olsrGpsMessage->nodeInfo.nodeIdType) {
+                       case PUD_NODEIDTYPE_MAC: /* hardware address */
+                               *bufferSize = PUD_NODEIDTYPE_MAC_BYTES;
+                               break;
+
+                       case PUD_NODEIDTYPE_MSISDN: /* an MSISDN number */
+                               *bufferSize = PUD_NODEIDTYPE_MSISDN_BYTES;
+                               break;
+
+                       case PUD_NODEIDTYPE_TETRA: /* a Tetra number */
+                               *bufferSize = PUD_NODEIDTYPE_TETRA_BYTES;
+                               break;
+
+                       case PUD_NODEIDTYPE_DNS: /* DNS name */
+                               *buffer = (unsigned char *) &olsrGpsMessage->nodeInfo.nodeId;
+                               *bufferSize = strlen((char *)*buffer);
+                               break;
+
+                       case PUD_NODEIDTYPE_192:
+                               *bufferSize = PUD_NODEIDTYPE_192_BYTES;
+                               break;
+
+                       case PUD_NODEIDTYPE_193:
+                               *bufferSize = PUD_NODEIDTYPE_193_BYTES;
+                               break;
+
+                       case PUD_NODEIDTYPE_194:
+                               *bufferSize = PUD_NODEIDTYPE_194_BYTES;
+                               break;
+
+                       case PUD_NODEIDTYPE_IPV4: /* IPv4 address */
+                       case PUD_NODEIDTYPE_IPV6: /* IPv6 address */
+                       default: /* unsupported */
+                               olsrGpsMessage->smask &= ~PUD_FLAGS_ID;
+                               goto noId;
+               }
+
+               return;
+       }
+
+       /* message has NO nodeId information */
+       noId: {
+               *buffer = (unsigned char *) getOlsrMessageOriginator(ipVersion,
+                               olsrMessage);
+               *bufferSize = (ipVersion == AF_INET) ? PUD_NODEIDTYPE_IPV4_BYTES :
+                               PUD_NODEIDTYPE_IPV6_BYTES;
+       }
+
+       return;
+}
index c12a3a1..ca38e51 100644 (file)
@@ -7,6 +7,7 @@
 /* System includes */
 #include <stdint.h>
 #include <time.h>
+#include <net/if.h>
 
 /*
  * Version
@@ -150,6 +151,30 @@ typedef enum _NodeIdType {
        PUD_NODEIDTYPE_194 = 194
 } NodeIdType;
 
+/** the number of nodeId bytes for PUD_NODEIDTYPE_MAC */
+#define PUD_NODEIDTYPE_MAC_BYTES               IFHWADDRLEN
+
+/** the number of nodeId bytes for PUD_NODEIDTYPE_MSISDN */
+#define PUD_NODEIDTYPE_MSISDN_BYTES            7
+
+/** the number of nodeId bytes for PUD_NODEIDTYPE_TETRA */
+#define PUD_NODEIDTYPE_TETRA_BYTES             8
+
+/** the number of nodeId bytes for PUD_NODEIDTYPE_192 */
+#define PUD_NODEIDTYPE_192_BYTES               3
+
+/** the number of nodeId bytes for PUD_NODEIDTYPE_193 */
+#define PUD_NODEIDTYPE_193_BYTES               3
+
+/** the number of nodeId bytes for PUD_NODEIDTYPE_194 */
+#define PUD_NODEIDTYPE_194_BYTES               2
+
+/** the number of nodeId bytes for PUD_NODEIDTYPE_IPV4 */
+#define PUD_NODEIDTYPE_IPV4_BYTES              sizeof(struct in_addr)
+
+/** the number of nodeId bytes for PUD_NODEIDTYPE_IPV6 */
+#define PUD_NODEIDTYPE_IPV6_BYTES              sizeof(struct in6_addr)
+
 /** The maximum size of the string representation of the nodeIdType */
 #define PUD_TX_NODEIDTYPE_DIGITS       3
 
@@ -248,4 +273,7 @@ double getHdopFromOlsr(uint32_t olsrHdop);
 
 NodeIdType getNodeIdType(int ipVersion, union olsr_message * olsrMessage);
 
+void getNodeIdPointers(int ipVersion, union olsr_message * olsrMessage,
+               unsigned char ** buffer, unsigned int * bufferSize);
+
 #endif /* _PUD_WIREFORMAT_H_ */