This source file includes following definitions.
- rarp_getipaddress
- rarpsend
- rarprecv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45 #include <netinet/in.h>
46
47 #include <netinet/if_ether.h>
48 #include <netinet/in_systm.h>
49
50 #include "stand.h"
51 #include "net.h"
52 #include "netif.h"
53
54 static ssize_t rarpsend(struct iodesc *, void *, size_t);
55 static ssize_t rarprecv(struct iodesc *, void *, size_t, time_t);
56
57
58
59
60 int
61 rarp_getipaddress(int sock)
62 {
63 struct iodesc *d;
64 struct ether_arp *ap;
65 struct {
66 u_char header[ETHER_SIZE];
67 struct {
68 struct ether_arp arp;
69 u_char pad[18];
70 } data;
71 } wbuf;
72 struct {
73 u_char header[ETHER_SIZE];
74 struct {
75 struct ether_arp arp;
76 u_char pad[24];
77 } data;
78 } rbuf;
79
80 #ifdef RARP_DEBUG
81 if (debug)
82 printf("rarp: socket=%d\n", sock);
83 #endif
84 if (!(d = socktodesc(sock))) {
85 printf("rarp: bad socket. %d\n", sock);
86 return (-1);
87 }
88 #ifdef RARP_DEBUG
89 if (debug)
90 printf("rarp: d=%x\n", (u_int)d);
91 #endif
92
93 bzero((char *)&wbuf.data, sizeof(wbuf.data));
94 ap = &wbuf.data.arp;
95 ap->arp_hrd = htons(ARPHRD_ETHER);
96 ap->arp_pro = htons(ETHERTYPE_IP);
97 ap->arp_hln = sizeof(ap->arp_sha);
98 ap->arp_pln = sizeof(ap->arp_spa);
99 ap->arp_op = htons(ARPOP_REVREQUEST);
100 bcopy(d->myea, ap->arp_sha, 6);
101 bcopy(d->myea, ap->arp_tha, 6);
102
103 if (sendrecv(d,
104 rarpsend, &wbuf.data, sizeof(wbuf.data),
105 rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0)
106 {
107 printf("No response for RARP request\n");
108 return (-1);
109 }
110
111 ap = &rbuf.data.arp;
112 bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
113 #if 0
114
115 bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
116 #endif
117
118
119 if (IN_CLASSA(myip.s_addr))
120 netmask = IN_CLASSA_NET;
121 else if (IN_CLASSB(myip.s_addr))
122 netmask = IN_CLASSB_NET;
123 else
124 netmask = IN_CLASSC_NET;
125
126 d->myip = myip;
127 return (0);
128 }
129
130
131
132
133 static ssize_t
134 rarpsend(struct iodesc *d, void *pkt, size_t len)
135 {
136
137 #ifdef RARP_DEBUG
138 if (debug)
139 printf("rarpsend: called\n");
140 #endif
141
142 return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
143 }
144
145
146
147
148
149 static ssize_t
150 rarprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
151 {
152 ssize_t n;
153 struct ether_arp *ap;
154 u_int16_t etype;
155
156 #ifdef RARP_DEBUG
157 if (debug)
158 printf("rarprecv: ");
159 #endif
160
161 n = readether(d, pkt, len, tleft, &etype);
162 errno = 0;
163 if (n < 0 || (size_t)n < sizeof(struct ether_arp)) {
164 #ifdef RARP_DEBUG
165 if (debug)
166 printf("bad len=%d\n", n);
167 #endif
168 return (-1);
169 }
170
171 if (etype != ETHERTYPE_REVARP) {
172 #ifdef RARP_DEBUG
173 if (debug)
174 printf("bad type=0x%x\n", etype);
175 #endif
176 return (-1);
177 }
178
179 ap = (struct ether_arp *)pkt;
180 if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
181 ap->arp_pro != htons(ETHERTYPE_IP) ||
182 ap->arp_hln != sizeof(ap->arp_sha) ||
183 ap->arp_pln != sizeof(ap->arp_spa) )
184 {
185 #ifdef RARP_DEBUG
186 if (debug)
187 printf("bad hrd/pro/hln/pln\n");
188 #endif
189 return (-1);
190 }
191
192 if (ap->arp_op != htons(ARPOP_REVREPLY)) {
193 #ifdef RARP_DEBUG
194 if (debug)
195 printf("bad op=0x%x\n", ntohs(ap->arp_op));
196 #endif
197 return (-1);
198 }
199
200
201 if (bcmp(ap->arp_tha, d->myea, 6)) {
202 #ifdef RARP_DEBUG
203 if (debug)
204 printf("unwanted address\n");
205 #endif
206 return (-1);
207 }
208
209
210 #ifdef RARP_DEBUG
211 if (debug)
212 printf("got it\n");
213 #endif
214 return (n);
215 }