]> git.saurik.com Git - apple/network_cmds.git/blob - traceroute.tproj/findsaddr-socket.c
88ca8bfe2c72119f3d51b8ab5ba1564bdb06bd8e
[apple/network_cmds.git] / traceroute.tproj / findsaddr-socket.c
1 /*
2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * Copyright (c) 2000
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
48 *
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
59 * SUCH DAMAGE.
60 *
61 * $FreeBSD: src/contrib/traceroute/findsaddr-socket.c,v 1.2 2002/07/30 04:49:13 fenner Exp $
62 */
63
64 /* XXX Yes this is WAY too complicated */
65
66 #ifndef lint
67 static const char rcsid[] =
68 "@(#) $Id: findsaddr-socket.c,v 1.3 2005/02/12 00:04:09 lindak Exp $ (LBL)";
69 #endif
70
71 #include <sys/param.h>
72 #include <sys/file.h>
73 #include <sys/ioctl.h>
74 #include <sys/socket.h>
75 #ifdef HAVE_SYS_SOCKIO_H
76 #include <sys/sockio.h>
77 #endif
78 #include <sys/time.h> /* concession to AIX */
79
80 #if __STDC__
81 struct mbuf;
82 struct rtentry;
83 #endif
84
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/route.h>
88 #include <netinet/in.h>
89
90 #include <errno.h>
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <string.h>
94 #include <unistd.h>
95
96 #include "gnuc.h"
97 #ifdef HAVE_OS_PROTO_H
98 #include "os-proto.h"
99 #endif
100
101 #include "findsaddr.h"
102
103 #ifdef HAVE_SOCKADDR_SA_LEN
104 #define SALEN(sa) ((sa)->sa_len)
105 #else
106 #define SALEN(sa) salen(sa)
107 #endif
108
109 #ifndef roundup
110 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
111 #endif
112
113 struct rtmsg {
114 struct rt_msghdr rtmsg;
115 u_char data[512];
116 };
117
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 } },
122 { 0 }
123 };
124
125 #ifndef HAVE_SOCKADDR_SA_LEN
126 static int salen(struct sockaddr *);
127 #endif
128
129 /*
130 * Return the source address for the given destination address
131 */
132 const char *
133 findsaddr(register const struct sockaddr_in *to,
134 register struct sockaddr_in *from)
135 {
136 register struct rt_msghdr *rp;
137 register u_char *cp;
138
139 register struct sockaddr_in *sp, *ifa;
140 register struct sockaddr *sa;
141 register int s, size, cc, seq, i;
142 register pid_t pid;
143 static char errbuf[512];
144
145 s = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
146 if (s < 0) {
147 snprintf(errbuf, sizeof(errbuf), "socket: %.128s", strerror(errno));
148 return (errbuf);
149 }
150
151 seq = 0;
152 pid = getpid();
153
154 rp = &rtmsg.rtmsg;
155 rp->rtm_seq = ++seq;
156 cp = (u_char *)(rp + 1);
157
158 sp = (struct sockaddr_in *)cp;
159 *sp = *to;
160 cp += roundup(SALEN((struct sockaddr *)sp), sizeof(u_int32_t));
161
162 size = cp - (u_char *)rp;
163 rp->rtm_msglen = size;
164
165 cc = write(s, (char *)rp, size);
166 if (cc < 0) {
167 snprintf(errbuf, sizeof(errbuf), "write: %.128s", strerror(errno));
168 close(s);
169 return (errbuf);
170 }
171 if (cc != size) {
172 snprintf(errbuf, sizeof(errbuf), "short write (%d != %d)", cc, size);
173 close(s);
174 return (errbuf);
175 }
176
177 size = sizeof(rtmsg);
178 do {
179 memset(rp, 0, size);
180 cc = read(s, (char *)rp, size);
181 if (cc < 0) {
182 snprintf(errbuf, sizeof(errbuf), "read: %.128s", strerror(errno));
183 close(s);
184 return (errbuf);
185 }
186
187 } while (rp->rtm_seq != seq || rp->rtm_pid != pid);
188 close(s);
189
190
191 if (rp->rtm_version != RTM_VERSION) {
192 snprintf(errbuf, sizeof(errbuf), "bad version %d", rp->rtm_version);
193 return (errbuf);
194 }
195 if (rp->rtm_msglen > cc) {
196 snprintf(errbuf, sizeof(errbuf), "bad msglen %d > %d", rp->rtm_msglen, cc);
197 return (errbuf);
198 }
199 if (rp->rtm_errno != 0) {
200 snprintf(errbuf, sizeof(errbuf), "rtm_errno: %.128s", strerror(rp->rtm_errno));
201 return (errbuf);
202 }
203
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;
209 switch (i) {
210
211 case RTA_IFA:
212 if (sa->sa_family == AF_INET) {
213 ifa = (struct sockaddr_in *)cp;
214 if (ifa->sin_addr.s_addr != 0) {
215 *from = *ifa;
216 return (NULL);
217 }
218 }
219 break;
220
221 default:
222 break;
223 /* empty */
224 }
225
226 if (SALEN(sa) == 0)
227 cp += sizeof (u_int32_t);
228 else
229 cp += roundup(SALEN(sa), sizeof (u_int32_t));
230 }
231
232 return ("failed!");
233 }
234
235 #ifndef HAVE_SOCKADDR_SA_LEN
236 static int
237 salen(struct sockaddr *sa)
238 {
239 switch (sa->sa_family) {
240
241 case AF_INET:
242 return (sizeof(struct sockaddr_in));
243
244 case AF_LINK:
245 return (sizeof(struct sockaddr_dl));
246
247 default:
248 return (sizeof(struct sockaddr));
249 }
250 }
251 #endif