diff mbox

[03/16] kvm tools: Add helper to tell if a UDP package is a DHCP package

Message ID 1310893024-21615-4-git-send-email-asias.hejun@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Asias He July 17, 2011, 8:56 a.m. UTC
This patch checks:

   - sport and dport

   - magic cookie

to detemine whether a UDP package is a DHCP package.

Signed-off-by: Asias He <asias.hejun@gmail.com>
---
 tools/kvm/Makefile          |    1 +
 tools/kvm/include/kvm/uip.h |    4 ++++
 tools/kvm/net/uip/dhcp.c    |   17 +++++++++++++++++
 3 files changed, 22 insertions(+), 0 deletions(-)
 create mode 100644 tools/kvm/net/uip/dhcp.c
diff mbox

Patch

diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile
index 5d04377..b5c5516 100644
--- a/tools/kvm/Makefile
+++ b/tools/kvm/Makefile
@@ -61,6 +61,7 @@  OBJS	+= net/uip/tcp.o
 OBJS	+= net/uip/udp.o
 OBJS	+= net/uip/buf.o
 OBJS	+= net/uip/csum.o
+OBJS	+= net/uip/dhcp.o
 OBJS	+= kvm-cmd.o
 OBJS	+= mptable.o
 OBJS	+= rbtree.o
diff --git a/tools/kvm/include/kvm/uip.h b/tools/kvm/include/kvm/uip.h
index c414267..7c84dea 100644
--- a/tools/kvm/include/kvm/uip.h
+++ b/tools/kvm/include/kvm/uip.h
@@ -30,9 +30,12 @@ 
 #define UIP_TCP_FLAG_URG	32
 
 #define UIP_DHCP_VENDOR_SPECIFIC_LEN	312
+#define UIP_DHCP_PORT_SERVER		67
+#define UIP_DHCP_PORT_CLIENT		68
 #define UIP_DHCP_MACPAD_LEN		10
 #define UIP_DHCP_HOSTNAME_LEN		64
 #define UIP_DHCP_FILENAME_LEN		128
+#define UIP_DHCP_MAGIC_COOKIE		0x63825363
 #define UIP_DHCP_MAGIC_COOKIE_LEN	4
 #define UIP_DHCP_OPTION_LEN		(UIP_DHCP_VENDOR_SPECIFIC_LEN - UIP_DHCP_MAGIC_COOKIE_LEN)
 /*
@@ -317,4 +320,5 @@  struct uip_buf *uip_buf_get_free(struct uip_info *info);
 struct uip_buf *uip_buf_clone(struct uip_tx_arg *arg);
 
 int uip_udp_make_pkg(struct uip_info *info, struct uip_udp_socket *sk, struct uip_buf *buf, u8 *payload, int payload_len);
+bool uip_udp_is_dhcp(struct uip_udp *udp);
 #endif /* KVM__UIP_H */
diff --git a/tools/kvm/net/uip/dhcp.c b/tools/kvm/net/uip/dhcp.c
new file mode 100644
index 0000000..af0407f
--- /dev/null
+++ b/tools/kvm/net/uip/dhcp.c
@@ -0,0 +1,17 @@ 
+#include "kvm/uip.h"
+
+bool uip_udp_is_dhcp(struct uip_udp *udp)
+{
+	struct uip_dhcp *dhcp;
+
+	if (ntohs(udp->sport) != UIP_DHCP_PORT_CLIENT ||
+	    ntohs(udp->dport) != UIP_DHCP_PORT_SERVER)
+		return false;
+
+	dhcp = (struct uip_dhcp *)udp;
+
+	if (ntohl(dhcp->magic_cookie) != UIP_DHCP_MAGIC_COOKIE)
+		return false;
+
+	return true;
+}