00001
00002
00003
00004
00005
00006
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 <time.h>
00018 #include <unistd.h>
00019
00020 #include "dnet.h"
00021 #include "aton.h"
00022 #include "mod.h"
00023
00024 void
00025 ip_usage(void)
00026 {
00027 fprintf(stderr, "Usage: dnet ip [tos|id|off|ttl|proto|src|dst "
00028 "<value>] ... \n");
00029 exit(1);
00030 }
00031
00032 int
00033 ip_main(int argc, char *argv[])
00034 {
00035 struct ip_hdr *ip;
00036 struct addr addr;
00037 u_char *p, buf[IP_LEN_MAX];
00038 char *name, *value;
00039 int c, len;
00040
00041 srand(time(NULL));
00042
00043 ip = (struct ip_hdr *)buf;
00044 ip->ip_hl = 5;
00045 ip->ip_v = 4;
00046 ip->ip_tos = 0;
00047 ip->ip_id = rand() & 0xffff;
00048 ip->ip_off = 0;
00049 ip->ip_ttl = IP_TTL_MAX;
00050 ip->ip_p = rand() & 0xff;
00051 ip->ip_sum = 0;
00052 ip->ip_src = rand();
00053 ip->ip_dst = rand();
00054
00055 for (c = 1; c + 1 < argc; c += 2) {
00056 name = argv[c];
00057 value = argv[c + 1];
00058
00059 if (strcmp(name, "tos") == 0)
00060 ip->ip_tos = atoi(value);
00061 else if (strcmp(name, "id") == 0)
00062 ip->ip_id = ntohs(atoi(value));
00063 else if (strcmp(name, "off") == 0) {
00064 if (off_aton(value, &ip->ip_off) < 0)
00065 ip_usage();
00066 } else if (strcmp(name, "ttl") == 0)
00067 ip->ip_ttl = atoi(value);
00068 else if (strcmp(name, "proto") == 0) {
00069 if (proto_aton(value, &ip->ip_p) < 0)
00070 ip_usage();
00071 } else if (strcmp(name, "src") == 0) {
00072 if (addr_aton(value, &addr) < 0)
00073 ip_usage();
00074 ip->ip_src = addr.addr_ip;
00075 } else if (strcmp(name, "dst") == 0) {
00076 if (addr_aton(value, &addr) < 0)
00077 ip_usage();
00078 ip->ip_dst = addr.addr_ip;
00079 } else
00080 ip_usage();
00081 }
00082 argc -= c;
00083 argv += c;
00084
00085 if (argc != 0)
00086 ip_usage();
00087
00088 if (isatty(STDIN_FILENO))
00089 errx(1, "can't read IP payload from tty");
00090
00091 p = buf + IP_HDR_LEN;
00092 len = sizeof(buf) - (p - buf);
00093
00094 while ((c = read(STDIN_FILENO, p, len)) > 0) {
00095 p += c;
00096 len -= c;
00097 }
00098 len = p - buf;
00099
00100 ip->ip_len = htons(len);
00101
00102 ip_checksum(buf, len);
00103
00104 if (write(STDOUT_FILENO, buf, len) != len)
00105 err(1, "write");
00106
00107 return (0);
00108 }
00109
00110 struct mod mod_ip = {
00111 "ip",
00112 MOD_TYPE_ENCAP,
00113 ip_main
00114 };