PUD: fix setting up network interface.
authorFerry Huberts <ferry.huberts@pelagic.nl>
Mon, 25 Jun 2012 13:28:22 +0000 (15:28 +0200)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Mon, 25 Jun 2012 15:23:12 +0000 (17:23 +0200)
Broke in 63e28a0

Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/pud/src/configuration.c
lib/pud/src/configuration.h
lib/pud/src/netTools.c
lib/pud/src/netTools.h
lib/pud/src/networkInterfaces.c

index 7640fb8..eaa4135 100644 (file)
@@ -359,6 +359,21 @@ int addRxNonOlsrInterface(const char *value, void *data __attribute__ ((unused))
        return false;
 }
 
+/**
+ * @return the number of configured non-olsr receive interfaces
+ */
+unsigned int getRxNonOlsrInterfaceCount(void) {
+       return rxNonOlsrInterfaceCount;
+}
+
+/**
+ * @param index the index of the configured non-olsr receive interface
+ * @return the index-th interface name
+ */
+unsigned char * getRxNonOlsrInterfaceName(unsigned int index) {
+       return &rxNonOlsrInterfaceNames[index][0];
+}
+
 /*
  * rxAllowedSourceIpAddress
  */
@@ -616,6 +631,21 @@ int addTxNonOlsrInterface(const char *value, void *data __attribute__ ((unused))
        return false;
 }
 
+/**
+ * @return the number of configured non-olsr transmit interfaces
+ */
+unsigned int getTxNonOlsrInterfaceCount(void) {
+       return txNonOlsrInterfaceCount;
+}
+
+/**
+ * @param index the index of the configured non-olsr transmit interface
+ * @return the index-th interface name
+ */
+unsigned char * getTxNonOlsrInterfaceName(unsigned int index) {
+       return &txNonOlsrInterfaceNames[index][0];
+}
+
 /*
  * txMcAddr + txMcPort
  */
index 851edaf..4152037 100644 (file)
@@ -44,6 +44,8 @@ int setNodeId(const char *value, void *data, set_plugin_parameter_addon addon);
 bool isRxNonOlsrInterface(const char *ifName);
 int addRxNonOlsrInterface(const char *value, void *data,
                set_plugin_parameter_addon addon);
+unsigned int getRxNonOlsrInterfaceCount(void);
+unsigned char * getRxNonOlsrInterfaceName(unsigned int index);
 
 /** The name of the allowed source IP address plugin parameter */
 #define PUD_RX_ALLOWED_SOURCE_IP_NAME                  "rxAllowedSourceIpAddress"
@@ -91,6 +93,8 @@ int setPositionFile(const char *value, void *data, set_plugin_parameter_addon ad
 bool isTxNonOlsrInterface(const char *ifName);
 int addTxNonOlsrInterface(const char *value, void *data,
                set_plugin_parameter_addon addon);
+unsigned int getTxNonOlsrInterfaceCount(void);
+unsigned char * getTxNonOlsrInterfaceName(unsigned int index);
 
 /** The name of the transmit multicast address plugin parameter */
 #define PUD_TX_MC_ADDR_NAME                                            "txMcAddr"
index 1c54e75..686f116 100644 (file)
@@ -51,3 +51,43 @@ unsigned char * getHardwareAddress(const char * ifName, int family,
 
        return (unsigned char *) &ifr->ifr_hwaddr.sa_data[0];
 }
+
+/**
+ Get the IPv4 address of an interface
+
+ @param ifName
+ the name of the interface
+ @param ifr
+ the buffer in which to write the IPv4 address
+
+ @return
+ - the pointer to the IPv4 address (inside ifr)
+ - NULL on failure
+ */
+struct in_addr * getIPv4Address(const char * ifName, struct ifreq *ifr) {
+       int fd;
+       int cpySize;
+
+       assert(ifName != NULL);
+       assert(strlen(ifName) <= IFNAMSIZ);
+       assert(ifr != NULL);
+
+       fd = socket(AF_INET, SOCK_DGRAM, 0);
+
+       ifr->ifr_addr.sa_family = AF_INET;
+       memset(ifr->ifr_name, 0, sizeof(ifr->ifr_name));
+       cpySize = (strlen(ifName) < sizeof(ifr->ifr_name)) ? strlen(ifName)
+                       : sizeof(ifr->ifr_name);
+       strncpy(ifr->ifr_name, ifName, cpySize);
+
+       errno = 0;
+       if (ioctl(fd, SIOCGIFADDR, ifr) < 0) {
+               pudError(true, "%s@%u: ioctl(SIOCGIFADDR) error", __FILE__, __LINE__);
+               close(fd);
+               return NULL;
+       }
+
+       close(fd);
+
+       return &((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr;
+}
index 1d1580f..47afeab 100644 (file)
@@ -83,4 +83,6 @@ static inline bool isMulticast(union olsr_sockaddr *addr) {
 unsigned char * getHardwareAddress(const char * ifName, int family,
                struct ifreq *ifr);
 
+struct in_addr * getIPv4Address(const char * ifName, struct ifreq *ifr);
+
 #endif /* _PUD_NETTOOLS_H_ */
index be55eb7..328f34b 100644 (file)
@@ -175,16 +175,17 @@ static int createRxSocket(TRxTxNetworkInterface * networkInterface,
         * which the multicast datagrams are to be received. */
        if (ipFamilySetting == AF_INET) {
                struct ip_mreq mc_settings;
-               struct interface* interface = if_ifwithname(networkInterface->name);
 
-               if (!interface) {
+               struct ifreq ifr;
+               struct in_addr * ifAddr = getIPv4Address(networkInterface->name, &ifr);
+               if (!ifAddr) {
                        pudError(true, "Could not get interface address of %s", networkInterface->name);
                        goto bail;
                }
 
                (void) memset(&mc_settings, 0, sizeof(mc_settings));
                mc_settings.imr_multiaddr = rxMcAddr->in4.sin_addr;
-               mc_settings.imr_interface = interface->int_addr.sin_addr;
+               mc_settings.imr_interface = *ifAddr;
                errno = 0;
                if (setsockopt(rxSocket, ipProtoSetting, ipAddMembershipSetting,
                                &mc_settings, sizeof(mc_settings)) < 0) {
@@ -333,9 +334,9 @@ static int createTxSocket(TRxTxNetworkInterface * networkInterface, union olsr_s
 
        memset(&address, 0, sizeof(address));
        if (txMcAddr->in.sa_family == AF_INET) {
-               struct interface* interface = if_ifwithname(networkInterface->name);
-
-               if (!interface) {
+               struct ifreq ifr;
+               struct in_addr * ifAddr = getIPv4Address(networkInterface->name, &ifr);
+               if (!ifAddr) {
                        pudError(true, "Could not get interface address of %s", networkInterface->name);
                        goto bail;
                }
@@ -350,7 +351,7 @@ static int createTxSocket(TRxTxNetworkInterface * networkInterface, union olsr_s
                ifIndex = 0;
 
                address.in4.sin_family = ipFamilySetting;
-               address.in4.sin_addr = interface->int_addr.sin_addr;
+               address.in4.sin_addr = *ifAddr;
                address.in4.sin_port = getTxMcPort();
                addr = &address.in4;
                addrSize = sizeof(struct sockaddr_in);
@@ -589,31 +590,25 @@ static int createDownlinkSocket(int ipVersion, socket_handler_func rxSocketHandl
  */
 bool createNetworkInterfaces(socket_handler_func rxSocketHandlerFunction,
                socket_handler_func rxSocketHandlerFunctionDownlink) {
-       TRxTxNetworkInterface * interfaces;
        union olsr_sockaddr * rxMcAddr = getRxMcAddr();
        union olsr_sockaddr * txMcAddr = getTxMcAddr();
+       unsigned int count = 0;
 
        /* loop over all configured rx interfaces */
-       interfaces = getRxNetworkInterfaces();
-       if (interfaces) {
-               TRxTxNetworkInterface * interface;
-               for (interface = interfaces; interface != NULL; interface = interfaces->next) {
-                       if (!createRxInterface(interface->name, rxSocketHandlerFunction, rxMcAddr)) {
-                               /* creating a receive interface failed */
-                               return false;
-                       }
+       count = getRxNonOlsrInterfaceCount();
+       while (count--) {
+               if (!createRxInterface((char *)getRxNonOlsrInterfaceName(count), rxSocketHandlerFunction, rxMcAddr)) {
+                       /* creating a receive interface failed */
+                       return false;
                }
        }
 
        /* loop over all configured tx interfaces */
-       interfaces = getTxNetworkInterfaces();
-       if (interfaces) {
-               TRxTxNetworkInterface * interface;
-               for (interface = interfaces; interface != NULL; interface = interfaces->next) {
-                       if (!createTxInterface(interface->name, txMcAddr)) {
-                               /* creating a transmit interface failed */
-                               return false;
-                       }
+       count = getTxNonOlsrInterfaceCount();
+       while (count--) {
+               if (!createTxInterface((char *)getTxNonOlsrInterfaceName(count), txMcAddr)) {
+                       /* creating a transmit interface failed */
+                       return false;
                }
        }