1/*
2 * Copyright (c) 2000
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD$
34 */
35
36/* XXX Yes this is WAY too complicated */
37
38#ifndef lint
39static const char rcsid[] =
40    "@(#) $Id: findsaddr-socket.c,v 1.1 2000/11/23 20:17:12 leres Exp $ (LBL)";
41#endif
42
43#include <sys/param.h>
44#include <sys/file.h>
45#include <sys/ioctl.h>
46#include <sys/socket.h>
47#ifdef HAVE_SYS_SOCKIO_H
48#include <sys/sockio.h>
49#endif
50#include <sys/time.h>				/* concession to AIX */
51
52#if __STDC__
53struct mbuf;
54struct rtentry;
55#endif
56
57#include <net/if.h>
58#include <net/if_dl.h>
59#include <net/route.h>
60#include <netinet/in.h>
61
62#include <errno.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <unistd.h>
67
68#include "gnuc.h"
69#ifdef HAVE_OS_PROTO_H
70#include "os-proto.h"
71#endif
72
73#include "findsaddr.h"
74
75#ifdef HAVE_SOCKADDR_SA_LEN
76#define SALEN(sa) ((sa)->sa_len)
77#else
78#define SALEN(sa) salen(sa)
79#endif
80
81#ifndef roundup
82#define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */
83#endif
84
85struct rtmsg {
86        struct rt_msghdr rtmsg;
87        u_char data[512];
88};
89
90static 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 } },
94	{ 0 }
95};
96
97#ifndef HAVE_SOCKADDR_SA_LEN
98static int salen(struct sockaddr *);
99#endif
100
101/*
102 * Return the source address for the given destination address
103 */
104const char *
105findsaddr(register const struct sockaddr_in *to,
106    register struct sockaddr_in *from)
107{
108	register struct rt_msghdr *rp;
109	register u_char *cp;
110
111	register struct sockaddr_in *sp, *ifa;
112	register struct sockaddr *sa;
113	register int s, size, cc, seq, i;
114	register pid_t pid;
115	static char errbuf[512];
116
117	s = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
118	if (s < 0) {
119		sprintf(errbuf, "socket: %.128s", strerror(errno));
120		return (errbuf);
121	}
122
123	seq = 0;
124	pid = getpid();
125
126	rp = &rtmsg.rtmsg;
127	rp->rtm_seq = ++seq;
128	cp = (u_char *)(rp + 1);
129
130	sp = (struct sockaddr_in *)cp;
131	*sp = *to;
132	cp += roundup(SALEN((struct sockaddr *)sp), sizeof(u_int32_t));
133
134	size = cp - (u_char *)rp;
135	rp->rtm_msglen = size;
136
137	cc = write(s, (char *)rp, size);
138	if (cc < 0) {
139		sprintf(errbuf, "write: %.128s", strerror(errno));
140		close(s);
141		return (errbuf);
142	}
143	if (cc != size) {
144		sprintf(errbuf, "short write (%d != %d)", cc, size);
145		close(s);
146		return (errbuf);
147	}
148
149	size = sizeof(rtmsg);
150	do {
151		memset(rp, 0, size);
152		cc = read(s, (char *)rp, size);
153		if (cc < 0) {
154			sprintf(errbuf, "read: %.128s", strerror(errno));
155			close(s);
156			return (errbuf);
157		}
158
159	} while (rp->rtm_type != RTM_GET || rp->rtm_seq != seq ||
160	    rp->rtm_pid != pid);
161	close(s);
162
163
164	if (rp->rtm_version != RTM_VERSION) {
165		sprintf(errbuf, "bad version %d", rp->rtm_version);
166		return (errbuf);
167	}
168	if (rp->rtm_msglen > cc) {
169		sprintf(errbuf, "bad msglen %d > %d", rp->rtm_msglen, cc);
170		return (errbuf);
171	}
172	if (rp->rtm_errno != 0) {
173		sprintf(errbuf, "rtm_errno: %.128s", strerror(rp->rtm_errno));
174		return (errbuf);
175	}
176
177	/* Find the interface sockaddr */
178	cp = (u_char *)(rp + 1);
179	for (i = 1; i != 0; i <<= 1)
180		if ((i & rp->rtm_addrs) != 0) {
181			sa = (struct sockaddr *)cp;
182			switch (i) {
183
184			case RTA_IFA:
185				if (sa->sa_family == AF_INET) {
186					ifa = (struct sockaddr_in *)cp;
187					if (ifa->sin_addr.s_addr != 0) {
188						*from = *ifa;
189						return (NULL);
190					}
191				}
192				break;
193
194			}
195
196			if (SALEN(sa) == 0)
197				cp += sizeof(long);
198			else
199				cp += roundup(SALEN(sa), sizeof(long));
200		}
201
202	return ("failed!");
203}
204
205#ifndef HAVE_SOCKADDR_SA_LEN
206static int
207salen(struct sockaddr *sa)
208{
209	switch (sa->sa_family) {
210
211	case AF_INET:
212		return (sizeof(struct sockaddr_in));
213
214	case AF_LINK:
215		return (sizeof(struct sockaddr_dl));
216
217	default:
218		return (sizeof(struct sockaddr));
219	}
220}
221#endif
222