Simple ethdump tool based on OSNICRecv

This commit is contained in:
Ali Mashtizadeh 2015-01-27 10:35:20 -08:00
parent 8f24dbe6d0
commit dd0a1d5612
5 changed files with 128 additions and 0 deletions

View File

@ -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")

21
bin/ethdump/SConscript Normal file
View File

@ -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)

76
bin/ethdump/ethdump.c Normal file
View File

@ -0,0 +1,76 @@
#include <stdio.h>
#include <errno.h>
#include <syscall.h>
#include <sys/nic.h>
#include <net/ethernet.h>
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);
}
}

28
include/net/ethernet.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __NET_ETHERNET_H__
#define __NET_ETHERNET_H__
#include <sys/cdefs.h>
#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__ */

View File

@ -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