PUD: decouple MAC address retrieval from createOlsrInterface function
authorFerry Huberts <f.huberts@mindef.nl>
Wed, 26 Oct 2011 14:08:13 +0000 (16:08 +0200)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 14 Feb 2012 13:32:58 +0000 (14:32 +0100)
Signed-off-by: Ferry Huberts <f.huberts@mindef.nl>
lib/pud/src/networkInterfaces.c

index b15f963..8776dd3 100644 (file)
 /** the MAC address of the main IP address */
 static unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES] = { 0 };
 
+/** true when the MAC address of the main IP address has been retrieved */
+static bool macSet = false;
+
 /**
  * @return
  * the MAC address of the main IP address
  */
 unsigned char * getMainIpMacAddress(void) {
+       if (!macSet) {
+               struct ifreq ifr;
+               unsigned char * macInIfr;
+
+               struct interface *mainInterface = if_ifwithaddr(&olsr_cnf->main_addr);
+               if (!mainInterface) {
+                       pudError(true, "Could not get the main interface");
+                       return NULL;
+               }
+               macInIfr = getHardwareAddress(mainInterface->int_name,olsr_cnf->ip_version,&ifr);
+               if (!macInIfr) {
+                       pudError(true, "Could not get the MAC address of the main interface");
+                       return NULL;
+               }
+               memcpy(&mac[0], &macInIfr[0], PUD_NODEIDTYPE_MAC_BYTES);
+               macSet = true;
+       }
+
        return &mac[0];
 }
 
@@ -600,20 +621,6 @@ bool createNetworkInterfaces(socket_handler_func rxSocketHandlerFunction,
        int retval = false;
        struct ifaddrs *ifAddrs = NULL;
        struct ifaddrs *ifAddr = NULL;
-       struct ifreq ifr;
-       unsigned char * macInIfr;
-
-       struct interface *mainInterface = if_ifwithaddr(&olsr_cnf->main_addr);
-       if (!mainInterface) {
-               pudError(true, "Could not get the main interface");
-               return retval;
-       }
-       macInIfr = getHardwareAddress(mainInterface->int_name,olsr_cnf->ip_version,&ifr);
-       if (!macInIfr) {
-               pudError(true, "Could not get the MAC address of the main interface");
-               return retval;
-       }
-       memcpy(&mac[0], &macInIfr[0], PUD_NODEIDTYPE_MAC_BYTES);
 
        errno = 0;
        if (getifaddrs(&ifAddrs) != 0) {