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

[PATCH v2] mini-os: netfront: Read netmask and gateway from Xenstore



When providing the IP address via the config file, one can also add the
netmask and gateway address, e.g. "ip=192.168.0.2 255.255.255.0
192.168.0.1", which will be further published to Xenstore. This patch adds
support for reading the netmask and gateway values.

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 include/netfront.h |  2 +-
 lwip-net.c         | 32 ++++++++++++++++++++++----------
 netfront.c         | 34 +++++++++++++++++++++++++++++-----
 3 files changed, 52 insertions(+), 16 deletions(-)

diff --git a/include/netfront.h b/include/netfront.h
index 1164d50..bc3080e 100644
--- a/include/netfront.h
+++ b/include/netfront.h
@@ -7,7 +7,7 @@ struct netfront_dev *init_netfront(char *nodename,
                                    void (*netif_rx)(unsigned char *data,
                                                     int len, void* arg),
                                    unsigned char rawmac[6],
-                                   char **ip);
+                                   char **ip, char **mask, char **gw);
 void netfront_xmit(struct netfront_dev *dev, unsigned char* data,int len);
 void shutdown_netfront(struct netfront_dev *dev);
 void suspend_netfront(void);
diff --git a/lwip-net.c b/lwip-net.c
index 449b70f..80d1c8f 100644
--- a/lwip-net.c
+++ b/lwip-net.c
@@ -343,22 +343,34 @@ void start_networking(void)
   struct ip_addr ipaddr = { htonl(IF_IPADDR) };
   struct ip_addr netmask = { htonl(IF_NETMASK) };
   struct ip_addr gw = { 0 };
-  char *ip = NULL;
+  char *ip = NULL, *netmask_str = NULL, *gw_str = NULL;
 
   tprintk("Waiting for network.\n");
 
-  dev = init_netfront(NULL, NULL, rawmac, &ip);
+  dev = init_netfront(NULL, NULL, rawmac, &ip, &netmask_str, &gw_str);
   
   if (ip) {
     ipaddr.addr = inet_addr(ip);
-    if (IN_CLASSA(ntohl(ipaddr.addr)))
-      netmask.addr = htonl(IN_CLASSA_NET);
-    else if (IN_CLASSB(ntohl(ipaddr.addr)))
-      netmask.addr = htonl(IN_CLASSB_NET);
-    else if (IN_CLASSC(ntohl(ipaddr.addr)))
-      netmask.addr = htonl(IN_CLASSC_NET);
-    else
-      tprintk("Strange IP %s, leaving netmask to 0.\n", ip);
+    free(ip);
+
+    if (netmask_str) {
+        netmask.addr = inet_addr(netmask_str);
+        free(netmask_str);
+    } else {
+        if (IN_CLASSA(ntohl(ipaddr.addr)))
+            netmask.addr = htonl(IN_CLASSA_NET);
+        else if (IN_CLASSB(ntohl(ipaddr.addr)))
+            netmask.addr = htonl(IN_CLASSB_NET);
+        else if (IN_CLASSC(ntohl(ipaddr.addr)))
+            netmask.addr = htonl(IN_CLASSC_NET);
+        else
+            tprintk("Strange IP %s, leaving netmask to 0.\n", ip);
+    }
+
+    if (gw_str) {
+        gw.addr = inet_addr(gw_str);
+        free(gw_str);
+    }
   }
   tprintk("IP %x netmask %x gateway %x.\n",
           ntohl(ipaddr.addr), ntohl(netmask.addr), ntohl(gw.addr));
diff --git a/netfront.c b/netfront.c
index 66f2bbc..58eb55e 100644
--- a/netfront.c
+++ b/netfront.c
@@ -71,6 +71,8 @@ struct netfront_dev_list {
     struct netfront_dev *dev;
     unsigned char rawmac[6];
     char *ip;
+    char *mask;
+    char *gw;
 
     int refcount;
 
@@ -81,7 +83,7 @@ static struct netfront_dev_list *dev_list = NULL;
 
 void init_rx_buffers(struct netfront_dev *dev);
 static struct netfront_dev *_init_netfront(struct netfront_dev *dev,
-                                           unsigned char rawmac[6], char **ip);
+                                           unsigned char rawmac[6], char **ip, 
char **mask, char **gw);
 static void _shutdown_netfront(struct netfront_dev *dev);
 void netfront_set_rx_handler(struct netfront_dev *dev,
                              void (*thenetif_rx)(unsigned char *data, int len,
@@ -301,7 +303,7 @@ struct netfront_dev *init_netfront(char *_nodename,
                                    void (*thenetif_rx)(unsigned char* data,
                                                        int len, void* arg),
                                    unsigned char rawmac[6],
-                                   char **ip)
+                                   char **ip, char **mask, char **gw)
 {
     char nodename[256];
     struct netfront_dev *dev;
@@ -344,7 +346,7 @@ struct netfront_dev *init_netfront(char *_nodename,
     ldev = malloc(sizeof(struct netfront_dev_list));
     memset(ldev, 0, sizeof(struct netfront_dev_list));
 
-    if (_init_netfront(dev, ldev->rawmac, &(ldev->ip))) {
+    if (_init_netfront(dev, ldev->rawmac, &(ldev->ip), &(ldev->mask), 
&(ldev->gw))) {
         ldev->dev = dev;
         ldev->refcount = 1;
         ldev->next = NULL;
@@ -374,6 +376,10 @@ out:
        }
     if (ip)
         *ip = strdup(ldev->ip);
+    if (mask)
+        *mask = strdup(ldev->mask);
+    if (gw)
+        *gw = strdup(ldev->gw);
 
 err:
     return dev;
@@ -381,7 +387,7 @@ err:
 
 static struct netfront_dev *_init_netfront(struct netfront_dev *dev,
                                           unsigned char rawmac[6],
-                                          char **ip)
+                                          char **ip, char **mask, char **gw)
 {
     xenbus_transaction_t xbt;
     char* err = NULL;
@@ -518,8 +524,26 @@ done:
         }
 
         if (ip) {
+            char *p;
+
             snprintf(path, sizeof(path), "%s/ip", dev->backend);
             xenbus_read(XBT_NIL, path, ip);
+
+            if (mask) {
+                p = strchr(*ip, ' ');
+                if (p) {
+                    *p++ = '\0';
+                    *mask = p;
+
+                    if (gw) {
+                        p = strchr(p, ' ');
+                        if (p) {
+                            *p++ = '\0';
+                            *gw = p;
+                        }
+                    }
+                }
+            }
         }
     }
 
@@ -676,7 +700,7 @@ void resume_netfront(void)
     struct netfront_dev_list *list;
 
     for (list = dev_list; list != NULL; list = list->next)
-        _init_netfront(list->dev, NULL, NULL);
+        _init_netfront(list->dev, NULL, NULL, NULL, NULL);
 }
 
 void init_rx_buffers(struct netfront_dev *dev)
-- 
2.20.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.