xref: /freebsd/usr.sbin/traceroute/findsaddr-socket.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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 
34 /* XXX Yes this is WAY too complicated */
35 
36 #include <sys/param.h>
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/time.h>				/* concession to AIX */
42 
43 #if __STDC__
44 struct mbuf;
45 struct rtentry;
46 #endif
47 
48 #include <net/if.h>
49 #include <net/if_dl.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
52 
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include "gnuc.h"
60 #include "os-proto.h"
61 
62 #include "findsaddr.h"
63 
64 #define SALEN(sa) ((sa)->sa_len)
65 
66 #ifndef roundup
67 #define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */
68 #endif
69 
70 struct rtmsg {
71         struct rt_msghdr rtmsg;
72         u_char data[512];
73 };
74 
75 static struct rtmsg rtmsg = {
76 	{ 0, RTM_VERSION, RTM_GET, 0,
77 	RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_STATIC,
78 	RTA_DST | RTA_IFA, 0, 0, 0, 0, 0, { 0 } },
79 	{ 0 }
80 };
81 
82 /*
83  * Return the source address for the given destination address
84  */
85 const char *
86 findsaddr(register const struct sockaddr_in *to,
87     register struct sockaddr_in *from)
88 {
89 	register struct rt_msghdr *rp;
90 	register u_char *cp;
91 
92 	register struct sockaddr_in *sp, *ifa;
93 	register struct sockaddr *sa;
94 	register int s, size, cc, seq, i;
95 	register pid_t pid;
96 	static char errbuf[512];
97 
98 	s = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
99 	if (s < 0) {
100 		sprintf(errbuf, "socket: %.128s", strerror(errno));
101 		return (errbuf);
102 	}
103 
104 	seq = 0;
105 	pid = getpid();
106 
107 	rp = &rtmsg.rtmsg;
108 	rp->rtm_seq = ++seq;
109 	cp = (u_char *)(rp + 1);
110 
111 	sp = (struct sockaddr_in *)cp;
112 	*sp = *to;
113 	cp += roundup(SALEN((struct sockaddr *)sp), sizeof(u_int32_t));
114 
115 	size = cp - (u_char *)rp;
116 	rp->rtm_msglen = size;
117 
118 	cc = write(s, (char *)rp, size);
119 	if (cc < 0) {
120 		sprintf(errbuf, "write: %.128s", strerror(errno));
121 		close(s);
122 		return (errbuf);
123 	}
124 	if (cc != size) {
125 		sprintf(errbuf, "short write (%d != %d)", cc, size);
126 		close(s);
127 		return (errbuf);
128 	}
129 
130 	size = sizeof(rtmsg);
131 	do {
132 		memset(rp, 0, size);
133 		cc = read(s, (char *)rp, size);
134 		if (cc < 0) {
135 			sprintf(errbuf, "read: %.128s", strerror(errno));
136 			close(s);
137 			return (errbuf);
138 		}
139 
140 	} while (rp->rtm_type != RTM_GET || rp->rtm_seq != seq ||
141 	    rp->rtm_pid != pid);
142 	close(s);
143 
144 
145 	if (rp->rtm_version != RTM_VERSION) {
146 		sprintf(errbuf, "bad version %d", rp->rtm_version);
147 		return (errbuf);
148 	}
149 	if (rp->rtm_msglen > cc) {
150 		sprintf(errbuf, "bad msglen %d > %d", rp->rtm_msglen, cc);
151 		return (errbuf);
152 	}
153 	if (rp->rtm_errno != 0) {
154 		sprintf(errbuf, "rtm_errno: %.128s", strerror(rp->rtm_errno));
155 		return (errbuf);
156 	}
157 
158 	/* Find the interface sockaddr */
159 	cp = (u_char *)(rp + 1);
160 	for (i = 1; i != 0; i <<= 1)
161 		if ((i & rp->rtm_addrs) != 0) {
162 			sa = (struct sockaddr *)cp;
163 			switch (i) {
164 
165 			case RTA_IFA:
166 				if (sa->sa_family == AF_INET) {
167 					ifa = (struct sockaddr_in *)cp;
168 					if (ifa->sin_addr.s_addr != 0) {
169 						*from = *ifa;
170 						return (NULL);
171 					}
172 				}
173 				break;
174 
175 			}
176 
177 			if (SALEN(sa) == 0)
178 				cp += sizeof(long);
179 			else
180 				cp += roundup(SALEN(sa), sizeof(long));
181 		}
182 
183 	return ("failed!");
184 }
185