]>
git.saurik.com Git - apple/network_cmds.git/blob - traceroute.tproj/findsaddr-socket.c
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the Computer Systems
16 * Engineering Group at Lawrence Berkeley Laboratory.
17 * 4. Neither the name of the University nor of the Laboratory may be used
18 * to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/contrib/traceroute/findsaddr-socket.c,v 1.2 2002/07/30 04:49:13 fenner Exp $
36 /* XXX Yes this is WAY too complicated */
39 static const char rcsid
[] =
40 "@(#) $Id: findsaddr-socket.c,v 1.3 2005/02/12 00:04:09 lindak Exp $ (LBL)";
43 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #ifdef HAVE_SYS_SOCKIO_H
48 #include <sys/sockio.h>
50 #include <sys/time.h> /* concession to AIX */
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <netinet/in.h>
69 #ifdef HAVE_OS_PROTO_H
73 #include "findsaddr.h"
75 #ifdef HAVE_SOCKADDR_SA_LEN
76 #define SALEN(sa) ((sa)->sa_len)
78 #define SALEN(sa) salen(sa)
82 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
86 struct rt_msghdr rtmsg
;
90 static struct rtmsg rtmsg
= {
91 { 0, RTM_VERSION
, RTM_GET
, 0,
92 RTF_UP
| RTF_GATEWAY
| RTF_HOST
| RTF_STATIC
,
93 RTA_DST
| RTA_IFA
, 0, 0, 0, 0, 0, { 0 } },
97 #ifndef HAVE_SOCKADDR_SA_LEN
98 static int salen(struct sockaddr
*);
102 * Return the source address for the given destination address
105 findsaddr(register const struct sockaddr_in
*to
,
106 register struct sockaddr_in
*from
)
108 register struct rt_msghdr
*rp
;
111 register struct sockaddr_in
*sp
, *ifa
;
112 register struct sockaddr
*sa
;
113 register int s
, size
, cc
, seq
, i
;
115 static char errbuf
[512];
117 s
= socket(PF_ROUTE
, SOCK_RAW
, AF_UNSPEC
);
119 sprintf(errbuf
, "socket: %.128s", strerror(errno
));
128 cp
= (u_char
*)(rp
+ 1);
130 sp
= (struct sockaddr_in
*)cp
;
132 cp
+= roundup(SALEN((struct sockaddr
*)sp
), sizeof(u_int32_t
));
134 size
= cp
- (u_char
*)rp
;
135 rp
->rtm_msglen
= size
;
137 cc
= write(s
, (char *)rp
, size
);
139 sprintf(errbuf
, "write: %.128s", strerror(errno
));
144 sprintf(errbuf
, "short write (%d != %d)", cc
, size
);
149 size
= sizeof(rtmsg
);
152 cc
= read(s
, (char *)rp
, size
);
154 sprintf(errbuf
, "read: %.128s", strerror(errno
));
159 } while (rp
->rtm_seq
!= seq
|| rp
->rtm_pid
!= pid
);
163 if (rp
->rtm_version
!= RTM_VERSION
) {
164 sprintf(errbuf
, "bad version %d", rp
->rtm_version
);
167 if (rp
->rtm_msglen
> cc
) {
168 sprintf(errbuf
, "bad msglen %d > %d", rp
->rtm_msglen
, cc
);
171 if (rp
->rtm_errno
!= 0) {
172 sprintf(errbuf
, "rtm_errno: %.128s", strerror(rp
->rtm_errno
));
176 /* Find the interface sockaddr */
177 cp
= (u_char
*)(rp
+ 1);
178 for (i
= 1; i
!= 0; i
<<= 1)
179 if ((i
& rp
->rtm_addrs
) != 0) {
180 sa
= (struct sockaddr
*)cp
;
184 if (sa
->sa_family
== AF_INET
) {
185 ifa
= (struct sockaddr_in
*)cp
;
186 if (ifa
->sin_addr
.s_addr
!= 0) {
199 cp
+= sizeof (u_int32_t
);
201 cp
+= roundup(SALEN(sa
), sizeof (u_int32_t
));
207 #ifndef HAVE_SOCKADDR_SA_LEN
209 salen(struct sockaddr
*sa
)
211 switch (sa
->sa_family
) {
214 return (sizeof(struct sockaddr_in
));
217 return (sizeof(struct sockaddr_dl
));
220 return (sizeof(struct sockaddr
));