Index: Makefile
===================================================================
--- Makefile	(revision a93ad0d94248cf0320bcfebd9ff952cbb8cde6a5)
+++ Makefile	(revision 519ab52e6461ec4e80487ea5eeb9b3f93b728142)
@@ -1,4 +1,4 @@
 CC=g++
-CFLAGS = -g 
+CFLAGS = -g -DLINUX
 # uncomment this for SunOS
 # LIBS = -lsocket -lnsl
@@ -6,10 +6,12 @@
 all: netslow
 
-netslow: netslow.o NetFlow9Processor.o 
-	$(CC) -o netslow netslow.o NetFlow9Processor.o  $(LIBS)
+netslow: netslow.o NetFlow9Processor.o NetFlowDataTemplateCache.o NetFlowDataTemplate.o
+	$(CC) -o netslow netslow.o NetFlow9Processor.o NetFlowDataTemplateCache.o $(LIBS)
 
 
 netslow.o: netslow.cpp port.h
 NetFlow9Processor.o: NetFlow9Processor.cpp
+NetFlowDataTemplateCache.o: NetFlowDataTemplateCache.cpp
+NetFlowDataTemplate.o: NetFlowDataTemplate.cpp
 
 clean:
Index: NetFlow9Processor.cpp
===================================================================
--- NetFlow9Processor.cpp	(revision a93ad0d94248cf0320bcfebd9ff952cbb8cde6a5)
+++ NetFlow9Processor.cpp	(revision 519ab52e6461ec4e80487ea5eeb9b3f93b728142)
@@ -1,13 +1,129 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 #include "NetFlow9Processor.h"
 
+unsigned int NetFlow9Processor::ByteTouInt32(unsigned char b[], int base) {
+	//TODO: Check for out of range muppets
+	//TODO: Endianess issues?
+	return (b[base] << 24) + (b[base+1] << 16) + (b[base+2] << 8) + b[base+3];
+}
+
+unsigned short NetFlow9Processor::ByteTouInt16(unsigned char b[], int base) {
+	//TODO: Check for out of range muppets
+	//TODO: Endianess issues?
+	return (b[base] << 8) + b[base+1];
+}
+
+
+NetFlow9Processor::NetFlow9Processor(void) {
+	dataTemplateCache = new NetFlowDataTemplateCache();
+}
+
 int NetFlow9Processor::ProcessPacket(unsigned char b[], int l) {
-	if ( l < 2) {
-		return 255;
+
+	int nextIndex = 0;					// Next byte in the buffer
+	int version = 0;					// NetFlow version
+	int count = 0;						// Records in the packet
+	unsigned short flowSetID = 0;		// ID of the current FlowSet
+	unsigned short flowSetLength = 0;	// Bytes in the current FlowSet
+	unsigned int uptime = 0;			// Router uptime in miliseconds
+	unsigned int packetTime = 0;		// Time sent by router (Unix seconds)
+	unsigned int sequenceID = 0;		// Incremented by one for each packet sent
+	unsigned int sourceID = 0;			// Locally significant source ID
+
+	if ( l < 24) {
+		//Packet is too short to contain a header
+		return 254;
 	}
-	int version = (b[0] << 8 ) + b [1];
+	
+	//First 2 bytes (0 and 1) are the version
+	version = (b[0] << 8 ) + b [1];
 	if (version != 9) {
+			//Not a NetFlow v9 packet
 			return 253;
 	}
-	int count;
-	return version;
+
+	//Bytes 2 and 3 are the count of records in the packet
+	count = (b[2] << 8 ) + b [3];
+
+	if (count < 1) {
+			//Not a NetFlow v9 packet
+			return 252;
+	}
+
+	nextIndex = 4;
+	uptime = ByteTouInt32(b,nextIndex);
+	nextIndex += 4;
+	packetTime = ByteTouInt32(b,nextIndex);
+	nextIndex += 4;
+	sequenceID = ByteTouInt32(b,nextIndex);
+	nextIndex += 4;
+	sourceID = ByteTouInt32(b,nextIndex);
+	nextIndex += 4;
+
+	do {
+		flowSetID = ByteTouInt16(b,nextIndex);
+		flowSetLength = ByteTouInt16(b,nextIndex+2);
+		if (nextIndex + flowSetLength > l) {
+			//Invalid/unsupported packet
+			return 251;
+		}
+		if (flowSetID > 255) {
+			//DATA FlowSet
+			//printf("  -> Found data flowSetID %d, length %d.\n", flowSetID, flowSetLength);
+			//processDataFlowSet(b,nextIndex,flowSetID,flowSetLength);
+		}
+		else if (flowSetID == 0) {
+			//TEMPLATE FlowSet
+			printf("  -> Found template flowSetID %d, length %d.\n", flowSetID, flowSetLength);
+			processTemplateFlowSet(b,nextIndex,flowSetLength);
+		}
+		else if (flowSetID == 1) {
+			//OPTIONS FlowSet
+			printf("  -> Found options flowSetID %d, length %d.\n", flowSetID, flowSetLength);
+		}
+		else {
+			//Invalid/unsupported packet
+			return 250;
+		}
+		nextIndex += flowSetLength;
+	}
+	while ( nextIndex < l);
+
+	return sequenceID;
 }
+
+void NetFlow9Processor::processDataFlowSet(unsigned char b[], int nextIndex, unsigned short flowSetID, unsigned short flowSetLength) {
+
+}
+void NetFlow9Processor::processTemplateFlowSet(unsigned char b[], int startIndex, unsigned short flowSetLength) {
+
+	int nextIndex = startIndex + 4;				// Next place to look (skip 4 header bytes already parsed)
+	int endIndex = startIndex + flowSetLength ; // End the loop at the end of the template
+	unsigned short count = 0;					// Count of fields to extract
+	unsigned short id = 0;						// ID the template applies to	
+
+	do {
+
+		//Lets get the Template ID and the count of fields
+		id = ByteTouInt16(b,nextIndex);
+		count = ByteTouInt16(b,nextIndex+2);
+
+		printf("  -> Template ID %d\n", id);
+	
+		//Increment nextIndex to skip the 4 bytes of the header already parsed
+		nextIndex += 4;
+
+		int i;
+
+		for (i=0; i < count; i++) {
+			unsigned short field = ByteTouInt16(b,nextIndex);
+			unsigned short length = ByteTouInt16(b,nextIndex+2);
+			printf("    -> Field %d. Field Code %d, length %d\n", i, field, length );
+			nextIndex += 4;
+		}
+	}
+	while ( nextIndex < endIndex );
+
+}
Index: NetFlow9Processor.h
===================================================================
--- NetFlow9Processor.h	(revision a93ad0d94248cf0320bcfebd9ff952cbb8cde6a5)
+++ NetFlow9Processor.h	(revision 519ab52e6461ec4e80487ea5eeb9b3f93b728142)
@@ -1,5 +1,20 @@
+#include "NetFlowDataTemplateCache.h"
+
 class NetFlow9Processor{
 
+	private:
+
+		NetFlowDataTemplateCache *dataTemplateCache;
+		NetFlowDataTemplate::DataTemplateItem_t *strtest;
+
+		unsigned int ByteTouInt32(unsigned char b[], int base);
+		unsigned short ByteTouInt16(unsigned char b[], int base);
+		void processDataFlowSet(unsigned char b[], int nextIndex, unsigned short flowSetID, unsigned short flowSetLength);
+		void processTemplateFlowSet(unsigned char b[], int nextIndex, unsigned short flowSetLength);
+
+
 	public:
+
+		NetFlow9Processor();
 		int ProcessPacket(unsigned char b[], int l);
 
Index: NetFlowDataTemplate.h
===================================================================
--- NetFlowDataTemplate.h	(revision a93ad0d94248cf0320bcfebd9ff952cbb8cde6a5)
+++ NetFlowDataTemplate.h	(revision 519ab52e6461ec4e80487ea5eeb9b3f93b728142)
@@ -2,4 +2,7 @@
 
 class NetFlowDataTemplate {
+
+
+	public:
 
 	typedef struct DataTemplateItem DataTemplateItem_t;
Index: netslow.cpp
===================================================================
--- netslow.cpp	(revision a93ad0d94248cf0320bcfebd9ff952cbb8cde6a5)
+++ netslow.cpp	(revision 519ab52e6461ec4e80487ea5eeb9b3f93b728142)
@@ -48,7 +48,7 @@
 	//received.
 	for (;;) {
-		printf("waiting on port %d\n", SERVICE_PORT);
+//		printf("waiting on port %d\n", SERVICE_PORT);
 		recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
-		printf("received %d bytes\n", recvlen);
+//		printf("received %d bytes\n", recvlen);
 		if (recvlen > 2) {
 			int version = (buf[0] << 8 ) + buf [1];
@@ -56,5 +56,5 @@
 				case 9:
 					//NFv9
-					printf("Len: %d...  Running NetFlow9Packet Processor\n", recvlen);
+//					printf("Len: %d...  Running NetFlow9Packet Processor\n", recvlen);
 					retval = NetFlow9->ProcessPacket(buf,recvlen);
 					break;
