1201806Sbz/*-
2201806Sbz * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
3201806Sbz * All rights reserved.
4201806Sbz *
5201806Sbz * Redistribution and use in source and binary forms, with or without
6201806Sbz * modification, are permitted provided that the following conditions
7201806Sbz * are met:
8201806Sbz * 1. Redistributions of source code must retain the above copyright
9201806Sbz * notice, this list of conditions and the following disclaimer.
10201806Sbz * 2. Redistributions in binary form must reproduce the above copyright
11201806Sbz * notice, this list of conditions and the following disclaimer in the
12201806Sbz * documentation and/or other materials provided with the distribution.
13201806Sbz *
14201806Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15201806Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16201806Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17201806Sbz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18201806Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19201806Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20201806Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21201806Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22201806Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23201806Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24201806Sbz * SUCH DAMAGE.
25201806Sbz *
26201806Sbz * $FreeBSD$
27201806Sbz */
28201806Sbz
29201806Sbz#include <string.h>
30201806Sbz#include <unistd.h>
31201806Sbz
32201806Sbz#include <sys/types.h>
33201806Sbz#include <sys/socket.h>
34201806Sbz
35201806Sbz#include <netinet/in.h>
36201806Sbz
37201806Sbz#include "findsaddr.h"
38201806Sbz#include "traceroute.h"
39201806Sbz
40201806Sbz/*
41201806Sbz * Return the source address for the given destination address.
42201806Sbz *
43201897Sbz * This makes use of proper source address selection in the FreeBSD kernel
44201806Sbz * even taking jails into account (sys/netinet/in_pcb.c:in_pcbladdr()).
45201806Sbz * We open a UDP socket, and connect to the destination, letting the kernel
46201806Sbz * do the bind and then read the source IPv4 address using getsockname(2).
47201806Sbz * This has multiple advantages: no need to do PF_ROUTE operations possibly
48201806Sbz * needing special privileges, jails properly taken into account and most
49201806Sbz * important - getting the result the kernel would give us rather than
50201806Sbz * best-guessing ourselves.
51201806Sbz */
52201806Sbzconst char *
53201806Sbzfindsaddr(register const struct sockaddr_in *to,
54201806Sbz    register struct sockaddr_in *from)
55201806Sbz{
56201806Sbz	const char *errstr;
57201806Sbz	struct sockaddr_in cto, cfrom;
58201806Sbz	int s;
59201806Sbz	socklen_t len;
60201806Sbz
61201806Sbz	s = socket(AF_INET, SOCK_DGRAM, 0);
62201806Sbz	if (s == -1)
63201806Sbz		return ("failed to open DGRAM socket for src addr selection.");
64201806Sbz
65201806Sbz	errstr = NULL;
66201806Sbz	len = sizeof(struct sockaddr_in);
67201806Sbz	memcpy(&cto, to, len);
68201806Sbz	cto.sin_port = htons(65535);	/* Dummy port for connect(2). */
69201806Sbz	if (connect(s, (struct sockaddr *)&cto, len) == -1) {
70201806Sbz		errstr = "failed to connect to peer for src addr selection.";
71201806Sbz		goto err;
72201806Sbz	}
73201806Sbz
74201806Sbz	if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
75201806Sbz		errstr = "failed to get socket name for src addr selection.";
76201806Sbz		goto err;
77201806Sbz	}
78201806Sbz
79201806Sbz	if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
80201806Sbz		errstr = "unexpected address family in src addr selection.";
81201806Sbz		goto err;
82201806Sbz	}
83201806Sbz
84201806Sbz	/* Update source address for traceroute. */
85201806Sbz	setsin(from, cfrom.sin_addr.s_addr);
86201806Sbz
87201806Sbzerr:
88201806Sbz	(void) close(s);
89201806Sbz
90201806Sbz	/* No error (string) to return. */
91201806Sbz	return (errstr);
92201806Sbz}
93201806Sbz
94201806Sbz/* end */
95