Browse Source

it's alive!

Joseph Henry 10 years ago
parent
commit
11731af45a
4 changed files with 63 additions and 20 deletions
  1. 1 1
      make-linux.mk
  2. 3 0
      netcon/LWIPStack.hpp
  3. 35 15
      netcon/NetconEthernetTap.cpp
  4. 24 4
      netcon/NetconEthernetTap.hpp

+ 1 - 1
make-linux.mk

@@ -111,7 +111,7 @@ installer: one FORCE
 	./ext/installfiles/linux/buildinstaller.sh
 	./ext/installfiles/linux/buildinstaller.sh
 
 
 clean:
 clean:
-	rm -rf *.o node/*.o controller/*.o osdep/*.o service/*.o ext/http-parser/*.o ext/lz4/*.o ext/json-parser/*.o zerotier-one zerotier-idtool zerotier-cli zerotier-selftest build-* ZeroTierOneInstaller-* *.deb *.rpm
+	rm -rf *.o netcon/*.o node/*.o controller/*.o osdep/*.o service/*.o ext/http-parser/*.o ext/lz4/*.o ext/json-parser/*.o zerotier-one zerotier-idtool zerotier-cli zerotier-selftest build-* ZeroTierOneInstaller-* *.deb *.rpm
 
 
 debug:	FORCE
 debug:	FORCE
 	make ZT_DEBUG=1 one
 	make ZT_DEBUG=1 one

+ 3 - 0
netcon/LWIPStack.hpp

@@ -95,6 +95,7 @@ typedef ip_addr ip_addr_t;
 #define NETIF_SET_DEFAULT_SIG struct netif *netif
 #define NETIF_SET_DEFAULT_SIG struct netif *netif
 #define NETIF_ADD_SIG struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input
 #define NETIF_ADD_SIG struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input
 #define NETIF_SET_UP_SIG struct netif *netif
 #define NETIF_SET_UP_SIG struct netif *netif
+#define NETIF_POLL_SIG struct netif *netif
 
 
 
 
 
 
@@ -138,6 +139,7 @@ public:
   void (*netif_set_default)(NETIF_SET_DEFAULT_SIG);
   void (*netif_set_default)(NETIF_SET_DEFAULT_SIG);
   struct netif * (*netif_add)(NETIF_ADD_SIG);
   struct netif * (*netif_add)(NETIF_ADD_SIG);
   void (*netif_set_up)(NETIF_SET_UP_SIG);
   void (*netif_set_up)(NETIF_SET_UP_SIG);
+  void (*netif_poll)(NETIF_POLL_SIG);
 
 
 
 
 
 
@@ -184,6 +186,7 @@ public:
     netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))dlsym(libref, "netif_set_default");
     netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))dlsym(libref, "netif_set_default");
     netif_add = (struct netif*(*)(NETIF_ADD_SIG))dlsym(libref, "netif_add");
     netif_add = (struct netif*(*)(NETIF_ADD_SIG))dlsym(libref, "netif_add");
     netif_set_up = (void(*)(NETIF_SET_UP_SIG))dlsym(libref, "netif_set_up");
     netif_set_up = (void(*)(NETIF_SET_UP_SIG))dlsym(libref, "netif_set_up");
+    netif_poll = (void(*)(NETIF_POLL_SIG))dlsym(libref, "netif_poll");
   }
   }
 };
 };
 
 

+ 35 - 15
netcon/NetconEthernetTap.cpp

@@ -173,35 +173,48 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
 		// Copy data into a pbuf chain
 		// Copy data into a pbuf chain
 		struct pbuf *p, *q;
 		struct pbuf *p, *q;
 	  //u16_t len;
 	  //u16_t len;
-	  char buf[1514];
-	  char *bufptr;
+	  //char buf[1514];
+	  const char *bufptr;
+		// Assemble ethernet header and call netif->output
+		struct eth_hdr *ethhdr = NULL;
 
 
 		// We allocate a pbuf chain of pbufs from the pool.
 		// We allocate a pbuf chain of pbufs from the pool.
-		p = lwipstack->pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
+		p = lwipstack->pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
 
 
 		if(p != NULL) {
 		if(p != NULL) {
+			fprintf(stderr, "p != NULL\n");
 			/* We iterate over the pbuf chain until we have read the entire
 			/* We iterate over the pbuf chain until we have read the entire
 				 packet into the pbuf. */
 				 packet into the pbuf. */
-			bufptr = &buf[0];
+			bufptr = (const char *)data;
 			for(q = p; q != NULL; q = q->next) {
 			for(q = p; q != NULL; q = q->next) {
 				/* Read enough bytes to fill this pbuf in the chain. The
 				/* Read enough bytes to fill this pbuf in the chain. The
 					 available data in the pbuf is given by the q->len
 					 available data in the pbuf is given by the q->len
 					 variable. */
 					 variable. */
 				/* read data into(q->payload, q->len); */
 				/* read data into(q->payload, q->len); */
-				memcpy(q->payload, bufptr, q->len);
-				bufptr += q->len;
+				char *pload = (char*)q->payload;
+				int plen = q->len;
+				if (!ethhdr) {
+					ethhdr = (struct eth_hdr *)p->payload;
+					pload += sizeof(struct eth_hdr);
+					plen -= sizeof(struct eth_hdr);
+				}
+				memcpy(pload, bufptr, plen);
+				bufptr += plen;
 			}
 			}
 			/* acknowledge that packet has been read(); */
 			/* acknowledge that packet has been read(); */
 		} else {
 		} else {
+			return;
 			/* drop packet(); */
 			/* drop packet(); */
 		}
 		}
 
 
-		// Assemble ethernet header and call netif->output
-		struct eth_hdr *ethhdr;
-		ethhdr = (struct eth_hdr *)p->payload;
 		from.copyTo(ethhdr->src.addr, 6);
 		from.copyTo(ethhdr->src.addr, 6);
 		_mac.copyTo(ethhdr->dest.addr, 6);
 		_mac.copyTo(ethhdr->dest.addr, 6);
-		ethhdr->type = ZT_ETHERTYPE_IPV4;
+		ethhdr->type = Utils::hton((uint16_t)etherType);
+
+		fprintf(stderr, "from = %s\n", from.toString().c_str());
+		fprintf(stderr, "_mac = %s\n", _mac.toString().c_str());
+		fprintf(stderr, "ethhdr->type = %x\n", ethhdr->type);
+		fprintf(stderr, "ethhdr->type = %x\n", ethhdr->type);
 
 
 		if(interface.input(p, &interface) != ERR_OK) {
 		if(interface.input(p, &interface) != ERR_OK) {
 			fprintf(stderr, "IP error (netif->input)\n");
 			fprintf(stderr, "IP error (netif->input)\n");
@@ -351,14 +364,20 @@ void NetconEthernetTap::threadMain()
 	lwipstack->netif_add(&interface,&ipaddr, &netmask, &gw, NULL, tapif_init, lwipstack->ethernet_input);
 	lwipstack->netif_add(&interface,&ipaddr, &netmask, &gw, NULL, tapif_init, lwipstack->ethernet_input);
 
 
   interface.state = this;
   interface.state = this;
+	interface.output = lwipstack->etharp_output;
+  _mac.copyTo(interface.hwaddr, 6);
+	interface.mtu = 2800;
+
+	/*
 	interface.name[0] = 't';
 	interface.name[0] = 't';
   interface.name[1] = 'p';
   interface.name[1] = 'p';
   interface.output = lwipstack->etharp_output;
   interface.output = lwipstack->etharp_output;
   interface.linkoutput = low_level_output;
   interface.linkoutput = low_level_output;
-  interface.mtu = 1500;
+
   interface.hwaddr_len = 6;
   interface.hwaddr_len = 6;
-	_mac.copyTo(interface.hwaddr, 6);
+
   interface.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
   interface.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
+	*/
 
 
 	fprintf(stderr, "netif_set_default\n");
 	fprintf(stderr, "netif_set_default\n");
   lwipstack->netif_set_default(&interface);
   lwipstack->netif_set_default(&interface);
@@ -377,16 +396,17 @@ void NetconEthernetTap::threadMain()
 	  if(since_tcp > tcp_time)
 	  if(since_tcp > tcp_time)
 	  {
 	  {
 	    prev_tcp_time = curr_time+1;
 	    prev_tcp_time = curr_time+1;
-			fprintf(stderr, "tcp_tmr\n");
+			//fprintf(stderr, "tcp_tmr\n");
 	    lwipstack->tcp_tmr();
 	    lwipstack->tcp_tmr();
 	  }
 	  }
 		if(since_etharp > etharp_time)
 		if(since_etharp > etharp_time)
 		{
 		{
 			prev_etharp_time = curr_time;
 			prev_etharp_time = curr_time;
-			fprintf(stderr, "etharp_tmr\n");
+			//fprintf(stderr, "etharp_tmr\n");
 			lwipstack->etharp_tmr();
 			lwipstack->etharp_tmr();
 		}
 		}
-		fprintf(stderr, "_run\n");
+		//fprintf(stderr, "_run\n");
+		//lwipstack->netif_poll(&interface);
 		_phy.poll(100); // conversion from usec to millisec, TODO: double check
 		_phy.poll(100); // conversion from usec to millisec, TODO: double check
 
 
 	}
 	}

+ 24 - 4
netcon/NetconEthernetTap.hpp

@@ -186,6 +186,9 @@ private:
 ------------------------ low-level Interface functions -------------------------
 ------------------------ low-level Interface functions -------------------------
 ------------------------------------------------------------------------------*/
 ------------------------------------------------------------------------------*/
 
 
+static err_t low_level_output(struct netif *netif, struct pbuf *p);
+
+
   static void low_level_init(struct netif *netif)
   static void low_level_init(struct netif *netif)
   {
   {
     fprintf(stderr, "low_level_init\n");
     fprintf(stderr, "low_level_init\n");
@@ -193,14 +196,25 @@ private:
 
 
   static err_t tapif_init(struct netif *netif)
   static err_t tapif_init(struct netif *netif)
   {
   {
-    fprintf(stderr, "tapif_init\n");
+    //netif->state = tapif;
+    netif->name[0] = 't';
+    netif->name[1] = 'p';
+    //netif->output = netif->state.lwipstack->etharp_output;
+    netif->linkoutput = low_level_output;
+    //netif->mtu = 1500;
+    /* hardware address length */
+    netif->hwaddr_len = 6;
+
+    //tapif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
+    netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
+    //low_level_init(netif);
     return ERR_OK;
     return ERR_OK;
   }
   }
 
 
   static err_t low_level_output(struct netif *netif, struct pbuf *p)
   static err_t low_level_output(struct netif *netif, struct pbuf *p)
   {
   {
     struct pbuf *q;
     struct pbuf *q;
-    char buf[1514];
+    char buf[2800+14];
     char *bufptr;
     char *bufptr;
     //struct tapif *tapif;
     //struct tapif *tapif;
     int tot_len = 0;
     int tot_len = 0;
@@ -233,8 +247,14 @@ private:
     src_mac.setTo(ethhdr->src.addr, 6);
     src_mac.setTo(ethhdr->src.addr, 6);
     dest_mac.setTo(ethhdr->dest.addr, 6);
     dest_mac.setTo(ethhdr->dest.addr, 6);
 
 
-    tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,ZT_ETHERTYPE_IPV4,0,buf,p->tot_len);
-    fprintf(stderr, "low_level_output(%d)\n", tap->_nwid);
+    tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),p->tot_len);
+    fprintf(stderr, "ethhdr->type = %x\n", ethhdr->type);
+    fprintf(stderr, "p->tot_len = %x\n", p->tot_len);
+    fprintf(stderr, "src_mac = %s\n", src_mac.toString().c_str());
+    fprintf(stderr, "dest_mac = %s\n", dest_mac.toString().c_str());
+
+    //fprintf(stderr, "htons(ethhdr->type) = %x\n", Utils::hton((uint16_t)ethhdr->type));
+    fprintf(stderr, "low_level_output(%x)\n", tap->_nwid);
 
 
     return ERR_OK;
     return ERR_OK;
   }
   }