1 /*
2 * send.c
3 *
4 * Copyright (c) 2002 Dug Song <dugsong@monkey.org>
5 *
6 * $Id$
7 */
8
9 #include "config.h"
10
11 #include <sys/types.h>
12
13 #include <err.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <time.h>
18 #include <unistd.h>
19
20 #include "dnet.h"
21 #include "aton.h"
22 #include "mod.h"
23
24 void
send_usage(void)25 send_usage(void)
26 {
27 fprintf(stderr, "Usage: dnet send [<device>]\n");
28 exit(1);
29 }
30
31 int
send_main(int argc,char * argv[])32 send_main(int argc, char *argv[])
33 {
34 eth_t *eth;
35 ip_t *ip;
36 u_char *p, buf[IP_LEN_MAX]; /* XXX */
37 int c, len;
38
39 if (argc == 2 && *(argv[1]) == '-')
40 send_usage();
41
42 if (isatty(STDIN_FILENO))
43 errx(1, "can't read packet to send from tty");
44
45 p = buf;
46 len = sizeof(buf) - (p - buf);
47
48 while ((c = read(STDIN_FILENO, p, len)) > 0) {
49 p += c;
50 len -= c;
51 }
52 len = p - buf;
53
54 if (argc == 1) {
55 if ((ip = ip_open()) == NULL)
56 err(1, "ip_open");
57 if (ip_send(ip, buf, len) != len)
58 err(1, "ip_send");
59 ip_close(ip);
60 } else if (argc == 2) {
61 if ((eth = eth_open(argv[1])) == NULL)
62 err(1, "eth_open");
63 if (eth_send(eth, buf, len) != len)
64 err(1, "eth_send");
65 eth_close(eth);
66 } else
67 send_usage();
68
69 exit(0);
70 }
71
72 struct mod mod_send = {
73 "send",
74 MOD_TYPE_XMIT,
75 send_main
76 };
77