1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* Sample program to be used as a transparent proxy.
*
* Must be executed with permission enough to do an ioctl on /dev/ipl
* or equivalent. This is just a sample and is only alpha quality.
* - Darren Reed (8 April 1996)
*/
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/socket.h>
#if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105000000)
# include <poll.h>
# define USE_POLL
#endif
#include "ip_nat.h"
#define RELAY_BUFSZ 8192
char ibuff[RELAY_BUFSZ];
char obuff[RELAY_BUFSZ];
int relay(ifd, ofd, rfd)
int ifd, ofd, rfd;
{
#ifdef USE_POLL
struct pollfd set[3];
#else
fd_set rfds, wfds;
#endif
char *irh, *irt, *rrh, *rrt;
char *iwh, *iwt, *rwh, *rwt;
int nfd, n, rw;
irh = irt = ibuff;
iwh = iwt = obuff;
nfd = ifd;
if (nfd < ofd)
nfd = ofd;
if (nfd < rfd)
nfd = rfd;
#ifdef USE_POLL
set[0].fd = rfd;
set[1].fd = ifd;
set[2].fd = ofd;
#endif
while (1) {
#ifdef USE_POLL
set[0].events = (iwh < (obuff + RELAY_BUFSZ) ? POLLIN : 0) |
(irh > irt ? POLLOUT : 0);
set[1].events = (irh < (ibuff + RELAY_BUFSZ) ? POLLIN : 0);
set[2].events = (iwh > iwt ? POLLOUT : 0);
switch ((n = poll(set, 3, INFTIM)))
#else
FD_ZERO(&rfds);
FD_ZERO(&wfds);
if (irh > irt)
FD_SET(rfd, &wfds);
if (irh < (ibuff + RELAY_BUFSZ))
FD_SET(ifd, &rfds);
if (iwh > iwt)
FD_SET(ofd, &wfds);
if (iwh < (obuff + RELAY_BUFSZ))
FD_SET(rfd, &rfds);
switch ((n = select(nfd + 1, &rfds, &wfds, NULL, NULL)))
#endif
{
case -1 :
case 0 :
return -1;
default :
#ifdef USE_POLL
if (set[1].revents & POLLIN)
#else
if (FD_ISSET(ifd, &rfds))
#endif
{
rw = read(ifd, irh, ibuff + RELAY_BUFSZ - irh);
if (rw == -1)
return -1;
if (rw == 0)
return 0;
irh += rw;
n--;
}
#ifdef USE_POLL
if (set[2].revents & POLLOUT)
#else
if (n && FD_ISSET(ofd, &wfds))
#endif
{
rw = write(ofd, iwt, iwh - iwt);
if (rw == -1)
return -1;
iwt += rw;
n--;
}
#ifdef USE_POLL
if (set[0].revents & POLLIN)
#else
if (n && FD_ISSET(rfd, &rfds))
#endif
{
rw = read(rfd, iwh, obuff + RELAY_BUFSZ - iwh);
if (rw == -1)
return -1;
if (rw == 0)
return 0;
iwh += rw;
n--;
}
#ifdef USE_POLL
if (set[0].revents & POLLOUT)
#else
if (n && FD_ISSET(rfd, &wfds))
#endif
{
rw = write(rfd, irt, irh - irt);
if (rw == -1)
return -1;
irt += rw;
n--;
}
if (irh == irt)
irh = irt = ibuff;
if (iwh == iwt)
iwh = iwt = obuff;
}
}
}
main(argc, argv)
int argc;
char *argv[];
{
struct sockaddr_in sin;
natlookup_t nl;
natlookup_t *nlp = &nl;
int fd, sl = sizeof(sl), se;
openlog(argv[0], LOG_PID|LOG_NDELAY, LOG_DAEMON);
if ((fd = open("/dev/ipnat", O_RDONLY)) == -1) {
se = errno;
perror("open");
errno = se;
syslog(LOG_ERR, "open: %m\n");
exit(-1);
}
bzero(&nl, sizeof(nl));
nl.nl_flags = IPN_TCP;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sl = sizeof(sin);
if (getsockname(0, (struct sockaddr *)&sin, &sl) == -1) {
se = errno;
perror("getsockname");
errno = se;
syslog(LOG_ERR, "getsockname: %m\n");
exit(-1);
} else {
nl.nl_inip.s_addr = sin.sin_addr.s_addr;
nl.nl_inport = sin.sin_port;
}
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sl = sizeof(sin);
if (getpeername(0, (struct sockaddr *)&sin, &sl) == -1) {
se = errno;
perror("getpeername");
errno = se;
syslog(LOG_ERR, "getpeername: %m\n");
exit(-1);
} else {
nl.nl_outip.s_addr = sin.sin_addr.s_addr;
nl.nl_outport = sin.sin_port;
}
if (ioctl(fd, SIOCGNATL, &nlp) == -1) {
se = errno;
perror("ioctl");
errno = se;
syslog(LOG_ERR, "ioctl: %m\n");
exit(-1);
}
sin.sin_port = nl.nl_realport;
sin.sin_addr = nl.nl_realip;
sl = sizeof(sin);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (connect(fd, (struct sockaddr *)&sin, sl) == -1) {
se = errno;
perror("connect");
errno = se;
syslog(LOG_ERR, "connect: %m\n");
exit(-1);
}
(void) ioctl(fd, F_SETFL, ioctl(fd, F_GETFL, 0)|O_NONBLOCK);
(void) ioctl(0, F_SETFL, ioctl(fd, F_GETFL, 0)|O_NONBLOCK);
(void) ioctl(1, F_SETFL, ioctl(fd, F_GETFL, 0)|O_NONBLOCK);
syslog(LOG_NOTICE, "connected to %s,%d\n", inet_ntoa(sin.sin_addr),
ntohs(sin.sin_port));
if (relay(0, 1, fd) == -1) {
se = errno;
perror("relay");
errno = se;
syslog(LOG_ERR, "relay: %m\n");
exit(-1);
}
exit(0);
}
|