2020-02-26 08:38:23 +00:00
|
|
|
@pragma entry main
|
|
|
|
@pragma implicit off
|
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
function destroy_bridge (&brtab, fd, mx)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
@local pfd;
|
|
|
|
|
|
|
|
pfd = brtab[fd];
|
|
|
|
|
|
|
|
print "Info: Destroying", fd, pfd;
|
|
|
|
|
2020-02-26 08:38:23 +00:00
|
|
|
sys::delfrommux (mx, fd);
|
2020-03-02 09:41:11 +00:00
|
|
|
sys::delfrommux (mx, pfd);
|
2020-02-26 08:38:23 +00:00
|
|
|
sys::close (fd);
|
2020-03-02 09:41:11 +00:00
|
|
|
sys::close (pfd);
|
|
|
|
|
|
|
|
delete brtab[fd];
|
|
|
|
delete brtab[fd,"connecting"];
|
|
|
|
delete brtab[fd,"pending"];
|
|
|
|
delete brtab[fd,"evmask"];
|
|
|
|
delete brtab[fd,"eof"];
|
|
|
|
|
|
|
|
delete brtab[pfd];
|
|
|
|
delete brtab[pfd,"connecting"];
|
|
|
|
delete brtab[pfd,"pending"];
|
|
|
|
delete brtab[pfd,"evmask"];
|
|
|
|
delete brtab[pfd,"eof"];
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
function prepare_bridge (&brtab, fd, pfd, mx)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
if (sys::addtomux(mx, pfd, sys::MUX_EVT_OUT) <= -1) return -1;
|
|
|
|
|
|
|
|
print "Info: Preparing", fd, pfd;
|
|
|
|
|
|
|
|
brtab[fd] = pfd;
|
|
|
|
brtab[fd,"evmask"] = 0;
|
|
|
|
|
|
|
|
brtab[pfd] = fd;
|
|
|
|
brtab[pfd,"connecting"] = 1;
|
|
|
|
brtab[pfd,"evmask"] = sys::MUX_EVT_OUT;
|
|
|
|
|
|
|
|
return 0;
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
function establish_bridge (&brtab, fd, mx)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
@local pfd;
|
|
|
|
|
|
|
|
pfd = brtab[fd];
|
|
|
|
if (sys::addtomux(mx, pfd, sys::MUX_EVT_IN) <= -1 ||
|
|
|
|
sys::modinmux(mx, fd, sys::MUX_EVT_IN) <= -1) return -1;
|
|
|
|
|
|
|
|
brtab[fd,"evmask"] = sys::MUX_EVT_IN;
|
|
|
|
brtab[pfd,"evmask"] = sys::MUX_EVT_IN;
|
|
|
|
|
|
|
|
delete brtab[fd,"connecting"];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handle_bridge_eof (&brtab, fd, mx)
|
|
|
|
{
|
|
|
|
@local pfd, evmask;
|
|
|
|
|
|
|
|
pfd = brtab[fd];
|
|
|
|
|
2020-03-04 09:54:38 +00:00
|
|
|
evmask = brtab[fd,"evmask"] & ~sys::MUX_EVT_IN;
|
2020-03-02 09:41:11 +00:00
|
|
|
if (sys::modinmux(mx, fd, evmask) <= -1) return -1;
|
|
|
|
brtab[fd,"evmask"] = evmask;
|
|
|
|
brtab[fd,"eof"] = 1;
|
|
|
|
|
|
|
|
## EOF on fd, disable transmission on pfd if no pending
|
|
|
|
if (length(brtab[pfd,"pending"]) == 0) sys::shutdown (pfd, sys::SHUT_WR);
|
|
|
|
|
|
|
|
# if peer fd reached eof and there are no pending data, arrange to abort the bridge
|
|
|
|
if (brtab[pfd,"eof"] != 0 && length(brtab[pfd,"pending"]) == 0 && length(brtab[fd,"pending"]) == 0) return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-26 08:38:23 +00:00
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
function bridge_traffic (&brtab, fd, mx)
|
|
|
|
{
|
|
|
|
@local len, buf, pfd, pos, x, evmask;
|
|
|
|
|
|
|
|
pfd = brtab[fd];
|
|
|
|
|
|
|
|
len = sys::read(fd, buf, 8096);
|
|
|
|
if (len <= -1)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
return -1;
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
2020-03-02 09:41:11 +00:00
|
|
|
else if (len == 0)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
return handle_bridge_eof (brtab, fd, mx);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = 1;
|
|
|
|
while (pos <= len)
|
|
|
|
{
|
|
|
|
x = sys::write(pfd, buf, pos);
|
|
|
|
if (x == sys::RC_EAGAIN)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-04 09:54:38 +00:00
|
|
|
evmask = brtab[fd,"evmask"] & ~sys::MUX_EVT_IN;
|
2020-03-02 09:41:11 +00:00
|
|
|
if (sys::modinmux(mx, fd, evmask) <= -1) return -1;
|
|
|
|
brtab[fd,"evmask"] = evmask;
|
2020-02-26 08:38:23 +00:00
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
evmask = brtab[pfd,"evmask"] | sys::MUX_EVT_OUT;
|
|
|
|
if (sys::modinmux(mx, pfd, evmask) <= -1) return -1;
|
|
|
|
brtab[pfd,"evmask"] = evmask;
|
2020-02-26 08:38:23 +00:00
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
brtab[pfd,"pending"] %%= substr(buf, pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (x <= -1)
|
|
|
|
{
|
|
|
|
return -1;
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
2020-03-02 09:41:11 +00:00
|
|
|
|
|
|
|
pos += x;
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
2020-03-02 09:41:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
function bridge_pendind_data (&brtab, fd, mx)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
@local pending_data, pos, len, x, pfd, evmask;
|
|
|
|
|
|
|
|
pfd = brtab[fd];
|
|
|
|
pending_data = brtab[fd,"pending"];
|
|
|
|
|
|
|
|
len = length(pending_data);
|
|
|
|
pos = 1;
|
|
|
|
while (pos <= len)
|
|
|
|
{
|
|
|
|
x = sys::write(fd, pending_data, pos);
|
|
|
|
if (x == sys::RC_EAGAIN)
|
|
|
|
{
|
|
|
|
brtab[fd,"pending"] = substr(pending_data, pos);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pos += x;
|
|
|
|
}
|
|
|
|
|
|
|
|
## sent all pending data.
|
|
|
|
delete brtab[fd,"pending"];
|
|
|
|
|
2020-03-04 09:54:38 +00:00
|
|
|
evmask = brtab[fd,"evmask"] & ~sys::MUX_EVT_OUT;
|
2020-03-02 09:41:11 +00:00
|
|
|
if (sys::modinmux(mx, fd, evmask) <= -1) return -1;
|
|
|
|
brtab[fd,"evmask"] = evmask;
|
|
|
|
|
|
|
|
if (brtab[pfd,"eof"] == 0)
|
|
|
|
{
|
|
|
|
evmask = brtab[pfd,"evmask"] | sys::MUX_EVT_IN;
|
|
|
|
if (sys::modinmux(mx, pfd, evmask) <= -1) return -1;
|
|
|
|
brtab[pfd,"evmask"] = evmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-26 08:38:23 +00:00
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
function handle_bridge_event (&brtab, fd, mx, evmask)
|
|
|
|
{
|
|
|
|
if (evmask & sys::MUX_EVT_ERR)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
destroy_bridge (brtab, fd, mx);
|
|
|
|
}
|
|
|
|
else if (evmask & sys::MUX_EVT_HUP)
|
|
|
|
{
|
|
|
|
print "Info: HUP detected on", fd;
|
|
|
|
if (handle_bridge_eof(brtab, fd, mx) <= -1)
|
|
|
|
{
|
|
|
|
destroy_bridge (brtab, fd, mx);
|
|
|
|
}
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
if (evmask & sys::MUX_EVT_OUT)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
if ((fd,"connecting") in brtab)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
## remote peer connection
|
|
|
|
if (establish_bridge(brtab, fd, mx) <= -1)
|
|
|
|
{
|
|
|
|
destroy_bridge (brtab, fd, mx);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
2020-03-02 09:41:11 +00:00
|
|
|
else if ((fd,"pending") in brtab)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
if (bridge_pending_data(brtab, fd, mx) <= -1)
|
|
|
|
{
|
|
|
|
destroy_bridge (brtab, fd, mx);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
2020-03-02 09:41:11 +00:00
|
|
|
}
|
2020-02-26 08:38:23 +00:00
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
if (evmask & sys::MUX_EVT_IN)
|
|
|
|
{
|
|
|
|
if (bridge_traffic(brtab, fd, mx) <= -1)
|
|
|
|
{
|
|
|
|
destroy_bridge (brtab, fd, mx);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function serve_connections (mx, ss, remoteaddr)
|
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
@local brtab, remotetab
|
2020-02-26 08:38:23 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
@local x, i;
|
|
|
|
|
|
|
|
if ((x = sys::waitonmux(mx, 3.10)) <= -1)
|
|
|
|
{
|
2020-03-03 08:09:34 +00:00
|
|
|
if (x == sys::RC_EINTR) continue;
|
2020-02-26 08:38:23 +00:00
|
|
|
print "Error: problem while waiting on multiplexer -", sys::errmsg();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == 0) continue; ## timed out
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < x; i++)
|
|
|
|
{
|
|
|
|
@local fd, evmask;
|
|
|
|
|
|
|
|
if (sys::getmuxevt(mx, i, fd, evmask) <= -1) continue;
|
|
|
|
|
|
|
|
if (fd == ss)
|
|
|
|
{
|
|
|
|
## server socket event
|
|
|
|
@local l, r;
|
|
|
|
|
|
|
|
l = sys::accept(ss, sys::SOCK_CLOEXEC | sys::SOCK_NONBLOCK);
|
|
|
|
if (l <= -1)
|
|
|
|
{
|
|
|
|
print "Error: failed to accept connection -", sys::errmsg();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
r = sys::socket(sys::sockaddrdom(remoteaddr), sys::SOCK_STREAM | sys::SOCK_CLOEXEC | sys::SOCK_NONBLOCK, 0);
|
2020-02-26 08:38:23 +00:00
|
|
|
if (r <= -1)
|
|
|
|
{
|
|
|
|
print "Error: unable to create remote socket for local socket", l, "-", sys::errmsg();
|
|
|
|
sys::close(l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
@local rc;
|
|
|
|
rc = sys::connect(r, remoteaddr);
|
|
|
|
if ((rc <= -1 && rc != sys::RC_EINPROG) || prepare_bridge(brtab, l, r, mx) <= -1)
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
print "Error: unable to conneect to", remoteaddr, "-", sys::errmsg();
|
2020-02-26 08:38:23 +00:00
|
|
|
sys::close (r);
|
|
|
|
sys::close (l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 09:41:11 +00:00
|
|
|
else
|
2020-02-26 08:38:23 +00:00
|
|
|
{
|
2020-03-02 09:41:11 +00:00
|
|
|
if (fd in brtab) handle_bridge_event (brtab, fd, mx, evmask);
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
2020-03-02 09:41:11 +00:00
|
|
|
|
2020-02-26 08:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function main (localaddr, remoteaddr, c)
|
|
|
|
{
|
|
|
|
@local ss, mx;
|
|
|
|
|
|
|
|
if (ARGC != 3)
|
|
|
|
{
|
2020-03-03 08:03:04 +00:00
|
|
|
###printf ("Usage: %s -f %s local-address remote-address\n", ARGV[0], SCRIPTNAME);
|
|
|
|
printf ("Usage: %s local-address remote-address\n", SCRIPTNAME);
|
2020-02-26 08:38:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mx = sys::openmux();
|
|
|
|
if (mx <= -1)
|
|
|
|
{
|
|
|
|
print "Error: unable to create multiplexer -", sys::errmsg();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-02 09:41:11 +00:00
|
|
|
ss = sys::socket(sys::sockaddrdom(localaddr), sys::SOCK_STREAM | sys::SOCK_CLOEXEC | sys::SOCK_NONBLOCK, 0);
|
2020-02-26 08:38:23 +00:00
|
|
|
if (ss <= -1)
|
|
|
|
{
|
|
|
|
print "Error: unable to create socket -", sys::errmsg();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys::setsockopt(ss, sys::SOL_SOCKET, sys::SO_REUSEADDR, 1);
|
|
|
|
sys::setsockopt(ss, sys::SOL_SOCKET, sys::SO_REUSEPORT, 1);
|
|
|
|
|
|
|
|
if (sys::bind(ss, localaddr) <= -1 || sys::listen(ss, 0) <= -1)
|
|
|
|
{
|
|
|
|
print "Error: socket error -", sys::errmsg();
|
|
|
|
sys::close (ss);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sys::addtomux(mx, ss, sys::MUX_EVT_IN) <= -1)
|
|
|
|
{
|
|
|
|
print "Error: unable to add server socket to multiplexer -", sys::errmsg();
|
|
|
|
sys::close (ss);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
serve_connections (mx, ss, remoteaddr);
|
|
|
|
|
|
|
|
sys::delfrommux (mx, ss);
|
|
|
|
sys::close (ss);
|
|
|
|
sys::close (mx);
|
|
|
|
}
|