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

icmp.c

Go to the documentation of this file.
00001 /*
00002  * icmp.c
00003  *
00004  * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
00005  *
00006  * $Id: icmp.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 icmp_usage(void)
00025 {
00026         fprintf(stderr, "Usage: dnet icmp [type|code <value>] ...\n");
00027         exit(1);
00028 }
00029 
00030 int
00031 icmp_main(int argc, char *argv[])
00032 {
00033         struct icmp_hdr *icmp;
00034         u_char *p, buf[IP_LEN_MAX];     /* XXX */
00035         char *name, *value;
00036         int c, len;
00037 
00038         icmp = (struct icmp_hdr *)buf;
00039         
00040         memset(icmp, 0, sizeof(*icmp));
00041         icmp->icmp_type = ICMP_ECHO;
00042         icmp->icmp_code = 0;
00043         
00044         for (c = 1; c + 1 < argc; c += 2) {
00045                 name = argv[c];
00046                 value = argv[c + 1];
00047 
00048                 if (strcmp(name, "type") == 0) {
00049                         icmp->icmp_type = atoi(value);
00050                 } else if (strcmp(name, "code") == 0) {
00051                         icmp->icmp_code = atoi(value);
00052                 } else
00053                         icmp_usage();
00054         }
00055         argc -= c;
00056         argv += c;
00057 
00058         if (argc != 0)
00059                 icmp_usage();
00060 
00061         p = buf + ICMP_HDR_LEN;
00062         
00063         if (!isatty(STDIN_FILENO)) {
00064                 len = sizeof(buf) - (p - buf);
00065                 while ((c = read(STDIN_FILENO, p, len)) > 0) {
00066                         p += c;
00067                         len -= c;
00068                 }
00069         }
00070         len = p - buf;
00071         
00072         if (write(STDOUT_FILENO, buf, len) != len)
00073                 err(1, "write");
00074 
00075         return (0);
00076 }
00077 
00078 struct mod mod_icmp = {
00079         "icmp",
00080         MOD_TYPE_ENCAP,
00081         icmp_main
00082 };

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