readelf: report ARM program and section header types
[freebsd-src.git] / contrib / traceroute / findsaddr-socket.c
blob00af84366c52f483ef9d304d99cb49ece2aa5f21
1 /*
2 * Copyright (c) 2000
3 * The Regents of the University of California. All rights reserved.
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.
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.
33 * $FreeBSD$
36 /* XXX Yes this is WAY too complicated */
38 #ifndef lint
39 static const char rcsid[] =
40 "@(#) $Id: findsaddr-socket.c,v 1.1 2000/11/23 20:17:12 leres Exp $ (LBL)";
41 #endif
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 */
52 #if __STDC__
53 struct mbuf;
54 struct rtentry;
55 #endif
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <netinet/in.h>
62 #include <errno.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
68 #include "gnuc.h"
69 #ifdef HAVE_OS_PROTO_H
70 #include "os-proto.h"
71 #endif
73 #include "findsaddr.h"
75 #ifdef HAVE_SOCKADDR_SA_LEN
76 #define SALEN(sa) ((sa)->sa_len)
77 #else
78 #define SALEN(sa) salen(sa)
79 #endif
81 #ifndef roundup
82 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
83 #endif
85 struct rtmsg {
86 struct rt_msghdr rtmsg;
87 u_char data[512];
90 static 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 }
97 #ifndef HAVE_SOCKADDR_SA_LEN
98 static int salen(struct sockaddr *);
99 #endif
102 * Return the source address for the given destination address
104 const char *
105 findsaddr(register const struct sockaddr_in *to,
106 register struct sockaddr_in *from)
108 register struct rt_msghdr *rp;
109 register u_char *cp;
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];
117 s = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
118 if (s < 0) {
119 sprintf(errbuf, "socket: %.128s", strerror(errno));
120 return (errbuf);
123 seq = 0;
124 pid = getpid();
126 rp = &rtmsg.rtmsg;
127 rp->rtm_seq = ++seq;
128 cp = (u_char *)(rp + 1);
130 sp = (struct sockaddr_in *)cp;
131 *sp = *to;
132 cp += roundup(SALEN((struct sockaddr *)sp), sizeof(u_int32_t));
134 size = cp - (u_char *)rp;
135 rp->rtm_msglen = size;
137 cc = write(s, (char *)rp, size);
138 if (cc < 0) {
139 sprintf(errbuf, "write: %.128s", strerror(errno));
140 close(s);
141 return (errbuf);
143 if (cc != size) {
144 sprintf(errbuf, "short write (%d != %d)", cc, size);
145 close(s);
146 return (errbuf);
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);
159 } while (rp->rtm_seq != seq || rp->rtm_pid != pid);
160 close(s);
163 if (rp->rtm_version != RTM_VERSION) {
164 sprintf(errbuf, "bad version %d", rp->rtm_version);
165 return (errbuf);
167 if (rp->rtm_msglen > cc) {
168 sprintf(errbuf, "bad msglen %d > %d", rp->rtm_msglen, cc);
169 return (errbuf);
171 if (rp->rtm_errno != 0) {
172 sprintf(errbuf, "rtm_errno: %.128s", strerror(rp->rtm_errno));
173 return (errbuf);
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) {
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);
191 break;
195 if (SALEN(sa) == 0)
196 cp += sizeof(long);
197 else
198 cp += roundup(SALEN(sa), sizeof(long));
201 return ("failed!");
204 #ifndef HAVE_SOCKADDR_SA_LEN
205 static int
206 salen(struct sockaddr *sa)
208 switch (sa->sa_family) {
210 case AF_INET:
211 return (sizeof(struct sockaddr_in));
213 case AF_LINK:
214 return (sizeof(struct sockaddr_dl));
216 default:
217 return (sizeof(struct sockaddr));
220 #endif