]>
git.saurik.com Git - apple/network_cmds.git/blob - traceroute.tproj/findsaddr-socket.c
88ca8bfe2c72119f3d51b8ab5ba1564bdb06bd8e
2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the Computer Systems
44 * Engineering Group at Lawrence Berkeley Laboratory.
45 * 4. Neither the name of the University nor of the Laboratory may be used
46 * to endorse or promote products derived from this software without
47 * specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * $FreeBSD: src/contrib/traceroute/findsaddr-socket.c,v 1.2 2002/07/30 04:49:13 fenner Exp $
64 /* XXX Yes this is WAY too complicated */
67 static const char rcsid
[] =
68 "@(#) $Id: findsaddr-socket.c,v 1.3 2005/02/12 00:04:09 lindak Exp $ (LBL)";
71 #include <sys/param.h>
73 #include <sys/ioctl.h>
74 #include <sys/socket.h>
75 #ifdef HAVE_SYS_SOCKIO_H
76 #include <sys/sockio.h>
78 #include <sys/time.h> /* concession to AIX */
86 #include <net/if_dl.h>
87 #include <net/route.h>
88 #include <netinet/in.h>
97 #ifdef HAVE_OS_PROTO_H
101 #include "findsaddr.h"
103 #ifdef HAVE_SOCKADDR_SA_LEN
104 #define SALEN(sa) ((sa)->sa_len)
106 #define SALEN(sa) salen(sa)
110 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
114 struct rt_msghdr rtmsg
;
118 static struct rtmsg rtmsg
= {
119 { 0, RTM_VERSION
, RTM_GET
, 0,
120 RTF_UP
| RTF_GATEWAY
| RTF_HOST
| RTF_STATIC
,
121 RTA_DST
| RTA_IFA
, 0, 0, 0, 0, 0, { 0 } },
125 #ifndef HAVE_SOCKADDR_SA_LEN
126 static int salen(struct sockaddr
*);
130 * Return the source address for the given destination address
133 findsaddr(register const struct sockaddr_in
*to
,
134 register struct sockaddr_in
*from
)
136 register struct rt_msghdr
*rp
;
139 register struct sockaddr_in
*sp
, *ifa
;
140 register struct sockaddr
*sa
;
141 register int s
, size
, cc
, seq
, i
;
143 static char errbuf
[512];
145 s
= socket(PF_ROUTE
, SOCK_RAW
, AF_UNSPEC
);
147 snprintf(errbuf
, sizeof(errbuf
), "socket: %.128s", strerror(errno
));
156 cp
= (u_char
*)(rp
+ 1);
158 sp
= (struct sockaddr_in
*)cp
;
160 cp
+= roundup(SALEN((struct sockaddr
*)sp
), sizeof(u_int32_t
));
162 size
= cp
- (u_char
*)rp
;
163 rp
->rtm_msglen
= size
;
165 cc
= write(s
, (char *)rp
, size
);
167 snprintf(errbuf
, sizeof(errbuf
), "write: %.128s", strerror(errno
));
172 snprintf(errbuf
, sizeof(errbuf
), "short write (%d != %d)", cc
, size
);
177 size
= sizeof(rtmsg
);
180 cc
= read(s
, (char *)rp
, size
);
182 snprintf(errbuf
, sizeof(errbuf
), "read: %.128s", strerror(errno
));
187 } while (rp
->rtm_seq
!= seq
|| rp
->rtm_pid
!= pid
);
191 if (rp
->rtm_version
!= RTM_VERSION
) {
192 snprintf(errbuf
, sizeof(errbuf
), "bad version %d", rp
->rtm_version
);
195 if (rp
->rtm_msglen
> cc
) {
196 snprintf(errbuf
, sizeof(errbuf
), "bad msglen %d > %d", rp
->rtm_msglen
, cc
);
199 if (rp
->rtm_errno
!= 0) {
200 snprintf(errbuf
, sizeof(errbuf
), "rtm_errno: %.128s", strerror(rp
->rtm_errno
));
204 /* Find the interface sockaddr */
205 cp
= (u_char
*)(rp
+ 1);
206 for (i
= 1; i
!= 0; i
<<= 1)
207 if ((i
& rp
->rtm_addrs
) != 0) {
208 sa
= (struct sockaddr
*)cp
;
212 if (sa
->sa_family
== AF_INET
) {
213 ifa
= (struct sockaddr_in
*)cp
;
214 if (ifa
->sin_addr
.s_addr
!= 0) {
227 cp
+= sizeof (u_int32_t
);
229 cp
+= roundup(SALEN(sa
), sizeof (u_int32_t
));
235 #ifndef HAVE_SOCKADDR_SA_LEN
237 salen(struct sockaddr
*sa
)
239 switch (sa
->sa_family
) {
242 return (sizeof(struct sockaddr_in
));
245 return (sizeof(struct sockaddr_dl
));
248 return (sizeof(struct sockaddr
));