summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--Makefile.common10
-rw-r--r--net/Makefile23
-rw-r--r--net/arp.c1
-rw-r--r--net/ipv4.c2
-rw-r--r--net/main.c90
-rw-r--r--net/net_sys.h4
-rw-r--r--net/net_sys_linux.h2
-rw-r--r--net/netdev.c1
-rw-r--r--net/ntp.c28
10 files changed, 146 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index 8543c84..3ea6daa 100644
--- a/Makefile
+++ b/Makefile
@@ -290,10 +290,3 @@ create_bridged:
sudo ip link set dev br0 up
sudo ip link set dev tap0 up
sudo dhclient
-
-
-
-
-
-
-
diff --git a/Makefile.common b/Makefile.common
index 2dd538c..d4e45c3 100644
--- a/Makefile.common
+++ b/Makefile.common
@@ -1,10 +1,10 @@
### explicit rules ###
%.o: %.asm
- @echo "Assembling (nasm) $@..."; nasm -f elf $*.asm -o $@ -f elf -F dwarf -g
-#%.o: %.s
-# i686-elf-as $*.s -o $@
+ @echo "ASM $@ ..."; nasm -f elf $*.asm -o $@ -f elf -F dwarf -g
%.o: %.c
- $(CC) -c $(CFLAGS) $*.c -o $*.o
- $(CC) -MM -MT $*.o $(CFLAGS) $*.c > $*.d
+ @echo "CC $@ ..."; $(CC) -c $(CFLAGS) $*.c -o $*.o
+
+%.d: %.c
+ @echo "DEP $@ ..."; $(CC) -MM -MT $*.o $(CFLAGS) $*.c > $*.d
diff --git a/net/Makefile b/net/Makefile
index 979a771..85cf348 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -3,6 +3,8 @@
# Fool Stack #
##############
+EXE=fool-stack
+
############ compiler ############
#use our cross compiler
@@ -22,6 +24,7 @@ AS=as
#https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
CFLAGS=
+CFLAGS+=-DPLATFORM_LINUX
#CFLAGS+=-fvar-tracking
#CFLAGS+=-DGIT_REVISION=\"$(GIT_REVISION)\"
#CFLAGS+=-ffreestanding # do we need this if using own compiler?
@@ -85,15 +88,25 @@ OBJECTS=$(patsubst %.c, %.o, $(SOURCES))
#derive and include deps
DEPS=$(patsubst %.c, %.d, $(SOURCES))
+# default target
+new: clean tags all
+
+-include ../Makefile.common
-include $(DEPS)
-new: clean compile
+run: all
+ ./$(EXE)
+
+all: $(OBJECTS)
+ @echo "Compiling and linking objects ..."
+ $(CC) *.o -o $(EXE)
+
+deps: $(DEPS)
+ @echo "Generating deps ..."
-compile: $(OBJECTS) $(DEPS)
tags:
- @echo "Generating ctags..."; ctags --recurse=yes .
+ @echo "Generating ctags .."; ctags --recurse=yes .
clean:
- @echo "Cleaning..."; rm -f *.d *.o tags
-
+ @echo "Cleaning up..."; rm -f *.d *.o tags $(EXE)
diff --git a/net/arp.c b/net/arp.c
index bfcf015..a06ee56 100644
--- a/net/arp.c
+++ b/net/arp.c
@@ -105,6 +105,7 @@ void arp_incoming(struct netdev *netdev, struct eth_hdr *hdr)
arpdata = (struct arp_ipv4 *) arphdr->data;
// merge = update_arp_translation_table(arphdr, arpdata);
+ klog("incoming arp\n");
if (netdev->ip != arpdata->dip) {
klog("ARP was not for us\n");
diff --git a/net/ipv4.c b/net/ipv4.c
index 78db274..9f010c7 100644
--- a/net/ipv4.c
+++ b/net/ipv4.c
@@ -8,7 +8,7 @@
bool ipv4_incoming(struct netdev *dev,struct eth_hdr *hdr)
{
struct ipv4_hdr *ipv4=hdr->payload;
- //klog("ipv4 incoming with checksum: 0x%04X",ntohs(ipv4->csum));
+ klog("ipv4 incoming with checksum: 0x%04X",ntohs(ipv4->csum));
ipv4->csum=0;
// klog("expected checksum: 0x%04X",checksum(ipv4,ntohs(ipv4->len)));
// klog("ipv4 header len=%d",ipv4->ihl);
diff --git a/net/main.c b/net/main.c
new file mode 100644
index 0000000..339f037
--- /dev/null
+++ b/net/main.c
@@ -0,0 +1,90 @@
+//http://www.saminiir.com/lets-code-tcp-ip-stack-1-ethernet-arp/
+//https://openvpn.net/tuntap
+
+#ifdef PLATFORM_LINUX
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <errno.h>
+#include <string.h>
+#include <linux/if_tun.h>
+#include <linux/if.h>
+
+#include "netdev.h"
+
+int fd;
+
+int tap_transmit(const void * p_data, uint16_t p_len)
+{
+ write(fd,p_data,p_len);
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int err;
+ char dev[]="tap0";
+ struct ifreq ifr;
+
+ if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) {
+ printf("Cannot open TUN/TAP dev\n");
+ exit(1);
+ }
+
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
+ strncpy(ifr.ifr_name, dev,IFNAMSIZ);
+
+ if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
+ printf("ioctl failed for TAP\n");
+ close(fd);
+ return err;
+ }
+
+
+ struct netdev net_dev;
+
+
+ uint8_t mac[6];
+ mac[0]=0x7a;
+ mac[1]=0xa4;
+ mac[2]=0x7b;
+ mac[3]=0x72;
+ mac[4]=0x56;
+ mac[5]=0xa2;
+
+ memcpy(net_dev.hwaddr,mac,6); // use mac obtained from device
+ net_dev.ip=(192<<0)+(168<<8)+(0<<16)+(20<<24); // 192.168.0.20 // TODO: not hardcode!
+ net_dev.transmit=tap_transmit;
+
+ uint8_t buf[1024];
+
+ while(1)
+ {
+ uint32_t len=read(fd,buf,1024);
+
+ if(len==-1)
+ {
+ printf("read error: %s \n",strerror(errno));
+ }
+
+ printf("read %i bytes\n",len);
+ net_incoming(&net_dev,buf);
+ }
+
+ return 0;
+}
+
+
+#endif
+
+
diff --git a/net/net_sys.h b/net/net_sys.h
index 3539bfc..507b334 100644
--- a/net/net_sys.h
+++ b/net/net_sys.h
@@ -17,6 +17,8 @@
#ifdef FOOLOS_KERNEL
#include "net_sys_foolos_kernel.h"
-#else
+#endif
+
+#ifdef PLATFORM_LINUX
#include "net_sys_linux.h"
#endif
diff --git a/net/net_sys_linux.h b/net/net_sys_linux.h
index 2f6b62a..d2ef179 100644
--- a/net/net_sys_linux.h
+++ b/net/net_sys_linux.h
@@ -12,6 +12,6 @@
//#define klog(...) log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__), 10, LOG_LABEL_INFO __VA_ARGS__)
#define S1(x) #x
#define S2(x) S1(x)
-#define klog(...) printf(__FILE__ ":" S2(__LINE__) __VA_ARGS__)
+#define klog(...) printf(__FILE__ ":" S2(__LINE__) __VA_ARGS__);
#define kbfree(x) free(x);
#define kballoc(x) malloc(x*4096);
diff --git a/net/netdev.c b/net/netdev.c
index 863661d..1177891 100644
--- a/net/netdev.c
+++ b/net/netdev.c
@@ -10,6 +10,7 @@
void net_incoming(struct netdev *netdev, struct eth_hdr *hdr)
{
+ klog("frame incoming: type [0x%04x]",hdr->ethertype);
if(hdr->ethertype==htons(ETH_P_ARP))arp_incoming(netdev,hdr);
if(hdr->ethertype==htons(ETH_P_IPV4))ipv4_incoming(netdev,hdr);
}
diff --git a/net/ntp.c b/net/ntp.c
index 0e9b19d..9c0d3c7 100644
--- a/net/ntp.c
+++ b/net/ntp.c
@@ -3,6 +3,29 @@
#include "net_sys.h"
#include "ntp.h"
+//http://ptspts.blogspot.com/2009/11/how-to-convert-unix-timestamp-to-civil.html
+// Convert a Unix timestamp to a civil date ([year, month, day, hour, minute,
+// second]) in GMT.
+void timestamp_to_gmt_civil(uint32_t ts)
+{
+ uint32_t s = ts%86400;
+ ts /= 86400;
+ uint32_t h = s/3600;
+ uint32_t m = s/60%60;
+ s = s%60;
+ uint32_t x = (ts*4+102032)/146097+15;
+ uint32_t b = ts+2442113+x-(x/4);
+ uint32_t c = (b*20-2442)/7305;
+ uint32_t d = b-365*c-c/4;
+ uint32_t e = d*1000/30601;
+ uint32_t f = d-e*30-e*601/1000;
+ if(e < 14){c-=4716;e-=1;}
+ else {c-=4715;e-=13;}
+
+ // final result is in: c / e / f / h / m / s
+ klog ("%d.%d.%d %d:%d:%d UTC",f,e,c,h,m,s);
+}
+
uint32_t ntp_generic(struct netdev *dev, uint32_t ip, uint16_t src, uint16_t dst, uint8_t* pos,uint8_t *end)
{
pos-=48; // we need 48 bytes
@@ -39,8 +62,9 @@ uint32_t ntp_generic(struct netdev *dev, uint32_t ip, uint16_t src, uint16_t dst
uint32_t ntp_incoming(uint8_t* start, uint8_t *pos)
{
struct udp_v4_ntp *ntp=pos;
- klog("0x%08X",ntohl(ntp->txTm_s)); // timestamp recevived
- klog("google's NTP says that %d seconds elapsed since 1 jan 1970",ntohl(ntp->txTm_s)); // timestamp recevived
+ uint32_t ntp_time=ntohl(ntp->txTm_s);
+ uint32_t unix_time=ntp_time-2208988800; // subtract offset to get unixtime
+ timestamp_to_gmt_civil(unix_time);
return 0; // all ok
}