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_seq != seq || rp->rtm_pid != pid);
160	close(s);
161
162
163	if (rp->rtm_version != RTM_VERSION) {
164		sprintf(errbuf, "bad version %d", rp->rtm_version);
165		return (errbuf);
166	}
167	if (rp->rtm_msglen > cc) {
168		sprintf(errbuf, "bad msglen %d > %d", rp->rtm_msglen, cc);
169		return (errbuf);
170	}
171	if (rp->rtm_errno != 0) {
172		sprintf(errbuf, "rtm_errno: %.128s", strerror(rp->rtm_errno));
173		return (errbuf);
174	}
175
176	/* Find the interface sockaddr */
177	cp = (u_char *)(rp + 1);
178	for (i = 1; i != 0; i <<= 1)
179		if ((i & rp->rtm_addrs) != 0) {
180			sa = (struct sockaddr *)cp;
181			switch (i) {
182
183			case RTA_IFA:
184				if (sa->sa_family == AF_INET) {
185					ifa = (struct sockaddr_in *)cp;
186					if (ifa->sin_addr.s_addr != 0) {
187						*from = *ifa;
188						return (NULL);
189					}
190				}
191				break;
192
193			}
194
195			if (SALEN(sa) == 0)
196				cp += sizeof(long);
197			else
198				cp += roundup(SALEN(sa), sizeof(long));
199		}
200
201	return ("failed!");
202}
203
204#ifndef HAVE_SOCKADDR_SA_LEN
205static int
206salen(struct sockaddr *sa)
207{
208	switch (sa->sa_family) {
209
210	case AF_INET:
211		return (sizeof(struct sockaddr_in));
212
213	case AF_LINK:
214		return (sizeof(struct sockaddr_dl));
215
216	default:
217		return (sizeof(struct sockaddr));
218	}
219}
220#endif
221