[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH 3/3 master] Consolidate ip address checking into functions.



---
 loader/kickstart.c  |   27 ++++++---------------------
 loader/loader.c     |   27 ++++++---------------------
 loader/net.c        |   45 ++++++++++++++++++++++++++++++++++++---------
 loader/net.h        |    4 ++++
 loader/nfsinstall.c |    3 +--
 5 files changed, 53 insertions(+), 53 deletions(-)

diff --git a/loader/kickstart.c b/loader/kickstart.c
index 5b1a57e..0397a0a 100644
--- a/loader/kickstart.c
+++ b/loader/kickstart.c
@@ -707,31 +707,16 @@ static void setKickstartNetwork(struct loaderData_s * loaderData, PyObject *hand
     attr = getObject(ele, "gateway", 0);
     if (isNotEmpty(attr)) {
         char *gateway = strdup(PyString_AsString(attr));
-        int rc;
-        struct in_addr addr;
-#ifdef ENABLE_IPV6
-        struct in6_addr addr6;
-#endif
-
-        if ((rc = inet_pton(AF_INET, gateway, &addr)) == 1) {
+        if (isValidIPv4Address(gateway)) {
             loaderData->gateway = gateway;
-        } else if (rc == 0) {
 #ifdef ENABLE_IPV6
-            if ((rc = inet_pton(AF_INET6, gateway, &addr6)) == 1) {
-                loaderData->gateway6 = gateway;
-            } else if (rc == 0) {
-#endif
-                logMessage(WARNING,
-                           "invalid address in kickstart --gateway");
-#ifdef ENABLE_IPV6
-            } else {
-                logMessage(ERROR, "%s (%d): %s", __func__, __LINE__,
-                           strerror(errno));
-            }
+        } else if (isValidIPv6Address(gateway)) {
+            loaderData->gateway6 = gateway;
 #endif
         } else {
-            logMessage(ERROR, "%s (%d): %s", __func__, __LINE__,
-                       strerror(errno));
+            logMessage(WARNING,
+                       "invalid address in kickstart --gateway");
+            free(gateway);
         }
     }
 
diff --git a/loader/loader.c b/loader/loader.c
index 208ed7d..63cb5c0 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -1090,31 +1090,16 @@ static void parseCmdLineFlags(struct loaderData_s * loaderData,
                 loaderData->netmask = g_strdup(v);
             } else if (!strcasecmp(k, "gateway")) {
                 char *gateway = g_strdup(v);
-                int rc;
-                struct in_addr addr;
-#ifdef ENABLE_IPV6
-                struct in6_addr addr6;
-#endif
-
-                if ((rc = inet_pton(AF_INET, gateway, &addr)) == 1) {
+                if (isValidIPv4Address(gateway)) {
                     loaderData->gateway = gateway;
-                } else if (rc == 0) {
 #ifdef ENABLE_IPV6
-                    if ((rc = inet_pton(AF_INET6, gateway, &addr6)) == 1) {
-                        loaderData->gateway6 = gateway;
-                    } else if (rc == 0) {
-#endif
-                        logMessage(WARNING,
-                                   "invalid address in boot option gateway");
-#ifdef ENABLE_IPV6
-                    } else {
-                        logMessage(ERROR, "%s (%d): %s", __func__, __LINE__,
-                                   strerror(errno));
-                    }
+                } else if (isValidIPv6Address(gateway)) {
+                    loaderData->gateway6 = gateway;
 #endif
                 } else {
-                    logMessage(ERROR, "%s (%d): %s", __func__, __LINE__,
-                               strerror(errno));
+                    logMessage(WARNING,
+                               "invalid address in boot option gateway");
+                    free(gateway);
                 }
             } else if (!strcasecmp(k, "dns")) {
                 loaderData->dns = g_strdup(v);
diff --git a/loader/net.c b/loader/net.c
index 22ed892..399aa16 100644
--- a/loader/net.c
+++ b/loader/net.c
@@ -68,13 +68,12 @@ extern uint64_t flags;
 static void cidrCallback(newtComponent co, void * dptr) {
     struct intfconfig_s * data = dptr;
     int cidr, upper = 0;
-    struct in_addr addr;
 
     if (co == data->cidr4Entry) {
         if (data->cidr4 == NULL && data->ipv4 == NULL)
             return;
 
-        if (inet_pton(AF_INET, data->cidr4, &addr) >= 1)
+        if (isValidIPv4Address(data->cidr4))
             return;
 
         errno = 0;
@@ -333,14 +332,14 @@ void setupIfaceStruct(iface_t * iface, struct loaderData_s * loaderData) {
     /* iBFT configured DNS */
     if(iface->ipv4method == IPV4_IBFT_METHOD){
 	if(iface->numdns<MAXNS){
-	    if(ibft_iface_dns1() && inet_pton(AF_INET, ibft_iface_dns1(), &addr)>=1){
+	    if(ibft_iface_dns1() && isValidIPv4Address(ibft_iface_dns1())){
 		iface->dns[iface->numdns] = strdup(ibft_iface_dns1());
 		iface->numdns++;
 		logMessage(INFO, "adding iBFT dns server %s", ibft_iface_dns1());
 	    }
 	}
 	if(iface->numdns<MAXNS){
-	    if(ibft_iface_dns2() && inet_pton(AF_INET, ibft_iface_dns2(), &addr)>=1){
+	    if(ibft_iface_dns2() && isValidIPv4Address(ibft_iface_dns2())){
 		iface->dns[iface->numdns] = strdup(ibft_iface_dns2());
 		iface->numdns++;
 		logMessage(INFO, "adding iBFT dns server %s", ibft_iface_dns2());
@@ -791,9 +790,7 @@ int manualNetConfig(char * device, iface_t * iface,
     int i, rows, pos, cidr, have[2], stack[2];
     char *buf = NULL;
     char ret[48];
-    struct in_addr addr;
 #ifdef ENABLE_IPV6
-    struct in6_addr addr6;
     int prefix;
 #endif
     struct in_addr *tmpaddr = NULL;
@@ -1129,10 +1126,10 @@ int manualNetConfig(char * device, iface_t * iface,
         /* gather nameservers */
         if (ipcomps->ns) {
 #ifdef ENABLE_IPV6
-            if ((inet_pton(AF_INET, ipcomps->ns, &addr) >= 1) ||
-                (inet_pton(AF_INET6, ipcomps->ns, &addr6) >= 1)) {
+            if (isValidIPv4Address(ipcomps->ns) ||
+                isValidIPv6Address(ipcomps->ns)) {
 #else
-            if (inet_pton(AF_INET, ipcomps->ns, &addr) >= 1) {
+            if (isValidIPv4Address(ipcomps->ns)) {
 #endif
                 iface->dns[0] = strdup(ipcomps->ns);
                 if (iface->numdns < 1)
@@ -2080,4 +2077,34 @@ int get_connection(iface_t *iface) {
     return 3;
 }
 
+int isValidIPv4Address(const char *address) {
+    int rc;
+    struct in_addr addr;
+    if ((rc = inet_pton(AF_INET, address, &addr)) >= 1) {
+        return 1;
+    } else if (rc == 0) {
+        return 0;
+    } else {
+        logMessage(ERROR, "%s (%d): %s", __func__, __LINE__,
+        strerror(errno));
+        return 0;
+    }
+}
+
+#ifdef ENABLE_IPV6
+int isValidIPv6Address(const char *address) {
+    int rc;
+    struct in6_addr addr;
+    if ((rc = inet_pton(AF_INET6, address, &addr)) >= 1) {
+        return 1;
+    } else if (rc == 0) {
+        return 0;
+    } else {
+        logMessage(ERROR, "%s (%d): %s", __func__, __LINE__,
+        strerror(errno));
+        return 0;
+    }
+}
+#endif
+
 /* vim:set shiftwidth=4 softtabstop=4: */
diff --git a/loader/net.h b/loader/net.h
index 96c1ffb..d34e42f 100644
--- a/loader/net.h
+++ b/loader/net.h
@@ -73,5 +73,9 @@ int kickstartNetworkUp(struct loaderData_s * loaderData,
                        iface_t * iface);
 void splitHostname (char *str, char **host, char **port);
 int get_connection(iface_t * iface);
+int isValidIPv4Address(const char *address);
+#ifdef ENABLE_IPV6
+int isValidIPv6Address(const char *address);
+#endif
 
 #endif
diff --git a/loader/nfsinstall.c b/loader/nfsinstall.c
index ed92f78..45260c2 100644
--- a/loader/nfsinstall.c
+++ b/loader/nfsinstall.c
@@ -362,7 +362,6 @@ int getFileFromNfs(char * url, char * dest, struct loaderData_s * loaderData) {
             NMDHCP4Config *dhcp = NULL;
             const char *server_name = NULL;
             const char *filename = NULL;
-            struct in_addr addr;
             char nextserver[INET_ADDRSTRLEN+1];
 
             if (nm_device_get_state(candidate) != NM_DEVICE_STATE_ACTIVATED)
@@ -386,7 +385,7 @@ int getFileFromNfs(char * url, char * dest, struct loaderData_s * loaderData) {
 
             /* 'server_name' may be a hostname or an IPv4 address */
             memset(&nextserver, '\0', sizeof(nextserver));
-            if (inet_pton(AF_INET, server_name, &addr) >= 1) {
+            if (isValidIPv4Address(server_name)) {
                 strcpy(nextserver, server_name);
             } else {
                 struct hostent *he = gethostbyname(server_name);
-- 
1.7.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]