Project Alice
Loading...
Searching...
No Matches
findsaddr-udp.c
Go to the documentation of this file.
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#ifdef HAVE_CONFIG_H
30#include "config.h"
31#else
32#include "default_config.h"
33#endif
34
35#include <string.h>
36#ifndef WIN32
37# include <sys/types.h>
38# include <sys/socket.h>
39#include <netinet/in.h>
40#else
41# include <winsock2.h>
42# include <ws2tcpip.h> /*sockaddr, addrinfo etc.*/
43# include <stdint.h>
44#endif /*WIN32*/
45
46#include "pcp.h"
47#include "findsaddr.h"
48#include "unp.h"
49
50/*
51 * Return the source address for the given destination address.
52 *
53 * This makes use of proper source address selection in the FreeBSD kernel
54 * even taking jails into account (sys/netinet/in_pcb.c:in_pcbladdr()).
55 * We open a UDP socket, and connect to the destination, letting the kernel
56 * do the bind and then read the source IPv4 address using getsockname(2).
57 * This has multiple advantages: no need to do PF_ROUTE operations possibly
58 * needing special privileges, jails properly taken into account and most
59 * important - getting the result the kernel would give us rather than
60 * best-guessing ourselves.
61 */
62
63#ifndef SOCKET
64#define SOCKET int
65#endif
66
67#ifndef INVALID_SOCKET
68#define INVALID_SOCKET -1
69#endif
70
71const char *findsaddr(const struct sockaddr_in *to,
72 struct in6_addr *from)
73{
74 const char *errstr;
75 struct sockaddr_in cto, cfrom;
76
77 socklen_t len;
78
79 auto s=socket(AF_INET, SOCK_DGRAM, 0);
80 if (s == (unsigned long long)(INVALID_SOCKET))
81 return ("failed to open DGRAM socket for src addr selection.");
82 errstr=NULL;
83 len=sizeof(struct sockaddr_in);
84 memcpy(&cto, to, len);
85 cto.sin_port=htons(65535); /* Dummy port for connect(2). */
86 if (connect(s, (struct sockaddr *)&cto, len) == -1) {
87 errstr="failed to connect to peer for src addr selection.";
88 goto err;
89 }
90
91 if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
92 errstr="failed to get socket name for src addr selection.";
93 goto err;
94 }
95
96 if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
97 errstr="unexpected address family in src addr selection.";
98 goto err;
99 }
100
101 ((uint32_t *)from)[0]=0;
102 ((uint32_t *)from)[1]=0;
103 ((uint32_t *)from)[2]=htonl(0xffff);
104 ((uint32_t *)from)[3]=cfrom.sin_addr.s_addr;
105
106err:
107 (void)CLOSE(s);
108
109 /* No error (string) to return. */
110 return (errstr);
111}
112
113const char *findsaddr6(const struct sockaddr_in6 *to,
114 struct in6_addr *from)
115{
116 const char *errstr;
117 struct sockaddr_in6 cto, cfrom;
118 socklen_t len;
119 uint32_t sock_flg=0;
120
121 if (IN6_IS_ADDR_LOOPBACK(&to->sin6_addr)) {
122 memcpy(from, &to->sin6_addr, sizeof(struct in6_addr));
123 return NULL;
124 }
125
126 auto s=socket(AF_INET6, SOCK_DGRAM, 0);
127 if (s == (unsigned long long)(INVALID_SOCKET))
128 return ("failed to open DGRAM socket for src addr selection.");
129
130 errstr=NULL;
131
132 //Enable Dual-stack socket for Vista and higher
133 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&sock_flg,
134 sizeof(sock_flg)) == -1) {
135 errstr="setsockopt failed to set dual stack mode.";
136 goto err;
137 }
138
139 len=sizeof(struct sockaddr_in6);
140 memcpy(&cto, to, len);
141 cto.sin6_port=htons(65535); /* Dummy port for connect(2). */
142 if (connect(s, (struct sockaddr *)&cto, len) == -1) {
143 errstr="failed to connect to peer for src addr selection.";
144 goto err;
145 }
146
147 if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
148 errstr="failed to get socket name for src addr selection.";
149 goto err;
150 }
151
152 if (len != sizeof(struct sockaddr_in6) || cfrom.sin6_family != AF_INET6) {
153 errstr="unexpected address family in src addr selection.";
154 goto err;
155 }
156
157 memcpy(from->s6_addr, cfrom.sin6_addr.s6_addr, sizeof(struct in6_addr));
158
159err:
160 (void) CLOSE(s);
161
162 /* No error (string) to return. */
163 return (errstr);
164}
165
166/* end */
#define INVALID_SOCKET
Definition: findsaddr-udp.c:68
const char * findsaddr6(const struct sockaddr_in6 *to, struct in6_addr *from)
const char * findsaddr(const struct sockaddr_in *to, struct in6_addr *from)
Definition: findsaddr-udp.c:71
uint uint32_t
#define CLOSE(sockfd)
Definition: pcp_socket.h:62