]>
git.saurik.com Git - apple/network_cmds.git/blob - traceroute.tproj/findsaddr-socket.c
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 */
66 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/socket.h>
70 #ifdef HAVE_SYS_SOCKIO_H
71 #include <sys/sockio.h>
73 #include <sys/time.h> /* concession to AIX */
81 #include <net/if_dl.h>
82 #include <net/route.h>
83 #include <netinet/in.h>
92 #ifdef HAVE_OS_PROTO_H
96 #include "findsaddr.h"
98 #ifdef HAVE_SOCKADDR_SA_LEN
99 #define SALEN(sa) ((sa)->sa_len)
101 #define SALEN(sa) salen(sa)
105 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
109 struct rt_msghdr rtmsg
;
113 static struct rtmsg rtmsg
= {
114 { 0, RTM_VERSION
, RTM_GET
, 0,
115 RTF_UP
| RTF_GATEWAY
| RTF_HOST
| RTF_STATIC
,
116 RTA_DST
| RTA_IFA
, 0, 0, 0, 0, 0, { 0 } },
120 #ifndef HAVE_SOCKADDR_SA_LEN
121 static int salen(struct sockaddr
*);
125 * Return the source address for the given destination address
128 findsaddr(register const struct sockaddr_in
*to
,
129 register struct sockaddr_in
*from
)
131 register struct rt_msghdr
*rp
;
134 register struct sockaddr_in
*sp
, *ifa
;
135 register struct sockaddr
*sa
;
136 register int s
, size
, cc
, seq
, i
;
138 static char errbuf
[512];
140 s
= socket(PF_ROUTE
, SOCK_RAW
, AF_UNSPEC
);
142 snprintf(errbuf
, sizeof(errbuf
), "socket: %.128s", strerror(errno
));
151 cp
= (u_char
*)(rp
+ 1);
153 sp
= (struct sockaddr_in
*)cp
;
155 cp
+= roundup(SALEN((struct sockaddr
*)sp
), sizeof(u_int32_t
));
157 size
= cp
- (u_char
*)rp
;
158 rp
->rtm_msglen
= size
;
160 cc
= write(s
, (char *)rp
, size
);
162 snprintf(errbuf
, sizeof(errbuf
), "write: %.128s", strerror(errno
));
167 snprintf(errbuf
, sizeof(errbuf
), "short write (%d != %d)", cc
, size
);
172 size
= sizeof(rtmsg
);
175 cc
= read(s
, (char *)rp
, size
);
177 snprintf(errbuf
, sizeof(errbuf
), "read: %.128s", strerror(errno
));
182 } while (rp
->rtm_seq
!= seq
|| rp
->rtm_pid
!= pid
);
186 if (rp
->rtm_version
!= RTM_VERSION
) {
187 snprintf(errbuf
, sizeof(errbuf
), "bad version %d", rp
->rtm_version
);
190 if (rp
->rtm_msglen
> cc
) {
191 snprintf(errbuf
, sizeof(errbuf
), "bad msglen %d > %d", rp
->rtm_msglen
, cc
);
194 if (rp
->rtm_errno
!= 0) {
195 snprintf(errbuf
, sizeof(errbuf
), "rtm_errno: %.128s", strerror(rp
->rtm_errno
));
199 /* Find the interface sockaddr */
200 cp
= (u_char
*)(rp
+ 1);
201 for (i
= 1; i
!= 0; i
<<= 1)
202 if ((i
& rp
->rtm_addrs
) != 0) {
203 sa
= (struct sockaddr
*)cp
;
207 if (sa
->sa_family
== AF_INET
) {
208 ifa
= (struct sockaddr_in
*)cp
;
209 if (ifa
->sin_addr
.s_addr
!= 0) {
222 cp
+= sizeof (u_int32_t
);
224 cp
+= roundup(SALEN(sa
), sizeof (u_int32_t
));
230 #ifndef HAVE_SOCKADDR_SA_LEN
232 salen(struct sockaddr
*sa
)
234 switch (sa
->sa_family
) {
237 return (sizeof(struct sockaddr_in
));
240 return (sizeof(struct sockaddr_dl
));
243 return (sizeof(struct sockaddr
));