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 tcp_usage(void)
00026 {
00027 fprintf(stderr, "Usage: dnet tcp [sport|dport|flags|seq|ack|"
00028 "win|urp <value>] ...\n");
00029 exit(1);
00030 }
00031
00032 int
00033 tcp_main(int argc, char *argv[])
00034 {
00035 struct tcp_hdr *tcp;
00036 u_char *p, buf[IP_LEN_MAX];
00037 char *name, *value;
00038 int c, len;
00039
00040 srand(time(NULL));
00041
00042 tcp = (struct tcp_hdr *)buf;
00043
00044 memset(tcp, 0, sizeof(*tcp));
00045 tcp->th_sport = rand() & 0xffff;
00046 tcp->th_dport = rand() & 0xffff;
00047 tcp->th_seq = rand();
00048 tcp->th_ack = 0;
00049 tcp->th_off = 5;
00050 tcp->th_flags = TH_SYN;
00051 tcp->th_win = TCP_WIN_MAX;
00052 tcp->th_urp = 0;
00053
00054 for (c = 1; c + 1 < argc; c += 2) {
00055 name = argv[c];
00056 value = argv[c + 1];
00057
00058 if (strcmp(name, "sport") == 0) {
00059 if (port_aton(value, &tcp->th_sport) < 0)
00060 tcp_usage();
00061 } else if (strcmp(name, "dport") == 0) {
00062 if (port_aton(value, &tcp->th_dport) < 0)
00063 tcp_usage();
00064 } else if (strcmp(name, "flags") == 0) {
00065 if (flags_aton(value, &tcp->th_flags) < 0)
00066 tcp_usage();
00067 } else if (strcmp(name, "seq") == 0) {
00068 if (seq_aton(value, &tcp->th_seq) < 0)
00069 tcp_usage();
00070 } else if (strcmp(name, "ack") == 0) {
00071 if (seq_aton(value, &tcp->th_ack) < 0)
00072 tcp_usage();
00073 } else if (strcmp(name, "win") == 0) {
00074 if (port_aton(value, &tcp->th_win) < 0)
00075 tcp_usage();
00076 } else if (strcmp(name, "urp") == 0) {
00077 if (port_aton(value, &tcp->th_urp) < 0)
00078 tcp_usage();
00079 } else
00080 tcp_usage();
00081 }
00082 argc -= c;
00083 argv += c;
00084
00085 if (argc != 0)
00086 tcp_usage();
00087
00088 p = buf + TCP_HDR_LEN;
00089
00090 if (!isatty(STDIN_FILENO)) {
00091 len = sizeof(buf) - (p - buf);
00092 while ((c = read(STDIN_FILENO, p, len)) > 0) {
00093 p += c;
00094 len -= c;
00095 }
00096 }
00097 len = p - buf;
00098
00099 if (write(STDOUT_FILENO, buf, len) != len)
00100 err(1, "write");
00101
00102 return (0);
00103 }
00104
00105 struct mod mod_tcp = {
00106 "tcp",
00107 MOD_TYPE_ENCAP,
00108 &tcp_main
00109 };