1 /* $NetBSD: rarp.c,v 1.16 1997/07/07 15:52:52 drochner Exp $ */
2 /* $DragonFly: src/lib/libstand/rarp.c,v 1.3 2005/12/11 02:27:26 swildner Exp $ */
5 * Copyright (c) 1992 Regents of the University of California.
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Lawrence Berkeley Laboratory and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
42 #include <sys/param.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
48 #include <netinet/in_systm.h>
57 static ssize_t
rarpsend(struct iodesc
*, void *, size_t);
58 static ssize_t
rarprecv(struct iodesc
*, void *, size_t, time_t);
61 * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
64 rarp_getipaddress(int sock
)
69 u_char header
[ETHER_SIZE
];
72 u_char pad
[18]; /* 60 - sizeof(arp) */
76 u_char header
[ETHER_SIZE
];
79 u_char pad
[24]; /* extra space */
85 printf("rarp: socket=%d\n", sock
);
87 if (!(d
= socktodesc(sock
))) {
88 printf("rarp: bad socket. %d\n", sock
);
93 printf("rarp: d=%x\n", (u_int
)d
);
96 bzero((char*)&wbuf
.data
, sizeof(wbuf
.data
));
98 ap
->arp_hrd
= htons(ARPHRD_ETHER
);
99 ap
->arp_pro
= htons(ETHERTYPE_IP
);
100 ap
->arp_hln
= sizeof(ap
->arp_sha
); /* hardware address length */
101 ap
->arp_pln
= sizeof(ap
->arp_spa
); /* protocol address length */
102 ap
->arp_op
= htons(ARPOP_REVREQUEST
);
103 bcopy(d
->myea
, ap
->arp_sha
, 6);
104 bcopy(d
->myea
, ap
->arp_tha
, 6);
107 rarpsend
, &wbuf
.data
, sizeof(wbuf
.data
),
108 rarprecv
, &rbuf
.data
, sizeof(rbuf
.data
)) < 0)
110 printf("No response for RARP request\n");
115 bcopy(ap
->arp_tpa
, (char *)&myip
, sizeof(myip
));
117 /* XXX - Can NOT assume this is our root server! */
118 bcopy(ap
->arp_spa
, (char *)&rootip
, sizeof(rootip
));
121 /* Compute our "natural" netmask. */
122 if (IN_CLASSA(myip
.s_addr
))
123 netmask
= IN_CLASSA_NET
;
124 else if (IN_CLASSB(myip
.s_addr
))
125 netmask
= IN_CLASSB_NET
;
127 netmask
= IN_CLASSC_NET
;
134 * Broadcast a RARP request (i.e. who knows who I am)
137 rarpsend(struct iodesc
*d
, void *pkt
, size_t len
)
142 printf("rarpsend: called\n");
145 return (sendether(d
, pkt
, len
, bcea
, ETHERTYPE_REVARP
));
149 * Returns 0 if this is the packet we're waiting for
150 * else -1 (and errno == 0)
153 rarprecv(struct iodesc
*d
, void *pkt
, size_t len
, time_t tleft
)
156 struct ether_arp
*ap
;
157 u_int16_t etype
; /* host order */
161 printf("rarprecv: ");
164 n
= readether(d
, pkt
, len
, tleft
, &etype
);
166 if (n
== -1 || n
< sizeof(struct ether_arp
)) {
169 printf("bad len=%d\n", n
);
174 if (etype
!= ETHERTYPE_REVARP
) {
177 printf("bad type=0x%x\n", etype
);
182 ap
= (struct ether_arp
*)pkt
;
183 if (ap
->arp_hrd
!= htons(ARPHRD_ETHER
) ||
184 ap
->arp_pro
!= htons(ETHERTYPE_IP
) ||
185 ap
->arp_hln
!= sizeof(ap
->arp_sha
) ||
186 ap
->arp_pln
!= sizeof(ap
->arp_spa
) )
190 printf("bad hrd/pro/hln/pln\n");
195 if (ap
->arp_op
!= htons(ARPOP_REVREPLY
)) {
198 printf("bad op=0x%x\n", ntohs(ap
->arp_op
));
203 /* Is the reply for our Ethernet address? */
204 if (bcmp(ap
->arp_tha
, d
->myea
, 6)) {
207 printf("unwanted address\n");
212 /* We have our answer. */