xref: /freebsd/usr.sbin/traceroute/findsaddr-udp.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1 /*-
2  * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
3  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 
35 #include <netinet/in.h>
36 
37 #include "findsaddr.h"
38 #include "traceroute.h"
39 
40 /*
41  * Return the source address for the given destination address.
42  *
43  * This makes use of proper source address selection in the FreeBSD kernel
44  * even taking jails into account (sys/netinet/in_pcb.c:in_pcbladdr()).
45  * We open a UDP socket, and connect to the destination, letting the kernel
46  * do the bind and then read the source IPv4 address using getsockname(2).
47  * This has multiple advantages: no need to do PF_ROUTE operations possibly
48  * needing special privileges, jails properly taken into account and most
49  * important - getting the result the kernel would give us rather than
50  * best-guessing ourselves.
51  */
52 const char *
53 findsaddr(register const struct sockaddr_in *to,
54     register struct sockaddr_in *from)
55 {
56 	const char *errstr;
57 	struct sockaddr_in cto, cfrom;
58 	int s;
59 	socklen_t len;
60 
61 	s = socket(AF_INET, SOCK_DGRAM, 0);
62 	if (s == -1)
63 		return ("failed to open DGRAM socket for src addr selection.");
64 
65 	errstr = NULL;
66 	len = sizeof(struct sockaddr_in);
67 	memcpy(&cto, to, len);
68 	cto.sin_port = htons(65535);	/* Dummy port for connect(2). */
69 	if (connect(s, (struct sockaddr *)&cto, len) == -1) {
70 		errstr = "failed to connect to peer for src addr selection.";
71 		goto err;
72 	}
73 
74 	if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
75 		errstr = "failed to get socket name for src addr selection.";
76 		goto err;
77 	}
78 
79 	if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
80 		errstr = "unexpected address family in src addr selection.";
81 		goto err;
82 	}
83 
84 	/* Update source address for traceroute. */
85 	setsin(from, cfrom.sin_addr.s_addr);
86 
87 err:
88 	(void) close(s);
89 
90 	/* No error (string) to return. */
91 	return (errstr);
92 }
93 
94 /* end */
95