etherfun.h revision 1.1 1 1.1 chuck /*
2 1.1 chuck *
3 1.1 chuck * Copyright (c) 1995 Charles D. Cranor and Seth Widoff
4 1.1 chuck * All rights reserved.
5 1.1 chuck *
6 1.1 chuck * Redistribution and use in source and binary forms, with or without
7 1.1 chuck * modification, are permitted provided that the following conditions
8 1.1 chuck * are met:
9 1.1 chuck * 1. Redistributions of source code must retain the above copyright
10 1.1 chuck * notice, this list of conditions and the following disclaimer.
11 1.1 chuck * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 chuck * notice, this list of conditions and the following disclaimer in the
13 1.1 chuck * documentation and/or other materials provided with the distribution.
14 1.1 chuck * 3. All advertising materials mentioning features or use of this software
15 1.1 chuck * must display the following acknowledgement:
16 1.1 chuck * This product includes software developed by Charles D. Cranor
17 1.1 chuck * and Seth Widoff.
18 1.1 chuck * 4. The name of the author may not be used to endorse or promote products
19 1.1 chuck * derived from this software without specific prior written permission.
20 1.1 chuck *
21 1.1 chuck * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 chuck * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 chuck * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 chuck * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 chuck * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 chuck * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 chuck * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 chuck * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 chuck * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 chuck * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 chuck */
32 1.1 chuck /* etherfun.h */
33 1.1 chuck
34 1.1 chuck /* constants */
35 1.1 chuck /* ether header */
36 1.1 chuck #define ETYPE_RARP 0x8035 /* ethertype is RARP */
37 1.1 chuck #define ETYPE_IP 0x800 /* ethertype is IP */
38 1.1 chuck
39 1.1 chuck /* rev arp */
40 1.1 chuck #define PTYPE_IP 0x800 /* Protocol type is IP */
41 1.1 chuck #define OPCODE_RARP 3 /* Optype is REVARP request */
42 1.1 chuck #define OPCODE_REPLY 4 /* Optype is REVARP reply */
43 1.1 chuck
44 1.1 chuck /* ip header */
45 1.1 chuck #define IPP_UDP 17 /* IP Protocol is UDP */
46 1.1 chuck #define IP_VERSION 4 /* IP version number */
47 1.1 chuck #define IP_HLEN 5 /* IP header length is a fixed 50 bytes */
48 1.1 chuck #define N 1536
49 1.1 chuck
50 1.1 chuck /* tftp header */
51 1.1 chuck #define FTPOP_ACKN 4 /* Opcode is acknowledge */
52 1.1 chuck #define FTPOP_ERR 5 /* Opcode is Error */
53 1.1 chuck #define FTP_PORT 69 /* Standard TFTP port number */
54 1.1 chuck #define MSG "\0\1xxxxxxxx.147\0octet\0" /* implicit NULL */
55 1.1 chuck
56 1.1 chuck /* data structures */
57 1.1 chuck
58 1.1 chuck struct ether_header {
59 1.1 chuck u_char ether_dhost[6];
60 1.1 chuck u_char ether_shost[6];
61 1.1 chuck u_short ether_type;
62 1.1 chuck };
63 1.1 chuck
64 1.1 chuck struct ether_arp {
65 1.1 chuck u_short ar_hrd; /* format of hardware address */
66 1.1 chuck u_short ar_pro; /* format of protocol address */
67 1.1 chuck u_char ar_hln; /* length of hardware address */
68 1.1 chuck u_char ar_pln; /* length of protocol address */
69 1.1 chuck u_short ar_op;
70 1.1 chuck u_char arp_sha[6]; /* sender hardware address */
71 1.1 chuck u_char arp_spa[4]; /* sender protocol address */
72 1.1 chuck u_char arp_tha[6]; /* target hardware address */
73 1.1 chuck u_char arp_tpa[4]; /* target protocol address */
74 1.1 chuck };
75 1.1 chuck
76 1.1 chuck struct ip {
77 1.1 chuck u_char ip_v:4, /* version */
78 1.1 chuck ip_hl:4; /* header length */
79 1.1 chuck u_char ip_tos; /* type of service */
80 1.1 chuck short ip_len; /* total length */
81 1.1 chuck u_short ip_id; /* identification */
82 1.1 chuck short ip_off; /* fragment offset field */
83 1.1 chuck #define IP_DF 0x4000 /* dont fragment flag */
84 1.1 chuck #define IP_MF 0x2000 /* more fragments flag */
85 1.1 chuck #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
86 1.1 chuck u_char ip_ttl; /* time to live */
87 1.1 chuck u_char ip_p; /* protocol */
88 1.1 chuck u_short ip_sum; /* checksum */
89 1.1 chuck u_char ip_src[4];
90 1.1 chuck u_char ip_dst[4]; /* source and dest address */
91 1.1 chuck };
92 1.1 chuck
93 1.1 chuck struct udp {
94 1.1 chuck u_short uh_sport;
95 1.1 chuck u_short uh_dport;
96 1.1 chuck short uh_ulen;
97 1.1 chuck u_short uh_sum;
98 1.1 chuck };
99 1.1 chuck
100 1.1 chuck struct tftph {
101 1.1 chuck u_short op_code;
102 1.1 chuck u_short block;
103 1.1 chuck };
104 1.1 chuck
105 1.1 chuck struct tftphr {
106 1.1 chuck struct tftph info;
107 1.1 chuck char data[1];
108 1.1 chuck };
109 1.1 chuck
110 1.1 chuck /* globals */
111 1.1 chuck int last_ack;
112 1.1 chuck char buf[N];
113 1.1 chuck struct ether_header *eh = (struct ether_header *)buf;
114 1.1 chuck struct ether_arp *rarp =
115 1.1 chuck (struct ether_arp *)(buf + sizeof(struct ether_header));
116 1.1 chuck struct ip *iph = (struct ip *)(buf + sizeof(struct ether_header));
117 1.1 chuck struct udp *udph = (struct udp *)(buf + sizeof(struct ether_header) +
118 1.1 chuck sizeof(struct ip));
119 1.1 chuck char *tftp_r = buf + sizeof(struct ether_header) + sizeof(struct ip) +
120 1.1 chuck sizeof(struct udp);
121 1.1 chuck struct tftph *tftp_a = (struct tftph *)(buf + sizeof(struct ether_header) +
122 1.1 chuck sizeof(struct ip) + sizeof(struct udp));
123 1.1 chuck struct tftphr *tftp = (struct tftphr *)(buf + sizeof(struct ether_header) +
124 1.1 chuck sizeof(struct ip) + sizeof(struct udp));
125