[klibc] [PATCH 1/6] ipconfig: Write $PROTO as configuration protocol to /tmp/net-$DEVICE.conf

KUMAAN 9maaan at gmail.com
Mon Aug 22 22:51:22 PDT 2011


This patch writes $PROTO as what protocol ipconfig used to configure
$DEVICE to /tmp/net-$DEVICE.conf file.

For example, $PROTO is either 'boop', 'dhcp', or 'none'.

Later, other scripts which source the file can known the protocol clearly.

Signed-off-by: KUMAAN <9maaan at gmail.com>
---
 usr/kinit/ipconfig/main.c   |   31 +++++++++++++++++++++++++++----
 usr/kinit/ipconfig/netdev.h |    1 +
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/usr/kinit/ipconfig/main.c b/usr/kinit/ipconfig/main.c
index 8782ae7..5945b29 100644
--- a/usr/kinit/ipconfig/main.c
+++ b/usr/kinit/ipconfig/main.c
@@ -44,6 +44,20 @@ struct state {
 	struct state *next;
 };
 
+/* #define PROTO_x : for uint8_t proto of struct netdev */
+struct protoinfo {
+	char *name;
+} protoinfos[] = {
+#define PROTO_NONE  0
+	{"none"},
+#define PROTO_BOOTP 1
+	{"bootp"},
+#define PROTO_DHCP  2
+	{"dhcp"},
+#define PROTO_RARP  3
+	{"rarp"}
+};
+
 static inline const char *my_inet_ntoa(uint32_t addr)
 {
 	struct in_addr a;
@@ -55,9 +69,13 @@ static inline const char *my_inet_ntoa(uint32_t addr)
 
 static void print_device_config(struct netdev *dev)
 {
-	printf("IP-Config: %s complete (from %s):\n", dev->name,
-	       my_inet_ntoa(dev->serverid ? dev->serverid : dev->ip_server));
-	printf(" address: %-16s ", my_inet_ntoa(dev->ip_addr));
+	printf("IP-Config: %s complete", dev->name);
+	if (dev->proto == PROTO_BOOTP || dev->proto == PROTO_DHCP)
+		printf(" (%s from %s)", protoinfos[dev->proto].name,
+		       my_inet_ntoa(dev->serverid ?
+				    dev->serverid : dev->ip_server));
+
+	printf(":\n address: %-16s ", my_inet_ntoa(dev->ip_addr));
 	printf("broadcast: %-16s ", my_inet_ntoa(dev->ip_broadcast));
 	printf("netmask: %-16s\n", my_inet_ntoa(dev->ip_netmask));
 	printf(" gateway: %-16s ", my_inet_ntoa(dev->ip_gateway));
@@ -128,6 +146,7 @@ static void dump_device_config(struct netdev *dev)
 	f = fopen(fn, "w");
 	if (f) {
 		write_option(f, "DEVICE", dev->name);
+		write_option(f, "PROTO", protoinfos[dev->proto].name);
 		write_option(f, "IPV4ADDR",
 				my_inet_ntoa(dev->ip_addr));
 		write_option(f, "IPV4BROADCAST",
@@ -217,6 +236,7 @@ static int process_receive_event(struct state *s, time_t now)
 			break;
 		case 1:
 			s->state = DEVST_COMPLETE;
+			s->dev->proto = PROTO_BOOTP;
 			dprintf("\n   bootp reply\n");
 			break;
 		}
@@ -249,6 +269,7 @@ static int process_receive_event(struct state *s, time_t now)
 			break;
 		case DHCPACK:	/* ACK received */
 			s->state = DEVST_COMPLETE;
+			s->dev->proto = PROTO_DHCP;
 			break;
 		case DHCPNAK:	/* NAK received */
 			s->state = DEVST_DHCPDISC;
@@ -584,8 +605,10 @@ static void bringup_device(struct netdev *dev)
 	if (netdev_up(dev) == 0) {
 		if (dev->caps)
 			add_one_dev(dev);
-		else
+		else {
+			dev->proto = PROTO_NONE;
 			complete_device(dev);
+		}
 	}
 }
 
diff --git a/usr/kinit/ipconfig/netdev.h b/usr/kinit/ipconfig/netdev.h
index 26d076a..eaf04dc 100644
--- a/usr/kinit/ipconfig/netdev.h
+++ b/usr/kinit/ipconfig/netdev.h
@@ -28,6 +28,7 @@ struct netdev {
 		int fd;
 	} rarp;
 
+	uint8_t proto;          /* a protocol used (e.g. PROTO_DHCP) */
 	uint32_t ip_addr;	/* my address           */
 	uint32_t ip_broadcast;	/* broadcast address    */
 	uint32_t ip_server;	/* server address       */
-- 
1.7.2.5




More information about the klibc mailing list