diff --git a/SConstruct b/SConstruct index f9d29e6..abb9cfa 100644 --- a/SConstruct +++ b/SConstruct @@ -161,6 +161,7 @@ CopyTree('build/include/machine', 'sys/' + env['ARCH'] + '/include', env) # Build Targets SConscript('sys/SConscript', variant_dir='build/sys') SConscript('lib/libc/SConscript', variant_dir='build/lib/libc') +SConscript('bin/ethdump/SConscript', variant_dir='build/bin/ethdump') SConscript('bin/shell/SConscript', variant_dir='build/bin/shell') SConscript('sbin/ifconfig/SConscript', variant_dir='build/sbin/ifconfig') SConscript('sbin/init/SConscript', variant_dir='build/sbin/init') @@ -183,6 +184,7 @@ if env["BOOTDISK"] == "1": env.Append(BUILDERS = {'BuildImage' : newfs}) bootdisk = env.BuildImage('#build/bootdisk.img', '#release/bootdisk.manifest') Depends(bootdisk, "#build/tools/newfs_o2fs/newfs_o2fs") + Depends(bootdisk, "#build/bin/ethdump/ethdump") Depends(bootdisk, "#build/bin/shell/shell") Depends(bootdisk, "#build/sbin/ifconfig/ifconfig") Depends(bootdisk, "#build/sbin/init/init") diff --git a/bin/ethdump/SConscript b/bin/ethdump/SConscript new file mode 100644 index 0000000..9a1216d --- /dev/null +++ b/bin/ethdump/SConscript @@ -0,0 +1,21 @@ +import sys + +Import('env') + +ethdump_env = env.Clone() + +src = [ ] + +src_common = [ + "ethdump.c" +] + +src.append(src_common) + +ethdump_env.Append(LINKFLAGS = ['-nostdlib']) +ethdump_env.Append(CPPFLAGS = ['-nostdinc']) +ethdump_env.Append(CPPPATH = ['#build/include']) +ethdump_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c']) + +ethdump_env.Program("ethdump", src) + diff --git a/bin/ethdump/ethdump.c b/bin/ethdump/ethdump.c new file mode 100644 index 0000000..b4443d7 --- /dev/null +++ b/bin/ethdump/ethdump.c @@ -0,0 +1,76 @@ + +#include +#include +#include +#include + +#include + +static int nicNo = 1; +static char buf[4096]; +static MBuf mbuf; + +void +dumpPacket() +{ + struct ether_header *hdr = (struct ether_header *)&buf; + char srcMac[18]; + char dstMac[18]; + + sprintf(srcMac, "%02x:%02x:%02x:%02x:%02x:%02x", + hdr->ether_shost[0], hdr->ether_shost[1], hdr->ether_shost[2], + hdr->ether_shost[3], hdr->ether_shost[4], hdr->ether_shost[5]); + sprintf(dstMac, "%02x:%02x:%02x:%02x:%02x:%02x", + hdr->ether_dhost[0], hdr->ether_dhost[1], hdr->ether_dhost[2], + hdr->ether_dhost[3], hdr->ether_dhost[4], hdr->ether_dhost[5]); + + printf("From %s to %s of type %04x\n", srcMac, dstMac, hdr->ether_type); +} + +void +readPacket(NIC *nic) +{ + uint64_t status; + + mbuf.vaddr = (uint64_t)&buf; + mbuf.maddr = 0; + mbuf.len = 4096; + mbuf.type = MBUF_TYPE_NULL; + mbuf.flags = 0; + mbuf.status = MBUF_STATUS_NULL; + + status = OSNICRecv(nicNo, &mbuf); + if (status != 0) { + printf("OSNICRecv failed!\n"); + return; + } + + if (mbuf.status == MBUF_STATUS_FAILED) { + printf("Failed to read packet!\n"); + return; + } + + dumpPacket(); +} + +int +main(int argc, const char *argv[]) +{ + uint64_t status; + NIC nic; + + printf("Ethernet Dump Tool\n"); + + status = OSNICStat(nicNo, &nic); + if (status == ENOENT) { + printf("nic%d not present\n", nicNo); + return 1; + } + + printf("Listening to nic%d\n", nic.nicNo); + + while (1) { + readPacket(&nic); + } +} + diff --git a/include/net/ethernet.h b/include/net/ethernet.h new file mode 100644 index 0000000..7f92db7 --- /dev/null +++ b/include/net/ethernet.h @@ -0,0 +1,28 @@ + +#ifndef __NET_ETHERNET_H__ +#define __NET_ETHERNET_H__ + +#include + +#define ETHER_ADDR_LEN 6 +#define ETHER_TYPE_LEN 2 +#define ETHER_CRC_LEN 4 + +#define ETHER_HDR_LEN (2*ETHER_ADDR_LEN + ETHER_TYPE_LEN) + +struct ether_header { + uint8_t ether_dhost[ETHER_ADDR_LEN]; + uint8_t ether_shost[ETHER_ADDR_LEN]; + uint16_t ether_type; +} PACKED; + +struct ether_addr { + uint8_t octet[ETHER_ADDR_LEN]; +} PACKED; + +#define ETHERTYPE_IP 0x0800 /* IP */ +#define ETHERTYPE_ARP 0x0806 /* ARP */ +#define ETHERTYPE_IPV6 0x86DD /* IPv6 */ + +#endif /* __NET_ETHERNET_H__ */ + diff --git a/release/bootdisk.manifest b/release/bootdisk.manifest index 691b15d..6b3b04f 100644 --- a/release/bootdisk.manifest +++ b/release/bootdisk.manifest @@ -5,6 +5,7 @@ DIR / DIR dev END DIR bin + FILE ethdump build/bin/ethdump/ethdump FILE shell build/bin/shell/shell END DIR sbin