[klibc] [PATCH 2/3] Remove packet_peek and adjusted packet_rcv as well as process_receive_event.

Ulrich Dangel uli at spamt.net
Mon Mar 28 09:59:35 PDT 2011


packet_peek is not really necessary as it does basically nothing besides
testing if a packet is availabe. This is already done in packet_recv.
Adjusted do_pkt_recv to only use process_receive_event.

packet_recv was modified to only receive and use packets from the specified
interface.

process_receive_event was adjusted to handle discared/ignored packages from
the corresponding recv functions. As packet_recv could return 0 for
ignored packets process_receive_event handled them as processed/handled
packets resulting in an endless loop if new (non valid dhcp/bootp) packets
were received.

As a side effect ignored packets are now shown automatically in the debug
output.

Signed-off-by: Ulrich Dangel <uli at spamt.net>
---
 usr/kinit/ipconfig/main.c   |   24 +++++++++++++-----------
 usr/kinit/ipconfig/packet.c |   43 +++++++------------------------------------
 usr/kinit/ipconfig/packet.h |    1 -
 3 files changed, 20 insertions(+), 48 deletions(-)

diff --git a/usr/kinit/ipconfig/main.c b/usr/kinit/ipconfig/main.c
index 1e48083..43b6637 100644
--- a/usr/kinit/ipconfig/main.c
+++ b/usr/kinit/ipconfig/main.c
@@ -170,7 +170,7 @@ static void complete_device(struct netdev *dev)
 
 /*
  * Returns:
- *  0 = Not handled, the packet is still in the queue
+ *  0 = Not handled, try again later
  *  1 = Handled
  */
 static int process_receive_event(struct state *s, time_t now)
@@ -187,6 +187,9 @@ static int process_receive_event(struct state *s, time_t now)
 		case -1:
 			s->state = DEVST_ERROR;
 			break;
+		case 0:
+			handled = 0;
+			break;
 		case 1:
 			s->state = DEVST_COMPLETE;
 			dprintf("\n   bootp reply\n");
@@ -200,6 +203,9 @@ static int process_receive_event(struct state *s, time_t now)
 		case -1:
 			s->state = DEVST_ERROR;
 			break;
+		case 0:
+			handled = 0;
+			break;
 		case DHCPOFFER:	/* Offer received */
 			s->state = DEVST_DHCPREQ;
 			dhcp_send_request(s->dev);
@@ -213,6 +219,9 @@ static int process_receive_event(struct state *s, time_t now)
 		case -1:	/* error */
 			s->state = DEVST_ERROR;
 			break;
+		case 0:
+			handled = 0;
+			break;
 		case DHCPACK:	/* ACK received */
 			s->state = DEVST_COMPLETE;
 			break;
@@ -299,23 +308,16 @@ struct netdev *ifaces;
 
 /*
  * Returns:
- *  0 = Error, packet not received or discarded
+ *  0 = No dhcp/bootp packet was received
  *  1 = A packet was received and handled
  */
 static int do_pkt_recv(int pkt_fd, time_t now)
 {
-	int ret = 0;
+	int ret;
 	struct state *s;
 
 	for (s = slist; s; s = s->next) {
-		ret = packet_peek(s->dev);
-		if (ret) {
-			ret = process_receive_event(s, now);
-			if (ret == 0) {
-				packet_discard(s->dev);
-			}
-			break;
-		}
+		ret |= process_receive_event(s, now);
 	}
 	return ret;
 }
diff --git a/usr/kinit/ipconfig/packet.c b/usr/kinit/ipconfig/packet.c
index 993a2fa..ea5a25d 100644
--- a/usr/kinit/ipconfig/packet.c
+++ b/usr/kinit/ipconfig/packet.c
@@ -166,41 +166,6 @@ int packet_send(struct netdev *dev, struct iovec *iov, int iov_len)
 	return sendmsg(pkt_fd, &msg, 0);
 }
 
-/*
- * Fetches a bootp packet from specified device, but doesn't remove it.
- * Returns:
- *  0 = Error
- * >0 = A packet of size "ret" is available for interface ifindex
- */
-int packet_peek(struct netdev *dev)
-{
-	struct sockaddr_ll sll;
-	struct iphdr iph;
-	int ret, sllen = sizeof(struct sockaddr_ll);
-
-	sll.sll_ifindex = dev->ifindex;
-	/*
-	 * Peek at the IP header.
-	 */
-	ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
-		       MSG_PEEK, (struct sockaddr *)&sll, &sllen);
-	if (ret == -1)
-		return 0;
-
-	if (sll.sll_family != AF_PACKET)
-		goto discard_pkt;
-
-	if (iph.ihl < 5 || iph.version != IPVERSION)
-		goto discard_pkt;
-
-
-	return ret;
-
-discard_pkt:
-	packet_discard(dev);
-	return 0;
-}
-
 void packet_discard(struct netdev *dev)
 {
 	struct iphdr iph;
@@ -235,9 +200,15 @@ int packet_recv(struct netdev* dev, struct iovec *iov, int iov_len)
 		.msg_flags	= 0
 	};
 	int ret, iphl;
+	struct sockaddr_ll sll;
+	socklen_t sllen = sizeof(sll);
+
+	sll.sll_ifindex = dev->ifindex;
+	msg.msg_name = &sll;
+	msg.msg_namelen = sllen;
 
 	ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
-		       MSG_PEEK, NULL, NULL);
+		       MSG_PEEK, (struct sockaddr *)&sll, &sllen);
 	if (ret == -1)
 		return -1;
 
diff --git a/usr/kinit/ipconfig/packet.h b/usr/kinit/ipconfig/packet.h
index 524f393..f6cef52 100644
--- a/usr/kinit/ipconfig/packet.h
+++ b/usr/kinit/ipconfig/packet.h
@@ -6,7 +6,6 @@ struct iovec;
 int packet_open(void);
 void packet_close(void);
 int packet_send(struct netdev *dev, struct iovec *iov, int iov_len);
-int packet_peek(struct netdev *dev);
 void packet_discard(struct netdev *dev);
 int packet_recv(struct netdev *dev, struct iovec *iov, int iov_len);
 
-- 
1.7.1



More information about the klibc mailing list