Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

eth.c

Go to the documentation of this file.
00001 /*
00002  * eth.c
00003  *
00004  * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
00005  *
00006  * $Id: eth.c,v 1.6 2002/02/08 07:43:53 dugsong Exp $
00007  */
00008 
00009 #include "config.h"
00010 
00011 #include <sys/types.h>
00012 
00013 #include <err.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <unistd.h>
00018 
00019 #include "dnet.h"
00020 #include "aton.h"
00021 #include "mod.h"
00022 
00023 void
00024 eth_usage(void)
00025 {
00026         fprintf(stderr, "Usage: dnet eth [type|src|dst <value>] ... \n");
00027         exit(1);
00028 }
00029 
00030 int
00031 eth_main(int argc, char *argv[])
00032 {
00033         struct eth_hdr *eth;
00034         struct addr addr;
00035         u_char *p, buf[ETH_LEN_MAX];    /* XXX */
00036         char *name, *value;
00037         int c, len;
00038 
00039         eth = (struct eth_hdr *)buf;
00040         memset(eth, 0, sizeof(*eth));
00041         eth->eth_type = htons(ETH_TYPE_IP);
00042 
00043         for (c = 1; c + 1 < argc; c += 2) {
00044                 name = argv[c];
00045                 value = argv[c + 1];
00046 
00047                 if (strcmp(name, "type") == 0) {
00048                         if (type_aton(value, &eth->eth_type) < 0)
00049                                 eth_usage();
00050                 } else if (strcmp(name, "src") == 0) {
00051                         if (addr_aton(value, &addr) < 0)
00052                                 eth_usage();
00053                         memcpy(&eth->eth_src, &addr.addr_eth, ETH_ADDR_LEN);
00054                 } else if (strcmp(name, "dst") == 0) {
00055                         if (addr_aton(value, &addr) < 0)
00056                                 eth_usage();
00057                         memcpy(&eth->eth_dst, &addr.addr_eth, ETH_ADDR_LEN);
00058                 } else
00059                         eth_usage();
00060         }
00061         argc -= c;
00062         argv += c;
00063 
00064         if (argc != 0)
00065                 eth_usage();
00066         
00067         if (isatty(STDIN_FILENO))
00068                 errx(1, "can't read Ethernet payload from tty");
00069         
00070         p = buf + ETH_HDR_LEN;
00071         len = sizeof(buf) - (p - buf);
00072         
00073         while ((c = read(STDIN_FILENO, p, len)) > 0) {
00074                 p += c;
00075                 len -= c;
00076         }
00077         len = p - buf;
00078         
00079         if (write(STDOUT_FILENO, buf, len) != len)
00080                 err(1, "write");
00081         
00082         return (0);
00083 }
00084 
00085 struct mod mod_eth = {
00086         "eth",
00087         MOD_TYPE_ENCAP,
00088         eth_main
00089 };

Generated on Sun May 14 14:51:12 2006 by  doxygen 1.4.2