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#include <sys/param.h>
67#include <sys/file.h>
68#include <sys/ioctl.h>
69#include <sys/socket.h>
70#ifdef HAVE_SYS_SOCKIO_H
71#include <sys/sockio.h>
72#endif
73#include <sys/time.h>				/* concession to AIX */
74
75#if __STDC__
76struct mbuf;
77struct rtentry;
78#endif
79
80#include <net/if.h>
81#include <net/if_dl.h>
82#include <net/route.h>
83#include <netinet/in.h>
84
85#include <errno.h>
86#include <stdio.h>
87#include <stdlib.h>
88#include <string.h>
89#include <unistd.h>
90
91#include "gnuc.h"
92#ifdef HAVE_OS_PROTO_H
93#include "os-proto.h"
94#endif
95
96#include "findsaddr.h"
97
98#ifdef HAVE_SOCKADDR_SA_LEN
99#define SALEN(sa) ((sa)->sa_len)
100#else
101#define SALEN(sa) salen(sa)
102#endif
103
104#ifndef roundup
105#define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */
106#endif
107
108struct rtmsg {
109        struct rt_msghdr rtmsg;
110        u_char data[512];
111};
112
113static 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 } },
117	{ 0 }
118};
119
120#ifndef HAVE_SOCKADDR_SA_LEN
121static int salen(struct sockaddr *);
122#endif
123
124/*
125 * Return the source address for the given destination address
126 */
127const char *
128findsaddr(register const struct sockaddr_in *to,
129    register struct sockaddr_in *from)
130{
131	register struct rt_msghdr *rp;
132	register u_char *cp;
133
134	register struct sockaddr_in *sp, *ifa;
135	register struct sockaddr *sa;
136	register int s, size, cc, seq, i;
137	register pid_t pid;
138	static char errbuf[512];
139
140	s = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
141	if (s < 0) {
142		snprintf(errbuf, sizeof(errbuf), "socket: %.128s", strerror(errno));
143		return (errbuf);
144	}
145
146	seq = 0;
147	pid = getpid();
148
149	rp = &rtmsg.rtmsg;
150	rp->rtm_seq = ++seq;
151	cp = (u_char *)(rp + 1);
152
153	sp = (struct sockaddr_in *)cp;
154	*sp = *to;
155	cp += roundup(SALEN((struct sockaddr *)sp), sizeof(u_int32_t));
156
157	size = cp - (u_char *)rp;
158	rp->rtm_msglen = size;
159
160	cc = write(s, (char *)rp, size);
161	if (cc < 0) {
162		snprintf(errbuf, sizeof(errbuf), "write: %.128s", strerror(errno));
163		close(s);
164		return (errbuf);
165	}
166	if (cc != size) {
167		snprintf(errbuf, sizeof(errbuf), "short write (%d != %d)", cc, size);
168		close(s);
169		return (errbuf);
170	}
171
172	size = sizeof(rtmsg);
173	do {
174		memset(rp, 0, size);
175		cc = read(s, (char *)rp, size);
176		if (cc < 0) {
177			snprintf(errbuf, sizeof(errbuf), "read: %.128s", strerror(errno));
178			close(s);
179			return (errbuf);
180		}
181
182	} while (rp->rtm_seq != seq || rp->rtm_pid != pid);
183	close(s);
184
185
186	if (rp->rtm_version != RTM_VERSION) {
187		snprintf(errbuf, sizeof(errbuf), "bad version %d", rp->rtm_version);
188		return (errbuf);
189	}
190	if (rp->rtm_msglen > cc) {
191		snprintf(errbuf, sizeof(errbuf), "bad msglen %d > %d", rp->rtm_msglen, cc);
192		return (errbuf);
193	}
194	if (rp->rtm_errno != 0) {
195		snprintf(errbuf, sizeof(errbuf), "rtm_errno: %.128s", strerror(rp->rtm_errno));
196		return (errbuf);
197	}
198
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;
204			switch (i) {
205
206			case RTA_IFA:
207				if (sa->sa_family == AF_INET) {
208					ifa = (struct sockaddr_in *)cp;
209					if (ifa->sin_addr.s_addr != 0) {
210						*from = *ifa;
211						return (NULL);
212					}
213				}
214				break;
215
216			default:
217				break;
218				/* empty */
219			}
220
221			if (SALEN(sa) == 0)
222				cp += sizeof (u_int32_t);
223			else
224				cp += roundup(SALEN(sa), sizeof (u_int32_t));
225		}
226
227	return ("failed!");
228}
229
230#ifndef HAVE_SOCKADDR_SA_LEN
231static int
232salen(struct sockaddr *sa)
233{
234	switch (sa->sa_family) {
235
236	case AF_INET:
237		return (sizeof(struct sockaddr_in));
238
239	case AF_LINK:
240		return (sizeof(struct sockaddr_dl));
241
242	default:
243		return (sizeof(struct sockaddr));
244	}
245}
246#endif
247