mirror of
https://github.com/samhocevar/rinetd.git
synced 2026-08-24 07:23:28 +08:00
Compare commits
32 Commits
v0.70
...
feature-ip
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
655584ad78 | ||
|
|
4c5a7a1a22 | ||
|
|
7790a617d8 | ||
|
|
7781e97734 | ||
|
|
79bafee414 | ||
|
|
b7fcf65a97 | ||
|
|
c9de417f2e | ||
|
|
26b0e63bf5 | ||
|
|
f58db7ea8d | ||
|
|
7ef50421c0 | ||
|
|
0ef4f1042f | ||
|
|
f7cd21f91f | ||
|
|
a5905bbcea | ||
|
|
a1d2c297ba | ||
|
|
5a70f2bf4e | ||
|
|
7e287f5913 | ||
|
|
4f64342ed5 | ||
|
|
44f973d9fd | ||
|
|
a8ca595620 | ||
|
|
2f8fd87518 | ||
|
|
04d1b74e24 | ||
|
|
2681d17eb4 | ||
|
|
8bde6f6259 | ||
|
|
ae9af9d030 | ||
|
|
ebd31cc72e | ||
|
|
da497404ab | ||
|
|
d9f661ee5d | ||
|
|
0ea83281e3 | ||
|
|
a37e13c910 | ||
|
|
3e18951d7a | ||
|
|
9d140d8f87 | ||
|
|
dd17267afe |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
.auto
|
||||
.deps
|
||||
.dirstamp
|
||||
Makefile
|
||||
Makefile.in
|
||||
stamp-*
|
||||
@@ -13,4 +14,8 @@ configure
|
||||
|
||||
*.o
|
||||
rinetd
|
||||
rinetd.exe
|
||||
|
||||
*.vcxproj.user
|
||||
Debug
|
||||
Release
|
||||
|
||||
339
COPYING
Normal file
339
COPYING
Normal file
@@ -0,0 +1,339 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
||||
28
Makefile.am
28
Makefile.am
@@ -1,35 +1,15 @@
|
||||
|
||||
AUTOMAKE_OPTIONS = foreign dist-bzip2
|
||||
|
||||
EXTRA_DIST = bootstrap CHANGES.md README.md index.html \
|
||||
parse.peg getopt.c getopt.h $(man_MANS) $(sysconf_DATA)
|
||||
SUBDIRS = src
|
||||
|
||||
EXTRA_DIST = bootstrap COPYING CHANGES.md README.md index.html \
|
||||
$(man_MANS) $(sysconf_DATA)
|
||||
|
||||
man_MANS = rinetd.8
|
||||
sysconf_DATA = rinetd.conf
|
||||
noinst_DATA = index.html
|
||||
|
||||
sbin_PROGRAMS = rinetd
|
||||
rinetd_SOURCES = rinetd.c rinetd.h \
|
||||
parse.c parse.h \
|
||||
match.c match.h \
|
||||
net.c net.h \
|
||||
types.h
|
||||
|
||||
BUILT_SOURCES = parse.c
|
||||
|
||||
# If peg/leg is available, regenerate parse.h
|
||||
parse.c: parse.peg
|
||||
if which leg >/dev/null 2>&1; then leg -o $@ $^; else touch $@; fi
|
||||
|
||||
# If roffit is available, regenerate index.html
|
||||
index.html: rinetd.8
|
||||
if which roffit >/dev/null 2>&1; then roffit < $^ > $@; else touch $@; fi
|
||||
|
||||
# _POSIX_C_SOURCE is for SA_RESTART and others
|
||||
# _XOPEN_SOURCE is for struct sigaction
|
||||
# _GNU_SOURCE is for h_errno and gethostbyname-related macros
|
||||
rinetd_CFLAGS = -std=c99 -D_XOPEN_SOURCE -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Wwrite-strings -I.
|
||||
|
||||
# This is for Travis CI; we don’t have a testsuite yet.
|
||||
test:
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
AC_PREREQ(2.52)
|
||||
AC_INIT(rinetd, 0.70, sam@hocevar.net)
|
||||
AC_CONFIG_AUX_DIR(.auto)
|
||||
AC_CONFIG_SRCDIR([getopt.h])
|
||||
AC_CONFIG_HEADER([config.h])
|
||||
AC_CONFIG_SRCDIR([src/rinetd.c])
|
||||
AC_CONFIG_HEADER([src/config.h])
|
||||
AM_INIT_AUTOMAKE([no-define tar-ustar silent-rules])
|
||||
|
||||
# Checks for programs.
|
||||
@@ -21,6 +21,8 @@ AC_CHECK_HEADERS([arpa/inet.h errno.h fcntl.h netdb.h netinet/in.h stdlib.h stri
|
||||
AC_C_CONST
|
||||
AC_HEADER_TIME
|
||||
AC_STRUCT_TM
|
||||
AC_CHECK_TYPES(ssize_t, [], [],
|
||||
[#include <sys/types.h>])
|
||||
AC_CHECK_TYPES(socklen_t, [], [],
|
||||
[#include <sys/types.h>
|
||||
#include <sys/socket.h>])
|
||||
@@ -34,4 +36,5 @@ AC_FUNC_STRFTIME
|
||||
AC_CHECK_FUNCS([gethostbyname memset select socket strstr daemon fork sigaction])
|
||||
|
||||
AC_CONFIG_FILES([Makefile])
|
||||
AC_CONFIG_FILES([src/Makefile])
|
||||
AC_OUTPUT
|
||||
|
||||
35
net.c
35
net.c
@@ -1,35 +0,0 @@
|
||||
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
|
||||
and Boutell.Com, Inc.
|
||||
© 2003—2017 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
This software is released for free use under the terms of
|
||||
the GNU Public License, version 2 or higher. NO WARRANTY
|
||||
IS EXPRESSED OR IMPLIED. USE THIS SOFTWARE AT YOUR OWN RISK. */
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "net.h"
|
||||
|
||||
void setSocketDefaults(SOCKET fd)
|
||||
{
|
||||
/* Make socket non-blocking (FIXME: this uses legacy API) */
|
||||
FIONBIO_ARG_T ioctltmp = 1;
|
||||
#if _WIN32
|
||||
ioctlsocket(fd, FIONBIO, &ioctltmp);
|
||||
#else
|
||||
ioctl(fd, FIONBIO, &ioctltmp);
|
||||
#endif
|
||||
|
||||
#if defined __linux__
|
||||
int tmp = 0;
|
||||
setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmp, sizeof(tmp));
|
||||
#endif
|
||||
|
||||
#if !defined __linux__ && !defined _WIN32
|
||||
int tmp = 1024;
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp));
|
||||
#endif
|
||||
}
|
||||
|
||||
110
rinetd.vcxproj
Normal file
110
rinetd.vcxproj
Normal file
@@ -0,0 +1,110 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\getopt.c" />
|
||||
<ClCompile Include="src\match.c" />
|
||||
<ClCompile Include="src\net.c" />
|
||||
<ClCompile Include="src\parse.c" />
|
||||
<ClCompile Include="src\rinetd.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\getopt.h" />
|
||||
<ClInclude Include="src\match.h" />
|
||||
<ClInclude Include="src\net.h" />
|
||||
<ClInclude Include="src\parse.h" />
|
||||
<ClInclude Include="src\rinetd.h" />
|
||||
<ClInclude Include="src\types.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="src\parse.peg" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{3F1D9D58-3205-48E9-9C93-557291A45D74}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>rinetd</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration">
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Configuration">
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<CompileAs>CompileAsC</CompileAs>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PreprocessorDefinitions>_CONSOLE;PACKAGE_VERSION="0.70";_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<ClCompile>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
|
||||
<ClCompile>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
27
src/Makefile.am
Normal file
27
src/Makefile.am
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
EXTRA_DIST = parse.peg getopt.c getopt.h
|
||||
|
||||
sbin_PROGRAMS = ../rinetd
|
||||
___rinetd_SOURCES = rinetd.c rinetd.h \
|
||||
parse.c parse.h \
|
||||
match.c match.h \
|
||||
net.c net.h \
|
||||
types.h
|
||||
|
||||
BUILT_SOURCES = parse.c
|
||||
|
||||
# If peg/leg is available, regenerate parse.h
|
||||
parse.c: parse.peg
|
||||
if which leg >/dev/null 2>&1; then leg -o $@ $^; else touch $@; fi
|
||||
|
||||
# If roffit is available, regenerate index.html
|
||||
index.html: rinetd.8
|
||||
if which roffit >/dev/null 2>&1; then roffit < $^ > $@; else touch $@; fi
|
||||
|
||||
# _POSIX_C_SOURCE is for SA_RESTART and others
|
||||
# _XOPEN_SOURCE is for struct sigaction
|
||||
# _GNU_SOURCE is for h_errno and gethostbyname-related macros
|
||||
___rinetd_CFLAGS = -std=c99 \
|
||||
-D_XOPEN_SOURCE -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L \
|
||||
-Wall -Wextra -Wwrite-strings
|
||||
|
||||
69
src/net.c
Normal file
69
src/net.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
|
||||
and Boutell.Com, Inc.
|
||||
© 2003—2021 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
This software is released for free use under the terms of
|
||||
the GNU Public License, version 2 or higher. NO WARRANTY
|
||||
IS EXPRESSED OR IMPLIED. USE THIS SOFTWARE AT YOUR OWN RISK. */
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "net.h"
|
||||
|
||||
void setSocketDefaults(SOCKET fd) {
|
||||
/* Make socket non-blocking (FIXME: this uses legacy API) */
|
||||
FIONBIO_ARG_T ioctltmp = 1;
|
||||
#if _WIN32
|
||||
ioctlsocket(fd, FIONBIO, &ioctltmp);
|
||||
#else
|
||||
ioctl(fd, FIONBIO, &ioctltmp);
|
||||
#endif
|
||||
|
||||
#if defined __linux__
|
||||
int tmp = 0;
|
||||
setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmp, sizeof(tmp));
|
||||
#endif
|
||||
|
||||
#if !defined __linux__ && !defined _WIN32
|
||||
int tmp = 1024;
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp));
|
||||
#endif
|
||||
}
|
||||
|
||||
int getSocketType(int protocol) {
|
||||
return protocol == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
|
||||
}
|
||||
|
||||
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b) {
|
||||
if (a->ss_family != b->ss_family)
|
||||
return 0;
|
||||
|
||||
switch (a->ss_family) {
|
||||
case AF_INET: {
|
||||
struct sockaddr_in *a4 = (struct sockaddr_in *)a;
|
||||
struct sockaddr_in *b4 = (struct sockaddr_in *)b;
|
||||
return a4->sin_port == b4->sin_port
|
||||
&& a4->sin_addr.s_addr == b4->sin_addr.s_addr;
|
||||
}
|
||||
case AF_INET6: {
|
||||
struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)a;
|
||||
struct sockaddr_in6 *b6 = (struct sockaddr_in6 *)b;
|
||||
return a6->sin6_port == b6->sin6_port
|
||||
&& a6->sin6_addr.s6_addr == b6->sin6_addr.s6_addr;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t getPort(struct addrinfo* ai) {
|
||||
switch (ai->ai_family) {
|
||||
case AF_INET:
|
||||
return ntohs(((struct sockaddr_in*)ai->ai_addr)->sin_port);
|
||||
case AF_INET6:
|
||||
return ntohs(((struct sockaddr_in6*)ai->ai_addr)->sin6_port);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
|
||||
and Boutell.Com, Inc.
|
||||
© 2003—2017 Sam Hocevar <sam@hocevar.net>
|
||||
© 2003—2021 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
This software is released for free use under the terms of
|
||||
the GNU Public License, version 2 or higher. NO WARRANTY
|
||||
@@ -12,6 +12,8 @@
|
||||
/* Define this to a reasonably large value */
|
||||
# define FD_SETSIZE 4096
|
||||
# include <winsock2.h>
|
||||
# include <ws2ipdef.h>
|
||||
# include <ws2tcpip.h>
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
@@ -22,6 +24,8 @@
|
||||
# include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined HAVE_ERRNO_H
|
||||
# include <errno.h>
|
||||
#endif
|
||||
@@ -36,6 +40,12 @@
|
||||
# include <sys/filio.h>
|
||||
#endif /* FIONBIO */
|
||||
|
||||
#if HAVE_SSIZE_T
|
||||
# define SSIZE_T ssize_t
|
||||
#else
|
||||
# define SSIZE_T int
|
||||
#endif
|
||||
|
||||
#if HAVE_SOCKLEN_T
|
||||
# define SOCKLEN_T socklen_t
|
||||
#else
|
||||
@@ -71,4 +81,6 @@ static inline int GetLastError(void) {
|
||||
#endif /* _WIN32 */
|
||||
|
||||
void setSocketDefaults(SOCKET fd);
|
||||
|
||||
int getSocketType(int protocol);
|
||||
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b);
|
||||
uint16_t getPort(struct addrinfo* ai);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
# Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
|
||||
# and Boutell.Com, Inc.
|
||||
# © 2003—2017 Sam Hocevar <sam@hocevar.net>
|
||||
# © 2003—2021 Sam Hocevar <sam@hocevar.net>
|
||||
#
|
||||
# This software is released for free use under the terms of
|
||||
# the GNU Public License, version 2 or higher. NO WARRANTY
|
||||
@@ -21,9 +21,8 @@
|
||||
FILE *fp; \
|
||||
int currentLine; \
|
||||
int isAuthAllow; \
|
||||
char *tmpPort; \
|
||||
int tmpPortNum, tmpProto; \
|
||||
int bindPortNum, bindProto, connectPortNum, connectProto; \
|
||||
char *tmpPort, *bindPort, *connectPort; \
|
||||
int tmpProto, bindProto, connectProto; \
|
||||
int serverTimeout; \
|
||||
char *bindAddress, *connectAddress, *sourceAddress;
|
||||
#define YY_INPUT(yyctx, buf, result, max_size) \
|
||||
@@ -50,8 +49,8 @@ comment = "#" (!eol .)*
|
||||
|
||||
server-rule = bind-address - bind-port - connect-address - connect-port (-? server-options)?
|
||||
{
|
||||
addServer(yy->bindAddress, yy->bindPortNum, yy->bindProto,
|
||||
yy->connectAddress, yy->connectPortNum, yy->connectProto,
|
||||
addServer(yy->bindAddress, yy->bindPort, yy->bindProto,
|
||||
yy->connectAddress, yy->connectPort, yy->connectProto,
|
||||
yy->serverTimeout > 0 ? yy->serverTimeout : RINETD_DEFAULT_UDP_TIMEOUT,
|
||||
yy->sourceAddress);
|
||||
yy->bindAddress = yy->connectAddress = yy->sourceAddress = NULL;
|
||||
@@ -60,8 +59,8 @@ server-rule = bind-address - bind-port - connect-address - connect-port (-? se
|
||||
|
||||
bind-address = < address > { yy->bindAddress = strdup(yytext); }
|
||||
connect-address = < address > { yy->connectAddress = strdup(yytext); }
|
||||
bind-port = full-port { yy->bindPortNum = yy->tmpPortNum; yy->bindProto = yy->tmpProto; }
|
||||
connect-port = full-port { yy->connectPortNum = yy->tmpPortNum; yy->connectProto = yy->tmpProto; }
|
||||
bind-port = full-port { yy->bindPort = yy->tmpPort; yy->bindProto = yy->tmpProto; }
|
||||
connect-port = full-port { yy->connectPort = yy->tmpPort; yy->connectProto = yy->tmpProto; }
|
||||
server-options = "[" -? option-list -? "]"
|
||||
|
||||
option-list = option (-? ',' -? option-list)?
|
||||
@@ -70,20 +69,11 @@ option-timeout = "timeout" -? "=" -? < number > { yy->serverTimeout = atoi(
|
||||
option-source = "src" -? "=" -? < address > { yy->sourceAddress = strdup(yytext); }
|
||||
|
||||
full-port = port proto
|
||||
{
|
||||
char const *proto = yy->tmpProto == protoTcp ? "tcp" : "udp";
|
||||
struct servent *service = getservbyname(yy->tmpPort, proto);
|
||||
yy->tmpPortNum = service ? ntohs(service->s_port) : atoi(yy->tmpPort);
|
||||
if (yy->tmpPortNum == 0 || yy->tmpPortNum >= 65536) {
|
||||
syslog(LOG_ERR, "port %s/%s missing or out of range\n", yy->tmpPort, proto);
|
||||
PARSE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
port = < (number | service) > { yy->tmpPort = strdup(yytext); }
|
||||
proto = '/tcp' { yy->tmpProto = protoTcp; }
|
||||
| '/udp' { yy->tmpProto = protoUdp; }
|
||||
| '' { yy->tmpProto = protoTcp; }
|
||||
proto = '/tcp' { yy->tmpProto = IPPROTO_TCP; }
|
||||
| '/udp' { yy->tmpProto = IPPROTO_UDP; }
|
||||
| '' { yy->tmpProto = IPPROTO_TCP; }
|
||||
|
||||
auth-rule = auth-key - < pattern >
|
||||
{
|
||||
@@ -139,20 +129,24 @@ invalid_syntax = < (!eol .)+ > eol
|
||||
}
|
||||
|
||||
service = name
|
||||
address = ipv4 | hostname # TODO: IPV6
|
||||
pattern = [0-9*?]+ ('.' [0-9*?]+ ('.' [0-9*?]+ ('.' [0-9*?]+)?)?)?
|
||||
address = ipv4 | ipv6 | hostname
|
||||
pattern = (hexdigit | '[' | ']' | ':' | '.' | [*?] )+
|
||||
number = digit+
|
||||
|
||||
ipv4 = number '.' number '.' number '.' number | '0'
|
||||
hostname = ([-0-9a-zA-Z_]+ '.')* [a-zA-Z][-0-9a-zA-Z_]* '.'?
|
||||
ipv6 = (hexdigit | ':')+ | '[' (hexdigit | ':')+ ']'
|
||||
hostname = (label '.')* name '.'?
|
||||
|
||||
|
||||
name = [a-zA-Z][a-zA-Z0-9_]*
|
||||
name = id ('-' | id | digit)*
|
||||
label = (id | digit) ('-' | id | digit)* # DNS labels may have digits
|
||||
filename = '"' [^"]+ '"'
|
||||
| [^ \t\r\n]+
|
||||
| [^ \t\r\n]+
|
||||
|
||||
- = [ \t]+
|
||||
digit = [0-9]
|
||||
hexdigit = [0-9a-fA-F]
|
||||
id = [a-zA-Z_]
|
||||
sol = { ++yy->currentLine; }
|
||||
eol = '\r'? '\n' | eof
|
||||
eof = '\0'
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
|
||||
and Boutell.Com, Inc.
|
||||
© 2003—2017 Sam Hocevar <sam@hocevar.net>
|
||||
© 2003—2021 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
This software is released for free use under the terms of
|
||||
the GNU Public License, version 2 or higher. NO WARRANTY
|
||||
@@ -14,6 +14,10 @@
|
||||
# define RETSIGTYPE void
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# include <malloc.h>
|
||||
#endif
|
||||
|
||||
#if _WIN32
|
||||
# include "getopt.h"
|
||||
#else
|
||||
@@ -76,7 +80,7 @@ int logFormatCommon = 0;
|
||||
FILE *logFile = NULL;
|
||||
|
||||
char const *logMessages[] = {
|
||||
"unknown-error",
|
||||
"unknown-error",
|
||||
"done-local-closed",
|
||||
"done-remote-closed",
|
||||
"accept-failed -",
|
||||
@@ -115,7 +119,6 @@ static void handleClose(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
|
||||
static void handleAccept(ServerInfo const *srv);
|
||||
static ConnectionInfo *findAvailableConnection(void);
|
||||
static void setConnectionCount(int newCount);
|
||||
static int getAddress(char const *host, struct in_addr *iaddr);
|
||||
static int checkConnectionAllowed(ConnectionInfo const *cnx);
|
||||
|
||||
static int readArgs (int argc, char **argv, RinetdOptions *options);
|
||||
@@ -207,6 +210,11 @@ static void clearConfiguration(void) {
|
||||
}
|
||||
free(srv->fromHost);
|
||||
free(srv->toHost);
|
||||
freeaddrinfo(srv->fromAddrInfo);
|
||||
freeaddrinfo(srv->toAddrInfo);
|
||||
if (srv->sourceAddrInfo) {
|
||||
freeaddrinfo(srv->sourceAddrInfo);
|
||||
}
|
||||
}
|
||||
/* Free memory associated with previous set. */
|
||||
free(seInfo);
|
||||
@@ -238,7 +246,7 @@ static void readConfiguration(char const *file) {
|
||||
logFile = NULL;
|
||||
}
|
||||
if (logFileName) {
|
||||
logFile = fopen(logFileName, "a");
|
||||
logFile = fopen(logFileName, "a+");
|
||||
if (logFile) {
|
||||
setvbuf(logFile, NULL, _IONBF, 0);
|
||||
} else {
|
||||
@@ -248,101 +256,112 @@ static void readConfiguration(char const *file) {
|
||||
}
|
||||
}
|
||||
|
||||
void addServer(char *bindAddress, int bindPort, int bindProto,
|
||||
char *connectAddress, int connectPort, int connectProto,
|
||||
void addServer(char *bindAddress, char *bindPort, int bindProtocol,
|
||||
char *connectAddress, char *connectPort, int connectProtocol,
|
||||
int serverTimeout, char *sourceAddress)
|
||||
{
|
||||
/* Turn all of this stuff into reasonable addresses */
|
||||
struct in_addr iaddr;
|
||||
if (getAddress(bindAddress, &iaddr) < 0) {
|
||||
fprintf(stderr, "rinetd: host %s could not be resolved.\n",
|
||||
bindAddress);
|
||||
exit(1);
|
||||
}
|
||||
struct in_addr isourceaddr;
|
||||
isourceaddr.s_addr = INADDR_ANY;
|
||||
if (sourceAddress && getAddress(sourceAddress, &isourceaddr) < 0) {
|
||||
fprintf(stderr, "rinetd: host %s could not be resolved.\n",
|
||||
sourceAddress);
|
||||
exit(1);
|
||||
}
|
||||
ServerInfo si = {
|
||||
.fromHost = bindAddress,
|
||||
.toHost = connectAddress,
|
||||
.serverTimeout = serverTimeout,
|
||||
};
|
||||
|
||||
/* Make a server socket */
|
||||
SOCKET fd = socket(PF_INET,
|
||||
bindProto == protoTcp ? SOCK_STREAM : SOCK_DGRAM,
|
||||
bindProto == protoTcp ? IPPROTO_TCP : IPPROTO_UDP);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
syslog(LOG_ERR, "couldn't create "
|
||||
"server socket! (%m)\n");
|
||||
exit(1);
|
||||
}
|
||||
struct sockaddr_in saddr;
|
||||
saddr.sin_family = AF_INET;
|
||||
memcpy(&saddr.sin_addr, &iaddr, sizeof(iaddr));
|
||||
saddr.sin_port = htons(bindPort);
|
||||
int tmp = 1;
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(const char *) &tmp, sizeof(tmp));
|
||||
if (bind(fd, (struct sockaddr *)
|
||||
&saddr, sizeof(saddr)) == SOCKET_ERROR) {
|
||||
/* Warn -- don't exit. */
|
||||
syslog(LOG_ERR, "couldn't bind to "
|
||||
"address %s port %d (%m)\n",
|
||||
bindAddress, bindPort);
|
||||
closesocket(fd);
|
||||
struct addrinfo hints =
|
||||
{
|
||||
.ai_family = AF_UNSPEC,
|
||||
.ai_protocol = bindProtocol,
|
||||
.ai_socktype = getSocketType(bindProtocol),
|
||||
.ai_flags = AI_PASSIVE,
|
||||
};
|
||||
struct addrinfo *servinfo;
|
||||
int ret = getaddrinfo(bindAddress, bindPort, &hints, &servinfo);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "rinetd: getaddrinfo error: %s\n", gai_strerrorA(ret));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (bindProto == protoTcp) {
|
||||
if (listen(fd, RINETD_LISTEN_BACKLOG) == SOCKET_ERROR) {
|
||||
/* Warn -- don't exit. */
|
||||
syslog(LOG_ERR, "couldn't listen to "
|
||||
"address %s port %d (%m)\n",
|
||||
bindAddress, bindPort);
|
||||
closesocket(fd);
|
||||
for (struct addrinfo *it = servinfo; it; it = it->ai_next) {
|
||||
si.fd = socket(it->ai_family, it->ai_socktype, it->ai_protocol);
|
||||
if (si.fd == INVALID_SOCKET) {
|
||||
syslog(LOG_ERR, "couldn't create server socket! (%m)\n");
|
||||
freeaddrinfo(servinfo);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Make socket nonblocking in TCP mode only, otherwise
|
||||
we may miss some data. */
|
||||
setSocketDefaults(fd);
|
||||
int tmp = 1;
|
||||
setsockopt(si.fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&tmp, sizeof(tmp));
|
||||
|
||||
if (bind(si.fd, it->ai_addr, it->ai_addrlen) == SOCKET_ERROR) {
|
||||
syslog(LOG_ERR, "couldn't bind to address %s port %s (%m)\n",
|
||||
bindAddress, bindPort);
|
||||
closesocket(si.fd);
|
||||
freeaddrinfo(servinfo);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (bindProtocol == IPPROTO_TCP) {
|
||||
if (listen(si.fd, RINETD_LISTEN_BACKLOG) == SOCKET_ERROR) {
|
||||
/* Warn -- don't exit. */
|
||||
syslog(LOG_ERR, "couldn't listen to address %s port %s (%m)\n",
|
||||
bindAddress, bindPort);
|
||||
/* XXX: check whether this is correct */
|
||||
closesocket(si.fd);
|
||||
}
|
||||
|
||||
/* Make socket nonblocking in TCP mode only, otherwise
|
||||
we may miss some data. */
|
||||
setSocketDefaults(si.fd);
|
||||
}
|
||||
|
||||
si.fromAddrInfo = it;
|
||||
break;
|
||||
}
|
||||
|
||||
if (getAddress(connectAddress, &iaddr) < 0) {
|
||||
/* Warn -- don't exit. */
|
||||
syslog(LOG_ERR, "host %s could not be resolved.\n",
|
||||
bindAddress);
|
||||
closesocket(fd);
|
||||
/* Resolve destination address. */
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_protocol = connectProtocol;
|
||||
hints.ai_socktype = getSocketType(connectProtocol);
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
ret = getaddrinfo(connectAddress, connectPort, &hints, &servinfo);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "rinetd: getaddrinfo error: %s\n", gai_strerrorA(ret));
|
||||
freeaddrinfo(si.fromAddrInfo);
|
||||
closesocket(si.fd);
|
||||
exit(1);
|
||||
}
|
||||
si.toAddrInfo = servinfo;
|
||||
|
||||
/* Resolve source address if applicable. */
|
||||
if (sourceAddress) {
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC,
|
||||
hints.ai_protocol = connectProtocol,
|
||||
hints.ai_socktype = getSocketType(connectProtocol),
|
||||
|
||||
ret = getaddrinfo(sourceAddress, NULL, &hints, &servinfo);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "rinetd: getaddrinfo error: %s\n", gai_strerrorA(ret));
|
||||
freeaddrinfo(si.fromAddrInfo);
|
||||
freeaddrinfo(si.toAddrInfo);
|
||||
exit(1);
|
||||
}
|
||||
si.sourceAddrInfo = servinfo;
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
if (si.fd > maxfd) {
|
||||
maxfd = si.fd;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Allocate server info */
|
||||
seInfo = (ServerInfo *)
|
||||
realloc(seInfo, sizeof(ServerInfo) * (seTotal + 1));
|
||||
seInfo = (ServerInfo *)realloc(seInfo, sizeof(ServerInfo) * (seTotal + 1));
|
||||
if (!seInfo) {
|
||||
exit(1);
|
||||
}
|
||||
ServerInfo *srv = &seInfo[seTotal];
|
||||
memset(srv, 0, sizeof(*srv));
|
||||
srv->fd = fd;
|
||||
srv->localAddr = iaddr;
|
||||
srv->localPort = htons(connectPort);
|
||||
srv->fromHost = bindAddress;
|
||||
if (!srv->fromHost) {
|
||||
exit(1);
|
||||
}
|
||||
srv->fromPort = bindPort;
|
||||
srv->fromProto = bindProto;
|
||||
srv->sourceAddr = isourceaddr;
|
||||
srv->toHost = connectAddress;
|
||||
if (!srv->toHost) {
|
||||
exit(1);
|
||||
}
|
||||
srv->toPort = connectPort;
|
||||
srv->toProto = connectProto;
|
||||
srv->serverTimeout = serverTimeout;
|
||||
#ifndef _WIN32
|
||||
if (fd > maxfd) {
|
||||
maxfd = fd;
|
||||
}
|
||||
#endif
|
||||
seInfo[seTotal] = si;
|
||||
++seTotal;
|
||||
}
|
||||
|
||||
@@ -357,7 +376,7 @@ static void setConnectionCount(int newCount)
|
||||
closesocket(coInfo[i].local.fd);
|
||||
}
|
||||
if (coInfo[i].remote.fd != INVALID_SOCKET) {
|
||||
if (coInfo[i].remote.proto == protoTcp)
|
||||
if (coInfo[i].remote.protocol == IPPROTO_TCP)
|
||||
closesocket(coInfo[i].remote.fd);
|
||||
}
|
||||
free(coInfo[i].local.buffer);
|
||||
@@ -439,7 +458,12 @@ static void selectPass(void)
|
||||
timeout.tv_sec = timeout.tv_usec = 0;
|
||||
time_t now = time(NULL);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
fd_set* readfds = (fd_set*)alloca(sizeof(*readfds) * fdSetCount);
|
||||
fd_set* writefds = (fd_set*)alloca(sizeof(*writefds) * fdSetCount);
|
||||
#else
|
||||
fd_set readfds[fdSetCount], writefds[fdSetCount];
|
||||
#endif
|
||||
FD_ZERO_EXT(readfds);
|
||||
FD_ZERO_EXT(writefds);
|
||||
/* Server sockets */
|
||||
@@ -468,7 +492,7 @@ static void selectPass(void)
|
||||
if (cnx->remote.recvPos < RINETD_BUFFER_SIZE) {
|
||||
FD_SET_EXT(cnx->remote.fd, readfds);
|
||||
/* For UDP connections, we need to handle timeouts */
|
||||
if (cnx->remote.proto == protoUdp) {
|
||||
if (cnx->remote.protocol == IPPROTO_UDP) {
|
||||
long delay = (long)(cnx->remoteTimeout - now);
|
||||
timeout.tv_sec = delay <= 1 ? 1
|
||||
: (delay < timeout.tv_sec || timeout.tv_sec == 0) ? delay
|
||||
@@ -488,7 +512,7 @@ static void selectPass(void)
|
||||
if (cnx->remote.fd != INVALID_SOCKET) {
|
||||
/* Do not read on remote UDP sockets, the server does it,
|
||||
but handle timeouts instead. */
|
||||
if (cnx->remote.proto == protoTcp) {
|
||||
if (cnx->remote.protocol == IPPROTO_TCP) {
|
||||
if (FD_ISSET_EXT(cnx->remote.fd, readfds)) {
|
||||
handleRead(cnx, &cnx->remote, &cnx->local);
|
||||
}
|
||||
@@ -530,8 +554,8 @@ static void handleRead(ConnectionInfo *cnx, Socket *socket, Socket *other_socket
|
||||
if (RINETD_BUFFER_SIZE == socket->recvPos) {
|
||||
return;
|
||||
}
|
||||
int got = recv(socket->fd, socket->buffer + socket->recvPos,
|
||||
RINETD_BUFFER_SIZE - socket->recvPos, 0);
|
||||
int wanted = RINETD_BUFFER_SIZE - socket->recvPos;
|
||||
int got = recv(socket->fd, socket->buffer + socket->recvPos, wanted, 0);
|
||||
if (got < 0) {
|
||||
if (GetLastError() == WSAEWOULDBLOCK) {
|
||||
return;
|
||||
@@ -545,7 +569,7 @@ static void handleRead(ConnectionInfo *cnx, Socket *socket, Socket *other_socket
|
||||
handleClose(cnx, socket, other_socket);
|
||||
return;
|
||||
}
|
||||
socket->recvBytes += got;
|
||||
socket->totalBytesIn += got;
|
||||
socket->recvPos += got;
|
||||
}
|
||||
|
||||
@@ -556,7 +580,7 @@ static void handleUdpRead(ConnectionInfo *cnx, char const *buffer, int bytes)
|
||||
? bytes : RINETD_BUFFER_SIZE - socket->recvPos;
|
||||
if (got > 0) {
|
||||
memcpy(socket->buffer + socket->recvPos, buffer, got);
|
||||
socket->recvBytes += got;
|
||||
socket->totalBytesIn += got;
|
||||
socket->recvPos += got;
|
||||
}
|
||||
}
|
||||
@@ -566,7 +590,7 @@ static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
|
||||
if (cnx->coClosing && (socket->sentPos == other_socket->recvPos)) {
|
||||
PERROR("rinetd: local closed and no more output");
|
||||
logEvent(cnx, cnx->server, cnx->coLog);
|
||||
if (socket->proto == protoTcp)
|
||||
if (socket->protocol == IPPROTO_TCP)
|
||||
closesocket(socket->fd);
|
||||
socket->fd = INVALID_SOCKET;
|
||||
return;
|
||||
@@ -574,14 +598,14 @@ static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
|
||||
|
||||
struct sockaddr const *addr = NULL;
|
||||
SOCKLEN_T addrlen = 0;
|
||||
if (socket->proto == protoUdp && socket == &cnx->remote) {
|
||||
if (socket->protocol == IPPROTO_UDP && socket == &cnx->remote) {
|
||||
addr = (struct sockaddr const*)&cnx->remoteAddress;
|
||||
addrlen = (SOCKLEN_T)sizeof(cnx->remoteAddress);
|
||||
}
|
||||
|
||||
int wanted = other_socket->recvPos - socket->sentPos;
|
||||
int got = sendto(socket->fd, other_socket->buffer + socket->sentPos,
|
||||
other_socket->recvPos - socket->sentPos, 0,
|
||||
addr, addrlen);
|
||||
wanted, 0, addr, addrlen);
|
||||
if (got < 0) {
|
||||
if (GetLastError() == WSAEWOULDBLOCK) {
|
||||
return;
|
||||
@@ -593,8 +617,8 @@ static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
|
||||
return;
|
||||
}
|
||||
socket->sentPos += got;
|
||||
socket->sentBytes += got;
|
||||
if (socket->sentPos == other_socket->recvPos) {
|
||||
socket->totalBytesOut += got;
|
||||
if (got == wanted) {
|
||||
socket->sentPos = other_socket->recvPos = 0;
|
||||
}
|
||||
}
|
||||
@@ -602,16 +626,16 @@ static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
|
||||
static void handleClose(ConnectionInfo *cnx, Socket *socket, Socket *other_socket)
|
||||
{
|
||||
cnx->coClosing = 1;
|
||||
if (socket->proto == protoTcp) {
|
||||
if (socket->protocol == IPPROTO_TCP) {
|
||||
/* One end fizzled out, so make sure we're all done with that */
|
||||
closesocket(socket->fd);
|
||||
} else /* if (socket->proto == protoUdp) */ {
|
||||
} else /* if (socket->protocol == IPPROTO_UDP) */ {
|
||||
/* Nothing to do in UDP mode */
|
||||
}
|
||||
socket->fd = INVALID_SOCKET;
|
||||
|
||||
if (other_socket->fd != INVALID_SOCKET) {
|
||||
if (other_socket->proto == protoTcp) {
|
||||
if (other_socket->protocol == IPPROTO_TCP) {
|
||||
#if !defined __linux__ && !defined _WIN32
|
||||
/* Now set up the other end for a polite closing */
|
||||
|
||||
@@ -622,7 +646,7 @@ static void handleClose(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
|
||||
setsockopt(other_socket->fd, SOL_SOCKET, SO_SNDLOWAT,
|
||||
&arg, sizeof(arg));
|
||||
#endif
|
||||
} else /* if (other_socket->proto == protoUdp) */ {
|
||||
} else /* if (other_socket->protocol == IPPROTO_UDP) */ {
|
||||
if (other_socket == &cnx->local)
|
||||
closesocket(other_socket->fd);
|
||||
other_socket->fd = INVALID_SOCKET;
|
||||
@@ -637,28 +661,28 @@ static void handleAccept(ServerInfo const *srv)
|
||||
{
|
||||
int udpBytes = 0;
|
||||
|
||||
struct sockaddr addr;
|
||||
struct sockaddr_storage addr;
|
||||
SOCKLEN_T addrlen = sizeof(addr);
|
||||
|
||||
SOCKET nfd;
|
||||
if (srv->fromProto == protoTcp) {
|
||||
if (srv->fromAddrInfo->ai_protocol == IPPROTO_TCP) {
|
||||
/* In TCP mode, get remote address using accept(). */
|
||||
nfd = accept(srv->fd, &addr, &addrlen);
|
||||
nfd = accept(srv->fd, (struct sockaddr *)&addr, &addrlen);
|
||||
if (nfd == INVALID_SOCKET) {
|
||||
syslog(LOG_ERR, "accept(%d): %m\n", srv->fd);
|
||||
syslog(LOG_ERR, "accept(%llu): %m\n", (long long unsigned int)srv->fd);
|
||||
logEvent(NULL, srv, logAcceptFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
setSocketDefaults(nfd);
|
||||
} else /* if (srv->fromProto == protoUdp) */ {
|
||||
} else /* if (srv->fromAddrInfo->ai_protocol == IPPROTO_UDP) */ {
|
||||
/* In UDP mode, get remote address using recvfrom() and check
|
||||
for an existing connection from this client. We need
|
||||
to read a lot of data otherwise the datagram contents
|
||||
may be lost later. */
|
||||
nfd = srv->fd;
|
||||
ssize_t ret = recvfrom(nfd, globalUdpBuffer,
|
||||
sizeof(globalUdpBuffer), 0, &addr, &addrlen);
|
||||
SSIZE_T ret = recvfrom(nfd, globalUdpBuffer,
|
||||
sizeof(globalUdpBuffer), 0, (struct sockaddr *)&addr, &addrlen);
|
||||
if (ret < 0) {
|
||||
if (GetLastError() == WSAEWOULDBLOCK) {
|
||||
return;
|
||||
@@ -666,20 +690,17 @@ static void handleAccept(ServerInfo const *srv)
|
||||
if (GetLastError() == WSAEINPROGRESS) {
|
||||
return;
|
||||
}
|
||||
syslog(LOG_ERR, "recvfrom(%d): %m\n", srv->fd);
|
||||
syslog(LOG_ERR, "recvfrom(%llu): %m\n", (long long unsigned int)srv->fd);
|
||||
logEvent(NULL, srv, logAcceptFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
udpBytes = (int)ret;
|
||||
|
||||
/* Look for an existing connection with the same remote host and port. */
|
||||
for (int i = 0; i < coTotal; ++i) {
|
||||
ConnectionInfo *cnx = &coInfo[i];
|
||||
struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
|
||||
if (cnx->remote.fd == nfd
|
||||
&& cnx->remoteAddress.sin_family == addr_in->sin_family
|
||||
&& cnx->remoteAddress.sin_port == addr_in->sin_port
|
||||
&& cnx->remoteAddress.sin_addr.s_addr == addr_in->sin_addr.s_addr) {
|
||||
if (cnx->remote.fd == nfd && sameSocketAddress(&cnx->remoteAddress, &addr)) {
|
||||
cnx->remoteTimeout = time(NULL) + srv->serverTimeout;
|
||||
handleUdpRead(cnx, globalUdpBuffer, udpBytes);
|
||||
return;
|
||||
@@ -693,16 +714,18 @@ static void handleAccept(ServerInfo const *srv)
|
||||
}
|
||||
|
||||
cnx->local.fd = INVALID_SOCKET;
|
||||
cnx->local.proto = srv->toProto;
|
||||
cnx->local.family = srv->toAddrInfo->ai_family;
|
||||
cnx->local.protocol = srv->toAddrInfo->ai_protocol;
|
||||
cnx->local.recvPos = cnx->local.sentPos = 0;
|
||||
cnx->local.recvBytes = cnx->local.sentBytes = 0;
|
||||
cnx->local.totalBytesIn = cnx->local.totalBytesOut = 0;
|
||||
|
||||
cnx->remote.fd = nfd;
|
||||
cnx->remote.proto = srv->fromProto;
|
||||
cnx->remote.family = srv->fromAddrInfo->ai_family;
|
||||
cnx->remote.protocol = srv->fromAddrInfo->ai_protocol;
|
||||
cnx->remote.recvPos = cnx->remote.sentPos = 0;
|
||||
cnx->remote.recvBytes = cnx->remote.sentBytes = 0;
|
||||
cnx->remoteAddress = *(struct sockaddr_in *)&addr;
|
||||
if (srv->fromProto == protoUdp)
|
||||
cnx->remote.totalBytesIn = cnx->remote.totalBytesOut = 0;
|
||||
cnx->remoteAddress = addr;
|
||||
if (srv->fromAddrInfo->ai_protocol == IPPROTO_UDP)
|
||||
cnx->remoteTimeout = time(NULL) + srv->serverTimeout;
|
||||
|
||||
cnx->coClosing = 0;
|
||||
@@ -713,7 +736,7 @@ static void handleAccept(ServerInfo const *srv)
|
||||
if (logCode != logAllowed) {
|
||||
/* Local fd is not open yet, so only
|
||||
close the remote socket. */
|
||||
if (cnx->remote.proto == protoTcp)
|
||||
if (cnx->remote.protocol == IPPROTO_TCP)
|
||||
closesocket(cnx->remote.fd);
|
||||
cnx->remote.fd = INVALID_SOCKET;
|
||||
logEvent(cnx, cnx->server, logCode);
|
||||
@@ -723,46 +746,38 @@ static void handleAccept(ServerInfo const *srv)
|
||||
/* Now open a connection to the local server.
|
||||
This, too, is nonblocking. Why wait
|
||||
for anything when you don't have to? */
|
||||
struct sockaddr_in saddr;
|
||||
cnx->local.fd = srv->toProto == protoTcp
|
||||
? socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
|
||||
: socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
struct addrinfo* to = srv->toAddrInfo;
|
||||
cnx->local.fd = socket(to->ai_family, to->ai_socktype, to->ai_protocol);
|
||||
if (cnx->local.fd == INVALID_SOCKET) {
|
||||
syslog(LOG_ERR, "socket(): %m\n");
|
||||
if (cnx->remote.proto == protoTcp)
|
||||
if (cnx->remote.protocol == IPPROTO_TCP)
|
||||
closesocket(cnx->remote.fd);
|
||||
cnx->remote.fd = INVALID_SOCKET;
|
||||
logEvent(cnx, srv, logLocalSocketFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
if (srv->toProto == protoTcp)
|
||||
if (srv->toAddrInfo->ai_protocol == IPPROTO_TCP)
|
||||
setSocketDefaults(cnx->local.fd);
|
||||
|
||||
/* Bind the local socket even if we use connect() later, so that
|
||||
/* Bind the local socket even though we use connect() later, so that
|
||||
we can specify a source address. */
|
||||
memset(&saddr, 0, sizeof(struct sockaddr_in));
|
||||
saddr.sin_family = AF_INET;
|
||||
memcpy(&saddr.sin_addr, &srv->sourceAddr, sizeof(struct in_addr));
|
||||
saddr.sin_port = 0;
|
||||
if (bind(cnx->local.fd, (struct sockaddr *)&saddr,
|
||||
sizeof(saddr)) == SOCKET_ERROR) {
|
||||
syslog(LOG_ERR, "bind(): %m\n");
|
||||
if (srv->sourceAddrInfo) {
|
||||
if (bind(cnx->local.fd, srv->sourceAddrInfo->ai_addr,
|
||||
srv->sourceAddrInfo->ai_addrlen) == SOCKET_ERROR) {
|
||||
syslog(LOG_ERR, "bind(): %m\n");
|
||||
}
|
||||
}
|
||||
|
||||
memset(&saddr, 0, sizeof(struct sockaddr_in));
|
||||
saddr.sin_family = AF_INET;
|
||||
memcpy(&saddr.sin_addr, &srv->localAddr, sizeof(struct in_addr));
|
||||
saddr.sin_port = srv->localPort;
|
||||
if (connect(cnx->local.fd, (struct sockaddr *)&saddr,
|
||||
sizeof(struct sockaddr_in)) == SOCKET_ERROR)
|
||||
if (connect(cnx->local.fd, srv->toAddrInfo->ai_addr,
|
||||
srv->toAddrInfo->ai_addrlen) == SOCKET_ERROR)
|
||||
{
|
||||
if ((GetLastError() != WSAEINPROGRESS) &&
|
||||
(GetLastError() != WSAEWOULDBLOCK))
|
||||
{
|
||||
PERROR("rinetd: connect");
|
||||
closesocket(cnx->local.fd);
|
||||
if (cnx->remote.proto == protoTcp)
|
||||
if (cnx->remote.protocol == IPPROTO_TCP)
|
||||
closesocket(cnx->remote.fd);
|
||||
cnx->remote.fd = INVALID_SOCKET;
|
||||
cnx->local.fd = INVALID_SOCKET;
|
||||
@@ -772,15 +787,15 @@ static void handleAccept(ServerInfo const *srv)
|
||||
}
|
||||
|
||||
/* Send a zero-size UDP packet to simulate a connection */
|
||||
if (srv->toProto == protoUdp) {
|
||||
if (srv->toAddrInfo->ai_protocol == IPPROTO_UDP) {
|
||||
int got = sendto(cnx->local.fd, NULL, 0, 0,
|
||||
&saddr, (SOCKLEN_T)sizeof(saddr));
|
||||
srv->toAddrInfo->ai_addr, srv->toAddrInfo->ai_addrlen);
|
||||
/* FIXME: we ignore errors here... is it safe? */
|
||||
(void)got;
|
||||
}
|
||||
|
||||
/* Send UDP data to the other socket */
|
||||
if (srv->fromProto == protoUdp) {
|
||||
if (srv->fromAddrInfo->ai_protocol == IPPROTO_UDP) {
|
||||
handleUdpRead(cnx, globalUdpBuffer, udpBytes);
|
||||
}
|
||||
|
||||
@@ -799,7 +814,10 @@ static void handleAccept(ServerInfo const *srv)
|
||||
static int checkConnectionAllowed(ConnectionInfo const *cnx)
|
||||
{
|
||||
ServerInfo const *srv = cnx->server;
|
||||
char const *addressText = inet_ntoa(cnx->remoteAddress.sin_addr);
|
||||
|
||||
char addressText[NI_MAXHOST];
|
||||
getnameinfo((struct sockaddr *)&cnx->remoteAddress, sizeof(cnx->remoteAddress),
|
||||
addressText, sizeof(addressText), NULL, 0, NI_NUMERICHOST);
|
||||
|
||||
/* 1. Check global allow rules. If there are no
|
||||
global allow rules, it's presumed OK at
|
||||
@@ -855,56 +873,6 @@ static int checkConnectionAllowed(ConnectionInfo const *cnx)
|
||||
return logAllowed;
|
||||
}
|
||||
|
||||
static int getAddress(char const *host, struct in_addr *iaddr)
|
||||
{
|
||||
/* If this is an IP address, use inet_addr() */
|
||||
int is_ipaddr = 1;
|
||||
for (char const *p = host; *p; ++p) {
|
||||
if (!isdigit(*p) && *p != '.') {
|
||||
is_ipaddr = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_ipaddr) {
|
||||
iaddr->s_addr = inet_addr(host);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Otherwise, use gethostbyname() */
|
||||
struct hostent *h = gethostbyname(host);
|
||||
if (h) {
|
||||
#ifdef h_addr
|
||||
memcpy(&iaddr->s_addr, h->h_addr, 4);
|
||||
#else
|
||||
memcpy(&iaddr->s_addr, h->h_addr_list[0], 4);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
char const *msg = "(unknown DNS error)";
|
||||
switch (h_errno)
|
||||
{
|
||||
case HOST_NOT_FOUND:
|
||||
msg = "The specified host is unknown.";
|
||||
break;
|
||||
#ifdef NO_DATA
|
||||
case NO_DATA:
|
||||
#else
|
||||
case NO_ADDRESS:
|
||||
#endif
|
||||
msg = "The requested name is valid but does not have an IP address.";
|
||||
break;
|
||||
case NO_RECOVERY:
|
||||
msg = "A non-recoverable name server error occurred.";
|
||||
break;
|
||||
case TRY_AGAIN:
|
||||
msg = "A temporary error occurred on an authoritative name server. Try again later.";
|
||||
break;
|
||||
}
|
||||
syslog(LOG_ERR, "While resolving `%s' got: %s\n", host, msg);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if !HAVE_SIGACTION && !_WIN32
|
||||
RETSIGTYPE plumber(int s)
|
||||
{
|
||||
@@ -972,6 +940,7 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
|
||||
thanks folks */
|
||||
int timz;
|
||||
char tstr[1024];
|
||||
char addressText[NI_MAXHOST] = { '?' };
|
||||
struct tm *t = get_gmtoff(&timz);
|
||||
char sign = (timz < 0 ? '-' : '+');
|
||||
if (timz < 0) {
|
||||
@@ -979,24 +948,21 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
|
||||
}
|
||||
strftime(tstr, sizeof(tstr), "%d/%b/%Y:%H:%M:%S ", t);
|
||||
|
||||
char const *addressText = "?";
|
||||
int bytesOutput = 0;
|
||||
int bytesInput = 0;
|
||||
int64_t bytesOut = 0, bytesIn = 0;
|
||||
if (cnx != NULL) {
|
||||
addressText = inet_ntoa(cnx->remoteAddress.sin_addr);
|
||||
bytesOutput = cnx->remote.sentBytes;
|
||||
bytesInput = cnx->remote.recvBytes;
|
||||
getnameinfo((struct sockaddr *)&cnx->remoteAddress, sizeof(cnx->remoteAddress),
|
||||
addressText, sizeof(addressText), NULL, 0, NI_NUMERICHOST);
|
||||
bytesOut = cnx->remote.totalBytesOut;
|
||||
bytesIn = cnx->remote.totalBytesIn;
|
||||
}
|
||||
|
||||
char const *fromHost = "?";
|
||||
int fromPort = 0;
|
||||
char const *toHost = "?";
|
||||
int toPort = 0;
|
||||
char const *fromHost = "?", *toHost = "?";
|
||||
uint16_t fromPort = 0, toPort = 0;
|
||||
if (srv != NULL) {
|
||||
fromHost = srv->fromHost;
|
||||
fromPort = srv->fromPort;
|
||||
fromPort = getPort(srv->fromAddrInfo);
|
||||
toHost = srv->toHost;
|
||||
toPort = srv->toPort;
|
||||
toPort = getPort(srv->toAddrInfo);
|
||||
}
|
||||
|
||||
if (result==logNotAllowed || result==logDenied)
|
||||
@@ -1020,28 +986,28 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
|
||||
fprintf(logFile, "%s - - "
|
||||
"[%s %c%.2d%.2d] "
|
||||
"\"GET /rinetd-services/%s/%d/%s/%d/%s HTTP/1.0\" "
|
||||
"200 %d - - - %d\n",
|
||||
"200 %llu - - - %llu\n",
|
||||
addressText,
|
||||
tstr,
|
||||
sign,
|
||||
timz / 60,
|
||||
timz % 60,
|
||||
fromHost, fromPort,
|
||||
toHost, toPort,
|
||||
fromHost, (int)fromPort,
|
||||
toHost, (int)toPort,
|
||||
logMessages[result],
|
||||
bytesOutput,
|
||||
bytesInput);
|
||||
(unsigned long long int)bytesOut,
|
||||
(unsigned long long int)bytesIn);
|
||||
} else {
|
||||
/* Write an rinetd-specific log entry with a
|
||||
less goofy format. */
|
||||
fprintf(logFile, "%s\t%s\t%s\t%d\t%s\t%d\t%d"
|
||||
"\t%d\t%s\n",
|
||||
fprintf(logFile, "%s\t%s\t%s\t%d\t%s\t%d\t%llu"
|
||||
"\t%llu\t%s\n",
|
||||
tstr,
|
||||
addressText,
|
||||
fromHost, fromPort,
|
||||
toHost, toPort,
|
||||
bytesInput,
|
||||
bytesOutput,
|
||||
fromHost, (int)fromPort,
|
||||
toHost, (int)toPort,
|
||||
(unsigned long long int)bytesIn,
|
||||
(unsigned long long int)bytesOut,
|
||||
logMessages[result]);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
|
||||
and Boutell.Com, Inc.
|
||||
© 2003—2017 Sam Hocevar <sam@hocevar.net>
|
||||
© 2003—2021 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
This software is released for free use under the terms of
|
||||
the GNU Public License, version 2 or higher. NO WARRANTY
|
||||
@@ -19,6 +19,8 @@
|
||||
# include <syslog.h>
|
||||
#endif /* _WIN32 */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Constants */
|
||||
|
||||
static int const RINETD_BUFFER_SIZE = 16384;
|
||||
@@ -44,7 +46,6 @@ extern FILE *logFile;
|
||||
|
||||
/* Functions */
|
||||
|
||||
void addServer(char *bindAddress, int bindPort, int bindProto,
|
||||
char *connectAddress, int connectPort, int connectProto,
|
||||
void addServer(char *bindAddress, char *bindPort, int bindProtocol,
|
||||
char *connectAddress, char *connectPort, int connectProtocol,
|
||||
int serverTimeout, char *sourceAddress);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
|
||||
and Boutell.Com, Inc.
|
||||
© 2003—2017 Sam Hocevar <sam@hocevar.net>
|
||||
© 2003—2021 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
This software is released for free use under the terms of
|
||||
the GNU Public License, version 2 or higher. NO WARRANTY
|
||||
@@ -9,22 +9,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum ruleType {
|
||||
typedef enum _rule_type ruleType;
|
||||
enum _rule_type {
|
||||
allowRule,
|
||||
denyRule,
|
||||
};
|
||||
|
||||
enum protocolType {
|
||||
protoTcp = 1,
|
||||
protoUdp = 2,
|
||||
};
|
||||
|
||||
typedef struct _rule Rule;
|
||||
struct _rule
|
||||
{
|
||||
char *pattern;
|
||||
int type;
|
||||
ruleType type;
|
||||
};
|
||||
|
||||
typedef struct _server_info ServerInfo;
|
||||
@@ -32,13 +29,10 @@ struct _server_info {
|
||||
SOCKET fd;
|
||||
|
||||
/* In network order, for network purposes */
|
||||
struct in_addr localAddr;
|
||||
unsigned short localPort;
|
||||
struct in_addr sourceAddr;
|
||||
struct addrinfo *fromAddrInfo, *toAddrInfo, *sourceAddrInfo;
|
||||
|
||||
/* In ASCII and local byte order, for logging purposes */
|
||||
/* In ASCII, for logging purposes */
|
||||
char *fromHost, *toHost;
|
||||
int fromPort, fromProto, toPort, toProto;
|
||||
|
||||
/* Offset and count into list of allow and deny rules. Any rules
|
||||
prior to globalAllowRules and globalDenyRules are global rules. */
|
||||
@@ -52,11 +46,11 @@ typedef struct _socket Socket;
|
||||
struct _socket
|
||||
{
|
||||
SOCKET fd;
|
||||
int proto;
|
||||
int family, protocol;
|
||||
/* recv: received on this socket
|
||||
sent: sent to this socket from the other buffer */
|
||||
sent: sent through this socket from the other buffer */
|
||||
int recvPos, sentPos;
|
||||
int recvBytes, sentBytes;
|
||||
uint64_t totalBytesIn, totalBytesOut;
|
||||
char *buffer;
|
||||
};
|
||||
|
||||
@@ -64,7 +58,7 @@ typedef struct _connection_info ConnectionInfo;
|
||||
struct _connection_info
|
||||
{
|
||||
Socket remote, local;
|
||||
struct sockaddr_in remoteAddress;
|
||||
struct sockaddr_storage remoteAddress;
|
||||
time_t remoteTimeout;
|
||||
int coClosing;
|
||||
int coLog;
|
||||
Reference in New Issue
Block a user