VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/portfwd.c@ 52822

最後變更 在這個檔案從52822是 51574,由 vboxsync 提交於 10 年 前

NAT/Net: #define LOG_GROUP LOG_GROUP_NAT_SERVICE

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 5.8 KB
 
1/* -*- indent-tabs-mode: nil; -*- */
2#define LOG_GROUP LOG_GROUP_NAT_SERVICE
3
4#include "winutils.h"
5#include "portfwd.h"
6
7#ifndef RT_OS_WINDOWS
8#include <arpa/inet.h>
9#include <netdb.h>
10#include <poll.h>
11#else
12# include "winpoll.h"
13#endif
14#include <stdio.h>
15#include <string.h>
16
17#include "proxy.h"
18#include "proxy_pollmgr.h"
19#include "pxremap.h"
20
21#include "lwip/netif.h"
22
23
24struct portfwd_msg {
25 struct fwspec *fwspec;
26 int add;
27};
28
29
30static int portfwd_chan_send(struct portfwd_msg *);
31static int portfwd_rule_add_del(struct fwspec *, int);
32static int portfwd_pmgr_chan(struct pollmgr_handler *, SOCKET, int);
33
34
35static struct pollmgr_handler portfwd_pmgr_chan_hdl;
36
37
38void
39portfwd_init(void)
40{
41 portfwd_pmgr_chan_hdl.callback = portfwd_pmgr_chan;
42 portfwd_pmgr_chan_hdl.data = NULL;
43 portfwd_pmgr_chan_hdl.slot = -1;
44 pollmgr_add_chan(POLLMGR_CHAN_PORTFWD, &portfwd_pmgr_chan_hdl);
45
46 /* add preconfigured forwarders */
47 fwtcp_init();
48 fwudp_init();
49}
50
51
52static int
53portfwd_chan_send(struct portfwd_msg *msg)
54{
55 ssize_t nsent;
56
57 nsent = pollmgr_chan_send(POLLMGR_CHAN_PORTFWD, &msg, sizeof(msg));
58 if (nsent < 0) {
59 free(msg);
60 return -1;
61 }
62
63 return 0;
64}
65
66
67static int
68portfwd_rule_add_del(struct fwspec *fwspec, int add)
69{
70 struct portfwd_msg *msg;
71
72 msg = (struct portfwd_msg *)malloc(sizeof(*msg));
73 if (msg == NULL) {
74 return -1;
75 }
76
77 msg->fwspec = fwspec;
78 msg->add = add;
79
80 return portfwd_chan_send(msg);
81}
82
83
84int
85portfwd_rule_add(struct fwspec *fwspec)
86{
87 return portfwd_rule_add_del(fwspec, 1);
88}
89
90
91int
92portfwd_rule_del(struct fwspec *fwspec)
93{
94 return portfwd_rule_add_del(fwspec, 0);
95}
96
97
98/**
99 * POLLMGR_CHAN_PORTFWD handler.
100 */
101static int
102portfwd_pmgr_chan(struct pollmgr_handler *handler, SOCKET fd, int revents)
103{
104 void *ptr = pollmgr_chan_recv_ptr(handler, fd, revents);
105 struct portfwd_msg *msg = (struct portfwd_msg *)ptr;
106
107 if (msg->fwspec->stype == SOCK_STREAM) {
108 if (msg->add) {
109 fwtcp_add(msg->fwspec);
110 }
111 else {
112 fwtcp_del(msg->fwspec);
113 }
114 }
115 else { /* SOCK_DGRAM */
116 if (msg->add) {
117 fwudp_add(msg->fwspec);
118 }
119 else {
120 fwudp_del(msg->fwspec);
121 }
122 }
123
124 free(msg->fwspec);
125 free(msg);
126
127 return POLLIN;
128}
129
130
131int
132fwspec_set(struct fwspec *fwspec, int sdom, int stype,
133 const char *src_addr_str, uint16_t src_port,
134 const char *dst_addr_str, uint16_t dst_port)
135{
136 struct addrinfo hints;
137 struct addrinfo *ai;
138 int status;
139
140 LWIP_ASSERT1(sdom == PF_INET || sdom == PF_INET6);
141 LWIP_ASSERT1(stype == SOCK_STREAM || stype == SOCK_DGRAM);
142
143 fwspec->sdom = sdom;
144 fwspec->stype = stype;
145
146 memset(&hints, 0, sizeof(hints));
147 hints.ai_family = (sdom == PF_INET) ? AF_INET : AF_INET6;
148 hints.ai_socktype = stype;
149 hints.ai_flags = AI_NUMERICHOST;
150
151 status = getaddrinfo(src_addr_str, NULL, &hints, &ai);
152 if (status != 0) {
153 LogRel(("\"%s\": %s\n", src_addr_str, gai_strerror(status)));
154 return -1;
155 }
156 LWIP_ASSERT1(ai != NULL);
157 LWIP_ASSERT1(ai->ai_addrlen <= sizeof(fwspec->src));
158 memcpy(&fwspec->src, ai->ai_addr, ai->ai_addrlen);
159 freeaddrinfo(ai);
160 ai = NULL;
161
162 status = getaddrinfo(dst_addr_str, NULL, &hints, &ai);
163 if (status != 0) {
164 LogRel(("\"%s\": %s\n", dst_addr_str, gai_strerror(status)));
165 return -1;
166 }
167 LWIP_ASSERT1(ai != NULL);
168 LWIP_ASSERT1(ai->ai_addrlen <= sizeof(fwspec->dst));
169 memcpy(&fwspec->dst, ai->ai_addr, ai->ai_addrlen);
170 freeaddrinfo(ai);
171 ai = NULL;
172
173 if (sdom == PF_INET) {
174 fwspec->src.sin.sin_port = htons(src_port);
175 fwspec->dst.sin.sin_port = htons(dst_port);
176 }
177 else { /* PF_INET6 */
178 fwspec->src.sin6.sin6_port = htons(src_port);
179 fwspec->dst.sin6.sin6_port = htons(dst_port);
180 }
181
182 return 0;
183}
184
185
186int
187fwspec_equal(struct fwspec *a, struct fwspec *b)
188{
189 LWIP_ASSERT1(a != NULL);
190 LWIP_ASSERT1(b != NULL);
191
192 if (a->sdom != b->sdom || a->stype != b->stype) {
193 return 0;
194 }
195
196 if (a->sdom == PF_INET) {
197 return a->src.sin.sin_port == b->src.sin.sin_port
198 && a->dst.sin.sin_port == b->dst.sin.sin_port
199 && a->src.sin.sin_addr.s_addr == b->src.sin.sin_addr.s_addr
200 && a->dst.sin.sin_addr.s_addr == b->dst.sin.sin_addr.s_addr;
201 }
202 else { /* PF_INET6 */
203 return a->src.sin6.sin6_port == b->src.sin6.sin6_port
204 && a->dst.sin6.sin6_port == b->dst.sin6.sin6_port
205 && IN6_ARE_ADDR_EQUAL(&a->src.sin6.sin6_addr, &b->src.sin6.sin6_addr)
206 && IN6_ARE_ADDR_EQUAL(&a->dst.sin6.sin6_addr, &b->dst.sin6.sin6_addr);
207 }
208}
209
210
211/**
212 * Set fwdsrc to the IP address of the peer.
213 *
214 * For port-forwarded connections originating from hosts loopback the
215 * source address is set to the address of one of lwIP interfaces.
216 *
217 * Currently we only have one interface so there's not much logic
218 * here. In the future we might need to additionally consult fwspec
219 * and routing table to determine which netif is used for connections
220 * to the specified guest.
221 */
222int
223fwany_ipX_addr_set_src(ipX_addr_t *fwdsrc, const struct sockaddr *peer)
224{
225 int mapping;
226
227 if (peer->sa_family == AF_INET) {
228 const struct sockaddr_in *peer4 = (const struct sockaddr_in *)peer;
229 ip_addr_t peerip4;
230
231 peerip4.addr = peer4->sin_addr.s_addr;
232 mapping = pxremap_inbound_ip4(&fwdsrc->ip4, &peerip4);
233 }
234 else if (peer->sa_family == AF_INET6) {
235 const struct sockaddr_in6 *peer6 = (const struct sockaddr_in6 *)peer;
236 ip6_addr_t peerip6;
237
238 memcpy(&peerip6, &peer6->sin6_addr, sizeof(ip6_addr_t));
239 mapping = pxremap_inbound_ip6(&fwdsrc->ip6, &peerip6);
240 }
241 else {
242 mapping = PXREMAP_FAILED;
243 }
244
245 return mapping;
246}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette