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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
38 #include <sys/param.h>
39 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
44 #include <netinet/in_systm.h>
53 static ssize_t
rarpsend(struct iodesc
*, void *, size_t);
54 static ssize_t
rarprecv(struct iodesc
*, void *, size_t, time_t);
57 * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
60 rarp_getipaddress(int sock
)
65 u_char header
[ETHER_SIZE
];
68 u_char pad
[18]; /* 60 - sizeof(arp) */
72 u_char header
[ETHER_SIZE
];
75 u_char pad
[24]; /* extra space */
81 printf("rarp: socket=%d\n", sock
);
83 if (!(d
= socktodesc(sock
))) {
84 printf("rarp: bad socket. %d\n", sock
);
89 printf("rarp: d=%x\n", (u_int
)d
);
92 bzero((char*)&wbuf
.data
, sizeof(wbuf
.data
));
94 ap
->arp_hrd
= htons(ARPHRD_ETHER
);
95 ap
->arp_pro
= htons(ETHERTYPE_IP
);
96 ap
->arp_hln
= sizeof(ap
->arp_sha
); /* hardware address length */
97 ap
->arp_pln
= sizeof(ap
->arp_spa
); /* protocol address length */
98 ap
->arp_op
= htons(ARPOP_REVREQUEST
);
99 bcopy(d
->myea
, ap
->arp_sha
, 6);
100 bcopy(d
->myea
, ap
->arp_tha
, 6);
103 rarpsend
, &wbuf
.data
, sizeof(wbuf
.data
),
104 rarprecv
, &rbuf
.data
, sizeof(rbuf
.data
)) < 0)
106 printf("No response for RARP request\n");
111 bcopy(ap
->arp_tpa
, (char *)&myip
, sizeof(myip
));
113 /* XXX - Can NOT assume this is our root server! */
114 bcopy(ap
->arp_spa
, (char *)&rootip
, sizeof(rootip
));
117 /* Compute our "natural" netmask. */
118 if (IN_CLASSA(myip
.s_addr
))
119 netmask
= IN_CLASSA_NET
;
120 else if (IN_CLASSB(myip
.s_addr
))
121 netmask
= IN_CLASSB_NET
;
123 netmask
= IN_CLASSC_NET
;
130 * Broadcast a RARP request (i.e. who knows who I am)
133 rarpsend(struct iodesc
*d
, void *pkt
, size_t len
)
138 printf("rarpsend: called\n");
141 return (sendether(d
, pkt
, len
, bcea
, ETHERTYPE_REVARP
));
145 * Returns 0 if this is the packet we're waiting for
146 * else -1 (and errno == 0)
149 rarprecv(struct iodesc
*d
, void *pkt
, size_t len
, time_t tleft
)
152 struct ether_arp
*ap
;
153 u_int16_t etype
; /* host order */
157 printf("rarprecv: ");
160 n
= readether(d
, pkt
, len
, tleft
, &etype
);
162 if (n
== -1 || n
< sizeof(struct ether_arp
)) {
165 printf("bad len=%d\n", n
);
170 if (etype
!= ETHERTYPE_REVARP
) {
173 printf("bad type=0x%x\n", etype
);
178 ap
= (struct ether_arp
*)pkt
;
179 if (ap
->arp_hrd
!= htons(ARPHRD_ETHER
) ||
180 ap
->arp_pro
!= htons(ETHERTYPE_IP
) ||
181 ap
->arp_hln
!= sizeof(ap
->arp_sha
) ||
182 ap
->arp_pln
!= sizeof(ap
->arp_spa
) )
186 printf("bad hrd/pro/hln/pln\n");
191 if (ap
->arp_op
!= htons(ARPOP_REVREPLY
)) {
194 printf("bad op=0x%x\n", ntohs(ap
->arp_op
));
199 /* Is the reply for our Ethernet address? */
200 if (bcmp(ap
->arp_tha
, d
->myea
, 6)) {
203 printf("unwanted address\n");
208 /* We have our answer. */